[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
+16 -13
View File
@@ -76,10 +76,10 @@ static auto TryAsClassScope(Context& context, SemIR::NameScopeId scope_id)
return std::nullopt;
}
auto& scope = context.name_scopes().Get(scope_id);
if (!scope.inst_id.is_valid()) {
if (!scope.inst_id().is_valid()) {
return std::nullopt;
}
return context.insts().TryGetAs<SemIR::ClassDecl>(scope.inst_id);
return context.insts().TryGetAs<SemIR::ClassDecl>(scope.inst_id());
}
static auto GetDefaultSelfType(Context& context) -> SemIR::TypeId {
@@ -145,7 +145,7 @@ static auto ExtendImpl(Context& context, Parse::NodeId extend_node,
CARBON_DIAGNOSTIC(ExtendImplForall, Error,
"cannot `extend` a parameterized `impl`");
context.emitter().Emit(extend_node, ExtendImplForall);
parent_scope.has_error = true;
parent_scope.set_has_error();
return;
}
@@ -158,7 +158,7 @@ static auto ExtendImpl(Context& context, Parse::NodeId extend_node,
// If the explicit self type is not the default, just bail out.
if (self_type_id != GetDefaultSelfType(context)) {
diag.Emit();
parent_scope.has_error = true;
parent_scope.set_has_error();
return;
}
@@ -176,18 +176,21 @@ static auto ExtendImpl(Context& context, Parse::NodeId extend_node,
if (!context.types().Is<SemIR::FacetType>(constraint_id)) {
context.TODO(node_id, "extending non-facet-type constraint");
parent_scope.has_error = true;
parent_scope.set_has_error();
return;
}
parent_scope.has_error |= !context.TryToDefineType(constraint_id, [&] {
CARBON_DIAGNOSTIC(ExtendUndefinedInterface, Error,
"`extend impl` requires a definition for facet type {0}",
InstIdAsType);
return context.emitter().Build(node_id, ExtendUndefinedInterface,
constraint_inst_id);
});
if (!context.TryToDefineType(constraint_id, [&] {
CARBON_DIAGNOSTIC(
ExtendUndefinedInterface, Error,
"`extend impl` requires a definition for facet type {0}",
InstIdAsType);
return context.emitter().Build(node_id, ExtendUndefinedInterface,
constraint_inst_id);
})) {
parent_scope.set_has_error();
};
parent_scope.extended_scopes.push_back(constraint_inst_id);
parent_scope.AddExtendedScope(constraint_inst_id);
}
// Pops the parameters of an `impl`, forming a `NameComponent` with no