File: Class.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 (426 lines) | stat: -rw-r--r-- 11,705 bytes parent folder | download | duplicates (2)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "stdafx.h"
#include "Class.h"
#include "Compiler/Exception.h"
#include "Compiler/TypeCtor.h"
#include "Compiler/TypeDtor.h"
#include "Compiler/Engine.h"
#include "Function.h"
#include "Ctor.h"
#include "Access.h"
#include "Doc.h"
#include "VariableInitializer.h"

namespace storm {
	namespace bs {

		Class *createClass(SrcPos pos, Scope env, syntax::SStr *name, syntax::Node *body) {
			return new (name) Class(typeClass, pos, env, name->v, body);
		}

		Class *createValue(SrcPos pos, Scope env, syntax::SStr *name, syntax::Node *body) {
			return new (name) Class(typeValue, pos, env, name->v, body);
		}


		Class::Class(TypeFlags flags, SrcPos pos, Scope scope, Str *name, syntax::Node *body)
			: Type(name, flags), scope(scope, this), body(body),
			  superName(null), threadName(null), threadMeaning(threadNone), allowLazyLoad(true) {

			this->pos = pos;
		}


		void Class::lookupTypes() {
			allowLazyLoad = false;

			// Lookup super-types and threads relative to the *parent* of the current scope,
			// otherwise we resolve them relative to the body of the class we are defining, which
			// does not make sense. It also unneccessary triggers the 'loadAll' in the parent in
			// case we have both a super-class and a thread set (e.g. when one does 'on Compiler:'
			// globally in the file). This could make loading happen in an incorrect order, and
			// cause issues that are difficult to track down.
			Scope scope = this->scope;
			if (scope.top)
				scope.top = scope.top->parent();

			try {
				// Set the super class first, this makes 'setThread' return true or false as appropriate:
				if (superName) {
					Type *t = as<Type>(scope.find(superName));
					if (!t) {
						Str *msg = TO_S(engine, S("Can not find the super class ") << superName << S("."));
						throw new (this) SyntaxError(superName->pos, msg);
					}
					setSuper(t);
				}

				if (threadMeaning != threadNone) {
					if (threadName == null) {
						if (superName) {
							throw new (this) SyntaxError(
								superName->pos,
								S("Can not use 'on ?' together with the 'extends' keyword."));
						}

						setSuper(TObject::stormType(engine));
					} else {
						NamedThread *t = as<NamedThread>(scope.find(threadName));
						if (!t) {
							Str *msg = TO_S(engine, S("Can not find the named thread ") << threadName << S("."));
							throw new (this) SyntaxError(threadName->pos, msg);
						}

						if (!setThread(t) && threadMeaning != threadDefault) {
							Str *msg = TO_S(engine, S("Failed to apply the 'on' keyword for this class since ")
											S("it inherits from a class type, or from an actor that is ")
											S("already bound to a thread."));
							throw new (this) SyntaxError(threadName->pos, msg);
						}
					}
				}

				threadName = null;
				threadMeaning = threadNone;
				superName = null;
			} catch (...) {
				allowLazyLoad = true;
				throw;
			}
			allowLazyLoad = true;
		}

		void Class::super(SrcName *super) {
			if (superName)
				throw new (this) SyntaxError(super->pos, S("Only one instance of 'extends' may be used for a single type. ")
											S("Multiple inheritance is not supported."));

			superName = super;
		}

		void Class::thread(SrcName *thread) {
			if (threadMeaning != threadNone && threadMeaning != threadDefault)
				throw new (this) SyntaxError(thread->pos, S("The 'on' keyword may only be used once."));

			threadName = thread;
			threadMeaning = threadExplicit;
		}

		void Class::unknownThread(SrcPos pos) {
			if (threadMeaning != threadNone && threadMeaning != threadDefault)
				throw new (this) SyntaxError(pos, S("The 'on' keyword may only be used once."));

			threadName = null;
			threadMeaning = threadExplicit;
		}

		void Class::defaultThread(SrcName *thread) {
			// Just ignore the default if it is already set!
			if (threadMeaning != threadNone)
				return;

			threadName = thread;
			threadMeaning = threadDefault;
		}

		void Class::decorate(SrcName *decorator) {
			if (!decorators)
				decorators = new (this) Array<SrcName *>();

			decorators->push(decorator);
		}

		void Class::makeFinal() {
			addTypeFlag(typeFinal);
		}

		Class::AddState::AddState() : ctor(false), copyCtor(false), deepCopy(false), assign(false) {}

		bool Class::loadAll() {
			if (!allowLazyLoad)
				return false;

			// Make sure that 'lookupTypes' was called. This might not be true in case some type
			// refers to this type inside its super type or something like that. It is fine to do
			// this, since 'lookupTypes' is a no-op if it was called already.
			lookupTypes();

			ClassBody *body = syntax::transformNode<ClassBody, Class *>(this->body, this);
			body->prepareItems();

			AddState added;

			// Add the named objects first.
			for (Nat i = 0; i < body->items->count(); i++) {
				addMember(body->items->at(i), added);
			}

			// Poke the classes to tell them they can load their super types now.
			for (Nat i = 0; i < body->items->count(); i++) {
				if (Class *c = as<Class>(body->items->at(i)))
					c->lookupTypes();
			}

			body->prepareWraps();

			// Add the wrapped items.
			for (Nat i = 0; i < body->wraps->count(); i++) {
				addMember(body->wraps->at(i)->transform(this), added);
			}


			// Add default members as required.
			if (needsDestructor(this))
				add(new (engine) TypeDefaultDtor(this));

			if (!added.ctor)
				add(classDefaultCtor(this));

			if (!added.copyCtor && runOn().state == RunOn::any)
				add(new (engine) TypeCopyCtor(this));

			if (!added.deepCopy && runOn().state == RunOn::any)
				add(new (engine) TypeDeepCopy(this));

			if ((typeFlags() & typeValue) && !added.assign)
				add(new (engine) TypeAssign(this));

			for (Nat i = 0; i < body->templates->count(); i++) {
				add(body->templates->at(i));
			}

			// Call any decorators.
			if (decorators) {
				for (Nat i = 0; i < decorators->count(); i++) {
					SrcName *name = decorators->at(i);
					Name *params = name->parent();
					params->add(new (this) SimplePart(name->last()->name, Value(Class::stormType(engine))));

					Function *found = as<Function>(scope.find(params));
					if (!found) {
						Str *msg = TO_S(engine, S("Could not find a decorator named ")
										<< name << S(" in the current scope."));
						throw new (this) SyntaxError(name->pos, msg);
					}

					if (found->result != Value())
						throw new (this) SyntaxError(name->pos, S("Decorators may not return a value."));

					// Call the function...
					typedef void (*Fn)(Class *);
					Fn fn = (Fn)found->pointer();
					(*fn)(this);
				}
			}

			// Done loading!
			body->finished();

			// We do not need the syntax tree anymore!
			this->body = null;

			// Super needs to be called!
			return Type::loadAll();
		}

		void Class::addMember(Named *item, AddState &state) {
			if (*item->name == Type::CTOR) {
				if (item->params->count() == 2 && item->params->at(0) == item->params->at(1))
					state.copyCtor = true;
				state.ctor = true;
			} else if (*item->name == S("deepCopy")) {
				if (item->params->count() == 2 && item->params->at(1).type == CloneEnv::stormType(this))
					state.deepCopy = true;
			} else if (*item->name == S("=")) {
				if (item->params->count() == 2 && item->params->at(1).type == this)
					state.assign = true;
			}

			add(item);
		}


		/**
		 * Wrap.
		 */

		MemberWrap::MemberWrap(syntax::Node *node) : node(node), visibility(null) {}

		Named *MemberWrap::transform(Class *owner) {
			Named *n = syntax::transformNode<Named, Class *>(node, owner);
			if (visibility)
				apply(node->pos, n, visibility);
			if (docPos.any())
				applyDoc(docPos, n);
			return n;
		}


		/**
		 * Body.
		 */

		ClassBody::ClassBody(Class *owner) : owner(owner) {
			items = new (this) Array<Named *>();
			wraps = new (this) Array<MemberWrap *>();
			templates = new (this) Array<Template *>();

			// TODO: Default to 'private' instead?
			defaultVisibility = engine().visibility(Engine::vPublic);
		}

		void ClassBody::add(Named *i) {
			if (!i->visibility)
				i->visibility = defaultVisibility;
			items->push(i);
		}

		void ClassBody::add(MemberWrap *w) {
			if (!w->visibility)
				w->visibility = defaultVisibility;
			wraps->push(w);
		}

		void ClassBody::add(Template *t) {
			templates->push(t);
		}

		void ClassBody::add(Visibility *v) {
			defaultVisibility = v;
		}

		void ClassBody::add(TObject *o) {
			if (Named *n = as<Named>(o)) {
				add(n);
			} else if (MemberWrap *w = as<MemberWrap>(o)) {
				add(w);
			} else if (Template *t = as<Template>(o)) {
				add(t);
			} else if (Visibility *v = as<Visibility>(o)) {
				add(v);
			} else {
				Str *msg = TO_S(this, S("Not a suitable type to ClassBody.add(): ")
								<< runtime::typeOf(o)->identifier());
				throw new (this) InternalError(msg);
			}
		}

		void ClassBody::prepareItems() {}

		void ClassBody::prepareWraps() {}

		void ClassBody::finished() {}

		/**
		 * Members.
		 */

		MemberVar *classVar(Class *owner, SrcName *type, syntax::SStr *name) {
			return classVar(owner, type, name, null);
		}

		MemberVar *classVar(Class *owner, SrcName *type, syntax::SStr *name, MAYBE(syntax::Node *) init) {
			Value t = owner->scope.value(type);
			MemberVar *var = new (owner) MemberVar(name->pos, name->v, t, owner);

			if (init) {
				var->initializer(new (owner) VariableInitializer(name->pos, t, owner->scope, init));
			}

			return var;
		}


		BSFunction *classFn(Class *owner,
							SrcPos pos,
							syntax::SStr *name,
							Name *result,
							Array<NameParam> *params,
							syntax::Node *body) {

			BSFunction *fn = new (owner) BSFunction(owner->scope.value(result, pos),
													name,
													resolve(params, owner, owner->scope),
													owner->scope,
													null, // thread
													body);
			fn->pos = pos;
			return fn;
		}

		Function *classAbstractFn(Class *owner,
								SrcPos pos,
								syntax::SStr *name,
								Name *result,
								Array<NameParam> *params,
								syntax::Node *options) {

			BSRawFn *f = new (owner) BSAbstractFn(owner->scope.value(result, pos),
												name,
												resolve(params, owner, owner->scope));
			f->pos = pos;

			syntax::transformNode<void, Class *, BSRawFn *>(options, owner, f);

			if (f->fnFlags() & fnAbstract)
				return f;

			throw new (owner) SyntaxError(pos, S("A function without implementation must be marked using ': abstract'."));
		}

		BSFunction *classAssign(Class *owner,
								SrcPos pos,
								syntax::SStr *name,
								Array<NameParam> *params,
								syntax::Node *body) {

			BSFunction *f = new (owner) BSFunction(Value(),
												name,
												resolve(params, owner, owner->scope),
												owner->scope,
												null, // thread
												body);
			f->pos = pos;
			f->make(fnAssign);
			return f;
		}

		BSCtor *classCtor(Class *owner,
						SrcPos pos,
						Array<NameParam> *params,
						syntax::Node *body) {

			return new (owner) BSCtor(resolve(params, owner, owner->scope),
									owner->scope,
									body,
									pos);
		}

		BSCtor *classCastCtor(Class *owner,
							SrcPos pos,
							Array<NameParam> *params,
							syntax::Node *body) {

			BSCtor *r = classCtor(owner, pos, params, body);
			r->makeAutoCast();
			return r;
		}


		BSCtor *classDefaultCtor(Class *owner) {
			Array<ValParam> *params = new (owner) Array<ValParam>();

			Bool needsThread = owner->runOn().state == RunOn::runtime;
			if (needsThread) {
				params->reserve(2);
				params->push(thisParam(owner));
				params->push(ValParam(StormInfo<Thread>::type(owner->engine), new (owner) Str()));
			} else {
				params->reserve(1);
				params->push(thisParam(owner));
			}

			return new (owner) BSCtor(params, owner->scope, null, owner->pos);
		}

	}
}