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.
[email protected]
     1
/* vim: set ts=4 sw=4 tw=99 et:
[email protected]
     2
 *
[email protected]
     3
 * Copyright (C) 2012 David Anderson
[email protected]
     4
 *
[email protected]
     5
 * This file is part of SourcePawn.
[email protected]
     6
 *
[email protected]
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
[email protected]
     8
 * the terms of the GNU General Public License as published by the Free
[email protected]
     9
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
    10
 * any later version.
[email protected]
    11
 * 
[email protected]
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
[email protected]
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    15
 *
[email protected]
    16
 * You should have received a copy of the GNU General Public License along with
[email protected]
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
[email protected]
    18
 */
[email protected]
    19
#include <string.h>
[email protected]
    20
#include "Types.h"
[email protected]
    21
#include "TypeManager.h"
[email protected]
    22
#include "Zone.h"
[email protected]
    23
#include "Strings.h"
[email protected]
    24
#include "Spaces-inl.h"
[email protected]
    25
#include "Heap-inl.h"
[email protected]
    26
[email protected]
    27
using namespace ke;
[email protected]
    28
[email protected]
    29
[email protected]
    30
TypeManager::TypeManager(Zone *zone)
[email protected]
    31
  : zone_(zone),
[email protected]
    32
    qualTable_(ZoneAllocatorPolicy())
[email protected]
    33
{
[email protected]
    34
}
[email protected]
    35
[email protected]
    36
bool
[email protected]
    37
TypeManager::initialize()
[email protected]
    38
{
[email protected]
    39
    voidType_ = Type::NewVoid(zone_);
[email protected]
    40
    if (!voidType_)
[email protected]
    41
        return false;
[email protected]
    42
[email protected]
    43
    uncheckedType_ = Type::NewUnchecked(zone_);
[email protected]
    44
    if (!uncheckedType_)
[email protected]
    45
        return false;
[email protected]
    46
[email protected]
    47
    stringType_ = Type::NewString(zone_);
[email protected]
    48
    if (!stringType_)
[email protected]
    49
        return false;
[email protected]
    50
[email protected]
    51
    importableType_ = Type::NewImportable(zone_);
[email protected]
    52
    if (!importableType_)
[email protected]
    53
        return false;
[email protected]
    54
[email protected]
    55
    if ((primitiveTypes_[PrimitiveType_Int32] = Type::NewPrimitive(zone_, PrimitiveType_Int32)) == NULL)
[email protected]
    56
        return false;
[email protected]
    57
    if ((primitiveTypes_[PrimitiveType_Float] = Type::NewPrimitive(zone_, PrimitiveType_Float)) == NULL)
[email protected]
    58
        return false;
[email protected]
    59
    if ((primitiveTypes_[PrimitiveType_Native] = Type::NewPrimitive(zone_, PrimitiveType_Native)) == NULL)
[email protected]
    60
        return false;
[email protected]
    61
    if ((primitiveTypes_[PrimitiveType_Char] = Type::NewPrimitive(zone_, PrimitiveType_Char)) == NULL)
[email protected]
    62
        return false;
[email protected]
    63
    if ((primitiveTypes_[PrimitiveType_Bool] = Type::NewPrimitive(zone_, PrimitiveType_Bool)) == NULL)
[email protected]
    64
        return false;
[email protected]
    65
[email protected]
    66
    Local<Type> type(zone_);
[email protected]
    67
    for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
[email protected]
    68
        type = primitiveTypes_[i];
[email protected]
    69
        if ((referenceTypes_[i] = ReferenceType::New(zone_, type)) == NULL)
[email protected]
    70
            return false;
[email protected]
    71
    }
[email protected]
    72
[email protected]
    73
    if (!qualTable_.init(32))
[email protected]
    74
        return false;
[email protected]
    75
[email protected]
    76
    return true;
[email protected]
    77
}
[email protected]
    78
[email protected]
    79
ArrayType *
[email protected]
    80
TypeManager::newArray(Handle<Type> contained, int elements)
[email protected]
    81
{
[email protected]
    82
    // :TODO: cache this.
[email protected]
    83
    return ArrayType::New(zone_, contained, elements);
[email protected]
    84
}
[email protected]
    85
[email protected]
    86
ArrayType *
[email protected]
    87
TypeManager::newExternalArray(Handle<Type> contained)
[email protected]
    88
{
[email protected]
    89
    return ArrayType::NewExternal(zone_, contained);
[email protected]
    90
}
[email protected]
    91
[email protected]
    92
ReferenceType *
[email protected]
    93
TypeManager::getReference(Handle<Type> type, bool isConst)
[email protected]
    94
{
[email protected]
    95
    if (!isConst && type->isPrimitive())
[email protected]
    96
        return referenceTypes_[type->primitive()];
[email protected]
    97
[email protected]
    98
    // :TODO: cache this.
[email protected]
    99
    return ReferenceType::New(zone_, type);
[email protected]
   100
}
[email protected]
   101
[email protected]
   102
EnumType *
[email protected]
   103
TypeManager::newEnum(Handle<String> name)
[email protected]
   104
{
[email protected]
   105
    return EnumType::New(zone_, name);
[email protected]
   106
}
[email protected]
   107
[email protected]
   108
Type *
[email protected]
   109
TypeManager::qualify(Handle<Type> argType, TypeQualifiers quals)
[email protected]
   110
{
[email protected]
   111
    Local<Type> type(zone_, argType->unqualified());
[email protected]
   112
[email protected]
   113
    if (!quals)
[email protected]
   114
        return type;
[email protected]
   115
[email protected]
   116
    LookupKey key(type, quals);
[email protected]
   117
[email protected]
   118
    // We can't use findForAdd because a GC could rehash the table.
[email protected]
   119
    TypeHashTable::Result r = qualTable_.find(key);
[email protected]
   120
    if (r.found())
[email protected]
   121
        return *r;
[email protected]
   122
[email protected]
   123
    Type *quald = Type::NewQualified(zone_, quals, type);
[email protected]
   124
    if (!quald)
[email protected]
   125
        return NULL;
[email protected]
   126
[email protected]
   127
    // :TODO: assert-no-gc
[email protected]
   128
    TypeHashTable::Insert i = qualTable_.findForAdd(key);
[email protected]
   129
    assert(!i.found());
[email protected]
   130
[email protected]
   131
    if (!qualTable_.add(i, quald))
[email protected]
   132
        return NULL;
[email protected]
   133
[email protected]
   134
    return quald;
[email protected]
   135
}
[email protected]
   136
[email protected]
   137
void
[email protected]
   138
TypeManager::accept(VirtualObjectVisitor *visitor)
[email protected]
   139
{
[email protected]
   140
    visitor->visit(voidType_.address());
[email protected]
   141
    visitor->visit(uncheckedType_.address());
[email protected]
   142
    visitor->visit(stringType_.address());
[email protected]
   143
    visitor->visit(importableType_.address());
[email protected]
   144
    for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
[email protected]
   145
        visitor->visit(primitiveTypes_[i].address());
[email protected]
   146
        visitor->visit(referenceTypes_[i].address());
[email protected]
   147
    }
[email protected]
   148
}
[email protected]
   149
[email protected]
   150
void
[email protected]
   151
TypeManager::sweep()
[email protected]
   152
{
[email protected]
   153
    for (TypeHashTable::iterator i(&qualTable_); !i.empty(); i.next()) {
[email protected]
   154
        Type *obj = *i;
[email protected]
   155
        MarkBit bits = HeapChunk::GetMarkBit(obj);
[email protected]
   156
        if (HeapChunk::IsWhite(bits))
[email protected]
   157
            i.erase();
[email protected]
   158
        else
[email protected]
   159
            assert(HeapChunk::IsBlack(bits));
[email protected]
   160
    }
[email protected]
   161
    qualTable_.checkDensity();
[email protected]
   162
}