src/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jul 30 22:24:57 2012 -0700 (2012-07-30)
changeset 143 393f1b8ac8c3
parent 142 a000bafece09
child 145 e9d804821c48
permissions -rw-r--r--
Initial support for booleans. Coercion does not work yet.
     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     Local<String> floatTag(zone_, zone_->makeSymbol("Float"));
    43     if (!floatTag)
    44         return false;
    45 
    46     Local<String> stringTag(zone_, zone_->makeSymbol("String"));
    47     if (!stringTag)
    48         return false;
    49 
    50     Local<String> boolTag(zone_, zone_->makeSymbol("bool"));
    51     if (!boolTag)
    52         return false;
    53 
    54     Local<String> nullTag(zone_);
    55 
    56     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, nullTag)) == NULL)
    57         return false;
    58     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, floatTag)) == NULL)
    59         return false;
    60     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    61         return false;
    62     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, stringTag)) == NULL)
    63         return false;
    64     if ((primitiveTypes_[PrimitiveType_Bool] = Type::NewPrimitive(zone_, boolTag)) == NULL)
    65         return false;
    66 
    67     Local<Type> type(zone_);
    68     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    69         type = primitiveTypes_[i];
    70         if ((referenceTypes_[i] = Type::NewReference(zone_, type, false)) == NULL)
    71             return false;
    72     }
    73 
    74     return true;
    75 }
    76 
    77 Type *
    78 TypeManager::getPrimitiveForTag(Handle<String> tag)
    79 {
    80     if (!tag || strcmp(tag->chars(), "_") == 0)
    81         return getPrimitive(PrimitiveType_Int32);
    82     if (strcmp(tag->chars(), "Float") == 0)
    83         return getPrimitive(PrimitiveType_Float);
    84     if (strcmp(tag->chars(), "String") == 0)
    85         return getPrimitive(PrimitiveType_Char);
    86 
    87     // :TODO: cache this.
    88     return Type::NewPrimitive(zone_, tag);
    89 }
    90 
    91 Type *
    92 TypeManager::unsafeArray(Handle<Type> contained, int elements, bool isConst)
    93 {
    94     // :TODO: cache this.
    95     return Type::NewArray(zone_, contained, elements, isConst);
    96 }
    97 
    98 Type *
    99 TypeManager::getReference(Handle<Type> type, bool isConst)
   100 {
   101     if (!isConst && (type->isFloat() || (type->isInt32() && !type->tag())))
   102         return referenceTypes_[type->primitive()];
   103 
   104     // :TODO: cache this.
   105     return Type::NewReference(zone_, type, isConst);
   106 }
   107 
   108 Type *
   109 TypeManager::newFunction(Handle<Type> returnType, Handle<FixedArray> args)
   110 {
   111     // :TODO: cache this.
   112     return Type::NewFunction(zone_, returnType, args);
   113 }
   114 
   115 Type *
   116 TypeManager::newNative(Handle<Type> returnType, Handle<FixedArray> args, bool variadic)
   117 {
   118     // :TODO: cache this.
   119     return Type::NewNative(zone_, returnType, args, variadic);
   120 }
   121 
   122 void
   123 TypeManager::accept(VirtualObjectVisitor *visitor)
   124 {
   125     visitor->visit(voidType_.address());
   126     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   127         visitor->visit(primitiveTypes_[i].address());
   128         visitor->visit(referenceTypes_[i].address());
   129     }
   130 }