src/TypeManager.h
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).
asherkin@263
     1
/* vim: set ts=4 sw=4 tw=99 et:
asherkin@263
     2
 *
asherkin@263
     3
 * Copyright (C) 2012 David Anderson
asherkin@263
     4
 *
asherkin@263
     5
 * This file is part of SourcePawn.
asherkin@263
     6
 *
asherkin@263
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     8
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     9
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
    10
 * any later version.
asherkin@263
    11
 * 
asherkin@263
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    15
 *
asherkin@263
    16
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    18
 */
asherkin@263
    19
#ifndef _include_jitcraft_type_manager_h_
asherkin@263
    20
#define _include_jitcraft_type_manager_h_
asherkin@263
    21
asherkin@263
    22
#include "Barriers.h"
asherkin@263
    23
#include "Types.h"
asherkin@263
    24
#include "HashTable.h"
asherkin@263
    25
#include "AllocatorPolicies.h"
asherkin@263
    26
#include "VirtualObjectVisitor.h"
asherkin@263
    27
asherkin@263
    28
namespace ke {
asherkin@263
    29
asherkin@263
    30
class String;
asherkin@263
    31
asherkin@263
    32
class TypeManager
asherkin@263
    33
{
asherkin@263
    34
    struct LookupKey
asherkin@263
    35
    {
asherkin@263
    36
        Handle<Type> type;
asherkin@263
    37
        TypeQualifiers quals;
asherkin@263
    38
asherkin@263
    39
        LookupKey(Handle<Type> type, TypeQualifiers quals)
asherkin@263
    40
          : type(type),
asherkin@263
    41
            quals(quals)
asherkin@263
    42
        {
asherkin@263
    43
        }
asherkin@263
    44
    };
asherkin@263
    45
asherkin@263
    46
    struct Policy
asherkin@263
    47
    {
asherkin@263
    48
        typedef Type * Payload;
asherkin@263
    49
asherkin@263
    50
        static uint32 hash(const LookupKey &key) {
asherkin@263
    51
            // We'll need to rehash with moving GC.
asherkin@263
    52
            return HashPointer(key.type);
asherkin@263
    53
        }
asherkin@263
    54
        
asherkin@263
    55
        static bool matches(const LookupKey &key, Type *type) {
asherkin@263
    56
            assert(type->hasTypeQualifiers());
asherkin@263
    57
            return key.type == type->unqualified() &&
asherkin@263
    58
                   key.quals == type->quals();                   
asherkin@263
    59
        }
asherkin@263
    60
    };
asherkin@263
    61
asherkin@263
    62
    typedef HashTable<Policy, ZoneAllocatorPolicy> TypeHashTable;
asherkin@263
    63
asherkin@263
    64
  public:
asherkin@263
    65
    TypeManager(Zone *zone);
asherkin@263
    66
asherkin@263
    67
    bool initialize();
asherkin@263
    68
asherkin@263
    69
    Type *getPrimitive(PrimitiveType type) {
asherkin@263
    70
        return primitiveTypes_[type];
asherkin@263
    71
    }
asherkin@263
    72
asherkin@263
    73
    Type *getString() {
asherkin@263
    74
        return stringType_;
asherkin@263
    75
    }
asherkin@263
    76
    Type *getVoid() {
asherkin@263
    77
        return voidType_;
asherkin@263
    78
    }
asherkin@263
    79
    Type *getUnchecked() {
asherkin@263
    80
        return uncheckedType_;
asherkin@263
    81
    }
asherkin@263
    82
    Type *getImportable() {
asherkin@263
    83
        return importableType_;
asherkin@263
    84
    }
asherkin@263
    85
    Type *getType() {
asherkin@263
    86
        return typeType_;
asherkin@263
    87
    }
asherkin@263
    88
    ReferenceType *getReference(Handle<Type> type, bool isConst);
asherkin@263
    89
    ArrayType *newArray(Handle<Type> contained, int elements);
asherkin@263
    90
    ArrayType *newExternalArray(Handle<Type> contained);
asherkin@263
    91
    EnumType *newEnum(Handle<String> name);
asherkin@263
    92
    
asherkin@263
    93
    void accept(VirtualObjectVisitor *visitor);
asherkin@263
    94
    void sweep();
asherkin@263
    95
asherkin@263
    96
    Type *qualify(Handle<Type> type, TypeQualifiers quals);
asherkin@263
    97
asherkin@263
    98
  private:
asherkin@263
    99
    Zone *zone_;
asherkin@263
   100
asherkin@263
   101
    // Primitive type cache.
asherkin@263
   102
    Unbarriered<Type> voidType_;
asherkin@263
   103
    Unbarriered<Type> stringType_;
asherkin@263
   104
    Unbarriered<Type> uncheckedType_;
asherkin@263
   105
    Unbarriered<Type> importableType_;
asherkin@263
   106
    Unbarriered<Type> typeType_;
asherkin@263
   107
    Unbarriered<Type> primitiveTypes_[PrimitiveTypes_Total];
asherkin@263
   108
    Unbarriered<ReferenceType> referenceTypes_[PrimitiveTypes_Total];
asherkin@263
   109
asherkin@263
   110
    TypeHashTable qualTable_;
asherkin@263
   111
};
asherkin@263
   112
asherkin@263
   113
}
asherkin@263
   114
asherkin@263
   115
#endif // _include_jitcraft_type_manager_h_