Move the EvalConstantInst overloads out of eval.cpp into their own file. (#5040)

For now they're all in the same file; we might consider splitting them
further if that file gets too large.
This commit is contained in:
Richard Smith
2025-02-28 00:50:43 +00:00
committed by GitHub
parent 536bfd9cbf
commit f30fa2d3db
6 changed files with 597 additions and 509 deletions
+58
View File
@@ -10,6 +10,64 @@
namespace Carbon::Check {
// Enforces that an integer type has a valid bit width.
auto ValidateIntType(Context& context, SemIRLoc loc, SemIR::IntType result)
-> bool {
auto bit_width =
context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
if (!bit_width) {
// Symbolic bit width.
return true;
}
const auto& bit_width_val = context.ints().Get(bit_width->int_id);
if (bit_width_val.isZero() ||
(context.types().IsSignedInt(bit_width->type_id) &&
bit_width_val.isNegative())) {
CARBON_DIAGNOSTIC(IntWidthNotPositive, Error,
"integer type width of {0} is not positive", TypedInt);
context.emitter().Emit(
loc, IntWidthNotPositive,
{.type = bit_width->type_id, .value = bit_width_val});
return false;
}
if (bit_width_val.ugt(IntStore::MaxIntWidth)) {
CARBON_DIAGNOSTIC(IntWidthTooLarge, Error,
"integer type width of {0} is greater than the "
"maximum supported width of {1}",
TypedInt, int);
context.emitter().Emit(loc, IntWidthTooLarge,
{.type = bit_width->type_id, .value = bit_width_val},
IntStore::MaxIntWidth);
return false;
}
return true;
}
// Enforces that the bit width is 64 for a float.
auto ValidateFloatBitWidth(Context& context, SemIRLoc loc,
SemIR::InstId inst_id) -> bool {
auto inst = context.insts().GetAs<SemIR::IntValue>(inst_id);
if (context.ints().Get(inst.int_id) == 64) {
return true;
}
CARBON_DIAGNOSTIC(CompileTimeFloatBitWidth, Error, "bit width must be 64");
context.emitter().Emit(loc, CompileTimeFloatBitWidth);
return false;
}
// Enforces that a float type has a valid bit width.
auto ValidateFloatType(Context& context, SemIRLoc loc, SemIR::FloatType result)
-> bool {
auto bit_width =
context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
if (!bit_width) {
// Symbolic bit width.
return true;
}
return ValidateFloatBitWidth(context, loc, result.bit_width_id);
}
// Gets or forms a type_id for a type, given the instruction kind and arguments.
template <typename InstT, typename... EachArgT>
static auto GetTypeImpl(Context& context, EachArgT... each_arg)