src/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 247 7721042bdb67
child 261 219b49bf4a03
permissions -rw-r--r--
Initial implementation of module imports.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn 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  * SourcePawn 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  * SourcePawn. 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     qualTable_(ZoneAllocatorPolicy())
    33 {
    34 }
    35 
    36 bool
    37 TypeManager::initialize()
    38 {
    39     voidType_ = Type::NewVoid(zone_);
    40     if (!voidType_)
    41         return false;
    42 
    43     uncheckedType_ = Type::NewUnchecked(zone_);
    44     if (!uncheckedType_)
    45         return false;
    46 
    47     stringType_ = Type::NewString(zone_);
    48     if (!stringType_)
    49         return false;
    50 
    51     importableType_ = Type::NewImportable(zone_);
    52     if (!importableType_)
    53         return false;
    54 
    55     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, PrimitiveType_Int32)) == NULL)
    56         return false;
    57     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, PrimitiveType_Float)) == NULL)
    58         return false;
    59     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    60         return false;
    61     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, PrimitiveType_Char)) == NULL)
    62         return false;
    63     if ((primitiveTypes_[PrimitiveType_Bool] = Type::NewPrimitive(zone_, PrimitiveType_Bool)) == NULL)
    64         return false;
    65 
    66     Local<Type> type(zone_);
    67     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    68         type = primitiveTypes_[i];
    69         if ((referenceTypes_[i] = ReferenceType::New(zone_, type)) == NULL)
    70             return false;
    71     }
    72 
    73     if (!qualTable_.init(32))
    74         return false;
    75 
    76     return true;
    77 }
    78 
    79 ArrayType *
    80 TypeManager::newArray(Handle<Type> contained, int elements)
    81 {
    82     // :TODO: cache this.
    83     return ArrayType::New(zone_, contained, elements);
    84 }
    85 
    86 ArrayType *
    87 TypeManager::newExternalArray(Handle<Type> contained)
    88 {
    89     return ArrayType::NewExternal(zone_, contained);
    90 }
    91 
    92 ReferenceType *
    93 TypeManager::getReference(Handle<Type> type, bool isConst)
    94 {
    95     if (!isConst && type->isPrimitive())
    96         return referenceTypes_[type->primitive()];
    97 
    98     // :TODO: cache this.
    99     return ReferenceType::New(zone_, type);
   100 }
   101 
   102 EnumType *
   103 TypeManager::newEnum(Handle<String> name)
   104 {
   105     return EnumType::New(zone_, name);
   106 }
   107 
   108 Type *
   109 TypeManager::qualify(Handle<Type> argType, TypeQualifiers quals)
   110 {
   111     Local<Type> type(zone_, argType->unqualified());
   112 
   113     if (!quals)
   114         return type;
   115 
   116     LookupKey key(type, quals);
   117 
   118     // We can't use findForAdd because a GC could rehash the table.
   119     TypeHashTable::Result r = qualTable_.find(key);
   120     if (r.found())
   121         return *r;
   122 
   123     Type *quald = Type::NewQualified(zone_, quals, type);
   124     if (!quald)
   125         return NULL;
   126 
   127     // :TODO: assert-no-gc
   128     TypeHashTable::Insert i = qualTable_.findForAdd(key);
   129     assert(!i.found());
   130 
   131     if (!qualTable_.add(i, quald))
   132         return NULL;
   133 
   134     return quald;
   135 }
   136 
   137 void
   138 TypeManager::accept(VirtualObjectVisitor *visitor)
   139 {
   140     visitor->visit(voidType_.address());
   141     visitor->visit(uncheckedType_.address());
   142     visitor->visit(stringType_.address());
   143     visitor->visit(importableType_.address());
   144     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   145         visitor->visit(primitiveTypes_[i].address());
   146         visitor->visit(referenceTypes_[i].address());
   147     }
   148 }
   149 
   150 void
   151 TypeManager::sweep()
   152 {
   153     for (TypeHashTable::iterator i(&qualTable_); !i.empty(); i.next()) {
   154         Type *obj = *i;
   155         MarkBit bits = HeapChunk::GetMarkBit(obj);
   156         if (HeapChunk::IsWhite(bits))
   157             i.erase();
   158         else
   159             assert(HeapChunk::IsBlack(bits));
   160     }
   161     qualTable_.checkDensity();
   162 }