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:
Richard Smith
2024-11-14 00:27:40 +00:00
committed by GitHub
parent abd12c18c7
commit cbd88e5c72
9 changed files with 2923 additions and 18 deletions
@@ -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