Skip to content
← Back

src/analysis/command_handlers.ghul

1
@@IF.dotnet()
2
3
namespace Analysis is
4
use System.Exception
5
use IO.Std
6
7
use Collections.Iterable
8
9
use Pair = Collections.KeyValuePair
10
11
use IoC
12
use Logging
13
use Source
14
use Compiler
15
16
use Ghul.Pipes
17
18
use Protocol.JSON_PROTOCOL
19
20
// Generic base for every command handler. The despatcher holds handlers as
21
// the non-generic CommandHandler trait and hands each a deserialized
22
// Protocol.Request; this base does the single cast to the concrete request
23
// type T and forwards to handle_request, so no individual handler casts.
24
class RequestHandler[T]: CommandHandler is
25
init() is si
26
27
handle(request: Protocol.Request, writer: IO.TextWriter) is
28
handle_request(cast T(request), writer)
29
si
30
31
handle_request(request: T, writer: IO.TextWriter)
32
si
33
34
class HOVER_HANDLER(
35
_watchdog: WATCHDOG,
36
_timers: TIMERS,
37
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
38
_full_compiler: FULL_COMPILER
39
): RequestHandler[Protocol.Request.HOVER] is
40
super()
41
42
// A constructor call reads as the type it constructs, so a
43
// constructor with no doc of its own shows its type's.
44
_doc_for(symbol: Semantic.Symbols.Symbol) -> string? is
45
if let doc = symbol.doc then
46
return doc
47
fi
48
49
if symbol.is_constructor then
50
return (cast Semantic.Symbols.Symbol?(symbol.owner))?.doc
51
fi
52
53
return null
54
si
55
56
// As with the doc, a constructor with no deprecation of its own
57
// shows its type's.
58
_deprecation_for(symbol: Semantic.Symbols.Symbol) -> Semantic.DEPRECATION? is
59
if let deprecation = symbol.deprecation then
60
return deprecation
61
fi
62
63
if symbol.is_constructor then
64
return (cast Semantic.Symbols.Symbol?(symbol.owner))?.deprecation
65
fi
66
67
return null
68
si
69
70
handle_request(request: Protocol.Request.HOVER, writer: IO.TextWriter) is
71
let path = request.path
72
let line = request.line
73
let column = request.column
74
75
let signature: string mut = ""
76
let kind_label: string? mut = null
77
let description: string mut = ""
78
let doc: string? mut = null
79
let deprecation: Semantic.DEPRECATION? mut = null
80
81
try
82
if !_watchdog.want_restart then
83
let hover mut = _symbol_use_locations.find_hover_use(path, line, column)
84
85
if !hover? then
86
_full_compiler.compile_all(writer, path)
87
88
hover = _symbol_use_locations.find_hover_use(path, line, column)
89
fi
90
91
if hover? then
92
signature = SIGNATURE_DOC.build(hover)
93
kind_label = hover.kind_label
94
description = hover.description
95
doc = _doc_for(hover.symbol.collapse_group_if_single_member())
96
deprecation = _deprecation_for(hover.symbol.collapse_group_if_single_member())
97
fi
98
fi
99
catch ex: Exception
100
debug_always("HOVER caught: {ex.get_type()} {ex.message}")
101
102
_watchdog.request_restart()
103
yrt
104
105
Std.error.flush()
106
107
JSON_PROTOCOL.write_response(writer, Protocol.Response.HOVER(
108
signature,
109
kind_label,
110
description,
111
doc,
112
deprecation?,
113
deprecation?.message,
114
deprecation?.is_error ?? false))
115
si
116
si
117
118
// Dumps every HOVER_USE recorded for one file. Unlike hover it takes no
119
// position and never falls back to a recompile; the caller drives an EDIT
120
// (and/or COMPILE) first. Lets a batch consumer — the ghul.dev example
121
// pipeline — collect a file's hovers in one response. Signatures are
122
// rendered against the same column budget hover uses, so a batch consumer
123
// lays a wide signature out over several lines exactly as an editor does.
124
class HOVERMAP_HANDLER(
125
_watchdog: WATCHDOG,
126
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS
127
): RequestHandler[Protocol.Request.HOVER_MAP] is
128
super()
129
130
handle_request(request: Protocol.Request.HOVER_MAP, writer: IO.TextWriter) is
131
let path = request.path
132
133
let entries = Collections.LIST[Protocol.HOVER_ENTRY]()
134
135
try
136
if !_watchdog.want_restart then
137
for entry in _symbol_use_locations.hover_uses_in_file(path) do
138
let location = entry.location
139
140
entries.add(
141
Protocol.HOVER_ENTRY(
142
location.start_line, location.start_column,
143
location.end_line, location.end_column,
144
SIGNATURE_DOC.build(entry.value),
145
entry.value.kind_label
146
)
147
)
148
od
149
fi
150
catch ex: Exception
151
debug_always("HOVERMAP caught: {ex.get_type()} {ex.message}")
152
153
_watchdog.request_restart()
154
yrt
155
156
Std.error.flush()
157
158
JSON_PROTOCOL.write_response(writer, Protocol.Response.HOVER_MAP(entries))
159
si
160
si
161
162
// Inlay-hint dump for VS Code's inlay-hints provider: reads the
163
// store's editor-only inlays recorded by the flow-narrowing sites
164
// during the last compile-expressions pass over `path`, optionally
165
// limited to a range - the editor's viewport. This is pure retrieval
166
// — no recompile — because the analyser has already run
167
// compile-expressions before serving any query for an open file.
168
class INLAY_HINTS_HANDLER(
169
_watchdog: WATCHDOG
170
): RequestHandler[Protocol.Request.INLAY_HINTS] is
171
super()
172
173
handle_request(request: Protocol.Request.INLAY_HINTS, writer: IO.TextWriter) is
174
let path = request.path
175
176
let hints = Collections.LIST[Protocol.INLAY_HINT]()
177
178
try
179
if !_watchdog.want_restart then
180
// Filter to the range before merging: both mergers
181
// group on exact position, so a range either contains
182
// a whole group or none of it. A merger that grouped
183
// by enclosing scope instead could see a group cut in
184
// half by this filter - pinned by the range tests.
185
let merged =
186
DEFINITION_VIRTUALITY_MERGER().merge(
187
NARROWING_INLAY_MERGER().merge(
188
INLAY_RANGE.filter(IoC.CONTAINER.instance.logger.inlays_for(path), request)
189
)
190
)
191
192
for i in merged do
193
hints.add(
194
Protocol.INLAY_HINT(
195
i.location.start_line,
196
i.location.start_column,
197
i.text,
198
i.detail ?? "",
199
i.code ?? ""
200
)
201
)
202
od
203
204
for t in _terminator_hints(request, path) do
205
hints.add(t)
206
od
207
fi
208
catch ex: Exception
209
debug_always("INLAY_HINTS caught: {ex.get_type()} {ex.message}")
210
211
_watchdog.request_restart()
212
yrt
213
214
Std.error.flush()
215
216
JSON_PROTOCOL.write_response(writer, Protocol.Response.INLAY_HINTS(hints))
217
si
218
219
// The inferred statement boundaries for `path`, one "∘︎" hint per
220
// boundary. These are homogeneous positions rather than
221
// text-carrying records, collected by the parser into their own
222
// sorted list - see DIAGNOSTICS_LIST. LOCATION's packed
223
// (line, column) form orders identically to the line-then-column
224
// range comparison, so the range test is a plain int comparison,
225
// and the list's source order lets the scan stop at the range's
226
// end. Served only when the terminator kind is enabled, which is
227
// also the only state in which the parser records them.
228
_terminator_hints(
229
request: Protocol.Request.INLAY_HINTS,
230
path: string
231
) -> Collections.LIST[Protocol.INLAY_HINT] static is
232
let result = Collections.LIST[Protocol.INLAY_HINT]()
233
234
let logger = IoC.CONTAINER.instance.logger
235
236
if !logger.is_inlay_kind_enabled(Logging.INLAY_KINDS.TERMINATOR) then
237
return result
238
fi
239
240
let range = INLAY_RANGE.bounds(request)
241
242
let lower: int mut = 0
243
let upper: int mut = 0
244
245
if let (start_line, start_column, end_line, end_column) = range then
246
lower = Source.LOCATION.pair(start_line, start_column)
247
upper = Source.LOCATION.pair(end_line, end_column)
248
fi
249
250
let hint = p =>
251
Protocol.INLAY_HINT(
252
Source.LOCATION.line_of(p),
253
Source.LOCATION.column_of(p),
254
"∘︎",
255
"",
256
Logging.INLAY_KINDS.TERMINATOR
257
)
258
259
for p in logger.inferred_terminators_for(path) do
260
if !range? \/ (p >= lower /\ p < upper) then
261
result.add(hint(p))
262
elif p >= upper then
263
break
264
fi
265
od
266
267
return result
268
si
269
si
270
271
// Whole-file semantic-token dump for VS Code's semantic-tokens provider:
272
// one token per recorded HOVER_USE whose kind maps to an LSP token type.
273
// A symbol with no LSP-mappable kind contributes no token. Like HOVERMAP
274
// this triggers a recompile only when the hover map is empty (first open).
275
class SEMANTICTOKENS_HANDLER(
276
_watchdog: WATCHDOG,
277
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
278
_source_file_lookup: SourceFileLookup,
279
_full_compiler: FULL_COMPILER
280
): RequestHandler[Protocol.Request.SEMANTIC_TOKENS] is
281
_classifier: SEMANTIC_TOKEN_CLASSIFIER
282
283
super()
284
285
init(..) is
286
_classifier = SEMANTIC_TOKEN_CLASSIFIER()
287
si
288
289
handle_request(request: Protocol.Request.SEMANTIC_TOKENS, writer: IO.TextWriter) is
290
let path = request.path
291
292
let tokens = Collections.LIST[Protocol.SEMANTIC_TOKEN]()
293
294
try
295
if !_watchdog.want_restart then
296
let uses mut = _symbol_use_locations.hover_uses_in_file(path)
297
298
// Unlike HOVERMAP (built for the offline ghul.dev pipeline
299
// that explicitly drives EDIT + COMPILE), this serves a live
300
// IDE that asks for tokens whenever a document opens. The
301
// hover map is only populated by a whole-project compile; on
302
// first open we get nothing back. Fall back like hover does
303
// and force one — the user otherwise sees TextMate-only
304
// coloring until the first edit triggers a debounced compile.
305
if uses.count == 0 then
306
_full_compiler.compile_all(writer, path)
307
308
uses = _symbol_use_locations.hover_uses_in_file(path)
309
fi
310
311
// Discard wide spans that strictly contain another recorded
312
// use on the same line. Desugared expressions register a
313
// hover use whose location is the original source span —
314
// useful for hover, but for semantic tokens it produces a
315
// giant token covering identifiers and operators. The
316
// strict-containment filter drops the outer use while
317
// keeping every inner identifier.
318
let useful = OUTER_SPAN_FILTER().apply(uses, u => u.location)
319
320
for entry in useful do
321
let symbol = entry.value.symbol
322
let token_type = _classifier.token_type(symbol)
323
324
if token_type? then
325
let location = entry.location
326
let modifiers = _classifier.modifiers(symbol)
327
328
// The leading underscore on a non-public member is
329
// an access marker, not part of the name. Split it
330
// into its own `modifier` token so it colours like
331
// `public`/`private`, while the rest of the name
332
// keeps its kind colour. `prefix` is 0 for public
333
// symbols and for names with no leading underscore.
334
let prefix = _classifier.access_underscore_prefix_length(symbol)
335
336
if prefix > 0 /\ location.start_line == location.end_line then
337
tokens.add(
338
Protocol.SEMANTIC_TOKEN(
339
location.start_line, location.start_column,
340
location.end_line, location.start_column + prefix - 1,
341
"modifier", ""
342
)
343
)
344
345
tokens.add(
346
Protocol.SEMANTIC_TOKEN(
347
location.start_line, location.start_column + prefix,
348
location.end_line, location.end_column,
349
token_type, modifiers
350
)
351
)
352
else
353
tokens.add(
354
Protocol.SEMANTIC_TOKEN(
355
location.start_line, location.start_column,
356
location.end_line, location.end_column,
357
token_type, modifiers
358
)
359
)
360
fi
361
fi
362
od
363
364
// Contextual modifier keywords (`init`, `open`) look like
365
// identifiers to the lexer, so tmLanguage can't safely
366
// colour them without false-positives at genuine
367
// identifier sites. The `collect-modifier-keyword-
368
// locations` post-parse pass captured every site the
369
// parser accepted; overlay a `keyword` token at each.
370
let source_file = _source_file_lookup.find_source_file(path)
371
if source_file? then
372
if let source_file.contextual_modifier_locations? then
373
for location in contextual_modifier_locations do
374
tokens.add(
375
Protocol.SEMANTIC_TOKEN(
376
location.start_line, location.start_column,
377
location.end_line, location.end_column,
378
"keyword", ""
379
)
380
)
381
od
382
fi
383
fi
384
fi
385
catch ex: Exception
386
debug_always("SEMANTICTOKENS caught: {ex.get_type()} {ex.message}")
387
388
_watchdog.request_restart()
389
yrt
390
391
Std.error.flush()
392
393
JSON_PROTOCOL.write_response(writer, Protocol.Response.SEMANTIC_TOKENS(tokens))
394
si
395
396
si
397
398
// Maps a Symbol to the LSP semantic-token type and modifier strings the
399
// VS Code client expects. Returns null token_type for kinds VS Code has no
400
// slot for; the handler then skips that token.
401
class SEMANTIC_TOKEN_CLASSIFIER is
402
init() is si
403
404
token_type(symbol: Semantic.Symbols.Symbol?) -> string? is
405
if !symbol? then
406
return null
407
fi
408
409
let s = symbol.collapse_group_if_single_member()
410
411
// Innate functions are the built-in operators (`+`, `==`,
412
// `<>`, …); they are methods underneath, but reading as
413
// operators matches how they are written and used.
414
if s.is_innate then
415
return "operator"
416
fi
417
418
case s.symbol_kind
419
when Semantic.Symbols.SymbolKind.NAMESPACE then
420
return "namespace"
421
when Semantic.Symbols.SymbolKind.CLASS then
422
return "class"
423
when Semantic.Symbols.SymbolKind.INTERFACE then
424
return "interface"
425
when Semantic.Symbols.SymbolKind.STRUCT then
426
return "struct"
427
when Semantic.Symbols.SymbolKind.ENUM then
428
return "enum"
429
when Semantic.Symbols.SymbolKind.ENUM_MEMBER then
430
return "enumMember"
431
when Semantic.Symbols.SymbolKind.TYPE_PARAMETER then
432
return "typeParameter"
433
when Semantic.Symbols.SymbolKind.METHOD then
434
return "method"
435
when Semantic.Symbols.SymbolKind.FUNCTION then
436
return "function"
437
when Semantic.Symbols.SymbolKind.PROPERTY then
438
return "property"
439
when Semantic.Symbols.SymbolKind.FIELD then
440
return "property"
441
when Semantic.Symbols.SymbolKind.VARIABLE then
442
// Arguments are classified as `variable` rather than
443
// `parameter`: the useful axis in ghūl is immutable vs
444
// `mut` (carried by the `readonly` modifier below), and
445
// `variable.readonly` is styled by mainstream themes where
446
// `parameter.readonly` typically is not — so an argument
447
// shares a local's colouring instead of diverging from it.
448
return "variable"
449
else
450
return null
451
esac
452
si
453
454
// The number of leading underscores that mark a non-public member —
455
// the prefix the handler peels into its own `modifier` token so it
456
// colours as an access modifier. Zero unless the symbol reports itself
457
// non-public (`is_workspace_visible` is the compiler's own access
458
// determination, so a public symbol whose name happens to begin with
459
// `_`, and any type — always workspace-visible — keep the underscore
460
// as part of the name). Zero too when the whole name is underscores,
461
// leaving nothing to colour by kind.
462
access_underscore_prefix_length(symbol: Semantic.Symbols.Symbol) -> int is
463
let s = symbol.collapse_group_if_single_member()
464
465
if s.is_workspace_visible then
466
return 0
467
fi
468
469
let name = s.name
470
471
let count mut = 0
472
473
while count < name.length /\ name[count] == '_' do
474
count = count + 1
475
od
476
477
if count == name.length then
478
return 0
479
fi
480
481
return count
482
si
483
484
// Comma-separated LSP modifiers, possibly empty. Emits `static` for
485
// STATIC_* subclasses and `readonly` for an immutable (non-`mut`)
486
// local or argument.
487
modifiers(symbol: Semantic.Symbols.Symbol?) -> string is
488
if !symbol? then
489
return ""
490
fi
491
492
let s = symbol.collapse_group_if_single_member()
493
494
let parts = Collections.LIST[string]()
495
496
if
497
isa Semantic.Symbols.STATIC_METHOD(s) \/
498
isa Semantic.Symbols.STATIC_PROPERTY(s) \/
499
isa Semantic.Symbols.STATIC_FIELD(s) \/
500
isa Semantic.Symbols.CONSTANT_FIELD(s)
501
then
502
parts.add("static")
503
fi
504
505
// Locals and arguments are immutable unless declared `mut`. An
506
// immutable one cannot be reassigned, so it carries the
507
// `readonly` modifier that mainstream themes already style. The
508
// `is_local` guard restricts this to locals and arguments; fields
509
// are Variable subclasses too, but are members, freely
510
// assignable, and carry no such modifier.
511
if isa Semantic.Symbols.Variable(s) /\ s.is_local /\ !s.is_mutable_marked then
512
parts.add("readonly")
513
fi
514
515
if parts.count == 0 then
516
return ""
517
fi
518
519
return string.join(",", parts)
520
si
521
si
522
523
// Append compiler LOCATIONs as LOCATION_DTOs into the target list — used
524
// by every location-list response handler.
525
append_location_dtos(into: Collections.LIST[Protocol.LOCATION_DTO], locations: Iterable[LOCATION]) is
526
for location in locations do
527
into.add(
528
Protocol.LOCATION_DTO(
529
location.file_name,
530
location.start_line,
531
location.start_column,
532
location.end_line,
533
location.end_column
534
)
535
)
536
od
537
si
538
539
class DEFINITION_HANDLER(
540
_watchdog: WATCHDOG,
541
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
542
_full_compiler: FULL_COMPILER
543
): RequestHandler[Protocol.Request.DEFINITION] is
544
super()
545
546
handle_request(request: Protocol.Request.DEFINITION, writer: IO.TextWriter) is
547
let path = request.path
548
let line = request.line
549
let column = request.column
550
551
let locations = Collections.LIST[Protocol.LOCATION_DTO]()
552
553
try
554
if !_watchdog.want_restart then
555
let symbol mut = _symbol_use_locations.find_definition_from_use(path, line, column)
556
557
if !symbol? then
558
_full_compiler.compile_all(writer, path)
559
560
symbol = _symbol_use_locations.find_definition_from_use(path, line, column)
561
fi
562
563
if symbol? /\ !symbol.is_internal /\ !symbol.is_reflected then
564
append_location_dtos(locations, [symbol.location])
565
fi
566
fi
567
catch e: Exception
568
debug_always("DEFINITION caught: {e.get_type()}: {e.message}")
569
_watchdog.request_restart()
570
yrt
571
572
Std.error.flush()
573
574
JSON_PROTOCOL.write_response(writer, Protocol.Response.DEFINITION(locations))
575
si
576
si
577
578
class DECLARATION_HANDLER(
579
_watchdog: WATCHDOG,
580
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
581
_full_compiler: FULL_COMPILER
582
): RequestHandler[Protocol.Request.DECLARATION] is
583
super()
584
585
handle_request(request: Protocol.Request.DECLARATION, writer: IO.TextWriter) is
586
let locations = Collections.LIST[Protocol.LOCATION_DTO]()
587
588
try
589
let path = request.path
590
let line = request.line
591
let column = request.column
592
593
if !_watchdog.want_restart then
594
let symbol mut = _symbol_use_locations.find_definition_from_use(path, line, column)
595
596
if !symbol? then
597
_full_compiler.compile_all(writer, path)
598
599
symbol = _symbol_use_locations.find_definition_from_use(path, line, column)
600
fi
601
602
if symbol? then
603
append_location_dtos(locations, _symbol_use_locations.find_declarations_of_symbol(symbol))
604
fi
605
fi
606
catch e: Exception
607
debug_always("DECLARATION caught: {e.get_type()}: {e.message}")
608
yrt
609
610
Std.error.flush()
611
612
JSON_PROTOCOL.write_response(writer, Protocol.Response.DECLARATION(locations))
613
si
614
si
615
616
class FULL_COMPILER(
617
_watchdog: WATCHDOG,
618
_compiler: COMPILER,
619
_source_files: Iterable[SOURCE_FILE],
620
_timers: TIMERS
621
) is
622
// Set while a query is being answered part-way through a compile.
623
// The query-miss recompile below would otherwise start a second
624
// compile inside the first, against state the outer one is in the
625
// middle of rewriting. A miss answers from what is there instead.
626
suspend_recompiles: bool public
627
628
is_compiled_through_expressions(source_file: SOURCE_FILE) -> bool =>
629
_compiler.is_compiled_through_expressions(source_file)
630
631
// True when every known file has been walked through the
632
// compile-expressions pass — the precondition for a complete
633
// cross-file use map. A single-file EDIT's rebuild leaves the
634
// non-edited files at up-to-expressions, so their expression-position
635
// uses are absent until a full recompile restores them.
636
all_compiled_through_expressions() -> bool is
637
for source_file in _source_files do
638
if !_compiler.is_compiled_through_expressions(source_file) then
639
return false
640
fi
641
od
642
643
return true
644
si
645
646
compile_all(writer: IO.TextWriter) is
647
compile_all(writer, null)
648
si
649
650
// Recompile (driven by a query miss), then emit the diagnostics as a
651
// standalone diagnostics response with phase "query" — the client
652
// drains this before reading the query's own response.
653
compile_all(writer: IO.TextWriter, only_for_file_name: string?) is
654
if suspend_recompiles then
655
return
656
fi
657
658
// A query can arrive before the first EDIT has registered any
659
// source files. Building then is not a harmless no-op: it
660
// clears the symbol table and runs the pass barriers with no
661
// files queued, so a barrier hook that touches a reflected
662
// type materializes it against a symbol table missing the
663
// innate types (Ghul.REFERENCE and friends),
664
// and the half-built symbol stays cached for the life of the
665
// process. Answer from the empty state instead; the first
666
// real EDIT performs the initial build.
667
if !(_source_files |> any(f => true)) then
668
JSON_PROTOCOL.write_response(
669
writer,
670
Protocol.Response.DIAGNOSTICS(
671
Collections.LIST[Protocol.DIAGNOSTIC](),
672
Collections.LIST[string](),
673
"query",
674
0.0D,
675
false
676
)
677
)
678
679
return
680
fi
681
682
// The retained declare/resolve state - symbols, ancestries,
683
// override links, store-free bits - reflects every file's
684
// current source (every EDIT rebuild leaves it that way; only
685
// a rebuild in flight invalidates it), and the type comparison
686
// cache keyed on those symbols stays valid. The only thing a
687
// query can be missing is a file's expression-level walk: a
688
// rebuild compiles expressions for the edited files only,
689
// leaving every other file at up-to-expressions with no
690
// use-map entries. Compile just the files that lack it instead
691
// of clearing the world; when none lack it the miss is
692
// authoritative and a recompile could not change the answer.
693
if _compiler.are_tables_current then
694
compile_missing_expressions(writer, only_for_file_name)
695
696
return
697
fi
698
699
try
700
_timers.start("compile-all")
701
702
Semantic.Types.NAMED.clear_cache()
703
704
if !_watchdog.want_restart then
705
for i in _source_files do
706
i.want_compile_up_to_expressions = true
707
708
if !only_for_file_name? \/ i.file_name =~ only_for_file_name then
709
IoC.CONTAINER.instance.logger.clear(i.file_name, true)
710
i.want_compile_expressions = true
711
else
712
IoC.CONTAINER.instance.logger.clear_global_declaration_diagnostics(i.file_name)
713
i.want_compile_expressions = false
714
fi
715
od
716
717
let clear_state = Syntax.Process.CLEAR_STATE_VISITOR()
718
for i in _source_files do
719
clear_state.apply(i.definition)
720
od
721
722
IoC.CONTAINER.instance.state_store_registry.clear_all()
723
724
IoC.CONTAINER.instance.logger.start_analysis()
725
726
_compiler.clear_symbols()
727
728
_compiler.queue(_source_files)
729
730
_compiler.build()
731
732
let diagnostics = Collections.LIST[Protocol.DIAGNOSTIC]()
733
let checked_paths = Collections.LIST[string]()
734
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
735
JSON_PROTOCOL.write_response(
736
writer,
737
Protocol.Response.DIAGNOSTICS(diagnostics, checked_paths, "query", 0.0D, false)
738
)
739
740
_watchdog.note_full_compile()
741
742
if !only_for_file_name? then
743
_compiler.is_full_compile_needed = false
744
fi
745
fi
746
747
catch e: Exception
748
debug_always("FULL_COMPILE caught: {e.get_type()}: {e.message}")
749
750
_watchdog.request_restart()
751
finally
752
IoC.CONTAINER.instance.logger.end_analysis()
753
754
_compiler.clear_queue()
755
756
_timers.finish("compile-all")
757
yrt
758
si
759
760
// The query-miss recompile when no interface change is pending:
761
// run compile-expressions for the files - the queried file, or
762
// all files for a cross-file query - that have not been walked
763
// through expressions in the current symbol generation. The
764
// retained symbol table is not cleared and no earlier pass is
765
// re-run. When every relevant file is already compiled through
766
// expressions there is nothing to do and no response is written -
767
// the caller answers from the maps as it would on a hit.
768
compile_missing_expressions(writer: IO.TextWriter, only_for_file_name: string?) is
769
let missing = Collections.LIST[SOURCE_FILE]()
770
771
for i in _source_files do
772
if
773
(!only_for_file_name? \/ i.file_name =~ only_for_file_name) /\
774
!_compiler.is_compiled_through_expressions(i)
775
then
776
missing.add(i)
777
fi
778
od
779
780
if missing.count == 0 then
781
// Timed as a pair so the skip shows up, with a count, in
782
// the analysis stats dump.
783
_timers.start("query-miss-authoritative")
784
_timers.finish("query-miss-authoritative")
785
786
return
787
fi
788
789
try
790
_timers.start("compile-on-demand")
791
792
IoC.CONTAINER.instance.logger.start_analysis()
793
794
if !_watchdog.want_restart then
795
// No CLEAR_STATE here: a not-compiled-through file
796
// arrives straight from a full rebuild, which cleared
797
// its per-build state and then re-resolved its type
798
// expressions - exactly the state compile-expressions
799
// expects. Clearing again would wipe the resolve
800
// passes' output (body TypeExpression.type) that the
801
// walk consumes, with no pass left to restore it.
802
for i in missing do
803
// The re-walk re-reports this file's expression
804
// diagnostics; drop the previous copies so they
805
// do not double up. Parse and declaration-level
806
// diagnostics are kept - no earlier pass re-runs.
807
IoC.CONTAINER.instance.logger.clear_expression_diagnostics(i.file_name)
808
Syntax.Process.PURE_SLOTS.clear_for(i.file_name)
809
Syntax.Process.KILL_LEDGER.clear_for(i.file_name)
810
811
_compiler.compile_expressions_only(i)
812
od
813
814
let diagnostics = Collections.LIST[Protocol.DIAGNOSTIC]()
815
let checked_paths = Collections.LIST[string]()
816
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
817
JSON_PROTOCOL.write_response(
818
writer,
819
Protocol.Response.DIAGNOSTICS(diagnostics, checked_paths, "query", 0.0D, false)
820
)
821
fi
822
catch e: Exception
823
debug_always("COMPILE_ON_DEMAND caught: {e.get_type()}: {e.message}")
824
825
_watchdog.request_restart()
826
finally
827
IoC.CONTAINER.instance.logger.end_analysis()
828
829
_timers.finish("compile-on-demand")
830
yrt
831
si
832
si
833
834
// Does a full compile of the files that have been edited, plus a partial
835
// compile (up to but not including expressions) of all other files.
836
class FILE_EDITED_HANDLER(
837
_watchdog: WATCHDOG,
838
_timers: TIMERS,
839
_compiler: COMPILER,
840
_build_flags: GLOBAL_BUILD_FLAGS,
841
library_files: Iterable[string],
842
_symbol_table: Semantic.SYMBOL_TABLE
843
): RequestHandler[Protocol.Request.EDIT], SourceFileLookup, Iterable[SOURCE_FILE] is
844
_library_files: Iterable[string]?
845
846
_source_files_by_path: Collections.MutableMap[string,SOURCE_FILE]
847
848
_is_interface_changed: bool
849
850
// The guard that last gave up on this EDIT, reported if it ends up
851
// taking the whole-project rebuild. Null when no incremental path
852
// was attempted at all.
853
_declined_reason: string?
854
855
// The pre-edit SOURCE_FILE of the most recent parse, stashed by
856
// parse_and_add_file. For an interface-preserving single-file EDIT the
857
// incremental body re-walk splices the new bodies onto it (its symbols
858
// are still valid). Null on a cold / first-parse EDIT.
859
_retained: SOURCE_FILE?
860
861
file_names: Iterable[string] => _source_files_by_path.keys
862
863
iterator: Collections.Iterator[SOURCE_FILE]
864
=> _source_files_by_path.values.iterator
865
866
super()
867
868
init(..) is
869
_source_files_by_path = Collections.MAP[string,SOURCE_FILE]()
870
si
871
872
find_source_file(path: string) -> SOURCE_FILE? =>
873
if _source_files_by_path.contains_key(path) then
874
_source_files_by_path[path]
875
else
876
null
877
fi
878
879
// A source file named on the command line, read from disk. It is
880
// parsed at its own path, so its declarations have locations a
881
// client can go to; one that cannot be read is reported there.
882
_add_file_from_disk(path: string) is
883
let text: string? mut = null
884
885
try
886
text = IO.File.read_all_text(path)
887
catch ex: Exception
888
IoC.CONTAINER.instance.logger.error(Source.LOCATION(path, 1, 1, 1, 1), "cannot read {path}: {ex.message}")
889
yrt
890
891
if let source = text then
892
parse_and_add_file(path, IO.StringReader(source), false)
893
fi
894
si
895
896
parse_and_add_file(path: string, reader: IO.TextReader, is_internal_file: bool) is
897
IoC.CONTAINER.instance.logger.clear(path, false)
898
899
// The re-parse replaces this file's tree; node-keyed state
900
// recorded against the old tree's nodes is reclaimed here.
901
// Any pass that later walks surviving spliced-in nodes
902
// rewrites its state before reading it, so dropping the
903
// whole file is safe on the incremental paths too.
904
IoC.CONTAINER.instance.state_store_registry.drop_file(path)
905
906
let previous = find_source_file(path)
907
908
_retained = previous
909
910
let source_file = _compiler.parse(path, reader, _build_flags.want_compile_up_to_expressions, _build_flags.want_compile_expressions, is_internal_file)
911
912
source_file.library = _build_flags.library_sources?.library_for(IO.Path.get_full_path(path))
913
914
_source_files_by_path[path] = source_file
915
916
_compiler.post_parse([source_file])
917
918
source_file.want_compile_expressions = true
919
920
let signature = Syntax.INTERFACE_SIGNATURE()
921
source_file.definition.walk(signature)
922
source_file.interface_signature = signature.signature
923
924
if
925
!previous? \/
926
!previous.interface_signature? \/
927
previous.interface_signature !~ source_file.interface_signature
928
then
929
_is_interface_changed = true
930
fi
931
si
932
933
// Latch why an incremental path gave up, and decline. Every
934
// bail-out in the incremental paths goes through here. The first
935
// reason latched wins, and it is reported only if the edit ends up
936
// rebuilding; WORK_COUNTERS carries why.
937
_decline(reason: string) -> bool is
938
if !_declined_reason? then
939
_declined_reason = reason
940
fi
941
942
return false
943
si
944
945
_definition_signature(definition: Syntax.Trees.Definitions.Definition) -> string is
946
let signature = Syntax.INTERFACE_SIGNATURE()
947
948
definition.walk(signature)
949
950
return signature.signature
951
si
952
953
// An interface-affecting edit handled incrementally. Two shapes
954
// qualify, classified by comparing per-top-level-definition
955
// interface signatures (the whole-file signature folds post-order
956
// - a parent's kind trails its children - so neither shape is a
957
// string-prefix relation on it):
958
//
959
// - append-only: every retained definition matches the donor's
960
// at the same index and the donor has extras. The appended
961
// declarations are adopted onto the retained AST and built
962
// against the retained tables; nothing referenced them before
963
// the edit, so nothing else changes.
964
// - replace-one: same count, exactly one definition differs, and
965
// the outgoing subtree declares only functions that nothing
966
// outside this file references or overrides. The old functions
967
// are removed from their scopes, the side tables purge the old
968
// subtree's span, and the new subtree is built in its place.
969
// This is the interface-typing stream: each keystroke pause
970
// re-replaces the one declaration in flux.
971
//
972
// Everything else falls back to the whole-project rebuild. A new
973
// or renamed name can in principle capture a previously
974
// differently-resolved use in another file; that stale resolution
975
// lasts until the next full rebuild - the same class of transient
976
// imprecision the store-free bits accept.
977
try_incremental_interface_edit(
978
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
979
checked_paths: Collections.LIST[string]
980
) -> bool is
981
let retained = _retained
982
983
if !retained? then
984
return _decline(WORK_COUNTERS.NO_RETAINED_PARSE)
985
fi
986
987
let edited_path = retained.file_name
988
let donor = _source_files_by_path[edited_path]
989
990
let donor_signature = donor.interface_signature
991
992
if !retained.interface_signature? \/ !donor_signature? then
993
return _decline(WORK_COUNTERS.MISSING_INTERFACE_SIGNATURE)
994
fi
995
996
let donor_list = cast Syntax.Trees.Definitions.LIST?(donor.definition)
997
let retained_list = cast Syntax.Trees.Definitions.LIST?(retained.definition)
998
999
if !donor_list? \/ !retained_list? then
1000
return _decline(WORK_COUNTERS.FILE_ROOT_NOT_A_LIST)
1001
fi
1002
1003
// See try_incremental_build: top-level statements put
1004
// namespace-declared symbols inside a body, which none of
1005
// the incremental routes re-derive.
1006
if Syntax.Process.SYNTHESISE_TOP_LEVEL_ENTRY.has_top_level_entry(donor_list) \/ Syntax.Process.SYNTHESISE_TOP_LEVEL_ENTRY.has_top_level_entry(retained_list) then
1007
return _decline(WORK_COUNTERS.TOP_LEVEL_STATEMENTS)
1008
fi
1009
1010
let donor_definitions = donor_list.definitions
1011
let retained_definitions = retained_list.definitions
1012
let retained_count = retained_definitions.count
1013
1014
if donor_definitions.count == retained_count then
1015
let changed_index mut = -1
1016
1017
for i in 0..retained_count do
1018
if _definition_signature(retained_definitions[i]) !~ _definition_signature(donor_definitions[i]) then
1019
if changed_index >= 0 then
1020
return _decline(WORK_COUNTERS.MULTIPLE_CHANGED_DEFINITIONS)
1021
fi
1022
1023
changed_index = i
1024
fi
1025
od
1026
1027
if changed_index < 0 then
1028
// The whole-file signatures differ but no top-level
1029
// definition's does - something file-level changed;
1030
// let the full rebuild sort it out.
1031
return _decline(WORK_COUNTERS.FILE_LEVEL_CHANGE)
1032
fi
1033
1034
let chain = Collections.LIST[Syntax.Trees.Definitions.Definition]()
1035
1036
return _try_narrow_edit(retained, donor_signature, chain, retained_definitions[changed_index], donor_definitions[changed_index], diagnostics, checked_paths)
1037
fi
1038
1039
if donor_definitions.count < retained_count then
1040
return _decline(WORK_COUNTERS.FEWER_DEFINITIONS)
1041
fi
1042
1043
for i in 0..retained_count do
1044
if _definition_signature(retained_definitions[i]) !~ _definition_signature(donor_definitions[i]) then
1045
return _decline(WORK_COUNTERS.RETAINED_PREFIX_MISMATCH)
1046
fi
1047
od
1048
1049
// Split the donor: the leading definitions pair with the
1050
// retained skeleton; the tail is the appended declarations.
1051
let appended = Collections.LIST[Syntax.Trees.Definitions.Definition]()
1052
1053
while donor_definitions.count > retained_count do
1054
appended.insert(0, donor_definitions[donor_definitions.count - 1])
1055
donor_definitions.remove_at(donor_definitions.count - 1)
1056
od
1057
1058
if !try_incremental_build(diagnostics, checked_paths, false) then
1059
// try_incremental_build re-registered the donor as the
1060
// live file; give it its appended tail back so the full
1061
// rebuild sees the whole edit. It named its own decline
1062
// reason, so none is named here.
1063
for d in appended do
1064
donor_definitions.add(d)
1065
od
1066
1067
return false
1068
fi
1069
1070
for d in appended do
1071
retained_list.add(d)
1072
od
1073
1074
_compiler.build_appended(
1075
retained,
1076
Syntax.Trees.Definitions.LIST(retained_list.location, appended)
1077
)
1078
1079
retained.interface_signature = donor_signature
1080
1081
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
1082
1083
return true
1084
si
1085
1086
// Narrow a changed definition pair down to the smallest changed
1087
// window. Containers of the same kind and name - namespaces, and
1088
// classes whose headers match - recurse into their member lists;
1089
// at each level the members split into a signature-matched common
1090
// prefix, a signature-matched common suffix, and the changed
1091
// window between them. A window consisting solely of function
1092
// definitions is replaced in place; anything else falls back to
1093
// the whole-project rebuild.
1094
_try_narrow_edit(
1095
retained: SOURCE_FILE,
1096
donor_signature: string,
1097
chain: Collections.LIST[Syntax.Trees.Definitions.Definition],
1098
retained_definition: Syntax.Trees.Definitions.Definition,
1099
donor_definition: Syntax.Trees.Definitions.Definition,
1100
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
1101
checked_paths: Collections.LIST[string]
1102
) -> bool is
1103
if let retained_namespace: Syntax.Trees.Definitions.NAMESPACE = retained_definition then
1104
let donor_namespace = cast Syntax.Trees.Definitions.NAMESPACE?(donor_definition)
1105
1106
if !donor_namespace? \/ retained_namespace.name.name !~ donor_namespace.name.name then
1107
return _decline(WORK_COUNTERS.NAMESPACE_NAME_MISMATCH)
1108
fi
1109
1110
chain.add(retained_namespace)
1111
1112
return _try_narrow_children(retained, donor_signature, chain, retained_namespace.body, donor_namespace.body, null, diagnostics, checked_paths)
1113
fi
1114
1115
if let retained_class: Syntax.Trees.Definitions.Classy = retained_definition /\ _is_member_container(retained_class) then
1116
let donor_class = cast Syntax.Trees.Definitions.Classy?(donor_definition)
1117
1118
if
1119
!donor_class? \/
1120
retained_definition.get_type() != donor_definition.get_type() \/
1121
_class_header_signature(retained_class) !~ _class_header_signature(donor_class)
1122
then
1123
return _decline(WORK_COUNTERS.CLASS_HEADER_MISMATCH)
1124
fi
1125
1126
let scope = _symbol_table.scope_for(retained_class)
1127
let classy = if scope? then cast Semantic.Symbols.Classy?(scope.underlying_scope) else null fi
1128
1129
if !classy? then
1130
return _decline(WORK_COUNTERS.NO_CLASS_SYMBOL)
1131
fi
1132
1133
// A class with ghul-declared subclasses or trait
1134
// implementors is handled by resetting and re-pulling the
1135
// whole implementor closure at the reconcile. That covers
1136
// pull-down bookkeeping, not member cloning: a closure
1137
// member extending a constructed generic re-creates its
1138
// inherited member clones on re-pull, leaving other files'
1139
// bindings on the dead clones, so those fall back to the
1140
// whole-project rebuild.
1141
if !_implementor_closure(classy)? then
1142
return _decline(WORK_COUNTERS.UNRESETTABLE_CLOSURE)
1143
fi
1144
1145
// The header signature leaves the doc comment out, so a
1146
// doc-only edit narrows to an empty window; the retained
1147
// class keeps its symbol and takes the new doc here.
1148
retained_class.doc = donor_class.doc
1149
classy.set_doc(donor_class.doc)
1150
1151
chain.add(retained_class)
1152
1153
return _try_narrow_children(retained, donor_signature, chain, retained_class.body, donor_class.body, classy, diagnostics, checked_paths)
1154
fi
1155
1156
// Not a container the search can descend into. This is the
1157
// recursion's base case rather than a guard rejecting the
1158
// edit - the caller falls through to replacing the definition
1159
// in its enclosing member list, and names a reason if that
1160
// does not apply either. Latching one here would mask it.
1161
return false
1162
si
1163
1164
_try_narrow_children(
1165
retained: SOURCE_FILE,
1166
donor_signature: string,
1167
chain: Collections.LIST[Syntax.Trees.Definitions.Definition],
1168
retained_body: Syntax.Trees.Definitions.LIST,
1169
donor_body: Syntax.Trees.Definitions.LIST,
1170
slice_class: Semantic.Symbols.Classy?,
1171
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
1172
checked_paths: Collections.LIST[string]
1173
) -> bool is
1174
let retained_members = retained_body.definitions
1175
let donor_members = donor_body.definitions
1176
1177
let retained_count = retained_members.count
1178
let donor_count = donor_members.count
1179
1180
let limit = if retained_count < donor_count then retained_count else donor_count fi
1181
1182
let prefix mut = 0
1183
1184
while
1185
prefix < limit /\
1186
_definition_signature(retained_members[prefix]) =~ _definition_signature(donor_members[prefix])
1187
do
1188
prefix = prefix + 1
1189
od
1190
1191
let suffix mut = 0
1192
1193
while
1194
suffix < limit - prefix /\
1195
_definition_signature(retained_members[retained_count - 1 - suffix]) =~ _definition_signature(donor_members[donor_count - 1 - suffix])
1196
do
1197
suffix = suffix + 1
1198
od
1199
1200
let retained_window = retained_count - prefix - suffix
1201
let donor_window = donor_count - prefix - suffix
1202
1203
if retained_window == 1 /\ donor_window == 1 then
1204
// A single changed member may itself be a container with a
1205
// still narrower change inside it. Recursion mutates the
1206
// chain, so work on a copy in case it declines and the
1207
// window falls through to function replacement.
1208
let inner_chain = Collections.LIST[Syntax.Trees.Definitions.Definition](chain)
1209
1210
if _try_narrow_edit(retained, donor_signature, inner_chain, retained_members[prefix], donor_members[prefix], diagnostics, checked_paths) then
1211
return true
1212
fi
1213
fi
1214
1215
return _try_replace_functions(retained, donor_signature, chain, retained_body, donor_body, prefix, retained_window, donor_window, slice_class, diagnostics, checked_paths)
1216
si
1217
1218
// Replace the changed window of a member list, when every member
1219
// in it (both outgoing and incoming) is a function definition. All
1220
// guards run before anything is mutated.
1221
_try_replace_functions(
1222
retained: SOURCE_FILE,
1223
donor_signature: string,
1224
chain: Collections.LIST[Syntax.Trees.Definitions.Definition],
1225
retained_body: Syntax.Trees.Definitions.LIST,
1226
donor_body: Syntax.Trees.Definitions.LIST,
1227
prefix: int,
1228
retained_window: int,
1229
donor_window: int,
1230
slice_class: Semantic.Symbols.Classy?,
1231
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
1232
checked_paths: Collections.LIST[string]
1233
) -> bool is
1234
let retained_members = retained_body.definitions
1235
let donor_members = donor_body.definitions
1236
1237
// Texts for the body comparison in the identity adoption after
1238
// the reconcile: the outgoing members' spans index into the
1239
// text the retained tree was parsed from, the incoming
1240
// members' into the donor's. Captured up front because a
1241
// successful try_incremental_build below moves the donor's
1242
// text onto the retained file.
1243
let outgoing_text = retained.source_text
1244
let incoming_text = find_source_file(retained.file_name)?.source_text
1245
1246
let removed_functions = Collections.LIST[Semantic.Symbols.Function]()
1247
1248
// Set when a body elsewhere in the edited file binds an
1249
// outgoing function: those bodies must re-compile against the
1250
// reconciled symbol. Referencing bodies in other files are
1251
// collected here and re-compiled the same way, but only for a
1252
// class member edit - see the reference scan below.
1253
let needs_dependent_recompile mut = false
1254
let dependent_file_names = Collections.SET[string]()
1255
1256
for i in 0..retained_window do
1257
let member = retained_members[prefix + i]
1258
1259
let function = _function_symbol_for(member)
1260
1261
if !function? then
1262
return _decline(WORK_COUNTERS.OUTGOING_NOT_A_FUNCTION)
1263
fi
1264
1265
// A body bound to the outgoing function must re-bind to the
1266
// reconciled symbol or it stays silently bound to the dead
1267
// one. Its file's expressions are recompiled after the
1268
// reconcile: the edited file itself when the reference is a
1269
// same-file body, or the referencing file when it is a
1270
// cross-file body of a class member (see the reference scan).
1271
// Only direct references count - a call bound to an overridden
1272
// base member stays valid when an override is removed.
1273
// Override links in either direction are undone by the slice
1274
// reset, but only when there is a class to reset - at
1275
// namespace level they reject the edit.
1276
if !slice_class? then
1277
if !function.has_no_overriders then
1278
return _decline(WORK_COUNTERS.OUTGOING_HAS_OVERRIDERS)
1279
fi
1280
1281
if let overridees = function.overridees then
1282
if overridees |> any(o => true) then
1283
return _decline(WORK_COUNTERS.OUTGOING_HAS_OVERRIDEES)
1284
fi
1285
fi
1286
else
1287
// A ghul-declared overrider in a subclass couples its
1288
// own declaration to this signature: the closure
1289
// re-pull rebuilds the override links, but the
1290
// subclass's declaration-level diagnostics would not
1291
// be re-derived against the new signature.
1292
if !function.has_no_overriders then
1293
return _decline(WORK_COUNTERS.OUTGOING_HAS_OVERRIDERS)
1294
fi
1295
fi
1296
1297
for reference in IoC.CONTAINER.instance.symbol_use_locations.direct_references_to(function) do
1298
if reference.file_name =~ retained.file_name then
1299
if reference.start < member.location.start \/ reference.start > member.location.end then
1300
// A same-file body outside the edited window
1301
// binds the outgoing function.
1302
needs_dependent_recompile = true
1303
fi
1304
else
1305
// A body in another file binds the outgoing function.
1306
// Recompiling that file rebinds it only when the
1307
// owner scope resolves to one symbol across files: a
1308
// class-like scope does, so a class member's
1309
// other-file callers re-bind, but a namespace-level
1310
// function's groups are per-file and the reconcile
1311
// updates only this file's, leaving other files
1312
// resolving the outgoing symbol. Fall back to the
1313
// whole-project rebuild for a namespace-level edit,
1314
// and for a reference in a file that is not a known
1315
// source file.
1316
if !slice_class? then
1317
return _decline(WORK_COUNTERS.CROSS_FILE_NAMESPACE_REFERENCE)
1318
fi
1319
1320
if !find_source_file(reference.file_name)? then
1321
return _decline(WORK_COUNTERS.CROSS_FILE_UNKNOWN_FILE)
1322
fi
1323
1324
dependent_file_names.add(reference.file_name)
1325
fi
1326
od
1327
1328
removed_functions.add(function)
1329
od
1330
1331
for i in 0..donor_window do
1332
if !isa Syntax.Trees.Definitions.FUNCTION(donor_members[prefix + i]) then
1333
return _decline(WORK_COUNTERS.INCOMING_NOT_A_FUNCTION)
1334
fi
1335
od
1336
1337
// The implementor closure that must reset and re-pull around
1338
// the member swap. Collected before anything mutates - the
1339
// journal undo removes the implementor registrations the
1340
// collection walks - and rejected (null) while rejection can
1341
// still fall back cleanly.
1342
let hierarchy_slice =
1343
if slice_class? then
1344
_implementor_closure(slice_class)
1345
else
1346
null
1347
fi
1348
1349
if slice_class? /\ !hierarchy_slice? then
1350
return _decline(WORK_COUNTERS.UNRESETTABLE_CLOSURE)
1351
fi
1352
1353
// Trim both windows so the remaining trees pair structurally
1354
// for the ordinary body re-walk; the outgoing members' spans
1355
// purge their side-table entries through the same
1356
// reconciliation that drops re-walked body spans.
1357
let removed_definitions = Collections.LIST[Syntax.Trees.Definitions.Definition]()
1358
let added_definitions = Collections.LIST[Syntax.Trees.Definitions.Definition]()
1359
1360
let extra_purge_spans = Collections.LIST[Source.LOCATION]()
1361
1362
for i in 0..retained_window do
1363
let member = retained_members[prefix]
1364
1365
removed_definitions.add(member)
1366
extra_purge_spans.add(member.location)
1367
1368
retained_members.remove_at(prefix)
1369
od
1370
1371
for i in 0..donor_window do
1372
added_definitions.add(donor_members[prefix])
1373
donor_members.remove_at(prefix)
1374
od
1375
1376
// A decline here was named by try_incremental_build itself.
1377
if !try_incremental_build(diagnostics, checked_paths, false, extra_purge_spans) then
1378
for i in 0..retained_window do
1379
retained_members.insert(prefix + i, removed_definitions[i])
1380
od
1381
1382
for i in 0..donor_window do
1383
donor_members.insert(prefix + i, added_definitions[i])
1384
od
1385
1386
return false
1387
fi
1388
1389
// Committed. Undo the pull-downs before touching the declared
1390
// members so each journal's record still matches the state it
1391
// was made against. The whole implementor closure resets: a
1392
// subclass's own journal holds the override links, implementor
1393
// registrations and pulled-down copies that referenced the
1394
// outgoing members.
1395
if hierarchy_slice? then
1396
for hierarchy_member in hierarchy_slice do
1397
hierarchy_member.reset_pulled_down_symbols()
1398
od
1399
fi
1400
1401
1402
for f in removed_functions do
1403
_remove_function_from_owner(f)
1404
od
1405
1406
1407
for i in 0..donor_window do
1408
retained_members.insert(prefix + i, added_definitions[i])
1409
od
1410
1411
let added_list = Syntax.Trees.Definitions.LIST(retained_body.location, added_definitions)
1412
1413
// The window replacement mutated this file's member tree
1414
// beyond the body splice (which already dropped the bucket);
1415
// stated here too so the mutation owns its staleness rule
1416
// rather than leaning on the splice having run first.
1417
1418
_compiler.build_members_interface(retained, chain, added_list)
1419
1420
1421
// Before anything recompiles against the new symbols: each
1422
// incoming function adopts its outgoing counterpart's
1423
// identity, carrying the id-keyed store-free bit across the
1424
// re-creation, so the next debounced compile's refresh
1425
// compares against the bits callers were compiled with
1426
// rather than a default and does not escalate to a full
1427
// rebuild when nothing store-free-relevant changed.
1428
_adopt_replaced_identities(
1429
removed_functions,
1430
removed_definitions,
1431
added_definitions,
1432
outgoing_text,
1433
incoming_text
1434
)
1435
1436
// Re-resolve the closure's inheritance against the changed
1437
// member set: overrides, pulled-down members and implementor
1438
// registration all rebuild through the ordinary pull-down.
1439
// Order is free - pull_down_super_symbols recursively pulls a
1440
// class's ancestors first, and a class already pulled is a
1441
// no-op.
1442
if hierarchy_slice? then
1443
for hierarchy_member in hierarchy_slice do
1444
hierarchy_member.pull_down_super_symbols()
1445
od
1446
fi
1447
1448
// A body that failed to bind a name recorded no reference, so
1449
// the reference scan above cannot see it as a dependent - yet
1450
// this interface change may be exactly what cures, or should
1451
// re-report, its error. The edited file re-checks its own
1452
// errors on this edit's recompile; any other file holding an
1453
// error is marked not-compiled-through, with its expression
1454
// state cleared now (bodies elsewhere in it can bind the
1455
// outgoing symbols), so the next debounced compile's
1456
// expressions-only pass re-checks it - deferring that cost to
1457
// the debounce tick keeps the per-keystroke cost flat.
1458
for error_path in IoC.CONTAINER.instance.logger.paths_with_errors do
1459
if error_path =~ retained.file_name then
1460
needs_dependent_recompile = true
1461
elif let error_file = find_source_file(error_path) then
1462
_compiler.invalidate_file_expressions(error_file)
1463
1464
_compiler.is_full_compile_needed = true
1465
fi
1466
od
1467
1468
if needs_dependent_recompile then
1469
// A same-file body bound the outgoing function; recompile
1470
// the whole edited file's expressions so every body rebinds
1471
// to the reconciled symbol. Covers the changed members too,
1472
// so the positioned expression build is skipped.
1473
_recompile_file_expressions(retained)
1474
else
1475
_compiler.build_members_expressions(retained, chain, added_list)
1476
fi
1477
1478
// Bodies in other files that bound an outgoing function rebind
1479
// to the reconciled symbol the same way - a compile-expressions-
1480
// only recompile against the unchanged interface of each.
1481
for dependent_file_name in dependent_file_names do
1482
if let dependent = find_source_file(dependent_file_name) then
1483
_recompile_file_expressions(dependent)
1484
fi
1485
od
1486
1487
retained.interface_signature = donor_signature
1488
1489
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
1490
1491
return true
1492
si
1493
1494
_recompile_file_expressions(source_file: SOURCE_FILE) is
1495
_compiler.recompile_file_expressions(source_file)
1496
si
1497
1498
// The header of a class-like definition - name, type parameters,
1499
// ancestors and modifiers - folded the way the interface signature
1500
// folds them, so two versions of a class can be compared ignoring
1501
// their member lists.
1502
_class_header_signature(`class: Syntax.Trees.Definitions.Classy) -> string is
1503
let signature = Syntax.INTERFACE_SIGNATURE()
1504
1505
`class.name.walk(signature)
1506
1507
if let arguments = `class.arguments then
1508
arguments.walk(signature)
1509
fi
1510
1511
if let ancestors = `class.ancestors then
1512
ancestors.walk(signature)
1513
fi
1514
1515
`class.modifiers.walk(signature)
1516
1517
return signature.signature
1518
si
1519
1520
// Whether the narrowing search can descend into this definition's
1521
// body looking for a changed member.
1522
//
1523
// Classes, structs and traits carry methods and properties, which
1524
// is what the window replacement below knows how to swap. Unions
1525
// and enums share the same `Classy` base and the same body shape,
1526
// but their bodies hold variants and enum members - the
1527
// replacement would reject every window, so descending only buys a
1528
// longer walk to the same answer.
1529
_is_member_container(definition: Syntax.Trees.Definitions.Definition) -> bool =>
1530
isa Syntax.Trees.Definitions.CLASS(definition) \/
1531
isa Syntax.Trees.Definitions.STRUCT(definition) \/
1532
isa Syntax.Trees.Definitions.TRAIT(definition)
1533
1534
_function_symbol_for(definition: Syntax.Trees.Definitions.Definition) -> Semantic.Symbols.Function? is
1535
if !isa Syntax.Trees.Definitions.FUNCTION(definition) then
1536
return null
1537
fi
1538
1539
let scope = _symbol_table.scope_for(definition)
1540
1541
if !scope? then
1542
return null
1543
fi
1544
1545
return cast Semantic.Symbols.Function?(scope.underlying_scope)
1546
si
1547
1548
// The class together with its transitive ghul-declared
1549
// subclasses and trait implementors, in discovery order. Null
1550
// when an edit to the class's members can't be handled by
1551
// resetting and re-pulling the closure: a closure member that
1552
// extends or implements a constructed generic re-creates its
1553
// inherited member clones on re-pull, and bindings elsewhere
1554
// would keep the dead clones. Reflected implementors are
1555
// skipped - they are import-lifetime and never reset.
1556
_implementor_closure(classy: Semantic.Symbols.Classy) -> Collections.LIST[Semantic.Symbols.Classy]? is
1557
let result = Collections.LIST[Semantic.Symbols.Classy]()
1558
let seen = Collections.SET[Semantic.Symbols.Classy]()
1559
1560
result.add(classy)
1561
seen.add(classy)
1562
1563
let index mut = 0
1564
1565
while index < result.count do
1566
let current = result[index]
1567
1568
index = index + 1
1569
1570
if let implementors = current.implementors then
1571
for implementor in implementors do
1572
if implementor.is_reflected then
1573
continue
1574
fi
1575
1576
if !isa Semantic.Symbols.Classy(implementor) then
1577
return null
1578
fi
1579
1580
if seen.contains(implementor) then
1581
continue
1582
fi
1583
1584
for a in 0..implementor.ancestors.count do
1585
if isa Semantic.Symbols.GENERIC(implementor.get_ancestor(a).symbol) then
1586
return null
1587
fi
1588
od
1589
1590
seen.add(implementor)
1591
result.add(implementor)
1592
od
1593
fi
1594
od
1595
1596
return result
1597
si
1598
1599
// After a reconcile replaced the changed window's members: each
1600
// incoming function that pairs with exactly one outgoing function
1601
// by name and arity adopts its identity, so the store-free bit —
1602
// keyed by symbol id — survives the re-creation. The carried bit
1603
// stays valid only while the body it was derived from is
1604
// unchanged; an incoming body that differs textually resets it
1605
// to the conservative default instead, and the next compile's
1606
// refresh re-derives it. (A changed parameter type can rebind a
1607
// call inside an unchanged body and move the derived bit; the
1608
// refresh catches the difference and escalates, the same true-up
1609
// that covers body-only edits.) Ambiguous pairings — several
1610
// same-name same-arity candidates on either side — are skipped:
1611
// the incoming function keeps its fresh identity and default bit.
1612
_adopt_replaced_identities(
1613
removed_functions: Collections.List[Semantic.Symbols.Function],
1614
removed_definitions: Collections.List[Syntax.Trees.Definitions.Definition],
1615
added_definitions: Collections.List[Syntax.Trees.Definitions.Definition],
1616
outgoing_text: string?,
1617
incoming_text: string?
1618
) is
1619
let adopted = Collections.SET[int]()
1620
1621
for i in 0..added_definitions.count do
1622
let added = added_definitions[i]
1623
1624
let incoming = _function_symbol_for(added)
1625
1626
if !incoming? then
1627
continue
1628
fi
1629
1630
let matched_index mut = -1
1631
let is_ambiguous mut = false
1632
1633
for j in 0..removed_functions.count do
1634
if adopted.contains(j) then
1635
continue
1636
fi
1637
1638
let outgoing = removed_functions[j]
1639
1640
if
1641
outgoing.name =~ incoming.name /\
1642
outgoing.arguments.count == incoming.arguments.count
1643
then
1644
if matched_index >= 0 then
1645
is_ambiguous = true
1646
break
1647
fi
1648
1649
matched_index = j
1650
fi
1651
od
1652
1653
if matched_index < 0 \/ is_ambiguous then
1654
continue
1655
fi
1656
1657
adopted.add(matched_index)
1658
1659
incoming.adopt_id(removed_functions[matched_index])
1660
1661
// the adopted id carries the outgoing symbol's proven
1662
// store-free bit; an edit that also changed the body
1663
// resets it to the conservative default, and the next
1664
// solve re-derives it
1665
if !_body_text_matches(removed_definitions[matched_index], added, outgoing_text, incoming_text) then
1666
incoming.set_proven_store_free(false)
1667
fi
1668
od
1669
si
1670
1671
// Whether two function definitions' bodies are textually
1672
// identical, each sliced from the source text its tree was
1673
// parsed from. Unavailable text or a non-function definition
1674
// compares as not matching — the conservative direction: the
1675
// carried bit resets and is re-derived.
1676
_body_text_matches(
1677
outgoing: Syntax.Trees.Definitions.Definition,
1678
incoming: Syntax.Trees.Definitions.Definition,
1679
outgoing_text: string?,
1680
incoming_text: string?
1681
) -> bool is
1682
if
1683
!isa Syntax.Trees.Definitions.FUNCTION(outgoing) \/
1684
!isa Syntax.Trees.Definitions.FUNCTION(incoming)
1685
then
1686
return false
1687
fi
1688
1689
let outgoing_body = (cast Syntax.Trees.Definitions.FUNCTION(outgoing)).body
1690
let incoming_body = (cast Syntax.Trees.Definitions.FUNCTION(incoming)).body
1691
1692
if !outgoing_body? /\ !incoming_body? then
1693
// both bodiless: nothing body-derived can have changed
1694
return true
1695
fi
1696
1697
if !outgoing_body? \/ !incoming_body? then
1698
return false
1699
fi
1700
1701
let outgoing_slice = _slice_span(outgoing_text, outgoing_body.location)
1702
let incoming_slice = _slice_span(incoming_text, incoming_body.location)
1703
1704
return outgoing_slice? /\ incoming_slice? /\ outgoing_slice =~ incoming_slice
1705
si
1706
1707
// The span's text, or null when the text is unavailable or the
1708
// span falls outside it. Lines and columns are 1-based; the end
1709
// column is taken as inclusive — the convention only has to be
1710
// applied identically to the two sides of an equality check.
1711
_slice_span(text: string?, location: Source.LOCATION) -> string? is
1712
if !text? then
1713
return null
1714
fi
1715
1716
let lines = text.replace_line_endings("\n").split(['\n'])
1717
1718
if location.start_line < 1 \/ location.end_line > lines.count \/ location.end_line < location.start_line then
1719
return null
1720
fi
1721
1722
let result = System.Text.StringBuilder()
1723
1724
for line_number in location.start_line..location.end_line + 1 do
1725
let line = lines[line_number - 1]
1726
1727
let from = if line_number == location.start_line then location.start_column - 1 else 0 fi
1728
let to mut = if line_number == location.end_line then location.end_column else line.length fi
1729
1730
if from < 0 \/ from > line.length then
1731
return null
1732
fi
1733
1734
if to > line.length then
1735
to = line.length
1736
fi
1737
1738
if to > from then
1739
result.append(line.substring(from, to - from))
1740
fi
1741
1742
if line_number < location.end_line then
1743
result.append('\n')
1744
fi
1745
od
1746
1747
return result.to_string()
1748
si
1749
1750
_remove_function_from_owner(function: Semantic.Symbols.Function) is
1751
let owner = cast Semantic.Symbols.Scoped?(function.owner)
1752
1753
if !owner? then
1754
return
1755
fi
1756
1757
let name = function.name
1758
1759
let existing = owner.find_direct(name)
1760
1761
if let group: Semantic.Symbols.FUNCTION_GROUP = existing then
1762
group.remove(function)
1763
1764
if group.is_empty then
1765
owner.remove_direct(name)
1766
fi
1767
elif existing == function then
1768
owner.remove_direct(name)
1769
fi
1770
si
1771
1772
// The incremental body re-walk for an interface-preserving single-file
1773
// EDIT: keep the retained AST registered, splice the freshly-parsed
1774
// bodies onto it, refresh its locations from the fresh parse, and
1775
// re-walk only the edited file. Returns false — restoring the fresh
1776
// parse as the registered file — if the parses do not pair or the
1777
// location refresh desyncs, so the caller falls back to a full rebuild.
1778
try_incremental_build(
1779
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
1780
checked_paths: Collections.LIST[string]
1781
) -> bool =>
1782
try_incremental_build(diagnostics, checked_paths, true, null)
1783
1784
try_incremental_build(
1785
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
1786
checked_paths: Collections.LIST[string],
1787
want_collect: bool
1788
) -> bool =>
1789
try_incremental_build(diagnostics, checked_paths, want_collect, null)
1790
1791
try_incremental_build(
1792
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
1793
checked_paths: Collections.LIST[string],
1794
want_collect: bool,
1795
extra_purge_spans: Collections.List[Source.LOCATION]?
1796
) -> bool is
1797
let retained = _retained
1798
1799
if !retained? then
1800
return _decline(WORK_COUNTERS.NO_RETAINED_PARSE)
1801
fi
1802
1803
let edited_path = retained.file_name
1804
let donor = _source_files_by_path[edited_path]
1805
1806
// A top-level `let` in a synthesised entry body declares a
1807
// symbol into the file's namespace, so an edit among the
1808
// top-level statements can add, remove or retype one with no
1809
// definition's signature changing. That namespace is private
1810
// to the file, and the re-walk re-declares the entry's
1811
// variables and then re-walks every body in the file, so
1812
// nothing outside the re-walk reads a stale one. Under
1813
// --global-namespace the variables are visible to every other
1814
// namespace-less file, and only the full rebuild re-derives
1815
// those readers.
1816
if
1817
_build_flags.want_global_namespace /\
1818
(
1819
Syntax.Process.SYNTHESISE_TOP_LEVEL_ENTRY.has_top_level_entry(retained.definition) \/
1820
Syntax.Process.SYNTHESISE_TOP_LEVEL_ENTRY.has_top_level_entry(donor.definition)
1821
)
1822
then
1823
return _decline(WORK_COUNTERS.TOP_LEVEL_STATEMENTS)
1824
fi
1825
1826
// Keep the retained AST as the registered file; the fresh parse is
1827
// only a body donor.
1828
_source_files_by_path[edited_path] = retained
1829
1830
let pairing = Syntax.FUNCTION_PAIRING(retained.definition, donor.definition)
1831
1832
let pairs = pairing.pairs
1833
1834
if !pairs? then
1835
_source_files_by_path[edited_path] = donor
1836
return _decline(WORK_COUNTERS.FUNCTION_PAIRING_DESYNC)
1837
fi
1838
1839
// Capture the pre-edit spans of the bodies about to be
1840
// re-walked, before the splice overwrites the retained bodies.
1841
// A stale symbol-use / definition entry inside one of these is
1842
// re-recorded by the re-walk, so reconciliation drops it.
1843
let body_spans = Source.BODY_SPANS()
1844
1845
for pair in pairs do
1846
if pair.retained.body? then
1847
body_spans.add(pair.retained.body!.extent)
1848
fi
1849
od
1850
1851
// A replaced definition's whole span purges the same way as a
1852
// re-walked body: its old entries drop and the replacement's
1853
// build re-records them.
1854
if extra_purge_spans? then
1855
for s in extra_purge_spans do
1856
body_spans.add(s)
1857
od
1858
fi
1859
1860
Syntax.BODY_SPLICE.apply(pairs)
1861
1862
// The lockstep reconciliation walk: copies the donor's correct
1863
// locations onto the retained interface and returns every
1864
// interface node's pre-edit -> post-edit correspondence. Null
1865
// if the two parses disagreed structurally — fall back.
1866
let correspondence = Syntax.Process.LOCATION_REFRESH.apply(retained.definition, donor.definition)
1867
1868
if !correspondence? then
1869
_source_files_by_path[edited_path] = donor
1870
return _decline(WORK_COUNTERS.LOCATION_REFRESH_DESYNC)
1871
fi
1872
1873
IoC.CONTAINER.instance.logger.clear(edited_path, true)
1874
IoC.CONTAINER.instance.logger.start_analysis()
1875
1876
// The retained tree now reflects the donor's content — spliced
1877
// bodies, donor locations — so it must carry the donor's text
1878
// too. Every successful incremental edit flows through here;
1879
// a stale text would let a later reconcile's body comparison
1880
// false-match a since-reverted body. The retained store-free
1881
// facts describe the pre-splice bodies for the same reason.
1882
retained.source_text = donor.source_text
1883
1884
_compiler.rewalk_bodies(retained, correspondence, body_spans)
1885
1886
if want_collect then
1887
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
1888
fi
1889
1890
return true
1891
si
1892
1893
handle_request(request: Protocol.Request.EDIT, writer: IO.TextWriter) is
1894
_is_interface_changed = false
1895
_retained = null
1896
_declined_reason = null
1897
1898
for i in _source_files_by_path.values do
1899
i.want_compile_expressions = false
1900
od
1901
1902
let paths = Collections.LIST[string]()
1903
1904
let want_timer = request.files.count == 1 /\ !_library_files?
1905
1906
if want_timer then
1907
_timers.start(TIMERS.edit_single_timer_name)
1908
fi
1909
1910
if _library_files? then
1911
for library_file_path in _library_files do
1912
_add_file_from_disk(library_file_path)
1913
od
1914
1915
_library_files = null
1916
fi
1917
1918
try
1919
for f in request.files do
1920
parse_and_add_file(f.path, IO.StringReader(f.source), false)
1921
1922
if let source_file = find_source_file(f.path) then
1923
source_file.source_text = f.source
1924
fi
1925
1926
paths.add(f.path)
1927
od
1928
catch ex: Exception
1929
debug_always("PARSE caught: {ex.get_type()} {ex.message}")
1930
yrt
1931
1932
// Classify this EDIT for the stats dump: interface-preserving (a
1933
// following COMPILE can be skipped) vs interface-affecting.
1934
let edit_class_timer =
1935
if _is_interface_changed then
1936
"edit-interface-affecting"
1937
else
1938
"edit-interface-preserving"
1939
fi
1940
1941
if want_timer then
1942
_timers.start(edit_class_timer)
1943
fi
1944
1945
let diagnostics = Collections.LIST[Protocol.DIAGNOSTIC]()
1946
let checked_paths = Collections.LIST[string]()
1947
1948
// An interface-preserving single-file EDIT takes the incremental
1949
// body re-walk instead of the whole-project rebuild.
1950
// On by default; `--no-incremental-analysis` turns it off.
1951
//
1952
// Neither incremental path is taken while the retained tables
1953
// are out of date: added references can change how any file
1954
// resolves, and only the whole-project rebuild reflects that.
1955
let incremental_eligible =
1956
_build_flags.want_incremental_analysis /\
1957
_compiler.are_tables_current /\
1958
!_is_interface_changed /\
1959
paths.count == 1 /\
1960
_retained? /\
1961
_retained.interface_signature?
1962
1963
// An interface-affecting edit takes the incremental interface
1964
// path - appended declarations, or one replaced declaration
1965
// whose old symbols nothing else references - instead of the
1966
// whole-project rebuild; the shape classification lives in
1967
// try_incremental_interface_edit.
1968
let interface_edit_eligible =
1969
_build_flags.want_incremental_analysis /\
1970
_compiler.are_tables_current /\
1971
_is_interface_changed /\
1972
paths.count == 1 /\
1973
_retained? /\
1974
_retained.interface_signature?
1975
1976
try
1977
Semantic.Types.NAMED.clear_cache()
1978
1979
if !_watchdog.want_restart then
1980
// Which path handled this EDIT, for the stats dump:
1981
// the body re-walk, the incremental interface path, or
1982
// the whole-project fallback. Counts across a session
1983
// are the edit-mix data the incremental work is tuned
1984
// against.
1985
let handled_by_body_rewalk =
1986
incremental_eligible /\ try_incremental_build(diagnostics, checked_paths)
1987
1988
let handled_by_interface_path =
1989
!handled_by_body_rewalk /\
1990
interface_edit_eligible /\
1991
try_incremental_interface_edit(diagnostics, checked_paths)
1992
1993
if handled_by_body_rewalk then
1994
_timers.bump(WORK_COUNTERS.EDIT_PATH_BODY_REWALK)
1995
elif handled_by_interface_path then
1996
_timers.bump(WORK_COUNTERS.EDIT_PATH_INTERFACE_INCREMENTAL)
1997
1998
// The interface path splices the new bodies in
1999
// without recompiling the file's expressions,
2000
// so its expression-level state — hover uses,
2001
// reliance records — still describes the
2002
// pre-edit bodies. Mark the file stale so the
2003
// debounced COMPILE recompiles exactly this
2004
// file and trues that state up.
2005
for edited_path in paths do
2006
if let edited = find_source_file(edited_path) then
2007
_compiler.invalidate_file_expressions(edited)
2008
fi
2009
od
2010
2011
_compiler.is_full_compile_needed = true
2012
fi
2013
2014
if !handled_by_body_rewalk /\ !handled_by_interface_path then
2015
_timers.bump(WORK_COUNTERS.EDIT_PATH_FULL_REBUILD)
2016
2017
// Exactly one reason per rebuilt edit: the guard
2018
// that gave up, or not-eligible when no
2019
// incremental path was attempted (the flag is off,
2020
// the edit spans several files, or this is the
2021
// first parse of the file).
2022
WORK_COUNTERS.declined(
2023
_timers,
2024
_declined_reason ?? WORK_COUNTERS.NOT_ELIGIBLE
2025
)
2026
2027
for i in _source_files_by_path.values do
2028
i.want_compile_up_to_expressions = true
2029
2030
IoC.CONTAINER.instance.logger.clear_global_declaration_diagnostics(i.file_name)
2031
od
2032
2033
// Match the COMPILE / query-miss rebuild paths:
2034
// retained ASTs carry expression-level state from
2035
// the previous build - IR values and types that
2036
// reference the symbols clear_symbols is about to
2037
// abandon. Clear it so no later walk of a retained
2038
// tree consumes state from a dead symbol
2039
// generation.
2040
let clear_state = Syntax.Process.CLEAR_STATE_VISITOR()
2041
for i in _source_files_by_path.values do
2042
clear_state.apply(i.definition)
2043
od
2044
2045
IoC.CONTAINER.instance.state_store_registry.clear_all()
2046
2047
IoC.CONTAINER.instance.logger.start_analysis()
2048
2049
_compiler.clear_symbols(paths)
2050
2051
_compiler.queue(self)
2052
2053
// Which function the assembly enters at is settled
2054
// by the debounced COMPILE, not here: an edit
2055
// reaching this path rebuilds the whole project,
2056
// and for a file carrying top-level statements
2057
// that is every edit.
2058
_compiler.build(false)
2059
2060
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
2061
fi
2062
fi
2063
2064
catch missing: Logging.MissingBuiltInException
2065
MISSING_BUILT_IN_REPORT.record(missing, _source_files_by_path.keys, diagnostics, checked_paths)
2066
catch ex: Exception
2067
debug_always("ANALYSE caught: {ex.get_type()} {ex.message}")
2068
2069
_watchdog.request_restart()
2070
finally
2071
_compiler.clear_queue()
2072
2073
IoC.CONTAINER.instance.logger.end_analysis()
2074
2075
if want_timer then
2076
_timers.finish(TIMERS.edit_single_timer_name)
2077
_timers.finish(edit_class_timer)
2078
fi
2079
2080
let compile_needed =
2081
_is_interface_changed /\
2082
(_source_files_by_path.values |> any(i => !i.want_compile_expressions))
2083
2084
if compile_needed then
2085
_compiler.is_full_compile_needed = true
2086
fi
2087
2088
let elapsed_ms = _timers.edit_single_timer.max_average_milliseconds
2089
2090
_timers.start("edit-write-response")
2091
2092
JSON_PROTOCOL.write_response(
2093
writer,
2094
Protocol.Response.DIAGNOSTICS(diagnostics, checked_paths, "partial", elapsed_ms, compile_needed)
2095
)
2096
2097
_timers.finish("edit-write-response")
2098
yrt
2099
si
2100
si
2101
2102
// Turns each file's replaced span into that file's whole new text,
2103
// then hands the result to the ordinary edit path.
2104
//
2105
// Reconstruction rather than partial reparsing is the whole of what
2106
// this saves: a client sending a keystroke's worth of text instead of
2107
// a whole file pays for the edit it made rather than for the size of
2108
// the file it made it in. Everything downstream sees exactly what an
2109
// edit carrying the same text would.
2110
//
2111
// Keeping the two copies of a file in step is the client's job: it
2112
// sends a delta only when it knows what the analyser holds, and sends
2113
// whole text whenever it does not. `expected_length` is the assertion
2114
// that it did so, not a negotiation - splicing a span into text that
2115
// is not the text it was computed against yields a plausible file
2116
// rather than a failure, and every later delta compounds it. So a
2117
// mismatch, or a delta for a file the analyser does not hold, is
2118
// treated as the desync it is: the analyser recycles, and the
2119
// replacement is primed with full text by the same path that primes
2120
// any freshly-spawned one.
2121
class EDIT_DELTA_HANDLER(
2122
_watchdog: WATCHDOG,
2123
_file_edited_handler: FILE_EDITED_HANDLER
2124
): RequestHandler[Protocol.Request.EDIT_DELTA] is
2125
super()
2126
2127
handle_request(request: Protocol.Request.EDIT_DELTA, writer: IO.TextWriter) is
2128
let files = Collections.LIST[Protocol.EDIT_FILE]()
2129
2130
for delta in request.files do
2131
let retained = _file_edited_handler.find_source_file(delta.path)?.source_text
2132
2133
if !retained? then
2134
_desynchronized(writer, delta.path, "no retained text for this file")
2135
2136
return
2137
fi
2138
2139
if retained.length != delta.expected_length then
2140
_desynchronized(
2141
writer,
2142
delta.path,
2143
"retained text is {retained.length} characters, "
2144
"the delta expected {delta.expected_length}"
2145
)
2146
2147
return
2148
fi
2149
2150
let edited = _apply(retained, delta)
2151
2152
if !edited? then
2153
_desynchronized(
2154
writer,
2155
delta.path,
2156
"the replaced span falls outside the retained text"
2157
)
2158
2159
return
2160
fi
2161
2162
files.add(Protocol.EDIT_FILE(delta.path, edited))
2163
od
2164
2165
_file_edited_handler.handle_request(Protocol.Request.EDIT(files), writer)
2166
si
2167
2168
// Report the desync and recycle. Recycling rather than answering
2169
// is what makes this safe to get wrong in only one direction: the
2170
// analyser never carries on holding text it cannot account for,
2171
// and the process that replaces it starts from whatever the
2172
// client sends it.
2173
_desynchronized(writer: IO.TextWriter, path: string, reason: string) is
2174
Std.error.write_line("edit delta for {path} cannot be applied: {reason}")
2175
Std.error.flush()
2176
2177
_watchdog.recycle(writer, "edit delta out of step with retained text")
2178
si
2179
2180
// `text` with the delta's span replaced by its new text, or null
2181
// when the span does not lie within `text`.
2182
_apply(text: string, delta: Protocol.EDIT_DELTA_FILE) -> string? is
2183
let start = _offset_of(text, delta.start_line, delta.start_column)
2184
let stop = _offset_of(text, delta.end_line, delta.end_column)
2185
2186
if start < 0 \/ stop < start then
2187
return null
2188
fi
2189
2190
let result = System.Text.StringBuilder()
2191
2192
result.append(text.substring(0, start))
2193
result.append(delta.new_text)
2194
result.append(text.substring(stop))
2195
2196
return result.to_string()
2197
si
2198
2199
// Character offset of a 1-based line and column in `text`, or -1
2200
// when the position lies outside it. Lines are delimited by a
2201
// newline and a carriage return belongs to the line it ends, so a
2202
// column counts the characters a client's own buffer holds
2203
// whichever line endings the file uses.
2204
_offset_of(text: string, line: int, column: int) -> int static is
2205
if line < 1 \/ column < 1 then
2206
return -1
2207
fi
2208
2209
let offset mut = 0
2210
let current_line mut = 1
2211
2212
while current_line < line do
2213
let next_line_start = text.index_of('\n', offset)
2214
2215
if next_line_start < 0 then
2216
return -1
2217
fi
2218
2219
offset = next_line_start + 1
2220
current_line = current_line + 1
2221
od
2222
2223
let line_end mut = text.index_of('\n', offset)
2224
2225
if line_end < 0 then
2226
line_end = text.length
2227
fi
2228
2229
let result = offset + column - 1
2230
2231
if result > line_end then
2232
return -1
2233
fi
2234
2235
return result
2236
si
2237
si
2238
2239
// Does a full compile of all files.
2240
class COMPILE_HANDLER(
2241
_watchdog: WATCHDOG,
2242
_timers: TIMERS,
2243
_compiler: COMPILER,
2244
_source_files: Iterable[SOURCE_FILE],
2245
_build_flags: GLOBAL_BUILD_FLAGS
2246
): RequestHandler[Protocol.Request.COMPILE] is
2247
super()
2248
2249
// Whether an edit has arrived that supersedes the compile now
2250
// running. Consulted at per-file boundaries of the
2251
// expressions-only walk, which is the one branch below that can
2252
// be abandoned part-way: stopping there leaves the remaining
2253
// files at up-to-expressions with no use-map entries, which is
2254
// what a single-file edit's rebuild leaves anyway and what
2255
// is_full_compile_needed already describes. Unset outside
2256
// analysis mode, and never consulted by the whole-project
2257
// rebuild, which has no such halfway state.
2258
is_superseded: (() -> bool)? public
2259
2260
// A flipped function's callers are enumerable from the recorded
2261
// references only when calls bind the function itself through
2262
// ordinary name lookup: a named, source-declared function that
2263
// its owner's scope resolves to directly (alone or through its
2264
// overload group). Everything else - reflected imports whose
2265
// trust changed, and accessor functions reached through a
2266
// property or indexer read, which record the use against the
2267
// property symbol - has callers this scan can't see.
2268
_has_complete_references(function: Semantic.Symbols.Function) -> bool is
2269
if function.is_reflected then
2270
return false
2271
fi
2272
2273
let name = function.name
2274
2275
let owner = cast Semantic.Symbols.Scoped?(function.owner)
2276
2277
if !owner? then
2278
return false
2279
fi
2280
2281
let found = owner.find_direct(name)
2282
2283
if !found? then
2284
return false
2285
fi
2286
2287
if found == cast Semantic.Symbols.Symbol(function) then
2288
return true
2289
fi
2290
2291
if isa Semantic.Symbols.FUNCTION_GROUP(found) then
2292
return (cast Semantic.Symbols.FUNCTION_GROUP(found)).functions |> any(f => f == function)
2293
fi
2294
2295
return false
2296
si
2297
2298
_was_superseded() -> bool is
2299
let superseded = is_superseded
2300
2301
return superseded? /\ superseded()
2302
si
2303
2304
handle_request(request: Protocol.Request.COMPILE, writer: IO.TextWriter) is
2305
let diagnostics = Collections.LIST[Protocol.DIAGNOSTIC]()
2306
let checked_paths = Collections.LIST[string]()
2307
2308
let compile_needed mut = false
2309
2310
// Spans the whole handler, whichever branch it takes, so the
2311
// elapsed time reported to the client covers what the client
2312
// waited for. The per-branch timers below split that total.
2313
_timers.start(TIMERS.compile_timer_name)
2314
2315
try
2316
if !_watchdog.want_restart then
2317
if !_compiler.is_full_compile_needed then
2318
// No interface-affecting edit pending — the rebuild is
2319
// skipped. Timed under `compile-skipped` so the
2320
// skipped-vs-run split shows in the stats dump.
2321
_timers.start("compile-skipped")
2322
2323
// The incremental EDIT paths and the targeted
2324
// recompile above record reliances without
2325
// judging them; re-solve the effect relations
2326
// over the edited bodies and judge here, inside
2327
// the analysis window so the errors are
2328
// expression-level diagnostics.
2329
_timers.start("infer-effects")
2330
IoC.CONTAINER.instance.logger.start_analysis()
2331
_compiler.run_effects_pass(_source_files)
2332
_timers.finish("infer-effects")
2333
2334
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
2335
2336
_timers.finish("compile-skipped")
2337
elif _compiler.are_tables_current then
2338
// A full compile is needed because some files'
2339
// expression-level diagnostics are stale, but the
2340
// declaration-level tables already reflect every
2341
// file's source - the interface-affecting EDIT that
2342
// raised the flag rebuilt them. Skip the redundant
2343
// clear-and-rebuild and run compile-expressions for
2344
// just the files that lack it. No CLEAR_STATE: the
2345
// rebuild cleared these files and re-resolved their
2346
// type expressions, which the expression walk
2347
// consumes.
2348
_timers.start("compile-run-expressions-only")
2349
// these bodies join the descent at the top; see
2350
// COMPILER._restart_descent
2351
Syntax.Process.KILL_LEDGER.kills_suspended = true
2352
2353
IoC.CONTAINER.instance.logger.start_analysis()
2354
2355
for i in _source_files do
2356
if _was_superseded() then
2357
break
2358
fi
2359
2360
if !_compiler.is_compiled_through_expressions(i) then
2361
// Cleared as each file is walked rather
2362
// than for every file before the walk
2363
// starts, so that abandoning the walk
2364
// cannot leave a file cleared and not
2365
// recomputed. Such a file reports as
2366
// checked with nothing against it, which
2367
// a client reads as having come back
2368
// clean and acts on by dropping the
2369
// squiggles it still has.
2370
IoC.CONTAINER.instance.logger.clear_expression_diagnostics(i.file_name)
2371
Syntax.Process.PURE_SLOTS.clear_for(i.file_name)
2372
Syntax.Process.KILL_LEDGER.clear_for(i.file_name)
2373
2374
_compiler.compile_expressions_only(i)
2375
fi
2376
od
2377
2378
if _was_superseded() then
2379
// The walk is part-done: the files it
2380
// reached are current and the rest hold what
2381
// the previous compile left, which is what
2382
// an edit's own rebuild leaves too. Nothing
2383
// is reported because the reliance judge
2384
// below never ran, so what the store holds
2385
// for the walked files is incomplete. Say a
2386
// compile is still needed; the edit that
2387
// superseded this one answers next.
2388
_timers.bump("compile-superseded")
2389
2390
compile_needed = true
2391
else
2392
// judge the reliances the expression recompiles
2393
// above just recorded, against freshly-solved
2394
// effect relations
2395
_timers.start("infer-effects")
2396
_compiler.run_effects_pass(_source_files)
2397
_timers.finish("infer-effects")
2398
2399
// No build ran on this path, so the entry
2400
// point is settled here. The branch below
2401
// does build, and settles it there.
2402
_compiler.select_entry_point(_source_files)
2403
2404
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
2405
2406
_watchdog.note_full_compile()
2407
2408
_compiler.is_full_compile_needed = false
2409
fi
2410
2411
_timers.finish("compile-run-expressions-only")
2412
elif _source_files |> any(f => true) then
2413
// A compile can arrive before the first EDIT has
2414
// registered any source files. Building then is not
2415
// a harmless no-op: it clears the symbol table and
2416
// runs the pass barriers with nothing queued, so the
2417
// same reasoning as compile_all's empty-state guard
2418
// applies — answer from the empty state instead;
2419
// the first real EDIT performs the initial build.
2420
_timers.start("compile-run")
2421
2422
Semantic.Types.NAMED.clear_cache()
2423
2424
for i in _source_files do
2425
IoC.CONTAINER.instance.logger.clear(i.file_name, true)
2426
2427
i.want_compile_up_to_expressions = true
2428
i.want_compile_expressions = true
2429
od
2430
2431
let clear_state = Syntax.Process.CLEAR_STATE_VISITOR()
2432
for i in _source_files do
2433
clear_state.apply(i.definition)
2434
od
2435
2436
IoC.CONTAINER.instance.state_store_registry.clear_all()
2437
2438
IoC.CONTAINER.instance.logger.start_analysis()
2439
2440
_compiler.clear_symbols()
2441
2442
_compiler.queue(_source_files)
2443
2444
_compiler.build()
2445
2446
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, diagnostics, checked_paths)
2447
2448
_watchdog.note_full_compile()
2449
2450
_compiler.is_full_compile_needed = false
2451
2452
_timers.finish("compile-run")
2453
fi
2454
fi
2455
2456
catch missing: Logging.MissingBuiltInException
2457
MISSING_BUILT_IN_REPORT.record(missing, _source_files |> map(f => f.file_name), diagnostics, checked_paths)
2458
catch ex: Exception
2459
debug_always("FULL COMPILE caught: {ex.get_type()} {ex.message}")
2460
2461
_watchdog.request_restart()
2462
finally
2463
IoC.CONTAINER.instance.logger.end_analysis()
2464
2465
_compiler.clear_queue()
2466
2467
_timers.finish(TIMERS.compile_timer_name)
2468
2469
let elapsed_ms = _timers.compile_timer.max_average_milliseconds
2470
2471
2472
JSON_PROTOCOL.write_response(
2473
writer,
2474
Protocol.Response.DIAGNOSTICS(diagnostics, checked_paths, "full", elapsed_ms, compile_needed)
2475
)
2476
yrt
2477
si
2478
si
2479
2480
class COMPLETION_HANDLER(
2481
_watchdog: WATCHDOG,
2482
_completer: Syntax.Process.COMPLETER,
2483
_source_file_lookup: SourceFileLookup,
2484
_full_compiler: FULL_COMPILER
2485
): RequestHandler[Protocol.Request.COMPLETE] is
2486
super()
2487
2488
handle_request(request: Protocol.Request.COMPLETE, writer: IO.TextWriter) is
2489
let path = request.path
2490
let target_line = request.line
2491
let target_column = request.column
2492
2493
let source_file = _source_file_lookup.find_source_file(path)
2494
2495
if !source_file? \/ !_full_compiler.is_compiled_through_expressions(source_file) then
2496
_full_compiler.compile_all(writer, path)
2497
fi
2498
2499
let items = Collections.LIST[Protocol.COMPLETION_ITEM]()
2500
2501
try
2502
if !_watchdog.want_restart then
2503
let results = find_completions(path, target_line, target_column)
2504
2505
// The client asked because the user typed a `.`, but
2506
// there is no member access at that position in the
2507
// text this analyser holds — so the text is older
2508
// than the client's and the enclosing scope is not
2509
// an answer to what was asked. Offering it anyway
2510
// fills the list with locals, globals, namespaces
2511
// and statement keywords, none of which can follow
2512
// a `.`.
2513
let answers_the_question =
2514
!request.is_member_trigger \/ _completer.is_member_completion
2515
2516
if results? /\ answers_the_question then
2517
for pair in results do
2518
let is_operator = Lexical.TOKENIZER.is_operator_name(pair.key)
2519
2520
items.add(
2521
Protocol.COMPLETION_ITEM(
2522
pair.key,
2523
if is_operator then
2524
cast int(Semantic.Symbols.CompletionKind.OPERATOR)
2525
else
2526
cast int(pair.value.completion_kind)
2527
fi,
2528
pair.value.signature,
2529
pair.value.kind_label,
2530
if is_operator then
2531
operator_insert_text(pair.key)
2532
else
2533
""
2534
fi,
2535
pair.value.collapse_group_if_single_member().doc,
2536
pair.value.collapse_group_if_single_member().deprecation?
2537
)
2538
)
2539
od
2540
fi
2541
2542
if answers_the_question then
2543
for keyword in _completer.keyword_results do
2544
items.add(
2545
Protocol.COMPLETION_ITEM(
2546
keyword.name,
2547
cast int(Semantic.Symbols.CompletionKind.KEYWORD),
2548
"",
2549
"keyword",
2550
keyword.snippet,
2551
null,
2552
false
2553
)
2554
)
2555
od
2556
fi
2557
fi
2558
catch ex: Exception
2559
debug_always("COMPLETION caught: {ex.get_type()} {ex.message}")
2560
2561
_watchdog.request_restart()
2562
yrt
2563
2564
Std.error.flush()
2565
2566
JSON_PROTOCOL.write_response(writer, Protocol.Response.COMPLETION(items))
2567
si
2568
2569
// What to insert for an operator member offered after a `.`.
2570
// Inserting the bare name would put the operator's characters
2571
// straight after the dot, where the tokenizer scans them as one
2572
// run and produces a single operator token (`.=~`) rather than a
2573
// member access. The backtick escape is the spelling that always
2574
// lexes as a member name, whatever the operator is made of.
2575
//
2576
// The client renders this as an LSP snippet, so the two
2577
// characters that carry meaning there are escaped; `$` and `\`
2578
// are both operator characters, so both can genuinely occur.
2579
operator_insert_text(name: string) -> string is
2580
let buffer = System.Text.StringBuilder("`")
2581
2582
for c in name do
2583
if c == '$' \/ c == '\\' then
2584
buffer.append('\\')
2585
fi
2586
2587
buffer.append(c)
2588
od
2589
2590
return buffer.to_string()
2591
si
2592
2593
find_completions(path: string, target_line: int, target_column: int) -> Iterable[Pair[string,Semantic.Symbols.Symbol]]? is
2594
let i = _source_file_lookup.find_source_file(path)
2595
2596
if !i? then
2597
return null
2598
fi
2599
2600
return _completer.find_completions(i.definition, target_line, target_column)
2601
si
2602
si
2603
2604
class SIGNATURE_HANDLER(
2605
_watchdog: WATCHDOG,
2606
_signature_help: Syntax.Process.SIGNATURE_HELP,
2607
_source_file_lookup: SourceFileLookup
2608
): RequestHandler[Protocol.Request.SIGNATURE] is
2609
super()
2610
2611
handle_request(request: Protocol.Request.SIGNATURE, writer: IO.TextWriter) is
2612
let best_signature_index: int mut = 0
2613
let current_parameter_index: int mut = 0
2614
let signatures = Collections.LIST[Protocol.SIGNATURE_DTO]()
2615
2616
try
2617
let path = request.path
2618
let target_line = request.line
2619
let target_column = request.column
2620
2621
if !_watchdog.want_restart then
2622
let results = find_signatures(path, target_line, target_column)
2623
2624
if results? then
2625
best_signature_index = results.best_signature_index
2626
current_parameter_index = results.current_parameter_index
2627
2628
for signature in results.signatures do
2629
signatures.add(to_signature_dto(signature))
2630
od
2631
fi
2632
fi
2633
catch ex: Exception
2634
debug_always("SIGNATURE caught: {ex.get_type()} {ex.message}")
2635
2636
_watchdog.request_restart()
2637
yrt
2638
2639
Std.error.flush()
2640
2641
JSON_PROTOCOL.write_response(
2642
writer,
2643
Protocol.Response.SIGNATURE(best_signature_index, current_parameter_index, signatures)
2644
)
2645
si
2646
2647
to_signature_dto(signature: Syntax.Process.SIGNATURE) -> Protocol.SIGNATURE_DTO is
2648
let parameters = Collections.LIST[string]()
2649
2650
for parameter_description in signature.parameter_descriptions do
2651
parameters.add(parameter_description)
2652
od
2653
2654
return Protocol.SIGNATURE_DTO(signature.description, parameters)
2655
si
2656
2657
find_signatures(path: string, target_line: int, target_column: int) -> Syntax.Process.SIGNATURE_HELP_RESULT? is
2658
let i = _source_file_lookup.find_source_file(path)
2659
2660
if !i? then
2661
return null
2662
fi
2663
2664
return _signature_help.find_signatures(i.definition, target_line, target_column)
2665
si
2666
si
2667
2668
class SYMBOLS_HANDLER(
2669
_watchdog: WATCHDOG,
2670
_symbol_definition_locations: Semantic.SYMBOL_DEFINITION_LOCATIONS,
2671
_source_file_lookup: SourceFileLookup,
2672
_full_compiler: FULL_COMPILER
2673
): RequestHandler[Protocol.Request.SYMBOLS] is
2674
super()
2675
2676
handle_request(request: Protocol.Request.SYMBOLS, writer: IO.TextWriter) is
2677
let path = request.path
2678
2679
let files = Collections.LIST[Protocol.SYMBOL_FILE]()
2680
2681
try
2682
if !_watchdog.want_restart then
2683
if path? /\ path.length > 0 then
2684
// VSCode can race the initial outline request in ahead
2685
// of the first project COMPILE; without this re-compile
2686
// the outline stays empty until the user provokes one
2687
// another way (e.g. workspace symbol search).
2688
if !_symbol_definition_locations.has_definitions_for(path) then
2689
_full_compiler.compile_all(writer, path)
2690
fi
2691
2692
add_symbol_file(files, path, _symbol_definition_locations.find_definitions_from_file(path, false))
2693
else
2694
_full_compiler.compile_all(writer)
2695
2696
for i in _source_file_lookup.file_names do
2697
if i.length > 0 then
2698
add_symbol_file(files, i, _symbol_definition_locations.find_definitions_from_file(i, true))
2699
fi
2700
od
2701
fi
2702
fi
2703
catch ex: Exception
2704
debug_always("SYMBOLS caught: {ex.get_type()} {ex.message}")
2705
2706
_watchdog.request_restart()
2707
yrt
2708
2709
Std.error.flush()
2710
2711
JSON_PROTOCOL.write_response(writer, Protocol.Response.SYMBOLS(files))
2712
si
2713
2714
add_symbol_file(files: Collections.LIST[Protocol.SYMBOL_FILE], path: string, symbols: Iterable[Semantic.Symbols.Symbol]?) is
2715
let symbol_file = Protocol.SYMBOL_FILE(path)
2716
2717
if symbols? then
2718
for symbol in symbols |> filter(s => !s.is_internal) do
2719
try
2720
symbol_file.symbols.add(to_symbol_dto(symbol))
2721
catch ex: Exception
2722
yrt
2723
od
2724
fi
2725
2726
files.add(symbol_file)
2727
si
2728
2729
to_symbol_dto(symbol: Semantic.Symbols.Symbol) -> Protocol.SYMBOL_DTO =>
2730
let qualified_name = symbol.qualified_name in
2731
// A definition in a file-private namespace has no qualifier: its
2732
// qualified name is its own name.
2733
let qualifier =
2734
if qualified_name.length > symbol.name.length then
2735
qualified_name.substring(0, qualified_name.length - symbol.name.length - 1)
2736
else
2737
""
2738
fi in
2739
Protocol.SYMBOL_DTO(
2740
symbol.search_description,
2741
cast int(symbol.symbol_kind),
2742
symbol.span.start_line,
2743
symbol.span.start_column,
2744
symbol.span.end_line,
2745
symbol.span.end_column,
2746
symbol.location.start_line,
2747
symbol.location.start_column,
2748
qualifier
2749
)
2750
si
2751
2752
class REFERENCES_HANDLER(
2753
_watchdog: WATCHDOG,
2754
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
2755
_full_compiler: FULL_COMPILER
2756
): RequestHandler[Protocol.Request.REFERENCES] is
2757
super()
2758
2759
handle_request(request: Protocol.Request.REFERENCES, writer: IO.TextWriter) is
2760
let locations = Collections.LIST[Protocol.LOCATION_DTO]()
2761
2762
try
2763
let path = request.path
2764
let line = request.line
2765
let column = request.column
2766
2767
if !_watchdog.want_restart then
2768
let symbol mut = _symbol_use_locations.find_definition_from_use(path, line, column)
2769
2770
let references mut =
2771
if symbol? then
2772
_symbol_use_locations.find_references_to_symbol(symbol)
2773
else
2774
null
2775
fi
2776
2777
// A non-local symbol's references span the whole project. A
2778
// file walked only up to expressions contributes its
2779
// type-annotation uses but none of its member-access, call,
2780
// or construction uses, so a use map built while any file
2781
// is short of expressions is silently incomplete even when
2782
// non-empty. Completeness therefore hinges on every file
2783
// reaching expressions. Locals are scoped to one function
2784
// body in the edited file, which is always fully compiled,
2785
// so an empty answer for them is authoritative.
2786
let needs_full_compile =
2787
!symbol? \/
2788
(!symbol.is_local /\ !_full_compiler.all_compiled_through_expressions())
2789
2790
if needs_full_compile then
2791
_full_compiler.compile_all(writer)
2792
2793
symbol = _symbol_use_locations.find_definition_from_use(path, line, column)
2794
2795
if symbol? then
2796
references = _symbol_use_locations.find_references_to_symbol(symbol)
2797
fi
2798
fi
2799
2800
if references? then
2801
append_location_dtos(locations, references)
2802
fi
2803
fi
2804
2805
catch ex: Exception
2806
debug_always("REFERENCES caught: {ex.get_type()} {ex.message}")
2807
yrt
2808
2809
Std.error.flush()
2810
2811
JSON_PROTOCOL.write_response(writer, Protocol.Response.REFERENCES(locations))
2812
si
2813
si
2814
2815
class TYPE_DEFINITION_HANDLER(
2816
_watchdog: WATCHDOG,
2817
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
2818
_full_compiler: FULL_COMPILER
2819
): RequestHandler[Protocol.Request.TYPE_DEFINITION] is
2820
super()
2821
2822
handle_request(request: Protocol.Request.TYPE_DEFINITION, writer: IO.TextWriter) is
2823
let path = request.path
2824
let line = request.line
2825
let column = request.column
2826
2827
let locations = Collections.LIST[Protocol.LOCATION_DTO]()
2828
2829
try
2830
if !_watchdog.want_restart then
2831
let symbol mut = _symbol_use_locations.find_definition_from_use(path, line, column)
2832
2833
if !symbol? then
2834
_full_compiler.compile_all(writer, path)
2835
2836
symbol = _symbol_use_locations.find_definition_from_use(path, line, column)
2837
fi
2838
2839
if symbol? then
2840
let type_symbol = type_symbol_of(symbol)
2841
2842
if type_symbol? /\ !type_symbol.is_internal /\ !type_symbol.is_reflected then
2843
append_location_dtos(locations, [type_symbol.location])
2844
fi
2845
fi
2846
fi
2847
catch e: Exception
2848
debug_always("TYPEDEFINITION caught: {e.get_type()}: {e.message}")
2849
_watchdog.request_restart()
2850
yrt
2851
2852
Std.error.flush()
2853
2854
JSON_PROTOCOL.write_response(writer, Protocol.Response.TYPE_DEFINITION(locations))
2855
si
2856
2857
// If the cursor sits on a type symbol itself (class/trait/struct/union/variant),
2858
// return that symbol — the type IS the type. Otherwise hop to the symbol's
2859
// declared type and return its symbol. Returns null when no useful answer
2860
// can be given.
2861
type_symbol_of(symbol: Semantic.Symbols.Symbol) -> Semantic.Symbols.Symbol? is
2862
if symbol.is_type then
2863
return symbol
2864
fi
2865
2866
let t = symbol.type
2867
2868
return t?.symbol
2869
si
2870
si
2871
2872
class IMPLEMENTATION_HANDLER(
2873
_watchdog: WATCHDOG,
2874
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
2875
_full_compiler: FULL_COMPILER
2876
): RequestHandler[Protocol.Request.IMPLEMENTATION] is
2877
super()
2878
2879
handle_request(request: Protocol.Request.IMPLEMENTATION, writer: IO.TextWriter) is
2880
let locations = Collections.LIST[Protocol.LOCATION_DTO]()
2881
2882
try
2883
let path = request.path
2884
let line = request.line
2885
let column = request.column
2886
2887
if !_watchdog.want_restart then
2888
let symbol mut = _symbol_use_locations.find_definition_from_use(path, line, column)
2889
2890
let implementations mut =
2891
if symbol? then
2892
_symbol_use_locations.find_implementations_of_symbol(symbol)
2893
else
2894
null
2895
fi
2896
2897
// Locals can't be inherited, so skip the compile_all
2898
// fallback for them. For non-locals, only recompile if
2899
// we didn't find anything — a warm cache answers
2900
// directly.
2901
let needs_full_compile =
2902
!symbol? \/
2903
(!symbol.is_local /\ (!implementations? \/ implementations |> count() == 0))
2904
2905
if needs_full_compile then
2906
_full_compiler.compile_all(writer)
2907
2908
symbol = _symbol_use_locations.find_definition_from_use(path, line, column)
2909
2910
if symbol? then
2911
implementations = _symbol_use_locations.find_implementations_of_symbol(symbol)
2912
fi
2913
fi
2914
2915
if implementations? then
2916
append_location_dtos(locations, implementations)
2917
fi
2918
fi
2919
2920
catch ex: Exception
2921
debug_always("IMPLEMENTATION caught: {ex.get_type()} {ex.message}")
2922
yrt
2923
2924
Std.error.flush()
2925
2926
JSON_PROTOCOL.write_response(writer, Protocol.Response.IMPLEMENTATION(locations))
2927
si
2928
si
2929
2930
class RENAME_REQUEST_HANDLER(
2931
_watchdog: WATCHDOG,
2932
_symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS,
2933
_full_compiler: FULL_COMPILER
2934
): RequestHandler[Protocol.Request.RENAME] is
2935
super()
2936
2937
handle_request(request: Protocol.Request.RENAME, writer: IO.TextWriter) is
2938
let edits = Collections.LIST[Protocol.RENAME_EDIT]()
2939
2940
try
2941
let path = request.path
2942
let line = request.line
2943
let column = request.column
2944
let new_name = request.new_name
2945
2946
if !_watchdog.want_restart then
2947
let symbol mut = _symbol_use_locations.find_definition_from_use(path, line, column)
2948
2949
let locations mut =
2950
if symbol? then
2951
_symbol_use_locations.find_references_to_symbol_for_rename(symbol)
2952
else
2953
null
2954
fi
2955
2956
// Locals can't be renamed across files, so the local-only
2957
// edit set is authoritative. A non-local rename spans the
2958
// whole project; a file walked only up to expressions
2959
// contributes its type-annotation uses but none of its
2960
// member-access, call, or construction uses, so the edit
2961
// set looks non-empty yet silently misses those
2962
// occurrences. Completeness hinges on every file reaching
2963
// expressions.
2964
let needs_full_compile =
2965
!symbol? \/
2966
(!symbol.is_local /\ !_full_compiler.all_compiled_through_expressions())
2967
2968
if needs_full_compile then
2969
_full_compiler.compile_all(writer)
2970
2971
symbol = _symbol_use_locations.find_definition_from_use(path, line, column)
2972
2973
if symbol? then
2974
locations = _symbol_use_locations.find_references_to_symbol_for_rename(symbol)
2975
fi
2976
fi
2977
2978
if locations? then
2979
for location in locations do
2980
edits.add(
2981
Protocol.RENAME_EDIT(
2982
location.file_name,
2983
location.start_line,
2984
location.start_column,
2985
location.end_line,
2986
location.end_column,
2987
new_name
2988
)
2989
)
2990
od
2991
fi
2992
fi
2993
2994
catch ex: Exception
2995
debug_always("RENAMEREQUEST caught: {ex.get_type()} {ex.message}")
2996
yrt
2997
2998
Std.error.flush()
2999
3000
JSON_PROTOCOL.write_response(writer, Protocol.Response.RENAME(edits))
3001
si
3002
si
3003
3004
class RESTART_HANDLER(_watchdog: WATCHDOG): RequestHandler[Protocol.Request.RESTART] is
3005
super()
3006
3007
handle_request(request: Protocol.Request.RESTART, writer: IO.TextWriter) is
3008
_watchdog.recycle(writer, "client requested restart")
3009
si
3010
si
3011
3012
// Records the client's current open-file set, replacing it wholesale.
3013
// Fire-and-forget — no response frame. Editor-only hints are produced
3014
// only for open files; see Logging.Logger.set_open_files.
3015
//
3016
// Hint gating changed for the files that were opened or closed, and
3017
// for no others, so those files' expression walks are the whole of
3018
// what this invalidates: their compile-expressions state is dropped
3019
// and their compiled-through marker cleared, which puts them in the
3020
// set the next COMPILE's expressions-only pass re-walks. Clearing the
3021
// state here rather than re-walking it defers the cost to the
3022
// debounced compile, so opening a file costs nothing at the moment it
3023
// opens.
3024
class SET_OPEN_FILES_HANDLER(
3025
_compiler: COMPILER,
3026
_source_files: SourceFileLookup
3027
): RequestHandler[Protocol.Request.SET_OPEN_FILES] is
3028
super()
3029
3030
handle_request(request: Protocol.Request.SET_OPEN_FILES, writer: IO.TextWriter) is
3031
let changed = IoC.CONTAINER.instance.logger.set_open_files(request.paths)
3032
3033
let any_invalidated mut = false
3034
3035
for path in changed do
3036
if let source_file = _source_files.find_source_file(path) then
3037
_compiler.invalidate_file_expressions(source_file)
3038
3039
any_invalidated = true
3040
fi
3041
od
3042
3043
if any_invalidated then
3044
_compiler.is_full_compile_needed = true
3045
fi
3046
si
3047
si
3048
3049
// Synthesizes quick fixes for one file over one range, on demand.
3050
//
3051
// Fixes are deliberately not attached to the diagnostics a compile
3052
// reports. Synthesis walks the whole AST of every file holding a coded
3053
// diagnostic, comparing every node against every diagnostic in that
3054
// file, so attaching them to an EDIT response costs the whole project
3055
// on every keystroke - and almost all of it is discarded, because the
3056
// user only ever asks for actions at one cursor position. Answering a
3057
// range instead makes the cost independent of both project size and
3058
// diagnostic count.
3059
//
3060
// Reads the diagnostics the last compile left in the store; it never
3061
// recompiles. A code action is a follow-up to squiggles the client is
3062
// already showing, so the store holds the same compile those came
3063
// from.
3064
class CODE_ACTIONS_HANDLER(
3065
_watchdog: WATCHDOG,
3066
_source_files: SourceFileLookup
3067
): RequestHandler[Protocol.Request.CODE_ACTIONS] is
3068
super()
3069
3070
handle_request(request: Protocol.Request.CODE_ACTIONS, writer: IO.TextWriter) is
3071
let in_range = Collections.LIST[Protocol.DIAGNOSTIC]()
3072
3073
try
3074
if !_watchdog.want_restart then
3075
let all = Collections.LIST[Protocol.DIAGNOSTIC]()
3076
let checked_paths = Collections.LIST[string]()
3077
3078
DIAGNOSTICS_COLLECTOR.collect_into(IoC.CONTAINER.instance.logger, all, checked_paths)
3079
3080
for d in all do
3081
if d.path =~ request.path /\ _overlaps(request, d) then
3082
in_range.add(d)
3083
fi
3084
od
3085
3086
let source_file = _source_files.find_source_file(request.path)
3087
3088
if in_range.count > 0 /\ source_file? then
3089
let one = Collections.LIST[SOURCE_FILE]()
3090
one.add(source_file)
3091
3092
QUICK_FIX_SYNTHESIZER.attach_fixes(in_range, one)
3093
fi
3094
fi
3095
catch ex: Exception
3096
debug_always("CODE_ACTIONS caught: {ex.get_type()} {ex.message}")
3097
3098
_watchdog.request_restart()
3099
yrt
3100
3101
Std.error.flush()
3102
3103
JSON_PROTOCOL.write_response(writer, Protocol.Response.CODE_ACTIONS(in_range))
3104
si
3105
3106
// Both ranges are 1-based with exclusive end columns, so they are
3107
// disjoint only when one ends strictly before the other starts.
3108
// Touching counts as overlapping: a cursor sitting at either end
3109
// of a squiggle is the position the user asks for a fix from, and
3110
// a bare cursor is a zero-width range that would otherwise miss
3111
// every diagnostic it is inside.
3112
_overlaps(request: Protocol.Request.CODE_ACTIONS, d: Protocol.DIAGNOSTIC) -> bool static =>
3113
!(
3114
_is_before(d.end_line, d.end_column, request.start_line, request.start_column) \/
3115
_is_before(request.end_line, request.end_column, d.start_line, d.start_column)
3116
)
3117
3118
_is_before(line: int, column: int, other_line: int, other_column: int) -> bool static =>
3119
line < other_line \/ (line == other_line /\ column < other_column)
3120
si
3121
3122
// Handles the heap_check request: an explicit request to sample the heap.
3123
// The VS Code extension sends one during a lull in editing, so the
3124
// watchdog's forced GC lands outside the latency path of an interactive
3125
// request rather than after every compile.
3126
class HEAP_CHECK_HANDLER(_watchdog: WATCHDOG): RequestHandler[Protocol.Request.HEAP_CHECK] is
3127
super()
3128
3129
handle_request(request: Protocol.Request.HEAP_CHECK, writer: IO.TextWriter) is
3130
// If the heap has grown past the recycle thresholds, check_heap
3131
// writes a RESTART response and exits; otherwise we ack so the
3132
// client's request/response loop completes.
3133
_watchdog.check_heap_on_request(writer)
3134
3135
JSON_PROTOCOL.write_response(writer, Protocol.Response.HEAP_CHECK())
3136
si
3137
si
3138
3139
// Adds assemblies to the reference set: the earlier cells of an
3140
// interactive session, which the analyser cannot see until they have
3141
// been compiled. Any file can resolve differently against the larger
3142
// set, so the retained tables no longer describe the project and the
3143
// next build is a whole-project rebuild rather than an incremental
3144
// one.
3145
class ADD_REFERENCES_HANDLER(_compiler: COMPILER): RequestHandler[Protocol.Request.ADD_REFERENCES] is
3146
super()
3147
3148
handle_request(request: Protocol.Request.ADD_REFERENCES, writer: IO.TextWriter) is
3149
let message: string? mut = null
3150
3151
try
3152
Semantic.DotNet.ADDITIONAL_REFERENCES.add(IoC.CONTAINER.instance, request.paths)
3153
catch ex: Exception
3154
message = ex.message
3155
yrt
3156
3157
_compiler.are_tables_current = false
3158
_compiler.is_full_compile_needed = true
3159
3160
JSON_PROTOCOL.write_response(writer, Protocol.Response.ADD_REFERENCES(message))
3161
si
3162
si
3163
3164
// Answers a STATS request with a snapshot of the analyser's timers,
3165
// read from the shared TIMERS instance the EDIT and COMPILE handlers
3166
// accumulate into. The edit-path tallies let a test assert which path
3167
// handled an edit - the interface-incremental counter rising by one,
3168
// and the full-rebuild counter not, is the signal that an EDIT was
3169
// served incrementally rather than by falling back.
3170
class STATS_HANDLER(_timers: TIMERS): RequestHandler[Protocol.Request.STATS] is
3171
super()
3172
3173
handle_request(request: Protocol.Request.STATS, writer: IO.TextWriter) is
3174
let entries = Collections.LIST[Protocol.STAT_ENTRY]()
3175
3176
for timer in _timers.all do
3177
entries.add(
3178
Protocol.STAT_ENTRY(
3179
timer.name,
3180
timer.execute_count,
3181
timer.moving_average_milliseconds
3182
)
3183
)
3184
od
3185
3186
JSON_PROTOCOL.write_response(writer, Protocol.Response.STATS(entries))
3187
si
3188
si
3189
3190
// Reformats a single file. The buffer is parsed fresh — no symbol table, no
3191
// dependence on prior EDIT state — and the reformatted text returned whole.
3192
// On any failure the original buffer is echoed back unchanged, so a format
3193
// request can never corrupt it.
3194
class FORMAT_HANDLER(
3195
_compiler: COMPILER,
3196
_build_flags: GLOBAL_BUILD_FLAGS
3197
): RequestHandler[Protocol.Request.FORMAT] is
3198
super()
3199
3200
handle_request(request: Protocol.Request.FORMAT, writer: IO.TextWriter) is
3201
let path = request.path
3202
let source = request.source
3203
3204
let result mut = source
3205
3206
try
3207
// Parse into a throwaway logger so a format request never
3208
// mutates the analyser's shared diagnostics store or its
3209
// speculation state stack.
3210
let format_logger = Logging.DIAGNOSTICS_STORE()
3211
3212
let source_file =
3213
_compiler.parse(
3214
path,
3215
IO.StringReader(source),
3216
_build_flags.want_compile_up_to_expressions,
3217
_build_flags.want_compile_expressions,
3218
false,
3219
format_logger
3220
)
3221
3222
if !format_logger.any_errors then
3223
let formatter =
3224
Syntax.Process.Printer.FORMATTER(source_file.trivia, 100)
3225
3226
result = formatter.format(source_file.definition)
3227
fi
3228
catch ex: Exception
3229
debug_always("FORMAT caught: {ex.get_type()} {ex.message}")
3230
yrt
3231
3232
JSON_PROTOCOL.write_response(writer, Protocol.Response.FORMAT(result))
3233
si
3234
si
3235
3236
// Reformats just the run of definitions/statements covering a requested
3237
// range. The response carries the whole-line span actually replaced and the
3238
// reformatted text; a zero span (all zeros) means nothing was formatted (the
3239
// buffer is left untouched).
3240
class FORMATRANGE_HANDLER(
3241
_compiler: COMPILER,
3242
_build_flags: GLOBAL_BUILD_FLAGS
3243
): RequestHandler[Protocol.Request.FORMAT_RANGE] is
3244
super()
3245
3246
handle_request(request: Protocol.Request.FORMAT_RANGE, writer: IO.TextWriter) is
3247
let path = request.path
3248
let start_line = request.start_line
3249
let start_column = request.start_column
3250
let end_line = request.end_line
3251
let end_column = request.end_column
3252
let source = request.source
3253
3254
let text: string mut = ""
3255
let response_start_line: int mut = 0
3256
let response_start_column: int mut = 0
3257
let response_end_line: int mut = 0
3258
let response_end_column: int mut = 0
3259
3260
try
3261
// Parse into a throwaway logger so a format request never
3262
// mutates the analyser's shared diagnostics store or its
3263
// speculation state stack.
3264
let format_logger = Logging.DIAGNOSTICS_STORE()
3265
3266
let source_file =
3267
_compiler.parse(
3268
path, IO.StringReader(source), _build_flags.want_compile_up_to_expressions, _build_flags.want_compile_expressions, false, format_logger
3269
)
3270
3271
if !format_logger.any_errors then
3272
let root = cast Syntax.Trees.Definitions.LIST?(source_file.definition)
3273
3274
if root? then
3275
let locator =
3276
Syntax.Process.Printer.RANGE_LOCATOR(
3277
start_line, start_column, end_line, end_column
3278
)
3279
3280
let target = locator.locate(root)
3281
3282
if target? then
3283
// the run's own leading indentation is not part of
3284
// the replaced span — continuation lines must be
3285
// re-indented back to it
3286
let continuation_indent = target.start_column - 1
3287
3288
let formatter =
3289
Syntax.Process.Printer.FORMATTER(
3290
_trivia_in_lines(
3291
source_file.trivia,
3292
target.start_line,
3293
target.end_line
3294
),
3295
100 - continuation_indent
3296
)
3297
3298
text = _reindent(formatter.format(target.node), continuation_indent)
3299
3300
response_start_line = target.start_line
3301
response_start_column = target.start_column
3302
response_end_line = target.end_line
3303
// exclusive end of the span to replace, one past
3304
// the run's last character (and any ';' the
3305
// statement node's location does not cover)
3306
response_end_column = _span_end_column(source, target) + 1
3307
fi
3308
fi
3309
fi
3310
catch ex: Exception
3311
debug_always("FORMATRANGE caught: {ex.get_type()} {ex.message}")
3312
yrt
3313
3314
JSON_PROTOCOL.write_response(
3315
writer,
3316
Protocol.Response.FORMAT_RANGE(response_start_line, response_start_column, response_end_line, response_end_column, text)
3317
)
3318
si
3319
3320
_trivia_in_lines(
3321
trivia: Collections.Iterable[Lexical.TRIVIA]?,
3322
start_line: int,
3323
end_line: int
3324
) -> Collections.Iterable[Lexical.TRIVIA] is
3325
let result = Collections.LIST[Lexical.TRIVIA]()
3326
3327
if trivia? then
3328
for t in trivia do
3329
let line = t.location.start_line
3330
if line >= start_line /\ line <= end_line then
3331
result.add(t)
3332
fi
3333
od
3334
fi
3335
3336
return result
3337
si
3338
3339
_char_index(source: string, line: int, column: int) -> int is
3340
let i mut = 0
3341
let current_line mut = 1
3342
while current_line < line /\ i < source.length do
3343
if source[i] == '\n' then
3344
current_line = current_line + 1
3345
fi
3346
i = i + 1
3347
od
3348
return i + column - 1
3349
si
3350
3351
// The column of the run's last character to replace. A statement's
3352
// trailing ';' is not part of its node location, so if one follows the
3353
// run it is swallowed into the span — otherwise the formatted text
3354
// (which re-emits the ';') would leave the original stranded.
3355
_span_end_column(source: string, target: Syntax.Process.Printer.RANGE_TARGET) -> int is
3356
let end_index = _char_index(source, target.end_line, target.end_column)
3357
3358
let scan mut = end_index + 1
3359
while
3360
scan < source.length /\
3361
(source[scan] == ' ' \/ source[scan] == '\t')
3362
do
3363
scan = scan + 1
3364
od
3365
3366
if scan < source.length /\ source[scan] == ';' then
3367
return target.end_column + (scan - end_index)
3368
fi
3369
3370
return target.end_column
3371
si
3372
3373
// Re-indent the formatted run for splicing back at its original
3374
// position: the first line goes in after the run's existing leading
3375
// indentation (which is not part of the replaced span), so it stays
3376
// bare; every later line is indented to that same depth. The trailing
3377
// newline the formatter appends is dropped.
3378
_reindent(body: string, continuation_indent: int) -> string is
3379
let prefix = System.Text.StringBuilder()
3380
let p mut = 0
3381
while p < continuation_indent do
3382
prefix.append(' ')
3383
p = p + 1
3384
od
3385
let prefix_string = prefix.to_string()
3386
3387
let result = System.Text.StringBuilder()
3388
let lines = Collections.LIST[string](body.split(['\n']))
3389
let index mut = 0
3390
3391
while index < lines.count do
3392
let line = lines[index]
3393
3394
if index == lines.count - 1 /\ line.length == 0 then
3395
break
3396
fi
3397
3398
if index > 0 then
3399
result.append('\n')
3400
if line.length > 0 then
3401
result.append(prefix_string)
3402
fi
3403
fi
3404
3405
result.append(line)
3406
index = index + 1
3407
od
3408
3409
return result.to_string()
3410
si
3411
si
3412
3413
// Handles describe_type: resolve a type by qualified name (dotted) and
3414
// return its kind, ancestors and members. The resolver walks name
3415
// components through find_direct starting at the symbol table's global
3416
// scope, which for imported .NET types transparently triggers dotnet
3417
// symbol loading. Trailing generic brackets are stripped so
3418
// `Collections.LIST[int]` resolves to `LIST`; generic argument
3419
// substitution isn't applied to member signatures yet.
3420
class DESCRIBE_TYPE_HANDLER(
3421
_watchdog: WATCHDOG,
3422
_symbol_table: Semantic.SYMBOL_TABLE,
3423
_full_compiler: FULL_COMPILER
3424
): RequestHandler[Protocol.Request.DESCRIBE_TYPE] is
3425
super()
3426
3427
handle_request(request: Protocol.Request.DESCRIBE_TYPE, writer: IO.TextWriter) is
3428
let resolved_name mut = ""
3429
let type_kind mut = ""
3430
let ancestors = Collections.LIST[string]()
3431
let members = Collections.LIST[Protocol.MEMBER_DTO]()
3432
3433
try
3434
if !_watchdog.want_restart then
3435
let symbol mut = resolve_type_expression(request.type_expression)
3436
3437
// A miss can mean either "unknown name" or "symbol table
3438
// not yet primed" - the initial analyser state has neither
3439
// project source EDIT'd nor .NET types loaded on demand.
3440
// Force a compile on the first miss and retry.
3441
if !symbol? then
3442
_full_compiler.compile_all(writer)
3443
symbol = resolve_type_expression(request.type_expression)
3444
fi
3445
3446
if symbol? then
3447
resolved_name = symbol.qualified_name
3448
type_kind = kind_name(symbol)
3449
3450
if let classy = cast Semantic.Symbols.Classy?(symbol) then
3451
collect_ancestors(classy, ancestors)
3452
collect_members(classy, members)
3453
else
3454
if symbol.is_namespace then
3455
collect_namespace_members(symbol, members)
3456
fi
3457
fi
3458
fi
3459
fi
3460
catch ex: Exception
3461
debug_always("DESCRIBETYPE caught: {ex.get_type()} {ex.message}")
3462
_watchdog.request_restart()
3463
yrt
3464
3465
Std.error.flush()
3466
3467
JSON_PROTOCOL.write_response(
3468
writer,
3469
Protocol.Response.DESCRIBE_TYPE(resolved_name, type_kind, ancestors, members)
3470
)
3471
si
3472
3473
// Resolve a dotted type expression. Try the .NET symbol table
3474
// directly first - `get_symbol("System.Text.StringBuilder")` loads
3475
// reflected types on demand. If that misses, walk the dotted
3476
// components through the symbol table's global scope, which finds
3477
// ghūl-declared namespaces and types. Any trailing generic-argument
3478
// brackets are dropped for the walk; the resolved symbol is the
3479
// type template itself.
3480
resolve_type_expression(expression: string?) -> Semantic.Symbols.Symbol? is
3481
if !expression? \/ expression.length == 0 then
3482
return null
3483
fi
3484
3485
let stripped = strip_generic_arguments(expression)
3486
3487
if stripped.length == 0 then
3488
return null
3489
fi
3490
3491
let dotnet_symbol_table = IoC.CONTAINER.instance.dotnet_symbol_table.value
3492
let dotnet_symbol = dotnet_symbol_table.get_symbol(stripped)
3493
3494
if dotnet_symbol? then
3495
return cast Semantic.Symbols.Symbol(dotnet_symbol)
3496
fi
3497
3498
let components = stripped.split(['.'])
3499
3500
if components.count == 0 then
3501
return null
3502
fi
3503
3504
let symbol: Semantic.Symbols.Symbol? mut = _symbol_table.global_scope.find_direct(components[0])
3505
3506
if !symbol? then
3507
return null
3508
fi
3509
3510
let index mut = 1
3511
3512
while index < components.count do
3513
symbol = symbol!.find_direct(components[index])
3514
3515
if !symbol? then
3516
return null
3517
fi
3518
3519
index = index + 1
3520
od
3521
3522
return symbol
3523
si
3524
3525
// Strip a top-level `[...]` suffix. `LIST[int]` -> `LIST`,
3526
// `Ghul.MAP[string, int]` -> `Ghul.MAP`. Anything before an inner
3527
// `[` inside a component (rare) is preserved via the substring.
3528
strip_generic_arguments(expression: string) -> string is
3529
let bracket = expression.index_of('[')
3530
3531
if bracket >= 0 then
3532
return expression.substring(0, bracket)
3533
fi
3534
3535
return expression
3536
si
3537
3538
kind_name(symbol: Semantic.Symbols.Symbol) -> string static is
3539
if symbol.is_namespace then return "namespace"; fi
3540
if symbol.is_variant then return "variant"; fi
3541
if symbol.is_union then return "union"; fi
3542
if symbol.is_trait then return "trait"; fi
3543
// ENUM_STRUCT extends STRUCT, so it must be tested first or an
3544
// enum falls through to the "struct" branch.
3545
if isa Semantic.Symbols.ENUM_STRUCT(symbol) then return "enum"; fi
3546
if isa Semantic.Symbols.STRUCT(symbol) then return "struct"; fi
3547
if symbol.is_class then return "class"; fi
3548
3549
return ""
3550
si
3551
3552
collect_ancestors(classy: Semantic.Symbols.Classy, ancestors: Collections.LIST[string]) is
3553
for ancestor_type in classy.ancestors do
3554
let name = ancestor_type.symbol.qualified_name
3555
3556
if !ancestors.contains(name) then
3557
ancestors.add(name)
3558
fi
3559
od
3560
si
3561
3562
// Members come from two sources: those declared directly on the
3563
// derived type, and those declared on an ancestor. The resolve-
3564
// overrides pass pulls inherited members into the derived's own
3565
// symbol store (wrapping methods in a FUNCTION_GROUP owned by the
3566
// derived), so `owner` can't distinguish inheritance reliably.
3567
// Instead: walk the derived's direct list first and mark those
3568
// direct; then walk each ancestor and mark any not-yet-seen
3569
// member inherited. A member reported as direct on both sides
3570
// (e.g. an interface method reflection copied into the class)
3571
// stays tagged direct.
3572
collect_members(classy: Semantic.Symbols.Classy, members: Collections.LIST[Protocol.MEMBER_DTO]) is
3573
let seen = Collections.SET[string]()
3574
3575
add_matches(classy, "", false, members, seen)
3576
3577
for ancestor_type in classy.ancestors do
3578
let ancestor = ancestor_type.symbol
3579
3580
if isa Semantic.Symbols.Classy(ancestor) then
3581
add_matches(ancestor, ancestor.qualified_name, true, members, seen)
3582
fi
3583
od
3584
si
3585
3586
// For a plain namespace, no ancestor semantics apply.
3587
collect_namespace_members(
3588
scope: Semantic.Symbols.Symbol,
3589
members: Collections.LIST[Protocol.MEMBER_DTO]
3590
) is
3591
add_matches(scope, "", false, members, Collections.SET[string]())
3592
si
3593
3594
add_matches(
3595
scope: Semantic.Symbols.Symbol,
3596
inherited_from: string,
3597
is_inherited: bool,
3598
members: Collections.LIST[Protocol.MEMBER_DTO],
3599
seen: Collections.SET[string]
3600
) is
3601
let matches = Collections.MAP[string, Semantic.Symbols.Symbol]()
3602
3603
scope.find_direct_matches("", matches)
3604
3605
for pair in matches do
3606
let name = pair.key
3607
let symbol = pair.value
3608
3609
if symbol.is_internal \/ seen.contains(name) then
3610
continue
3611
fi
3612
3613
seen.add(name)
3614
3615
members.add(
3616
Protocol.MEMBER_DTO(
3617
name,
3618
symbol.signature,
3619
symbol.kind_label,
3620
cast int(symbol.completion_kind),
3621
is_inherited,
3622
inherited_from
3623
)
3624
)
3625
od
3626
si
3627
si
3628
3629
// A missing built-in type stops the analyser answering anything, and
3630
// restarting it would fail the same way. Each file the analyser knows
3631
// carries the cause, so a client shows why rather than a clean sheet.
3632
class MISSING_BUILT_IN_REPORT is
3633
record(
3634
missing: Logging.MissingBuiltInException,
3635
paths: Iterable[string],
3636
diagnostics: Collections.LIST[Protocol.DIAGNOSTIC],
3637
checked_paths: Collections.LIST[string]
3638
) static is
3639
let logger = IoC.CONTAINER.instance.logger
3640
3641
for path in paths do
3642
logger.exception(Source.LOCATION(path, 1, 1, 1, 1), missing, missing.message)
3643
od
3644
3645
diagnostics.clear()
3646
checked_paths.clear()
3647
3648
DIAGNOSTICS_COLLECTOR.collect_into(logger, diagnostics, checked_paths)
3649
si
3650
si
3651
si