mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:34:43 +01:00
Construct LocId from InstId directly (explicitly) instead of doing lookups when possible (#5355)
Remove calls to `InstStore::GetLocId()` to build a LocId from an InstId now that they can be constructed directly from the InstId. Most uses of LocId are just plumbing, so this does not affect them. However places that want to look inside the LocId do not want to work with the InstId form. In these places, introduce `InstStore::GetResolvedLocId()` which converts a LocId (or an InstId as an optimization) into a LocId which is not backed by an InstId. These locations can be printed (they have a line and column when they are a NodeId), they can have flags added to them (`ToImplicit`, `ToTokenOnly`), they can be converted to an underlying ImportIRInstId, or they may be `None`. `Dump()` is made to print a resolved location instead of printing the InstId in the location, since (at least in my experience) the resolved location is what is interesting in debugging, and this saves manual `MakeInstId` steps in the debugger every time a location is of interest. The LocId constructor from InstId is made `explicit` to add clarity to function calls passing an `inst_id` now directly instead of calling `context.insts().GetLocId(inst_id)`. To avoid needing to construct `SemIR::LocId(...)` explicitly in all cases though, the diagnostics code in Check uses `DiagnosticLocId` as its template parameter which accepts InstId as well and does the construction of LocId from it. Because LocId now requires an explicit construction from InstId, any callers to `AddInst()` functions will have to explicitly convert to LocId if they had an InstId, but not if they pass a NodeId. To make this difference clear to callers, we `requires` that the input type can be converted to LocId. This ensures that passing an InstId results in an error at the callsite where the InstId is passed, instead of generating a compiler error when trying to construct `LocIdAndInst` inside `AddInst()`, which is less clear about what went wrong and doesn't seem entirely intentional. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
fafb655d39
commit
315e206ff1
@@ -88,8 +88,7 @@ class MatchContext {
|
||||
|
||||
// Implementations of `EmitPatternMatch` for particular pattern inst kinds.
|
||||
// The pattern argument is always equal to
|
||||
// `context.insts().Get(entry.pattern_id)`, and `pattern_loc_id` is always
|
||||
// equal to `context.insts().GetLocId(entry.pattern_id)`.
|
||||
// `context.insts().Get(entry.pattern_id)`.
|
||||
auto DoEmitPatternMatch(Context& context,
|
||||
SemIR::AnyBindingPattern binding_pattern,
|
||||
SemIR::InstId pattern_inst_id, WorkItem entry)
|
||||
@@ -161,7 +160,6 @@ auto MatchContext::DoWork(Context& context) -> SemIR::InstBlockId {
|
||||
static auto InsertHere(Context& context, SemIR::ExprRegionId region_id)
|
||||
-> SemIR::InstId {
|
||||
auto region = context.sem_ir().expr_regions().Get(region_id);
|
||||
auto loc_id = context.insts().GetLocId(region.result_id);
|
||||
auto exit_block = context.inst_blocks().Get(region.block_ids.back());
|
||||
if (region.block_ids.size() == 1) {
|
||||
// TODO: Is it possible to avoid leaving an "orphan" block in the IR in the
|
||||
@@ -174,13 +172,13 @@ static auto InsertHere(Context& context, SemIR::ExprRegionId region_id)
|
||||
return region.result_id;
|
||||
}
|
||||
return AddInst<SemIR::SpliceBlock>(
|
||||
context, loc_id,
|
||||
context, SemIR::LocId(region.result_id),
|
||||
{.type_id = context.insts().Get(region.result_id).type_id(),
|
||||
.block_id = region.block_ids.front(),
|
||||
.result_id = region.result_id});
|
||||
}
|
||||
if (context.region_stack().empty()) {
|
||||
context.TODO(loc_id,
|
||||
context.TODO(region.result_id,
|
||||
"Control flow expressions are currently only supported inside "
|
||||
"functions.");
|
||||
return SemIR::ErrorInst::InstId;
|
||||
@@ -195,7 +193,8 @@ static auto InsertHere(Context& context, SemIR::ExprRegionId region_id)
|
||||
context.insts().GetAs<SemIR::Branch>(exit_block.back()).target_id;
|
||||
CARBON_CHECK(context.inst_blocks().GetOrEmpty(resume_with_block_id).empty());
|
||||
context.inst_block_stack().Push(resume_with_block_id);
|
||||
context.region_stack().AddToRegion(resume_with_block_id, loc_id);
|
||||
context.region_stack().AddToRegion(resume_with_block_id,
|
||||
SemIR::LocId(region.result_id));
|
||||
return region.result_id;
|
||||
}
|
||||
|
||||
@@ -221,8 +220,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
auto value_id = SemIR::InstId::None;
|
||||
if (kind_ == MatchKind::Local) {
|
||||
value_id =
|
||||
Convert(context, context.insts().GetLocId(entry.scrutinee_id),
|
||||
entry.scrutinee_id,
|
||||
Convert(context, SemIR::LocId(entry.scrutinee_id), entry.scrutinee_id,
|
||||
{.kind = bind_name_id.has_value() ? ConversionTarget::ValueOrRef
|
||||
: ConversionTarget::Discarded,
|
||||
.type_id = context.insts().Get(bind_name_id).type_id()});
|
||||
@@ -264,7 +262,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
CARBON_DIAGNOSTIC(AddrSelfIsNonRef, Error,
|
||||
"`addr self` method cannot be invoked on a value");
|
||||
context.emitter().Emit(
|
||||
TokenOnly(context.insts().GetLocId(entry.scrutinee_id)),
|
||||
context.insts().GetCanonicalLocId(entry.scrutinee_id).ToTokenOnly(),
|
||||
AddrSelfIsNonRef);
|
||||
// Add fake reference expression to preserve invariants.
|
||||
auto scrutinee = context.insts().GetWithLocId(entry.scrutinee_id);
|
||||
@@ -275,7 +273,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
auto scrutinee_ref_type_inst_id =
|
||||
context.types().GetInstId(scrutinee_ref.type_id());
|
||||
auto new_scrutinee = AddInst<SemIR::AddrOf>(
|
||||
context, context.insts().GetLocId(scrutinee_ref_id),
|
||||
context, SemIR::LocId(scrutinee_ref_id),
|
||||
{.type_id = GetPointerType(context, scrutinee_ref_type_inst_id),
|
||||
.lvalue_id = scrutinee_ref_id});
|
||||
AddWork({.pattern_id = addr_pattern.inner_id, .scrutinee_id = new_scrutinee});
|
||||
@@ -296,8 +294,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
results_.push_back(SemIR::ErrorInst::InstId);
|
||||
} else {
|
||||
results_.push_back(ConvertToValueOfType(
|
||||
context, context.insts().GetLocId(entry.scrutinee_id),
|
||||
entry.scrutinee_id,
|
||||
context, SemIR::LocId(entry.scrutinee_id), entry.scrutinee_id,
|
||||
ExtractScrutineeType(
|
||||
context.sem_ir(),
|
||||
SemIR::GetTypeOfInstInSpecific(
|
||||
@@ -312,7 +309,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
param_pattern.index = NextRuntimeIndex();
|
||||
ReplaceInstBeforeConstantUse(context, entry.pattern_id, param_pattern);
|
||||
auto param_id = AddInst<SemIR::ValueParam>(
|
||||
context, context.insts().GetLocId(pattern_inst_id),
|
||||
context, SemIR::LocId(pattern_inst_id),
|
||||
{.type_id =
|
||||
ExtractScrutineeType(context.sem_ir(), param_pattern.type_id),
|
||||
.index = param_pattern.index,
|
||||
@@ -354,7 +351,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
param_pattern.index = NextRuntimeIndex();
|
||||
ReplaceInstBeforeConstantUse(context, entry.pattern_id, param_pattern);
|
||||
auto param_id = AddInst<SemIR::RefParam>(
|
||||
context, context.insts().GetLocId(pattern_inst_id),
|
||||
context, SemIR::LocId(pattern_inst_id),
|
||||
{.type_id =
|
||||
ExtractScrutineeType(context.sem_ir(), param_pattern.type_id),
|
||||
.index = param_pattern.index,
|
||||
@@ -400,7 +397,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
param_pattern.index = NextRuntimeIndex();
|
||||
ReplaceInstBeforeConstantUse(context, entry.pattern_id, param_pattern);
|
||||
auto param_id = AddInst<SemIR::OutParam>(
|
||||
context, context.insts().GetLocId(pattern_inst_id),
|
||||
context, SemIR::LocId(pattern_inst_id),
|
||||
{.type_id =
|
||||
ExtractScrutineeType(context.sem_ir(), param_pattern.type_id),
|
||||
.index = param_pattern.index,
|
||||
@@ -424,7 +421,7 @@ auto MatchContext::DoEmitPatternMatch(
|
||||
auto type_id =
|
||||
ExtractScrutineeType(context.sem_ir(), return_slot_pattern.type_id);
|
||||
auto return_slot_id = AddInst<SemIR::ReturnSlot>(
|
||||
context, context.insts().GetLocId(pattern_inst_id),
|
||||
context, SemIR::LocId(pattern_inst_id),
|
||||
{.type_id = type_id,
|
||||
.type_inst_id = context.types().GetInstId(type_id),
|
||||
.storage_id = entry.scrutinee_id});
|
||||
@@ -459,7 +456,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
}
|
||||
case MatchKind::Caller: {
|
||||
storage_id = AddInstWithCleanup<SemIR::TemporaryStorage>(
|
||||
context, context.insts().GetLocId(pattern_inst_id),
|
||||
context, SemIR::LocId(pattern_inst_id),
|
||||
{.type_id =
|
||||
ExtractScrutineeType(context.sem_ir(), var_pattern.type_id)});
|
||||
CARBON_CHECK(entry.scrutinee_id.has_value());
|
||||
@@ -473,11 +470,11 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
context.global_init().Resume();
|
||||
}
|
||||
if (entry.scrutinee_id.has_value()) {
|
||||
auto loc_id = context.insts().GetLocId(pattern_inst_id);
|
||||
auto init_id = Initialize(context, loc_id, storage_id, entry.scrutinee_id);
|
||||
auto init_id = Initialize(context, SemIR::LocId(pattern_inst_id),
|
||||
storage_id, entry.scrutinee_id);
|
||||
// TODO: Consider using different instruction kinds for assignment
|
||||
// versus initialization.
|
||||
AddInst<SemIR::Assign>(context, loc_id,
|
||||
AddInst<SemIR::Assign>(context, SemIR::LocId(pattern_inst_id),
|
||||
{.lhs_id = storage_id, .rhs_id = init_id});
|
||||
}
|
||||
AddWork(
|
||||
@@ -529,9 +526,9 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
|
||||
|
||||
auto tuple_type_id =
|
||||
ExtractScrutineeType(context.sem_ir(), tuple_pattern.type_id);
|
||||
auto converted_scrutinee = ConvertToValueOrRefOfType(
|
||||
context, context.insts().GetLocId(pattern_inst_id), entry.scrutinee_id,
|
||||
tuple_type_id);
|
||||
auto converted_scrutinee =
|
||||
ConvertToValueOrRefOfType(context, SemIR::LocId(pattern_inst_id),
|
||||
entry.scrutinee_id, tuple_type_id);
|
||||
if (auto scrutinee_value =
|
||||
context.insts().TryGetAs<SemIR::TupleValue>(converted_scrutinee)) {
|
||||
add_all_subscrutinees(
|
||||
|
||||
Reference in New Issue
Block a user