mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Add builtin for performing checked conversion between integer types. (#4523)
As a prerequisite for switching the type of int literals to be the `IntLiteral` type, add support for performing conversions of in-bounds integer constant values to other integer types in which they fit. This incidentally is our first compile-time-only builtin function, so add very minimal support for compile-time-only functions while we're here.
This commit is contained in:
@@ -202,6 +202,10 @@ constexpr BuiltinInfo FloatMakeType = {"float.make_type",
|
||||
constexpr BuiltinInfo BoolMakeType = {"bool.make_type",
|
||||
ValidateSignature<auto()->Type>};
|
||||
|
||||
// Converts between integer types, with a diagnostic if the value doesn't fit.
|
||||
constexpr BuiltinInfo IntConvertChecked = {
|
||||
"int.convert_checked", ValidateSignature<auto(AnyInt)->AnyInt>};
|
||||
|
||||
// "int.snegate": integer negation.
|
||||
constexpr BuiltinInfo IntSNegate = {"int.snegate",
|
||||
ValidateSignature<auto(IntT)->IntT>};
|
||||
@@ -373,4 +377,8 @@ auto BuiltinFunctionKind::IsValidType(const File& sem_ir,
|
||||
return ValidateFns[AsInt()](sem_ir, arg_types, return_type);
|
||||
}
|
||||
|
||||
auto BuiltinFunctionKind::IsCompTimeOnly() const -> bool {
|
||||
return *this == BuiltinFunctionKind::IntConvertChecked;
|
||||
}
|
||||
|
||||
} // namespace Carbon::SemIR
|
||||
|
||||
Reference in New Issue
Block a user