|
| 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