File: Scope.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 (315 lines) | stat: -rw-r--r-- 7,089 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
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
#include "stdafx.h"
#include "Scope.h"
#include "Name.h"
#include "Named.h"
#include "Package.h"
#include "Type.h"
#include "Engine.h"
#include "Exception.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"

namespace storm {

	Named *find(Scope scope, NameLookup *root, SimpleName *name) {
		if (name->empty())
			return as<Named>(root);

		Named *at = root->find(name->at(0), scope);
		for (nat i = 1; at != null && i < name->count(); i++) {
			at = at->find(name->at(i), scope);
		}

		return at;
	}

	/**
	 * ScopeLookup.
	 */

	ScopeLookup::ScopeLookup() {}

	ScopeLookup::ScopeLookup(Str *v) : voidName(v) {}

	ScopeLookup::ScopeLookup(const wchar *s) {
		voidName = new (this) Str(s);
	}

	ScopeLookup *ScopeLookup::clone() const {
		return new (this) ScopeLookup(voidName);
	}

	Package *ScopeLookup::firstPkg(NameLookup *l) {
		NameLookup *now = l;
		while (!as<Package>(now)) {
			now = now->parent();
			if (!now)
				throw new (l) InternalError(TO_S(l, S("Unable to find a package when traversing parents of ") << l));
		}
		return as<Package>(now);
	}

	Package *ScopeLookup::rootPkg(Package *p) {
		return p->engine().package();

		// NameLookup *at = p;
		// while (at->parent())
		// 	at = at->parent();

		// assert(as<Package>(at), L"Detached NameLookup found!");
		// return as<Package>(at);
	}

	Package *ScopeLookup::corePkg(NameLookup *l) {
		return l->engine().corePackage();

		// Package *top = firstPkg(l);
		// Package *root = rootPkg(top);
		// SimplePart *core = new (l) SimplePart(new (l) Str(L"core"));
		// return as<Package>(root->find(core, Scope(root)));
	}

	NameLookup *ScopeLookup::nextCandidate(NameLookup *prev) {
		if (Package *pkg = as<Package>(prev)) {
			// Examine the root next.
			Package *root = rootPkg(pkg);
			if (pkg == root)
				return null;
			else
				return root;
		} else {
			// Default behaviour is to look at the nearest parent.
			return prev->parent();
		}
	}

	Named *ScopeLookup::find(Scope in, SimpleName *name) {
		// Regular path.
		for (NameLookup *at = in.top; at; at = nextCandidate(at)) {
			if (Named *found = storm::find(in, at, name))
				return found;
		}

		// Core.
		// TODO: We might not want to lookup inside 'core' always?
		if (Package *core = corePkg(in.top))
			if (Named *found = storm::find(in, core, name))
				return found;

		// We failed!
		return null;
	}

	Value ScopeLookup::value(Scope in, SimpleName *name, SrcPos pos) {
		// TODO: We may want to consider type aliases in the future, and implement 'void' that way.
		if (voidName != null && name->count() == 1) {
			SimplePart *last = name->last();
			if (*last->name == *voidName && last->params->empty())
				return Value();
		}

		try {
			Type *t = as<Type>(find(in, name));
			if (!t) {
				throw new (this) SyntaxError(pos, TO_S(engine(), name << S(" can not be resolved to a type.")));
			}

			return Value(t);
		} catch (CodeError *error) {
			if (error->pos.empty())
				error->pos = pos;
			throw;
		}
	}


	/**
	 * Scope.
	 */

	Scope::Scope() : top(null), lookup(null) {}

	Scope::Scope(NameLookup *top) : top(top) {
		assert(top);
		lookup = top->engine().scopeLookup();
	}

	Scope::Scope(NameLookup *top, ScopeLookup *lookup) : top(top), lookup(lookup) {}

	Scope::Scope(Scope parent, NameLookup *top) : top(top), lookup(parent.lookup) {}

	void Scope::deepCopy(CloneEnv *env) {
		// Should be OK to not do anything here... All our members are threaded objects.
	}

	Named *Scope::find(Name *name) const {
		try {
			SimpleName *simple = name->simplify(*this);
			if (simple)
				return find(simple);

		} catch (CodeError *error) {
			if (error->pos.empty()) {
				if (SrcName *s = as<SrcName>(name))
					error->pos = s->pos;
			}
			throw;
		}

		return null;
	}

	Named *Scope::find(SimpleName *name) const {
		if (lookup)
			return lookup->find(*this, name);

		return null;
	}

	Named *Scope::find(const wchar *name, Array<Value> *params) const {
		return find(new (params) SimpleName(new (params) Str(name), params));
	}

	Value Scope::value(Name *name, SrcPos pos) const {
		if (lookup) {
			SimpleName *simple = name->simplify(*this);
			if (simple)
				return lookup->value(*this, simple, pos);
		}

		throw new (name) SyntaxError(pos, TO_S(name, name << S(" can not be resolved to a type.")));
	}

	Value Scope::value(SrcName *name) const {
		return value(name, name->pos);
	}

	Scope Scope::withPos(SrcPos pos) const {
		if (!top)
			return *this;

		LookupPos *sub = new (top) LookupPos(pos);
		sub->parentLookup(top);
		return child(sub);
	}

	Scope rootScope(EnginePtr e) {
		return e.v.scope();
	}

	wostream &operator <<(wostream &to, const Scope &s) {
		if (!s.lookup) {
			to << L"<empty>";
		} else {
			to << L"<using ";
			to << s.lookup->type()->identifier();
			to << L"> ";

			bool first = true;
			for (NameLookup *at = s.top; at; at = ScopeLookup::nextCandidate(at)) {
				if (!first)
					to << L" -> ";
				first = false;

				if (Named *n = as<Named>(at)) {
					to << n->identifier();
				} else if (LookupPos *p = as<LookupPos>(at)) {
					to << p->pos;
				} else {
					to << at->type()->identifier();
				}
			}
		}

		return to;
	}

	void Scope::toS(StrBuf *to) const {
		if (!lookup) {
			*to << S("<empty>");
		} else {
			*to << S("<using ");
			*to << lookup->type()->identifier();
			*to << S("> ");

			bool first = true;
			for (NameLookup *at = top; at; at = ScopeLookup::nextCandidate(at)) {
				if (!first)
					*to << S(" -> ");
				first = false;

				if (Named *n = as<Named>(at)) {
					*to << n->identifier();
				} else if (LookupPos *p = as<LookupPos>(at)) {
					*to << p->pos;
				} else {
					*to << at->type()->identifier();
				}
			}
		}
	}

	MAYBE(NameLookup *) findTopmost(Scope scope, Type *type) {
		if (!scope.top)
			return null;
		return findTopmost(scope.top, type);
	}

	/**
	 * ScopeExtra.
	 */

	ScopeExtra::ScopeExtra() {
		search = new (this) Array<NameLookup *>();
		inSearch = new (this) Set<TObject *>();
	}

	ScopeExtra::ScopeExtra(Str *v) : ScopeLookup(v) {
		search = new (this) Array<NameLookup *>();
		inSearch = new (this) Set<TObject *>();
	}

	ScopeLookup *ScopeExtra::clone() const {
		ScopeExtra *copy = new (this) ScopeExtra();
		copy->search->append(search);
		copy->inSearch = new (this) Set<TObject *>(*inSearch);
		return copy;
	}

	void ScopeExtra::addExtra(NameLookup *lookup) {
		addExtra(lookup, true);
	}

	void ScopeExtra::addExtra(NameLookup *lookup, Bool useExports) {
		if (inSearch->has(lookup))
			return;

		inSearch->put(lookup);
		search->push(lookup);

		if (useExports) {
			if (Package *p = as<Package>(lookup)) {
				Array<Package *> *add = p->exports();
				for (Nat i = 0; i < add->count(); i++)
					addExtra(add->at(i), true);
			}
		}
	}

	Array<NameLookup *> *ScopeExtra::extra() const {
		return new (this) Array<NameLookup *>(*search);
	}

	Named *ScopeExtra::find(Scope in, SimpleName *name) {
		if (Named *found = ScopeLookup::find(in, name))
			return found;

		for (nat i = 0; i < search->count(); i++) {
			if (Named *found = storm::find(in, search->at(i), name))
				return found;
		}

		return null;
	}

}