src/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Tue Jul 24 23:53:34 2012 -0700 (2012-07-24)
changeset 86 32ad0523b309
parent 72 jitcraft/TypeManager.cpp@40746ab0a30b
child 91 5f4402180204
permissions -rw-r--r--
Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
     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     voidType_ = Type::NewVoid(zone_);
    36     if (!voidType_)
    37         return false;
    38 
    39     Local<String> floatTag(zone_, zone_->makeSymbol("Float"));
    40     if (!floatTag)
    41         return false;
    42 
    43     Local<String> stringTag(zone_, zone_->makeSymbol("String"));
    44     if (!stringTag)
    45         return false;
    46 
    47     Local<String> nullTag(zone_);
    48 
    49     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, nullTag)) == NULL)
    50         return false;
    51     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, floatTag)) == NULL)
    52         return false;
    53     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    54         return false;
    55     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, stringTag)) == NULL)
    56         return false;
    57 
    58     Local<Type> type(zone_);
    59     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    60         type = primitiveTypes_[i];
    61         if ((referenceTypes_[i] = Type::NewReference(zone_, type, false)) == NULL)
    62             return false;
    63     }
    64 
    65     return true;
    66 }
    67 
    68 Type *
    69 TypeManager::getPrimitiveForTag(Handle<String> tag)
    70 {
    71     if (!tag)
    72         return getPrimitive(PrimitiveType_Int32);
    73     if (strcmp(tag->chars(), "Float") == 0)
    74         return getPrimitive(PrimitiveType_Float);
    75     if (strcmp(tag->chars(), "String") == 0)
    76         return getPrimitive(PrimitiveType_Char);
    77     return NULL;
    78 }
    79 
    80 Type *
    81 TypeManager::unsafeArray(Handle<Type> contained, int elements, bool isConst)
    82 {
    83     // :TODO: cache this.
    84     return Type::NewArray(zone_, contained, elements, isConst);
    85 }
    86 
    87 Type *
    88 TypeManager::getReference(Handle<Type> type, bool isConst)
    89 {
    90     if (!isConst && (type->isFloat() || (type->isInt32() && !type->tag())))
    91         return referenceTypes_[type->primitive()];
    92 
    93     // :TODO: cache this.
    94     return Type::NewReference(zone_, type, isConst);
    95 }
    96 
    97 Type *
    98 TypeManager::newFunction(Handle<Type> returnType, Handle<FixedArray> args)
    99 {
   100     // :TODO: cache this.
   101     return Type::NewFunction(zone_, returnType, args);
   102 }
   103 
   104 Type *
   105 TypeManager::newNative(Handle<Type> returnType, Handle<FixedArray> args, bool variadic)
   106 {
   107     // :TODO: cache this.
   108     return Type::NewNative(zone_, returnType, args, variadic);
   109 }
   110 
   111 void
   112 TypeManager::accept(VirtualObjectVisitor *visitor)
   113 {
   114     visitor->visit(voidType_.address());
   115     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   116         visitor->visit(primitiveTypes_[i].address());
   117         visitor->visit(referenceTypes_[i].address());
   118     }
   119 }