Files
carbon-lang/toolchain/sem_ir/absolute_node_id.cpp
T
Dana JansensandJon Ross-Perkins 315e206ff1 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>
2025-04-28 19:06:24 +00:00

152 lines
5.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/sem_ir/absolute_node_id.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
// Notes an import on the diagnostic and updates cursors to point at the
// imported IR.
static auto FollowImportRef(
llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids,
const File*& cursor_ir, InstId& cursor_inst_id,
ImportIRInstId import_ir_inst_id) -> void {
auto import_ir_inst = cursor_ir->import_ir_insts().Get(import_ir_inst_id);
CARBON_CHECK(import_ir_inst.ir_id() != ImportIRId::Cpp);
const auto& import_ir = cursor_ir->import_irs().Get(import_ir_inst.ir_id());
CARBON_CHECK(import_ir.decl_id.has_value(),
"If we get `None` locations here, we may need to more "
"thoroughly track ImportDecls.");
auto import_loc_id = cursor_ir->insts().GetCanonicalLocId(import_ir.decl_id);
switch (import_loc_id.kind()) {
case LocId::Kind::None:
break;
case LocId::Kind::ImportIRInstId: {
// For implicit imports, we need to unravel the location a little
// further.
auto implicit_import_ir_inst =
cursor_ir->import_ir_insts().Get(import_loc_id.import_ir_inst_id());
const auto& implicit_ir =
cursor_ir->import_irs().Get(implicit_import_ir_inst.ir_id());
auto implicit_loc_id = implicit_ir.sem_ir->insts().GetCanonicalLocId(
implicit_import_ir_inst.inst_id());
CARBON_CHECK(implicit_loc_id.kind() == LocId::Kind::NodeId,
"Should only be one layer of implicit imports");
absolute_node_ids.push_back(
{.check_ir_id = implicit_ir.sem_ir->check_ir_id(),
.node_id = implicit_loc_id.node_id()});
break;
}
case LocId::Kind::InstId:
CARBON_FATAL("Unexpected LocId: {0}", import_loc_id);
case LocId::Kind::NodeId: {
// For imports in the current file, the location is simple.
absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
.node_id = import_loc_id.node_id()});
break;
}
}
cursor_ir = import_ir.sem_ir;
cursor_inst_id = import_ir_inst.inst_id();
}
// Returns true if this is the final parse node location. If the location is is
// an import, follows it and returns false.
static auto HandleLocId(llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids,
const File*& cursor_ir, InstId& cursor_inst_id,
LocId loc_id) -> bool {
switch (loc_id.kind()) {
case LocId::Kind::ImportIRInstId: {
FollowImportRef(absolute_node_ids, cursor_ir, cursor_inst_id,
loc_id.import_ir_inst_id());
return false;
}
case LocId::Kind::NodeId: {
// Parse nodes always refer to the current IR.
absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
.node_id = loc_id.node_id()});
return true;
}
case LocId::Kind::None:
case LocId::Kind::InstId:
CARBON_FATAL("Unexpected LocId: {0}", loc_id);
}
}
// Loops through imported instructions until the actual instruction is found.
static auto GetAbsoluteNodeIdImpl(
llvm::SmallVector<AbsoluteNodeId>& absolute_node_ids, const File* cursor_ir,
InstId cursor_inst_id) -> void {
while (cursor_inst_id.has_value()) {
auto cursor_inst = cursor_ir->insts().Get(cursor_inst_id);
if (auto bind_ref = cursor_inst.TryAs<ExportDecl>();
bind_ref && bind_ref->value_id.has_value()) {
cursor_inst_id = bind_ref->value_id;
continue;
}
// If the parse node has a value, use it for the location.
if (auto loc_id = cursor_ir->insts().GetCanonicalLocId(cursor_inst_id);
loc_id.has_value()) {
if (HandleLocId(absolute_node_ids, cursor_ir, cursor_inst_id, loc_id)) {
return;
}
continue;
}
// If a namespace has an instruction for an import, switch to looking at it.
if (auto ns = cursor_inst.TryAs<Namespace>()) {
if (ns->import_id.has_value()) {
cursor_inst_id = ns->import_id;
continue;
}
}
break;
}
// `None` parse node but not an import; just nothing to point at.
absolute_node_ids.push_back({.check_ir_id = cursor_ir->check_ir_id(),
.node_id = Parse::NodeId::None});
}
auto GetAbsoluteNodeId(const File* sem_ir, LocId loc_id)
-> llvm::SmallVector<AbsoluteNodeId> {
llvm::SmallVector<AbsoluteNodeId> absolute_node_ids;
switch (loc_id.kind()) {
case LocId::Kind::None:
absolute_node_ids.push_back({.check_ir_id = sem_ir->check_ir_id(),
.node_id = Parse::NodeId::None});
break;
case LocId::Kind::InstId:
GetAbsoluteNodeIdImpl(absolute_node_ids, sem_ir, loc_id.inst_id());
break;
case LocId::Kind::ImportIRInstId:
case LocId::Kind::NodeId: {
const File* cursor_ir = sem_ir;
InstId cursor_inst_id = InstId::None;
if (HandleLocId(absolute_node_ids, cursor_ir, cursor_inst_id,
cursor_ir->insts().GetCanonicalLocId(loc_id))) {
break;
}
CARBON_CHECK(cursor_inst_id.has_value(), "Should be set by HandleLocId");
GetAbsoluteNodeIdImpl(absolute_node_ids, cursor_ir, cursor_inst_id);
break;
}
}
return absolute_node_ids;
}
} // namespace Carbon::SemIR