Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Logging | |
| 3 | use Source | |
| 4 | use Trees | |
| 5 | ||
| 6 | // Declares everything below type level: functions, properties, fields, | |
| 7 | // enum members, and the block scopes, locals and closures inside | |
| 8 | // bodies. Runs after resolve-uses - the declare-symbols pass declares | |
| 9 | // only the type-level skeleton (namespaces, types, variants, type | |
| 10 | // parameters), so by the time members are declared every import is | |
| 11 | // bound and an impl/partial block's target name can be reached through | |
| 12 | // a use import and declared anywhere. | |
| 13 | // | |
| 14 | // The inherited ScopedVisitor handlers enter the scopes declare-symbols | |
| 15 | // associated with the type-level nodes; the overrides here declare the | |
| 16 | // member-level symbols into them. impl and partial blocks are handled | |
| 17 | // in place: the target resolves against the block's write site, and the | |
| 18 | // block's members are declared into it through an injection scope. | |
| 19 | class DECLARE_MEMBERS: ScopedVisitor is | |
| 20 | _logger: Logger | |
| 21 | _symbol_definition_listener: Semantic.SymbolDefinitionListener | |
| 22 | ||
| 23 | // Labels seen since the last loop body scope was opened; the | |
| 24 | // LABELLED pre pushes and the wrapped loop's pre declares into | |
| 25 | // that scope (see declare_members_bodies.ghul). | |
| 26 | _pending_labels: Collections.LIST[Trees.Identifiers.Identifier] | |
| 27 | _symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS | |
| 28 | _local_id_generator: IR.LOCAL_ID_GENERATOR | |
| 29 | ||
| 30 | _anon_index: int | |
| 31 | _pragma_scope_stack: PRAGMA_SCOPE_STACK | |
| 32 | _generic_argument_declarer: GENERIC_ARGUMENT_DECLARER | |
| 33 | ||
| 34 | init( | |
| 35 | logger: Logger, | |
| 36 | symbol_table: Semantic.SYMBOL_TABLE, | |
| 37 | namespaces: Semantic.NAMESPACES, | |
| 38 | symbol_definition_listener: Semantic.SymbolDefinitionListener, | |
| 39 | symbol_use_locations: Semantic.SYMBOL_USE_LOCATIONS, | |
| 40 | local_id_generator: IR.LOCAL_ID_GENERATOR | |
| 41 | ) is | |
| 42 | super.init(logger, symbol_table, namespaces) | |
| 43 | ||
| 44 | _logger = logger | |
| 45 | _symbol_definition_listener = symbol_definition_listener | |
| 46 | _symbol_use_locations = symbol_use_locations | |
| 47 | _local_id_generator = local_id_generator | |
| 48 | ||
| 49 | _pragma_scope_stack = PRAGMA_SCOPE_STACK() | |
| 50 | _generic_argument_declarer = GENERIC_ARGUMENT_DECLARER(logger, symbol_table, symbol_definition_listener) | |
| 51 | ||
| 52 | _pending_labels = Collections.LIST[Trees.Identifiers.Identifier]() | |
| 53 | si | |
| 54 | ||
| 55 | apply(node: Node) is | |
| 56 | assert _pragma_scope_stack.is_balanced | |
| 57 | ||
| 58 | node.walk(self) | |
| 59 | ||
| 60 | assert _pragma_scope_stack.is_balanced | |
| 61 | si | |
| 62 | ||
| 63 | next_anon_name() -> string is | |
| 64 | let result = "$anon_{_anon_index}" | |
| 65 | _anon_index = _anon_index + 1 | |
| 66 | return result | |
| 67 | si | |
| 68 | ||
| 69 | // `_` is a discard placeholder: every occurrence | |
| 70 | // gets its own unique slot so the redefinition check doesn't | |
| 71 | // fire and there's no shared name for code to read from. Used | |
| 72 | // in `let _ = expr`, tuple destructure (`let (_, _, z) = ...`), | |
| 73 | // and lambda parameters. | |
| 74 | next_discard_name() -> string is | |
| 75 | let result = "$discard_{_anon_index}" | |
| 76 | _anon_index = _anon_index + 1 | |
| 77 | return result | |
| 78 | si | |
| 79 | ||
| 80 | // Name of the single physical parameter slot backing a | |
| 81 | // destructured formal argument - the parameter itself has no | |
| 82 | // user-written name, only its leaves do. | |
| 83 | next_argument_group_name() -> string is | |
| 84 | let result = "$arg_{_anon_index}" | |
| 85 | _anon_index = _anon_index + 1 | |
| 86 | return result | |
| 87 | si | |
| 88 | ||
| 89 | pre(pragma: Definitions.PRAGMA) -> bool is | |
| 90 | _pragma_scope_stack.enter(pragma.pragma) | |
| 91 | ||
| 92 | return false | |
| 93 | si | |
| 94 | ||
| 95 | visit(pragma: Definitions.PRAGMA) is | |
| 96 | let p = pragma.pragma | |
| 97 | ||
| 98 | let name = p.name.to_string() | |
| 99 | ||
| 100 | if IL_PRAGMAS.is_name_pragma(name) then | |
| 101 | if p.arguments.expressions.count != 1 then | |
| 102 | _logger.error(p.arguments.location, "expected one argument") | |
| 103 | return | |
| 104 | fi | |
| 105 | ||
| 106 | let argument = p.arguments.expressions[0] | |
| 107 | ||
| 108 | if !isa Expressions.Literals.STRING(argument) then | |
| 109 | _logger.error(p.arguments.location, "expected a string literal argument") | |
| 110 | return | |
| 111 | fi | |
| 112 | ||
| 113 | let il_name = argument.value_string | |
| 114 | ||
| 115 | // A name reaches metadata as written. Quoting was once | |
| 116 | // how a name colliding with an IL keyword was spelled; | |
| 117 | // now it would just become part of the name. | |
| 118 | if il_name.index_of('\'') >= 0 then | |
| 119 | _logger.error( | |
| 120 | p.arguments.location, "an IL name cannot contain a quote") | |
| 121 | ||
| 122 | return | |
| 123 | fi | |
| 124 | ||
| 125 | let definition mut = pragma.definition | |
| 126 | ||
| 127 | while isa Definitions.PRAGMA(definition) do | |
| 128 | definition = cast Definitions.PRAGMA(definition).definition | |
| 129 | od | |
| 130 | ||
| 131 | let symbol = symbol_for(definition) | |
| 132 | ||
| 133 | if symbol? then | |
| 134 | if isa Semantic.Symbols.Property(symbol) then | |
| 135 | let property = symbol | |
| 136 | ||
| 137 | if name =~ IL_PRAGMAS.NAME_READ then | |
| 138 | property.read_function_il_name_override = il_name | |
| 139 | elif name =~ IL_PRAGMAS.NAME_ASSIGN then | |
| 140 | property.assign_function_il_name_override = il_name | |
| 141 | elif name =~ IL_PRAGMAS.NAME then | |
| 142 | property.il_name_override = il_name | |
| 143 | ||
| 144 | if !property.read_function_il_name_override? then | |
| 145 | property.read_function_il_name_override = "get_{il_name}" | |
| 146 | fi | |
| 147 | ||
| 148 | if !property.assign_function_il_name_override? then | |
| 149 | property.assign_function_il_name_override = "set_{il_name}" | |
| 150 | fi | |
| 151 | fi | |
| 152 | ||
| 153 | return | |
| 154 | fi | |
| 155 | ||
| 156 | symbol.il_name_override = il_name | |
| 157 | fi | |
| 158 | else | |
| 159 | _pragma_scope_stack.leave(p) | |
| 160 | fi | |
| 161 | si | |
| 162 | ||
| 163 | visit(`union: Definitions.UNION) is | |
| 164 | // Pick the default variant now that every variant's fields | |
| 165 | // have been declared: prefer one with the explicit-default | |
| 166 | // flag; otherwise fall back to a sole variant whose own | |
| 167 | // (non-inherited) field count is positive. | |
| 168 | // Cross-assembly unions don't pass through here; they | |
| 169 | // get `default_variant` set by | |
| 170 | // `SYMBOL_FACTORY.materialize_variant` when it sees | |
| 171 | // the `DEFAULT_VARIANT_ATTRIBUTE` marker on a reflected | |
| 172 | // variant. | |
| 173 | let union_symbol: Semantic.Symbols.UNION? mut = null | |
| 174 | if isa Semantic.Symbols.UNION(current_scope) then | |
| 175 | union_symbol = cast Semantic.Symbols.UNION(current_scope) | |
| 176 | fi | |
| 177 | ||
| 178 | if union_symbol? then | |
| 179 | let explicit: Semantic.Symbols.VARIANT? mut = null | |
| 180 | let explicit_count mut = 0 | |
| 181 | let sole_with_own_fields: Semantic.Symbols.VARIANT? mut = null | |
| 182 | let with_own_fields_count mut = 0 | |
| 183 | ||
| 184 | for s in union_symbol.symbols do | |
| 185 | if isa Semantic.Symbols.VARIANT(s) then | |
| 186 | let v = cast Semantic.Symbols.VARIANT(s) | |
| 187 | ||
| 188 | if v.is_default then | |
| 189 | explicit = v | |
| 190 | explicit_count = explicit_count + 1 | |
| 191 | fi | |
| 192 | ||
| 193 | if v.own_field_count > 0 then | |
| 194 | sole_with_own_fields = v | |
| 195 | with_own_fields_count = with_own_fields_count + 1 | |
| 196 | fi | |
| 197 | fi | |
| 198 | od | |
| 199 | ||
| 200 | if explicit_count == 1 then | |
| 201 | union_symbol.default_variant = explicit | |
| 202 | elif explicit_count == 0 /\ with_own_fields_count == 1 then | |
| 203 | union_symbol.default_variant = sole_with_own_fields | |
| 204 | fi | |
| 205 | fi | |
| 206 | ||
| 207 | leave_scope(`union) | |
| 208 | si | |
| 209 | ||
| 210 | pre(variant: Definitions.VARIANT) -> bool is | |
| 211 | enter_scope(variant) | |
| 212 | ||
| 213 | // Take charge of the walk so inherited-primary names and | |
| 214 | // own-field declarations register on the variant scope | |
| 215 | // interleaved in source position — positional destructure | |
| 216 | // and field-description rendering both consult | |
| 217 | // `_field_names` by index, so the order must match the | |
| 218 | // synthesised init's argument order (which is taken from | |
| 219 | // `variant.fields` in source order). Variables.VARIABLE.walk | |
| 220 | // still skips inherited entries, so own fields go through | |
| 221 | // the normal visit(VARIABLE) → declare_variable path and | |
| 222 | // inherited entries are registered here directly. | |
| 223 | let variant_scope: Semantic.Symbols.VARIANT? mut = null | |
| 224 | if isa Semantic.Symbols.VARIANT(current_declaration_context) then | |
| 225 | variant_scope = cast Semantic.Symbols.VARIANT?(current_declaration_context)! | |
| 226 | fi | |
| 227 | ||
| 228 | for f in variant.fields do | |
| 229 | if f.is_inherited_primary then | |
| 230 | if variant_scope? then | |
| 231 | if let f.name?, field_name = name.name then | |
| 232 | variant_scope.register_inherited_primary_field_name(field_name) | |
| 233 | fi | |
| 234 | fi | |
| 235 | else | |
| 236 | f.walk(self) | |
| 237 | fi | |
| 238 | od | |
| 239 | ||
| 240 | variant.body.walk(self) | |
| 241 | ||
| 242 | return true | |
| 243 | si | |
| 244 | ||
| 245 | // An impl/partial block declares its members into an | |
| 246 | // already-declared target type. The target resolves here, at the | |
| 247 | // block's write site - after resolve-uses, so the name can come | |
| 248 | // through a use import and the target can be declared anywhere. | |
| 249 | // The block gets an injection scope rather than the target's | |
| 250 | // scope directly, so that names its members reference resolve at | |
| 251 | // the write site while the target's members and type parameters | |
| 252 | // stay visible first; the members are then declared by this | |
| 253 | // visitor's ordinary member handlers. | |
| 254 | pre(`partial: Definitions.PARTIAL) -> bool is | |
| 255 | _enter_injection( | |
| 256 | `partial, | |
| 257 | "cannot find type {`partial.name.name} to add members to", | |
| 258 | "cannot add members to imported type" | |
| 259 | ) | |
| 260 | ||
| 261 | `partial.body.walk(self) | |
| 262 | ||
| 263 | return true | |
| 264 | si | |
| 265 | ||
| 266 | pre(`impl: Definitions.IMPL) -> bool is | |
| 267 | _enter_injection( | |
| 268 | `impl, | |
| 269 | "cannot find type {`impl.name.name} to implement an interface for", | |
| 270 | "cannot implement an interface for imported type" | |
| 271 | ) | |
| 272 | ||
| 273 | `impl.body.walk(self) | |
| 274 | ||
| 275 | return true | |
| 276 | si | |
| 277 | ||
| 278 | _enter_injection(block: Definitions.Classy, cannot_find_message: string, imported_target_message: string) is | |
| 279 | let target = cast Semantic.Symbols.Classy?(find_enclosing(block.name)) | |
| 280 | ||
| 281 | if !target? then | |
| 282 | _logger.error(block.name.location, cannot_find_message) | |
| 283 | associate_and_enter_scope(block, _rejecting_scope()) | |
| 284 | elif target.is_reflected then | |
| 285 | // The CLR fixes a compiled type's members and interfaces at | |
| 286 | // its definition, so only a type declared in this compilation | |
| 287 | // can be reopened. | |
| 288 | _logger.error(block.name.location, "{imported_target_message} {target.name}") | |
| 289 | associate_and_enter_scope(block, _rejecting_scope()) | |
| 290 | else | |
| 291 | // The target name is a reference to the target type, so | |
| 292 | // record it: hover, rename and find-references all read | |
| 293 | // these recorded uses. | |
| 294 | _symbol_use_locations.add_symbol_use(block.name.right_location, cast Semantic.Symbols.Symbol(target)) | |
| 295 | ||
| 296 | if let block.name.qualifier? /\ isa Semantic.Symbols.Classy(target.owner) then | |
| 297 | _symbol_use_locations.add_symbol_use( | |
| 298 | qualifier.right_location, | |
| 299 | cast Semantic.Symbols.Symbol(target.owner) | |
| 300 | ) | |
| 301 | fi | |
| 302 | ||
| 303 | associate_and_enter_scope(block, Semantic.INJECTION_SCOPE(target, current_scope)) | |
| 304 | fi | |
| 305 | si | |
| 306 | ||
| 307 | // When a block's target cannot be resolved to a same-assembly type, | |
| 308 | // its members have nowhere valid to live. Route them into a scope | |
| 309 | // that refuses them without another error each, since the target | |
| 310 | // has already been reported, rather than cascading to "no scope | |
| 311 | // found" downstream or declaring them as globals in the enclosing | |
| 312 | // scope. A block scope | |
| 313 | // still chains name lookups to the enclosing scope, so the block's | |
| 314 | // own header references - an impl's interface name - resolve | |
| 315 | // normally, as do the type names in each rejected member's own | |
| 316 | // header. | |
| 317 | _rejecting_scope() -> Semantic.Scope => | |
| 318 | Semantic.REJECTED_BLOCK_SCOPE(current_scope) | |
| 319 | ||
| 320 | pre(enum_member: Definitions.ENUM_MEMBER) -> bool is | |
| 321 | let value: string? mut = null | |
| 322 | ||
| 323 | if enum_member.initializer? then | |
| 324 | let i = enum_member.initializer | |
| 325 | ||
| 326 | // Any other literal reaches metadata as a Constant row | |
| 327 | // the enum's underlying type cannot hold, so it is | |
| 328 | // rejected here rather than emitted. A NONE stands for | |
| 329 | // an initializer the parser could not read, which it | |
| 330 | // has already reported. | |
| 331 | if isa Trees.Expressions.Literals.INTEGER(i) then | |
| 332 | value = i.value_string | |
| 333 | elif !isa Trees.Expressions.Literals.NONE(i) then | |
| 334 | _logger.error(i.location, "enum member initializer must be an integer literal") | |
| 335 | fi | |
| 336 | fi | |
| 337 | ||
| 338 | current_declaration_context.declare_enum_member( | |
| 339 | enum_member.name.location, | |
| 340 | enum_member.name.name, | |
| 341 | value, | |
| 342 | _symbol_definition_listener | |
| 343 | ) | |
| 344 | ||
| 345 | if enum_member.doc? then | |
| 346 | current_scope.find_direct(enum_member.name.name)?.set_doc(enum_member.doc) | |
| 347 | fi | |
| 348 | return false | |
| 349 | si | |
| 350 | ||
| 351 | pre(function: Definitions.FUNCTION) -> bool is | |
| 352 | // parse recovery can leave the declaration nameless; | |
| 353 | // there is nothing to declare for it | |
| 354 | let name = function.name | |
| 355 | ||
| 356 | if !name? then | |
| 357 | return true | |
| 358 | fi | |
| 359 | ||
| 360 | let is_innate = function.body? /\ isa Trees.Bodies.INNATE(function.body) | |
| 361 | let is_static = function.modifiers.is_static | |
| 362 | ||
| 363 | if is_static /\ name.name =~ "init" /\ function.arguments.count > 0 then | |
| 364 | _logger.error(function.location, "a static constructor cannot take parameters") | |
| 365 | fi | |
| 366 | ||
| 367 | // An operator declared on a type takes its left operand as | |
| 368 | // `self`, so its argument list holds the right operand alone. | |
| 369 | // One declaring both operands type-checks where its name is | |
| 370 | // in lexical scope, but the call it resolves to passes the | |
| 371 | // right operand alone, so it can never be emitted. Declared | |
| 372 | // undefined rather than as half an operator later passes | |
| 373 | // would try to call. | |
| 374 | let is_invalid_operator = | |
| 375 | !is_static /\ | |
| 376 | current_instance_context? /\ | |
| 377 | Lexical.TOKENIZER.is_operator_name(name.name) /\ | |
| 378 | function.arguments.count > 1 | |
| 379 | ||
| 380 | if is_invalid_operator then | |
| 381 | _logger.error( | |
| 382 | function.location, | |
| 383 | "an operator declared on a type takes one argument; its left operand is self", | |
| 384 | function.location, | |
| 385 | "help: declare it static to take both operands as arguments") | |
| 386 | fi | |
| 387 | ||
| 388 | let is_private = function.modifiers.is_private | |
| 389 | ||
| 390 | // An underscore-prefixed method, or the accessor of an underscore- | |
| 391 | // prefixed property (whose own $get_/$set_ name is not underscore- | |
| 392 | // prefixed), is subject to the underscore access policy. | |
| 393 | let is_underscore_method = name.name.starts_with('_') \/ function.is_underscore_scoped | |
| 394 | ||
| 395 | let property: Semantic.Symbols.Property? mut = null | |
| 396 | ||
| 397 | _local_id_generator.enter_function() | |
| 398 | ||
| 399 | if function.for_property? then | |
| 400 | let symbol = symbol_for(function.for_property) | |
| 401 | ||
| 402 | assert symbol? else "function is accessor for property but no property found: {function.location}" | |
| 403 | assert isa Semantic.Symbols.Property(symbol) else "function is accessor for property but associated symbol is not a property: {function.location}" | |
| 404 | ||
| 405 | property = cast Semantic.Symbols.Property(symbol) | |
| 406 | fi | |
| 407 | ||
| 408 | if is_innate then | |
| 409 | let `innate = cast Trees.Bodies.INNATE?(function.body)! | |
| 410 | ||
| 411 | associate_and_enter_scope( | |
| 412 | function, | |
| 413 | current_declaration_context.declare_innate( | |
| 414 | function.location, | |
| 415 | name.name, | |
| 416 | `innate.name.to_string(), | |
| 417 | current_scope,_symbol_definition_listener | |
| 418 | ) | |
| 419 | ) | |
| 420 | else | |
| 421 | // Generator vs async detection. A body with `yield` | |
| 422 | // (not inside a nested lambda) becomes a generator; | |
| 423 | // a body with `await` (in this body, not in a nested | |
| 424 | // lambda) becomes an async function. | |
| 425 | // | |
| 426 | // Either-or only — a body with both is diagnosed | |
| 427 | // and falls back to generator classification. | |
| 428 | let has_body = function.body? /\ !function.body.is_null | |
| 429 | ||
| 430 | let body_has_yield = | |
| 431 | has_body /\ YIELD_SCANNER().body_has_yield(function.body!) | |
| 432 | ||
| 433 | let await_scanner = AWAIT_SCANNER() | |
| 434 | if has_body then | |
| 435 | await_scanner.scan(function.body!) | |
| 436 | fi | |
| 437 | ||
| 438 | let body_has_await = await_scanner.found | |
| 439 | ||
| 440 | function.contains_let_await = body_has_await | |
| 441 | function.is_void_async = body_has_await /\ !await_scanner.found_value_return | |
| 442 | ||
| 443 | if body_has_yield /\ body_has_await then | |
| 444 | _logger.error( | |
| 445 | function.location, | |
| 446 | "function cannot be both a generator and async" | |
| 447 | ) | |
| 448 | fi | |
| 449 | ||
| 450 | let is_generator = body_has_yield | |
| 451 | let is_async = body_has_await /\ !is_generator | |
| 452 | ||
| 453 | let symbol: Semantic.Symbols.Symbol mut | |
| 454 | ||
| 455 | if is_invalid_operator then | |
| 456 | // The rejecting scope below then keeps the header and | |
| 457 | // body resolvable without leaving a callable operator | |
| 458 | // for later passes to select. Built directly rather | |
| 459 | // than through `declare_undefined`, whose own "cannot | |
| 460 | // declare operator here" is not what is wrong here. | |
| 461 | symbol = Semantic.Symbols.UNDEFINED(name.location, current_scope, name.name) | |
| 462 | elif is_generator then | |
| 463 | symbol = current_declaration_context.declare_generator_function( | |
| 464 | name.location, | |
| 465 | function.location, | |
| 466 | name.name, | |
| 467 | is_static, | |
| 468 | is_private, | |
| 469 | has_body, | |
| 470 | current_scope, | |
| 471 | _symbol_definition_listener) | |
| 472 | elif is_async then | |
| 473 | symbol = current_declaration_context.declare_async_function( | |
| 474 | name.location, | |
| 475 | function.location, | |
| 476 | name.name, | |
| 477 | is_static, | |
| 478 | is_private, | |
| 479 | has_body, | |
| 480 | current_scope, | |
| 481 | _symbol_definition_listener) | |
| 482 | else | |
| 483 | symbol = current_declaration_context.declare_function( | |
| 484 | name.location, | |
| 485 | function.location, | |
| 486 | name.name, | |
| 487 | is_static, | |
| 488 | is_underscore_method, | |
| 489 | has_body, | |
| 490 | function.for_property?, | |
| 491 | current_scope, | |
| 492 | _symbol_definition_listener) | |
| 493 | fi | |
| 494 | ||
| 495 | // A rejected declaration has no function symbol to scope | |
| 496 | // its arguments and body against. Entering it anyway makes | |
| 497 | // every name in the header and body report as not found on | |
| 498 | // top of the rejection; a rejecting scope keeps them | |
| 499 | // resolvable. | |
| 500 | if isa Semantic.Symbols.UNDEFINED(symbol) then | |
| 501 | associate_and_enter_scope(function, _rejecting_scope()) | |
| 502 | else | |
| 503 | associate_and_enter_scope(function, symbol) | |
| 504 | fi | |
| 505 | fi | |
| 506 | ||
| 507 | let symbol = symbol_for(function) | |
| 508 | ||
| 509 | symbol?.set_doc(function.doc) | |
| 510 | ||
| 511 | if symbol? /\ isa Semantic.Symbols.Function(symbol) then | |
| 512 | let function_symbol = symbol | |
| 513 | ||
| 514 | let pure_owner = current_instance_context | |
| 515 | ||
| 516 | if function.modifiers.is_pure then | |
| 517 | function_symbol.mark_declared_pure() | |
| 518 | elif | |
| 519 | (!function.body? \/ function.body.is_null) /\ | |
| 520 | !function.modifiers.is_static /\ | |
| 521 | !function.is_assign_accessor /\ | |
| 522 | pure_owner? /\ | |
| 523 | pure_owner.is_pure | |
| 524 | then | |
| 525 | // A bodiless instance member of a pure type is a | |
| 526 | // contract rather than an implementation, so it | |
| 527 | // carries the declaration: implementors then inherit | |
| 528 | // the obligation through the pure-override contract. | |
| 529 | function_symbol.mark_declared_pure() | |
| 530 | fi | |
| 531 | ||
| 532 | if function.modifiers.is_stable then | |
| 533 | if function.for_property? then | |
| 534 | // The synthesized accessors copy the property's | |
| 535 | // modifier list; only the read accessor carries | |
| 536 | // the contract — the setter's copy is inert. | |
| 537 | if function.arguments.variables.count == 0 then | |
| 538 | function_symbol.mark_declared_stable() | |
| 539 | fi | |
| 540 | else | |
| 541 | _logger.error(function.modifiers.location, "stable is only valid on a property") | |
| 542 | fi | |
| 543 | fi | |
| 544 | ||
| 545 | if let operation = _pragma_scope_stack.intrinsic_operation ?? function.intrinsic_operation then | |
| 546 | function_symbol.intrinsic_operation = operation | |
| 547 | fi | |
| 548 | ||
| 549 | if function.is_top_level_entry then | |
| 550 | function_symbol.is_top_level_entry = true | |
| 551 | fi | |
| 552 | ||
| 553 | // Record the primary constructor on its owning type so a | |
| 554 | // hover on the declaration can show the primary parameters. | |
| 555 | if | |
| 556 | function.is_primary_constructor /\ | |
| 557 | isa Semantic.Symbols.Classy(function_symbol.owner) | |
| 558 | then | |
| 559 | (cast Semantic.Symbols.Classy?(function_symbol.owner)!).primary_constructor = function_symbol | |
| 560 | fi | |
| 561 | ||
| 562 | // Record on the owning type that it has its own | |
| 563 | // zero-argument constructor — needed early (before | |
| 564 | // constructor signatures are resolved) to check a | |
| 565 | // `init` type-parameter constraint. | |
| 566 | if | |
| 567 | name.name =~ "init" /\ | |
| 568 | function.arguments.variables.count == 0 /\ | |
| 569 | isa Semantic.Symbols.Classy(function_symbol.owner) | |
| 570 | then | |
| 571 | (cast Semantic.Symbols.Classy?(function_symbol.owner)!).has_parameterless_constructor = true | |
| 572 | fi | |
| 573 | ||
| 574 | // A user-written body-less instance method on a class | |
| 575 | // makes that class implicitly abstract — the user | |
| 576 | // meant the method as a contract for subclasses to | |
| 577 | // implement, and a bare instance would throw if the | |
| 578 | // method were ever invoked. Skip property accessors, | |
| 579 | // since a write-only property leaves the synthesised | |
| 580 | // getter body-less without the user intending the | |
| 581 | // class to be abstract; skip `init` (that's a | |
| 582 | // separate concern handled by primary-ctor rewriting) | |
| 583 | // and static methods (statics don't make instances | |
| 584 | // abstract). | |
| 585 | let body_is_null = | |
| 586 | !function.body? \/ function.body.is_null | |
| 587 | ||
| 588 | function_symbol.is_declared_without_body = body_is_null | |
| 589 | ||
| 590 | if | |
| 591 | body_is_null /\ | |
| 592 | !function.for_property? /\ | |
| 593 | !is_static /\ | |
| 594 | name.name !~ "init" /\ | |
| 595 | isa Semantic.Symbols.CLASS(function_symbol.owner) | |
| 596 | then | |
| 597 | (cast Semantic.Symbols.CLASS(function_symbol.owner)).mark_has_bodyless_method() | |
| 598 | fi | |
| 599 | ||
| 600 | if property? then | |
| 601 | function_symbol.mark_synthesized() | |
| 602 | ||
| 603 | if function.arguments.variables.count == 0 then | |
| 604 | property.read_function = function_symbol | |
| 605 | ||
| 606 | // an auto property's synthesized getter reads | |
| 607 | // the backing field and nothing else, so it is | |
| 608 | // store-free by construction | |
| 609 | if function.for_property? /\ function.for_property.is_auto_property then | |
| 610 | function_symbol.mark_backing_read() | |
| 611 | fi | |
| 612 | ||
| 613 | if property.read_function_il_name_override? then | |
| 614 | function_symbol.il_name_override = property.read_function_il_name_override | |
| 615 | fi | |
| 616 | else | |
| 617 | property.assign_function = function_symbol | |
| 618 | ||
| 619 | if property.assign_function_il_name_override? then | |
| 620 | function_symbol.il_name_override = property.assign_function_il_name_override | |
| 621 | fi | |
| 622 | fi | |
| 623 | fi | |
| 624 | ||
| 625 | if function.generic_arguments.count > 0 then | |
| 626 | let generic_arguments = _generic_argument_declarer.declare_generic_arguments(function.generic_arguments) | |
| 627 | ||
| 628 | function_symbol.generic_argument_names = generic_arguments.names | |
| 629 | function_symbol.generic_arguments = generic_arguments.types | |
| 630 | function_symbol.is_generic = true | |
| 631 | fi | |
| 632 | ||
| 633 | function_symbol.start_declaring_arguments() | |
| 634 | function.arguments.walk(self) | |
| 635 | function_symbol.end_declaring_arguments() | |
| 636 | elif isa Semantic.BLOCK_SCOPE(current_declaration_context) then | |
| 637 | // The declaration was rejected above, so there is no | |
| 638 | // function symbol to open a declaring-arguments window | |
| 639 | // against — but the parameters still need somewhere to | |
| 640 | // resolve, or every reference downstream reports as | |
| 641 | // missing on top of the rejection. Declare them into the | |
| 642 | // rejecting scope entered above instead. | |
| 643 | function.arguments.walk(self) | |
| 644 | fi | |
| 645 | ||
| 646 | if function.body? then | |
| 647 | function.body.walk(self) | |
| 648 | fi | |
| 649 | ||
| 650 | return true | |
| 651 | si | |
| 652 | ||
| 653 | visit(function: Definitions.FUNCTION) is | |
| 654 | _local_id_generator.leave_function() | |
| 655 | ||
| 656 | leave_scope(function) | |
| 657 | si | |
| 658 | ||
| 659 | // Declare just a function's body — its block scopes, locals and | |
| 660 | // closures — reusing the body-handling visit methods. The caller | |
| 661 | // must already have the cursor positioned at the function's | |
| 662 | // (retained) scope. Used by the incremental body re-walk, which | |
| 663 | // keeps the interface symbols and re-declares only the edited | |
| 664 | // bodies; the interface is never re-walked here, so the pass's | |
| 665 | // non-idempotency on declarations is not exercised. | |
| 666 | declare_body(function: Definitions.FUNCTION) is | |
| 667 | if !function.body? then | |
| 668 | return | |
| 669 | fi | |
| 670 | ||
| 671 | _local_id_generator.enter_function() | |
| 672 | ||
| 673 | function.body!.walk(self) | |
| 674 | ||
| 675 | _local_id_generator.leave_function() | |
| 676 | si | |
| 677 | ||
| 678 | pre(property: Definitions.PROPERTY) -> bool is | |
| 679 | // parse recovery can leave the declaration nameless; | |
| 680 | // there is nothing to declare for it | |
| 681 | let name = property.name | |
| 682 | ||
| 683 | if !name? then | |
| 684 | return true | |
| 685 | fi | |
| 686 | ||
| 687 | let is_static = property.modifiers.is_static | |
| 688 | let instance_context = current_instance_context | |
| 689 | ||
| 690 | if property.modifiers.is_field then | |
| 691 | // is_field implies the storage class modifier is present | |
| 692 | if !instance_context? \/ instance_context.is_trait then | |
| 693 | _logger.error(property.modifiers.storage_class!.location, "field is not valid here") | |
| 694 | ||
| 695 | // Only a trait recovers as a property. Outside any type | |
| 696 | // the declaration stays field-shaped and is declared as | |
| 697 | // a variable below: accessor synthesis has already run | |
| 698 | // and skipped it as a field, so clearing the storage | |
| 699 | // class here would declare a property with no accessors | |
| 700 | // and every read of it would fail with "does not have a | |
| 701 | // read function" - a crash on top of the error already | |
| 702 | // reported. | |
| 703 | if instance_context? then | |
| 704 | property.modifiers.clear_storage_class() | |
| 705 | fi | |
| 706 | elif property.read_body? \/ property.assign_body? \/ property.assign_argument? then | |
| 707 | _logger.error(property.modifiers.storage_class!.location, "field cannot have a body") | |
| 708 | fi | |
| 709 | fi | |
| 710 | ||
| 711 | if | |
| 712 | property.modifiers.is_field \/ ( | |
| 713 | name.name.starts_with('_') /\ | |
| 714 | !property.read_body? /\ | |
| 715 | !property.assign_body? | |
| 716 | ) | |
| 717 | then | |
| 718 | if property.modifiers.is_stable then | |
| 719 | _logger.error(property.modifiers.location, "stable is only valid on a property") | |
| 720 | fi | |
| 721 | ||
| 722 | let symbol = current_declaration_context.declare_variable(name.location, name.name, is_static, _symbol_definition_listener) | |
| 723 | ||
| 724 | symbol.set_doc(property.doc) | |
| 725 | ||
| 726 | associate_node_with_scope(property, symbol) | |
| 727 | ||
| 728 | else | |
| 729 | let owner_is_trait = instance_context? /\ instance_context.is_trait | |
| 730 | ||
| 731 | let is_assignable = !property.read_body? \/ property.assign_argument? | |
| 732 | let is_private = !property.modifiers.is_public /\ !owner_is_trait | |
| 733 | ||
| 734 | let symbol = | |
| 735 | current_declaration_context | |
| 736 | .declare_property( | |
| 737 | name.location, | |
| 738 | property.location, | |
| 739 | name.name, | |
| 740 | is_static, | |
| 741 | is_private, | |
| 742 | is_assignable, | |
| 743 | _symbol_definition_listener | |
| 744 | ) | |
| 745 | ||
| 746 | symbol.set_doc(property.doc) | |
| 747 | ||
| 748 | if property.read_body == null /\ property.assign_body == null then | |
| 749 | current_declaration_context.declare_variable( | |
| 750 | name.location, | |
| 751 | "${name.name}", | |
| 752 | is_static, | |
| 753 | _symbol_definition_listener) | |
| 754 | .mark_synthesized() | |
| 755 | fi | |
| 756 | ||
| 757 | // the rewriter that synthesized the accessors decided | |
| 758 | // auto-ness before any body existed; carry its answer | |
| 759 | // onto the symbol, where flow narrowing keys on it | |
| 760 | if property.is_auto_property /\ isa Semantic.Symbols.Property(symbol) then | |
| 761 | (cast Semantic.Symbols.Property(symbol)).is_auto = true | |
| 762 | fi | |
| 763 | ||
| 764 | // A property with a getter also marks the accessor in | |
| 765 | // the function path; this mark is the only carrier for | |
| 766 | // a body-less declaration (a trait requirement). | |
| 767 | if property.modifiers.is_stable /\ isa Semantic.Symbols.Property(symbol) then | |
| 768 | (cast Semantic.Symbols.Property(symbol)).mark_declared_stable() | |
| 769 | fi | |
| 770 | ||
| 771 | associate_node_with_scope(property, symbol) | |
| 772 | fi | |
| 773 | ||
| 774 | return true | |
| 775 | si | |
| 776 | ||
| 777 | pre(indexer: Definitions.INDEXER) -> bool is | |
| 778 | // ??? | |
| 779 | ||
| 780 | return true | |
| 781 | si | |
| 782 | si | |
| 783 | si |