Skip to content

Commit 9ef96ab

Browse files
jonmeowbricknerb
authored andcommitted
Add an int min_prelude (carbon-language#5578)
The general intent here is to support basic use of `i32` and similar integer types in min_prelude tests, without all the various arithmetic support. This is in support of carbon-language#5547 and carbon-language#5549. However, I was originally asking for this to be reviewed as part of carbon-language#5546 rather than either of those PRs, and I've retracted carbon-language#5546 due to [the discussion on Discord about test change complexity](https://discord.com/channels/655572317891461132/655578254970716160/1377406366200758293). So, to try to still get this in (and then merge the already-approved carbon-language#5547 and carbon-language#5549), splitting out this file. Note this is actually the form of the prelude in carbon-language#5549, which was adding negate -- it felt better to me to add that together as long as I'm splitting it out.
1 parent 0c43e50 commit 9ef96ab

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
// A minimal prelude for testing using `Int` or `i32`; required for arrays.
6+
package Core library "prelude";
7+
8+
interface As(Dest:! type) {
9+
fn Convert[self: Self]() -> Dest;
10+
}
11+
12+
interface ImplicitAs(Dest:! type) {
13+
// TODO: extend As(Dest);
14+
fn Convert[self: Self]() -> Dest;
15+
}
16+
17+
fn IntLiteral() -> type = "int_literal.make_type";
18+
19+
private fn MakeInt(size: IntLiteral()) -> type = "int.make_type_signed";
20+
21+
class Int(N:! IntLiteral()) {
22+
adapt MakeInt(N);
23+
}
24+
25+
// Conversions.
26+
27+
impl forall [To:! IntLiteral()] IntLiteral() as ImplicitAs(Int(To)) {
28+
fn Convert[self: Self]() -> Int(To) = "int.convert_checked";
29+
}
30+
31+
impl forall [From:! IntLiteral()] Int(From) as ImplicitAs(IntLiteral()) {
32+
fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
33+
}
34+
35+
// TODO: Remove these once ImplicitAs extends As.
36+
impl forall [To:! IntLiteral()] IntLiteral() as As(Int(To)) {
37+
fn Convert[self: Self]() -> Int(To) = "int.convert_checked";
38+
}
39+
40+
impl forall [From:! IntLiteral()] Int(From) as As(IntLiteral()) {
41+
fn Convert[self: Self]() -> IntLiteral() = "int.convert_checked";
42+
}
43+
44+
// Support testing negative values.
45+
46+
interface Negate {
47+
fn Op[self: Self]() -> Self;
48+
}
49+
50+
impl IntLiteral() as Negate {
51+
fn Op[self: Self]() -> Self = "int.snegate";
52+
}

0 commit comments

Comments
 (0)