Skip to content
← Back

src/syntax/process/resolve_uses.ghul

1
namespace Syntax.Process is
2
use Logging
3
use Source
4
use Trees
5
6
use Ghul.Pipes
7
8
// Resolves use clauses into their namespace block's scope. Runs
9
// twice: a first round after declare-symbols binds every import that
10
// refers to a type - classes, structs, unions, variants, enums - and
11
// namespaces, which is everything resolve-type-expressions needs. A
12
// name that fails to bind is left for the second round (`_final`
13
// unset marks the first-round instance), which runs after
14
// declare-members, so an import can also name a static method, a
15
// global function or an enum member - including one an impl or
16
// partial block injects; anything still unresolved then is an error.
17
class RESOLVE_USES: ScopeVisitorBase is
18
_logger: Logger
19
_symbol_table: Semantic.SYMBOL_TABLE
20
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS
21
_final: bool
22
23
init(
24
logger: Logger,
25
symbol_table: Semantic.SYMBOL_TABLE,
26
namespaces: Semantic.NAMESPACES,
27
symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
28
final: bool
29
) is
30
super.init(symbol_table, namespaces)
31
_logger = logger
32
_symbol_table = symbol_table
33
_symbol_use_locations = symbol_use_locations
34
_final = final
35
si
36
37
apply(node: Node) is
38
node.walk(self)
39
si
40
41
pre(`namespace: Definitions.NAMESPACE) -> bool is
42
let namespace_scope = enter_namespace(`namespace)
43
44
// FIXME: these searches probably should start at the scope immediately enclosing the namespace
45
for u in `namespace.body.uses do
46
if u.is_alias then
47
if !_final then
48
_declare_alias(u, namespace_scope)
49
u.is_import_resolved = true
50
fi
51
52
continue
53
fi
54
55
if u.is_default then
56
_resolve_default_uses(u, namespace_scope)
57
58
continue
59
fi
60
61
if u.is_all then
62
if !_final then
63
u.is_import_resolved = _resolve_use_all(u, namespace_scope)
64
elif !u.is_import_resolved then
65
_resolve_use_all(u, namespace_scope)
66
fi
67
68
continue
69
fi
70
71
if !u.`use? then
72
continue
73
fi
74
75
if !_final then
76
u.is_import_resolved = _resolve_use(u, namespace_scope)
77
elif !u.is_import_resolved then
78
_resolve_use(u, namespace_scope)
79
fi
80
od
81
return false
82
si
83
84
visit(`namespace: Definitions.NAMESPACE) is
85
leave_namespace(`namespace)
86
si
87
88
89
// Declares the symbol for a type alias clause. The target type
90
// expression is left for resolve-type-expressions, which is the
91
// pass that can resolve it; all that is needed here is a symbol
92
// for the name to bind to, carrying its type parameters.
93
_declare_alias(u: Definitions.USE, namespace_scope: Semantic.NAMESPACE_SCOPE) is
94
let name = u.name
95
96
if !name? then
97
return
98
fi
99
100
let existing = namespace_scope.get_used_symbol(name.name)
101
102
if existing? then
103
// Re-running the pass over a block whose scope already
104
// carries this clause's symbol is a no-op; any other
105
// symbol under the name is a genuine collision.
106
if let previous = u.alias_symbol /\ existing == cast Semantic.Symbols.Symbol(previous) then
107
return
108
fi
109
110
_logger.error(name.location, "duplicate use")
111
112
return
113
fi
114
115
let declared = namespace_scope.find_direct(name.name)
116
117
if declared? then
118
_logger.error(
119
name.location,
120
"type alias {name.name} has the same name as a symbol declared in this namespace",
121
declared.location,
122
"symbol declared here"
123
)
124
125
return
126
fi
127
128
let alias =
129
Semantic.Symbols.TYPE_ALIAS(
130
name.location,
131
namespace_scope,
132
name.name,
133
namespace_scope
134
)
135
136
if let arguments = u.arguments then
137
for a in arguments do
138
if isa TypeExpressions.NAMED(a) then
139
let argument = a
140
141
alias.declare_argument(argument.name.location, argument.name.name, null)
142
elif !a.is_poisoned then
143
_logger.error(a.location, "a type alias type parameter must be a plain identifier")
144
fi
145
od
146
fi
147
148
u.alias_symbol = alias
149
150
_symbol_use_locations.add_symbol_use(name.right_location, alias)
151
152
namespace_scope.add(name.name, alias)
153
si
154
155
// Imports each name `use default` stands for. Every name is
156
// attempted in both rounds: a namespace binds in the first, a
157
// global function or static method only once members exist, and
158
// one already imported by an explicit clause is left alone rather
159
// than reported as a duplicate - writing both is how a file adds
160
// to the set rather than a mistake to point at.
161
// Every synthesised identifier below carries `use default`'s own
162
// location, since there is no real per-name token in source to
163
// attach a use to. Recording them would leave the `use default`
164
// clause itself hovering and colouring as whichever imported
165
// symbol resolved uses last, so the whole round is suppressed.
166
_resolve_default_uses(u: Definitions.USE, namespace_scope: Semantic.NAMESPACE_SCOPE) is
167
_symbol_use_locations.begin_suppress()
168
169
try
170
for name in Compiler.DEFAULT_USES.names(IoC.CONTAINER.instance.build_flags) do
171
let identifier = _default_use_identifier(u.location, name)
172
173
if namespace_scope.contains_used_symbol(identifier.name) then
174
continue
175
fi
176
177
if !find_enclosing(identifier)? then
178
if _final then
179
_logger.error(u.location, "the default use set names {name}, which is not defined")
180
fi
181
182
continue
183
fi
184
185
let clause = Definitions.USE(u.location, null, identifier)
186
187
_resolve_use(clause, namespace_scope)
188
od
189
finally
190
_symbol_use_locations.end_suppress()
191
yrt
192
si
193
194
// The qualified identifier a dotted name in the set spells.
195
_default_use_identifier(location: LOCATION, name: string) -> Identifiers.Identifier is
196
let parts = name.split(['.'])
197
198
let result mut = Identifiers.Identifier(location, parts[0])
199
200
for i in 1..parts.count do
201
result = Identifiers.QUALIFIED(location, result, parts[i], location, location)
202
od
203
204
return result
205
si
206
207
// Returns true when the clause was definitively handled - a
208
// successful import or, in the final round, a reported error.
209
_resolve_use(u: Definitions.USE, namespace_scope: Semantic.NAMESPACE_SCOPE) -> bool is
210
let used_name = u.`use
211
212
if !used_name? then
213
return true
214
fi
215
216
let found = find_enclosing(used_name)
217
let used_symbol = found ?? _find_attribute_type(used_name)
218
219
if !used_symbol? then
220
if _final then
221
_logger.error(used_name.location, "used identifier {used_name} is not defined ")
222
fi
223
224
return false
225
fi
226
227
_import_symbol(used_symbol, used_name, u.name ?? _name_as_written(found, used_name), namespace_scope)
228
229
return true
230
si
231
232
// A type reached through an old name of its own is imported under
233
// that old name, since the code after the use goes on to write the
234
// name the use wrote. Null where the symbol's own name is the one
235
// written, which imports it under that name as usual.
236
_name_as_written(found: Semantic.Symbols.Symbol?, used_name: Identifiers.Identifier) -> Identifiers.Identifier? is
237
if !found? \/ found.name =~ used_name.name then
238
return null
239
fi
240
241
return Identifiers.Identifier(used_name.right_location, used_name.name)
242
si
243
244
// A pragma names an attribute type in the short form, so a use
245
// clause written to bring one into scope is written the same way.
246
// The suffixed type is imported under its own name, which is what
247
// the pragma's own short-form lookup then finds. Ancestors are not
248
// resolved yet, so the name is the only evidence available that
249
// the type is an attribute.
250
_find_attribute_type(used_name: Identifiers.Identifier) -> Semantic.Symbols.Symbol? is
251
if used_name.name.ends_with("Attribute") then
252
return null
253
fi
254
255
let suffixed_name = "{used_name.name}Attribute"
256
257
let suffixed =
258
if let qualifier = used_name.qualifier then
259
Identifiers.QUALIFIED(
260
used_name.location,
261
qualifier,
262
suffixed_name,
263
used_name.location,
264
used_name.location)
265
else
266
Identifiers.Identifier(used_name.location, suffixed_name)
267
fi
268
269
let found = find_enclosing(suffixed)
270
271
if isa Semantic.Symbols.Classy(found) then
272
return found
273
fi
274
275
return null
276
si
277
278
// Resolves `use X.*`. A namespace target is handled exactly like a
279
// plain `use X;` - its members are already reachable through the
280
// existing per-scope namespace fallback, so there is nothing extra
281
// to enumerate. A class, struct, union or enum target has no
282
// members to enumerate until declare-members has run, so the first
283
// round only resolves and records the container; the second round
284
// - the same one that lets an ordinary use clause resolve a static
285
// method or enum member - enumerates and imports them.
286
_resolve_use_all(u: Definitions.USE, namespace_scope: Semantic.NAMESPACE_SCOPE) -> bool is
287
let used_name = u.`use
288
289
if !used_name? then
290
return true
291
fi
292
293
let container = u.wildcard_target ?? find_enclosing(used_name)
294
295
if !container? then
296
if _final then
297
_logger.error(used_name.location, "used identifier {used_name} is not defined ")
298
fi
299
300
return false
301
fi
302
303
if isa Semantic.Symbols.NAMESPACE(container) then
304
return _resolve_use(u, namespace_scope)
305
fi
306
307
if !isa Semantic.Symbols.Classy(container) then
308
_logger.error(used_name.location, "cannot use all members of {used_name}")
309
310
return true
311
fi
312
313
u.wildcard_target = container
314
315
if !_final then
316
return false
317
fi
318
319
let classy = cast Semantic.Symbols.Classy?(container)!
320
321
// `used_name` names the container, not any one member - the
322
// `.*` has no per-member token to attach a use to. Record
323
// the container itself once, then suppress the per-member
324
// imports below so they don't pile more uses onto the same
325
// span and win the hover / colouring tie-break.
326
_symbol_use_locations.add_symbol_use(used_name.right_location, container)
327
328
_symbol_use_locations.begin_suppress()
329
330
try
331
for member in classy.symbols do
332
if _is_wildcard_importable(member) then
333
_import_symbol(member, used_name, null, namespace_scope)
334
fi
335
od
336
finally
337
_symbol_use_locations.end_suppress()
338
yrt
339
340
return true
341
si
342
343
// Whether a directly-declared member is something `use X.*` may
344
// import: the same shapes a hand-written `use X.member;` succeeds
345
// on, minus a constructor and a synthesized member (an
346
// auto-property's backing field), neither of which anything
347
// sensibly imports by name. Accessibility is left to the ordinary
348
// reference-site check, exactly as it is for an explicit `use
349
// X.member;` - importing does not itself require the name to be
350
// usable from here, only reaching it later does.
351
_is_wildcard_importable(symbol: Semantic.Symbols.Symbol) -> bool is
352
if symbol.is_internal \/ symbol.is_constructor \/ symbol.is_static_constructor then
353
return false
354
fi
355
356
if symbol.is_function_group then
357
let fg = cast Semantic.Symbols.FUNCTION_GROUP?(symbol)!
358
359
return fg.functions |> any(f => !f.is_instance /\ !f.is_constructor /\ !f.is_static_constructor)
360
fi
361
362
return !symbol.is_instance
363
si
364
365
// Imports a single resolved symbol into the namespace's scope,
366
// under `alias`'s name when given and the symbol's own name
367
// otherwise. Shared by an explicit `use X.member;` and, per member,
368
// by `use X.*`'s expansion, so a wildcard import reports the same
369
// diagnostics for the same shapes of symbol that the explicit
370
// imports it stands for would.
371
_import_symbol(
372
used_symbol: Semantic.Symbols.Symbol,
373
used_name: Identifiers.Identifier,
374
alias: Identifiers.Identifier?,
375
namespace_scope: Semantic.NAMESPACE_SCOPE
376
) is
377
let use_name mut = used_symbol.name
378
379
if alias? then
380
use_name = alias.name
381
fi
382
383
if isa Semantic.Symbols.NAMESPACE(used_symbol) then
384
_symbol_use_locations.add_symbol_use(used_name.right_location, used_symbol)
385
386
if namespace_scope.contains_used_symbol(use_name) then
387
if alias? then
388
_logger.error(alias.location, "duplicate use")
389
else
390
_logger.error(used_name.location, "duplicate use")
391
fi
392
elif alias? then
393
namespace_scope.add(alias.name, used_symbol)
394
else
395
namespace_scope.add(used_symbol)
396
fi
397
elif used_symbol.is_function_group then
398
let fg = cast Semantic.Symbols.FUNCTION_GROUP?(used_symbol)!
399
400
if !(fg.functions |> any(f => !f.is_instance)) then
401
if fg.functions.count == 1 then
402
_logger.error(used_name.location, "cannot use instance function")
403
else
404
_logger.error(used_name.location, "cannot use function group comprising only instance functions")
405
fi
406
fi
407
408
_symbol_use_locations.add_symbol_use(used_name.right_location, used_symbol.collapse_group_if_single_member())
409
410
let existing_used_symbol = namespace_scope.get_used_symbol(use_name)
411
412
let use_function_group: Semantic.Symbols.FUNCTION_GROUP mut
413
414
if existing_used_symbol? then
415
use_function_group = cast Semantic.Symbols.FUNCTION_GROUP?(existing_used_symbol)!
416
else
417
use_function_group = Semantic.Symbols.FUNCTION_GROUP(LOCATION.unknown, namespace_scope, use_name)
418
namespace_scope.add(use_name, use_function_group)
419
fi
420
421
use_function_group.add(fg)
422
elif !used_symbol.is_instance then
423
if namespace_scope.contains_used_symbol(use_name) then
424
if alias? then
425
_logger.error(alias.location, "duplicate use")
426
else
427
_logger.error(used_name.location, "duplicate use")
428
fi
429
else
430
_symbol_use_locations.add_symbol_use(used_name.right_location, used_symbol)
431
namespace_scope.add(use_name, used_symbol)
432
fi
433
else
434
_logger.error(used_name.location, "cannot use instance member")
435
fi
436
si
437
si
438
si