Skip to content
← Back

src/semantic/symbol_use_locations.ghul

1
namespace Semantic is
2
use IO.Std
3
use Ghul.Disposable
4
5
use Collections
6
7
use Pair = Collections.KeyValuePair
8
9
use Ghul.Pipes
10
11
use Source
12
13
trait SymbolUseListener is
14
add_symbol_use(location: LOCATION, symbol: Symbols.Symbol)
15
si
16
17
class SYMBOL_USE_LOCATIONS: SymbolUseListener is
18
_symbol_use_map: LOCATION_MAP[Symbols.Symbol]
19
_hover_info_map: LOCATION_MAP[HOVER_USE]
20
_symbol_reference_map: Collections.MAP[Symbols.Symbol,Collections.SET[LOCATION]]
21
22
init() is
23
clear()
24
si
25
26
dump_counts() is
27
_symbol_use_map.dump_counts()
28
Std.error.write_line("symbol reference map: {_symbol_reference_map.count}")
29
si
30
31
clear() is
32
_symbol_use_map = LOCATION_MAP[Symbols.Symbol]()
33
_hover_info_map = LOCATION_MAP[HOVER_USE]()
34
_symbol_reference_map = Collections.MAP[Symbols.Symbol,Collections.SET[LOCATION]](65521)
35
_reference_frames = Collections.LIST[Collections.LIST[Pair[Symbols.Symbol,LOCATION]]]()
36
si
37
38
// Speculation frames over everything this store records: hover
39
// uses, symbol uses and reference-set entries. The iterative
40
// inference re-walks (a lambda body walked again once its
41
// parameter types settle, and the whole-body retry loop around
42
// it) roll their diagnostics back before each re-walk; without
43
// the same discipline here, every discarded walk leaves its
44
// recorded uses behind — a hover, definition target or
45
// reference resolved against not-yet-settled types sits at the
46
// same span as the settled walk's record and can win the
47
// tie-break. Callers open a frame wherever they speculate the
48
// logger AND are guaranteed to re-walk everything recorded
49
// since, so a rolled-back frame's information is always
50
// re-recorded by the walk whose compilation stands.
51
_reference_frames: Collections.LIST[Collections.LIST[Pair[Symbols.Symbol,LOCATION]]]
52
53
speculate() is
54
_symbol_use_map.speculate()
55
_hover_info_map.speculate()
56
_reference_frames.add(Collections.LIST[Pair[Symbols.Symbol,LOCATION]]())
57
si
58
59
roll_back() is
60
assert _reference_frames.count > 0 else "roll_back with no open uses speculation frame"
61
62
_symbol_use_map.roll_back()
63
_hover_info_map.roll_back()
64
65
let frame = _reference_frames[_reference_frames.count - 1]
66
_reference_frames.remove_at(_reference_frames.count - 1)
67
68
let i mut = frame.count - 1
69
70
while i >= 0 do
71
let entry = frame[i]
72
let references: Collections.SET[LOCATION] mut
73
74
if _symbol_reference_map.try_get_value(entry.key, references ref) then
75
references.remove(entry.value)
76
fi
77
78
i = i - 1
79
od
80
si
81
82
commit() is
83
assert _reference_frames.count > 0 else "commit with no open uses speculation frame"
84
85
_symbol_use_map.commit()
86
_hover_info_map.commit()
87
88
let frame = _reference_frames[_reference_frames.count - 1]
89
_reference_frames.remove_at(_reference_frames.count - 1)
90
91
if _reference_frames.count > 0 then
92
_reference_frames[_reference_frames.count - 1].add_range(frame)
93
fi
94
si
95
96
mark() -> int => _reference_frames.count
97
98
release(mark: int) is
99
while _reference_frames.count > mark do
100
roll_back()
101
od
102
si
103
104
// Exception-recovery guard: `let use` one of these around a
105
// walk that opens speculation frames, so frames left open by a
106
// throw are rolled back instead of desynchronising the stack.
107
// On the normal path all frames are already closed and dispose
108
// is a no-op.
109
mark_then_release() -> USES_MARK_THEN_RELEASE =>
110
USES_MARK_THEN_RELEASE(self)
111
112
add_symbol_use(location: LOCATION, symbol: Symbols.Symbol) is
113
_add_use(location, symbol, null, null)
114
si
115
116
// The type an expression took from the context it sits in,
117
// where the source says what to do but never names the type:
118
// `cast(v)` and a bare `_`. Hover is the only place a reader
119
// can see what those resolved to, so it is recorded for hover
120
// alone. Deliberately not a symbol use — the expression is not
121
// an occurrence of the type's name, so it must not turn up
122
// among that type's references, answer go-to-definition, or
123
// colour as a type name.
124
add_inferred_type_hover(location: LOCATION, type: Types.Type) is
125
let symbol = type.symbol
126
127
if
128
_suppress_depth > 0 \/
129
location.is_internal \/
130
location.is_reflected \/
131
symbol.is_internal
132
then
133
return
134
fi
135
136
_hover_info_map.put(
137
location,
138
HOVER_USE(symbol, null, type, IoC.CONTAINER.instance.symbol_table.current_scope)
139
)
140
si
141
142
// Reconcile one file's recorded uses after an interface-preserving
143
// incremental EDIT, before the body re-walk re-records its bodies.
144
//
145
// The edited file's entries split three ways:
146
// - inside a re-walked body — discarded here; the re-walk
147
// re-records them in current coordinates;
148
// - at a retained interface node — not re-walked, so moved here
149
// to that node's post-edit location (`correspondence`);
150
// - other files — untouched.
151
//
152
// Without this the re-walk's records pile up on top of the stale
153
// ones (duplicate, wrong-line find-references) and the retained
154
// interface keeps pre-edit line numbers.
155
refresh_edited_file(
156
file_name: string,
157
correspondence: Source.LOCATION_CORRESPONDENCE,
158
body_spans: Source.BODY_SPANS
159
) is
160
// _symbol_use_map and _symbol_reference_map hold the same set
161
// of (location, symbol) facts — rebuild both from the use
162
// map's entries.
163
let use_entries = _symbol_use_map.file_entries(file_name)
164
165
_symbol_use_map.remove_file(file_name)
166
167
for entry in use_entries do
168
let location = entry.location
169
let symbol = entry.value
170
171
let references = _get_references_or_empty(symbol)
172
173
references.remove(location)
174
175
let reconciled = correspondence.translate(location)
176
177
if reconciled? then
178
// A retained interface node — move the entry to its
179
// post-edit location. When the entry is the symbol's
180
// own definition site, move the retained symbol with
181
// it — both its name location and its declaration span
182
// (a separate field on functions / classes / traits /
183
// properties). The match fails on a later pass (the
184
// symbol now holds `reconciled`), so this is idempotent.
185
if location =~ symbol.location then
186
let new_span = correspondence.translate(symbol.span)
187
188
symbol.set_location(reconciled)
189
190
if new_span? then
191
symbol.set_span(new_span)
192
fi
193
fi
194
195
_symbol_use_map.put(reconciled, symbol)
196
197
references.add(reconciled)
198
elif !body_spans.contains(location.start) then
199
// Neither a reconciled interface node nor inside a
200
// re-walked body — keep it unchanged rather than drop
201
// it (dropping would silently lose the reference).
202
_symbol_use_map.put(location, symbol)
203
204
references.add(location)
205
fi
206
// else: inside a re-walked body — dropped; the re-walk
207
// re-records it in current coordinates.
208
od
209
210
let hover_entries = _hover_info_map.file_entries(file_name)
211
212
_hover_info_map.remove_file(file_name)
213
214
for entry in hover_entries do
215
let reconciled = correspondence.translate(entry.location)
216
217
if reconciled? then
218
_hover_info_map.put(reconciled, entry.value)
219
elif !body_spans.contains(entry.location.start) then
220
_hover_info_map.put(entry.location, entry.value)
221
fi
222
od
223
si
224
225
// Drop every use and hover entry recorded inside `span`: a body
226
// about to be re-walked re-records its own, and the older
227
// entry would otherwise win the same-location tie and keep
228
// reporting the shape the first walk saw.
229
drop_within(file_name: string, span: LOCATION) is
230
let use_entries = _symbol_use_map.file_entries(file_name)
231
232
_symbol_use_map.remove_file(file_name)
233
234
for entry in use_entries do
235
let location = entry.location
236
let symbol = entry.value
237
238
if span.contains(location) then
239
_get_references_or_empty(symbol).remove(location)
240
else
241
_symbol_use_map.put(location, symbol)
242
fi
243
od
244
245
let hover_entries = _hover_info_map.file_entries(file_name)
246
247
_hover_info_map.remove_file(file_name)
248
249
for entry in hover_entries do
250
if !span.contains(entry.location) then
251
_hover_info_map.put(entry.location, entry.value)
252
fi
253
od
254
si
255
256
// A symbol use that also carries the use-site AST node, so
257
// HOVER can read the type observed at this occurrence off
258
// the node instead of the symbol — see HOVER_USE.description.
259
add_variable_use(location: LOCATION, symbol: Symbols.Symbol, value: IR.Values.Value) is
260
_add_use(location, symbol, value, null)
261
si
262
263
// A symbol use whose observed type is supplied directly —
264
// the assignment-target case, where the type this occurrence
265
// should report (the state the assignment leaves behind) is
266
// not the type of any single IR node.
267
add_variable_use(location: LOCATION, symbol: Symbols.Symbol, observed_type: Types.Type) is
268
_add_use(location, symbol, null, observed_type)
269
si
270
271
// Replace any existing hover entry at exactly this location
272
// with a narrowed one — used by visit_member when path
273
// narrowing wraps the receiver in a NARROW_VIEW / NARROW_PROJECT
274
// after an earlier `add_symbol_use` has already recorded the
275
// symbol without a value. Without the replace, both entries
276
// sit at the same location and the tie-break picks the older
277
// (unnarrowed) one.
278
replace_with_variable_use(location: LOCATION, symbol: Symbols.Symbol?, value: IR.Values.Value) is
279
if
280
_suppress_depth > 0 \/
281
!symbol? \/
282
location.is_internal \/
283
location.is_reflected \/
284
symbol.is_internal
285
then
286
return
287
fi
288
289
_hover_info_map.put_replacing(location, HOVER_USE(symbol, value, null, IoC.CONTAINER.instance.symbol_table.current_scope))
290
291
let root = symbol.root_specialized_from
292
293
_symbol_use_map.put(location, root)
294
_add_symbol_reference(location, root)
295
si
296
297
// Speculative walks (the assignment left-type probe)
298
// compile an expression purely to read a type off it; the
299
// uses they record would duplicate — and, recorded first at
300
// an assignment target, out-rank — the ones the real walk
301
// records. A depth so nested probes compose.
302
_suppress_depth: int
303
304
begin_suppress() is
305
_suppress_depth = _suppress_depth + 1
306
si
307
308
end_suppress() is
309
_suppress_depth = _suppress_depth - 1
310
si
311
312
_add_use(location: LOCATION, symbol: Symbols.Symbol? mut, value: IR.Values.Value?, observed_type: Types.Type?) is
313
if
314
_suppress_depth > 0 \/
315
!symbol? \/
316
location.is_internal \/
317
location.is_reflected \/
318
symbol.is_internal
319
then
320
return
321
fi
322
323
// A symbol's own declaration renders relative to its enclosing
324
// namespace, so a member keeps its type qualifier (COLOR.RED)
325
// rather than resolving bare against the type it is declared in.
326
let symbol_table = IoC.CONTAINER.instance.symbol_table
327
let render_scope =
328
if location =~ symbol.location then
329
symbol_table.current_namespace_scope
330
else
331
symbol_table.current_scope
332
fi
333
334
_hover_info_map.put(location, HOVER_USE(symbol, value, observed_type, render_scope))
335
336
symbol = symbol.root_specialized_from
337
338
_symbol_use_map.put(location, symbol)
339
_add_symbol_reference(location, symbol)
340
si
341
342
// Ranks symbol uses that share a source range; the higher rank
343
// wins a hover / go-to-definition / semantic-token tie. A symbol
344
// whose own definition site *is* this range ranks lowest: the
345
// if-let leaf-name shorthand declares its synthesised local on
346
// the scrutinee's member token (`if let x.y.z?` defines `z` at
347
// the `.z` access), so a use recorded there should describe the
348
// member the value came from, not the local derived from it. A
349
// resolved Function outranks an ordinary use so a call target
350
// still wins over a co-recorded overload-group or type name.
351
_hover_priority(location: LOCATION, symbol: Symbols.Symbol?) -> int is
352
if !symbol? then
353
return 0
354
fi
355
356
if symbol.location =~ location then
357
return 0
358
fi
359
360
if isa Symbols.Function(symbol) then
361
return 2
362
fi
363
364
return 1
365
si
366
367
find_hover_use(file_name: string, line: int, column: int) -> HOVER_USE? is
368
let matches = _hover_info_map.find_all(file_name, line, column)
369
370
// find_all returns null when nothing matches
371
@suppress("presence-test-non-optional")
372
if !matches? \/ matches.count == 0 then
373
return null
374
elif matches.count == 1 then
375
return matches[0].value
376
fi
377
378
let shortest_length mut = 1_000_000_000
379
let best_match: HOVER_USE? mut = null
380
let best_priority mut = -1
381
382
for m in matches do
383
let length = m.location.length
384
385
if length < shortest_length then
386
best_match = m.value
387
shortest_length = length
388
best_priority = _hover_priority(m.location, m.value.symbol)
389
elif length == shortest_length then
390
let priority = _hover_priority(m.location, m.value.symbol)
391
392
if priority > best_priority then
393
best_match = m.value
394
best_priority = priority
395
fi
396
fi
397
od
398
399
return best_match
400
si
401
402
// Every use of a symbol recorded in the file, with the symbol it
403
// resolved to: the resolved member rather than its overload group
404
// where both were recorded, as hover prefers. A type shown only as
405
// an inferred-type hover is not a use of it.
406
symbol_uses_in_file(file_name: string) -> List[LOCATION_SEARCH_RESULT[Symbols.Symbol]] is
407
let used = Collections.SET[LOCATION]()
408
409
for entry in _symbol_use_map.file_entries(file_name) do
410
used.add(entry.location)
411
od
412
413
let result = LIST[LOCATION_SEARCH_RESULT[Symbols.Symbol]]()
414
415
for entry in hover_uses_in_file(file_name) do
416
if used.contains(entry.location) then
417
result.add(LOCATION_SEARCH_RESULT[Symbols.Symbol](entry.location, entry.value.symbol))
418
fi
419
od
420
421
return result
422
si
423
424
// Every HOVER_USE recorded for one file, one per source range —
425
// powering #HOVERMAP#, a whole-file hover dump the ghul.dev
426
// example pipeline consumes offline instead of probing position
427
// by position with #HOVER#. Where several uses share a range (an
428
// overload group and the resolved member both record the
429
// call-target identifier) the resolved Function is preferred,
430
// mirroring find_hover_use so #HOVERMAP# and #HOVER# agree.
431
hover_uses_in_file(file_name: string) -> List[LOCATION_SEARCH_RESULT[HOVER_USE]] is
432
let best = Collections.MAP[LOCATION, HOVER_USE]()
433
434
for entry in _hover_info_map.file_entries(file_name) do
435
let current: HOVER_USE mut
436
437
if !best.try_get_value(entry.location, current ref) then
438
best[entry.location] = entry.value
439
elif
440
_hover_priority(entry.location, entry.value.symbol) >
441
_hover_priority(entry.location, current.symbol)
442
then
443
best[entry.location] = entry.value
444
fi
445
od
446
447
let result = LIST[LOCATION_SEARCH_RESULT[HOVER_USE]]()
448
449
for kv in best do
450
result.add(LOCATION_SEARCH_RESULT[HOVER_USE](kv.key, kv.value))
451
od
452
453
return result
454
si
455
456
find_definition_from_use(file_name: string, line: int, column: int) -> Symbols.Symbol? =>
457
let matches = _symbol_use_map.find_all(file_name, line, column) in
458
find_best_match(matches)
459
460
find_best_match(matches: Collections.List[LOCATION_SEARCH_RESULT[Symbols.Symbol]]?) -> Symbols.Symbol? =>
461
if !matches? \/ matches.count == 0 then
462
null
463
elif matches.count == 1 then
464
matches[0].value
465
else
466
let shortest_length mut = 1_000_000_000
467
let best_match: Symbols.Symbol? mut = null
468
let best_priority mut = -1
469
470
for m in matches do
471
let location = m.location
472
let symbol = m.value
473
let length = location.length
474
475
if length < shortest_length then
476
best_match = symbol
477
shortest_length = length
478
best_priority = _hover_priority(location, symbol)
479
elif length == shortest_length then
480
let priority = _hover_priority(location, symbol)
481
482
if priority > best_priority then
483
best_match = symbol
484
best_priority = priority
485
fi
486
fi
487
od
488
489
best_match
490
fi
491
492
// - include definitions only
493
// - don't include the definition location of the seached symbol itself unless no other matches found
494
// - for methods and properties, include all definitions that override the searched symbol
495
// - for classes and traits, include all definitions that inherit from the searched symbol
496
// - for other symbols, return only the searched symbol
497
find_declarations_of_symbol(symbol: Symbols.Symbol mut) -> Collections.Iterable[LOCATION] is
498
symbol = symbol.root_specialized_from
499
500
let results = Collections.SET[Symbols.Symbol]()
501
502
if symbol.is_classy then
503
_get_super_class(symbol, results)
504
else
505
_get_overridees(symbol, results)
506
fi
507
508
if results.count == 0 then
509
results.add(symbol)
510
fi
511
512
return results |> map(s => s.location)
513
si
514
515
// - include definitions only
516
// - include the definition location of the seached symbol itself
517
// - for methods and properties, include all definitions that override the searched symbol
518
// - for classes and traits, include all definitions that inherit from the searched symbol
519
// - for other symbols, return only the searched symbol
520
find_implementations_of_symbol(symbol: Symbols.Symbol mut) -> Collections.Iterable[LOCATION] is
521
symbol = symbol.root_specialized_from
522
523
let results = Collections.SET[Symbols.Symbol]()
524
525
if symbol.is_classy then
526
_get_all_implementing_symbols(symbol, results)
527
else
528
_get_all_overriding_symbols(symbol, results)
529
fi
530
531
return results |>map(s => s.location)
532
si
533
534
// - include uses but not definitions
535
// - for methods and properties, search up the inheritance tree to find the root symbols that are overridden
536
// - include references to all symbols that override the root overridee symbols
537
// - for classes, traits and other symbols, include only references to the searched symbol
538
// Uses bound to exactly this symbol (normalized to its
539
// unspecialized root), without the override family
540
// find-references folds in. The incremental interface edit's
541
// teardown guard wants precisely the sites bound to the outgoing
542
// symbol: a call bound to an overridden base member stays valid
543
// when an override of it is removed.
544
direct_references_to(symbol: Symbols.Symbol) -> Collections.Iterable[LOCATION] =>
545
_get_references_or_empty(symbol.root_specialized_from)
546
547
find_references_to_symbol(symbol: Symbols.Symbol mut) -> Collections.Iterable[LOCATION] is
548
symbol = symbol.root_specialized_from
549
550
let definitions = Collections.SET[Symbols.Symbol]()
551
552
_collect_override_family(symbol, definitions)
553
554
let results = Collections.SET[LOCATION]()
555
556
_get_use_locations_for_symbols(definitions, results, false)
557
558
return results
559
si
560
561
// - include definitions and references
562
// - include references to the seached symbol itself
563
// - for methods and properties, search up the inheritance tree to find the root symbols that are overridden
564
// - include references to all symbols that override the root overridee symbols
565
// - for classes, traits and other symbols, include only references to the searched symbol
566
find_references_to_symbol_for_rename(symbol: Symbols.Symbol mut) -> Collections.Iterable[LOCATION] is
567
symbol = symbol.root_specialized_from
568
569
// A construction site records both the constructor and the
570
// type it constructs at the same span, and the constructor
571
// wins the best-match tie. Renaming a type from one of its
572
// construction sites should rename the type, so redirect a
573
// constructor to its owning type rather than refusing.
574
if symbol.is_constructor /\ isa Symbols.Symbol(symbol.owner) then
575
symbol = (cast Symbols.Symbol?(symbol.owner)!).root_specialized_from
576
fi
577
578
if symbol.is_constructor then
579
return Collections.SET[LOCATION]()
580
fi
581
582
let definitions = Collections.SET[Symbols.Symbol]()
583
584
_collect_override_family(symbol, definitions)
585
586
let results = Collections.SET[LOCATION]()
587
588
_get_use_locations_for_symbols(definitions, results, true)
589
590
results.add(symbol.location)
591
592
return results
593
si
594
595
// Every method/property symbol whose uses share a single rename or
596
// find-references identity with `symbol`: the symbol itself, every
597
// root overridee reachable up the inheritance chain, and every
598
// symbol overriding those roots. For a classy or non-overridable
599
// symbol the family is just the symbol itself.
600
_collect_override_family(symbol: Symbols.Symbol, definitions: Collections.SET[Symbols.Symbol]) is
601
if symbol.is_classy then
602
definitions.add(symbol)
603
return
604
fi
605
606
let root_overridees = Collections.SET[Symbols.Symbol]()
607
608
_get_root_overridees(symbol, root_overridees)
609
610
for overridee in root_overridees do
611
_get_all_overriding_symbols(overridee, definitions)
612
od
613
si
614
615
_add_symbol_reference(location: LOCATION, symbol: Symbols.Symbol mut) is
616
symbol = symbol.root_specialized_from
617
618
let refs = _get_references_set(symbol)
619
620
// Re-walks of the same body (inference retries) legitimately
621
// re-record the same symbol at the same location; the set
622
// membership check keeps the entry single and the journal
623
// records only genuine additions so roll_back removes
624
// exactly what this frame added.
625
if !refs.contains(location) then
626
refs.add(location)
627
628
if _reference_frames.count > 0 then
629
_reference_frames[_reference_frames.count - 1].add(Pair[Symbols.Symbol,LOCATION](symbol, location))
630
fi
631
fi
632
si
633
634
_get_use_locations_for_symbols(
635
symbols: Collections.Iterable[Symbols.Symbol],
636
into: Collections.SET[LOCATION],
637
include_definitions: bool
638
) is
639
for d in symbols do
640
if include_definitions then
641
into.add(d.location)
642
fi
643
644
let references = _get_references_or_empty(d)
645
646
for reference in references do
647
if include_definitions \/ reference !~ d.location then
648
into.add(reference)
649
fi
650
od
651
od
652
si
653
654
_get_root_overridees(symbol: Symbols.Symbol, results: Collections.SET[Symbols.Symbol]) is
655
if results.contains(symbol) then
656
return
657
fi
658
659
let overridees = symbol.overridees
660
661
if !overridees? \/ overridees |> count() == 0 \/ (overridees |> find(o => o.is_reflected)).has_value then
662
results.add(symbol)
663
return
664
fi
665
666
for overridee in overridees do
667
_get_root_overridees(overridee, results)
668
od
669
si
670
671
_get_overridees(symbol: Symbols.Symbol, results: Collections.SET[Symbols.Symbol]) is
672
let overridees = symbol.overridees
673
674
if !overridees? then
675
return
676
fi
677
678
for overridee in overridees |> filter(overridee => !overridee.is_internal /\ !overridee.is_reflected) do
679
results.add(overridee)
680
od
681
si
682
683
_get_super_class(symbol: Symbols.Symbol, results: Collections.SET[Symbols.Symbol]) is
684
let ancestors = symbol.ancestors
685
686
if ancestors.count == 0 then
687
return
688
fi
689
690
let result = ancestors[0].symbol
691
692
if !result.is_internal /\ !result.is_reflected then
693
results.add(result)
694
fi
695
si
696
697
// walk down the tree adding overriding methods
698
_get_all_overriding_symbols(symbol: Symbols.Symbol, results: Collections.SET[Symbols.Symbol]) is
699
if results.contains(symbol) then
700
return
701
fi
702
703
results.add(symbol)
704
705
let overriders = symbol.overriders
706
707
if !overriders? \/ overriders |> count() == 0 then
708
return
709
fi
710
711
for overrider in overriders do
712
_get_all_overriding_symbols(overrider, results)
713
od
714
si
715
716
// walk down the tree adding implementing classes
717
_get_all_implementing_symbols(symbol: Symbols.Symbol, results: Collections.SET[Symbols.Symbol]) is
718
if results.contains(symbol) then
719
return
720
fi
721
722
results.add(symbol)
723
724
let implementors = symbol.implementors
725
726
if !implementors? \/ implementors |> count() == 0 then
727
return
728
fi
729
730
for implementor in implementors do
731
_get_all_implementing_symbols(implementor, results)
732
od
733
si
734
735
_get_symbol_references_for_rename(symbol: Symbols.Symbol) -> Collections.SET[LOCATION] is
736
let all_definitions = Collections.SET[Symbols.Symbol]()
737
738
let root_overridees = Collections.SET[Symbols.Symbol]()
739
740
_get_root_overridees(symbol, root_overridees)
741
742
for root_overridee in root_overridees do
743
_get_all_overriding_symbols(root_overridee, all_definitions)
744
od
745
746
let results = Collections.SET[LOCATION]()
747
748
for d in all_definitions do
749
let references = _get_references_or_empty(d)
750
751
for reference in references do
752
results.add(reference)
753
od
754
od
755
756
return results
757
si
758
759
_get_references_or_empty(symbol: Symbols.Symbol) -> Collections.SET[LOCATION] is
760
let results: Collections.SET[LOCATION] mut
761
762
if _symbol_reference_map.try_get_value(symbol, results ref) then
763
return results
764
fi
765
766
return Collections.SET[LOCATION]()
767
si
768
769
_get_references_set(symbol: Symbols.Symbol mut) -> Collections.SET[LOCATION] is
770
// FIXME: is this correct in all cases?
771
symbol = symbol.root_specialized_from
772
let results: Collections.SET[LOCATION] mut
773
774
if !_symbol_reference_map.try_get_value(symbol, results ref) then
775
results = Collections.SET[LOCATION]()
776
_symbol_reference_map[symbol] = results
777
fi
778
779
return results
780
si
781
si
782
783
struct USES_MARK_THEN_RELEASE: Disposable is
784
_uses: SYMBOL_USE_LOCATIONS
785
_mark: int
786
787
init(uses: SYMBOL_USE_LOCATIONS) is
788
_uses = uses
789
_mark = uses.mark()
790
si
791
792
dispose() is
793
_uses.release(_mark)
794
si
795
si
796
797
// One reversible mutation of a LOCATION_MAP line-list, journalled
798
// while a speculation frame is open so the frame can be undone.
799
// APPENDED needs no payload: undo happens in strict reverse order,
800
// so the appended entry is still the list's tail when its turn
801
// comes. REMOVED re-inserts the entry at its original index, which
802
// is valid at undo time for the same reason.
803
union LocationMapOp[T] is
804
APPENDED(list: Collections.LIST[Pair[LOCATION,T]])
805
REMOVED(list: Collections.LIST[Pair[LOCATION,T]], entry: Pair[LOCATION,T], index: int)
806
si
807
808
class LOCATION_MAP[T] is
809
_file_name_to_file: Collections.MAP[string, Collections.MAP[int, Collections.LIST[Pair[LOCATION,T]]]]
810
811
// Speculation frames. While at least one frame is open, every
812
// mutation journals a LocationMapOp into the innermost frame;
813
// roll_back undoes the frame's ops in reverse, commit folds them
814
// into the enclosing frame (so an outer roll_back also undoes
815
// inner committed work), and at the bottom of the stack commit
816
// makes the entries permanent. With no frame open, mutations
817
// are permanent immediately and cost nothing extra.
818
_frames: Collections.LIST[Collections.LIST[LocationMapOp[T]]]
819
820
init() is
821
_file_name_to_file = Collections.MAP[string, Collections.MAP[int, Collections.LIST[Pair[LOCATION,T]]]]()
822
_frames = Collections.LIST[Collections.LIST[LocationMapOp[T]]]()
823
si
824
825
speculate() is
826
_frames.add(Collections.LIST[LocationMapOp[T]]())
827
si
828
829
roll_back() is
830
assert _frames.count > 0 else "roll_back with no open speculation frame"
831
832
let frame = _frames[_frames.count - 1]
833
_frames.remove_at(_frames.count - 1)
834
835
let i mut = frame.count - 1
836
837
while i >= 0 do
838
let op = frame[i]
839
840
if let appended: LocationMapOp.APPENDED[T] = op then
841
appended.list.remove_at(appended.list.count - 1)
842
elif let removed: LocationMapOp.REMOVED[T] = op then
843
removed.list.insert(removed.index, removed.entry)
844
fi
845
846
i = i - 1
847
od
848
si
849
850
commit() is
851
assert _frames.count > 0 else "commit with no open speculation frame"
852
853
let frame = _frames[_frames.count - 1]
854
_frames.remove_at(_frames.count - 1)
855
856
if _frames.count > 0 then
857
_frames[_frames.count - 1].add_range(frame)
858
fi
859
si
860
861
mark() -> int => _frames.count
862
863
release(mark: int) is
864
while _frames.count > mark do
865
roll_back()
866
od
867
si
868
869
_journal(op: LocationMapOp[T]) is
870
if _frames.count > 0 then
871
_frames[_frames.count - 1].add(op)
872
fi
873
si
874
875
dump_counts() is
876
Std.error.write_line("file name to file map: {_file_name_to_file.count}")
877
si
878
879
put(location: LOCATION, value: T) is
880
let existing = _get_file(location.file_name)
881
882
let file =
883
if existing? then
884
existing
885
else
886
let created = Collections.MAP[int, Collections.LIST[Pair[LOCATION,T]]]()
887
_file_name_to_file[location.file_name] = created
888
created
889
fi
890
891
let start_line = location.start_line
892
let end_line = location.end_line
893
894
let list: Collections.LIST[Pair[LOCATION,T]] mut
895
896
for line in start_line::end_line do
897
if file.contains_key(line) then
898
list = file[line]
899
else
900
list = Collections.LIST[Pair[LOCATION,T]]()
901
file[line] = list
902
fi
903
list.add(Pair[LOCATION,T](location,value))
904
_journal(LocationMapOp.APPENDED[T](list))
905
od
906
si
907
908
// Like `put`, but first drops every existing entry whose
909
// location matches exactly — the caller is stamping fresh
910
// information (a flow-narrowed observed type on a member
911
// access that was earlier recorded as a plain symbol use)
912
// and wants to replace, not accumulate.
913
put_replacing(location: LOCATION, value: T) is
914
let existing = _get_file(location.file_name)
915
if existing? then
916
let start_line = location.start_line
917
let end_line = location.end_line
918
for line in start_line::end_line do
919
if existing.contains_key(line) then
920
let list = existing[line]
921
let i mut = list.count - 1
922
while i >= 0 do
923
if list[i].key =~ location then
924
_journal(LocationMapOp.REMOVED[T](list, list[i], i))
925
list.remove_at(i)
926
fi
927
i = i - 1
928
od
929
fi
930
od
931
fi
932
933
put(location, value)
934
si
935
936
find_all(file_name: string, line: int, column: int) -> List[LOCATION_SEARCH_RESULT[T]]? is
937
let file = _get_file(file_name)
938
939
if !file? \/ !file.contains_key(line) then
940
return null
941
fi
942
943
let list = file[line]
944
945
let line_column = LOCATION.pair(line, column)
946
947
let result = LIST[LOCATION_SEARCH_RESULT[T]]()
948
949
for p in list do
950
if p.key.contains(line_column) then
951
result.add(LOCATION_SEARCH_RESULT[T](p.key, p.value))
952
fi
953
od
954
955
return result
956
si
957
958
// Every stored entry for one file. `put` records a multi-line
959
// location once per line it spans; each is yielded once, on its
960
// own start line. Several entries at the same location (e.g. an
961
// overload group and the resolved member) are all kept — the
962
// caller chooses between them.
963
file_entries(file_name: string) -> List[LOCATION_SEARCH_RESULT[T]] is
964
let result = LIST[LOCATION_SEARCH_RESULT[T]]()
965
966
let file = _get_file(file_name)
967
968
if !file? then
969
return result
970
fi
971
972
for line_entry in file do
973
let line = line_entry.key
974
975
for p in line_entry.value do
976
if p.key.start_line == line then
977
result.add(LOCATION_SEARCH_RESULT[T](p.key, p.value))
978
fi
979
od
980
od
981
982
return result
983
si
984
985
_get_file(file_name: string) -> Collections.MAP[int, Collections.LIST[Pair[LOCATION,T]]]? =>
986
if _file_name_to_file.contains_key(file_name) then
987
_file_name_to_file[file_name]
988
else
989
null
990
fi
991
992
// Drop every entry for one file. The incremental body re-walk
993
// rebuilds the edited file's entries; see
994
// SYMBOL_USE_LOCATIONS.refresh_edited_file.
995
remove_file(file_name: string) is
996
_file_name_to_file.remove(file_name)
997
si
998
si
999
1000
struct LOCATION_SEARCH_RESULT[T] is
1001
location: LOCATION
1002
value: T
1003
1004
init(location: LOCATION, value: T) is
1005
self.location = location
1006
self.value = value
1007
si
1008
si
1009
1010
// What HOVER knows about one symbol occurrence: the symbol
1011
// itself, plus — for variable uses — the use-site AST node.
1012
class HOVER_USE is
1013
symbol: Symbols.Symbol public
1014
value: IR.Values.Value? public
1015
observed_type: Types.Type? public
1016
1017
// The scope enclosing the use, captured when it was recorded, so
1018
// the hover renders names relative to where the reader's cursor
1019
// is rather than fully qualified.
1020
scope: Scope? public
1021
1022
init(symbol: Symbols.Symbol, value: IR.Values.Value?, observed_type: Types.Type?, scope: Scope?) is
1023
self.symbol = symbol
1024
self.value = value
1025
self.observed_type = observed_type
1026
self.scope = scope
1027
si
1028
1029
// For a variable use, the resolved narrowed type — from the
1030
// recorded observed type or the use-site value — provided it's
1031
// fully settled. Flow-sensitive narrowing mutates a Variable's
1032
// `type` field during the compile walk and restores it after,
1033
// so by hover-request time `type` is the declared shape; the
1034
// observed type reaches us via the recorder. Node types can
1035
// freeze a partly-inferred form (`LIST[***]`), so we only
1036
// trust settled ones; the symbol's own type is a better fall
1037
// back for unsettled cases.
1038
observed_type_for_narrowing() -> Types.Type? is
1039
// Read the occurrence value into a local: presence
1040
// narrowing holds across the member accesses below for a
1041
// local, not for the `value` property, whose getter call
1042
// cannot carry a narrow.
1043
let occurrence = value
1044
1045
let observed =
1046
if observed_type? then
1047
observed_type
1048
elif occurrence? /\ occurrence.type? then
1049
occurrence.type
1050
else
1051
null
1052
fi
1053
1054
if !observed? \/ !observed.is_settled then
1055
return null
1056
fi
1057
1058
return observed
1059
si
1060
1061
// Build a describe-context that carries this occurrence's
1062
// observed (narrowed) type keyed by the collapsed symbol —
1063
// the shape both HOVER_USE's own kind_label / description
1064
// accessors and `Analysis.SIGNATURE_DOC` want.
1065
context() -> Symbols.DESCRIBE_CONTEXT is
1066
let observed = observed_type_for_narrowing()
1067
if !observed? then
1068
return Symbols.DESCRIBE_CONTEXT.instance
1069
fi
1070
let map = Collections.MAP[Symbols.Symbol, Types.Type]()
1071
map[symbol.collapse_group_if_single_member()] = observed
1072
return Symbols.DESCRIBE_CONTEXT.with_observed_types(map)
1073
si
1074
1075
// Single-line hover text with the classifier appended as a trailing
1076
// `// kind` comment. The wire's own `signature` field is rendered
1077
// separately against a column budget by `Analysis.SIGNATURE_DOC`.
1078
description: string is
1079
let s = symbol.collapse_group_if_single_member()
1080
let ctx = context()
1081
let sig = _render_in_scope(s, ctx)
1082
let kind = s.describe_kind(ctx)
1083
if kind? then
1084
return "{sig} // {kind}"
1085
fi
1086
return sig
1087
si
1088
1089
// Render the symbol's signature with names shortened relative to
1090
// the use's own scope, restoring the previous render scope even if
1091
// rendering throws.
1092
_render_in_scope(s: Symbols.Symbol, ctx: Symbols.DESCRIBE_CONTEXT) -> string is
1093
let use render_scope = IoC.CONTAINER.instance.name_display.with_scope(scope)
1094
1095
return Symbols.TEXT_RENDERER(ctx).render(s.describe(ctx))
1096
si
1097
1098
// Human-readable classifier (`instance method`, `local variable`,
1099
// `class`, `variant`, …) or null when the symbol has none —
1100
// namespaces and labels.
1101
kind_label: string? is
1102
let s = symbol.collapse_group_if_single_member()
1103
return s.describe_kind(context())
1104
si
1105
si
1106
si