Add namespace-scoped private keyword support. (#4026)

Adds access to the name lookup table in name scopes. This is so that we
can quickly check access during name lookup without resolving the entity
itself. Does this for names in general, but does not implement handling
for entity-scoped names, only namespace-scoped names (where they're
essentially just not exported).

Excludes `private` names from exports. Although names should be
accessible to `impl` files, that's not implemented here because we'll
probably want to do it by directly copying name lookup tables.
This commit is contained in:
Jon Ross-Perkins authored and GitHub committed 2024-06-06 00:19:56 +00:00
1 parent d1862e829b
commit 807a3f1370
33 files changed
+2022 -335

No files matched your search

+24 -26
View File
@@ -6,6 +6,7 @@
#include "toolchain/check/convert.h"
#include "toolchain/check/handle.h"
#include "toolchain/check/modifiers.h"
#include "toolchain/lex/token_index.h"
namespace Carbon::Check {
@@ -50,31 +51,45 @@ auto HandleVariableDecl(Context& context, Parse::VariableDeclId node_id)
return context.TODO(node_id, "tuple pattern in var");
}
// Extract the name binding.
auto value_id = context.node_stack().PopPattern();
// Pop the `returned` specifier if present.
context.node_stack()
.PopAndDiscardSoloNodeIdIf<Parse::NodeKind::ReturnedModifier>();
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::VariableIntroducer>();
// Process declaration modifiers.
// TODO: For a qualified `var` declaration, this should use the target scope
// of the name introduced in the declaration. See #2590.
auto [_, parent_scope_inst] = context.name_scopes().GetInstIfValid(
context.scope_stack().PeekNameScopeId());
auto introducer =
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Var>();
CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Access);
// Extract the name binding.
if (auto bind_name = context.insts().TryGetAs<SemIR::AnyBindName>(value_id)) {
// Form a corresponding name in the current context, and bind the name to
// the variable.
auto name_context = context.decl_name_stack().MakeUnqualifiedName(
context.insts().GetLocId(value_id),
context.bind_names().Get(bind_name->bind_name_id).name_id);
context.decl_name_stack().AddNameOrDiagnoseDuplicate(name_context,
value_id);
context.decl_name_stack().AddNameOrDiagnoseDuplicate(
name_context, value_id, introducer.modifier_set.GetAccessKind());
value_id = bind_name->value_id;
} else if (auto field_decl =
context.insts().TryGetAs<SemIR::FieldDecl>(value_id)) {
// Introduce the field name into the class.
auto name_context = context.decl_name_stack().MakeUnqualifiedName(
context.insts().GetLocId(value_id), field_decl->name_id);
context.decl_name_stack().AddNameOrDiagnoseDuplicate(name_context,
value_id);
context.decl_name_stack().AddNameOrDiagnoseDuplicate(
name_context, value_id, introducer.modifier_set.GetAccessKind());
}
// TODO: Handle other kinds of pattern.
// Pop the `returned` specifier if present.
context.node_stack()
.PopAndDiscardSoloNodeIdIf<Parse::NodeKind::ReturnedModifier>();
// If there was an initializer, assign it to the storage.
if (init_id) {
if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
@@ -93,23 +108,6 @@ auto HandleVariableDecl(Context& context, Parse::VariableDeclId node_id)
}
}
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::VariableIntroducer>();
// Process declaration modifiers.
// TODO: For a qualified `var` declaration, this should use the target scope
// of the name introduced in the declaration. See #2590.
auto [_, parent_scope_inst] = context.name_scopes().GetInstIfValid(
context.scope_stack().PeekNameScopeId());
auto introducer =
context.decl_introducer_state_stack().Pop<Lex::TokenKind::Var>();
CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Access);
if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Access)) {
context.TODO(introducer.modifier_node_id(ModifierOrder::Access),
"access modifier");
}
return true;
}