mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 10:01:06 +01:00
Replace form insts with actions (#7100)
See [here](https://docs.google.com/document/d/1rWcueFwIfZox6GKVGxiUG4cBzjrZ6djXiIDGyJDtrE4/edit?tab=t.0) for the design doc. This also removes the default value of the `result_type_inst_id` parameter of `HandleAction`, moves it before the action in the parameter list, and documents it. This solves two problems: - The default made it easy to forget, leading to unnecessary `TypeOfInst` instructions. - When it was present, putting it after the fairly "bulky" action argument tended to make the callsite harder to read.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "toolchain/check/pattern.h"
|
||||
|
||||
#include "toolchain/base/kind_switch.h"
|
||||
#include "toolchain/check/action.h"
|
||||
#include "toolchain/check/control_flow.h"
|
||||
#include "toolchain/check/inst.h"
|
||||
#include "toolchain/check/return.h"
|
||||
@@ -102,9 +103,6 @@ auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
|
||||
SemIR::AnyBindingPattern pattern) -> BindingPatternInfo {
|
||||
SemIR::InstKind bind_name_kind;
|
||||
switch (pattern.kind) {
|
||||
case SemIR::FormBindingPattern::Kind:
|
||||
bind_name_kind = SemIR::FormBinding::Kind;
|
||||
break;
|
||||
case SemIR::RefBindingPattern::Kind:
|
||||
bind_name_kind = SemIR::RefBinding::Kind;
|
||||
break;
|
||||
@@ -117,9 +115,6 @@ auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
|
||||
case SemIR::WrapperBindingPattern::Kind: {
|
||||
auto subpattern = context.insts().Get(pattern.subpattern_id);
|
||||
CARBON_KIND_SWITCH(subpattern) {
|
||||
case SemIR::FormParamPattern::Kind:
|
||||
bind_name_kind = SemIR::FormBinding::Kind;
|
||||
break;
|
||||
case SemIR::RefParamPattern::Kind:
|
||||
case SemIR::VarPattern::Kind:
|
||||
bind_name_kind = SemIR::RefBinding::Kind;
|
||||
@@ -250,4 +245,77 @@ auto AddParamPattern(Context& context, SemIR::LocId loc_id,
|
||||
}
|
||||
}
|
||||
|
||||
auto PerformAction(Context& context, SemIR::LocId loc_id,
|
||||
SemIR::FormParamPatternAction action) -> SemIR::InstId {
|
||||
auto form_inst = context.insts().Get(
|
||||
context.constant_values().GetConstantInstId(action.form_id));
|
||||
auto type_id =
|
||||
GetPatternType(context, GetTypeComponent(context, action.form_id));
|
||||
CARBON_KIND_SWITCH(form_inst) {
|
||||
case SemIR::InitForm::Kind: {
|
||||
auto ref_param_pattern_id = AddInst(
|
||||
context,
|
||||
SemIR::LocIdAndInst::RuntimeVerified(
|
||||
context.sem_ir(), loc_id,
|
||||
SemIR::RefParamPattern{.type_id = type_id,
|
||||
.pretty_name_id = action.pretty_name_id}));
|
||||
return AddInst(context, SemIR::LocIdAndInst::RuntimeVerified(
|
||||
context.sem_ir(), loc_id,
|
||||
SemIR::VarPattern{
|
||||
.type_id = type_id,
|
||||
.subpattern_id = ref_param_pattern_id}));
|
||||
}
|
||||
case SemIR::RefForm::Kind: {
|
||||
return AddInst(
|
||||
context,
|
||||
SemIR::LocIdAndInst::RuntimeVerified(
|
||||
context.sem_ir(), loc_id,
|
||||
SemIR::RefParamPattern{.type_id = type_id,
|
||||
.pretty_name_id = action.pretty_name_id}));
|
||||
}
|
||||
case SemIR::ValueForm::Kind: {
|
||||
return AddInst(context,
|
||||
SemIR::LocIdAndInst::RuntimeVerified(
|
||||
context.sem_ir(), loc_id,
|
||||
SemIR::ValueParamPattern{
|
||||
.type_id = type_id,
|
||||
.pretty_name_id = action.pretty_name_id}));
|
||||
}
|
||||
case SemIR::ErrorInst::Kind: {
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
default:
|
||||
CARBON_FATAL("Unexpected param pattern form: {0}", form_inst);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: can we share code with FormParamPatternAction?
|
||||
auto PerformAction(Context& context, SemIR::LocId /*loc_id*/,
|
||||
SemIR::OutFormParamPatternAction action) -> SemIR::InstId {
|
||||
auto form_inst = context.insts().Get(
|
||||
context.constant_values().GetConstantInstId(action.form_id));
|
||||
auto type_id =
|
||||
GetPatternType(context, GetTypeComponent(context, action.form_id));
|
||||
CARBON_KIND_SWITCH(form_inst) {
|
||||
case SemIR::ValueForm::Kind: {
|
||||
return AddInst<SemIR::ValueReturnPattern>(
|
||||
context, SemIR::LocId(action.form_id), {.type_id = type_id});
|
||||
}
|
||||
case SemIR::RefForm::Kind: {
|
||||
return AddInst<SemIR::RefReturnPattern>(
|
||||
context, SemIR::LocId(action.form_id), {.type_id = type_id});
|
||||
}
|
||||
case CARBON_KIND(SemIR::InitForm _): {
|
||||
return AddInst<SemIR::OutParamPattern>(
|
||||
context, SemIR::LocId(action.form_id),
|
||||
{.type_id = type_id, .pretty_name_id = SemIR::NameId::ReturnSlot});
|
||||
}
|
||||
case SemIR::ErrorInst::Kind: {
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
default:
|
||||
CARBON_FATAL("unexpected inst kind: {0}", form_inst);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon::Check
|
||||
|
||||
Reference in New Issue
Block a user