src/TypeManager.cpp
author David Anderson <dvander@alliedmods.net>
Tue Jan 01 20:06:25 2013 -0800 (2013-01-01)
changeset 253 544c478362c2
parent 247 7721042bdb67
child 256 3c184218462d
permissions -rw-r--r--
Add WIN32 define for MSVC builds.
     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     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, PrimitiveType_Int32)) == NULL)
    52         return false;
    53     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, PrimitiveType_Float)) == NULL)
    54         return false;
    55     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    56         return false;
    57     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, PrimitiveType_Char)) == NULL)
    58         return false;
    59     if ((primitiveTypes_[PrimitiveType_Bool] = Type::NewPrimitive(zone_, PrimitiveType_Bool)) == NULL)
    60         return false;
    61 
    62     Local<Type> type(zone_);
    63     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    64         type = primitiveTypes_[i];
    65         if ((referenceTypes_[i] = ReferenceType::New(zone_, type)) == NULL)
    66             return false;
    67     }
    68 
    69     if (!qualTable_.init(32))
    70         return false;
    71 
    72     return true;
    73 }
    74 
    75 ArrayType *
    76 TypeManager::newArray(Handle<Type> contained, int elements)
    77 {
    78     // :TODO: cache this.
    79     return ArrayType::New(zone_, contained, elements);
    80 }
    81 
    82 ArrayType *
    83 TypeManager::newExternalArray(Handle<Type> contained)
    84 {
    85     return ArrayType::NewExternal(zone_, contained);
    86 }
    87 
    88 ReferenceType *
    89 TypeManager::getReference(Handle<Type> type, bool isConst)
    90 {
    91     if (!isConst && type->isPrimitive())
    92         return referenceTypes_[type->primitive()];
    93 
    94     // :TODO: cache this.
    95     return ReferenceType::New(zone_, type);
    96 }
    97 
    98 EnumType *
    99 TypeManager::newEnum(Handle<String> name)
   100 {
   101     return EnumType::New(zone_, name);
   102 }
   103 
   104 Type *
   105 TypeManager::qualify(Handle<Type> argType, TypeQualifiers quals)
   106 {
   107     Local<Type> type(zone_, argType->unqualified());
   108 
   109     if (!quals)
   110         return type;
   111 
   112     LookupKey key(type, quals);
   113 
   114     // We can't use findForAdd because a GC could rehash the table.
   115     TypeHashTable::Result r = qualTable_.find(key);
   116     if (r.found())
   117         return *r;
   118 
   119     Type *quald = Type::NewQualified(zone_, quals, type);
   120     if (!quald)
   121         return NULL;
   122 
   123     // :TODO: assert-no-gc
   124     TypeHashTable::Insert i = qualTable_.findForAdd(key);
   125     assert(!i.found());
   126 
   127     if (!qualTable_.add(i, quald))
   128         return NULL;
   129 
   130     return quald;
   131 }
   132 
   133 void
   134 TypeManager::accept(VirtualObjectVisitor *visitor)
   135 {
   136     visitor->visit(voidType_.address());
   137     visitor->visit(uncheckedType_.address());
   138     visitor->visit(stringType_.address());
   139     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   140         visitor->visit(primitiveTypes_[i].address());
   141         visitor->visit(referenceTypes_[i].address());
   142     }
   143 }
   144 
   145 void
   146 TypeManager::sweep()
   147 {
   148     for (TypeHashTable::iterator i(&qualTable_); !i.empty(); i.next()) {
   149         Type *obj = *i;
   150         MarkBit bits = HeapChunk::GetMarkBit(obj);
   151         if (HeapChunk::IsWhite(bits))
   152             i.erase();
   153         else
   154             assert(HeapChunk::IsBlack(bits));
   155     }
   156     qualTable_.checkDensity();
   157 }