File: Named.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 (260 lines) | stat: -rw-r--r-- 6,195 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
#include "stdafx.h"
#include "Named.h"
#include "Engine.h"
#include "Core/Str.h"
#include "Core/StrBuf.h"
#include "Name.h"
#include "Exception.h"

namespace storm {

	/**
	 * NameLookup.
	 */

	NameLookup::NameLookup() : pLookup(null) {}

	NameLookup::NameLookup(NameLookup *parent) : pLookup(parent) {}

	Bool NameLookup::hasParent(MAYBE(NameLookup *) parent) const {
		for (const NameLookup *at = this; at; at = at->parent())
			if (at == parent)
				return true;
		return false;
	}

	NameLookup *NameLookup::parent() const {
		if (!pLookup) {
			Str *msg = new (this) Str(S("No parent for this NameLookup node yet."));
			throw new (this) LookupError(msg);
		}
		return pLookup;
	}

	Named *NameLookup::find(SimplePart *part, Scope source) {
		return null;
	}

	Named *NameLookup::find(Str *name, Array<Value> *params, Scope source) {
		return find(new (this) SimplePart(name, params), source);
	}

	Named *NameLookup::find(Str *name, Value param, Scope source) {
		return find(new (this) SimplePart(name, param), source);
	}

	Named *NameLookup::find(Str *name, Scope source) {
		return find(new (this) SimplePart(name), source);
	}


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

	Named *NameLookup::find(const wchar *name, Value param, Scope source) {
		return find(new (this) Str(name), param, source);
	}

	Named *NameLookup::find(const wchar *name, Scope source) {
		return find(new (this) Str(name), source);
	}

	void NameLookup::parentLookup(MAYBE(NameLookup *) parent) {
		pLookup = parent;
		onNewParent(parent, false);
	}

	void NameLookup::onNewParent(MAYBE(NameLookup *) parent, Bool added) {
		// Nothing to do here.
	}

	void NameLookup::makeChild(NameLookup *other) {
		other->pLookup = this;
		other->onNewParent(this, true);
	}

	MAYBE(NameLookup *) findTopmost(NameLookup *at, Type *type) {
		while (at) {
			if (runtime::isA(at, type))
				return at;
			at = at->parent();
		}
		return null;
	}


	/**
	 * LookupPos.
	 */

	LookupPos::LookupPos() {}

	LookupPos::LookupPos(SrcPos pos) : pos(pos) {}

	LookupPos::LookupPos(MAYBE(NameLookup *) parent, SrcPos pos) : NameLookup(parent), pos(pos) {}


	/**
	 * Named.
	 */

	Named::Named(Str *name) : name(name), flags(namedDefault), visibility(null) {
		if (engine().has(bootTemplates)) {
			params = new (this) Array<Value>();
		}
	}

	Named::Named(Str *name, Array<Value> *params) : name(name), params(params), flags(namedDefault), visibility(null) {}

	Named::Named(SrcPos pos, Str *name) : LookupPos(pos), name(name), flags(namedDefault), visibility(null) {
		if (engine().has(bootTemplates)) {
			params = new (this) Array<Value>();
		}
	}

	Named::Named(SrcPos pos, Str *name, Array<Value> *params) : LookupPos(pos), name(name), params(params), flags(namedDefault), visibility(null) {}

	void Named::lateInit() {
		if (!params)
			params = new (this) Array<Value>();
	}

	NameLookup *Named::parent() const {
		NameLookup *p = parentLookup();
		if (!p) {
			StrBuf *msg = new (this) StrBuf();
			*msg << S("No parent for ") << identifier() << S(" yet.");
			throw new (this) LookupError(msg->toS());
		}
		return p;
	}

	Named *Named::closestNamed() const {
		for (NameLookup *p = parent(); p; p = p->parent()) {
			if (Named *n = as<Named>(p))
				return n;
		}
		return null;
	}

	SimpleName *Named::path() const {
		if (Named *parent = closestNamed()) {
			SimpleName *p = parent->path();
			p->add(new (this) SimplePart(name, params));
			return p;
		} else {
			return new (this) SimpleName();
		}
	}

	Named *Named::safeClosestNamed() const {
		for (NameLookup *p = parentLookup(); p; p = p->parentLookup()) {
			if (Named *n = as<Named>(p))
				return n;
		}
		return null;
	}

	SimpleName *Named::safePath() const {
		if (Named *parent = safeClosestNamed()) {
			SimpleName *p = parent->safePath();
			p->add(new (this) SimplePart(name, params));
			return p;
		} else {
			return new (this) SimpleName();
		}
	}

	Str *Named::identifier() const {
		// We want to be quite robust here, as 'identifier' is commonly used in error reporting. If
		// we throw an exception here, we will most likely just give a bad error message to the user.
		if (parentLookup() == null)
			return shortIdentifier();

		return safePath()->toS();
	}

	Str *Named::shortIdentifier() const {
		StrBuf *out = new (this) StrBuf();
		*out << name;
		if (params != null && params->count() > 0) {
			*out << L"(" << params->at(0);
			for (nat i = 1; i < params->count(); i++)
				*out << L", " << params->at(i);
			*out << L")";
		}
		return out->toS();
	}

	void Named::notifyAdded(NameSet *to, Named *added) {}

	void Named::notifyRemoved(NameSet *to, Named *added) {}

	void Named::compile() {}

	void Named::findEquivalentTypes(Named *old, ReplaceContext *ctx) {}

	MAYBE(Str *) Named::canReplace(Named *, ReplaceContext *) {
		StrBuf *msg = new (this) StrBuf();
		*msg << S("Reloading not implemented for entities of type ")
			 << runtime::typeOf(this)->identifier();
		return msg->toS();
	}

	void Named::replace(Named *old, ReplaceTasks *tasks, ReplaceContext *ctx) {
		if (Str *error = canReplace(old, ctx))
			throw new (this) ReplaceError(pos, error);

		doReplace(old, tasks, ctx);
	}

	void Named::automaticReplace(Named *) {
		// Nothing.
	}

	void Named::doReplace(Named *, ReplaceTasks *, ReplaceContext *) {
		// Nothing.
	}

	void Named::discardSource() {}

	void Named::toS(StrBuf *buf) const {
		*buf << identifier() << S(" [");
		putVisibility(buf);
		*buf << S("]");
	}

	void Named::putVisibility(StrBuf *to) const {
		if (visibility)
			*to << visibility;
		else
			*to << S("public (unset)");
	}

	Bool Named::visibleFrom(Scope source) {
		return visibleFrom(source.top);
	}

	Bool Named::visibleFrom(MAYBE(NameLookup *) source) {
		if (visibility && source)
			return visibility->visible(this, source);

		// If either 'visibility' or 'source' is null, we default to 'public'.
		return true;
	}

	Doc *Named::findDoc() {
		if (documentation)
			return documentation->get();
		else
			return doc(this);
	}

	void Named::onNewParent(MAYBE(NameLookup *) parent, Bool added) {
		if (!added) {
			discardSource();
		}
	}

}