More const preparation: allow type casts to follow qualification.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
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)
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.
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/.
23 #include "Structures.h"
36 Type::NewVoid(Zone *zone)
38 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
47 Type::NewString(Zone *zone)
49 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
58 Type::NewUnchecked(Zone *zone)
60 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
64 type->init(UNCHECKED);
69 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
71 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
75 type->init(PRIMITIVE);
76 type->primitive_ = prim;
81 ArrayType::New(Zone *zone, Handle<Type> contained, int elements)
83 if (contained->isArray() &&
84 ArrayType::cast(contained)->levels() == MAX_ARRAY_DEPTH)
86 zone->reportError(Message_MaximumArrayDepth);
90 Local<ArrayType> type(zone, (ArrayType *)zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old));
95 type->contained_ = contained;
96 type->elements_ = elements;
97 if (!contained->isArray())
100 type->levels_ = ArrayType::cast(contained)->levels() + 1;
105 ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
107 Local<ArrayType> type(zone, (ArrayType *)zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old));
111 assert(contained->isPrimitive());
113 type->init(EXTERNAL_ARRAY);
114 type->contained_ = contained;
115 type->elements_ = DYNAMIC_ARRAY_SIZE;
121 ReferenceType::New(Zone *zone, Handle<Type> contained)
123 Local<ReferenceType> type(zone, (ReferenceType *)zone->allocate(MapKind_ReferenceType, sizeof(ReferenceType), Heap::Tenure_Old));
127 type->init(REFERENCE);
128 type->contained_ = contained;
133 EnumType::New(Zone *zone, Handle<String> name)
135 Local<EnumType> type(zone, (EnumType *)zone->allocate(MapKind_EnumType, sizeof(EnumType), Heap::Tenure_Old));
145 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
147 Local<StructType> type(zone, (StructType *)zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old));
152 type->newMap_ = NULL;
153 type->fields_ = fields;
156 // Fill in field offsets.
157 StructureBufferBuilder builder;
158 Local<Type> fieldType(zone);
159 for (size_t i = 0; i < fields->length(); i++) {
160 Descriptor *desc = Descriptor::cast(fields->at(i));
161 fieldType = desc->type();
164 if (!builder.add(fieldType, &offset))
166 desc->setOffset(offset);
169 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
179 StructType::lookupField(Handle<String> name, unsigned *index)
181 for (unsigned i = 0; i < fields_->length(); i++) {
182 Descriptor *desc = Descriptor::cast(fields_->at(i));
183 if (desc->name() == name) {
194 FunctionType::New(Zone *zone, Handle<String> name, TokenKind token)
196 Local<FunctionType> type(zone, (FunctionType *)zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old));
200 type->init(FUNCTION);
201 type->parameters_ = NULL;
202 type->defaults_ = NULL;
203 type->returnType_ = NULL;
205 type->token_ = token;
209 // :TODO: this leaks an interior pointer.
211 ke::GetTypeName(Type *type)
215 if (type->isFunction())
220 return EnumType::cast(type)->name()->chars();
221 if (type->isStruct())
222 return StructType::cast(type)->name()->chars();
223 if (type->isReference())
224 type = ReferenceType::cast(type)->contained();
225 switch (type->primitive()) {
226 case PrimitiveType_Bool:
228 case PrimitiveType_Char:
230 case PrimitiveType_Int32:
232 case PrimitiveType_Float: