mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:30:14 +01:00
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>
117 lines
4.7 KiB
C++
117 lines
4.7 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_introducer_state.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/check/type.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
#include "toolchain/sem_ir/name_scope.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleParseNode(Context& context, Parse::NamespaceStartId /*node_id*/)
|
|
-> bool {
|
|
// Namespaces 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);
|
|
// Optional modifiers and the name follow.
|
|
context.decl_introducer_state_stack().Push<Lex::TokenKind::Namespace>();
|
|
context.decl_name_stack().PushScopeAndStartName();
|
|
return true;
|
|
}
|
|
|
|
static auto IsNamespaceScope(Context& context, SemIR::NameScopeId name_scope_id)
|
|
-> bool {
|
|
auto [_, inst] = context.name_scopes().GetInstIfValid(name_scope_id);
|
|
return inst && inst->Is<SemIR::Namespace>();
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::NamespaceId node_id) -> bool {
|
|
auto name_context = context.decl_name_stack().FinishName(
|
|
PopNameComponentWithoutParams(context, Lex::TokenKind::Namespace));
|
|
|
|
DiscardGenericDecl(context);
|
|
|
|
auto introducer =
|
|
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Namespace>();
|
|
LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
|
|
|
|
auto namespace_inst = SemIR::Namespace{
|
|
GetSingletonType(context, SemIR::NamespaceType::TypeInstId),
|
|
SemIR::NameScopeId::None, SemIR::InstId::None};
|
|
auto namespace_id = AddPlaceholderInst(context, node_id, namespace_inst);
|
|
|
|
SemIR::ScopeLookupResult lookup_result =
|
|
context.decl_name_stack().LookupOrAddName(name_context, namespace_id,
|
|
SemIR::AccessKind::Public);
|
|
if (lookup_result.is_poisoned()) {
|
|
DiagnosePoisonedName(context, name_context.name_id_for_new_inst(),
|
|
lookup_result.poisoning_loc_id(), name_context.loc_id);
|
|
} else if (lookup_result.is_found()) {
|
|
SemIR::InstId existing_inst_id = lookup_result.target_inst_id();
|
|
if (auto existing =
|
|
context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
|
|
// If there's a name conflict with a namespace, "merge" by using the
|
|
// previous declaration. Otherwise, diagnose the issue.
|
|
|
|
// Point at the other namespace.
|
|
namespace_inst.name_scope_id = existing->name_scope_id;
|
|
|
|
if (context.name_scopes()
|
|
.Get(existing->name_scope_id)
|
|
.is_closed_import()) {
|
|
// The existing name is a package name, so this is a name conflict.
|
|
DiagnoseDuplicateName(context, name_context.name_id,
|
|
name_context.loc_id,
|
|
SemIR::LocId(existing_inst_id));
|
|
|
|
// Treat this as a local namespace name from now on to avoid further
|
|
// diagnostics.
|
|
context.name_scopes()
|
|
.Get(existing->name_scope_id)
|
|
.set_is_closed_import(false);
|
|
} else if (existing->import_id.has_value() &&
|
|
!context.insts()
|
|
.GetCanonicalLocId(existing_inst_id)
|
|
.has_value()) {
|
|
// When the name conflict is an imported namespace, fill the location ID
|
|
// so that future diagnostics point at this declaration.
|
|
SetNamespaceNodeId(context, existing_inst_id, node_id);
|
|
}
|
|
} else {
|
|
DiagnoseDuplicateName(context, name_context.name_id, name_context.loc_id,
|
|
SemIR::LocId(existing_inst_id));
|
|
}
|
|
}
|
|
|
|
// If we weren't able to merge namespaces, add a new name scope. Note this
|
|
// occurs even for duplicates where we discard the namespace, because we want
|
|
// to produce a valid constant.
|
|
if (!namespace_inst.name_scope_id.has_value()) {
|
|
namespace_inst.name_scope_id = context.name_scopes().Add(
|
|
namespace_id, name_context.name_id_for_new_inst(),
|
|
name_context.parent_scope_id);
|
|
if (!IsNamespaceScope(context, name_context.parent_scope_id)) {
|
|
CARBON_DIAGNOSTIC(NamespaceDeclNotAtTopLevel, Error,
|
|
"`namespace` declaration not at top level");
|
|
context.emitter().Emit(node_id, NamespaceDeclNotAtTopLevel);
|
|
}
|
|
}
|
|
|
|
ReplaceInstBeforeConstantUse(context, namespace_id, namespace_inst);
|
|
|
|
context.decl_name_stack().PopScope();
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|