Support more types in MapConstantToAPValue by refactoring code out of ConvertArgToTemplateArg (#6923)

This doesn't change any of the current tests, but will be useful for
calling constexpr functions with bool/float params.
This commit is contained in:
Nicholas Bishop
2026-03-18 16:11:42 +00:00
committed by GitHub
parent 98e2567524
commit 0482b27c6b
3 changed files with 34 additions and 41 deletions
+20 -9
View File
@@ -160,21 +160,32 @@ auto EvalCppVarDecl(Context& context, SemIR::LocId loc_id,
return SemIR::ConstantId::NotConstant;
}
static auto ConvertConstantToAPValue(Context& context,
SemIR::InstId const_inst_id,
clang::QualType param_type)
auto MapConstantToAPValue(Context& context, SemIR::InstId const_inst_id,
clang::QualType param_type)
-> std::optional<clang::APValue> {
if (param_type->isIntegerType()) {
const bool is_signed = param_type->isSignedIntegerOrEnumerationType();
if (auto int_value =
context.insts().TryGetAs<SemIR::IntValue>(const_inst_id)) {
const auto& ap_int = context.ints().GetAtWidth(
int_value->int_id, context.ast_context().getIntWidth(param_type));
const auto& ap_int = context.ints().Get(int_value->int_id);
auto aps_int =
llvm::APSInt(ap_int, !param_type->isSignedIntegerOrEnumerationType())
llvm::APSInt(ap_int, !is_signed)
.extOrTrunc(context.ast_context().getIntWidth(param_type));
return clang::APValue(aps_int);
} else if (auto bool_value = context.insts().TryGetAs<SemIR::BoolLiteral>(
const_inst_id)) {
llvm::APInt ap_int(context.ast_context().getIntWidth(param_type),
bool_value->value.ToBool(), is_signed);
auto aps_int =
llvm::APSInt(ap_int, !is_signed)
.extOrTrunc(context.ast_context().getIntWidth(param_type));
return clang::APValue(aps_int);
}
} else if (param_type->isFloatingType()) {
if (auto float_value =
context.insts().TryGetAs<SemIR::FloatValue>(const_inst_id)) {
const auto& ap_float = context.floats().Get(float_value->float_id);
return clang::APValue(ap_float);
}
}
@@ -189,7 +200,7 @@ static auto ConvertArgToExpr(Context& context, SemIR::InstId arg_inst_id,
return nullptr;
}
auto ap_value = ConvertConstantToAPValue(context, const_inst_id, param_type);
auto ap_value = MapConstantToAPValue(context, const_inst_id, param_type);
if (!ap_value.has_value()) {
return nullptr;
}