Re-implement array assignment.
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"
29 Type::NewVoid(Zone *zone)
31 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
40 Type::NewString(Zone *zone)
42 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
51 Type::NewUnchecked(Zone *zone)
53 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
57 type->kind_ = UNCHECKED;
62 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
64 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
68 type->kind_ = PRIMITIVE;
69 type->primitive_ = prim;
74 ArrayType::New(Zone *zone, Handle<Type> contained, int elements)
76 if (contained->isArray() &&
77 ArrayType::cast(contained)->levels() == MAX_ARRAY_DEPTH)
79 zone->reportError(Message_MaximumArrayDepth);
83 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
88 type->contained_ = contained;
89 type->elements_ = elements;
90 if (!contained->isArray())
93 type->levels_ = ArrayType::cast(contained)->levels() + 1;
98 ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
100 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
104 assert(contained->isPrimitive());
106 type->kind_ = EXTERNAL_ARRAY;
107 type->contained_ = contained;
108 type->elements_ = DYNAMIC_ARRAY_SIZE;
114 ReferenceType::New(Zone *zone, Handle<Type> contained)
116 Local<ReferenceType> type(zone, ReferenceType::cast(zone->allocate(MapKind_ReferenceType, sizeof(ReferenceType), Heap::Tenure_Old)));
120 type->kind_ = REFERENCE;
121 type->const_ = false;
122 type->contained_ = contained;
127 EnumType::New(Zone *zone, Handle<String> name)
129 Local<EnumType> type(zone, EnumType::cast(zone->allocate(MapKind_EnumType, sizeof(EnumType), Heap::Tenure_Old)));
139 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
141 Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
145 type->kind_ = STRUCT;
146 type->newMap_ = NULL;
147 type->fields_ = fields;
150 // Fill in field offsets.
151 StructureBufferBuilder builder;
152 Local<Type> fieldType(zone);
153 for (size_t i = 0; i < fields->length(); i++) {
154 Descriptor *desc = Descriptor::cast(fields->at(i));
155 fieldType = desc->type();
158 if (!builder.add(fieldType, &offset))
160 desc->setOffset(offset);
163 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
173 StructType::lookupField(Handle<String> name, unsigned *index)
175 for (unsigned i = 0; i < fields_->length(); i++) {
176 Descriptor *desc = Descriptor::cast(fields_->at(i));
177 if (desc->name() == name) {
188 FunctionType::New(Zone *zone, Handle<String> name, TokenKind token)
190 Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
194 type->kind_ = FUNCTION;
195 type->parameters_ = NULL;
196 type->defaults_ = NULL;
197 type->returnType_ = NULL;
199 type->token_ = token;
203 // :TODO: this leaks an interior pointer.
205 ke::GetTypeName(Type *type)
209 if (type->isFunction())
214 return EnumType::cast(type)->name()->chars();
215 if (type->isStruct())
216 return StructType::cast(type)->name()->chars();
217 if (type->isReference())
218 type = ReferenceType::cast(type)->contained();
219 switch (type->primitive()) {
220 case PrimitiveType_Bool:
222 case PrimitiveType_Char:
224 case PrimitiveType_Int32:
226 case PrimitiveType_Float: