jitcraft/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 22:31:14 2012 -0700 (2012-06-30)
changeset 14 0784ac5cc347
parent 12 75e385f68b7e
child 20 e020a14e21be
permissions -rw-r--r--
Added support for non-array references.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include "Types.h"
    19 #include "TypeManager.h"
    20 #include "Zone.h"
    21 
    22 using namespace jc;
    23 
    24 
    25 TypeManager::TypeManager(Zone *zone)
    26   : zone_(zone)
    27 {
    28 }
    29 
    30 bool
    31 TypeManager::initialize()
    32 {
    33     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, NULL)) == NULL)
    34         return false;
    35     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, "Float")) == NULL)
    36         return false;
    37     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    38         return false;
    39 
    40     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    41         if ((referenceTypes_[i] = Type::NewReference(zone_, primitiveTypes_[i])) == NULL)
    42             return false;
    43     }
    44 
    45     return true;
    46 }
    47 
    48 Type *
    49 TypeManager::getPrimitiveForTag(Name *tag)
    50 {
    51     if (tag == NULL)
    52         return getPrimitive(PrimitiveType_Int32);
    53     if (strcmp(tag, "Float") == 0)
    54         return getPrimitive(PrimitiveType_Float);
    55     assert(!"tag cache");
    56     return NULL;
    57 }
    58 
    59 Type *
    60 TypeManager::unsafeArray(Type *contained, int elements)
    61 {
    62     // :TODO: cache this.
    63     return Type::NewUnsafeArray(zone_, contained, elements);
    64 }
    65 
    66 Type *
    67 TypeManager::getReference(Type *type)
    68 {
    69     if (type->isFloat() || (type->isInt() && !type->tag()))
    70         return referenceTypes_[type->primitive()];
    71 
    72     // :TODO: cache this.
    73     return Type::NewReference(zone_, type);
    74 }
    75 
    76 Type *
    77 TypeManager::newFunction(Type *returnType, FixedArray *args)
    78 {
    79     // :TODO: cache this.
    80     return Type::NewFunction(zone_, returnType, args);
    81 }