Change how to get info for a parameter (#4366)

Updates `SemIR::Function::GetParamFromParamRefId` to return more
information in the form of a new `ParamInfo` struct. This struct has a
method for getting the `NameId` from the name binding instruction. The
callers previously got it from the `Param` instruction, but the plan is
for that instruction to no longer be associated with a name.

---------

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
josh11b
2024-10-04 16:30:00 +00:00
committed by GitHub
co-authored by Josh L Geoff Romer
parent 568ad197d1
commit 82937e1a3c
8 changed files with 64 additions and 67 deletions
+11 -30
View File
@@ -78,35 +78,15 @@ static auto CheckFunctionSignature(Context& context,
for (auto param_id : llvm::concat<const SemIR::InstId>(
context.inst_blocks().GetOrEmpty(name_and_params.implicit_params_id),
context.inst_blocks().GetOrEmpty(name_and_params.params_id))) {
auto param = context.insts().Get(param_id);
// Find the parameter in the pattern.
// TODO: This duplicates work done by Function::GetParamFromParamRefId.
if (auto addr_pattern = param.TryAs<SemIR::AddrPattern>()) {
param_id = addr_pattern->inner_id;
param = context.insts().Get(param_id);
}
auto bind_name = param.TryAs<SemIR::AnyBindName>();
if (bind_name) {
param_id = bind_name->value_id;
param = context.insts().Get(param_id);
}
auto param_inst = param.TryAs<SemIR::Param>();
if (!param_inst) {
// Once we support more generalized patterns we will need to diagnose
// parameters with unsupported patterns.
context.TODO(param_id, "unexpected syntax for parameter");
// TODO: Also repair the param ID so downstream code doesn't need to deal
// with this.
continue;
}
auto param_info =
SemIR::Function::GetParamFromParamRefId(context.sem_ir(), param_id);
// If this is a runtime parameter, number it.
if (bind_name && bind_name->kind == SemIR::BindName::Kind) {
param_inst->runtime_index = next_index;
context.ReplaceInstBeforeConstantUse(param_id, *param_inst);
if (param_info.bind_name &&
param_info.bind_name->kind == SemIR::BindName::Kind) {
param_info.inst.runtime_index = next_index;
context.ReplaceInstBeforeConstantUse(param_info.inst_id, param_info.inst);
++next_index.index;
}
}
@@ -380,17 +360,18 @@ static auto HandleFunctionDefinitionAfterSignature(
for (auto param_ref_id : llvm::concat<const SemIR::InstId>(
context.inst_blocks().GetOrEmpty(function.implicit_param_refs_id),
context.inst_blocks().GetOrEmpty(function.param_refs_id))) {
auto [param_id, param] =
auto param_info =
SemIR::Function::GetParamFromParamRefId(context.sem_ir(), param_ref_id);
// The parameter types need to be complete.
context.TryToCompleteType(param.type_id, [&] {
context.TryToCompleteType(param_info.inst.type_id, [&] {
CARBON_DIAGNOSTIC(
IncompleteTypeInFunctionParam, Error,
"parameter has incomplete type `{0}` in function definition",
SemIR::TypeId);
return context.emitter().Build(param_id, IncompleteTypeInFunctionParam,
param.type_id);
return context.emitter().Build(param_info.inst_id,
IncompleteTypeInFunctionParam,
param_info.inst.type_id);
});
}