jitcraft/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jul 01 18:21:15 2012 -0700 (2012-07-01)
changeset 23 8a31742781c1
parent 21 af5d8e985502
child 41 34987baade41
permissions -rw-r--r--
Implement ++ and -- operators.
     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     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, "String")) == NULL)
    40         return false;
    41 
    42     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    43         if ((referenceTypes_[i] = Type::NewReference(zone_, primitiveTypes_[i])) == NULL)
    44             return false;
    45     }
    46 
    47     return true;
    48 }
    49 
    50 Type *
    51 TypeManager::getPrimitiveForTag(Name *tag)
    52 {
    53     if (tag == NULL)
    54         return getPrimitive(PrimitiveType_Int32);
    55     if (strcmp(tag, "Float") == 0)
    56         return getPrimitive(PrimitiveType_Float);
    57     if (strcmp(tag, "String") == 0)
    58         return getPrimitive(PrimitiveType_Char);
    59     return NULL;
    60 }
    61 
    62 Type *
    63 TypeManager::unsafeArray(Type *contained, int elements)
    64 {
    65     // :TODO: cache this.
    66     return Type::NewArray(zone_, contained, elements, false);
    67 }
    68 
    69 Type *
    70 TypeManager::getReference(Type *type)
    71 {
    72     if (type->isFloat() || (type->isInt32() && !type->tag()))
    73         return referenceTypes_[type->primitive()];
    74 
    75     // :TODO: cache this.
    76     return Type::NewReference(zone_, type);
    77 }
    78 
    79 Type *
    80 TypeManager::newFunction(Type *returnType, FixedArray *args)
    81 {
    82     // :TODO: cache this.
    83     return Type::NewFunction(zone_, returnType, args);
    84 }