/// This function is the main loop of `Sema` and it can be used in two different ways: /// * The traditional way where there are N breaks out of the block and peer type /// resolution is done on the break operands. In this case, the `zir.Inst.Index` /// part of the return value will be `undefined`, and callsites should ignore it, /// finding the block result value via the block scope. /// * The "flat" way. There is only 1 break out of the block, and it is with a `break_flat` /// instruction. In this case, the `zir.Inst.Index` part of the return value will be /// the block result value. No block scope needs to be created for this strategy. pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Index) !zir.Inst.Index { // No tracy calls here, to avoid interfering with the tail call mechanism. const map = block.sema.inst_map; const tags = block.sema.code.instructions.items(.tag); // We use a while(true) loop here to avoid a redundant way of breaking out of // the loop. The only way to break out of the loop is with a `noreturn` // instruction. // TODO: As an optimization, make sure the codegen for these switch prongs // directly jump to the next one, rather than detouring through the loop // continue expression. Related: https://github.com/ziglang/zig/issues/8220 var i: usize = 0; while (true) : (i += 1) { const inst = body[i]; map[inst] = switch (tags[inst]) { .elided => continue, .add => try sema.zirArithmetic(block, inst), .addwrap => try sema.zirArithmetic(block, inst), .alloc => try sema.zirAlloc(block, inst), .alloc_inferred => try sema.zirAllocInferred(block, inst, Type.initTag(.inferred_alloc_const)), .alloc_inferred_mut => try sema.zirAllocInferred(block, inst, Type.initTag(.inferred_alloc_mut)), .alloc_mut => try sema.zirAllocMut(block, inst), .array_cat => try sema.zirArrayCat(block, inst), .array_mul => try sema.zirArrayMul(block, inst), .array_type => try sema.zirArrayType(block, inst), .array_type_sentinel => try sema.zirArrayTypeSentinel(block, inst), .as => try sema.zirAs(block, inst), .as_node => try sema.zirAsNode(block, inst), .@"asm" => try sema.zirAsm(block, inst, false), .asm_volatile => try sema.zirAsm(block, inst, true), .bit_and => try sema.zirBitwise(block, inst), .bit_not => try sema.zirBitNot(block, inst), .bit_or => try sema.zirBitwise(block, inst), .bitcast => try sema.zirBitcast(block, inst), .bitcast_ref => try sema.zirBitcastRef(block, inst), .bitcast_result_ptr => try sema.zirBitcastResultPtr(block, inst), .block => try sema.zirBlock(block, inst, false), .block_comptime => try sema.zirBlock(block, inst, true), .bool_not => try sema.zirBoolNot(block, inst), .bool_and => try sema.zirBoolOp(block, inst, false), .bool_or => try sema.zirBoolOp(block, inst, true), .bool_br_and => try sema.zirBoolBr(block, inst, false), .bool_br_or => try sema.zirBoolBr(block, inst, true), .call => try sema.zirCall(block, inst, .auto), .call_compile_time => try sema.zirCall(block, inst, .compile_time), .call_none => try sema.zirCallNone(block, inst), .cmp_eq => try sema.zirCmp(block, inst, .eq), .cmp_gt => try sema.zirCmp(block, inst, .gt), .cmp_gte => try sema.zirCmp(block, inst, .gte), .cmp_lt => try sema.zirCmp(block, inst, .lt), .cmp_lte => try sema.zirCmp(block, inst, .lte), .cmp_neq => try sema.zirCmp(block, inst, .neq), .coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst), .@"const" => try sema.zirConst(block, inst), .decl_ref => try sema.zirDeclRef(block, inst), .decl_val => try sema.zirDeclVal(block, inst), .deref_node => try sema.zirDerefNode(block, inst), .div => try sema.zirArithmetic(block, inst), .elem_ptr => try sema.zirElemPtr(block, inst), .elem_ptr_node => try sema.zirElemPtrNode(block, inst), .elem_val => try sema.zirElemVal(block, inst), .elem_val_node => try sema.zirElemValNode(block, inst), .enum_literal => try sema.zirEnumLiteral(block, inst), .enum_literal_small => try sema.zirEnumLiteralSmall(block, inst), .err_union_code => try sema.zirErrUnionCode(block, inst), .err_union_code_ptr => try sema.zirErrUnionCodePtr(block, inst), .err_union_payload_safe => try sema.zirErrUnionPayload(block, inst, true), .err_union_payload_safe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, true), .err_union_payload_unsafe => try sema.zirErrUnionPayload(block, inst, false), .err_union_payload_unsafe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, false), .error_set => try sema.zirErrorSet(block, inst), .error_union_type => try sema.zirErrorUnionType(block, inst), .error_value => try sema.zirErrorValue(block, inst), .field_ptr => try sema.zirFieldPtr(block, inst), .field_ptr_named => try sema.zirFieldPtrNamed(block, inst), .field_val => try sema.zirFieldVal(block, inst), .field_val_named => try sema.zirFieldValNamed(block, inst), .floatcast => try sema.zirFloatcast(block, inst), .fn_type => try sema.zirFnType(block, inst, false), .fn_type_cc => try sema.zirFnTypeCc(block, inst, false), .fn_type_cc_var_args => try sema.zirFnTypeCc(block, inst, true), .fn_type_var_args => try sema.zirFnType(block, inst, true), .import => try sema.zirImport(block, inst), .indexable_ptr_len => try sema.zirIndexablePtrLen(block, inst), .int => try sema.zirInt(block, inst), .int_type => try sema.zirIntType(block, inst), .intcast => try sema.zirIntcast(block, inst), .is_err => try sema.zirIsErr(block, inst), .is_err_ptr => try sema.zirIsErrPtr(block, inst), .is_non_null => try sema.zirIsNull(block, inst, true), .is_non_null_ptr => try sema.zirIsNullPtr(block, inst, true), .is_null => try sema.zirIsNull(block, inst, false), .is_null_ptr => try sema.zirIsNullPtr(block, inst, false), .merge_error_sets => try sema.zirMergeErrorSets(block, inst), .mod_rem => try sema.zirArithmetic(block, inst), .mul => try sema.zirArithmetic(block, inst), .mulwrap => try sema.zirArithmetic(block, inst), .negate => @panic("TODO"), .negate_wrap => @panic("TODO"), .optional_payload_safe => try sema.zirOptionalPayload(block, inst, true), .optional_payload_safe_ptr => try sema.zirOptionalPayloadPtr(block, inst, true), .optional_payload_unsafe => try sema.zirOptionalPayload(block, inst, false), .optional_payload_unsafe_ptr => try sema.zirOptionalPayloadPtr(block, inst, false), .optional_type => try sema.zirOptionalType(block, inst), .optional_type_from_ptr_elem => try sema.zirOptionalTypeFromPtrElem(block, inst), .param_type => try sema.zirParamType(block, inst), .ptr_type => try sema.zirPtrType(block, inst), .ptr_type_simple => try sema.zirPtrTypeSimple(block, inst), .ptrtoint => try sema.zirPtrtoint(block, inst), .ref => try sema.zirRef(block, inst), .ret_ptr => try sema.zirRetPtr(block, inst), .ret_type => try sema.zirRetType(block, inst), .shl => try sema.zirShl(block, inst), .shr => try sema.zirShr(block, inst), .slice_end => try sema.zirSliceEnd(block, inst), .slice_sentinel => try sema.zirSliceSentinel(block, inst), .slice_start => try sema.zirSliceStart(block, inst), .str => try sema.zirStr(block, inst), .sub => try sema.zirArithmetic(block, inst), .subwrap => try sema.zirArithmetic(block, inst), .typeof => try sema.zirTypeof(block, inst), .typeof_peer => try sema.zirTypeofPeer(block, inst), .xor => try sema.zirBitwise(block, inst), // TODO //.switchbr => try sema.zirSwitchBr(block, inst, false), //.switchbr_ref => try sema.zirSwitchBr(block, inst, true), //.switch_range => try sema.zirSwitchRange(block, inst), // Instructions that we know to *always* be noreturn based solely on their tag. // These functions match the return type of analyzeBody so that we can // tail call them here. .condbr => return sema.zirCondbr(block, inst), .@"break" => return sema.zirBreak(block, inst), .break_void_tok => return sema.zirBreakVoidTok(block, inst), .break_flat => return sema.code.instructions.items(.data)[inst].un_node.operand, .compile_error => return sema.zirCompileError(block, inst), .ret_coerce => return sema.zirRetTok(block, inst, true), .ret_node => return sema.zirRetNode(block, inst), .ret_tok => return sema.zirRetTok(block, inst, false), .@"unreachable" => return sema.zirUnreachable(block, inst), .loop => return sema.zirLoop(block, inst), // Instructions that we know can *never* be noreturn based solely on // their tag. We avoid needlessly checking if they are noreturn and // continue the loop. // We also know that they cannot be referenced later, so we avoid // putting them into the map. .breakpoint => { try sema.zirBreakpoint(block, inst); continue; }, .dbg_stmt_node => { try sema.zirDbgStmtNode(block, inst); continue; }, .ensure_err_payload_void => { try sema.zirEnsureErrPayloadVoid(block, inst); continue; }, .ensure_result_non_error => { try sema.zirEnsureResultNonError(block, inst); continue; }, .ensure_result_used => { try sema.zirEnsureResultUsed(block, inst); continue; }, .compile_log => { try sema.zirCompileLog(block, inst); continue; }, .set_eval_branch_quota => { try sema.zirSetEvalBranchQuota(block, inst); continue; }, .store => { try sema.zirStore(block, inst); continue; }, .store_to_block_ptr => { try sema.zirStoreToBlockPtr(block, inst); continue; }, .store_to_inferred_ptr => { try sema.zirStoreToInferredPtr(block, inst); continue; }, .resolve_inferred_alloc => { try sema.zirResolveInferredAlloc(block, inst); continue; }, }; if (map[inst].ty.isNoReturn()) return always_noreturn; } }