Pass imports to SemIR. (#3415)

This adds instructions so that we get printing. I may adjust the
instruction format a little further to add a type, but I think the basic
setup will remain.

Note this builds on #3414
This commit is contained in:
Jon Ross-Perkins
2023-11-28 19:55:38 +00:00
committed by GitHub
parent 35d15a390c
commit 239f8030a4
20 changed files with 228 additions and 38 deletions
+42
View File
@@ -15,8 +15,10 @@
#include "toolchain/lex/tokenized_buffer.h"
#include "toolchain/parse/node_kind.h"
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst.h"
#include "toolchain/sem_ir/inst_kind.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
@@ -112,6 +114,45 @@ auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
}
}
auto Context::AddPackageImports(Parse::Node import_node,
IdentifierId package_id,
llvm::ArrayRef<const SemIR::File*> sem_irs,
bool has_load_error) -> void {
CARBON_CHECK(has_load_error || !sem_irs.empty())
<< "There should be either a load error or at least one IR.";
auto name_id = SemIR::NameId::ForIdentifier(package_id);
SemIR::CrossRefIRId first_id(cross_ref_irs().size());
for (const auto* sem_ir : sem_irs) {
cross_ref_irs().Add(sem_ir);
}
if (has_load_error) {
cross_ref_irs().Add(nullptr);
}
SemIR::CrossRefIRId last_id(cross_ref_irs().size() - 1);
auto type_id = GetBuiltinType(SemIR::BuiltinKind::NamespaceType);
auto inst_id = AddInst(SemIR::Import{.parse_node = import_node,
.type_id = type_id,
.first_cross_ref_ir_id = first_id,
.last_cross_ref_ir_id = last_id});
if (name_id.is_valid()) {
// Add the import to lookup. Should always succeed because imports will be
// uniquely named.
AddNameToLookup(import_node, name_id, inst_id);
// Add a name for formatted output. This isn't used in name lookup in order
// to reduce indirection, but it's separate from the Import because it
// otherwise fits in an Inst.
AddInst(SemIR::BindName{.parse_node = import_node,
.type_id = type_id,
.name_id = name_id,
.value_id = inst_id});
} else {
// TODO: All names from the current package should be added.
}
}
auto Context::AddNameToLookup(Parse::Node name_node, SemIR::NameId name_id,
SemIR::InstId target_id) -> void {
if (current_scope().names.insert(name_id).second) {
@@ -811,6 +852,7 @@ class TypeCompleter {
case SemIR::Deref::Kind:
case SemIR::Field::Kind:
case SemIR::FunctionDecl::Kind:
case SemIR::Import::Kind:
case SemIR::InitializeFrom::Kind:
case SemIR::IntegerLiteral::Kind:
case SemIR::NameRef::Kind: