Skip to content

Commit df68bf9

Browse files
authored
Switch to using Core.BigInt as the type of the size of a type literal. (#4450)
This removes one of the few ways in which `i32` is special and gets us closer to removing it as a special case.
1 parent 7ec8cea commit df68bf9

29 files changed

+485
-448
lines changed

core/prelude/types.carbon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ export import library "prelude/types/bool";
1313

1414
fn BigInt() -> type = "big_int.make_type";
1515
fn Int32() -> type = "int.make_type_32";
16-
fn Int(size: i32) -> type = "int.make_type_signed";
17-
fn UInt(size: i32) -> type = "int.make_type_unsigned";
18-
fn Float(size: i32) -> type = "float.make_type";
16+
fn Int(size: BigInt()) -> type = "int.make_type_signed";
17+
fn UInt(size: BigInt()) -> type = "int.make_type_unsigned";
18+
fn Float(size: BigInt()) -> type = "float.make_type";

toolchain/check/handle_literal.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ static auto MakeI32Literal(Context& context, Parse::NodeId node_id,
4949
.int_id = context.ints().Add(i32_val)});
5050
}
5151

52+
// Forms an IntLiteral instruction with type `BigInt` for a given literal
53+
// integer value, which is assumed to be unsigned.
54+
static auto MakeBigIntLiteral(Context& context, Parse::NodeId node_id,
55+
IntId int_id) -> SemIR::InstId {
56+
// TODO: `IntId`s with different bit-widths are considered different values
57+
// here. Decide how we want to canonicalize these. For now this is only used
58+
// by type literals, so we rely on the lexer picking some consistent rule.
59+
return context.AddInst<SemIR::IntLiteral>(
60+
node_id,
61+
{.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::BigIntType),
62+
.int_id = int_id});
63+
}
64+
5265
auto HandleParseNode(Context& context, Parse::IntLiteralId node_id) -> bool {
5366
// Convert the literal to i32.
5467
// TODO: Form an integer literal value and a corresponding type here instead.
@@ -134,7 +147,7 @@ static auto HandleIntOrUnsignedIntTypeLiteral(Context& context,
134147
node_id, IntWidthNotMultipleOf8, int_kind.is_signed(),
135148
llvm::APSInt(context.ints().Get(size_id), /*isUnsigned=*/true));
136149
}
137-
auto width_id = MakeI32Literal(context, node_id, size_id);
150+
auto width_id = MakeBigIntLiteral(context, node_id, size_id);
138151
auto fn_inst_id = context.LookupNameInCore(
139152
node_id, int_kind == SemIR::IntKind::Signed ? "Int" : "UInt");
140153
auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {width_id});
@@ -175,7 +188,7 @@ auto HandleParseNode(Context& context, Parse::FloatTypeLiteralId node_id)
175188
}
176189
auto tok_id = context.parse_tree().node_token(node_id);
177190
auto size_id = context.tokens().GetTypeLiteralSize(tok_id);
178-
auto width_id = MakeI32Literal(context, node_id, size_id);
191+
auto width_id = MakeBigIntLiteral(context, node_id, size_id);
179192
auto fn_inst_id = context.LookupNameInCore(node_id, "Float");
180193
auto type_inst_id = PerformCall(context, node_id, fn_inst_id, {width_id});
181194
context.node_stack().Push(node_id, type_inst_id);

toolchain/check/testdata/array/base.carbon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var c: [(); 5] = ((), (), (), (), (),);
2424
// CHECK:STDOUT: %.5: type = tuple_type (i32) [template]
2525
// CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
2626
// CHECK:STDOUT: %array.1: %.3 = tuple_value (%.2) [template]
27-
// CHECK:STDOUT: %.7: i32 = int_literal 64 [template]
27+
// CHECK:STDOUT: %.7: Core.BigInt = int_literal 64 [template]
2828
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
2929
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
3030
// CHECK:STDOUT: %.8: i32 = int_literal 2 [template]
@@ -58,7 +58,7 @@ var c: [(); 5] = ((), (), (), (), (),);
5858
// CHECK:STDOUT: import Core//prelude/types/bool
5959
// CHECK:STDOUT: }
6060
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
61-
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
61+
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
6262
// CHECK:STDOUT: }
6363
// CHECK:STDOUT:
6464
// CHECK:STDOUT: file {
@@ -76,7 +76,7 @@ var c: [(); 5] = ((), (), (), (), (),);
7676
// CHECK:STDOUT: %.loc11_15: type = array_type %.loc11_14, i32 [template = constants.%.3]
7777
// CHECK:STDOUT: %a.var: ref %.3 = var a
7878
// CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
79-
// CHECK:STDOUT: %.loc12_9.1: i32 = int_literal 64 [template = constants.%.7]
79+
// CHECK:STDOUT: %.loc12_9.1: Core.BigInt = int_literal 64 [template = constants.%.7]
8080
// CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc12_9.1) [template = f64]
8181
// CHECK:STDOUT: %.loc12_14: i32 = int_literal 2 [template = constants.%.8]
8282
// CHECK:STDOUT: %.loc12_9.2: type = value_of_initializer %float.make_type [template = f64]
@@ -94,7 +94,7 @@ var c: [(); 5] = ((), (), (), (), (),);
9494
// CHECK:STDOUT:
9595
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
9696
// CHECK:STDOUT:
97-
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
97+
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
9898
// CHECK:STDOUT:
9999
// CHECK:STDOUT: fn @__global_init() {
100100
// CHECK:STDOUT: !entry:

toolchain/check/testdata/basics/builtin_types.carbon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var test_type: type = i32;
2020
// CHECK:STDOUT: %.1: type = tuple_type () [template]
2121
// CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
2222
// CHECK:STDOUT: %.2: i32 = int_literal 0 [template]
23-
// CHECK:STDOUT: %.3: i32 = int_literal 64 [template]
23+
// CHECK:STDOUT: %.3: Core.BigInt = int_literal 64 [template]
2424
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
2525
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
2626
// CHECK:STDOUT: %.4: f64 = float_literal 0.10000000000000001 [template]
@@ -42,7 +42,7 @@ var test_type: type = i32;
4242
// CHECK:STDOUT: import Core//prelude/types/bool
4343
// CHECK:STDOUT: }
4444
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
45-
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
45+
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
4646
// CHECK:STDOUT: }
4747
// CHECK:STDOUT:
4848
// CHECK:STDOUT: file {
@@ -59,7 +59,7 @@ var test_type: type = i32;
5959
// CHECK:STDOUT: %.loc11_15.2: type = converted %int.make_type_32, %.loc11_15.1 [template = i32]
6060
// CHECK:STDOUT: %test_i32.var: ref i32 = var test_i32
6161
// CHECK:STDOUT: %test_i32: ref i32 = bind_name test_i32, %test_i32.var
62-
// CHECK:STDOUT: %.loc12_15.1: i32 = int_literal 64 [template = constants.%.3]
62+
// CHECK:STDOUT: %.loc12_15.1: Core.BigInt = int_literal 64 [template = constants.%.3]
6363
// CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc12_15.1) [template = f64]
6464
// CHECK:STDOUT: %.loc12_15.2: type = value_of_initializer %float.make_type [template = f64]
6565
// CHECK:STDOUT: %.loc12_15.3: type = converted %float.make_type, %.loc12_15.2 [template = f64]
@@ -71,7 +71,7 @@ var test_type: type = i32;
7171
// CHECK:STDOUT:
7272
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
7373
// CHECK:STDOUT:
74-
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
74+
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
7575
// CHECK:STDOUT:
7676
// CHECK:STDOUT: fn @__global_init() {
7777
// CHECK:STDOUT: !entry:

toolchain/check/testdata/basics/fail_numeric_literal_overflow.carbon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let e: f64 = 5.0e39999999999999999993;
4343
// CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
4444
// CHECK:STDOUT: %.1: type = tuple_type () [template]
4545
// CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
46-
// CHECK:STDOUT: %.2: i32 = int_literal 64 [template]
46+
// CHECK:STDOUT: %.2: Core.BigInt = int_literal 64 [template]
4747
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
4848
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
4949
// CHECK:STDOUT: }
@@ -62,7 +62,7 @@ let e: f64 = 5.0e39999999999999999993;
6262
// CHECK:STDOUT: import Core//prelude/types/bool
6363
// CHECK:STDOUT: }
6464
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
65-
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
65+
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
6666
// CHECK:STDOUT: }
6767
// CHECK:STDOUT:
6868
// CHECK:STDOUT: file {
@@ -84,19 +84,19 @@ let e: f64 = 5.0e39999999999999999993;
8484
// CHECK:STDOUT: %int.make_type_32.loc27: init type = call constants.%Int32() [template = i32]
8585
// CHECK:STDOUT: %.loc27_8.1: type = value_of_initializer %int.make_type_32.loc27 [template = i32]
8686
// CHECK:STDOUT: %.loc27_8.2: type = converted %int.make_type_32.loc27, %.loc27_8.1 [template = i32]
87-
// CHECK:STDOUT: %.loc33_8.1: i32 = int_literal 64 [template = constants.%.2]
87+
// CHECK:STDOUT: %.loc33_8.1: Core.BigInt = int_literal 64 [template = constants.%.2]
8888
// CHECK:STDOUT: %float.make_type.loc33: init type = call constants.%Float(%.loc33_8.1) [template = f64]
8989
// CHECK:STDOUT: %.loc33_8.2: type = value_of_initializer %float.make_type.loc33 [template = f64]
9090
// CHECK:STDOUT: %.loc33_8.3: type = converted %float.make_type.loc33, %.loc33_8.2 [template = f64]
91-
// CHECK:STDOUT: %.loc38_8.1: i32 = int_literal 64 [template = constants.%.2]
91+
// CHECK:STDOUT: %.loc38_8.1: Core.BigInt = int_literal 64 [template = constants.%.2]
9292
// CHECK:STDOUT: %float.make_type.loc38: init type = call constants.%Float(%.loc38_8.1) [template = f64]
9393
// CHECK:STDOUT: %.loc38_8.2: type = value_of_initializer %float.make_type.loc38 [template = f64]
9494
// CHECK:STDOUT: %.loc38_8.3: type = converted %float.make_type.loc38, %.loc38_8.2 [template = f64]
9595
// CHECK:STDOUT: }
9696
// CHECK:STDOUT:
9797
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
9898
// CHECK:STDOUT:
99-
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
99+
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
100100
// CHECK:STDOUT:
101101
// CHECK:STDOUT: fn @__global_init() {
102102
// CHECK:STDOUT: !entry:

toolchain/check/testdata/basics/numeric_literals.carbon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn F() {
5151
// CHECK:STDOUT: %.13: i32 = int_literal 4 [template]
5252
// CHECK:STDOUT: %.14: i32 = int_literal 5 [template]
5353
// CHECK:STDOUT: %array.1: %.3 = tuple_value (%.5, %.6, %.5, %.5, %.7, %.7) [template]
54-
// CHECK:STDOUT: %.15: i32 = int_literal 64 [template]
54+
// CHECK:STDOUT: %.15: Core.BigInt = int_literal 64 [template]
5555
// CHECK:STDOUT: %Float.type: type = fn_type @Float [template]
5656
// CHECK:STDOUT: %Float: %Float.type = struct_value () [template]
5757
// CHECK:STDOUT: %.16: type = array_type %.2, f64 [template]
@@ -80,7 +80,7 @@ fn F() {
8080
// CHECK:STDOUT: import Core//prelude/types/bool
8181
// CHECK:STDOUT: }
8282
// CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
83-
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+57, loaded [template = constants.%Float]
83+
// CHECK:STDOUT: %import_ref.2: %Float.type = import_ref Core//prelude/types, inst+60, loaded [template = constants.%Float]
8484
// CHECK:STDOUT: }
8585
// CHECK:STDOUT:
8686
// CHECK:STDOUT: file {
@@ -129,7 +129,7 @@ fn F() {
129129
// CHECK:STDOUT: %.loc21_3.20: init %.3 = array_init (%.loc21_3.4, %.loc21_3.7, %.loc21_3.10, %.loc21_3.13, %.loc21_3.16, %.loc21_3.19) to %ints.var [template = constants.%array.1]
130130
// CHECK:STDOUT: %.loc21_4: init %.3 = converted %.loc21_3.1, %.loc21_3.20 [template = constants.%array.1]
131131
// CHECK:STDOUT: assign %ints.var, %.loc21_4
132-
// CHECK:STDOUT: %.loc22_16.1: i32 = int_literal 64 [template = constants.%.15]
132+
// CHECK:STDOUT: %.loc22_16.1: Core.BigInt = int_literal 64 [template = constants.%.15]
133133
// CHECK:STDOUT: %float.make_type: init type = call constants.%Float(%.loc22_16.1) [template = f64]
134134
// CHECK:STDOUT: %.loc22_21: i32 = int_literal 6 [template = constants.%.2]
135135
// CHECK:STDOUT: %.loc22_16.2: type = value_of_initializer %float.make_type [template = f64]
@@ -170,5 +170,5 @@ fn F() {
170170
// CHECK:STDOUT:
171171
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
172172
// CHECK:STDOUT:
173-
// CHECK:STDOUT: fn @Float(%size.param_patt: i32) -> type = "float.make_type";
173+
// CHECK:STDOUT: fn @Float(%size.param_patt: Core.BigInt) -> type = "float.make_type";
174174
// CHECK:STDOUT:

0 commit comments

Comments
 (0)