src/TypeManager.cpp
author Michael Busby <prodigysim@gmail.com>
Mon Jun 10 01:50:18 2013 +0100 (2013-06-10)
changeset 264 70b8f318c379
parent 263 ba85a47ee414
permissions -rw-r--r--
Parse assignment RTL instead of LTR, fixes multiple assignment (bug 5420).
     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     typeType_ = Type::NewType(zone_);
    56     if (!typeType_)
    57         return false;
    58 
    59     if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, PrimitiveType_Int32)) == NULL)
    60         return false;
    61     if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, PrimitiveType_Float)) == NULL)
    62         return false;
    63     if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
    64         return false;
    65     if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, PrimitiveType_Char)) == NULL)
    66         return false;
    67     if ((primitiveTypes_[PrimitiveType_Bool] = Type::NewPrimitive(zone_, PrimitiveType_Bool)) == NULL)
    68         return false;
    69 
    70     Local<Type> type(zone_);
    71     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
    72         type = primitiveTypes_[i];
    73         if ((referenceTypes_[i] = ReferenceType::New(zone_, type)) == NULL)
    74             return false;
    75     }
    76 
    77     if (!qualTable_.init(32))
    78         return false;
    79 
    80     return true;
    81 }
    82 
    83 ArrayType *
    84 TypeManager::newArray(Handle<Type> contained, int elements)
    85 {
    86     // :TODO: cache this.
    87     return ArrayType::New(zone_, contained, elements);
    88 }
    89 
    90 ArrayType *
    91 TypeManager::newExternalArray(Handle<Type> contained)
    92 {
    93     return ArrayType::NewExternal(zone_, contained);
    94 }
    95 
    96 ReferenceType *
    97 TypeManager::getReference(Handle<Type> type, bool isConst)
    98 {
    99     if (!isConst && type->isPrimitive())
   100         return referenceTypes_[type->primitive()];
   101 
   102     // :TODO: cache this.
   103     return ReferenceType::New(zone_, type);
   104 }
   105 
   106 EnumType *
   107 TypeManager::newEnum(Handle<String> name)
   108 {
   109     return EnumType::New(zone_, name);
   110 }
   111 
   112 Type *
   113 TypeManager::qualify(Handle<Type> argType, TypeQualifiers quals)
   114 {
   115     Local<Type> type(zone_, argType->unqualified());
   116 
   117     if (!quals)
   118         return type;
   119 
   120     LookupKey key(type, quals);
   121 
   122     // We can't use findForAdd because a GC could rehash the table.
   123     TypeHashTable::Result r = qualTable_.find(key);
   124     if (r.found())
   125         return *r;
   126 
   127     Type *quald = Type::NewQualified(zone_, quals, type);
   128     if (!quald)
   129         return NULL;
   130 
   131     // :TODO: assert-no-gc
   132     TypeHashTable::Insert i = qualTable_.findForAdd(key);
   133     assert(!i.found());
   134 
   135     if (!qualTable_.add(i, quald))
   136         return NULL;
   137 
   138     return quald;
   139 }
   140 
   141 void
   142 TypeManager::accept(VirtualObjectVisitor *visitor)
   143 {
   144     visitor->visit(voidType_.address());
   145     visitor->visit(uncheckedType_.address());
   146     visitor->visit(stringType_.address());
   147     visitor->visit(importableType_.address());
   148     visitor->visit(typeType_.address());
   149     for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
   150         visitor->visit(primitiveTypes_[i].address());
   151         visitor->visit(referenceTypes_[i].address());
   152     }
   153 }
   154 
   155 void
   156 TypeManager::sweep()
   157 {
   158     for (TypeHashTable::iterator i(&qualTable_); !i.empty(); i.next()) {
   159         Type *obj = *i;
   160         MarkBit bits = HeapChunk::GetMarkBit(obj);
   161         if (HeapChunk::IsWhite(bits))
   162             i.erase();
   163         else
   164             assert(HeapChunk::IsBlack(bits));
   165     }
   166     qualTable_.checkDensity();
   167 }