Files
carbon-lang/toolchain/check/handle_export.cpp
T
Jon Ross-Perkins 40b2217421 Initial 'export name' handling. (#3949)
This adds a `BindExport` instruction in order to better track the
location of the `export` itself, but a `bind_name_id` is also added to
`ImportRef` so that we know quickly where to put it in name lookup.

Merging identical names is a TODO. I haven't quite decided how best to
achieve that, because I do think the BindExport should be what's
actually added to name lookup.

Also, I will probably add a mode to DeclNameStack that blocks
non-namespace scopes. This seems to already be an error, but the wrong
one (maybe due to lack of support for cross-file decl/def support).
2024-05-17 18:05:52 +00:00

59 lines
2.0 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/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 {
// TODO: Probably need to update DeclNameStack to restrict to only namespaces.
context.decl_name_stack().PushScopeAndStartName();
return true;
}
auto HandleExportDirective(Context& context, Parse::ExportDirectiveId node_id)
-> bool {
auto name_context = context.decl_name_stack().FinishName();
context.decl_name_stack().PopScope();
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 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(
{node_id, SemIR::BindExport{.type_id = import_ref->type_id,
.bind_name_id = import_ref->bind_name_id,
.value_id = inst_id}});
context.AddExport(export_id);
return true;
}
} // namespace Carbon::Check