Skip to content
← Back

src/semantic/dotnet/symbol_table.ghul

1
@@IF.dotnet()
2
3
namespace Semantic.DotNet is
4
use TYPE = System.Type
5
use IO.Std
6
7
class SYMBOL_TABLE is
8
_type_details_lookup: TYPE_DETAILS_LOOKUP
9
_symbol_factory: SYMBOL_FACTORY
10
11
_symbol_store: SYMBOL_STORE
12
13
// ghul names whose by-name cache entry is the authoritative result
14
// (a TYPE_GROUP, a confirmed single with no sibling arities, or a
15
// no-result). Distinguishes those from the provisional single that
16
// `SYMBOL_STORE.add_symbol` caches while a type materialises (needed
17
// to break cycles in the reflected type graph): a provisional entry
18
// must be re-resolved through the arity scan so an arity-colliding
19
// name (`Ghul.Comparable` / `Comparable[T]`) yields its group
20
// rather than shadowing it. Populated once per name and never
21
// cleared, so resolution is stable across analysis-mode compile cycles.
22
_authoritative_names: Collections.SET[string]
23
24
// A namespace's globals can arrive on several carriers - one per
25
// contributing assembly, and the runtime's staging namespace
26
// re-homed into `Ghul.Intrinsics` adds a second from one assembly. Overloads of one name split
27
// across carriers form one group, held here so the merged group has
28
// one identity across lookups; keyed on the qualified name that was
29
// looked up, which is the name and not a rendering of the symbol.
30
_merged_globals: Collections.MAP[string, Symbols.Symbol]
31
32
init(
33
type_details_lookup: TYPE_DETAILS_LOOKUP,
34
symbol_factory: SYMBOL_FACTORY
35
) is
36
_type_details_lookup = type_details_lookup
37
_symbol_factory = symbol_factory
38
39
_symbol_factory.set_symbol_table(self)
40
41
_symbol_store = SYMBOL_STORE()
42
_authoritative_names = Collections.SET[string]()
43
_merged_globals = Collections.MAP[string, Symbols.Symbol]()
44
si
45
46
find_root_matches(matches: Collections.MutableMap[string, Symbols.Symbol]) is
47
let distinct_namespaces = Collections.SET[string]()
48
49
let owner = EMPTY_SCOPE("")
50
51
for ns in _type_details_lookup.find_all_root_namespaces() do
52
if !matches.contains_key(ns) then
53
matches.add(ns, COMPLETION_SYMBOL(owner, ns, Symbols.SymbolKind.NAMESPACE, Symbols.CompletionKind.MODULE))
54
fi
55
od
56
si
57
58
find_member_matches(namespace_name: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is
59
if HIDDEN_SYMBOLS.is_hidden_namespace(namespace_name) then
60
return
61
fi
62
63
let owner = EMPTY_SCOPE(namespace_name)
64
let distinct_namespaces = Collections.SET[string](_type_details_lookup.find_all_namespaces_under(namespace_name))
65
66
let type_details = _type_details_lookup.get_all_type_details_in_ghul_namespace(namespace_name)
67
68
if !type_details? then
69
return
70
fi
71
72
for td in type_details do
73
// A globals carrier hosts a namespace's globals in IL, and
74
// import re-homes those members onto the namespace itself,
75
// so the carrier is never user-spellable. The same goes for
76
// any type the declaring compiler synthesised - a generator
77
// or async state machine's frame, a closure class - which
78
// its CompilerGeneratedAttribute names. Both stay resolvable
79
// by name; neither is offered as something to spell.
80
if td.is_globals_carrier \/ td.is_compiler_generated then
81
continue
82
fi
83
84
let dotnet_type = td.dotnet_type
85
let ghul_name = td.ghul_type_name
86
let ghul_namespace = td.ghul_namespace
87
88
if ghul_namespace =~ namespace_name then
89
if !matches.contains_key(ghul_name) then
90
let symbol_kind mut = Symbols.SymbolKind.UNDEFINED
91
let completion_kind mut = Symbols.CompletionKind.UNDEFINED
92
93
if dotnet_type.is_value_type then
94
symbol_kind = Symbols.SymbolKind.STRUCT
95
completion_kind = Symbols.CompletionKind.STRUCT
96
elif dotnet_type.is_interface then
97
symbol_kind = Symbols.SymbolKind.INTERFACE
98
completion_kind = Symbols.CompletionKind.INTERFACE
99
elif dotnet_type.is_class then
100
symbol_kind = Symbols.SymbolKind.CLASS
101
completion_kind = Symbols.CompletionKind.CLASS
102
fi
103
104
matches.add(ghul_name, COMPLETION_SYMBOL(owner, ghul_name, symbol_kind, completion_kind))
105
fi
106
elif ghul_namespace.starts_with("{namespace_name}.") then
107
let suffix = ghul_namespace.substring(namespace_name.length + 1)
108
let parts = suffix.split(['.'])
109
110
if parts.count >= 2 then
111
distinct_namespaces.add(parts[0])
112
fi
113
fi
114
od
115
116
for ns in distinct_namespaces do
117
if HIDDEN_SYMBOLS.is_hidden_namespace("{namespace_name}.{ns}") then
118
continue
119
fi
120
121
if !matches.contains_key(ns) then
122
matches.add(ns, COMPLETION_SYMBOL(owner, ns, Symbols.SymbolKind.NAMESPACE, Symbols.CompletionKind.MODULE))
123
fi
124
od
125
si
126
127
get_symbol(ghul_name: string) -> Symbols.Scoped? is
128
// An old name of a renamed type is the type under its new name:
129
// one symbol, two spellings.
130
if let renamed = _type_details_lookup.aliases.renamed(ghul_name) then
131
return get_symbol(renamed)
132
fi
133
134
// Trust the by-name cache only once the name has been resolved
135
// authoritatively. A provisional single (cached by add_symbol
136
// while some type's ancestors load) is deliberately not trusted
137
// here: an arity-colliding name must fall through to the scan
138
// below so its TYPE_GROUP forms instead of the lone member
139
// shadowing it. The provisional entry stays in the store, so a
140
// recursive by-name lookup during the group's own materialisation
141
// still resolves (breaking reflected-graph cycles). The probe's
142
// bool matters: a cached null is a known no-result and must be
143
// returned as-is, not fall through to the scan (which would
144
// re-cache the no-result and throw on the duplicate key).
145
if _authoritative_names.contains(ghul_name) /\ _symbol_store.has_symbol(ghul_name) then
146
return _symbol_store.get_symbol(ghul_name)
147
fi
148
149
let type_name = TYPE_NAME(ghul_name)
150
151
// Single-pass scan: track the first matching TYPE_DETAILS
152
// without allocating; only build a LIST when a second
153
// match shows up (the rare multi-generic-count case). Keeps
154
// the common one-match path allocation-free, matching the
155
// pre-argument-count-overloading cost profile.
156
let search_list = _type_details_lookup.get_all_type_details_in_ghul_namespace(type_name.namespace_name)
157
let first: TYPE_DETAILS? mut = null
158
let all: Collections.LIST[TYPE_DETAILS]? mut = null
159
160
if search_list? then
161
for details in search_list do
162
if details.matches(type_name.namespace_name, type_name.name) then
163
if !first? then
164
first = details
165
elif !all? then
166
all = Collections.LIST[TYPE_DETAILS]()
167
all.add(first)
168
all.add(details)
169
else
170
all.add(details)
171
fi
172
fi
173
od
174
fi
175
176
if all? then
177
// Mark authoritative before materialising: a recursive by-name
178
// lookup during the group build then trusts the provisional
179
// single already in the store rather than re-entering here.
180
_authoritative_names.add(ghul_name)
181
return materialize_type_group(ghul_name, type_name, all)
182
elif first? then
183
assert first.assembly_name? /\ first.assembly_name.length > 0 else " invalid assembly name: {first}"
184
185
_authoritative_names.add(ghul_name)
186
187
// Reuse the by-dotnet-type cache if this type was already
188
// materialised (e.g. while loading another type's ancestors)
189
// rather than creating a parallel symbol for the same .NET
190
// type — otherwise the two would fail to unify (`int` vs
191
// `Ghul.int`). Cache the confirmed single under the bare name.
192
let cached = _symbol_store.get_symbol(first.dotnet_type)
193
let single: Symbols.Scoped? = if cached? then cached else create_symbol(first) fi
194
195
if single? then
196
_symbol_store.set_name_symbol(ghul_name, single)
197
fi
198
199
return single
200
fi
201
202
_authoritative_names.add(ghul_name)
203
_symbol_store.cache_no_result(ghul_name)
204
return null
205
si
206
207
// A lookup that found nothing is remembered, and so is the merged
208
// answer for a global's name. Both go when an assembly is imported
209
// into a reference set already in use, since either can change.
210
forget_no_results() is
211
for name in _symbol_store.forget_no_results() do
212
_authoritative_names.remove(name)
213
od
214
215
_merged_globals.clear()
216
si
217
218
// Search every already-materialised globals carrier in a
219
// namespace. Nothing is materialised here: this backs a lookup
220
// that runs on each missed name in the namespace, and the
221
// carrier a name does resolve to has been materialised by the
222
// caller before it gets here.
223
//
224
// Functions of the name found on more than one carrier merge into
225
// one group, so overloads a re-homed staging namespace contributes,
226
// or in two assemblies contributing to one namespace, resolve
227
// together as they do when declared in one compilation. Any other
228
// kind of symbol is taken from the first carrier that has it.
229
find_global_member(namespace_name: string, name: string) -> Symbols.Symbol? is
230
if !_type_details_lookup.has_globals_carrier(namespace_name) then
231
return null
232
fi
233
234
let key = "{namespace_name}.{name}"
235
236
if _merged_globals.contains_key(key) then
237
return _merged_globals[key]
238
fi
239
240
let search_list = _type_details_lookup.get_all_type_details_in_ghul_namespace(namespace_name)
241
242
if !search_list? then
243
return null
244
fi
245
246
let found = Collections.LIST[Symbols.Symbol]()
247
let all_materialised mut = true
248
249
for details in search_list do
250
if !details.is_globals_carrier then
251
continue
252
fi
253
254
if let carrier: Symbols.Classy = _symbol_store.get_symbol(details.dotnet_type) then
255
let member = carrier.find_member(name)
256
257
if member? then
258
found.add(member)
259
fi
260
else
261
all_materialised = false
262
fi
263
od
264
265
if found.count == 0 then
266
return null
267
fi
268
269
let result = GLOBAL_OVERLOAD_MERGE.merge(found)
270
271
// A carrier still to materialise could hold another overload,
272
// so the merged group is only fixed once every carrier has
273
// been seen.
274
if all_materialised /\ result != found[0] then
275
_merged_globals[key] = result
276
fi
277
278
return result
279
si
280
281
// Every global the namespace's carriers hold, for completion. The
282
// by-name lookup above materialises one carrier member on demand,
283
// so enumerating the symbols already materialised would offer
284
// whatever an earlier lookup happened to ask for. This materialises
285
// each carrier and reads its members instead, which is what makes
286
// the candidates the same set the unqualified name resolves against.
287
find_global_member_matches(
288
namespace_name: string,
289
prefix: string,
290
matches: Collections.MutableMap[string, Symbols.Symbol]
291
) is
292
if !_type_details_lookup.has_globals_carrier(namespace_name) then
293
return
294
fi
295
296
let search_list = _type_details_lookup.get_all_type_details_in_ghul_namespace(namespace_name)
297
298
if !search_list? then
299
return
300
fi
301
302
for details in search_list do
303
if !details.is_globals_carrier then
304
continue
305
fi
306
307
if let carrier: Symbols.Classy = _symbol_store.get_symbol(details.dotnet_type) then
308
carrier.find_member_matches(prefix, matches)
309
fi
310
od
311
si
312
313
materialize_type_group(ghul_name: string, type_name: TYPE_NAME, all_details: Collections.LIST[TYPE_DETAILS]) -> Symbols.Scoped? is
314
// The string-keyed cache holds the group itself after all
315
// members materialize, so subsequent bare-name lookups skip
316
// the multi-materialize cost. Each member's by-dotnet-type
317
// cache entry still points to that member; only the bare
318
// ghul_name key gets replaced.
319
let owner = EMPTY_SCOPE(type_name.namespace_name)
320
let group = Symbols.TYPE_GROUP(Source.LOCATION.reflected, owner, type_name.name)
321
322
for details in all_details do
323
assert details.assembly_name? /\ details.assembly_name.length > 0 else " invalid assembly name: {details}"
324
325
// The .NET type may already have been materialized via
326
// `get_symbol(TYPE)` — reuse the cached Classy rather
327
// than creating a parallel one with a fresh EMPTY_SCOPE
328
// owner.
329
let cached = _symbol_store.get_symbol(details.dotnet_type)
330
let member: Symbols.Scoped? = if cached? then cached else create_symbol(details) fi
331
332
if isa Symbols.Classy(member) then
333
group.add(member)
334
fi
335
od
336
337
if group.count == 0 then
338
return null
339
fi
340
341
if group.count == 1 then
342
// Defensive: if only one of the materializations stuck,
343
// return the bare Classy and avoid IL-mangling it as if
344
// it had sibling generic-argument counts.
345
let only = group.classies[0]
346
only.has_argument_count_siblings = false
347
_symbol_store.set_name_symbol(ghul_name, only)
348
return only
349
fi
350
351
_symbol_store.set_name_symbol(ghul_name, group)
352
353
return group
354
si
355
356
get_symbol(type: TYPE) -> Symbols.Scoped? is
357
let result mut = _symbol_store.get_symbol(type)
358
359
if result? then
360
return result
361
fi
362
363
let type_details mut = _type_details_lookup.get_type_details_by_dotnet_type(type)
364
365
if !type_details? /\ _symbol_factory.has_variant_attribute(type) /\ type.base_type? then
366
// Cross-assembly variant: its `.NET` Namespace equals the
367
// parent union's full name and it isn't registered in
368
// the by-dotnet-type lookup (assemblies.ghul queues it
369
// for materialization-via-union instead). Trigger the
370
// parent union's creation here so `materialize_variants`
371
// populates the variant as a child of the union, then
372
// return that union-owned member symbol — falling
373
// through would route this through `create_class` and
374
// produce a detached `Symbols.VARIANT` with an
375
// `EMPTY_SCOPE` owner.
376
let parent_type mut = type.base_type!
377
if parent_type.is_generic_type /\ !parent_type.is_generic_type_definition then
378
parent_type = parent_type.get_generic_type_definition()
379
fi
380
381
let union_symbol = get_symbol(parent_type)
382
383
if isa Symbols.Classy(union_symbol) then
384
let variant_member = union_symbol.find_member(type.name)
385
if isa Symbols.Scoped(variant_member) then
386
return variant_member
387
fi
388
fi
389
fi
390
391
if !type_details? then
392
Std.error.write_line("warning: no type details for type {type} in assembly {type.assembly.get_name()}")
393
394
let asm_name = type.assembly.get_name()
395
396
// FIXME: nested type name bodge?
397
type_details =
398
TYPE_DETAILS(
399
type,
400
type.`namespace ?? "",
401
type.name,
402
null,
403
asm_name.name ?? ""
404
)
405
fi
406
407
assert type_details.assembly_name? /\ type_details.assembly_name.length > 0 else " invalid assembly name: {type_details}"
408
409
return create_symbol(type_details)
410
si
411
412
create_symbol(type_details: TYPE_DETAILS) -> Symbols.Scoped? is
413
let result = _symbol_factory.create_symbol(type_details)
414
415
if !result? then
416
@IF.debug() Std.error.write_line("not something we can handle yet: ignoring: {type}")
417
418
return null
419
fi
420
421
let dotnet_type = type_details.dotnet_type
422
423
_symbol_store.add_symbol(dotnet_type, "{type_details.ghul_namespace}.{type_details.ghul_type_name}", result)
424
425
// The symbol is registered in the store before its ancestors
426
// and members load so self-referential generics (bool's
427
// Comparable[bool], Equatable[bool], ...) resolve to it
428
// instead of recursing. The flip side: a failure in the
429
// population below leaves a permanently half-built symbol
430
// cached for the life of the process. Rethrowing does not undo
431
// that, and this runs on the lazy lookup path as well as the
432
// eager import one, so the throw surfaced from whatever member
433
// access touched the type first — as an internal error naming
434
// neither the type nor the assembly it needed.
435
try
436
_symbol_factory.add_ancestors(result, dotnet_type)
437
438
_symbol_factory.add_members(result, dotnet_type)
439
440
// When the union's symbol has just been created, also
441
// materialize each of its variants as a child member.
442
// Cross-assembly variants don't go through normal type
443
// lookup (their reflected `Namespace` is the union's
444
// full name and would clash with the union as a
445
// namespace) — assemblies.ghul stashed them keyed by
446
// the union's full name in TYPE_DETAILS_LOOKUP.
447
if let union_result: Symbols.UNION = result then
448
_symbol_factory.materialize_variants(union_result, dotnet_type)
449
fi
450
451
_symbol_factory.resolve_overrides(result)
452
catch ex: System.Exception
453
_symbol_factory.report_materialization_failure(dotnet_type, ex)
454
yrt
455
456
return result
457
si
458
si
459
si