src/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Mon Dec 24 02:49:20 2012 -0800 (2012-12-24)
changeset 234 26e3035cdffc
parent 233 bbb498ccb4ac
child 235 43287eef2fff
permissions -rw-r--r--
Get string literals working again.
     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 <string.h>
    20 #include "Types.h"
    21 #include "TypeManager.h"
    22 #include "Zone.h"
    23 #include "Strings.h"
    24 #include "Spaces-inl.h"
    25 #include "Heap-inl.h"
    26 
    27 using namespace ke;
    28 
    29 
    30 TypeManager::TypeManager(Zone *zone)
    31   : zone_(zone)
    32 {
    33 }
    34 
    35 bool
    36 TypeManager::initialize()
    37 {
    38     voidType_ = Type::NewVoid(zone_);
    39     if (!voidType_)
    40         return false;
    41 
    42     uncheckedType_ = Type::NewUnchecked(zone_);
    43     if (!uncheckedType_)
    44         return false;
    45 
    46     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, PrimitiveType_Int32)) == NULL)
    47         return false;
    48     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, PrimitiveType_Float)) == NULL)
    49         return false;
    50     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    51         return false;
    52     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, PrimitiveType_Char)) == NULL)
    53         return false;
    54     if ((primitiveTypes_[PrimitiveType_Bool] = Type::NewPrimitive(zone_, PrimitiveType_Bool)) == NULL)
    55         return false;
    56     if ((primitiveTypes_[PrimitiveType_String] = Type::NewPrimitive(zone_, PrimitiveType_String)) == NULL)
    57         return false;
    58 
    59     Local<Type> type(zone_);
    60     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    61         type = primitiveTypes_[i];
    62         if ((referenceTypes_[i] = ReferenceType::New(zone_, type)) == NULL)
    63             return false;
    64     }
    65 
    66     return true;
    67 }
    68 
    69 ArrayType *
    70 TypeManager::newArray(Handle<Type> contained, int elements)
    71 {
    72     // :TODO: cache this.
    73     return ArrayType::New(zone_, contained, elements);
    74 }
    75 
    76 ArrayType *
    77 TypeManager::newExternalArray(Handle<Type> contained)
    78 {
    79     return ArrayType::NewExternal(zone_, contained);
    80 }
    81 
    82 ReferenceType *
    83 TypeManager::getReference(Handle<Type> type, bool isConst)
    84 {
    85     if (!isConst && type->isPrimitive())
    86         return referenceTypes_[type->primitive()];
    87 
    88     // :TODO: cache this.
    89     return ReferenceType::New(zone_, type);
    90 }
    91 
    92 EnumType *
    93 TypeManager::newEnum(Handle<String> name)
    94 {
    95     return EnumType::New(zone_, name);
    96 }
    97 
    98 void
    99 TypeManager::accept(VirtualObjectVisitor *visitor)
   100 {
   101     visitor->visit(voidType_.address());
   102     visitor->visit(uncheckedType_.address());
   103     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   104         visitor->visit(primitiveTypes_[i].address());
   105         visitor->visit(referenceTypes_[i].address());
   106     }
   107 }