Files
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

109 lines
4.3 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/check/context.h"
#include "toolchain/check/decl_name_stack.h"
#include "toolchain/check/generic.h"
#include "toolchain/check/handle.h"
#include "toolchain/check/inst.h"
#include "toolchain/check/modifiers.h"
#include "toolchain/check/name_component.h"
#include "toolchain/check/name_lookup.h"
#include "toolchain/parse/typed_nodes.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
auto HandleParseNode(Context& context, Parse::ExportIntroducerId /*node_id*/)
-> bool {
// Export declarations can't be generic, but we might have parsed a generic
// parameter in their name, so enter a generic scope just in case.
StartGenericDecl(context);
context.decl_introducer_state_stack().Push<Lex::TokenKind::Export>();
// TODO: Probably need to update DeclNameStack to restrict to only namespaces.
context.decl_name_stack().PushScopeAndStartName();
return true;
}
auto HandleParseNode(Context& context, Parse::ExportDeclId node_id) -> bool {
auto name_context = context.decl_name_stack().FinishName(
PopNameComponentWithoutParams(context, Lex::TokenKind::Export));
DiscardGenericDecl(context);
context.decl_name_stack().PopScope();
auto introducer =
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Export>();
LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
if (name_context.state == DeclNameStack::NameContext::State::Error) {
// Should already be diagnosed.
return true;
}
// Exporting uses the decl name primarily for lookup, so treat poisoning the
// same as "not found".
auto inst_id =
name_context.state == DeclNameStack::NameContext::State::Poisoned
? SemIR::InstId::None
: name_context.prev_inst_id();
if (!inst_id.has_value()) {
DiagnoseNameNotFound(context, node_id, name_context.name_id_for_new_inst());
return true;
}
auto inst = context.insts().Get(inst_id);
if (inst.Is<SemIR::ExportDecl>()) {
CARBON_DIAGNOSTIC(ExportRedundant, Warning,
"`export` matches previous `export`");
CARBON_DIAGNOSTIC(ExportPrevious, Note, "previous `export` here");
context.emitter()
.Build(node_id, ExportRedundant)
// Use the location of the export itself, not the exported instruction.
//
// TODO: This construction of a LocId that does not just contain the
// InstId prevents GetAbsoluteNodeIdImpl() from seeing the `ExportDecl`
// instruction, which prevents it from chasing through it to the entity
// being exported. It might be nice to make this more explicit.
.Note(context.insts().GetCanonicalLocId(inst_id), ExportPrevious)
.Emit();
return true;
}
auto import_ref = context.insts().TryGetAs<SemIR::ImportRefLoaded>(inst_id);
if (!import_ref) {
CARBON_DIAGNOSTIC(ExportNotImportedEntity, Error,
"only imported entities are valid for `export`");
CARBON_DIAGNOSTIC(ExportNotImportedEntitySource, Note,
"name is declared here");
context.emitter()
.Build(node_id, ExportNotImportedEntity)
.Note(inst_id, ExportNotImportedEntitySource)
.Emit();
return true;
}
auto export_id =
AddInst<SemIR::ExportDecl>(context, node_id,
{.type_id = import_ref->type_id,
.entity_name_id = import_ref->entity_name_id,
.value_id = inst_id});
context.exports().push_back(export_id);
// Replace the ImportRef in name lookup, both for the above duplicate
// diagnostic and so that cross-package imports can find it easily.
auto entity_name = context.entity_names().Get(import_ref->entity_name_id);
auto& parent_scope = context.name_scopes().Get(entity_name.parent_scope_id);
auto& scope_result =
parent_scope.GetEntry(*parent_scope.Lookup(entity_name.name_id)).result;
CARBON_CHECK(scope_result.target_inst_id() == inst_id);
scope_result = SemIR::ScopeLookupResult::MakeFound(
export_id, scope_result.access_kind());
return true;
}
} // namespace Carbon::Check