jitcraft/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jul 07 21:30:23 2012 -0700 (2012-07-07)
changeset 59 b7d61ddb3c8d
parent 54 8e22792a3921
child 67 b765a6383849
permissions -rw-r--r--
Rename jc:: to ke::
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include "Types.h"
    20 #include "TypeManager.h"
    21 #include "Zone.h"
    22 #include "Strings.h"
    23 
    24 using namespace ke;
    25 
    26 
    27 TypeManager::TypeManager(Zone *zone)
    28   : zone_(zone)
    29 {
    30 }
    31 
    32 bool
    33 TypeManager::initialize()
    34 {
    35     Local<String> floatTag(zone_, zone_->makeSymbol("Float"));
    36     if (!floatTag)
    37         return false;
    38 
    39     Local<String> stringTag(zone_, zone_->makeSymbol("String"));
    40     if (!stringTag)
    41         return false;
    42 
    43     Local<String> nullTag(zone_);
    44 
    45     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, nullTag)) == NULL)
    46         return false;
    47     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, floatTag)) == NULL)
    48         return false;
    49     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    50         return false;
    51     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, stringTag)) == NULL)
    52         return false;
    53 
    54     Local<Type> type(zone_);
    55     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    56         type = primitiveTypes_[i];
    57         if ((referenceTypes_[i] = Type::NewReference(zone_, type)) == NULL)
    58             return false;
    59     }
    60 
    61     return true;
    62 }
    63 
    64 Type *
    65 TypeManager::getPrimitiveForTag(Handle<String> tag)
    66 {
    67     if (!tag)
    68         return getPrimitive(PrimitiveType_Int32);
    69     if (strcmp(tag->chars(), "Float") == 0)
    70         return getPrimitive(PrimitiveType_Float);
    71     if (strcmp(tag->chars(), "String") == 0)
    72         return getPrimitive(PrimitiveType_Char);
    73     return NULL;
    74 }
    75 
    76 Type *
    77 TypeManager::unsafeArray(Handle<Type> contained, int elements)
    78 {
    79     // :TODO: cache this.
    80     return Type::NewArray(zone_, contained, elements, false);
    81 }
    82 
    83 Type *
    84 TypeManager::getReference(Handle<Type> type)
    85 {
    86     if (type->isFloat() || (type->isInt32() && !type->tag()))
    87         return referenceTypes_[type->primitive()];
    88 
    89     // :TODO: cache this.
    90     return Type::NewReference(zone_, type);
    91 }
    92 
    93 Type *
    94 TypeManager::newFunction(Handle<Type> returnType, Handle<FixedArray> args)
    95 {
    96     // :TODO: cache this.
    97     return Type::NewFunction(zone_, returnType, args);
    98 }
    99 
   100 void
   101 TypeManager::accept(VirtualObjectVisitor *visitor)
   102 {
   103     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   104         visitor->visit(primitiveTypes_[i].address());
   105         visitor->visit(referenceTypes_[i].address());
   106     }
   107 }
   108