mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 23:02:42 +00:00
Name scopes store the names in their scope in a `DenseMap`. Several places reasonably avoid depending on the iteration order by sorting the names -- they're in the formatting code path where that's a solid approach. Unfortunately, when we're importing one scope into another, we also need to walk the entire scope and do something for each name. =[ This doesn't seem like a great place to sort things to stabilize them. I've switched to a fairly simplistic solution of having a vector of name entries that can be iterated stably, and a separate map for lookups. I didn't use the set-of-indices trick here because it's not clear that's the right trade-off for a scope: likely a lot of small scopes here with relatively hot name lookups. And the key here isn't a large or dynamically sized thing that we're canonicalizing, it's a `NameId`. That made me lean towards duplicating the name in the hashtable for lookup and the vector for iteration. I thought about a fancy approach of sorting the hashtable keys by their values (the indices), but that would still require a bit of copying and more code. I also thought a bit about other optimizations, but decided to leave a comment for now -- it's not obvious to me exactly how hot this is and whether it's better served by faster lookups, being more memory dense, etc. And that might involve more of an SOA layout change or some other approach. Rather than do that here, and especially before switching hashtables, I stuck with a simple approach to address the ordering. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
90 lines
3.3 KiB
C++
90 lines
3.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/handle.h"
|
|
#include "toolchain/check/modifiers.h"
|
|
#include "toolchain/check/name_component.h"
|
|
#include "toolchain/parse/typed_nodes.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleExportIntroducer(Context& context,
|
|
Parse::ExportIntroducerId /*node_id*/) -> bool {
|
|
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 HandleExportDecl(Context& context, Parse::ExportDeclId node_id) -> bool {
|
|
auto name_context = context.decl_name_stack().FinishName(
|
|
PopNameComponentWithoutParams(context, Lex::TokenKind::Export));
|
|
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;
|
|
}
|
|
|
|
auto inst_id = name_context.prev_inst_id();
|
|
if (!inst_id.is_valid()) {
|
|
context.DiagnoseNameNotFound(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.
|
|
.Note(context.insts().GetLocId(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 = context.AddInst<SemIR::ExportDecl>(
|
|
node_id, {.type_id = import_ref->type_id,
|
|
.bind_name_id = import_ref->bind_name_id,
|
|
.value_id = inst_id});
|
|
context.AddExport(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 bind_name = context.bind_names().Get(import_ref->bind_name_id);
|
|
auto& parent_scope = context.name_scopes().Get(bind_name.parent_scope_id);
|
|
auto it = parent_scope.name_map.find(bind_name.name_id);
|
|
auto& scope_inst_id = parent_scope.names[it->second].inst_id;
|
|
CARBON_CHECK(scope_inst_id == inst_id);
|
|
scope_inst_id = export_id;
|
|
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|