Refactor LocId to merge in SemIRLoc (#5284)

The main goal of this is to collapse the LocId and SemIRLoc types into a
single type, eliminating the need for APIs to decide which to use. This
originated from discussion about UnwrapSemIRLoc in #5169. Although that
was removed in #5202, it's probably still a good direction for LocId.

This changes the packing of LocId to allow adding InstId, making it
tri-modal: ImportIRInstId, InstId, or NodeId. This has a side-effect of
reducing the available space for ImportIRInstId, although not by much
due to the pre-existing `ImplicitBit` behavior. If needed, we could also
probably play with packing a bit more since `ImplicitBit` really only
applies to `NodeId`, but I was trying to keep the logic a little
simpler. Note `TokenOnlyBit` can still apply to `ImportIRInstId`.

This leaves in place a typedef for SemIRLoc -- I intend to clean that up
separately.

Some Discord discussion is
[here](https://discord.com/channels/655572317891461132/655578254970716160/1353755830058745959).
This commit is contained in:
Jon Ross-Perkins
2025-04-11 13:35:58 +00:00
committed by GitHub
parent 422df75a92
commit fe29224016
15 changed files with 403 additions and 214 deletions
+30 -21
View File
@@ -108,29 +108,38 @@ LLVM_DUMP_METHOD static auto Dump(const Context& context,
LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::LocId loc_id)
-> std::string {
RawStringOstream out;
if (!loc_id.has_value()) {
out << "LocId(<none>)";
return out.TakeStr();
}
switch (loc_id.kind()) {
case SemIR::LocId::Kind::None: {
out << "LocId(<none>)";
break;
}
if (loc_id.is_node_id()) {
auto token = context.parse_tree().node_token(loc_id.node_id());
auto line = context.tokens().GetLineNumber(token);
auto col = context.tokens().GetColumnNumber(token);
const char* implicit = loc_id.is_implicit() ? " implicit" : "";
out << "LocId(" << FormatEscaped(context.sem_ir().filename()) << ":" << line
<< ":" << col << implicit << ")";
} else {
CARBON_CHECK(loc_id.is_import_ir_inst_id());
case SemIR::LocId::Kind::ImportIRInstId: {
auto import_ir_id = context.sem_ir()
.import_ir_insts()
.Get(loc_id.import_ir_inst_id())
.ir_id;
const auto* import_file =
context.sem_ir().import_irs().Get(import_ir_id).sem_ir;
out << "LocId(import from \"" << FormatEscaped(import_file->filename())
<< "\")";
break;
}
auto import_ir_id = context.sem_ir()
.import_ir_insts()
.Get(loc_id.import_ir_inst_id())
.ir_id;
const auto* import_file =
context.sem_ir().import_irs().Get(import_ir_id).sem_ir;
out << "LocId(import from \"" << FormatEscaped(import_file->filename())
<< "\")";
case SemIR::LocId::Kind::InstId: {
out << "LocId(" << SemIR::Dump(context.sem_ir(), loc_id.inst_id()) << ")";
break;
}
case SemIR::LocId::Kind::NodeId: {
auto token = context.parse_tree().node_token(loc_id.node_id());
auto line = context.tokens().GetLineNumber(token);
auto col = context.tokens().GetColumnNumber(token);
const char* implicit = loc_id.is_implicit() ? " implicit" : "";
out << "LocId(" << FormatEscaped(context.sem_ir().filename()) << ":"
<< line << ":" << col << implicit << ")";
break;
}
}
return out.TakeStr();
}