import { Set, HashMap } from 'zena:collections'; import { findDeclaration, collectValueExports, isArrayIntrinsic } from '../lookup.zena'; /** * AST → ZIR lowering (docs/design/ir.md §6). * * This is the compiler's only backend: every function body compiles * AST -> ZIR -> wasm. A ZirUnsupported bail is a compiler bug and * fails the compile (see lowerFunction), never a silent fallback. * * SSA construction is the structured-AST form: an environment maps each * variable symbol to its current value; control-flow joins introduce * block parameters for the variables assigned in the joined regions * (computed by a pre-scan), and loop headers carry parameters for every * variable assigned anywhere in the loop. */ import { Node, Statement, Expression, FunctionExpression, BlockStatement, UsingStatement, ShieldedStatement, ReturnStatement, YieldStatement, BreakStatement, ContinueStatement, IfStatement, WhileStatement, ExpressionStatement, VariableDeclaration, BinaryExpression, UnaryExpression, AssignmentExpression, Identifier, NumberLiteral, BooleanLiteral, IdentifierPattern, BindingPattern, CallExpression, IfExpression, AsExpression, ForStatement, ForInStatement, IndexExpression, TemplateLiteral, NullLiteral, StringLiteral, NewExpression, MemberExpression, MethodDefinition, ThisExpression, Parameter, ClassDeclaration, MixinDeclaration, FieldDefinition, FieldInitializer, SuperInitializer, TypeAnnotation, AccessorDeclaration, SetterInfo, MatchExpression, LiteralPattern, TupleLiteral, EnumDeclaration, getMemberPropertyName, IsExpression, RangeExpression, PipelineExpression, PipePlaceholder, SuperExpression, ArrayLiteral, RecordLiteral, MapLiteral, ThrowExpression, TryExpression, TaggedTemplateExpression, ImportDeclaration, ImportSpecifier, Module, SpreadElement, LetPatternCondition, TuplePattern, ArrayPattern, RestPattern, PropertyAssignment, SealedVariant, RecordPattern, BindingProperty, AwaitExpression, SymbolPropertyName, ContractClause, ContractKind, isOldCallee, hasOptionalChainStep, isMethodCallStep, isOptionalPostfixStep, postfixStepTarget, samePostfixChain, } from '../../ast.zena'; import { Type, VoidType, NeverType, ClassType, InterfaceType, FunctionType, FieldInfo, MethodInfo, ArrayType, InlineTupleType, InlineRecordType, TypeAliasType, TupleType, RecordType, EnumType, TypeParameterType, UnionType, baseType, isVoidOrNeverType, isRefType, AnyRefType, EqRefType, arrayElementType, isSignedNarrowIntType, typeToString, ownershipHandleOf, OwnershipHandle, soleNonNullMember, NullType, BoolLiteralType, NumLiteralType, HoleType, isIntegerType, isNullableType, typeContainsTypeParameter, representsRestricted, parseTupleIndexLiteral, sameNumericLiteralRaw, } from '../../types.zena'; import { TemplateAbandon, TemplateFamilies, TemplateRecorder, internedCopy, memberProvidedIn, vtableSlotIn, accessorOverriddenIn, } from './template.zena'; import { nowMs } from '../../time.zena'; import { RecordAdaptation, RecordUnionAdaptation, FunctionAdaptation, SemanticModel, memberKeyOf, accessKeyOf, } from '../../semantic-model.zena'; import { SymbolId, Symbol, noSymbolId } from '../../scope/symbol.zena'; import { Program, CompilationUnit } from '../../program.zena'; import { dirName, normalizePath } from '../../module-resolver.zena'; import { ValType, ValTypeVoid, ValTypeI32, ValTypeI64, ValTypeF32, ValTypeF64, ValTypeV128, ValTypeRef, ValTypeRefNull, ValTypeRefExtern, ValTypeAnyRef, ValTypeEqRef, ValTypeStructRef, ValTypeExternRef, ValTypeNullRef, ValTypeI8, ValTypeI16, valTypeEquals, isWasmSubtype, isRefValType, isNullableAnyRef, WasmType, refTargetOf } from '../wasm.zena'; import { targetUsesJsStrings } from '../../targets.zena'; import { WasmModule, WasmFunction, WasmStruct, WasmField, WasmGlobal, WasmSignature, ClassCodegenInfo, isValSubtype, sortWasmFields, RecordDispatchInfo, asWasmStruct, asWasmSignature, asWasmArray, valTypeIdentical, ClosureForwarderSpec, ExplosionSpec, AdaptationPack, noCaptures, } from '../wasm-module.zena'; import { typeToValType, typeToValTypeWithSub, eraseTypeParameters, substituteTypeParamsInClassContext, substituteTypeParamsInFunctionContext, InlineLayout, inlineLayoutOf, inlineParamLayoutOf, inlineRecordParamOf, inlineRecordResultOf, getInterfaceTypeParameters, substituteTypeParamsInCodegen, nestedInlineTuple, nestedInlineRecord, nestedInlineUnion, inlineRecordUnionMembers, flattenInlineTuple, recordFieldsTuple, recordFieldNames, inlineRecordFieldsLayout, boxedRecordWithFields, boxedFieldType, pathIndex, getClosureStruct, getClosureKey, getNonNullableType, recordWasmFields, sortedOptionalNames, interfaceCallSlotName, } from '../type-mapping.zena'; import { IrBody, IrOp, IrRefFunc, IrRefGlobal, IrRefType, IrRefField, IrRefTag } from './ir.zena'; import { getClassKey, getClassMethodKey, getClassPrivateFieldKey, getMemberRootClass, getInterfaceKey, InterfaceKey, getInterfaceConstructorBaseName, getClosureSpecializationKey, getSignatureKey, ClassKey, getTypeKeyForSpecialization, getTypeUniqueKey, collectMixinFields, findMixinDeclaration, mixinScopeOfNode, MixinKey, getSpecializedFunctionKey, getSpecializedName, getFunctionValueWrapperKey, getOverloadedFunctionKey, parseFloatLiteral, parseI32WithHex, parseI64OrU64Literal, unwrapAliases, isPresentWordName, presentWordCount, presentWordName, } from '../codegen-utils.zena'; import { IrBuilder } from './builder.zena'; import { synthesizeScaffold } from './scaffold.zena'; import { lowerGeneratorRamp, lowerGeneratorNext } from './generators.zena'; import { lowerAsyncRamp, lowerAsyncStep, lowerAsyncMainWrapper, } from './async.zena'; import { lowerComponentAdapter } from './component-adapters.zena'; import { walk } from '../../visitor.zena'; import { lowerContractClause } from './contracts.zena'; import { lowerIntrinsic } from './intrinsics.zena'; import { ExplodedRecord, InlineVar, LetCondBinds, LoweringContext, MergeState, ResultSlots, ZirUnsupported, noValues, oneValue, repackInterfaceValue, } from './lowering-context.zena'; import { lowerBigIntLiteral, lowerStringLiteral, lowerTaggedTemplate, lowerTemplateLiteral } from './templates.zena'; import { exactClassTest, lowerCaseClassEq, lowerCaseClassHash, lowerErasedEq, lowerErasedEqDispatch, lowerEqIntrinsic, lowerHashIntrinsic, lowerValueEqFunction, lowerValueHash, lowerValueHashFunction, } from './equality.zena'; import { existentialAs, existentialHash, existentialIs, existentialUnderlying, lowerExistentialForward, packExistential, projectExistential, } from './existential.zena'; import { isValueHashCodeCall } from '../value-equality.zena'; import { bindRecordProp, branchUnless, collectPatternBindSyms, lowerPatternBindings, lowerPatternTest, patternBinds, patternRefutable, arrayLengthRead, arrayElementRead, arraySliceRead, } from './patterns.zena'; import { isUnsignedNode, lowerAs, lowerBinary, lowerUnary, mapBinaryOp, lowerIntPow, lowerFloatPow, } from './operators.zena'; import { declScopeDrop, lowerFor, lowerForIn, lowerIf, lowerIfExpression, breakThroughFinally, continueThroughFinally, returnThroughFinally, returnMultiThroughFinally, collectAffineDropFields, emitMarkDropped, lowerFieldDropRegions, lowerSyntheticRelease, disposeOwnerClasses, emitMaybeDispose, emitDrop, dropPlanOf, emitSlotsDrop, emitScopedFieldReplace, emitExitDrops, lowerLetCondition, lowerMatch, lowerParamDropRegions, lowerStatementRange, lowerTryExpression, lowerShieldedRegion, lowerUsingRegion, lowerValueNode, lowerWhile, } from './control-flow.zena'; import { WellKnown, isWellKnown } from '../../well-known.zena'; import { growable } from 'zena:core'; // ZirUnsupported now lives in lowering-context.zena (imported above): // split modules throw it too, and importing it from lowering would // cycle. /** * The absence of a value, where an IR value id is expected. * * A `void` expression is ZERO-WIDTH: it occupies no wasm slot, so * lowering it yields no operand. Only consumers that genuinely accept * `void` ever see this — a statement, a void `return`, a store to an * erased field, or an argument for an erased parameter — because the * checker rejects a void value anywhere else. Passing it on as a real * operand is a bug that the ZIR verifier catches. */ export let NO_VALUE = -1; /** * One class level's construction inputs (non-nullable-refs.md): * field values keyed by resolved struct field name, and the evaluated * super() arguments. */ type CtorLevel = {values: HashMap, superArgs: GrowableArray}; /** * The record members of a union whose every member is a record type or * `null`, or null for any other union. A field read off such a union * dispatches on the member * (`FunctionLowerer.#lowerRecordUnionRead`). * * A `null` member drops out because by the time either caller runs it * is accounted for: a plain `o.f` on a nullable receiver is a checker * error, and `o?.f` reaches the read only on the non-null side of the * chain's guard. */ function recordUnionMembers(ut: UnionType): GrowableArray | null { let members = new Array(); for (let memberType in ut.types) { if (memberType is NullType) { continue; } let rec = unwrapAliases(memberType); if (!(rec is RecordType)) { return null; } members.push(rec); } if (members.length < 2) { return null; } return members; } /** Bail-reason tag for expression kinds without a dispatch arm. */ function kindTagOf(e: Expression) { return match (e) { case IsExpression: ' is' case RangeExpression: ' range' case PipelineExpression: ' pipeline' case SuperExpression: ' super' case ArrayLiteral: ' arraylit' case RecordLiteral: ' recordlit' case MapLiteral: ' maplit' case TupleLiteral: ' tuplelit' case ThrowExpression: ' throw' case TryExpression: ' try' case _: ' other' }; } /** * A HOST-import wrapper's body. "Import" here is a wasm-level host * import (a `declare` function provided by the embedder, like * __captureStackTrace), not a module import — module imports resolve * to their declarations and are plain calls. The host ABI cannot * traffic in wasm-GC references, so the raw import returns externref; * Zena-side callers hold the declared reference type. The wrapper is * that ABI shim: forward the params, call the raw import, convert the * externref result into the GC hierarchy (any.convert_extern), and * cast to the declared type. It exists as a real function (not * inlined at call sites) so the declared function keeps one uniform * callable identity — vtable slots and function values reference it. */ function lowerImportWrapper(func: WasmFunction) { let target = func.importWrapperTarget as WasmFunction; let b = new IrBuilder(); let entry = b.newBlock(); let args = new Array(); for (let vt in func.signature.params) { args.push(b.blockParam(entry, vt)); } b.switchTo(entry); if (func.signature.returns.length == 0) { b.callFunc(b.addRef(new IrRefFunc(target)), args, null); b.retVoid(); return b.finish(); } if (func.signature.returns.length != 1 || target.signature.returns.length != 1) { throw new ZirUnsupported('import wrapper shape'); } let raw = b.callFunc(b.addRef(new IrRefFunc(target)), args, target.signature.returns[0]); let wantVt = func.signature.returns[0]; var result = raw; if (target.signature.returns[0] is ValTypeExternRef) { result = b.emit(IrOp.any_convert_extern, new ValTypeAnyRef() as ValType, raw, -1); if (!(wantVt is ValTypeAnyRef)) { result = b.emit(IrOp.ref_cast, wantVt, result, b.internType(wantVt)); } } else if (!isValSubtype(b.body.resultType(raw) as ValType, wantVt)) { result = b.emit(IrOp.ref_cast, wantVt, result, b.internType(wantVt)); } b.ret(result); return b.finish(); } /** * A function-adaptation wrapper's body: its context (param 0) is the * WHOLE source closure pair. Unpack it, forward the source arity's * worth of parameters (casting where the target declared a wider * type), and tail-call through the source pair's func field — * streaming's generateFunctionAdaptationBody. * * A value one side holds as a class or a sub-interface and the other * as an interface is packed on the way through (AdaptationPack): an * argument before the call, which keeps the tail call, and the result * after it, which gives the tail call up. */ function lowerFunctionAdaptation(wasm: WasmModule, func: WasmFunction) { let actualFt = func.functionAdaptationSource as FunctionType; let actualStruct = func.functionAdaptationResolvedSourceStruct ?? getClosureStruct(wasm, actualFt); // ZIR synthesis runs AFTER layout: a getClosureStruct MISS here // mints a fresh struct that can never receive a type index (its // casts emit heap type -1 — invalid wasm). The miss happens when // the adaptation's source FunctionType keys differently from the // pre-layout interning (nullability normalization). Streaming // resolves the pair through its own adaptation records, so bail. var inTypes = false; for (let t in wasm.types) { if (t == (actualStruct as WasmType)) { inTypes = true; } } if (!inTypes) { throw new ZirUnsupported('adaptation source struct not laid out'); } let actualSig = asWasmSignature(actualStruct.fields[0].type); if (actualSig == null) { throw new ZirUnsupported('adaptation source shape'); } let b = new IrBuilder(); let entry = b.newBlock(); let params = new Array(); for (let vt in func.signature.params) { params.push(b.blockParam(entry, vt)); } b.switchTo(entry); let pairVt = new ValTypeRef(actualStruct) as ValType; let pair = b.emit(IrOp.ref_cast, pairVt, params[0], b.internType(pairVt)); let ctxVal = b.emit(IrOp.struct_get, actualStruct.fields[1].type, pair, b.addRef(new IrRefField(actualStruct, 'ctx'))); let args = growable([ctxVal]); // The wrapper's own params cover the target arity; the source // consumes a prefix of them. if (actualSig.params.length > func.signature.params.length) { throw new ZirUnsupported('adaptation arity shape'); } for (var i = 1; i < actualSig.params.length; i += 1) { var v = params[i]; let want = actualSig.params[i]; let pack = adaptationPackAt(wasm, func, i); if (pack != null) { v = applyAdaptationPack(wasm, b, v, pack); } else if (!isValSubtype(b.body.resultType(v) as ValType, want)) { if (!(want is ValTypeRef || want is ValTypeRefNull)) { throw new ZirUnsupported('adaptation param shape'); } // The cast narrows a type the target declared wider, such as an // erased type parameter. Between two types neither of which // contains the other, the cast always fails: the argument has a // different representation on each side (a record of another // shape, a function value of another type) and needs converting. if (!isValSubtype(want, b.body.resultType(v) as ValType)) { throw new ZirUnsupported( 'function-adaptation wrapper cannot convert an argument: it ' + 'packs interfaces, not records or function values (#543)'); } v = b.emit(IrOp.ref_cast, want, v, b.internType(want)); } args.push(v); } let funcVal = b.emit(IrOp.struct_get, actualStruct.fields[0].type, pair, b.addRef(new IrRefField(actualStruct, 'func'))); if (actualSig.returns.length > 1) { throw new ZirUnsupported('adaptation result shape'); } if (unwrapAliases(actualFt.returnType) is NeverType) { // Source never returns normally (throws or loops forever), so its // call has 0 wasm results and satisfies any target return signature. b.callRef(funcVal, args, null); b.unreachable(); return b.finish(); } let resultPack = adaptationPackAt(wasm, func, -1); if (resultPack == null) { if (actualSig.returns.length == 1 && func.signature.returns.length == 1 && !isValSubtype(actualSig.returns[0], func.signature.returns[0])) { let wantRet = func.signature.returns[0]; if (!(wantRet is ValTypeRef || wantRet is ValTypeRefNull) || !isValSubtype(wantRet, actualSig.returns[0])) { throw new ZirUnsupported( 'function-adaptation wrapper cannot convert its result: it ' + 'packs interfaces, not records or function values (#543)'); } // Source returns a union widened to `eqref` whose members all extend // the target class (`Square | Circle` → `Shape`): narrow on the way back. let raw = b.callRef(funcVal, args, actualSig.returns[0]); let narrowed = b.emit(IrOp.ref_cast, wantRet, raw, b.internType(wantRet)); b.ret(narrowed); return b.finish(); } // The fourth forwarding adapter, and a tail call for the same // reason as the other three (see requireForwardableResults). requireForwardableResultTypes(func.signature.returns, actualSig.returns, 'function-adaptation wrapper'); b.retCallRef(funcVal, args); return b.finish(); } // The pack runs after the source returns, so the source gets an // ordinary call and this frame stays while it runs. Such frames // cannot pile up in a chain of tail calls: see // requireForwardableResults. let raw = b.callRef(funcVal, args, actualSig.returns[0]); b.ret(applyAdaptationPack(wasm, b, raw, resultPack)); return b.finish(); } /** The pack `func` makes at parameter `index` (-1: the result), if any. */ function adaptationPackAt(wasm: WasmModule, func: WasmFunction, index: i32): AdaptationPack | null { if (let (true, packs) = wasm.functionAdaptationPacks.get(func)) { for (let p in packs) { if (p.index == index) { return p; } } } return null; } /** * Packs `value` into `pack`'s interface. A value that can be null * branches around the pack, because packing null would build a fat * pointer that is not itself null (as FunctionLowerer's * #packInterfaceNullable does for an expression). */ function applyAdaptationPack(wasm: WasmModule, b: IrBuilder, value: i32, pack: AdaptationPack): i32 { if (!pack.nullable) { return packAdaptedValue(wasm, b, value, pack); } let vt = b.body.resultType(value) as ValType; let isNull = b.emit(IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1); let nullB = b.newBlock(); let packB = b.newBlock(); let mergeB = b.newBlock(); b.brIf(isNull, nullB, new Array(), packB, new Array()); b.switchTo(packB); let nonNull = if (vt is ValTypeRefNull) { b.emit(IrOp.ref_as_non_null, new ValTypeRef(vt.target) as ValType, value, -1) } else { value }; let packed = packAdaptedValue(wasm, b, nonNull, pack); let packedVt = b.body.resultType(packed) as ValType; if (!(packedVt is ValTypeRef)) { throw new ZirUnsupported('nullable adaptation pack result'); } let mergedVt = new ValTypeRefNull(packedVt.target) as ValType; let result = b.blockParam(mergeB, mergedVt); b.br(mergeB, growable([packed])); b.switchTo(nullB); let nullValue = b.emit(IrOp.ref_null, mergedVt, -1, -1); b.br(mergeB, growable([nullValue])); b.switchTo(mergeB); return result; } /** * The pack itself: a class instance through its class-interface * vtable, a sub-interface fat pointer by an upcast. */ function packAdaptedValue(wasm: WasmModule, b: IrBuilder, value: i32, pack: AdaptationPack): i32 { let {pair, sourceIface, targetIface} = pack; if (pair == null) { return repackInterfaceValue(wasm, b, value, sourceIface as InterfaceKey, targetIface); } let vtable = wasm.getClassInterfaceVTable(pair); if (vtable == null) { throw new ZirUnsupported('function-adaptation pack vtable missing'); } let fatPtr = if (let (true, st) = wasm.interfaceStructs.get(targetIface)) st else null; if (fatPtr == null) { throw new ZirUnsupported('function-adaptation pack interface missing'); } return b.emit(IrOp.iface_pack, new ValTypeRef(fatPtr) as ValType, value, b.addRef(new IrRefGlobal(vtable))); } /** * Synthesized or adapted machinery lowering does not model: wrappers, * trampolines, accessor thunks, case-class equality/hash synthesis, * generics, and intrinsics. The direct-call gate in #lowerCall shares * several of these tests but intentionally differs — imports and class * methods are callable, just not lowerable. */ function usesSynthesizedMachinery(func: WasmFunction) { return func.importWrapperTarget != null || func.decoratorAccessorTarget != null || func.interfaceTrampolineTarget != null || func.isFunctionAdaptation || func.closureForwarder != null || func.isClosureWrapper || func.isClosureTrampoline || func.getterStruct != null || func.setterStruct != null || func.isCaseClassEq || func.isCaseClassHash || func.valueEqLeft != null || func.valueHashType != null || func.typeParameters != null || func.typeArguments != null || func.intrinsic != null; } /** Injected-default bookkeeping for one call site (see * #defaultArgInfo): where the defaults start, the callee's parameter * symbols, the declaring class of this-referencing defaults, and the * argument values lowered so far. */ final class DefaultArgInfo { originalCount: i32; paramSymbols: GrowableArray | null; owner: ClassType | null; values = new Array(); new(this.originalCount, this.paramSymbols, this.owner); } final class MemberTarget { owner: WasmStruct; /** Set for physical-field reads. */ fieldName: String | null; /** Set for getter calls (direct, or the slot signature when virtual). */ getter: WasmFunction | null; /** Set when the getter dispatches through the class vtable. */ var vtSlot: String | null = null; /** The receiver class's codegen info, when vtSlot is set. */ var vtInfo: ClassCodegenInfo | null = null; new(this.owner, this.fieldName, this.getter); } /** * Lowers `func` to ZIR. A ZirUnsupported bail is a compiler bug — * re-thrown with the function named so the failure points straight * at the construct that failed to lower; any other exception * propagates untouched. */ export function lowerFunction(wasm: WasmModule, func: WasmFunction): IrBody { // (The catch arm yields null rather than rethrowing directly — a // catch whose every arm throws miscompiles under the bootstrap.) var failMsg: String | null = null; let result = try { lowerFunctionOrThrow(wasm, func) } catch (e) { if (e is ZirUnsupported) { // message carries the class's own 'zir unsupported: ' prefix; // strip it so re-wrapping does not double it. let m = e.message; failMsg = m.sliceBytes(17, m.length) + ' [in ' + (if (func.name != null) func.name as String else '') + ']'; null } else { throw e } }; if (failMsg != null) { throw new ZirUnsupported(failMsg); } return result as IrBody; } /** * Which constructor phase `func` is, by its method name: 1 for a value * phase (``, returning its class's field values), 2 for a * written-body phase (``), 0 for anything else. */ function ctorPhaseOf(func: WasmFunction): i32 { let name = func.methodName; if (name == null) { return 0; } let m = name as String; if (m == '' || m.startsWith('.')) { return 1; } if (m == '' || m.startsWith('.')) { return 2; } return 0; } /** * `func`'s body, lowered directly or made from its family's template * (template.zena). A family's first two members lower in template mode * and teach the family its template; every later member is specialized * from it, or lowered directly when specialization declines. */ export function lowerWithTemplates(wasm: WasmModule, families: TemplateFamilies, func: WasmFunction): IrBody { let plan = families.plan(func); if (plan == 2) { let t0 = nowMs(); let made = families.specialize(func); families.specializeMs += nowMs() - t0; if (made != null) { if (families.checkMode) { families.compare(func, made, lowerFunction(wasm, func)); } return made; } return lowerFunction(wasm, func); } if (plan == 0) { return lowerFunction(wasm, func); } let t0 = nowMs(); let recorder = new TemplateRecorder(); let lowerer = new FunctionLowerer(wasm, func); // A constructor phase lowers as its phase, as in lowerFunction. lowerer.ctorPhase = ctorPhaseOf(func); lowerer.b.recorder = recorder; var abandoned: String | null = null; // (A catch arm yields null rather than rethrowing directly — see // lowerFunction.) let raw = try { lowerer.lower() } catch (e) { if (e is TemplateAbandon) { abandoned = e.reason; null } else if (e is ZirUnsupported) { // Lowering it directly reports the failure the usual way. abandoned = 'unsupported'; null } else { throw e } }; if (raw == null) { families.stop(func, abandoned as String); families.abandonedMs += nowMs() - t0; return lowerFunction(wasm, func); } let body = internedCopy(raw as IrBody); families.observe(func, raw as IrBody, recorder.guards); families.representativeMs += nowMs() - t0; if (families.checkMode) { families.compare(func, body, lowerFunction(wasm, func)); } return body; } /** Entry block with one param per signature slot; leaves it current. */ function synthEntryParams(b: IrBuilder, func: WasmFunction) { let entry = b.newBlock(); let params = new Array(); for (let vt in func.signature.params) { params.push(b.blockParam(entry, vt)); } b.switchTo(entry); return params; } /** * Casts a value to a concrete-ref parameter type. Mirrors streaming's * trampoline casts, which are NON-null (emitRefCast) even when the * target slot is declared nullable. Non-concrete targets pass through. */ function castToConcreteRef(b: IrBuilder, value: i32, targetVt: ValType) { let target = refTargetOf(targetVt); if (target == null) { return value; } let vt = new ValTypeRef(target) as ValType; return b.emit(IrOp.ref_cast, vt, value, b.internType(vt)); } /** * The class whose vtable an interface trampoline dispatches through, or * null when it calls its target directly. A value packed as the * trampoline's class may be an instance of any subclass, so when some * subclass provides its own version of the target, the call goes * through the slot, as a virtual call on the class does. For a field's * synthesized accessor that means a subclass accessor reading something * else, as for a property read on the class (`accessorOverriddenIn`). A * slot that pruning dropped has no reached override, and the direct * call is right. */ function trampolineDispatchClass(wasm: WasmModule, func: WasmFunction): ClassCodegenInfo | null { let {interfaceTrampolineClass, interfaceTrampolineSlot, interfaceTrampolineTarget} = func; if (interfaceTrampolineClass == null || interfaceTrampolineSlot == null || interfaceTrampolineTarget == null || interfaceTrampolineClass.isExtension) { return null; } if (let (true, info) = wasm.classInfos.get(getClassKey(interfaceTrampolineClass))) { if (vtableSlotIn(info, interfaceTrampolineSlot) < 0) { return null; } let fieldName = synthesizedAccessorField(interfaceTrampolineTarget, interfaceTrampolineSlot); let overridden = if (fieldName != null) { accessorOverriddenIn(wasm, info, interfaceTrampolineSlot, fieldName) } else { memberProvidedIn(wasm, info, interfaceTrampolineSlot) }; if (overridden) { return info; } } return null; } /** The field `f` reads or writes when it is a synthesized accessor for `slot`, or null. */ function synthesizedAccessorField(f: WasmFunction, slot: String): String | null { let isGetter = slot.startsWith('get#'); if (f.node != null || (!isGetter && !slot.startsWith('set#'))) { return null; } let st = if (isGetter) f.getterStruct else f.setterStruct; let idx = if (isGetter) f.getterFieldIndex else f.setterFieldIndex; if (st == null || idx < 0 || idx >= st.fields.length) { return null; } return st.fields[idx].name; } /** * The `slot` function of `receiver`'s class vtable, cast to `sig`: the * callee of a call_ref (`LoweringContext.vtableSlotCallee`). */ function trampolineSlotCallee(b: IrBuilder, receiver: i32, info: ClassCodegenInfo, slot: String, sig: WasmSignature) { let {structType} = info; let vtStruct = info.vtableStructType as WasmStruct; let vtField = structType.fields[structType.fieldIndexByName('')].type; let vtRaw = b.emit(IrOp.struct_get, vtField, receiver, b.addRef(new IrRefField(structType, ''))); let vtVt: ValType = new ValTypeRef(vtStruct); let vt = b.emit(IrOp.ref_cast, vtVt, vtRaw, b.internType(vtVt)); let slotField = vtStruct.fields[vtStruct.fieldIndexByName(slot)].type; let slotRaw = b.emit(IrOp.struct_get, slotField, vt, b.addRef(new IrRefField(vtStruct, slot))); let sigVt: ValType = new ValTypeRef(sig); return b.emit(IrOp.ref_cast, sigVt, slotRaw, b.internType(sigVt)); } /** * Interface trampoline: cast the erased receiver to the target's * concrete type, cast any erased params likewise, and tail-call the * target (docs: FunctionGenerator's interfaceTrampolineTarget arm), or * dispatch to it through the class vtable when a subclass overrides it * (`trampolineDispatchClass`). The tail call is what keeps the adapter * out of the stack; see requireForwardableResults. */ function synthesizeInterfaceTrampoline(wasm: WasmModule, func: WasmFunction) { let target = func.interfaceTrampolineTarget as WasmFunction; let b = new IrBuilder(); let params = synthEntryParams(b, func); let {intrinsic} = target; if (intrinsic != null) { if (intrinsic == 'array.len') { let recv = castToConcreteRef(b, params[0], target.signature.params[0]); let len = b.emit(IrOp.array_len, new ValTypeI32(), recv, -1); b.ret(len); return b.finish(); } // Array element access through an interface (FixedArray/ImmutableArray // as Array): inline the op, like every direct call site does. if (intrinsic == 'array.get' || intrinsic == 'array.set') { let arrType = asWasmArray(target.signature.params[0]); if (arrType != null) { let recv = castToConcreteRef(b, params[0], target.signature.params[0]); if (intrinsic == 'array.get') { // Packed i8 elements read unsigned as i32; the erased slot is i32. if (arrType.elementType is ValTypeI8) { let value = b.emit(IrOp.array_get_u, new ValTypeI32(), recv, params[1]); b.ret(value); return b.finish(); } var value = b.emit(IrOp.array_get, arrType.elementType, recv, params[1]); if (func.signature.returns.length == 1 && !isWasmSubtype(arrType.elementType, func.signature.returns[0])) { value = castToConcreteRef(b, value, func.signature.returns[0]); } b.ret(value); return b.finish(); } var elem = params[2]; if (!isWasmSubtype(func.signature.params[2], arrType.elementType)) { elem = castToConcreteRef(b, elem, arrType.elementType); } let triple = growable([recv, params[1], elem]); b.emitVoid(IrOp.array_set, b.pushExtra(triple), -1); b.retVoid(); return b.finish(); } } b.unreachable(); return b.finish(); } if (target.index == -1) { // Target never reached; streaming emits the same trap. b.unreachable(); return b.finish(); } if (func.signature.params.length != target.signature.params.length) { throw new ZirUnsupported('trampoline shape'); } let wraps = func.trampolineParamWraps; let dispatch = trampolineDispatchClass(wasm, func); // Dispatching reads the vtable off the trampoline's own class, which // is the target's class or a subclass of it. let receiver = if (dispatch != null) { castToConcreteRef(b, params[0], new ValTypeRef(dispatch.structType)) } else { castToConcreteRef(b, params[0], target.signature.params[0]) }; let args = growable([receiver]); for (var i = 1; i < params.length; i += 1) { var v = params[i]; var wrapped = false; if (wraps != null) { for (let w in wraps) { if (w.paramIndex == i) { // The class's copy narrows this callback (a `this`-typed // parameter in the interface declaration): rebuild the // argument as a class-view pair whose func is the // synthesized forwarder and whose ctx is the caller's // interface-view pair (generic-interface-dispatch.md). let implStruct = w.implClosureStruct; let funcVal = b.emit(IrOp.ref_func, implStruct.fields[0].type, -1, b.addRef(new IrRefFunc(w.forwarder))); let pairArgs = growable([funcVal, v]); v = b.emit(IrOp.struct_new, new ValTypeRef(implStruct) as ValType, b.pushArgList(pairArgs), b.addRef(new IrRefType(implStruct))); wrapped = true; } } } if (!wrapped && !isWasmSubtype(func.signature.params[i], target.signature.params[i])) { v = castToConcreteRef(b, v, target.signature.params[i]); } args.push(v); } let callee = if (dispatch != null) { trampolineSlotCallee(b, receiver, dispatch, func.interfaceTrampolineSlot as String, target.signature) } else { -1 }; let funcRef = b.addRef(new IrRefFunc(target)); let packPair = func.trampolineResultPackPair; let packIface = func.trampolineResultPackIface; if (packPair == null || packIface == null) { requireForwardableResults(func, target, 'interface trampoline'); if (callee >= 0) { b.retCallRef(callee, args); } else { b.retCall(funcRef, args); } return b.finish(); } // A covariant class-typed result: call, pack into the interface fat // pointer through the class-interface pair vtable, return. No tail // call — the pack happens after the target returns. if (target.signature.returns.length != 1 || func.signature.returns.length != 1) { throw new ZirUnsupported('generic trampoline result shape'); } let raw = if (callee >= 0) { b.callRef(callee, args, target.signature.returns[0]) } else { b.callFunc(funcRef, args, target.signature.returns[0]) }; if (!wasm.classInterfaceVTables.has(packPair)) { throw new ZirUnsupported('generic trampoline pack vtable missing'); } let packGlobal = wasm.classInterfaceVTables[packPair]; if (!wasm.interfaceStructs.has(packIface)) { throw new ZirUnsupported('generic trampoline pack interface missing'); } let fatPtr = wasm.interfaceStructs[packIface]; let packed = b.emit(IrOp.iface_pack, new ValTypeRef(fatPtr) as ValType, raw, b.addRef(new IrRefGlobal(packGlobal))); b.ret(packed); return b.finish(); } /** * A closure forwarder's body (generic interface dispatch): the ctx * (param 0) is the caller's interface-view closure pair. Unpack it, * forward the arguments — packing each one the implementing class * narrowed into its interface fat pointer — and call_ref through the * pair's func field. The structural twin of lowerFunctionAdaptation, * plus the packs. */ function lowerClosureForwarder(wasm: WasmModule, func: WasmFunction) { let spec = func.closureForwarder as ClosureForwarderSpec; let src = spec.sourceStruct; var inTypes = false; for (let t in wasm.types) { if (t == (src as WasmType)) { inTypes = true; } } if (!inTypes) { throw new ZirUnsupported('forwarder source struct not laid out'); } let srcSigOpt = asWasmSignature(src.fields[0].type); if (srcSigOpt == null) { throw new ZirUnsupported('forwarder source shape'); } let srcSig = srcSigOpt; if (srcSig.params.length != func.signature.params.length) { throw new ZirUnsupported('forwarder arity shape'); } let b = new IrBuilder(); let entry = b.newBlock(); let params = new Array(); for (let vt in func.signature.params) { params.push(b.blockParam(entry, vt)); } b.switchTo(entry); let pairVt = new ValTypeRef(src) as ValType; let pair = b.emit(IrOp.ref_cast, pairVt, params[0], b.internType(pairVt)); let ctxVal = b.emit(IrOp.struct_get, src.fields[1].type, pair, b.addRef(new IrRefField(src, 'ctx'))); let args = growable([ctxVal]); for (var i = 1; i < srcSig.params.length; i += 1) { var v = params[i]; var packedArg = false; for (let p in spec.packs) { if (p.argIndex == i) { if (!wasm.interfaceStructs.has(p.ifaceKey)) { throw new ZirUnsupported('forwarder pack interface missing'); } let fatPtr = wasm.interfaceStructs[p.ifaceKey]; if (!wasm.classInterfaceVTables.has(p.pair)) { throw new ZirUnsupported('forwarder pack vtable missing'); } let g = wasm.classInterfaceVTables[p.pair]; v = b.emit(IrOp.iface_pack, new ValTypeRef(fatPtr) as ValType, v, b.addRef(new IrRefGlobal(g))); packedArg = true; } } if (!packedArg) { let want = srcSig.params[i]; if (!isValSubtype(b.body.resultType(v) as ValType, want)) { if (!(want is ValTypeRef || want is ValTypeRefNull)) { throw new ZirUnsupported('forwarder param shape'); } v = b.emit(IrOp.ref_cast, want, v, b.internType(want)); } } args.push(v); } let funcVal = b.emit(IrOp.struct_get, src.fields[0].type, pair, b.addRef(new IrRefField(src, 'func'))); if (srcSig.returns.length == 0) { b.callRef(funcVal, args, null); b.retVoid(); return b.finish(); } if (srcSig.returns.length != 1 || func.signature.returns.length != 1) { throw new ZirUnsupported('forwarder result shape'); } let result = b.callRef(funcVal, args, srcSig.returns[0]); b.ret(result); return b.finish(); } /** * Synthesized default accessors: cast the erased receiver to the * declaring struct and read (getter) or write (setter) the field, * with the getter's vtable-signature compat casts mirrored from * streaming. A field index of -1 means the accessor is a vtable slot * for a member no reachable class stores physically — it traps. */ /** The zero value of a wasm value type (absent optional-field slots). */ function zeroValueOf(b: IrBuilder, vt: ValType) { return match (vt) { case ValTypeI32: b.constI32(0, vt) case ValTypeI64: b.constI64(0, vt) case ValTypeF32: b.constF32(0.0, vt) case ValTypeF64: b.constF64(0.0, vt) case _: b.emit(IrOp.ref_null, vt, -1, -1) }; } function synthesizeAccessor(func: WasmFunction, isGetter: boolean) { let st = if (isGetter) func.getterStruct as WasmStruct else func.setterStruct as WasmStruct; let idx = if (isGetter) func.getterFieldIndex else func.setterFieldIndex; let b = new IrBuilder(); let params = synthEntryParams(b, func); let maskSpec = func.getterMaskSpec; if (isGetter && maskSpec != null) { // Adapted `$present` word getter: assemble this getter's 64-bit // slice of the target mask from constant bits and remapped // source-mask bits (docs/design/record-presence.md §2). let i64Vt: ValType = new ValTypeI64(); let lo = func.getterMaskWord * 64; var hi = lo + 64; if (maskSpec.length < hi) { hi = maskSpec.length; } var constBits: i64 = 0; for (var bit = lo; bit < hi; bit += 1) { if (maskSpec[bit] == -2) { constBits = constBits | ((1 as i64) << ((bit - lo) as i64)); } } var result = b.constI64(constBits, i64Vt); var casted = -1; for (var bit = lo; bit < hi; bit += 1) { let src = maskSpec[bit]; if (src < 0) { continue; } if (casted < 0) { let castVt: ValType = new ValTypeRef(st); casted = b.emit(IrOp.ref_cast, castVt, params[0], b.internType(castVt)); } let srcFieldName = presentWordName(src >> 6); if (st.fieldIndexByName(srcFieldName) < 0) { throw new ZirUnsupported('mask spec without source mask'); } let srcMask = b.emit(IrOp.struct_get, i64Vt, casted, b.addRef(new IrRefField(st, srcFieldName))); let srcBitVal = b.binary(IrOp.i64_and, i64Vt, b.binary(IrOp.i64_shr_u, i64Vt, srcMask, b.constI64((src & 63) as i64, i64Vt)), b.constI64(1, i64Vt)); let shifted = b.binary(IrOp.i64_shl, i64Vt, srcBitVal, b.constI64((bit - lo) as i64, i64Vt)); result = b.binary(IrOp.i64_or, i64Vt, result, shifted); } b.ret(result); return b.finish(); } if (isGetter && func.getterReturnsZero) { // Adapted getter for an optional field the source shape lacks: the // mask reports the absence; return a well-typed placeholder. let expected = func.signature.returns[0]; b.ret(zeroValueOf(b, expected)); return b.finish(); } if (idx == -1) { b.unreachable(); return b.finish(); } let fieldName = st.fields[idx].name; if (fieldName == null) { throw new ZirUnsupported('unnamed accessor field'); } let castVt: ValType = new ValTypeRef(st); let casted = b.emit(IrOp.ref_cast, castVt, params[0], b.internType(castVt)); let fieldRef = b.addRef(new IrRefField(st, fieldName)); if (!isGetter) { let pairIdx = b.pushExtra(twoValuesOf(casted, params[1])); b.emitVoid(IrOp.struct_set, pairIdx, fieldRef); b.retVoid(); return b.finish(); } let actual = st.fields[idx].type; var read = b.emit(IrOp.struct_get, actual, casted, fieldRef); if (func.signature.returns.length == 0) { b.retVoid(); return b.finish(); } // The getter's declared return is the vtable slot's erased type; cast // when the field's storage type differs (mirrors streaming). let expected = func.signature.returns[0]; if (refTargetOf(expected) != null) { if (!valTypeIdentical(actual, expected)) { read = b.emit(IrOp.ref_cast, expected, read, b.internType(expected)); } } else if (expected is ValTypeEqRef && !(actual is ValTypeEqRef)) { let vt: ValType = new ValTypeEqRef(); read = b.emit(IrOp.ref_cast, vt, read, b.internType(vt)); } b.ret(read); return b.finish(); } /** * A forwarding adapter hands its target's results straight back, so it * tail-calls: `return_call` needs the callee's results to be subtypes * of the caller's, which is what handing them back already meant. * * The frame this removes is synthetic — no source line, nothing a * backtrace loses — and removing it is what makes `tail return` mean * what it says. A tail call whose callee is a function value or an * interface method lands in one of these four adapters — closure * wrapper, closure trampoline, interface trampoline, * function-adaptation wrapper — and while they kept a frame such a * chain grew the stack one frame per hop. * * A narrower result is a hard error rather than a call-and-cast * fallback. For three of the four the fallback was never valid anyway * — they returned the target's result with no conversion, so a result * that needed one produced a module wasm rejects. The interface * trampoline did cast, and dropping that costs nothing measurable: the * branch is unreachable across the compiler, the stdlib, every * portable test and the WIT parser, verified by throwing here and * running all of it. If a program ever reaches it, the fix is to widen * the vtable slot's result and cast at the call site, and this message * is what asks for it. * * Packing a result into an interface is the exception, and two adapters * make it without coming here: an interface trampoline whose method * returns a class where the slot returns an interface, and a * function-adaptation wrapper whose source returns a class or a * sub-interface where its target returns an interface. Each calls, * packs and returns, keeping its frame while the target runs, and those * frames cannot pile up. A `tail return` calls only a function that * returns exactly its own type (tail-calls.md §3), so a chain of tail * calls that enters such an adapter continues at the target's result * type, a strict subtype of the adapter's. Each packing adapter it * enters narrows the type again, so the chain keeps at most one frame * per step down the class and interface hierarchy, however many hops it * makes. */ function requireForwardableResults(func: WasmFunction, target: WasmFunction, what: String): void { requireForwardableResultTypes(func.signature.returns, target.signature.returns, what); } /** requireForwardableResults over the result lists themselves, for an * adapter whose target is a signature rather than a function. */ function requireForwardableResultTypes(mine: GrowableArray, theirs: GrowableArray, what: String): void { if (mine.length != theirs.length) { throw new ZirUnsupported(what + ' result shape'); } for (var i = 0; i < mine.length; i += 1) { if (!isValSubtype(theirs[i], mine[i])) { throw new ZirUnsupported(what + ' returns a result its target does not: an adapter that has to ' + 'convert on the way back cannot tail-call, and tail calls ' + 'through it would grow the stack'); } } } /** Closure wrapper: forward params[1..] (dropping the ctx) to the target. */ /** * A closure trampoline: the value-position form of a plain function — * drops the (unused) context param and forwards the rest to the * target, streaming's generateClosureTrampolineBody. */ function synthesizeClosureTrampoline(func: WasmFunction) { let target = func.closureTrampolineTarget; if (target == null) { throw new ZirUnsupported('closure trampoline without target'); } if (func.signature.params.length != target.signature.params.length + 1) { throw new ZirUnsupported('closure trampoline shape'); } let b = new IrBuilder(); let params = synthEntryParams(b, func); let args = new Array(); for (var i = 1; i < params.length; i += 1) { args.push(params[i]); } let funcRef = b.addRef(new IrRefFunc(target)); if (target.signature.returns.length > 1) { throw new ZirUnsupported('closure trampoline result shape'); } requireForwardableResults(func, target, 'closure trampoline'); b.retCall(funcRef, args); return b.finish(); } function synthesizeClosureWrapper(func: WasmFunction) { let target = func.closureWrapperTarget; if (target == null) { throw new ZirUnsupported('closure wrapper without target'); } if (func.signature.params.length != target.signature.params.length + 1) { throw new ZirUnsupported('closure wrapper shape'); } let b = new IrBuilder(); let params = synthEntryParams(b, func); let args = new Array(); for (var i = 1; i < params.length; i += 1) { args.push(params[i]); } let funcRef = b.addRef(new IrRefFunc(target)); requireForwardableResults(func, target, 'closure wrapper'); b.retCall(funcRef, args); return b.finish(); } /** * A field decorator's `context.get` or `context.set`: a closure-ABI * function `(ctx, host[, value])` that casts `host` to the application's * intermediate class and calls the decorated field's accessor on it. */ function synthesizeDecoratorAccessorWrapper(func: WasmFunction) { let target = func.decoratorAccessorTarget; if (target == null) { throw new ZirUnsupported('decorator accessor wrapper without target'); } if (func.signature.params.length != target.signature.params.length + 1 || target.signature.params.length < 1) { throw new ZirUnsupported('decorator accessor wrapper shape'); } let b = new IrBuilder(); let params = synthEntryParams(b, func); let receiverVt = target.signature.params[0]; var receiver = params[1]; if (!isValSubtype(b.body.resultType(receiver) as ValType, receiverVt)) { receiver = b.emit(IrOp.ref_cast, receiverVt, receiver, b.internType(receiverVt)); } let args = new Array(); args.push(receiver); for (var i = 2; i < params.length; i += 1) { args.push(params[i]); } let funcRef = b.addRef(new IrRefFunc(target)); requireForwardableResults(func, target, 'decorator accessor wrapper'); b.retCall(funcRef, args); return b.finish(); } /** The one intrinsic with a body streaming synthesizes: array.len. */ function synthesizeIntrinsic(func: WasmFunction) { if ((func.intrinsic as String) == 'array.len' && func.signature.params.length == 1 && func.signature.returns.length == 1) { let b = new IrBuilder(); let params = synthEntryParams(b, func); let len = b.emit(IrOp.array_len, new ValTypeI32(), params[0], -1); b.ret(len); return b.finish(); } // Every other standalone intrinsic body mirrors the streaming // backend: call sites always inline the op, so the standalone // function exists only for slot uniformity and traps if reached. let b = new IrBuilder(); synthEntryParams(b, func); b.unreachable(); return b.finish(); } function twoValuesOf(a: i32, bVal: i32): GrowableArray { let result = growable([a, bVal]); return result; } function lowerFunctionOrThrow(wasm: WasmModule, func: WasmFunction): IrBody { // Metadata-only functions first: no AST, but their bodies are tiny // fixed shapes ZIR synthesizes directly (they are half of all // fallbacks on the compiler itself). The node==null gate matters: // DECLARED accessors also carry getterStruct/setterStruct, but they // have real bodies (and often no physical field), so they take the // normal lowering path below. // Host-import wrappers keep their declare node but never have a // lowered body: the embedder-provided import trafficks in externref, // and the wrapper is the ABI shim to the declared GC types — // streaming's shape. if (func.importWrapperTarget != null) { return lowerImportWrapper(func); } // Case-class synthesizers win over any carried node: a leaf variant // whose sealed base writes hashCode()/== keeps the base's METHOD // NODE attached while the flag says "synthesize structurally" — // streaming dispatches on the flag first, so mirror that. if (func.isCaseClassEq) { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerCaseClassEq(); } // Structural `==` on a pair of record or tuple types, and the hash // of one. if (func.valueEqLeft != null) { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerValueEqFunction(); } if (func.valueHashType != null) { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerValueHashFunction(); } if (func === wasm.erasedEqFunc) { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerErasedEqDispatch(); } if (func.isCaseClassHash) { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerCaseClassHash(); } if (func.isSyntheticRelease) { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerSyntheticRelease(); } // Constructor phases (non-nullable-refs.md, Construction protocol): // the value phase and written-body phase of a superclass // constructor, lowered from the same AST (or synthesized shape) as // its factory. Dispatch is on the FUNCTION's method name — the node, // when present, is the factory's `` MethodDefinition. // A phase function's core (explode.zena) has no method name of its // own; it runs the phase its forwarder runs. let explosion = func.explosion; if (explosion != null && explosion.core === func) { let forwarderMeth = explosion.forwarder.methodName; if (forwarderMeth != null) { let fm = forwarderMeth as String; let init = fm == '' || fm.startsWith('.'); if (init || fm == '' || fm.startsWith('.')) { let lowerer = new FunctionLowerer(wasm, func); lowerer.ctorPhase = if (init) 1 else 2; return lowerer.lower(); } } } let phase = ctorPhaseOf(func); if (phase != 0) { let lowerer = new FunctionLowerer(wasm, func); lowerer.ctorPhase = phase; if (func.node == null) { return lowerer.lowerSynthesizedConstructor(); } return lowerer.lower(); } if (func.node == null) { if (func.isGeneratorNext) { return lowerGeneratorNext(wasm, func); } if (func.isAsyncStep) { return lowerAsyncStep(wasm, func); } if (func.isAsyncMainWrapper) { return lowerAsyncMainWrapper(wasm, func); } if (func.isComponentAdapter) { return lowerComponentAdapter(wasm, func); } if (func.interfaceTrampolineTarget != null) { return synthesizeInterfaceTrampoline(wasm, func); } if (func.closureForwarder != null) { return lowerClosureForwarder(wasm, func); } if (func.existentialForward != null) { return lowerExistentialForward(func); } if (func.getterStruct != null) { return synthesizeAccessor(func, true); } if (func.setterStruct != null) { return synthesizeAccessor(func, false); } if (func.isClosureWrapper) { return synthesizeClosureWrapper(func); } if (func.decoratorAccessorTarget != null) { return synthesizeDecoratorAccessorWrapper(func); } if (func.isClosureTrampoline) { return synthesizeClosureTrampoline(func); } if (func.isFunctionAdaptation) { return lowerFunctionAdaptation(wasm, func); } if (func.name == '__start') { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerStart(); } // Named module-scaffold helpers ($stringCreate & co.): fixed // shapes synthesized instruction-for-instruction from streaming's // generators. let scaffold = synthesizeScaffold(wasm, func); if (scaffold != null) { return scaffold; } if (func.intrinsic != null) { return synthesizeIntrinsic(func); } if (func.methodName != null && (func.methodName as String) == '') { let lowerer = new FunctionLowerer(wasm, func); return lowerer.lowerSynthesizedConstructor(); } // Vtable slot stubs: the vtable struct declares one exactly-typed // funcref field per method of the hierarchy, so a method with no // proven call still needs a function of the right signature in its // slot. Streaming emits a bare `unreachable` for these; mirror it — // a dispatch landing here means liveness was wrong, so trap loudly. if (func.isVtableSlotStub) { let b = new IrBuilder(); synthEntryParams(b, func); b.unreachable(); return b.finish(); } throw new ZirUnsupported("synthesized function"); } // Generators: the split pass (ir/generators.zena) prepared the ramp // body before layout; the state-machine body lives on the // synthesized next() function routed above. if (func.isGeneratorRamp) { return lowerGeneratorRamp(wasm, func); } // Async functions: same shape, prepared by ir/async.zena. if (func.isAsyncRamp) { return lowerAsyncRamp(wasm, func); } var isGenFn = false; var isAsyncFn = false; match (func.node as Node) { case FunctionExpression {isGenerator, isAsync}: { isGenFn = isGenerator; isAsyncFn = isAsync; } case MethodDefinition {isGenerator, isAsync}: { isGenFn = isGenerator; isAsyncFn = isAsync; } case _: {} } // An exploded generator's or async function's forwarder shares its // node with the core the split prepared, but only reads the record's // fields and calls that core (`lower()` routes it there). if (explosion != null && explosion.forwarder === func) { isGenFn = false; isAsyncFn = false; } if (isGenFn) { let genName = if (func.name != null) func.name as String else 'anonymous'; throw new Error( `generator was not prepared by the split pass [in ${genName}]`); } if (isAsyncFn) { let asyncName = if (func.name != null) func.name as String else 'anonymous'; throw new Error( `async function was not prepared by the split pass [in ${asyncName}]`); } // No adaptations, trampolines, synthesized accessors, imports, or // generics. Plain top-level functions, written-out method bodies, // constructors, and closure implementation bodies are eligible. // Declared intrinsic methods have no lowerable body: their // standalone functions mirror streaming (array.len real, all // others unreachable — call sites always inline the op). if (func.intrinsic != null) { return synthesizeIntrinsic(func); } // Written accessor bodies (grouped accessors) lower like methods; // getterStruct/setterStruct only routes to synthesis when there is // no AST node (field-backed synthesized accessors above). if (func.isImport || func.importWrapperTarget != null || func.decoratorAccessorTarget != null || func.interfaceTrampolineTarget != null || func.isFunctionAdaptation || func.closureForwarder != null || func.isClosureWrapper || func.isClosureTrampoline) { // Wrapper flags on a node-carrying function: not a shape lowering // models (the hoisted routes above handle the synthesized ones). throw new ZirUnsupported("function shape"); } // Monomorphized generic bodies (_spec_ copies) carry both the // template's type parameters and the solved arguments; every type // query routes through substituteTypeParamsInFunctionContext, so // they lower like plain functions. A generic with no solved // arguments used to be lowered at its erasure here — nothing is // registered at an erasure now (reachability/CONTEXT.md), so reaching // this with an unsolved generic is a reachability bug, not a shape to // synthesize a solution for. if (func.typeParameters == null && func.typeArguments != null) { throw new ZirUnsupported("function shape"); } if (func.typeParameters != null && func.typeArguments == null) { throw new ZirUnsupported("generic function at its erasure"); } if (func.isClosure) { if (func.contextStruct == null) { throw new ZirUnsupported("closure without context struct"); } } else if ((func.captures ?? noCaptures).length > 0 || func.capturesThis || func.contextStruct != null) { // Capture state on a non-closure is a shape lowering doesn't know. throw new ZirUnsupported("function shape"); } if (func.node is MethodDefinition) { let md = func.node as MethodDefinition; let methName = if (md.name is Identifier) { (md.name as Identifier).name } else { "" }; let isCtor = md.isConstructor || methName == ""; // Declares/abstracts have no body; non-constructor methods with // initializer machinery would be malformed; decorated constructors // (synthetic case-class ctors) have argument-forwarding rules the // prologue lowering does not model. if (md.isDeclare || md.isAbstract || (md.typeParameters != null && func.typeArguments == null) || (!isCtor && (md.body == null || md.initializerList != null || md.superInitializer != null)) || (isCtor && md.decorators.length > 0)) { throw new ZirUnsupported("method shape"); } } else if (func.node is AccessorDeclaration || func.node is SetterInfo) { // Written grouped-accessor bodies: lowered like methods by // FunctionLowerer's accessor branches. } else if (!(func.node is FunctionExpression)) { throw new ZirUnsupported("synthesized function"); } else if (!func.isClosure && (func.classType != null || func.structType != null)) { // A non-closure FunctionExpression carrying class context // (extension methods) still bails; closures keep their enclosing // class context for `this` captures and type substitution. throw new ZirUnsupported("function shape"); } let lowerer = new FunctionLowerer(wasm, func); return lowerer.lower(); } export final class FunctionLowerer { wasm: WasmModule; func: WasmFunction; b = new IrBuilder(); // Mutable lowering state lives on the shared LoweringContext (the // split modules operate on the same env/reachability the // coordinator does); see ir/lowering-context.zena. /** Backing store for #cx(); built on first use. */ var #cxCache: LoweringContext | null = null; /** * `??` nodes whose interface adaptation the coalesce lowering has * already applied. When `??` coalesces a maybe-form, the value comes * out of a tuple slot or an optional record field, which is not an * expression the post-expression hook can reach, so the checker * hangs that arm's adaptation on the `??` itself. The arm packs * inside the coalesce; packing the merged value again here would * wrap the fat pointer in a second fat pointer. */ #coalesceArmsAdapted = new Set(); /** * Presplit-generator mode (generators.md §4): yield statements lower * to yield_ terminators, returns complete the generator (always * void), and value-block tail expressions are plain statements. The * produced body is never emitted directly — the split pass * (generators.zena) rewrites it into the frame state machine. */ var presplitGenerator = false; /** The wasm type of the yield slot, set alongside presplitGenerator. */ var presplitYieldVt: ValType | null = null; /** * Which constructor role this function lowers as * (non-nullable-refs.md, Construction protocol): 0 = the factory * (`` — computes field values, allocates with one * struct.new, runs bodies, returns the instance), 1 = a superclass's * value phase (``), 2 = a superclass's written body * (``). */ var ctorPhase = 0; /** * The allocated instance while the factory's own body statements * lower inline, or -1. A bare `return;` in that body returns it. */ var ctorFactoryThis = -1; /** * While a field default's expression lowers: the level's computed * field values, so `this.f` inside it reads the value instead of a * not-yet-existing object; the mixin scope mangles private names. */ var #ctorLevelFields: HashMap | null = null; var #ctorLevelScope = ''; var #syntheticTailReturn: Node | null = null; var #syntheticTailScope: Node | null = null; /** * Presplit-async mode (async.md §3): `await` lowers to an await_ * terminator plus a resume segment that reads the settled value back * off the awaited future, and `return e` returns the future's VALUE * type rather than the declared `Future` (the split pass rewrites * those returns into completions of the frame's own future). Like * presplitGenerator, the produced body is never emitted directly. */ var presplitAsync = false; /** * The wasm type of the async function's value slot — the `T` of its * declared `Future` — set alongside presplitAsync. Null for * `Future`, which returns no value. */ var presplitAsyncValueVt: ValType | null = null; /** * Per-await-site `Future.subscribe`, keyed by the id of * the await_ instruction that suspends on it. Each await site parks * on its OWN awaited type's specialization, so the split pass cannot * derive this from the frame — it is recorded here, where the * operand's semantic type is still in hand. */ awaitSubscribeTargets = new Map(); /** * Await sites whose `subscribe` a subclass overrides, keyed like * `awaitSubscribeTargets`: the receiver's vtable slot, loaded and * cast presplit, for the split pass to `call_ref` through. The * devirt-safety rule ordinary method calls follow — a direct call * would skip the override for every subclass of `Future`. */ awaitSubscribeCallees = new Map(); /** Await terminators that are a union await's bare-arm hop: the * split pass schedules the frame directly instead of subscribing * to a future (async.md §1, the always-async rule). */ awaitHopTerms = new Set(); /** Await terminators lexically inside a `shielded { }` region: the * split pass emits no checkpoint for them, and their resumes * re-install the shield scope as the current scope instead * (docs/design/cancellation.md §Shields). */ shieldedAwaits = new Set(); /** Nesting depth of `shielded` regions during presplit lowering; * the await sites read it to fill `shieldedAwaits`. */ var shieldDepth = 0; /** The function's clauses, when it has `ensures` clauses to run at its returns. */ var #ensures: GrowableArray | null = null; /** Whether `result` in those clauses is the function's one returned value. */ var #bindsResult = false; /** * The value of each `old(e)` in those clauses, by the call's node id. * `e` is evaluated on entry, in the entry block, which dominates * every block that reads the value. */ var #oldValues: HashMap | null = null; new(this.wasm, this.func); lower(): IrBody { this.#lowerBody(); this.#lowerPostconditions(); return this.b.finish(); } /** * The checks of the function's `ensures` clauses, in the block its * returns were redirected to, and then the return itself. The block's * parameters are the returned values, so `result` is the first. The * names a postcondition can read are the parameters, `this` and the * names around the function, none of which the body rebinds, so the * environment the body left reads them as it did on entry. */ #lowerPostconditions(): void { let ensures = this.#ensures; if (ensures == null) { return; } let block = this.b.endReturnRedirect(); // No return was emitted: the function never returns normally. if (block < 0) { return; } let cx = this.#cx(); this.b.switchTo(block); cx.reachable = true; let results = this.b.body.blocks[block].params; // An inline result is the block's parameters together, bound as an // inline parameter is: its lanes and their layout. let inlineResult = if (this.#bindsResult) this.#functionReturnLayout() else null; for (let clause in ensures) { if (clause.kind != ContractKind.Ensures) { continue; } if (this.#bindsResult) { let result = this.func.model.resolveDeclaration(clause); if (result != null) { if (inlineResult != null) { cx.env[result.id] = NO_VALUE; cx.symbols[result.id] = result; cx.inlineBindings[result.id] = new MultiValues( results.slice(0, results.length), inlineResult); } else { cx.bindPatternSym(result, results[0]); } } } lowerContractClause(cx, clause); } if (!cx.reachable) { return; } if (results.length == 0) { this.b.retVoid(); } else if (results.length == 1) { this.b.ret(results[0]); } else { this.b.retMulti(results); } } /** * The types of the values a return in this body carries: the * function's results, or in a suspending body what the split pass * expects of a presplit return. A generator's is bare, and so is an * `async gen`'s; an async function's carries the value its future * completes with, when it has one. */ #returnedValueTypes(sig: WasmSignature): GrowableArray { if (this.presplitGenerator) { return new Array(0); } if (this.presplitAsync) { let valueVt = this.presplitAsyncValueVt; return if (valueVt == null) new Array(0) else growable([valueVt]); } return sig.returns; } /** * Evaluates the operand of each `old(e)` in the function's `ensures` * clauses. The checker makes `old` the entry-value operator only * directly in a condition, so a function written inside one is not * searched. */ #lowerOldValues(contracts: GrowableArray): void { let values = new HashMap(); for (let clause in contracts) { if (clause.kind != ContractKind.Ensures) { continue; } if (!this.wasm.runsContractClause(clause)) { continue; } walk(clause.condition, (n) => { if (n is FunctionExpression) { return false; } if (n is CallExpression && isOldCallee(n.callee) && n.args.length == 1) { if (this.#cx().reachable) { values[n.id as i32] = lowerExpr(this.#cx(), n.args[0], null); } return false; } return true; }, (n) => {}); } this.#oldValues = values; } #lowerBody(): IrBody { let sig = this.func.signature; for (let rt in sig.returns) { this.#requireSupported(rt, 'return type'); } let explosion = this.func.explosion; if (explosion != null && explosion.forwarder === this.func) { return this.#lowerExplosionForwarder(explosion); } // In an exploded core, the parameters taken as their fields. let exploded: GrowableArray | null = if (explosion != null) explosion.params else null; var params: GrowableArray | null = null; var bodyNode: Node | null = null; var paramOffset = 0; var ctorDef: MethodDefinition | null = null; var extCtorDef: MethodDefinition | null = null; let node = this.func.node as Node; var setterParamIdent: Identifier | null = null; // The function's `requires` and `ensures` clauses, if it can have any. var contracts: GrowableArray | null = null; if (node is MethodDefinition) { params = node.params; bodyNode = node.body; contracts = node.contracts; if (!node.isStatic) { paramOffset = 1; } let methName = if (node.name is Identifier) { (node.name as Identifier).name } else { "" }; if (node.isConstructor || methName == "") { if (this.func.classType == null) { // An interface constructor: a factory with no receiver and no // allocation of its own — a plain function whose body returns // the instance. It is specialized like a generic top-level // function (Specializer.instantiateInterfaceConstructor). paramOffset = 0; } else if ((this.func.classType as ClassType).isExtension) { // Extension constructors take NO receiver (their signature // is the declared params, returning the underlying value). extCtorDef = node; paramOffset = 0; } else if (this.ctorPhase != 2) { // Factory and value phase run before the object exists: // declared params only, no receiver. The body phase (2) // keeps the receiver slot. ctorDef = node; paramOffset = 0; } else { ctorDef = node; } } } else if (node is AccessorDeclaration) { // Written getter body: `this` plus no declared parameters. if (node.getter == null) { this.#bail('accessor without getter body'); } params = new Array(); bodyNode = node.getter as Node; paramOffset = if (this.func.isStatic) 0 else 1; } else if (node is SetterInfo) { // Written setter body: `this` plus the single value parameter // (a bare Identifier, bound manually after the entry params). params = new Array(); bodyNode = node.body; paramOffset = if (this.func.isStatic) 0 else 1; setterParamIdent = node.param; } else { let fExpr = node as FunctionExpression; params = fExpr.params; bodyNode = fExpr.body; contracts = fExpr.contracts; if (this.func.isClosure) { // Closure implementations take their context as param 0. paramOffset = 1; } } let setterExtra = if (setterParamIdent != null) 1 else 0; // Zero-width (`void`) parameters occupy NO wasm slot: the signature // was built by mapParamTypeToWasmParams, which drops them. So both // the shape gate and the binding loop below count only the // parameters that survived, and the erased ones bind no value. // Source parameter index -> signature index, or -1 when the // parameter is zero-width and has no slot at all. An exploded // parameter's index is its first field's, and it spans one slot per // field. let paramSigIndex = new Array(); var liveParamCount = 0; for (var pi = 0; pi < params.length; pi += 1) { // An inline parameter spans its slots or fields. let inlineParam = this.#inlineParamFor(params[pi]); if (inlineParam != null) { paramSigIndex.push(liveParamCount + paramOffset); liveParamCount += inlineParam.width(); continue; } if (this.#isZeroWidthParam(params[pi])) { paramSigIndex.push(-1); } else { paramSigIndex.push(liveParamCount + paramOffset); let d = if (exploded != null) exploded[pi] else null; liveParamCount += if (d != null) d.fields.length else 1; } } if (sig.params.length != liveParamCount + paramOffset + setterExtra) { this.#bail('signature/param shape mismatch'); } let entry = this.b.newBlock(); var ctxParam = -1; if (paramOffset == 1) { this.#requireSupported(sig.params[0], 'receiver type'); let p0 = this.b.blockParam(entry, sig.params[0]); if (this.func.isClosure) { ctxParam = p0; } else { this.#cx().thisValue = p0; } } var needsThisDowncast = false; if (paramOffset == 1 && ctorDef == null && !this.func.isClosure && this.func.classType != null && this.#methodName() != null) { // Override methods receive the ROOT class's receiver type (vtable // signature compatibility) and must downcast to their own class. // An extension class has no struct to cast to: every class in the // chain takes the same erased `on` value as its receiver, which // is already the right type. let ct = this.func.classType as ClassType; let rootClass = getMemberRootClass(ct, this.#methodName() as String); if (ct != rootClass && !ct.isExtension) { needsThisDowncast = true; } } let patternParams = new Array(); let patternValues = new Array(); let explodedIndexes = new Array(); let explodedValues = new Array>(); // For an inline record parameter among them, its dispatch; an // exploded one's is the core's ExplosionSpec's. let inlineRecordDispatches = new Array(); // An inline record parameter with inline fields: held as its leaves // once instructions can emit. let leafRecordIndexes = new Array(); let leafRecordSlots = new Array>(); let leafRecordParams = new Array(); for (var i = 0; i < params.length; i += 1) { let p = params[i]; // this-params bind like plain params — the parser already // synthesized the FieldInitializer entries that store them // (lowered by the constructor prologue). Default values are // CALLER-supplied (desugared into each call site's argument // list), so the callee binds the parameter normally; call sites // that actually omit arguments still bail at 'call arity // adaptation'. let sigIndex = paramSigIndex[i]; if (sigIndex < 0) { // Zero-width parameter: no wasm slot, so nothing to bind. Every // use of it in the body is itself zero-width and lowers to no // operand (NO_VALUE), so the binding is never read. let voidSym = this.func.model.resolveDeclaration(p.name); if (voidSym != null) { this.#cx().env[voidSym.id] = NO_VALUE; this.#cx().symbols[voidSym.id] = voidSym; } if (p.pattern != null) { this.#bail('destructuring a zero-width parameter'); } continue; } let inlineParam = this.#inlineParamFor(p); if (inlineParam != null) { let slots = new Array(); for (var k = 0; k < inlineParam.width(); k += 1) { this.#requireSupported(sig.params[sigIndex + k], 'parameter type'); slots.push(this.b.blockParam(entry, sig.params[sigIndex + k])); } if (p.pattern != null) { this.#bail('destructuring an inline parameter'); } if (inlineParam.record != null && inlineParam.layout != null) { leafRecordIndexes.push(i); leafRecordSlots.push(slots); leafRecordParams.push(inlineParam); continue; } let inlineLayout = inlineParam.layout; if (inlineLayout == null) { // A record's fields: its record is rebuilt from them once // instructions can emit, as an exploded parameter's is. explodedIndexes.push(i); explodedValues.push(slots); inlineRecordDispatches.push(inlineParam.dispatch as RecordDispatchInfo); continue; } let sym = this.func.model.resolveDeclaration(p.name); if (sym != null) { this.#cx().env[sym.id] = NO_VALUE; this.#cx().symbols[sym.id] = sym; this.#cx().inlineBindings[sym.id] = new MultiValues(slots, inlineLayout); } continue; } let d = if (exploded != null) exploded[i] else null; if (d != null) { // Its record is rebuilt from these once instructions can emit. let values = new Array(); for (var k = 0; k < d.fields.length; k += 1) { this.#requireSupported(sig.params[sigIndex + k], 'parameter type'); values.push(this.b.blockParam(entry, sig.params[sigIndex + k])); } explodedIndexes.push(i); explodedValues.push(values); continue; } let vt = sig.params[sigIndex]; this.#requireSupported(vt, 'parameter type'); let value = this.b.blockParam(entry, vt); let sym = this.func.model.resolveDeclaration(p.name); if (sym != null) { this.#cx().env[sym.id] = value; this.#cx().symbols[sym.id] = sym; } // Destructuring parameters bind their pattern from the same // value once the entry block is current (below). let {pattern} = p; if (pattern != null) { patternParams.push(pattern as Node); patternValues.push(value); } else if (sym == null) { this.#bail('unresolved parameter'); } } if (setterParamIdent != null) { let sym = this.func.model.resolveDeclaration(setterParamIdent as Node); if (sym == null) { this.#bail('unresolved setter parameter'); } let vt = sig.params[paramOffset]; this.#requireSupported(vt, 'parameter type'); let value = this.b.blockParam(entry, vt); this.#cx().env[sym.id] = value; this.#cx().symbols[sym.id] = sym; } this.b.switchTo(entry); for (var r = 0; r < leafRecordIndexes.length; r += 1) { let leafParam = leafRecordParams[r]; let value = this.#heldRecordFromSlots(leafParam.record as RecordType, leafParam.layout as InlineLayout, leafRecordSlots[r]); let sym = this.func.model.resolveDeclaration(params[leafRecordIndexes[r]].name); if (sym != null) { if (this.#isCelled(sym.id)) { this.#bail('inline parameter is celled'); } this.#cx().env[sym.id] = value; this.#cx().symbols[sym.id] = sym; } } // An exploded parameter binds to a record rebuilt from its fields. // Every read of it answers from the fields (`explodedRecords`), so // the record itself is dead unless some use needs it whole. var inlineRecordIndex = 0; for (var e = 0; e < explodedIndexes.length; e += 1) { let p = params[explodedIndexes[e]]; let explodedD = exploded?[explodedIndexes[e]]; let d = if (explodedD != null) { explodedD } else { inlineRecordIndex += 1; inlineRecordDispatches[inlineRecordIndex - 1] }; let values = explodedValues[e]; let record = this.#packRecord(d, values); this.#cx().explodedRecords[record] = new ExplodedRecord(d, values); let sym = this.func.model.resolveDeclaration(p.name); if (sym != null) { if (this.#isCelled(sym.id)) { this.#bail('exploded parameter is celled'); } this.#cx().env[sym.id] = record; this.#cx().symbols[sym.id] = sym; } let {pattern} = p; if (pattern != null) { patternParams.push(pattern as Node); patternValues.push(record); } else if (sym == null) { this.#bail('unresolved parameter'); } } // Destructuring parameters bind now that instructions can emit. for (var i = 0; i < patternParams.length; i += 1) { this.#lowerPatternBindings(patternParams[i], patternValues[i]); } // Celled parameters (captured and mutated through a closure) move // into their heap cells before any body code runs. for (var i = 0; i < params.length; i += 1) { if (paramSigIndex[i] < 0) { // Nothing to spill: a zero-width parameter holds no value, so // capturing it through a closure captures nothing. continue; } if (exploded != null && exploded[i] != null) { continue; } let sym = this.func.model.resolveDeclaration(params[i].name); if (sym != null && this.#isCelled(sym.id)) { let symId = sym.id; this.#cx().env[symId] = this.#cellAlloc( sig.params[paramSigIndex[i]], this.#cx().env[symId]); } } if (needsThisDowncast) { let ct = this.func.classType as ClassType; var infoOpt: ClassCodegenInfo | null = null; if (let (true, info) = this.wasm.classInfos.get(getClassKey(ct))) { infoOpt = info; } if (infoOpt == null) { this.#bail('override class not discovered'); } let ownStruct = infoOpt.structType; let castVt = new ValTypeRef(ownStruct) as ValType; this.#cx().thisValue = this.b.emit(IrOp.ref_cast, castVt, this.#cx().thisValue, this.b.internType(castVt)); } // A consuming method's receiver is a binding to the release // bookkeeping, like an owned parameter: bound to `this`, so its // release region and a branch's compensating drop read it from the // environment. let receiverSym = this.#receiverSymbol(); if (receiverSym != null) { this.#cx().env[receiverSym.id] = this.#cx().thisValue; this.#cx().symbols[receiverSym.id] = receiverSym; } if (paramOffset == 1 && ctorDef == null && !this.func.isClosure && this.#methodName() == "==" && params.length == 1 && paramSigIndex[0] >= 0 && (exploded == null || exploded[0] == null)) { this.#lowerEqualityPrologue(params[0]); } if (this.func.isClosure) { this.#lowerClosurePrologue(ctxParam); } if (paramOffset == 1 && ctorDef == null && !this.func.isClosure && this.#methodName() == ':dispose' && this.func.classType != null && (this.func.classType as ClassType).isResource) { // A resource's consuming dispose ends the object's life: mark the // lifecycle flag before the body, so every release route agrees. // The member key is still spelled ':dispose' — the checker keys // symbol members as ':' + name; only the surface syntax moved to // `[Disposable.dispose]`. emitMarkDropped(this.#cx()); } // Preconditions run once per call, ahead of everything the function // does. A constructor's guard its prologue, which the factory and // the value phase each run; the body phase follows one of them. if (contracts != null && this.ctorPhase != 2) { for (let clause in contracts) { if (clause.kind == ContractKind.Requires) { lowerContractClause(this.#cx(), clause); } } } // Postconditions run where the function returns. From here its // returns branch to one block, which #lowerPostconditions fills in // once the body is lowered, and each `old(e)` is evaluated now, on // entry. A constructor's value phase is left out: it hands field // values to the factory that called it, before there is an object, // and the postconditions run after the body phase or the factory. // // A generator or async body is lowered here as ordinary control // flow, with plain returns, and split into a state machine // afterwards. So the same redirect serves it; what differs is what // a return carries there: nothing in a generator, and in an async // function the value its future completes with. if (contracts != null && this.wasm.runsEnsuresClause(contracts) && this.ctorPhase != 1 && extCtorDef == null) { let returned = this.#returnedValueTypes(sig); this.#ensures = contracts; this.#bindsResult = ctorDef == null && (returned.length == 1 || this.#functionReturnLayout() != null); this.#lowerOldValues(contracts); this.b.redirectReturns(returned); } if (extCtorDef != null) { this.#lowerExtensionConstructor(extCtorDef, sig); return this.b.finish(); } if (ctorDef != null) { if (this.ctorPhase == 2) { // Body phase: receiver and params are bound; just the written // statements, post-allocation. if (bodyNode != null) { this.#lowerStatement(bodyNode as Statement); } if (this.#cx().reachable) { this.b.retVoid(); } return this.b.finish(); } let level = this.#lowerCtorLevel(ctorDef); if (this.ctorPhase == 1) { this.#emitCtorInitReturn(level); return this.b.finish(); } // The factory: allocate in one struct.new, run ancestor bodies, // then the own body inline with `this` bound to the instance. let inst = this.#allocateInstance(level); this.#cx().thisValue = inst; this.ctorFactoryThis = inst; if (bodyNode != null) { this.#lowerStatement(bodyNode as Statement); } if (this.#cx().reachable) { this.b.ret(inst); } return this.b.finish(); } let body = bodyNode as Node; // Owned parameters the checker marked for release wrap the whole // body in regions, first parameter outermost. A consuming method's // receiver comes first: it is released last. let dropParamSyms = new Array(); if (receiverSym != null) { let receiverNode = (node as MethodDefinition).receiver as Node; if (this.func.model.hasScopeExitDrop(receiverNode)) { dropParamSyms.push(receiverSym); } } for (var pi = 0; pi < params.length; pi += 1) { if (this.func.model.hasScopeExitDrop(params[pi] as Node)) { let ds = this.func.model.resolveDeclaration(params[pi].name); if (ds != null) { dropParamSyms.push(ds); } } } // A resource's `:dispose` wraps its body in release regions for // the class's own affine fields (ownership.md §Affine fields). let dropFieldNames = new Array(); let dropFieldTypes = new Array(); if (this.#methodName() != null && (this.#methodName() as String) == ':dispose') { collectAffineDropFields(this.#cx(), dropFieldNames, dropFieldTypes); } if (body is BlockStatement) { // Value-block function bodies: a trailing expression statement // that conforms to the return type IS the return value (the // checker accepts exactly that shape). Generators have no return // value, so in presplit mode the tail is an ordinary statement. let stmts = body.body; // Cells for self/forward-referencing closure bindings exist // before any statement runs (see precellBindings). this.#cx().precellBindings(stmts); var tailExpr: Expression | null = null; if (!this.presplitGenerator && this.#returnSlotVt() != null && stmts.length > 0 && stmts[stmts.length - 1] is ExpressionStatement) { let te = (stmts[stmts.length - 1] as ExpressionStatement).expression; let tt = this.func.model.getNodeType(te); if (tt != null && !isVoidOrNeverType(tt)) { tailExpr = te; } } let bodyCount = if (tailExpr != null) stmts.length - 1 else stmts.length; var usingInBody = dropParamSyms.length > 0; for (var si = 0; si < bodyCount; si += 1) { let s = stmts[si]; if (s is UsingStatement) { usingInBody = true; } if (s is VariableDeclaration && declScopeDrop(this.#cx(), s)) { // An implicit-drop binding opens a region exactly like a // `using`, so a value tail needs the same return spelling. usingInBody = true; } } if (usingInBody && tailExpr != null) { // A value tail under an open `using` region has to be evaluated // where a `return` would be — inside the region, before the // release — or it would read a resource that is already gone. // Spelling it as a return is what puts it there, and reuses the // one path that already knows what a return owes. let withReturn = new Array(bodyCount); for (var si = 0; si < bodyCount; si += 1) { withReturn.push(stmts[si]); } let synRet = new ReturnStatement(tailExpr, false, tailExpr.loc); this.#syntheticTailReturn = synRet as Node; this.#syntheticTailScope = body; withReturn.push(synRet as Statement); if (dropFieldNames.length > 0) { lowerFieldDropRegions(this.#cx(), dropFieldNames, dropFieldTypes, 0, dropParamSyms, withReturn, 0, withReturn.length); } else if (dropParamSyms.length > 0) { lowerParamDropRegions(this.#cx(), dropParamSyms, 0, withReturn, 0, withReturn.length); } else { this.#lowerStatementList(withReturn, 0); } tailExpr = null; } else { let normalExit = if (tailExpr == null) body else null; if (dropFieldNames.length > 0) { lowerFieldDropRegions(this.#cx(), dropFieldNames, dropFieldTypes, 0, dropParamSyms, stmts, 0, bodyCount, normalExit); } else if (dropParamSyms.length > 0) { lowerParamDropRegions(this.#cx(), dropParamSyms, 0, stmts, 0, bodyCount, normalExit); } else { this.#lowerStatementList(stmts, 0, bodyCount, normalExit); } } if (tailExpr != null && this.#cx().reachable && this.#explodedResult() != null) { this.#returnFields(tailExpr, body); } else if (tailExpr != null && this.#cx().reachable) { let tailValue = this.#lowerExpressionCtx( tailExpr, this.#returnSlotVt()); this.#emitExitDrops(body); this.#emitCheckedRet(tailValue); } else if (this.#cx().reachable) { // A `Future` async body falls off the end like a void // function; a valued one still must have returned (below). if (sig.returns.length == 0 || this.presplitGenerator || (this.presplitAsync && this.presplitAsyncValueVt == null)) { // Falling off a generator body completes it (rewritten to the // DONE return by the split pass). this.b.retVoid(); } else { // The checker guarantees a returning path; a reachable block end // here means dead code we could not prove terminated. this.b.unreachable(); } } } else { // Expression-bodied function. if (this.presplitAsync) { // `async (): Future => expr`: the expression IS the value the // frame's future completes with (or a void body's effects). if (this.presplitAsyncValueVt == null) { this.#lowerExpression(body as Expression); this.#emitExitDrops(body); if (this.#cx().reachable) { this.b.retVoid(); } } else { let value = this.#lowerExpressionCtx( body as Expression, this.presplitAsyncValueVt); this.#emitExitDrops(body); this.#emitCheckedRet(value); } return this.b.finish(); } if (dropParamSyms.length > 0 || dropFieldNames.length > 0) { // The release region wraps a statement list; spell the // expression body as one — a return when the function produces // a value (the value computes inside the region, before the // release), a bare expression statement when it is void. let fieldsResult = this.#explodedResult() != null; if (sig.returns.length > 1 && !fieldsResult) { this.#bail('param drop multi-return'); } let bodyExpr = body as Expression; let synthesized = new Array(); var normalExit: Node | null = null; if (sig.returns.length == 1 || fieldsResult) { let synRet = new ReturnStatement(bodyExpr, false, bodyExpr.loc); this.#syntheticTailReturn = synRet as Node; this.#syntheticTailScope = body; synthesized.push(synRet as Statement); } else { normalExit = body; synthesized.push( new ExpressionStatement(bodyExpr, bodyExpr.loc) as Statement); } lowerFieldDropRegions(this.#cx(), dropFieldNames, dropFieldTypes, 0, dropParamSyms, synthesized, 0, synthesized.length, normalExit); if (this.#cx().reachable) { if (sig.returns.length == 0) { this.b.retVoid(); } else { // Every path returned inside the region. this.b.unreachable(); } } return this.b.finish(); } if (sig.returns.length == 0) { // Void body: evaluate for effects; any produced value is // simply never used (SSA has no stack to pop). this.#lowerExpression(body as Expression); this.#emitExitDrops(body); if (this.#cx().reachable) { this.b.retVoid(); } return this.b.finish(); } if (this.#explodedResult() != null) { this.#returnFields(body as Expression, body); } else if (sig.returns.length > 1) { this.#lowerMultiReturn(body as Expression, body); } else { let value = this.#lowerExpressionCtx(body as Expression, sig.returns[0]); this.#emitExitDrops(body); this.#emitCheckedRet(value); } } return this.b.finish(); } /** * Synthesized (node-less) constructors: the DEFAULT constructor of a * class with no written one, and the decorated case-class * constructor. Streaming's shapes differ from written constructors: * the default constructor calls super FIRST (forwarding its own * params) and then stores field defaults by RAW name; the case-class * constructor calls super with the receiver only, stores each case * parameter into its field, then stores field defaults with the * accessor rename applied. Both mirrored exactly. */ /** Synthesized case-class operator== (see ir/equality.zena). */ lowerErasedEqDispatch(): IrBody { return lowerErasedEqDispatch(this.#cx()); } lowerCaseClassEq(): IrBody { return lowerCaseClassEq(this.#cx()); } /** Synthesized structural `==` on records or tuples (see ir/equality.zena). */ lowerValueEqFunction(): IrBody { return lowerValueEqFunction(this.#cx()); } /** Synthesized hash of a record or tuple type (see ir/equality.zena). */ lowerValueHashFunction(): IrBody { return lowerValueHashFunction(this.#cx()); } /** A synthesized release member (see ir/control-flow.zena). */ lowerSyntheticRelease(): IrBody { return lowerSyntheticRelease(this.#cx()); } /** Synthesized case-class hashCode (see ir/equality.zena). */ lowerCaseClassHash(): IrBody { return lowerCaseClassHash(this.#cx()); } /** * Extension-class constructors have no receiver and RETURN the * underlying value: `new(len, v) : super(expr)` evaluates the * single super argument to the onType value, which becomes `this` * for any body statements and the function's result — streaming's * shape, with the this-local replaced by an SSA value. (A bare * `return;` in such a body would emit a void return against a * one-result signature; the verifier and wasm validation reject * that loudly. No stdlib extension constructor has a body today.) */ #lowerExtensionConstructor(md: MethodDefinition, sig: WasmSignature): void { if (sig.returns.length != 1) { this.#bail('extension constructor shape'); } if (md.initializerList != null) { this.#bail('extension constructor initializers'); } if (md.superInitializer == null) { this.#bail('extension constructor without super'); } let si = md.superInitializer as SuperInitializer; if (si.args.length != 1) { this.#bail('extension super arity'); } this.#requireSupported(sig.returns[0], 'extension constructor type'); var value = this.#lowerExpressionCtx(si.args[0], sig.returns[0]); value = this.#conformToSlot(value, sig.returns[0], 'extension constructor value type'); this.#cx().thisValue = value; if (md.body != null) { this.#lowerStatement(md.body as Statement); } if (this.#cx().reachable) { this.b.ret(this.#cx().thisValue); } } /** * __start: the module initializer, mirroring streaming — the shared * string segment materializes first, then each unit's non-literal * global initializers in order (memory.zena's unit before all * others, then static class fields per unit), each lowered under * ITS OWN unit's model, after the namespace-import record globals * materialize. Any bail restores the swapped model * before propagating, since streaming re-generates this function * with the same WasmFunction object. */ /** * A namespace import's record global: one closure pair per exported * function (its value trampoline when one exists), exported globals * by value, then the record's fat pointer with the shape's vtable — * streaming's __start shape, field for field. */ #lowerNamespaceImportInit(g: WasmGlobal): void { let spec = g.namespaceImport as ImportSpecifier; let moduleModel = g.model; let parentNode = moduleModel.getParent(spec); if (parentNode == null || !(parentNode is ImportDeclaration)) { this.#bail('namespace import shape'); } var canonicalPath = parentNode.moduleSpecifier.value; if (canonicalPath.startsWith('./') || canonicalPath.startsWith('../')) { let referrer = moduleModel.getParent(parentNode); if (referrer is Module) { canonicalPath = normalizePath( `${dirName(referrer.path)}/${canonicalPath}`); } } let prog = this.wasm.program; if (prog == null) { this.#bail('namespace import shape'); } if (!prog.unitsByPath.has(canonicalPath)) { this.#bail('namespace import module'); } let targetUnit = prog.unitsByPath[canonicalPath]; let exports = new Map(); collectValueExports(prog, targetUnit, exports); let rt = moduleModel.getNodeType(spec as Node); if (rt == null || !(rt is RecordType)) { this.#bail('namespace import shape'); } let fields = recordWasmFields(this.wasm, rt, null); let dispatch = this.wasm.getRecordDispatch( moduleModel, fields, sortedOptionalNames(rt)); let args = new Array(); for (let field in fields) { let fieldName = if (field.name != null) field.name as String else ''; var value = -1; if (let (true, sym) = exports.get(fieldName)) { if (let (true, f) = this.wasm.functionMap.get(sym.id)) { let closureStruct = this.wasm.getClosureStruct( f.signature.params, f.signature.returns); let refTarget = if (f.closureTrampoline != null) { f.closureTrampoline as WasmFunction } else { f }; let funcVal = this.b.emit(IrOp.ref_func, closureStruct.fields[0].type, -1, this.b.addRef(new IrRefFunc(refTarget))); let nullCtx = this.b.emit(IrOp.ref_null, new ValTypeStructRef() as ValType, -1, -1); value = this.b.emit(IrOp.struct_new, new ValTypeRef(closureStruct) as ValType, this.b.pushArgList(this.#twoValues(funcVal, nullCtx)), this.b.addRef(new IrRefType(closureStruct))); } else if (let (true, tg) = this.wasm.globalMap.get(sym.id)) { value = this.b.emit(IrOp.global_get, tg.type, -1, this.b.addRef(new IrRefGlobal(tg))); } else { value = this.b.emit(IrOp.ref_null, field.type, -1, -1); } } else { value = this.b.emit(IrOp.ref_null, field.type, -1, -1); } args.push(value); } let instance = this.b.emit(IrOp.struct_new, new ValTypeRef(dispatch.concreteType) as ValType, this.b.pushArgList(args), this.b.addRef(new IrRefType(dispatch.concreteType))); let packed = this.b.emit(IrOp.iface_pack, new ValTypeRef(dispatch.fatPtrType) as ValType, instance, this.b.addRef(new IrRefGlobal(dispatch.vtableGlobal))); this.b.emitVoid(IrOp.global_set, packed, this.b.addRef(new IrRefGlobal(g))); } lowerStart(): IrBody { let entry = this.b.newBlock(); this.b.switchTo(entry); let {sharedStringsGlobal} = this.wasm; if (sharedStringsGlobal != null) { let byteArr = this.wasm.getByteArrayType(); let zero = this.b.constI32(0, new ValTypeI32()); let len = this.b.constI32(this.wasm.sharedStringsLength, new ValTypeI32()); let bytes = this.b.arrayNewData(zero, len, this.wasm.sharedStringsDataIdx, this.b.addRef(new IrRefType(byteArr)), new ValTypeRef(byteArr) as ValType); let g = sharedStringsGlobal; this.b.emitVoid(IrOp.global_set, bytes, this.b.addRef(new IrRefGlobal(g))); } if (targetUsesJsStrings(this.wasm.target)) { let sc = this.wasm.stringClass; if (sc != null) { let sKey = getClassKey(sc as ClassType); let ctorKey = getClassMethodKey(sKey, ".#fromHandle"); let ctor = if (let (true, c) = this.wasm.classMethodMap.get(ctorKey)) c else null; if (ctor != null) { let funcRef = this.b.addRef(new IrRefFunc(ctor)); for (let strVal in this.wasm.importedStringGlobals.keys()) { let g = this.wasm.importedStringGlobals[strVal]; let gw = this.wasm.getImportedStringWrapperGlobal(strVal, g.model); let rawExt = this.b.emit(IrOp.global_get, g.type, -1, this.b.addRef(new IrRefGlobal(g))); let strInst = this.b.callFunc(funcRef, oneValue(rawExt), ctor.signature.returns[0]); this.b.emitVoid(IrOp.global_set, strInst, this.b.addRef(new IrRefGlobal(gw))); } } } } for (let g in this.wasm.globals) { if (g.namespaceImport != null) { this.#lowerNamespaceImportInit(g); } } let prog = this.wasm.program; if (prog != null) { for (let unit in prog.units) { if (unit.model.sourcePath.contains('memory.zena')) { this.#lowerUnitGlobalInits(unit); } } for (let unit in prog.units) { if (!unit.model.sourcePath.contains('memory.zena')) { this.#lowerUnitGlobalInits(unit); } } } this.b.retVoid(); return this.b.finish(); } /** One unit's non-literal global initializers, streaming's order. */ #lowerUnitGlobalInits(unit: CompilationUnit): void { let {model} = unit; for (let stmt in unit.ast.body) { if (stmt is VariableDeclaration) { if (stmt.pattern is IdentifierPattern) { let sym = model.resolveDeclaration(stmt.pattern); if (sym != null) { if (let (true, g) = this.wasm.globalMap.get(sym.id)) { if (g.initExpr != null && !this.#isLiteralInit(g.initExpr as Expression, g.type)) { this.#lowerGlobalInit(g); } } } } } else if (stmt is ClassDeclaration) { for (let member in stmt.body) { if (member is FieldDefinition) { if (member.isStatic && member.value != null) { if (member.name is Identifier) { let expected = `${stmt.name.name}_${(member.name as Identifier).name}`; for (let g in this.wasm.globals) { if (g.name == expected && !this.#isLiteralInit(member.value as Expression, g.type)) { this.#lowerGlobalInit(g); } } } } } } } } } /** Numeric/boolean scalar literals init in the global's own initializer. */ #isLiteralInit(e: Expression, globalType: ValType): boolean { if (e is NumberLiteral) { return !isRefValType(globalType); } return e is BooleanLiteral; } /** * Lowers one global's initializer under the global's OWN model — * restoring the swap even when the expression throws so no caller * ever observes a leaked swap. */ #lowerGlobalInit(g: WasmGlobal): void { let saved = this.func.model; this.func.model = g.model; let value = try { this.#lowerExpressionCtx(g.initExpr as Expression, g.type) } catch (e) { this.func.model = saved; throw e }; this.func.model = saved; if (!isValSubtype(this.#valueType(value), g.type)) { this.#failConformance(value, g.type, 'global init type'); } this.b.emitVoid(IrOp.global_set, value, this.b.addRef(new IrRefGlobal(g))); } lowerSynthesizedConstructor(): IrBody { let sig = this.func.signature; if (this.func.classType == null || this.func.structType == null) { this.#bail('constructor without class'); } let ct = this.func.classType as ClassType; let structType = this.func.structType as WasmStruct; if (ct.isExtension) { this.#bail('extension constructor'); } if (this.ctorPhase == 0 && sig.returns.length != 1) { this.#bail('constructor shape'); } if (this.ctorPhase == 2) { // Synthesized constructors have no written body to phase out. this.#bail('synthesized constructor body phase'); } let {classDecl} = this.func; // Sealed-class variants (`case Lit(...)`) declare via a // SealedVariant node: no class body, so no local field defaults — // inherited initializers run in the base's value phase. var cdOpt: ClassDeclaration | null = null; if (classDecl is ClassDeclaration) { cdOpt = classDecl; } else if (classDecl == null || !(classDecl is SealedVariant)) { this.#bail('constructor declaration shape'); } let entry = this.b.newBlock(); let params = new Array(); for (let vt in sig.params) { this.#requireSupported(vt, 'constructor param type'); params.push(this.b.blockParam(entry, vt)); } this.b.switchTo(entry); let level = if (ct.isCaseClass) { this.#synthCaseLevel(ct, structType, cdOpt, params) } else { this.#synthDefaultLevel(ct, structType, cdOpt, params) }; if (this.ctorPhase == 1) { this.#emitCtorInitReturn(level); return this.b.finish(); } let inst = this.#allocateInstance(level); this.b.ret(inst); return this.b.finish(); } #synthDefaultLevel(ct: ClassType, structType: WasmStruct, cd: ClassDeclaration | null, params: GrowableArray): CtorLevel { let values = new Map(); if (cd != null) { for (let member in cd.body) { if (member is FieldDefinition) { if (member.value != null && !member.isStatic) { if (!(member.name is Identifier)) { this.#bail('field name shape'); } // Through the accessor rename: a `var(#x) x` field's // struct slot carries the setter name, initializer or no // constructor at all (issue #463 — the raw-name mirror of // the retired streaming backend dropped the initializer). this.#computeCtorField(values, ct, structType, (member.name as Identifier).name, member.value as Expression); } } } this.#mixinFieldValues(values, ct, structType, cd, false); } let superArgs = new Array(); var superWithCtor = ct.superType; while (superWithCtor != null && superWithCtor.isMixinIntermediate) { superWithCtor = superWithCtor.superType; } if (superWithCtor != null) { let superType = superWithCtor as ClassType; let ctorKey = getClassMethodKey(getClassKey(superType), ''); if (let (true, superCtor) = this.wasm.classMethodMap.get(ctorKey)) { // The default constructor forwards its own params to super. if (superCtor.signature.params.length > params.length || superCtor.signature.returns.length != 1) { this.#bail('super constructor shape'); } for (var i = 0; i < superCtor.signature.params.length; i += 1) { if (!isValSubtype(this.#valueType(params[i]), superCtor.signature.params[i])) { this.#bail('super constructor arg type'); } superArgs.push(params[i]); } } } return {values: values, superArgs: superArgs}; } /** * Field values for the fields a class gains from its mixins, after * the class's own defaults (streaming's order). Private mixin field * names mangle with the MIXIN's scope key via #computeCtorField. */ #mixinFieldValues(values: HashMap, ct: ClassType, structType: WasmStruct, cd: ClassDeclaration, mapSetterNames: boolean): void { if (cd.mixins != null && (cd.mixins as GrowableArray).length > 0) { let mixinFields = new Array(); let mixinFieldScopes = new Array(); let seenMixins = new Map(); collectMixinFields(this.wasm.program as Program, this.func.model, cd.mixins, mixinFields, mixinFieldScopes, seenMixins); for (var i = 0; i < mixinFields.length; i += 1) { let fd = mixinFields[i]; if (fd.value != null && !fd.isStatic) { if (!(fd.name is Identifier)) { this.#bail('field name shape'); } this.#computeCtorField(values, ct, structType, (fd.name as Identifier).name, fd.value as Expression, mapSetterNames, mixinFieldScopes[i]); } } } this.#decoratorFieldValues(values, ct, structType, cd, mapSetterNames); } /** * Field values for the fields a class gains from its field decorators * (docs/design/decorators.md): each decorator's own fields with * initializers, under the application's scope key. The storage field * takes the decorated field's initializer when it has one — the class's * own-field pass already stored that, routed by #computeCtorField — and * the decorator's default otherwise. */ #decoratorFieldValues(values: HashMap, ct: ClassType, structType: WasmStruct, cd: ClassDeclaration, mapSetterNames: boolean): void { let decorated = ct.decoratedFields; if (decorated == null) { return; } for (let member in cd.body) { if (!(member is FieldDefinition) || member.isStatic) { continue; } if (!(member.name is Identifier)) { continue; } let fieldName = (member.name as Identifier).name; if (!decorated.has(fieldName)) { continue; } let df = decorated[fieldName]; let decl = findMixinDeclaration(this.wasm.program as Program, df.decorator); if (decl == null) { this.#bail('decorator declaration not found'); } let source = df.decorator.genericSource ?? df.decorator; let storageName = "#" + (source.decoratorSubjectName as String); let sharedScope = source.privateScopeKey ?? (source.name + "_This"); for (let fd in (decl as MixinDeclaration).body) { if (!(fd is FieldDefinition) || fd.isStatic || fd.value == null) { continue; } if (!(fd.name is Identifier)) { this.#bail('field name shape'); } let name = (fd.name as Identifier).name; if (name == storageName && member.value != null) { continue; } // The initializer is the decorator's body compiled under this // application: the intermediate whose scope key is the field's. // Its type parameters bind before the host's while it lowers — // a second application of the same decorator in this class // may bind them differently — and `context` in it names it. let app = this.#decoratorIntermediateOf(ct, df.scope); let savedApp = this.#cx().decoratorApplication; let savedSubst = this.func.substitutionApplication; let savedCache = this.func.codegenSubstCache; this.#cx().decoratorApplication = app; this.func.substitutionApplication = app; this.func.codegenSubstCache = null; if (fd.isShared) { // One copy per class: the first application initializes it. if (!values.has(`${sharedScope}::${name}`)) { this.#computeCtorField(values, ct, structType, name, fd.value as Expression, mapSetterNames, sharedScope); } } else { this.#computeCtorField(values, ct, structType, name, fd.value as Expression, mapSetterNames, df.scope); } this.#cx().decoratorApplication = savedApp; this.func.substitutionApplication = savedSubst; this.func.codegenSubstCache = savedCache; } } } /** The intermediate class of the decorator application scoped `scope` in `ct`'s chain. */ #decoratorIntermediateOf(ct: ClassType, scope: String): ClassType | null { var chain = ct.superType; while (chain != null && chain.isMixinIntermediate) { let sc = chain; if (sc.decoratorSubjectName != null && sc.privateScopeKey != null && (sc.privateScopeKey as String) == scope) { return sc; } chain = sc.superType; } return null; } #synthCaseLevel(ct: ClassType, structType: WasmStruct, cd: ClassDeclaration | null, params: GrowableArray): CtorLevel { var superWithCtor = ct.superType; while (superWithCtor != null && superWithCtor.isMixinIntermediate) { superWithCtor = superWithCtor.superType; } if (superWithCtor != null) { let superType = superWithCtor as ClassType; let ctorKey = getClassMethodKey(getClassKey(superType), ''); if (let (true, superCtor) = this.wasm.classMethodMap.get(ctorKey)) { // The case-class constructor passes nothing to super: the // sealed base's factory takes no arguments. if (superCtor.signature.params.length != 0 || superCtor.signature.returns.length != 1) { this.#bail('case super constructor shape'); } } } let values = new Map(); let {constructorType} = ct; if (constructorType != null) { let ctorType = constructorType; let parameterNames = ctorType.parameterNames; if (parameterNames != null) { // `params` is the lowered parameter list, which drops // zero-width parameters; walk it with its own cursor so a // case field at `T = void` (no wasm param, no struct field) // does not shift every later pairing. var wi = 0; for (var pi = 0; pi < ctorType.parameters.length; pi += 1) { if (pi >= parameterNames.length) { this.#bail('case constructor arity'); } let pSem = substituteTypeParamsInClassContext(this.wasm, ct, ctorType.parameters[pi]); if (isVoidOrNeverType(pSem)) { continue; } if (wi >= params.length) { this.#bail('case constructor arity'); } let pName = parameterNames[pi]; var lookupName = if (pName.length > 0 && pName.sliceBytes(0, 1) == "#") { getClassPrivateFieldKey(getClassKey(ct), pName) } else { pName }; let fieldIdx = structType.fieldIndexByName(lookupName); if (fieldIdx >= 0) { let fieldVt = structType.fields[fieldIdx].type; if (!isValSubtype(this.#valueType(params[wi]), fieldVt)) { this.#bail('case constructor param type'); } values[lookupName] = params[wi]; } wi += 1; } } } if (cd != null) { for (let member in cd.body) { if (member is FieldDefinition) { if (member.value != null && !member.isStatic) { // Streaming skips (not bails) non-identifier field parameterNames here. if (member.name is Identifier) { this.#computeCtorField(values, ct, structType, (member.name as Identifier).name, member.value as Expression); } } } } } return {values: values, superArgs: new Array()}; } /** * Closure implementation prologue, mirroring the streaming * generator: cast the structref context parameter to the context * struct and bind `this` (when captured) and each captured value * into the SSA environment. Celled captures bailed in pre-flight, * and captured symbols are never assigned in a lowered body (an * assignment would have made them celled), so plain environment * entries are sound — they never need merge parameters. */ #lowerClosurePrologue(ctxParam: i32): void { let cs = this.func.contextStruct as WasmStruct; if (cs.fields.length == 0) { return; } let castVt: ValType = new ValTypeRef(cs); let casted = this.b.emit(IrOp.ref_cast, castVt, ctxParam, this.b.internType(castVt)); var fieldIdx = 0; if (this.func.capturesThis) { let field = cs.fields[0]; let fieldName = field.name; if (fieldName == null) { this.#bail('unnamed context field'); } let fieldRef = this.b.addRef(new IrRefField(cs, fieldName)); this.#cx().thisValue = this.b.emit(IrOp.struct_get, field.type, casted, fieldRef); fieldIdx = 1; } let coalesced2 = this.func.captures ?? noCaptures; for (let symId in coalesced2) { let field = cs.fields[fieldIdx]; let fieldName = field.name; if (fieldName == null) { this.#bail('unnamed context field'); } this.#requireSupported(field.type, 'capture type'); let fieldRef = this.b.addRef(new IrRefField(cs, fieldName)); let value = this.b.emit(IrOp.struct_get, field.type, casted, fieldRef); this.#cx().env[symId] = value; fieldIdx += 1; } } /** * One class level's construction values (non-nullable-refs.md, * Construction protocol), evaluated with no `this` in scope, in the * order the old mutate-a-shell prologue stored them: field defaults * (own fields, then mixin fields), then the initializer list (which * carries desugared `this.x` params, overwriting defaults), then * the super() arguments. `values` is keyed by resolved struct field * name. */ #lowerCtorLevel(md: MethodDefinition): CtorLevel { if (this.func.classType == null || this.func.structType == null) { this.#bail('constructor without class'); } let ct = this.func.classType as ClassType; let structType = this.func.structType as WasmStruct; if (ct.isExtension) { this.#bail('extension constructor'); } let {classDecl} = this.func; if (classDecl == null || !(classDecl is ClassDeclaration)) { this.#bail('constructor declaration shape'); } let values = new Map(); for (let member in classDecl.body) { if (member is FieldDefinition) { if (member.value != null && !member.isStatic) { if (!(member.name is Identifier)) { this.#bail('field name shape'); } this.#computeCtorField(values, ct, structType, (member.name as Identifier).name, member.value as Expression); } } } this.#mixinFieldValues(values, ct, structType, classDecl, true); let {initializerList} = md; if (initializerList != null) { for (let init in initializerList) { if (init is FieldInitializer) { this.#computeCtorField(values, ct, structType, init.field.name, init.value); } else { this.#bail('initializer kind'); } } } let superArgs = new Array(); let {superInitializer} = md; if (superInitializer != null) { let si = superInitializer as SuperInitializer; var superWithCtor = ct.superType; while (superWithCtor != null && superWithCtor.isMixinIntermediate) { superWithCtor = superWithCtor.superType; } if (superWithCtor == null) { this.#bail('super without superclass'); } let superCtorMeth = if (si.constructorName != null) { '.' + getNodeMemberName(this.func.model, si.constructorName as Node) } else { '' }; let superKey = getClassMethodKey( getClassKey(superWithCtor), superCtorMeth); let superCtor = if (let (true, sc) = this.wasm.classMethodMap.get(superKey)) sc else null; if (superCtor == null) { this.#bail('super constructor not found'); } let superSig = superCtor.signature; let inlineParams = this.#inlineLayoutsOf(superCtor); if (superSig.params.length != this.#liveArgWidth(si.args, inlineParams)) { this.#bail('super constructor arity'); } // A super constructor whose record parameter explodes: a literal // for it is built as fields, and its record registered as held in // fields, so the phase calls (#allocateInstance) can pass the // fields and the record dies. let superExplosion = superCtor.explosion; let superSpec = if (superExplosion != null && superExplosion.forwarder === superCtor) superExplosion else null; var sIdx = 0; for (var i = 0; i < si.args.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(si.args[i], inlineLayout, superArgs); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(si.args[i])) { continue; } let paramVt = superSig.params[sIdx]; let d = if (superSpec != null && sIdx < superSpec.params.length) superSpec.params[sIdx] else null; sIdx += 1; let fields = if (d != null) this.#exactLiteralFields(si.args[i], d) else null; if (d != null && fields != null) { let record = this.#packRecord(d, fields); this.#cx().explodedRecords[record] = new ExplodedRecord(d, fields); superArgs.push(record); continue; } this.#requireSupported(paramVt, 'super arg type'); var value = this.#lowerExpressionCtx(si.args[i], paramVt); value = this.#conformToSlot(value, paramVt, 'super argument type'); superArgs.push(value); } } return {values: values, superArgs: superArgs}; } /** * Evaluates one constructor field initialization into the value map, * applying the public name -> private storage mapping (var(#x) x) * and private-field key mangling. Fields absent from the struct are * skipped WITHOUT evaluating the value, matching the streaming * backend. */ #computeCtorField(values: HashMap, ct: ClassType, structType: WasmStruct, fieldName: String, value: Expression, mapSetterNames: boolean = true, mixinScope: String = ''): void { var actualName = fieldName; // Streaming's DEFAULT-constructor path stores by the raw field name; // the written-constructor and case-class paths apply the accessor // rename first. Mirror whichever the caller mirrors. if (mapSetterNames && ct.fieldSetterNames != null) { let mapping = ct.fieldSetterNames as HashMap; if (let (true, mapped) = mapping.get(fieldName)) { actualName = mapped; } } var lookupName = actualName; // A decorated field's value — initializer, `this.x` parameter or // initializer-list entry — lands in the decorator's storage field, // whose struct name is already fully scoped. let decorated = ct.decoratedFields; if (decorated != null && mixinScope == '') { if (let (true, df) = decorated.get(fieldName)) { lookupName = df.storageKey; actualName = df.storageKey; } } if (actualName.length > 0 && actualName.sliceBytes(0, 1) == "#") { // Mixin privates are lexical to the MIXIN: their struct fields // carry the mixin's scope key, never the host class's. lookupName = if (mixinScope != '') { `${mixinScope}::${actualName}` } else { getClassPrivateFieldKey(getClassKey(ct), actualName) }; } let savedFields = this.#ctorLevelFields; let savedScope = this.#ctorLevelScope; this.#ctorLevelFields = values; this.#ctorLevelScope = mixinScope; let found = structType.fieldIndexByName(lookupName) >= 0; if (!found) { if (this.#isZeroWidthExpr(value)) { // Erased field (T = void): no slot to store into, but the // initializer still runs. this.#lowerExpression(value); } this.#ctorLevelFields = savedFields; this.#ctorLevelScope = savedScope; return; } let fieldVt = this.#fieldValType(structType, lookupName); var lowered = this.#lowerExpressionCtx(value, fieldVt); lowered = this.#conformToSlot(lowered, fieldVt, 'constructor field type'); this.#ctorLevelFields = savedFields; this.#ctorLevelScope = savedScope; values[lookupName] = lowered; } /** * A `this.f` read inside a field default: the level's computed value * for f, or f's type default when it is not yet initialized (what * struct.new_default held under the old protocol). Anything that is * not a plain field — a getter, a method — has no value yet and * bails. */ #ctorLevelFieldRead(values: HashMap, member: MemberExpression): i32 { let ct = this.func.classType as ClassType; let structType = this.func.structType as WasmStruct; if (this.#isZeroWidthExpr(member as Expression)) { return NO_VALUE; } let propName = accessKeyOf(this.func.model, member); var actualName = propName; if (ct.fieldSetterNames != null) { let mapping = ct.fieldSetterNames as HashMap; if (let (true, mapped) = mapping.get(propName)) { actualName = mapped; } } let lookupName = if (actualName.length > 0 && actualName.sliceBytes(0, 1) == "#") { if (this.#ctorLevelScope != '') { `${this.#ctorLevelScope}::${actualName}` } else { getClassPrivateFieldKey(getClassKey(ct), actualName) } } else { actualName }; if (let (true, v) = values.get(lookupName)) { return v; } let fieldIdx = structType.fieldIndexByName(lookupName); if (fieldIdx < 0) { this.#bail('field initializer reads `this.' + propName + '`, which is not a field'); } return this.#defaultValueFor(structType.fields[fieldIdx].type); } /** * The value-phase return: own field values in struct order (defaults * filled) followed by the evaluated super() arguments. */ #emitCtorInitReturn(level: CtorLevel): void { let list = this.#ctorOwnFieldValues( this.func.structType as WasmStruct, level.values); // A core with a tail returns those `super(...)` arguments as their // fields: held ones as they are, any other read here. let explosion = this.func.explosion; let tail = if (explosion != null && explosion.core === this.func) explosion.tail else null; let returns = this.func.signature.returns; for (var j = 0; j < level.superArgs.length; j += 1) { let a = level.superArgs[j]; let d = if (tail != null && j < tail.length) tail[j] else null; if (d == null) { list.push(a); continue; } if (let (true, held) = this.#cx().explodedRecords.get(a)) { if (held.dispatch === d) { for (let v in held.values) { list.push(v); } continue; } } for (let field in d.fields) { let read = this.#cx().recordDispatchRead(d, field.name as String, a); list.push(this.#conformToSlot(read, returns[list.length], 'exploded field type')); } } this.b.retMulti(list); } /** * The class's own struct-field range as an ordered value list, with * each field the level did not initialize holding its type's * default — the same contents struct.new_default gave it. */ #ctorOwnFieldValues(structType: WasmStruct, values: HashMap): GrowableArray { var ownStart = 1; var s = structType.superStruct; while (s != null) { let curr = s; if (curr.classType != null && (curr.classType as ClassType).isMixinIntermediate) { s = curr.superStruct; continue; } ownStart = curr.fields.length; break; } let list = new Array(); var fi = ownStart; while (fi < structType.fields.length) { let f = structType.fields[fi]; let fname = if (f.name != null) f.name as String else ''; if (let (true, v) = values.get(fname)) { list.push(v); } else { list.push(this.#defaultValueFor(f.type)); } fi += 1; } return list; } #defaultValueFor(vt: ValType): i32 { return match (vt) { case ValTypeI32: this.b.constI32(0, vt) case ValTypeI8: this.b.constI32(0, new ValTypeI32()) case ValTypeI16: this.b.constI32(0, new ValTypeI32()) case ValTypeI64: this.b.constI64(0 as i64, vt) case ValTypeF32: this.b.constF32(0.0, vt) case ValTypeF64: this.b.constF64(0.0, vt) case ValTypeV128: this.b.constV128Zero(vt) case ValTypeRef: { // The checker requires initializing non-defaultable fields; // reaching this is a registration/lowering disagreement. this.#bailValue('non-defaultable field without initializer') } case ValTypeVoid: this.#bailValue('zero-width field slot') case _: this.b.emit(IrOp.ref_null, vt, -1, -1) }; } /** * Allocation and the post-allocation body chain. Drives the * ancestor value phases (each returning its field values plus the * arguments for ITS super), assembles every field in struct order, * allocates with one struct.new — the vtable is an operand, not a * post-allocation store — and runs the written ancestor bodies * root-first on the instance. */ #allocateInstance(level: CtorLevel): i32 { let ct = this.func.classType as ClassType; let structType = this.func.structType as WasmStruct; // Per-level own-field values, own class first, root last. let levelValues = new Array>(); levelValues.push(this.#ctorOwnFieldValues(structType, level.values)); let bodyFns = new Array(); let bodyArgs = new Array>(); var args = level.superArgs; var anc = ct.superType; var currentCtorNode = this.func.node; while (anc != null) { let ancCt = anc; if (ancCt.isMixinIntermediate) { anc = ancCt.superType; continue; } let ancKey = getClassKey(ancCt); var superCtorSuffix = ""; if (currentCtorNode is MethodDefinition) { let md = currentCtorNode as MethodDefinition; if (md.superInitializer != null) { let si = md.superInitializer as SuperInitializer; if (si.constructorName != null) { superCtorSuffix = "." + getNodeMemberName(this.func.model, si.constructorName as Node); } } } let initMethName = "" + superCtorSuffix; let bodyMethName = "" + superCtorSuffix; let initFn = if (let (true, f) = this.wasm.classMethodMap.get( getClassMethodKey(ancKey, initMethName))) f else null; if (initFn == null) { this.#bail('super constructor phases missing for ' + (ancKey as String) + ' ' + initMethName); } let ancStruct = initFn.structType as WasmStruct; var ancOwnStart = 1; var s = ancStruct.superStruct; while (s != null) { let curr = s; if (curr.classType != null && (curr.classType as ClassType).isMixinIntermediate) { s = curr.superStruct; continue; } ancOwnStart = curr.fields.length; break; } let ownCount = ancStruct.fields.length - ancOwnStart; let rets = initFn.signature.returns; let initCoreArgs = this.#explodedCallArgs(initFn, new Array(), args); var projs = new Array(); if (initCoreArgs != null) { let spec = initFn.explosion as ExplosionSpec; let tail = spec.tail; projs = if (tail != null) this.#callValuePhaseCore(spec, initCoreArgs, tail) else this.b.callFuncMulti(this.b.addRef(new IrRefFunc(spec.core)), initCoreArgs, rets); } else { projs = this.b.callFuncMulti( this.b.addRef(new IrRefFunc(initFn)), args, rets); } let vals = new Array(ownCount); var k = 0; while (k < ownCount) { vals.push(projs[k]); k += 1; } levelValues.push(vals); let nextArgs = new Array(projs.length); while (k < projs.length) { nextArgs.push(projs[k]); k += 1; } let bodyFn = if (let (true, f) = this.wasm.classMethodMap.get( getClassMethodKey(ancKey, bodyMethName))) f else null; bodyFns.push(bodyFn); bodyArgs.push(args); args = nextArgs; currentCtorNode = if (initFn != null) initFn.node else null; anc = ancCt.superType; } let structArgs = growable([this.#ctorVtableValue(structType)]); var li = levelValues.length - 1; while (li >= 0) { for (let v in levelValues[li]) { structArgs.push(v); } li -= 1; } if (structArgs.length != structType.fields.length) { this.#bail('constructor field arity for ' + (getClassKey(ct) as String)); } let vt: ValType = new ValTypeRef(structType); let inst = this.b.emit(IrOp.struct_new, vt, this.b.pushArgList(structArgs), this.b.addRef(new IrRefType(structType))); this.#cx().exactValues.add(inst); var bi = bodyFns.length - 1; while (bi >= 0) { let bf = bodyFns[bi]; if (bf != null) { let receiver = growable([inst]); let coreArgs = this.#explodedCallArgs(bf, receiver, bodyArgs[bi]); if (coreArgs != null) { this.b.callFunc(this.b.addRef(new IrRefFunc( (bf.explosion as ExplosionSpec).core)), coreArgs, null); } else { let callArgs = growable([inst]); for (let a in bodyArgs[bi]) { callArgs.push(a); } this.b.callFunc(this.b.addRef(new IrRefFunc(bf)), callArgs, null); } } bi -= 1; } return inst; } /** * The arguments for the core of constructor phase `fn`, when it has * one: `leading` as they are, then each exploded argument as its * fields. A record this function already holds as fields * (`explodedRecords`) passes them; any other record is read here, once, * and registered as held, so the next phase call for the same * arguments passes the same values instead of each phase's forwarder * reading the record again. Null when `fn` has no core. */ #explodedCallArgs(fn: WasmFunction, leading: GrowableArray, args: GrowableArray): GrowableArray | null { let spec = fn.explosion; if (spec == null || spec.forwarder !== fn || args.length != spec.params.length) { return null; } let coreParams = spec.core.signature.params; let out = new Array(); for (let v in leading) { out.push(v); } for (var i = 0; i < args.length; i += 1) { let d = spec.params[i]; if (d == null) { out.push(args[i]); continue; } if (let (true, held) = this.#cx().explodedRecords.get(args[i])) { if (held.dispatch === d) { for (let v in held.values) { out.push(v); } continue; } } let reads = new Array(); for (let field in d.fields) { let read = this.#cx().recordDispatchRead( d, field.name as String, args[i]); reads.push(this.#conformToSlot(read, coreParams[out.length], 'exploded field type')); out.push(reads[reads.length - 1]); } this.#cx().explodedRecords[args[i]] = new ExplodedRecord(d, reads); } return out; } /** * The start of a declared `operator ==`: it answers only for an operand * of exactly the receiver's runtime class, and is false otherwise * (equality.md, "Overrides compare the same class"). Past the test, * `other` is seen as its declared type, which the slot's parameter may * be wider than. */ #lowerEqualityPrologue(param: Parameter): void { let ownCt = this.func.classType; if (ownCt == null || ownCt.isExtension) { return; } let otherSym = this.func.model.resolveDeclaration(param.name); if (otherSym == null) { return; } let declared = this.func.model.getBindingType(otherSym); if (declared == null || !(baseType(declared) is ClassType)) { return; } if (this.#isCelled(otherSym.id)) { this.#bail('celled operator == parameter'); } let other = this.#cx().env[otherSym.id]; let ok = exactClassTest(this.#cx(), this.#cx().thisValue, other, ownCt); let okB = this.b.newBlock(); let failB = this.b.newBlock(); this.b.brIf(ok, okB, noValues(), failB, noValues()); this.b.switchTo(failB); this.b.ret(this.b.constI32(0, new ValTypeI32())); this.b.switchTo(okB); let declVt = typeToValTypeWithSub(this.wasm, declared, this.func); let target = refTargetOf(declVt); if (target != null) { let castVt = new ValTypeRef(target) as ValType; if (!isValSubtype(this.#cx().valueTypeOf(other), castVt)) { this.#cx().env[otherSym.id] = this.b.emit(IrOp.ref_cast, castVt, other, this.b.internType(castVt)); } } } /** * The method this function's body is: its own name, or for a method's * core (explode.zena), which has none so that it never enters the class * method map, its forwarder's. */ /** * The receiver's symbol when the lowered function is a method that * declared one (`this: Own`), which the checker tracks for * release like an owned parameter. */ #receiverSymbol(): Symbol | null { let node = this.func.node; if (node == null || !(node is MethodDefinition)) { return null; } let receiver = (node as MethodDefinition).receiver; if (receiver == null) { return null; } return this.func.model.resolveDeclaration(receiver as Node); } #methodName(): String | null { let own = this.func.methodName; if (own != null) { return own; } let explosion = this.func.explosion; if (explosion != null && explosion.core === this.func && this.func.classType != null) { // Constructors and their phases keep their own handling. let name = explosion.forwarder.methodName; if (name != null && !(name as String).startsWith('<')) { return name; } } return null; } /** The operand: the class's vtable global, or null. */ #ctorVtableValue(structType: WasmStruct): i32 { let ct = this.func.classType as ClassType; let info = if (let (true, i) = this.wasm.classInfos.get( getClassKey(ct))) i else null; if (info == null) { this.#bail('constructor class not discovered'); } let vtFieldVt = this.#fieldValType(structType, ''); let {vtableGlobal} = info; if (vtableGlobal != null) { let vtg = vtableGlobal; let v = this.b.emit(IrOp.global_get, vtg.type, -1, this.b.addRef(new IrRefGlobal(vtg))); return this.#conformToSlot(v, vtFieldVt, 'vtable field type'); } return this.b.emit(IrOp.ref_null, vtFieldVt, -1, -1); } /** * Emits a return after checking the value is a wasm subtype of the * declared return type. A mismatch means an implicit conversion the * lowering doesn't model yet — most commonly packing a class instance * into an interface fat pointer — so the function bails rather than * emitting invalid wasm. */ /** * Multi-value returns: only the inline-tuple-literal form for now — * elements lower straight onto the result stack (with `_` producing * the slot's default, mirroring streaming). Boxed tuples and call * forwarding still bail. */ #lowerMultiReturn(arg: Expression, scopeExit: Node | null = null): void { if (scopeExit != null) { this.#cx().tailExitScopes.push(scopeExit); } let values = this.#lowerMultiReturnValues(arg); if (scopeExit != null) { this.#cx().tailExitScopes.pop(); this.#emitExitDrops(scopeExit); } if (this.#cx().reachable) { this.b.retMulti(values); } } /** The conformed component values of a multi-value return operand, * without emitting the return — so a `return` inside a finalizer * region can park them instead (one variable per slot). */ #lowerMultiReturnValues(arg: Expression): GrowableArray { let sig = this.func.signature; let layout = this.#functionReturnLayout(); if (layout != null && layout.slots.length != sig.returns.length) { this.#bail('multi-value return arity'); } if (arg is IfExpression || arg is MatchExpression) { // Each arm returns its own value, written into the slots as a // `return` of it would be (`returnInlineTail`): an arm has no // single value to merge. this.#cx().returnTail = true; this.#lowerExpression(arg); this.#cx().returnTail = false; if (this.#cx().reachable) { this.#bail('returned inline arm fell through'); } return new Array(); } return this.#lowerIntoSlots(arg, layout, sig.returns); } /** * The values of `arg` in the slots `slotVts` of the inline layout * `layout`: a function's results, or an inline parameter's slots * (docs/design/inline-parameters.md). A call forwards its slots, an * inline binding its own, a boxed tuple is read field by field, and a * tuple literal writes each element into its slot. `layout` is null * for a positional multi-value (no union, no nesting), whose slots * are the elements in order. */ #unreachableSlots(count: i32): GrowableArray { let none = new Array(); for (var i = 0; i < count; i += 1) { none.push(-1); } return none; } #isDivergingCall(call: CallExpression): boolean { let callT = this.func.model.getNodeType(call); return callT != null && unwrapAliases(callT) is NeverType; } #lowerIntoSlots(arg: Expression, layout: InlineLayout | null, slotVts: GrowableArray): GrowableArray { if (arg is ThrowExpression) { this.#lowerThrowStatement(arg); return this.#unreachableSlots(slotVts.length); } if (arg is IfExpression || arg is MatchExpression) { // Each arm is lowered into the slots, and the merge takes them // (MergeState.slots). this.#cx().resultSlots = new ResultSlots(layout, slotVts); this.#cx().mergedSlots = null; this.#lowerExpression(arg); this.#cx().resultSlots = null; let merged = this.#cx().mergedSlots; this.#cx().mergedSlots = null; if (merged != null) { return merged; } // Every arm ended unreachable, so nothing reads these. return this.#unreachableSlots(slotVts.length); } let held = this.#heldInline(arg); if (held != null) { if (layout == null || this.#slotsForwardDirectly(held.values, slotVts)) { return held.values; } return this.#convertInlineSlots(held.values, held.layout as InlineLayout, layout); } if (arg is CallExpression) { if (this.#isDivergingCall(arg)) { this.#lowerCall(arg); if (this.#cx().reachable) { this.b.unreachable(); this.#cx().reachable = false; } return this.#unreachableSlots(slotVts.length); } // Forwarding form: `return f(...)`. The callee's union can be a // narrower one than this function's (`Step` into // `AsyncStep`), with different slots. let outs = new Array(); let single = this.#lowerCall(arg, outs); if (outs.length == 0 && single != NO_VALUE) { // A single result: the tuple boxed. outs.push(single); } var calleeLayout = this.#callResultLayout(arg); var flat = outs; if (layout != null && outs.length == 1 && slotVts.length != 1 && (calleeLayout == null || calleeLayout.slots.length != 1)) { // The callee returns the tuple boxed: read its fields into slots. // The call's type is the inline one it converts to, so the boxed // type is the callee's. let calleeType = this.func.model.getNodeType(arg.callee); let boxedType = if (calleeType is FunctionType) { substituteTypeParamsInFunctionContext(this.wasm, this.func, calleeType.returnType) } else { null }; let unboxed = if (boxedType != null) this.#unboxTuples(outs[0], boxedType) else null; if (unboxed != null) { flat = unboxed.values; calleeLayout = unboxed.layout; } } if (layout != null && calleeLayout != null && !this.#slotsForwardDirectly(flat, slotVts)) { flat = this.#convertInlineSlots(flat, calleeLayout as InlineLayout, layout); } if (flat.length != slotVts.length) { this.#bail('multi-value return arity'); } for (var i = 0; i < flat.length; i += 1) { let have = this.#valueType(flat[i]); if (have is ValTypeNullRef && !isValSubtype(have, slotVts[i])) { // The callee's slot is `_` in every member (`nullref`), and // this function's holds something — a wider union that fills // it in a member the callee never produces. There is no // value to forward, so the slot takes its default. flat[i] = this.#defaultValueOf(slotVts[i]); continue; } if (!isValSubtype(have, slotVts[i])) { this.#failConformance(flat[i], slotVts[i], 'multi-value return type'); } } return flat; } let unionRecords = layout?.records; if (unionRecords != null) { let recordLayout = layout as InlineLayout; if (arg is RecordLiteral) { return this.#recordLiteralIntoSlots(arg, recordLayout, slotVts); } // A boxed union of records: read the member it holds. return this.#unboxRecordUnion(this.#lowerExpression(arg), recordLayout); } if (!(arg is TupleLiteral)) { // A boxed tuple returned as an inline one: read its fields into // slots, and move them into this function's. let argType = this.#substitutedNodeType(arg); if (argType == null || layout == null) { this.#bail('multi-value return'); } let unboxed = this.#unboxTuples(this.#lowerExpression(arg), argType as Type); if (unboxed == null) { this.#bail('multi-value return'); } let mv = unboxed as MultiValues; if (this.#slotsForwardDirectly(mv.values, slotVts)) { return mv.values; } return this.#convertInlineSlots(mv.values, mv.layout as InlineLayout, layout as InlineLayout); } // A literal writes its elements into the slots whatever its own type: // `make(1) != (1, 2)` has a boxed literal beside an inline result. let nt = this.func.model.getNodeType(arg); if (nt == null || !(baseType(nt) is InlineTupleType || baseType(nt) is TupleType)) { this.#bail('boxed tuple return'); } // Slots come from the FUNCTION's declared return type, not this // literal's elements: a `(false, _, Error)` member has a hole where // a `T = void` instantiation dropped a slot, and only the union of // members says which is which. if (layout != null) { let shape = layout as InlineLayout; // The members this literal can be: its literal elements rule // the others out. Each position goes in the slot they agree on. let elements = new Array(); for (let el in arg.elements) { elements.push(el); } let candidates = inlineMembersMatching(shape, elements); let values = new Array(); for (var s = 0; s < shape.slots.length; s += 1) { values.push(NO_VALUE); } // Each element by the path it stands for: a nested tuple literal at // a nested inline position is spliced in, and any other element // there gives its leaves. let entries = pathElements( shape.originals[representativeMember(shape, candidates)], elements); for (let entry in entries) { let el = entry.node as Expression; let nested = entry.nested; let record = entry.record; if (nested != null && shape.isUnionAt(entry.path)) { this.#writeNestedUnion(el, entry.path, shape, candidates, values); continue; } if (record != null) { this.#writeRecordLeaves(el, record, entry.path, shape, candidates, values); continue; } if (nested != null) { this.#writeNestedLeaves(el, nested, entry.path, shape, candidates, values); continue; } let isHole = el is Identifier && el.name == '_'; let slot = shape.pathSlot(entry.path, candidates); if (slot == -2) { this.#bail('inline union member of a returned tuple'); } if (slot < 0) { // No member has a value here at this instantiation: a hole // contributes nothing, and a real element runs for its // effects only. if (!isHole) { this.#lowerExpression(el); } continue; } let expected = shape.slots[slot]; let v = if (isHole) { this.#defaultValueOf(expected) } else { this.#lowerExpressionCtx(el, expected) }; values[slot] = this.#conformToSlot(v, expected, 'multi-value return type'); } // The slots this member does not use hold their defaults. for (var s = 0; s < values.length; s += 1) { if (values[s] == NO_VALUE) { values[s] = this.#defaultValueOf(shape.slots[s]); } } return values; } if (arg.elements.length != slotVts.length) { this.#bail('multi-value return arity'); } let values = new Array(); for (var i = 0; i < arg.elements.length; i += 1) { let el = arg.elements[i]; let expected = slotVts[i]; let v = if (el is Identifier && el.name == '_') { this.#defaultValueOf(expected) } else { this.#lowerExpressionCtx(el, expected) }; values.push(this.#conformToSlot(v, expected, 'multi-value return type')); } return values; } /** * The fields of `el`, a value of the nested inline record `record` at * `path`, written into their slots of `shape` (`#recordArgFields`): a * record literal writes them, a record held as fields passes its own, * and any other record is read through its dispatch. */ #writeRecordLeaves(el: Expression, record: RecordType, path: String, shape: InlineLayout, candidates: GrowableArray, values: GrowableArray): void { let leaves = this.#recordExprLeaves(el, record); let relative = new Array(); collectLeafPaths(recordFieldsTuple(record), '', relative); for (var k = 0; k < leaves.length && k < relative.length; k += 1) { let slot = shape.pathSlot(`${path}.${relative[k]}`, candidates); if (slot == -2) { this.#bail('inline union member of a returned tuple'); } if (slot < 0 || leaves[k] == NO_VALUE) { continue; } values[slot] = this.#conformToSlot(leaves[k], shape.slots[slot], 'multi-value return type'); } } /** * The leaves of `el`, a value of the nested inline tuple `inner` at * `path`, written into their slots of `shape`. The value is read as a * multi-value source: an inline result gives its slots, and a boxed * tuple its fields, a field that is itself a tuple read field by field. */ #writeNestedLeaves(el: Expression, inner: InlineTupleType, path: String, shape: InlineLayout, candidates: GrowableArray, values: GrowableArray): void { let source = this.#lowerMultiSource(el); let relative = new Array(); collectLeafPaths(inner, '', relative); for (let rel in relative) { let slot = shape.pathSlot(`${path}.${rel}`, candidates); if (slot == -2) { this.#bail('inline union member of a returned tuple'); } let v = this.#leafOf(source, rel); if (slot < 0 || v == NO_VALUE) { continue; } values[slot] = this.#conformToSlot(v, shape.slots[slot], 'multi-value return type'); } } /** The leaf at `path` of a multi-value source: its slot, or a boxed * tuple's field reached position by position. */ #leafOf(source: MultiValues, path: String): i32 { let layout = source.layout; if (layout != null) { let slot = layout.pathSlot(path, null); if (slot >= 0) { return source.values[slot]; } } // A boxed tuple at the head of the path: read on through its fields. let segments = path.split('.'); var head = 0; var value = if (layout != null) { let s = layout.pathSlot(segments[0], null); if (s >= 0) source.values[s] else NO_VALUE } else { if (let (true, k) = parseI32(segments[0])) { if (k < source.values.length) source.values[k] else NO_VALUE } else { NO_VALUE } }; head = 1; while (head < segments.length && value != NO_VALUE) { let st = asWasmStruct(this.#valueType(value)); if (st == null) { return this.#bailValue('nested inline leaf of a non-tuple'); } let name = segments[head]; if (st.fieldIndexByName(name) < 0) { return NO_VALUE; } value = this.b.emit(IrOp.struct_get, this.#fieldValType(st, name), value, this.b.addRef(new IrRefField(st, name))); head += 1; } return value; } /** Whether a call's results already fit this function's results slot * for slot: the same count, each a subtype or a `nullref` hole. */ #slotsForwardDirectly(values: GrowableArray, returns: GrowableArray): boolean { if (values.length != returns.length) { return false; } for (var i = 0; i < values.length; i += 1) { let have = this.#valueType(values[i]); if (!(have is ValTypeNullRef) && !isValSubtype(have, returns[i])) { return false; } } return true; } /** * Moves the slots of one inline union into the slots of a wider one. * Each source member corresponds to the destination members its * literal elements allow, and each of its values moves to the slot * those members use at the same position. A destination slot no * source value moves to takes its default. When two source slots * would land in one destination slot, choosing between them needs a * branch on the member, which is not implemented. */ #convertInlineSlots(values: GrowableArray, from: InlineLayout, to: InlineLayout): GrowableArray { let target = new Array(); // A destination slot filled from a boxed union `from` holds where // `to` distributes one: the value, read out below. let direct = new Array(); let pendingSlots = new Array(); let pendingPaths = new Array(); let pendingTypes = new Array(); let pendingCandidates = new Array>(); // A destination leaf of a nested inline tuple that `from` holds // boxed: the source slot, and the path on into its fields. let deepSource = new Array(); let deepPath = new Array(); for (var d = 0; d < to.slots.length; d += 1) { target.push(-1); direct.push(NO_VALUE); deepSource.push(-1); deepPath.push(''); } for (var m = 0; m < from.members.length; m += 1) { let candidates = inlineMembersLike(to, from.originals[m]); // By path, so a leaf of a nested inline tuple lands on the same // leaf of the wider union's. let paths = from.memberPaths[m]; for (var i = 0; i < paths.length; i += 1) { let s = from.memberSlots[m][i]; if (s < 0) { continue; } let d = to.pathSlot(paths[i], candidates); if (d == -2) { this.#bail('inline union conversion needs a branch'); } if (d < 0 && to.isUnionAt(paths[i])) { // A boxed union where `to` distributes it: unboxed below. pendingSlots.push(s); pendingPaths.push(paths[i]); pendingTypes.push(from.members[m].elementTypes[i]); pendingCandidates.push(candidates); continue; } if (d < 0) { // `to` may hold this position inline where `from` boxes it. let prefix = `${paths[i]}.`; for (var dq = 0; dq < to.slots.length; dq += 1) { let q = to.slotPaths[dq]; if (!q.startsWith(prefix) || to.pathSlot(q, candidates) != dq) { continue; } if (deepSource[dq] == -1) { deepSource[dq] = s; deepPath[dq] = q; } else if (deepSource[dq] != s) { this.#bail('inline union conversion needs a branch'); } } continue; } if (target[d] == -1) { target[d] = s; } else if (target[d] != s) { this.#bail('inline union conversion needs a branch'); } } } for (var q = 0; q < pendingSlots.length; q += 1) { let unboxed = this.#unboxTuples(values[pendingSlots[q]], pendingTypes[q]); if (unboxed == null) { this.#bail('nested inline union from a boxed value'); } let subSource = unboxed; let sub = subSource.layout as InlineLayout; // Each of the boxed union's members against the shape `to` puts // there with the same literals. let shapes = new Array(); let toShapes = shapesAt(to, pendingPaths[q], pendingCandidates[q]); for (let member in sub.originals) { var match: InlineTupleType | null = null; for (let candidate in toShapes) { if (match == null && literalsAgree(member, candidate)) { match = candidate; } } if (match == null) { this.#bail('nested inline union member without a place'); } shapes.push(match); } this.#moveNestedSlots(subSource.values, sub, shapes, pendingPaths[q], to, pendingCandidates[q], direct); } let out = new Array(); for (var d = 0; d < to.slots.length; d += 1) { let s = target[d]; if (direct[d] != NO_VALUE) { out.push(direct[d]); } else if (s >= 0) { out.push(values[s]); } else if (deepSource[d] >= 0) { out.push(this.#boxedLeaf(values[deepSource[d]], deepPath[d].split('.'), to.slots[d])); } else { out.push(this.#defaultValueOf(to.slots[d])); } } return out; } /** * The field of the boxed tuple `value` that `segments`, past the * first, lead to, conformed to `slotVt`. The tuple is null in a member * that leaves its slot empty, and then the leaf takes its default. */ #boxedLeaf(value: i32, segments: Array, slotVt: ValType): i32 { let have = this.#valueType(value); if (have is ValTypeRefNull) { let join = this.b.newBlock(); let result = this.b.blockParam(join, slotVt); let isNull = this.b.emit(IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1); let nullB = this.b.newBlock(); let readB = this.b.newBlock(); this.b.brIf(isNull, nullB, this.#noArgs(), readB, this.#noArgs()); this.b.switchTo(nullB); this.b.br(join, this.#oneArg(this.#defaultValueOf(slotVt))); this.b.switchTo(readB); let nonNull = this.b.emit(IrOp.ref_as_non_null, new ValTypeRef(have.target) as ValType, value, -1); this.b.br(join, this.#oneArg(this.#boxedLeaf(nonNull, segments, slotVt))); this.b.switchTo(join); return result; } var current = value; for (var i = 1; i < segments.length; i += 1) { let st = asWasmStruct(this.#valueType(current)); if (st == null) { return this.#bailValue('nested inline leaf of a non-tuple'); } let name = segments[i]; // A boxed record where the inline form nests its fields: leaf k is // its k-th field in dispatch order. Dispatches that share a fat // pointer share their table's layout, so any of them reads it. let recordDispatch = this.wasm.recordDispatchOfFatPointer(st); if (recordDispatch != null) { let k = if (let (true, n) = parseI32(name)) n else -1; if (k < 0 || k >= recordDispatch.fields.length) { return this.#bailValue('nested inline record leaf'); } current = this.#cx().recordDispatchRead(recordDispatch, recordDispatch.fields[k].name as String, current); continue; } if (st.fieldIndexByName(name) < 0) { return this.#defaultValueOf(slotVt); } current = this.b.emit(IrOp.struct_get, this.#fieldValType(st, name), current, this.b.addRef(new IrRefField(st, name))); } return this.#conformToSlot(current, slotVt, 'unboxing a nested tuple'); } /** The slot layout of the current function's declared (substituted) * return type, when it is an inline tuple or a union of them. Every * `return` asks, so it is worked out once. */ #functionReturnLayout(): InlineLayout | null { if (!this.#returnLayoutKnown) { this.#returnLayoutKnown = true; this.#returnLayout = this.#computeReturnLayout(); } return this.#returnLayout; } var #returnLayoutKnown: boolean = false; var #returnLayout: InlineLayout | null = null; #computeReturnLayout(): InlineLayout | null { let node = this.func.node; if (node == null) { return null; } let ft = this.func.model.getNodeType(node); if (ft == null || !(ft is FunctionType)) { return null; } let rt = substituteTypeParamsInFunctionContext( this.wasm, this.func, ft.returnType); return inlineLayoutOf(this.wasm, rt); } /** The zero value of a result slot (for `_` tuple placeholders). */ #defaultValueOf(vt: ValType): i32 { if (vt is ValTypeI32) { return this.b.constI32(0, vt); } if (vt is ValTypeI64) { return this.b.constI64(0 as i64, vt); } if (vt is ValTypeF64) { return this.b.constF64(0.0, vt); } if (vt is ValTypeF32) { return this.b.constF32(0.0, vt); } if (vt is ValTypeV128) { return this.b.constV128Zero(vt); } if (isNullableAnyRef(vt) || vt is ValTypeExternRef) { return this.b.emit(IrOp.ref_null, vt, -1, -1); } return this.#bailValue('placeholder type'); } /** * `tail return f()` in an async body (async.md §8): the frame's * future ADOPTS the callee's. Lowered as a checkpoint (before the * callee's eager ramp — a tail-recursive descent is one synchronous * cascade, and this is its only cancellation delivery point), the * call, and a `ret` of the callee's future that the split pass * rewrites into an adoption instead of a completion * (`IrBody.adoptReturns`). */ #lowerAdoptReturn(call: CallExpression): void { if (this.#cx().finallyScopes.length > 0) { this.#bail('tail return owes a finalizer'); } if (this.wasm.scopeCancellationActive) { let ck = this.wasm.getStdlibFunc('async', 'checkCancellation'); if (ck == null) { this.#bail('async tail return: checkCancellation was not reached'); } let idx = this.b.pushArgList(new Array()); this.b.emitVoid(IrOp.call, idx, this.b.addRef(new IrRefFunc(ck as WasmFunction))); } let value = this.#lowerCall(call); if (!this.#cx().reachable) { this.#bail('tail return call left the function'); } let ret = this.b.ret(value); this.b.body.adoptReturns.add(ret); this.#cx().reachable = false; } /** * The type a `return e` must conform to. Normally the function's * single wasm result; in presplit-async mode the awaited-away value * type `T` of the declared `Future` instead (§3: the split pass * turns these returns into completions of the frame's future). */ #returnSlotVt(): ValType | null { if (this.presplitAsync) { return this.presplitAsyncValueVt; } // A core returning its record's fields still returns that record in // the source: the slot is the forwarder's, or the boxed record's for // a function returning it inline. let spec = this.func.explosion; if (spec != null && spec.core === this.func && spec.result != null) { return spec.forwarder.signature.returns[0]; } let inlineRecord = inlineRecordResultOf(this.wasm, this.func); if (inlineRecord != null) { return typeToValTypeWithSub(this.wasm, inlineRecord, this.func); } let sig = this.func.signature; return if (sig.returns.length == 1) sig.returns[0] else null; } /** * The dispatch of the record this function returns as its fields: in a * core explosion made, or in a function whose result is an inline * record (docs/design/inline-parameters.md). Its returns go through * `#returnFields`. */ #explodedResult(): RecordDispatchInfo | null { let spec = this.func.explosion; if (spec != null && spec.core === this.func) { return spec.result; } if (!this.#inlineResultKnown) { this.#inlineResultKnown = true; let record = inlineRecordResultOf(this.wasm, this.func); if (record != null) { this.#inlineResult = this.wasm.findRecordDispatch( recordWasmFields(this.wasm, record, this.func), sortedOptionalNames(record)); if (this.#inlineResult == null) { this.#bail('inline record result without a dispatch'); } } } return this.#inlineResult; } var #inlineResultKnown: boolean = false; var #inlineResult: RecordDispatchInfo | null = null; /** * Returns the record `expr` evaluates to as its fields, from a core * that returns them (#explodedResult): a record literal is lowered * straight to its fields, a record held as fields (`explodedRecords`) * passes them, and any other record is read field by field. */ #returnFields(expr: Expression, exit: Node | null = null): void { let d = this.#explodedResult() as RecordDispatchInfo; let coreReturns = this.func.signature.returns; var values = new Array(); // An inline record with inline fields returns its leaves. let leafRecord = if (this.func.explosion == null) inlineRecordResultOf(this.wasm, this.func) else null; let leafLayout = if (leafRecord != null) inlineRecordFieldsLayout(this.wasm, leafRecord) else null; if (leafRecord != null && leafLayout != null) { values = this.#leavesToSlots(leafRecord, leafLayout, this.#recordExprLeaves(expr, leafRecord)); } else if (this.#isExactLiteral(expr, d)) { values = this.#exactLiteralFields(expr, d) as GrowableArray; } else { // A returned `if` or `match` merges its arms' fields instead of // their records (MergeState.fields), and gives back a record held // as the merged fields. if (expr is IfExpression || expr is MatchExpression) { this.#cx().resultFields = d; } let record = this.#lowerExpressionCtx(expr, this.#returnSlotVt()); this.#cx().resultFields = null; if (!this.#cx().reachable) { return; } var held = false; if (let (true, h) = this.#cx().explodedRecords.get(record)) { if (h.dispatch === d) { values = h.values; held = true; } } if (!held) { for (let field in d.fields) { let read = this.#cx().recordDispatchRead( d, field.name as String, record); values.push(this.#conformToSlot(read, coreReturns[values.length], 'exploded result field type')); } } } if (!this.#cx().reachable) { return; } if (exit != null) { this.#emitExitDrops(exit); } if (values.length == 1) { if (!returnThroughFinally(this.#cx(), values[0])) { this.b.ret(values[0]); } } else if (!returnMultiThroughFinally(this.#cx(), values)) { this.b.retMulti(values); } } /** * Releases the bindings the checker found still live at this early * exit, which a move elsewhere kept their scope's end from releasing * (SemanticModel.exitDropsOf). After the exit's value is computed, so * a value read from one is read first, and before control leaves. */ #emitExitDrops(exit: Node): void { if (!this.#cx().reachable) { return; } let drops = this.func.model.exitDropsOf(exit); if (drops != null) { emitExitDrops(this.#cx(), drops); } if (exit === this.#syntheticTailReturn && this.#syntheticTailScope != null) { let scopeDrops = this.func.model.exitDropsOf(this.#syntheticTailScope as Node); if (scopeDrops != null) { emitExitDrops(this.#cx(), scopeDrops); } } } /** * `tail return f(x);` (tail-calls.md §4). The call lowers exactly as * an ordinary call — which is what makes every callee shape the * backend handles a tail call — and the instruction it produced is * then rewritten in place into `ret_call`/`ret_call_ref`: same * operands, no result, and no `ret` after it. * * The rewrite needs that call to be the last instruction lowered * into the current block and to be the value being returned, which * is exactly "nothing runs between the call and the return". The * checker's rules (§3) are what keep a program from reaching a bail * here. */ #lowerTailReturn(argument: Expression | null): void { if (argument == null || !(argument is CallExpression)) { this.#bail('tail return operand is not a call'); } if (this.presplitGenerator) { this.#bail('tail return in a suspending body'); } if (this.presplitAsync) { this.#lowerAdoptReturn(argument as CallExpression); return; } if (this.#cx().finallyScopes.length > 0) { this.#bail('tail return owes a finalizer'); } let sig = this.func.signature; if (sig.returns.length > 1) { this.#bail('tail return multi-value'); } let value = this.#lowerCall(argument as CallExpression); if (!this.#cx().reachable) { this.#bail('tail return call left the function'); } let body = this.b.body; let blk = body.blocks[this.b.currentBlock()]; if (blk.insts.length == 0) { this.#bail('tail return produced no call'); } let last = blk.insts[blk.insts.length - 1]; let op = body.op(last); if (op != IrOp.call && op != IrOp.call_ref) { this.#bail('tail return: the operand did not lower to one wasm ' + 'call (an @intrinsic, or a call through a union of function ' + 'types, cannot be a tail call)'); } if (sig.returns.length == 0) { if (body.producesValue(last)) { this.#bail('tail return of a value in a void function'); } } else { if (last != value || !body.producesValue(last)) { this.#bail('tail return: the operand did not lower to one wasm ' + 'call whose result is the returned value'); } // The wasm rule is that the callee's results are assignable to // this function's. The checker made the source types identical, // so a mismatch here is a lowering bug, not a program error. if (!isValSubtype(body.resultType(last) as ValType, sig.returns[0])) { this.#bail('tail return result type'); } } body.opcode[last] = (if (op == IrOp.call) { IrOp.ret_call } else { IrOp.ret_call_ref }) as i32; body.typeId[last] = -1; } #emitCheckedRet(value: i32): void { if (!this.#cx().reachable) { // The tail expression never produced a value — every path // through it left the function already (an if/match/try all of // whose arms `return` or `throw`). There is nothing to return // and no block left to return it from. return; } let slot = this.#returnSlotVt(); if (slot == null) { this.#bail('return value in void function'); } // Returns conform like stores (the signature is the binding): a // nullable value whose non-null form conforms re-asserts (the // checker proved the return non-null), a wider ref value (an erased // eqref out of a merge-typed expression) casts down at the boundary, // and a tuple of subtypes is copied. this.b.ret(this.#conformToSlot(value, slot, 'return requires conversion')); } // -- Statements ------------------------------------------------------ /** * Lowers `body[start..]`, giving a `using` the rest of the list. * * `using` scopes what follows it, so the tail of the block belongs to * its region rather than to this loop — which is why this returns as * soon as it hands one over. */ #lowerStatementList(body: GrowableArray, start: i32, end: i32 = -1, scopeExit: Node | null = null): void { let last = if (end < 0) body.length else end; lowerStatementRange(this.#cx(), body, start, last, scopeExit); } #lowerStatement(stmt: Statement): void { if (!this.#cx().reachable) { // Statically dead statements after return/break/continue: skip. return; } match (stmt) { case BlockStatement {body}: { // Cells for self/forward-referencing closure bindings exist // before any statement runs (see precellBindings). this.#cx().precellBindings(body); this.#lowerStatementList(body, 0, -1, stmt as Node); } case UsingStatement as us: { // A `using` outside a block — `if (c) using acquire(l);` — scopes // nothing after it, so the region is empty and the release runs // immediately. Inside a block, #lowerStatementList gets there // first and hands it the rest of the block. lowerUsingRegion(this.#cx(), us, new Array(0), 0, 0); } case ShieldedStatement as ss: { lowerShieldedRegion(this.#cx(), ss); } case ExpressionStatement as es: { let expression = es.expression; // A result that owns something and is discarded: the checker // marked the statement, and the owners are released here. let releases = this.func.model.hasScopeExitDrop(es as Node); if (expression is ThrowExpression) { this.#lowerThrowStatement(expression); } else { if (expression is CallExpression && this.#isDivergingCall(expression)) { // A never-returning call ends the path like a throw: emit // the call, then close the block — nothing after it runs. // An intrinsic (cancel.raise) may have ended it already // with its own terminator; only close a block still open. this.#lowerCall(expression); if (this.#cx().reachable) { this.b.unreachable(); this.#cx().reachable = false; } } else if (expression is CallExpression && this.#isInlineTupleTyped(expression)) { // A multi-value call as a statement — a `Result` // returned for its effect: the values are produced as a // destructuring site would take them, and dropped. let source = this.#lowerMultiSource(expression); if (releases && this.#cx().reachable) { emitSlotsDrop(this.#cx(), source); } } else if (releases) { let value = this.#lowerExpression(expression); let t = this.#substitutedNodeType(expression); if (t == null) { this.#bail('untyped discarded owner'); } if (this.#cx().reachable) { emitDrop(this.#cx(), value, dropPlanOf(this.#cx(), t as Type)); } } else { this.#lowerExpression(expression); } } } case VariableDeclaration as vd: { this.#lowerVariableDeclaration(vd); } case ReturnStatement {argument, isTail}: { // A `return` inside a `finally`-protected region leaves that // region, so it owes every finalizer between here and the // function's exit: returnThroughFinally parks it at the // innermost dispatch, which replays it once the finalizer has // run (and re-enters this on the way out of the next one). if (isTail) { this.#lowerTailReturn(argument); } else if (argument == null) { this.#emitExitDrops(stmt as Node); if (this.ctorFactoryThis >= 0) { // A bare `return;` in a constructor body inlined into the // factory returns the allocated instance. if (!returnThroughFinally(this.#cx(), this.ctorFactoryThis)) { this.b.ret(this.ctorFactoryThis); } } else if (!returnThroughFinally(this.#cx(), -1)) { this.b.retVoid(); } } else if (this.#isZeroWidthExpr(argument) && (this.func.signature.returns.length == 0 || (this.presplitAsync && this.presplitAsyncValueVt == null))) { // `return this.value;` where T = void: the operand exists in // the source but not in wasm. Run it for its effects, then // return nothing. (Guarded on the signature having no result // so a `never`-typed operand — a throw — in a value-returning // function keeps its existing path.) this.#lowerExpression(argument); if (this.#cx().reachable) { this.#emitExitDrops(stmt as Node); if (!returnThroughFinally(this.#cx(), -1)) { this.b.retVoid(); } } } else if (this.#explodedResult() != null) { this.#returnFields(argument, stmt as Node); } else if (!this.presplitAsync && (this.func.signature.returns.length > 1 || this.#functionReturnLayout() != null)) { // The second disjunct: an inline-tuple return whose // zero-width lanes leave one (or zero) wasm results — // `find` at `T = void` returns `inline (boolean, T)` as a // single i32. The lane-aware path runs dropped lanes for // their effects; the value-position lowering has no lanes to // consult and bails, for a tuple literal and for a call that // returns another function's inline result alike. this.#cx().tailExitScopes.push(stmt as Node); let values = this.#lowerMultiReturnValues(argument); this.#cx().tailExitScopes.pop(); this.#emitExitDrops(stmt as Node); if (this.#cx().reachable && !returnMultiThroughFinally(this.#cx(), values)) { this.b.retMulti(values); } } else { let ctx = this.#returnSlotVt(); let value = this.#lowerExpressionCtx(argument, ctx); this.#emitExitDrops(stmt as Node); // The operand may have left the function itself (a `throw` // as the operand); #emitCheckedRet checks for that, and // parking the return has to check for it too. if (this.#cx().reachable && !returnThroughFinally(this.#cx(), value)) { this.#emitCheckedRet(value); } } this.#cx().reachable = false; } case YieldStatement {argument}: { if (!this.presplitGenerator) { // Only presplit generator bodies contain yields; the checker // rejects yield everywhere else. throw new Error('yield statement reached ZIR lowering outside a generator body'); } var value = this.#lowerExpressionCtx(argument, this.presplitYieldVt); if (this.#cx().reachable && this.presplitYieldVt != null) { value = this.#conformToSlot(value, this.presplitYieldVt as ValType, 'yield value type'); } let resume = this.b.newBlock(); this.b.yieldTo(value, resume, new Array(0)); this.b.switchTo(resume); } case IfStatement as ifStmt: { this.#lowerIf(ifStmt); } case WhileStatement as whileStmt: { this.#lowerWhile(whileStmt); } case ForStatement as forStmt: { this.#lowerFor(forStmt); } case ForInStatement as forIn: { this.#lowerForIn(forIn); } case BreakStatement {}: { if (this.#cx().loops.length == 0) { this.#bail('break outside loop'); } this.#emitExitDrops(stmt as Node); // Leaves any `finally` region opened inside this loop, and so // owes its finalizer before it reaches the exit. if (!breakThroughFinally(this.#cx())) { let loop = this.#cx().loops[this.#cx().loops.length - 1]; this.b.br(loop.exit, this.#envValues(loop.carried)); } this.#cx().reachable = false; } case ContinueStatement {}: { if (this.#cx().loops.length == 0) { this.#bail('continue outside loop'); } this.#emitExitDrops(stmt as Node); if (!continueThroughFinally(this.#cx())) { let loop = this.#cx().loops[this.#cx().loops.length - 1]; this.b.br(loop.continueTarget, this.#envValues(loop.carried)); } this.#cx().reachable = false; } case _: { this.#bail('statement kind'); } } } #lowerVariableDeclaration(vd: VariableDeclaration): void { var pat = vd.pattern; if (pat is BindingPattern) { pat = pat.pattern; } if (pat is TuplePattern) { this.#lowerTupleDeclaration(vd, pat); return; } if (pat is ArrayPattern) { this.#lowerArrayDeclaration(vd, pat); return; } if (pat is RecordPattern) { this.#lowerObjectPatternDeclaration(vd, pat); return; } if (!(pat is IdentifierPattern)) { this.#bail('destructuring declaration'); } let sym = this.func.model.resolveDeclaration(pat); if (sym == null) { this.#bail('unresolved declaration'); } let bindingType = this.func.model.getBindingType(sym); if (bindingType == null) { this.#bail('untyped binding'); } let subBinding = substituteTypeParamsInFunctionContext( this.wasm, this.func, bindingType); // An inline `var` crosses joins as its slots (#813). if (vd.kind == 'var') { let iv = this.#inlineVarOf(bindingType, subBinding); if (iv != null) { if (this.#isCelled(sym.id)) { this.#bail('inline var is celled'); } // Lowered before the env is named: an `if` or `match` value // replaces the env when it merges. let entry = iv.write(vd.init); this.#cx().inlineVars[sym.id] = iv; this.#cx().symbols[sym.id] = sym; this.#cx().env[sym.id] = entry; return; } } // An inline `let` holds its initializer's slots // (docs/design/inline-parameters.md). let inlineLayout = inlineParamLayoutOf(this.wasm, subBinding); if (inlineLayout != null) { let values = this.#lowerIntoSlots(vd.init, inlineLayout, inlineLayout.slots); if (!this.#cx().reachable) { this.#cx().env[sym.id] = NO_VALUE; this.#cx().symbols[sym.id] = sym; return; } if (this.#isCelled(sym.id)) { this.#bail('inline let is celled'); } this.#cx().env[sym.id] = NO_VALUE; this.#cx().symbols[sym.id] = sym; this.#cx().inlineBindings[sym.id] = new MultiValues(values, inlineLayout); return; } // An inline record `let` holds its initializer's fields, bound as an // inline record parameter is: to a record rebuilt from them. let inlineRecord = inlineRecordParamOf(subBinding); if (inlineRecord != null) { let param = this.#inlineParamOf(bindingType, this.func) as InlineParam; let d = param.dispatch as RecordDispatchInfo; if (this.#isCelled(sym.id)) { this.#bail('inline let is celled'); } let leafLayout = param.layout; let record = if (leafLayout != null) { this.#heldRecordFromSlots(inlineRecord, leafLayout, this.#leavesToSlots(inlineRecord, leafLayout, this.#recordExprLeaves(vd.init, inlineRecord))) } else { let fields = this.#recordArgFields(vd.init, d, inlineRecord); let packed = this.#packRecord(d, fields); this.#cx().explodedRecords[packed] = new ExplodedRecord(d, fields); packed }; this.#cx().env[sym.id] = record; this.#cx().symbols[sym.id] = sym; return; } if (isVoidOrNeverType(subBinding)) { // A zero-width binding holds no value — per specialization, the // way arguments and returns already work: `let v = await f()` at // `Future` runs the initializer for its effects and binds // nothing, and reads yield NO_VALUE the way a void parameter's // do. A celled zero-width binding stays a loud bail: the closure // capture path would try to allocate a cell for it. if (this.#isCelled(sym.id)) { this.#bail('celled zero-width binding'); } this.#lowerExpression(vd.init); this.#cx().env[sym.id] = NO_VALUE; this.#cx().symbols[sym.id] = sym; return; } let bindingVt = typeToValTypeWithSub(this.wasm, bindingType, this.func); this.#requireSupported(bindingVt, 'binding type'); let heldDispatch = this.#heldVarDispatch(sym.id); let value = if (heldDispatch != null) { let heldVal = this.#lowerHeldWrite(vd.init, heldDispatch); if (!this.#cx().reachable) { this.#cx().env[sym.id] = NO_VALUE; this.#cx().symbols[sym.id] = sym; return; } heldVal } else { let rawInit = this.#lowerExpressionCtx(vd.init, bindingVt); if (!this.#cx().reachable) { this.#cx().env[sym.id] = NO_VALUE; this.#cx().symbols[sym.id] = sym; return; } this.#conformToBinding(rawInit, bindingVt) }; if (this.#isCelled(sym.id)) { // The binding lives in a heap cell shared with capturing // closures; the environment holds the (invariant) cell ref. let symId = sym.id; if (this.#cx().precelledBindings.has(symId)) { // Pre-allocated for self/forward-referencing closures, which // already captured this cell: write through it, don't replace. this.#cx().precelledBindings.delete(symId); this.#cellWrite(this.#cx().env[symId], value); } else { this.#cx().env[symId] = this.#cellAlloc(bindingVt, value); } } else { this.#cx().env[sym.id] = value; } this.#cx().symbols[sym.id] = sym; } /** The dispatch of a `var` held as its fields (ir/explode.zena), or null. */ /** * How a `var` of type `bindingType` (`subBinding` substituted) crosses * joins when its type is inline (#813), or null when it is not: an * inline tuple or union as its slots, an inline record as its fields, * and an inline record with inline fields as its leaves' slots. Each * env entry is what the matching inline `let` binds: slots, or a * record held as them. */ #inlineVarOf(bindingType: Type, subBinding: Type): InlineVar | null { let cx = this.#cx(); let layout = inlineParamLayoutOf(this.wasm, subBinding); if (layout != null) { return new InlineVar(layout.slots, (entry) => cx.heldSlots(entry).values, (slots) => cx.holdSlots(new MultiValues(slots, layout)), (value) => cx.holdSlots(new MultiValues( this.#lowerIntoSlots(value, layout, layout.slots), layout))); } let record = inlineRecordParamOf(subBinding); if (record == null) { return null; } let param = this.#inlineParamOf(bindingType, this.func) as InlineParam; let d = param.dispatch as RecordDispatchInfo; let leafLayout = param.layout; if (leafLayout != null) { return new InlineVar(leafLayout.slots, (entry) => this.#leavesToSlots(record, leafLayout, this.#recordValueLeaves(entry, record)), (slots) => this.#heldRecordFromSlots(record, leafLayout, slots), (value) => this.#heldRecordFromSlots(record, leafLayout, this.#leavesToSlots(record, leafLayout, this.#recordExprLeaves(value, record)))); } let fieldTypes = new Array(); for (let field in d.fields) { fieldTypes.push(field.type); } return new InlineVar(fieldTypes, (entry) => cx.heldFields(d, entry), (fields) => cx.heldRecord(d, fields), (value) => cx.heldRecord(d, this.#recordArgFields(value, d, record))); } #heldVarDispatch(symId: SymbolId): RecordDispatchInfo | null { if (let (true, d) = this.wasm.fieldHeldVars.get(symId)) { return d; } return null; } /** * A value written to a held var, as a held record: a record literal * builds it from its fields, and any other value (a call whose result * explodes, another held var) is held once lowered. */ #lowerHeldWrite(expr: Expression, d: RecordDispatchInfo): i32 { let literalFields = this.#exactLiteralFields(expr, d); if (literalFields != null) { return this.#cx().heldRecord(d, literalFields); } let value = this.#lowerExpressionCtx(expr, new ValTypeRef(d.fatPtrType) as ValType); if (!this.#cx().reachable) { return NO_VALUE; } this.#cx().heldFields(d, value); return value; } #lowerLetCondition(lpc: LetPatternCondition): LetCondBinds { return lowerLetCondition(this.#cx(), lpc); } /** * Evaluates a let-condition init to its result VALUES: multi-value * direct calls (devirtualized method or top-level function) and * receiver-only interface dispatch (iterator next()). Single-result * sources come back as one-element lists. */ #lowerMultiSource(init: Expression): MultiValues { let source = this.#lowerMultiSourceValues(init); if (source.layout == null && source.values.length == 1 && !(init is TupleLiteral)) { // A boxed tuple, or a union of them, reads through its slots like // an inline value. let t = this.#substitutedNodeType(init); if (t != null) { let unboxed = this.#unboxTuples(source.values[0], t); if (unboxed != null) { return unboxed; } } } return source; } /** * A boxed tuple, or a union of boxed tuples, read into the slots its * inline form would occupy (`InlineLayout`), so that destructuring, * `if let` and `match` read it like an inline value. Null when `t` is * not tuples. * * Each member's struct is tested in turn, and the one that matches has * its fields read into its slots. Members that share a struct share * slots too, because their fields have the same wasm types: `(0, Color)` * and `(1, Size)` are one struct of two `i32`s, and the literal in * field 0 tells them apart. A join block takes every slot, with the * slots a struct does not fill at their default. */ #unboxTuples(value: i32, t: Type): MultiValues | null { let tuples = new Array(); let resolved = unwrapAliases(t); if (resolved is TupleType) { tuples.push(resolved); } else if (resolved is UnionType) { for (let member in resolved.types) { if (!(member is TupleType)) { return null; } tuples.push(member); } } else { return null; } let inlineMembers = new Array(); for (let tuple in tuples) { if (tuple.row != null) { return null; } inlineMembers.push(new InlineTupleType(tuple.elementTypes)); } let layoutType = if (inlineMembers.length == 1) { inlineMembers[0] } else { new UnionType(inlineMembers) as Type }; let layoutOpt = inlineLayoutOf(this.wasm, layoutType); if (layoutOpt == null) { return null; } let layout = layoutOpt as InlineLayout; // Each distinct struct, with the members that have it. let structs = new Array(); let structMembers = new Array>(); for (var m = 0; m < tuples.length; m += 1) { let st = this.#tupleStruct(tuples[m]); var seen = -1; for (var i = 0; i < structs.length; i += 1) { if (structs[i] === st) { seen = i; } } if (seen >= 0) { structMembers[seen].push(m); } else { structs.push(st); structMembers.push(growable([m])); } } let have = this.#valueType(value); if (structs.length == 1 && isValSubtype(have, new ValTypeRef(structs[0]) as ValType)) { return new MultiValues( this.#tupleFieldsToSlots(value, structs[0], layout, structMembers[0]), layout); } let join = this.b.newBlock(); let params = new Array(); for (let slotVt in layout.slots) { params.push(this.b.blockParam(join, slotVt)); } for (var i = 0; i < structs.length; i += 1) { let structVt = new ValTypeRef(structs[i]) as ValType; let isLast = i == structs.length - 1; var next = -1; if (!isLast) { let test = this.b.emit(IrOp.ref_test, new ValTypeI32() as ValType, value, this.b.internType(structVt)); let hit = this.b.newBlock(); next = this.b.newBlock(); this.b.brIf(test, hit, this.#noArgs(), next, this.#noArgs()); this.b.switchTo(hit); } // The last struct is the only one left, so its cast cannot fail. let cast = this.b.emit(IrOp.ref_cast, structVt, value, this.b.internType(structVt)); this.b.br(join, this.#tupleFieldsToSlots(cast, structs[i], layout, structMembers[i])); if (!isLast) { this.b.switchTo(next); } } this.b.switchTo(join); return new MultiValues(params, layout); } /** * Whether `value` is a tuple of type `tt`, which starts with a literal: * its struct, and that literal in field 0. A struct test alone is not * enough, because the members of a tuple union can share a struct: * `(0, Color)` and `(1, Size)` are both two `i32`s. */ #tupleTagTest(value: i32, tt: TupleType): i32 { let st = this.#tupleStruct(tt); let structVt = new ValTypeRef(st) as ValType; let i32Vt = new ValTypeI32() as ValType; if (isValSubtype(this.#valueType(value), structVt)) { return this.#tupleTagAndElements(value, st, tt); } let join = this.b.newBlock(); let result = this.b.blockParam(join, i32Vt); let test = this.b.emit(IrOp.ref_test, i32Vt, value, this.b.internType(structVt)); let hit = this.b.newBlock(); let miss = this.b.newBlock(); this.b.brIf(test, hit, this.#noArgs(), miss, this.#noArgs()); this.b.switchTo(hit); let cast = this.b.emit(IrOp.ref_cast, structVt, value, this.b.internType(structVt)); this.b.br(join, growable([this.#tupleTagAndElements(cast, st, tt)])); this.b.switchTo(miss); this.b.br(join, growable([this.b.constI32(0, i32Vt)])); this.b.switchTo(join); return result; } /** * Whether `value`, a tuple of struct `st`, has the literal `tt` starts * with and the elements it narrows (`tupleElementChecks`). */ #tupleTagAndElements(value: i32, st: WasmStruct, tt: TupleType): i32 { let tag = this.#tupleTagCompare(value, st, tt); if (!this.#cx().tupleNarrows(st, tt)) { return tag; } let nonNull = this.#strippedNonNull(value); let elements = this.#cx().tupleElementChecks(nonNull, st, tt, false); return this.b.binary(IrOp.i32_and, new ValTypeI32() as ValType, tag, elements); } /** Whether field 0 of a tuple struct holds the literal `tt` starts with. */ #tupleTagCompare(value: i32, st: WasmStruct, tt: TupleType): i32 { let i32Vt = new ValTypeI32() as ValType; let fieldRef = this.b.addRef(new IrRefField(st, '0')); let fieldVt = this.#fieldValType(st, '0'); let field = this.b.emit(IrOp.struct_get, fieldVt, value, fieldRef); let tag = tt.elementTypes[0]; if (let BoolLiteralType {value as expected} = tag) { return if (expected) field else this.b.emit(IrOp.i32_eqz, i32Vt, field, -1); } if (let NumLiteralType {raw} = tag) { if (let (true, parsed) = parseI64OrU64Literal(raw)) { if (fieldVt is ValTypeI32) { return this.b.binary(IrOp.i32_eq, i32Vt, field, this.b.constI32(parsed as i32, i32Vt)); } if (fieldVt is ValTypeI64) { return this.b.binary(IrOp.i64_eq, i32Vt, field, this.b.constI64(parsed, fieldVt)); } } } return this.#bailValue('tuple tag type'); } /** * A tuple struct's fields placed in the slots of the members of * `layout` that have the struct, with every other slot at its default. * Members share a struct when their fields have the same wasm types, * and a `_` in one can sit where another has a value: `(0, _)` and * `(1, i32)` are both two `i32`s, and field 1 is the second's. */ #tupleFieldsToSlots(value: i32, st: WasmStruct, layout: InlineLayout, members: GrowableArray): GrowableArray { let slots = new Array(); for (var s = 0; s < layout.slots.length; s += 1) { slots.push(NO_VALUE); } for (let m in members) { let n = layout.members[m].elementTypes.length; for (var k = 0; k < n; k += 1) { let slot = layout.slotOf(m, k); let fieldName = i32ToString(k); // A `_` in every member is a `nullref` slot with nothing to read. if (slot < 0 || slots[slot] != NO_VALUE || layout.slots[slot] is ValTypeNullRef || st.fieldIndexByName(fieldName) < 0) { continue; } let fieldRef = this.b.addRef(new IrRefField(st, fieldName)); let field = this.b.emit(IrOp.struct_get, this.#fieldValType(st, fieldName), value, fieldRef); slots[slot] = this.#conformToSlot(field, layout.slots[slot], 'unboxing a tuple'); } } for (var s = 0; s < slots.length; s += 1) { if (slots[s] == NO_VALUE) { slots[s] = this.#defaultValueOf(layout.slots[s]); } } return slots; } #lowerMultiSourceValues(init: Expression): MultiValues { let held = this.#heldInline(init); if (held != null) { return held; } let out = new Array(); // A literal with spreads is not one value per element; it lowers as // one tuple, which the destructuring site splits. if (init is TupleLiteral) { let literal = init; if (literal.spreadIndices == null) { for (let el in literal.elements) { out.push(this.#lowerExpression(el)); } return new MultiValues(out, null); } } if (!(init is CallExpression)) { // Any other expression: a single value (a boxed tuple splits at // the destructuring site). out.push(this.#lowerExpression(init)); return new MultiValues(out, null); } if (this.#isDivergingCall(init)) { this.#lowerCall(init); if (this.#cx().reachable) { this.b.unreachable(); this.#cx().reachable = false; } return new MultiValues(out, null); } if (init.callee is MemberExpression) { let member = init.callee as MemberExpression; let objT = this.#substitutedNodeType(member.object); if (objT != null && init.args.length == 0 && this.#resolveSelfType(objT) is InterfaceType) { let recv = this.#lowerExpression(member.object); let raw = this.#cx().slotCallOnValue(recv, accessKeyOf(this.func.model, member)); return this.#multiValuesOf(init, raw); } let rep = this.#lowerMethodCall(init, member, out); if (out.length == 0) { out.push(rep); } return this.#multiValuesOf(init, out); } if (init.callee is Identifier) { let rep = this.#lowerCall(init, out); if (out.length == 0) { out.push(rep); } return this.#multiValuesOf(init, out); } // Any other expression: a single value (a boxed tuple splits at // the destructuring site). out.push(this.#lowerExpression(init)); return new MultiValues(out, null); } /** * `let (a, b) = init;` — the init's values come from the same * multi-value machinery as let-conditions (direct calls, interface * next(), or a tuple literal), and each element binds like a plain * declaration: the binding's own checked type, with a runtime * downcast when the source slot is erased/nullable. */ #lowerTupleDeclaration(vd: VariableDeclaration, tp: TuplePattern): void { if (tupleRestIndexOf(tp) >= 0) { let init = vd.init; if (init == null) { this.#bail('destructuring without initializer'); } let tOpt = this.#substitutedNodeType(init); if (tOpt == null) { this.#bail('untyped destructuring source'); } let bt = unwrapAliases(tOpt); if (!(bt is TupleType)) { this.#bail('tuple rest of a non-tuple'); } let st = this.#tupleStruct(bt); let value = this.#lowerExpressionCtx(init, new ValTypeRef(st) as ValType); if (!this.#cx().reachable) { return; } this.#bindTupleWithRest(tp, st, value, bt.elementTypes.length); return; } let source = this.#lowerMultiSource(vd.init); if (!this.#cx().reachable) { return; } // One value per element, a nested tuple pattern at a nested inline // position read leaf by leaf. var elements = new Array(); // A held inline union narrowed by an `is` or a pattern reads only the // members its type still names. let sourceLayout = source.layout; let initType = this.#substitutedNodeType(vd.init); let narrowed = if (sourceLayout != null && initType != null) { this.#narrowedMembers(sourceLayout, initType) } else { null }; var values = source.patternValues(this.#cx(), tp.elements, narrowed, elements); if (values.length == 1 && tp.elements.length != 1) { // A single value can still be a BOXED tuple (fields "0"..): // non-inline tuple returns arrive as one struct reference. let unboxed = this.#unboxTupleValue(values[0], tp.elements.length); if (unboxed != null) { values = unboxed; elements = new Array(); for (let el in tp.elements) { elements.push(el); } } } if (values.length != elements.length) { this.#bail('destructuring arity'); } for (var i = 0; i < elements.length; i += 1) { let el = elements[i]; if (el == null || el is LiteralPattern || el is BooleanLiteral || el is NumberLiteral || el is StringLiteral || el is NullLiteral) { continue; } if (this.#patternBinds(el)) { this.#lowerPatternBindings(el, values[i]); } } } /** * `let [a, b, ...rest] = init;` — evaluates the init array once and binds * each element and/or rest slice. */ #lowerArrayDeclaration(vd: VariableDeclaration, ap: ArrayPattern): void { if (vd.init == null) { this.#bail('uninitialized array destructuring'); } let arrVal = this.#lowerExpression(vd.init as Expression); if (!this.#cx().reachable) { return; } for (var i = 0; i < ap.elements.length; i += 1) { let el = ap.elements[i]; if (el == null || el is LiteralPattern || el is BooleanLiteral || el is NumberLiteral || el is StringLiteral || el is NullLiteral) { continue; } if (el is RestPattern) { let restPat = (el as RestPattern).pattern; if (this.#patternBinds(restPat)) { let sliceVal = arraySliceRead(this.#cx(), arrVal, i); this.#lowerPatternBindings(restPat, sliceVal); } } else if (this.#patternBinds(el)) { let elemVal = arrayElementRead(this.#cx(), arrVal, i); this.#lowerPatternBindings(el, elemVal); } } } /** * Splits a boxed-tuple struct value (fields "0".."n-1") into its * element values, or null when the value is not that shape. */ #unboxTupleValue(value: i32, n: i32): GrowableArray | null { let st = asWasmStruct(this.#valueType(value)); if (st == null) { return null; } let out = new Array(); for (var i = 0; i < n; i += 1) { let fieldName = i32ToString(i); if (st.fieldIndexByName(fieldName) < 0) { return null; } let fieldRef = this.b.addRef(new IrRefField(st, fieldName)); out.push(this.b.emit(IrOp.struct_get, this.#fieldValType(st, fieldName), value, fieldRef)); } return out; } /** * `let { x, y } = value`: the source evaluates once; each property * binds via an ordinary member read on it (class fields/getters or * record dispatch), with the binding's own checked type. */ #lowerObjectPatternDeclaration(vd: VariableDeclaration, recordPat: RecordPattern): void { if (vd.init == null) { this.#bail('destructuring without initializer'); } let properties = recordPat.properties; let source = this.#lowerExpression(vd.init); if (!this.#cx().reachable) { return; } let initTypeOpt = this.#substitutedNodeType(vd.init); if (initTypeOpt == null) { this.#bail('untyped destructuring source'); } // A union of record types repacks to the one shape the checker // merged for this pattern; from there it destructures like any // single record. var recordSource = source; var recordTypeOpt: RecordType | null = null; let raOpt = this.func.model.getRecordUnionAdaptation(recordPat as Node); if (raOpt != null) { recordSource = this.#applyRecordUnionAdaptation(raOpt, source); recordTypeOpt = raOpt.target; } else { let initBase = unwrapAliases(getNonNullableType(initTypeOpt)); if (initBase is RecordType) { recordTypeOpt = initBase; } } if (recordTypeOpt != null) { let recordType = recordTypeOpt as RecordType; let dispatch = this.#cx().recordDispatchOf(recordType); let recv = this.#conformRecordReceiver(dispatch, recordSource); for (let prop in properties) { if (!this.#patternBinds(prop.value)) { continue; } bindRecordProp(this.#cx(), dispatch, recordType, prop, recv); } let rest = recordPat.rest; if (rest != null) { this.#lowerPatternBindings(rest, this.#recordRestValue(rest, recv)); } return; } for (let prop in properties) { if (!this.#patternBinds(prop.value)) { continue; } let read = this.#lowerObjectPropertyRead(prop.name.name, source); this.#lowerPatternBindings(prop.value, read); } } /** * Conforms a receiver to the dispatch's fat-pointer type: adapted * or wider receivers cast down; exact ones pass through. */ #conformRecordReceiver(dispatch: RecordDispatchInfo, recv: i32): i32 { let fatVt = new ValTypeRefNull(dispatch.fatPtrType) as ValType; if (isValSubtype(this.#valueType(recv), fatVt)) { return recv; } let castVt = new ValTypeRef(dispatch.fatPtrType) as ValType; return this.b.emit(IrOp.ref_cast, castVt, recv, this.b.internType(castVt)); } #lowerObjectPropertyRead(propName: String, receiver: i32): i32 { return this.#cx().objectPropertyRead(propName, receiver); } #lowerIf(stmt: IfStatement): void { lowerIf(this.#cx(), stmt); } #lowerWhile(stmt: WhileStatement): void { lowerWhile(this.#cx(), stmt); } #lowerFor(stmt: ForStatement): void { lowerFor(this.#cx(), stmt); } #lowerForIn(stmt: ForInStatement): void { lowerForIn(this.#cx(), stmt); } #slotCallSingle(recv: i32, memberName: String): i32 { return this.#cx().slotCallSingle(recv, memberName); } #slotCallMulti(recv: i32, memberName: String): GrowableArray { return this.#cx().slotCallMulti(recv, memberName); } /** * Static method calls: the receiver expression names the class (its * checked type IS the ClassType), the method resolves by walking * the class and its supertypes for an isStatic registration under * the (overload-mangled) slot name, and the call passes no * receiver — streaming's shape exactly. */ #lowerStaticCall(call: CallExpression, member: MemberExpression, multiOut: GrowableArray | null): i32 { let objectType = this.#substitutedNodeType(member.object); if (objectType == null) { this.#bail('untyped static receiver'); } let resolved = this.#resolveSelfType(objectType); if (!(resolved is ClassType)) { this.#bail('static receiver kind'); } let ct = eraseTypeParameters(this.wasm, resolved) as ClassType; let propName = accessKeyOf(this.func.model, member); let name = this.#resolveClassMethodSlotName(ct, propName, call, true); var target: WasmFunction | null = null; var curr: ClassType | null = ct; while (curr != null && target == null) { let key = getClassMethodKey(getClassKey(curr), name); if (let (true, f) = this.wasm.staticMethodMap.get(key)) { if (f.isStatic) { target = f; } } curr = curr.superType; } if (target == null) { this.#bail('static method not found'); } // A monomorphized copy keeps the declaration's type parameters // alongside the arguments it was specialized at; only an // UNspecialized generic is unlowerable here. let unspecialized = target.typeParameters != null && target.typeArguments == null; if (unspecialized || target.intrinsic != null) { this.#bail('static method shape'); } let sig = target.signature; if (sig.returns.length > 1 && multiOut == null) { this.#bail('method multi-value return'); } let inlineParams = this.#inlineLayoutsOf(target); if (sig.params.length != this.#liveArgWidth(call.args, inlineParams)) { this.#bail('static call arity'); } // A static method whose record parameters or result explode // (explode.zena): every call to it is direct, so it calls the core // as a call to a function does. let explosion = target.explosion; if (explosion != null && explosion.forwarder === target && this.#defaultArgInfo(call) == null && (this.#passesUnpackedArg(call.args, explosion) || this.#wantsResultFields(call, explosion))) { let core = explosion.core; let coreArgs = this.#lowerExplodedArgs(call.args, explosion); let explodedResult = explosion.result; if (explodedResult != null) { return this.#callForFields(core, coreArgs, explodedResult); } return this.#emitDirectCall(call, core, coreArgs, multiOut); } let args = new Array(); var sIdx = 0; for (var i = 0; i < call.args.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(call.args[i], inlineLayout, args); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(call.args[i])) { continue; } let pt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(pt, 'method param type'); var v = this.#lowerExpressionCtx(call.args[i], pt); v = this.#conformToSlot(v, pt, 'method argument type'); args.push(v); } let fr = this.b.addRef(new IrRefFunc(target)); if (sig.returns.length > 1) { for (let r in sig.returns) { this.#requireSupported(r, 'method result type'); } let projs = this.b.callFuncMulti(fr, args, sig.returns); let outs = multiOut as GrowableArray; this.#pushMultiResults(call, projs, outs); return projs[0]; } var resultType: ValType | null = null; if (sig.returns.length == 1) { this.#requireSupported(sig.returns[0], 'method result type'); resultType = sig.returns[0]; } let result = this.b.callFunc(fr, args, resultType); return this.#reassertCallResult(call, result, resultType); } /** A call's wasm results with the layout of its result type. */ #multiValuesOf(call: CallExpression, raw: GrowableArray): MultiValues { let layout = this.#callResultLayout(call); if (layout != null && raw.length != layout.slots.length) { this.#bail('multi-value slot count'); } return new MultiValues(raw, layout); } /** The slot layout of a call's (substituted) result type when it is * an inline tuple or a union of them, else null. */ #callResultLayout(call: CallExpression): InlineLayout | null { let t = this.#substitutedNodeType(call as Expression); if (t == null) { return null; } return inlineLayoutOf(this.wasm, t); } /** Pushes a multi-value call's projections into `outs`, one per * wasm slot. Readers map slots to positions through the call's * `InlineLayout` (`MultiValues.positions`). */ #pushMultiResults(call: CallExpression, projs: GrowableArray, outs: GrowableArray): void { for (let p in projs) { outs.push(p); } } /** * The checker-solved type arguments recorded on a generic call's * callee type, substituted into this function's context; null when * the callee's checked type records none (non-generic call). */ #solvedCallTypeArguments(call: CallExpression): Array | null { let ftNode = this.func.model.getNodeType(call.callee); if (ftNode == null || !(ftNode is FunctionType)) { return null; } let raw = ftNode.typeArguments; if (raw == null) { return null; } if (raw.length == 0) { return null; } let out = new Array(); for (let a in raw) { out.push(substituteTypeParamsInFunctionContext(this.wasm, this.func, a)); } return out; } /** * Direct call to a monomorphized (_spec_) copy of a top-level * generic function: the solved type arguments named a fully typed * specialization, so this is an ordinary direct call. Generic * METHODS resolve through the devirt path instead — their * specializations register under mangled classMethodMap slot names. */ #lowerSpecializedCall(call: CallExpression, target: WasmFunction, multiOut: GrowableArray | null): i32 { let sig = target.signature; if (sig.returns.length > 1 && multiOut == null) { this.#bail('method multi-value return'); } let inlineParams = this.#inlineLayoutsOf(target); if (sig.params.length != this.#liveArgWidth(call.args, inlineParams)) { this.#bail('specialized call arity'); } let defaults = this.#defaultArgInfo(call); // A specialization whose record parameter or result explodes: as // for a plain function (#lowerDirectCallTo). let explosion = target.explosion; if (defaults == null && explosion != null && explosion.forwarder === target && (this.#passesUnpackedArg(call.args, explosion) || this.#wantsResultFields(call, explosion))) { let coreArgs = this.#lowerExplodedArgs(call.args, explosion); let result = explosion.result; if (result == null) { return this.#emitDirectCall(call, explosion.core, coreArgs, multiOut); } return this.#callForFields(explosion.core, coreArgs, result); } let args = new Array(); var sIdx = 0; for (var i = 0; i < call.args.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(call.args[i], inlineLayout, args); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(call.args[i])) { continue; } let pt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(pt, 'method param type'); var v = this.#lowerArgWithDefaults(call, i, pt, defaults, -1); v = this.#conformToSlot(v, pt, 'method argument type'); args.push(v); } let fr = this.b.addRef(new IrRefFunc(target)); if (sig.returns.length > 1) { for (let r in sig.returns) { this.#requireSupported(r, 'method result type'); } let projs = this.b.callFuncMulti(fr, args, sig.returns); let outs = multiOut as GrowableArray; this.#pushMultiResults(call, projs, outs); return projs[0]; } var resultType: ValType | null = null; if (sig.returns.length == 1) { this.#requireSupported(sig.returns[0], 'method result type'); resultType = sig.returns[0]; } let result = this.b.callFunc(fr, args, resultType); return this.#reassertCallResult(call, result, resultType); } // -- Expressions ----------------------------------------------------- #lowerExpression(expr: Expression): i32 { if (expr is CallExpression) { // An inline result where one value is wanted: the checker allowed // it because the value converts to its boxed form. let t = this.#substitutedNodeType(expr); if (t != null && inlineLayoutOf(this.wasm, t) != null) { let source = this.#lowerMultiSource(expr); return this.#applyAdaptations(expr, this.#boxInline(source)); } // An inline record result: its fields, held as the record, so reads // answer from them and the record dies unless a use needs it whole. let d = this.#inlineRecordResultOfCall(expr); if (d != null) { let source = this.#lowerMultiSourceValues(expr); let callRecord = this.#inlineRecordOfCall(expr) as RecordType; let leafLayout = inlineRecordFieldsLayout(this.wasm, callRecord); if (leafLayout != null) { return this.#applyAdaptations(expr, this.#heldRecordFromSlots(callRecord, leafLayout, source.values)); } let fields = new Array(); for (var i = 0; i < source.values.length; i += 1) { fields.push(this.#conformToSlot(source.values[i], d.fields[i].type, 'inline record result field type')); } if (fields.length != d.fields.length) { this.#bail('inline record result width'); } let record = this.#packRecord(d, fields); this.#cx().explodedRecords[record] = new ExplodedRecord(d, fields); return this.#applyAdaptations(expr, record); } } let value = this.#lowerExpressionDispatch(expr); if (!this.#cx().reachable) { return value; } return this.#applyAdaptations(expr, value); } /** * An inline value's boxed form: the struct of the member it holds, * built from that member's slots. With several members the tag in * position 0 says which, so each member but the last is a branch on * its literal, and a join block takes the reference. */ #boxInline(source: MultiValues): i32 { let layoutOpt = source.layout; if (layoutOpt == null) { return this.#bailValue('boxing a value with no slots'); } let layout = layoutOpt as InlineLayout; let unionRecords = layout.records; if (unionRecords != null) { return this.#boxRecordUnion(source, layout, unionRecords); } // Each member boxes as the member it was written as, a nested union // position holding the boxed union. let boxedMembers = new Array(); let tuples = new Array(); for (var m = 0; m < layout.originals.length; m += 1) { tuples.push(boxedTupleOf(layout.written[m])); var seen = false; for (var w = 0; w < m; w += 1) { if (layout.written[w] === layout.written[m]) { seen = true; } } if (!seen) { boxedMembers.push(tuples[m]); } } if (tuples.length == 1) { return this.#buildTupleFromSlots(source.values, layout, 0, tuples[0]); } let resultVt = typeToValTypeWithSub(this.wasm, if (boxedMembers.length == 1) boxedMembers[0] else new UnionType(boxedMembers), this.func); let join = this.b.newBlock(); let result = this.b.blockParam(join, resultVt); for (var m = 0; m < tuples.length; m += 1) { if (m < tuples.length - 1) { let hit = this.b.newBlock(); let next = this.b.newBlock(); let cond = memberTest(this.#cx(), layout, source.values, m); this.b.brIf(cond, hit, this.#noArgs(), next, this.#noArgs()); this.b.switchTo(hit); this.b.br(join, growable([ this.#buildTupleFromSlots(source.values, layout, m, tuples[m])])); this.b.switchTo(next); } else { this.b.br(join, growable([ this.#buildTupleFromSlots(source.values, layout, m, tuples[m])])); } } this.b.switchTo(join); return result; } /** * The field `member` reads off a union of inline records held in slots: * its slot for the members the object's type allows, which narrowing * may have cut down to one. */ #inlineRecordUnionField(source: MultiValues, layout: InlineLayout, member: MemberExpression): i32 { let position = layout.fieldPosition(member.property.name); if (position < 0) { return this.#bailValue('field of an inline record union'); } let allowed = this.#recordMembersAllowed(layout, member.object); let value = source.valueAt(this.#cx(), i32ToString(position), allowed); if (value == NO_VALUE) { return this.#bailValue('field of an inline record union without a value'); } return value; } /** * The members of a union of inline records an expression's type leaves: * the records its narrowed type names, or every member (null). */ #recordMembersAllowed(layout: InlineLayout, e: Expression): GrowableArray | null { let records = layout.records as GrowableArray; let t = this.#substitutedNodeType(e); if (t == null) { return null; } let named = new Array(); let base = unwrapAliases(t); if (base is RecordType) { named.push(base); } else if (base is UnionType) { for (let m in base.types) { let rec = inlineRecordParamOf(m) ?? (if (unwrapAliases(m) is RecordType) unwrapAliases(m) as RecordType else null); if (rec == null) { return null; } named.push(rec); } } else { let rec = inlineRecordParamOf(t); if (rec == null) { return null; } named.push(rec); } let allowed = new Array(); var any = false; for (let rec in records) { var found = false; for (let n in named) { if (n === rec || sameRecordShape(n, rec)) { found = true; } } allowed.push(found); any = any || found; } return if (any) allowed else null; } /** * A union of inline records boxed: the record of the member it holds, * built from that member's slots and held as them, the member told by * its literal field. */ #boxRecordUnion(source: MultiValues, layout: InlineLayout, records: GrowableArray): i32 { let boxedMembers = new Array(); for (let rec in records) { boxedMembers.push(rec); } if (records.length == 1) { return this.#recordFromSlots(source.values, layout, 0, records[0]); } let resultVt = typeToValTypeWithSub(this.wasm, new UnionType(boxedMembers), this.func); let join = this.b.newBlock(); let result = this.b.blockParam(join, resultVt); for (var m = 0; m < records.length; m += 1) { if (m < records.length - 1) { let hit = this.b.newBlock(); let next = this.b.newBlock(); let cond = memberTest(this.#cx(), layout, source.values, m); this.b.brIf(cond, hit, this.#noArgs(), next, this.#noArgs()); this.b.switchTo(hit); let record = this.#recordFromSlots(source.values, layout, m, records[m]); this.b.br(join, growable([this.#conformToSlot(record, resultVt, 'boxing an inline record union')])); this.b.switchTo(next); } else { let record = this.#recordFromSlots(source.values, layout, m, records[m]); this.b.br(join, growable([this.#conformToSlot(record, resultVt, 'boxing an inline record union')])); } } this.b.switchTo(join); return result; } /** Member `m`'s record, built from its slots and held as them. */ #recordFromSlots(values: GrowableArray, layout: InlineLayout, m: i32, record: RecordType): i32 { let d = this.#recordDispatchFor(record); let fields = new Array(); for (let field in d.fields) { let position = layout.fieldPosition(field.name as String); let slot = if (position >= 0) layout.slotAt(m, i32ToString(position)) else -1; fields.push(if (slot >= 0) { this.#conformToSlot(values[slot], field.type, 'inline record union field') } else { this.#defaultValueOf(field.type) }); } let value = this.#packRecord(d, fields); this.#cx().explodedRecords[value] = new ExplodedRecord(d, fields); return value; } /** * A record literal for a union of inline records, written into the slots * of the member its literals name, properties in written order. */ #recordLiteralIntoSlots(literal: RecordLiteral, layout: InlineLayout, slotVts: GrowableArray): GrowableArray { for (let prop in literal.properties) { if (!(prop is PropertyAssignment)) { return this.#bailSlots('inline record union literal with a spread'); } } let candidates = recordMembersMatching(layout, literal); let values = new Array(); for (var t = 0; t < slotVts.length; t += 1) { values.push(NO_VALUE); } for (let prop in literal.properties) { let assignment = prop as PropertyAssignment; let position = layout.fieldPosition(assignment.name.name); let slot = layout.pathSlot(i32ToString(position), candidates); if (slot == -2) { return this.#bailSlots('inline record union literal names no one member'); } if (slot < 0) { this.#lowerExpression(assignment.value); continue; } values[slot] = this.#conformToSlot(this.#lowerExpressionCtx(assignment.value, slotVts[slot]), slotVts[slot], 'inline record union field'); } for (var t = 0; t < values.length; t += 1) { if (values[t] == NO_VALUE) { values[t] = this.#defaultValueOf(slotVts[t]); } } return values; } #bailSlots(reason: String): GrowableArray { this.#bail(reason); } /** * A boxed union of records read into the slots of a union of inline * records: each member's record tested for, by its fat pointer and, where * members share one, by its literal field, and its fields read into the * member's slots. */ #unboxRecordUnion(value: i32, layout: InlineLayout): GrowableArray { let records = layout.records as GrowableArray; let join = this.b.newBlock(); let params = new Array(); for (let vt in layout.slots) { params.push(this.b.blockParam(join, vt)); } let i32Vt = new ValTypeI32() as ValType; for (var m = 0; m < records.length; m += 1) { let d = this.#recordDispatchFor(records[m]); let fatVt = new ValTypeRef(d.fatPtrType) as ValType; let isLast = m == records.length - 1; var next = -1; if (!isLast) { let test = this.b.emit(IrOp.ref_test, i32Vt, value, this.b.internType(fatVt)); let cast = this.b.newBlock(); next = this.b.newBlock(); this.b.brIf(test, cast, this.#noArgs(), next, this.#noArgs()); this.b.switchTo(cast); } let record = this.b.emit(IrOp.ref_cast, fatVt, value, this.b.internType(fatVt)); let fields = this.#cx().recordFieldsOf(d, record); let slots = new Array(); for (var t = 0; t < layout.slots.length; t += 1) { slots.push(NO_VALUE); } for (var k = 0; k < d.fields.length; k += 1) { let position = layout.fieldPosition(d.fields[k].name as String); let slot = if (position >= 0) layout.slotAt(m, i32ToString(position)) else -1; if (slot >= 0) { slots[slot] = this.#conformToSlot(fields[k], layout.slots[slot], 'inline record union field'); } } for (var t = 0; t < slots.length; t += 1) { if (slots[t] == NO_VALUE) { slots[t] = this.#defaultValueOf(layout.slots[t]); } } if (!isLast && sharesFatPointer(this, records, m)) { // Another member has this fat pointer: its literal field decides. let hit = this.b.newBlock(); let cond = memberTest(this.#cx(), layout, slots, m); this.b.brIf(cond, hit, this.#noArgs(), next, this.#noArgs()); this.b.switchTo(hit); } this.b.br(join, slots); if (!isLast) { this.b.switchTo(next); } } this.b.switchTo(join); return params; } hostHeldInline(e: Expression): MultiValues | null { return this.#heldInline(e); } hostRecordDispatchFor(record: RecordType): RecordDispatchInfo { return this.#recordDispatchFor(record); } /** Member `m`'s boxed tuple, built from its slots. */ #buildTupleFromSlots(values: GrowableArray, layout: InlineLayout, m: i32, tuple: TupleType): i32 { return this.#buildBoxedAt(layout.originals[m], '', tuple, (path) => { let slot = layout.slotAt(m, path); return if (slot >= 0) values[slot] else NO_VALUE; }); } hostBoxInline(source: MultiValues): i32 { return this.#boxInline(source); } hostLowerIntoSlots(e: Expression, layout: InlineLayout | null, slotVts: GrowableArray): GrowableArray { return this.#lowerIntoSlots(e, layout, slotVts); } hostReturnInline(e: Expression): void { let values = this.#lowerMultiReturnValues(e); if (!(e is IfExpression || e is MatchExpression)) { let scopes = this.#cx().tailExitScopes; var si = scopes.length - 1; while (si >= 0) { this.#emitExitDrops(scopes[si]); si -= 1; } } if (this.#cx().reachable && !returnMultiThroughFinally(this.#cx(), values)) { this.b.retMulti(values); } this.#cx().reachable = false; } hostBoxInlineAt(source: MultiValues, path: String, inner: InlineTupleType, allowed: GrowableArray | null): i32 { let cx = this.#cx(); return this.#buildBoxedAt(inner, `${path}.`, boxedTupleOf(inner), (leaf) => source.valueAt(cx, leaf, allowed)); } hostBoxInlineUnionAt(source: MultiValues, path: String, allowed: GrowableArray | null): i32 { let layout = source.layout as InlineLayout; let shapes = shapesAt(layout, path, allowed); let sub = this.#nestedUnionLayout(shapes); let values = new Array(); let from = new Array(); for (var t = 0; t < sub.slots.length; t += 1) { values.push(NO_VALUE); from.push(-1); } for (var m = 0; m < shapes.length; m += 1) { let candidates = membersWithShapeAt(layout, path, shapes[m], allowed); let paths = sub.memberPaths[m]; for (var i = 0; i < paths.length; i += 1) { let subSlot = sub.memberSlots[m][i]; let outer = layout.pathSlot(`${path}.${paths[i]}`, candidates); if (subSlot < 0 || outer < 0) { continue; } if (from[subSlot] == -1) { from[subSlot] = outer; values[subSlot] = this.#conformToSlot(source.values[outer], sub.slots[subSlot], 'nested inline union slot'); } else if (from[subSlot] != outer) { return this.#bailValue('nested inline union slot needs a branch'); } } } for (var t = 0; t < values.length; t += 1) { if (values[t] == NO_VALUE) { values[t] = this.#defaultValueOf(sub.slots[t]); } } return this.#boxInline(new MultiValues(values, sub)); } /** The layout of a nested inline union whose members are `shapes`. */ #nestedUnionLayout(shapes: GrowableArray): InlineLayout { let members = new Array(); for (let shape in shapes) { members.push(shape); } let t: Type = if (members.length == 1) members[0] else new UnionType(members); return inlineLayoutOf(this.wasm, t) ?? this.#bailLayout('nested inline union layout'); } #bailLayout(reason: String): InlineLayout { this.#bail(reason); } /** * The value of `el`, a nested inline union at `path`, written into its * slots of `shape`: lowered into the union's own slots, which then move * to the outer slots of the members that put the same member there. */ #writeNestedUnion(el: Expression, path: String, shape: InlineLayout, candidates: GrowableArray, values: GrowableArray): void { let shapes = shapesAt(shape, path, candidates); let sub = this.#nestedUnionLayout(shapes); let subValues = this.#lowerIntoSlots(el, sub, sub.slots); this.#moveNestedSlots(subValues, sub, shapes, path, shape, candidates, values); } /** * Moves `subValues`, a nested union's slots in `sub` (whose members are * `shapes`, in order), to the slots of `shape` at `path`. */ #moveNestedSlots(subValues: GrowableArray, sub: InlineLayout, shapes: GrowableArray, path: String, shape: InlineLayout, candidates: GrowableArray, values: GrowableArray): void { let from = new Array(); for (var t = 0; t < values.length; t += 1) { from.push(-1); } for (var m = 0; m < shapes.length && m < sub.memberPaths.length; m += 1) { let members = membersWithShapeAt(shape, path, shapes[m], candidates); let paths = sub.memberPaths[m]; for (var i = 0; i < paths.length; i += 1) { let subSlot = sub.memberSlots[m][i]; if (subSlot < 0) { continue; } let outer = shape.pathSlot(`${path}.${paths[i]}`, members); if (outer == -2) { this.#bail('inline union member of a returned tuple'); } if (outer < 0) { continue; } if (from[outer] == -1) { from[outer] = subSlot; values[outer] = this.#conformToSlot(subValues[subSlot], shape.slots[outer], 'nested inline union slot'); } else if (from[outer] != subSlot) { this.#bail('nested inline union slot needs a branch'); } } } } hostBoxInlineRecordAt(source: MultiValues, path: String, record: RecordType, allowed: GrowableArray | null): i32 { let cx = this.#cx(); return this.#heldRecordFromLeaves(record, `${path}.`, (leaf) => source.valueAt(cx, leaf, allowed)); } /** The dispatch of `record`'s boxed form, which its fields are read through. */ #recordDispatchFor(record: RecordType): RecordDispatchInfo { return this.wasm.findRecordDispatch(recordWasmFields(this.wasm, record, this.func), sortedOptionalNames(record)) ?? this.#bailDispatch('inline record without a dispatch') as RecordDispatchInfo; } /** * The leaves of `e`, a record of type `record`, in the order of * `collectLeafPaths(recordFieldsTuple(record))`: each field's value, an * inline field's own leaves in its place. A record literal is lowered * leaf by leaf, its properties in written order; any other record is * lowered whole and read field by field (`#recordValueLeaves`). */ #recordExprLeaves(e: Expression, record: RecordType): GrowableArray { if (e is RecordLiteral && this.func.model.getRecordAdaptation(e) == null) { let literal = e; var plain = true; for (let prop in literal.properties) { if (!(prop is PropertyAssignment)) { plain = false; } } if (plain) { let byName = new Map>(); for (let prop in literal.properties) { let assignment = prop as PropertyAssignment; let name = assignment.name.name; if (record.properties.has(name)) { byName[name] = this.#fieldExprLeaves(assignment.value, record.properties[name]); } } let out = new Array(); for (let name in recordFieldNames(record)) { if (let (true, leaves) = byName.get(name)) { for (let v in leaves) { out.push(v); } } } return out; } } let d = this.#recordDispatchFor(record); if (e is IfExpression || e is MatchExpression) { this.#cx().resultFields = d; } let value = this.#lowerExpressionCtx(e, typeToValTypeWithSub(this.wasm, boxedRecordWithFields(record), this.func)); this.#cx().resultFields = null; return this.#recordValueLeaves(value, record); } /** The leaves of `e` as a field of type `t`: its own, when `t` is inline. */ #fieldExprLeaves(e: Expression, t: Type): GrowableArray { let sub = nestedInlineRecord(t); if (sub != null) { return this.#recordExprLeaves(e, sub); } let inner = nestedInlineTuple(t); if (inner != null) { let layout = inlineLayoutOf(this.wasm, inner) as InlineLayout; let slots = this.#lowerIntoSlots(e, layout, layout.slots); let out = new Array(); let relative = new Array(); collectLeafPaths(inner, '', relative); for (let rel in relative) { let slot = layout.pathSlot(rel, null); out.push(if (slot >= 0) slots[slot] else NO_VALUE); } return out; } return growable([this.#lowerExpressionCtx(e, typeToValTypeWithSub(this.wasm, t, this.func))]); } /** The leaves of `value`, a record of type `record`, read field by field. */ #recordValueLeaves(value: i32, record: RecordType): GrowableArray { let d = this.#recordDispatchFor(record); let fields = this.#cx().recordFieldsOf(d, value); let types = recordFieldsTuple(record).elementTypes; let out = new Array(); for (var k = 0; k < fields.length; k += 1) { let fieldType = if (k < types.length) types[k] else null; let sub = if (fieldType != null) nestedInlineRecord(fieldType) else null; let inner = if (fieldType != null) nestedInlineTuple(fieldType) else null; if (sub != null) { for (let v in this.#recordValueLeaves(fields[k], sub)) { out.push(v); } } else if (inner != null) { let source = this.#unboxTuples(fields[k], boxedFieldType(fieldType as Type)) ?? this.#bailMulti('inline tuple field of a record'); let relative = new Array(); collectLeafPaths(inner, '', relative); for (let rel in relative) { out.push(this.#leafOf(source as MultiValues, rel)); } } else { out.push(fields[k]); } } return out; } #bailMulti(reason: String): MultiValues | null { this.#bail(reason); } /** * `leaves`, in the order of `collectLeafPaths(recordFieldsTuple(record))`, * in the order of `layout`'s slots and conformed to them; a leaf with no * value takes its slot's default. */ #leavesToSlots(record: RecordType, layout: InlineLayout, leaves: GrowableArray): GrowableArray { let relative = new Array(); collectLeafPaths(recordFieldsTuple(record), '', relative); let out = new Array(); for (var slot = 0; slot < layout.slots.length; slot += 1) { let i = pathIndex(relative, layout.slotPaths[slot]); let v = if (i >= 0 && i < leaves.length) leaves[i] else NO_VALUE; out.push(if (v == NO_VALUE || !this.#cx().reachable) { this.#defaultValueOf(layout.slots[slot]) } else { this.#conformToSlot(v, layout.slots[slot], 'inline record leaf type') }); } return out; } /** * The record of type `record` held as `leaves`, values in the order of * `layout`'s slots, which is an inline record's with inline fields. */ #heldRecordFromSlots(record: RecordType, layout: InlineLayout, leaves: GrowableArray): i32 { return this.#heldRecordFromLeaves(record, '', (path) => { let slot = layout.pathSlot(path, null); return if (slot >= 0 && slot < leaves.length) leaves[slot] else NO_VALUE; }); } /** * The record `record` built from leaf values, `leaf(prefix + k)` for its * k-th field in dispatch order, and held as them (`explodedRecords`). */ #heldRecordFromLeaves(record: RecordType, prefix: String, leaf: (path: String) => i32): i32 { let d = this.#recordDispatchFor(record); // An inline field is its own leaves: a record built and held as them, // or a boxed tuple built from them. let types = recordFieldsTuple(record).elementTypes; let fields = new Array(); for (var k = 0; k < d.fields.length; k += 1) { let fieldVt = d.fields[k].type; let fieldType = if (k < types.length) types[k] else null; let sub = if (fieldType != null) nestedInlineRecord(fieldType) else null; let inner = if (fieldType != null) nestedInlineTuple(fieldType) else null; let found = if (sub != null) { this.#heldRecordFromLeaves(sub, `${prefix}${k}.`, leaf) } else if (inner != null) { this.#buildBoxedAt(inner, `${prefix}${k}.`, boxedTupleOf(inner), leaf) } else { leaf(`${prefix}${k}`) }; fields.push(if (found == NO_VALUE) { this.#defaultValueOf(fieldVt) } else { this.#conformToSlot(found, fieldVt, 'nested inline record field type') }); } let value = this.#packRecord(d, fields); this.#cx().explodedRecords[value] = new ExplodedRecord(d, fields); return value; } /** * The boxed tuple `tuple`, the boxed form of the inline tuple `shape`, * built from leaf values: `leaf(path)` gives the value at `path` * (`prefix` then the position, "k" or "k.j" for a nested tuple's), or * NO_VALUE for a slot the member does not fill, which takes its * default. A nested inline tuple becomes a nested boxed one. */ #buildBoxedAt(shape: InlineTupleType, prefix: String, tuple: TupleType, leaf: (path: String) => i32): i32 { let st = this.#tupleStruct(tuple); let fields = new Array(); for (var k = 0; k < tuple.elementTypes.length; k += 1) { let name = i32ToString(k); if (st.fieldIndexByName(name) < 0) { // A zero-width element. continue; } let fieldVt = this.#fieldValType(st, name); let inner = if (k < shape.elementTypes.length) nestedInlineTuple(shape.elementTypes[k]) else null; let element = if (k < shape.elementTypes.length) shape.elementTypes[k] else null; let record = if (element != null) nestedInlineRecord(element) else null; let boxedElement = tuple.elementTypes[k]; let v = if (record != null) { this.#heldRecordFromLeaves(record, `${prefix}${k}.`, leaf) } else if (inner != null) { // Where the written tuple holds a union, `shape` holds the member. this.#buildBoxedAt(inner, `${prefix}${k}.`, if (boxedElement is TupleType) boxedElement else boxedTupleOf(inner), leaf) } else if (element is HoleType) { // A `_` holds nothing; its slot, if it has one, is a `nullref` // shared with the other members. this.#defaultValueOf(fieldVt) } else { let found = leaf(`${prefix}${k}`); if (found == NO_VALUE) this.#defaultValueOf(fieldVt) else found }; // A slot is nullable when another member leaves it empty; this // member always fills it. fields.push(this.#cx().conformToSlot(v, fieldVt, 'boxing an inline slot')); } return this.b.emit(IrOp.struct_new, new ValTypeRef(st) as ValType, this.b.pushArgList(fields), this.b.addRef(new IrRefType(st))); } /** * Post-expression adaptations, mirroring the streaming generator's * hook after generateExpression: the model marks expressions whose * value must be repacked for the consuming context. Interface * adaptations pack a class instance into a fat pointer (iface_pack); * record and function adaptations are not lowered yet and bail — * loudly, since silently skipping a required repack would miscompile. */ #applyAdaptations(expr: Expression, value: i32): i32 { var v = value; let raOpt = this.func.model.getRecordAdaptation(expr); if (raOpt != null) { v = this.#applyRecordAdaptation(raOpt, v); } let ruOpt = this.func.model.getRecordUnionAdaptation(expr); if (ruOpt != null) { v = this.#applyRecordUnionAdaptation(ruOpt, v); } v = this.#applyInterfaceAdaptation(expr, v); let faOpt = this.func.model.getFunctionAdaptation(expr); if (faOpt != null) { v = this.#applyFunctionAdaptation( expr, faOpt, v); } return v; } /** * Record repack: the value is a record fat pointer; the adapted view * keeps the same instance and swaps in the vtable whose getters * were generated for the target shape (registered during discovery * — a missing registration bails). */ /** * The value a record pattern's `...rest` binds: the matched record * converted to the rest's type, which the checker recorded on it. */ #recordRestValue(rest: Node, value: i32): i32 { let raOpt = this.func.model.getRecordAdaptation(rest); if (raOpt == null) { this.#bail('record rest without its conversion'); } return this.#applyRecordAdaptation(raOpt, value); } hostRecordRestValue(rest: Node, value: i32): i32 { return this.#recordRestValue(rest, value); } /** * A conversion involving an existential record type: a pack into one; * a projection out of one to a closed type naming only fields it * knows; or an `as` to a closed type naming others, which finds the * record held at run time (ir/existential.zena). */ #convertExistential(source: RecordType, target: RecordType, value: i32): i32 { let cx = this.#cx(); let sourceD = cx.recordDispatchOf(source); let targetD = cx.recordDispatchOf(target); if (target.isExistential) { return packExistential(cx, value, sourceD, targetD); } for (let key in target.properties.keys()) { if (!source.properties.has(key)) { return existentialAs(cx, value, sourceD, targetD); } } return projectExistential(cx, value, sourceD, targetD); } #applyRecordAdaptation(ra: RecordAdaptation, value: i32): i32 { if (value < 0 || this.b.body.typeId[value] < 0) { this.#bail('record adaptation without value'); } let sourceRec = substituteTypeParamsInFunctionContext(this.wasm, this.func, ra.source); let targetRec = substituteTypeParamsInFunctionContext(this.wasm, this.func, ra.target); if (!(sourceRec is RecordType) || !(targetRec is RecordType)) { return this.#bailValue('record adaptation shape'); } if (sourceRec.isExistential || targetRec.isExistential) { return this.#convertExistential(sourceRec, targetRec, value); } let inInfo = this.#cx().recordDispatchOf(sourceRec); let outFields = recordWasmFields(this.wasm, targetRec, this.func); // Look up the registered dispatch directly: the generator and async // split passes lower bodies before layout() assigns global indices, // so vtableGlobal.index is still -1 there. let outInfoOpt = this.wasm.findAdaptedDispatch( inInfo.fields, inInfo.optionalNames, outFields, sortedOptionalNames(targetRec)); if (outInfoOpt == null) { return this.#bailValue('record adaptation vtable unregistered'); } let outInfo = outInfoOpt; let inVt = new ValTypeRef(inInfo.fatPtrType) as ValType; var src = value; if (!isValSubtype(this.#valueType(src), inVt)) { src = this.b.emit(IrOp.ref_cast, inVt, src, this.b.internType(inVt)); } let inst = this.b.emit(IrOp.iface_instance, inInfo.fatPtrType.fields[0].type, src, -1); return this.b.emit(IrOp.iface_pack, new ValTypeRef(outInfo.fatPtrType) as ValType, inst, this.b.addRef(new IrRefGlobal(outInfo.vtableGlobal))); } /** * Record repack for a union of record shapes: the shape to repack * from is the member the value actually is, which only the run time * knows, so the repack is a chain of type tests. * * A union of two record types lowers to `eqref`, and each member's * fat pointer is its own wasm struct, so `ref.test` against a * member's fat pointer type identifies the member. The arm that * matches repacks that member to the target shape exactly as * `#applyRecordAdaptation` does, and branches to a join block * carrying the result. All members share the join's type: the adapted * vtable's getter signatures come from the target's fields alone, so * every member's adapted fat pointer is the same struct. * * Falling off the end of the chain means the value was none of the * union's members. A null is the one way that happens in a * well-typed program (`{a} | {a, b} | null` read as `{a} | null`), so * a nullable source tests for null up front and passes it through, * and the tail of the chain traps. */ #applyRecordUnionAdaptation(ra: RecordUnionAdaptation, value: i32): i32 { if (value < 0 || this.b.body.typeId[value] < 0) { this.#bail('record union adaptation without value'); } if (!isRefValType(this.#valueType(value))) { this.#bail('record union adaptation operand'); } // The dispatches before any block: the join's type comes from them, // and the null guard that precedes the chain branches to the join. let targetRec = substituteTypeParamsInFunctionContext(this.wasm, this.func, ra.target); if (!(targetRec is RecordType)) { return this.#bailValue('record union adaptation shape'); } let outFields = recordWasmFields(this.wasm, targetRec, this.func); let outOptionals = sortedOptionalNames(targetRec); let inInfos = new Array(); let outInfos = new Array(); for (let member in ra.members) { let memberRec = substituteTypeParamsInFunctionContext(this.wasm, this.func, member); if (!(memberRec is RecordType)) { return this.#bailValue('record union adaptation shape'); } let inInfo = this.#cx().recordDispatchOf(memberRec); inInfos.push(inInfo); let outInfoOpt = this.wasm.findAdaptedDispatch( inInfo.fields, inInfo.optionalNames, outFields, outOptionals); if (outInfoOpt == null) { return this.#bailValue('record union adaptation vtable unregistered'); } outInfos.push(outInfoOpt); } if (outInfos.length == 0) { return this.#bailValue('record union adaptation without members'); } let packedVt = new ValTypeRef(outInfos[0].fatPtrType) as ValType; let mergeVt = if (ra.sourceNullable) { new ValTypeRefNull(outInfos[0].fatPtrType) as ValType } else { packedVt }; let mergeB = this.b.newBlock(); let resultParam = this.b.blockParam(mergeB, mergeVt); if (ra.sourceNullable) { let isNull = this.b.emit( IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1); let nullB = this.b.newBlock(); let chainB = this.b.newBlock(); this.b.brIf(isNull, nullB, this.#noArgs(), chainB, this.#noArgs()); this.b.switchTo(nullB); this.b.br(mergeB, this.#oneArg(this.#nullDefault(mergeVt))); this.b.switchTo(chainB); } for (var i = 0; i < outInfos.length; i += 1) { let inInfo = inInfos[i]; let inVt = new ValTypeRef(inInfo.fatPtrType) as ValType; let isMember = this.b.emit(IrOp.ref_test, new ValTypeI32() as ValType, value, this.b.internType(inVt)); let hitB = this.b.newBlock(); let nextB = this.b.newBlock(); this.b.brIf(isMember, hitB, this.#noArgs(), nextB, this.#noArgs()); this.b.switchTo(hitB); let src = this.b.emit(IrOp.ref_cast, inVt, value, this.b.internType(inVt)); let inst = this.b.emit(IrOp.iface_instance, inInfo.fatPtrType.fields[0].type, src, -1); let packed = this.b.emit(IrOp.iface_pack, packedVt, inst, this.b.addRef(new IrRefGlobal(outInfos[i].vtableGlobal))); this.b.br(mergeB, this.#oneArg(packed)); this.b.switchTo(nextB); } this.b.unreachable(); this.b.switchTo(mergeB); this.#cx().reachable = true; return resultParam; } /** * Function repack: the wrapper registered for this site (during * discovery) forwards the target arity to the source closure; the * adapted value is a target-shaped pair whose context is the WHOLE * source pair. */ #applyFunctionAdaptation(expr: Expression, fa: FunctionAdaptation, value: i32): i32 { if (value < 0 || this.b.body.typeId[value] < 0) { this.#bail('function adaptation without value'); } let wrapper = if (let (true, w) = this.wasm.functionAdaptationWrappers.get(expr)) w else null; if (wrapper == null) { // Discovery registers none when the closure is one of the target // type already. let source = substituteTypeParamsInFunctionContext(this.wasm, this.func, fa.source); let target = substituteTypeParamsInFunctionContext(this.wasm, this.func, fa.target); if (source is FunctionType && target is FunctionType && getClosureStruct(this.wasm, source) === getClosureStruct(this.wasm, target)) { return value; } this.#bail('function adaptation wrapper unregistered'); } let actualStruct = wrapper.functionAdaptationResolvedSourceStruct ?? getClosureStruct(this.wasm, fa.source); let expectedStruct = wrapper.functionAdaptationResolvedTargetStruct ?? getClosureStruct(this.wasm, fa.target); let actualVt = new ValTypeRef(actualStruct) as ValType; var pair = value; if (!isValSubtype(this.#valueType(pair), actualVt)) { pair = this.b.emit(IrOp.ref_cast, actualVt, pair, this.b.internType(actualVt)); } let funcVal = this.b.emit(IrOp.ref_func, expectedStruct.fields[0].type, -1, this.b.addRef(new IrRefFunc(wrapper))); let pairArgs = this.#twoValues(funcVal, pair); return this.b.emit(IrOp.struct_new, new ValTypeRef(expectedStruct) as ValType, this.b.pushArgList(pairArgs), this.b.addRef(new IrRefType(expectedStruct))); } #applyInterfaceAdaptation(expr: Expression, value: i32): i32 { let ia = this.func.model.getInterfaceAdaptation(expr); if (ia == null) { return value; } if (this.#coalesceArmsAdapted.has(expr.id as i32)) { return value; } if (value < 0 || this.b.body.typeId[value] < 0) { this.#bail('interface adaptation without value'); } var srcType = ia.source as Type; var tgtType = ia.target as Type; // Mixin-intermediate and `_This`-marker sources stand in for the // enclosing class (mirrors the streaming hook). if (srcType is ClassType) { let sct = srcType; let n = sct.name; if (sct.isMixinIntermediate || (n.length >= 5 && n.sliceBytes(n.length - 5, n.length) == "_This")) { let {classType} = this.func; if (classType != null) { srcType = classType; } } } // Function context, not class context alone: a monomorphized // generic body (_spec_ copy) carries its own solved type // arguments, and an adaptation source like `W` must key the // vtable lookup at `W`, not at the template. srcType = substituteTypeParamsInFunctionContext(this.wasm, this.func, srcType); tgtType = substituteTypeParamsInFunctionContext(this.wasm, this.func, tgtType); if (srcType is ClassType && tgtType is InterfaceType) { if (ia.sourceNullable) { return this.#packInterfaceNullable(value, srcType, tgtType); } return this.#packInterface(value, srcType, tgtType); } if (srcType is InterfaceType && tgtType is InterfaceType) { if (ia.sourceNullable) { return this.#repackInterfaceNullable(value, srcType, tgtType); } return this.#cx().repackInterface(value, srcType, tgtType); } this.#bail('interface adaptation shape'); return value; } #packInterface(value: i32, ct: ClassType, it: InterfaceType): i32 { return this.#cx().packInterface(value, ct, it); } /** * The pack for a source that can be null (`C | null` consumed as * `I | null`): packing a null instance would build a fat pointer that * is not itself null, and the consumer's `== null` tests the pointer. * So null branches around the pack and produces a null fat pointer. */ #packInterfaceNullable(value: i32, ct: ClassType, it: InterfaceType): i32 { if (!isRefValType(this.#valueType(value))) { this.#bail('nullable interface adaptation operand'); } let isNull = this.b.emit( IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1); let nullB = this.b.newBlock(); let packB = this.b.newBlock(); let mergeB = this.b.newBlock(); this.b.brIf(isNull, nullB, this.#noArgs(), packB, this.#noArgs()); this.b.switchTo(packB); let packed = this.#packInterface(this.#strippedNonNull(value), ct, it); let packedVt = this.#valueType(packed); if (!(packedVt is ValTypeRef)) { this.#bail('nullable interface adaptation result'); } let mergedVt = new ValTypeRefNull(packedVt.target) as ValType; let resultParam = this.b.blockParam(mergeB, mergedVt); this.b.br(mergeB, this.#oneArg(packed)); this.b.switchTo(nullB); this.b.br(mergeB, this.#oneArg(this.#nullDefault(mergedVt))); this.b.switchTo(mergeB); this.#cx().reachable = true; return resultParam; } #repackInterfaceNullable(value: i32, srcIt: InterfaceType, tgtIt: InterfaceType): i32 { if (!isRefValType(this.#valueType(value))) { this.#bail('nullable interface adaptation operand'); } let isNull = this.b.emit( IrOp.ref_is_null, new ValTypeI32() as ValType, value, -1); let nullB = this.b.newBlock(); let packB = this.b.newBlock(); let mergeB = this.b.newBlock(); this.b.brIf(isNull, nullB, this.#noArgs(), packB, this.#noArgs()); this.b.switchTo(packB); let packed = this.#cx().repackInterface(this.#strippedNonNull(value), srcIt, tgtIt); let packedVt = this.#valueType(packed); if (!(packedVt is ValTypeRef)) { this.#bail('nullable interface adaptation result'); } let mergedVt = new ValTypeRefNull(packedVt.target) as ValType; let resultParam = this.b.blockParam(mergeB, mergedVt); this.b.br(mergeB, this.#oneArg(packed)); this.b.switchTo(nullB); this.b.br(mergeB, this.#oneArg(this.#nullDefault(mergedVt))); this.b.switchTo(mergeB); this.#cx().reachable = true; return resultParam; } /** * Template mode (template.zena) abandons the template where an * expression's lowering may depend on a type argument beyond what * the template records: a type test against or involving a type * parameter, a cast to anything but a class, `match` on one, string * conversion, and calls to intrinsics other than `eq` and `hash`. A * cast to a class records whether it was needed, `==` on a class * records that it was identity (`lowerClassEquality`), and a method * found on a class's chain records where (`resolveDevirtualizedMethod`). */ #templateGate(expr: Expression): void { let model = this.func.model; let mentions = (e: Node): boolean => { let t = model.getNodeType(e); return t != null && typeContainsTypeParameter(t as Type); }; if (expr is IsExpression) { if (mentions((expr as IsExpression).expression)) { throw new TemplateAbandon('type test'); } } else if (expr is AsExpression) { if (mentions(expr) || mentions((expr as AsExpression).expression)) { let target = this.#substitutedNodeType(expr); if (target == null || !(getNonNullableType(baseType(target as Type)) is ClassType)) { throw new TemplateAbandon('cast to a non-class'); } } } else if (expr is MatchExpression) { if (mentions(expr.discriminant)) { throw new TemplateAbandon('match'); } } else if (expr is TemplateLiteral) { for (let e in expr.expressions) { if (mentions(e)) { throw new TemplateAbandon('template literal'); } } } else if (expr is TaggedTemplateExpression) { throw new TemplateAbandon('tagged template'); } else if (expr is AssignmentExpression) { // What the store does with the value it replaces depends on the // type argument (emitScopedFieldReplace). if (model.isScopedFieldStore(expr as Node) && mentions(expr.left)) { throw new TemplateAbandon('scoped field store'); } } else if (expr is MemberExpression) { // Whether a take stores null depends on the type argument // (#takesHere). if (model.isFieldTake(expr as Node) && mentions(expr)) { throw new TemplateAbandon('field take'); } } else if (expr is CallExpression) { let callee = (expr as CallExpression).callee; // A call can depend on a type argument that its signature does // not mention: `isFirstClass()` is `() => boolean`. let instantiatedAt = (e: Node): boolean => { let t = model.getNodeType(e); if (t is FunctionType) { let targs = t.typeArguments; if (targs != null) { for (let a in targs) { if (typeContainsTypeParameter(a)) { return true; } } } } return false; }; if (callee is Identifier && (mentions(callee) || instantiatedAt(callee)) && !isArrayIntrinsic((callee as Identifier).name)) { let sym = model.resolve(callee); let ct = model.getNodeType(callee); if (sym != null && ct is FunctionType) { let decl = findDeclaration(this.wasm, sym.resolveTarget(), ct as FunctionType); if (decl != null) { if (let (true, name) = this.wasm.intrinsics.get(decl.id)) { // `eq` and `hash` on a class decide through // `lowerClassEquality` and `resolveDevirtualizedMethod`, // which record their answers. if (name != 'eq' && name != 'hash') { throw new TemplateAbandon(`intrinsic ${name}`); } } } } } } } #lowerExpressionDispatch(expr: Expression): i32 { if (this.b.recorder != null) { this.#templateGate(expr); } return match (expr) { case NumberLiteral as lit: this.#lowerNumberLiteral(lit) case BooleanLiteral {value}: this.b.constI32(if (value) 1 else 0, new ValTypeI32()) case Identifier as ident: this.#lowerIdentifier(ident) case BinaryExpression as bin: this.#lowerBinary(bin) case UnaryExpression as un: this.#lowerUnary(un) case AssignmentExpression as assign: this.#lowerAssignment(assign) case CallExpression as call: { if (this.#isDivergingCall(call)) { this.#lowerCall(call); if (this.#cx().reachable) { this.b.unreachable(); this.#cx().reachable = false; } return NO_VALUE; } this.#lowerCall(call) } case ThrowExpression as thr: { this.#lowerThrowStatement(thr); NO_VALUE } case IfExpression as ifExpr: this.#lowerIfExpression(ifExpr) case MatchExpression as matchExpr: this.#lowerMatch(matchExpr) case AsExpression as asExpr: this.#lowerAs(asExpr) case StringLiteral as strLit: this.#lowerStringLiteral(strLit) case NewExpression as newExpr: this.#lowerNew(newExpr) case MemberExpression as member: this.#lowerMemberRead(member) case ThisExpression: this.#lowerThis(expr) case FunctionExpression as fExpr: this.#lowerFunctionExpression(fExpr) case NullLiteral: this.#bailValue('null without context') case IndexExpression as idx: this.#lowerIndexRead(idx) case TemplateLiteral as tpl: this.#lowerTemplateLiteral(tpl) case IsExpression as isExpr: this.#lowerIs(isExpr) case ArrayLiteral as arrLit: this.#lowerArrayLiteral(arrLit) case RecordLiteral as recLit: this.#lowerRecordLiteral(recLit) case TupleLiteral as tupLit: this.#lowerTupleLiteralExpr(tupLit) case RangeExpression as range: this.#lowerRangeExpr(range) case TryExpression as tryExpr: this.#lowerTryExpression(tryExpr) case MapLiteral as mapLit: this.#lowerMapLiteral(mapLit) case PipelineExpression as pipe: this.#lowerPipeline(pipe) case PipePlaceholder: this.#lowerPipePlaceholder() case TaggedTemplateExpression as tagged: this.#lowerTaggedTemplate(tagged) case AwaitExpression as aw: this.#lowerAwait(aw) case _: this.#bailValue('expression kind' + kindTagOf(expr)) }; } /** * A tuple literal in value position: the boxed form only — one * struct with fields "0".."n-1" (the same shape-deduped struct the * return-type mapping uses). Inline tuples never appear in value * position; their construction lives in the multi-value paths. */ /** * `await e` (async.md §3): suspend on the future `e` produces, and * resume by reading the settled result back off that same future. * * The awaited future is an ordinary SSA value that is live across the * suspension, so the split pass's existing liveness/spill machinery * gives it a frame field for free — the resume segment needs no * incoming block parameter. Reading the result with `valueOrThrow()` * is what makes a rejected future propagate as a plain throw into * step()'s failure-capture region. */ #lowerAwait(aw: AwaitExpression): i32 { if (!this.presplitAsync) { // The checker rejects await outside an async body, so reaching // here means the split pass failed to enable presplit-async mode. throw new Error('await reached ZIR lowering outside an async body'); } let operandTypeOpt = this.#substitutedNodeType(aw.argument); if (operandTypeOpt == null) { this.#bail('untyped await operand'); } let resolved = eraseTypeParameters(this.wasm, baseType(operandTypeOpt)); if (resolved is UnionType) { return this.#lowerUnionAwait(aw, resolved); } if (resolved is ClassType && this.#futureMethodOwner(resolved, ':valueOrThrow') != null && this.#futureMethodOwner(resolved, ':subscribe') != null) { let future = this.#lowerExpression(aw.argument); let awaitedVal = this.#suspendOnFuture(resolved, future); let awT = this.#substitutedNodeType(aw); if (awT != null && unwrapAliases(awT) is NeverType) { this.b.unreachable(); this.#cx().reachable = false; return -1; } return awaitedVal; } // Anything else reached lowering through the GENERIC await — // `await x` with `x: T` open, typed `Awaited` — specialized at // a non-future type (the checker rejects concrete non-future // operands at the use site). One queue hop, never synchronous, // delivering the value itself at the resume: the same parking the // union await's bare arms use, unconditional. Unlike a union's // bare arm, a bare T substituted to a future is NOT a divergence // here — bare await means "await whatever arrives", so the future // case above is exactly right for it. let value = this.#lowerExpression(aw.argument); let resume = this.b.newBlock(); let suspend = this.b.awaitTo(value, resume, new Array(0)); this.awaitHopTerms.add(suspend); if (this.shieldDepth > 0) { this.shieldedAwaits.add(suspend); } this.b.switchTo(resume); return value; } /** * The class whose `name` an `await` of a `ct` calls: `ct`, or the * nearest superclass that registers it. A subclass of `Future` * inherits `:valueOrThrow` from `Future`, while a `DeferredFuture` * registers its own `:subscribe`, so the await calls that one * directly unless a subclass of it overrides it in turn. Null when * no class in the chain has it: `ct` is not a future. */ #futureMethodOwner(ct: ClassType, name: String): ClassType | null { var current: ClassType | null = ct; while (current != null) { if (this.#futureMethodOf(current, name) != null) { return current; } current = current.superType; } return null; } /** The class's `:valueOrThrow`/`subscribe`, or null if it has * neither registered — which is what makes a union arm a future. */ #futureMethodOf(ct: ClassType, name: String): WasmFunction | null { let key = getClassMethodKey(getClassKey(ct), name); return if (let (true, f) = this.wasm.classMethodMap.get(key)) f else null; } /** * Suspends on `future` (typed `ct`) and resumes with its settled * value: the single-future await, and each future arm of a union * await. The future value is live across the suspension, so the * split pass spills it; the resumed segment reads the result back * with `:valueOrThrow`, which is what makes a rejected future * surface as a plain throw at the resume point. */ #suspendOnFuture(ct: ClassType, future: i32): i32 { let getterOwner = this.#futureMethodOwner(ct, ':valueOrThrow'); if (getterOwner == null) { this.#bail('await operand is not a Future: ' + (getClassKey(ct) as String)); } let getter = this.#futureMethodOf(getterOwner, ':valueOrThrow'); let subOwner = this.#futureMethodOwner(ct, ':subscribe'); if (subOwner == null) { this.#bail('await operand has no subscribe: ' + (getClassKey(ct) as String)); } let sub = this.#futureMethodOf(subOwner, ':subscribe') as WasmFunction; // A subclass overriding `subscribe` (a `DeferredFuture`, say) is // reached through the vtable, as any overridden method is; the // slot loads before the terminator, in the await's own block. The // question is asked of the class that registers the `subscribe` // called, so a `DeferredFuture`-typed await calls its own directly. var virtualCallee = -1; if (let (true, info) = this.wasm.classInfos.get(getClassKey(subOwner))) { if (this.#cx().memberProvidedBySubclass(info, ':subscribe')) { if (this.#vtableSlotIndex(info, ':subscribe') < 0) { this.#bail('overridden subscribe without vtable slot'); } virtualCallee = this.#cx().vtableSlotCallee( future, info, ':subscribe', sub.signature); } } let resume = this.b.newBlock(); let suspend = this.b.awaitTo(future, resume, new Array(0)); this.awaitSubscribeTargets[suspend] = sub; if (virtualCallee >= 0) { this.awaitSubscribeCallees[suspend] = virtualCallee; } if (this.shieldDepth > 0) { this.shieldedAwaits.add(suspend); } this.b.switchTo(resume); let args = growable([future]); let g = getter as WasmFunction; let sig = g.signature; let resultVt = if (sig.returns.length == 1) sig.returns[0] else null; return this.b.callFunc(this.b.addRef(new IrRefFunc(g)), args, resultVt); } /** Widens or narrows `value` to `want`; unions widen to their * common representation and narrow back with an exact cast. */ #conformUnionArm(value: i32, want: ValType): i32 { let have = this.#valueType(value); if (this.#cx().refConforms(have, want)) { return this.#conformToSlot(value, want, 'await result type'); } return this.b.emit(IrOp.ref_cast, want, value, this.b.internType(want)); } /** * `await` on a union (async.md §1): the future arms are * distinguished at runtime by their own specializations and each * suspends on its future; the bare arms pass through — with one * queue hop, so awaiting is always asynchronous whichever arm * arrived. Every path joins at the checked result type, the union * of bare arms and payloads. */ #lowerUnionAwait(aw: AwaitExpression, union: UnionType): i32 { let resultTypeOpt = this.#substitutedNodeType(aw); if (resultTypeOpt == null) { this.#bail('untyped await result'); } let resultT = eraseTypeParameters(this.wasm, resultTypeOpt); let resultVt = typeToValTypeWithSub(this.wasm, resultT, this.func); this.#cx().requireSupported(resultVt, 'await result type'); // Partition on the CONCRETE arm types. The checker partitioned the // unsubstituted arms; a bare arm substituted to a Future would land // in the future set here and silently change meaning (a value the // source passes through would be awaited), so that divergence is // detected below and refused loudly (async.md §1: an instantiation // where T = Future is rejected at the use site). let futureArms = new Array(); var bareCount = 0; for (let arm in union.types) { let armR = eraseTypeParameters(this.wasm, baseType(arm)); if (armR is ClassType && this.#futureMethodOf(armR, ':valueOrThrow') != null && this.#futureMethodOf(armR, ':subscribe') != null) { futureArms.push(armR); } else { bareCount += 1; } } if (futureArms.length == 0) { this.#bail('await union has no future arm: ' + typeToString(union)); } if (bareCount == 0) { // All-future partition: sound only if the checker also saw no // bare arm — i.e. the result contains no future. A future in the // result means a bare arm was substituted into a Future (or the // rare explicit Future> payload, refused with it). let resultArms = new Array(); if (resultT is UnionType) { for (let ra in resultT.types) { resultArms.push(ra); } } else { resultArms.push(resultT); } for (let ra in resultArms) { let raR = eraseTypeParameters(this.wasm, baseType(ra)); if (raR is ClassType && this.#futureMethodOf(raR, ':valueOrThrow') != null) { this.#bail('await union instantiation is ambiguous: an arm ' + 'meant to pass through was substituted to a Future in ' + typeToString(union)); } } } let opVal = this.#lowerExpression(aw.argument); let join = this.b.newBlock(); let joinParam = this.b.blockParam(join, resultVt); let noArgs = new Array(0); var fi = 0; while (fi < futureArms.length) { let ct = futureArms[fi]; let armVt = typeToValTypeWithSub(this.wasm, ct as Type, this.func); let target = refTargetOf(armVt); if (target == null) { this.#bail('await union arm has no struct: ' + (getClassKey(ct) as String)); } let castVt = new ValTypeRef(target as WasmType) as ValType; let isLast = fi == futureArms.length - 1 && bareCount == 0; var armEntry = -1; if (isLast) { // No fall-through remains: the value must be this arm. armEntry = -1; } else { let armBlock = this.b.newBlock(); let nextBlock = this.b.newBlock(); let test = this.b.emit(IrOp.ref_test, new ValTypeI32(), opVal, this.b.internType(castVt)); this.b.brIf(test, armBlock, noArgs, nextBlock, noArgs); this.b.switchTo(armBlock); armEntry = nextBlock; } let cast = this.b.emit(IrOp.ref_cast, castVt, opVal, this.b.internType(castVt)); let v = this.#suspendOnFuture(ct, cast); let conformed = this.#conformUnionArm(v, resultVt); let joinArgs = growable([conformed]); this.b.br(join, joinArgs); if (armEntry >= 0) { this.b.switchTo(armEntry); } fi += 1; } if (bareCount > 0) { // The bare arms: park the frame directly on the microtask queue // (the split pass emits scheduleTask for a hop suspension), and // deliver the value at the resume — one hop, never synchronous. let resume = this.b.newBlock(); let suspend = this.b.awaitTo(opVal, resume, noArgs); this.awaitHopTerms.add(suspend); if (this.shieldDepth > 0) { this.shieldedAwaits.add(suspend); } this.b.switchTo(resume); let conformed = this.#conformUnionArm(opVal, resultVt); let joinArgs = growable([conformed]); this.b.br(join, joinArgs); } this.b.switchTo(join); return joinParam; } #lowerTupleLiteralExpr(tl: TupleLiteral): i32 { let tOpt = this.#substitutedNodeType(tl); if (tOpt == null) { this.#bail('untyped tuple literal'); } let bt = baseType(tOpt); if (!(bt is TupleType)) { this.#bail('inline tuple in value position'); } let st = this.#tupleStruct(bt); let fields = this.#lowerTupleLiteralFields(tl, bt, st); if (!this.#cx().reachable) { return NO_VALUE; } return this.b.emit(IrOp.struct_new, new ValTypeRef(st) as ValType, this.b.pushArgList(fields), this.b.addRef(new IrRefType(st))); } /** * A tuple literal's element values, one per field of `st`, the struct * of its type `bt`: what the tuple is built from, or what an exploded * parameter is passed as. */ #lowerTupleLiteralFields(tl: TupleLiteral, bt: TupleType, st: WasmStruct): GrowableArray { // One wasm type per element; a zero-width one has no field, and its // value is lowered for its effects only. let elementVts = new Array(); for (let el in bt.elementTypes) { elementVts.push(typeToValTypeWithSub(this.wasm, el, this.func)); } let spreadIndices = tl.spreadIndices; if (spreadIndices == null && tl.elements.length != elementVts.length) { this.#bail('tuple literal arity'); } let args = new Array(); var pos = 0; for (var i = 0; i < tl.elements.length; i += 1) { if (spreadIndices != null && spreadIndices.contains(i)) { // `...t`: t's elements, read off its struct in order. for (let v in this.#tupleElementValues(tl.elements[i])) { if (pos >= elementVts.length) { this.#bail('tuple literal arity'); } let fieldVt = elementVts[pos]; pos += 1; if (!(fieldVt is ValTypeVoid)) { args.push(this.#conformToSlot(v, fieldVt, 'tuple literal element type')); } } continue; } if (pos >= elementVts.length) { this.#bail('tuple literal arity'); } let fieldVt = elementVts[pos]; pos += 1; if (fieldVt is ValTypeVoid) { this.#lowerExpression(tl.elements[i]); if (!this.#cx().reachable) { return args; } continue; } let el = tl.elements[i]; if (el is Identifier && el.name == '_') { // A hole holds nothing; its field takes the default. args.push(this.#defaultValueOf(fieldVt)); continue; } var v = this.#lowerExpressionCtx(el, fieldVt); if (!this.#cx().reachable) { return args; } v = this.#conformToSlot(v, fieldVt, 'tuple literal element type'); args.push(v); } if (pos != elementVts.length || args.length != st.fields.length) { this.#bail('tuple literal arity'); } return args; } /** * Binds a tuple pattern with a `...rest` against `value`, a tuple of * struct `st`: the elements before the rest bind a prefix, the ones * after it a suffix, and the rest a new tuple of what is between, of * the type the checker recorded on the rest pattern. */ #bindTupleWithRest(tp: TuplePattern, st: WasmStruct, value: i32, count: i32 = -1): void { let restIndex = tupleRestIndexOf(tp); let all = this.#tupleFieldsOf(st, value, count); let after = tp.elements.length - restIndex - 1; let length = all.length; if (restIndex + after > length) { this.#bail('tuple rest arity'); } for (var i = 0; i < tp.elements.length; i += 1) { let el = tp.elements[i]; if (el == null || !this.#patternBinds(el)) { continue; } if (i == restIndex) { let restPat = (el as RestPattern).pattern; let restTypeOpt = this.func.model.getNodeType(el); if (restTypeOpt == null) { this.#bail('untyped tuple rest'); } let restBase = baseType(substituteTypeParamsInFunctionContext( this.wasm, this.func, restTypeOpt)); if (!(restBase is TupleType)) { this.#bail('tuple rest type'); } let restSt = this.#tupleStruct(restBase); let args = new Array(); for (var j = restIndex; j < length - after; j += 1) { if (all[j] == NO_VALUE) { continue; } let restField = restSt.fieldIndexByName(i32ToString(j - restIndex)); if (restField < 0) { this.#bail('tuple rest element type'); } args.push(this.#conformToSlot(all[j], restSt.fields[restField].type, 'tuple rest element type')); } let rest = this.b.emit(IrOp.struct_new, new ValTypeRef(restSt) as ValType, this.b.pushArgList(args), this.b.addRef(new IrRefType(restSt))); this.#lowerPatternBindings(restPat, rest); } else if (i < restIndex) { this.#lowerPatternBindings(el, all[i]); } else { this.#lowerPatternBindings(el, all[length - after + (i - restIndex - 1)]); } } } hostBindTupleWithRest(tp: TuplePattern, value: i32): void { let st = asWasmStruct(this.#valueType(value)); if (st == null) { this.#bail('tuple rest operand'); } this.#bindTupleWithRest(tp, st as WasmStruct, value); } /** The struct a (boxed) tuple type lowers to. A zero-width element * (`void`) has no field; the others are named by their position. */ #tupleStruct(tt: TupleType): WasmStruct { let elements = new Array(tt.elementTypes.length); for (let el in tt.elementTypes) { elements.push(typeToValTypeWithSub(this.wasm, el, this.func)); } return this.wasm.getTupleStruct(tt, elements); } /** Each element of the tuple `expr` evaluates to, in order. */ #tupleElementValues(expr: Expression): GrowableArray { let tOpt = this.#substitutedNodeType(expr); if (tOpt == null) { this.#bail('untyped tuple spread'); } let bt = baseType(tOpt); if (!(bt is TupleType)) { this.#bail('tuple spread of a non-tuple'); } let st = this.#tupleStruct(bt); return this.#tupleFieldsOf(st, this.#lowerExpressionCtx(expr, new ValTypeRef(st) as ValType), bt.elementTypes.length); } /** * The `count` elements of a tuple of struct `st`, read off `value` by * position: NO_VALUE for a zero-width element, which has no field. * A negative count means one per field (no zero-width elements). */ #tupleFieldsOf(st: WasmStruct, value: i32, count: i32 = -1): GrowableArray { let end = if (count < 0) st.fields.length else count; let stVt = new ValTypeRef(st) as ValType; var recv = value; if (!isValSubtype(this.#valueType(recv), stVt)) { recv = this.b.emit(IrOp.ref_cast, stVt, recv, this.b.internType(stVt)); } let values = new Array(); for (var j = 0; j < end; j += 1) { let name = i32ToString(j); let index = st.fieldIndexByName(name); if (index < 0) { values.push(NO_VALUE); continue; } values.push(this.b.emit(IrOp.struct_get, st.fields[index].type, recv, this.b.addRef(new IrRefField(st, name)))); } return values; } /** * The body of a function whose record parameters were exploded: read * each exploded record's fields and call the core with them. Every * caller that does not pass literals arrives here. */ #lowerExplosionForwarder(spec: ExplosionSpec): IrBody { let sig = this.func.signature; let entry = this.b.newBlock(); let params = new Array(); for (let vt in sig.params) { this.#requireSupported(vt, 'parameter type'); params.push(this.b.blockParam(entry, vt)); } this.b.switchTo(entry); let coreSig = spec.core.signature; // Parameters ahead of the declared ones (a phase's instance) pass // through as they are. let leading = params.length - spec.params.length; let args = new Array(); for (var i = 0; i < leading; i += 1) { args.push(params[i]); } for (var i = 0; i < spec.params.length; i += 1) { let value = params[leading + i]; let d = spec.params[i]; if (d == null) { args.push(value); continue; } for (let field in d.fields) { let read = this.#cx().recordDispatchRead( d, field.name as String, value); args.push(this.#conformToSlot(read, coreSig.params[args.length], 'exploded field type')); } } let funcRef = this.b.addRef(new IrRefFunc(spec.core)); let result = spec.result; let tail = spec.tail; if (result != null) { // The core returns the record's fields; this builds the record. this.b.ret(this.#callForFields(spec.core, args, result)); } else if (tail != null) { // A value phase: the core returns some `super(...)` arguments as // their fields; this builds their records. this.b.retMulti(this.#callValuePhaseCore(spec, args, tail)); } else if (sig.returns.length > 1) { this.b.retMulti(this.b.callFuncMulti(funcRef, args, sig.returns)); } else if (sig.returns.length == 1) { this.b.ret(this.b.callFunc(funcRef, args, sig.returns[0])); } else { this.b.callFunc(funcRef, args, null); this.b.retVoid(); } return this.b.finish(); } /** * Calls the core of the value phase `spec.forwarder`, which returns * the `super(...)` arguments `tail` explodes as their fields, and * returns the forwarder's results: the own field values, then each * argument, an exploded one as a record built from its fields and held * as them (`explodedRecords`), so a caller that passes it on to the * next phase's core passes the fields and the record dies. */ #callValuePhaseCore(spec: ExplosionSpec, args: GrowableArray, tail: GrowableArray): GrowableArray { let projs = this.b.callFuncMulti(this.b.addRef(new IrRefFunc(spec.core)), args, spec.core.signature.returns); let boxed = spec.forwarder.signature.returns; let own = boxed.length - tail.length; let out = new Array(); var k = 0; while (k < own) { out.push(projs[k]); k += 1; } for (var j = 0; j < tail.length; j += 1) { let d = tail[j]; if (d == null) { out.push(projs[k]); k += 1; continue; } let fields = new Array(); for (let field in d.fields) { fields.push(projs[k]); k += 1; } let record = this.#conformToSlot(this.#packRecord(d, fields), boxed[own + j], 'super argument type'); this.#cx().explodedRecords[record] = new ExplodedRecord(d, fields); out.push(record); } return out; } /** * `{ x: 1, y: 2 }`: the struct of the literal's own record type, * built from #lowerRecordLiteralFields, packed with the shape's * vtable global. */ #lowerRecordLiteral(lit: RecordLiteral): i32 { let tOpt = this.#substitutedNodeType(lit); if (tOpt == null) { this.#bail('untyped record literal'); } let bt = unwrapAliases(tOpt); if (!(bt is RecordType)) { this.#bail('record literal type'); } let dispatch = this.#cx().recordDispatchOf(bt); let fields = this.#lowerRecordLiteralFields(lit, dispatch); if (!this.#cx().reachable) { return NO_VALUE; } return this.#packRecord(dispatch, fields); } /** * A record literal's field values for `dispatch`, in `dispatch.fields` * order with the `$present` mask words in their slots: what the * record struct is built from, or what an exploded parameter is * passed as. */ #lowerRecordLiteralFields(lit: RecordLiteral, dispatch: RecordDispatchInfo): GrowableArray { // Every property is evaluated once, in written order: explicit // values and spread sources alike, including one whose key a later // property replaces. `propVals[i]` is property i's value; the // per-field loop below only selects among them. let propVals = new Array(); for (let prop in lit.properties) { if (prop is PropertyAssignment) { var slotType: ValType | null = null; for (let f in dispatch.fields) { if (f.name != null && (f.name as String) == prop.name.name) { slotType = f.type; } } propVals.push(this.#lowerExpressionCtx(prop.value, slotType)); } else if (prop is SpreadElement) { propVals.push(this.#lowerExpression(prop.argument)); } else { propVals.push(-1); } if (!this.#cx().reachable) { return propVals; } } let args = new Array(); let i64Vt: ValType = new ValTypeI64(); // Presence contributions for the `$present` mask words: constant // bits for definitely-provided optional fields, dynamic parts for // optional fields fed by a spread source that itself tracks // presence. Bits are globally indexed; word = bit >> 6. let wordCount = presentWordCount(dispatch.optionalNames.length); let maskConstWords = new Array(); let presentWordSlots = new Array(); for (var w = 0; w < wordCount; w += 1) { maskConstWords.push(0); presentWordSlots.push(-1); } let maskDynWords = new Array(); let maskDynVals = new Array(); for (let f in dispatch.fields) { let key = if (f.name != null) f.name as String else ''; if (isPresentWordName(key)) { // Filled after the loop, once the contributions are known. for (var w = 0; w < wordCount; w += 1) { if (presentWordName(w) == key) { presentWordSlots[w] = args.length; } } args.push(-1); continue; } let optBit = dispatch.optionalBitOf(key); // Scanning from the end, the sources of this field up to the first // that always supplies it: an explicit property, a required spread // field, a class field. A spread copies only the fields present in // it, so each optional spread field after that source replaces the // value only when present. var v = -1; var definite = false; let optionalProps = new Array(); let optionalDispatches = new Array(); var pi = lit.properties.length - 1; while (pi >= 0 && !definite) { let prop = lit.properties[pi]; if (prop is PropertyAssignment) { if (prop.name.name == key) { v = propVals[pi]; definite = true; } } else if (prop is SpreadElement) { let spT = this.#substitutedNodeType(prop.argument); if (spT != null) { let sv = propVals[pi]; let nn = unwrapAliases(getNonNullableType(spT)); if (nn is RecordType && nn.properties.has(key)) { let srcDispatch = this.#cx().recordDispatchOf(nn); if (srcDispatch.optionalBitOf(key) >= 0) { optionalProps.push(pi); optionalDispatches.push(srcDispatch); } else { v = this.#recordDispatchRead(srcDispatch, key, sv); definite = true; } } else if (nn is ClassType) { // Class spreads copy PHYSICAL public fields; anything // else falls through and the missing-field bail fires. let ct = eraseTypeParameters(this.wasm, this.#resolveSelfType(nn as Type)) as ClassType; if (ct.members.has(key) && !(key.length > 0 && key.sliceBytes(0, 1) == '#')) { let infoOpt = if (let (true, info) = this.wasm.classInfos.get(getClassKey(ct))) info else null; if (infoOpt != null) { let st = infoOpt.structType; if (st.fieldIndexByName(key) >= 0) { let fieldRef = this.b.addRef(new IrRefField(st, key)); v = this.b.emit(IrOp.struct_get, this.#fieldValType(st, key), this.#coerceTo(sv, new ValTypeRef(st) as ValType), fieldRef); definite = true; } } } } } } pi -= 1; } if (v >= 0) { v = this.#conformToSlot(v, f.type, 'record literal field type'); } // Fold in the optional sources, earliest first. `anyPresent` is // whether any of them supplied the field (an i64 0 or 1), which is // the field's presence when no source always supplies it. var anyPresent = -1; for (var oi = optionalProps.length - 1; oi >= 0; oi -= 1) { let srcDispatch = optionalDispatches[oi]; let sv = propVals[optionalProps[oi]]; let srcBit = srcDispatch.optionalBitOf(key); let srcMask = this.#recordDispatchRead( srcDispatch, presentWordName(srcBit >> 6), sv); let bitVal = this.b.binary(IrOp.i64_and, i64Vt, this.b.binary(IrOp.i64_shr_u, i64Vt, srcMask, this.b.constI64((srcBit & 63) as i64, i64Vt)), this.b.constI64(1, i64Vt)); if (v < 0) { // Nothing earlier: an absent slot already holds the absent // representation. v = this.#conformToSlot(this.#recordDispatchRead(srcDispatch, key, sv), f.type, 'record literal field type'); } else { let presentB = this.b.newBlock(); let absentB = this.b.newBlock(); let merge = this.b.newBlock(); let chosen = this.b.blockParam(merge, f.type); let cond = this.b.emit(IrOp.i32_wrap_i64, new ValTypeI32() as ValType, bitVal, -1); this.b.brIf(cond, presentB, this.#noArgs(), absentB, this.#noArgs()); this.b.switchTo(presentB); let read = this.#conformToSlot( this.#recordDispatchRead(srcDispatch, key, sv), f.type, 'record literal field type'); this.b.br(merge, this.#oneArg(read)); this.b.switchTo(absentB); this.b.br(merge, this.#oneArg(v)); this.b.switchTo(merge); v = chosen; } anyPresent = if (anyPresent < 0) bitVal else this.b.binary(IrOp.i64_or, i64Vt, anyPresent, bitVal); } if (v < 0) { if (optBit < 0) { this.#bail('record literal field missing'); } // An omitted optional field: zero slot, mask bit stays 0. v = zeroValueOf(this.b, f.type); } if (optBit >= 0) { if (definite) { maskConstWords[optBit >> 6] = maskConstWords[optBit >> 6] | ((1 as i64) << ((optBit & 63) as i64)); } else if (anyPresent >= 0) { maskDynWords.push(optBit >> 6); maskDynVals.push(this.b.binary(IrOp.i64_shl, i64Vt, anyPresent, this.b.constI64((optBit & 63) as i64, i64Vt))); } } args.push(v); } for (var w = 0; w < wordCount; w += 1) { if (presentWordSlots[w] < 0) { continue; } var mask = this.b.constI64(maskConstWords[w], i64Vt); for (var d = 0; d < maskDynWords.length; d += 1) { if (maskDynWords[d] == w) { mask = this.b.binary(IrOp.i64_or, i64Vt, mask, maskDynVals[d]); } } args[presentWordSlots[w]] = mask; } return args; } /** A record value: the struct built from `args`, packed with its vtable. */ #packRecord(dispatch: RecordDispatchInfo, args: GrowableArray): i32 { return this.#cx().packRecord(dispatch, args); } /** * Reading a record member: the fat pointer's vtable slot holds the * getter for the field (record values may be adapted views, so the * read always dispatches — streaming's shape). */ #lowerRecordRead(rt: RecordType, member: MemberExpression): i32 { let recv = this.#lowerExpression(member.object); return this.#lowerRecordReadOn(rt, member, recv); } /** * Reading a field off a union of record types: the getter that reads * it belongs to the member the value is, and only the run time knows * the member, so the read is a chain of type tests like * `#applyRecordUnionAdaptation`'s. Each arm reads the field through * its own member's dispatch and branches to a join carrying the * value. The field has one type across the members — the checker * requires it — so the join takes one parameter, and the tail of the * chain traps. */ #lowerRecordUnionRead(members: GrowableArray, member: MemberExpression): i32 { let recv = this.#lowerExpression(member.object); return this.#lowerRecordUnionReadOn(members, member, recv); } /** The receiver-value core of a record-union member read. */ #lowerRecordUnionReadOn(members: GrowableArray, member: MemberExpression, recv: i32): i32 { if (!isRefValType(this.#valueType(recv))) { this.#bail('record union read receiver'); } let mergeB = this.b.newBlock(); let resultParam = this.b.blockParam(mergeB, this.#fieldReadValType(member)); for (let rt in members) { let dispatch = this.#cx().recordDispatchOf(rt); let fatVt = new ValTypeRef(dispatch.fatPtrType) as ValType; let isMember = this.b.emit(IrOp.ref_test, new ValTypeI32() as ValType, recv, this.b.internType(fatVt)); let hitB = this.b.newBlock(); let nextB = this.b.newBlock(); this.b.brIf(isMember, hitB, this.#noArgs(), nextB, this.#noArgs()); this.b.switchTo(hitB); let field = this.#lowerRecordReadOn(rt, member, recv); this.b.br(mergeB, this.#oneArg(field)); this.b.switchTo(nextB); } this.b.unreachable(); this.b.switchTo(mergeB); this.#cx().reachable = true; return resultParam; } /** The receiver-value core of a record member read. */ #lowerRecordReadOn(rt: RecordType, member: MemberExpression, recvIn: i32): i32 { let dispatch = this.#cx().recordDispatchOf(rt); let propName = accessKeyOf(this.func.model, member); let recv = this.#conformRecordReceiver(dispatch, recvIn); let result = this.#recordDispatchRead(dispatch, propName, recv); return this.#reassertNonNull(result, this.#fieldReadValType(member)); } /** * `[a, b, c]`: each element lowers against the array's element * type, then one array.new_fixed builds the array — streaming's * shape. The literal's checked type names the wasm array type. */ #lowerArrayLiteral(lit: ArrayLiteral): i32 { let vt = this.#exprValType(lit); let arr = asWasmArray(vt); if (arr == null) { this.#bail('array literal type'); } let elemVt = arr.elementType; let args = new Array(); for (let el in lit.elements) { let v = this.#lowerExpressionCtx(el, elemVt); if (!this.#cx().reachable) { return NO_VALUE; } // Packed i8 elements store i32 values; everything else must // conform to the element type. args.push(if (elemVt is ValTypeI8) v else this.#conformToSlot(v, elemVt, 'array literal element type')); } return this.b.emit(IrOp.array_new_fixed, vt, this.b.pushArgList(args), this.b.addRef(new IrRefType(arr))); } /** * The members of `layout` whose literals agree with one of the tuples * `t` names (a tuple or a union of them, boxed or inline), or null when * that is every member, or `t` names something else. */ #narrowedMembers(layout: InlineLayout, t: Type): GrowableArray | null { let resolved = unwrapAliases(t); let named = if (resolved is UnionType) resolved.types else growable([resolved]); let allowed = new Array(); for (var m = 0; m < layout.originals.length; m += 1) { allowed.push(false); } for (let n in named) { let tuple = unwrapAliases(n); let inlineTuple = if (tuple is TupleType) { new InlineTupleType(tuple.elementTypes) } else if (tuple is InlineTupleType) { tuple } else { null }; if (inlineTuple == null) { return null; } let like = inlineMembersLike(layout, inlineTuple); for (var m = 0; m < like.length; m += 1) { if (like[m]) { allowed[m] = true; } } } for (let a in allowed) { if (!a) { return allowed; } } return null; } /** * Whether `values`, an inline value in `layout`'s slots, is one of the * tuples `target` names: an or of the member tests of the members whose * literals agree with them. */ #inlineIsTest(values: GrowableArray, layout: InlineLayout, target: Type): i32 { let i32Vt = new ValTypeI32() as ValType; let narrowed = this.#narrowedMembers(layout, target); if (narrowed == null) { return this.b.constI32(1, i32Vt); } let allowed = narrowed; var cond = -1; for (var m = 0; m < allowed.length; m += 1) { if (allowed[m]) { let test = memberTest(this.#cx(), layout, values, m); cond = if (cond == -1) test else this.b.binary(IrOp.i32_or, i32Vt, cond, test); } } return if (cond == -1) this.b.constI32(0, i32Vt) else cond; } /** * `x is T`: ref.test for reference sources; non-reference sources * fold to a constant by type identity (the operand still evaluates * for effects). Mirrors streaming's IsExpression codegen. */ #lowerIs(isExpr: IsExpression): i32 { let sourceType = this.#substitutedNodeType(isExpr.expression); if (sourceType == null) { this.#bail('untyped is source'); } let targetTypeOpt = this.func.model.getNodeType(isExpr.typeAnnotation); if (targetTypeOpt == null) { this.#bail('untyped is target'); } let targetType = substituteTypeParamsInFunctionContext(this.wasm, this.func, targetTypeOpt); // An inline union's member is told by its literals (#812): the test is // whether the value is one of the members whose literals the tested // tuples agree with, read off the slots, so nothing is boxed. let sourceLayout = inlineLayoutOf(this.wasm, sourceType); if (sourceLayout != null) { let source = this.#lowerMultiSource(isExpr.expression); let layout = source.layout ?? sourceLayout; return this.#inlineIsTest(source.values, layout, targetType); } return this.#isTestOf(this.#lowerExpression(isExpr.expression), sourceType, targetType); } /** Whether `value`, of type `sourceType`, is a `targetType`, as an i32. */ #isTestOf(value: i32, sourceType: Type, targetType: Type): i32 { // An existential holds a record of a shape only the run time knows: // `is` asks whether it is exactly a value of the closed target type. let sourceBase = unwrapAliases(sourceType); let targetBase = unwrapAliases(targetType); if (sourceBase is RecordType && targetBase is RecordType) { if (sourceBase.isExistential && !targetBase.isExistential) { return existentialIs(this.#cx(), value, this.#cx().recordDispatchOf(sourceBase), this.#cx().recordDispatchOf(targetBase)); } } if (!isRefType(sourceType)) { let same = getTypeUniqueKey(sourceType) == getTypeUniqueKey(targetType) || targetType is AnyRefType || targetType is EqRefType; return this.b.constI32(if (same) 1 else 0, new ValTypeI32()); } if (!isRefValType(this.#valueType(value))) { this.#bail('is operand type'); } // An interface target means "the value's class implements it" // when some class can answer true (docs/design/ // interface-type-tests.md); with no such class the fat pointer // test below is the whole answer. if (targetBase is InterfaceType && this.#cx().typeTestHasSlot(targetBase)) { return this.#cx().typeTestImplements(value, targetBase); } if (targetBase is TupleType && targetBase.elementTypes.length > 0 && isTupleTag(targetBase.elementTypes[0])) { return this.#tupleTagTest(value, targetBase); } if (targetBase is TupleType) { let source = if (sourceBase is TupleType) sourceBase else null; let elementwise = this.#tupleIsTest(value, source, targetBase); if (elementwise >= 0) { return elementwise; } } let targetVt = typeToValTypeWithSub(this.wasm, targetType, this.func); let target = refTargetOf(targetVt); if (target == null) { this.#bail('is target kind: ' + typeToString(targetType)); } // A value held as an interface (or a record) is a fat POINTER — the // instance paired with a vtable — so testing it directly asks // whether the pair is a Square, which is false for every concrete // class. Unwrap to the instance first, exactly as identity compares // already do. Only when the target is a class: interface-to- // interface is a question about vtables, not representation, and // `ref.test` cannot answer it either way. let tested = if (targetBase is ClassType) { this.#cx().unwrapFatPointer(sourceBase, value) } else { value }; // Streaming emits a plain (non-null) ref.test either way. return this.b.emit(IrOp.ref_test, new ValTypeI32(), tested, this.b.internType(new ValTypeRef(target) as ValType)); } /** * `value is target` for a tuple type `target` with no literal tag, when * the answer depends on the elements: `value` is of type `source`, or of * a type that is not a tuple when `source` is null. A tuple of another * struct is tested element by element (#483): the structs are * unrelated, so a test of the tuple itself would always be false. A * tuple of the target's struct, which tuples that meet share (#1137), * is tested for each element the target narrows. -1 when a test of the * struct alone answers. */ #tupleIsTest(value: i32, source: TupleType | null, target: TupleType): i32 { let to = this.#tupleStruct(target); if (source != null && this.#tupleStruct(source) !== to) { return this.#tupleIsTestAcross(value, source, target); } if (!this.#cx().tupleNarrows(to, target)) { return -1; } let toVt = new ValTypeRef(to) as ValType; let have = this.#valueType(value); if (have is ValTypeRef && isValSubtype(have, toVt)) { return this.#cx().tupleElementChecks(value, to, target, false); } let i32Vt = new ValTypeI32() as ValType; let join = this.b.newBlock(); let result = this.b.blockParam(join, i32Vt); let test = this.b.emit(IrOp.ref_test, i32Vt, value, this.b.internType(toVt)); let hit = this.b.newBlock(); let miss = this.b.newBlock(); this.b.brIf(test, hit, this.#noArgs(), miss, this.#noArgs()); this.b.switchTo(hit); let cast = this.b.emit(IrOp.ref_cast, toVt, value, this.b.internType(toVt)); this.b.br(join, growable([this.#cx().tupleElementChecks(cast, to, target, false)])); this.b.switchTo(miss); this.b.br(join, growable([this.b.constI32(0, i32Vt)])); this.b.switchTo(join); return result; } /** `#tupleIsTest` for a tuple of a struct other than the target's. */ #tupleIsTestAcross(value: i32, source: TupleType, target: TupleType): i32 { let from = this.#tupleStruct(source); if (source.elementTypes.length != target.elementTypes.length) { return -1; } let i32Vt = new ValTypeI32() as ValType; var all = this.b.constI32(1, i32Vt); for (var i = 0; i < source.elementTypes.length; i += 1) { let name = i32ToString(i); let index = from.fieldIndexByName(name); if (index < 0) { // A zero-width element has no field to test. continue; } let element = this.b.emit(IrOp.struct_get, from.fields[index].type, value, this.b.addRef(new IrRefField(from, name))); let test = this.#isTestOf(element, source.elementTypes[i], target.elementTypes[i]); all = this.b.binary(IrOp.i32_and, i32Vt, all, test); } return all; } /** * `throw e` in statement or expression position: the payload value is * stored in the shared exception-payload global (the tag itself carries * no values), then the module-wide tag is thrown and the current block * is marked unreachable. */ #lowerThrowStatement(thr: ThrowExpression): void { this.wasm.ensureExceptionInfra(this.func.model); let tagOpt = this.wasm.exceptionTag; let payload = this.wasm.exceptionPayload; if (tagOpt == null || payload == null) { this.#bail('exception infra missing'); } var value = this.#lowerExpressionCtx(thr.argument, payload.type); value = this.#conformToSlot(value, payload.type, 'throw payload type'); this.b.emitVoid(IrOp.global_set, value, this.b.addRef(new IrRefGlobal(payload))); this.b.throwWithTag(this.#noArgs(), this.b.addRef(new IrRefTag(tagOpt))); this.#cx().reachable = false; } #lowerNumberLiteral(lit: NumberLiteral): i32 { let vt = this.#exprValType(lit); if (isRefValType(vt)) { return lowerBigIntLiteral(this.#cx(), lit.raw, false); } if (vt is ValTypeI32) { if (let (true, value) = parseI32(lit.raw)) { return this.b.constI32(value, vt); } // Destructure-then-if: the bootstrap rejects this call inside an // if-let tuple pattern. let (intOk, intValue) = parseI32WithHex(lit.raw); if (intOk) { return this.b.constI32(intValue, vt); } this.#bail('unparseable i32 literal'); } if (vt is ValTypeI64) { if (let (true, value) = parseI64OrU64Literal(lit.raw)) { return this.b.constI64(value, vt); } this.#bail('unparseable i64 literal'); } if (vt is ValTypeF64) { return this.b.constF64(parseFloatLiteral(lit.raw), vt); } if (vt is ValTypeF32) { return this.b.constF32(parseFloatLiteral(lit.raw), vt); } return this.#bailValue('literal type'); } /** * A decorator body's `context`: the record `{name, get[, set]}` for * the application this body is compiled for — the intermediate class * the function was registered on, or the one the host constructor is * initializing. `name` is the decorated field's name as a literal; * `get` and `set` are closure pairs around the wrappers RTA registered * for the application (`decoratorContextWrappers`). */ #lowerDecoratorContext(ident: Identifier): i32 { var appOpt = this.#cx().decoratorApplication; if (appOpt == null) { appOpt = this.func.declaringType; } if (appOpt == null || appOpt.decoratorFieldName == null) { this.#bail('decorator context outside an application'); } let app = appOpt; let fieldName = app.decoratorFieldName as String; // The function context binds the application's `T` first — the // body's own in an accessor, `substitutionApplication` in a host // constructor — so the closures' shapes are this field's type, // which is what the wrappers RTA minted for this application take. let tOpt = this.#substitutedNodeType(ident); if (tOpt == null) { this.#bail('untyped decorator context'); } let bt = baseType(tOpt as Type); if (!(bt is RecordType)) { this.#bail('decorator context type'); } let rt = bt as RecordType; let dispatch = this.#cx().recordDispatchOf(rt); let appKey = getClassKey(app) as String; let args = new Array(); for (let f in dispatch.fields) { let key = if (f.name != null) f.name as String else ''; if (key == 'name') { args.push(this.#lowerStringLiteral(new StringLiteral(fieldName, ident.loc))); } else if (key == 'get' || key == 'set') { let wrapperKey = `${appKey}::${key}`; let wrapper = if (let (true, w) = this.wasm.decoratorContextWrappers.get(wrapperKey)) w else null; if (wrapper == null) { this.#bail(`decorator context ${key} wrapper missing`); } let fieldT = if (rt.properties.has(key)) rt.properties[key] else null; if (fieldT == null || !(fieldT is FunctionType)) { this.#bail('decorator context accessor type'); } let closureStruct = getClosureStruct(this.wasm, fieldT); let funcVal = this.b.emit(IrOp.ref_func, closureStruct.fields[0].type, -1, this.b.addRef(new IrRefFunc(wrapper))); let nullCtx = this.b.emit(IrOp.ref_null, new ValTypeStructRef() as ValType, -1, -1); args.push(this.b.emit(IrOp.struct_new, new ValTypeRef(closureStruct) as ValType, this.b.pushArgList(this.#twoValues(funcVal, nullCtx)), this.b.addRef(new IrRefType(closureStruct)))); } else { this.#bail('decorator context field'); } } return this.#packRecord(dispatch, args); } #lowerIdentifier(ident: Identifier): i32 { // An inline binding is its slots; the checker leaves only the uses // that read it whole as one value (`==`), which box it here. let held = this.#heldInline(ident); if (held != null) { return this.#boxInline(held); } // A use typed `void` or `never` may still have an ordinary slot // behind it, because narrowing can empty a use's type (see the // `never` note further down). So this is only a question; the slot // lookups below answer it, and a read with no slot is the one that // yields nothing. let zeroWidthUse = this.#isZeroWidthExpr(ident as Expression); let sym = this.func.model.resolve(ident); if (sym == null) { if (zeroWidthUse) { return NO_VALUE; } this.#bail('unresolved identifier'); } let resolved = sym.resolveTarget(); if (resolved.isDecoratorContext) { return this.#lowerDecoratorContext(ident); } if (!this.#cx().env.has(resolved.id)) { if (zeroWidthUse) { // A zero-width binding (a `void` parameter) holds no value and // reading it has no effect, so it yields nothing. Returning // NO_VALUE here keeps it away from #valueType. return NO_VALUE; } // Module-level globals read via global.get; the checked type of // the identifier reasserts non-nullness where the global's wasm // type is a nullable ref. if (let (true, g) = this.wasm.globalMap.get(resolved.id)) { let globalRef = this.b.addRef(new IrRefGlobal(g)); let raw = this.b.emit(IrOp.global_get, g.type, -1, globalRef); return this.#reassertNonNull(raw, this.#exprValType(ident)); } // A function referenced as a VALUE: a fresh closure pair around // the discovery-registered value wrapper (already // reference-taken for the elem section) — streaming's shape, // which also makes two references of one function compare // unequal by identity. let identT = this.#substitutedNodeType(ident); if (identT is FunctionType) { let isSpecialized = identT.typeArguments != null && (identT.typeArguments as GrowableArray).length > 0; let keyName = if (isSpecialized) { getSpecializedName(ident.name, identT.typeArguments as GrowableArray) } else { i32ToString(resolved.id as i32) }; let wrapperKey = getFunctionValueWrapperKey(keyName, identT); if (let (true, wrapper) = this.wasm.functionValueWrappers.get(wrapperKey)) { let closureStruct = getClosureStruct(this.wasm, identT); let funcVal = this.b.emit(IrOp.ref_func, closureStruct.fields[0].type, -1, this.b.addRef(new IrRefFunc(wrapper))); let nullCtx = this.b.emit(IrOp.ref_null, new ValTypeStructRef() as ValType, -1, -1); return this.b.emit(IrOp.struct_new, new ValTypeRef(closureStruct) as ValType, this.b.pushArgList(this.#twoValues(funcVal, nullCtx)), this.b.addRef(new IrRefType(closureStruct))); } } this.#bail('non-local identifier: ' + ident.name); } var value = this.#cx().env[resolved.id]; if (value == NO_VALUE) { // A zero-width binding bound by a pattern: `bindPatternSym` parks // NO_VALUE in the environment for one, so the slot is absent even // though the symbol is present. return NO_VALUE; } if (this.#isCelled(resolved.id)) { value = this.#cellRead(value); } // Narrowing arrives as explicit coercions (ir.md §5): the checker may // have narrowed this reference's type at this use site — most // commonly `x != null` making a nullable binding non-null. Re-assert // non-nullness when the narrowed type demands it; subtype narrowing // (casts to a subclass) is not lowered yet and bails. let nodeType = this.func.model.getNodeType(ident); if (nodeType == null || isVoidOrNeverType(nodeType)) { // `never` here is narrowing that emptied the type, which happens // when the test that narrowed it was a tautology: `catch (e)` // binds `Error`, so after `if (e is Error) { return ... }` the // rest of the handler reads `e` at `never`. There is no wasm type // to conform to and nothing to re-assert — the slot still holds // the `Error` it was bound to, and control cannot reach the read // anyway, so read it as it stands. return value; } let wantVt = typeToValTypeWithSub(this.wasm, nodeType, this.func); let haveVt = this.#valueType(value); if (valTypeIdentical(haveVt, wantVt)) { return value; } if (wantVt is ValTypeRef && haveVt is ValTypeRefNull) { let wantTarget = wantVt.target; let haveTarget = haveVt.target; if (wantTarget == haveTarget) { return this.b.emit(IrOp.ref_as_non_null, wantVt, value, -1); } } if (wantVt is ValTypeRefExtern && haveVt is ValTypeExternRef) { return this.b.emit(IrOp.ref_as_non_null, wantVt, value, -1); } if (isValSubtype(haveVt, wantVt)) { // Widening (e.g. a non-null value read at a nullable type): the // value passes as-is under wasm subtyping. return value; } // A binding narrowed by `x is I` to an interface some test reads // type info for, read at `I` while the value in hand is the // instance, an `anyref`, or another interface's fat pointer: pack // it the way the test answered. Before the downcast below, which // would cast an `anyref` to the fat pointer and trap. if (isRefValType(haveVt)) { let packed = this.#cx().typeTestPackForSlot(value, wantVt); if (packed >= 0) { return packed; } } // Downcast narrowing: the checker's flow analysis licenses reads // at a SUBTYPE of the stored value's type (`x is Dog` narrowing // later reads to Dog). Narrowing arrives as explicit coercions // (ir.md §5): emit the cast at the use. Only strictly-downward // casts qualify — any other mismatch stays a loud bail. if ((wantVt is ValTypeRef || wantVt is ValTypeRefNull) && isRefValType(haveVt) && isValSubtype(wantVt, haveVt)) { return this.b.emit(IrOp.ref_cast, wantVt, value, this.b.internType(wantVt)); } // The same narrowing, one representation further out: an // interface-typed binding narrowed by `c is Cell` is read at // `Cell` while the value in hand is still the fat pointer, and // a pair is not a subtype of the instance inside it. Unwrap and ask // again — `unwrapFatPointerValue` returns the value untouched when // it is not a fat pointer, so nothing else takes this path. let inner = this.#cx().unwrapFatPointerValue(value); if (inner != value) { let innerVt = this.#valueType(inner); if (valTypeIdentical(innerVt, wantVt) || isValSubtype(innerVt, wantVt)) { return inner; } if ((wantVt is ValTypeRef || wantVt is ValTypeRefNull) && isRefValType(innerVt) && isValSubtype(wantVt, innerVt)) { return this.b.emit(IrOp.ref_cast, wantVt, inner, this.b.internType(wantVt)); } } // A tuple narrowed by `t is (Bird, i32)` is read at a struct unrelated // to the one in hand (#483): a copy with each element cast down. let copied = this.#cx().copyTuple(value, haveVt, wantVt, `identifier type shift: ${ident.name}`); if (copied >= 0) { return copied; } return this.#bailValue(`identifier type shift: ${ident.name}`); } /** Binary operators (see ir/operators.zena). */ #lowerBinary(bin: BinaryExpression): i32 { return lowerBinary(this.#cx(), bin); } /** * Erased-operand equality: streaming's runtime type-dispatch * diamond, tier for tier. Identity first (which also makes * null == null true); then not-ref-equal + either-null -> false; * then String content equality when BOTH are Strings; then each * REACHED Box specialization unwraps field 1 and * compares at its width; anything else -> false. Operands coerce * anyref -> eqref first, since ref.eq requires eqref. */ #unwrapFatPointer(t: Type, value: i32): i32 { return this.#cx().unwrapFatPointer(t, value); } /** Erased (eqref-tiered) equality (see ir/equality.zena). */ #lowerErasedEq(leftIn: i32, rightIn: i32): i32 { return lowerErasedEq(this.#cx(), leftIn, rightIn); } /** Unary operators (see ir/operators.zena). */ #lowerUnary(un: UnaryExpression): i32 { return lowerUnary(this.#cx(), un); } #lowerIfExpression(expr: IfExpression): i32 { return lowerIfExpression(this.#cx(), expr); } #lowerTryExpression(expr: TryExpression): i32 { return lowerTryExpression(this.#cx(), expr); } #lowerValueNode(node: Node, isVoid: boolean, ctxVt: ValType | null): i32 { return lowerValueNode(this.#cx(), node, isVoid, ctxVt); } // -- Match expressions ------------------------------------------------- #lowerMatch(expr: MatchExpression): i32 { return lowerMatch(this.#cx(), expr); } /** Refutable tests for `pattern` (see ir/patterns.zena). */ #lowerPatternTest(pattern: Node, value: i32, failB: i32): void { lowerPatternTest(this.#cx(), pattern, value, failB); } #recordDispatchRead(dispatch: RecordDispatchInfo, propName: String, receiver: i32): i32 { return this.#cx().recordDispatchRead(dispatch, propName, receiver); } /** All symbols a pattern binds (see ir/patterns.zena). */ #collectPatternBindSyms(pattern: Node, out: GrowableArray): void { collectPatternBindSyms(this.#cx(), pattern, out); } /** Binds `pattern`'s variables (see ir/patterns.zena). */ #lowerPatternBindings(pattern: Node, value: i32): void { lowerPatternBindings(this.#cx(), pattern, value); } /** * Binds a pattern symbol, downcasting to the (narrowed) binding type * when needed — the preceding ref.test guarantees the cast succeeds. */ /** * Values stored into a binding conform to the binding's DECLARED * type: env-merge params are typed from the binding, so a wider * value (an erased eqref flowing out of a merge-typed expression) * must cast down at the store — streaming's typed locals enforce * exactly this at local.set. Only downward ref casts qualify; any * other mismatch bails loudly. */ #conformToSlot(value: i32, want: ValType, reason: String): i32 { return this.#cx().conformToSlot(value, want, reason); } /** LoweringContext.conformReceiver: re-assert a nullable receiver. */ #conformReceiver(receiver: i32, want: ValType, reason: String): i32 { return this.#cx().conformReceiver(receiver, want, reason); } #conformToBinding(value: i32, bindingVt: ValType): i32 { return this.#cx().conformToBinding(value, bindingVt); } #bindPatternSym(sym: Symbol, value: i32): void { this.#cx().bindPatternSym(sym, value); } #isCelled(symId: SymbolId): boolean { return this.#cx().isCelled(symId); } #cellAlloc(bindingVt: ValType, value: i32): i32 { return this.#cx().cellAlloc(bindingVt, value); } #cellRead(cell: i32): i32 { return this.#cx().cellRead(cell); } #cellWrite(cell: i32, value: i32): void { this.#cx().cellWrite(cell, value); } /** Whether `pattern` can fail to match (see ir/patterns.zena). */ #patternRefutable(pattern: Node): boolean { return patternRefutable(this.#cx(), pattern); } /** Whether `pattern` introduces bindings (see ir/patterns.zena). */ #patternBinds(pattern: Node): boolean { return patternBinds(this.#cx(), pattern); } /** Branch to `failB` unless `cond` (see ir/patterns.zena). */ #branchUnless(cond: i32, failB: i32): void { branchUnless(this.#cx(), cond, failB); } /** Scalar and reference `as` casts (see ir/operators.zena). */ #lowerAs(expr: AsExpression): i32 { return lowerAs(this.#cx(), expr); } /** u32/u64-typed operand test (see ir/operators.zena). */ #isUnsignedNode(expr: Expression): boolean { return isUnsignedNode(this.#cx(), expr); } /** * `this` in an instance method: the receiver block param. Method * receivers are typed non-null, so the re-assert below is for the * contexts that still hold `this` nullable, such as a closure's * captured-context field. */ #lowerThis(expr: Expression): i32 { if (this.#cx().thisValue < 0) { // NOT checker-impossible: default-parameter expressions are // supplied by the CALLER (desugared into the call's argument // list), so `this` in a default like `end = this.length - start` // reaches the lowering of a call site in a plain function, where // it means the callee's receiver. Until #lowerMethodCall binds // the receiver for default-argument expressions, bail. this.#bail('this outside instance method'); } let haveVt = this.#valueType(this.#cx().thisValue); if (haveVt is ValTypeRefNull) { let want: ValType = new ValTypeRef(haveVt.target); return this.b.emit(IrOp.ref_as_non_null, want, this.#cx().thisValue, -1); } return this.#cx().thisValue; } #resolveSelfType(t: Type): Type { return this.#cx().resolveSelfType(t); } /** * `new C(args)` for plain (non-extension) classes: struct.new_default, * store the vtable when the class has one, call the constructor * directly. Zero-field case classes read their pre-allocated * singleton global instead. */ #lowerNew(expr: NewExpression): i32 { let nodeType = this.#substitutedNodeType(expr); if (nodeType == null) { this.#bail('untyped new'); } // Inside a class body, `new Point(...)` may be typed via the checker's // Point_This marker; resolve to the concrete class first. Erase before the // class check, not after: constructing a resource is typed `Own`, a // distinct-type alias that erases to `R`, so checking first would bail on // a type that does have a class behind it. // A `new` on an interface: the checker types the callee as the // instantiated interface (its result may narrow to a class, so the // expression's own type does not say which kind of `new` this is). let calleeT = this.#substitutedNodeType(expr.callee); if (calleeT is InterfaceType) { let calleeResolved = eraseTypeParameters(this.wasm, calleeT); if (calleeResolved is InterfaceType) { return this.#lowerInterfaceNew(expr, calleeResolved); } } let selfResolved = eraseTypeParameters(this.wasm, this.#resolveSelfType(nodeType)); if (!(selfResolved is ClassType)) { this.#bail('new of non-class'); } let classType = eraseTypeParameters(this.wasm, selfResolved); if (!(classType is ClassType)) { this.#bail('new erased kind'); } let ctorMethName = if (expr.constructorName != null) { '.' + getNodeMemberName(this.func.model, expr.constructorName as Node) } else { '' }; if (classType.isExtension) { // Extension `new` is a plain call: the constructor takes the // declared arguments (no receiver) and RETURNS the underlying // value (streaming's extension branch in generateNewExpression). let ctorKey2 = getClassMethodKey(getClassKey(classType), ctorMethName); let ctor = if (let (true, c) = this.wasm.classMethodMap.get(ctorKey2)) c else null; if (ctor == null) { this.#bail('extension constructor not found: ' + (getClassKey(classType) as String)); } let sig = ctor.signature; if (sig.returns.length != 1 || sig.params.length != this.#liveArgCount(expr.args)) { this.#bail('extension constructor shape'); } let args = new Array(); var sIdx = 0; for (var i = 0; i < expr.args.length; i += 1) { if (this.#dropZeroWidthArg(expr.args[i])) { continue; } let pt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(pt, 'constructor param type'); var value = this.#lowerExpressionCtx(expr.args[i], pt); value = this.#conformToSlot(value, pt, 'constructor argument type'); args.push(value); } this.#requireSupported(sig.returns[0], 'extension constructor type'); return this.b.callFunc(this.b.addRef(new IrRefFunc(ctor)), args, sig.returns[0]); } let cKey = getClassKey(classType); let classInfo = if (let (true, info) = this.wasm.classInfos.get(cKey)) info else null; if (classInfo == null) { this.#bail('class not discovered'); } let {structType} = classInfo; new ValTypeRef(structType); let {singletonGlobal} = classInfo; if (singletonGlobal != null && expr.constructorName == null) { let singleton = singletonGlobal; let globalRef = this.b.addRef(new IrRefGlobal(singleton)); let sv = this.b.emit(IrOp.global_get, singleton.type, -1, globalRef); this.#cx().exactValues.add(sv); return sv; } return this.#constructInstance(classInfo, cKey, expr.args, ctorMethName); } /** * `new I.name(...)` on an interface: a call to the constructor's * monomorphized factory, which RTA minted at this instantiation's * type arguments (registerInterfaceConstructorUse). The factory's own * return already packed the instance into an interface value. */ #lowerInterfaceNew(expr: NewExpression, it: InterfaceType): i32 { let ctorName = if (expr.constructorName != null) { getNodeMemberName(this.func.model, expr.constructorName as Node) } else { '' }; let targsOpt = it.typeArguments; let typeArgs = if (targsOpt != null) targsOpt else new Array(); let baseName = getInterfaceConstructorBaseName(it, ctorName); let specKey = getSpecializedFunctionKey(baseName, typeArgs); let target = if (let (true, f) = this.wasm.specializedFunctions.get(specKey)) f else null; if (target == null) { this.#bail('interface constructor not instantiated: ' + baseName); } let sig = target.signature; if (sig.params.length != this.#liveArgCount(expr.args) || sig.returns.length != 1) { this.#bail('interface constructor arity'); } let args = new Array(); var sIdx = 0; for (var i = 0; i < expr.args.length; i += 1) { if (this.#dropZeroWidthArg(expr.args[i])) { continue; } let pt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(pt, 'constructor param type'); var value = this.#lowerExpressionCtx(expr.args[i], pt); value = this.#conformToSlot(value, pt, 'constructor argument type'); args.push(value); } this.#requireSupported(sig.returns[0], 'interface constructor result'); return this.b.callFunc(this.b.addRef(new IrRefFunc(target)), args, sig.returns[0]); } /** * The shared construction tail of `new` and range literals: a call * to the class's constructor factory, which allocates with one * struct.new and returns the instance (non-nullable-refs.md). */ #constructInstance(classInfo: ClassCodegenInfo, cKey: ClassKey, argExprs: GrowableArray, ctorMethName: String = ''): i32 { let ctorKey = getClassMethodKey(cKey, ctorMethName); let ctor = if (let (true, c) = this.wasm.classMethodMap.get(ctorKey)) c else null; if (ctor == null) { this.#bail(`constructor missing for ${cKey as String} ${ctorMethName}`); } let sig = ctor.signature; let inlineParams = this.#inlineLayoutsOf(ctor); if (sig.params.length != this.#liveArgWidth(argExprs, inlineParams) || sig.returns.length != 1) { this.#bail('constructor arity adaptation'); } // A constructor whose record parameter explodes: a `new` with the // fields on hand calls the factory's core (see #lowerDirectCallTo). let explosion = ctor.explosion; if (explosion != null && explosion.forwarder === ctor && this.#passesUnpackedArg(argExprs, explosion)) { let coreRef = this.b.addRef(new IrRefFunc(explosion.core)); let built = this.b.callFunc(coreRef, this.#lowerExplodedArgs(argExprs, explosion), sig.returns[0]); this.#cx().exactValues.add(built); return built; } let args = new Array(); var sIdx = 0; for (var i = 0; i < argExprs.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(argExprs[i], inlineLayout, args); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(argExprs[i])) { continue; } let paramVt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(paramVt, 'constructor param type'); var value = this.#lowerExpressionCtx(argExprs[i], paramVt); value = this.#conformToSlot(value, paramVt, 'constructor argument type'); args.push(value); } let ctorRef = this.b.addRef(new IrRefFunc(ctor)); let inst = this.b.callFunc(ctorRef, args, sig.returns[0]); this.#cx().exactValues.add(inst); return inst; } /** * Map literals construct their checked HashMap class (shell + * constructor with the capacity argument) and then call `[]=` once * per entry — streaming's shape, including the minimum capacity. */ #lowerMapLiteral(lit: MapLiteral): i32 { let ct = this.#substitutedNodeType(lit); if (ct == null) { this.#bail('untyped map literal'); } if (!(ct is ClassType)) { this.#bail('map literal type'); } let cKey = getClassKey(ct); let infoOpt = if (let (true, info) = this.wasm.classInfos.get(cKey)) info else null; if (infoOpt == null) { this.#bail('map class not discovered'); } let ctor = if (let (true, c) = this.wasm.classMethodMap.get( getClassMethodKey(cKey, ''))) c else null; if (ctor == null) { this.#bail('map constructor missing'); } if (ctor.signature.params.length != 1 || !(ctor.signature.params[0] is ValTypeI32) || ctor.signature.returns.length != 1) { this.#bail('map constructor shape'); } let setFn = if (let (true, f) = this.wasm.classMethodMap.get( getClassMethodKey(cKey, '[]='))) f else null; if (setFn == null) { this.#bail('map setter missing'); } let setSig = setFn.signature; if (setSig.params.length != 3 || setSig.returns.length != 0) { this.#bail('map setter shape'); } let capacity = if (lit.entries.length > 16) lit.entries.length else 16; let capValue = this.b.constI32(capacity, new ValTypeI32()); let inst = this.b.callFunc(this.b.addRef(new IrRefFunc(ctor)), this.#oneArg(capValue), ctor.signature.returns[0]); this.#cx().exactValues.add(inst); for (let entry in lit.entries) { this.#requireSupported(setSig.params[1], 'map key type'); this.#requireSupported(setSig.params[2], 'map value type'); var key = this.#lowerExpressionCtx(entry.key, setSig.params[1]); key = this.#conformToSlot(key, setSig.params[1], 'map key type'); var value = this.#lowerExpressionCtx(entry.value, setSig.params[2]); value = this.#conformToSlot(value, setSig.params[2], 'map value type'); let args = growable([inst, key, value]); this.b.callFunc(this.b.addRef(new IrRefFunc(setFn)), args, null); } return inst; } /** * `left |> right`: the left value is an SSA value on the pipeline * stack; every $ inside the right side reads it directly — no * temporaries needed. */ #lowerPipeline(pipe: PipelineExpression): i32 { let left = this.#lowerExpression(pipe.left); this.#cx().pipelineValues.push(left); let result = try { this.#lowerExpression(pipe.right) } catch (e) { this.#cx().pipelineValues.pop(); throw e }; this.#cx().pipelineValues.pop(); return result; } #lowerPipePlaceholder(): i32 { if (this.#cx().pipelineValues.length == 0) { this.#bail('pipeline placeholder outside pipeline'); } return this.#cx().pipelineValues[this.#cx().pipelineValues.length - 1]; } /** * Range literals construct their prelude range class exactly like * `new`: the range form names the class (BoundedRange carries start * and end, FromRange start, ToRange end, FullRange nothing) and its * operands become the constructor arguments. */ #lowerRangeExpr(expr: RangeExpression): i32 { let nodeType = this.#substitutedNodeType(expr); if (nodeType == null) { this.#bail('untyped range'); } if (!(nodeType is ClassType)) { this.#bail('range of non-class'); } // The range form is the AST shape itself: which bounds exist is // exhaustive by construction (start/end presence), so there is // nothing to dispatch on — the checker already resolved the class // for this shape, and the constructor arity check below catches // any operand mismatch loudly. let operands = new Array(); if (expr.start != null) { operands.push(expr.start as Expression); } if (expr.end != null) { operands.push(expr.end as Expression); } let cKey = getClassKey(nodeType); let infoOpt = if (let (true, info) = this.wasm.classInfos.get(cKey)) info else null; if (infoOpt == null) { this.#bail('range class not discovered'); } return this.#constructInstance(infoOpt, cKey, operands); } /** * Member writes: direct struct.set when the property is a physical * field and no transitive subclass provides a setter for it. Setter * dispatch and static writes bail. Compound assignments read through * the same receiver value, so the object expression evaluates once. */ #lowerMemberAssign(assign: AssignmentExpression): i32 { let member = assign.left as MemberExpression; if (member.optional) { this.#bail('optional member write'); } if (this.func.model.isStaticAccess(member)) { return this.#lowerStaticMemberWrite(assign, member); } if (member.object is SuperExpression) { return this.#lowerSuperWrite(assign, member); } let {operator} = assign; if (operator == '??') { return this.#lowerMemberNullishAssign(assign, member); } // Interface receivers write through the set# vtable slot. let objT = this.#substitutedNodeType(member.object); if (objT != null && this.#resolveSelfType(objT) is InterfaceType) { if (assign.operator != null) { this.#bail('interface compound write'); } let it = this.#resolveSelfType(objT) as InterfaceType; let propName2 = accessKeyOf(this.func.model, member); let setterArgs = growable([assign.value]); // Setter dispatch returns no value; assignment-as-expression on // an interface member would need the value replayed — the // verifier's produces-no-value check trips loudly if used. return this.#lowerInterfaceDispatch( it, 'set#' + propName2, member.object, setterArgs); } let receiver = this.#lowerExpression(member.object); let receiverCt = this.#receiverClass(receiver, member); let target = this.#resolveMemberTarget(member, receiverCt); let {fieldName} = target; if (fieldName == null) { return this.#lowerSetterWrite(assign, member, receiver, receiverCt, target.getter as WasmFunction, target.vtSlot != null); } // Setter-side override checks (the read-side resolve checked get#): // neither a redefined setter between receiver and declarer nor a // transitive-subclass setter may exist for a direct struct.set. let propName = accessKeyOf(this.func.model, member); let isPrivateWrite = propName.length > 0 && propName.sliceBytes(0, 1) == "#"; var infoOpt: ClassCodegenInfo | null = null; if (let (true, info) = this.wasm.classInfos.get(getClassKey(receiverCt))) { infoOpt = info; } // A synthesized default setter on a subclass targets this same // physical field, so it is not an override — see // accessorOverriddenBySubclass. Safe to use the field-name form // here because this branch already established a physical field. if (!isPrivateWrite && (this.#accessorRedefined(receiverCt, propName, 'set#' + propName) || (infoOpt != null && this.#cx().accessorOverriddenBySubclass( infoOpt, 'set#' + propName, fieldName)))) { // An accessor shadows the physical field somewhere between the // receiver and a subclass: the write must dispatch. return this.#lowerVirtualSetterWrite( assign, member, receiver, receiverCt, propName); } if (!isValSubtype(this.#valueType(receiver), new ValTypeRefNull(target.owner) as ValType)) { this.#bail('member receiver struct mismatch'); } let fieldVt = this.#fieldValType(target.owner, fieldName); var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, fieldVt); } else { let fieldRefRead = this.b.addRef(new IrRefField(target.owner, fieldName)); let readVt = this.#exprValType(member); let current = this.b.emit(IrOp.struct_get, readVt, receiver, fieldRefRead); value = this.#combineCompound(current, assign, member as Expression); } // Conform, not just check: a non-null field takes narrowed and // `as`-cast nullable values whose re-assert the consumers own // (lowerAs passes same-target values through raw). value = this.#conformToSlot(value, fieldVt, 'member write type'); let fieldRef = this.b.addRef(new IrRefField(target.owner, fieldName)); // A store into an owner field releases the value it replaces // (ownership.md §Affine fields): the new value is computed first, // so a throwing right-hand side leaves the field intact, then the // old value is read and disposed — null-guarded for a nullable // field — and the store lands. The checker rooted the dispose. A // store into a field a dispose moved out replaces nothing. if (assign.operator == null && receiverCt.isResource && receiverCt.members.has(propName) && !this.func.model.isRevivingStore(assign as Node)) { let fieldMember = receiverCt.members[propName]; if (fieldMember is FieldInfo) { let fieldT = fieldMember.type; let coreT = soleNonNullMember(fieldT) ?? fieldT; if (ownershipHandleOf(coreT) == OwnershipHandle.Own) { let old = this.b.emit(IrOp.struct_get, fieldVt, receiver, fieldRef); let owners = disposeOwnerClasses(this.#cx(), fieldT); emitMaybeDispose(this.#cx(), old, owners); } } } // A store into a `scoped T` field does with the value it replaces // what its type at this specialization says. if (assign.operator == null && this.func.model.isScopedFieldStore(assign as Node)) { let storedT = this.#substitutedNodeType(member); if (storedT != null) { emitScopedFieldReplace(this.#cx(), storedT, receiver, fieldRef, fieldVt); } } let pairIdx = this.b.pushExtra(this.#twoValues(receiver, value)); this.b.emitVoid(IrOp.struct_set, pairIdx, fieldRef); return value; } #fieldValType(owner: WasmStruct, name: String): ValType { return this.#cx().fieldValType(owner, name); } /** * Writes through an accessor: a direct call to a non-overridden * setter. Private setters resolve lexically (never virtual); public * setters walk the receiver's super chain (through generic sources), * bailing when any transitive subclass overrides the slot — the same * devirt-safety rule as method calls. Compound assignments read the * current value through the already-resolved getter, so the receiver * evaluates once. */ #lowerSetterWrite(assign: AssignmentExpression, member: MemberExpression, receiver: i32, receiverCt: ClassType, getter: WasmFunction, getterVirtual: boolean): i32 { let propName = accessKeyOf(this.func.model, member); let isPrivate = propName.length > 0 && propName.sliceBytes(0, 1) == "#"; var setterVirtual = false; var vtInfoOpt: ClassCodegenInfo | null = null; var setter: WasmFunction | null = null; if (isPrivate) { if (this.func.classType == null) { this.#bail('private access outside class'); } let lexicalCt = this.#lexicalClassOf(member, receiverCt); setter = this.#resolvePrivateAccessor( receiverCt, lexicalCt, 'set#' + propName); } else { var currClass: ClassType | null = receiverCt; while (currClass != null && setter == null) { var g: ClassType | null = currClass; while (g != null && setter == null) { let key = getClassMethodKey( getClassKey(g), 'set#' + propName); if (let (true, f) = this.wasm.classMethodMap.get(key)) { setter = f; } g = g.genericSource; } currClass = currClass.superType; } if (let (true, info) = this.wasm.classInfos.get(getClassKey(receiverCt))) { vtInfoOpt = info; if (this.#cx().memberProvidedBySubclass(info, 'set#' + propName)) { // A subclass overrides the setter: the write must dispatch. if (this.#vtableSlotIndex(info, 'set#' + propName) < 0) { this.#bail('overridden setter without vtable slot'); } setterVirtual = true; } } } if (setter == null) { this.#bail('setter not found'); } if (setter.isStatic || (setter.typeParameters != null && setter.typeArguments == null) || setter.intrinsic != null) { this.#bail('setter shape'); } let sig = setter.signature; if (sig.params.length != 2 || sig.returns.length != 0) { this.#bail('setter shape'); } let recv = this.#conformReceiver(receiver, sig.params[0], 'setter receiver type'); let valueVt = sig.params[1]; this.#requireSupported(valueVt, 'setter param type'); var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, valueVt); } else { let gsig = getter.signature; if (gsig.returns.length != 1) { this.#bail('getter shape'); } var current = if (getterVirtual) { this.#lowerVtableCall(recv, vtInfoOpt as ClassCodegenInfo, 'get#' + propName, gsig, this.#oneArg(recv), gsig.returns[0]) } else { this.b.callFunc(this.b.addRef(new IrRefFunc(getter)), this.#oneArg(recv), gsig.returns[0]) }; current = this.#reassertNonNull(current, this.#exprValType(member)); value = this.#combineCompound(current, assign, member as Expression); } if (!isValSubtype(this.#valueType(value), valueVt)) { this.#failConformance(value, valueVt, 'setter value type'); } let args = growable([recv, value]); if (setterVirtual) { this.#lowerVtableCall(recv, vtInfoOpt as ClassCodegenInfo, 'set#' + propName, sig, args, null); } else { this.b.callFunc(this.b.addRef(new IrRefFunc(setter)), args, null); } return value; } /** * A write whose physical field is shadowed by an accessor somewhere * between the receiver and a subclass: resolve the nearest default * setter for the slot signature and dispatch through the vtable. * Compound assignments read the current value through the get# slot. */ #lowerVirtualSetterWrite(assign: AssignmentExpression, member: MemberExpression, receiver: i32, receiverCt: ClassType, propName: String): i32 { var infoOpt: ClassCodegenInfo | null = null; if (let (true, info) = this.wasm.classInfos.get(getClassKey(receiverCt))) { infoOpt = info; } if (infoOpt == null) { this.#bail('method receiver not discovered'); } let info = infoOpt; if (this.#vtableSlotIndex(info, 'set#' + propName) < 0) { this.#bail('overridden field without setter slot'); } var setter: WasmFunction | null = null; var currClass: ClassType | null = receiverCt; while (currClass != null && setter == null) { var g: ClassType | null = currClass; while (g != null && setter == null) { let key = getClassMethodKey( getClassKey(g), 'set#' + propName); if (let (true, f) = this.wasm.classMethodMap.get(key)) { setter = f; } g = g.genericSource; } currClass = currClass.superType; } if (setter == null) { this.#bail('overridden field without setter slot'); } let sig = setter.signature; if (sig.params.length != 2 || sig.returns.length != 0) { this.#bail('setter shape'); } let valueVt = sig.params[1]; this.#requireSupported(valueVt, 'setter param type'); var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, valueVt); } else { if (this.#vtableSlotIndex(info, 'get#' + propName) < 0) { this.#bail('overridden field without getter slot'); } var getterOpt: WasmFunction | null = null; var gc: ClassType | null = receiverCt; while (gc != null && getterOpt == null) { let key = getClassMethodKey( getClassKey(gc), 'get#' + propName); if (let (true, g) = this.wasm.classMethodMap.get(key)) { getterOpt = g; } gc = gc.superType; } if (getterOpt == null) { this.#bail('overridden field without getter slot'); } let gsig = getterOpt.signature; if (gsig.returns.length != 1) { this.#bail('getter shape'); } let grecv = this.#conformReceiver(receiver, gsig.params[0], 'setter receiver type'); var current = this.#lowerVtableCall(grecv, info, 'get#' + propName, gsig, this.#oneArg(grecv), gsig.returns[0]); current = this.#reassertNonNull(current, this.#exprValType(member)); value = this.#combineCompound(current, assign, member as Expression); } if (!isValSubtype(this.#valueType(value), valueVt)) { this.#failConformance(value, valueVt, 'setter value type'); } let recv = this.#conformReceiver(receiver, sig.params[0], 'setter receiver type'); let args = growable([recv, value]); this.#lowerVtableCall(recv, info, 'set#' + propName, sig, args, null); return value; } /** * Member reads on class-typed receivers: a direct struct.get when the * property is a physical field on the receiver's struct and no * subclass overrides its accessor; otherwise a direct call to a * non-overridden getter. Interfaces, statics, optional chains, and * anything needing dynamic dispatch bail. */ #lowerMemberRead(member: MemberExpression): i32 { if (hasOptionalChainStep(member as Expression)) { return this.#lowerOptionalChainExpr(member as Expression); } // A field of a union of inline records, read from its slot: off a // binding that holds the slots, or a call that returns them. let heldUnion = this.#heldInline(member.object); if (heldUnion != null) { let unionLayout = heldUnion.layout; if (unionLayout != null && unionLayout.records != null) { return this.#inlineRecordUnionField(heldUnion, unionLayout, member); } } if (member.object is CallExpression) { let objectType = this.#substitutedNodeType(member.object); if (objectType != null && inlineRecordUnionMembers(objectType) != null) { let source = this.#lowerMultiSource(member.object); let unionLayout = source.layout; if (unionLayout != null && unionLayout.records != null) { return this.#inlineRecordUnionField(source, unionLayout, member); } } } // Field defaults evaluate before the object exists // (non-nullable-refs.md): `this.f` inside one resolves to the // level's already-computed value — an earlier field's value, or // the type's default for a field not yet initialized, exactly // what the old allocate-then-mutate shell held at this point. if (member.object is ThisExpression) { let lf = this.#ctorLevelFields; if (lf != null) { return this.#ctorLevelFieldRead(lf, member); } } if (this.#isZeroWidthExpr(member as Expression)) { // A zero-width field (`Box.value`) has no storage to read. // The receiver still has to be evaluated for its effects; the // read itself yields nothing. this.#lowerExpression(member.object); return NO_VALUE; } let objectType = this.#substitutedNodeType(member.object); // An enum member read off the enum's name lowers to a constant: // numeric members inline their (implicit or literal) value, // string-enum members their string literal. The name itself is // never lowered. if (objectType != null && baseType(objectType) is EnumType) { return this.#lowerEnumMember( (baseType(objectType) as EnumType).declaration, member); } if (objectType != null && unwrapAliases(objectType) is RecordType) { return this.#lowerRecordRead(unwrapAliases(objectType) as RecordType, member); } if (objectType != null && unwrapAliases(objectType) is UnionType) { let members = recordUnionMembers(unwrapAliases(objectType) as UnionType); if (members != null) { return this.#lowerRecordUnionRead(members, member); } } if (objectType != null && this.#resolveSelfType(objectType) is InterfaceType) { if (this.func.model.isStaticAccess(member)) { this.#bail('static interface access'); } let it = this.#resolveSelfType(objectType) as InterfaceType; let propName = accessKeyOf(this.func.model, member); let result = this.#lowerInterfaceDispatch( it, 'get#' + propName, member.object, null); return this.#reassertNonNull(result, this.#exprValType(member)); } if (objectType != null && this.func.model.isStaticAccess(member)) { let selfR = this.#resolveSelfType(objectType); if (selfR is ClassType) { return this.#lowerStaticMemberRead( member, eraseTypeParameters(this.wasm, selfR) as ClassType); } } if (member.object is SuperExpression) { return this.#lowerSuperRead(member); } let receiver = this.#lowerExpression(member.object); return this.#lowerMemberReadOn(member, receiver); } /** * The superclass of the enclosing method's class: the resolution * root for every `super` access. Super dispatch is static — it * names the superclass's implementation directly and never goes * through the vtable, which would loop back to the override. */ #superClassOfThis(): ClassType { if (this.func.classType == null) { this.#bail('super outside instance method'); } let ct = this.func.declaringType ?? (this.func.classType as ClassType); if (ct.superType == null) { this.#bail('super without superclass'); } return ct.superType as ClassType; } /** * `super.prop`: a non-static getter registered on the superclass * chain wins (called directly); otherwise the property is a plain * field read against the superclass struct — `this` is a subtype of * it, so the read needs no cast. */ #lowerSuperRead(member: MemberExpression): i32 { let superCt = this.#superClassOfThis(); let receiver = this.#lowerThis(member as Expression); let propName = accessKeyOf(this.func.model, member); var getter: WasmFunction | null = null; var curr: ClassType | null = superCt; while (curr != null && getter == null) { var g: ClassType | null = curr; while (g != null && getter == null) { let key = getClassMethodKey( getClassKey(g), 'get#' + propName); if (let (true, f) = this.wasm.classMethodMap.get(key)) { if (!f.isStatic) { getter = f; } } g = g.genericSource; } curr = curr.superType; } if (getter != null) { let sig = getter.signature; if (sig.params.length != 1 || sig.returns.length != 1) { this.#bail('super getter shape'); } let recv = this.#conformReceiver(receiver, sig.params[0], 'super receiver type'); this.#requireSupported(sig.returns[0], 'super getter type'); let result = this.b.callFunc( this.b.addRef(new IrRefFunc(getter)), this.#oneArg(recv), sig.returns[0]); return this.#reassertNonNull(result, this.#exprValType(member)); } let cKey = getClassKey(superCt); let infoOpt = if (let (true, info) = this.wasm.classInfos.get(cKey)) info else null; if (infoOpt == null) { this.#bail('super class not discovered'); } let st = infoOpt.structType; if (st.fieldIndexByName(propName) < 0) { this.#bail('super member not found'); } let fieldRef = this.b.addRef(new IrRefField(st, propName)); let read = this.b.emit(IrOp.struct_get, this.#fieldValType(st, propName), receiver, fieldRef); return this.#reassertNonNull(read, this.#exprValType(member)); } /** * `super.prop = v`: a non-static setter on the superclass chain wins * (called directly, never through the vtable); otherwise the write * is a plain struct.set against the superclass struct. Compound * assignments read the current value through the super read path. */ /** * `recv.field ??= rhs`: read the field once; keep it when non-null, * else evaluate rhs (short-circuit), store it, and yield it — * physical fields only, matching streaming's member nullish write. */ #lowerMemberNullishAssign(assign: AssignmentExpression, member: MemberExpression): i32 { let receiver = this.#lowerExpression(member.object); let receiverCt = this.#receiverClass(receiver, member); let target = this.#resolveMemberTarget(member, receiverCt); if (target.fieldName == null || target.vtSlot != null) { this.#bail('nullish member assign target'); } let fieldName = target.fieldName as String; let fieldVt = this.#fieldValType(target.owner, fieldName); if (!(fieldVt is ValTypeRefNull)) { this.#bail('nullish member assign type'); } let readRef = this.b.addRef(new IrRefField(target.owner, fieldName)); let current = this.b.emit(IrOp.struct_get, fieldVt, receiver, readRef); let resultVt = this.#exprValType(assign as Expression); let isNull = this.b.emit(IrOp.ref_is_null, new ValTypeI32(), current, -1); let writeB = this.b.newBlock(); let keepB = this.b.newBlock(); let merge = this.b.newBlock(); let result = this.b.blockParam(merge, resultVt); this.b.brIf(isNull, writeB, this.#noArgs(), keepB, this.#noArgs()); // The keep-path conform narrows nullable -> non-null, so it MUST // sit in its own arm: emitted before the branch it would trap on // exactly the null values the write arm exists to handle. this.b.switchTo(keepB); let keep = this.#conformToSlot(current, resultVt, 'nullish member value'); this.b.br(merge, this.#oneArg(keep)); this.b.switchTo(writeB); var rhs = this.#lowerExpressionCtx(assign.value, fieldVt); rhs = this.#conformToSlot(rhs, fieldVt, 'member write type'); let writeRef = this.b.addRef(new IrRefField(target.owner, fieldName)); let pairIdx = this.b.pushExtra(this.#twoValues(receiver, rhs)); this.b.emitVoid(IrOp.struct_set, pairIdx, writeRef); let stored = this.#conformToSlot(rhs, resultVt, 'nullish member value'); this.b.br(merge, this.#oneArg(stored)); this.b.switchTo(merge); return result; } #lowerSuperWrite(assign: AssignmentExpression, member: MemberExpression): i32 { let superCt = this.#superClassOfThis(); let receiver = this.#lowerThis(member as Expression); let propName = accessKeyOf(this.func.model, member); var setter: WasmFunction | null = null; var curr: ClassType | null = superCt; while (curr != null && setter == null) { var g: ClassType | null = curr; while (g != null && setter == null) { let key = getClassMethodKey( getClassKey(g), 'set#' + propName); if (let (true, f) = this.wasm.classMethodMap.get(key)) { if (!f.isStatic) { setter = f; } } g = g.genericSource; } curr = curr.superType; } var targetVt: ValType = new ValTypeI32(); var fieldStruct: WasmStruct | null = null; if (setter != null) { let sig = setter.signature; if (sig.params.length != 2 || sig.returns.length != 0) { this.#bail('super setter shape'); } targetVt = sig.params[1]; } else { let infoOpt = if (let (true, info) = this.wasm.classInfos.get(getClassKey(superCt))) info else null; if (infoOpt == null) { this.#bail('super class not discovered'); } let st = infoOpt.structType; if (st.fieldIndexByName(propName) < 0) { this.#bail('super member not found'); } fieldStruct = st; targetVt = this.#fieldValType(st, propName); } this.#requireSupported(targetVt, 'super write type'); var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, targetVt); } else { let current = this.#lowerSuperRead(member); value = this.#combineCompound(current, assign, member as Expression); } value = this.#conformToSlot(value, targetVt, 'super write type'); if (setter != null) { let recv = this.#conformReceiver(receiver, setter.signature.params[0], 'super receiver type'); this.b.callFunc(this.b.addRef(new IrRefFunc(setter)), this.#twoValues(recv, value), null); return value; } let st = fieldStruct as WasmStruct; let fieldRef = this.b.addRef(new IrRefField(st, propName)); let pairIdx = this.b.pushExtra(this.#twoValues(receiver, value)); this.b.emitVoid(IrOp.struct_set, pairIdx, fieldRef); return value; } /** * Resolves the `classMethodMap` and vtable slot name for `call` on * `classType`: appends the signature key when the checker selected an * overload, or the specialization suffix when `propName` is a generic * method in `classType.members` (which looks up both own and inherited * members via `MemberTable`). */ #resolveClassMethodSlotName( classType: ClassType, propName: String, call: CallExpression, isStatic: boolean = false): String { let chosenOverload = this.func.model.getResolvedOverload(call); var slotName = if (chosenOverload != null) { propName + getSignatureKey(chosenOverload) } else { propName }; if (let (true, memberInfo) = classType.memberTable(isStatic).get(propName)) { let memberType = memberInfo.type; if (memberType is FunctionType) { let {typeParameters} = memberType; if (typeParameters != null && typeParameters.length > 0) { let typeArguments = this.#solvedCallTypeArguments(call); if (typeArguments == null) { throw new Error( `Internal error: missing solved type arguments for generic method call ${classType.name}.${propName}`); } slotName = getSpecializedName(propName, typeArguments); } } } return slotName; } /** * `super.m(...)`: resolve the method on the superclass chain * (overload slots via the checker's recorded selection, shared * generic templates via the genericSource walk) and call it * directly with `this` as the receiver. */ #lowerSuperMethodCall(call: CallExpression, member: MemberExpression, multiOut: GrowableArray | null): i32 { let superCt = this.#superClassOfThis(); let receiver = this.#lowerThis(member.object); let propName = accessKeyOf(this.func.model, member); if (propName.length > 0 && propName.sliceBytes(0, 1) == '#') { this.#bail('super private call'); } let slotName = this.#resolveClassMethodSlotName(superCt, propName, call); var method: WasmFunction | null = null; var currClass: ClassType | null = superCt; while (currClass != null && method == null) { var g: ClassType | null = currClass; while (g != null && method == null) { let key = getClassMethodKey(getClassKey(g), slotName); if (let (true, m) = this.wasm.classMethodMap.get(key)) { method = m; } g = g.genericSource; } currClass = currClass.superType; } if (method == null) { this.#bail('super method not found'); } if (method.isStatic || (method.typeParameters != null && method.typeArguments == null) || method.intrinsic != null) { this.#bail('super method shape'); } let sig = method.signature; if (sig.returns.length > 1) { this.#bail('super multi-value call'); } if (multiOut != null) { this.#bail('super call in multi-value position'); } let inlineParams = this.#inlineLayoutsOf(method); if (sig.params.length != this.#liveArgWidth(call.args, inlineParams) + 1) { this.#bail('super method arity'); } let args = growable([ this.#conformReceiver(receiver, sig.params[0], 'super receiver type'), ]); var sIdx = 1; for (var i = 0; i < call.args.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(call.args[i], inlineLayout, args); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(call.args[i])) { continue; } let paramVt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(paramVt, 'super param type'); var value = this.#lowerExpressionCtx(call.args[i], paramVt); value = this.#conformToSlot(value, paramVt, 'super argument type'); args.push(value); } let resultType = if (sig.returns.length == 1) sig.returns[0] else null; if (resultType != null) { this.#requireSupported(resultType, 'super result type'); } let result = this.b.callFunc( this.b.addRef(new IrRefFunc(method)), args, resultType); if (resultType != null) { let nodeType = this.#refinableCallType(call); if (nodeType != null) { let want = typeToValTypeWithSub(this.wasm, nodeType, this.func); if (want is ValTypeRef && resultType is ValTypeRefNull) { return this.b.emit(IrOp.ref_as_non_null, want, result, -1); } if (want is ValTypeRefExtern && resultType is ValTypeExternRef) { return this.b.emit(IrOp.ref_as_non_null, want, result, -1); } } } return result; } /** * Static member writes: the field's module global, with compound * assignments reading the same global first. Static setters bail. */ #lowerStaticMemberWrite(assign: AssignmentExpression, member: MemberExpression): i32 { let objectType = this.#substitutedNodeType(member.object); if (objectType == null) { this.#bail('untyped static receiver'); } let selfR = this.#resolveSelfType(objectType); if (!(selfR is ClassType)) { this.#bail('static receiver kind'); } let ct = eraseTypeParameters(this.wasm, selfR) as ClassType; let propName = accessKeyOf(this.func.model, member); var setter: WasmFunction | null = null; var currClass: ClassType | null = ct; while (currClass != null && setter == null) { let key = getClassMethodKey( getClassKey(currClass), 'set#' + propName); if (let (true, f) = this.wasm.staticMethodMap.get(key)) { if (f.isStatic) { setter = f; } } currClass = currClass.superType; } if (setter != null) { let sig = setter.signature; if (sig.params.length != 1 || sig.returns.length != 0) { this.#bail('static setter shape'); } var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, sig.params[0]); } else { let current = this.#lowerStaticMemberRead(member, ct); value = this.#combineCompound(current, assign, member as Expression); } this.b.callFunc(this.b.addRef(new IrRefFunc(setter)), this.#oneArg(value), null); return value; } let g = this.#staticFieldGlobal(ct, propName); if (g == null) { this.#bail('static member not found'); } var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, g.type); } else { let current = this.b.emit(IrOp.global_get, g.type, -1, this.b.addRef(new IrRefGlobal(g))); value = this.#combineCompound(current, assign, member as Expression); } if (!isValSubtype(this.#valueType(value), g.type)) { this.#failConformance(value, g.type, 'global write type'); } this.b.emitVoid(IrOp.global_set, value, this.b.addRef(new IrRefGlobal(g))); return value; } /** * Static member reads: a static getter registered in the class * hierarchy wins; otherwise the field's module global (named * '_'), cast to the member's checked type when the * global's declared type is wider — streaming's shape. */ #lowerStaticMemberRead(member: MemberExpression, ct: ClassType): i32 { let propName = accessKeyOf(this.func.model, member); var getter: WasmFunction | null = null; var currClass: ClassType | null = ct; while (currClass != null && getter == null) { let key = getClassMethodKey( getClassKey(currClass), 'get#' + propName); if (let (true, f) = this.wasm.staticMethodMap.get(key)) { if (f.isStatic) { getter = f; } } currClass = currClass.superType; } if (getter != null) { let sig = getter.signature; if (sig.params.length != 0 || sig.returns.length != 1) { this.#bail('static getter shape'); } let call = this.b.callFunc(this.b.addRef(new IrRefFunc(getter)), this.#noArgs(), sig.returns[0]); return this.#reassertNonNull(call, this.#exprValType(member)); } let g = this.#staticFieldGlobal(ct, propName); if (g == null) { this.#bail('static member not found'); } let raw = this.b.emit(IrOp.global_get, g.type, -1, this.b.addRef(new IrRefGlobal(g))); let want = this.#exprValType(member); if (valTypeEquals(g.type, want)) { return this.#reassertNonNull(raw, want); } if (isRefValType(want) && isRefValType(g.type)) { if (isValSubtype(g.type, want)) { return this.#reassertNonNull(raw, want); } return this.b.emit(IrOp.ref_cast, want, raw, this.b.internType(want)); } return this.#bailValue('static member type'); } /** The module global backing a static field, walking the supers. */ #staticFieldGlobal(ct: ClassType, propName: String): WasmGlobal | null { var currClass: ClassType | null = ct; while (currClass != null) { let wanted = `${currClass.name}_${propName}`; for (let g in this.wasm.globals) { if (g.name == wanted) { return g; } } currClass = currClass.superType; } return null; } /** * A member read whose receiver has no struct behind it: the value is * a wasm array or a bare primitive, and an extension class declared * `on` that type owns the member. Extensions have no fields and no * vtable, so the target is always a getter, called directly with the * underlying value as its only argument — which is also why this * costs nothing beyond the call: there is no wrapper to unpack. * * Returns -1 when the static type is not an extension at all, so the * caller can report the failure in its own terms. */ #lowerExtensionMemberOn(member: MemberExpression, receiver: i32, what: String): i32 { let objectType = this.#substitutedNodeType(member.object); var extCt: ClassType | null = null; if (objectType != null) { let nonNull = if (member.optional) getNonNullableType(objectType) else objectType; let selfR = this.#resolveSelfType(nonNull); if (selfR is ClassType && selfR.isExtension) { extCt = eraseTypeParameters(this.wasm, selfR) as ClassType; } } if (extCt == null) { return -1; } let extTarget = this.#resolveMemberTarget(member, extCt); if (extTarget.fieldName != null || extTarget.vtSlot != null || extTarget.getter == null) { this.#bail(what); } let extGetter = extTarget.getter as WasmFunction; let egSig = extGetter.signature; if (egSig.params.length != 1 || egSig.returns.length != 1) { this.#bail('getter shape'); } let recv = this.#conformReceiver(receiver, egSig.params[0], 'member receiver type'); let extCall = this.b.callFunc(this.b.addRef(new IrRefFunc(extGetter)), this.#oneArg(recv), egSig.returns[0]); return this.#reassertNonNull(extCall, this.#exprValType(member)); } /** * The receiver-value core of a member read: array .length, then * field / non-overridden getter resolution against the receiver's * class. Shared by the plain path and the optional-chain hit arm. */ #lowerMemberReadOn(member: MemberExpression, receiver: i32): i32 { // .length on wasm-array receivers: array.len (mirrors the // FixedArray length intrinsic in the streaming backend). let recvArr = asWasmArray(this.#valueType(receiver)); if (recvArr != null) { let arrProp = accessKeyOf(this.func.model, member); if (arrProp == "length") { return this.b.emit(IrOp.array_len, new ValTypeI32(), receiver, -1); } // An extension class on the array type owns other members. let arrExt = this.#lowerExtensionMemberOn(member, receiver, 'array member'); if (arrExt == -1) { this.#bail('array member'); } return arrExt; } let earlyVt = this.#valueType(receiver); if (!(earlyVt is ValTypeRef) && !(earlyVt is ValTypeRefNull)) { // A receiver with no struct behind it — a `v128` under a shaped // view, say. Only an extension class declared `on` that type can // own a member here; nothing else has a member to read. let primExt = this.#lowerExtensionMemberOn( member, receiver, 'member receiver kind'); if (primExt != -1) { return primExt; } this.#bail('member receiver kind'); } let receiverCt = this.#receiverClass(receiver, member); let target = this.#resolveMemberTarget(member, receiverCt); let receiverVt = this.#valueType(receiver); // The receiver value must actually be (a subtype of) the struct the // member resolved against — mismatched specialization keys (e.g. // intrinsic array classes) would otherwise emit invalid struct.gets. // Extension receivers are exempt: their getters take the onType // value, checked at the call below. if (!receiverCt.isExtension && !isValSubtype(receiverVt, new ValTypeRefNull(target.owner) as ValType)) { this.#bail('member receiver struct mismatch'); } let {vtSlot} = target; if (vtSlot != null) { let vgetter = target.getter as WasmFunction; let vsig = vgetter.signature; if (vsig.returns.length != 1) { this.#bail('getter shape'); } let recv = this.#conformReceiver(receiver, vsig.params[0], 'member receiver type'); let vresult = this.#lowerVtableCall(recv, target.vtInfo as ClassCodegenInfo, vtSlot, vsig, this.#oneArg(recv), vsig.returns[0]); return this.#reassertNonNull(vresult, this.#exprValType(member)); } if (target.fieldName != null) { let fieldName = target.fieldName as String; let fieldRef = this.b.addRef(new IrRefField(target.owner, fieldName)); // The struct.get result carries the field's STORAGE type; the // member's checked type may be narrower (non-null). let fieldVt = this.#fieldValType(target.owner, fieldName); let read = this.b.emit(IrOp.struct_get, fieldVt, receiver, fieldRef); if (this.func.model.isFieldTake(member as Node) && this.#takesHere(member)) { // A take: the owner moves out and the field is left null, which // the release glue skips (ownership.md §Affine fields). let nullValue = this.b.emit(IrOp.ref_null, fieldVt, -1, -1); let pairIdx = this.b.pushExtra(this.#twoValues(receiver, nullValue)); this.b.emitVoid(IrOp.struct_set, pairIdx, fieldRef); } return this.#reassertNonNull(read, this.#exprValType(member)); } let getter = target.getter as WasmFunction; let sig = getter.signature; if (sig.returns.length != 1) { this.#bail('getter shape'); } let recv = this.#conformReceiver(receiver, sig.params[0], 'member receiver type'); let getterRef = this.b.addRef(new IrRefFunc(getter)); let call = this.b.callFunc(getterRef, this.#oneArg(recv), sig.returns[0]); // Getter signatures return the field's (nullable) storage type; the // member's checked type may be non-null. return this.#reassertNonNull(call, this.#exprValType(member)); } /** * Whether a field take moves the value out at this specialization. An * owner field's take always does. A `scoped T` field's does only where * `T` may be restricted, judged from its representation * (representsRestricted); at a first-class `T` the read is a copy and * the field keeps its value (owner-futures.md §"The class half of * `scoped T`"). */ #takesHere(member: MemberExpression): boolean { let t = this.#substitutedNodeType(member); return t == null || representsRestricted(t as Type); } /** * A ref-typed value with the null stripped from its type: the raw * value when already non-null, else a re-asserted copy. */ #strippedNonNull(value: i32): i32 { return this.#cx().stripNonNull(value); } #lowerChainReceiver(obj: Expression): i32 { return this.#cx().chainReceiver(obj); } /** * The hit arm of an optional-chain access: the access itself, * performed on the already-null-checked receiver value. */ #lowerOptionalHit(expr: Expression, receiver: i32): i32 { if (expr is MemberExpression) { let objectType = this.#substitutedNodeType(expr.object); if (objectType != null) { let nonNull = unwrapAliases(getNonNullableType(objectType)); if (nonNull is RecordType) { return this.#lowerRecordReadOn(nonNull, expr, receiver); } if (nonNull is UnionType) { let members = recordUnionMembers(nonNull); if (members != null) { return this.#lowerRecordUnionReadOn(members, expr, receiver); } } let selfResolved = eraseTypeParameters(this.wasm, this.#resolveSelfType(nonNull)); if (selfResolved is InterfaceType) { if (this.func.model.isStaticAccess(expr)) { this.#bail('static interface access'); } let propName = accessKeyOf(this.func.model, expr); let result = this.#lowerInterfaceDispatch( selfResolved, 'get#' + propName, expr.object, null, null, receiver); return this.#reassertNonNull(result, this.#exprValType(expr)); } } return this.#lowerMemberReadOn(expr, receiver); } if (expr is IndexExpression) { let objectType = this.#substitutedNodeType(expr.object); if (objectType == null) { this.#bail('untyped index receiver'); } let selfResolved = this.#resolveSelfType( getNonNullableType(objectType)); let erased = eraseTypeParameters(this.wasm, selfResolved); if (erased is InterfaceType) { let args = growable([expr.index]); let slotName = interfaceCallSlotName(this.wasm, erased, '[]', this.func.model.getResolvedOverload(expr)); let result = this.#lowerInterfaceDispatch( erased, slotName, expr.object, args, null, receiver); return this.#reassertNonNull(result, this.#exprValType(expr)); } return this.#lowerIndexReadOn(expr, selfResolved, receiver); } let call = expr as CallExpression; let callResult = if (isMethodCallStep(call)) { this.#lowerMethodCallOn(call, call.callee as MemberExpression, receiver, null) } else { this.#lowerClosureCallOn(call, receiver) }; let calleeT = this.#substitutedNodeType(call.callee); if (calleeT != null) { let baseCallee = unwrapAliases(getNonNullableType(calleeT)); if (baseCallee is FunctionType && unwrapAliases(baseCallee.returnType) is NeverType && this.#cx().reachable) { this.b.unreachable(); this.#cx().reachable = false; return NO_VALUE; } } return callResult; } /** * Splits an unparenthesized postfix chain `expr` into `(baseExpr, steps)`, * where `steps[0]` is the innermost optional step (`?.`, `?[`, `?(`) and * `steps[steps.length - 1]` is `expr`. Any non-optional prefix inward of * `steps[0]` (such as `getRoot()` in `getRoot()?.a.b`) stays in `baseExpr`. */ #collectOptionalChain(expr: Expression): (Expression, GrowableArray) { let rev = new Array(); var cur = expr; while (true) { let next = postfixStepTarget(cur); if (next == null) { break; } rev.push(cur); if (!samePostfixChain(cur, next)) { break; } cur = next; } var firstOptRev = 0; for (var i = rev.length - 1; i >= 0; i -= 1) { if (isOptionalPostfixStep(rev[i])) { firstOptRev = i; break; } } let baseExpr = postfixStepTarget(rev[firstOptRev]) as Expression; let steps = new Array(); for (var i = firstOptRev; i >= 0; i -= 1) { steps.push(rev[i]); } return (baseExpr, steps); } /** * Evaluates `baseExpr` and runs `steps` in order, branching to a lazily * allocated `missB` block (carrying `missSyms`) whenever an optional step * encounters `null`. Returns `(finalValue, missB, missParams, savedEnv)`. */ #lowerOptionalChainSteps(baseExpr: Expression, steps: GrowableArray, missSyms: GrowableArray): (i32, i32, GrowableArray, HashMap) { var cur = this.#lowerChainReceiver(baseExpr); let saved = this.#copyEnv(); var missB = -1; var missParams = new Array(); if (!this.#cx().reachable) { return (NO_VALUE, -1, missParams, saved); } for (let step in steps) { if (isOptionalPostfixStep(step)) { let curVt = this.#valueType(cur); if (!isRefValType(curVt)) { this.#bail('optional receiver kind'); } if (isNullableAnyRef(curVt) || curVt is ValTypeExternRef) { if (missB < 0) { missB = this.b.newBlock(); missParams = this.#addParams(missB, missSyms); } let isNull = this.b.emit( IrOp.ref_is_null, new ValTypeI32() as ValType, cur, -1); let okB = this.b.newBlock(); this.b.brIf(isNull, missB, this.#envValues(missSyms), okB, this.#noArgs()); this.b.switchTo(okB); cur = this.#strippedNonNull(cur); } } cur = this.#lowerOptionalHit(step, cur); if (!this.#cx().reachable) { break; } } return (cur, missB, missParams, saved); } #lowerOptionalVoidChain(expr: Expression): i32 { let (baseExpr, steps) = this.#collectOptionalChain(expr); let mergeSyms = this.#assignedEnvSyms(expr); let (_, missB, missParams, saved) = this.#lowerOptionalChainSteps(baseExpr, steps, mergeSyms); if (missB < 0) { return NO_VALUE; } if (this.#cx().reachable) { this.b.br(missB, this.#envValues(mergeSyms)); } this.#cx().env = saved; this.b.switchTo(missB); this.#applyParams(mergeSyms, missParams); this.#cx().reachable = true; return NO_VALUE; } /** * A standalone optional-chain expression (`obj?.prop.sub`, `arr?[i].x`, * `fn?()`, `obj?.method()`): each optional hop tests for `null` and * short-circuits the rest of the unparenthesized postfix chain to `null` * (or skips a `void` tail). Chains producing primitives are handled via * `hostOptionalChainCoalesce` under `??`. */ #lowerOptionalChainExpr(expr: Expression): i32 { if (this.#isZeroWidthExpr(expr)) { return this.#lowerOptionalVoidChain(expr); } let (baseExpr, steps) = this.#collectOptionalChain(expr); let mergeSyms = this.#assignedEnvSyms(expr); let (value, missB, missParams, saved) = this.#lowerOptionalChainSteps(baseExpr, steps, mergeSyms); if (!this.#cx().reachable && missB < 0) { return NO_VALUE; } if (missB < 0) { return value; } var exprVt = this.#exprValType(expr); if (!isRefValType(exprVt)) { this.#bail('optional primitive result'); } exprVt = match (exprVt) { case ValTypeRef as r: new ValTypeRefNull(r.target) case ValTypeRefExtern: new ValTypeExternRef() case _: exprVt }; let mergeB = this.b.newBlock(); let mergeParams = this.#addParams(mergeB, mergeSyms); let resultParam = this.b.blockParam(mergeB, exprVt); if (this.#cx().reachable) { let conformed = this.#conformToSlot(value, exprVt, 'optional chain type'); let okArgs = this.#envValues(mergeSyms); okArgs.push(conformed); this.b.br(mergeB, okArgs); } this.#cx().env = this.#copyOf(saved); this.#cx().reachable = true; this.b.switchTo(missB); this.#applyParams(mergeSyms, missParams); let nullValue = this.#nullDefault(exprVt); let nullArgs = this.#envValues(mergeSyms); nullArgs.push(nullValue); this.b.br(mergeB, nullArgs); this.#cx().env = saved; this.b.switchTo(mergeB); this.#applyParams(mergeSyms, mergeParams); this.#cx().reachable = true; return resultParam; } hostOptionalChainCoalesce(bin: BinaryExpression, overallVt: ValType): i32 { let (baseExpr, steps) = this.#collectOptionalChain(bin.left); let leftSyms = this.#assignedEnvSyms(bin.left); let allSyms = this.#assignedEnvSyms(bin as Expression); let (value, missB, missParams, saved) = this.#lowerOptionalChainSteps(baseExpr, steps, leftSyms); if (!this.#cx().reachable && missB < 0) { return NO_VALUE; } if (missB < 0) { return this.#conformToSlot( this.hostCoalesceArmAdaptation(bin, value), overallVt, 'nullish arm type'); } let mergeB = this.b.newBlock(); let mergeParams = this.#addParams(mergeB, allSyms); let resultParam = this.b.blockParam(mergeB, overallVt); if (this.#cx().reachable) { let okValue = this.#conformToSlot( this.hostCoalesceArmAdaptation(bin, value), overallVt, 'nullish arm type'); let okArgs = this.#envValues(allSyms); okArgs.push(okValue); this.b.br(mergeB, okArgs); } this.#cx().env = this.#copyOf(saved); this.#cx().reachable = true; this.b.switchTo(missB); this.#applyParams(leftSyms, missParams); let rightValue = this.#lowerExpressionCtx(bin.right, overallVt); if (this.#cx().reachable) { let conformedRight = this.#conformToSlot(rightValue, overallVt, 'nullish arm type'); let rightArgs = this.#envValues(allSyms); rightArgs.push(conformedRight); this.b.br(mergeB, rightArgs); } this.#cx().env = saved; this.b.switchTo(mergeB); this.#applyParams(allSyms, mergeParams); this.#cx().reachable = true; return resultParam; } /** A null constant conforming to a (nullable) reference valtype. */ #nullDefault(vt: ValType): i32 { return this.b.emit(IrOp.ref_null, vt, -1, -1); } /** * Devirtualized method calls only: the receiver's class (or an * ancestor) provides the method, no transitive subclass overrides it, * the name resolves directly (overloads use mangled keys and miss), * and arity matches exactly. Everything else — statics, interfaces, * generic methods, optional calls, dynamic dispatch — bails. */ #isClosureMemberCallee(member: MemberExpression): boolean { if (member.optional) { return true; } let rawObj = this.#substitutedNodeType(member.object); if (rawObj == null) { return false; } let u = unwrapAliases(rawObj); if (u is RecordType) { return true; } if (u is UnionType && recordUnionMembers(u) != null) { return true; } let selfR = eraseTypeParameters(this.wasm, this.#resolveSelfType(rawObj)); if (selfR is ClassType) { let propName = accessKeyOf(this.func.model, member); if (let (true, info) = selfR.members.get(propName)) { return info is FieldInfo; } } return false; } #lowerMethodCall(call: CallExpression, member: MemberExpression, multiOut: GrowableArray | null = null): i32 { if (call.optional && !this.#isClosureMemberCallee(member)) { if (!hasOptionalChainStep(member as Expression)) { return this.#lowerMethodCallOn(call, member, -1, multiOut); } } if (hasOptionalChainStep(call as Expression)) { if (multiOut != null) { this.#bail('optional multi-value call'); } return this.#lowerOptionalChainExpr(call as Expression); } return this.#lowerMethodCallOn(call, member, -1, multiOut); } #lowerMethodCallOn(call: CallExpression, member: MemberExpression, givenReceiver: i32, multiOut: GrowableArray | null = null): i32 { if (this.func.model.isStaticAccess(member)) { return this.#lowerStaticCall(call, member, multiOut); } if (member.object is SuperExpression) { return this.#lowerSuperMethodCall(call, member, multiOut); } let rawObjectType = this.#substitutedNodeType(member.object); if (rawObjectType == null) { this.#bail('untyped method receiver'); } let objectType = if (member.optional) { getNonNullableType(rawObjectType) } else { rawObjectType }; // `hashCode()` on a record or tuple, through a `Hashable` bound: the // value's structural hash (ir/equality.zena), the one `hash` // gives. A record field of that name is an ordinary member instead. if (isValueHashCodeCall(call, member, objectType as Type, this.func.model)) { let recv = if (givenReceiver >= 0) givenReceiver else this.#lowerExpression(member.object); return lowerValueHash(this.#cx(), objectType as Type, recv); } // Record-typed receivers (including namespace imports, which are // record globals of the module's exports): the member is a field // holding a function value — read it, call the closure pair. if (unwrapAliases(objectType) is RecordType) { let rt = unwrapAliases(objectType) as RecordType; let recv = if (givenReceiver >= 0) givenReceiver else this.#lowerExpression(member.object); let fnValue = this.#lowerRecordReadOn(rt, member, recv); return this.#lowerClosureCallOn(call, fnValue); } if (unwrapAliases(objectType) is UnionType) { let members = recordUnionMembers(unwrapAliases(objectType) as UnionType); if (members != null) { let recv = if (givenReceiver >= 0) givenReceiver else this.#lowerExpression(member.object); let fnValue = this.#lowerRecordUnionReadOn(members, member, recv); return this.#lowerClosureCallOn(call, fnValue); } } // Erase before dispatching on the receiver's kind: a receiver typed // `Own`/`Borrow`/`Unmanaged` is a distinct-type alias that erases // to `R`, and member access forwards through it (ownership.md §Handles). let selfResolved = eraseTypeParameters(this.wasm, this.#resolveSelfType(objectType)); if (selfResolved is InterfaceType) { let propName = accessKeyOf(this.func.model, member); // A generic method dispatches through the slot RTA minted for // the solved type-argument list — the same specialized-name // mangle the class-side monomorphized copies register under. var slotName = propName; if (let (true, declInfo) = selfResolved.members.get(propName)) { if (declInfo.type is FunctionType) { let declFt = declInfo.type as FunctionType; let {typeParameters} = declFt; if (typeParameters != null && typeParameters.length > 0) { let targs = this.#solvedCallTypeArguments(call); if (targs == null) { this.#bail('generic interface method call'); } slotName = getSpecializedName(propName, targs); } else { // An overloaded method has a slot per overload. slotName = interfaceCallSlotName(this.wasm, selfResolved, propName, this.func.model.getResolvedOverload(call)); } } } let result = this.#lowerInterfaceDispatch( selfResolved, slotName, member.object, call.args, multiOut, givenReceiver); if (multiOut != null && multiOut.length > 1) { // Multi-value results have no single checked type to // re-assert against; the projections carry their slot types. return result; } return this.#reassertCallResult(call, result, this.b.body.resultType(result)); } // Extension methods on arrays (map/fold/indexOf/...): resolved // by element-type specialization key against wasm.arrayMethods, // mirroring the streaming backend. The callee may itself be // streaming-compiled (the synthesized map/fold bodies) — both // backends share one module, so cross-backend calls are plain // direct calls. var arrayElem: Type | null = null; var arrayElemsMutable = true; match (selfResolved) { case ArrayType as at: { arrayElem = at.elementType; arrayElemsMutable = at.elementsMutable; } case ClassType as arrCt: { if (arrCt.name == "array" && arrCt.typeArguments != null && (arrCt.typeArguments as GrowableArray).length > 0) { arrayElem = (arrCt.typeArguments as GrowableArray)[0]; } } case _: {} } if (arrayElem != null) { return this.#lowerArrayExtensionCall( call, member, arrayElem, arrayElemsMutable, givenReceiver); } if (!(selfResolved is ClassType)) { this.#bail('method receiver kind'); } // Resolve against the class the receiver VALUE carries: checked // node types name the GENERIC class inside specialized-method // bodies, but specialized methods register under their own key. var receiver = if (givenReceiver >= 0) givenReceiver else this.#lowerExpression(member.object); let checkedCt = eraseTypeParameters(this.wasm, selfResolved) as ClassType; var ct = checkedCt; let fromValue = this.#classTypeOfValue(receiver); if (fromValue != null && !ct.isExtension) { ct = fromValue; } var narrowedFromChecked = false; let cKey = getClassKey(ct); var classInfo = if (let (true, info) = this.wasm.classInfos.get(cKey)) info else null; if (classInfo == null) { this.#bail('method receiver not discovered'); } let propName = accessKeyOf(this.func.model, member); // One namespace, one lookup: the checker's member info names the // KIND. A field holding a function value is a member read plus a // closure call — decided here, before method resolution, never as // a fallback to a failed method lookup. if (let (true, kindInfo) = ct.members.get(propName)) { if (kindInfo is FieldInfo) { if (!(kindInfo.type is FunctionType)) { this.#bail('call of non-function field'); } // A field whose name shadows an inherited METHOD has no // settled semantics (streaming resolves the call to the base // method; the checker accepts the declaration silently — see // elematic/zena#107). Refuse to pick a side. var superWalk = ct.superType; while (superWalk != null) { if (let (true, superInfo) = superWalk.members.get(propName)) { if (superInfo is MethodInfo) { this.#bail('field shadows inherited method'); } } superWalk = superWalk.superType; } if (multiOut != null) { this.#bail('closure call in multi-value position'); } let calleeValue = this.#lowerMemberReadOn(member, receiver); return this.#lowerClosureCallOn(call, calleeValue); } } let isPrivate = propName.length > 0 && propName.sliceBytes(0, 1) == "#"; let slotClass = if (isPrivate) this.#lexicalClassOf(member, ct) else ct; let slotName = this.#resolveClassMethodSlotName(slotClass, propName, call); var method: WasmFunction | null = null; if (isPrivate) { // Mixin-declared bodies resolve their privates under the // mixin's scope key ("::#name", registered per host // class) and under nothing else — mixin privates are lexical to // the mixin declaration and never alias a host's same-named // private. The walk is over the receiver's chain because the // mixin may have been applied at an ancestor. let mixScope = this.#mixinScope(); if (mixScope != null) { method = this.#scopedClassMethod(ct, `${mixScope}::${slotName}`); if (method == null) { // A decorator body calling one of its `shared` methods. let sharedScope = this.#decoratorSharedScope(); if (sharedScope != null) { method = this.#scopedClassMethod(ct, `${sharedScope}::${slotName}`); } } if (method == null) { this.#bail('mixin private method not found'); } } else { // Private methods are lexically scoped and never virtual: they // resolve ONLY against the class the code was written in — plus // that class's own specializations (same canonical class), since // specialized copies register under their specialized keys. // Never against super- or subclasses: private names cannot be // shared across a hierarchy. let lexicalCt = this.#lexicalClassOf(member, ct); let lexKey = getClassMethodKey(getClassKey(lexicalCt), slotName); if (let (true, m) = this.wasm.classMethodMap.get(lexKey)) { method = m; } if (method == null) { let lexCanonical = this.#canonicalKeyOf(lexicalCt); var c: ClassType | null = ct; while (c != null && method == null) { let cc = c; if (this.#canonicalKeyOf(cc) == lexCanonical) { let key = getClassMethodKey(getClassKey(cc), slotName); if (let (true, m2) = this.wasm.classMethodMap.get(key)) { method = m2; } } c = cc.superType; } } if (method == null) { this.#bail('private method not found'); } } } var currClass: ClassType | null = ct; while (currClass != null && method == null) { // Inner walk through generic sources: shared (unspecialized) // template methods register under the template's key. var g: ClassType | null = currClass; while (g != null && method == null) { let key = getClassMethodKey(getClassKey(g), slotName); if (let (true, m) = this.wasm.classMethodMap.get(key)) { method = m; } g = g.genericSource; } currClass = currClass.superType; } if (method == null && checkedCt != ct) { // Nothing from the value's class carries this slot. A `this` // return is where that happens: `clone(): this` on a Dog is a // Dog to the checker, but its wasm signature returns the Animal // that declared it, so the value says Animal and a call to Dog's // own `bark` finds nothing. The checker's class is the narrower // one, and the `this` contract says the value really is of it, // so resolve there and downcast the receiver below. // // Only as a fallback: while the value's class does carry the // slot, it stays the right answer — it is the one an override // dispatches from, and narrowing first would resolve a subclass // body statically and lose the vtable. var narrowWalk: ClassType | null = checkedCt; while (narrowWalk != null && method == null) { let nw = narrowWalk; var ng: ClassType | null = nw; while (ng != null && method == null) { let key = getClassMethodKey(getClassKey(ng), slotName); if (let (true, m) = this.wasm.classMethodMap.get(key)) { method = m; narrowedFromChecked = true; } ng = ng.genericSource; } narrowWalk = nw.superType; } if (narrowedFromChecked) { ct = checkedCt; if (let (true, info) = this.wasm.classInfos.get(getClassKey(ct))) { classInfo = info; } } } if (method == null) { this.#bail('method not found'); } // Intrinsic methods inline as instructions, exactly like their // call-position forms — streaming routes them through the same // per-name generators (the marker receiver carries no value). let {intrinsic} = method; if (intrinsic != null) { return lowerIntrinsic(this.#cx(), intrinsic, call, multiOut); } if (method.isStatic || (method.typeParameters != null && method.typeArguments == null)) { this.#bail('method shape'); } // Private access is never virtual; overload slots are overridden // independently (member-lookup.md §5.3), so the check keys on the // SLOT name. An overridden slot dispatches through the vtable. // // An extension class never dispatches. It erases to its underlying // value, which carries nothing saying which class in the hierarchy // it was reached as — a `RootedFilePath` and a `RootedPath` are the // same String at runtime — so there is no vtable to consult and no // question to ask it. An override there shadows: which body runs // follows the static type of the receiver, as the language // reference specifies. Without this the call tried to dispatch and // hit the missing slot as an internal error. var isVirtual = false; if (!isPrivate && !ct.isExtension && !this.#cx().exactValues.has(receiver) && this.#cx().memberProvidedBySubclass(classInfo, slotName)) { if (this.#vtableSlotIndex(classInfo, slotName) < 0) { this.#bail('overridden method without vtable slot'); } isVirtual = true; } let sig = method.signature; if (sig.returns.length > 1 && multiOut == null) { this.#bail('method multi-value return'); } let inlineParams = this.#inlineLayoutsOf(method); if (sig.params.length != this.#liveArgWidth(call.args, inlineParams) + 1) { this.#bail('method arity adaptation'); } if (narrowedFromChecked) { // The value is the declaring class, the slot wants the subclass // the `this` return named. conformToSlot emits the ref.cast, // which traps if a body broke the contract by handing back // something other than its own receiver. receiver = this.#conformToSlot(receiver, sig.params[0], 'method receiver type'); } else { receiver = this.#conformReceiver(receiver, sig.params[0], 'method receiver type'); } let defaults = this.#defaultArgInfo(call); // A direct call to a method whose record parameters or result explode // (explode.zena) calls the core when it has a record unpacked or only // reads the result's fields, as a call to a function does. A virtual // call reaches the method through its vtable slot, which keeps the // record signature. let explosion = method.explosion; if (!isVirtual && defaults == null && explosion != null && explosion.forwarder === method && (this.#passesUnpackedArg(call.args, explosion) || this.#wantsResultFields(call, explosion))) { let core = explosion.core; let coreArgs = growable([ this.#conformToSlot(receiver, core.signature.params[0], 'method receiver type'), ]); for (let value in this.#lowerExplodedArgs(call.args, explosion, 1)) { coreArgs.push(value); } let explodedResult = explosion.result; if (explodedResult != null) { return this.#callForFields(core, coreArgs, explodedResult); } let coreReturns = core.signature.returns; if (coreReturns.length > 1) { let outs = multiOut as GrowableArray; let projs = this.b.callFuncMulti(this.b.addRef(new IrRefFunc(core)), coreArgs, coreReturns); this.#pushMultiResults(call, projs, outs); return projs[0]; } return this.b.callFunc(this.b.addRef(new IrRefFunc(core)), coreArgs, if (coreReturns.length == 1) coreReturns[0] else null); } let args = growable([receiver]); var sIdx = 1; for (var i = 0; i < call.args.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(call.args[i], inlineLayout, args); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(call.args[i])) { continue; } let paramVt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(paramVt, 'method param type'); var value = this.#lowerArgWithDefaults(call, i, paramVt, defaults, receiver); value = this.#conformToSlot(value, paramVt, 'method argument type'); args.push(value); } if (sig.returns.length > 1) { for (let r in sig.returns) { this.#requireSupported(r, 'method result type'); } let projs = if (isVirtual) { this.#cx().lowerVtableCallMulti(receiver, classInfo, slotName, sig, args) } else { this.b.callFuncMulti(this.b.addRef(new IrRefFunc(method)), args, sig.returns) }; let outs = multiOut as GrowableArray; this.#pushMultiResults(call, projs, outs); return projs[0]; } let resultType = if (sig.returns.length == 1) sig.returns[0] else null; if (resultType != null) { this.#requireSupported(resultType, 'method result type'); } let result = if (isVirtual) { this.#lowerVtableCall(receiver, classInfo, slotName, sig, args, resultType) } else { this.b.callFunc(this.b.addRef(new IrRefFunc(method)), args, resultType) }; if (resultType != null) { let nodeType = this.#refinableCallType(call); if (nodeType != null) { let want = typeToValTypeWithSub(this.wasm, nodeType, this.func); if (want is ValTypeRef && resultType is ValTypeRefNull) { return this.b.emit(IrOp.ref_as_non_null, want, result, -1); } if (want is ValTypeRefExtern && resultType is ValTypeExternRef) { return this.b.emit(IrOp.ref_as_non_null, want, result, -1); } } } return result; } /** * Index reads. Raw wasm arrays (FixedArray values ARE wasm arrays) * read via array_get; boxed tuples read the literal-index field; * interface receivers dispatch through the [] vtable slot; class * receivers resolve the [] overload by mangled signature key exactly * like streaming (intrinsics preferred per class, supertype and * generic-source walk) and call it directly. */ #lowerIndexRead(idx: IndexExpression): i32 { if (hasOptionalChainStep(idx as Expression)) { return this.#lowerOptionalChainExpr(idx as Expression); } let objectType = this.#substitutedNodeType(idx.object); if (objectType == null) { this.#bail('untyped index receiver'); } let inlineObject = nestedInlineTuple(objectType); if (inlineObject != null) { return this.#lowerInlineIndexRead(idx, inlineObject); } let selfResolved = this.#resolveSelfType(objectType); if (selfResolved is InterfaceType) { let args = growable([idx.index]); let slotName = interfaceCallSlotName(this.wasm, selfResolved, '[]', this.func.model.getResolvedOverload(idx)); let result = this.#lowerInterfaceDispatch( selfResolved, slotName, idx.object, args); return this.#reassertNonNull(result, this.#exprValType(idx)); } let receiver = this.#lowerExpression(idx.object); return this.#lowerIndexReadOn(idx, selfResolved, receiver); } /** * `f()[k]` on an inline result (#155): the value in position `k`'s * slot, or that position boxed when it is itself an inline tuple. The * other positions are lowered and dropped, so nothing is boxed. */ #lowerInlineIndexRead(idx: IndexExpression, shape: InlineTupleType): i32 { let source = this.#lowerMultiSource(idx.object); if (!(idx.index is NumberLiteral)) { return this.#bailValue('dynamic inline tuple index'); } let k = if (let (true, n) = parseTupleIndexLiteral((idx.index as NumberLiteral).raw)) n else -1; if (k < 0 || k >= shape.elementTypes.length) { return this.#bailValue('inline tuple index literal'); } let path = i32ToString(k); let sourceLayout = source.layout; if (sourceLayout != null && sourceLayout.isUnionAt(path)) { return boxInlineUnionAt(this.#cx(), source, path, null); } let record = nestedInlineRecord(shape.elementTypes[k]); if (record != null) { return boxInlineRecordAt(this.#cx(), source, path, record, null); } let inner = nestedInlineTuple(shape.elementTypes[k]); if (inner != null) { return boxInlineAt(this.#cx(), source, path, inner, null); } let value = if (source.layout != null) { source.valueAt(this.#cx(), path, null) } else if (k < source.values.length) { source.values[k] } else { NO_VALUE }; if (value == NO_VALUE) { // A zero-width element has no slot, and no value. return NO_VALUE; } return this.#reassertNonNull(value, this.#exprValType(idx)); } /** * The receiver-value core of an index read: boxed-tuple fields, * array elements, then [] overloads on classes. Shared by the plain * path and the optional-chain hit arm. */ #lowerIndexReadOn(idx: IndexExpression, selfResolved: Type, receiver: i32): i32 { if (baseType(selfResolved) is TupleType) { if (!(idx.index is NumberLiteral)) { this.#bail('dynamic tuple index'); } let elemIdx = if (let (true, n) = parseTupleIndexLiteral((idx.index as NumberLiteral).raw)) n else -1; if (elemIdx < 0) { this.#bail('tuple index literal'); } let st = asWasmStruct(this.#valueType(receiver)); if (st == null) { this.#bail('tuple receiver kind'); } let fieldName = i32ToString(elemIdx); if (st.fieldIndexByName(fieldName) < 0) { // A zero-width element has no field, and no value. return NO_VALUE; } let fieldRef = this.b.addRef(new IrRefField(st, fieldName)); let read = this.b.emit(IrOp.struct_get, this.#fieldValType(st, fieldName), receiver, fieldRef); return this.#reassertNonNull(read, this.#exprValType(idx)); } let arr = asWasmArray(this.#valueType(receiver)); // A class-typed index (a range: arr[1..3]) is not an element read; // it dispatches to the matching [] overload like any class receiver. // Extension classes stay on the element path: their values ARE // their onType (an i32 index handle reads like an i32). let idxTypeOpt = this.#substitutedNodeType(idx.index); var classIndex = false; if (idxTypeOpt != null) { let idxBase = baseType(idxTypeOpt); if (idxBase is ClassType) { classIndex = !idxBase.isExtension; } } if (arr != null && !classIndex) { let index = this.#lowerExpression(idx.index); if (!(this.#valueType(index) is ValTypeI32)) { this.#bail('index operand type'); } if (arr.elementType is ValTypeI8 || arr.elementType is ValTypeI16) { // Packed elements unpack to i32. The wasm array type does not // distinguish u8 from i8, so the extension comes from the // semantic element type of the receiver. let op = if (this.#packedElementSigned(idx.object)) { IrOp.array_get_s } else { IrOp.array_get_u }; return this.b.emit(op, new ValTypeI32(), receiver, index); } this.#requireSupported(arr.elementType, 'array element type'); let value = this.b.emit(IrOp.array_get, arr.elementType, receiver, index); return this.#reassertNonNull(value, this.#exprValType(idx)); } // Class receiver: [] overload, resolved by mangled signature key. // The receiver VALUE's class wins (specializations), except for // extension classes where the value is the underlying onType. let fromValue = this.#classTypeOfValue(receiver); let ct = if (selfResolved is ClassType && selfResolved.isExtension) { eraseTypeParameters(this.wasm, selfResolved) as ClassType } else if (fromValue != null) { fromValue } else if (selfResolved is ClassType) { eraseTypeParameters(this.wasm, selfResolved) as ClassType } else { null }; if (ct == null) { this.#bail('index receiver kind'); } // Overload choice belongs to the checker: it records the selected // declared FunctionType per index node, and registration mangles // overloaded members with that same declaration's signature key — // so the exact registered name is reproducible with NO argument- // type reconstruction here. No record means no overloads: the // plain name is unambiguous. let chosenFt = this.func.model.getResolvedOverload(idx); let mangled = if (chosenFt != null) { '[]' + getSignatureKey(chosenFt) } else { '[]' }; let method = this.#resolveIndexMethod(ct, mangled, chosenFt != null); if (method == null) { this.#bail('index method not found'); } if (method.intrinsic != null) { // Linear-memory byte access (mem[i]) is the only [] intrinsic: // one load instruction, align 0 — streaming's shape. if ((method.intrinsic as String) == 'i32.load8_u') { let addr = this.#lowerExpression(idx.index); return this.b.emit(IrOp.i32_load8_u, new ValTypeI32(), addr, 0); } this.#bail('index intrinsic'); } if (method.isStatic || method.typeParameters != null) { this.#bail('index method shape'); } let sig = method.signature; if (sig.params.length != 2 || sig.returns.length > 1) { this.#bail('index method arity'); } let recv = this.#conformReceiver(receiver, sig.params[0], 'index receiver type'); this.#requireSupported(sig.params[1], 'index param type'); var indexValue = this.#lowerExpressionCtx(idx.index, sig.params[1]); indexValue = this.#conformToSlot(indexValue, sig.params[1], 'index operand type'); let resultType = if (sig.returns.length == 1) sig.returns[0] else null; if (resultType != null) { this.#requireSupported(resultType, 'index result type'); } let methodRef = this.b.addRef(new IrRefFunc(method)); let result = this.b.callFunc(methodRef, this.#twoValues(recv, indexValue), resultType); if (resultType == null) { return result; } return this.#reassertNonNull(result, this.#exprValType(idx)); } /** * Finds the [] implementation registered under `mangled`, walking * supertype and generic-source chains, preferring intrinsic * registrations per class (streaming's rule). When the checker * recorded an overload choice (`exact`), ONLY that name is * acceptable — falling back to the plain name could silently bind a * different overload. */ #resolveIndexMethod(ct: ClassType, mangled: String, exact: boolean): WasmFunction | null { var currClass: ClassType | null = ct; while (currClass != null) { var g: ClassType | null = currClass; while (g != null) { let cKey = getClassKey(g); let keyWithSig = getClassMethodKey(cKey, mangled); if (let (true, f) = this.wasm.classMethodMap.get(keyWithSig)) { if (f.intrinsic != null) { return f; } } if (!exact) { if (let (true, f2) = this.wasm.classMethodMap.get(keyWithSig)) { return f2; } } else if (let (true, f3) = this.wasm.classMethodMap.get(keyWithSig)) { return f3; } g = g.genericSource; } currClass = currClass.superType; } return null; } /** * Interface vtable dispatch (ir.md §5): unpack the fat pointer, load * the member's funcref slot from the vtable, and call_ref through it. * `memberName` is the vtable slot name — a method name or a get#/set# * accessor. `argExprs` is null for accessor reads (receiver only). */ #lowerInterfaceDispatch(it: InterfaceType, memberName: String, receiverExpr: Expression, argExprs: GrowableArray | null, multiOut: GrowableArray | null = null, givenReceiver: i32 = -1): i32 { let iName = getInterfaceKey(it); let vst = if (let (true, v) = this.wasm.interfaceVTableStructs.get(iName)) v else null; let fatPtr = if (let (true, st) = this.wasm.interfaceStructs.get(iName)) st else null; if (vst == null || fatPtr == null) { this.#bail('interface not discovered'); } let slot = vst.fieldIndexByName(memberName); if (slot < 0) { // Function-typed interface fields called as methods land here // (streaming falls back to a closure call); closures bail. this.#bail(`interface member not in vtable: ${iName as String}.${memberName}`); } // The vtable slot's declared type IS the signature (the RTA pass // types slots as exact (ref $sig)); using it keeps the callee value // consistent with what emission derives the call_ref type from. let sig = asWasmSignature(vst.fields[slot].type); if (sig == null) { this.#bail('interface slot shape'); } if (sig.returns.length > 1 && multiOut == null) { this.#bail('interface member multi-value return'); } // The member's parameters, at this interface's type arguments: an // inline one takes its slots. let member = if (it.members.has(memberName)) it.members[memberName] else null; let inlineParams = if (member != null && member.type is FunctionType) { this.#inlineParamLayouts(substituteTypeParamsInCodegen(this.wasm, getInterfaceTypeParameters(it), it.typeArguments, member.type)) } else { null }; let argCount = if (argExprs == null) 0 else this.#liveArgWidth(argExprs, inlineParams); if (sig.params.length != argCount + 1) { this.#bail('interface call arity adaptation'); } let recv = if (givenReceiver >= 0) givenReceiver else this.#lowerExpression(receiverExpr); if (!isValSubtype(this.#valueType(recv), new ValTypeRefNull(fatPtr) as ValType)) { this.#bail('interface receiver type'); } let inst = this.b.emit( IrOp.iface_instance, fatPtr.fields[0].type, recv, -1); if (!isValSubtype(this.#valueType(inst), sig.params[0])) { this.#failConformance(inst, sig.params[0], 'interface receiver adaptation'); } let args = growable([inst]); if (argExprs != null) { var sIdx = 1; for (var i = 0; i < argExprs.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(argExprs[i], inlineLayout, args); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(argExprs[i])) { continue; } let paramVt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(paramVt, 'interface param type'); var value = this.#lowerExpressionCtx(argExprs[i], paramVt); value = this.#conformToSlot(value, paramVt, 'interface argument type'); args.push(value); } } let vt = this.b.emit(IrOp.iface_vtable, fatPtr.fields[1].type, recv, -1); let fieldRef = this.b.addRef(new IrRefField(vst, memberName)); let callee = this.b.emit( IrOp.struct_get, vst.fields[slot].type, vt, fieldRef); if (sig.returns.length > 1) { for (let r in sig.returns) { this.#requireSupported(r, 'interface result type'); } let projs = this.b.callRefMulti(callee, args, sig.returns); let outs = multiOut as GrowableArray; for (let pj in projs) { outs.push(pj); } return projs[0]; } var resultType: ValType | null = null; if (sig.returns.length == 1) { this.#requireSupported(sig.returns[0], 'interface result type'); resultType = sig.returns[0]; } return this.b.callRef(callee, args, resultType); } #classTypeOfValue(value: i32): ClassType | null { return this.#cx().classTypeOfValue(value); } /** * The LEXICAL class of a member access: derived from the checked * type of the receiver expression, which names the class the * accessing code was written in — the generic template inside * specialized-method bodies, and the DECLARING class inside method * bodies the compiler copies into subclasses. Private members are * lexically scoped, so their mangled names must resolve against * this, never against the receiver value's (possibly more derived, * possibly more specialized) runtime class. */ #lexicalClassOf(member: MemberExpression, fallback: ClassType): ClassType { let objectType = this.#substitutedNodeType(member.object); if (objectType != null) { let selfResolved = this.#resolveSelfType(objectType); if (selfResolved is ClassType) { return eraseTypeParameters(this.wasm, selfResolved) as ClassType; } } return fallback; } /** * The mixin private scope in force for this function, or null when * its body was written in a class. When it is non-null, EVERY `#` * access the body makes is the mixin's — a mixin body cannot name a * host class's private (privacy is lexical, pinned by * `mixins/mixin-base-private-field.zena`) — so the three private * resolvers below treat the scoped name as the only answer and bail * rather than fall back. Falling back is what silently bound the * host's same-named member. * * Registration sets the key where it knows it; otherwise it is * derived here from where the function's node was WRITTEN, which is * the only thing that answers for closures inside mixin methods and * for monomorphized copies of generic mixin methods (their * registration paths never see a mixin). */ /** * The declaration scope key of the decorator whose body this function * is, where the decorator's `shared` members live; null for any other * function. The application's own key is `#mixinScope()`. */ #decoratorSharedScope(): String | null { let declaring = this.func.declaringType; if (declaring == null) { return null; } return declaring.decoratorSharedScopeKey; } /** * The registered method `scopedName` on `ct` or an ancestor, or null. * Scoped (mixin or decorator) registrations are walked from the * receiver's class because the body may be applied at an ancestor. */ #scopedClassMethod(ct: ClassType, scopedName: String): WasmFunction | null { var applied: ClassType | null = ct; while (applied != null) { let key = getClassMethodKey(getClassKey(applied), scopedName); if (let (true, f) = this.wasm.classMethodMap.get(key)) { return f; } applied = applied.superType; } return null; } #mixinScope(): String | null { if (!this.func.privateScopeResolved) { this.func.privateScopeResolved = true; if (this.func.privateScopeKey == null && this.func.node != null) { this.func.privateScopeKey = mixinScopeOfNode(this.func.model, this.func.node as Node); } } return this.func.privateScopeKey; } /** * Resolves a private member's mangled struct-field name by walking * the LEXICAL declaring chain — supertypes and generic sources — * since fields are stored under the DECLARING class's key (the * template's for specialized generics, an ancestor's for privates * accessed from copied method bodies). */ #privateLookupName(st: WasmStruct, ct: ClassType, propName: String): String | null { // Functions compiled from a mixin body resolve privates under the // mixin's scope key ("_This@::#x") and under nothing // else — mixin privates are lexical to the mixin declaration and // never alias a host's same-named private (#mixinScope). A miss // here is not a reason to try the host's field; it means the name // is a private ACCESSOR (the caller's next probe) or the field was // never registered, and the caller bails. let mixScope = this.#mixinScope(); if (mixScope != null) { let scopedName = `${mixScope}::${propName}`; if (st.fieldIndexByName(scopedName) >= 0) { return scopedName; } // A decorator body's `shared` fields live under the declaration's // key rather than the application's. let sharedScope = this.#decoratorSharedScope(); if (sharedScope != null) { let sharedName = `${sharedScope}::${propName}`; if (st.fieldIndexByName(sharedName) >= 0) { return sharedName; } } return null; } // Shared with the streaming backend: struct field names carry the // key of whatever class the struct was BUILT for, so the struct // itself resolves, lexically disambiguated. return st.resolvePrivateFieldName(ct, propName); } /** * Resolves a private accessor function (get#/set# + private name) * lexically: the calling function's mixin scope key first (scoped * registrations walk the receiver's chain, since the mixin may be * applied at an ancestor), then the LEXICAL class and its generic * sources. Null when no such accessor is registered. */ #resolvePrivateAccessor(ct: ClassType, lexicalCt: ClassType, accName: String): WasmFunction | null { let mixScope = this.#mixinScope(); if (mixScope != null) { let scoped = this.#scopedClassMethod(ct, `${mixScope}::${accName}`); if (scoped != null) { return scoped; } let sharedScope = this.#decoratorSharedScope(); if (sharedScope != null) { return this.#scopedClassMethod(ct, `${sharedScope}::${accName}`); } // The scoped registration is the only one a mixin body may // name; the lexical walk below would find the HOST's accessor. return null; } var candidate: ClassType | null = lexicalCt; while (candidate != null) { let key = getClassMethodKey(getClassKey(candidate), accName); if (let (true, f) = this.wasm.classMethodMap.get(key)) { return f; } candidate = candidate.genericSource; } return null; } #canonicalKeyOf(ct: ClassType): String { return this.#cx().canonicalKeyOf(ct); } /** Receiver class for member/method resolution: the value's own * class when its struct carries one, else the checked type. */ #receiverClass(receiver: i32, member: MemberExpression): ClassType { // Extension receivers: the checked extension class wins — the // VALUE is the underlying onType and never names the extension. let checkedType = this.#substitutedNodeType(member.object); if (checkedType != null) { let nonNullChecked = if (member.optional) getNonNullableType(checkedType) else checkedType; let selfR = this.#resolveSelfType(nonNullChecked); if (selfR is ClassType && selfR.isExtension) { return eraseTypeParameters(this.wasm, selfR) as ClassType; } } let fromValue = this.#classTypeOfValue(receiver); if (fromValue != null) { return fromValue; } let objectType = this.#substitutedNodeType(member.object); if (objectType == null) { this.#bail('untyped member receiver'); } let nonNullObj = if (member.optional) getNonNullableType(objectType) else objectType; let selfResolved = this.#resolveSelfType(nonNullObj); if (!(selfResolved is ClassType)) { this.#bail('member on non-class'); } return eraseTypeParameters(this.wasm, selfResolved) as ClassType; } #vtableSlotIndex(classInfo: ClassCodegenInfo, slotName: String): i32 { return this.#cx().vtableSlotIndex(classInfo, slotName); } #lowerVtableCall(receiver: i32, classInfo: ClassCodegenInfo, slotName: String, sig: WasmSignature, args: GrowableArray, resultType: ValType | null): i32 { return this.#cx().lowerVtableCall(receiver, classInfo, slotName, sig, args, resultType); } /** * Resolves a member read against the receiver's class: either a * physical field (fieldName set) or a non-overridden getter function. */ #resolveMemberTarget(member: MemberExpression, ct: ClassType): MemberTarget { let cKey = getClassKey(ct); let classInfo = if (let (true, info) = this.wasm.classInfos.get(cKey)) info else null; if (classInfo == null) { this.#bail('receiver class not discovered'); } let {structType} = classInfo; let propName = accessKeyOf(this.func.model, member); var lookupName = propName; if (propName.length > 0 && propName.sliceBytes(0, 1) == "#") { if (this.func.classType == null) { this.#bail('private access outside class'); } let lexicalCt = this.#lexicalClassOf(member, ct); let resolved = this.#privateLookupName(structType, lexicalCt, propName); if (resolved != null) { lookupName = resolved; } else { // No physical field: a private ACCESSOR. Resolve the getter // LEXICALLY (never against the receiver chain — copied-down // method bodies are lowered with the subclass as receiver // class, and a subclass's shadowing private accessor must not // win): mixin scope key first, then the lexical class and its // generic sources. let privGetter = this.#resolvePrivateAccessor(ct, lexicalCt, 'get#' + propName); if (privGetter != null) { return new MemberTarget(structType, null, privGetter); } // Never fall through to the receiver-chain getter walk below: // it could bind a subclass's shadowing private accessor. this.#bail('private member not found'); } } // Physical field with the property's name? (Memoized on the // struct: this runs once per member access.) A direct read is only // sound when no class between the receiver and the field's declarer // redefined the accessor (a subclass shadowing an inherited field // with a getter leaves the parent's physical field in the struct), // and no transitive subclass overrides it either. Private access // is never virtual, so those checks only apply to public members. let isPrivate = propName.length > 0 && propName.sliceBytes(0, 1) == "#"; let hasField = structType.fieldIndexByName(lookupName) >= 0; if (hasField && isPrivate) { return new MemberTarget(structType, lookupName, null); } if (hasField && !this.#accessorRedefined(ct, propName, 'get#' + propName)) { if (this.#cx().accessorOverriddenBySubclass( classInfo, 'get#' + propName, lookupName)) { // A subclass overrides the accessor: the read must dispatch. // The slot signature is the DEFAULT getter registered by the // declaring class (present whenever the slot is). return this.#virtualGetterTarget( structType, classInfo, ct, propName); } return new MemberTarget(structType, lookupName, null); } // Getter? getClassMethodKey(cKey, 'get#' + propName); var getter: WasmFunction | null = null; var currClass: ClassType | null = ct; while (currClass != null && getter == null) { let key = getClassMethodKey(getClassKey(currClass), 'get#' + propName); if (let (true, g) = this.wasm.classMethodMap.get(key)) { getter = g; } currClass = currClass.superType; } if (getter == null) { this.#bail(`member not found: ${propName} on ${ct.name}`); } if (getter.isStatic) { this.#bail('static member'); } if (!isPrivate && this.#cx().memberProvidedBySubclass(classInfo, 'get#' + propName)) { let target = new MemberTarget(structType, null, getter); if (this.#vtableSlotIndex(classInfo, 'get#' + propName) < 0) { this.#bail('overridden getter without vtable slot'); } target.vtSlot = 'get#' + propName; target.vtInfo = classInfo; return target; } return new MemberTarget(structType, null, getter); } /** * A virtual-getter member target: resolves the slot's signature * from the nearest registered getter and marks the target for * vtable dispatch. */ #virtualGetterTarget(structType: WasmStruct, classInfo: ClassCodegenInfo, ct: ClassType, propName: String): MemberTarget { var getter: WasmFunction | null = null; var currClass: ClassType | null = ct; while (currClass != null && getter == null) { let key = getClassMethodKey( getClassKey(currClass), 'get#' + propName); if (let (true, g) = this.wasm.classMethodMap.get(key)) { getter = g; } currClass = currClass.superType; } if (getter == null) { this.#bail('overridden field without getter slot'); } if (this.#vtableSlotIndex(classInfo, 'get#' + propName) < 0) { this.#bail('overridden field without vtable slot'); } let target = new MemberTarget(structType, null, getter); target.vtSlot = 'get#' + propName; target.vtInfo = classInfo; return target; } /** * True when a class between the receiver and the property's declaring * class redefines the accessor: the field-declaring class provides the * DEFAULT accessor, and if the nearest definition walking up from the * receiver differs, physical field access is wrong (mirrors the * streaming generator's hasOverride computation). */ #accessorRedefined(ct: ClassType, propName: String, accessorName: String): boolean { var fieldDeclaringClass = ct; var curr: ClassType | null = ct; while (curr != null) { let c = curr; if (c.members.has(propName) && c.members[propName] is FieldInfo) { fieldDeclaringClass = c; } curr = c.superType; } let defKey = getClassMethodKey(getClassKey(fieldDeclaringClass), accessorName); let defaultAccessor = if (let (true, d) = this.wasm.classMethodMap.get(defKey)) d else null; var resolved: WasmFunction | null = null; var currClass: ClassType | null = ct; while (currClass != null && resolved == null) { let key = getClassMethodKey( getClassKey(currClass), accessorName); if (let (true, r) = this.wasm.classMethodMap.get(key)) { resolved = r; } currClass = currClass.superType; } return resolved != null && defaultAccessor != null && resolved != defaultAccessor; } /** * Index writes `recv[i] = v`: array.set for wasm-array receivers, * vtable dispatch through the []= slot for interface receivers, * else a direct call to the checker-selected `[]=` overload (the * selection is recorded on the ASSIGNMENT node; the read selection * of a compound assignment lives on the IndexExpression). Yields * the assigned value, like every assignment expression. */ #lowerIndexWrite(assign: AssignmentExpression): i32 { let idx = assign.left as IndexExpression; if (idx.optional) { this.#bail('optional index'); } let objectType = this.#substitutedNodeType(idx.object); if (objectType == null) { this.#bail('untyped index receiver'); } let selfResolved = this.#resolveSelfType(objectType); // Interface receivers write through the []= vtable slot, like // reads through [] and property writes through set#. if (selfResolved is InterfaceType) { if (assign.operator != null) { return this.#lowerInterfaceCompoundIndexWrite(assign, idx, selfResolved); } let dispatchArgs = growable([idx.index, assign.value]); // Setter dispatch returns no value; assignment-as-expression on // an interface index would need the value replayed — the // verifier's produces-no-value check trips loudly if used. let slotName = interfaceCallSlotName(this.wasm, selfResolved, '[]=', this.func.model.getResolvedOverload(assign)); return this.#lowerInterfaceDispatch( selfResolved, slotName, idx.object, dispatchArgs); } let receiver = this.#lowerExpression(idx.object); let arr = asWasmArray(this.#valueType(receiver)); if (arr != null) { let index = this.#lowerExpression(idx.index); if (!(this.#valueType(index) is ValTypeI32)) { this.#bail('index operand type'); } // Packed elements write plain i32 values; array.set truncates. let packedElem = arr.elementType is ValTypeI8 || arr.elementType is ValTypeI16; var elemCtx: ValType = new ValTypeI32(); if (!packedElem) { this.#requireSupported(arr.elementType, 'array element type'); elemCtx = arr.elementType; } var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, elemCtx); } else { // Compound: SSA reuses the receiver and index values, so the // element reads once and writes once. let current = if (packedElem) { let op = if (this.#packedElementSigned(idx.object)) { IrOp.array_get_s } else { IrOp.array_get_u }; this.b.emit(op, new ValTypeI32(), receiver, index) } else { this.b.emit(IrOp.array_get, arr.elementType, receiver, index) }; value = this.#combineCompound(current, assign, idx as Expression); } if (!isValSubtype(this.#valueType(value), elemCtx)) { this.#failConformance(value, elemCtx, 'array write type'); } let tripleIdx = this.b.pushExtra(this.#threeValues(receiver, index, value)); this.b.emitVoid(IrOp.array_set, tripleIdx, -1); return value; } // Class receiver: the checker records the selected `[]=` overload // on the assignment node; registration mangles overloaded members // with the same declaration's signature key. No record means no // overloads. let fromValue = this.#classTypeOfValue(receiver); let ct = if (selfResolved is ClassType && selfResolved.isExtension) { eraseTypeParameters(this.wasm, selfResolved) as ClassType } else if (fromValue != null) { fromValue } else if (selfResolved is ClassType) { eraseTypeParameters(this.wasm, selfResolved) as ClassType } else { null }; if (ct == null) { this.#bail('index receiver kind'); } let chosenFt = this.func.model.getResolvedOverload(assign); let mangled = if (chosenFt != null) { '[]=' + getSignatureKey(chosenFt) } else { '[]=' }; let method = this.#resolveIndexMethod(ct, mangled, chosenFt != null); if (method == null) { this.#bail('index setter not found'); } if (method.intrinsic != null) { if (assign.operator != null) { this.#bail('compound index intrinsic'); } // Linear-memory byte store (mem[i] = v): one store instruction, // align 0 — streaming's shape. if ((method.intrinsic as String) == 'i32.store8') { let addr = this.#lowerExpression(idx.index); let sv = this.#lowerExpressionCtx(assign.value, new ValTypeI32()); let pair = growable([addr, sv]); this.b.emitVoid(IrOp.i32_store8, this.b.pushExtra(pair), 0); return sv; } this.#bail('index intrinsic'); } if (method.isStatic || method.typeParameters != null) { this.#bail('index method shape'); } let sig = method.signature; if (sig.params.length != 3 || sig.returns.length > 0) { this.#bail('index setter shape'); } let recv = this.#conformReceiver(receiver, sig.params[0], 'index receiver type'); var index = this.#lowerExpressionCtx(idx.index, sig.params[1]); index = this.#conformToSlot(index, sig.params[1], 'index operand type'); var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, sig.params[2]); } else { // Compound: the checker records the read [] overload on the // IndexExpression; the receiver and index values feed the read // and the write, so each evaluates once. let readFt = this.func.model.getResolvedOverload(idx); let readMangled = if (readFt != null) { '[]' + getSignatureKey(readFt) } else { '[]' }; let readMethod = this.#resolveIndexMethod(ct, readMangled, readFt != null); if (readMethod == null) { this.#bail('index method not found'); } if (readMethod.intrinsic != null) { this.#bail('compound index intrinsic'); } if (readMethod.isStatic || readMethod.typeParameters != null) { this.#bail('index method shape'); } let readSig = readMethod.signature; if (readSig.params.length != 2 || readSig.returns.length != 1) { this.#bail('index method arity'); } let readRecv = this.#conformReceiver(receiver, readSig.params[0], 'index receiver type'); if (!isValSubtype(this.#valueType(index), readSig.params[1])) { this.#failConformance(index, readSig.params[1], 'index operand type'); } this.#requireSupported(readSig.returns[0], 'index result type'); let current = this.b.callFunc( this.b.addRef(new IrRefFunc(readMethod)), this.#twoValues(readRecv, index), readSig.returns[0]); value = this.#combineCompound(current, assign, idx as Expression); } value = this.#conformToSlot(value, sig.params[2], 'index write type'); let funcRef = this.b.addRef(new IrRefFunc(method)); let args = growable([recv, index, value]); this.b.callFunc(funcRef, args, null); return value; } /** * A compound index write through an interface receiver * (`recv[i] op= v`): the [] vtable slot reads the current element, * #combineCompound applies the operator, and the []= slot writes * the result. The receiver and index lower once, feeding both * dispatches. Yields the assigned value. */ #lowerInterfaceCompoundIndexWrite(assign: AssignmentExpression, idx: IndexExpression, it: InterfaceType): i32 { let iName = getInterfaceKey(it); let vst = if (let (true, v) = this.wasm.interfaceVTableStructs.get(iName)) v else null; let fatPtr = if (let (true, st) = this.wasm.interfaceStructs.get(iName)) st else null; if (vst == null || fatPtr == null) { this.#bail('interface not discovered'); } // An overloaded `[]` or `[]=` has a slot per overload: the read's // overload is recorded on the index expression, the write's on the // assignment. let readName = interfaceCallSlotName(this.wasm, it, '[]', this.func.model.getResolvedOverload(idx)); let writeName = interfaceCallSlotName(this.wasm, it, '[]=', this.func.model.getResolvedOverload(assign)); let readSlot = vst.fieldIndexByName(readName); let writeSlot = vst.fieldIndexByName(writeName); if (readSlot < 0 || writeSlot < 0) { this.#bail('interface member not in vtable'); } let readSig = asWasmSignature(vst.fields[readSlot].type); let writeSig = asWasmSignature(vst.fields[writeSlot].type); if (readSig == null || writeSig == null) { this.#bail('interface slot shape'); } if (readSig.params.length != 2 || readSig.returns.length != 1 || writeSig.params.length != 3 || writeSig.returns.length != 0) { this.#bail('interface call arity adaptation'); } let recv = this.#lowerExpression(idx.object); if (!isValSubtype(this.#valueType(recv), new ValTypeRefNull(fatPtr) as ValType)) { this.#bail('interface receiver type'); } let inst = this.b.emit( IrOp.iface_instance, fatPtr.fields[0].type, recv, -1); if (!isValSubtype(this.#valueType(inst), readSig.params[0]) || !isValSubtype(this.#valueType(inst), writeSig.params[0])) { this.#failConformance(inst, writeSig.params[0], 'interface receiver adaptation'); } this.#requireSupported(writeSig.params[1], 'interface param type'); this.#requireSupported(writeSig.params[2], 'interface param type'); this.#requireSupported(readSig.returns[0], 'interface result type'); var index = this.#lowerExpressionCtx(idx.index, writeSig.params[1]); index = this.#conformToSlot(index, writeSig.params[1], 'interface argument type'); if (!isValSubtype(this.#valueType(index), readSig.params[1])) { this.#failConformance(index, readSig.params[1], 'interface argument type'); } let vt = this.b.emit(IrOp.iface_vtable, fatPtr.fields[1].type, recv, -1); let readCallee = this.b.emit(IrOp.struct_get, vst.fields[readSlot].type, vt, this.b.addRef(new IrRefField(vst, readName))); let current = this.b.callRef( readCallee, this.#twoValues(inst, index), readSig.returns[0]); var value = this.#combineCompound(current, assign, idx as Expression); value = this.#conformToSlot(value, writeSig.params[2], 'index write type'); let writeCallee = this.b.emit(IrOp.struct_get, vst.fields[writeSlot].type, vt, this.b.addRef(new IrRefField(vst, writeName))); this.b.callRef(writeCallee, this.#threeValues(inst, index, value), null); return value; } /** Module-level global writes: global.set, with compound * assignments reading through global.get first. Yields the * assigned value like every assignment expression. */ #lowerGlobalAssign(assign: AssignmentExpression, g: WasmGlobal): i32 { var value = -1; if (assign.operator == null) { value = this.#lowerExpressionCtx(assign.value, g.type); } else { let readRef = this.b.addRef(new IrRefGlobal(g)); let current = this.b.emit(IrOp.global_get, g.type, -1, readRef); value = this.#combineCompound(current, assign, assign.left); } if (!isValSubtype(this.#valueType(value), g.type)) { this.#failConformance(value, g.type, 'global write type'); } let writeRef = this.b.addRef(new IrRefGlobal(g)); this.b.emitVoid(IrOp.global_set, value, writeRef); return value; } /** equals intrinsic (see ir/equality.zena). */ #lowerEqIntrinsic(call: CallExpression): i32 { return lowerEqIntrinsic(this.#cx(), call); } /** hash intrinsic (see ir/equality.zena). */ #lowerHashIntrinsic(call: CallExpression): i32 { return lowerHashIntrinsic(this.#cx(), call); } /** A value coerced to the target ValType, or a bail: identity when * already a subtype, ref.as_non_null for nullable-to-non-null of * the same target. */ #lowerOperatorMemberOn(opMember: String, left: i32, right: i32, leftCt: ClassType, chosenFt: FunctionType | null = null): i32 { return this.#cx().operatorMemberOn(opMember, left, right, leftCt, chosenFt); } /** * The combining step of a compound assignment: current (op) rhs. * Scalar operands map to the numeric operator (unsignedness read * from the target and value expressions); class operands (including * extension classes on primitives) resolve the operator member on * the target's class, exactly like binary expressions. */ #combineCompound(current: i32, assign: AssignmentExpression, target: Expression): i32 { let opName = assign.operator as String; let rhs = this.#lowerExpression(assign.value); let chosenFt = this.func.model.getResolvedCompoundOperator(assign); let targetType = this.#cx().substitutedNodeType(target); let targetBase = if (targetType != null) baseType(targetType) else null; if (targetBase != null && targetBase is ClassType) { let targetCt = eraseTypeParameters(this.wasm, targetBase) as ClassType; if (targetCt.members.has(opName)) { return this.#lowerOperatorMemberOn(opName, current, rhs, targetCt, chosenFt); } } let vt = this.#valueType(current); if (isRefValType(vt)) { let ct = this.#classTypeOfValue(current); if (ct == null) { this.#bail('compound operand kind'); } if (!ct.members.has(opName)) { this.#bail('compound operator member not found'); } return this.#lowerOperatorMemberOn(opName, current, rhs, ct, chosenFt); } if (!valTypeEquals(vt, this.#valueType(rhs))) { this.#bail('mixed compound assignment types'); } if (opName == "**") { let unsigned = this.#isUnsignedNode(target) || this.#isUnsignedNode(assign.value); if (vt is ValTypeI32 || vt is ValTypeI64) { return lowerIntPow(this.#cx(), current, rhs, vt, unsigned); } if (vt is ValTypeF32 || vt is ValTypeF64) { return lowerFloatPow(this.#cx(), current, rhs, vt); } } let unsigned = this.#isUnsignedNode(target) || this.#isUnsignedNode(assign.value); let op = this.#mapBinaryOp(opName, vt, unsigned); return this.b.binary(op, vt, current, rhs); } #coerceTo(value: i32, target: ValType): i32 { return this.#cx().coerceTo(value, target); } #resolveDevirtualizedMethod(ct: ClassType, methName: String): WasmFunction | null { return this.#cx().resolveDevirtualizedMethod(ct, methName); } /** Enum member constants (see the streaming generator's enum * path): sequential numeric values with literal overrides, or the * member's string literal for string enums. */ #lowerEnumMember(ed: EnumDeclaration, member: MemberExpression): i32 { let propName = accessKeyOf(this.func.model, member); let isStringEnum = ed.members.length > 0 && ed.members[0].initializer is StringLiteral; var nextValue = 0; for (let m in ed.members) { if (isStringEnum) { if (m.name.name == propName) { if (m.initializer is StringLiteral) { return this.#lowerStringLiteral(m.initializer as StringLiteral); } this.#bail('string enum member literal'); } } else { var currentValue = nextValue; let {initializer} = m; if (initializer != null) { // A number literal, or one with a minus sign, which parses as // a negation; the checker allows nothing else. var literal: NumberLiteral | null = null; var sign = 1; if (initializer is NumberLiteral) { literal = initializer; } else if (let UnaryExpression {operator, argument} = initializer) { if (operator == '-' && argument is NumberLiteral) { literal = argument; sign = -1; } } if (literal == null) { this.#bail('enum member initializer'); } else { let (ok, v) = parseI32WithHex(literal.raw); if (ok) { currentValue = sign * v; } else { this.#bail('enum member literal'); } } } if (m.name.name == propName) { return this.b.constI32(currentValue, new ValTypeI32() as ValType); } nextValue = currentValue + 1; } } this.#bail('enum member not found'); return -1; } /** * Calls a FixedArray/ImmutableArray extension method on an * array-typed receiver: the target is the arrayMethods entry whose * class specialization matches the element type (the generic * template when the element is a type parameter), mirroring the * streaming backend's matching. Receiver is argument 0. */ #lowerArrayExtensionCall(call: CallExpression, member: MemberExpression, elemType: Type, elemsMutable: boolean, givenReceiver: i32 = -1): i32 { let propName = accessKeyOf(this.func.model, member); // Overloaded extension methods (slice(start) / slice(start, end)) // register under mangled names; the checker records the selected // declaration per call node (member-lookup.md §5.2). var lookupName = propName; let chosenFt = this.func.model.getResolvedOverload(call); if (chosenFt != null) { lookupName = propName + getSignatureKey(chosenFt); } let isGenericElem = elemType is TypeParameterType; var target: WasmFunction | null = null; // Self-calls inside FixedArray/ImmutableArray's own specialized // methods (`this.slice(...)` from the Range wrappers): the // element type reads as erased there, but the target is simply // this function's own specialization. let {classType} = this.func; if (classType != null) { let ownCt = classType; if (isWellKnown(WellKnown.FixedArrayClass, ownCt.name, ownCt.sourcePath) || isWellKnown(WellKnown.ImmutableArrayClass, ownCt.name, ownCt.sourcePath)) { let key = getClassMethodKey(getClassKey(ownCt), lookupName); if (let (true, m) = this.wasm.classMethodMap.get(key)) { target = m; } } } // The receiver's element mutability picks its backing class: // methods of the other one take the other Wasm array type. let backingName = if (elemsMutable) "FixedArray" else "ImmutableArray"; if (target == null) { for (let f in this.wasm.arrayMethods) { if (f.classType == null || f.methodName == null || (f.methodName as String) != lookupName) { continue; } let fClass = f.classType as ClassType; if (fClass.name != backingName) { continue; } let fArgs = fClass.typeArguments; if (isGenericElem) { if (fArgs == null || fArgs.length == 0) { target = f; } } else if (fArgs != null && fArgs.length > 0) { if (getTypeKeyForSpecialization(elemType) == getTypeKeyForSpecialization(fArgs[0])) { target = f; } } } } if (target == null) { this.#bail('array extension method'); } if (target.intrinsic != null || target.typeParameters != null) { this.#bail('array extension shape'); } let sig = target.signature; if (sig.returns.length > 1) { this.#bail('array extension multi-value'); } if (sig.params.length != this.#liveArgCount(call.args) + 1) { this.#bail('array extension arity'); } for (let p in sig.params) { this.#requireSupported(p, 'array extension param'); } var resultType: ValType | null = null; if (sig.returns.length == 1) { this.#requireSupported(sig.returns[0], 'array extension result'); resultType = sig.returns[0]; } var receiver = if (givenReceiver >= 0) givenReceiver else this.#lowerExpressionCtx(member.object, sig.params[0]); receiver = this.#conformToSlot(receiver, sig.params[0], 'array extension receiver'); let args = growable([receiver]); var sIdx = 1; for (var i = 0; i < call.args.length; i += 1) { if (this.#dropZeroWidthArg(call.args[i])) { continue; } let paramVt = sig.params[sIdx]; sIdx += 1; var value = this.#lowerExpressionCtx(call.args[i], paramVt); value = this.#conformToSlot(value, paramVt, 'array extension argument'); args.push(value); } let funcRef = this.b.addRef(new IrRefFunc(target)); let raw = this.b.callFunc(funcRef, args, resultType); return this.#reassertCallResult(call, raw, resultType); } #threeValues(a: i32, b: i32, c: i32): GrowableArray { let out = growable([a, b, c]); return out; } #twoValues(a: i32, b: i32): GrowableArray { let result = growable([a, b]); return result; } /** * The shared context split-out lowering modules receive (see * ir/lowering-context.zena) — the recursive core as closures, * everything else direct. ONE instance per FunctionLowerer, built * lazily (field initializers cannot call methods): the closures * defer to `this` at call time, so early construction is safe. */ #cx(): LoweringContext { let cached = this.#cxCache; if (cached != null) { return cached; } let cx = new LoweringContext( this.b, this.wasm, this.func, ); cx.host = this; this.#cxCache = cx; return cx; } // Recursion entry points for the module-level seam functions at the // bottom of this file (the emission modules call back through them // across the import cycle). hostLowerExpr(e: Expression, want: ValType | null): i32 { if (want == null) { return this.#lowerExpression(e); } return this.#lowerExpressionCtx(e, want); } /** * True while lowering a `gen`/`async` body, before the split pass * turns it into a state machine. Wasm locals do not survive a * suspension, so a mutable variable cannot back a `try`-assigned * local here — those are celled instead. */ inSuspensionBody(): boolean { return this.presplitGenerator || this.presplitAsync; } hostLowerExprRaw(e: Expression): i32 { return this.#lowerExpressionDispatch(e); } hostLowerStmt(st: Statement): void { this.#lowerStatement(st); } hostVarDecl(vd: VariableDeclaration): void { this.#lowerVariableDeclaration(vd); } hostExactLiteralFields(e: Expression, d: RecordDispatchInfo): GrowableArray | null { return this.#exactLiteralFields(e, d); } hostPackHeldRecord(d: RecordDispatchInfo, values: GrowableArray): i32 { let record = this.#packRecord(d, values); this.#cx().explodedRecords[record] = new ExplodedRecord(d, values); return record; } hostThrowStmt(thr: ThrowExpression): void { this.#lowerThrowStatement(thr); } hostUnboxTuples(value: i32, t: Type): MultiValues | null { return this.#unboxTuples(value, t); } hostTupleTagTest(value: i32, tt: TupleType): i32 { return this.#tupleTagTest(value, tt); } hostMultiSource(init: Expression): MultiValues { return this.#lowerMultiSource(init); } hostRepackRecordUnion(ra: RecordUnionAdaptation, value: i32): i32 { return this.#applyRecordUnionAdaptation(ra, value); } hostSuspendOnFuture(ct: ClassType, future: i32): i32 { return this.#suspendOnFuture(ct, future); } /** * The value arm of a `??` over a maybe-form: packs it into the * interface the context asked for, when the checker recorded one on * this `??`. The node is remembered so the post-expression hook * leaves the merged value alone. */ hostCoalesceArmAdaptation(bin: BinaryExpression, value: i32): i32 { if (this.func.model.getInterfaceAdaptation(bin) == null) { return value; } let packed = this.#applyInterfaceAdaptation(bin, value); this.#coalesceArmsAdapted.add(bin.id as i32); return packed; } returnSlotVt(): ValType | null { return this.#returnSlotVt(); } #lowerStringLiteral(lit: StringLiteral): i32 { return lowerStringLiteral(this.#cx(), lit); } #lowerTaggedTemplate(expr: TaggedTemplateExpression): i32 { return lowerTaggedTemplate(this.#cx(), expr); } #lowerTemplateLiteral(tpl: TemplateLiteral): i32 { return lowerTemplateLiteral(this.#cx(), tpl); } /** * Caller-supplied default arguments: the checker injects each * omitted parameter's default expression into the call's argument * list, so args at index >= the ORIGINAL count are the callee's * own expressions. They lower here, in the caller, with the * callee's context bound: `this` is the call's receiver, the * callee's earlier parameters are the argument values already * computed, and the lexical class is the default's declaring class * (private-field defaults resolve against it). Null when the call * carries no injected defaults. */ #defaultArgInfo(call: CallExpression): DefaultArgInfo | null { let orig = this.func.model.getOriginalArgCount(call); if (orig == -1 || orig >= call.args.length) { return null; } let ftNode = this.func.model.getResolvedOverload(call) ?? this.func.model.getNodeType(call.callee); let paramSyms = if (ftNode is FunctionType) { ftNode.parameterSymbols } else { null }; let ownerT = this.func.model.getDefaultArgsOwner(call); let owner = if (ownerT is ClassType) { ownerT } else { null }; return new DefaultArgInfo(orig, paramSyms, owner); } /** * Lowers one call argument. Explicit arguments lower as written; * injected defaults lower under the callee's bindings (see * #defaultArgInfo), restored even when lowering throws so no * caller ever observes a leaked swap. */ #lowerArgWithDefaults(call: CallExpression, i: i32, paramVt: ValType, info: DefaultArgInfo | null, receiver: i32): i32 { if (info == null || i < info.originalCount) { let value = this.#lowerExpressionCtx(call.args[i], paramVt); if (info != null) { info.values.push(value); } return value; } let savedThis = this.#cx().thisValue; let savedClass = this.func.classType; let savedSyms = new Array(); let savedVals = new Array(); let syms = info.paramSymbols; if (syms != null) { for (var j = 0; j < info.values.length && j < syms.length; j += 1) { let symId = syms[j]; if (symId == noSymbolId) { continue; } savedSyms.push(symId); savedVals.push( if (let (true, v) = this.#cx().env.get(symId)) v else -1); this.#cx().env[symId] = info.values[j]; } } if (receiver >= 0) { this.#cx().thisValue = receiver; } if (info.owner != null) { this.func.classType = info.owner; } let value = try { this.#lowerExpressionCtx(call.args[i], paramVt) } catch (e) { this.#cx().thisValue = savedThis; this.func.classType = savedClass; this.#restoreEnvBindings(savedSyms, savedVals); throw e }; this.#cx().thisValue = savedThis; this.func.classType = savedClass; this.#restoreEnvBindings(savedSyms, savedVals); info.values.push(value); return value; } #restoreEnvBindings(syms: GrowableArray, vals: GrowableArray): void { for (var j = 0; j < syms.length; j += 1) { if (vals[j] < 0) { this.#cx().env.delete(syms[j]); } else { this.#cx().env[syms[j]] = vals[j]; } } } /** * Direct calls only: the callee is an identifier statically resolving * to a non-generic, non-adapted top-level function (or import) whose * signature is all scalars and whose arity matches the call exactly * (an arity mismatch means default-argument adaptation). Everything * else — methods, function values, generics, optional calls — bails. * * A void call returns its (valueless) instruction id; the checker * rejects void values in operand position, and if one ever slipped * through, the verifier's produces-no-value check fails loudly. */ #lowerCall(call: CallExpression, multiOut: GrowableArray | null = null): i32 { // `old(e)` in a postcondition: the value `e` had on entry. let oldValues = this.#oldValues; if (oldValues != null) { if (let (true, value) = oldValues.get(call.id as i32)) { return value; } } if (call.callee is MemberExpression) { return this.#lowerMethodCall(call, call.callee as MemberExpression, multiOut); } if (hasOptionalChainStep(call as Expression)) { if (multiOut != null) { this.#bail('optional multi-value call'); } return this.#lowerOptionalChainExpr(call as Expression); } if (!(call.callee is Identifier)) { // A computed callee — a call's result, a cast, a parenthesized // value — is an ordinary function value: call through its // closure pair (issue #448). A union-typed one dispatches by // ref.test, like any union call. if (this.func.model.getUnionCallCalleeType(call.callee) != null) { return this.#lowerUnionCall(call); } return this.#lowerClosureCall(call, multiOut); } // Prelude array builtins (__array_new_empty & co.) are checker- // registered values with no declaration and no compiled function; // streaming name-dispatches them to inline array instructions // (generateArrayIntrinsic). Same here — including the explicit- // type-argument form __array_new_empty(n), which must route // before the generic-call gate. let calleeName = (call.callee as Identifier).name; if (isArrayIntrinsic(calleeName)) { return lowerIntrinsic(this.#cx(), calleeName, call, multiOut); } if (this.func.model.getUnionCallCalleeType(call.callee) != null) { return this.#lowerUnionCall(call); } let sym = this.func.model.resolve(call.callee); if (sym == null) { this.#bail('unresolved callee'); } let resolved = sym.resolveTarget(); if (this.#cx().env.has(resolved.id)) { // A local variable holding a function value: closure call. return this.#lowerClosureCall(call, multiOut); } // Calls to @intrinsic declare functions (equals/hash from // zena:map and friends) never become compiled functions — // type-directed inline lowering, mirroring the streaming // backend's global intrinsics. let calleeFtNode = this.func.model.getNodeType(call.callee); let calleeFt = if (calleeFtNode is FunctionType) { calleeFtNode } else { null }; let intrDecl = findDeclaration(this.wasm, resolved, calleeFt); if (intrDecl != null) { if (let (true, intrName) = this.wasm.intrinsics.get(intrDecl.id)) { return lowerIntrinsic(this.#cx(), intrName, call, multiOut); } } // A generic call: the solved type arguments name a monomorphized // copy registered under the resolved symbol's name. A miss means // the specialization was never instantiated — bail, don't fall // through to the erased template. let solvedTargs = this.#solvedCallTypeArguments(call); if (solvedTargs != null) { let specKey = getSpecializedFunctionKey( resolved.name, solvedTargs); let specOpt = if (let (true, f) = this.wasm.specializedFunctions.get(specKey)) f else null; if (specOpt == null) { this.#bail('generic call'); } return this.#lowerSpecializedCall( call, specOpt, multiOut); } if (call.typeArguments != null) { // Explicit type arguments but no solution recorded on the // callee's checked type: not a shape lowering models. this.#bail('generic call'); } let chosenFt = this.func.model.getResolvedOverload(call) ?? calleeFt; var targetOpt: WasmFunction | null = null; if (chosenFt != null) { let overKey = getOverloadedFunctionKey(resolved.id, chosenFt); if (let (true, t) = this.wasm.overloadedFunctionMap.get(overKey)) { targetOpt = t; } } if (targetOpt == null) { if (let (true, t) = this.wasm.functionMap.get(resolved.id)) { targetOpt = t; } } if (targetOpt == null) { // Not a compiled function: a binding holding a closure value // (global function values still bail when the callee lowers). return this.#lowerClosureCall(call, multiOut); } return this.#lowerDirectCallTo(call, targetOpt, multiOut); } /** The direct-call tail shared by identifier callees and * namespace-import members: shape gates, argument lowering (with * caller-side defaults), and the call itself. */ #lowerDirectCallTo(call: CallExpression, target: WasmFunction, multiOut: GrowableArray | null): i32 { if (target.typeParameters != null || target.typeArguments != null || target.intrinsic != null || target.isFunctionAdaptation || target.isClosure || target.contextStruct != null || (target.captures ?? noCaptures).length > 0 || target.capturesThis || target.classType != null || target.structType != null) { this.#bail('callee shape'); } let sig = target.signature; if (sig.returns.length > 1 && multiOut == null) { this.#bail('callee multi-value return'); } let inlineParams = this.#inlineParamLayouts(this.#calleeTypeOf(call)); if (sig.params.length != this.#liveArgWidth(call.args, inlineParams)) { this.#bail('call arity adaptation'); } for (var i = 0; i < sig.params.length; i += 1) { this.#requireSupported(sig.params[i], 'call param type'); } let defaults = this.#defaultArgInfo(call); let explosion = target.explosion; if (defaults == null && explosion != null && explosion.forwarder === target && (this.#passesUnpackedArg(call.args, explosion) || this.#wantsResultFields(call, explosion))) { let coreArgs = this.#lowerExplodedArgs(call.args, explosion); let result = explosion.result; if (result == null) { return this.#emitDirectCall(call, explosion.core, coreArgs, multiOut); } return this.#callForFields(explosion.core, coreArgs, result); } let args = new Array(); var sIdx = 0; for (var i = 0; i < call.args.length; i += 1) { let inlineLayout = this.#inlineArgLayout(inlineParams, i); if (inlineLayout != null) { this.#pushInlineArg(call.args[i], inlineLayout, args); sIdx += inlineLayout.width(); continue; } if (this.#dropZeroWidthArg(call.args[i])) { continue; } let paramVt = sig.params[sIdx]; sIdx += 1; var value = this.#lowerArgWithDefaults(call, i, paramVt, defaults, -1); value = this.#conformToSlot(value, paramVt, 'call argument type'); args.push(value); } return this.#emitDirectCall(call, target, args, multiOut); } /** * Whether some argument for an exploded parameter is unpacked: a * record literal of exactly that parameter's record type, an exploded * parameter of this core passed on whole, or a call whose result is * held as its fields (`wasm.fieldsOnlyResultCalls`). Such a call goes * to the core, so the unpacked argument is never built. Its other * exploded arguments are heap records, read field by field here. */ #passesUnpackedArg(argExprs: GrowableArray, spec: ExplosionSpec): boolean { if (argExprs.length != spec.params.length) { return false; } for (var i = 0; i < spec.params.length; i += 1) { let d = spec.params[i]; let arg = argExprs[i]; if (d != null && (this.#isExactLiteral(arg, d) || this.#explodedRecordOf(arg, d) != null || (arg is CallExpression && this.wasm.fieldsOnlyResultCalls.has((arg as Node).id as i32)))) { return true; } } return false; } /** * Whether `call`'s record result is only read for its fields * (`wasm.fieldsOnlyResultCalls`) and `spec`'s core returns them. */ #wantsResultFields(call: CallExpression, spec: ExplosionSpec): boolean { return spec.result != null && this.wasm.fieldsOnlyResultCalls.has((call as Node).id as i32); } /** * Calls `core`, which returns the fields of a record of `result`'s * type, and holds them as that record (`explodedRecords`): the record * built from them dies unless some use needs it whole. */ #callForFields(core: WasmFunction, args: GrowableArray, result: RecordDispatchInfo): i32 { let returns = core.signature.returns; let funcRef = this.b.addRef(new IrRefFunc(core)); var fields = new Array(); if (returns.length == 1) { fields.push(this.b.callFunc(funcRef, args, returns[0])); } else { fields = this.b.callFuncMulti(funcRef, args, returns); } let record = this.#packRecord(result, fields); this.#cx().explodedRecords[record] = new ExplodedRecord(result, fields); return record; } /** * The field values of `arg` for `d` when it is a literal of exactly * `d`'s type (`#isExactLiteral`); else null. */ #exactLiteralFields(arg: Expression, d: RecordDispatchInfo): GrowableArray | null { if (!this.#isExactLiteral(arg, d)) { return null; } if (d.isTuple) { let bt = baseType(this.#substitutedNodeType(arg) as Type) as TupleType; return this.#lowerTupleLiteralFields(arg as TupleLiteral, bt, d.concreteType); } return this.#lowerRecordLiteralFields(arg as RecordLiteral, d); } /** * A record literal of exactly `d`'s record type, with no adaptation, * or, for a tuple's dispatch, a tuple literal of a type with its struct. */ #isExactLiteral(arg: Expression, d: RecordDispatchInfo): boolean { if (d.isTuple) { if (!(arg is TupleLiteral)) { return false; } let t = this.#substitutedNodeType(arg); if (t == null) { return false; } let bt = baseType(t); return bt is TupleType && this.#tupleStruct(bt) === d.concreteType; } if (!(arg is RecordLiteral)) { return false; } if (this.func.model.getRecordAdaptation(arg) != null) { return false; } let t = this.#substitutedNodeType(arg); if (t == null) { return false; } let bt = unwrapAliases(t); return bt is RecordType && this.#cx().recordDispatchOf(bt) === d; } /** * The field values of `arg` when it names an exploded parameter of * this core whose record has dispatch `d`; else null. */ #explodedRecordOf(arg: Expression, d: RecordDispatchInfo): ExplodedRecord | null { if (!(arg is Identifier)) { return null; } let sym = this.func.model.resolve(arg); if (sym == null) { return null; } if (let (true, value) = this.#cx().env.get(sym.id)) { if (let (true, exploded) = this.#cx().explodedRecords.get(value)) { if (exploded.dispatch === d) { return exploded; } } } return null; } /** * The core's arguments, left to right: an unpacked record argument as * the fields it already has, and a heap record as its fields read * through its getters. */ #lowerExplodedArgs(argExprs: GrowableArray, spec: ExplosionSpec, leading: i32 = 0): GrowableArray { let coreParams = spec.core.signature.params; let boxedParams = spec.forwarder.signature.params; let args = new Array(); for (var i = 0; i < argExprs.length; i += 1) { let arg = argExprs[i]; let d = spec.params[i]; if (d == null) { let paramVt = coreParams[leading + args.length]; let value = this.#lowerExpressionCtx(arg, paramVt); args.push(this.#conformToSlot(value, paramVt, 'call argument type')); continue; } let literalFields = this.#exactLiteralFields(arg, d); if (literalFields != null) { for (let value in literalFields) { args.push(value); } continue; } let exploded = this.#explodedRecordOf(arg, d); if (exploded != null) { for (let value in exploded.values) { args.push(value); } continue; } let boxedVt = boxedParams[leading + i]; let lowered = this.#lowerExpressionCtx(arg, boxedVt); // A call whose result is held as its fields passes them. if (let (true, held) = this.#cx().explodedRecords.get(lowered)) { if (held.dispatch === d) { for (let value in held.values) { args.push(value); } continue; } } let record = this.#conformToSlot(lowered, boxedVt, 'call argument type'); for (let field in d.fields) { let value = this.#cx().recordDispatchRead( d, field.name as String, record); args.push(this.#conformToSlot(value, coreParams[leading + args.length], 'exploded field type')); } } return args; } #emitDirectCall(call: CallExpression, target: WasmFunction, args: GrowableArray, multiOut: Array | null): i32 { let sig = target.signature; let funcRef = this.b.addRef(new IrRefFunc(target)); if (sig.returns.length > 1) { for (let r in sig.returns) { this.#requireSupported(r, 'call result type'); } let projs = this.b.callFuncMulti(funcRef, args, sig.returns); let outs = multiOut as GrowableArray; this.#pushMultiResults(call, projs, outs); return projs[0]; } var resultType: ValType | null = null; if (sig.returns.length == 1) { this.#requireSupported(sig.returns[0], 'call result type'); resultType = sig.returns[0]; } return this.b.callFunc(funcRef, args, resultType); } /** * The narrow slice of this lowerer that intrinsics.zena needs (its * module cannot import FunctionLowerer without a cycle). The eq and * hash lowerings ride along as closures: their implementations stay * on the lowerer because they share the primitive cluster with the * case-class ==/hashCode synthesizers. */ /** * Closure creation: ref.func of the (separately compiled, streaming * or ZIR) implementation, a context struct holding the captured * values, and the closure pair struct. Non-celled captures are BY * VALUE; celled captures pass the heap cell ref the environment * already holds — the context-field subtype check verifies each * capture matches what the closure body expects either way. */ #lowerFunctionExpression(fExpr: FunctionExpression): i32 { let classKey = if (this.func.classType != null) { getClassKey(this.func.classType as ClassType) } else { "" as ClassKey }; // Inside a specialized generic function (or method, or a closure // impl minted for one), the closure exists per instantiation: the // key's function axis is this function's own argument vector, // mirroring what RTA keyed when it walked this specialization's // body. The by-node fallback is the erasure's template and must // not stand in — its context fields are anyref, which a primitive // capture cannot inhabit. let closureKey = getClosureSpecializationKey(fExpr.id, classKey, this.func.typeParameters, this.func.typeArguments); var impl: WasmFunction | null = null; if (let (true, sc) = this.wasm.specializedClosures.get(closureKey)) { impl = sc; } else if (!closureKey.specializesFunction()) { if (let (true, cf) = this.wasm.closureFunctions.get(fExpr)) { impl = cf; } } if (impl == null) { this.#bail('closure not registered'); } // The pair struct is keyed by the IMPLEMENTATION's own signature — // deriving it from the checked node type drifted under // substitution, and the func slot must hold exactly (ref implSig) // anyway. let pairKey = getClosureKey(impl.signature.params, impl.signature.returns); let closureStruct = if (let (true, st) = this.wasm.closureStructs.get(pairKey)) st else null; if (closureStruct == null) { this.#bail('closure pair struct not found'); } if (impl.contextStruct == null) { this.#bail('closure without context struct'); } let contextStruct = impl.contextStruct as WasmStruct; var ctxVal = -1; if (contextStruct.fields.length > 0) { let ctxArgs = new Array(); var fieldIdx = 0; if (impl.capturesThis) { if (this.#cx().thisValue < 0) { this.#bail('closure captures this outside method'); } if (!isValSubtype(this.#valueType(this.#cx().thisValue), contextStruct.fields[0].type)) { this.#bail('closure this capture type'); } ctxArgs.push(this.#cx().thisValue); fieldIdx = 1; } let coalesced3 = impl.captures ?? noCaptures; for (let symId in coalesced3) { if (!this.#cx().env.has(symId)) { this.#bail('captured symbol not local'); } let v = this.#cx().env[symId]; if (!isValSubtype(this.#valueType(v), contextStruct.fields[fieldIdx].type)) { // Celled capture (context field wants a cell ref). this.#bail('celled capture'); } ctxArgs.push(v); fieldIdx += 1; } let ctxTypeRef = this.b.addRef(new IrRefType(contextStruct)); ctxVal = this.b.emit(IrOp.struct_new, new ValTypeRef(contextStruct) as ValType, this.b.pushArgList(ctxArgs), ctxTypeRef); } else { ctxVal = this.b.emit(IrOp.ref_null, new ValTypeStructRef(), -1, -1); } // The func slot's declared signature must be the implementation's // own signature (both come from the interned signature table). let slotSig = asWasmSignature(closureStruct.fields[0].type); if (slotSig == null || slotSig != impl.signature) { this.#bail('closure signature adaptation'); } let funcVal = this.b.emit(IrOp.ref_func, closureStruct.fields[0].type, -1, this.b.addRef(new IrRefFunc(impl))); let pairArgs = this.#twoValues(funcVal, ctxVal); return this.b.emit(IrOp.struct_new, new ValTypeRef(closureStruct) as ValType, this.b.pushArgList(pairArgs), this.b.addRef(new IrRefType(closureStruct))); } /** * Calls through a closure value {func: (ref $implSig), ctx: structref}: * the context rides as argument 0, and call_ref goes through the * exact-signature func field. Only the CALLING side is lowered — * closure creation and closure bodies still bail. */ /** * A callee whose static type is a UNION of function types: the * runtime value is one member's closure pair, found by ref.test * against each member's closure struct in order (streaming's * chain). The matching arm passes that member's ARITY worth of the * pre-evaluated arguments — extra arguments simply go unused, which * is the union-call adaptation semantics. No member matching traps. */ #lowerUnionCall(call: CallExpression): i32 { let ut = this.func.model.getUnionCallCalleeType(call.callee) as UnionType; // RAW read: the checker records the SELECTED member's type on the // callee node, but the runtime value may be any member — the // normal identifier read would re-assert the wrong closure struct. let callee = this.#lowerChainReceiver(call.callee); let args = new Array(); for (let a in call.args) { args.push(this.#lowerExpression(a)); } let callT = this.func.model.getNodeType(call); let isVoid = callT == null || isVoidOrNeverType(callT); var resultVt: ValType | null = null; if (!isVoid) { let vt = typeToValTypeWithSub(this.wasm, callT as Type, this.func); this.#requireSupported(vt, 'union call result'); resultVt = vt; } let merge = this.b.newBlock(); let resultParam = if (resultVt != null) { this.b.blockParam(merge, resultVt) } else { -1 }; for (let member in ut.types) { if (!(member is FunctionType)) { continue; } let cs = getClosureStruct(this.wasm, member); let csVt = new ValTypeRef(cs) as ValType; let hit = this.b.emit(IrOp.ref_test, new ValTypeI32(), callee, this.b.internType(csVt)); let callB = this.b.newBlock(); let nextB = this.b.newBlock(); this.b.brIf(hit, callB, this.#noArgs(), nextB, this.#noArgs()); this.b.switchTo(callB); let cv = this.b.emit(IrOp.ref_cast, csVt, callee, this.b.internType(csVt)); let sig = asWasmSignature(cs.fields[0].type); if (sig == null) { this.#bail('union member shape'); } if (sig.returns.length > 1) { this.#bail('union member shape'); } let arity = sig.params.length - 1; if (arity > args.length) { this.#bail('union call arity'); } let ctxVal = this.b.emit(IrOp.struct_get, cs.fields[1].type, cv, this.b.addRef(new IrRefField(cs, 'ctx'))); let callArgs = growable([ctxVal]); for (var i = 0; i < arity; i += 1) { if (!isValSubtype(this.#valueType(args[i]), sig.params[i + 1])) { this.#failConformance(args[i], sig.params[i + 1], 'union call argument type'); } callArgs.push(args[i]); } let fnVal = this.b.emit(IrOp.struct_get, cs.fields[0].type, cv, this.b.addRef(new IrRefField(cs, 'func'))); let memberResult = if (sig.returns.length == 1) { this.b.callRef(fnVal, callArgs, sig.returns[0]) } else { this.b.callRef(fnVal, callArgs, null) }; if (resultVt != null) { if (sig.returns.length != 1 || !isValSubtype(this.#valueType(memberResult), resultVt)) { this.#failConformance(memberResult, resultVt, 'union call result type'); } this.b.br(merge, this.#oneArg(memberResult)); } else { this.b.br(merge, this.#noArgs()); } this.b.switchTo(nextB); } // The checker proved the value is one of the members. this.b.unreachable(); this.b.switchTo(merge); if (resultParam >= 0) { return resultParam; } return this.b.constI32(0, new ValTypeI32()); } #lowerClosureCall(call: CallExpression, multiOut: GrowableArray | null = null): i32 { let callee = this.#lowerExpression(call.callee); return this.#lowerClosureCallOn(call, callee, multiOut); } /** * The callee-value core of a closure call. A function value with an * inline result returns several values, which go to `multiOut` as a * direct call's do. */ #lowerClosureCallOn(call: CallExpression, callee: i32, multiOut: GrowableArray | null = null): i32 { // The callee value's OWN type carries the authoritative closure // struct; recomputing it from the checked node type drifted // whenever substitution or nullability normalization disagreed // with the type the value actually flowed in with. let closureStruct = asWasmStruct(this.#valueType(callee)); if (closureStruct == null) { this.#bail('closure callee kind'); } if (closureStruct.fields.length != 2) { // Not a closure pair struct (e.g. a class instance): bail. this.#bail('closure callee shape'); } let sig = asWasmSignature(closureStruct.fields[0].type); if (sig == null) { this.#bail('closure callee shape'); } if (sig.returns.length > 1 && multiOut == null) { this.#bail('closure multi-value return'); } if (sig.params.length != this.#liveArgCount(call.args) + 1) { this.#bail('closure arity adaptation'); } let ctxRef = this.b.addRef(new IrRefField(closureStruct, 'ctx')); let ctxVal = this.b.emit(IrOp.struct_get, closureStruct.fields[1].type, callee, ctxRef); let args = growable([ctxVal]); var sIdx = 1; for (var i = 0; i < call.args.length; i += 1) { if (this.#dropZeroWidthArg(call.args[i])) { continue; } let paramVt = sig.params[sIdx]; sIdx += 1; this.#requireSupported(paramVt, 'closure param type'); var value = this.#lowerExpressionCtx(call.args[i], paramVt); value = this.#conformToSlot(value, paramVt, 'closure argument type'); args.push(value); } let funcRef = this.b.addRef(new IrRefField(closureStruct, 'func')); let funcVal = this.b.emit(IrOp.struct_get, closureStruct.fields[0].type, callee, funcRef); if (sig.returns.length > 1) { for (let r in sig.returns) { this.#requireSupported(r, 'closure result type'); } let projs = this.b.callRefMulti(funcVal, args, sig.returns); this.#pushMultiResults(call, projs, multiOut as GrowableArray); return projs[0]; } var resultType: ValType | null = null; if (sig.returns.length == 1) { this.#requireSupported(sig.returns[0], 'closure result type'); resultType = sig.returns[0]; } let result = this.b.callRef(funcVal, args, resultType); return this.#reassertCallResult(call, result, resultType); } #lowerAssignment(assign: AssignmentExpression): i32 { if (this.#isZeroWidthExpr(assign.left)) { // Assigning to a zero-width location (`this.value = v` where the // field is erased) stores nothing. Both sides still run for // their effects, and the assignment yields no value. if (assign.left is MemberExpression) { this.#lowerExpression((assign.left as MemberExpression).object); } this.#lowerExpression(assign.value); return NO_VALUE; } if (assign.left is MemberExpression) { return this.#lowerMemberAssign(assign); } if (assign.left is IndexExpression) { return this.#lowerIndexWrite(assign); } if (!(assign.left is Identifier)) { this.#bail('assignment target'); } let sym = this.func.model.resolve(assign.left); if (sym == null) { this.#bail('unresolved assignment target'); } let resolved = sym.resolveTarget(); if (!this.#cx().env.has(resolved.id)) { if (let (true, g) = this.wasm.globalMap.get(resolved.id)) { return this.#lowerGlobalAssign(assign, g); } this.#bail('non-local assignment target'); } // Writing an inline var replaces its slots (#813). The checker allows // neither a compound write nor one a handler protects, so no mirror // variable has to see it, and the assignment's value is never read. if (let (true, iv) = this.#cx().inlineVars.get(resolved.id)) { if (assign.operator != null) { this.#bail('compound assignment to an inline var'); } // Lowered before the env is named: an `if` or `match` value // replaces the env when it merges. let entry = iv.write(assign.value); this.#cx().env[resolved.id] = entry; return NO_VALUE; } let celled = this.#isCelled(resolved.id); var value = -1; let {operator} = assign; if (operator != null && operator == "??") { return this.#lowerNullishAssign(assign, resolved.id, celled); } let heldDispatch = this.#heldVarDispatch(resolved.id); if (heldDispatch != null && assign.operator == null) { value = this.#lowerHeldWrite(assign.value, heldDispatch); } else if (assign.operator == null) { var assignCtx: ValType | null = null; let targetBinding = this.func.model.getBindingType(resolved); if (targetBinding != null) { assignCtx = typeToValTypeWithSub(this.wasm, targetBinding, this.func); } value = this.#lowerExpressionCtx(assign.value, assignCtx); } else { // Compound assignment: current op= value. let current = if (celled) this.#cellRead(this.#cx().env[resolved.id]) else this.#cx().env[resolved.id]; value = this.#combineCompound(current, assign, assign.left); } if (!this.#cx().reachable) { return NO_VALUE; } if (heldDispatch == null) { if (let (true, storeSym) = this.#cx().symbols.get(resolved.id)) { let bt = this.func.model.getBindingType(storeSym); if (bt != null) { let bvt = typeToValTypeWithSub(this.wasm, bt, this.func); this.#requireSupported(bvt, 'binding type'); value = this.#conformToBinding(value, bvt); } } } // A store into a `var` owner binding releases the value it held // (ownership.md §Implicit drop): the checker marked the stores // where the binding is live, after the right-hand side ran. if (assign.operator == null && this.func.model.hasAssignmentRelease(assign)) { let bt = this.func.model.getBindingType(resolved); if (bt == null) { this.#bail('untyped owner binding'); } let old = if (celled) this.#cellRead(this.#cx().env[resolved.id]) else this.#cx().env[resolved.id]; emitDrop(this.#cx(), old, dropPlanOf(this.#cx(), bt)); } if (celled) { // Mutation goes through the heap cell; the env binding (the cell // ref) is invariant. this.#cellWrite(this.#cx().env[resolved.id], value); } else { this.#cx().env[resolved.id] = value; this.#cx().noteVarWrite(resolved.id, value); } return value; } /** * `x ??= rhs` on a local binding: the current value is kept when * non-null, else the right side evaluates and stores. The whole * expression's value is the binding's post-assignment value. Celled * bindings write through their heap cell; plain bindings merge the * two SSA values through a block param. */ #lowerNullishAssign(assign: AssignmentExpression, symId: SymbolId, celled: boolean): i32 { let current = if (celled) this.#cellRead(this.#cx().env[symId]) else this.#cx().env[symId]; if (!isRefValType(this.#valueType(current))) { this.#bail('nullish on non-reference'); } let isNull = this.b.emit( IrOp.ref_is_null, new ValTypeI32() as ValType, current, -1); let mergeSyms = this.#assignedEnvSyms(assign.value); if (!celled && !this.#containsSym(mergeSyms, symId)) { mergeSyms.push(symId); } let assignB = this.b.newBlock(); let skipB = this.b.newBlock(); this.b.brIf(isNull, assignB, this.#noArgs(), skipB, this.#noArgs()); let resultVt = this.#valueType(current); let saved = this.#copyEnv(); this.b.switchTo(assignB); let rhs = this.#lowerExpressionCtx(assign.value, resultVt); let mergeB = this.b.newBlock(); let mergeParams = this.#addParams(mergeB, mergeSyms); let resultParam = this.b.blockParam(mergeB, resultVt); if (this.#cx().reachable) { if (!isValSubtype(this.#valueType(rhs), resultVt)) { this.#bail('nullish assign type'); } if (celled) { this.#cellWrite(this.#cx().env[symId], rhs); } else { this.#cx().env[symId] = rhs; this.#cx().noteVarWrite(symId, rhs); } let assignArgs = this.#envValues(mergeSyms); assignArgs.push(rhs); this.b.br(mergeB, assignArgs); } this.#cx().env = this.#copyOf(saved); this.#cx().reachable = true; this.b.switchTo(skipB); let skipArgs = this.#envValues(mergeSyms); skipArgs.push(current); this.b.br(mergeB, skipArgs); this.#cx().env = saved; this.b.switchTo(mergeB); this.#applyParams(mergeSyms, mergeParams); this.#cx().reachable = true; return resultParam; } /** Whether `syms` already contains `symId`. */ #containsSym(syms: GrowableArray, symId: SymbolId): boolean { for (let s in syms) { if (s == symId) { return true; } } return false; } // -- Helpers --------------------------------------------------------- /** Operator -> IrOp table (see ir/operators.zena). */ #mapBinaryOp(operator: String, vt: ValType, unsigned: boolean): IrOp { return mapBinaryOp(this.#cx(), operator, vt, unsigned); } /** The wasm type of an already-lowered value. */ #valueType(value: i32): ValType { return this.#cx().valueTypeOf(value); } /** * Re-asserts a call result against the CALL NODE's checked type: * the checker often knows the result non-null even when the callee * signature stores it nullable. Void-typed calls and valueless * results pass through. */ #reassertCallResult(call: Expression, result: i32, resultType: ValType | null): i32 { if (resultType == null || this.b.body.typeId[result] < 0) { return result; } let nodeType = this.#refinableCallType(call); if (nodeType != null) { let want = typeToValTypeWithSub(this.wasm, nodeType, this.func); return this.#reassertNonNull(result, want); } return result; } /** * A call's checked type, when its one wasm result can be refined to * it: not `void`, and not an inline tuple union, which has no single * value type. Such a union lowers to one result when its payload is * `void`, `inline (true, _) | inline (false, T)` at `T = void`, and * that result is the tag, already exactly typed. */ #refinableCallType(call: Expression): Type | null { let nodeType = this.func.model.getNodeType(call); if (nodeType == null || baseType(nodeType as Type) is VoidType) { return null; } if (this.#isInlineTupleTyped(call)) { return null; } return nodeType; } /** * Whether a packed array read through `receiver` must sign-extend. * * The wasm array type cannot say: `array` and `array` are * both `(array (mut i8))`, and the extension is chosen by the * instruction. So the semantic element type of the receiver decides, * and unsigned is the safe default when it cannot be recovered — a * zero-extending read of a signed value is visibly wrong at the top * of the range, while the reverse invents negatives everywhere. */ #packedElementSigned(receiver: Expression): boolean { // MUST be the substituted type. Inside a generic like // `GrowableArray`, the buffer's checked type is the // unsubstituted `FixedArray`, whose element is a type parameter // and therefore never looks narrow — which silently selects the // unsigned read and turns every stored -1 into 255. let t = this.#substitutedNodeType(receiver); if (t == null) { return false; } let elem = arrayElementType(t); if (elem == null) { return false; } return isSignedNarrowIntType(elem); } /** * Re-asserts non-nullness when the checked type is non-null but the * value's storage type is nullable with the same heap target. Other * shape differences pass through unchanged — downstream subtype * checks bail on actual misuse. */ #reassertNonNull(value: i32, want: ValType): i32 { return this.#cx().reassertNonNull(value, want); } #exprValType(expr: Expression): ValType { return this.#cx().exprVt(expr); } /** * The value a record field read gives: the field's type, or for an * inline field of an inline record, its boxed form, which is what the * record holds (`#heldRecordFromLeaves`). */ #fieldReadValType(member: MemberExpression): ValType { let t = this.#substitutedNodeType(member); if (t != null && nestedInlineTuple(t) != null) { return typeToValTypeWithSub(this.wasm, boxedFieldType(t), this.func); } return this.#exprValType(member); } /** * Whether this expression is zero-width in THIS specialization — a * `void`/`never` value, which occupies no wasm slot. * * Substitution matters: the same `f(x)` in a generic template is a * real argument at `T = i32` and no argument at all at `T = void`. */ #isZeroWidthExpr(expr: Expression): boolean { let t = this.#substitutedNodeType(expr); if (t == null) { return false; } // A semantic test, NOT typeToValType: this runs on every argument // and receiver, including multi-value expressions (an inline tuple // from `map.get(k)`) that have no single wasm type at all and make // typeToValType throw. It agrees with mapParamTypeToWasmParams on // everything that can actually BE a parameter type — the one // divergence, a union containing void, cannot arise because void // absorbs in unions (`T | void` collapses to `void`). return isVoidOrNeverType(t); } /** * How many of these arguments occupy a wasm slot. * * Every call-site arity gate compares against THIS, not * `args.length`: the callee's signature dropped its zero-width * parameters, so the argument list has to be counted the same way. */ #liveArgCount(args: GrowableArray): i32 { var live = 0; for (var i = 0; i < args.length; i += 1) { if (!this.#isZeroWidthExpr(args[i])) { live += 1; } } return live; } /** * If this argument is zero-width, lower it for its side effects and * report that it contributes no operand. * * The evaluation is not skipped: `c.set(record(1))` still has to run * `record(1)`. Only the (nonexistent) value is dropped. */ #dropZeroWidthArg(arg: Expression): boolean { if (!this.#isZeroWidthExpr(arg)) { return false; } this.#lowerExpression(arg); return true; } /** * Whether this parameter is zero-width, and so contributes no wasm * slot — the callee-side mirror of #isZeroWidthExpr, and the exact * condition mapParamTypeToWasmParams drops the slot on. */ /** * How each parameter of `callee` is passed when it is inline * (docs/design/inline-parameters.md), or null when none is: then the * call lowers one value per argument, as it always has. */ #inlineParamLayouts(callee: Type | null): GrowableArray | null { if (callee == null || !(callee is FunctionType)) { return null; } var any = false; let params = new Array(); for (let p in callee.parameters) { let param = this.#inlineParamOf(p, this.func); if (param != null) { any = true; } params.push(param); } return if (any) params else null; } /** * `#inlineParamLayouts` for a function about to be called, from its own * parameters at its own specialization. */ #inlineLayoutsOf(f: WasmFunction): GrowableArray | null { let fn = f.node; let declared = if (fn is FunctionExpression) { fn.params } else if (fn is MethodDefinition) { fn.params } else { null }; if (declared == null) { return null; } var any = false; let params = new Array(); for (let p in declared) { let sym = f.model.resolve(p.name); let bt = if (sym != null) f.model.getBindingType(sym) else null; let param = if (bt != null) this.#inlineParamOf(bt, f) else null; if (param != null) { any = true; } params.push(param); } return if (any) params else null; } /** * How a parameter of type `t`, in `f`'s context, is passed when it is * inline: a tuple's slots, or a record's fields. Null for one value. */ #inlineParamOf(t: Type, f: WasmFunction): InlineParam | null { var curr = t; while (curr is TypeAliasType) { curr = curr.target; } if (!(curr is InlineTupleType) && !(curr is UnionType) && !(curr is InlineRecordType)) { return null; } let sub = substituteTypeParamsInFunctionContext(this.wasm, f, t); let layout = inlineParamLayoutOf(this.wasm, sub); if (layout != null) { return new InlineParam(layout, null, null); } let record = inlineRecordParamOf(sub); if (record == null) { return null; } let d = this.wasm.findRecordDispatch(recordWasmFields(this.wasm, record, f), sortedOptionalNames(record)); if (d == null) { return this.#bailInline('inline record parameter without a dispatch'); } // With inline fields, its values are its leaves (`inlineRecordFieldsLayout`). let leaves = inlineRecordFieldsLayout(this.wasm, record); return new InlineParam(leaves, d, record); } #bailInline(reason: String): InlineParam | null { this.#bail(reason); } /** The record `call` returns inline, or null. */ #inlineRecordOfCall(call: CallExpression): RecordType | null { let calleeType = this.#calleeTypeOf(call); if (!(calleeType is FunctionType)) { return null; } return inlineRecordParamOf(substituteTypeParamsInFunctionContext( this.wasm, this.func, calleeType.returnType)); } /** The dispatch of the record `call` returns inline, or null. */ #inlineRecordResultOfCall(call: CallExpression): RecordDispatchInfo | null { let record = this.#inlineRecordOfCall(call); if (record == null) { return null; } return this.wasm.findRecordDispatch(recordWasmFields(this.wasm, record, this.func), sortedOptionalNames(record)) ?? this.#bailDispatch('inline record result without a dispatch'); } #bailDispatch(reason: String): RecordDispatchInfo | null { this.#bail(reason); } /** The callee type a call's arguments are lowered against. */ #calleeTypeOf(call: CallExpression): Type | null { return this.func.model.getResolvedOverload(call) ?? this.func.model.getNodeType(call.callee); } /** Parameter `i`'s inline form, from `#inlineParamLayouts`. */ #inlineArgLayout(params: GrowableArray | null, i: i32): InlineParam | null { return if (params != null && i < params.length) params[i] else null; } /** * How many wasm values these arguments take: `#liveArgCount`, with an * argument for an inline parameter taking that parameter's slots or * fields. */ #liveArgWidth(args: GrowableArray, params: GrowableArray | null): i32 { if (params == null) { return this.#liveArgCount(args); } var live = 0; for (var i = 0; i < args.length; i += 1) { let param = this.#inlineArgLayout(params, i); if (param != null) { live += param.width(); } else if (!this.#isZeroWidthExpr(args[i])) { live += 1; } } return live; } /** * Lowers `arg` for an inline parameter, onto `args`: a tuple into its * slots, a record as its fields. A record literal writes them, a held * record passes its own, and a boxed record is read through its * getters, as an argument for an exploded parameter is. */ #pushInlineArg(arg: Expression, param: InlineParam, args: GrowableArray): void { let layout = param.layout; let leafRecord = param.record; if (layout != null && leafRecord != null) { for (let v in this.#leavesToSlots(leafRecord, layout, this.#recordExprLeaves(arg, leafRecord))) { args.push(v); } return; } if (layout != null) { for (let v in this.#lowerIntoSlots(arg, layout, layout.slots)) { args.push(v); } return; } let d = param.dispatch as RecordDispatchInfo; for (let v in this.#recordArgFields(arg, d, param.record as RecordType)) { args.push(v); } } /** The fields of `arg`, a record of `d`'s shape, in `d.fields` order. */ #recordArgFields(arg: Expression, d: RecordDispatchInfo, record: RecordType): GrowableArray { let literalFields = this.#exactLiteralFields(arg, d); if (literalFields != null) { return literalFields; } if (arg is IfExpression || arg is MatchExpression) { // The arms merge their fields, and the record the merge rebuilds // is held as them (MergeState.fields). this.#cx().resultFields = d; } let exploded = this.#explodedRecordOf(arg, d); if (exploded != null) { return exploded.values; } let boxedVt = typeToValTypeWithSub(this.wasm, record, this.func); let lowered = this.#lowerExpressionCtx(arg, boxedVt); if (let (true, held) = this.#cx().explodedRecords.get(lowered)) { if (held.dispatch === d) { return held.values; } } let value = this.#conformToSlot(lowered, boxedVt, 'inline record argument type'); let fields = new Array(); for (let field in d.fields) { let read = this.#cx().recordDispatchRead(d, field.name as String, value); fields.push(this.#conformToSlot(read, field.type, 'inline record field type')); } return fields; } /** * The slots of the inline parameter, `let` or `var` `e` names, or null. * An inline record binds a record held as its fields instead, which * every read of it answers from. */ #heldInline(e: Expression): MultiValues | null { if (!(e is Identifier)) { return null; } let sym = this.func.model.resolve(e); if (sym == null) { return null; } let id = sym.resolveTarget().id; if (let (true, source) = this.#cx().inlineBindings.get(id)) { return source; } if (this.#cx().inlineVars.has(id)) { let entry = this.#cx().env[id]; if (entry < NO_VALUE) { return this.#cx().heldSlots(entry); } } return null; } /** How a parameter of this function is passed when it is inline, or null. */ #inlineParamFor(p: Parameter): InlineParam | null { let sym = this.func.model.resolveDeclaration(p.name); if (sym == null) { return null; } let bt = this.func.model.getBindingType(sym); if (bt == null) { return null; } return this.#inlineParamOf(bt, this.func); } #isZeroWidthParam(p: Parameter): boolean { let sym = this.func.model.resolveDeclaration(p.name); if (sym == null) { return false; } let bt = this.func.model.getBindingType(sym); if (bt == null) { return false; } return typeToValTypeWithSub(this.wasm, bt, this.func) is ValTypeVoid; } /** * Requires the checked type of `expr` to be a signed scalar the subset * handles. Unsigned types share wasm representations but need _u * operator variants; they bail until lowering learns them. */ /** * The checked type of `expr` with the enclosing class/method context * substituted: inside a FixedArray body a node checker-typed `T` * (or `ArrayIterator`) reads as i32 (ArrayIterator) here. * Type-directed decisions — scalar-ness, signedness, operator-member * dispatch, and especially class-key resolution before * eraseTypeParameters — must use this rather than the raw node type: * the raw type can name a type parameter that is concrete in this * function, and erasing it unsubstituted lands on the _anyref * specialization where streaming (whose getNodeType substitutes) * uses the concrete one. */ #substitutedNodeType(expr: Expression): Type | null { return this.#cx().substitutedNodeType(expr); } /** Whether `expr`'s type is a multi-value shape — an inline tuple, * or a union of them like `Result`'s two arms. */ #isInlineTupleTyped(expr: Expression): boolean { let t = this.#substitutedNodeType(expr); if (t == null) { return false; } return inlineLayoutOf(this.wasm, t) != null; } /** Base type of #substitutedNodeType (unions unwrapped by baseType). */ #substitutedBaseType(expr: Expression): Type | null { let t = this.#substitutedNodeType(expr); if (t == null) { return null; } return baseType(t); } #requireScalar(vt: ValType, what: String): void { let ok = vt is ValTypeI32 || vt is ValTypeI64 || vt is ValTypeF32 || vt is ValTypeF64; if (!ok) { this.#bail(what); } } /** * Types values may HAVE in the lowered subset: scalars plus reference * types (concrete, anyref, eqref). Reference values only flow — the * operations on them are limited to null tests, identity, calls, and * assignment; anything else still bails at its own site. */ #requireSupported(vt: ValType, what: String): void { this.#cx().requireSupported(vt, what); } /** * Lowers an expression with a contextual wasm type: null literals take * the context's (nullable) type, and non-null results coerce is not * needed — wasm subtyping covers widening at use sites. */ #lowerExpressionCtx(expr: Expression, contextVt: ValType | null): i32 { if (expr is NullLiteral) { if (contextVt == null) { this.#bail('null without context'); } if (!isNullableAnyRef(contextVt) && !(contextVt is ValTypeExternRef)) { this.#bail('null into non-nullable context'); } return this.b.emit(IrOp.ref_null, contextVt, -1, -1); } return this.#lowerExpression(expr); } #assignedEnvSyms(node: Node): GrowableArray { return this.#cx().assignedEnvSyms(node); } #appendAssignedEnvSyms(node: Node, out: GrowableArray): void { this.#cx().appendAssignedEnvSyms(node, out); } /** Adds one block param per symbol, typed by the symbol's binding. */ #mergeArm(m: MergeState, value: i32, armKind: String = 'arm type'): void { this.#cx().mergeArm(m, value, armKind); } #finishMerge(m: MergeState, saved: HashMap): i32 { return this.#cx().finishMerge(m, saved); } #addParams(block: i32, syms: GrowableArray): GrowableArray { return this.#cx().addParams(block, syms); } #applyParams(syms: GrowableArray, params: GrowableArray): void { this.#cx().applyParams(syms, params); } #envValues(syms: GrowableArray): GrowableArray { return this.#cx().envValues(syms); } #copyEnv(): HashMap { return this.#cx().copyEnv(); } #copyOf(source: HashMap): HashMap { return this.#cx().copyOf(source); } #noArgs(): GrowableArray { return new Array(); } #oneArg(value: i32): GrowableArray { let result = growable([value]); return result; } /** * Value-conformance failure: a lowered value does not satisfy the * type its consumer requires. A scalar flowing into an any/eqref * context would need auto-boxing, which ZIR deliberately does not * implement (the `any` type is slated for removal) — that case * bails under its own permanent name so the histogram tracks it. */ #failConformance(value: i32, want: ValType, reason: String): never { this.#cx().failConformance(value, want, reason); throw new ZirUnsupported(reason); } #bail(reason: String): never { throw new ZirUnsupported(`${reason} @${this.func.name ?? '?'}`); } /** * `#bail` for spots that must sit in an i32 value position (match * arms producing a value id). The i32 never arrives — `fail` throws — * but a bare never-typed call cannot be a value arm yet * (elematic/zena#79). */ #bailValue(reason: String): i32 { this.#cx().fail(reason); } } // ============================================================================ // Recursion seams // ============================================================================ /** * The emission modules (templates, equality, patterns, operators, * control-flow, intrinsics) and the member machinery in * lowering-context.zena lower subexpressions by importing these * functions back across the module cycle. `cx.host` is the owning * FunctionLowerer — the shared core names this class across the * cycle's back edge. */ function hostOf(cx: LoweringContext) { return cx.host as FunctionLowerer; } /** Lower `e` as a value; a non-null `want` supplies the context type. */ export function lowerExpr(cx: LoweringContext, e: Expression, want: ValType | null): i32 { return hostOf(cx).hostLowerExpr(e, want); } /** True while lowering a presplit `gen`/`async` body. */ export function inSuspensionBody(cx: LoweringContext): boolean { return hostOf(cx).inSuspensionBody(); } /** The slot a `return` value conforms to, or null in a void (or * multi-value) function. */ export function returnSlotVt(cx: LoweringContext): ValType | null { return hostOf(cx).returnSlotVt(); } /** Adaptation-free expression lowering (see #lowerExpressionDispatch). */ export function lowerExprRaw(cx: LoweringContext, e: Expression): i32 { return hostOf(cx).hostLowerExprRaw(e); } /** Lower one statement. */ export function lowerStmt(cx: LoweringContext, st: Statement): void { hostOf(cx).hostLowerStmt(st); } /** Lower a variable declaration (patterns, celling, env binding). */ export function varDecl(cx: LoweringContext, vd: VariableDeclaration): void { hostOf(cx).hostVarDecl(vd); } /** * `e`'s fields through `d` when `e` is a record literal of exactly that * shape, lowered without building the record; null otherwise. */ export function exactLiteralFields(cx: LoweringContext, e: Expression, d: RecordDispatchInfo): GrowableArray | null { return hostOf(cx).hostExactLiteralFields(e, d); } /** * A record of shape `d` packed from `values` and held as them * (`explodedRecords`), so reads of it answer from the values and an * otherwise unused record dies. */ export function packHeldRecord(cx: LoweringContext, d: RecordDispatchInfo, values: GrowableArray): i32 { return hostOf(cx).hostPackHeldRecord(d, values); } /** Lower a throw in statement position. */ export function throwStmt(cx: LoweringContext, thr: ThrowExpression): void { hostOf(cx).hostThrowStmt(thr); } /** * A boxed tuple, or a union of them, read into the slots its inline form * occupies; null when `t` is not tuples. See `#unboxTuples`. */ export function unboxTuples(cx: LoweringContext, value: i32, t: Type): MultiValues | null { return hostOf(cx).hostUnboxTuples(value, t); } /** Whether `value` is a tuple of type `tt`, which starts with a literal. */ export function tupleTagTest(cx: LoweringContext, value: i32, tt: TupleType): i32 { return hostOf(cx).hostTupleTagTest(value, tt); } /** Whether a tuple's first element type is a literal that can tell it * apart from the other members of a union (a boolean or an integer). */ export function isTupleTag(t: Type): boolean { return t is BoolLiteralType || (t is NumLiteralType && isIntegerType(baseType(t))); } /** Lower a let-condition multi-value source. */ export function multiSource(cx: LoweringContext, init: Expression): MultiValues { return hostOf(cx).hostMultiSource(init); } /** * The values a multi-value source produced: one per wasm slot of * `layout`, or one per element when the source was a tuple literal or a * single value (`layout` null). */ export final class MultiValues { values: GrowableArray; layout: InlineLayout | null; new(this.values, this.layout); /** * One value per position, for a reader that can only see the members * `allowed` keeps (every member when null): the slot those members * use at the position, or NO_VALUE where none has a value. Where they * use different slots, the value is chosen by the discriminant * (`joinInlinePosition`). */ positions(cx: LoweringContext, allowed: GrowableArray | null): GrowableArray { let layout = this.layout; if (layout == null) { return this.values; } // One value per leaf of a member the reader can see: its paths are // the positions when nothing is nested. let rep = representativeMember(layout, allowed); let out = new Array(); for (let path in layout.memberPaths[rep]) { out.push(this.valueAt(cx, path, allowed)); } return out; } /** * The value at `path` for the members `allowed` keeps: the slot they * put it in, NO_VALUE where none has a value, or a join on the * discriminant where they use different slots. */ valueAt(cx: LoweringContext, path: String, allowed: GrowableArray | null): i32 { let layout = this.layout as InlineLayout; var s = layout.pathSlot(path, allowed); if (s == -1 && allowed != null) { // The kept members write `_` here. The slot the others use // holds its default for them, which is as good as any value. let other = layout.pathSlot(path, null); if (other >= 0) { s = other; } } if (s == -2) { return joinInlinePosition(cx, layout, this.values, path, allowed); } return if (s >= 0) this.values[s] else NO_VALUE; } /** * One value per element of a tuple pattern read against this source: * a nested tuple pattern at a nested inline position is spliced in, * so `(1, (x, y), _)` reads four leaves. `nodes` receives the element * each value is for. An element that stands for a nested inline * tuple whole gets the tuple boxed, which is what its binding holds. */ patternValues(cx: LoweringContext, elements: GrowableArray, allowed: GrowableArray | null, nodes: GrowableArray): GrowableArray { let layout = this.layout; if (layout == null || !layout.isNested()) { for (let el in elements) { nodes.push(el); } return this.positions(cx, allowed); } let rep = representativeMember(layout, allowed); let entries = pathElements(layout.originals[rep], elements); let out = new Array(); for (let entry in entries) { nodes.push(entry.node); let nested = entry.nested; let record = entry.record; out.push(if (nested != null && layout.isUnionAt(entry.path)) { boxInlineUnionAt(cx, this, entry.path, allowed) } else if (record != null) { boxInlineRecordAt(cx, this, entry.path, record, allowed) } else if (nested != null) { boxInlineAt(cx, this, entry.path, nested, allowed) } else { this.valueAt(cx, entry.path, allowed) }); } return out; } } /** * How an inline parameter is passed (docs/design/inline-parameters.md): * a tuple or tuple union as the slots of `layout`, or a record as the * fields of `dispatch`, in `dispatch.fields` order. */ export final class InlineParam { layout: InlineLayout | null; dispatch: RecordDispatchInfo | null; record: RecordType | null; new(this.layout, this.dispatch, this.record); /** How many wasm values it takes. */ width(): i32 { let layout = this.layout; if (layout != null) { return layout.slots.length; } return (this.dispatch as RecordDispatchInfo).fields.length; } } /** The member a reader stands for: the first one `allowed` keeps. */ export function representativeMember(layout: InlineLayout, allowed: GrowableArray | null): i32 { if (allowed != null) { for (var m = 0; m < allowed.length; m += 1) { if (allowed[m]) { return m; } } } return 0; } /** A tuple pattern's or literal's element with the path it stands for. */ export final class PathElement { node: Node | null; path: String; /** * The nested inline tuple the element stands for whole, or null when * it stands for one leaf. */ nested: InlineTupleType | null; /** * The record the element stands for when its position is an inline * record, whose fields are the leaves of `nested`. */ record: RecordType | null; new(this.node, this.path, this.nested, this.record); } /** * The elements of a tuple pattern or literal against `original`, each * with the path of what it stands for: a nested tuple pattern or literal * at a position where `original` nests an inline tuple contributes its * own elements, at `p.k`; any other element stands for its position. */ export function pathElements(original: InlineTupleType, elements: GrowableArray): GrowableArray { let out = new Array(); appendPathElements(original.elementTypes, elements, '', out); return out; } function appendPathElements(types: Array, elements: GrowableArray, prefix: String, out: GrowableArray): void { for (var p = 0; p < elements.length; p += 1) { let el = elements[p]; let path = `${prefix}${p}`; let inner = if (p < types.length) nestedInlineTuple(types[p]) else null; if (inner == null) { out.push(new PathElement(el, path, null, null)); continue; } // A record stands for its fields whole: a pattern or literal for it // is not a tuple to splice. let record = nestedInlineRecord(types[p]); let parts = if (record == null) tupleParts(el) else null; if (parts != null && parts.length == inner.elementTypes.length) { appendPathElements(inner.elementTypes, parts, `${path}.`, out); } else { out.push(new PathElement(el, path, inner, record)); } } } /** The boxed form of an inline tuple, nested inline tuples boxed too. */ function boxedTupleOf(t: InlineTupleType): TupleType { let elements = new Array(); for (let el in t.elementTypes) { let record = nestedInlineRecord(el); let inner = nestedInlineTuple(el); let union = nestedInlineUnion(el); elements.push(if (union != null) { boxedUnionOf(union) } else if (record != null) { record as Type } else if (inner != null) { boxedTupleOf(inner) as Type } else { el }); } return new TupleType(elements, null, 0); } /** * The members of a union of inline records a record literal can be: those * whose literal-typed fields its literal values agree with. */ function recordMembersMatching(layout: InlineLayout, literal: RecordLiteral): GrowableArray { let allowed = new Array(); for (let member in layout.originals) { var ok = true; for (let prop in literal.properties) { if (let PropertyAssignment {name, value} = prop) { let position = layout.fieldPosition(name.name); if (position < 0 || position >= member.elementTypes.length) { continue; } let memberType = member.elementTypes[position]; if (memberType is HoleType) { ok = false; } else if (memberType is BoolLiteralType && value is BooleanLiteral && memberType.value != value.value) { ok = false; } else if (memberType is NumLiteralType && value is NumberLiteral && !sameNumberLiteral(memberType.raw, value.raw)) { ok = false; } } } allowed.push(ok); } return allowed; } /** * The slots of the inline parameter or `let` `e` names, or null * (`FunctionLowerer.#heldInline`). */ export function heldInline(cx: LoweringContext, e: Expression): MultiValues | null { return hostOf(cx).hostHeldInline(e); } /** Whether two record types have the same fields, at the same types. */ function sameRecordShape(a: RecordType, b: RecordType): boolean { if (a.properties.size != b.properties.size) { return false; } for (let pair in a.properties) { if (!b.properties.has(pair.key) || typeToString(b.properties[pair.key]) != typeToString(pair.value)) { return false; } } return true; } /** Whether another member of `records` has member `m`'s fat pointer. */ function sharesFatPointer(host: FunctionLowerer, records: GrowableArray, m: i32): boolean { let mine = host.hostRecordDispatchFor(records[m]).fatPtrType; for (var other = 0; other < records.length; other += 1) { if (other != m && host.hostRecordDispatchFor(records[other]).fatPtrType === mine) { return true; } } return false; } /** The boxed form of an inline union's members: a union of boxed tuples. */ function boxedUnionOf(members: GrowableArray): Type { let boxed = new Array(); for (let member in members) { boxed.push(boxedTupleOf(member)); } return if (boxed.length == 1) boxed[0] else new UnionType(boxed); } /** * The element at `path` of `t`, through its nested inline tuples, or null. * "1.0" is element 0 of the tuple at position 1. */ function elementAtPath(t: InlineTupleType, path: String): Type | null { var current: InlineTupleType | null = t; var found: Type | null = null; for (let segment in path.split('.')) { let tuple = current; if (tuple == null) { return null; } let k = if (let (true, n) = parseI32(segment)) n else -1; if (k < 0 || k >= tuple.elementTypes.length) { return null; } found = tuple.elementTypes[k]; current = nestedInlineTuple(tuple.elementTypes[k]); } return found; } /** * The members of `layout` that `allowed` keeps (every one when null) and * that put `shape` at `path`. */ function membersWithShapeAt(layout: InlineLayout, path: String, shape: Type, allowed: GrowableArray | null): GrowableArray { let out = new Array(); for (var m = 0; m < layout.originals.length; m += 1) { out.push((allowed == null || allowed[m]) && elementAtPath(layout.originals[m], path) === shape); } return out; } /** * The distinct tuples the members `allowed` keeps put at `path`, a * position where a nested inline union is distributed. */ function shapesAt(layout: InlineLayout, path: String, allowed: GrowableArray | null): GrowableArray { let shapes = new Array(); for (var m = 0; m < layout.originals.length; m += 1) { if (allowed != null && !allowed[m]) { continue; } let el = elementAtPath(layout.originals[m], path); let tuple = if (el != null) nestedInlineTuple(el) else null; if (el != null && tuple != null) { var seen = false; for (let s in shapes) { if ((s as Type) === el) { seen = true; } } if (!seen) { shapes.push(el as InlineTupleType); } } } return shapes; } /** * Whether the value in `values` is member `m` of `layout`: its literal at * each discriminant path compared with the member's, the results anded. * 1 when the layout has no discriminant. */ export function memberTest(cx: LoweringContext, layout: InlineLayout, values: GrowableArray, m: i32): i32 { let i32Vt = new ValTypeI32() as ValType; var cond = -1; for (let path in layout.discriminantPaths) { let slot = layout.slotAt(m, path); let i = pathIndex(layout.memberPaths[m], path); if (slot < 0 || i < 0) { continue; } let tag = values[slot]; let test = match (layout.members[m].elementTypes[i]) { case BoolLiteralType {value}: if (value) tag else cx.b.emit(IrOp.i32_eqz, i32Vt, tag, -1) case NumLiteralType {raw}: { if (let (true, parsed) = parseI64OrU64Literal(raw)) { cx.b.binary(IrOp.i32_eq, i32Vt, tag, cx.b.constI32(parsed as i32, i32Vt)) } else { bail(cx, 'unparseable discriminant literal') } } case _: bail(cx, 'inline union discriminant type') }; cond = if (cond == -1) test else cx.b.binary(IrOp.i32_and, i32Vt, cond, test); } return if (cond == -1) cx.b.constI32(1, i32Vt) else cond; } /** The paths of an inline tuple's leaves, relative to it: "0", "1.0"... */ function collectLeafPaths(t: InlineTupleType, prefix: String, out: GrowableArray): void { for (var k = 0; k < t.elementTypes.length; k += 1) { let inner = nestedInlineTuple(t.elementTypes[k]); if (inner != null) { collectLeafPaths(inner, `${prefix}${k}.`, out); } else { out.push(`${prefix}${k}`); } } } /** The elements of a tuple pattern or a spread-free tuple literal, or * null for anything else. */ function tupleParts(node: Node | null): GrowableArray | null { var n = node; if (let BindingPattern {pattern} = n) { n = pattern as Node; } if (let TuplePattern {elements} = n) { return elements; } if (let TupleLiteral {elements, spreadIndices} = n) { if (spreadIndices != null) { return null; } let parts = new Array(); for (let e in elements) { parts.push(e); } return parts; } return null; } /** An inline value boxed: the struct (or union) of its boxed form. */ export function boxInline(cx: LoweringContext, source: MultiValues): i32 { return hostOf(cx).hostBoxInline(source); } /** * The values of `e` in the slots `slotVts` of the inline layout `layout` * (`FunctionLowerer.#lowerIntoSlots`). */ export function lowerIntoSlots(cx: LoweringContext, e: Expression, layout: InlineLayout | null, slotVts: GrowableArray): GrowableArray { return hostOf(cx).hostLowerIntoSlots(e, layout, slotVts); } /** * Returns `e`, an arm of a returned `if` or `match` whose value is an * inline value, from the arm: its values go to the function's results, * through any pending finalizer. */ export function returnInlineTail(cx: LoweringContext, e: Expression): void { hostOf(cx).hostReturnInline(e); } /** * The nested inline tuple at `path` of `source`, boxed: the struct of * its boxed form, built from its leaves. */ export function boxInlineAt(cx: LoweringContext, source: MultiValues, path: String, inner: InlineTupleType, allowed: GrowableArray | null): i32 { return hostOf(cx).hostBoxInlineAt(source, path, inner, allowed); } /** * The nested inline union at `path` of `source`, boxed: which member it * holds is told by its own literal, as for a whole inline union. */ export function boxInlineUnionAt(cx: LoweringContext, source: MultiValues, path: String, allowed: GrowableArray | null): i32 { return hostOf(cx).hostBoxInlineUnionAt(source, path, allowed); } /** * The nested inline record at `path` of `source`: a record built from its * leaves and held as them, so reads take the leaves and it dies unless a * use needs it whole. */ export function boxInlineRecordAt(cx: LoweringContext, source: MultiValues, path: String, record: RecordType, allowed: GrowableArray | null): i32 { return hostOf(cx).hostBoxInlineRecordAt(source, path, record, allowed); } /** Bail with the lowering-side message shape (reason @function). */ function bail(cx: LoweringContext, reason: String): never { throw new ZirUnsupported(`${reason} @${cx.func.name ?? '?'}`); } /** * The value at position `p` when the members `allowed` keeps put it in * different slots: `let (ok, v) = r` on `(true, String) | (false, Error)`. * The discriminant's literal says which member `r` is, so each slot's * value branches to a join block when the discriminant names a member * that uses it. The join holds `anyref`, and the binding casts it down * like any erased slot. */ function joinInlinePosition(cx: LoweringContext, layout: InlineLayout, values: GrowableArray, path: String, allowed: GrowableArray | null): i32 { if (layout.discriminantPaths.length == 0) { bail(cx, 'inline union without a discriminant'); } let slots = new Array(); let elementTypes = new Array(); for (var m = 0; m < layout.members.length; m += 1) { if (allowed != null && !allowed[m]) { continue; } let s = layout.slotAt(m, path); if (s >= 0) { elementTypes.push(layout.members[m].elementTypes[pathIndex(layout.memberPaths[m], path)]); if (!slots.contains(s)) { slots.push(s); } } } for (let s in slots) { if (!isRefValType(layout.slots[s])) { bail(cx, 'inline union position of mixed value types'); } } // The type the checker gives the position: the union of the members' // element types there. let joinVt = typeToValType(cx.wasm, new UnionType(elementTypes)); for (let s in slots) { if (!isValSubtype(layout.slots[s], joinVt)) { bail(cx, 'inline union position of mixed value types'); } } let i32Vt = new ValTypeI32() as ValType; let join = cx.b.newBlock(); let result = cx.b.blockParam(join, joinVt); for (var i = 0; i < slots.length - 1; i += 1) { // Whether the discriminant names a member that uses this slot. var cond = -1; for (var m = 0; m < layout.members.length; m += 1) { if ((allowed != null && !allowed[m]) || layout.slotAt(m, path) != slots[i]) { continue; } let test = memberTest(cx, layout, values, m); cond = if (cond == -1) test else cx.b.binary(IrOp.i32_or, i32Vt, cond, test); } let next = cx.b.newBlock(); cx.b.brIf(cond, join, growable([values[slots[i]]]), next, new Array()); cx.b.switchTo(next); } cx.b.br(join, growable([values[slots[slots.length - 1]]])); cx.b.switchTo(join); return result; } /** * Which members of an inline union a tuple literal or tuple pattern can * be: a literal element (`true`, `1`) rules out every member whose * element at that position is a different literal or `_`. */ export function inlineMembersMatching(layout: InlineLayout, elements: GrowableArray): GrowableArray { let allowed = new Array(); // Literals stand at positions of the members with their nested inline // tuples, a nested tuple pattern or literal reaching into one, so a // nested union's literal picks its members too. for (let member in layout.originals) { allowed.push(elementsMatch(member, elements)); } return allowed; } function elementsMatch(member: InlineTupleType, elements: GrowableArray): boolean { for (var p = 0; p < elements.length && p < member.elementTypes.length; p += 1) { var el = elements[p]; if (el is LiteralPattern) { el = el.value; } let memberType = member.elementTypes[p]; if (el is BooleanLiteral) { if (memberType is HoleType) { return false; } else if (memberType is BoolLiteralType && memberType.value != el.value) { return false; } } else if (el is NumberLiteral) { if (memberType is HoleType) { return false; } else if (memberType is NumLiteralType && !sameNumberLiteral(memberType.raw, el.raw)) { return false; } } else { let inner = nestedInlineTuple(memberType); let parts = tupleParts(el); if (inner != null && parts != null && nestedInlineRecord(memberType) == null && !elementsMatch(inner, parts)) { return false; } } } return true; } /** * Which members of `layout`'s union a value of `member` can become: the * ones whose literal elements agree with its literal elements. */ export function inlineMembersLike(layout: InlineLayout, member: InlineTupleType): GrowableArray { let allowed = new Array(); for (let other in layout.originals) { allowed.push(literalsAgree(member, other)); } return allowed; } /** Whether two tuples' literal elements agree, nested inline tuples too. */ function literalsAgree(a: InlineTupleType, b: InlineTupleType): boolean { let n = if (a.elementTypes.length < b.elementTypes.length) { a.elementTypes.length } else { b.elementTypes.length }; for (var p = 0; p < n; p += 1) { let x = a.elementTypes[p]; let y = b.elementTypes[p]; if (x is BoolLiteralType && y is BoolLiteralType && x.value != y.value) { return false; } else if (x is NumLiteralType && y is NumLiteralType && !sameNumberLiteral(x.raw, y.raw)) { return false; } let ix = if (nestedInlineRecord(x) == null) nestedInlineTuple(x) else null; let iy = if (nestedInlineRecord(y) == null) nestedInlineTuple(y) else null; if (ix != null && iy != null && !literalsAgree(ix, iy)) { return false; } } return true; } function sameNumberLiteral(a: String, b: String): boolean { return sameNumericLiteralRaw(a, b); } /** * Repacks a record-union value to one record shape, picking the member * at run time (patterns, which destructure the repacked value). */ export function repackRecordUnion(cx: LoweringContext, ra: RecordUnionAdaptation, value: i32): i32 { return hostOf(cx).hostRepackRecordUnion(ra, value); } /** The position of a tuple pattern's `...rest` element, or -1. */ export function tupleRestIndexOf(tp: TuplePattern): i32 { for (var i = 0; i < tp.elements.length; i += 1) { if (tp.elements[i] is RestPattern) { return i; } } return -1; } /** Binds a tuple pattern with a `...rest` against a boxed tuple (patterns). */ export function bindTupleWithRest(cx: LoweringContext, tp: TuplePattern, value: i32): void { hostOf(cx).hostBindTupleWithRest(tp, value); } /** The value a record pattern's `...rest` binds (patterns). */ export function recordRestValue(cx: LoweringContext, rest: Node, value: i32): i32 { return hostOf(cx).hostRecordRestValue(rest, value); } /** * Suspends on `future`, typed `ct`, and resumes with its settled value: * an `await` with no expression behind it, as a `for await` makes on a * Pending step (control-flow). */ export function suspendOnFuture(cx: LoweringContext, ct: ClassType, future: i32): i32 { return hostOf(cx).hostSuspendOnFuture(ct, future); } /** Pack the value arm of a `??` over a maybe-form (control-flow). */ export function coalesceArmAdaptation(cx: LoweringContext, bin: BinaryExpression, value: i32): i32 { return hostOf(cx).hostCoalesceArmAdaptation(bin, value); } /** Fused primitive optional-chain `chain ?? default` lowering (control-flow). */ export function lowerOptionalChainCoalesce(cx: LoweringContext, bin: BinaryExpression, overallVt: ValType): i32 { return hostOf(cx).hostOptionalChainCoalesce(bin, overallVt); } function getNodeMemberName(model: SemanticModel, node: Node): String { return memberKeyOf(model, node); }