Fix warnings when building with clang.
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/.
24 #include "Structures.h"
30 Type::init(Kind kind, Type *root)
33 this->root_ = root ? root : this;
37 Type::NewVoid(Zone *zone)
39 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
48 Type::NewImportable(Zone *zone)
50 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
54 type->init(IMPORTABLE);
59 Type::NewString(Zone *zone)
61 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
70 Type::NewUnchecked(Zone *zone)
72 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
76 type->init(UNCHECKED);
81 Type::NewType(Zone *zone)
83 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
92 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
94 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
98 type->init(PRIMITIVE);
99 type->primitive_ = prim;
104 Type::NewQualified(Zone *zone, TypeQualifiers qual, Handle<Type> root)
106 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
110 type->init(QUALTYPE, root);
116 ArrayType::New(Zone *zone, Handle<Type> contained, int elements)
118 if (contained->isArray() &&
119 ArrayType::cast(contained)->levels() == MAX_ARRAY_DEPTH)
121 zone->reportError(Message_MaximumArrayDepth);
125 Local<ArrayType> type(zone, (ArrayType *)zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old));
130 type->contained_ = contained;
131 type->elements_ = elements;
132 if (!contained->isArray())
135 type->levels_ = ArrayType::cast(contained)->levels() + 1;
137 type->newMap_ = ArrayMap::New(zone, type);
145 ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
147 Local<ArrayType> type(zone, (ArrayType *)zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old));
151 assert(contained->isPrimitive());
153 type->init(EXTERNAL_ARRAY);
154 type->contained_ = contained;
155 type->elements_ = DYNAMIC_ARRAY_SIZE;
161 ReferenceType::New(Zone *zone, Handle<Type> contained)
163 Local<ReferenceType> type(zone, (ReferenceType *)zone->allocate(MapKind_ReferenceType, sizeof(ReferenceType), Heap::Tenure_Old));
167 type->init(REFERENCE);
168 type->contained_ = contained;
173 EnumType::New(Zone *zone, Handle<String> name)
175 Local<EnumType> type(zone, (EnumType *)zone->allocate(MapKind_EnumType, sizeof(EnumType), Heap::Tenure_Old));
181 type->fields_ = NULL;
186 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
188 Local<StructType> type(zone, (StructType *)zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old));
193 type->newMap_ = NULL;
194 type->fields_ = fields;
197 // Fill in field offsets.
198 StructureBufferBuilder builder;
199 Local<Type> fieldType(zone);
200 for (size_t i = 0; i < fields->length(); i++) {
201 Descriptor *desc = Descriptor::cast(fields->at(i));
202 fieldType = desc->type();
205 if (!builder.add(fieldType, &offset))
207 desc->setOffset(offset);
210 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
220 StructType::lookupField(String *name, unsigned *index)
222 // :TODO: assert no GC
223 for (unsigned i = 0; i < fields_->length(); i++) {
224 Descriptor *desc = Descriptor::cast(fields_->at(i));
225 if (desc->name() == name) {
236 FunctionType::New(Zone *zone, Handle<String> name, TokenKind token)
238 Local<FunctionType> type(zone, (FunctionType *)zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old));
242 type->init(FUNCTION);
243 type->parameters_ = NULL;
244 type->defaults_ = NULL;
245 type->returnType_ = NULL;
247 type->token_ = token;
252 FormatVA(char *buffer, size_t maxlen, const char *text, va_list ap)
254 size_t len = vsnprintf(buffer, maxlen, text, ap);
256 buffer[maxlen - 1] = '\0';
263 Format(char *buffer, size_t maxlen, const char *text, ...)
267 size_t len = FormatVA(buffer, maxlen, text, ap);
273 FormatTypeName(Type *type, char *buffer, size_t maxlen)
275 if (type->isArray()) {
276 ArrayType *atype = ArrayType::cast(type);
277 Type *innermost = atype->innermost();
279 size_t len = FormatTypeName(innermost, buffer, maxlen);
281 if (atype->isFixedLength())
282 len += Format(buffer + len, maxlen - len, "[%d]", atype->fixedLength());
284 len += Format(buffer + len, maxlen - len, "[]");
285 if (!atype->contained()->isArray())
287 atype = ArrayType::cast(atype->contained());
291 if (type->hasTypeQualifiers()) {
293 if (type->quals() & TypeQual_Const)
294 len += Format(buffer, maxlen, "const ");
295 return len + FormatTypeName(type->unqualified(), buffer + len, maxlen - len);
297 if (type->isFunction())
298 return Format(buffer, maxlen, "function");
300 return Format(buffer, maxlen, "void");
301 if (type->isUnchecked())
302 return Format(buffer, maxlen, "unchecked");
303 if (type->isImportable())
304 return Format(buffer, maxlen, "import");
306 return Format(buffer, maxlen, "%s", EnumType::cast(type)->name()->chars());
307 if (type->isStruct())
308 return Format(buffer, maxlen, "%s", StructType::cast(type)->name()->chars());
309 if (type->isReference()) {
310 ReferenceType *ref = ReferenceType::cast(type);
311 size_t len = Format(buffer, maxlen, "&");
312 return len + FormatTypeName(ref->contained(), buffer + len, maxlen - len);
314 switch (type->primitive()) {
315 case PrimitiveType_Bool:
316 return Format(buffer, maxlen, "bool");
317 case PrimitiveType_Char:
318 return Format(buffer, maxlen, "char");
319 case PrimitiveType_Int32:
320 return Format(buffer, maxlen, "int");
321 case PrimitiveType_Float:
322 return Format(buffer, maxlen, "float");
324 return Format(buffer, maxlen, "<UNKNOWN>");
329 ke::GetTypeName(Type *type, char *buffer, size_t maxlen)
331 FormatTypeName(type, buffer, maxlen);