[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 Bricknerandjonmeow authored and GitHub committed 2024-12-06 21:50:50 +00:00
1 parent e7a86b03c6
commit daba2c72cf
19 files changed
+526 -140

No files matched your search

+6 -6
View File
@@ -614,8 +614,8 @@ class FormatterImpl {
auto FormatNameScope(NameScopeId id, llvm::StringRef label = "") -> void {
const auto& scope = sem_ir_->name_scopes().Get(id);
if (scope.names.empty() && scope.extended_scopes.empty() &&
scope.import_ir_scopes.empty() && !scope.has_error) {
if (scope.entries().empty() && scope.extended_scopes().empty() &&
scope.import_ir_scopes().empty() && !scope.has_error()) {
// Name scope is empty.
return;
}
@@ -625,7 +625,7 @@ class FormatterImpl {
out_ << label;
}
for (auto [name_id, inst_id, access_kind] : scope.names) {
for (auto [name_id, inst_id, access_kind] : scope.entries()) {
Indent();
out_ << ".";
FormatName(name_id);
@@ -644,7 +644,7 @@ class FormatterImpl {
out_ << "\n";
}
for (auto extended_scope_id : scope.extended_scopes) {
for (auto extended_scope_id : scope.extended_scopes()) {
Indent();
out_ << "extend ";
FormatName(extended_scope_id);
@@ -656,7 +656,7 @@ class FormatterImpl {
// add or remove an unused prelude file, but is intended to still show the
// existence of indirect imports.
bool has_prelude_components = false;
for (auto [import_ir_id, unused] : scope.import_ir_scopes) {
for (auto [import_ir_id, unused] : scope.import_ir_scopes()) {
auto label = GetImportIRLabel(import_ir_id);
if (label.starts_with("Core//prelude/")) {
if (has_prelude_components) {
@@ -671,7 +671,7 @@ class FormatterImpl {
out_ << "import " << label << "\n";
}
if (scope.has_error) {
if (scope.has_error()) {
Indent();
out_ << "has_error\n";
}