Appearance
| 1 | namespace Compiler is | |
| 2 | use IO.Std | |
| 3 | use Syntax.Process.EXPAND_NAMESPACES | |
| 4 | ||
| 5 | // Accessibility policy for underscore-prefixed declarations. LEGACY keeps | |
| 6 | // the historic emission (methods public, fields assembly, types public). | |
| 7 | // PRIVATE and PROTECTED both make underscore members and types invisible | |
| 8 | // outside the assembly; they differ only in whether a subclass in the same | |
| 9 | // assembly can see them. | |
| 10 | enum UnderscoreAccess is | |
| 11 | LEGACY, PRIVATE, PROTECTED | |
| 12 | si | |
| 13 | ||
| 14 | class GLOBAL_BUILD_FLAGS is | |
| 15 | is_valid: bool | |
| 16 | ||
| 17 | underscore_access: UnderscoreAccess public | |
| 18 | ||
| 19 | /// What the build produces, set by `--target`. | |
| 20 | target: Target public | |
| 21 | ||
| 22 | /// Where the wasm backend writes the text form of its module, set | |
| 23 | /// by `--wat`. Nothing is written where this is absent. | |
| 24 | wat_output: string? public | |
| 25 | ||
| 26 | /// Where the llvm backend writes the text form of its module, set by | |
| 27 | /// `--llvm-output`. The text is otherwise written only for linking, | |
| 28 | /// and deleted afterwards. | |
| 29 | llvm_output: string? public | |
| 30 | ||
| 31 | /// Whether the text form `--wat` writes carries a `;; file:line` | |
| 32 | /// comment before each statement's instructions, set by `--wat-lines`. | |
| 33 | want_wat_lines: bool public | |
| 34 | ||
| 35 | /// Whether the text form `--wat` writes holds only the functions | |
| 36 | /// compiled from the program's own sources, leaving out the | |
| 37 | /// source libraries' and the module's declarations, set by | |
| 38 | /// `--wat-program-only`. | |
| 39 | want_wat_program_only: bool public | |
| 40 | ||
| 41 | /// Whether the text form `--llvm-output` writes holds only the | |
| 42 | /// functions compiled from the program's own sources, leaving out the | |
| 43 | /// source libraries' functions and the module's types, globals and | |
| 44 | /// metadata, set by `--llvm-program-only`. The module linked is whole. | |
| 45 | want_llvm_program_only: bool public | |
| 46 | ||
| 47 | /// Whether a build for llvm that writes `--llvm-output` writes only | |
| 48 | /// that text and links nothing, set by `--llvm-no-link`. | |
| 49 | want_llvm_no_link: bool public | |
| 50 | ||
| 51 | /// Whether the values a body compiles to record the source position | |
| 52 | /// they came from: for the debug information `--debug` writes, and | |
| 53 | /// for the comments `--wat-lines` prints. | |
| 54 | want_source_locations: bool => want_debug \/ want_wat_lines | |
| 55 | ||
| 56 | /// The source libraries `--library-source` declared, which say | |
| 57 | /// which library a source file belongs to. | |
| 58 | library_sources: Source.SOURCE_LIBRARIES? public | |
| 59 | ||
| 60 | _logger: Logging.Logger? | |
| 61 | ||
| 62 | want_analyse: bool public | |
| 63 | want_debug: bool public | |
| 64 | ||
| 65 | /// The optimisation level `--optimise` asked for, 0 to 3, or null | |
| 66 | /// where it was not given. | |
| 67 | optimisation_level: int? public | |
| 68 | ||
| 69 | /// The level an llvm build is optimised at: the one asked for, or 0 | |
| 70 | /// for a debug build and 2 otherwise. | |
| 71 | llvm_optimisation_level: int => | |
| 72 | optimisation_level ?? (if want_debug then 0 else 2 fi) | |
| 73 | want_declare_symbols: bool public | |
| 74 | want_compile_up_to_expressions: bool public | |
| 75 | want_compile_expressions: bool public | |
| 76 | want_executable: bool public | |
| 77 | // Set by `--library`: emit a DLL rather than an EXE, and | |
| 78 | // require no entry point. | |
| 79 | want_library: bool public | |
| 80 | want_assembler: bool public | |
| 81 | want_analysis_stats: bool public | |
| 82 | ||
| 83 | // The definite-return / definite-assignment / possible-null- | |
| 84 | // dereference / non-optional-by-default flow warnings are on | |
| 85 | // by default. Each `no_warn_*` getter consults the logger's | |
| 86 | // suppression set, populated by `--no-warn-*` and `--suppress` | |
| 87 | // CLI flags (both routes feed the same set). | |
| 88 | no_warn_definite_return: bool => _logger? /\ _logger.is_suppressed("definite-return") | |
| 89 | no_warn_definite_assignment: bool => _logger? /\ _logger.is_suppressed("definite-assignment") | |
| 90 | no_warn_field_definite_assignment: bool => _logger? /\ _logger.is_suppressed("field-definite-assignment") | |
| 91 | no_warn_non_optional: bool => _logger? /\ _logger.is_suppressed("non-optional") | |
| 92 | no_warn_impossible_cast: bool => _logger? /\ _logger.is_suppressed("impossible-cast") | |
| 93 | no_warn_cast_may_throw: bool => _logger? /\ _logger.is_suppressed("cast-may-throw") | |
| 94 | no_warn_narrowing_always_succeeds: bool => _logger? /\ _logger.is_suppressed("narrowing-always-succeeds") | |
| 95 | no_warn_irrefutable_destructure: bool => _logger? /\ _logger.is_suppressed("irrefutable-destructure") | |
| 96 | no_warn_likely_override_mismatch: bool => _logger? /\ _logger.is_suppressed("likely-override-mismatch") | |
| 97 | no_warn_redundant_unwrap: bool => _logger? /\ _logger.is_suppressed("redundant-unwrap") | |
| 98 | no_warn_impure_function_value: bool => _logger? /\ _logger.is_suppressed("impure-function-value") | |
| 99 | no_warn_impure_function_argument: bool => _logger? /\ _logger.is_suppressed("impure-function-argument") | |
| 100 | ignore_errors: bool public | |
| 101 | exclude_runtime_symbols: bool public | |
| 102 | is_test_run: bool public | |
| 103 | ||
| 104 | // Report every diagnostic a pass makes, including ones that | |
| 105 | // repeat another exactly. On for a test run, and separately | |
| 106 | // available so a test of the analyser can ask for it. | |
| 107 | keep_duplicate_diagnostics: bool public | |
| 108 | ||
| 109 | // Emit diagnostics in MSBuild's canonical error format | |
| 110 | // (see the `--msbuild-diagnostics` driver flag). MSBuild parses | |
| 111 | // that shape and nothing else, so without it a build reports the | |
| 112 | // compiler's own diagnostics as untyped message text and counts | |
| 113 | // only the non-zero exit as an error. | |
| 114 | want_msbuild_diagnostics: bool public | |
| 115 | ||
| 116 | // Suppresses the analysis-mode heap watchdog's post-compile heap | |
| 117 | // sampling (see the `--no-analysis-heap-watchdog` driver flag). The | |
| 118 | // sampling forces a full GC after every compile; turning it off is | |
| 119 | // useful when profiling the analyser. | |
| 120 | no_analysis_heap_watchdog: bool public | |
| 121 | ||
| 122 | // Opts into the incremental body re-walk for interface-preserving | |
| 123 | // single-file EDITs. On by default; `--no-incremental-analysis` | |
| 124 | // turns it off. | |
| 125 | want_incremental_analysis: bool public | |
| 126 | ||
| 127 | // Seconds an analyser may sit with no request before exiting (see | |
| 128 | // `--analysis-idle-timeout`); 0 disables it. A warm analyser retains | |
| 129 | // the whole symbol table, so an editor window left open overnight | |
| 130 | // holds hundreds of megabytes to answer nothing; the cold rebuild | |
| 131 | // the next request pays is the cheaper side of that trade. Only | |
| 132 | // consulted in analysis mode, where the process is long-lived. | |
| 133 | analysis_idle_timeout_seconds: int public | |
| 134 | ||
| 135 | // Aggregates definitions outside any namespace into a single unnamed | |
| 136 | // global namespace shared across every file, rather than the default | |
| 137 | // per-file private namespace (see `--global-namespace`). Off by | |
| 138 | // default, so each file's global definitions stay file-private. | |
| 139 | want_global_namespace: bool public | |
| 140 | ||
| 141 | // The name of the namespace a file with no namespace of its own | |
| 142 | // is given, in place of one derived from the file's path, and | |
| 143 | // the mark of a submission: one step of an interactive session, | |
| 144 | // compiled as a library whose entry returns the value of its | |
| 145 | // final expression (see `--submission`). Null otherwise. | |
| 146 | submission_name: string? public | |
| 147 | ||
| 148 | // Whether every file with no namespace of its own is a cell of a | |
| 149 | // session built as one program (see `--cells`): its namespace is | |
| 150 | // its file name, its top-level statements are an entry that returns | |
| 151 | // the value of its final expression, and it is never the program's | |
| 152 | // entry point. | |
| 153 | want_cells: bool public | |
| 154 | ||
| 155 | // Whether a namespace-less file is one step of a session, a | |
| 156 | // submission or a cell: its entry returns its final expression's | |
| 157 | // value, and a top-level `let` may read the earlier step's variable | |
| 158 | // of the same name. | |
| 159 | is_session_step: bool => submission_name? \/ want_cells | |
| 160 | ||
| 161 | // Whether the build is a session whose code is kept across cells | |
| 162 | // (see `--session`): cells as `--cells` builds them, plus the | |
| 163 | // semantics a retained cell needs, because a later cell can | |
| 164 | // subclass or override against what an earlier cell was compiled | |
| 165 | // to. Implies `want_cells`. | |
| 166 | want_session: bool public | |
| 167 | ||
| 168 | // Whether the build compiles its code against a hierarchy a later | |
| 169 | // step of the same session can extend: a submission, which a later | |
| 170 | // step subclasses through an assembly reference, or a session, | |
| 171 | // whose cells keep their emitted code while later cells add to the | |
| 172 | // hierarchy. The narrow guards, the complement singletons they | |
| 173 | // read, and the exhaustive-`case` throw key on this; an ordinary | |
| 174 | // build knows every alternative and emits none of them. | |
| 175 | wants_session_guards: bool => submission_name? \/ want_session | |
| 176 | ||
| 177 | // The cells of a session, in the order they were given: the file | |
| 178 | // stems of the namespace-less files, as `--cells` and `--session` | |
| 179 | // name their namespaces. A cell's code was written against the | |
| 180 | // cells before it, so a decision keyed on a closed hierarchy - a | |
| 181 | // narrowing complement, a case's coverage - is made over the | |
| 182 | // alternatives the cells up to that point declare, and the guards | |
| 183 | // carry what a later cell adds. Empty in an ordinary build. | |
| 184 | session_cell_names: Collections.LIST[string] public | |
| 185 | ||
| 186 | // A file's place among the session's cells, by its name: the | |
| 187 | // zero-based position of its stem in `session_cell_names`, or null | |
| 188 | // when the file is not a cell. A file that is not a cell - the | |
| 189 | // host's own entry, a namespaced file - is compiled with the whole | |
| 190 | // session in view, so its readers treat null as "sees everything". | |
| 191 | session_cell_at(file_name: string) -> int? is | |
| 192 | let stem = IO.Path.get_file_name_without_extension(file_name) | |
| 193 | ||
| 194 | if !stem? then | |
| 195 | return null | |
| 196 | fi | |
| 197 | ||
| 198 | // The registry holds namespace names, which spell a file's | |
| 199 | // stem as an identifier the way expand_namespaces does; the | |
| 200 | // same conversion here, or a stem carrying '-' or '.' looks | |
| 201 | // like no cell at all and the view fails open. | |
| 202 | let name = EXPAND_NAMESPACES.convert_file_name_to_identifier(stem) | |
| 203 | ||
| 204 | let flags = IoC.CONTAINER.instance.build_flags | |
| 205 | ||
| 206 | for i in 0..flags.session_cell_names.count do | |
| 207 | if flags.session_cell_names[i] =~ name then | |
| 208 | return i | |
| 209 | fi | |
| 210 | od | |
| 211 | ||
| 212 | return null | |
| 213 | si | |
| 214 | ||
| 215 | // The imports `use default` stands for, when the project names | |
| 216 | // its own set with `--default-use`. Empty leaves the curated set | |
| 217 | // in `DEFAULT_USES` in force. | |
| 218 | default_uses: Collections.LIST[string] public | |
| 219 | ||
| 220 | // Gives every file with no namespace of its own an implicit | |
| 221 | // `use default`, as if the file had written it first (see | |
| 222 | // `--implicit-default-use`). A script run directly wants this; | |
| 223 | // a project, whose files choose their own imports, does not. | |
| 224 | want_implicit_default_use: bool public | |
| 225 | ||
| 226 | // Default idle timeout, in seconds. Long enough that a user stepping | |
| 227 | // away from an editor and coming back rarely pays the cold rebuild, | |
| 228 | // short enough that an abandoned window gives its memory back within | |
| 229 | // the hour. | |
| 230 | default_analysis_idle_timeout_seconds: int static => 1800 | |
| 231 | ||
| 232 | init() is | |
| 233 | underscore_access = UnderscoreAccess.PRIVATE | |
| 234 | target = Target.DOTNET | |
| 235 | analysis_idle_timeout_seconds = default_analysis_idle_timeout_seconds | |
| 236 | want_incremental_analysis = true | |
| 237 | default_uses = Collections.LIST[string]() | |
| 238 | session_cell_names = Collections.LIST[string]() | |
| 239 | si | |
| 240 | ||
| 241 | set_logger(logger: Logging.Logger) is | |
| 242 | _logger = logger | |
| 243 | si | |
| 244 | ||
| 245 | mark_valid() is | |
| 246 | is_valid = true | |
| 247 | si | |
| 248 | ||
| 249 | check_valid() is | |
| 250 | assert is_valid else "build flags accessed before being set" | |
| 251 | si | |
| 252 | si | |
| 253 | si |