Skip to content
← Back

src/semantic/symbol_table.ghul

1
namespace Semantic is
2
use IO.Std
3
4
use Logging
5
use Source
6
7
use Syntax.Trees
8
9
// the symbol table helps keeps track of what scope the compiler is currently working in. it does not
10
// manage searching for symbols within or across scopes: that's handled by the scopes themselves
11
12
class SYMBOL_TABLE is
13
_logger: Logger
14
_stack: Collections.LIST[Scope]
15
16
stack: Collections.List[Scope] => _stack
17
18
// The scope at the top of the stack. Non-optional: `clear()`
19
// seeds the stack with the root namespace, so during
20
// compilation there is always at least one scope. Bookkeeping
21
// discipline (matched enter/leave) keeps it that way.
22
current_scope: Scope => _stack[_stack.count-1]
23
24
// The innermost enclosing namespace scope. A symbol's own
25
// declaration renders relative to this - where a reader references
26
// it from - rather than the type or block it is declared inside,
27
// so a member keeps its type qualifier unless it is imported.
28
current_namespace_scope: Scope is
29
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
30
if scope.is_namespace then
31
return scope
32
fi
33
od
34
35
return current_scope
36
si
37
38
global_scope: Scope => _stack[0]
39
40
current_namespace_context: NamespaceContext is
41
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
42
if isa NamespaceContext(scope) then
43
return scope
44
fi
45
od
46
47
assert false else "no current namespace"
48
si
49
50
// The nearest enclosing namespace symbol, however deep the
51
// current scope sits inside functions and blocks — the
52
// declaration target for a top-level `let` promoted out of the
53
// synthesised entry.
54
current_namespace_symbol: Symbols.NAMESPACE? is
55
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
56
if isa Symbols.NAMESPACE(scope) then
57
return scope
58
elif isa NAMESPACE_SCOPE(scope) then
59
return scope.containing_namespace
60
fi
61
od
62
63
return null
64
si
65
66
// The innermost enclosing namespace block's own scope: where a
67
// name looked up from inside the block is remembered.
68
current_namespace_block: NAMESPACE_SCOPE? is
69
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
70
if isa NAMESPACE_SCOPE(scope) then
71
return scope
72
fi
73
od
74
75
return null
76
si
77
78
current_declaration_context: DeclarationContext is
79
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
80
let underlying = scope.underlying_scope
81
82
if isa DeclarationContext(underlying) then
83
return underlying
84
fi
85
od
86
87
assert false else "no current declaration context"
88
si
89
90
current_instance_context: Symbols.Classy? is
91
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
92
let underlying = scope.underlying_scope
93
94
if underlying.is_instance_context then
95
return cast Symbols.Classy?(underlying)!
96
fi
97
od
98
return null
99
si
100
101
current_union_context: Symbols.UNION? is
102
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
103
let underlying = scope.underlying_scope
104
105
if isa Symbols.UNION(underlying) then
106
return underlying
107
fi
108
od
109
return null
110
si
111
112
// The class whose code is making the current access, used as the
113
// accessor for the underscore-policy access check. Ordinarily the
114
// current function's owner; for a closure declared in a partial/impl
115
// block that owner is the block's injection scope rather than the
116
// target type, so fall back to the innermost enclosing instance type
117
// on the scope stack (which resolves to the target through the
118
// injection scope). A closure that captures is re-owned by its
119
// capture frame when it is loaded, so that IL emission names the
120
// method inside the frame - the frame is a synthesized carrier
121
// rather than the accessing type, and falls back the same way.
122
// Null in a global function.
123
current_accessor: Symbols.Classy? =>
124
let owner = cast Symbols.Classy?(current_function?.owner) in
125
if owner? /\ !isa Symbols.FRAME(owner) then
126
owner
127
else
128
current_instance_context
129
fi
130
131
current_function: Symbols.Function? is
132
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
133
if isa Symbols.Function(scope) then
134
return scope
135
fi
136
od
137
return null
138
si
139
140
// Whether the walk is anywhere inside the entry synthesised
141
// from a file's top-level statements - directly among them, or
142
// in a function literal one of them writes. That is where the
143
// walk maintains a top-level `let`'s defined flag, clearing it
144
// before each walk of the `let` and setting it on the `let`'s
145
// left, so it is also where asking whether one is defined yet
146
// answers about the reading position. A named function's body
147
// is walked with the entry nowhere on the stack, and the flag
148
// says nothing about position there.
149
is_within_top_level_entry: bool is
150
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
151
if let function: Symbols.Function = scope /\ function.is_top_level_entry then
152
return true
153
fi
154
od
155
return false
156
si
157
158
current_closure_context: ClosureContext is
159
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
160
if isa ClosureContext(scope) then
161
return scope
162
fi
163
od
164
165
assert false else "no current closure context"
166
si
167
168
current_capture_context: Symbols.Symbol? is
169
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
170
if scope.is_capture_context then
171
return cast Symbols.Symbol?(scope)
172
fi
173
od
174
return null
175
si
176
177
current_closure: Symbols.Closure? is
178
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
179
if let result: Symbols.Closure = scope then
180
return result
181
fi
182
od
183
return null
184
si
185
186
// The nearest enclosing `name` that could be called: a function
187
// group, or a value whose type is a function type. Walks the
188
// scope stack outward from the innermost scope, skipping any
189
// binding that is neither - so a local, field or property named
190
// `name` does not hide a callable `name` further out.
191
//
192
// Each scope's own find_enclosing does the resolution, so a
193
// callable reached through a `use` import is found the same way
194
// an ordinary reference finds it. Scopes whose find_enclosing
195
// already searches outward can return a binding declared further
196
// out than the scope being asked, which is why a non-callable
197
// result continues the walk rather than ending it.
198
find_enclosing_callable(name: string) -> Symbols.Symbol? =>
199
find_enclosing_matching(name, candidate => is_callable_symbol(candidate))
200
201
// The nearest enclosing `name` that `accept` takes, walking the
202
// scope stack outward as find_enclosing_callable does, so a binding
203
// `accept` refuses does not hide one further out.
204
find_enclosing_matching(name: string, accept: (Symbols.Symbol) -> bool) -> Symbols.Symbol? is
205
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
206
let candidate = scope.find_enclosing(name)
207
208
if candidate? /\ accept(candidate) then
209
return candidate
210
fi
211
od
212
213
return null
214
si
215
216
// Callable for the purposes of the search above: an overload
217
// group, or anything holding a function value. A closure is a
218
// Function, so it satisfies the first test.
219
is_callable_symbol(symbol: Symbols.Symbol) -> bool static is
220
if symbol.is_function_group \/ symbol.is_function then
221
return true
222
fi
223
224
let type = cast Types.Typed?(symbol)?.type
225
226
return type? /\ (type.is_function \/ type.is_action)
227
si
228
229
current_function_group: Symbols.FUNCTION_GROUP? is
230
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
231
if isa Symbols.FUNCTION_GROUP(scope) then
232
return scope
233
fi
234
od
235
return null
236
si
237
238
current_property: Symbols.Property? is
239
for scope in Collections.LIST_REVERSE_ITERATOR[Scope](_stack) do
240
if isa Symbols.Property(scope) then
241
return scope
242
fi
243
od
244
return null
245
si
246
247
init(logger: Logger) is
248
_logger = logger
249
250
clear()
251
si
252
253
clear() is
254
_stack = Collections.LIST[Scope](50)
255
256
enter_scope(Symbols.NAMESPACE(LOCATION.internal, "", null, "", true))
257
si
258
259
scope_for(node: Node) -> Scope? =>
260
let carrier = cast ScopeCarrier?(node) in
261
if carrier? then carrier.scope else null fi
262
263
associate_node_with_scope(node: ScopeCarrier, scope: Scope) is
264
node.scope = scope
265
si
266
267
mark_scope_stack() -> int => _stack.count
268
release_scope_stack(mark: int) is
269
assert mark <= _stack.count
270
271
while _stack.count > mark do
272
_stack.remove_at(_stack.count - 1)
273
od
274
si
275
276
enter_scope(node: ScopeCarrier) is
277
let scope = node.scope
278
279
if !scope? then
280
_logger.poison(node.location, "no scope found for {node.get_type()}")
281
282
return
283
fi
284
285
enter_scope(scope)
286
si
287
288
enter_scope(scope: Scope) is
289
_stack.add(scope)
290
si
291
292
leave_scope(node: ScopeCarrier) is
293
let scope = node.scope
294
295
if !scope? then
296
_logger.poison(node.location, "no scope found for {node.get_type()}")
297
return
298
fi
299
300
leave_scope(scope)
301
si
302
303
leave_scope(scope: Scope) is
304
assert current_scope == scope else "scope stack corrupt: stack top: {current_scope} leaving scope: {scope}"
305
306
_stack.remove_at(_stack.count - 1)
307
si
308
309
leave_scope() is
310
let scope = _stack[_stack.count - 1]
311
312
_stack.remove_at(_stack.count - 1)
313
si
314
315
to_string() -> string is
316
let result = System.Text.StringBuilder()
317
318
result.append("symbol table:\n")
319
320
for scope in Collections.LIST_REVERSE_ITERATOR[Semantic.Scope](_stack) do
321
result
322
.append(scope)
323
.append("\n")
324
od
325
326
result.append("\n")
327
328
return result.to_string()
329
si
330
si
331
si