Skip to content
← Back

src/syntax/process/scopedvisitor.ghul

1
namespace Syntax.Process is
2
use IO.Std
3
4
5
use Logging
6
use Trees
7
8
class ScopedVisitor: ScopeVisitorBase abstract is
9
_logger: Logger
10
_symbol_table: Semantic.SYMBOL_TABLE
11
12
init(
13
logger: Logger,
14
symbol_table: Semantic.SYMBOL_TABLE,
15
namespaces: Semantic.NAMESPACES
16
)
17
is
18
super.init(
19
symbol_table,
20
namespaces
21
)
22
23
_logger = logger
24
_symbol_table = symbol_table
25
si
26
27
find(name: string) -> Semantic.Symbols.Symbol? => _symbol_table.current_scope.find_enclosing(name)
28
find_matches(prefix: string, matches: Collections.MutableMap[string,Semantic.Symbols.Symbol]) is
29
_symbol_table.current_scope.find_enclosing_matches(prefix, matches)
30
si
31
32
find(identifier: Identifiers.Identifier) -> Semantic.Symbols.Symbol? is
33
let result: Semantic.Symbols.Symbol? mut = null
34
35
if identifier.qualifier? then
36
let qualifier = find(identifier.qualifier)
37
38
if qualifier == null then
39
return null
40
fi
41
42
result = qualifier.find_member(identifier.name)
43
else
44
result =
45
_symbol_table
46
.current_scope
47
.find_enclosing(identifier.name)
48
fi
49
50
// A name the compiler synthesised carries an internal
51
// location, and a scope broken enough not to hold it - a
52
// file mixing globals with a namespace, say - has a real
53
// diagnostic of its own. Reporting a name nobody wrote on
54
// top of it points at nothing the author can fix.
55
if
56
result == null /\
57
!identifier.is_poisoned /\
58
!identifier.location.is_internal
59
then
60
_logger.error(identifier.location, "symbol not found: {identifier}")
61
fi
62
63
return result
64
si
65
66
// As find(identifier), but returns null silently instead of
67
// logging "symbol not found" — for callers that resolve a name
68
// speculatively and report their own diagnostic on failure.
69
try_find(identifier: Identifiers.Identifier) -> Semantic.Symbols.Symbol? is
70
if identifier.qualifier? then
71
let qualifier = try_find(identifier.qualifier)
72
73
if qualifier == null then
74
return null
75
fi
76
77
return qualifier.find_member(identifier.name)
78
fi
79
80
return
81
_symbol_table
82
.current_scope
83
.find_enclosing(identifier.name)
84
si
85
pre(`namespace: Definitions.NAMESPACE) -> bool is
86
enter_namespace(`namespace)
87
enter_uses(`namespace)
88
return false
89
si
90
91
visit(`namespace: Definitions.NAMESPACE) is
92
leave_uses(`namespace)
93
leave_namespace(`namespace)
94
si
95
96
pre(`class: Definitions.CLASS) -> bool is
97
enter_scope(`class)
98
return false
99
si
100
101
visit(`class: Definitions.CLASS) is
102
leave_scope(`class)
103
si
104
105
pre(`trait: Definitions.TRAIT) -> bool is
106
enter_scope(`trait)
107
return false
108
si
109
110
visit(`trait: Definitions.TRAIT) is
111
leave_scope(`trait)
112
si
113
114
pre(`struct: Definitions.STRUCT) -> bool is
115
enter_scope(`struct)
116
return false
117
si
118
119
pre(`union: Definitions.UNION) -> bool is
120
enter_scope(`union)
121
return false
122
si
123
124
visit(`union: Definitions.UNION) is
125
leave_scope(`union)
126
si
127
128
pre(variant: Definitions.VARIANT) -> bool is
129
enter_scope(variant)
130
return false
131
si
132
133
visit(variant: Definitions.VARIANT) is
134
leave_scope(variant)
135
si
136
137
visit(`struct: Definitions.STRUCT) is
138
leave_scope(`struct)
139
si
140
141
pre(`partial: Definitions.PARTIAL) -> bool is
142
enter_scope(`partial)
143
return false
144
si
145
146
visit(`partial: Definitions.PARTIAL) is
147
leave_scope(`partial)
148
si
149
150
pre(`impl: Definitions.IMPL) -> bool is
151
enter_scope(`impl)
152
return false
153
si
154
155
visit(`impl: Definitions.IMPL) is
156
leave_scope(`impl)
157
si
158
159
pre(`enum: Definitions.ENUM) -> bool is
160
enter_scope(`enum)
161
return false
162
si
163
164
visit(`enum: Definitions.ENUM) is
165
leave_scope(`enum)
166
si
167
168
pre(function: Definitions.FUNCTION) -> bool is
169
enter_scope(function)
170
return false
171
si
172
173
visit(function: Definitions.FUNCTION) is
174
leave_scope(function)
175
si
176
177
pre(property: Definitions.PROPERTY) -> bool => false
178
visit(property: Definitions.PROPERTY) is
179
si
180
181
pre(indexer: Definitions.INDEXER) -> bool is
182
// enter_scope(indexer);
183
return true
184
si
185
186
visit(indexer: Definitions.INDEXER) is
187
// leave_scope(indexer);
188
si
189
190
pre(block: Expressions.VAL_BLOCK) -> bool is
191
enter_scope(block)
192
return false
193
si
194
195
visit(block: Expressions.VAL_BLOCK) is
196
leave_scope(block)
197
si
198
199
pre(if_branch: Statements.IF_BRANCH) -> bool is
200
enter_scope(if_branch)
201
return false
202
si
203
204
visit(if_branch: Statements.IF_BRANCH) is
205
leave_scope(if_branch)
206
si
207
208
pre(`case: Statements.CASE) -> bool is
209
enter_scope(`case)
210
return false
211
si
212
213
visit(`case: Statements.CASE) is
214
leave_scope(`case)
215
si
216
217
pre(case_match: Statements.CASE_MATCH) -> bool is
218
enter_scope(case_match)
219
return false
220
si
221
222
visit(case_match: Statements.CASE_MATCH) is
223
leave_scope(case_match)
224
si
225
226
pre(`try: Statements.TRY) -> bool is
227
enter_scope(`try)
228
return false
229
si
230
231
// The try's scope holds the body's locals, so it closes where the
232
// body ends; visit only closes it for a walk that never reached
233
// the end of the body.
234
end_try_body(`try: Statements.TRY) is
235
leave_try_scope(`try)
236
si
237
238
visit(`try: Statements.TRY) is
239
leave_try_scope(`try)
240
si
241
242
leave_try_scope(`try: Statements.TRY) is
243
if `try.scope? /\ current_scope == `try.scope then
244
leave_scope(`try)
245
fi
246
si
247
248
pre(`catch: Statements.CATCH) -> bool is
249
enter_scope(`catch)
250
return false
251
si
252
253
visit(`catch: Statements.CATCH) is
254
leave_scope(`catch)
255
si
256
257
pre(`do: Statements.DO) -> bool is
258
enter_scope(`do)
259
return false
260
si
261
262
visit(`do: Statements.DO) is
263
leave_scope(`do)
264
si
265
266
pre(`for: Statements.FOR) -> bool is
267
enter_scope(`for)
268
return false
269
si
270
271
visit(`for: Statements.FOR) is
272
leave_scope(`for)
273
si
274
275
pre(function: Expressions.FUNCTION) -> bool is
276
enter_scope(function)
277
return false
278
si
279
280
visit(function: Expressions.FUNCTION) is
281
leave_scope(function)
282
si
283
284
pre(let_in: Expressions.LET_IN) -> bool is
285
enter_scope(let_in)
286
return false
287
si
288
289
visit(let_in: Expressions.LET_IN) is
290
leave_scope(let_in)
291
si
292
293
// A lazy comprehension's clauses and element are the body of the
294
// generator literal it owns.
295
pre(comprehension: Expressions.COMPREHENSION) -> bool is
296
if comprehension.is_lazy then
297
enter_scope(comprehension)
298
fi
299
300
return false
301
si
302
303
visit(comprehension: Expressions.COMPREHENSION) is
304
if comprehension.is_lazy then
305
leave_scope(comprehension)
306
fi
307
si
308
309
pre(clause: Expressions.COMPREHENSION_FOR) -> bool is
310
enter_scope(clause)
311
return false
312
si
313
314
visit(clause: Expressions.COMPREHENSION_FOR) is
315
leave_scope(clause)
316
si
317
318
pre(expression: Bodies.EXPRESSION) -> bool is
319
enter_scope(expression)
320
return false
321
si
322
323
visit(expression: Bodies.EXPRESSION) is
324
leave_scope(expression)
325
si
326
327
pre(block: Bodies.BLOCK) -> bool is
328
enter_scope(block)
329
return false
330
si
331
332
visit(block: Bodies.BLOCK) is
333
leave_scope(block)
334
si
335
si
336
si