authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-14 23:59:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-20 12:19:16-07:00
log0da66339096c21d9ca524ff7a0c11a5707b60041
treee5c530e4c42e7ae4a6b65040da9e7ac7e3bae264
parent1294ebe1f5eaca1f11d68284d1b96419d53253be

Sema: fix implementation of getTypeOf

and rename it to typeOf

1 files changed, 258 insertions(+), 209 deletions(-)

src/Sema.zig+258-209
......@@ -199,65 +199,65 @@ pub fn analyzeBody(
199199 .cmp_lt => try sema.zirCmp(block, inst, .lt),
200200 .cmp_lte => try sema.zirCmp(block, inst, .lte),
201201 .cmp_neq => try sema.zirCmp(block, inst, .neq),
202 //.coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst),
203 //.decl_ref => try sema.zirDeclRef(block, inst),
204 //.decl_val => try sema.zirDeclVal(block, inst),
205 //.load => try sema.zirLoad(block, inst),
206 //.elem_ptr => try sema.zirElemPtr(block, inst),
207 //.elem_ptr_node => try sema.zirElemPtrNode(block, inst),
208 //.elem_val => try sema.zirElemVal(block, inst),
209 //.elem_val_node => try sema.zirElemValNode(block, inst),
210 //.elem_type => try sema.zirElemType(block, inst),
211 //.enum_literal => try sema.zirEnumLiteral(block, inst),
212 //.enum_to_int => try sema.zirEnumToInt(block, inst),
213 //.int_to_enum => try sema.zirIntToEnum(block, inst),
214 //.err_union_code => try sema.zirErrUnionCode(block, inst),
215 //.err_union_code_ptr => try sema.zirErrUnionCodePtr(block, inst),
216 //.err_union_payload_safe => try sema.zirErrUnionPayload(block, inst, true),
217 //.err_union_payload_safe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, true),
218 //.err_union_payload_unsafe => try sema.zirErrUnionPayload(block, inst, false),
219 //.err_union_payload_unsafe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, false),
220 //.error_union_type => try sema.zirErrorUnionType(block, inst),
221 //.error_value => try sema.zirErrorValue(block, inst),
222 //.error_to_int => try sema.zirErrorToInt(block, inst),
223 //.int_to_error => try sema.zirIntToError(block, inst),
224 //.field_ptr => try sema.zirFieldPtr(block, inst),
225 //.field_ptr_named => try sema.zirFieldPtrNamed(block, inst),
226 //.field_val => try sema.zirFieldVal(block, inst),
227 //.field_val_named => try sema.zirFieldValNamed(block, inst),
228 //.func => try sema.zirFunc(block, inst, false),
229 //.func_inferred => try sema.zirFunc(block, inst, true),
230 //.import => try sema.zirImport(block, inst),
231 //.indexable_ptr_len => try sema.zirIndexablePtrLen(block, inst),
232 //.int => try sema.zirInt(block, inst),
233 //.int_big => try sema.zirIntBig(block, inst),
234 //.float => try sema.zirFloat(block, inst),
235 //.float128 => try sema.zirFloat128(block, inst),
236 //.int_type => try sema.zirIntType(block, inst),
237 //.is_non_err => try sema.zirIsNonErr(block, inst),
238 //.is_non_err_ptr => try sema.zirIsNonErrPtr(block, inst),
239 //.is_non_null => try sema.zirIsNonNull(block, inst),
240 //.is_non_null_ptr => try sema.zirIsNonNullPtr(block, inst),
241 //.loop => try sema.zirLoop(block, inst),
242 //.merge_error_sets => try sema.zirMergeErrorSets(block, inst),
243 //.negate => try sema.zirNegate(block, inst, .sub),
244 //.negate_wrap => try sema.zirNegate(block, inst, .subwrap),
245 //.optional_payload_safe => try sema.zirOptionalPayload(block, inst, true),
246 //.optional_payload_safe_ptr => try sema.zirOptionalPayloadPtr(block, inst, true),
247 //.optional_payload_unsafe => try sema.zirOptionalPayload(block, inst, false),
248 //.optional_payload_unsafe_ptr => try sema.zirOptionalPayloadPtr(block, inst, false),
249 //.optional_type => try sema.zirOptionalType(block, inst),
250 //.param_type => try sema.zirParamType(block, inst),
251 //.ptr_type => try sema.zirPtrType(block, inst),
252 //.ptr_type_simple => try sema.zirPtrTypeSimple(block, inst),
253 //.ref => try sema.zirRef(block, inst),
254 //.ret_err_value_code => try sema.zirRetErrValueCode(block, inst),
255 //.shl => try sema.zirShl(block, inst),
256 //.shr => try sema.zirShr(block, inst),
257 //.slice_end => try sema.zirSliceEnd(block, inst),
258 //.slice_sentinel => try sema.zirSliceSentinel(block, inst),
259 //.slice_start => try sema.zirSliceStart(block, inst),
260 //.str => try sema.zirStr(block, inst),
202 .coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst),
203 .decl_ref => try sema.zirDeclRef(block, inst),
204 .decl_val => try sema.zirDeclVal(block, inst),
205 .load => try sema.zirLoad(block, inst),
206 .elem_ptr => try sema.zirElemPtr(block, inst),
207 .elem_ptr_node => try sema.zirElemPtrNode(block, inst),
208 .elem_val => try sema.zirElemVal(block, inst),
209 .elem_val_node => try sema.zirElemValNode(block, inst),
210 .elem_type => try sema.zirElemType(block, inst),
211 .enum_literal => try sema.zirEnumLiteral(block, inst),
212 .enum_to_int => try sema.zirEnumToInt(block, inst),
213 .int_to_enum => try sema.zirIntToEnum(block, inst),
214 .err_union_code => try sema.zirErrUnionCode(block, inst),
215 .err_union_code_ptr => try sema.zirErrUnionCodePtr(block, inst),
216 .err_union_payload_safe => try sema.zirErrUnionPayload(block, inst, true),
217 .err_union_payload_safe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, true),
218 .err_union_payload_unsafe => try sema.zirErrUnionPayload(block, inst, false),
219 .err_union_payload_unsafe_ptr => try sema.zirErrUnionPayloadPtr(block, inst, false),
220 .error_union_type => try sema.zirErrorUnionType(block, inst),
221 .error_value => try sema.zirErrorValue(block, inst),
222 .error_to_int => try sema.zirErrorToInt(block, inst),
223 .int_to_error => try sema.zirIntToError(block, inst),
224 .field_ptr => try sema.zirFieldPtr(block, inst),
225 .field_ptr_named => try sema.zirFieldPtrNamed(block, inst),
226 .field_val => try sema.zirFieldVal(block, inst),
227 .field_val_named => try sema.zirFieldValNamed(block, inst),
228 .func => try sema.zirFunc(block, inst, false),
229 .func_inferred => try sema.zirFunc(block, inst, true),
230 .import => try sema.zirImport(block, inst),
231 .indexable_ptr_len => try sema.zirIndexablePtrLen(block, inst),
232 .int => try sema.zirInt(block, inst),
233 .int_big => try sema.zirIntBig(block, inst),
234 .float => try sema.zirFloat(block, inst),
235 .float128 => try sema.zirFloat128(block, inst),
236 .int_type => try sema.zirIntType(block, inst),
237 .is_non_err => try sema.zirIsNonErr(block, inst),
238 .is_non_err_ptr => try sema.zirIsNonErrPtr(block, inst),
239 .is_non_null => try sema.zirIsNonNull(block, inst),
240 .is_non_null_ptr => try sema.zirIsNonNullPtr(block, inst),
241 .loop => try sema.zirLoop(block, inst),
242 .merge_error_sets => try sema.zirMergeErrorSets(block, inst),
243 .negate => try sema.zirNegate(block, inst, .sub),
244 .negate_wrap => try sema.zirNegate(block, inst, .subwrap),
245 .optional_payload_safe => try sema.zirOptionalPayload(block, inst, true),
246 .optional_payload_safe_ptr => try sema.zirOptionalPayloadPtr(block, inst, true),
247 .optional_payload_unsafe => try sema.zirOptionalPayload(block, inst, false),
248 .optional_payload_unsafe_ptr => try sema.zirOptionalPayloadPtr(block, inst, false),
249 .optional_type => try sema.zirOptionalType(block, inst),
250 .param_type => try sema.zirParamType(block, inst),
251 .ptr_type => try sema.zirPtrType(block, inst),
252 .ptr_type_simple => try sema.zirPtrTypeSimple(block, inst),
253 .ref => try sema.zirRef(block, inst),
254 .ret_err_value_code => try sema.zirRetErrValueCode(block, inst),
255 .shl => try sema.zirShl(block, inst),
256 .shr => try sema.zirShr(block, inst),
257 .slice_end => try sema.zirSliceEnd(block, inst),
258 .slice_sentinel => try sema.zirSliceSentinel(block, inst),
259 .slice_start => try sema.zirSliceStart(block, inst),
260 .str => try sema.zirStr(block, inst),
261261 //.switch_block => try sema.zirSwitchBlock(block, inst, false, .none),
262262 //.switch_block_multi => try sema.zirSwitchBlockMulti(block, inst, false, .none),
263263 //.switch_block_else => try sema.zirSwitchBlock(block, inst, false, .@"else"),
......@@ -536,7 +536,7 @@ pub fn analyzeBody(
536536 },
537537 else => |t| @panic(@tagName(t)),
538538 };
539 if (sema.getTypeOf(air_inst).isNoReturn())
539 if (sema.typeOf(air_inst).isNoReturn())
540540 return always_noreturn;
541541 try map.put(sema.gpa, inst, air_inst);
542542 i += 1;
......@@ -620,10 +620,10 @@ fn resolveConstString(
620620
621621pub fn resolveType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type {
622622 const air_inst = sema.resolveInst(zir_ref);
623 return sema.resolveAirAsType(block, src, air_inst);
623 return sema.analyzeAsType(block, src, air_inst);
624624}
625625
626fn resolveAirAsType(
626fn analyzeAsType(
627627 sema: *Sema,
628628 block: *Scope.Block,
629629 src: LazySrcLoc,
......@@ -664,26 +664,26 @@ fn resolvePossiblyUndefinedValue(
664664 sema: *Sema,
665665 block: *Scope.Block,
666666 src: LazySrcLoc,
667 air_ref: Air.Inst.Ref,
667 inst: Air.Inst.Ref,
668668) CompileError!?Value {
669 const ty = sema.getTypeOf(air_ref);
670 if (try sema.typeHasOnePossibleValue(block, src, ty)) |opv| {
671 return opv;
672 }
673669 // First section of indexes correspond to a set number of constant values.
674 var i: usize = @enumToInt(air_ref);
670 var i: usize = @enumToInt(inst);
675671 if (i < Air.Inst.Ref.typed_value_map.len) {
676672 return Air.Inst.Ref.typed_value_map[i].val;
677673 }
678674 i -= Air.Inst.Ref.typed_value_map.len;
679675
676 if (try sema.typeHasOnePossibleValue(block, src, sema.typeOf(inst))) |opv| {
677 return opv;
678 }
679
680680 switch (sema.air_instructions.items(.tag)[i]) {
681681 .constant => {
682682 const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
683683 return sema.air_values.items[ty_pl.payload];
684684 },
685685 .const_ty => {
686 return sema.air_instructions.items(.data)[i].ty.toValue(undefined) catch unreachable;
686 return try sema.air_instructions.items(.data)[i].ty.toValue(sema.arena);
687687 },
688688 else => return null,
689689 }
......@@ -739,7 +739,7 @@ pub fn resolveInstConst(
739739 const air_ref = sema.resolveInst(zir_ref);
740740 const val = try sema.resolveConstValue(block, src, air_ref);
741741 return TypedValue{
742 .ty = sema.getTypeOf(air_ref),
742 .ty = sema.typeOf(air_ref),
743743 .val = val,
744744 };
745745}
......@@ -1230,7 +1230,7 @@ fn ensureResultUsed(
12301230 operand: Air.Inst.Ref,
12311231 src: LazySrcLoc,
12321232) CompileError!void {
1233 const operand_ty = sema.getTypeOf(operand);
1233 const operand_ty = sema.typeOf(operand);
12341234 switch (operand_ty.zigTypeTag()) {
12351235 .Void, .NoReturn => return,
12361236 else => return sema.mod.fail(&block.base, src, "expression value is ignored", .{}),
......@@ -1244,7 +1244,7 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
12441244 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
12451245 const operand = sema.resolveInst(inst_data.operand);
12461246 const src = inst_data.src();
1247 const operand_ty = sema.getTypeOf(operand);
1247 const operand_ty = sema.typeOf(operand);
12481248 switch (operand_ty.zigTypeTag()) {
12491249 .ErrorSet, .ErrorUnion => return sema.mod.fail(&block.base, src, "error is discarded", .{}),
12501250 else => return,
......@@ -1259,7 +1259,7 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
12591259 const src = inst_data.src();
12601260 const array_ptr = sema.resolveInst(inst_data.operand);
12611261
1262 const elem_ty = sema.getTypeOf(array_ptr).elemType();
1262 const elem_ty = sema.typeOf(array_ptr).elemType();
12631263 if (!elem_ty.isIndexable()) {
12641264 const cond_src: LazySrcLoc = .{ .node_offset_for_cond = inst_data.src_node };
12651265 const msg = msg: {
......@@ -1282,7 +1282,8 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
12821282 return sema.mod.failWithOwnedErrorMsg(&block.base, msg);
12831283 }
12841284 const result_ptr = try sema.namedFieldPtr(block, src, array_ptr, "len", src);
1285 return sema.analyzeLoad(block, src, result_ptr, result_ptr.src);
1285 const result_ptr_src = src;
1286 return sema.analyzeLoad(block, src, result_ptr, result_ptr_src);
12861287}
12871288
12881289fn zirArg(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -1630,7 +1631,7 @@ fn zirParamType(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
16301631
16311632 const inst_data = sema.code.instructions.items(.data)[inst].param_type;
16321633 const fn_inst = sema.resolveInst(inst_data.callee);
1633 const fn_inst_ty = sema.getTypeOf(fn_inst);
1634 const fn_inst_ty = sema.typeOf(fn_inst);
16341635 const param_index = inst_data.param_index;
16351636
16361637 const fn_ty: Type = switch (fn_inst_ty.zigTypeTag()) {
......@@ -1859,7 +1860,7 @@ fn zirLoop(sema: *Sema, parent_block: *Scope.Block, inst: Zir.Inst.Index) Compil
18591860 sema.air_instructions.items(.data)[loop_inst].ty_pl.payload = sema.addExtraAssumeCapacity(
18601861 Air.Block{ .body_len = @intCast(u32, loop_block.instructions.items.len) },
18611862 );
1862 sema.air_extra.appendAssumeCapacity(loop_block.instructions.items);
1863 sema.air_extra.appendSliceAssumeCapacity(loop_block.instructions.items);
18631864 return sema.analyzeBlockBody(parent_block, src, &child_block, merges);
18641865}
18651866
......@@ -1957,7 +1958,7 @@ fn analyzeBlockBody(
19571958
19581959 // Blocks must terminate with noreturn instruction.
19591960 assert(child_block.instructions.items.len != 0);
1960 assert(sema.getTypeOf(indexToRef(child_block.instructions.items[child_block.instructions.items.len - 1])).isNoReturn());
1961 assert(sema.typeOf(indexToRef(child_block.instructions.items[child_block.instructions.items.len - 1])).isNoReturn());
19611962
19621963 if (merges.results.items.len == 0) {
19631964 // No need for a block instruction. We can put the new instructions
......@@ -1999,7 +2000,7 @@ fn analyzeBlockBody(
19992000 for (merges.br_list.items) |br| {
20002001 const br_operand = sema.air_instructions.items(.data)[br].br.operand;
20012002 const br_operand_src = src;
2002 const br_operand_ty = sema.getTypeOf(br_operand);
2003 const br_operand_ty = sema.typeOf(br_operand);
20032004 if (br_operand_ty.eql(resolved_ty)) {
20042005 // No type coercion needed.
20052006 continue;
......@@ -2252,7 +2253,7 @@ fn analyzeCall(
22522253 ensure_result_used: bool,
22532254 args: []const Air.Inst.Ref,
22542255) CompileError!Air.Inst.Ref {
2255 const func_ty = sema.getTypeOf(func);
2256 const func_ty = sema.typeOf(func);
22562257 if (func_ty.zigTypeTag() != .Fn)
22572258 return sema.mod.fail(&block.base, func_src, "type '{}' not a function", .{func_ty});
22582259
......@@ -2606,8 +2607,8 @@ fn zirMergeErrorSets(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Com
26062607 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
26072608 const lhs = sema.resolveInst(extra.lhs);
26082609 const rhs = sema.resolveInst(extra.rhs);
2609 const lhs_ty = sema.getTypeOf(lhs);
2610 const rhs_ty = sema.getTypeOf(rhs);
2610 const lhs_ty = sema.typeOf(lhs);
2611 const rhs_ty = sema.typeOf(rhs);
26112612 if (rhs_ty.zigTypeTag() == .Bool and lhs_ty.zigTypeTag() == .Bool) {
26122613 const msg = msg: {
26132614 const msg = try sema.mod.errMsg(&block.base, lhs_src, "expected error set type, found 'bool'", .{});
......@@ -2699,7 +2700,7 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
26992700 const src = inst_data.src();
27002701 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
27012702 const operand = sema.resolveInst(inst_data.operand);
2702 const operand_ty = sema.getTypeOf(operand);
2703 const operand_ty = sema.typeOf(operand);
27032704
27042705 const enum_tag: Air.Inst.Ref = switch (operand_ty.zigTypeTag()) {
27052706 .Enum => operand,
......@@ -2720,7 +2721,7 @@ fn zirEnumToInt(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
27202721 });
27212722 },
27222723 };
2723 const enum_tag_ty = sema.getTypeOf(enum_tag);
2724 const enum_tag_ty = sema.typeOf(enum_tag);
27242725
27252726 var int_tag_type_buffer: Type.Payload.Bits = undefined;
27262727 const int_tag_ty = try enum_tag_ty.intTagType(&int_tag_type_buffer).copy(arena);
......@@ -2821,7 +2822,7 @@ fn zirOptionalPayloadPtr(
28212822
28222823 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
28232824 const optional_ptr = sema.resolveInst(inst_data.operand);
2824 const optional_ptr_ty = sema.getTypeOf(optional_ptr);
2825 const optional_ptr_ty = sema.typeOf(optional_ptr);
28252826 assert(optional_ptr_ty.zigTypeTag() == .Pointer);
28262827 const src = inst_data.src();
28272828
......@@ -2863,7 +2864,7 @@ fn zirOptionalPayload(
28632864 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
28642865 const src = inst_data.src();
28652866 const operand = sema.resolveInst(inst_data.operand);
2866 const operand_ty = sema.getTypeOf(operand);
2867 const operand_ty = sema.typeOf(operand);
28672868 const opt_type = operand_ty;
28682869 if (opt_type.zigTypeTag() != .Optional) {
28692870 return sema.mod.fail(&block.base, src, "expected optional type, found {}", .{opt_type});
......@@ -2900,7 +2901,7 @@ fn zirErrUnionPayload(
29002901 const src = inst_data.src();
29012902 const operand = sema.resolveInst(inst_data.operand);
29022903 const operand_src = src;
2903 const operand_ty = sema.getTypeOf(operand);
2904 const operand_ty = sema.typeOf(operand);
29042905 if (operand_ty.zigTypeTag() != .ErrorUnion)
29052906 return sema.mod.fail(&block.base, operand_src, "expected error union type, found '{}'", .{operand_ty});
29062907
......@@ -2936,7 +2937,7 @@ fn zirErrUnionPayloadPtr(
29362937 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
29372938 const src = inst_data.src();
29382939 const operand = sema.resolveInst(inst_data.operand);
2939 const operand_ty = sema.getTypeOf(operand);
2940 const operand_ty = sema.typeOf(operand);
29402941 assert(operand_ty.zigTypeTag() == .Pointer);
29412942
29422943 if (operand_ty.elemType().zigTypeTag() != .ErrorUnion)
......@@ -2976,7 +2977,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compi
29762977 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
29772978 const src = inst_data.src();
29782979 const operand = sema.resolveInst(inst_data.operand);
2979 const operand_ty = sema.getTypeOf(operand);
2980 const operand_ty = sema.typeOf(operand);
29802981 if (operand_ty.zigTypeTag() != .ErrorUnion)
29812982 return sema.mod.fail(&block.base, src, "expected error union type, found '{}'", .{operand_ty});
29822983
......@@ -3000,7 +3001,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
30003001 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
30013002 const src = inst_data.src();
30023003 const operand = sema.resolveInst(inst_data.operand);
3003 const operand_ty = sema.getTypeOf(operand);
3004 const operand_ty = sema.typeOf(operand);
30043005 assert(operand_ty.zigTypeTag() == .Pointer);
30053006
30063007 if (operand_ty.elemType().zigTypeTag() != .ErrorUnion)
......@@ -3026,7 +3027,7 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
30263027 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
30273028 const src = inst_data.src();
30283029 const operand = sema.resolveInst(inst_data.operand);
3029 const operand_ty = sema.getTypeOf(operand);
3030 const operand_ty = sema.typeOf(operand);
30303031 if (operand_ty.zigTypeTag() != .ErrorUnion)
30313032 return sema.mod.fail(&block.base, src, "expected error union type, found '{}'", .{operand_ty});
30323033 if (operand_ty.castTag(.error_union).?.data.payload.zigTypeTag() != .Void) {
......@@ -3214,7 +3215,6 @@ fn funcCommon(
32143215 .state = anal_state,
32153216 .zir_body_inst = body_inst,
32163217 .owner_decl = sema.owner_decl,
3217 .body = undefined,
32183218 .lbrace_line = src_locs.lbrace_line,
32193219 .rbrace_line = src_locs.rbrace_line,
32203220 .lbrace_column = @truncate(u16, src_locs.columns),
......@@ -3283,12 +3283,13 @@ fn zirFieldVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
32833283 const extra = sema.code.extraData(Zir.Inst.Field, inst_data.payload_index).data;
32843284 const field_name = sema.code.nullTerminatedString(extra.field_name_start);
32853285 const object = sema.resolveInst(extra.lhs);
3286 const object_ptr = if (sema.getTypeOf(object).zigTypeTag() == .Pointer)
3286 const object_ptr = if (sema.typeOf(object).zigTypeTag() == .Pointer)
32873287 object
32883288 else
32893289 try sema.analyzeRef(block, src, object);
32903290 const result_ptr = try sema.namedFieldPtr(block, src, object_ptr, field_name, field_name_src);
3291 return sema.analyzeLoad(block, src, result_ptr, result_ptr.src);
3291 const result_ptr_src = src;
3292 return sema.analyzeLoad(block, src, result_ptr, result_ptr_src);
32923293}
32933294
32943295fn zirFieldPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -3356,7 +3357,7 @@ fn zirIntCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
33563357 ),
33573358 };
33583359
3359 const operand_ty = sema.getTypeOf(operand);
3360 const operand_ty = sema.typeOf(operand);
33603361 switch (operand_ty.zigTypeTag()) {
33613362 .ComptimeInt, .Int => {},
33623363 else => return sema.mod.fail(
......@@ -3414,7 +3415,7 @@ fn zirFloatCast(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileE
34143415 ),
34153416 };
34163417
3417 const operand_ty = sema.getTypeOf(operand);
3418 const operand_ty = sema.typeOf(operand);
34183419 switch (operand_ty.zigTypeTag()) {
34193420 .ComptimeFloat, .Float, .ComptimeInt => {},
34203421 else => return sema.mod.fail(
......@@ -3440,7 +3441,7 @@ fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
34403441
34413442 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
34423443 const array = sema.resolveInst(bin_inst.lhs);
3443 const array_ty = sema.getTypeOf(array);
3444 const array_ty = sema.typeOf(array);
34443445 const array_ptr = if (array_ty.zigTypeTag() == .Pointer)
34453446 array
34463447 else
......@@ -3459,7 +3460,7 @@ fn zirElemValNode(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
34593460 const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node };
34603461 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
34613462 const array = sema.resolveInst(extra.lhs);
3462 const array_ty = sema.getTypeOf(array);
3463 const array_ty = sema.typeOf(array);
34633464 const array_ptr = if (array_ty.zigTypeTag() == .Pointer)
34643465 array
34653466 else
......@@ -3502,7 +3503,7 @@ fn zirSliceStart(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compile
35023503 const array_ptr = sema.resolveInst(extra.lhs);
35033504 const start = sema.resolveInst(extra.start);
35043505
3505 return sema.analyzeSlice(block, src, array_ptr, start, null, null, .unneeded);
3506 return sema.analyzeSlice(block, src, array_ptr, start, .none, .none, .unneeded);
35063507}
35073508
35083509fn zirSliceEnd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -3516,7 +3517,7 @@ fn zirSliceEnd(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
35163517 const start = sema.resolveInst(extra.start);
35173518 const end = sema.resolveInst(extra.end);
35183519
3519 return sema.analyzeSlice(block, src, array_ptr, start, end, null, .unneeded);
3520 return sema.analyzeSlice(block, src, array_ptr, start, end, .none, .unneeded);
35203521}
35213522
35223523fn zirSliceSentinel(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -3668,7 +3669,7 @@ fn analyzeSwitch(
36683669 const src: LazySrcLoc = .{ .node_offset = src_node_offset };
36693670 const special_prong_src: LazySrcLoc = .{ .node_offset_switch_special_prong = src_node_offset };
36703671 const operand_src: LazySrcLoc = .{ .node_offset_switch_operand = src_node_offset };
3671 const operand_ty = sema.getTypeOf(operand);
3672 const operand_ty = sema.typeOf(operand);
36723673
36733674 // Validate usage of '_' prongs.
36743675 if (special_prong == .under and !operand_ty.isNonexhaustiveEnum()) {
......@@ -4590,8 +4591,8 @@ fn zirBitwise(
45904591 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
45914592 const lhs = sema.resolveInst(extra.lhs);
45924593 const rhs = sema.resolveInst(extra.rhs);
4593 const lhs_ty = sema.getTypeOf(lhs);
4594 const rhs_ty = sema.getTypeOf(rhs);
4594 const lhs_ty = sema.typeOf(lhs);
4595 const rhs_ty = sema.typeOf(rhs);
45954596
45964597 const instructions = &[_]Air.Inst.Ref{ lhs, rhs };
45974598 const resolved_type = try sema.resolvePeerTypes(block, src, instructions);
......@@ -4722,8 +4723,8 @@ fn analyzeArithmetic(
47224723 lhs_src: LazySrcLoc,
47234724 rhs_src: LazySrcLoc,
47244725) CompileError!Air.Inst.Ref {
4725 const lhs_ty = sema.getTypeOf(lhs);
4726 const rhs_ty = sema.getTypeOf(rhs);
4726 const lhs_ty = sema.typeOf(lhs);
4727 const rhs_ty = sema.typeOf(rhs);
47274728 if (lhs_ty.zigTypeTag() == .Vector and rhs_ty.zigTypeTag() == .Vector) {
47284729 if (lhs_ty.arrayLen() != rhs_ty.arrayLen()) {
47294730 return sema.mod.fail(&block.base, src, "vector length mismatch: {d} and {d}", .{
......@@ -4944,8 +4945,8 @@ fn zirCmp(
49444945 .eq, .neq => true,
49454946 else => false,
49464947 };
4947 const lhs_ty = sema.getTypeOf(lhs);
4948 const rhs_ty = sema.getTypeOf(rhs);
4948 const lhs_ty = sema.typeOf(lhs);
4949 const rhs_ty = sema.typeOf(rhs);
49494950 const lhs_ty_tag = lhs_ty.zigTypeTag();
49504951 const rhs_ty_tag = rhs_ty.zigTypeTag();
49514952 if (is_equality_cmp and lhs_ty_tag == .Null and rhs_ty_tag == .Null) {
......@@ -5007,8 +5008,8 @@ fn zirCmp(
50075008 if (!is_equality_cmp) {
50085009 return mod.fail(&block.base, src, "{s} operator not allowed for types", .{@tagName(op)});
50095010 }
5010 const lhs_as_type = try sema.resolveAirAsType(block, lhs_src, lhs);
5011 const rhs_as_type = try sema.resolveAirAsType(block, rhs_src, rhs);
5011 const lhs_as_type = try sema.analyzeAsType(block, lhs_src, lhs);
5012 const rhs_as_type = try sema.analyzeAsType(block, rhs_src, rhs);
50125013 if (lhs_as_type.eql(rhs_as_type) == (op == .eq)) {
50135014 return Air.Inst.Ref.bool_true;
50145015 } else {
......@@ -5144,7 +5145,7 @@ fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
51445145 const zir_datas = sema.code.instructions.items(.data);
51455146 const inst_data = zir_datas[inst].un_node;
51465147 const operand = sema.resolveInst(inst_data.operand);
5147 const operand_ty = sema.getTypeOf(operand);
5148 const operand_ty = sema.typeOf(operand);
51485149 return sema.addType(operand_ty);
51495150}
51505151
......@@ -6375,7 +6376,7 @@ fn addSafetyCheck(
63756376 const block_inst = @intCast(Air.Inst.Index, sema.air_instructions.len);
63766377 const cond_br_inst = block_inst + 1;
63776378 const br_inst = cond_br_inst + 1;
6378 sema.air_instructions.appendAssumeCapacity(gpa, .{
6379 sema.air_instructions.appendAssumeCapacity(.{
63796380 .tag = .block,
63806381 .data = .{ .ty_pl = .{
63816382 .ty = .void_type,
......@@ -6386,7 +6387,7 @@ fn addSafetyCheck(
63866387 });
63876388 sema.air_extra.appendAssumeCapacity(cond_br_inst);
63886389
6389 sema.air_instructions.appendAssumeCapacity(gpa, .{
6390 sema.air_instructions.appendAssumeCapacity(.{
63906391 .tag = .cond_br,
63916392 .data = .{ .pl_op = .{
63926393 .operand = ok,
......@@ -6399,7 +6400,7 @@ fn addSafetyCheck(
63996400 sema.air_extra.appendAssumeCapacity(br_inst);
64006401 sema.air_extra.appendSliceAssumeCapacity(fail_block.instructions.items);
64016402
6402 sema.air_instructions.appendAssumeCapacity(gpa, .{
6403 sema.air_instructions.appendAssumeCapacity(.{
64036404 .tag = .br,
64046405 .data = .{ .br = .{
64056406 .block_inst = block_inst,
......@@ -6497,9 +6498,11 @@ fn namedFieldPtr(
64976498 const mod = sema.mod;
64986499 const arena = sema.arena;
64996500
6500 const elem_ty = switch (object_ptr.ty.zigTypeTag()) {
6501 .Pointer => object_ptr.ty.elemType(),
6502 else => return mod.fail(&block.base, object_ptr.src, "expected pointer, found '{}'", .{object_ptr.ty}),
6501 const object_ptr_src = src; // TODO better source location
6502 const object_ptr_ty = sema.typeOf(object_ptr);
6503 const elem_ty = switch (object_ptr_ty.zigTypeTag()) {
6504 .Pointer => object_ptr_ty.elemType(),
6505 else => return mod.fail(&block.base, object_ptr_src, "expected pointer, found '{}'", .{object_ptr_ty}),
65036506 };
65046507 switch (elem_ty.zigTypeTag()) {
65056508 .Array => {
......@@ -6545,9 +6548,9 @@ fn namedFieldPtr(
65456548 }
65466549 },
65476550 .Type => {
6548 _ = try sema.resolveConstValue(block, object_ptr.src, object_ptr);
6549 const result = try sema.analyzeLoad(block, src, object_ptr, object_ptr.src);
6550 const val = result.value().?;
6551 _ = try sema.resolveConstValue(block, object_ptr_src, object_ptr);
6552 const result = try sema.analyzeLoad(block, src, object_ptr, object_ptr_src);
6553 const val = (sema.resolveDefinedValue(block, src, result) catch unreachable).?;
65516554 const child_type = try val.toType(arena);
65526555 switch (child_type.zigTypeTag()) {
65536556 .ErrorSet => {
......@@ -6694,7 +6697,16 @@ fn analyzeStructFieldPtr(
66946697 }
66956698
66966699 try sema.requireRuntimeBlock(block, src);
6697 return block.addStructFieldPtr(src, ptr_field_ty, struct_ptr, @intCast(u32, field_index));
6700 return block.addInst(.{
6701 .tag = .struct_field_ptr,
6702 .data = .{ .ty_pl = .{
6703 .ty = try sema.addType(ptr_field_ty),
6704 .payload = try sema.addExtra(Air.StructField{
6705 .struct_ptr = struct_ptr,
6706 .field_index = @intCast(u32, field_index),
6707 }),
6708 } },
6709 });
66986710}
66996711
67006712fn analyzeUnionFieldPtr(
......@@ -6742,16 +6754,18 @@ fn elemPtr(
67426754 elem_index: Air.Inst.Ref,
67436755 elem_index_src: LazySrcLoc,
67446756) CompileError!Air.Inst.Ref {
6745 const array_ty = switch (array_ptr.ty.zigTypeTag()) {
6746 .Pointer => array_ptr.ty.elemType(),
6747 else => return sema.mod.fail(&block.base, array_ptr.src, "expected pointer, found '{}'", .{array_ptr.ty}),
6757 const array_ptr_src = src; // TODO better source location
6758 const array_ptr_ty = sema.typeOf(array_ptr);
6759 const array_ty = switch (array_ptr_ty.zigTypeTag()) {
6760 .Pointer => array_ptr_ty.elemType(),
6761 else => return sema.mod.fail(&block.base, array_ptr_src, "expected pointer, found '{}'", .{array_ptr_ty}),
67486762 };
67496763 if (!array_ty.isIndexable()) {
67506764 return sema.mod.fail(&block.base, src, "array access of non-array type '{}'", .{array_ty});
67516765 }
67526766 if (array_ty.isSinglePointer() and array_ty.elemType().zigTypeTag() == .Array) {
67536767 // we have to deref the ptr operand to get the actual array pointer
6754 const array_ptr_deref = try sema.analyzeLoad(block, src, array_ptr, array_ptr.src);
6768 const array_ptr_deref = try sema.analyzeLoad(block, src, array_ptr, array_ptr_src);
67556769 return sema.elemPtrArray(block, src, array_ptr_deref, elem_index, elem_index_src);
67566770 }
67576771 if (array_ty.zigTypeTag() == .Array) {
......@@ -6776,7 +6790,7 @@ fn elemPtrArray(
67766790 // @intCast here because it would have been impossible to construct a value that
67776791 // required a larger index.
67786792 const elem_ptr = try array_ptr_val.elemPtr(sema.arena, @intCast(usize, index_u64));
6779 const pointee_type = array_ptr.ty.elemType().elemType();
6793 const pointee_type = sema.typeOf(array_ptr).elemType().elemType();
67806794
67816795 return sema.addConstant(
67826796 try Type.Tag.single_const_pointer.create(sema.arena, pointee_type),
......@@ -6800,7 +6814,7 @@ fn coerce(
68006814 return sema.coerceVarArgParam(block, inst, inst_src);
68016815 }
68026816
6803 const inst_ty = sema.getTypeOf(inst);
6817 const inst_ty = sema.typeOf(inst);
68046818 // If the types are the same, we can return the operand.
68056819 if (dest_type.eql(inst_ty))
68066820 return inst;
......@@ -7021,7 +7035,7 @@ fn coerceVarArgParam(
70217035 inst: Air.Inst.Ref,
70227036 inst_src: LazySrcLoc,
70237037) !Air.Inst.Ref {
7024 const inst_ty = sema.getTypeOf(inst);
7038 const inst_ty = sema.typeOf(inst);
70257039 switch (inst_ty.zigTypeTag()) {
70267040 .ComptimeInt, .ComptimeFloat => return sema.mod.fail(&block.base, inst_src, "integer and float literals in var args function must be casted", .{}),
70277041 else => {},
......@@ -7170,8 +7184,8 @@ fn analyzeRef(
71707184 src: LazySrcLoc,
71717185 operand: Air.Inst.Ref,
71727186) CompileError!Air.Inst.Ref {
7173 const operand_ty = sema.getTypeOf(operand);
7174 const ptr_type = try sema.mod.simplePtrType(sema.arena, operand_ty, false, .One);
7187 const operand_ty = sema.typeOf(operand);
7188 const ptr_type = try Module.simplePtrType(sema.arena, operand_ty, false, .One);
71757189
71767190 if (try sema.resolvePossiblyUndefinedValue(block, src, operand)) |val| {
71777191 return sema.addConstant(ptr_type, try Value.Tag.ref_val.create(sema.arena, val));
......@@ -7188,7 +7202,7 @@ fn analyzeLoad(
71887202 ptr: Air.Inst.Ref,
71897203 ptr_src: LazySrcLoc,
71907204) CompileError!Air.Inst.Ref {
7191 const ptr_ty = sema.getTypeOf(ptr);
7205 const ptr_ty = sema.typeOf(ptr);
71927206 const elem_ty = switch (ptr_ty.zigTypeTag()) {
71937207 .Pointer => ptr_ty.elemType(),
71947208 else => return sema.mod.fail(&block.base, ptr_src, "expected pointer, found '{}'", .{ptr_ty}),
......@@ -7235,7 +7249,7 @@ fn analyzeIsNonErr(
72357249 src: LazySrcLoc,
72367250 operand: Air.Inst.Ref,
72377251) CompileError!Air.Inst.Ref {
7238 const operand_ty = sema.getTypeOf(operand);
7252 const operand_ty = sema.typeOf(operand);
72397253 const ot = operand_ty.zigTypeTag();
72407254 if (ot != .ErrorSet and ot != .ErrorUnion) return Air.Inst.Ref.bool_true;
72417255 if (ot == .ErrorSet) return Air.Inst.Ref.bool_false;
......@@ -7261,13 +7275,14 @@ fn analyzeSlice(
72617275 src: LazySrcLoc,
72627276 array_ptr: Air.Inst.Ref,
72637277 start: Air.Inst.Ref,
7264 end_opt: ?Air.Inst.Index,
7265 sentinel_opt: ?Air.Inst.Index,
7278 end_opt: Air.Inst.Ref,
7279 sentinel_opt: Air.Inst.Ref,
72667280 sentinel_src: LazySrcLoc,
72677281) CompileError!Air.Inst.Ref {
7268 const ptr_child = switch (array_ptr.ty.zigTypeTag()) {
7269 .Pointer => array_ptr.ty.elemType(),
7270 else => return sema.mod.fail(&block.base, src, "expected pointer, found '{}'", .{array_ptr.ty}),
7282 const array_ptr_ty = sema.typeOf(array_ptr);
7283 const ptr_child = switch (array_ptr_ty.zigTypeTag()) {
7284 .Pointer => array_ptr_ty.elemType(),
7285 else => return sema.mod.fail(&block.base, src, "expected pointer, found '{}'", .{array_ptr_ty}),
72717286 };
72727287
72737288 var array_type = ptr_child;
......@@ -7287,15 +7302,15 @@ fn analyzeSlice(
72877302 else => return sema.mod.fail(&block.base, src, "slice of non-array type '{}'", .{ptr_child}),
72887303 };
72897304
7290 const slice_sentinel = if (sentinel_opt) |sentinel| blk: {
7291 const casted = try sema.coerce(block, elem_type, sentinel, sentinel.src);
7305 const slice_sentinel = if (sentinel_opt != .none) blk: {
7306 const casted = try sema.coerce(block, elem_type, sentinel_opt, sentinel_src);
72927307 break :blk try sema.resolveConstValue(block, sentinel_src, casted);
72937308 } else null;
72947309
72957310 var return_ptr_size: std.builtin.TypeInfo.Pointer.Size = .Slice;
72967311 var return_elem_type = elem_type;
7297 if (end_opt) |end| {
7298 if (try sema.resolveDefinedValue(block, src, end)) |end_val| {
7312 if (end_opt != .none) {
7313 if (try sema.resolveDefinedValue(block, src, end_opt)) |end_val| {
72997314 if (try sema.resolveDefinedValue(block, src, start)) |start_val| {
73007315 const start_u64 = start_val.toUnsignedInt();
73017316 const end_u64 = end_val.toUnsignedInt();
......@@ -7316,7 +7331,7 @@ fn analyzeSlice(
73167331 const return_type = try sema.mod.ptrType(
73177332 sema.arena,
73187333 return_elem_type,
7319 if (end_opt == null) slice_sentinel else null,
7334 if (end_opt == .none) slice_sentinel else null,
73207335 0, // TODO alignment
73217336 0,
73227337 0,
......@@ -7341,8 +7356,8 @@ fn cmpNumeric(
73417356 lhs_src: LazySrcLoc,
73427357 rhs_src: LazySrcLoc,
73437358) CompileError!Air.Inst.Ref {
7344 const lhs_ty = sema.getTypeOf(lhs);
7345 const rhs_ty = sema.getTypeOf(rhs);
7359 const lhs_ty = sema.typeOf(lhs);
7360 const rhs_ty = sema.typeOf(rhs);
73467361
73477362 assert(lhs_ty.isNumeric());
73487363 assert(rhs_ty.isNumeric());
......@@ -7609,14 +7624,14 @@ fn resolvePeerTypes(
76097624 return Type.initTag(.noreturn);
76107625
76117626 if (instructions.len == 1)
7612 return sema.getTypeOf(instructions[0]);
7627 return sema.typeOf(instructions[0]);
76137628
76147629 const target = sema.mod.getTarget();
76157630
76167631 var chosen = instructions[0];
76177632 for (instructions[1..]) |candidate| {
7618 const candidate_ty = sema.getTypeOf(candidate);
7619 const chosen_ty = sema.getTypeOf(chosen);
7633 const candidate_ty = sema.typeOf(candidate);
7634 const chosen_ty = sema.typeOf(chosen);
76207635 if (candidate_ty.eql(chosen_ty))
76217636 continue;
76227637 if (candidate_ty.zigTypeTag() == .NoReturn)
......@@ -7677,7 +7692,7 @@ fn resolvePeerTypes(
76777692 return sema.mod.fail(&block.base, src, "incompatible types: '{}' and '{}'", .{ chosen_ty, candidate_ty });
76787693 }
76797694
7680 return sema.getTypeOf(chosen);
7695 return sema.typeOf(chosen);
76817696}
76827697
76837698fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type {
......@@ -7753,7 +7768,7 @@ fn getBuiltin(
77537768 "builtin",
77547769 );
77557770 const builtin_inst = try sema.analyzeLoad(block, src, opt_builtin_inst.?, src);
7756 const builtin_ty = try sema.resolveAirAsType(block, src, builtin_inst);
7771 const builtin_ty = try sema.analyzeAsType(block, src, builtin_inst);
77577772 const opt_ty_inst = try sema.analyzeNamespaceLookup(
77587773 block,
77597774 src,
......@@ -7770,7 +7785,7 @@ fn getBuiltinType(
77707785 name: []const u8,
77717786) CompileError!Type {
77727787 const ty_inst = try sema.getBuiltin(block, src, name);
7773 return sema.resolveAirAsType(block, src, ty_inst);
7788 return sema.analyzeAsType(block, src, ty_inst);
77747789}
77757790
77767791/// There is another implementation of this in `Type.onePossibleValue`. This one
......@@ -7970,70 +7985,104 @@ fn enumFieldSrcLoc(
79707985 } else unreachable;
79717986}
79727987
7988/// This is only meant to be called by `typeOf`.
7989fn analyzeAsTypeInfallible(sema: *Sema, inst: Air.Inst.Ref) Type {
7990 var i: usize = @enumToInt(inst);
7991 if (i < Air.Inst.Ref.typed_value_map.len) {
7992 return Air.Inst.Ref.typed_value_map[i].val.toType(undefined) catch unreachable;
7993 }
7994 i -= Air.Inst.Ref.typed_value_map.len;
7995 assert(sema.air_instructions.items(.tag)[i] == .const_ty);
7996 return sema.air_instructions.items(.data)[i].ty;
7997}
7998
79737999/// Returns the type of the AIR instruction.
7974fn getTypeOf(sema: *Sema, air_ref: Air.Inst.Ref) Type {
7975 switch (air_ref) {
7976 .none => unreachable,
7977 .u8_type => return Type.initTag(.u8),
7978 .i8_type => return Type.initTag(.i8),
7979 .u16_type => return Type.initTag(.u16),
7980 .i16_type => return Type.initTag(.i16),
7981 .u32_type => return Type.initTag(.u32),
7982 .i32_type => return Type.initTag(.i32),
7983 .u64_type => return Type.initTag(.u64),
7984 .i64_type => return Type.initTag(.i64),
7985 .u128_type => return Type.initTag(.u128),
7986 .i128_type => return Type.initTag(.i128),
7987 .usize_type => return Type.initTag(.usize),
7988 .isize_type => return Type.initTag(.isize),
7989 .c_short_type => return Type.initTag(.c_short),
7990 .c_ushort_type => return Type.initTag(.c_ushort),
7991 .c_int_type => return Type.initTag(.c_int),
7992 .c_uint_type => return Type.initTag(.c_uint),
7993 .c_long_type => return Type.initTag(.c_long),
7994 .c_ulong_type => return Type.initTag(.c_ulong),
7995 .c_longlong_type => return Type.initTag(.c_longlong),
7996 .c_ulonglong_type => return Type.initTag(.c_ulonglong),
7997 .c_longdouble_type => return Type.initTag(.c_longdouble),
7998 .f16_type => return Type.initTag(.f16),
7999 .f32_type => return Type.initTag(.f32),
8000 .f64_type => return Type.initTag(.f64),
8001 .f128_type => return Type.initTag(.f128),
8002 .c_void_type => return Type.initTag(.c_void),
8003 .bool_type => return Type.initTag(.bool),
8004 .void_type => return Type.initTag(.void),
8005 .type_type => return Type.initTag(.type),
8006 .anyerror_type => return Type.initTag(.anyerror),
8007 .comptime_int_type => return Type.initTag(.comptime_int),
8008 .comptime_float_type => return Type.initTag(.comptime_float),
8009 .noreturn_type => return Type.initTag(.noreturn),
8010 .anyframe_type => return Type.initTag(.@"anyframe"),
8011 .null_type => return Type.initTag(.@"null"),
8012 .undefined_type => return Type.initTag(.@"undefined"),
8013 .enum_literal_type => return Type.initTag(.enum_literal),
8014 .atomic_ordering_type => return Type.initTag(.atomic_ordering),
8015 .atomic_rmw_op_type => return Type.initTag(.atomic_rmw_op),
8016 .calling_convention_type => return Type.initTag(.calling_convention),
8017 .float_mode_type => return Type.initTag(.float_mode),
8018 .reduce_op_type => return Type.initTag(.reduce_op),
8019 .call_options_type => return Type.initTag(.call_options),
8020 .export_options_type => return Type.initTag(.export_options),
8021 .extern_options_type => return Type.initTag(.extern_options),
8022 .manyptr_u8_type => return Type.initTag(.manyptr_u8),
8023 .manyptr_const_u8_type => return Type.initTag(.manyptr_const_u8),
8024 .fn_noreturn_no_args_type => return Type.initTag(.fn_noreturn_no_args),
8025 .fn_void_no_args_type => return Type.initTag(.fn_void_no_args),
8026 .fn_naked_noreturn_no_args_type => return Type.initTag(.fn_naked_noreturn_no_args),
8027 .fn_ccc_void_no_args_type => return Type.initTag(.fn_ccc_void_no_args),
8028 .single_const_pointer_to_comptime_int_type => return Type.initTag(.single_const_pointer_to_comptime_int),
8029 .const_slice_u8_type => return Type.initTag(.const_slice_u8),
8030 else => {},
8000fn typeOf(sema: *Sema, inst: Air.Inst.Ref) Type {
8001 var i: usize = @enumToInt(inst);
8002 if (i < Air.Inst.Ref.typed_value_map.len) {
8003 return Air.Inst.Ref.typed_value_map[i].ty;
80318004 }
8032 const air_index = @as(usize, @enumToInt(air_ref)) - Air.Inst.Ref.typed_value_map.len;
8033 const air_tags = sema.air_instructions.items(.tag);
8005 i -= Air.Inst.Ref.typed_value_map.len;
8006
80348007 const air_datas = sema.air_instructions.items(.data);
8035 assert(air_tags[air_index] == .const_ty);
8036 return air_datas[air_index].ty;
8008 switch (sema.air_instructions.items(.tag)[i]) {
8009 .arg => return sema.analyzeAsTypeInfallible(air_datas[i].ty_str.ty),
8010
8011 .add,
8012 .addwrap,
8013 .sub,
8014 .subwrap,
8015 .mul,
8016 .mulwrap,
8017 .div,
8018 .bit_and,
8019 .bit_or,
8020 .xor,
8021 => return sema.typeOf(air_datas[i].bin_op.lhs),
8022
8023 .cmp_lt,
8024 .cmp_lte,
8025 .cmp_eq,
8026 .cmp_gte,
8027 .cmp_gt,
8028 .cmp_neq,
8029 .is_null,
8030 .is_non_null,
8031 .is_null_ptr,
8032 .is_non_null_ptr,
8033 .is_err,
8034 .is_non_err,
8035 .is_err_ptr,
8036 .is_non_err_ptr,
8037 .bool_and,
8038 .bool_or,
8039 => return Type.initTag(.bool),
8040
8041 .const_ty => return Type.initTag(.type),
8042
8043 .alloc => return air_datas[i].ty,
8044
8045 .assembly,
8046 .block,
8047 .constant,
8048 .varptr,
8049 .struct_field_ptr,
8050 => return sema.analyzeAsTypeInfallible(air_datas[i].ty_pl.ty),
8051
8052 .not,
8053 .bitcast,
8054 .load,
8055 .ref,
8056 .floatcast,
8057 .intcast,
8058 .optional_payload,
8059 .optional_payload_ptr,
8060 .wrap_optional,
8061 .unwrap_errunion_payload,
8062 .unwrap_errunion_err,
8063 .unwrap_errunion_payload_ptr,
8064 .unwrap_errunion_err_ptr,
8065 .wrap_errunion_payload,
8066 .wrap_errunion_err,
8067 => return sema.analyzeAsTypeInfallible(air_datas[i].ty_op.ty),
8068
8069 .loop,
8070 .br,
8071 .cond_br,
8072 .switch_br,
8073 .ret,
8074 .unreach,
8075 => return Type.initTag(.noreturn),
8076
8077 .breakpoint,
8078 .dbg_stmt,
8079 .store,
8080 => return Type.initTag(.void),
8081
8082 .ptrtoint => return Type.initTag(.usize),
8083
8084 .call => @panic("TODO Sema.typeOf call"),
8085 }
80378086}
80388087
80398088pub fn addType(sema: *Sema, ty: Type) !Air.Inst.Ref {