Appearance
| 1 | namespace Semantic is | |
| 2 | use System.NotImplementedException | |
| 3 | ||
| 4 | use Logging | |
| 5 | ||
| 6 | use IR.Values | |
| 7 | use IR.VALUE_BOXER | |
| 8 | ||
| 9 | use Types.Type | |
| 10 | ||
| 11 | class SYMBOL_LOADER is | |
| 12 | _null_find_symbol: (string) -> Symbols.Symbol? static | |
| 13 | ||
| 14 | _logger: Logger | |
| 15 | _symbol_table: SYMBOL_TABLE | |
| 16 | _function_caller: FUNCTION_CALLER | |
| 17 | _value_boxer: IR.VALUE_BOXER | |
| 18 | ||
| 19 | _innate_symbol_lookup: Lookups.InnateSymbolLookup | |
| 20 | ||
| 21 | find_symbol: (string) -> Symbols.Symbol? public | |
| 22 | ||
| 23 | // A call through a null anon function does not always produce a | |
| 24 | // sane stack trace, so the unset state is a function that says so. | |
| 25 | init() static is | |
| 26 | _null_find_symbol = | |
| 27 | (name: string) -> Symbols.Symbol? is | |
| 28 | throw NotImplementedException("find_symbol is not set") | |
| 29 | si | |
| 30 | si | |
| 31 | ||
| 32 | init( | |
| 33 | logger: Logger, | |
| 34 | symbol_table: SYMBOL_TABLE, | |
| 35 | function_caller: FUNCTION_CALLER, | |
| 36 | value_boxer: VALUE_BOXER, | |
| 37 | innate_symbol_lookup: Lookups.InnateSymbolLookup | |
| 38 | ) is | |
| 39 | super.init() | |
| 40 | ||
| 41 | _logger = logger | |
| 42 | _symbol_table = symbol_table | |
| 43 | _function_caller = function_caller | |
| 44 | _value_boxer = value_boxer | |
| 45 | _innate_symbol_lookup = innate_symbol_lookup | |
| 46 | ||
| 47 | find_symbol = _null_find_symbol | |
| 48 | si | |
| 49 | ||
| 50 | load_self(location: Source.LOCATION) -> Value is | |
| 51 | let context = _symbol_table.current_function | |
| 52 | ||
| 53 | if context? then | |
| 54 | let result = context.load_self(location, self) | |
| 55 | ||
| 56 | return result | |
| 57 | fi | |
| 58 | ||
| 59 | IoC.CONTAINER.instance.logger.error(location, "cannot access instance member from non-instance context") | |
| 60 | ||
| 61 | return IR.Values.DUMMY(Types.ERROR(), Source.LOCATION.internal) | |
| 62 | si | |
| 63 | ||
| 64 | load_outer_self(location: Source.LOCATION) -> Value is | |
| 65 | let context = _symbol_table.current_function | |
| 66 | ||
| 67 | if context? then | |
| 68 | let result = context.load_outer_self(location, self) | |
| 69 | ||
| 70 | assert result? else "load outer self did not return a value: {context}" | |
| 71 | ||
| 72 | return result | |
| 73 | fi | |
| 74 | ||
| 75 | IoC.CONTAINER.instance.logger.error(location, "cannot access instance member from non-instance context") | |
| 76 | ||
| 77 | return IR.Values.DUMMY(Types.ERROR(), Source.LOCATION.internal) | |
| 78 | si | |
| 79 | ||
| 80 | load_namespace(symbol: Symbols.NAMESPACE) -> Value => | |
| 81 | Load.SYMBOL(null, symbol) | |
| 82 | ||
| 83 | load_class(symbol: Symbols.CLASS) -> Value => | |
| 84 | TYPE_EXPRESSION(symbol.type!, Source.LOCATION.internal) | |
| 85 | ||
| 86 | load_trait(symbol: Symbols.TRAIT) -> Value => | |
| 87 | TYPE_EXPRESSION(symbol.type!, Source.LOCATION.internal) | |
| 88 | ||
| 89 | load_struct(symbol: Symbols.STRUCT) -> Value => | |
| 90 | TYPE_EXPRESSION(symbol.type!, Source.LOCATION.internal) | |
| 91 | ||
| 92 | load_union(symbol: Symbols.UNION) -> Value => | |
| 93 | TYPE_EXPRESSION(symbol.type!, Source.LOCATION.internal) | |
| 94 | ||
| 95 | load_variant(symbol: Symbols.VARIANT) -> Value => | |
| 96 | TYPE_EXPRESSION(symbol.type!, Source.LOCATION.internal) | |
| 97 | ||
| 98 | load_enum_struct_member(symbol: Symbols.ENUM_STRUCT_MEMBER) -> Value => | |
| 99 | if let number = symbol.numeric_value then | |
| 100 | Symbols.ENUM_CONSTANT.from_number(symbol.type, number, _innate_symbol_lookup) | |
| 101 | else | |
| 102 | Symbols.ENUM_CONSTANT.from_pattern( | |
| 103 | symbol.type, | |
| 104 | _wide_enum_pattern(symbol.emitted_value), | |
| 105 | _innate_symbol_lookup) | |
| 106 | fi | |
| 107 | ||
| 108 | // A member of an enum wider than int32 has no int32 value to | |
| 109 | // read and arrives as the text it was declared with. What the | |
| 110 | // load takes is the bit pattern, so both signednesses are tried | |
| 111 | // before giving up: `[Flags] enum : uint` with the high bit set | |
| 112 | // reads as unsigned, a negative `enum : long` as signed. | |
| 113 | _wide_enum_pattern(text: string) -> ulong static is | |
| 114 | let unsigned: ulong mut = _ | |
| 115 | ||
| 116 | if ulong.try_parse(text, unsigned ref) then | |
| 117 | return unsigned | |
| 118 | fi | |
| 119 | ||
| 120 | let signed: long mut = _ | |
| 121 | ||
| 122 | if long.try_parse(text, signed ref) then | |
| 123 | return cast ulong(signed) | |
| 124 | fi | |
| 125 | ||
| 126 | return 0UL | |
| 127 | si | |
| 128 | ||
| 129 | load_instance_anonymous_function(symbol: Symbols.Symbol, func_type: Type) -> Value => | |
| 130 | Load.INSTANCE_ANONYMOUS_FUNCTION(symbol, func_type) | |
| 131 | ||
| 132 | load_static_anonymous_function(symbol: Symbols.Symbol, func_type: Type) -> Value => | |
| 133 | Load.STATIC_ANONYMOUS_FUNCTION(symbol, func_type) | |
| 134 | ||
| 135 | load_global_anonymous_function(symbol: Symbols.Symbol, func_type: Type) -> Value => | |
| 136 | Load.GLOBAL_ANONYMOUS_FUNCTION(symbol, func_type) | |
| 137 | ||
| 138 | load_function_group(from: Value?, symbol: Symbols.Symbol) -> Value => | |
| 139 | Load.SYMBOL(from, symbol) | |
| 140 | ||
| 141 | load_global_function(symbol: Symbols.Symbol) -> Value => | |
| 142 | Load.SYMBOL(null, symbol) | |
| 143 | ||
| 144 | load_instance_method(location: Source.LOCATION, from: Value?, symbol: Symbols.Symbol) -> Value | |
| 145 | => Load.SYMBOL(from, symbol) | |
| 146 | ||
| 147 | load_struct_method(location: Source.LOCATION, from: Value?, symbol: Symbols.Symbol) -> Value | |
| 148 | => Load.SYMBOL(from, symbol) | |
| 149 | ||
| 150 | // FIXME: this needs to create a pointer to the function: | |
| 151 | load_static_method(symbol: Symbols.Symbol) -> Value | |
| 152 | => Load.SYMBOL(null, symbol) | |
| 153 | ||
| 154 | load_local_variable(location: Source.LOCATION, symbol: Symbols.Variable) -> Value is | |
| 155 | let function = _symbol_table.current_function | |
| 156 | ||
| 157 | if let inlined = _load_inlined_parameter(location, symbol) then | |
| 158 | return inlined | |
| 159 | fi | |
| 160 | ||
| 161 | // A variable an enclosing literal being inlined does not own | |
| 162 | // is read the way the function the body is emitted into reads | |
| 163 | // it, since the inlined literal has no frame of its own. | |
| 164 | if let inlined = _load_through_inlined_method(location, symbol) then | |
| 165 | return inlined | |
| 166 | fi | |
| 167 | ||
| 168 | if function? /\ symbol.owner != function then | |
| 169 | let raw = function.load_captured_value(location, symbol, self) | |
| 170 | ||
| 171 | // Captured boxed locals: the frame field holds | |
| 172 | // BOX[T]; the user-code-side read wants T, so | |
| 173 | // unwrap via `.value`. Inter-frame transfers | |
| 174 | // (load_outer_captured_value chaining for | |
| 175 | // nested closures) call load_captured_value | |
| 176 | // directly and don't hit this unwrap — they | |
| 177 | // need the box reference to pass to the next | |
| 178 | // frame's constructor. | |
| 179 | if symbol.is_boxed then | |
| 180 | let value_member = resolve_box_value_member(symbol) | |
| 181 | ||
| 182 | if value_member? then | |
| 183 | return value_member.load(location, raw, self) | |
| 184 | fi | |
| 185 | fi | |
| 186 | ||
| 187 | return raw | |
| 188 | fi | |
| 189 | ||
| 190 | if symbol.is_boxed then | |
| 191 | return _load_boxed_local(location, symbol) | |
| 192 | fi | |
| 193 | ||
| 194 | return Load.LOCAL_VARIABLE(symbol) | |
| 195 | si | |
| 196 | ||
| 197 | load_outer_local_variable(location: Source.LOCATION, symbol: Symbols.Variable) -> Value is | |
| 198 | let function = _symbol_table.current_function | |
| 199 | ||
| 200 | if function? /\ symbol.owner != function then | |
| 201 | let result = function.load_outer_captured_value(location, symbol, self) | |
| 202 | ||
| 203 | assert result? else "load outer captured value did not return a value: {function} {symbol}" | |
| 204 | ||
| 205 | return result | |
| 206 | fi | |
| 207 | ||
| 208 | let raw = Load.LOCAL_VARIABLE(symbol) | |
| 209 | ||
| 210 | if symbol.is_boxed then | |
| 211 | // The slot value IS the BOX[T] reference; tell the | |
| 212 | // IR layer its type is BOX[T] rather than T (the | |
| 213 | // load IR derives its type from `symbol.type`). | |
| 214 | // Used by `closure.find_or_add_capture` -> frame | |
| 215 | // construction at closure-build time. | |
| 216 | return TYPE_WRAPPER(symbol.storage_type!, raw) | |
| 217 | fi | |
| 218 | ||
| 219 | return raw | |
| 220 | si | |
| 221 | ||
| 222 | store_local_variable(location: Source.LOCATION, symbol: Symbols.Variable, value: Value, is_initialize: bool) -> Value is | |
| 223 | // A local written to before the declaration that types it | |
| 224 | // has been compiled - an assignment inside the local's own | |
| 225 | // initializer, which the walk reaches first - has no type to | |
| 226 | // store through. The read that got there is already | |
| 227 | // reported, so this is the consequence of a diagnostic | |
| 228 | // rather than one of its own; report only where nothing | |
| 229 | // else has, so that no store reaches code generation | |
| 230 | // unexplained. | |
| 231 | if !symbol.type? then | |
| 232 | if IoC.CONTAINER.instance.logger.is_clean then | |
| 233 | IoC.CONTAINER.instance.logger.error(location, "cannot assign to {symbol.name} here") | |
| 234 | fi | |
| 235 | ||
| 236 | return DUMMY(Types.ERROR(), location) | |
| 237 | fi | |
| 238 | ||
| 239 | let function = _symbol_table.current_function | |
| 240 | ||
| 241 | if !is_initialize then | |
| 242 | // Assignability is a static property of the declaration: | |
| 243 | // a mut local is always assignable - captured mut locals | |
| 244 | // are stored through a shared BOX[T] cell - and a non-mut | |
| 245 | // local never is. Whether the box exists is a code | |
| 246 | // generation concern, not a legality one. | |
| 247 | if symbol.is_disposed then | |
| 248 | IoC.CONTAINER.instance.logger.error(location, "scoped disposal value may not be assigned to") | |
| 249 | elif !symbol.is_mutable_marked then | |
| 250 | IoC.CONTAINER.instance.logger.error(location, "local value cannot be reassigned") | |
| 251 | elif | |
| 252 | !symbol.is_boxed /\ | |
| 253 | (symbol.is_captured \/ !function? \/ symbol.owner != function) /\ | |
| 254 | _wants_il | |
| 255 | then | |
| 256 | // mark-boxed-locals boxes every captured, reassigned | |
| 257 | // mut local before compile-expressions on an IL-bound | |
| 258 | // build, so reaching this store unboxed means the | |
| 259 | // boxing analysis missed the assignment - erroring | |
| 260 | // beats emitting a store the capture cannot observe. | |
| 261 | IoC.CONTAINER.instance.logger.error(location, "captured value may not be assigned to") | |
| 262 | fi | |
| 263 | fi | |
| 264 | ||
| 265 | // Boxed store from inside the closure body that | |
| 266 | // captured this local: route through the closure's | |
| 267 | // frame so we hit the shared BOX[T] cell, not the | |
| 268 | // (invisible-from-here) outer slot. | |
| 269 | if symbol.is_boxed /\ !is_initialize /\ function? /\ symbol.owner != function then | |
| 270 | let result = function.store_captured_value(location, symbol, value, self) | |
| 271 | ||
| 272 | return result | |
| 273 | fi | |
| 274 | ||
| 275 | if symbol.is_boxed then | |
| 276 | return _store_boxed_local(location, symbol, value, is_initialize) | |
| 277 | fi | |
| 278 | ||
| 279 | return Store.LOCAL_VARIABLE(symbol, _value_boxer.box_if_needed(value, symbol.type!)) | |
| 280 | si | |
| 281 | ||
| 282 | // Boxed-local read: load slot (gives BOX[T] reference) | |
| 283 | // and field-load `.value`. The TYPE_WRAPPER re-types the | |
| 284 | // slot load as BOX[T] for the benefit of the follow-on | |
| 285 | // instance-field access; emitted IL is just ldloc. | |
| 286 | // The `.value` access goes through `member.load` | |
| 287 | // polymorphically — works whether the resolved member | |
| 288 | // is a Field, INSTANCE_FIELD, or auto-generated property | |
| 289 | // shape that wraps a public field. | |
| 290 | _load_boxed_local(location: Source.LOCATION, symbol: Symbols.Variable) -> Value is | |
| 291 | let member = resolve_box_value_member(symbol) | |
| 292 | ||
| 293 | if !member? then | |
| 294 | return Load.LOCAL_VARIABLE(symbol) | |
| 295 | fi | |
| 296 | ||
| 297 | let raw_load = Load.LOCAL_VARIABLE(symbol) | |
| 298 | let typed_box = TYPE_WRAPPER(symbol.storage_type!, raw_load) | |
| 299 | ||
| 300 | return member.load(location, typed_box, self) | |
| 301 | si | |
| 302 | ||
| 303 | // Boxed-local write: on declaration init, construct a | |
| 304 | // fresh `Ghul.BOX[T](value)` (or `Ghul.BOX[T]()` when no | |
| 305 | // initializer is supplied) and store the reference; on | |
| 306 | // reassignment, load the slot's BOX[T] and field-store to | |
| 307 | // `.value`. Allocating the empty box at declaration time — | |
| 308 | // not lazily on first write — is what makes by-reference | |
| 309 | // capture work for a forward-declared mutable (`let f mut; | |
| 310 | // let g = () => f(); f = ...`): the closure-frame ctor | |
| 311 | // receives the same heap cell the later assignment writes | |
| 312 | // to. | |
| 313 | // Declaring a captured, reassigned local that has no initializer: | |
| 314 | // there is no value to store, only the empty box to allocate. | |
| 315 | store_empty_boxed_local(location: Source.LOCATION, symbol: Symbols.Variable) -> Value => | |
| 316 | _store_boxed_local(location, symbol, null, true) | |
| 317 | ||
| 318 | _store_boxed_local(location: Source.LOCATION, symbol: Symbols.Variable, value: Value?, is_initialize: bool) -> Value is | |
| 319 | if is_initialize then | |
| 320 | // `box_type.symbol` is the specialised | |
| 321 | // Symbols.GENERIC (BOX[T]), constructed by | |
| 322 | // `Types.GENERIC.init`. find_member on it | |
| 323 | // returns the specialised init constructor(s) | |
| 324 | // with the concrete T substituted in — but | |
| 325 | // BOX has two overloads (`init()` and | |
| 326 | // `init(value: T)`), so we get a FUNCTION_GROUP | |
| 327 | // back. Pick the overload matching what we have. | |
| 328 | let box_type = symbol.storage_type! | |
| 329 | let arity = if value? then 1 else 0 fi | |
| 330 | let ctor = _resolve_box_constructor(box_type, arity) | |
| 331 | ||
| 332 | assert ctor? else "no box constructor of arity {arity} for {box_type}" | |
| 333 | ||
| 334 | let arguments = | |
| 335 | if value? then | |
| 336 | Collections.LIST[Value]([value]) | |
| 337 | else | |
| 338 | Collections.LIST[Value]() | |
| 339 | fi | |
| 340 | let new_box = NEW(box_type, ctor, arguments) | |
| 341 | ||
| 342 | return Store.LOCAL_VARIABLE(symbol, new_box) | |
| 343 | fi | |
| 344 | ||
| 345 | let member = resolve_box_value_member(symbol) | |
| 346 | ||
| 347 | if !member? then | |
| 348 | return Store.LOCAL_VARIABLE(symbol, _value_boxer.box_if_needed(value!, symbol.type!)) | |
| 349 | fi | |
| 350 | ||
| 351 | let raw_load = Load.LOCAL_VARIABLE(symbol) | |
| 352 | let typed_box = TYPE_WRAPPER(symbol.storage_type!, raw_load) | |
| 353 | ||
| 354 | return member.store(location, typed_box, value!, self, false) | |
| 355 | si | |
| 356 | ||
| 357 | // Constructor of `Ghul.BOX[T]` taking `arity` arguments — | |
| 358 | // 1 for the value-carrying overload, 0 for the empty one. | |
| 359 | // `find_member("init")` on the specialised GENERIC | |
| 360 | // returns a FUNCTION_GROUP (BOX has both); pick the | |
| 361 | // matching overload. Returns null if box_type is unresolved | |
| 362 | // or the constructor can't be located — callers null-check | |
| 363 | // before use. | |
| 364 | _resolve_box_constructor(box_type: Type?, arity: int) -> Symbols.Function? is | |
| 365 | if !box_type? then | |
| 366 | return null | |
| 367 | fi | |
| 368 | ||
| 369 | let member = box_type.symbol.find_member("init") | |
| 370 | ||
| 371 | if !member? then | |
| 372 | return null | |
| 373 | fi | |
| 374 | ||
| 375 | let direct = cast Symbols.Function?(member) | |
| 376 | ||
| 377 | if direct? then | |
| 378 | if direct.are_arguments_declared /\ direct.arguments.count == arity then | |
| 379 | return direct | |
| 380 | else | |
| 381 | return null | |
| 382 | fi | |
| 383 | fi | |
| 384 | ||
| 385 | let group = cast Symbols.FUNCTION_GROUP?(member) | |
| 386 | ||
| 387 | if !group? then | |
| 388 | return null | |
| 389 | fi | |
| 390 | ||
| 391 | for f in group.functions do | |
| 392 | if f.are_arguments_declared /\ f.arguments.count == arity then | |
| 393 | return f | |
| 394 | fi | |
| 395 | od | |
| 396 | ||
| 397 | return null | |
| 398 | si | |
| 399 | ||
| 400 | // The `value` member of `Ghul.BOX[T]` — what reads and | |
| 401 | // writes ultimately touch when `symbol.is_boxed`. | |
| 402 | // Returned as a generic Symbol so polymorphic | |
| 403 | // `.load(location, from, loader)` / | |
| 404 | // `.store(location, from, value, loader, is_initialize)` | |
| 405 | // dispatch handles Field vs INSTANCE_FIELD vs | |
| 406 | // auto-generated property shapes uniformly. | |
| 407 | resolve_box_value_member(symbol: Symbols.Variable) -> Symbols.Symbol? is | |
| 408 | let box_type = symbol.storage_type | |
| 409 | ||
| 410 | if !box_type? then | |
| 411 | return null | |
| 412 | fi | |
| 413 | ||
| 414 | return box_type.symbol.find_direct("value") | |
| 415 | si | |
| 416 | ||
| 417 | // The method local an inlined literal reads in place of its own | |
| 418 | // parameter, and null for every other symbol - including a local a | |
| 419 | // block in that literal's body declares, which the closure also | |
| 420 | // owns and which loads as it does in any method. The owner is asked | |
| 421 | // rather than the current function, so a literal nested inside the | |
| 422 | // inlined one reaches the local the same way. | |
| 423 | _inlined_parameter_local(symbol: Symbols.Symbol) -> Symbols.Variable? is | |
| 424 | if let owner = cast Symbols.Closure?(symbol.owner) then | |
| 425 | if let inlined = owner.inlined_into then | |
| 426 | if inlined.parameter == symbol then | |
| 427 | return inlined.local | |
| 428 | fi | |
| 429 | fi | |
| 430 | fi | |
| 431 | ||
| 432 | return null | |
| 433 | si | |
| 434 | ||
| 435 | // How the function an inlined body is emitted into reads a variable | |
| 436 | // the inlined literal does not own: its own local as a local, and | |
| 437 | // one from further out through its own frame chain. Only the | |
| 438 | // inlined literal has no frame, so a function further out reaches | |
| 439 | // what it closed over exactly as it does anywhere else in its body. | |
| 440 | // Null when the walk is not directly inside an inlined literal, or | |
| 441 | // when the variable is one the literal itself owns. | |
| 442 | _load_through_inlined_method(location: Source.LOCATION, symbol: Symbols.Variable) -> Value? is | |
| 443 | let inlined = _current_inlined_closure | |
| 444 | ||
| 445 | if !inlined? then | |
| 446 | return null | |
| 447 | fi | |
| 448 | ||
| 449 | let method = inlined.inlined_into!.method | |
| 450 | ||
| 451 | if symbol.owner == method then | |
| 452 | if isa Symbols.LOCAL_ARGUMENT(symbol) then | |
| 453 | return Load.LOCAL_ARGUMENT(symbol) | |
| 454 | fi | |
| 455 | ||
| 456 | if symbol.is_boxed then | |
| 457 | return _load_boxed_local(location, symbol) | |
| 458 | fi | |
| 459 | ||
| 460 | return Load.LOCAL_VARIABLE(symbol) | |
| 461 | fi | |
| 462 | ||
| 463 | if symbol.owner == inlined then | |
| 464 | return null | |
| 465 | fi | |
| 466 | ||
| 467 | if !isa Symbols.Closure(method) then | |
| 468 | return null | |
| 469 | fi | |
| 470 | ||
| 471 | let raw = method.load_captured_value(location, symbol, self) | |
| 472 | ||
| 473 | if symbol.is_boxed then | |
| 474 | if let value_member = resolve_box_value_member(symbol) then | |
| 475 | return value_member.load(location, raw, self) | |
| 476 | fi | |
| 477 | fi | |
| 478 | ||
| 479 | return raw | |
| 480 | si | |
| 481 | ||
| 482 | // Whether the walk is directly inside a literal being compiled | |
| 483 | // inline, rather than inside a literal nested within one. A | |
| 484 | // captured outer variable loads as the method's own local there, | |
| 485 | // since an inlined literal has no frame; a nested literal captures | |
| 486 | // it through its own frame as any literal in the method would. | |
| 487 | _current_inlined_closure: Symbols.Closure? is | |
| 488 | if let closure = cast Symbols.Closure?(_symbol_table.current_function) then | |
| 489 | if closure.is_inlined then | |
| 490 | return closure | |
| 491 | fi | |
| 492 | fi | |
| 493 | ||
| 494 | return null | |
| 495 | si | |
| 496 | ||
| 497 | // How a name owned by a literal being compiled inline is read. | |
| 498 | // Directly inside that literal it is the method local the loop | |
| 499 | // stores each element into. Inside a literal nested within it, | |
| 500 | // that local is one of the enclosing method's, so it is captured | |
| 501 | // through the nested literal's own frame exactly as any method | |
| 502 | // local it closed over would be - a nested literal is emitted as | |
| 503 | // its own method, where the loop's local does not exist. | |
| 504 | _load_inlined_parameter(location: Source.LOCATION, symbol: Symbols.Symbol) -> Value? is | |
| 505 | let parameter = _inlined_parameter_local(symbol) | |
| 506 | ||
| 507 | if !parameter? then | |
| 508 | return null | |
| 509 | fi | |
| 510 | ||
| 511 | if _current_inlined_closure == symbol.owner then | |
| 512 | return Load.LOCAL_VARIABLE(parameter) | |
| 513 | fi | |
| 514 | ||
| 515 | if let function = _symbol_table.current_function then | |
| 516 | return function.load_captured_value(location, parameter, self) | |
| 517 | fi | |
| 518 | ||
| 519 | return Load.LOCAL_VARIABLE(parameter) | |
| 520 | si | |
| 521 | ||
| 522 | load_local_argument(location: Source.LOCATION, symbol: Symbols.Variable) -> Value is | |
| 523 | let function = _symbol_table.current_function | |
| 524 | ||
| 525 | if let inlined = _load_inlined_parameter(location, symbol) then | |
| 526 | return inlined | |
| 527 | fi | |
| 528 | ||
| 529 | if let inlined = _load_through_inlined_method(location, symbol) then | |
| 530 | return inlined | |
| 531 | fi | |
| 532 | ||
| 533 | if function? /\ symbol.owner != function then | |
| 534 | let result = function.load_captured_value(location, symbol, self) | |
| 535 | ||
| 536 | return result | |
| 537 | fi | |
| 538 | ||
| 539 | return Load.LOCAL_ARGUMENT(symbol) | |
| 540 | si | |
| 541 | ||
| 542 | load_outer_local_argument(location: Source.LOCATION, symbol: Symbols.Variable) -> Value is | |
| 543 | let function = _symbol_table.current_function | |
| 544 | ||
| 545 | if let inlined = _load_inlined_parameter(location, symbol) then | |
| 546 | return inlined | |
| 547 | fi | |
| 548 | ||
| 549 | if let inlined = _load_through_inlined_method(location, symbol) then | |
| 550 | return inlined | |
| 551 | fi | |
| 552 | ||
| 553 | if function? /\ symbol.owner != function then | |
| 554 | let result = function.load_outer_captured_value(location, symbol, self) | |
| 555 | ||
| 556 | assert result? else "load outer captured value did not return a value: {function} {symbol}" | |
| 557 | ||
| 558 | return result | |
| 559 | fi | |
| 560 | ||
| 561 | return Load.LOCAL_VARIABLE(symbol) | |
| 562 | si | |
| 563 | ||
| 564 | store_local_argument(location: Source.LOCATION, symbol: Symbols.Variable, value: Value, is_initialize: bool) -> Value is | |
| 565 | let function = _symbol_table.current_function | |
| 566 | ||
| 567 | if !is_initialize then | |
| 568 | // FIXME: need proper dataflow analysis | |
| 569 | if !symbol.is_mutable_marked then | |
| 570 | IoC.CONTAINER.instance.logger.error(location, "local value cannot be reassigned") | |
| 571 | elif symbol.is_captured \/ !function? \/ symbol.owner != function then | |
| 572 | // Captured arguments are not yet routed through a | |
| 573 | // shared BOX[T] cell the way captured mut locals | |
| 574 | // are - the closure frame copies the argument value | |
| 575 | // when it is constructed. Until they are boxed, an | |
| 576 | // assignment on either side of the capture would be | |
| 577 | // invisible to the other, so it is rejected even | |
| 578 | // for a mut argument. | |
| 579 | IoC.CONTAINER.instance.logger.error(location, "captured argument may not be assigned to") | |
| 580 | fi | |
| 581 | fi | |
| 582 | ||
| 583 | return Store.LOCAL_ARGUMENT(symbol, _value_boxer.box_if_needed(value, symbol.type!)) | |
| 584 | si | |
| 585 | ||
| 586 | // True when this build lowers the IR to IL - the only case where | |
| 587 | // boxing decisions have observable consequences. | |
| 588 | _wants_il: bool => | |
| 589 | IoC.CONTAINER.instance.build_flags.want_assembler \/ | |
| 590 | IoC.CONTAINER.instance.build_flags.want_executable | |
| 591 | ||
| 592 | load_global_variable(symbol: Symbols.Variable) -> Value => | |
| 593 | Load.GLOBAL_FIELD(symbol) | |
| 594 | ||
| 595 | // Whether the value wants boxing is a question about the | |
| 596 | // variable's type, and a top-level `let` assigned above its own | |
| 597 | // declaration has none - the statement that types it has not | |
| 598 | // been walked. The assignment is reported at its own site, so | |
| 599 | // the unboxed value stands in rather than forcing an absent | |
| 600 | // type here. | |
| 601 | store_global_variable(symbol: Symbols.Variable, value: Value) -> Value => | |
| 602 | Store.GLOBAL_FIELD( | |
| 603 | symbol, | |
| 604 | if let variable_type = symbol.type then | |
| 605 | _value_boxer.box_if_needed(value, variable_type) | |
| 606 | else | |
| 607 | value | |
| 608 | fi | |
| 609 | ) | |
| 610 | ||
| 611 | load_instance_variable(location: Source.LOCATION, from: Value? mut, symbol: Symbols.Variable) -> Value is | |
| 612 | if !from? then | |
| 613 | from = load_self(location) | |
| 614 | fi | |
| 615 | ||
| 616 | return Load.INSTANCE_FIELD(from, symbol) | |
| 617 | si | |
| 618 | ||
| 619 | store_instance_variable(location: Source.LOCATION, from: Value? mut, symbol: Symbols.Variable, value: Value) -> Value is | |
| 620 | if !from? then | |
| 621 | from = load_self(location) | |
| 622 | fi | |
| 623 | ||
| 624 | return Store.INSTANCE_FIELD(from, symbol, _value_boxer.box_if_needed(value, symbol.type!)) | |
| 625 | si | |
| 626 | ||
| 627 | load_struct_variable(location: Source.LOCATION, from: Value? mut, symbol: Symbols.Variable) -> Value is | |
| 628 | if !from? then | |
| 629 | from = load_self(location) | |
| 630 | elif from.has_address then | |
| 631 | from = ADDRESS(from) | |
| 632 | fi | |
| 633 | ||
| 634 | return Load.INSTANCE_FIELD(from, symbol) | |
| 635 | si | |
| 636 | ||
| 637 | store_struct_variable(location: Source.LOCATION, from: Value? mut, symbol: Symbols.Variable, value: Value) -> Value is | |
| 638 | if !from? then | |
| 639 | from = load_self(location) | |
| 640 | elif from.has_address then | |
| 641 | from = ADDRESS(from) | |
| 642 | else | |
| 643 | _logger.info(location, "member updated in discarded value") | |
| 644 | fi | |
| 645 | ||
| 646 | return Store.INSTANCE_FIELD(from, symbol, _value_boxer.box_if_needed(value, symbol.type!)) | |
| 647 | si | |
| 648 | ||
| 649 | ||
| 650 | load_static_field(symbol: Symbols.Variable) -> Value => | |
| 651 | Load.STATIC_FIELD(symbol) | |
| 652 | ||
| 653 | store_static_field(symbol: Symbols.Variable, value: Value) -> Value => | |
| 654 | Store.STATIC_FIELD(symbol, _value_boxer.box_if_needed(value, symbol.type!)) | |
| 655 | ||
| 656 | // A constant is carried in metadata as text, in the same form a | |
| 657 | // reflected argument default is, so the same conversion reads it | |
| 658 | // back at the type the constant was declared with - through the | |
| 659 | // entry that takes the text as a value, because a constant string | |
| 660 | // can hold any text and so cannot reserve one to mean null. | |
| 661 | load_constant_field(symbol: Symbols.CONSTANT_FIELD) -> Value => | |
| 662 | if let text = symbol.constant_value then | |
| 663 | Syntax.Process.DEFAULT_ARGUMENT_VALUES.build_value( | |
| 664 | text, | |
| 665 | symbol.type!, | |
| 666 | _innate_symbol_lookup | |
| 667 | ) | |
| 668 | else | |
| 669 | IR.Values.DEFAULT(symbol.type!) | |
| 670 | fi | |
| 671 | ||
| 672 | // Reached when a constant is written to through a name brought | |
| 673 | // into scope by `use`, which stores without asking whether the | |
| 674 | // symbol accepts one. The member-access form is turned away | |
| 675 | // earlier, as a field only its declaring type may assign to, so | |
| 676 | // the two spellings report differently - but both have to | |
| 677 | // report, because a constant has no field for a write to reach. | |
| 678 | store_constant_field(location: Source.LOCATION, symbol: Symbols.CONSTANT_FIELD) -> Value is | |
| 679 | _logger.error(location, "{symbol} is not assignable") | |
| 680 | ||
| 681 | return IR.Values.DUMMY(Types.ERROR(), location) | |
| 682 | si | |
| 683 | ||
| 684 | // A property error recovery declared without building its read | |
| 685 | // function has nothing to load, and the diagnostic for it is already | |
| 686 | // reported, so the wrappers load an ERROR value there, as the | |
| 687 | // `store_*_property` wrappers below do for a store. | |
| 688 | load_instance_property(location: Source.LOCATION, from: Value?, symbol: Symbols.Property) -> Value is | |
| 689 | let result = load_property(location, from, symbol, false) | |
| 690 | return if result? then result else IR.Values.DUMMY(Types.ERROR(), location) fi | |
| 691 | si | |
| 692 | ||
| 693 | load_static_property(location: Source.LOCATION, symbol: Symbols.Property) -> Value is | |
| 694 | let result = load_property(location, null, symbol, true) | |
| 695 | return if result? then result else IR.Values.DUMMY(Types.ERROR(), location) fi | |
| 696 | si | |
| 697 | ||
| 698 | load_global_property(location: Source.LOCATION, symbol: Symbols.Property) -> Value is | |
| 699 | let result = load_property(location, null, symbol, true) | |
| 700 | return if result? then result else IR.Values.DUMMY(Types.ERROR(), location) fi | |
| 701 | si | |
| 702 | ||
| 703 | load_property(location: Source.LOCATION, from: Value? mut, symbol: Symbols.Property, is_static: bool) -> Value? is | |
| 704 | if !from? /\ !is_static then | |
| 705 | from = load_self(location) | |
| 706 | fi | |
| 707 | ||
| 708 | // Error recovery can declare a property from a malformed | |
| 709 | // declaration without a read function; the syntax error is the | |
| 710 | // diagnosis, and reading the property is an ordinary error | |
| 711 | // after it rather than the end of the compile. | |
| 712 | if !symbol.read_function? then | |
| 713 | _logger.error(location, "property {symbol.name} is not readable") | |
| 714 | return null | |
| 715 | fi | |
| 716 | ||
| 717 | let read_function = symbol.read_function | |
| 718 | ||
| 719 | find_symbol = _null_find_symbol | |
| 720 | ||
| 721 | // Flow narrowing reconciles a narrowed property's | |
| 722 | // `Property.type` the same way it does `Variable.type`; | |
| 723 | // the load's value must carry that narrowed view rather | |
| 724 | // than the getter's declared return type. Outside a | |
| 725 | // narrow the two are the same type instance, so this is | |
| 726 | // inert. | |
| 727 | let narrowed_type = | |
| 728 | if symbol.type? /\ symbol.type != read_function.return_type then | |
| 729 | symbol.type | |
| 730 | else | |
| 731 | null | |
| 732 | fi | |
| 733 | ||
| 734 | return read_function.call(location, from, Collections.LIST[Value](0), narrowed_type, _function_caller) | |
| 735 | si | |
| 736 | ||
| 737 | store_instance_property(location: Source.LOCATION, from: Value?, symbol: Symbols.Property, value: Value) -> Value is | |
| 738 | let result = store_property(location, from, symbol, value, false) | |
| 739 | return if result? then result else IR.Values.DUMMY(Types.ERROR(), location) fi | |
| 740 | si | |
| 741 | store_static_property(location: Source.LOCATION, symbol: Symbols.Property, value: Value) -> Value is | |
| 742 | let result = store_property(location, null, symbol, value, true) | |
| 743 | return if result? then result else IR.Values.DUMMY(Types.ERROR(), location) fi | |
| 744 | si | |
| 745 | store_global_property(location: Source.LOCATION, symbol: Symbols.Property, value: Value) -> Value is | |
| 746 | let result = store_property(location, null, symbol, value, true) | |
| 747 | return if result? then result else IR.Values.DUMMY(Types.ERROR(), location) fi | |
| 748 | si | |
| 749 | ||
| 750 | store_property(location: Source.LOCATION, from: Value? mut, symbol: Symbols.Property, value: Value, is_static: bool) -> Value? is | |
| 751 | if !from? /\ !is_static then | |
| 752 | from = load_self(location) | |
| 753 | fi | |
| 754 | ||
| 755 | if symbol.assign_function == null then | |
| 756 | _logger.error(location, "property {symbol} is not assignable") | |
| 757 | ||
| 758 | assert !symbol.is_assignable else "property {symbol} is assignable but does not have an assign accessor function" | |
| 759 | ||
| 760 | return null | |
| 761 | fi | |
| 762 | ||
| 763 | find_symbol = _null_find_symbol | |
| 764 | ||
| 765 | return TYPE_WRAPPER( | |
| 766 | cast Types.Typed(symbol).type!, | |
| 767 | symbol.assign_function!.call(location, from, Collections.LIST([value]), null, _function_caller) | |
| 768 | ) | |
| 769 | si | |
| 770 | si | |
| 771 | si |