Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use Logging | |
| 5 | ||
| 6 | class RESOLVE_ANCESTORS: ScopedVisitor is | |
| 7 | _logger: Logger | |
| 8 | _symbol_table: Semantic.SYMBOL_TABLE | |
| 9 | _innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup | |
| 10 | ||
| 11 | init( | |
| 12 | logger: Logger, | |
| 13 | symbol_table: Semantic.SYMBOL_TABLE, | |
| 14 | namespaces: Semantic.NAMESPACES, | |
| 15 | innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup | |
| 16 | ) | |
| 17 | is | |
| 18 | super.init(logger, symbol_table, namespaces) | |
| 19 | ||
| 20 | _logger = logger | |
| 21 | _symbol_table = symbol_table | |
| 22 | _innate_symbol_lookup = innate_symbol_lookup | |
| 23 | si | |
| 24 | ||
| 25 | apply(root: Trees.Node) is | |
| 26 | // The constructed built-ins take ancestry from what the | |
| 27 | // compilation declares - the array family from a declared | |
| 28 | // Collections.List - and the declarations exist by the time | |
| 29 | // this pass runs. | |
| 30 | _innate_symbol_lookup.wire_declared_ancestry() | |
| 31 | ||
| 32 | root.walk(self) | |
| 33 | si | |
| 34 | ||
| 35 | pre(`class: Trees.Definitions.CLASS) -> bool => true | |
| 36 | ||
| 37 | visit(`class: Trees.Definitions.CLASS) is | |
| 38 | let class_symbol = cast Semantic.Symbols.CLASS?(scope_for(`class))! | |
| 39 | ||
| 40 | let seen_class_ancestor mut = false | |
| 41 | let is_first mut = true | |
| 42 | ||
| 43 | if `class.ancestors? then | |
| 44 | for a in `class.ancestors do | |
| 45 | let ancestor_type = a.type | |
| 46 | ||
| 47 | if !a.is_poisoned /\ ancestor_type? /\ !ancestor_type.is_error then | |
| 48 | if ancestor_type.is_inheritable then | |
| 49 | if ancestor_type.is_class then | |
| 50 | if seen_class_ancestor then | |
| 51 | _logger.error(a.location, "multiple superclasses") | |
| 52 | elif !is_first then | |
| 53 | _logger.error(a.location, "superclass must be first") | |
| 54 | fi | |
| 55 | ||
| 56 | _check_extension_of_closed_imported(a, ancestor_type, "extend") | |
| 57 | ||
| 58 | seen_class_ancestor = true | |
| 59 | else | |
| 60 | _check_extension_of_closed_imported(a, ancestor_type, "implement") | |
| 61 | fi | |
| 62 | ||
| 63 | a.check_is_not_void(_logger, "cannot use void type here") | |
| 64 | ||
| 65 | class_symbol.add_ancestor(ancestor_type) | |
| 66 | ||
| 67 | is_first = false | |
| 68 | else | |
| 69 | _logger.error(a.location, "cannot inherit from this") | |
| 70 | fi | |
| 71 | fi | |
| 72 | od | |
| 73 | fi | |
| 74 | ||
| 75 | if !seen_class_ancestor then | |
| 76 | let object_type = _innate_symbol_lookup.get_object_type() | |
| 77 | ||
| 78 | if class_symbol != object_type.symbol then | |
| 79 | class_symbol.push_ancestor(_innate_symbol_lookup.get_object_type()) | |
| 80 | fi | |
| 81 | fi | |
| 82 | si | |
| 83 | ||
| 84 | // A class or trait carrying the CLOSED_ATTRIBUTE marker is closed | |
| 85 | // to the assembly it was declared in. Extending an imported one | |
| 86 | // from here would break the closure its own assembly relies on - | |
| 87 | // for a class, the enumerability of its subclass set that | |
| 88 | // narrowing rests on; for a trait, the author's decision not to | |
| 89 | // hand out an extension point. | |
| 90 | _check_extension_of_closed_imported( | |
| 91 | ancestor_node: Trees.TypeExpressions.TypeExpression, | |
| 92 | ancestor_type: Semantic.Types.Type?, | |
| 93 | verb: string | |
| 94 | ) is | |
| 95 | if !ancestor_type? then | |
| 96 | return | |
| 97 | fi | |
| 98 | ||
| 99 | let ancestor_symbol = ancestor_type.symbol | |
| 100 | ||
| 101 | if !isa Semantic.Symbols.Classy(ancestor_symbol) then | |
| 102 | return | |
| 103 | fi | |
| 104 | ||
| 105 | let ancestor_classy = cast Semantic.Symbols.Classy(ancestor_symbol) | |
| 106 | ||
| 107 | if !ancestor_classy.is_reflected then | |
| 108 | return | |
| 109 | fi | |
| 110 | ||
| 111 | if ancestor_classy.is_open then | |
| 112 | return | |
| 113 | fi | |
| 114 | ||
| 115 | // An earlier cell of this session is the same program as | |
| 116 | // far as the user is concerned: they wrote it a moment ago | |
| 117 | // and are extending it now. Closure is what makes a | |
| 118 | // session's exhaustive case and else-edge narrowing work at | |
| 119 | // all, so the two are reconciled by letting a later cell | |
| 120 | // through rather than by leaving every hierarchy open. What | |
| 121 | // that costs an earlier cell's compiled code is paid for by | |
| 122 | // the guards a submission build emits. | |
| 123 | @IF.dotnet() | |
| 124 | if IoC.CONTAINER.instance.assemblies.is_session_cell_named(ancestor_classy.il_assembly_name) then | |
| 125 | return | |
| 126 | fi | |
| 127 | ||
| 128 | let kind = if ancestor_classy.is_trait then "trait" else "class" fi | |
| 129 | ||
| 130 | _logger.error( | |
| 131 | ancestor_node.location, | |
| 132 | "cannot {verb} closed {kind} {ancestor_classy.name} from outside its assembly" | |
| 133 | ) | |
| 134 | si | |
| 135 | ||
| 136 | // An `impl <Interface> for <Target>` block attaches the interface to | |
| 137 | // the target's own symbol - this node's scope is an injection scope | |
| 138 | // standing in for the target, which class_or_trait_for unwraps. The | |
| 139 | // target then implements the interface exactly as a header-declared | |
| 140 | // one would; the members filling its slots are declared as the | |
| 141 | // target's own. | |
| 142 | pre(`impl: Trees.Definitions.IMPL) -> bool => true | |
| 143 | ||
| 144 | visit(`impl: Trees.Definitions.IMPL) is | |
| 145 | let target_symbol = class_or_trait_for(`impl) | |
| 146 | ||
| 147 | if !target_symbol? then | |
| 148 | return | |
| 149 | fi | |
| 150 | ||
| 151 | if `impl.ancestors? then | |
| 152 | for a in `impl.ancestors do | |
| 153 | let ancestor_type = a.type | |
| 154 | ||
| 155 | if ancestor_type?.is_error ?? false then | |
| 156 | continue | |
| 157 | fi | |
| 158 | ||
| 159 | if ancestor_type? then | |
| 160 | if isa Semantic.Types.NAMED(ancestor_type) then | |
| 161 | let ancestor_named_type = ancestor_type | |
| 162 | ||
| 163 | if ancestor_named_type.symbol.is_trait then | |
| 164 | _check_extension_of_closed_imported(a, ancestor_named_type, "implement") | |
| 165 | ||
| 166 | target_symbol.add_ancestor(ancestor_named_type) | |
| 167 | ||
| 168 | a.check_is_not_void(_logger, "cannot use void type here") | |
| 169 | ||
| 170 | continue | |
| 171 | fi | |
| 172 | fi | |
| 173 | ||
| 174 | _logger.error(a.location, "impl can only implement a trait") | |
| 175 | fi | |
| 176 | od | |
| 177 | fi | |
| 178 | si | |
| 179 | ||
| 180 | pre(`trait: Trees.Definitions.TRAIT) -> bool => true | |
| 181 | ||
| 182 | visit(`trait: Trees.Definitions.TRAIT) is | |
| 183 | let trait_symbol = cast Semantic.Symbols.TRAIT?(scope_for(`trait))! | |
| 184 | ||
| 185 | let seen_valid_ancestor mut = false | |
| 186 | ||
| 187 | if `trait.ancestors? then | |
| 188 | for a in `trait.ancestors do | |
| 189 | let ancestor_type = a.type | |
| 190 | ||
| 191 | if ancestor_type?.is_error ?? false then | |
| 192 | continue | |
| 193 | fi | |
| 194 | ||
| 195 | if ancestor_type? then | |
| 196 | if isa Semantic.Types.NAMED(ancestor_type) then | |
| 197 | let ancestor_named_type = ancestor_type | |
| 198 | ||
| 199 | if ancestor_named_type.symbol.is_trait then | |
| 200 | _check_extension_of_closed_imported(a, ancestor_named_type, "extend") | |
| 201 | ||
| 202 | trait_symbol.add_ancestor(ancestor_named_type) | |
| 203 | seen_valid_ancestor = true | |
| 204 | ||
| 205 | a.check_is_not_void(_logger, "cannot use void type here") | |
| 206 | else | |
| 207 | _logger.error(a.location, "trait cannot inherit from class") | |
| 208 | fi | |
| 209 | else | |
| 210 | _logger.error(a.location, "cannot inherit from this") | |
| 211 | fi | |
| 212 | else | |
| 213 | Std.error.write_line("refusing to add ancestor with null type {a} to trait {trait_symbol}") | |
| 214 | fi | |
| 215 | od | |
| 216 | fi | |
| 217 | ||
| 218 | let object_type = _innate_symbol_lookup.get_object_type() | |
| 219 | ||
| 220 | trait_symbol.push_ancestor(object_type) | |
| 221 | si | |
| 222 | ||
| 223 | pre(`struct: Trees.Definitions.STRUCT) -> bool => true | |
| 224 | ||
| 225 | visit(`struct: Trees.Definitions.STRUCT) is | |
| 226 | let struct_symbol = cast Semantic.Symbols.STRUCT?(scope_for(`struct))! | |
| 227 | ||
| 228 | if `struct.ancestors? then | |
| 229 | for a in `struct.ancestors do | |
| 230 | let ancestor_type = a.type | |
| 231 | ||
| 232 | if ancestor_type?.is_error ?? false then | |
| 233 | continue | |
| 234 | fi | |
| 235 | ||
| 236 | if ancestor_type? then | |
| 237 | if isa Semantic.Types.NAMED(ancestor_type) then | |
| 238 | let ancestor_named_type = ancestor_type | |
| 239 | ||
| 240 | if ancestor_named_type.symbol.is_trait then | |
| 241 | _check_extension_of_closed_imported(a, ancestor_named_type, "implement") | |
| 242 | ||
| 243 | struct_symbol.add_ancestor(ancestor_named_type) | |
| 244 | ||
| 245 | a.check_is_not_void(_logger, "cannot use void type here") | |
| 246 | ||
| 247 | continue | |
| 248 | fi | |
| 249 | fi | |
| 250 | ||
| 251 | _logger.error(a.location, "structs can only inherit from traits") | |
| 252 | fi | |
| 253 | od | |
| 254 | fi | |
| 255 | ||
| 256 | struct_symbol.push_ancestor(_innate_symbol_lookup.get_value_type()) | |
| 257 | si | |
| 258 | ||
| 259 | pre(`union: Trees.Definitions.UNION) -> bool => super.pre(`union) | |
| 260 | ||
| 261 | visit(`union: Trees.Definitions.UNION) is | |
| 262 | let union_symbol = cast Semantic.Symbols.UNION?(scope_for(`union))! | |
| 263 | ||
| 264 | if `union.ancestors? then | |
| 265 | for a in `union.ancestors do | |
| 266 | let ancestor_type = a.type | |
| 267 | ||
| 268 | if ancestor_type?.is_error ?? false then | |
| 269 | continue | |
| 270 | fi | |
| 271 | ||
| 272 | if ancestor_type? then | |
| 273 | if isa Semantic.Types.NAMED(ancestor_type) then | |
| 274 | let ancestor_named_type = ancestor_type | |
| 275 | ||
| 276 | if ancestor_named_type.symbol.is_trait then | |
| 277 | _check_extension_of_closed_imported(a, ancestor_named_type, "implement") | |
| 278 | ||
| 279 | union_symbol.add_ancestor(ancestor_named_type) | |
| 280 | ||
| 281 | a.check_is_not_void(_logger, "cannot use void type here") | |
| 282 | ||
| 283 | continue | |
| 284 | fi | |
| 285 | fi | |
| 286 | ||
| 287 | _logger.error(a.location, "unions can only inherit from traits") | |
| 288 | fi | |
| 289 | od | |
| 290 | fi | |
| 291 | ||
| 292 | union_symbol.push_ancestor(_innate_symbol_lookup.get_object_type()) | |
| 293 | ||
| 294 | super.visit(`union) | |
| 295 | si | |
| 296 | ||
| 297 | pre(`variant: Trees.Definitions.VARIANT) -> bool => true | |
| 298 | ||
| 299 | visit(`variant: Trees.Definitions.VARIANT) is | |
| 300 | let variant_symbol = cast Semantic.Symbols.VARIANT?(scope_for(`variant))! | |
| 301 | // a variant is always resolved inside its union's scope | |
| 302 | let union_symbol = current_union_context! | |
| 303 | let arguments = Collections.LIST[Semantic.Types.Type]() | |
| 304 | ||
| 305 | let union_type = | |
| 306 | if let union_symbol.argument_names? /\ argument_names.count > 0 then | |
| 307 | for argument_name in argument_names do | |
| 308 | let argument_symbol = variant_symbol.find_direct(argument_name) | |
| 309 | let argument_symbol_type = if argument_symbol? then argument_symbol.type else null fi | |
| 310 | ||
| 311 | if argument_symbol_type? then | |
| 312 | arguments.add(argument_symbol_type) | |
| 313 | else | |
| 314 | arguments.add(Semantic.Types.ERROR()) | |
| 315 | fi | |
| 316 | od | |
| 317 | ||
| 318 | Semantic.Types.GENERIC( | |
| 319 | Source.LOCATION.internal, | |
| 320 | union_symbol, | |
| 321 | arguments) | |
| 322 | else | |
| 323 | Semantic.Types.NAMED( | |
| 324 | union_symbol) | |
| 325 | fi | |
| 326 | ||
| 327 | variant_symbol.push_ancestor(union_type) | |
| 328 | si | |
| 329 | ||
| 330 | pre(`enum: Trees.Definitions.ENUM) -> bool => true | |
| 331 | ||
| 332 | visit(`enum: Trees.Definitions.ENUM) is | |
| 333 | let enum_symbol = cast Semantic.Symbols.ENUM_STRUCT?(scope_for(`enum))! | |
| 334 | ||
| 335 | let enum_type = _innate_symbol_lookup.get_enum_type() | |
| 336 | ||
| 337 | if !enum_type.is_error then | |
| 338 | enum_symbol.push_ancestor(enum_type) | |
| 339 | fi | |
| 340 | si | |
| 341 | si | |
| 342 | si |