src/TypeManager.cpp
author David Anderson <danderson@mozilla.com>
Wed Jul 25 00:48:08 2012 -0700 (2012-07-25)
changeset 91 5f4402180204
parent 86 32ad0523b309
child 92 e3974e7360e9
permissions -rw-r--r--
Added clang support. A lot of the motion here is introducing a new offsetof because clang's seems broken?
     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 <strings.h>
    20 #include "Types.h"
    21 #include "TypeManager.h"
    22 #include "Zone.h"
    23 #include "Strings.h"
    24 
    25 using namespace ke;
    26 
    27 
    28 TypeManager::TypeManager(Zone *zone)
    29   : zone_(zone)
    30 {
    31 }
    32 
    33 bool
    34 TypeManager::initialize()
    35 {
    36     voidType_ = Type::NewVoid(zone_);
    37     if (!voidType_)
    38         return false;
    39 
    40     Local<String> floatTag(zone_, zone_->makeSymbol("Float"));
    41     if (!floatTag)
    42         return false;
    43 
    44     Local<String> stringTag(zone_, zone_->makeSymbol("String"));
    45     if (!stringTag)
    46         return false;
    47 
    48     Local<String> nullTag(zone_);
    49 
    50     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, nullTag)) == NULL)
    51         return false;
    52     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, floatTag)) == NULL)
    53         return false;
    54     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    55         return false;
    56     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, stringTag)) == 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] = Type::NewReference(zone_, type, false)) == NULL)
    63             return false;
    64     }
    65 
    66     return true;
    67 }
    68 
    69 Type *
    70 TypeManager::getPrimitiveForTag(Handle<String> tag)
    71 {
    72     if (!tag)
    73         return getPrimitive(PrimitiveType_Int32);
    74     if (strcmp(tag->chars(), "Float") == 0)
    75         return getPrimitive(PrimitiveType_Float);
    76     if (strcmp(tag->chars(), "String") == 0)
    77         return getPrimitive(PrimitiveType_Char);
    78     return NULL;
    79 }
    80 
    81 Type *
    82 TypeManager::unsafeArray(Handle<Type> contained, int elements, bool isConst)
    83 {
    84     // :TODO: cache this.
    85     return Type::NewArray(zone_, contained, elements, isConst);
    86 }
    87 
    88 Type *
    89 TypeManager::getReference(Handle<Type> type, bool isConst)
    90 {
    91     if (!isConst && (type->isFloat() || (type->isInt32() && !type->tag())))
    92         return referenceTypes_[type->primitive()];
    93 
    94     // :TODO: cache this.
    95     return Type::NewReference(zone_, type, isConst);
    96 }
    97 
    98 Type *
    99 TypeManager::newFunction(Handle<Type> returnType, Handle<FixedArray> args)
   100 {
   101     // :TODO: cache this.
   102     return Type::NewFunction(zone_, returnType, args);
   103 }
   104 
   105 Type *
   106 TypeManager::newNative(Handle<Type> returnType, Handle<FixedArray> args, bool variadic)
   107 {
   108     // :TODO: cache this.
   109     return Type::NewNative(zone_, returnType, args, variadic);
   110 }
   111 
   112 void
   113 TypeManager::accept(VirtualObjectVisitor *visitor)
   114 {
   115     visitor->visit(voidType_.address());
   116     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   117         visitor->visit(primitiveTypes_[i].address());
   118         visitor->visit(referenceTypes_[i].address());
   119     }
   120 }