| 1 |
// https://github.com/ziglang/zig/raw/master/src-self-hosted/zir_sema.zig |
| 2 |
// |
| 3 |
//! This file operates on a `Module` instance, transforming untyped ZIR |
| 4 |
//! instructions into semantically-analyzed IR instructions. It does type |
| 5 |
//! checking, comptime control flow, and safety-check generation. This is the |
| 6 |
//! the heart of the Zig compiler. |
| 7 |
//! When deciding if something goes into this file or into Module, here is a |
| 8 |
//! guiding principle: if it has to do with (untyped) ZIR instructions, it goes |
| 9 |
//! here. If the analysis operates on typed IR instructions, it goes in Module. |
| 10 |
|
| 11 |
const std = @import("std"); |
| 12 |
const mem = std.mem; |
| 13 |
const Allocator = std.mem.Allocator; |
| 14 |
const Value = @import("value.zig").Value; |
| 15 |
const Type = @import("type.zig").Type; |
| 16 |
const TypedValue = @import("TypedValue.zig"); |
| 17 |
const assert = std.debug.assert; |
| 18 |
const ir = @import("ir.zig"); |
| 19 |
const zir = @import("zir.zig"); |
| 20 |
const Module = @import("Module.zig"); |
| 21 |
const Inst = ir.Inst; |
| 22 |
const Body = ir.Body; |
| 23 |
const trace = @import("tracy.zig").trace; |
| 24 |
const Scope = Module.Scope; |
| 25 |
const InnerError = Module.InnerError; |
| 26 |
const Decl = Module.Decl; |
| 27 |
|
| 28 |
pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!*Inst { |
| 29 |
switch (old_inst.tag) { |
| 30 |
.alloc => return analyzeInstAlloc(mod, scope, old_inst.castTag(.alloc).?), |
| 31 |
.alloc_inferred => return analyzeInstAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred).?), |
| 32 |
.arg => return analyzeInstArg(mod, scope, old_inst.castTag(.arg).?), |
| 33 |
.bitcast_ref => return analyzeInstBitCastRef(mod, scope, old_inst.castTag(.bitcast_ref).?), |
| 34 |
.bitcast_result_ptr => return analyzeInstBitCastResultPtr(mod, scope, old_inst.castTag(.bitcast_result_ptr).?), |
| 35 |
.block => return analyzeInstBlock(mod, scope, old_inst.castTag(.block).?, false), |
| 36 |
.block_comptime => return analyzeInstBlock(mod, scope, old_inst.castTag(.block_comptime).?, true), |
| 37 |
.block_flat => return analyzeInstBlockFlat(mod, scope, old_inst.castTag(.block_flat).?, false), |
| 38 |
.block_comptime_flat => return analyzeInstBlockFlat(mod, scope, old_inst.castTag(.block_comptime_flat).?, true), |
| 39 |
.@"break" => return analyzeInstBreak(mod, scope, old_inst.castTag(.@"break").?), |
| 40 |
.breakpoint => return analyzeInstBreakpoint(mod, scope, old_inst.castTag(.breakpoint).?), |
| 41 |
.breakvoid => return analyzeInstBreakVoid(mod, scope, old_inst.castTag(.breakvoid).?), |
| 42 |
.call => return analyzeInstCall(mod, scope, old_inst.castTag(.call).?), |
| 43 |
.coerce_result_block_ptr => return analyzeInstCoerceResultBlockPtr(mod, scope, old_inst.castTag(.coerce_result_block_ptr).?), |
| 44 |
.coerce_result_ptr => return analyzeInstCoerceResultPtr(mod, scope, old_inst.castTag(.coerce_result_ptr).?), |
| 45 |
.coerce_to_ptr_elem => return analyzeInstCoerceToPtrElem(mod, scope, old_inst.castTag(.coerce_to_ptr_elem).?), |
| 46 |
.compileerror => return analyzeInstCompileError(mod, scope, old_inst.castTag(.compileerror).?), |
| 47 |
.@"const" => return analyzeInstConst(mod, scope, old_inst.castTag(.@"const").?), |
| 48 |
.dbg_stmt => return analyzeInstDbgStmt(mod, scope, old_inst.castTag(.dbg_stmt).?), |
| 49 |
.declref => return analyzeInstDeclRef(mod, scope, old_inst.castTag(.declref).?), |
| 50 |
.declref_str => return analyzeInstDeclRefStr(mod, scope, old_inst.castTag(.declref_str).?), |
| 51 |
.declval => return analyzeInstDeclVal(mod, scope, old_inst.castTag(.declval).?), |
| 52 |
.declval_in_module => return analyzeInstDeclValInModule(mod, scope, old_inst.castTag(.declval_in_module).?), |
| 53 |
.ensure_result_used => return analyzeInstEnsureResultUsed(mod, scope, old_inst.castTag(.ensure_result_used).?), |
| 54 |
.ensure_result_non_error => return analyzeInstEnsureResultNonError(mod, scope, old_inst.castTag(.ensure_result_non_error).?), |
| 55 |
.ensure_indexable => return analyzeInstEnsureIndexable(mod, scope, old_inst.castTag(.ensure_indexable).?), |
| 56 |
.ref => return analyzeInstRef(mod, scope, old_inst.castTag(.ref).?), |
| 57 |
.ret_ptr => return analyzeInstRetPtr(mod, scope, old_inst.castTag(.ret_ptr).?), |
| 58 |
.ret_type => return analyzeInstRetType(mod, scope, old_inst.castTag(.ret_type).?), |
| 59 |
.single_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.single_const_ptr_type).?, false, .One), |
| 60 |
.single_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.single_mut_ptr_type).?, true, .One), |
| 61 |
.many_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.many_const_ptr_type).?, false, .Many), |
| 62 |
.many_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.many_mut_ptr_type).?, true, .Many), |
| 63 |
.c_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_const_ptr_type).?, false, .C), |
| 64 |
.c_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_mut_ptr_type).?, true, .C), |
| 65 |
.const_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.const_slice_type).?, false, .Slice), |
| 66 |
.mut_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.mut_slice_type).?, true, .Slice), |
| 67 |
.ptr_type => return analyzeInstPtrType(mod, scope, old_inst.castTag(.ptr_type).?), |
| 68 |
.store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?), |
| 69 |
.str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?), |
| 70 |
.int => { |
| 71 |
const big_int = old_inst.castTag(.int).?.positionals.int; |
| 72 |
return mod.constIntBig(scope, old_inst.src, Type.initTag(.comptime_int), big_int); |
| 73 |
}, |
| 74 |
.inttype => return analyzeInstIntType(mod, scope, old_inst.castTag(.inttype).?), |
| 75 |
.loop => return analyzeInstLoop(mod, scope, old_inst.castTag(.loop).?), |
| 76 |
.param_type => return analyzeInstParamType(mod, scope, old_inst.castTag(.param_type).?), |
| 77 |
.ptrtoint => return analyzeInstPtrToInt(mod, scope, old_inst.castTag(.ptrtoint).?), |
| 78 |
.fieldptr => return analyzeInstFieldPtr(mod, scope, old_inst.castTag(.fieldptr).?), |
| 79 |
.deref => return analyzeInstDeref(mod, scope, old_inst.castTag(.deref).?), |
| 80 |
.as => return analyzeInstAs(mod, scope, old_inst.castTag(.as).?), |
| 81 |
.@"asm" => return analyzeInstAsm(mod, scope, old_inst.castTag(.@"asm").?), |
| 82 |
.@"unreachable" => return analyzeInstUnreachable(mod, scope, old_inst.castTag(.@"unreachable").?, true), |
| 83 |
.unreach_nocheck => return analyzeInstUnreachable(mod, scope, old_inst.castTag(.unreach_nocheck).?, false), |
| 84 |
.@"return" => return analyzeInstRet(mod, scope, old_inst.castTag(.@"return").?), |
| 85 |
.returnvoid => return analyzeInstRetVoid(mod, scope, old_inst.castTag(.returnvoid).?), |
| 86 |
.@"fn" => return analyzeInstFn(mod, scope, old_inst.castTag(.@"fn").?), |
| 87 |
.@"export" => return analyzeInstExport(mod, scope, old_inst.castTag(.@"export").?), |
| 88 |
.primitive => return analyzeInstPrimitive(mod, scope, old_inst.castTag(.primitive).?), |
| 89 |
.fntype => return analyzeInstFnType(mod, scope, old_inst.castTag(.fntype).?), |
| 90 |
.intcast => return analyzeInstIntCast(mod, scope, old_inst.castTag(.intcast).?), |
| 91 |
.bitcast => return analyzeInstBitCast(mod, scope, old_inst.castTag(.bitcast).?), |
| 92 |
.floatcast => return analyzeInstFloatCast(mod, scope, old_inst.castTag(.floatcast).?), |
| 93 |
.elemptr => return analyzeInstElemPtr(mod, scope, old_inst.castTag(.elemptr).?), |
| 94 |
.add => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.add).?), |
| 95 |
.addwrap => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.addwrap).?), |
| 96 |
.sub => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.sub).?), |
| 97 |
.subwrap => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.subwrap).?), |
| 98 |
.mul => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.mul).?), |
| 99 |
.mulwrap => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.mulwrap).?), |
| 100 |
.div => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.div).?), |
| 101 |
.mod_rem => return analyzeInstArithmetic(mod, scope, old_inst.castTag(.mod_rem).?), |
| 102 |
.array_cat => return analyzeInstArrayCat(mod, scope, old_inst.castTag(.array_cat).?), |
| 103 |
.array_mul => return analyzeInstArrayMul(mod, scope, old_inst.castTag(.array_mul).?), |
| 104 |
.bitand => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitand).?), |
| 105 |
.bitnot => return analyzeInstBitNot(mod, scope, old_inst.castTag(.bitnot).?), |
| 106 |
.bitor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitor).?), |
| 107 |
.xor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.xor).?), |
| 108 |
.shl => return analyzeInstShl(mod, scope, old_inst.castTag(.shl).?), |
| 109 |
.shr => return analyzeInstShr(mod, scope, old_inst.castTag(.shr).?), |
| 110 |
.cmp_lt => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_lt).?, .lt), |
| 111 |
.cmp_lte => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_lte).?, .lte), |
| 112 |
.cmp_eq => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_eq).?, .eq), |
| 113 |
.cmp_gte => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_gte).?, .gte), |
| 114 |
.cmp_gt => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_gt).?, .gt), |
| 115 |
.cmp_neq => return analyzeInstCmp(mod, scope, old_inst.castTag(.cmp_neq).?, .neq), |
| 116 |
.condbr => return analyzeInstCondBr(mod, scope, old_inst.castTag(.condbr).?), |
| 117 |
.isnull => return analyzeInstIsNonNull(mod, scope, old_inst.castTag(.isnull).?, true), |
| 118 |
.isnonnull => return analyzeInstIsNonNull(mod, scope, old_inst.castTag(.isnonnull).?, false), |
| 119 |
// a comment |
| 120 |
.iserr => return analyzeInstIsErr(mod, scope, old_inst.castTag(.iserr).?), // another comment |
| 121 |
.boolnot => return analyzeInstBoolNot(mod, scope, old_inst.castTag(.boolnot).?), |
| 122 |
.typeof => return analyzeInstTypeOf(mod, scope, old_inst.castTag(.typeof).?), |
| 123 |
.optional_type => return analyzeInstOptionalType(mod, scope, old_inst.castTag(.optional_type).?), |
| 124 |
.unwrap_optional_safe => return analyzeInstUnwrapOptional(mod, scope, old_inst.castTag(.unwrap_optional_safe).?, true), |
| 125 |
.unwrap_optional_unsafe => return analyzeInstUnwrapOptional(mod, scope, old_inst.castTag(.unwrap_optional_unsafe).?, false), |
| 126 |
.unwrap_err_safe => return analyzeInstUnwrapErr(mod, scope, old_inst.castTag(.unwrap_err_safe).?, true), |
| 127 |
.unwrap_err_unsafe => return analyzeInstUnwrapErr(mod, scope, old_inst.castTag(.unwrap_err_unsafe).?, false), |
| 128 |
.unwrap_err_code => return analyzeInstUnwrapErrCode(mod, scope, old_inst.castTag(.unwrap_err_code).?), |
| 129 |
.ensure_err_payload_void => return analyzeInstEnsureErrPayloadVoid(mod, scope, old_inst.castTag(.ensure_err_payload_void).?), |
| 130 |
.array_type => return analyzeInstArrayType(mod, scope, old_inst.castTag(.array_type).?), |
| 131 |
.array_type_sentinel => return analyzeInstArrayTypeSentinel(mod, scope, old_inst.castTag(.array_type_sentinel).?), |
| 132 |
.enum_literal => return analyzeInstEnumLiteral(mod, scope, old_inst.castTag(.enum_literal).?), |
| 133 |
.merge_error_sets => return analyzeInstMergeErrorSets(mod, scope, old_inst.castTag(.merge_error_sets).?), |
| 134 |
.error_union_type => return analyzeInstErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?), |
| 135 |
.anyframe_type => return analyzeInstAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?), |
| 136 |
.error_set => return analyzeInstErrorSet(mod, scope, old_inst.castTag(.error_set).?), |
| 137 |
.slice => return analyzeInstSlice(mod, scope, old_inst.castTag(.slice).?), |
| 138 |
.slice_start => return analyzeInstSliceStart(mod, scope, old_inst.castTag(.slice_start).?), |
| 139 |
} |
| 140 |
} |