Skip to content
← Back

src/driver/main.ghul

1
namespace Driver is
2
use System.Exception
3
4
use IO.Std
5
use IO.Path
6
use IO.File
7
use IO.Directory
8
9
use Collections.Iterable
10
use Collections.LIST
11
use Collections.SET
12
13
use Ghul.Pipes
14
15
use IoC
16
use Logging
17
use Compiler
18
19
class MAIN is
20
paths: Driver.PATH_CONFIG
21
project_name: string
22
container: IoC.CONTAINER
23
compiler: COMPILER
24
flags: GLOBAL_BUILD_FLAGS
25
output_file_name_generator: OUTPUT_FILE_NAME_GENERATOR
26
assemblies: LIST[string]
27
28
// Assemblies named with `--reference`: imported as well as the
29
// reference set, where `-a` names the whole of it.
30
references: LIST[string]
31
32
// Each is `<path>=<logical-name>`, collected from `--resource`
33
// and written into the manifest resource table at the end of
34
// the build.
35
resources: LIST[(path: string, logical_name: string)]
36
37
ghul_source_files: LIST[string]
38
analyse_files: LIST[string]
39
module_version: string
40
@IF.dotnet()
41
assembly_info: Semantic.DotNet.ASSEMBLY_INFO
42
43
// The source libraries declared with --library-source, which say
44
// which library each queued file belongs to and which declared
45
// roots are compiled from
46
library_sources: Source.SOURCE_LIBRARIES
47
48
// Whether warnings, infos and hints located in a source library's
49
// files are reported. They are the library's concern, so not by
50
// default.
51
want_library_warnings: bool
52
53
want_format: bool
54
55
want_dump_tokens: bool
56
want_compile_server: bool
57
58
// --check-complete parses the one source named and prints whether it
59
// is complete, incomplete or invalid, running no other pass.
60
want_check_complete: bool
61
62
// --format-in-place rewrites each source file rather than printing
63
// to standard output. The write goes through a sibling temporary
64
// file and an atomic rename, so a failure anywhere leaves the
65
// original untouched.
66
want_format_in_place: bool
67
68
// --annotate-inferred prints each named source with the types the
69
// build inferred written in; --annotate-inferred-in-place rewrites
70
// the files instead.
71
want_annotate_inferred: bool
72
73
// --strip-annotations <selection> prints each named source with
74
// the chosen written annotations left out, for a program to be
75
// rebuilt with less than its author wrote; `list` prints the
76
// numbered sites instead, each with the span its annotation
77
// occupies.
78
want_strip_annotations: bool
79
strip_selection: string
80
want_annotate_inferred_in_place: bool
81
82
// Column the formatter wraps at. 100 unless --format-width says otherwise: source is
83
// read in narrower places than an editor - a fixed-width block on a web page, a prose
84
// column beside it - and what fits there is the caller's to say.
85
format_width: int
86
87
@IF.dotnet()
88
compiler_version: string =>
89
let assembly_version =
90
System.Reflection.Assembly
91
.get_entry_assembly()!
92
.get_custom_attributes(typeof System.Reflection.AssemblyInformationalVersionAttribute, false)
93
|> map(va => cast System.Reflection.AssemblyInformationalVersionAttribute?(va)!)
94
|> first()
95
96
in
97
if assembly_version? then
98
"{assembly_version.informational_version}"
99
else
100
"v0.0.0-unknown.1"
101
fi
102
103
entry(arguments: string[]) static is
104
let full_arguments = LIST[string](arguments.count + 1)
105
106
add_arguments(full_arguments, arguments)
107
108
let instance = MAIN(LIST[string](full_arguments))
109
si
110
111
add_arguments(result: LIST[string], source: Iterable[string]) static is
112
let seen_files = SET()
113
114
add_arguments(seen_files, result, source)
115
si
116
117
add_arguments(seen_files: SET[string], result: LIST[string], source: Iterable[string]) static is
118
for argument in source do
119
if argument.starts_with('@') then
120
read_arguments_from_file(seen_files, result, argument.substring(1))
121
elif argument.length > 0 then
122
result.add(argument)
123
fi
124
od
125
si
126
127
read_arguments_from_file(seen_files: SET[string], result: LIST[string], path: string) static is
128
// Keyed on the resolved path rather than on what was written, so
129
// that two spellings of one file - one relative and one absolute,
130
// or one with a redundant ./ - are recognised as the same file and
131
// a cycle between them ends.
132
let key = Path.get_full_path(path)
133
134
if seen_files.contains(key) then
135
return
136
fi
137
138
seen_files.add(key)
139
140
let parser = ARGUMENTS_PARSER()
141
142
let text = File.read_all_text(path)
143
144
let arguments = parser.parse_arguments(text)
145
146
add_arguments(seen_files, result, arguments)
147
si
148
149
// This constructor is the whole program: it sets its state up
150
// inside the try, and the only paths that leave without doing
151
// so report the failure and exit the process.
152
@suppress("field-definite-assignment")
153
init(arguments: LIST[string]) is
154
let result mut = 1
155
156
try
157
@IF.dotnet()
158
Std.input_encoding = System.Text.UTF8Encoding(false)
159
@IF.dotnet()
160
Std.output_encoding = System.Text.UTF8Encoding(false)
161
162
if arguments.count == 0 then
163
Std.out.write("ghūl {compiler_version}\n")
164
Std.out.flush()
165
166
Driver.COMPILER_ENVIRONMENT.exit(0)
167
fi
168
169
if arguments.count == 1 /\ arguments[0] =~ "--requirements" then
170
Std.out.write(REQUIREMENTS.text)
171
Std.out.flush()
172
173
Driver.COMPILER_ENVIRONMENT.exit(0)
174
fi
175
176
if USAGE.is_requested(arguments) then
177
Std.out.write(USAGE.text)
178
Std.out.flush()
179
180
Driver.COMPILER_ENVIRONMENT.exit(0)
181
fi
182
183
container = IoC.CONTAINER.instance
184
185
paths = container.path_config
186
flags = container.build_flags
187
compiler = COMPILER()
188
output_file_name_generator = OUTPUT_FILE_NAME_GENERATOR()
189
@IF.dotnet()
190
assembly_info = container.assembly_info
191
module_version = "0:0:0:0"
192
193
parse_flags(arguments)
194
195
@IF.not.dotnet()
196
if flags.target == Target.DOTNET then
197
Std.error.write_line("error: the dotnet target is not available in this build of the compiler")
198
result = 1
199
200
return
201
fi
202
203
if !want_library_warnings then
204
let libraries = library_sources
205
206
container.logger.set_quiet_files(path => libraries.library_for(Path.get_full_path(path))?)
207
fi
208
209
// A conflicting --library-source declaration leaves
210
// nothing worth building: the two identities cannot both
211
// hold, and compiling the sources would only add a
212
// cascade of redefinitions under the errors that say why.
213
// parse_flags reports nothing else as a diagnostic.
214
if container.logger.any_errors then
215
result = finish_build()
216
217
return
218
fi
219
220
if want_check_complete then
221
check_complete()
222
223
Driver.COMPILER_ENVIRONMENT.exit(0)
224
fi
225
226
check_assemblies_exist()
227
228
// A target with a source core library loads no
229
// assemblies: its built-in types are symbols the
230
// compiler constructs. An empty
231
// reference set cannot stand in for this — it means the
232
// SDK-discovered defaults, which would load System.Runtime
233
// and ghul-runtime back in and the front end would pass
234
// for the wrong reason.
235
if !IoC.CONTAINER.instance.target_profile.has_source_core_library then
236
@IF.dotnet()
237
IoC.CONTAINER.instance.assemblies.import(assemblies, references)
238
else
239
Semantic.INTRINSICS_NAMESPACE.materialise(
240
IoC.CONTAINER.instance.symbol_table,
241
IoC.CONTAINER.instance.namespaces)
242
243
// Construct the built-ins before any pass runs: a
244
// signature is resolved against them by name, which
245
// does not go through the lookup, so waiting for the
246
// lookup's first use would be too late.
247
let _ = IoC.CONTAINER.instance.innate_symbol_lookup.get_void_type()
248
fi
249
250
// After the import, so a reference set that provides
251
// Ghul.Intrinsics wins, and before any pass runs: the
252
// ambient `use Ghul.Intrinsics` resolves against what
253
// this guarantees exists.
254
Semantic.INTRINSICS_NAMESPACE.materialise(
255
IoC.CONTAINER.instance.symbol_table,
256
IoC.CONTAINER.instance.namespaces)
257
258
if flags.want_analyse then
259
analyse()
260
261
Driver.COMPILER_ENVIRONMENT.exit(0)
262
fi
263
264
if want_compile_server then
265
serve_compiles()
266
267
Driver.COMPILER_ENVIRONMENT.exit(0)
268
fi
269
270
if want_dump_tokens then
271
dump_tokens()
272
273
Driver.COMPILER_ENVIRONMENT.exit(0)
274
fi
275
276
if want_format then
277
format_files()
278
279
Driver.COMPILER_ENVIRONMENT.exit(0)
280
fi
281
282
if want_strip_annotations then
283
strip_annotations_files()
284
285
Driver.COMPILER_ENVIRONMENT.exit(0)
286
fi
287
288
// A warning rather than an error: every ghul.runtime release so
289
// far passes AssemblyMetadataAttribute a single string, and a
290
// build using one must not start failing. The attribute is left
291
// out either way, so nothing refers to a missing constructor.
292
@IF.dotnet()
293
for problem in assembly_info.resolve(name => IoC.CONTAINER.instance.assemblies.find_type(name)) do
294
container.logger.warn(Source.LOCATION.internal, Semantic.DotNet.ASSEMBLY_INFO.UNMATCHED, problem)
295
od
296
297
start_build()
298
299
compiler.post_parse()
300
301
compiler.build()
302
303
result = finish_build()
304
305
if want_annotate_inferred /\ result == 0 then
306
annotate_inferred_files()
307
fi
308
catch e: INPUT_EXCEPTION
309
Std.error.write_line("error: {e.message}")
310
Std.error.flush()
311
312
result = 1
313
catch e: Exception
314
Std.error.write(e)
315
Std.error.write("\n")
316
Std.error.flush()
317
318
result = 1
319
yrt
320
321
if result == 0 then
322
Std.error.write("*** succeeded ***")
323
else
324
Std.error.write("!!! failed !!!")
325
fi
326
327
Std.error.write("\n")
328
329
Driver.COMPILER_ENVIRONMENT.exit(result)
330
si
331
332
// Every -a names an assembly the compiler is to read. A path that is
333
// not there is always a mistake in what supplied it - nothing is
334
// compiled more successfully for having skipped one - so it is
335
// reported rather than left to surface as whatever the load happens to
336
// throw, or worse, as the missing symbols of a reference that was
337
// silently dropped.
338
check_assemblies_exist() is
339
let message = missing_assemblies_message(assemblies |> cat(references))
340
341
if message? then
342
throw INPUT_EXCEPTION(message)
343
fi
344
si
345
346
// The message naming the assembly paths that are not there, or null
347
// when they all are.
348
missing_assemblies_message(paths: Iterable[string]) -> string? static is
349
let missing = LIST[string]()
350
351
for path in paths do
352
if !File.exists(path) then
353
missing.add(path)
354
fi
355
od
356
357
if missing.count == 0 then
358
return null
359
fi
360
361
let names = missing |> join(", ")
362
363
return
364
if missing.count == 1 then
365
"reference assembly not found: {names}"
366
else
367
"reference assemblies not found: {names}"
368
fi
369
si
370
371
// The value a flag takes from the argument after it.
372
next_flag_value(arguments: Collections.Iterator[string], flag: string) -> string static is
373
if !arguments.move_next() then
374
throw INPUT_EXCEPTION("{flag} needs a value")
375
fi
376
377
return arguments.current.trim()
378
si
379
380
// Records one --library-source declaration. A conflict with an
381
// earlier declaration is a fact about the command line rather
382
// than the source, so it is reported rather than thrown.
383
add_library_source(declaration: string) is
384
let (name, version, root) = parse_library_source(declaration)
385
386
let conflict = library_sources.add(name, version, root)
387
388
if conflict? then
389
container.logger.environment_error(Source.LOCATION.reflected, conflict)
390
fi
391
si
392
393
// Splits a --library-source value into its name, version and root.
394
// The root is everything after the first `=`, so a Windows path
395
// with a drive letter needs no escaping; the first `@` ends the
396
// name and the first `=` ends the version, so neither can contain
397
// those characters.
398
parse_library_source(value: string) -> (name: string, version: string, root: string) static is
399
let complaint = bad_library_source(value)
400
401
if complaint? then
402
throw INPUT_EXCEPTION(complaint)
403
fi
404
405
let equals = value.index_of('=')
406
let at = value.substring(0, equals).index_of('@')
407
408
return (
409
name = value.substring(0, at),
410
version = value.substring(at + 1, equals - at - 1),
411
root = value.substring(equals + 1)
412
)
413
si
414
415
// The complaint about a --library-source value that does not carry
416
// a name, a version and a root, or carries one of them empty.
417
// Null when the value is well formed: the one message names the
418
// one problem there is, the value's shape.
419
bad_library_source(value: string) -> string? static is
420
let complaint = "--library-source expects <name>@<version>=<root>, not '{value}'"
421
422
let equals = value.index_of('=')
423
424
if equals < 0 \/ value.substring(equals + 1).length == 0 then
425
return complaint
426
fi
427
428
let at = value.substring(0, equals).index_of('@')
429
430
if at <= 0 \/ at >= equals - 1 then
431
return complaint
432
fi
433
434
return null
435
si
436
437
parse_flags(args: Iterable[string]) is
438
assemblies = LIST()
439
references = LIST()
440
resources = LIST()
441
ghul_source_files = LIST()
442
library_sources = Source.SOURCE_LIBRARIES()
443
flags.library_sources = library_sources
444
445
format_width = 100
446
447
let args_iterator = args.iterator
448
449
flags.want_compile_up_to_expressions = true
450
flags.want_compile_expressions = true
451
flags.want_assembler = true
452
flags.want_executable = true
453
454
let want_type_check = false
455
let do_not_want_type_check = false
456
457
let deprecated_no_warn = (flag: string, slug: string) is
458
Std.error.write_line("warning: {flag} is deprecated; use --suppress {slug} instead")
459
container.logger.suppress(slug)
460
si
461
462
let conditional_defines = Collections.LIST[string]()
463
464
for s in args_iterator do
465
if s =~ "-A" \/ s =~ "--analyse" then
466
flags.want_analyse = true
467
flags.want_assembler = false
468
flags.want_executable = false
469
elif s =~ "-G" \/ s =~ "--type-check" then
470
flags.want_assembler = false
471
flags.want_executable = false
472
elif s =~ "-g" \/ s =~ "--no-type-check" then
473
flags.want_compile_up_to_expressions = false
474
flags.want_compile_expressions = false
475
flags.want_assembler = false
476
flags.want_executable = false
477
elif s =~ "-E" \/ s =~ "--ignore-errors" then
478
flags.ignore_errors = true
479
elif s =~ "-S" \/ s =~ "--assembler" then
480
flags.want_executable = false
481
elif s =~ "--library" then
482
flags.want_library = true
483
elif s =~ "--library-source" then
484
add_library_source(next_flag_value(args_iterator, s))
485
elif s =~ "--library-warnings" then
486
want_library_warnings = true
487
elif s =~ "--debug" then
488
flags.want_debug = true
489
elif s =~ "--optimise" then
490
let level = next_flag_value(args_iterator, s)
491
492
if level =~ "0" \/ level =~ "1" \/ level =~ "2" \/ level =~ "3" then
493
flags.optimisation_level = int.parse(level)
494
else
495
throw INPUT_EXCEPTION("--optimise must be 0, 1, 2 or 3, not {level}")
496
fi
497
elif s =~ "--trace-inference" then
498
Semantic.INFERENCE_TRACE.enable("-")
499
elif s =~ "--trace-inference-file" then
500
Semantic.INFERENCE_TRACE.enable(next_flag_value(args_iterator, s))
501
elif s =~ "--define" then
502
conditional_defines.add(next_flag_value(args_iterator, s))
503
elif s =~ "--entry" then
504
container.entry_point.name = next_flag_value(args_iterator, s)
505
container.entry_point.name_is_explicit = true
506
elif s =~ "--v3" then
507
conditional_defines.add("v3")
508
elif s =~ "--test-run" then
509
flags.is_test_run = true
510
elif s =~ "--keep-duplicate-diagnostics" then
511
flags.keep_duplicate_diagnostics = true
512
elif s =~ "--msbuild-diagnostics" then
513
flags.want_msbuild_diagnostics = true
514
elif s =~ "-N" \/ s =~ "--dotnet" then
515
flags.target = Target.DOTNET
516
elif s =~ "--wat" then
517
flags.wat_output = next_flag_value(args_iterator, s)
518
elif s =~ "--llvm-output" then
519
flags.llvm_output = next_flag_value(args_iterator, s)
520
elif s =~ "--wat-lines" then
521
flags.want_wat_lines = true
522
elif s =~ "--wat-program-only" then
523
flags.want_wat_program_only = true
524
elif s =~ "--llvm-program-only" then
525
flags.want_llvm_program_only = true
526
elif s =~ "--llvm-no-link" then
527
flags.want_llvm_no_link = true
528
elif s =~ "--target" then
529
let name = next_flag_value(args_iterator, s)
530
if name =~ "dotnet" then
531
flags.target = Target.DOTNET
532
elif name =~ "wasm" then
533
flags.target = Target.WASM
534
elif name =~ "llvm" then
535
flags.target = Target.LLVM
536
else
537
throw INPUT_EXCEPTION("--target must be dotnet, wasm or llvm, not {name}")
538
fi
539
elif s =~ "-o" \/ s =~ "--output" then
540
output_file_name_generator.force(next_flag_value(args_iterator, s))
541
elif s =~ "-p" \/ s =~ "--library-prefix" then
542
paths.library_prefix = next_flag_value(args_iterator, s)
543
elif s =~ "-a" \/ s =~ "--assembly" then
544
assemblies.add(next_flag_value(args_iterator, s))
545
elif s =~ "--reference" then
546
references.add(next_flag_value(args_iterator, s))
547
elif s =~ "--resource" then
548
let resource_arg = next_flag_value(args_iterator, s)
549
let separator_index = resource_arg.last_index_of('=')
550
551
if separator_index > 0 /\ separator_index < resource_arg.length - 1 then
552
resources.add((
553
path = resource_arg.substring(0, separator_index),
554
logical_name = resource_arg.substring(separator_index + 1)))
555
else
556
Std.error.write_line("warning: ignoring garbled --resource value: {resource_arg}")
557
fi
558
elif s =~ "--assembly-info-string" then
559
let info_arg = next_flag_value(args_iterator, s)
560
let separator_index = info_arg.index_of('=')
561
562
if separator_index > 0 then
563
@IF.dotnet()
564
assembly_info.add_attribute(
565
info_arg.substring(0, separator_index).trim(),
566
info_arg.substring(separator_index + 1).trim())
567
else
568
Std.error.write_line("warning: ignoring garbled assembly info: {info_arg}")
569
fi
570
elif s =~ "--version" then
571
let version_string = next_flag_value(args_iterator, s)
572
573
let version_string_first_part = version_string.split(['-', '+']) |> first()
574
575
try
576
if
577
version_string_first_part?
578
then
579
let version_parts = version_string_first_part.split(['.'])
580
581
if version_parts.count > 4 then
582
throw Exception("invalid version number")
583
fi
584
585
let b = System.Text.StringBuilder()
586
let seen_any mut = false
587
588
for p in version_parts do
589
if seen_any then
590
b.append(":")
591
fi
592
593
b.append(int.parse(p))
594
595
seen_any = true
596
od
597
598
for i in version_parts.count..4 do
599
b.append(":0")
600
od
601
602
module_version = b.to_string()
603
else
604
throw Exception("invalid version number")
605
fi
606
607
catch ex: Exception
608
Std.error.write_line("warning: ignoring garbled version number: {version_string}")
609
yrt
610
elif s =~ "--sdk-ref-path" then
611
paths.sdk_reference_path = next_flag_value(args_iterator, s)
612
elif s =~ "--target-framework" then
613
paths.target_framework = next_flag_value(args_iterator, s)
614
elif s =~ "--exclude-runtime-symbols" then
615
flags.exclude_runtime_symbols = true
616
elif s =~ "--show-analysis-stats" then
617
flags.want_analysis_stats = true
618
elif s =~ "--no-analysis-heap-watchdog" then
619
flags.no_analysis_heap_watchdog = true
620
elif s =~ "--incremental-analysis" then
621
flags.want_incremental_analysis = true
622
elif s =~ "--no-incremental-analysis" then
623
flags.want_incremental_analysis = false
624
elif s =~ "--analysis-idle-timeout" then
625
let seconds_string = next_flag_value(args_iterator, s)
626
627
try
628
flags.analysis_idle_timeout_seconds = int.parse(seconds_string)
629
catch ex: Exception
630
Std.error.write_line("warning: ignoring garbled --analysis-idle-timeout value: {seconds_string}")
631
yrt
632
elif s =~ "--default-use" then
633
for name in next_flag_value(args_iterator, s).split([',']) do
634
let trimmed = name.trim()
635
636
if trimmed.length > 0 then
637
flags.default_uses.add(trimmed)
638
fi
639
od
640
elif s =~ "--implicit-default-use" then
641
flags.want_implicit_default_use = true
642
elif s =~ "--cells" then
643
flags.want_cells = true
644
elif s =~ "--session" then
645
flags.want_session = true
646
flags.want_cells = true
647
elif s =~ "--submission" then
648
flags.submission_name = next_flag_value(args_iterator, s)
649
elif s =~ "--global-namespace" then
650
flags.want_global_namespace = true
651
elif s =~ "--underscore-access" then
652
let mode = next_flag_value(args_iterator, s)
653
if mode =~ "private" then
654
flags.underscore_access = UnderscoreAccess.PRIVATE
655
elif mode =~ "protected" then
656
flags.underscore_access = UnderscoreAccess.PROTECTED
657
elif mode =~ "legacy" then
658
flags.underscore_access = UnderscoreAccess.LEGACY
659
else
660
Std.error.write_line("warning: unrecognized --underscore-access mode: {mode} (expected legacy, private or protected)")
661
fi
662
elif s =~ "--compile-server" then
663
want_compile_server = true
664
elif s =~ "--dump-tokens" then
665
want_dump_tokens = true
666
elif s =~ "--check-complete" then
667
want_check_complete = true
668
elif s =~ "--format" then
669
want_format = true
670
elif s =~ "--format-in-place" then
671
want_format = true
672
want_format_in_place = true
673
elif s =~ "--strip-annotations" then
674
want_strip_annotations = true
675
strip_selection = next_flag_value(args_iterator, s)
676
elif s =~ "--annotate-inferred" then
677
want_annotate_inferred = true
678
flags.want_assembler = false
679
flags.want_executable = false
680
elif s =~ "--annotate-inferred-in-place" then
681
want_annotate_inferred = true
682
want_annotate_inferred_in_place = true
683
flags.want_assembler = false
684
flags.want_executable = false
685
elif s =~ "--format-width" then
686
let width = next_flag_value(args_iterator, s)
687
688
try
689
format_width = int.parse(width)
690
691
if format_width < 20 then
692
Std.error.write_line("warning: --format-width {format_width} is too narrow to lay anything out; using 20")
693
694
format_width = 20
695
fi
696
catch ex: Exception
697
Std.error.write_line("warning: --format-width expects a number, not '{width}'")
698
yrt
699
elif s =~ "--warn-implicit-mutable-let" then
700
Std.error.write_line("warning: --warn-implicit-mutable-let is deprecated; the implicit-mutable-let warning is now an error and the flag has no effect")
701
elif s =~ "--no-warn-definite-return" then
702
deprecated_no_warn("--no-warn-definite-return", "definite-return")
703
elif s =~ "--no-warn-definite-assignment" then
704
deprecated_no_warn("--no-warn-definite-assignment", "definite-assignment")
705
elif s =~ "--no-warn-non-optional" then
706
deprecated_no_warn("--no-warn-non-optional", "non-optional")
707
elif s =~ "--no-warn-impossible-cast" then
708
deprecated_no_warn("--no-warn-impossible-cast", "impossible-cast")
709
elif s =~ "--no-warn-narrowing-always-succeeds" then
710
deprecated_no_warn("--no-warn-narrowing-always-succeeds", "narrowing-always-succeeds")
711
elif s =~ "--no-warn-likely-override-mismatch" then
712
deprecated_no_warn("--no-warn-likely-override-mismatch", "likely-override-mismatch")
713
elif s =~ "--suppress" then
714
for slug in next_flag_value(args_iterator, s).split([',']) do
715
let trimmed = slug.trim()
716
if trimmed.length > 0 then
717
container.logger.suppress(trimmed)
718
fi
719
od
720
elif s =~ "--warn-as-error" then
721
for slug in next_flag_value(args_iterator, s).split([',']) do
722
let trimmed = slug.trim()
723
if trimmed =~ "all" then
724
container.logger.set_all_warnings_are_errors(true)
725
elif trimmed.length > 0 then
726
container.logger.promote_to_error(trimmed)
727
fi
728
od
729
elif s =~ "--warn-as-hint" then
730
for slug in next_flag_value(args_iterator, s).split([',']) do
731
let trimmed = slug.trim()
732
if trimmed.length > 0 then
733
container.logger.demote_to_hint(trimmed)
734
fi
735
od
736
elif s =~ "--warn-as-info" then
737
for slug in next_flag_value(args_iterator, s).split([',']) do
738
let trimmed = slug.trim()
739
if trimmed.length > 0 then
740
container.logger.demote_to_info(trimmed)
741
fi
742
od
743
elif s =~ "--warn" then
744
for slug in next_flag_value(args_iterator, s).split([',']) do
745
let trimmed = slug.trim()
746
if trimmed.length > 0 then
747
container.logger.unsuppress(trimmed)
748
fi
749
od
750
elif s =~ "--inlay" then
751
for kind in next_flag_value(args_iterator, s).split([',']) do
752
let trimmed = kind.trim()
753
754
if trimmed.length > 0 then
755
if Logging.INLAY_KINDS.is_known(trimmed) then
756
container.logger.enable_inlay_kind(trimmed)
757
else
758
Std.error.write_line("warning: ignoring unknown inlay kind: {trimmed}")
759
fi
760
fi
761
od
762
elif s =~ "--no-inlay" then
763
for kind in next_flag_value(args_iterator, s).split([',']) do
764
let trimmed = kind.trim()
765
766
if trimmed.length > 0 then
767
if Logging.INLAY_KINDS.is_known(trimmed) then
768
container.logger.disable_inlay_kind(trimmed)
769
else
770
Std.error.write_line("warning: ignoring unknown inlay kind: {trimmed}")
771
fi
772
fi
773
od
774
elif s.starts_with('-') then
775
Std.error.write_line("warning: ignoring unknown option: {s}")
776
elif SOURCE_FILE_CATEGORIZER.is_ghul(s) then
777
output_file_name_generator.seen_file(s)
778
ghul_source_files.add(s)
779
elif IO.Directory.exists(s) then
780
ghul_source_files.add(s)
781
else
782
Std.error.write_line("warning: ignoring unrecognized argument: {s}")
783
fi
784
od
785
786
flags.mark_valid()
787
788
if flags.want_msbuild_diagnostics then
789
container.want_msbuild_logger(Std.error)
790
elif flags.is_test_run then
791
container.want_human_readable_logger(Std.error)
792
container.want_duplicate_diagnostics()
793
fi
794
795
if flags.keep_duplicate_diagnostics then
796
container.want_duplicate_diagnostics()
797
fi
798
799
container.conditional_compilation.set_is_enabled(conditional_defines, container.target_profile)
800
si
801
802
// One word on standard output, and a zero exit status whatever the
803
// answer: a non-zero status means the check itself could not run.
804
check_complete() is
805
if ghul_source_files.count != 1 then
806
throw INPUT_EXCEPTION("--check-complete takes exactly one source file")
807
fi
808
809
let path = ghul_source_files[0]
810
811
let completeness = compiler.check_complete(path, File.open_text(path))
812
813
Std.out.write_line(
814
case completeness
815
when Syntax.Parsers.COMPLETENESS.COMPLETE then "complete"
816
when Syntax.Parsers.COMPLETENESS.INCOMPLETE then "incomplete"
817
else "invalid"
818
esac
819
)
820
821
Std.out.flush()
822
si
823
824
dump_tokens() is
825
for path in expand_format_sources() do
826
try
827
let tokenizer = Lexical.TOKENIZER(
828
IoC.CONTAINER.instance.logger,
829
path,
830
File.open_text(path),
831
false
832
)
833
834
let pair mut = tokenizer.read_token()
835
836
while pair.token != Lexical.TOKEN.END_OF_INPUT do
837
let value = pair.value_string
838
.replace("\\", "\\\\")
839
.replace("\t", "\\t")
840
.replace("\n", "\\n")
841
.replace("\r", "\\r")
842
843
Std.out.write_line("{path}\t{pair.location.start_line}\t{pair.location.start_column}\t{pair.location.end_line}\t{pair.location.end_column}\t{pair.token}\t{value}")
844
845
pair = tokenizer.read_token()
846
od
847
catch ex: Exception
848
Std.error.write_line("error: could not tokenize {path}: {ex.message}")
849
yrt
850
od
851
852
Std.out.flush()
853
si
854
855
// A library source is read alongside the program's own and is not
856
// the program's to rewrite, so it is never formatted.
857
format_files() is
858
for path in expand_format_sources() do
859
if library_sources.library_for(Path.get_full_path(path))? then
860
continue
861
fi
862
863
try
864
let source_file =
865
compiler.parse(path, File.open_text(path), flags.want_compile_up_to_expressions, flags.want_compile_expressions, false)
866
867
let formatter =
868
Syntax.Process.Printer.FORMATTER(source_file.trivia, format_width)
869
870
let formatted = formatter.format(source_file.definition)
871
872
if want_format_in_place then
873
write_formatted_in_place(path, formatted)
874
else
875
Std.out.write(formatted)
876
Std.out.flush()
877
fi
878
catch ex: Exception
879
Std.error.write_line("error: could not format {path}: {ex.message}")
880
yrt
881
od
882
si
883
884
// Each named source printed with the selected written annotations
885
// left out. Under `list` the numbered sites go to standard output
886
// in place of the source; otherwise the source goes there and the
887
// sites to standard error, one per line.
888
strip_annotations_files() is
889
for path in expand_format_sources() do
890
let source_file =
891
compiler.parse(path, File.open_text(path), false, false, false)
892
893
let formatter =
894
Syntax.Process.Printer.FORMATTER(source_file.trivia, format_width)
895
896
let stripper = Syntax.Process.Printer.ANNOTATION_STRIPPER(strip_selection)
897
898
formatter.stripper = stripper
899
900
let formatted = formatter.format(source_file.definition)
901
902
if strip_selection =~ "list" \/ strip_selection =~ "cast:list" then
903
for site in stripper.sites do
904
Std.out.write_line(site)
905
od
906
else
907
Std.out.write(formatted)
908
909
for site in stripper.sites do
910
Std.error.write_line(site)
911
od
912
fi
913
914
Std.out.flush()
915
Std.error.flush()
916
od
917
si
918
919
// Each named source printed with the types the build settled
920
// at its inferred sites. The built tree is not what is printed:
921
// the passes splice nodes the source never had, so the types are
922
// collected from it by position and written into a fresh parse.
923
annotate_inferred_files() is
924
let annotated mut = 0
925
let skipped mut = 0
926
927
for source_file in compiler.source_files do
928
let path = source_file.file_name
929
930
if !(ghul_source_files |> any(f => f =~ path)) then
931
continue
932
fi
933
934
let annotations =
935
Syntax.Process.INFERRED_ANNOTATIONS(container.logger, container.symbol_table, container.namespaces)
936
937
annotations.collect(source_file.definition)
938
939
annotated = annotated + annotations.annotated
940
skipped = skipped + annotations.skipped
941
942
let use reader = File.open_text(path)
943
944
let fresh =
945
compiler.parse(path, reader, false, false, false, Logging.DIAGNOSTICS_STORE())
946
947
let formatter =
948
Syntax.Process.Printer.FORMATTER(fresh.trivia, format_width)
949
950
formatter.annotations = annotations
951
952
let formatted = formatter.format(fresh.definition)
953
954
if want_annotate_inferred_in_place then
955
write_formatted_in_place(path, formatted)
956
else
957
Std.out.write(formatted)
958
Std.out.flush()
959
fi
960
od
961
962
Std.error.write_line("annotated {annotated} inferred sites, skipped {skipped}")
963
si
964
965
// Under --format a directory argument names a tree of sources: every
966
// .ghul file beneath it, recursively.
967
expand_format_sources() -> LIST[string] is
968
let paths = LIST[string]()
969
970
for path in ghul_source_files do
971
if IO.Directory.exists(path) then
972
for file in IO.Directory.get_files(path, "*.ghul", IO.SearchOption.ALL_DIRECTORIES) do
973
paths.add(file)
974
od
975
else
976
paths.add(path)
977
fi
978
od
979
980
return paths
981
si
982
983
// Write through a sibling temporary file and rename it over the
984
// original: the rename is atomic, so a failure anywhere before it
985
// leaves the original untouched rather than truncated.
986
write_formatted_in_place(path: string, contents: string) is
987
let temporary = "{path}.tmp"
988
989
File.write_all_text(temporary, contents)
990
File.move(temporary, path, true)
991
si
992
993
start_build() is
994
queue_source_files()
995
996
if flags.want_assembler then
997
container.value_boxer.want_boxing = true
998
fi
999
1000
let module_name =
1001
Path.get_file_name_without_extension(output_file_name_generator.result) ?? "module"
1002
1003
install_assembly_emitter(module_name)
1004
si
1005
1006
finish_build() -> int is
1007
container.logger.write_all_diagnostics(container.logger_writer, container.logger_formatter)
1008
1009
if container.logger.any_errors /\ !flags.ignore_errors then
1010
return 1
1011
elif container.logger.is_poisoned then
1012
container.logger.write_poison_messages()
1013
1014
Std.error.write_line("internal error")
1015
return 2
1016
fi
1017
1018
if !flags.want_assembler then
1019
return 0
1020
fi
1021
1022
if flags.want_cells then
1023
write_cell_export_report()
1024
fi
1025
1026
return
1027
case flags.target
1028
when Target.DOTNET then finish_build_dotnet()
1029
when Target.WASM then finish_build_wasm()
1030
when Target.LLVM then finish_build_llvm()
1031
esac
1032
si
1033
1034
// The cells' exportable names, beside the build's output: what a
1035
// session host without assemblies to read builds its `use cellK.name`
1036
// prelude from. Named for the output's stem, so a host that wrote
1037
// `session.wasm` finds `session.cells.json` beside it.
1038
write_cell_export_report() is
1039
let output = output_file_name_generator.result
1040
let stem = Path.get_file_name_without_extension(output) ?? "module"
1041
1042
let report =
1043
CELL_EXPORT_REPORT.build(container.symbol_table, flags.session_cell_names)
1044
1045
let directory = Path.get_directory_name(output) ?? ""
1046
1047
let sidecar =
1048
if directory.length == 0 then
1049
"{stem}.cells.json"
1050
else
1051
"{directory}{Path.directory_separator_char}{stem}.cells.json"
1052
fi
1053
1054
File.write_all_text(sidecar, CELL_EXPORT_REPORT.write(report))
1055
si
1056
1057
finish_build_llvm() -> int is
1058
let output_file mut = output_file_name_generator.result
1059
1060
if flags.want_library /\ output_file.last_index_of('.') < 0 then
1061
output_file = "{output_file}.ll"
1062
fi
1063
1064
if !flags.want_executable then
1065
return 0
1066
fi
1067
1068
if resources.count > 0 then
1069
Std.error.write_line("error: resources are not supported on the llvm target")
1070
1071
return 1
1072
fi
1073
1074
try
1075
container.backend.write(output_file)
1076
catch e: IR.LLVMTarget.LinkFailedException
1077
Std.error.write_line("internal error: {e.message}")
1078
1079
return 2
1080
yrt
1081
1082
return 0
1083
si
1084
1085
finish_build_wasm() -> int is
1086
let output_file mut = output_file_name_generator.result
1087
1088
// GetExtension reads the file name, so a dot in a directory
1089
// along the way (mktemp's default template has one) is not the
1090
// module's extension.
1091
if (IO.Path.get_extension(output_file) ?? "") =~ "" then
1092
output_file = "{output_file}.wasm"
1093
fi
1094
1095
if !flags.want_executable then
1096
return 0
1097
fi
1098
1099
if resources.count > 0 then
1100
Std.error.write_line("error: resources are not supported on the wasm target")
1101
1102
return 1
1103
fi
1104
1105
container.backend.write(output_file)
1106
1107
return 0
1108
si
1109
1110
@IF.dotnet()
1111
finish_build_dotnet() -> int is
1112
let output_file mut = output_file_name_generator.result
1113
1114
if output_file.last_index_of('.') < 0 then
1115
output_file = "{output_file}.{if flags.want_library then "dll" else "exe" fi}"
1116
fi
1117
1118
if !flags.want_executable then
1119
return 0
1120
fi
1121
1122
for r in resources do
1123
container.backend.add_resource(
1124
r.logical_name,
1125
File.read_all_bytes(r.path))
1126
od
1127
1128
container.backend.write(output_file)
1129
1130
let result mut = 0
1131
1132
// A library assembly is loaded into a host's runtime config,
1133
// not run directly, so it needs neither its own
1134
// `.runtimeconfig.json` nor the executable bit.
1135
if !flags.want_library then
1136
let runtime_config_file =
1137
if output_file.last_index_of('.') == output_file.length - 4 then
1138
"{output_file.substring(0, output_file.last_index_of('.'))}.runtimeconfig.json"
1139
else
1140
"{output_file}.runtimeconfig.json"
1141
fi
1142
1143
let resolver = TARGET_FRAMEWORK_RESOLVER()
1144
let tfm = resolver.resolve_tfm(paths.target_framework, paths.sdk_reference_path, paths.host_target_framework)
1145
let framework_version = resolver.resolve_framework_version(tfm)
1146
1147
let runtime_config = File.create_text(runtime_config_file)
1148
runtime_config.write("{{\"runtimeOptions\":{{\"tfm\":\"{tfm}\",\"framework\":{{\"name\":\"Microsoft.NETCore.App\",\"version\":\"{framework_version}\"}} }} }}")
1149
runtime_config.close()
1150
1151
if !System.Runtime.InteropServices.RuntimeInformation.is_o_s_platform(System.Runtime.InteropServices.OSPlatform.windows) then
1152
let chmod = System.Diagnostics.Process.start("/bin/chmod", "+x {output_file}")
1153
1154
chmod.wait_for_exit()
1155
1156
result = chmod.exit_code
1157
1158
if result != 0 then
1159
Std.error.write_line("compiled successfully but failed to set executable bit on resulting binary: {output_file}")
1160
fi
1161
fi
1162
fi
1163
1164
return result
1165
si
1166
1167
queue_source_files() is
1168
for file in ghul_source_files do
1169
if file.ends_with(".ghul") then
1170
// A file under a root the library is not compiled from
1171
// is skipped: the library is already in from an
1172
// earlier root, and compiling both would define its
1173
// every symbol twice
1174
if !library_sources.is_skipped(Path.get_full_path(file)) then
1175
queue_source_file(file, false)
1176
fi
1177
elif IO.Directory.exists(file) then
1178
Std.error.write_line(
1179
"warning: ignoring directory argument {file}: a directory is expanded only by --format")
1180
fi
1181
od
1182
si
1183
1184
queue_source_file(path: string, is_internal_file: bool) is
1185
if flags.want_analyse then
1186
analyse_files.add(path)
1187
1188
return
1189
fi
1190
1191
let use reader = File.open_text(path)
1192
1193
let source_file =
1194
compiler.parse(
1195
path,
1196
reader,
1197
flags.want_compile_up_to_expressions,
1198
flags.want_compile_expressions,
1199
is_internal_file
1200
)
1201
1202
source_file.library = library_sources.library_for(Path.get_full_path(path))
1203
1204
compiler.queue(source_file)
1205
si
1206
1207
// Every mode that runs the passes needs an emitter, whether or not
1208
// it writes an assembly: a pass's prologue runs before the pass
1209
// decides it has nothing to emit, so analysis reaches one too. It
1210
// is installed from here rather than built with the context,
1211
// because the emitter is reached through the container the
1212
// context is being built into.
1213
install_assembly_emitter(module_name: string) is
1214
container.backend.install(
1215
module_name, module_version, flags.want_library)
1216
si
1217
1218
@IF.dotnet()
1219
serve_compiles() is
1220
COMPILE_SERVER(compiler, container, module_version, Std.`in, Std.out).serve()
1221
si
1222
1223
@IF.dotnet()
1224
analyse() is
1225
// Analysis names no output, and writes none: this emitter is
1226
// what the passes hold, not something they fill.
1227
install_assembly_emitter("module")
1228
1229
analyse_files = LIST()
1230
1231
// The source files named on the command line - a manifest
1232
// project's library sources - are read from disk once the
1233
// client sends its first edit; the files the client sends
1234
// replace any of them it opens.
1235
queue_source_files()
1236
1237
container.watchdog.heap_check_disabled = flags.no_analysis_heap_watchdog
1238
1239
// The analyser reads protocol frames and EDIT payloads from stdin a
1240
// character at a time. Console.In (Std.`in`) buffers only 256 bytes,
1241
// so a large EDIT payload costs thousands of read() syscalls. Read
1242
// instead through a 256 KB buffer over the raw standard-input
1243
// stream: that exceeds every compiler source file (largest ~230 KB,
1244
// p99 ~44 KB), so a single-file EDIT is drained in one read() and a
1245
// whole-project EDIT in few. A buffered read still returns as soon
1246
// as any bytes are available — a fully-received request is never
1247
// delayed waiting for the buffer to fill.
1248
let analyser_input =
1249
IO.StreamReader(
1250
Std.open_standard_input(),
1251
System.Text.UTF8Encoding(false),
1252
false,
1253
262144
1254
)
1255
1256
let analyser = Analysis.ANALYSER(
1257
compiler,
1258
container.timers,
1259
container.symbol_table,
1260
container.symbol_use_locations,
1261
container.symbol_definition_locations,
1262
container.completer,
1263
container.signature_help,
1264
analyser_input,
1265
Std.out,
1266
analyse_files,
1267
flags,
1268
container.watchdog
1269
)
1270
1271
Std.error.write_line("ghūl compiler {compiler_version}: serving analysis requests")
1272
1273
analyser.run()
1274
si
1275
1276
@IF.not.dotnet()
1277
compiler_version: string => NATIVE_BUILD.version
1278
1279
@IF.not.dotnet()
1280
finish_build_dotnet() -> int is
1281
Std.error.write_line("error: the dotnet target is not available in this build of the compiler")
1282
1283
return 1
1284
si
1285
1286
@IF.not.dotnet()
1287
serve_compiles() is
1288
Std.error.write_line("error: the compile server is not available in this build of the compiler")
1289
1290
Driver.COMPILER_ENVIRONMENT.exit(1)
1291
si
1292
1293
@IF.not.dotnet()
1294
analyse() is
1295
Std.error.write_line("error: analysis mode is not available on this target")
1296
1297
Driver.COMPILER_ENVIRONMENT.exit(1)
1298
si
1299
si
1300
si