File: Variable.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 (269 lines) | stat: -rw-r--r-- 7,429 bytes parent folder | download
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
#include "stdafx.h"
#include "Variable.h"
#include "Type.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"
#include "Lib/Fn.h"
#include "Exception.h"
#include "Engine.h"

namespace storm {

	static void checkType(Engine &e, Value type, const SrcPos &pos) {
		if (!type.type)
			throw new (e) TypedefError(pos, S("Unable to create a variable of type 'void'."));
	}

	Variable::Variable(SrcPos pos, Str *name, Value type) :
		Named(name), type(type.asRef(false)) {
		this->pos = pos;

		checkType(engine(), type, pos);
	}

	Variable::Variable(SrcPos pos, Str *name, Value type, Type *member) :
		Named(name, new (name) Array<Value>(1, thisPtr(member))), type(type) {
		this->pos = pos;

		checkType(engine(), type, pos);
	}


	void Variable::toS(StrBuf *to) const {
		*to << type << S(" ");
		Named::toS(to);
	}


	/**
	 * Member variable.
	 */

	MemberVar::MemberVar(Str *name, Value type, Type *member) : Variable(SrcPos(), name, type, member) {
		ref = new (this) NamedOffsetSource(this, Char('o'));
		hasLayout = false;
	}

	MemberVar::MemberVar(SrcPos pos, Str *name, Value type, Type *member) : Variable(pos, name, type, member) {
		ref = new (this) NamedOffsetSource(this, Char('o'));
		hasLayout = false;
	}

	code::OffsetRef MemberVar::offset() const {
		return code::OffsetRef(ref);
	}

	Offset MemberVar::rawOffset() const {
		if (!hasLayout) {
			owner()->doLayout();
		}
		return off;
	}

	void MemberVar::setOffset(Offset to) {
		hasLayout = true;
		off = to;
		ref->set(to);
	}

	Type *MemberVar::owner() const {
		assert(!params->empty());
		return params->at(0).type;
	}

	void MemberVar::initializer(MAYBE(Function *) f) {
		initFn = f;
		f->make(fnStatic);
		makeChild(f);
	}

	void MemberVar::discardSource() {
		if (initFn)
			initFn->discardSource();
	}

	MAYBE(Str *) MemberVar::canReplace(Named *old, ReplaceContext *ctx) {
		MemberVar *o = as<MemberVar>(old);
		if (!o)
			return new (this) Str(S("Cannot replace a non-member variable with something else."));

		if (!ctx->normalize(type).mayStore(ctx->normalize(type)))
			return new (this) Str(S("Can not change the type of a member variable to something non-compatible."));

		if (initFn && o->initFn) {
			if (Str *error = initFn->canReplace(o->initFn, ctx))
				return error;
		} else if (!initFn && o->initFn) {
			return new (this) Str(S("Can not remove an initializer from a variable."));
		}

		return null;
	}

	void MemberVar::doReplace(Named *old, ReplaceTasks *tasks, ReplaceContext *context) {
		MemberVar *o = (MemberVar *)old;
		hasLayout = o->hasLayout;
		off = o->off;
		ref->set(off);
		ref->steal(o->ref);

		if (initFn && o->initFn)
			initFn->replace(o->initFn, tasks, context);
		initFn = o->initFn;
		if (initFn)
			makeChild(initFn);
	}


	/**
	 * Global variable.
	 */

	GlobalVar::GlobalVar(Str *name, Value type, NamedThread *thread, FnBase *initializer) :
		Variable(SrcPos(), name, type), owner(thread), initializer(initializer), hasArray(false) {

		FnType *fnType = as<FnType>(runtime::typeOf(initializer));
		if (!fnType)
			throw new (this) UsageError(S("Invalid type of the initializer passed to GlobalVar. Must be a function pointer."));
		if (fnType->params->count() != 1)
			throw new (this) UsageError(S("An initializer provided to GlobalVar may not take parameters."));

		hasArray = type.isValue();
	}

	GlobalVar::GlobalVar(SrcPos pos, Str *name, Value type, NamedThread *thread, FnBase *initializer) :
		Variable(pos, name, type), owner(thread), initializer(initializer), hasArray(false) {

		FnType *fnType = as<FnType>(runtime::typeOf(initializer));
		if (!fnType)
			throw new (this) UsageError(S("Invalid type of the initializer passed to GlobalVar. Must be a function pointer."));
		if (fnType->params->count() != 1)
			throw new (this) UsageError(S("An initializer provided to GlobalVar may not take parameters."));

		hasArray = type.isValue();
	}

	Bool GlobalVar::accessibleFrom(RunOn thread) {
		return thread.canRun(RunOn(owner));
	}

	void GlobalVar::create() {
		// Already created?
		if (!initializer)
			return;

		FnType *fnType = as<FnType>(runtime::typeOf(initializer));

		// Find the 'call' function so that we may call that.
		Function *callFn = as<Function>(fnType->find(S("call"), thisPtr(fnType), engine().scope()));
		if (!callFn)
			throw new (this) InternalError(S("Can not find 'call()' in the provided function pointer. Is the signature correct?"));

		// Check the return type.
		if (!type.mayReferTo(callFn->result)) {
			Str *msg = TO_S(this, S("The global variable ") << name
							<< S(" can not store the type returned from the initializer. Expected ")
							<< type << S(", got ") << callFn->result << S("."));
			throw new (this) UsageError(msg);
		}

		void *outPtr = &data;
		if (hasArray) {
			// Create the storage for the data.
			GcArray<byte> *d = (GcArray<byte> *)runtime::allocArray(engine(), type.type->gcArrayType(), 1);
			// Set 'filled' to be nice to other parts of the system.
			d->filled = 1;
			data = d;
			outPtr = d->v;
		}

		// Call the function using its thunk (additional parameter just in case).
		void *params[2] = { &initializer, null };
		os::FnCallRaw call(params, callFn->callThunk());

		// Call on the specified thread.
		os::Thread initOn = owner->thread()->thread();
		if (initOn == os::Thread::current()) {
			// No thread call needed.
			call.callRaw(callFn->ref().address(), true, null, outPtr);
		} else {
			// Call on the specified thread.
			os::FutureBase future;
			os::UThread::spawnRaw(callFn->ref().address(), true, null, call, future, outPtr, &initOn);
			future.result(&updateFutureExceptions, null);
		}

		// Mark ourselves as initialized.
		initializer = null;
	}

	void GlobalVar::compile() {
		create();
	}

	void *GlobalVar::dataPtr() {
		// Make sure it is created.
		rawDataPtr();

		if (hasArray) {
			GcArray<byte> *a = (GcArray<byte> *)data;
			return a->v;
		} else {
			return &data;
		}
	}

	void *GlobalVar::rawDataPtr() {
		if (atomicRead(initializer) != null) {
			// We might need to initialize! Call 'create' on the proper thread to do that. Note:
			// 'create' will not initialize us again if initialization was done already, so we will
			// be safe even if two threads happen to call 'dataPtr' for the first time more or less
			// simultaneously.
			const os::Thread &t = TObject::associatedThread()->thread();
			if (t != os::Thread::current()) {
				GlobalVar *me = this;
				os::Future<void> f(os::FutureMode::exclusive);
				os::FnCall<void, 1> p = os::fnCall().add(me);
				os::UThread::spawn(address(&GlobalVar::create), true, p, f, &t);
				f.result(&updateFutureExceptions, null);
			} else {
				create();
			}
		}

		return data;
	}

	Str *GlobalVar::strValue() const {
		const Handle &h = type.type->handle();
		if (h.toSFn) {
			StrBuf *out = new (this) StrBuf();
			void *ptr = const_cast<GlobalVar *>(this)->dataPtr();
			(*h.toSFn)(ptr, out);
			return out->toS();
		} else {
			return new (this) Str(S("<no string representation>"));
		}
	}

	Variant GlobalVar::value() {
		if (hasArray) {
			return Variant(dataPtr(), type.type);
		} else {
			return Variant(*(RootObject **)dataPtr());
		}
	}

	void GlobalVar::toS(StrBuf *to) const {
		Variable::toS(to);
		*to << S(" on ") << owner->identifier();
		const Handle &h = type.type->handle();
		if (h.toSFn) {
			*to << S(" = ");
			// Sorry...
			void *ptr = const_cast<GlobalVar *>(this)->dataPtr();
			(*h.toSFn)(ptr, to);
		}
	}

}