Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use Collections.Iterable | |
| 5 | ||
| 6 | use Logging | |
| 7 | use Source | |
| 8 | use Trees | |
| 9 | ||
| 10 | class CONDITIONAL_COMPILATION: Visitor is | |
| 11 | _flags: Collections.MAP[string,bool] | |
| 12 | ||
| 13 | is_analysis: bool | |
| 14 | ||
| 15 | init() is | |
| 16 | super.init() | |
| 17 | ||
| 18 | _flags = Collections.MAP[string,bool]() | |
| 19 | si | |
| 20 | ||
| 21 | // The names `--define` turns on, and then the ones the build | |
| 22 | // decides for itself. The target comes after the defines so that | |
| 23 | // it always wins: a `--define dotnet` on a wasm build, or the | |
| 24 | // reverse, describes a build that is not the one being made. | |
| 25 | set_is_enabled(names: Iterable[string], profile: Semantic.TargetProfile) is | |
| 26 | for name in names do | |
| 27 | set_is_enabled(name, true) | |
| 28 | od | |
| 29 | ||
| 30 | set_is_enabled("legacy", false) | |
| 31 | ||
| 32 | for other in Semantic.TARGET_PROFILES.all do | |
| 33 | for condition in other.conditions do | |
| 34 | set_is_enabled(condition, false) | |
| 35 | od | |
| 36 | od | |
| 37 | ||
| 38 | for condition in profile.conditions do | |
| 39 | set_is_enabled(condition, true) | |
| 40 | od | |
| 41 | si | |
| 42 | ||
| 43 | set_is_enabled(name: string, value: bool) is | |
| 44 | _flags[name] = value | |
| 45 | si | |
| 46 | ||
| 47 | get_is_enabled(name: string mut) -> bool is | |
| 48 | let not_result mut = false | |
| 49 | let result mut = false | |
| 50 | ||
| 51 | if name.starts_with("not.") then | |
| 52 | name = name.substring(4) | |
| 53 | ||
| 54 | not_result = true | |
| 55 | fi | |
| 56 | ||
| 57 | if _flags.contains_key(name) then | |
| 58 | result = _flags[name] | |
| 59 | fi | |
| 60 | ||
| 61 | if not_result then | |
| 62 | return !result | |
| 63 | fi | |
| 64 | ||
| 65 | return result | |
| 66 | si | |
| 67 | ||
| 68 | get_is_enabled(pragma: Pragmas.PRAGMA) -> bool is | |
| 69 | let name = pragma.name.to_string() | |
| 70 | ||
| 71 | if !name.starts_with("IF.") then | |
| 72 | return true | |
| 73 | fi | |
| 74 | ||
| 75 | return get_is_enabled(name.substring(3)) | |
| 76 | si | |
| 77 | ||
| 78 | apply( | |
| 79 | node: Node | |
| 80 | ) is | |
| 81 | node.walk(self) | |
| 82 | si | |
| 83 | ||
| 84 | // A file-level `@@IF.<name>()` whose name is off leaves the whole | |
| 85 | // file out: its definitions and its top-level statements alike. | |
| 86 | pre(definitions: Definitions.LIST) -> bool is | |
| 87 | if let file_pragmas = definitions.file_pragmas then | |
| 88 | for pragma in file_pragmas do | |
| 89 | if !get_is_enabled(pragma) then | |
| 90 | definitions.clear_definitions() | |
| 91 | definitions.top_level_statements = null | |
| 92 | ||
| 93 | return true | |
| 94 | fi | |
| 95 | od | |
| 96 | fi | |
| 97 | ||
| 98 | return false | |
| 99 | si | |
| 100 | ||
| 101 | // FIXME: pragma handling code is duplicated across multiple visitors | |
| 102 | visit(pragma: Definitions.PRAGMA) is | |
| 103 | if !get_is_enabled(pragma.pragma) then | |
| 104 | pragma.definition = | |
| 105 | Definitions.LIST( | |
| 106 | pragma.definition.location, | |
| 107 | Collections.LIST[Definitions.Definition](0) | |
| 108 | ) | |
| 109 | fi | |
| 110 | si | |
| 111 | ||
| 112 | visit(pragma: Statements.PRAGMA) is | |
| 113 | if !get_is_enabled(pragma.pragma) then | |
| 114 | pragma.statement = null | |
| 115 | fi | |
| 116 | si | |
| 117 | si | |
| 118 | si |