Implement a list of exported names for imports. (#3552)

This adds a block for exported InstIds, rather than scanning the package
scope. This working down a path discussed last month, which we'll need
to add enclosing namespaces to the Inst in order to complete import of
something like `namespace NS; var NS.a;`

Exports could've been a separate `vector<InstId>` on `SemIR::File`, but
using an entry in `inst_blocks` felt more consistent.
This commit is contained in:
Jon Ross-Perkins
2024-01-02 22:17:27 +00:00
committed by GitHub
parent 13b8c33c79
commit b6ffe0197b
13 changed files with 118 additions and 51 deletions
+45 -12
View File
@@ -13,6 +13,7 @@
#include "toolchain/parse/tree.h"
#include "toolchain/parse/tree_node_location_translator.h"
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
@@ -68,6 +69,32 @@ struct UnitInfo {
llvm::SmallVector<UnitInfo*> incoming_imports;
};
// Returns the NameId for the entity. May return Invalid for a TODO.
// TODO: This will need to handle enclosing namespaces.
static auto GetImportNameId(Parse::NodeId parse_node, Context& context,
const SemIR::File& import_sem_ir,
SemIR::InstId import_inst_id) -> SemIR::NameId {
auto import_inst = import_sem_ir.insts().Get(import_inst_id);
switch (import_inst.kind()) {
case SemIR::InstKind::BindName: {
auto bind_name = import_inst.As<SemIR::BindName>();
return bind_name.name_id;
}
case SemIR::InstKind::FunctionDecl: {
auto bind_name = import_inst.As<SemIR::FunctionDecl>();
return import_sem_ir.functions().Get(bind_name.function_id).name_id;
}
default:
context.TODO(parse_node, (llvm::Twine("Support GetImportNameId of ") +
import_inst.kind().name())
.str());
return SemIR::NameId::Invalid;
}
}
// Add imports to the root block.
static auto InitPackageScopeAndImports(Context& context, UnitInfo& unit_info)
-> void {
@@ -92,33 +119,38 @@ static auto InitPackageScopeAndImports(Context& context, UnitInfo& unit_info)
for (const auto& import : self_import->second.imports) {
const auto& import_sem_ir = **import.unit_info->unit->sem_ir;
const auto& import_scope =
import_sem_ir.name_scopes().Get(SemIR::NameScopeId::Package);
// If an import of the current package caused an error for the imported
// file, it transitively affects the current file too.
package_scope.has_error |= import_scope.has_error;
package_scope.has_error |= import_sem_ir.name_scopes()
.Get(SemIR::NameScopeId::Package)
.has_error;
auto ir_id = context.sem_ir().cross_ref_irs().Add(&import_sem_ir);
for (const auto& [import_name_id, import_inst_id] : import_scope.names) {
// Translate the name to the current IR.
auto name_id = SemIR::NameId::Invalid;
if (auto import_identifier_id = import_name_id.AsIdentifierId();
for (const auto import_inst_id :
import_sem_ir.inst_blocks().Get(SemIR::InstBlockId::Exports)) {
// TODO: Handle enclosing namespaces.
auto name_id = GetImportNameId(self_import->second.node, context,
import_sem_ir, import_inst_id);
// Translate the name to the current IR. It will usually be an
// identifier, but could also be a builtin name ID which is
// equivalent cross-IR.
if (auto import_identifier_id = name_id.AsIdentifierId();
import_identifier_id.is_valid()) {
auto name = import_sem_ir.identifiers().Get(import_identifier_id);
name_id =
SemIR::NameId::ForIdentifier(context.identifiers().Add(name));
} else {
// A builtin name ID which is equivalent cross-IR.
name_id = import_name_id;
}
// Leave a placeholder that the inst comes from the other IR.
auto target_id = context.AddInst(
SemIR::LazyImportRef{.ir_id = ir_id, .inst_id = import_inst_id});
// TODO: The scope's names should be changed to allow for ambiguous
// names.
// TODO: When importing from other packages, the scope's names should be
// changed to allow for ambiguous names. When importing from the current
// package, as is currently being done, we should issue a diagnostic on
// conflicts.
package_scope.names.insert({name_id, target_id});
}
}
@@ -198,6 +230,7 @@ static auto CheckParseTree(const SemIR::File& builtin_ir, UnitInfo& unit_info,
// Pop information for the file-level scope.
sem_ir.set_top_inst_block_id(context.inst_block_stack().Pop());
context.PopScope();
context.FinalizeExports();
context.VerifyOnFinish();