Commit 18e42661dc

Andrew Kelley <andrew@ziglang.org>
2022-03-01 22:03:32
Sema: eliminate use of resolveAlreadyCoercedInt
1 parent 543bee0
Changed files (3)
src/AstGen.zig
@@ -290,6 +290,7 @@ pub const ResultLoc = union(enum) {
 };
 
 pub const align_rl: ResultLoc = .{ .ty = .u16_type };
+pub const coerced_align_rl: ResultLoc = .{ .coerced_ty = .u16_type };
 pub const bool_rl: ResultLoc = .{ .ty = .bool_type };
 pub const type_rl: ResultLoc = .{ .ty = .type_type };
 pub const coerced_type_rl: ResultLoc = .{ .coerced_ty = .type_type };
@@ -2971,7 +2972,7 @@ fn ptrType(
         trailing_count += 1;
     }
     if (ptr_info.ast.align_node != 0) {
-        align_ref = try expr(gz, scope, align_rl, ptr_info.ast.align_node);
+        align_ref = try expr(gz, scope, coerced_align_rl, ptr_info.ast.align_node);
         trailing_count += 1;
     }
     if (ptr_info.ast.addrspace_node != 0) {
@@ -2980,8 +2981,8 @@ fn ptrType(
     }
     if (ptr_info.ast.bit_range_start != 0) {
         assert(ptr_info.ast.bit_range_end != 0);
-        bit_start_ref = try expr(gz, scope, .none, ptr_info.ast.bit_range_start);
-        bit_end_ref = try expr(gz, scope, .none, ptr_info.ast.bit_range_end);
+        bit_start_ref = try expr(gz, scope, .{ .coerced_ty = .u16_type }, ptr_info.ast.bit_range_start);
+        bit_end_ref = try expr(gz, scope, .{ .coerced_ty = .u16_type }, ptr_info.ast.bit_range_end);
         trailing_count += 2;
     }
 
@@ -7122,7 +7123,7 @@ fn builtinCall(
         .error_to_int          => return simpleUnOp(gz, scope, rl, node, .none,                               params[0], .error_to_int),
         .int_to_error          => return simpleUnOp(gz, scope, rl, node, .{ .ty = .u16_type },                params[0], .int_to_error),
         .compile_error         => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type },     params[0], .compile_error),
-        .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .ty = .u32_type },                params[0], .set_eval_branch_quota),
+        .set_eval_branch_quota => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .u32_type },        params[0], .set_eval_branch_quota),
         .enum_to_int           => return simpleUnOp(gz, scope, rl, node, .none,                               params[0], .enum_to_int),
         .bool_to_int           => return simpleUnOp(gz, scope, rl, node, bool_rl,                             params[0], .bool_to_int),
         .embed_file            => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type },     params[0], .embed_file),
@@ -7437,7 +7438,7 @@ fn builtinCall(
         },
         .Vector => {
             const result = try gz.addPlNode(.vector_type, node, Zir.Inst.Bin{
-                .lhs = try comptimeExpr(gz, scope, .{ .ty = .u32_type }, params[0]),
+                .lhs = try comptimeExpr(gz, scope, .{ .coerced_ty = .u32_type }, params[0]),
                 .rhs = try typeExpr(gz, scope, params[1]),
             });
             return rvalue(gz, rl, result, node);
src/Sema.zig
@@ -1464,26 +1464,6 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
     return error.AnalysisFail;
 }
 
-/// Appropriate to call when the coercion has already been done by result
-/// location semantics. Asserts the value fits in the provided `Int` type.
-/// Only supports `Int` types 64 bits or less.
-/// TODO don't ever call this since we're migrating towards ResultLoc.coerced_ty.
-fn resolveAlreadyCoercedInt(
-    sema: *Sema,
-    block: *Block,
-    src: LazySrcLoc,
-    zir_ref: Zir.Inst.Ref,
-    comptime Int: type,
-) !Int {
-    comptime assert(@typeInfo(Int).Int.bits <= 64);
-    const air_inst = sema.resolveInst(zir_ref);
-    const val = try sema.resolveConstValue(block, src, air_inst);
-    switch (@typeInfo(Int).Int.signedness) {
-        .signed => return @intCast(Int, val.toSignedInt()),
-        .unsigned => return @intCast(Int, val.toUnsignedInt()),
-    }
-}
-
 fn resolveAlign(
     sema: *Sema,
     block: *Block,
@@ -3380,7 +3360,7 @@ fn storeToInferredAllocComptime(
 fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
     const inst_data = sema.code.instructions.items(.data)[inst].un_node;
     const src = inst_data.src();
-    const quota = try sema.resolveAlreadyCoercedInt(block, src, inst_data.operand, u32);
+    const quota = @intCast(u32, try sema.resolveInt(block, src, inst_data.operand, Type.u32));
     if (sema.branch_quota < quota)
         sema.branch_quota = quota;
 }
@@ -5080,11 +5060,11 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
     const elem_type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
     const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
     const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
-    const len = try sema.resolveAlreadyCoercedInt(block, len_src, extra.lhs, u32);
+    const len = try sema.resolveInt(block, len_src, extra.lhs, Type.u32);
     const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs);
     try sema.checkVectorElemType(block, elem_type_src, elem_type);
     const vector_type = try Type.Tag.vector.create(sema.arena, .{
-        .len = len,
+        .len = @intCast(u32, len),
         .elem_type = elem_type,
     });
     return sema.addType(vector_type);
@@ -11341,7 +11321,8 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
     const abi_align = if (inst_data.flags.has_align) blk: {
         const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
         extra_i += 1;
-        break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u32);
+        const abi_align = try sema.resolveInt(block, .unneeded, ref, Type.u32);
+        break :blk @intCast(u32, abi_align);
     } else 0;
 
     const address_space = if (inst_data.flags.has_addrspace) blk: {
@@ -11353,13 +11334,15 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
     const bit_offset = if (inst_data.flags.has_bit_range) blk: {
         const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
         extra_i += 1;
-        break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u16);
+        const bit_offset = try sema.resolveInt(block, .unneeded, ref, Type.u16);
+        break :blk @intCast(u16, bit_offset);
     } else 0;
 
     const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {
         const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]);
         extra_i += 1;
-        break :blk try sema.resolveAlreadyCoercedInt(block, .unneeded, ref, u16);
+        const host_size = try sema.resolveInt(block, .unneeded, ref, Type.u16);
+        break :blk @intCast(u16, host_size);
     } else 0;
 
     if (host_size != 0 and bit_offset >= host_size * 8) {
src/type.zig
@@ -5063,6 +5063,7 @@ pub const Type = extern union {
     };
 
     pub const @"u8" = initTag(.u8);
+    pub const @"u16" = initTag(.u16);
     pub const @"u32" = initTag(.u32);
     pub const @"u64" = initTag(.u64);