src/TypeManager.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 240 e3c851a14f16
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 #ifndef _include_jitcraft_type_manager_h_
    20 #define _include_jitcraft_type_manager_h_
    21 
    22 #include "Barriers.h"
    23 #include "Types.h"
    24 #include "HashTable.h"
    25 #include "AllocatorPolicies.h"
    26 #include "VirtualObjectVisitor.h"
    27 
    28 namespace ke {
    29 
    30 class String;
    31 
    32 class TypeManager
    33 {
    34     struct LookupKey
    35     {
    36         Handle<Type> type;
    37         TypeQualifiers quals;
    38 
    39         LookupKey(Handle<Type> type, TypeQualifiers quals)
    40           : type(type),
    41             quals(quals)
    42         {
    43         }
    44     };
    45 
    46     struct Policy
    47     {
    48         typedef Type * Payload;
    49 
    50         static uint32 hash(const LookupKey &key) {
    51             // We'll need to rehash with moving GC.
    52             return HashPointer(key.type);
    53         }
    54         
    55         static bool matches(const LookupKey &key, Type *type) {
    56             assert(type->hasTypeQualifiers());
    57             return key.type == type->unqualified() &&
    58                    key.quals == type->quals();                   
    59         }
    60     };
    61 
    62     typedef HashTable<Policy, ZoneAllocatorPolicy> TypeHashTable;
    63 
    64   public:
    65     TypeManager(Zone *zone);
    66 
    67     bool initialize();
    68 
    69     Type *getPrimitive(PrimitiveType type) {
    70         return primitiveTypes_[type];
    71     }
    72 
    73     Type *getString() {
    74         return stringType_;
    75     }
    76     Type *getVoid() {
    77         return voidType_;
    78     }
    79     Type *getUnchecked() {
    80         return uncheckedType_;
    81     }
    82     Type *getImportable() {
    83         return importableType_;
    84     }
    85     ReferenceType *getReference(Handle<Type> type, bool isConst);
    86     ArrayType *newArray(Handle<Type> contained, int elements);
    87     ArrayType *newExternalArray(Handle<Type> contained);
    88     EnumType *newEnum(Handle<String> name);
    89     
    90     void accept(VirtualObjectVisitor *visitor);
    91     void sweep();
    92 
    93     Type *qualify(Handle<Type> type, TypeQualifiers quals);
    94 
    95   private:
    96     Zone *zone_;
    97 
    98     // Primitive type cache.
    99     Unbarriered<Type> voidType_;
   100     Unbarriered<Type> stringType_;
   101     Unbarriered<Type> uncheckedType_;
   102     Unbarriered<Type> importableType_;
   103     Unbarriered<Type> primitiveTypes_[PrimitiveTypes_Total];
   104     Unbarriered<ReferenceType> referenceTypes_[PrimitiveTypes_Total];
   105 
   106     TypeHashTable qualTable_;
   107 };
   108 
   109 }
   110 
   111 #endif // _include_jitcraft_type_manager_h_