File: TypeRef.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (404 lines) | stat: -rw-r--r-- 9,974 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "stdafx.h"
#include "TypeRef.h"
#include "Exception.h"
#include "World.h"

TypeRef::TypeRef(const SrcPos &pos) : pos(pos), constType(false) {}


TemplateType::TemplateType(const SrcPos &pos, const CppName &name, const vector<Auto<TypeRef>> &params) :
	TypeRef(pos), name(name), params(params) {}

Size TemplateType::size() const {
	throw Error(L"Templates are unsupported in general!", pos);
}

Auto<TypeRef> TemplateType::resolve(World &in, const CppName &context) const {
	vector<Auto<TypeRef>> p = params;
	for (nat i = 0; i < p.size(); i++)
		p[i] = p[i]->resolve(in, context);

	// This is special, as it is not exported to Storm, but we have to know about it to properly GC it.
	if (name == L"GcArray" && p.size() == 1) {
		return new GcArrayType(pos, p[0], false);
	} else if (name == L"GcWeakArray" && p.size() == 1) {
		return new GcArrayType(pos, p[0], true);
	} else if (Template *found = in.templates.findUnsafe(name, context)) {
		// Found it! Note that the Maybe<> type is special.
		if (found->name == L"storm::Maybe") {
			return new MaybeValueType(p[0]);
		} else {
			return new ResolvedTemplateType(pos, found, p);
		}
	} else {
		// Nothing found. Remain a unique, non-gc:d type.
		return new TemplateType(pos, name, params);
	}
}

void TemplateType::print(wostream &to) const {
	to << name << L"<";
	join(to, params, L", ");
	to << L">";
}

size_t TemplateType::sortId() const {
	if (params.empty())
		return 0;
	else
		return params[0]->sortId();
}

ResolvedTemplateType::ResolvedTemplateType(const SrcPos &pos, Template *templ, const vector<Auto<TypeRef>> &params) :
	TypeRef(pos), type(templ), params(params) {}

Size ResolvedTemplateType::size() const {
	throw Error(L"Can not use templates as values, as their size is unknown.", pos);
}

Auto<TypeRef> ResolvedTemplateType::resolve(World &in, const CppName &context) const {
	vector<Auto<TypeRef>> p = params;
	for (nat i = 0; i < p.size(); i++)
		p[i] = p[i]->resolve(in, context);
	return new ResolvedTemplateType(pos, type, p);
}

void ResolvedTemplateType::print(wostream &to) const {
	to << type->name << L"<";
	join(to, params, L", ");
	to << L">";
}

size_t ResolvedTemplateType::sortId() const {
	if (params.empty())
		return type->id;
	else
		return params[0]->sortId() + type->id + 1;
}

PtrType::PtrType(Auto<TypeRef> of) : TypeRef(of->pos), of(of) {}

Auto<TypeRef> PtrType::resolve(World &in, const CppName &context) const {
	Auto<PtrType> r = new PtrType(*this);
	r->of = of->resolve(in, context);
	return r;
}

void PtrType::print(wostream &to) const {
	to << of << L"*";
}

size_t PtrType::sortId() const {
	return of->sortId();
}

RefType::RefType(Auto<TypeRef> of) : TypeRef(of->pos), of(of) {}

Auto<TypeRef> RefType::resolve(World &in, const CppName &context) const {
	Auto<RefType> r = new RefType(*this);
	r->of = of->resolve(in, context);
	return r;
}

void RefType::print(wostream &to) const {
	to << of << L"&";
}

size_t RefType::sortId() const {
	return of->sortId();
}

MaybeClassType::MaybeClassType(Auto<TypeRef> of) : TypeRef(of->pos) {
	if (this->of = of.as<PtrType>())
		;
	else
		throw Error(L"MAYBE(T) must contain a pointer, use Maybe<T> instead.", of->pos);
}

Auto<TypeRef> MaybeClassType::resolve(World &in, const CppName &context) const {
	Auto<MaybeClassType> r = new MaybeClassType(*this);
	r->of = of->resolve(in, context).as<PtrType>();
	if (!r->of)
		throw Error(L"MAYBE content turned into non-pointer.", of->pos);
	return r;
}

void MaybeClassType::print(wostream &to) const {
	to << L"MAYBE(" << of << L")";
}

size_t MaybeClassType::sortId() const {
	return of->sortId();
}

MaybeValueType::MaybeValueType(Auto<TypeRef> of) : TypeRef(of->pos), of(of) {
	if (of.as<PtrType>())
		throw Error(L"Maybe<T> must refer to a value, use MAYBE(T *) instead.", of->pos);
}

Size MaybeValueType::size() const {
	Size s = of->size();
	// Align to struct boundary (should already be done).
	s += s.align();

	// Add a bool and align again.
	s += Size::sByte;
	s += s.align();

	return s;
}

Auto<TypeRef> MaybeValueType::resolve(World &in, const CppName &context) const {
	Auto<MaybeValueType> r = new MaybeValueType(*this);
	r->of = of->resolve(in, context);
	if (Auto<ResolvedType> x = r->of.as<ResolvedType>()) {
		if (Class *c = as<Class>(x->type)) {
			if (c->hasDtor())
				throw Error(L"The maybe-type does not support values with destructors or copy-ctors.", of->pos);
		}
	}
	return r;
}

void MaybeValueType::print(wostream &to) const {
	to << L"storm::Maybe<" << of << L">";
}

size_t MaybeValueType::sortId() const {
	return of->sortId();
}

NamedType::NamedType(const SrcPos &pos, const CppName &name) : TypeRef(pos), name(name) {}

NamedType::NamedType(const SrcPos &pos, const String &name) : TypeRef(pos), name(name) {}

Size NamedType::size() const {
	throw Error(L"Unknown size of the non-exported type " + name +
				L"\nUse PTR_NOGC or similar if this is the intent.", pos);
}

Auto<TypeRef> NamedType::resolve(World &in, const CppName &context) const {
	Type *t = in.types.findUnsafe(name, context);
	if (t)
		return new ResolvedType(*this, t);
	else if (name == L"void")
		return new VoidType(pos);
	else if (name == L"GcWatch" || name == L"storm::GcWatch")
		return new GcWatchType(pos);
	else if (name == L"EnginePtr" || name == L"storm::EnginePtr")
		return new EnginePtrType(pos);
	else if (name == L"GcType" || name == L"storm::GcType")
		return new GcTypeType(pos);

	// Last restort: built in type?
	map<String, Size>::const_iterator i = in.builtIn.find(name);
	if (i != in.builtIn.end())
		return new BuiltInType(pos, name, i->second);
	else
		return new NamedType(*this);
}

void NamedType::print(wostream &to) const {
	to << name;
}

size_t NamedType::sortId() const {
	return 0;
}


ResolvedType::ResolvedType(const TypeRef &templ, Type *type) : TypeRef(templ), type(type) {}

ResolvedType::ResolvedType(Type *type) : TypeRef(type->pos), type(type) {}

Size ResolvedType::size() const {
	return type->size();
}

bool ResolvedType::gcType() const {
	return type->heapAlloc();
}

Auto<TypeRef> ResolvedType::resolve(World &in, const CppName &context) const {
	return new ResolvedType(*this, type);
}

void ResolvedType::print(wostream &to) const {
	to << type->name;
}

size_t ResolvedType::sortId() const {
	return (type->id + 1) * 100;
}


BuiltInType::BuiltInType(const SrcPos &pos, const String &name, Size size) : TypeRef(pos), name(name), tSize(size) {}

Auto<TypeRef> BuiltInType::resolve(World &in, const CppName &context) const {
	return new BuiltInType(pos, name, tSize);
}

void BuiltInType::print(wostream &to) const {
	to << name;
}

size_t BuiltInType::sortId() const {
	return 0;
}


VoidType::VoidType(const SrcPos &pos) : TypeRef(pos) {}

Auto<TypeRef> VoidType::resolve(World &in, const CppName &context) const {
	return new VoidType(pos);
}

void VoidType::print(wostream &to) const {
	to << L"void";
}

size_t VoidType::sortId() const {
	return 0;
}


GcArrayType::GcArrayType(const SrcPos &pos, Auto<TypeRef> of, bool weak) : TypeRef(pos), of(of), weak(weak) {}

Auto<TypeRef> GcArrayType::resolve(World &in, const CppName &context) const {
	Auto<GcArrayType> r = new GcArrayType(*this);
	r->of = of->resolve(in, context);
	return r;
}

Size GcArrayType::size() const {
	throw Error(L"GcArray<> should only be used as a pointer!", pos);
}

void GcArrayType::print(wostream &to) const {
	if (weak)
		to << L"storm::GcWeakArray<" << of << L">";
	else
		to << L"storm::GcArray<" << of << L">";
}

size_t GcArrayType::sortId() const {
	return of->sortId();
}


GcWatchType::GcWatchType(const SrcPos &pos) : TypeRef(pos) {}

Auto<TypeRef> GcWatchType::resolve(World &in, const CppName &context) const {
	return new GcWatchType(*this);
}

Size GcWatchType::size() const {
	throw Error(L"GcWatch should only be used as a pointer!", pos);
}

void GcWatchType::print(wostream &to) const {
	to << L"storm::GcWatch";
}

size_t GcWatchType::sortId() const {
	return 0;
}


Auto<TypeRef> EnginePtrType::resolve(World &in, const CppName &context) const {
	return new EnginePtrType(*this);
}

Size EnginePtrType::size() const {
	throw Error(L"EnginePtr should only be used as parameters to functions.", pos);
}

void EnginePtrType::print(wostream &to) const {
	to << L"storm::EnginePtr";
}

size_t EnginePtrType::sortId() const {
	return 0;
}

Auto<TypeRef> GcTypeType::resolve(World &in, const CppName &context) const {
	return new GcTypeType(*this);
}

Size GcTypeType::size() const {
	throw Error(L"GcType should only be used as a pointer.", pos);
}

void GcTypeType::print(wostream &to) const {
	to << L"storm::GcType";
}

size_t GcTypeType::sortId() const {
	return 0;
}


const UnknownType::ID UnknownType::ids[] = {
	{ L"PTR_NOGC", Size::sPtr, false },
	{ L"PTR_GC", Size::sPtr, true },
	{ L"INT", Size::sInt, false },
	{ null, Size::sInt, false },
};

UnknownType::UnknownType(const CppName &kind, Auto<TypeRef> of) : TypeRef(of->pos), of(of), id(null) {
	for (nat i = 0; ids[i].name; i++) {
		if (kind == ids[i].name) {
			id = &ids[i];
			break;
		}
	}

	if (!id)
		alias = new NamedType(of->pos, kind);
}

Size UnknownType::size() const {
	if (id)
		return id->size;
	else
		return alias->size();
}

bool UnknownType::gcType() const {
	if (id)
		return id->gc;
	else
		return alias->gcType();
}

Auto<TypeRef> UnknownType::resolve(World &in, const CppName &ctx) const {
	if (id) {
		return new UnknownType(*this);
	} else if (alias) {
		Auto<TypeRef> r = alias->resolve(in, ctx);
		UnknownType *u = new UnknownType(*this);
		u->alias = r;
		return u;
	} else {
		throw Error(L"Invalid UNKNOWN() declaration.", of->pos);
	}
}

Auto<TypeRef> UnknownType::wrapper(World &in) const {
	if (!id)
		return alias;
	else
		return new ResolvedType(*this, in.unknown(id->name, pos));
}

void UnknownType::print(wostream &to) const {
	if (id) {
		to << L"UNKNOWN(" << id->name << L") " << of;
	} else {
		to << L"UNKNOWN(" << alias << L") " << of;
	}
}

size_t UnknownType::sortId() const {
	return alias->sortId();
}