Skip to content
← Back

src/semantic/symbols/symbol.ghul

1
namespace Semantic.Symbols is
2
use System.Exception
3
use System.NotImplementedException
4
use System.Text.StringBuilder
5
6
use IO.Std
7
8
use IoC
9
use Logging
10
use Source
11
12
use IR.Values.Value
13
14
use Semantic.Types.Type
15
16
enum ACCESS is
17
PRIVATE, PUBLIC, PROTECTED
18
si
19
20
// FIXME: think the language server needs to declare it can
21
// use the newer list below for completions as well as symbol info:
22
enum CompletionKind is
23
UNDEFINED = 0,
24
METHOD = 2,
25
FUNCTION = 3,
26
CONSTRUCTOR = 4,
27
FIELD = 5,
28
VARIABLE = 6,
29
CLASS = 7,
30
INTERFACE = 8,
31
MODULE = 9,
32
PROPERTY = 10,
33
ENUM = 13,
34
KEYWORD = 14,
35
SNIPPET = 15,
36
COLOR = 16,
37
FILE = 17,
38
REFERENCE = 18,
39
FOLDER = 19,
40
ENUM_MEMBER = 20,
41
CONSTANT = 21,
42
STRUCT = 22,
43
EVENT = 23,
44
OPERATOR = 24,
45
TYPE_PARAMETER = 25
46
si
47
48
enum SymbolKind is
49
UNDEFINED = 0,
50
FILE = 1,
51
MODULE = 2,
52
NAMESPACE = 3,
53
PACKAGE = 4,
54
CLASS = 5,
55
METHOD = 6,
56
PROPERTY = 7,
57
FIELD = 8,
58
CONSTRUCTOR = 9,
59
ENUM = 10,
60
INTERFACE = 11,
61
FUNCTION = 12,
62
VARIABLE = 13,
63
CONSTANT = 14,
64
STRING = 15,
65
NUMBER = 16,
66
BOOLEAN = 17,
67
ARRAY = 18,
68
OBJECT = 19,
69
KEY = 20,
70
NULL = 21,
71
ENUM_MEMBER = 22,
72
STRUCT = 23,
73
EVENT = 24,
74
OPERATOR = 25,
75
TYPE_PARAMETER = 26
76
si
77
78
enum TypeParameterConstraintKind is
79
NONE = 0,
80
REFERENCE = 1,
81
VALUE = 2,
82
OPTIONAL = 3
83
si
84
85
class Symbol: Scope abstract is
86
_next_id: int static
87
88
// What a symbol that holds no others answers for the symbols it
89
// holds, shared rather than made afresh each time it is asked.
90
_no_symbols: Symbol[] static
91
92
init() static is
93
_no_symbols = []
94
si
95
96
// Creation-ordered identity, unique per symbol within a process.
97
// Facts keyed by id survive a symbol being re-created by an
98
// incremental edit when the replacement adopts its predecessor's
99
// id — reference identity made transferable. Never use ids for
100
// ordering: assignment order differs between batch builds and
101
// analysis-mode edit histories.
102
_id: int
103
104
_location: LOCATION
105
_name: string?
106
_synthesized: bool
107
_doc: string?
108
_deprecation: Semantic.DEPRECATION?
109
110
owner: Scope?
111
type: Type? => Types.NONE.instance
112
depth: int => 1
113
114
symbols: Collections.Iterable[Symbol] => _no_symbols
115
116
overriders: Collections.Iterable[Symbol]? => null
117
overridees: Collections.Iterable[Symbol]? => null
118
119
implementors: Collections.Iterable[Symbol]? => null
120
121
unspecialized_symbol: Symbol => self
122
123
root_unspecialized_symbol: Symbol => self
124
125
// The declaration's `///` doc comment, as Markdown. Held on the
126
// declared symbol; a specialisation reads its root's.
127
doc: string? =>
128
if root_unspecialized_symbol == self then
129
_doc
130
else
131
root_unspecialized_symbol.doc
132
fi
133
134
set_doc(text: string?) is
135
_doc = text
136
si
137
138
// What the declaration's System.ObsoleteAttribute says, or null when it
139
// has none. As with doc, a specialisation reads its root's.
140
deprecation: Semantic.DEPRECATION? =>
141
if root_unspecialized_symbol == self then
142
_deprecation
143
else
144
root_unspecialized_symbol.deprecation
145
fi
146
147
set_deprecation(value: Semantic.DEPRECATION?) is
148
_deprecation = value
149
si
150
151
id: int => _id
152
153
// Adopt a predecessor's identity, so id-keyed facts recorded
154
// against it are found by lookups through self. Only for an
155
// incremental reconcile replacing a declaration with its edited
156
// successor; the caller owns deciding which facts remain valid
157
// across the edit and resetting the ones that do not.
158
adopt_id(predecessor: Symbol) is
159
_id = predecessor._id
160
si
161
162
// The same, for a predecessor that no longer exists as an object:
163
// a rebuild re-declaring a retained file replays its ids.
164
adopt_id(id: int) is
165
_id = id
166
si
167
168
location: LOCATION => _location
169
span: LOCATION => _location
170
171
// Move the symbol's definition location. Used by the incremental
172
// body re-walk: a retained interface symbol below an edited body
173
// is shifted into current coordinates rather than rebuilt.
174
set_location(location: LOCATION) is
175
_location = location
176
si
177
178
// Move the symbol's declaration span. For a base symbol the span
179
// *is* the location (`span => _location`), so this is a no-op —
180
// `set_location` already handled it. Functions, classes / traits
181
// / structs / unions and properties carry a separate span field
182
// and override this to shift it. Called alongside `set_location`
183
// by the incremental body re-walk's reconciliation.
184
set_span(span_location: LOCATION) is si
185
186
name: string => _name!
187
188
// Recorded directly by whichever pass synthesises this symbol
189
// (mark_synthesized), rather than re-derived from the `$` naming
190
// convention: `$` is also an operator character, so a name test
191
// alone conflates a synthesised name with a user-declared operator
192
// that happens to start with `$`. `location.is_internal` catches
193
// the majority of synthesised symbols for free, since most are
194
// declared against LOCATION.internal; mark_synthesized is only
195
// needed for the minority that deliberately keep a real user
196
// source location (property accessors, auto-property backing
197
// fields, ...) so hover and go-to-definition still work.
198
is_internal: bool => _synthesized \/ location.is_internal
199
is_reflected: bool => location.is_reflected
200
201
// Marks this symbol as compiler-synthesised, for `is_internal`.
202
// Called by the pass that synthesises the symbol, at the point
203
// of synthesis — never inferred afterwards from its name.
204
mark_synthesized() is
205
_synthesized = true
206
si
207
208
// True for symbols whose uses are confined to a single function body —
209
// locals, parameters, labels, function-level type parameters. Cross-file
210
// queries (references / implementation / etc.) need a full COMPILE only
211
// when a NON-local symbol's recorded uses are missing; locals can always
212
// be answered from the current file's parse alone.
213
is_local: bool => false
214
is_object: bool => false
215
is_root_value_type: bool => false
216
is_root_array_type: bool => false
217
is_void: bool => false
218
is_type: bool => false
219
is_generic_type_specialization: bool => false
220
is_type_variable: bool => false
221
is_argument: bool => false
222
is_field: bool => false
223
is_private: bool => false
224
is_public_readable: bool => true
225
226
// Compile-time access rule for the underscore policy, dispatched on the
227
// symbol's access kind. `accessor` is the class whose code is making
228
// the reference. Public symbols (the default) are reachable anywhere;
229
// the underscore method and field kinds narrow this to the declaring
230
// class (private) or the declaring class and its subclasses (protected).
231
is_accessible_to(accessor: Classy?) -> bool => true
232
233
// True when `accessor` is the same declaring type as this
234
// symbol's owner. Each side is normalised through
235
// unspecialized_symbol: a member reached through a
236
// specialization of a generic type carries the GENERIC for the
237
// constructed type as its owner rather than the declaring type,
238
// and GENERIC is a Symbol rather than a Classy, so a raw cast
239
// would yield null and a peer access on a second instance of
240
// the same generic - the equality/comparison pattern - would be
241
// rejected as inaccessible. For a non-generic type the
242
// unspecialized symbol is the symbol itself, leaving this a
243
// plain declaring-type comparison.
244
is_accessible_to_declaring_type(accessor: Classy?) -> bool is
245
let o = cast Classy?(owner?.unspecialized_symbol)
246
247
return
248
accessor? /\ o? /\
249
o == cast Classy?(accessor.unspecialized_symbol)
250
si
251
252
// Access modifier shown in a hover / describe. Empty for public
253
// symbols; the underscore method and field kinds return "private " or
254
// "protected " so the hover reads e.g. "pure private method".
255
access_prefix: string => ""
256
is_assignable: bool => false
257
is_function: bool => false
258
is_function_group: bool => false
259
is_type_group: bool => false
260
is_constructor: bool => false
261
is_static_constructor: bool => false
262
is_instance_context: bool => false
263
is_union: bool => false
264
is_variant: bool => false
265
is_closed_root: bool => false
266
is_unit_variant: bool => false
267
is_specializable: bool => true
268
can_accept_actual_type_arguments: bool => false
269
can_hide_inherited: bool => false
270
271
qualified_name: string =>
272
if owner? then
273
owner.qualify(name)
274
else
275
name
276
fi
277
278
// This symbol's name shortened relative to `scope`: as compact as
279
// the scope allows while still resolving back to this symbol. Bare
280
// when the bare name is in scope there (a same-scope declaration or
281
// any `use` import); `owner.member` when the symbol is a member of
282
// a type; the full namespace for a top-level type or global that is
283
// not in scope; and bare for a local. With no scope, the full
284
// qualified name. Overridden where a kind names itself differently
285
// (a constructed generic keeps its type arguments).
286
render_name(scope: Scope?) -> string =>
287
"{_render_scope_relative_name(scope)}{render_type_argument_suffix()}"
288
289
// The scope-relative name without any type-argument suffix. A member
290
// still qualifies through its owner's full `render_name`, so the
291
// owner keeps its own arguments (`LIST[T].count`).
292
_render_scope_relative_name(scope: Scope?) -> string is
293
// A name the ambient namespace holds is never qualified here,
294
// with or without a scope: there is no scope in which the
295
// qualified spelling is what resolves it. A diagnostic that
296
// interpolates a type rather than rendering it through this
297
// still names the namespace.
298
if let owning_namespace: NAMESPACE = owner then
299
if owning_namespace.is_implicitly_imported then
300
return name
301
fi
302
fi
303
304
if !scope? then
305
return qualified_name
306
fi
307
308
if owner? /\ scope.find_enclosing(name) == self then
309
return name
310
fi
311
312
let owning = owner
313
314
if owning? /\ isa Symbol(owning) /\ owning.is_type then
315
// A variant carries the type arguments itself
316
// (`Result.OK[int,string]`), so its owning union is named
317
// without them; every other member keeps its owner's
318
// arguments (`BOX[T].bit`).
319
if is_variant then
320
return "{owning._render_scope_relative_name(scope)}.{name}"
321
fi
322
return "{owning.render_name(scope)}.{name}"
323
fi
324
325
if is_local then
326
return name
327
fi
328
329
return qualified_name
330
si
331
332
// A type that takes type parameters always renders them; a generic
333
// classy fills this in from its `argument_names`. Empty for everything
334
// else. A constructed generic renders its actual arguments through its
335
// own `render_name` override, off the bare scope-relative head.
336
render_type_argument_suffix() -> string => ""
337
338
339
// The .NET name of this symbol: the override where one was
340
// recorded, and otherwise the name it was declared under. Bare,
341
// because that is what the metadata string heap stores; a
342
// renderer that needs the name quoted quotes it at the point of
343
// rendering.
344
il_name: string => il_name_override ?? name
345
346
il_name_override: string? public
347
348
// Which built-in type this is, for a type the target provides
349
// itself.
350
built_in_type: BuiltInType? public
351
352
is_built_in_type: bool => built_in_type?
353
354
// FIXME: can these go somewhere more specific?
355
356
is_unsafe_constraints: bool public
357
358
argument_names: Collections.List[string] => Collections.LIST[string](0)
359
arguments: Collections.List[Type] => Collections.LIST[Type](0)
360
ancestors: Collections.List[Type] => Collections.LIST[Type](0)
361
362
set_ancestor_types(ancestor_types: Collections.List[Type]) is si
363
364
constraint_kind: TypeParameterConstraintKind => TypeParameterConstraintKind.NONE
365
set_constraint_kind(kind: TypeParameterConstraintKind) is si
366
367
has_constructor_constraint: bool => false
368
set_has_constructor_constraint(value: bool) is si
369
370
is_argument_pack: bool => false
371
set_is_argument_pack(value: bool) is si
372
373
get_argument_constraint_kind(index: int) -> TypeParameterConstraintKind => TypeParameterConstraintKind.NONE
374
375
get_argument_has_constructor_constraint(index: int) -> bool => false
376
377
get_argument_type_bounds(index: int) -> Collections.List[Type] => Collections.LIST[Type](0)
378
379
specialized_from: Symbol? public
380
381
root_specialized_from: Symbol =>
382
let sf = specialized_from in
383
384
if sf? then
385
sf.root_specialized_from
386
else
387
self
388
fi
389
390
access: ACCESS => ACCESS.PUBLIC
391
392
// The formatted declaration head with no trailing kind classifier —
393
// the structured counterpart to `kind_label`. Rendered from
394
// `describe(context)`; overriding it changes both this and the
395
// signature the hover renderer displays.
396
signature: string =>
397
TEXT_RENDERER(DESCRIBE_CONTEXT.instance).render(describe(DESCRIBE_CONTEXT.instance))
398
399
// The kind classifier (`instance method`, `class`, `variant`, …) or
400
// null when the symbol has none. Structured counterpart to
401
// `signature`.
402
kind_label: string? =>
403
describe_kind(DESCRIBE_CONTEXT.instance)
404
405
// Flat-text form — `signature` plus a trailing ` // kind` classifier
406
// when one is defined. Used for IL comments; the analysis wire keeps
407
// `signature` and `kind_label` separate.
408
description: string is
409
let kind = kind_label
410
if kind? then
411
return "{signature} // {kind}"
412
fi
413
return signature
414
si
415
416
short_description: string => name
417
search_description: string => "{name}{render_type_argument_suffix()}"
418
419
// Structured signature body — no trailing ` // kind`. Subclasses
420
// override to produce a rich tree whose text render matches
421
// their own signature; the default here mirrors the plain
422
// `qualified_name`. The DOC-layout hover renderer walks this
423
// tree, so overrides determine which parts of a signature can
424
// wrap.
425
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
426
SignaturePart.NAME(self)
427
428
// Human-readable classifier for the description trailer —
429
// `instance method`, `local variable`, `pure global function`,
430
// etc. Null when the symbol has no natural label (namespaces,
431
// labels).
432
describe_kind(context: DESCRIBE_CONTEXT) -> string? => null
433
434
// Shared body for any typed symbol's `<name>: <type>` display.
435
// Renders the declared type on the primary line; when the
436
// context carries a narrowed observed type for this symbol
437
// that differs from the declared shape, emits the narrowed
438
// type on a second `► narrowed` line beneath, matching the
439
// direction sigil used by narrowing-introduction inlays.
440
// `!~` compares rendered forms because `!=` on strings is
441
// reference-only in ghūl; two Type instances that render the
442
// same qualified name are still distinct references.
443
_describe_typed(
444
context: DESCRIBE_CONTEXT,
445
name_part: SignaturePart,
446
declared: Type?
447
) -> SignaturePart is
448
let narrowed = context.observed_type_for(self)
449
if narrowed? /\ declared? /\ "{narrowed}" !~ "{declared}" then
450
return PARTS.sequence([
451
name_part,
452
PARTS.literal(": "),
453
PARTS.type_ref(declared),
454
PARTS.hanging(4, "► ", PARTS.type_ref(narrowed))
455
])
456
fi
457
458
let display = narrowed ?? declared
459
460
if !display? then
461
return name_part
462
fi
463
464
return PARTS.sequence([
465
name_part,
466
PARTS.literal(": "),
467
PARTS.type_ref(display)
468
])
469
si
470
471
symbol_kind: SymbolKind => SymbolKind.UNDEFINED
472
completion_kind: CompletionKind => CompletionKind.UNDEFINED
473
474
// .NET custom attributes applied to this symbol via attribute
475
// pragmas — resolved by ATTRIBUTE_RESOLVER, emitted by generate-il.
476
// Null until the first attribute is attached.
477
custom_attributes: Collections.LIST[Semantic.CUSTOM_ATTRIBUTE]? public
478
479
is_value_type: bool => false
480
is_instance: bool => false
481
is_innate: bool => false
482
is_inheritable: bool => false
483
is_class: bool => false
484
is_trait: bool => false
485
is_variable: bool => false
486
is_capture_context: bool => false
487
is_closure: bool => false
488
is_namespace: bool => false
489
is_classy: bool => false
490
is_workspace_visible: bool => false
491
492
=~(other: object?) -> bool =>
493
if !other? /\ !isa Symbol(other) then
494
false
495
else
496
self == other
497
fi
498
499
// Declared: the body (reference identity) is itself provably
500
// store-free, but Symbols.GENERIC overrides this overload and
501
// its own body cannot be proven (see its declaration) — the
502
// fixpoint requires every overrider to prove the same bit, so
503
// that unprovability propagates up to here regardless of what
504
// this body does.
505
=~(other: Symbol) -> bool pure => self == other
506
507
init(location: LOCATION, owner: Scope, name: string) is
508
_next_id = _next_id + 1
509
_id = _next_id
510
511
_location = location
512
self.owner = owner
513
_name = name
514
si
515
516
define() is
517
si
518
519
add_member(function: Symbol) -> bool => throw System.NotImplementedException("cannot add member to {self}")
520
add_implementor(symbol: Symbol) is
521
throw System.NotImplementedException("cannot add implementor to {self}")
522
si
523
524
get_ancestor(i: int) -> Type is
525
if ancestors.count > 0 then
526
Std.error.write_line("oops: {self.get_type()} {self} has ancestors but expected none")
527
528
for a in ancestors do
529
Std.error.write_line("ancestor: {a}")
530
od
531
fi
532
533
throw NotImplementedException("{get_type()} has no ancestor {i}")
534
si
535
536
get_element_name(index: int) -> string? => null
537
538
qualify(name: string) -> string => "{qualified_name}.{name}"
539
540
// Excludes an ambiguous reflected overload from resolution: its
541
// IL name is preserved separately since emission still needs it,
542
// and it is marked synthesised so lookups, completions and
543
// duplicate checks skip it as they would any other internal
544
// symbol.
545
hide() is
546
if !il_name_override? then
547
il_name_override = _name!
548
fi
549
550
_name = "${_name}"
551
552
mark_synthesized()
553
si
554
555
compare_type(other: Symbol) -> Types.MATCH
556
=> Types.MATCH.DIFFERENT
557
558
specialize(type_map: Collections.Map[Symbol,Type], owner: GENERIC) -> Symbol => throw NotImplementedException("{get_type()} cannot be specialized: {self}")
559
specialize(arguments: Collections.List[Type]) -> Symbol => throw NotImplementedException("{get_type()} cannot be specialized: {self}")
560
try_specialize(
561
location: LOCATION,
562
logger: Logger,
563
actual_type_arguments: Collections.List[Type]
564
) -> Symbol? is
565
logger.error(location, "cannot supply explicit type arguments here")
566
return null
567
si
568
569
freeze() -> Symbol? => null
570
571
load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value =>
572
// will report error when consumed:
573
IR.Values.Load.SYMBOL(from, self)
574
575
load_outer(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value =>
576
// will report error when consumed:
577
IR.Values.Load.SYMBOL(from, self)
578
579
store(location: LOCATION, from: Value?, value: Value, loader: SYMBOL_LOADER, is_initialize: bool) -> Value =>
580
// will report error when consumed:
581
IR.Values.Store.SYMBOL(from, self, value)
582
583
call(location: Source.LOCATION, from: Value?, arguments: Collections.List[Value], type: Type?, caller: FUNCTION_CALLER) -> Value => throw NotImplementedException("{get_type()} cannot be called: {self}")
584
try_pull_down_into(
585
into: Classy,
586
other_overridee_symbols: Collections.Iterable[Symbol],
587
logger: Logging.Logger
588
) is
589
into.add_member(self)
590
si
591
592
assert_symbols_pulled_down() => throw System.NotImplementedException("abstract method: implement me")
593
pull_down_super_symbols() => throw System.NotImplementedException("abstract method: implement me")
594
add_overrider(overrider: Symbol) is si
595
add_overridee(overridee: Symbol) is si
596
remove_overrider(overrider: Symbol) is si
597
remove_overridee(overridee: Symbol) is si
598
599
find_direct(name: string) -> Symbol? => null
600
601
find_member(name: string) -> Symbol? => null
602
603
get_destructure_member_name(index: int) -> string? =>
604
"{index}"
605
606
is_positional_member_name(name: string?) -> bool static is
607
if !name? \/ name.length == 0 then
608
return false
609
fi
610
for i in 0..name.length do
611
let c = name[i]
612
if c < '0' \/ c > '9' then
613
return false
614
fi
615
od
616
return true
617
si
618
619
find_enclosing(name: string) -> Symbol? => null
620
621
// FIXME: the way we handle inheritance and specialization of generics makes it tricky to get the correctly specialized owner of methods that
622
// are interited from a super class or trait. The following works, but the fact that it's needed suggests we ought to be tracking this some other way
623
find_owning_ancestor(o: Scope) -> Scope? is
624
let search_symbol = o.unspecialized_symbol
625
626
if !search_symbol? then
627
return null
628
fi
629
630
if self.unspecialized_symbol == search_symbol then
631
return self
632
fi
633
634
if ancestors.count == 0 then
635
return o
636
fi
637
638
for i in 0..ancestors.count do
639
let aa = get_ancestor(i)
640
641
if aa.unspecialized_symbol == o.unspecialized_symbol then
642
return get_ancestor(i).symbol
643
fi
644
645
let result = aa.symbol.find_owning_ancestor(o)
646
647
if result? then
648
return result
649
fi
650
od
651
return null
652
si
653
654
find_ancestor(search_type: Type) -> Type? is
655
if unspecialized_symbol == search_type.unspecialized_symbol then
656
return self.type
657
fi
658
659
for i in 0..ancestors.count do
660
let result = get_ancestor(i).find_ancestor(search_type)
661
662
if result? then
663
return result
664
fi
665
od
666
return null
667
si
668
669
get_all_direct_ancestor_members() -> Collections.LIST[Symbol] is
670
let result = Collections.LIST[Symbol]()
671
672
for i in 0..ancestors.count do
673
let a = get_ancestor(i)
674
675
if a.scope? then
676
for member in a.scope!.symbols do
677
if isa FUNCTION_GROUP(member) then
678
for function in member.functions do
679
if
680
function.is_abstract \/
681
function.is_default_trait_method \/
682
(function.name !~ "init" /\ !a.is_trait)
683
then
684
result.add(function)
685
fi
686
od
687
elif isa Function(member) then
688
let function = member
689
690
if
691
function.is_abstract \/
692
function.is_default_trait_method \/
693
(function.name !~ "init" /\ !a.is_trait)
694
then
695
result.add(function)
696
fi
697
elif !member.is_type_variable then
698
result.add(member)
699
fi
700
od
701
fi
702
od
703
704
return result
705
si
706
707
find_direct_matches(prefix: string, matches: Collections.MutableMap[string, Symbol]) is
708
si
709
710
find_ancestor_matches(prefix: string, matches: Collections.MutableMap[string, Symbol]) is
711
si
712
713
find_member_matches(prefix: string, matches: Collections.MutableMap[string, Symbol]) is
714
si
715
716
find_enclosing_matches(prefix: string, matches: Collections.MutableMap[string, Symbol]) is
717
si
718
719
collapse_group_if_single_member() -> Symbol => self
720
721
set_emitted_position(position: (() -> TYPE_PARAMETER_POSITION)?) => throw System.NotImplementedException("not implemented by {get_type()}")
722
current_emitted_position: (() -> TYPE_PARAMETER_POSITION)? => null
723
emitted_position: TYPE_PARAMETER_POSITION? => null
724
725
to_string() -> string => short_description
726
si
727
728
class Scoped: Symbol, DeclarationContext abstract is
729
_symbols: SYMBOL_STORE
730
731
symbols: Collections.Iterable[Symbol] => _symbols.values
732
is_empty: bool => _symbols.count == 0
733
734
init(location: LOCATION, owner: Scope, name: string) is
735
super.init(location, owner, name)
736
_symbols = SYMBOL_STORE()
737
si
738
739
clear() is
740
_symbols.clear()
741
si
742
743
find_direct(name: string) -> Symbol? => _symbols[name]
744
745
find_direct_matches(prefix: string, matches: Collections.MutableMap[string, Symbol]) is
746
_symbols.find_matches(prefix, matches)
747
si
748
749
// Direct store mutators for INHERITANCE_JOURNAL undo: reverse an
750
// entry a pull-down added or promoted. Not for general use - the
751
// declare/add_member paths own the store's invariants.
752
remove_direct(name: string) is
753
_symbols.remove(name)
754
si
755
756
put_direct(name: string, symbol: Symbol) is
757
_symbols[name] = symbol
758
si
759
760
declare(location: LOCATION, symbol: Symbol, symbol_definition_listener: SymbolDefinitionListener?) is
761
let name = symbol.name
762
let existing = find_direct(name)
763
764
if existing? then
765
// Argument-count overloading: a Classy may join an
766
// existing Classy (or TYPE_GROUP) under the same name as
767
// long as no member already occupies its generic-argument
768
// count. `class Foo is` and `class Foo[T] is` form a
769
// TYPE_GROUP holding both.
770
let new_classy = cast Classy?(symbol)
771
772
if new_classy? then
773
let existing_group = cast TYPE_GROUP?(existing)
774
775
if existing_group? then
776
if existing_group.find_by_generic_arguments_count(new_classy.argument_names.count)? then
777
CONTAINER.instance.logger.error(location, "redefining symbol {symbol.name}", existing.location, "symbol declared here")
778
CONTAINER.instance.logger.error(existing.location, "symbol {symbol.name} is redefined", location, "redefined here")
779
780
return
781
fi
782
783
existing_group.add(new_classy)
784
785
if symbol_definition_listener? then
786
symbol_definition_listener.add_symbol_definition(location, symbol)
787
fi
788
789
return
790
fi
791
792
let existing_classy = cast Classy?(existing)
793
794
if existing_classy? /\ existing_classy.argument_names.count != new_classy.argument_names.count then
795
let group = TYPE_GROUP(existing.location, self, name)
796
797
group.add(existing_classy)
798
group.add(new_classy)
799
800
if symbol_definition_listener? then
801
symbol_definition_listener.add_symbol_definition(location, symbol)
802
fi
803
804
_symbols[name] = group
805
806
return
807
fi
808
fi
809
810
CONTAINER.instance.logger.error(location, "redefining symbol {symbol.name}", existing.location, "symbol declared here")
811
CONTAINER.instance.logger.error(existing.location, "symbol {symbol.name} is redefined", location, "redefined here")
812
813
return
814
fi
815
816
if symbol_definition_listener? then
817
symbol_definition_listener.add_symbol_definition(location, symbol)
818
fi
819
820
_symbols[name] = symbol
821
si
822
823
declare_undefined(location: LOCATION, kind: string, name: string) -> UNDEFINED is
824
CONTAINER.instance.logger.error(location, "cannot declare {kind} here")
825
826
return UNDEFINED(location, self, name)
827
si
828
829
declare_namespace(location: LOCATION, name: string, `namespace: NAMESPACE, symbol_definition_listener: SymbolDefinitionListener?) is
830
declare_undefined(location, "namespace", name)
831
si
832
833
declare_class(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
834
declare_undefined(location, "class", name)
835
836
declare_trait(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
837
declare_undefined(location, "trait", name)
838
839
declare_struct(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
840
declare_undefined(location, "struct", name)
841
842
declare_union(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
843
declare_undefined(location, "union", name)
844
845
declare_variant(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
846
declare_undefined(location, "variant", name)
847
848
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
849
declare_undefined(location, "type", name)
850
851
declare_enum(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
852
declare_undefined(location, "enum", name)
853
854
declare_enum_member(location: LOCATION, name: string, value: string?, symbol_definition_listener: SymbolDefinitionListener?) is
855
declare_undefined(location, "enum member", name)
856
si
857
858
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
859
declare_undefined(location, "closure", name)
860
861
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
862
declare_undefined(location, "async closure", name)
863
864
865
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
866
declare_undefined(location, "generator closure", name)
867
868
declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
869
declare_undefined(location, "innate", name)
870
871
declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
872
declare_undefined(location, "function", name)
873
874
declare_generator_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
875
declare_undefined(location, "generator function", name)
876
877
declare_async_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
878
declare_undefined(location, "async function", name)
879
880
declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
881
declare_undefined(location, "variable", name)
882
883
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
884
declare_undefined(location, "property", name)
885
886
declare_label(location: LOCATION, name: string, symbol_definition_listener: SymbolDefinitionListener?) is
887
declare_undefined(location, "label", name)
888
si
889
890
declare_function_group(location: LOCATION, function: Function, symbol_definition_listener: SymbolDefinitionListener?) is
891
let name = function.name
892
893
// The group joined is this scope's own: a namespace's lookup
894
// also reaches the globals a referenced assembly contributes
895
// under the name, and a declaration added to that group would
896
// be missing from this scope and so never emitted.
897
let existing = _symbols[name]
898
let function_group: Symbols.FUNCTION_GROUP mut
899
900
if existing? then
901
if !isa Symbols.FUNCTION_GROUP(existing) then
902
CONTAINER.instance.logger.error(location, "redefining symbol {function.name}", existing.location, "symbol declared here")
903
CONTAINER.instance.logger.error(existing.location, "symbol {function.name} is redefined", location, "redefined here")
904
905
return
906
fi
907
908
function_group = existing
909
else
910
function_group = Symbols.FUNCTION_GROUP(location, self, name)
911
_symbols[name] = function_group
912
fi
913
914
if symbol_definition_listener? then
915
symbol_definition_listener.add_symbol_definition(location, function)
916
fi
917
918
function_group.add(function)
919
si
920
921
add_member(member: Symbol) -> bool is
922
let name = member.name
923
let journal = INHERITANCE_JOURNAL.current
924
925
if _symbols.contains_key(name) then
926
927
let existing = _symbols[name]!
928
929
if existing == member then
930
return true
931
fi
932
933
if isa FUNCTION_GROUP(existing) then
934
if isa Function(member) then
935
existing.add(member)
936
937
if journal? then
938
journal.record(InheritanceOp.GROUP_MEMBER_ADDED(existing, member))
939
fi
940
elif !member.is_reflected \/ !existing.is_reflected then
941
throw Exception("cannot add function {member} over the top of non function member {existing}")
942
fi
943
944
return true
945
elif isa Function(existing) then
946
if isa Function(member) then
947
let fg = FUNCTION_GROUP(location, self, name)
948
949
fg.add(existing)
950
fg.add(member)
951
952
_symbols[name] = fg
953
954
if journal? then
955
journal.record(InheritanceOp.MEMBER_REPLACED(self, name, existing))
956
fi
957
elif !member.is_reflected \/ !existing.is_reflected then
958
throw Exception("cannot add function {member} over the top of non function member {existing}")
959
fi
960
961
return true
962
elif isa TYPE_GROUP(existing) then
963
if isa Classy(member) /\ !existing.find_by_generic_arguments_count(member.argument_names.count)? then
964
existing.add(member)
965
966
if journal? then
967
journal.record(InheritanceOp.TYPE_GROUP_MEMBER_ADDED(existing, member))
968
fi
969
970
return true
971
fi
972
973
return false
974
elif isa Classy(existing) then
975
let new_classy = cast Classy?(member)
976
977
if new_classy? /\ existing.argument_names.count != new_classy.argument_names.count then
978
let group = TYPE_GROUP(location, self, name)
979
980
group.add(existing)
981
group.add(new_classy)
982
983
_symbols[name] = group
984
985
if journal? then
986
journal.record(InheritanceOp.MEMBER_REPLACED(self, name, existing))
987
fi
988
989
return true
990
fi
991
992
return false
993
elif !member.is_reflected \/ !existing.is_reflected then
994
throw System.Exception("cannot add non-function {member} over the top of non-function member {existing}")
995
fi
996
997
// FIXME: specific error?
998
999
return false
1000
fi
1001
1002
if isa Function(member) then
1003
let fg = FUNCTION_GROUP(location, self, name)
1004
1005
fg.add(member)
1006
1007
_symbols[name] = fg
1008
else
1009
_symbols[name] = member
1010
fi
1011
1012
if journal? then
1013
journal.record(InheritanceOp.MEMBER_ADDED(self, name))
1014
fi
1015
1016
return true
1017
si
1018
si
1019
1020
class NONE: Symbol is
1021
_instance: NONE? static
1022
1023
instance: NONE static is
1024
if !_instance? then
1025
_instance = NONE()
1026
fi
1027
1028
return _instance
1029
si
1030
1031
init() is
1032
super.init(LOCATION.internal, Semantic.BLOCK_SCOPE(), "!!!")
1033
si
1034
si
1035
1036
class UNDEFINED: Symbol, DeclarationContext, Types.Typed is
1037
type: Type? => Types.ERROR()
1038
1039
describe(context: DESCRIBE_CONTEXT) -> SignaturePart =>
1040
PARTS.literal("undefined")
1041
1042
init(location: LOCATION, owner: Scope, name: string) is
1043
super.init(location, owner, name)
1044
si
1045
1046
declare_undefined(location: LOCATION, kind: string, name: string) -> UNDEFINED is
1047
CONTAINER.instance.logger.error(location, "cannot declare {kind} here")
1048
1049
return UNDEFINED(location, self, name)
1050
si
1051
1052
declare_namespace(location: LOCATION, name: string, `namespace: NAMESPACE, symbol_definition_listener: SymbolDefinitionListener?) is
1053
declare_undefined(location, "namespace", name)
1054
si
1055
1056
declare_class(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1057
declare_undefined(location, "class", name)
1058
1059
declare_trait(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1060
declare_undefined(location, "trait", name)
1061
1062
declare_struct(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1063
declare_undefined(location, "struct", name)
1064
1065
declare_union(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1066
declare_undefined(location, "union", name)
1067
1068
declare_variant(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1069
declare_undefined(location, "variant", name)
1070
1071
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1072
declare_undefined(location, "type", name)
1073
1074
declare_enum(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1075
declare_undefined(location, "enum", name)
1076
1077
declare_enum_member(location: LOCATION, name: string, value: string?, symbol_definition_listener: SymbolDefinitionListener?) is
1078
declare_undefined(location, "enum member", name)
1079
si
1080
1081
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1082
declare_undefined(location, "closure", name)
1083
1084
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1085
declare_undefined(location, "async closure", name)
1086
1087
1088
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1089
declare_undefined(location, "generator closure", name)
1090
1091
declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1092
declare_undefined(location, "innate", name)
1093
1094
declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1095
declare_undefined(location, "function", name)
1096
1097
declare_generator_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1098
declare_undefined(location, "generator function", name)
1099
1100
declare_async_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1101
declare_undefined(location, "async function", name)
1102
1103
declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1104
declare_undefined(location, "variable", name)
1105
1106
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
1107
declare_undefined(location, "undefine", name)
1108
1109
declare_label(location: LOCATION, name: string, symbol_definition_listener: SymbolDefinitionListener?) is
1110
declare_undefined(location, "label", name)
1111
si
1112
si
1113
1114
class ScopedWithEnclosingScope: Scoped abstract is
1115
enclosing_scope: Scope?
1116
1117
init(location: LOCATION, owner: Scope, name: string, enclosing_scope: Scope?) is
1118
super.init(location, owner, name)
1119
1120
self.enclosing_scope = enclosing_scope
1121
si
1122
1123
find_enclosing_only(name: string) -> Symbol? is
1124
if enclosing_scope? then
1125
return enclosing_scope.find_enclosing(name)
1126
fi
1127
1128
return null
1129
si
1130
1131
find_enclosing(name: string) -> Symbol? is
1132
let result = find_direct(name)
1133
1134
if result? then
1135
return result
1136
else
1137
return find_enclosing_only(name)
1138
fi
1139
si
1140
1141
find_enclosing_only_matches(prefix: string, matches: Collections.MutableMap[string, Symbol]) is
1142
if enclosing_scope? then
1143
enclosing_scope.find_enclosing_matches(prefix, matches)
1144
fi
1145
si
1146
1147
find_enclosing_matches(prefix: string, matches: Collections.MutableMap[string, Symbol]) is
1148
find_direct_matches(prefix, matches)
1149
find_enclosing_only_matches(prefix, matches)
1150
si
1151
si
1152
si