Skip to content
← Back

src/compiler/compiler.ghul

1
namespace Compiler is
2
use System.Exception
3
4
use IO.Std
5
6
use Collections.Iterable
7
use Collections.MutableList
8
use Collections.LIST
9
10
use IoC
11
use Logging
12
13
class COMPILER is
14
container: CONTAINER
15
logger: Logger
16
17
source_files: LIST[SOURCE_FILE]
18
19
post_parse_passes: MutableList[Pass]
20
21
build_passes: MutableList[Pass]
22
23
_compile_expressions_pass: Pass
24
_verify_compile_state_had_errors: bool
25
26
is_full_compile_needed: bool public
27
28
// True when the declaration-level state - symbols, ancestries,
29
// override links, store-free bits - reflects every registered
30
// file's current source: set by a completed build, cleared the
31
// moment clear_symbols abandons the tables. Distinct from
32
// is_full_compile_needed, which tracks whether expression-level
33
// DIAGNOSTICS are stale for some files; right after an
34
// interface-affecting EDIT's rebuild the tables are current while
35
// a full compile is still needed.
36
are_tables_current: bool public
37
38
// Called at each per-file boundary of the compile-expressions
39
// walk, if anything is interested. Analysis mode uses it to
40
// answer queries that arrived while a compile was running; a
41
// batch build leaves it unset.
42
//
43
// The boundary is where a file has just been walked and both the
44
// scope stack and the namespace stack are back at their base -
45
// the only point during a compile at which the retained state is
46
// as consistent as it is between builds.
47
on_file_boundary: (() -> void)? public
48
49
generated_source_files: MutableList[string]
50
51
_effects_stopwatch: System.Diagnostics.Stopwatch?
52
53
// See the build() overload below.
54
_want_entry_point_selection: bool
55
56
// Files with a body walked since the effect relations were last
57
// solved: what the next solve has to re-derive before it can
58
// answer for them.
59
_effects_stale_files: Collections.SET[string]
60
61
init() is
62
container = IoC.CONTAINER.instance
63
logger = container.logger
64
_effects_stale_files = Collections.SET[string]()
65
66
// Nothing has been compiled yet, so expression-level state is
67
// certainly absent: a compile request against this fresh state
68
// has to run the full build, not the incremental effects pass,
69
// which assumes the symbol tables exist (in particular the
70
// namespaces only `build()` creates).
71
is_full_compile_needed = true
72
73
source_files = LIST[SOURCE_FILE]()
74
generated_source_files = LIST[string]()
75
76
post_parse_passes = LIST[Pass]()
77
add_pass(post_parse_passes, "conditional-compilation", (source_file) -> bool => conditional_compilation_pass(source_file))
78
add_pass(post_parse_passes, "collect-modifier-keyword-locations", (source_file) -> bool => collect_modifier_keyword_locations_pass(source_file))
79
add_pass(post_parse_passes, "rewrite-syntax-trees", (source_file) -> bool => rewrite_syntax_tree_pass(source_file))
80
add_pass(post_parse_passes, "collect-suppress-pragmas", (source_file) -> bool => collect_suppress_pragmas_pass(source_file))
81
add_pass(post_parse_passes, "attach-doc-comments", (source_file) -> bool => attach_doc_comments_pass(source_file))
82
83
build_passes = LIST[Pass]()
84
// Declares the type-level skeleton only: namespaces, types,
85
// variants and type parameters - everything a type expression
86
// can name. Members are declared by declare-members below.
87
add_pass(build_passes, "declare-symbols", (source_file) -> bool => declare_symbols_pass(source_file))
88
// First round: binds imports of namespaces and types. Imports
89
// of members bind in resolve-member-uses, once they exist.
90
add_pass(build_passes, "resolve-uses", (source_file) -> bool => resolve_uses_pass(source_file))
91
// Runs after resolve-uses so a member - in particular an
92
// impl/partial block's target name - can be reached through a
93
// use import and declared anywhere relative to its uses.
94
add_pass(build_passes, "declare-members", (source_file) -> bool => declare_members_pass(source_file))
95
// Second round: binds imports of members - static methods,
96
// global functions, enum members - and reports anything still
97
// unresolved.
98
add_pass(build_passes, "resolve-member-uses", (source_file) -> bool => resolve_member_uses_pass(source_file))
99
add_pass(build_passes, "resolve-type-expressions", (source_file: SOURCE_FILE) -> bool => resolve_type_expressions_pass(source_file))
100
add_pass(build_passes, "resolve-ancestors", (source_file) -> bool => resolve_ancestors_pass(source_file))
101
// Runs after resolve-ancestors so it can tell an exception
102
// class from its ancestors, and so after declare-members, so
103
// it can read each class's computed `is_abstract` (explicit +
104
// implicit) rather than just the syntactic modifier.
105
// Walks every file to report warnings but leaves nothing a
106
// later pass reads, so it reports its walk without advancing
107
// the compiled_through marker.
108
add_pass(build_passes, "check-name-conventions", (source_file) -> bool => check_name_conventions_pass(source_file))
109
.advances_milestone = false
110
add_pass(build_passes, "synthesize-class-equality", (source_file) -> bool => synthesize_class_equality_pass(source_file))
111
add_pass(build_passes, "synthesize-iterator-reset", (source_file) -> bool => synthesize_iterator_reset_pass(source_file))
112
add_pass(build_passes, "resolve-explicit-types", (source_file) -> bool => resolve_explicit_types_pass(source_file))
113
add_pass(build_passes, "check-type-argument-bounds", (source_file) -> bool => check_type_argument_bounds_pass(source_file))
114
add_pass(
115
build_passes,
116
"resolve-overrides",
117
() -> void is si,
118
(source_file) -> bool => resolve_overrides_pass(source_file),
119
() -> void is
120
container.resolve_overrides.check_duplicate_global_functions()
121
si
122
)
123
// Editor-only: announces each declaration's dispatch state
124
// once every file's overrides have been resolved. Reports
125
// nothing a later pass reads, so it does not advance the
126
// milestone.
127
add_pass(build_passes, "definition-virtuality", (source_file) -> bool => definition_virtuality_pass(source_file))
128
.advances_milestone = false
129
add_pass(build_passes, "register-source-intrinsics", (source_file) -> bool => register_source_intrinsics_pass(source_file))
130
add_pass(build_passes, "record-type-argument-uses", (source_file) -> bool => record_type_argument_uses_pass(source_file))
131
132
add_pass(build_passes, "mark-boxed-locals", (source_file) -> bool => mark_boxed_locals_pass(source_file))
133
134
_compile_expressions_pass = add_pass(
135
build_passes,
136
"compile-expressions",
137
() -> void is si,
138
(source_file) -> bool => compile_expressions_pass(source_file),
139
() -> void is si
140
)
141
142
add_pass(build_passes, "check-deprecated-uses", (source_file) -> bool => check_deprecated_uses_pass(source_file))
143
144
// Re-walks every body with the types compile-expressions
145
// resolved, then solves the member-granular effect
146
// relations the crossing discharge reads. Reports nothing a
147
// later pass consumes through the milestone, so it does not
148
// advance it.
149
add_pass(
150
build_passes,
151
"infer-effects",
152
() -> void is
153
_effects_stopwatch = System.Diagnostics.Stopwatch.start_new()
154
155
Syntax.Process.EFFECT_FACTS.collecting = true
156
Syntax.Process.EFFECT_FACTS.begin_generation("infer-effects")
157
158
container.infer_store_free.set_resolved_mode(true)
159
container.infer_store_free.start_run()
160
si,
161
(source_file) -> bool => infer_effects_pass(source_file),
162
() -> void is
163
let stopwatch = _effects_stopwatch
164
let walk_ms = if stopwatch? then stopwatch.elapsed_milliseconds else 0L fi
165
166
let complete = _all_compiled_through_expressions(source_files)
167
168
container.infer_store_free.solve_effects(complete)
169
container.infer_store_free.set_resolved_mode(false)
170
171
Syntax.Process.EFFECT_FACTS.collecting = false
172
173
let solve_ms = if stopwatch? then stopwatch.elapsed_milliseconds else 0L fi
174
175
// Re-walk the files whose walk disagrees with the
176
// relations until a solve finds none, then judge
177
// and check against the final relations.
178
run_combined_solve_rounds(source_files)
179
180
let rounds_ms = if stopwatch? then stopwatch.elapsed_milliseconds else 0L fi
181
182
run_post_effects_purity_checks(source_files)
183
184
Syntax.Process.RETRY_STATS.report()
185
Syntax.Process.RETRY_SITE_STATS.report()
186
Syntax.Process.PIPE_FUSION_STUDY.report()
187
188
_dump_kills()
189
190
if Driver.COMPILER_ENVIRONMENT.effects_timing /\ stopwatch? then
191
IO.Std.error.write_line("effects round: {stopwatch.elapsed_milliseconds} ms (walk {walk_ms} solve {solve_ms - walk_ms} rounds {rounds_ms - solve_ms} judge {stopwatch.elapsed_milliseconds - rounds_ms})")
192
fi
193
si
194
).advances_milestone = false
195
196
// After the effect rounds have re-walked whatever they were
197
// going to, so the state checked is the state generate-il
198
// will read. Whether the build had errors is read once, before
199
// any file is checked: a violation is itself an error, and
200
// reading the live count would stop the check at the first
201
// file that reports one.
202
add_pass(
203
build_passes,
204
"verify-compile-state",
205
() -> void is _verify_compile_state_had_errors = logger.any_errors si,
206
(source_file) -> bool => verify_compile_state_pass(source_file),
207
() -> void is si
208
).advances_milestone = false
209
210
// Before generate-il, so the choice - and the diagnostics it
211
// produces - are made by a pass that also runs in analysis
212
// mode, and after compile-expressions, so a candidate's
213
// inferred return type is settled when its signature is
214
// checked.
215
add_pass(
216
build_passes,
217
"select-entry-point",
218
() -> void is
219
if _want_entry_point_selection then
220
container.select_entry_point.start()
221
fi
222
si,
223
(source_file) -> bool => select_entry_point_pass(source_file),
224
() -> void is
225
if _want_entry_point_selection then
226
container.select_entry_point.finish()
227
fi
228
si
229
).advances_milestone = false
230
231
add_pass(
232
build_passes,
233
"generate-il",
234
() -> void is
235
if !is_emitting_assembly then
236
return
237
fi
238
239
// Reported once for the whole build, after the front
240
// end has reported whatever it found in the sources.
241
if !container.target_profile.has_code_generation then
242
container.logger.error(
243
Source.LOCATION.internal,
244
"code generation for the {container.target_profile.capabilities.target_name} target is not supported")
245
246
return
247
fi
248
249
container.backend.begin_assembly()
250
si,
251
(source_file) -> bool => generate_il_pass(source_file),
252
() -> void is
253
if !is_generating_code then
254
return
255
fi
256
257
if !container.logger.any_errors then
258
container.backend.end_assembly()
259
fi
260
261
// An executable build that named no entry point would
262
// otherwise be written out and fail only when something
263
// tried to run it. Surface it as a located compiler
264
// error instead; `finish_build` aborts on `any_errors`
265
// before the image is written. Library builds
266
// (`--library`) legitimately have no entry point. Gated
267
// on no prior errors so it doesn't pile onto a build
268
// already failing for another reason (an error-aborted
269
// build never reaches `.entrypoint` emission, so
270
// `seen_entrypoint` is false regardless of whether the
271
// source declares an `entry`).
272
let flags = container.build_flags
273
274
if flags.want_executable /\
275
!flags.want_library /\
276
!container.entry_point.seen /\
277
!container.logger.any_errors
278
then
279
let message =
280
if container.select_entry_point.named_entry_point_not_found then
281
"no function matches the --entry name {container.entry_point.name}"
282
elif container.select_entry_point.left_top_level_statements_unrun then
283
"no entry point selected; name one with --entry, or leave one file carrying top-level statements"
284
else
285
"no entry point declared; add an 'entry()' function or build with --library"
286
fi
287
288
container.logger.error(Source.LOCATION.internal, message)
289
fi
290
si
291
)
292
293
// Pass.order is set in registration order so a SOURCE_FILE's
294
// compiled_through.order can be compared numerically against the
295
// order of a target pass (e.g. _compile_expressions_pass). Post-
296
// parse passes get their own counter starting at zero and don't
297
// participate in compiled_through queries.
298
assign_pass_orders(post_parse_passes)
299
assign_pass_orders(build_passes)
300
si
301
302
assign_pass_orders(passes: Collections.Iterable[Pass]) static is
303
let order mut = 0
304
for p in passes do
305
p.order = order
306
order = order + 1
307
od
308
si
309
310
dump_counts() is
311
container.namespaces.dump_counts()
312
container.symbol_use_locations.dump_counts()
313
container.symbol_definition_locations.dump_counts()
314
si
315
316
clear_symbols() is
317
clear_symbols(Collections.LIST[string]())
318
si
319
320
// Abandon the symbol tables ahead of a rebuild. Every source
321
// symbol is re-created; those of a file that was not edited are
322
// re-declared from its retained tree in the same order as
323
// before, and adopt the ids they had, so everything keyed by
324
// symbol id - the solved effect relations above all - carries
325
// over. Symbols of an edited file get fresh ids.
326
clear_symbols(edited_files: Collections.Iterable[string]) is
327
are_tables_current = false
328
329
// The retained effect facts describe the symbols being
330
// abandoned, and what the solve reads off a symbol - its
331
// owner's openness, its overriders, its purity - can change
332
// with the rebuild; the next solve starts from every body.
333
container.infer_store_free.start_run()
334
_effects_stale_files.clear()
335
336
container.symbol_table.clear()
337
container.namespaces.clear()
338
339
// The ambient `use Ghul.Intrinsics` resolves against a
340
// namespace that must exist again before the rebuild.
341
Semantic.INTRINSICS_NAMESPACE.materialise(container.symbol_table, container.namespaces)
342
container.innate_symbol_lookup.redeclare_built_ins()
343
container.symbol_use_locations.clear()
344
container.symbol_definition_locations.clear_and_replay_ids(edited_files)
345
Semantic.Symbols.STORE_FREE_BITS.clear()
346
347
// The emitter's rows and reference caches name symbols that
348
// have just been abandoned, and analysis rebuilds on every
349
// edit: kept, they accumulate for the session's lifetime.
350
container.backend.reset()
351
// container.logger.clear();
352
353
// Every previously-queued file's expression-level state now
354
// references symbols that have just been abandoned. Drop the
355
// marker so query handlers can tell.
356
for f in source_files do
357
f.compiled_through = null
358
od
359
si
360
361
is_compiled_through_expressions(source_file: SOURCE_FILE) -> bool =>
362
let compiled_through = source_file?.compiled_through in
363
compiled_through? /\
364
compiled_through.order >= _compile_expressions_pass.order
365
366
clear_queue() is
367
source_files.clear()
368
si
369
370
add_pass(passes: Collections.MutableList[Pass], description: string, apply: SOURCE_FILE -> bool) -> Pass is
371
let pass = PASS(container.timers, description, null, apply, null)
372
passes.add(pass)
373
return pass
374
si
375
376
add_pass(
377
passes: Collections.MutableList[Pass],
378
description: string,
379
start: () -> void,
380
apply: SOURCE_FILE -> bool,
381
finish: () -> void
382
) -> Pass
383
is
384
let pass = PASS(container.timers, description, start, apply, finish)
385
passes.add(pass)
386
return pass
387
si
388
389
add_pass(passes: Collections.MutableList[Pass], pass: Pass) is
390
passes.add(pass)
391
si
392
393
parse_and_queue(path: string, reader: IO.TextReader, want_compile_up_to_expressions: bool, want_compile_expressions: bool, is_internal_file: bool) is
394
queue(
395
parse(path, reader, want_compile_up_to_expressions, want_compile_expressions, is_internal_file)
396
)
397
si
398
399
queue(source_file: SOURCE_FILE) is
400
source_files.add(source_file)
401
si
402
403
queue(source_files: Collections.Iterable[SOURCE_FILE]) is
404
self.source_files.add_range(source_files)
405
si
406
407
parse(path: string, reader: IO.TextReader, want_compile_up_to_expressions: bool, want_compile_expressions: bool, is_internal_file: bool) -> SOURCE_FILE =>
408
parse(path, reader, want_compile_up_to_expressions, want_compile_expressions, is_internal_file, logger)
409
410
// Parsing into an explicitly-supplied logger lets a caller (the
411
// analysis-mode format handlers) parse without touching the shared
412
// diagnostics store: a throwaway logger keeps parse diagnostics and
413
// the speculation state stack isolated from the live analyser state.
414
parse(
415
path: string,
416
reader: IO.TextReader,
417
want_compile_up_to_expressions: bool,
418
want_compile_expressions: bool,
419
is_internal_file: bool,
420
logger: Logger
421
) -> SOURCE_FILE is
422
let tokenizer = Lexical.TOKENIZER(
423
logger,
424
path,
425
reader,
426
is_internal_file
427
)
428
429
// Speculative-lookahead ring buffer; a power of 2 (the
430
// queue masks indices with `size - 1`), sized generously so
431
// deep-but-finite speculation does not overflow it. A true
432
// speculation loop is caught separately by the tokenizer's
433
// backtrack-loop detector.
434
let token_queue = Lexical.TOKEN_QUEUE(2048)
435
436
let token_lookahead = Lexical.TOKEN_LOOKAHEAD(
437
token_queue,
438
tokenizer
439
)
440
441
let context = Syntax.Parsers.CONTEXT(
442
token_lookahead,
443
logger
444
)
445
446
// The file root parses as a definition list at namespace depth 0,
447
// where a file with no namespace may carry bare top-level
448
// statements (collected into a synthesised entry point).
449
let definitions = container.definition_global_list_parser.parse(context)!
450
451
let result =
452
SOURCE_FILE(
453
want_compile_up_to_expressions,
454
want_compile_expressions,
455
path,
456
definitions
457
)
458
459
result.trivia = tokenizer.trivia
460
461
return result
462
si
463
464
// Parses one file and nothing more, into a logger of its own, and says
465
// whether it is complete, stops before its end, or is wrong. A front
466
// end reading a program a line at a time asks this to decide whether
467
// to run what it has or to wait for another line.
468
check_complete(path: string, reader: IO.TextReader) -> Syntax.Parsers.COMPLETENESS =>
469
check(path, reader).completeness
470
471
// As check_complete, and what the last thing at the file's root is,
472
// as Syntax.Parsers.LAST_STATEMENT_KIND names it.
473
check(path: string, reader: IO.TextReader) -> (completeness: Syntax.Parsers.COMPLETENESS, last_statement: string?) is
474
let completeness_logger = Logging.DIAGNOSTICS_STORE()
475
476
let tokenizer = Lexical.TOKENIZER(completeness_logger, path, reader, false)
477
478
let context =
479
Syntax.Parsers.CONTEXT(
480
Lexical.TOKEN_LOOKAHEAD(Lexical.TOKEN_QUEUE(2048), tokenizer),
481
completeness_logger
482
)
483
484
let root = container.definition_global_list_parser.parse(context)
485
486
let errors = LIST[Source.LOCATION]()
487
488
for d in completeness_logger.diagnostics_for(path) do
489
if d.severity == DiagnosticSeverity.ERROR then
490
errors.add(d.location)
491
fi
492
od
493
494
let end_of_input_errors = LIST[Source.LOCATION]()
495
496
end_of_input_errors.add_range(context.end_of_input_error_locations)
497
end_of_input_errors.add_range(tokenizer.end_of_input_error_locations)
498
499
let completeness = Syntax.Parsers.COMPLETENESS_CLASSIFIER().classify(errors, end_of_input_errors)
500
501
let last_statement =
502
if completeness == Syntax.Parsers.COMPLETENESS.COMPLETE /\ root? then
503
Syntax.Parsers.LAST_STATEMENT_KIND.of(root)
504
else
505
null
506
fi
507
508
return (completeness = completeness, last_statement = last_statement)
509
si
510
511
post_parse(source_files: Iterable[SOURCE_FILE]) is
512
build(post_parse_passes, source_files)
513
si
514
515
post_parse() is
516
build(post_parse_passes, source_files)
517
si
518
519
build() is
520
build(true)
521
si
522
523
// `want_entry_point_selection` false leaves out the pass that
524
// settles which function the assembly enters at. Its answer can
525
// only change when the project's declarations do, and nothing an
526
// editor shows between one debounced compile and the next depends
527
// on it, so an edit-driven rebuild skips a whole-project walk it
528
// would repeat on every keystroke.
529
build(want_entry_point_selection: bool) is
530
_want_entry_point_selection = want_entry_point_selection
531
532
@IF.dotnet()
533
container.ghul_namespace_creator.create_namespaces()
534
535
Syntax.Process.PURE_SLOTS.clear()
536
IR.Emitter.SRM_MEMBER_ORDER.clear()
537
Syntax.Process.KILL_LEDGER.clear_all()
538
539
_move_top_level_entry_file_first()
540
541
build(build_passes, source_files)
542
543
are_tables_current = true
544
si
545
546
// The file carrying the synthesised top-level entry compiles
547
// first: the entry's walk settles its top-level `let` variables'
548
// inferred types, and under --global-namespace a function in any
549
// other file can read them.
550
_move_top_level_entry_file_first() is
551
for i in 0..source_files.count do
552
if Syntax.Process.SYNTHESISE_TOP_LEVEL_ENTRY.has_top_level_entry(source_files[i].definition) then
553
if i > 0 then
554
let f = source_files[i]
555
556
source_files.remove_at(i)
557
source_files.insert(0, f)
558
fi
559
560
return
561
fi
562
od
563
si
564
565
build(passes: Collections.Iterable[Pass], source_files: Collections.Iterable[SOURCE_FILE]) is
566
let is_compiling_expressions mut = false
567
568
for pass in passes do
569
if pass == _compile_expressions_pass then
570
is_compiling_expressions = true
571
fi
572
573
logger.set_is_compiling_expressions(is_compiling_expressions)
574
575
pass.start()
576
577
for i in source_files do
578
let symbol_table = IoC.CONTAINER.instance.symbol_table
579
let symbol_table_mark = symbol_table.mark_scope_stack()
580
let diagnostics_mark = logger.mark()
581
let symbol_uses_mark = container.symbol_use_locations.mark()
582
583
let did_work mut = false
584
585
try
586
did_work = pass.apply(i)
587
catch e: Exception
588
logger.exception(diagnostics_mark, i.definition.location, e, "caught exception running pass {pass} on work item {i}")
589
finally
590
logger.release(diagnostics_mark)
591
container.symbol_use_locations.release(symbol_uses_mark)
592
symbol_table.release_scope_stack(symbol_table_mark)
593
yrt
594
595
if did_work then
596
WORK_COUNTERS.file_pass(container.timers, pass.description)
597
598
if pass.advances_milestone then
599
i.compiled_through = pass
600
fi
601
fi
602
603
IoC.CONTAINER.instance.namespaces.pop_all_namespaces()
604
605
if pass == _compile_expressions_pass then
606
_reached_file_boundary()
607
fi
608
od
609
610
// Every method has now been walked, which is what the
611
// constructor field check needs: a constructor is routinely
612
// written before the helper it delegates to, so the helper's
613
// summary does not exist until the pass is over.
614
if pass == _compile_expressions_pass then
615
container.compile_expressions.report_field_assignments()
616
fi
617
618
pass.finish()
619
od
620
si
621
622
// On-demand expression compile for one file against the retained
623
// symbol table, replaying only the pipeline's compile-expressions
624
// step with the same per-file bookkeeping as the build loop.
625
// Valid when the file has been built up to expressions in the
626
// current symbol generation - declare/resolve state current, only
627
// the expression-level walk missing. The analysis-mode query-miss
628
// recompile takes this instead of a whole-project rebuild when no
629
// interface change is pending.
630
compile_expressions_only(source_file: SOURCE_FILE) is
631
source_file.want_compile_expressions = true
632
_effects_stale_files.add(source_file.file_name)
633
634
// This walk is the only pass a file the client opened after
635
// the last full build ever gets: the build ran the
636
// declaration-level virtuality pass gated on the open-files
637
// set, so a file that was closed then recorded none of its
638
// carriers. Re-run it here, as the body re-walk does, so its
639
// inlays arrive with the expression-level ones.
640
container.definition_virtuality.apply(source_file)
641
642
// Same reasoning again: a closed file recorded none of the
643
// whole-project build's name-convention warnings either, and
644
// nothing else re-runs that walk for it.
645
container.check_name_conventions.apply(source_file)
646
647
// Same reasoning: this walk is the file's first and only
648
// expression-level walk, so the marking narrowing consults
649
// has to happen here rather than having been done by the
650
// pass the build skipped for a closed file.
651
container.mark_boxed_locals.apply(source_file.definition, _want_boxing)
652
653
logger.set_is_compiling_expressions(true)
654
655
let symbol_table = container.symbol_table
656
let symbol_table_mark = symbol_table.mark_scope_stack()
657
let diagnostics_mark = logger.mark()
658
let symbol_uses_mark = container.symbol_use_locations.mark()
659
660
let did_work mut = false
661
662
try
663
did_work = _compile_expressions_pass.apply(source_file)
664
catch e: Exception
665
logger.exception(diagnostics_mark, source_file.definition.location, e, "caught exception compiling expressions on demand for {source_file}")
666
finally
667
logger.release(diagnostics_mark)
668
container.symbol_use_locations.release(symbol_uses_mark)
669
symbol_table.release_scope_stack(symbol_table_mark)
670
671
container.namespaces.pop_all_namespaces()
672
673
logger.set_is_compiling_expressions(false)
674
yrt
675
676
if did_work then
677
WORK_COUNTERS.file_pass(container.timers, _compile_expressions_pass.description)
678
679
source_file.compiled_through = _compile_expressions_pass
680
fi
681
682
// This file is the whole of the walk, so its constructors are
683
// reported here rather than waiting for a build loop that may
684
// never run - without this an on-demand recompile reports the
685
// file differently from a whole-project build of the same
686
// source.
687
container.compile_expressions.report_field_assignments()
688
689
Syntax.Process.CHECK_DEPRECATED_USES(logger, container.symbol_use_locations)
690
.check(source_file)
691
692
_reached_file_boundary()
693
si
694
695
_reached_file_boundary() is
696
let boundary = on_file_boundary
697
698
if boundary? then
699
boundary()
700
fi
701
si
702
703
// Drop what compile-expressions left behind for one file and put
704
// it back in the set that pass has yet to walk. A CE-output-only
705
// clear (preserve_resolved_types) drops that output - including
706
// the node-keyed stores - while leaving TypeExpression.type
707
// intact, so the walk can run again without re-resolving (and
708
// thus double-setting) the retained interface types.
709
//
710
// The caller either re-walks the file now, through
711
// recompile_file_expressions, or leaves it for the next compile's
712
// expressions-only pass to pick up.
713
invalidate_file_expressions(source_file: SOURCE_FILE) is
714
let ce_clear = Syntax.Process.CLEAR_STATE_VISITOR(true)
715
ce_clear.apply(source_file.definition)
716
717
container.state_store_registry.drop_file(source_file.file_name)
718
719
source_file.compiled_through = null
720
si
721
722
// Recompile one file's expressions against the current symbol
723
// table, keeping its resolved interface, so every body rebinds.
724
recompile_file_expressions(source_file: SOURCE_FILE) is
725
Syntax.Process.KILL_LEDGER.clear_for(source_file.file_name)
726
727
invalidate_file_expressions(source_file)
728
729
logger.clear_expression_diagnostics(source_file.file_name)
730
Syntax.Process.PURE_SLOTS.clear_for(source_file.file_name)
731
732
compile_expressions_only(source_file)
733
si
734
735
// Re-walk one definition's body against the current symbol table
736
// and the current effect relations: what the previous walk of that
737
// body left behind - its node state, its expression-level
738
// diagnostics and inlays, its ledger records and candidates - is
739
// dropped, and compile-expressions is driven over that body
740
// alone. The interface, and every other body in the file, is
741
// untouched.
742
recompile_body_expressions(source_file: SOURCE_FILE, function: Syntax.Trees.Definitions.FUNCTION) is
743
let file_name = source_file.file_name
744
let span = function.location
745
746
_effects_stale_files.add(file_name)
747
748
Syntax.Process.KILL_LEDGER.clear_for(function)
749
750
if let body = function.body then
751
Syntax.Process.CLEAR_STATE_VISITOR(true).apply(body)
752
fi
753
754
logger.clear_expression_diagnostics_within(file_name, span)
755
container.symbol_use_locations.drop_within(file_name, span)
756
Syntax.Process.PURE_SLOTS.clear_within(file_name, span)
757
758
let only = Collections.SET[Syntax.Trees.Definitions.FUNCTION]()
759
only.add(function)
760
761
logger.set_is_compiling_expressions(true)
762
763
container.compile_expressions.reset_between_walks()
764
765
let symbol_table = container.symbol_table
766
let mark = symbol_table.mark_scope_stack()
767
768
let rewalker = Syntax.Process.BODY_REWALKER(
769
logger,
770
symbol_table,
771
container.namespaces,
772
container.compile_expressions,
773
only
774
)
775
776
try
777
source_file.definition.walk(rewalker)
778
catch e: Exception
779
logger.exception(function.location, e, "caught exception re-walking body in {source_file}")
780
finally
781
symbol_table.release_scope_stack(mark)
782
container.namespaces.pop_all_namespaces()
783
784
logger.set_is_compiling_expressions(false)
785
yrt
786
787
container.compile_expressions.report_field_assignments()
788
789
// The re-walk records the bodies' uses afresh; report the
790
// deprecated ones among them, as a whole build does.
791
Syntax.Process.CHECK_DEPRECATED_USES(logger, container.symbol_use_locations)
792
.check(source_file)
793
si
794
795
// Declare, resolve and compile just the definitions an append-only
796
// interface edit added to the end of a file, against the retained
797
// symbol table. The appended subtree is new - nothing referenced
798
// it before this edit - so no retained symbol changes and no other
799
// file's state is touched: each pass walks only the new nodes.
800
// The new functions carry no proven store-free bit, which is
801
// the sound not-proven default until the next effects solve.
802
// Namespaces named by the new declarations
803
// merge into the retained namespace map exactly as they would in
804
// a full build.
805
build_appended(source_file: SOURCE_FILE, new_definitions: Syntax.Trees.Definitions.LIST) is
806
source_file.want_compile_up_to_expressions = true
807
source_file.want_compile_expressions = true
808
809
_run_appended_pass(source_file, "declare-symbols", () is container.declare_symbols.apply(new_definitions); si)
810
_run_appended_pass(source_file, "resolve-uses", () is container.resolve_uses.apply(new_definitions); si)
811
_run_appended_pass(source_file, "declare-members", () is container.declare_members.apply(new_definitions); si)
812
_run_appended_pass(source_file, "resolve-member-uses", () is container.resolve_member_uses.apply(new_definitions); si)
813
_run_appended_pass(source_file, "resolve-type-expressions", () is container.resolve_type_expressions.apply(new_definitions); si)
814
_run_appended_pass(source_file, "resolve-ancestors", () is container.resolve_ancestors.apply(new_definitions); si)
815
_run_appended_pass(source_file, "synthesize-class-equality", () is container.synthesize_class_equality.apply(new_definitions); si)
816
_run_appended_pass(source_file, "synthesize-iterator-reset", () is container.synthesize_iterator_reset.apply(new_definitions); si)
817
_run_appended_pass(source_file, "resolve-explicit-types", () is container.resolve_explicit_types.apply(new_definitions); si)
818
_run_appended_pass(source_file, "check-type-argument-bounds", () is container.check_type_argument_bounds.apply(new_definitions); si)
819
_run_appended_pass(source_file, "resolve-overrides", () is container.resolve_overrides.apply(new_definitions); si)
820
821
// These two are whole-file, declaration-level walks that
822
// only otherwise run as part of build_passes - the same gap
823
// rewalk_bodies and compile_expressions_only close for their
824
// own paths. Appended declarations get no other pass over
825
// them for either, so a badly-named or virtuality-relevant
826
// new member would go unflagged until the next full rebuild.
827
container.check_name_conventions.apply(source_file)
828
container.definition_virtuality.apply(source_file)
829
830
_run_appended_pass(source_file, "mark-boxed-locals", () is container.mark_boxed_locals.apply(new_definitions, _want_boxing); si)
831
832
logger.set_is_compiling_expressions(true)
833
834
_run_appended_pass(source_file, "compile-expressions", () is container.compile_expressions.apply(new_definitions); si)
835
836
logger.set_is_compiling_expressions(false)
837
si
838
839
// Declare and resolve a container's changed function members
840
// against the retained symbol table, with the scope cursor
841
// positioned inside the enclosing namespace and class scopes the
842
// way the build loop's tree walk would have it. Split from the
843
// expression walk so a class's pull-down can re-run between the
844
// two: overrides and pulled-down members must reflect the new
845
// member set before any body compiles against them.
846
build_members_interface(
847
source_file: SOURCE_FILE,
848
enclosing: Collections.List[Syntax.Trees.Definitions.Definition],
849
members: Syntax.Trees.Definitions.LIST
850
) is
851
source_file.want_compile_up_to_expressions = true
852
source_file.want_compile_expressions = true
853
854
_run_positioned_pass(source_file, enclosing, "declare-members", () is container.declare_members.apply(members); si)
855
_run_positioned_pass(source_file, enclosing, "resolve-type-expressions", () is container.resolve_type_expressions.apply(members); si)
856
_run_positioned_pass(source_file, enclosing, "resolve-explicit-types", () is container.resolve_explicit_types.apply(members); si)
857
_run_positioned_pass(source_file, enclosing, "check-type-argument-bounds", () is container.check_type_argument_bounds.apply(members); si)
858
si
859
860
build_members_expressions(
861
source_file: SOURCE_FILE,
862
enclosing: Collections.List[Syntax.Trees.Definitions.Definition],
863
members: Syntax.Trees.Definitions.LIST
864
) is
865
_run_positioned_pass(source_file, enclosing, "mark-boxed-locals", () is container.mark_boxed_locals.apply(members, _want_boxing); si)
866
867
logger.set_is_compiling_expressions(true)
868
869
_run_positioned_pass(source_file, enclosing, "compile-expressions", () is container.compile_expressions.apply(members); si)
870
871
logger.set_is_compiling_expressions(false)
872
873
// Same reasoning as build_appended: the replaced window's
874
// members get no other pass over these two whole-file
875
// declaration-level walks. Run here, after
876
// build_members_interface's caller has re-pulled overrides
877
// for the changed closure, so definition-virtuality's glyphs
878
// reflect the reconciled dispatch state rather than the
879
// pre-replace one.
880
container.check_name_conventions.apply(source_file)
881
container.definition_virtuality.apply(source_file)
882
si
883
884
// One pass application over a changed member subtree, with the
885
// symbol-table and namespace cursors positioned inside the
886
// enclosing scopes first - a namespace enters through its own
887
// node's scope plus the namespace prefix stack, a class-like
888
// definition through its own node's scope, mirroring
889
// ScopedVisitor. The finally's release and pop restore both
890
// cursors however deep the positioning got.
891
_run_positioned_pass(
892
source_file: SOURCE_FILE,
893
enclosing: Collections.List[Syntax.Trees.Definitions.Definition],
894
pass_name: string,
895
apply_pass: () -> void
896
) is
897
WORK_COUNTERS.subtree_pass(container.timers, pass_name)
898
899
let symbol_table = container.symbol_table
900
let symbol_table_mark = symbol_table.mark_scope_stack()
901
let diagnostics_mark = logger.mark()
902
let symbol_uses_mark = container.symbol_use_locations.mark()
903
904
try
905
for node in enclosing do
906
if let namespace_definition: Syntax.Trees.Definitions.NAMESPACE = node then
907
container.namespaces.enter_namespace(namespace_definition.name.location, namespace_definition.name.name)
908
symbol_table.enter_scope(namespace_definition)
909
elif let carrier: Syntax.Trees.ScopeCarrier = node then
910
symbol_table.enter_scope(carrier)
911
else
912
logger.poison(node.location, "no scope found for {node.get_type()}")
913
fi
914
od
915
916
apply_pass()
917
catch e: Exception
918
logger.exception(diagnostics_mark, source_file.definition.location, e, "caught exception building changed members in {source_file}")
919
finally
920
logger.release(diagnostics_mark)
921
container.symbol_use_locations.release(symbol_uses_mark)
922
symbol_table.release_scope_stack(symbol_table_mark)
923
924
container.namespaces.pop_all_namespaces()
925
yrt
926
si
927
928
// One pass application over an appended subtree, under the build
929
// loop's per-file bookkeeping.
930
_run_appended_pass(source_file: SOURCE_FILE, pass_name: string, apply_pass: () -> void) is
931
WORK_COUNTERS.subtree_pass(container.timers, pass_name)
932
933
let symbol_table = container.symbol_table
934
let symbol_table_mark = symbol_table.mark_scope_stack()
935
let diagnostics_mark = logger.mark()
936
let symbol_uses_mark = container.symbol_use_locations.mark()
937
938
try
939
apply_pass()
940
catch e: Exception
941
logger.exception(diagnostics_mark, source_file.definition.location, e, "caught exception building appended declarations in {source_file}")
942
finally
943
logger.release(diagnostics_mark)
944
container.symbol_use_locations.release(symbol_uses_mark)
945
symbol_table.release_scope_stack(symbol_table_mark)
946
947
container.namespaces.pop_all_namespaces()
948
yrt
949
si
950
951
// Incremental body re-walk: re-process only the edited file's new
952
// function bodies, after its donor bodies have been spliced in by the
953
// analysis-mode EDIT handler. Every pass runs body-scoped — through
954
// BODY_DECLARER for declare-symbols, BODY_REWALKER for the rest — so
955
// the retained interface (arguments, signatures, indexer / property
956
// parameters) is never re-processed. Re-resolving a retained,
957
// already-typed argument would poison it ("set type twice"); the
958
// bodies are leaves, so processing them alone is also sufficient.
959
// resolve-uses / resolve-ancestors / resolve-overrides are skipped —
960
// they do not visit body interiors and the interface is unchanged.
961
//
962
// Before the re-walk re-records the bodies, the edited file's stale
963
// symbol-use / definition entries are reconciled: `correspondence`
964
// gives every retained interface node's post-edit location, and
965
// `body_spans` the pre-edit spans of the re-walked bodies. Body
966
// entries are dropped (re-recorded here), interface entries and
967
// symbols moved to their post-edit locations.
968
rewalk_bodies(
969
source_file: SOURCE_FILE,
970
correspondence: Source.LOCATION_CORRESPONDENCE,
971
body_spans: Source.BODY_SPANS
972
) is
973
source_file.want_compile_up_to_expressions = true
974
source_file.want_compile_expressions = true
975
_effects_stale_files.add(source_file.file_name)
976
977
Syntax.Process.KILL_LEDGER.clear_for(source_file.file_name)
978
979
// Definition reconciliation first — it classifies symbols by
980
// their pre-edit location, before the use reconciliation moves
981
// the interface symbols.
982
container.symbol_definition_locations.refresh_edited_file(source_file.file_name, body_spans)
983
container.symbol_use_locations.refresh_edited_file(source_file.file_name, correspondence, body_spans)
984
985
let flags = container.build_flags
986
let symbol_table = container.symbol_table
987
988
let body_declarer = Syntax.Process.BODY_DECLARER(
989
logger,
990
symbol_table,
991
container.namespaces,
992
container.declare_members
993
)
994
995
// body-scoped declare-symbols
996
WORK_COUNTERS.file_rewalk(container.timers, "declare-symbols")
997
998
let mark = symbol_table.mark_scope_stack()
999
let diagnostics_mark = logger.mark()
1000
1001
try
1002
source_file.definition.walk(body_declarer)
1003
catch e: Exception
1004
logger.exception(diagnostics_mark, source_file.definition.location, e, "caught exception declaring bodies in incremental re-walk of {source_file}")
1005
finally
1006
logger.release(diagnostics_mark)
1007
symbol_table.release_scope_stack(mark)
1008
container.namespaces.pop_all_namespaces()
1009
yrt
1010
1011
rewalk_pass(source_file, "resolve-type-expressions", container.resolve_type_expressions)
1012
rewalk_pass(source_file, "synthesize-class-equality", container.synthesize_class_equality)
1013
rewalk_pass(source_file, "synthesize-iterator-reset", container.synthesize_iterator_reset)
1014
rewalk_pass(source_file, "resolve-explicit-types", container.resolve_explicit_types)
1015
rewalk_pass(source_file, "check-type-argument-bounds", container.check_type_argument_bounds)
1016
1017
if flags.want_assembler \/ flags.want_executable then
1018
rewalk_pass(source_file, "record-type-argument-uses", container.record_type_argument_uses)
1019
fi
1020
1021
// The edited file's declaration-level diagnostics were
1022
// dropped along with everything else before the re-walk, and
1023
// no whole-project pass runs to replace them. This one is a
1024
// per-file declaration walk over symbols an
1025
// interface-preserving edit did not touch, so re-running it
1026
// here restores its inlays against the refreshed locations
1027
// rather than leaving them absent until the next full build.
1028
container.definition_virtuality.apply(source_file)
1029
1030
// Same reasoning: check-name-conventions is another
1031
// whole-file, declaration-level walk that only otherwise runs
1032
// as part of build_passes. Without this, a name-convention
1033
// warning on a declaration an interface-preserving edit did
1034
// not touch is dropped by the clear above and never comes
1035
// back - `are_tables_current` staying true means the
1036
// whole-project rebuild that would re-run it never happens
1037
// again for this file.
1038
container.check_name_conventions.apply(source_file)
1039
1040
logger.set_is_compiling_expressions(true)
1041
1042
// The re-walk drives the visitor directly rather than through
1043
// its apply(), so the state apply() drops at a walk boundary
1044
// has to be dropped here instead. Left in place, the phantom
1045
// type-arg placeholders minted during the previous walk stay
1046
// in the registry, and the first function whose retry loop
1047
// fails to converge sweeps them and reports them against
1048
// their pre-edit locations.
1049
container.compile_expressions.reset_between_walks()
1050
1051
// Driven over the re-walked bodies rather than through
1052
// `apply`, so the marking is bracketed by hand.
1053
container.mark_boxed_locals.begin_marking()
1054
rewalk_pass(source_file, "mark-boxed-locals", container.mark_boxed_locals)
1055
container.mark_boxed_locals.end_marking(_want_boxing)
1056
1057
rewalk_pass(source_file, "compile-expressions", container.compile_expressions)
1058
1059
logger.set_is_compiling_expressions(false)
1060
1061
// Every constructor this re-walk touched has a fresh
1062
// summary; without this, the field check's report() -
1063
// called only from a whole-project build and from
1064
// compile_expressions_only - never runs on this path, and
1065
// a field a prior full build already flagged (or cleared)
1066
// stops tracking the source entirely once analysis mode
1067
// starts driving edits through incremental re-walk.
1068
container.compile_expressions.report_field_assignments()
1069
1070
Syntax.Process.CHECK_DEPRECATED_USES(logger, container.symbol_use_locations)
1071
.check(source_file)
1072
si
1073
1074
// Run one body-visiting pass over the edited file's bodies only,
1075
// driven by BODY_REWALKER, under a scope-stack mark/release. The
1076
// pass name is passed separately because a body-visiting pass is
1077
// a bare visitor, not a registered Pass with a description.
1078
rewalk_pass(source_file: SOURCE_FILE, pass_name: string, pass: Syntax.Visitor) is
1079
WORK_COUNTERS.file_rewalk(container.timers, pass_name)
1080
1081
let symbol_table = container.symbol_table
1082
let mark = symbol_table.mark_scope_stack()
1083
let diagnostics_mark = logger.mark()
1084
1085
let rewalker = Syntax.Process.BODY_REWALKER(
1086
logger,
1087
symbol_table,
1088
container.namespaces,
1089
pass
1090
)
1091
1092
try
1093
source_file.definition.walk(rewalker)
1094
catch e: Exception
1095
logger.exception(diagnostics_mark, source_file.definition.location, e, "caught exception in incremental re-walk of {source_file}")
1096
finally
1097
logger.release(diagnostics_mark)
1098
symbol_table.release_scope_stack(mark)
1099
container.namespaces.pop_all_namespaces()
1100
yrt
1101
si
1102
1103
conditional_compilation_pass(source_file: SOURCE_FILE) -> bool is
1104
let definition = source_file.definition
1105
1106
container
1107
.conditional_compilation
1108
.apply(
1109
definition
1110
)
1111
1112
return true
1113
si
1114
1115
rewrite_syntax_tree_pass(source_file: SOURCE_FILE) -> bool is
1116
let definition = source_file.definition
1117
1118
container
1119
.rewrite_primary_constructors
1120
.apply(definition)
1121
1122
container
1123
.synthesise_top_level_entry
1124
.apply(definition)
1125
1126
container
1127
.synthesise_entry_environment_wrapper
1128
.apply(definition)
1129
1130
container
1131
.expand_namespaces
1132
.apply(definition)
1133
1134
container
1135
.add_accessors_for_properties
1136
.apply(definition)
1137
1138
container
1139
.spill_awaits
1140
.apply(definition)
1141
1142
return true
1143
si
1144
1145
collect_suppress_pragmas_pass(source_file: SOURCE_FILE) -> bool is
1146
container
1147
.collect_suppress_pragmas
1148
.apply(source_file)
1149
1150
return true
1151
si
1152
1153
attach_doc_comments_pass(source_file: SOURCE_FILE) -> bool is
1154
Syntax.Process.ATTACH_DOC_COMMENTS().apply(source_file)
1155
1156
return true
1157
si
1158
1159
collect_modifier_keyword_locations_pass(source_file: SOURCE_FILE) -> bool is
1160
container
1161
.collect_modifier_keyword_locations
1162
.apply(source_file)
1163
1164
return true
1165
si
1166
1167
check_name_conventions_pass(source_file: SOURCE_FILE) -> bool is
1168
container
1169
.check_name_conventions
1170
.apply(source_file)
1171
1172
return true
1173
si
1174
1175
// Settle the entry point outside a build. The debounced COMPILE
1176
// an editor sends once edits stop is the one place the answer is
1177
// wanted in analysis mode, and by then its own path may have
1178
// rebuilt nothing - the edit that preceded it already did.
1179
select_entry_point(files: Collections.Iterable[SOURCE_FILE]) is
1180
container.select_entry_point.start()
1181
1182
for file in files do
1183
container.select_entry_point.apply(file)
1184
od
1185
1186
container.select_entry_point.finish()
1187
si
1188
1189
select_entry_point_pass(source_file: SOURCE_FILE) -> bool is
1190
if !_want_entry_point_selection then
1191
return false
1192
fi
1193
1194
container
1195
.select_entry_point
1196
.apply(source_file)
1197
1198
return true
1199
si
1200
1201
definition_virtuality_pass(source_file: SOURCE_FILE) -> bool is
1202
container
1203
.definition_virtuality
1204
.apply(source_file)
1205
1206
return true
1207
si
1208
1209
declare_symbols_pass(source_file: SOURCE_FILE) -> bool is
1210
let flags = container.build_flags
1211
let definition = source_file.definition
1212
1213
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1214
container
1215
.declare_symbols
1216
.apply(definition)
1217
1218
return true
1219
fi
1220
1221
return false
1222
si
1223
1224
resolve_uses_pass(source_file: SOURCE_FILE) -> bool is
1225
let flags = container.build_flags
1226
let definition = source_file.definition
1227
1228
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1229
container
1230
.resolve_uses
1231
.apply(definition)
1232
1233
return true
1234
fi
1235
1236
return false
1237
si
1238
1239
declare_members_pass(source_file: SOURCE_FILE) -> bool is
1240
let flags = container.build_flags
1241
let definition = source_file.definition
1242
1243
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1244
container
1245
.declare_members
1246
.apply(definition)
1247
1248
return true
1249
fi
1250
1251
return false
1252
si
1253
1254
resolve_member_uses_pass(source_file: SOURCE_FILE) -> bool is
1255
let flags = container.build_flags
1256
let definition = source_file.definition
1257
1258
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1259
container
1260
.resolve_member_uses
1261
.apply(definition)
1262
1263
return true
1264
fi
1265
1266
return false
1267
si
1268
1269
resolve_ancestors_pass(source_file: SOURCE_FILE) -> bool is
1270
let flags = container.build_flags
1271
let definition = source_file.definition
1272
1273
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1274
container
1275
.resolve_ancestors
1276
.apply(definition)
1277
1278
return true
1279
fi
1280
1281
return false
1282
si
1283
1284
synthesize_class_equality_pass(source_file: SOURCE_FILE) -> bool is
1285
let flags = container.build_flags
1286
let definition = source_file.definition
1287
1288
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1289
container
1290
.synthesize_class_equality
1291
.apply(definition)
1292
1293
return true
1294
fi
1295
1296
return false
1297
si
1298
1299
synthesize_iterator_reset_pass(source_file: SOURCE_FILE) -> bool is
1300
let flags = container.build_flags
1301
let definition = source_file.definition
1302
1303
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1304
container
1305
.synthesize_iterator_reset
1306
.apply(definition)
1307
1308
return true
1309
fi
1310
1311
return false
1312
si
1313
1314
resolve_type_expressions_pass(source_file: SOURCE_FILE) -> bool is
1315
let flags = container.build_flags
1316
let definition = source_file.definition
1317
1318
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1319
container
1320
.resolve_type_expressions
1321
.apply(definition)
1322
1323
return true
1324
fi
1325
1326
return false
1327
si
1328
1329
resolve_explicit_types_pass(source_file: SOURCE_FILE) -> bool is
1330
let flags = container.build_flags
1331
let definition = source_file.definition
1332
1333
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1334
container
1335
.resolve_explicit_types
1336
.apply(definition)
1337
1338
return true
1339
fi
1340
1341
return false
1342
si
1343
1344
check_type_argument_bounds_pass(source_file: SOURCE_FILE) -> bool is
1345
let flags = container.build_flags
1346
let definition = source_file.definition
1347
1348
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1349
container
1350
.check_type_argument_bounds
1351
.apply(definition)
1352
1353
return true
1354
fi
1355
1356
return false
1357
si
1358
1359
verify_compile_state_pass(source_file: SOURCE_FILE) -> bool is
1360
if source_file.want_compile_expressions /\ !_verify_compile_state_had_errors then
1361
container
1362
.verify_compile_state
1363
.apply(source_file.definition)
1364
1365
return true
1366
fi
1367
1368
return false
1369
si
1370
1371
check_deprecated_uses_pass(source_file: SOURCE_FILE) -> bool is
1372
if !source_file.want_compile_expressions then
1373
return false
1374
fi
1375
1376
Syntax.Process.CHECK_DEPRECATED_USES(logger, container.symbol_use_locations)
1377
.check(source_file)
1378
1379
return true
1380
si
1381
1382
compile_expressions_pass(source_file: SOURCE_FILE) -> bool is
1383
let definition = source_file.definition
1384
1385
if source_file.want_compile_expressions then
1386
container
1387
.compile_expressions
1388
.apply(definition)
1389
1390
return true
1391
fi
1392
1393
return false
1394
si
1395
1396
// Whether the boxing flags are worth deriving from the
1397
// marking. Boxing is a code generation strategy; the marking
1398
// itself is needed on every build, because flow narrowing
1399
// consults it.
1400
_want_boxing: bool =>
1401
container.build_flags.want_assembler \/
1402
container.build_flags.want_executable
1403
1404
mark_boxed_locals_pass(source_file: SOURCE_FILE) -> bool is
1405
let definition = source_file.definition
1406
1407
// The capture and reassignment marking is needed on
1408
// every build, because flow narrowing consults it to
1409
// decline facts on a local a closure body can rewrite.
1410
// Boxing only matters once the IR is lowered to IL, so
1411
// deriving `is_boxed` from the same flags stays gated on
1412
// an IL-bound build. Either way, pair the gate with
1413
// compile-expressions - if the file isn't being walked
1414
// for expression-level semantics this run, no downstream
1415
// consumer would see the flags we'd set.
1416
if source_file.want_compile_expressions then
1417
container
1418
.mark_boxed_locals
1419
.apply(definition, _want_boxing)
1420
1421
return true
1422
fi
1423
1424
return false
1425
si
1426
1427
resolve_overrides_pass(source_file: SOURCE_FILE) -> bool is
1428
let definition = source_file.definition
1429
1430
if source_file.want_compile_up_to_expressions then
1431
container
1432
.resolve_overrides
1433
.apply(definition)
1434
1435
return true
1436
fi
1437
1438
return false
1439
si
1440
1441
// The re-walk loop of the combined type + effect solve
1442
// prototype. A body walked before any relations were solved,
1443
// or against relations an edit has since changed, may disagree
1444
// with the relations just solved: the judge reports facts it
1445
// kept that are unbacked, and the ledger reports facts it
1446
// dropped that are backed. Files with either are re-walked
1447
// against the new relations, the relations are solved again,
1448
// and the loop stops when a solve finds every file exact.
1449
// Widening can only add callees and writes, and a re-walk
1450
// only reads the relations, so the loop is bounded; the cap is
1451
// a guard. See KILL_LEDGER.
1452
run_combined_solve_rounds(files: Collections.Iterable[SOURCE_FILE]) is
1453
if !Syntax.Process.EFFECTS.is_complete then
1454
return
1455
fi
1456
1457
let by_name = Collections.MAP[string, SOURCE_FILE]()
1458
1459
for source_file in files do
1460
by_name[source_file.file_name] = source_file
1461
od
1462
1463
let round mut = 1
1464
let max_rounds = 16
1465
1466
while round <= max_rounds do
1467
let inexact = Syntax.Process.KILL_LEDGER.take_inexact()
1468
1469
if inexact.is_empty then
1470
break
1471
fi
1472
1473
// Whole-file re-walks, for comparing against the per-body form.
1474
if Driver.COMPILER_ENVIRONMENT.solve_filewise then
1475
for function in inexact.bodies do
1476
inexact.files.add(function.location.file_name)
1477
od
1478
1479
inexact.bodies.clear()
1480
fi
1481
1482
let timing = Driver.COMPILER_ENVIRONMENT.effects_timing
1483
let sw = System.Diagnostics.Stopwatch.start_new()
1484
1485
Syntax.Process.KILL_LEDGER.in_descent = true
1486
1487
let rewalked = Collections.SET[SOURCE_FILE]()
1488
1489
// A file re-walked whole covers its inexact bodies too.
1490
for file_name in inexact.files do
1491
let source_file: SOURCE_FILE mut
1492
1493
if by_name.try_get_value(file_name, source_file ref) then
1494
recompile_file_expressions(source_file)
1495
rewalked.add(source_file)
1496
fi
1497
od
1498
1499
for function in inexact.bodies do
1500
let source_file: SOURCE_FILE mut
1501
1502
if by_name.try_get_value(function.location.file_name, source_file ref) then
1503
if !inexact.files.contains(source_file.file_name) then
1504
recompile_body_expressions(source_file, function)
1505
fi
1506
1507
rewalked.add(source_file)
1508
fi
1509
od
1510
1511
Syntax.Process.KILL_LEDGER.in_descent = false
1512
1513
let recompile_ms = sw.elapsed_milliseconds
1514
1515
let names = Collections.LIST[string]()
1516
1517
for source_file in rewalked do
1518
names.add(source_file.file_name)
1519
od
1520
1521
_run_partial_effects_round(files, names)
1522
1523
if timing then
1524
IO.Std.error.write_line("combined solve round {round}: recompile {recompile_ms} ms, effects {sw.elapsed_milliseconds - recompile_ms} ms")
1525
fi
1526
1527
round = round + 1
1528
od
1529
si
1530
1531
_dump_kills() is
1532
if let dump = Driver.COMPILER_ENVIRONMENT.kill_dump then
1533
let writer = IO.File.append_text(dump)
1534
writer.write_line("=== solve")
1535
Syntax.Process.KILL_LEDGER.dump(writer)
1536
writer.dispose()
1537
fi
1538
si
1539
1540
// The infer-effects pass as a directly callable round, for the
1541
// analysis-mode COMPILE paths that recompile expressions
1542
// without running the pass list: re-walk every file's bodies
1543
// with resolved types, solve the effect relations, and judge
1544
// whatever reliances those recompiles recorded. Without this,
1545
// a reliance recorded on an incremental path would never be
1546
// reported.
1547
run_effects_pass(files: Collections.Iterable[SOURCE_FILE]) is
1548
let timing = Driver.COMPILER_ENVIRONMENT.effects_timing
1549
let sw = System.Diagnostics.Stopwatch.start_new()
1550
1551
_restart_descent(files)
1552
1553
let t_restart = sw.elapsed_milliseconds
1554
1555
// Only the files walked since the last solve can have
1556
// changed what it reads; everything else's facts are
1557
// retained. That needs a complete solve to build on, and
1558
// for every such file to have been compiled through
1559
// expressions, or the round is the full one.
1560
let stale = Collections.LIST[string]()
1561
1562
for file_name in _effects_stale_files do
1563
stale.add(file_name)
1564
od
1565
1566
if _can_solve_partially(files, stale) then
1567
_run_partial_effects_round(files, stale)
1568
else
1569
_run_effects_round(files)
1570
fi
1571
1572
let t_round = sw.elapsed_milliseconds
1573
1574
run_combined_solve_rounds(files)
1575
1576
let t_rounds = sw.elapsed_milliseconds
1577
1578
_dump_kills()
1579
1580
run_post_effects_purity_checks(files)
1581
1582
if timing then
1583
IO.Std.error.write_line("effects pass: restart {t_restart} round {t_round - t_restart} descent {t_rounds - t_round} purity {sw.elapsed_milliseconds - t_rounds} ms")
1584
fi
1585
si
1586
1587
// The answer is defined as the descent from every fact kept, which
1588
// is what a batch build does. An edit path walks a body against
1589
// the last complete solve instead, which is fast but starts the
1590
// descent lower than the top, and from there it can settle short
1591
// of the batch answer. So a COMPILE after any such walk first
1592
// re-walks the bodies that killed outside a descent with kills
1593
// suspended; every other body walked as it would have from the
1594
// top already.
1595
_restart_descent(files: Collections.Iterable[SOURCE_FILE]) is
1596
Syntax.Process.KILL_LEDGER.kills_suspended = false
1597
1598
if !Syntax.Process.KILL_LEDGER.needs_restart then
1599
return
1600
fi
1601
1602
let by_name = Collections.MAP[string, SOURCE_FILE]()
1603
1604
for source_file in files do
1605
by_name[source_file.file_name] = source_file
1606
od
1607
1608
let bodies = Syntax.Process.KILL_LEDGER.take_restart_bodies()
1609
let sw = System.Diagnostics.Stopwatch.start_new()
1610
1611
Syntax.Process.KILL_LEDGER.kills_suspended = true
1612
1613
for function in bodies do
1614
let source_file: SOURCE_FILE mut
1615
1616
if by_name.try_get_value(function.location.file_name, source_file ref) then
1617
recompile_body_expressions(source_file, function)
1618
fi
1619
od
1620
1621
Syntax.Process.KILL_LEDGER.kills_suspended = false
1622
Syntax.Process.KILL_LEDGER.needs_restart = false
1623
1624
if Driver.COMPILER_ENVIRONMENT.effects_timing then
1625
IO.Std.error.write_line("descent restarted from the top: {bodies.count} body(ies) re-walked in {sw.elapsed_milliseconds} ms")
1626
fi
1627
si
1628
1629
// One walk-and-solve round of the effects pass. The judge and
1630
// the purity checks run once, against the relations the last
1631
// round solved.
1632
_run_effects_round(files: Collections.Iterable[SOURCE_FILE]) is
1633
_effects_stale_files.clear()
1634
1635
Syntax.Process.EFFECT_FACTS.collecting = true
1636
Syntax.Process.EFFECT_FACTS.begin_generation("infer-effects")
1637
1638
container.infer_store_free.set_resolved_mode(true)
1639
container.infer_store_free.start_run()
1640
1641
for source_file in files do
1642
infer_effects_pass(source_file)
1643
od
1644
1645
container.infer_store_free.solve_effects(_all_compiled_through_expressions(files))
1646
container.infer_store_free.set_resolved_mode(false)
1647
1648
Syntax.Process.EFFECT_FACTS.collecting = false
1649
si
1650
1651
// Whether every one of these files has had its expressions
1652
// compiled, which is what makes a solve over them complete.
1653
_all_compiled_through_expressions(files: Collections.Iterable[SOURCE_FILE]) -> bool is
1654
let timing = Driver.COMPILER_ENVIRONMENT.effects_timing
1655
let complete mut = true
1656
1657
for source_file in files do
1658
if !is_compiled_through_expressions(source_file) then
1659
if timing then
1660
IO.Std.error.write_line("effects: solve incomplete, not compiled through expressions: {source_file.file_name} (compiled_through {source_file.compiled_through?.description ?? "none"})")
1661
fi
1662
1663
complete = false
1664
fi
1665
od
1666
1667
return complete
1668
si
1669
1670
// Whether the stale files can be solved as a partial round over
1671
// the retained facts: the relations in use were solved from
1672
// them, and every stale file still present is compiled through
1673
// expressions.
1674
_can_solve_partially(files: Collections.Iterable[SOURCE_FILE], stale: Collections.List[string]) -> bool is
1675
if !container.infer_store_free.solved_over_facts then
1676
return false
1677
fi
1678
1679
let names = Collections.SET[string]()
1680
1681
for file_name in stale do
1682
names.add(file_name)
1683
od
1684
1685
for source_file in files do
1686
if names.contains(source_file.file_name) /\ !is_compiled_through_expressions(source_file) then
1687
return false
1688
fi
1689
od
1690
1691
return true
1692
si
1693
1694
// A round over only the files whose bodies were re-walked: their
1695
// facts are dropped and re-derived, every other file's are kept
1696
// from the previous round, and the relations are solved over
1697
// the union. The previous round left the facts in place for
1698
// exactly this.
1699
_run_partial_effects_round(files: Collections.Iterable[SOURCE_FILE], names: Collections.List[string]) is
1700
_effects_stale_files.clear()
1701
1702
Syntax.Process.EFFECT_FACTS.collecting = true
1703
1704
container.infer_store_free.set_resolved_mode(true)
1705
1706
let by_name = Collections.MAP[string, SOURCE_FILE]()
1707
1708
for source_file in files do
1709
by_name[source_file.file_name] = source_file
1710
od
1711
1712
// The re-walk usually re-derives exactly what it replaces: a
1713
// kill widens a receiver, and the widened dispatch reaches the
1714
// same callees and writes the same members; an edit that
1715
// touches no call or store leaves them alone too. Then the
1716
// installed relations already answer for the facts and the
1717
// solve is skipped.
1718
let timing = Driver.COMPILER_ENVIRONMENT.effects_timing
1719
let sw = System.Diagnostics.Stopwatch.start_new()
1720
1721
let snapshot = container.infer_store_free.snapshot_inputs(names)
1722
1723
for file_name in names do
1724
container.infer_store_free.drop_file(file_name)
1725
od
1726
1727
for file_name in names do
1728
let source_file: SOURCE_FILE mut
1729
1730
if by_name.try_get_value(file_name, source_file ref) then
1731
infer_effects_pass(source_file)
1732
fi
1733
od
1734
1735
if timing then
1736
IO.Std.error.write_line("effects: partial round snapshot, drop and re-walk {sw.elapsed_milliseconds} ms")
1737
fi
1738
1739
let changed = container.infer_store_free.changed_inputs(names, snapshot)
1740
1741
if changed.count == 0 then
1742
if timing then
1743
IO.Std.error.write_line("effects: partial round over {names.count} file(s) changed no solve input, relations kept")
1744
fi
1745
else
1746
if timing then
1747
IO.Std.error.write_line("effects: partial round over {names.count} file(s) changed the inputs of {changed.count} function(s), re-solving")
1748
fi
1749
1750
container.infer_store_free.resolve_effects_for(changed)
1751
fi
1752
1753
container.infer_store_free.set_resolved_mode(false)
1754
1755
Syntax.Process.EFFECT_FACTS.collecting = false
1756
si
1757
1758
// The two purity checks that need a body's own store-freedom:
1759
// what reached a pure function-typed slot, and whether an
1760
// operator or override that has to be pure actually is. Both
1761
// ask the solved effect relations, so both run here rather
1762
// than against the declared-types-only approximation that is
1763
// all a pass before compile-expressions could have.
1764
run_post_effects_purity_checks(files: Collections.Iterable[SOURCE_FILE]) is
1765
logger.set_is_compiling_expressions(true)
1766
1767
Syntax.Process.PURE_SLOT_JUDGE(
1768
container.logger,
1769
container.infer_store_free.function_values).judge()
1770
1771
for source_file in files do
1772
if source_file.want_compile_up_to_expressions then
1773
check_pure_overrides_pass(source_file)
1774
fi
1775
od
1776
1777
logger.set_is_compiling_expressions(false)
1778
si
1779
1780
infer_effects_pass(source_file: SOURCE_FILE) -> bool is
1781
let definition = source_file.definition
1782
1783
if source_file.want_compile_up_to_expressions then
1784
// Always the full body re-walk: the per-file facts
1785
// retained for the pre-expressions run were derived
1786
// from declared types only, and reusing them here would
1787
// reintroduce exactly the imprecision this pass exists
1788
// to remove.
1789
container.infer_store_free.apply(definition)
1790
1791
return true
1792
fi
1793
1794
return false
1795
si
1796
1797
register_source_intrinsics_pass(source_file: SOURCE_FILE) -> bool is
1798
let flags = container.build_flags
1799
1800
if flags.want_declare_symbols \/ source_file.want_compile_up_to_expressions then
1801
// A source library's constructor intrinsics serve the
1802
// program compiled with it, as a reflected assembly's serve
1803
// the programs that reference it.
1804
let offers_constructors = container.target_profile.has_source_core_library /\ source_file.library?
1805
1806
container
1807
.register_source_intrinsics
1808
.apply(source_file.definition, offers_constructors)
1809
1810
return true
1811
fi
1812
1813
return false
1814
si
1815
1816
check_pure_overrides_pass(source_file: SOURCE_FILE) -> bool is
1817
let definition = source_file.definition
1818
1819
if source_file.want_compile_up_to_expressions then
1820
container
1821
.check_pure_overrides
1822
.apply(definition)
1823
1824
return true
1825
fi
1826
1827
return false
1828
si
1829
1830
record_type_argument_uses_pass(source_file: SOURCE_FILE) -> bool is
1831
let flags = container.build_flags
1832
1833
if flags.want_assembler \/ flags.want_executable then
1834
let definition = source_file.definition
1835
1836
container
1837
.record_type_argument_uses
1838
.apply(definition)
1839
1840
return true
1841
fi
1842
1843
return false
1844
si
1845
1846
// Whether this build produces an assembly. Analysis mode does not,
1847
// and the generate-il pass then has nothing to do - including in
1848
// its whole-build hooks, which lay out and write the metadata for
1849
// an image nobody asks for.
1850
is_emitting_assembly: bool =>
1851
let flags = container.build_flags in
1852
flags.want_assembler \/ flags.want_executable
1853
1854
// Whether the build emits an image and its target has a backend
1855
// to generate the code for it.
1856
is_generating_code: bool =>
1857
is_emitting_assembly /\ container.target_profile.has_code_generation
1858
1859
generate_il_pass(source_file: SOURCE_FILE) -> bool is
1860
if is_generating_code then
1861
let definition = source_file.definition
1862
1863
container
1864
.generate_il
1865
.apply(definition)
1866
1867
return true
1868
fi
1869
1870
return false
1871
si
1872
si
1873
si