[NFC] Convert NameScope from struct to class (#4623)

This is a preparation change for adding name poisoning support
(https://github.com/carbon-language/carbon-lang/issues/4622), which is
expected to require more elaborate logic around NameScope since a name
can be not defined yet, defined, or poisoned.

The API separates looking up a name from getting the full entry since we
have cases where the entries are invalidated between the time we're
looking for the name and when we access (and sometimes modify) the
entry.

This change has the following benefits:
* `names` and `name_map` are internal to `NameScope` and are guaranteed
to match.
* `extended_scopes` and `import_ir_scopes` can not be manipulated (only
new scopes can be added).
* `inst_id`, `name_id` and `parent_scope_id` are constants.
* `has_error` can only be mutated from false to true.

---------

Co-authored-by: jonmeow <jperkins@google.com>
This commit is contained in:
Boaz Brickner
2024-12-06 21:50:50 +00:00
committed by GitHub
co-authored by jonmeow
parent e7a86b03c6
commit daba2c72cf
19 changed files with 526 additions and 140 deletions
+5 -5
View File
@@ -135,7 +135,7 @@ auto DeclNameStack::AddName(NameContext name_context, SemIR::InstId target_id,
auto& name_scope =
context_->name_scopes().Get(name_context.parent_scope_id);
if (name_context.has_qualifiers) {
auto inst = context_->insts().Get(name_scope.inst_id);
auto inst = context_->insts().Get(name_scope.inst_id());
if (!inst.Is<SemIR::Namespace>()) {
// TODO: Point at the declaration for the scoped entity.
CARBON_DIAGNOSTIC(
@@ -231,7 +231,7 @@ auto DeclNameStack::ApplyNameQualifier(const NameComponent& name) -> void {
if (scope_id.is_valid()) {
PushNameQualifierScope(*context_, name_context.resolved_inst_id, scope_id,
specific_id,
context_->name_scopes().Get(scope_id).has_error);
context_->name_scopes().Get(scope_id).has_error());
name_context.parent_scope_id = scope_id;
} else {
name_context.state = NameContext::State::Error;
@@ -416,12 +416,12 @@ auto DeclNameStack::ResolveAsScope(const NameContext& name_context,
SemIR::InstBlockId::Invalid))) {
return InvalidResult;
}
if (scope.is_closed_import) {
if (scope.is_closed_import()) {
DiagnoseQualifiedDeclInImportedPackage(*context_, name_context.loc_id,
scope.inst_id);
scope.inst_id());
// Only error once per package. Recover by allowing this package name to
// be used as a name qualifier.
scope.is_closed_import = false;
scope.set_is_closed_import(false);
}
return {scope_id, SemIR::SpecificId::Invalid};
}