File: GlobalVar.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (57 lines) | stat: -rw-r--r-- 1,807 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
#include "stdafx.h"
#include "GlobalVar.h"
#include "Scope.h"
#include "VariableInitializer.h"
#include "Compiler/Variable.h"
#include "Compiler/NamedThread.h"
#include "Compiler/Exception.h"
#include "Compiler/Lib/Fn.h"

namespace storm {
	namespace bs {

		GlobalVarDecl::GlobalVarDecl(Scope scope, SrcName *type, syntax::SStr *name, SrcName *thread) :
			scope(scope), type(type), name(name), thread(thread), initExpr(null) {}

		void GlobalVarDecl::init(syntax::Node *initTo) {
			this->initExpr = initTo;
		}

		void GlobalVarDecl::toS(StrBuf *to) const {
			*to << type << S(" ") << name->v << S(" on ") << thread;
		}

		Named *GlobalVarDecl::doCreate() {
			Scope fScope = fileScope(scope, name->pos);

			Value type = fScope.value(this->type);
			NamedThread *thread = as<NamedThread>(fScope.find(this->thread));
			if (!thread) {
				Str *msg = TO_S(engine(), S("The name ") << this->thread << S(" does not refer to a named thread."));
				throw new (this) SyntaxError(this->thread->pos, msg);
			}

			Function *init = createInitializer(type, scope, thread);
			GlobalVar *var = new (this) GlobalVar(name->pos, name->v, type, thread, pointer(init));
			var->pos = name->pos;
			return var;
		}

		void GlobalVarDecl::doResolve(Named *entity) {
			if (GlobalVar *v = as<GlobalVar>(entity)) {
				v->create();
			}
		}

		Function *GlobalVarDecl::createInitializer(Value type, Scope scope, NamedThread *thread) {
			VariableInitializer *r = new (this) VariableInitializer(this->type->pos, type, scope, initExpr);
			// Trick the function into believing that it has a proper name (just like
			// lambdas). Necessary in order to be able to call 'runOn', which is done when we create
			// function pointers to the function.
			r->parentLookup(scope.top);
			r->runOn(thread);
			return r;
		}

	}
}