From d9c62b106d375c1abc07d953f82c6fe4c7ae1101 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Tue, 4 Jun 2024 12:57:14 -0700 Subject: [PATCH] Rename enclosing scope to parent scope (#4020) Following up on discussion from #3948, doing a general rename of "enclosing scope" to "parent scope" (and "enclosing scopes" to "ancestor scopes"). The intent is to improve understandability and collide less with C++ terminology for "enclosing scope". Note this changes most uses of "enclosing", but leaves behind a few like "enclosing function" and "enclosing block". Note this does create some "parent class" mentions for "adapt" and "var" (the class they're within), which is maybe unfortunate, but we'd probably say "base class" if we meant inheritance so perhaps that's okay. Along the same lines, these are the only `parent_class` uses I see now, and we do have a few `base_class`. --- toolchain/check/check.cpp | 18 ++--- toolchain/check/context.cpp | 9 +-- toolchain/check/decl_name_stack.cpp | 28 +++---- toolchain/check/decl_name_stack.h | 22 +++--- toolchain/check/handle_alias.cpp | 2 +- toolchain/check/handle_binding_pattern.cpp | 11 ++- toolchain/check/handle_class.cpp | 39 +++++----- toolchain/check/handle_export.cpp | 2 +- toolchain/check/handle_function.cpp | 32 ++++---- toolchain/check/handle_impl.cpp | 24 +++--- toolchain/check/handle_interface.cpp | 12 +-- toolchain/check/handle_let.cpp | 9 +-- toolchain/check/handle_namespace.cpp | 2 +- toolchain/check/handle_variable.cpp | 5 +- toolchain/check/import.cpp | 76 +++++++++---------- toolchain/check/import_ref.cpp | 30 ++++---- toolchain/check/modifiers.cpp | 44 +++++------ toolchain/check/modifiers.h | 27 ++++--- toolchain/check/scope_stack.cpp | 6 +- toolchain/check/scope_stack.h | 12 +-- .../testdata/basics/builtin_insts.carbon | 2 +- .../multifile_raw_and_textual_ir.carbon | 8 +- .../basics/no_prelude/multifile_raw_ir.carbon | 8 +- .../no_prelude/raw_and_textual_ir.carbon | 6 +- .../testdata/basics/no_prelude/raw_ir.carbon | 6 +- toolchain/lower/file_context.cpp | 2 +- toolchain/parse/context.cpp | 9 +-- toolchain/parse/context.h | 3 +- toolchain/sem_ir/bind_name.h | 10 +-- toolchain/sem_ir/class.h | 7 +- toolchain/sem_ir/function.h | 6 +- toolchain/sem_ir/interface.h | 7 +- toolchain/sem_ir/name_scope.h | 10 +-- 33 files changed, 237 insertions(+), 257 deletions(-) diff --git a/toolchain/check/check.cpp b/toolchain/check/check.cpp index 7ec6826d34a1..620068e2cddf 100644 --- a/toolchain/check/check.cpp +++ b/toolchain/check/check.cpp @@ -444,7 +444,7 @@ class DeferredDefinitionWorklist { std::optional suspended_name; // Whether this scope is itself within an outer deferred definition scope. // If so, we'll delay processing its contents until we reach the end of the - // enclosing scope. For example: + // parent scope. For example: // // ``` // class A { @@ -497,10 +497,10 @@ class DeferredDefinitionWorklist { // Push a task to re-enter a function scope, so that functions defined within // it are type-checked in the right context. auto PushEnterDeferredDefinitionScope(Context& context) -> void { - bool nested = !enclosing_scopes_.empty() && - enclosing_scopes_.back().scope_index == + bool nested = !entered_scopes_.empty() && + entered_scopes_.back().scope_index == context.decl_name_stack().PeekInitialScopeIndex(); - enclosing_scopes_.push_back( + entered_scopes_.push_back( {.worklist_start_index = worklist_.size(), .scope_index = context.scope_stack().PeekIndex()}); worklist_.push_back( @@ -543,7 +543,7 @@ class DeferredDefinitionWorklist { // CHECK that the work list has no further work. auto VerifyEmpty() { - CARBON_CHECK(worklist_.empty() && enclosing_scopes_.empty()) + CARBON_CHECK(worklist_.empty() && entered_scopes_.empty()) << "Tasks left behind on worklist."; } @@ -558,21 +558,21 @@ class DeferredDefinitionWorklist { llvm::SmallVector worklist_; // A deferred definition scope that is currently still open. - struct EnclosingScope { + struct EnteredScope { // The index in worklist_ of the EnterDeferredDefinitionScope task. size_t worklist_start_index; // The corresponding lexical scope index. ScopeIndex scope_index; }; - // The deferred definition scopes enclosing the current checking actions. - llvm::SmallVector enclosing_scopes_; + // The deferred definition scopes for the current checking actions. + llvm::SmallVector entered_scopes_; }; } // namespace auto DeferredDefinitionWorklist::SuspendFinishedScopeAndPush(Context& context) -> bool { - auto start_index = enclosing_scopes_.pop_back_val().worklist_start_index; + auto start_index = entered_scopes_.pop_back_val().worklist_start_index; // If we've not found any deferred definitions in this scope, clean up the // stack. diff --git a/toolchain/check/context.cpp b/toolchain/check/context.cpp index 7eca3f2e05e8..d9d8cffec204 100644 --- a/toolchain/check/context.cpp +++ b/toolchain/check/context.cpp @@ -252,10 +252,10 @@ auto Context::LookupUnqualifiedName(Parse::NodeId node_id, SemIR::NameId name_id) -> SemIR::InstId { // TODO: Check for shadowed lookup results. - // Find the results from enclosing lexical scopes. These will be combined with + // Find the results from ancestor lexical scopes. These will be combined with // results from non-lexical scopes such as namespaces and classes. auto [lexical_result, non_lexical_scopes] = - scope_stack().LookupInEnclosingScopes(name_id); + scope_stack().LookupInLexicalScopes(name_id); // Walk the non-lexical scopes and perform lookups into each of them. for (auto [index, name_scope_id] : llvm::reverse(non_lexical_scopes)) { @@ -340,7 +340,7 @@ static auto LookupInImportIRScopes(Context& context, SemIRLoc loc, // Add the first result found. auto bind_name_id = context.bind_names().Add( {.name_id = name_id, - .enclosing_scope_id = scope_id, + .parent_scope_id = scope_id, .bind_index = SemIR::CompileTimeBindIndex::Invalid}); result_id = AddImportRef(context, {.ir_id = import_ir_id, .inst_id = it->second}, @@ -567,7 +567,6 @@ auto Context::SetBlockArgResultBeforeConstantUse(SemIR::InstId select_id, } } -// Add the current code block to the enclosing function. auto Context::AddCurrentCodeBlockToFunction(Parse::NodeId node_id) -> void { CARBON_CHECK(!inst_block_stack().empty()) << "no current code block"; @@ -621,7 +620,7 @@ auto Context::FinalizeGlobalInit() -> void { auto name_id = sem_ir().identifiers().Add("__global_init"); sem_ir().functions().Add( {.name_id = SemIR::NameId::ForIdentifier(name_id), - .enclosing_scope_id = SemIR::NameScopeId::Package, + .parent_scope_id = SemIR::NameScopeId::Package, .decl_id = SemIR::InstId::Invalid, .implicit_param_refs_id = SemIR::InstBlockId::Invalid, .param_refs_id = SemIR::InstBlockId::Empty, diff --git a/toolchain/check/decl_name_stack.cpp b/toolchain/check/decl_name_stack.cpp index 182ef1d313ea..b845c1415ffe 100644 --- a/toolchain/check/decl_name_stack.cpp +++ b/toolchain/check/decl_name_stack.cpp @@ -38,7 +38,7 @@ auto DeclNameStack::NameContext::prev_inst_id() -> SemIR::InstId { auto DeclNameStack::MakeEmptyNameContext() -> NameContext { return NameContext{ .initial_scope_index = context_->scope_stack().PeekIndex(), - .enclosing_scope_id = context_->scope_stack().PeekNameScopeId()}; + .parent_scope_id = context_->scope_stack().PeekNameScopeId()}; } auto DeclNameStack::MakeUnqualifiedName(SemIR::LocId loc_id, @@ -88,19 +88,19 @@ auto DeclNameStack::Suspend() -> SuspendedName { << "Missing call to FinishName before Suspend"; SuspendedName result = {.name_context = decl_name_stack_.pop_back_val(), .scopes = {}}; - auto enclosing_index = result.name_context.initial_scope_index; + auto scope_index = result.name_context.initial_scope_index; auto& scope_stack = context_->scope_stack(); - while (scope_stack.PeekIndex() > enclosing_index) { + while (scope_stack.PeekIndex() > scope_index) { result.scopes.push_back(scope_stack.Suspend()); } - CARBON_CHECK(scope_stack.PeekIndex() == enclosing_index) - << "Scope index " << enclosing_index - << " does not enclose the current scope " << scope_stack.PeekIndex(); + CARBON_CHECK(scope_stack.PeekIndex() == scope_index) + << "Scope index " << scope_index << " does not enclose the current scope " + << scope_stack.PeekIndex(); return result; } auto DeclNameStack::Restore(SuspendedName sus) -> void { - // The enclosing state must be the same when a name is restored. + // The parent state must be the same when a name is restored. CARBON_CHECK(context_->scope_stack().PeekIndex() == sus.name_context.initial_scope_index) << "Name restored at the wrong position in the name stack."; @@ -121,11 +121,11 @@ auto DeclNameStack::AddName(NameContext name_context, SemIR::InstId target_id) return; case NameContext::State::Unresolved: - if (!name_context.enclosing_scope_id.is_valid()) { + if (!name_context.parent_scope_id.is_valid()) { context_->AddNameToLookup(name_context.unresolved_name_id, target_id); } else { auto& name_scope = - context_->name_scopes().Get(name_context.enclosing_scope_id); + context_->name_scopes().Get(name_context.parent_scope_id); if (name_context.has_qualifiers) { auto inst = context_->insts().Get(name_scope.inst_id); if (!inst.Is()) { @@ -150,7 +150,7 @@ auto DeclNameStack::AddName(NameContext name_context, SemIR::InstId target_id) CARBON_CHECK(success) << "Duplicate names should have been resolved previously: " << name_context.unresolved_name_id << " in " - << name_context.enclosing_scope_id; + << name_context.parent_scope_id; } break; @@ -210,7 +210,7 @@ auto DeclNameStack::ApplyAndLookupName(NameContext& name_context, // For identifier nodes, we need to perform a lookup on the identifier. auto resolved_inst_id = context_->LookupNameInDecl( - name_context.loc_id, name_id, name_context.enclosing_scope_id); + name_context.loc_id, name_id, name_context.parent_scope_id); if (!resolved_inst_id.is_valid()) { // Invalid indicates an unresolved name. Store it and return. name_context.unresolved_name_id = name_id; @@ -255,7 +255,7 @@ auto DeclNameStack::UpdateScopeIfNeeded(NameContext& name_context, const auto& class_info = context_->classes().Get(resolved_inst.class_id); if (class_info.is_defined()) { name_context.state = NameContext::State::Resolved; - name_context.enclosing_scope_id = class_info.scope_id; + name_context.parent_scope_id = class_info.scope_id; if (!is_unqualified) { PushNameQualifierScope(*context_, name_context.resolved_inst_id, class_info.scope_id); @@ -270,7 +270,7 @@ auto DeclNameStack::UpdateScopeIfNeeded(NameContext& name_context, context_->interfaces().Get(resolved_inst.interface_id); if (interface_info.is_defined()) { name_context.state = NameContext::State::Resolved; - name_context.enclosing_scope_id = interface_info.scope_id; + name_context.parent_scope_id = interface_info.scope_id; if (!is_unqualified) { PushNameQualifierScope(*context_, name_context.resolved_inst_id, interface_info.scope_id); @@ -283,7 +283,7 @@ auto DeclNameStack::UpdateScopeIfNeeded(NameContext& name_context, case CARBON_KIND(SemIR::Namespace resolved_inst): { auto scope_id = resolved_inst.name_scope_id; name_context.state = NameContext::State::Resolved; - name_context.enclosing_scope_id = scope_id; + name_context.parent_scope_id = scope_id; auto& scope = context_->name_scopes().Get(scope_id); if (scope.is_closed_import) { CARBON_DIAGNOSTIC(QualifiedDeclOutsidePackage, Error, diff --git a/toolchain/check/decl_name_stack.h b/toolchain/check/decl_name_stack.h index 5d5002c7c662..be8ac0e02b32 100644 --- a/toolchain/check/decl_name_stack.h +++ b/toolchain/check/decl_name_stack.h @@ -39,9 +39,9 @@ class Context; // fn ClassA.ClassB(T:! U).Fn() { var x: V; } // ``` // -// the lookup for `U` looks in `ClassA`, and the lookup for `V` looks in -// `ClassA.ClassB` then in its enclosing scope `ClassA`. Scopes entered as part -// of processing the name are exited when the name is popped from the stack. +// the lookup for `U` looks in `ClassA`; the lookup for `V` looks first in +// `ClassA.ClassB`, then its parent scope `ClassA`. Scopes entered as part of +// processing the name are exited when the name is popped from the stack. // // Example state transitions: // @@ -102,10 +102,10 @@ class DeclNameStack { : SemIR::NameId::Invalid; } - // Returns the enclosing_scope_id for a new instruction. This is invalid + // Returns the parent_scope_id for a new instruction. This is invalid // when the name resolved. - auto enclosing_scope_id_for_new_inst() -> SemIR::NameScopeId { - return state == State::Unresolved ? enclosing_scope_id + auto parent_scope_id_for_new_inst() -> SemIR::NameScopeId { + return state == State::Unresolved ? parent_scope_id : SemIR::NameScopeId::Invalid; } @@ -121,7 +121,7 @@ class DeclNameStack { // The scope which qualified names are added to. For unqualified names in // an unnamed scope, this will be Invalid to indicate the current scope // should be used. - SemIR::NameScopeId enclosing_scope_id; + SemIR::NameScopeId parent_scope_id; // The last location ID used. SemIR::LocId loc_id = SemIR::LocId::Invalid; @@ -191,13 +191,13 @@ class DeclNameStack { // This should be called at the end of the declaration. auto PopScope() -> void; - // Peeks the current enclosing scope of the name on top of the stack. Note + // Peeks the current parent scope of the name on top of the stack. Note // that if we're still processing the name qualifiers, this can change before // the name is completed. Also, if the name up to this point was already // declared and is a scope, this will be that scope, rather than the scope - // enclosing it. - auto PeekEnclosingScopeId() const -> SemIR::NameScopeId { - return decl_name_stack_.back().enclosing_scope_id; + // containing it. + auto PeekParentScopeId() const -> SemIR::NameScopeId { + return decl_name_stack_.back().parent_scope_id; } // Peeks the resolution scope index of the name on top of the stack. diff --git a/toolchain/check/handle_alias.cpp b/toolchain/check/handle_alias.cpp index c38a43618fea..0325886e8a38 100644 --- a/toolchain/check/handle_alias.cpp +++ b/toolchain/check/handle_alias.cpp @@ -41,7 +41,7 @@ auto HandleAlias(Context& context, Parse::AliasId /*node_id*/) -> bool { auto bind_name_id = context.bind_names().Add( {.name_id = name_context.name_id_for_new_inst(), - .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(), + .parent_scope_id = name_context.parent_scope_id_for_new_inst(), .bind_index = SemIR::CompileTimeBindIndex::Invalid}); auto alias_type_id = SemIR::TypeId::Invalid; diff --git a/toolchain/check/handle_binding_pattern.cpp b/toolchain/check/handle_binding_pattern.cpp index f09e6b5251f9..618fdcaf8f91 100644 --- a/toolchain/check/handle_binding_pattern.cpp +++ b/toolchain/check/handle_binding_pattern.cpp @@ -38,7 +38,7 @@ auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id, // scopes, but right now we don't support qualified names here. auto bind_name_id = context.bind_names().Add( {.name_id = name_id, - .enclosing_scope_id = context.scope_stack().PeekNameScopeId(), + .parent_scope_id = context.scope_stack().PeekNameScopeId(), // TODO: Don't allocate a compile-time binding index for an associated // constant declaration. .bind_index = is_generic && !is_associated_constant @@ -86,22 +86,21 @@ auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id, : context.parse_tree().As(node_id); // A `var` declaration at class scope introduces a field. - auto enclosing_class_decl = context.GetCurrentScopeAs(); + auto parent_class_decl = context.GetCurrentScopeAs(); cast_type_id = context.AsCompleteType(cast_type_id, [&] { CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error, "{0} has incomplete type `{1}`.", llvm::StringLiteral, SemIR::TypeId); return context.emitter().Build(type_node, IncompleteTypeInVarDecl, - enclosing_class_decl + parent_class_decl ? llvm::StringLiteral("Field") : llvm::StringLiteral("Variable"), cast_type_id); }); - if (enclosing_class_decl) { + if (parent_class_decl) { CARBON_CHECK(context_node_kind == Parse::NodeKind::VariableIntroducer) << "`returned var` at class scope"; - auto& class_info = - context.classes().Get(enclosing_class_decl->class_id); + auto& class_info = context.classes().Get(parent_class_decl->class_id); auto field_type_id = context.GetUnboundElementType( class_info.self_type_id, cast_type_id); auto field_id = context.AddInst( diff --git a/toolchain/check/handle_class.cpp b/toolchain/check/handle_class.cpp index 546a3504cc06..cc5965843ffb 100644 --- a/toolchain/check/handle_class.cpp +++ b/toolchain/check/handle_class.cpp @@ -103,7 +103,7 @@ static auto MergeClassRedecl(Context& context, SemIRLoc new_loc, (prev_is_extern && !new_is_extern)) { prev_class.decl_id = new_class.decl_id; ReplacePrevInstForMerge( - context, prev_class.enclosing_scope_id, prev_class.name_id, + context, prev_class.parent_scope_id, prev_class.name_id, new_is_import ? new_loc.inst_id : new_class.decl_id); } return true; @@ -184,16 +184,15 @@ static auto BuildClassDecl(Context& context, Parse::AnyClassDeclId node_id, .PopAndDiscardSoloNodeId(); // Process modifiers. - auto [_, enclosing_scope_inst] = - context.name_scopes().GetInstIfValid(name_context.enclosing_scope_id); - CheckAccessModifiersOnDecl(context, Lex::TokenKind::Class, - enclosing_scope_inst); + auto [_, parent_scope_inst] = + context.name_scopes().GetInstIfValid(name_context.parent_scope_id); + CheckAccessModifiersOnDecl(context, Lex::TokenKind::Class, parent_scope_inst); LimitModifiersOnDecl(context, KeywordModifierSet::Class | KeywordModifierSet::Access | KeywordModifierSet::Extern, Lex::TokenKind::Class); RestrictExternModifierOnDecl(context, Lex::TokenKind::Class, - enclosing_scope_inst, is_definition); + parent_scope_inst, is_definition); auto modifiers = context.decl_state_stack().innermost().modifier_set; if (modifiers.HasAnyOf(KeywordModifierSet::Access)) { @@ -221,7 +220,7 @@ static auto BuildClassDecl(Context& context, Parse::AnyClassDeclId node_id, // TODO: Store state regarding is_extern. SemIR::Class class_info = { .name_id = name_context.name_id_for_new_inst(), - .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(), + .parent_scope_id = name_context.parent_scope_id_for_new_inst(), .implicit_param_refs_id = name.implicit_params_id, .param_refs_id = name.params_id, // `.self_type_id` depends on the ClassType, so is set below. @@ -282,7 +281,7 @@ auto HandleClassDefinitionStart(Context& context, if (!class_info.is_defined()) { class_info.definition_id = class_decl_id; class_info.scope_id = context.name_scopes().Add( - class_decl_id, SemIR::NameId::Invalid, class_info.enclosing_scope_id); + class_decl_id, SemIR::NameId::Invalid, class_info.parent_scope_id); } // Enter the class scope. @@ -321,10 +320,10 @@ static auto DiagnoseClassSpecificDeclOutsideClass(Context& context, context.emitter().Emit(loc, ClassSpecificDeclOutsideClass, tok); } -// Returns the declaration of the immediately-enclosing class scope, or -// diagonses if there isn't one. -static auto GetEnclosingClassOrDiagnose(Context& context, SemIRLoc loc, - Lex::TokenKind tok) +// Returns the current scope's class declaration, or diagnoses if it isn't a +// class. +static auto GetCurrentScopeAsClassOrDiagnose(Context& context, SemIRLoc loc, + Lex::TokenKind tok) -> std::optional { auto class_scope = context.GetCurrentScopeAs(); if (!class_scope) { @@ -369,13 +368,13 @@ auto HandleAdaptDecl(Context& context, Parse::AdaptDeclId node_id) -> bool { auto modifiers = context.decl_state_stack().innermost().modifier_set; context.decl_state_stack().Pop(DeclState::Adapt); - auto enclosing_class_decl = - GetEnclosingClassOrDiagnose(context, node_id, Lex::TokenKind::Adapt); - if (!enclosing_class_decl) { + auto parent_class_decl = + GetCurrentScopeAsClassOrDiagnose(context, node_id, Lex::TokenKind::Adapt); + if (!parent_class_decl) { return true; } - auto& class_info = context.classes().Get(enclosing_class_decl->class_id); + auto& class_info = context.classes().Get(parent_class_decl->class_id); if (class_info.adapt_id.is_valid()) { DiagnoseClassSpecificDeclRepeated(context, node_id, class_info.adapt_id, Lex::TokenKind::Adapt); @@ -505,13 +504,13 @@ auto HandleBaseDecl(Context& context, Parse::BaseDeclId node_id) -> bool { } context.decl_state_stack().Pop(DeclState::Base); - auto enclosing_class_decl = - GetEnclosingClassOrDiagnose(context, node_id, Lex::TokenKind::Base); - if (!enclosing_class_decl) { + auto parent_class_decl = + GetCurrentScopeAsClassOrDiagnose(context, node_id, Lex::TokenKind::Base); + if (!parent_class_decl) { return true; } - auto& class_info = context.classes().Get(enclosing_class_decl->class_id); + auto& class_info = context.classes().Get(parent_class_decl->class_id); if (class_info.base_id.is_valid()) { DiagnoseClassSpecificDeclRepeated(context, node_id, class_info.base_id, Lex::TokenKind::Base); diff --git a/toolchain/check/handle_export.cpp b/toolchain/check/handle_export.cpp index c51d389c17ac..5724a0ddced4 100644 --- a/toolchain/check/handle_export.cpp +++ b/toolchain/check/handle_export.cpp @@ -76,7 +76,7 @@ auto HandleExportDecl(Context& context, Parse::ExportDeclId node_id) -> bool { // Replace the ImportRef in name lookup, both for the above duplicate // diagnostic and so that cross-package imports can find it easily. auto bind_name = context.bind_names().Get(import_ref->bind_name_id); - auto& names = context.name_scopes().Get(bind_name.enclosing_scope_id).names; + auto& names = context.name_scopes().Get(bind_name.parent_scope_id).names; auto it = names.find(bind_name.name_id); CARBON_CHECK(it->second == inst_id); it->second = export_id; diff --git a/toolchain/check/handle_function.cpp b/toolchain/check/handle_function.cpp index e5c72d471b75..59b3cf608d7e 100644 --- a/toolchain/check/handle_function.cpp +++ b/toolchain/check/handle_function.cpp @@ -45,21 +45,21 @@ auto HandleReturnType(Context& context, Parse::ReturnTypeId node_id) -> bool { } static auto DiagnoseModifiers(Context& context, bool is_definition, - SemIR::InstId enclosing_scope_inst_id, - std::optional enclosing_scope_inst) + SemIR::InstId parent_scope_inst_id, + std::optional parent_scope_inst) -> KeywordModifierSet { - CheckAccessModifiersOnDecl(context, Lex::TokenKind::Fn, enclosing_scope_inst); + CheckAccessModifiersOnDecl(context, Lex::TokenKind::Fn, parent_scope_inst); LimitModifiersOnDecl(context, KeywordModifierSet::Access | KeywordModifierSet::Extern | KeywordModifierSet::Method | KeywordModifierSet::Interface, Lex::TokenKind::Fn); - RestrictExternModifierOnDecl(context, Lex::TokenKind::Fn, - enclosing_scope_inst, is_definition); - CheckMethodModifiersOnFunction(context, enclosing_scope_inst_id, - enclosing_scope_inst); + RestrictExternModifierOnDecl(context, Lex::TokenKind::Fn, parent_scope_inst, + is_definition); + CheckMethodModifiersOnFunction(context, parent_scope_inst_id, + parent_scope_inst); RequireDefaultFinalOnlyInInterfaces(context, Lex::TokenKind::Fn, - enclosing_scope_inst); + parent_scope_inst); return context.decl_state_stack().innermost().modifier_set; } @@ -130,7 +130,7 @@ static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc, (prev_function.is_extern && !new_function.is_extern)) { prev_function.is_extern = new_function.is_extern; prev_function.decl_id = new_function.decl_id; - ReplacePrevInstForMerge(context, prev_function.enclosing_scope_id, + ReplacePrevInstForMerge(context, prev_function.parent_scope_id, prev_function.name_id, new_function.decl_id); } return true; @@ -224,10 +224,10 @@ static auto BuildFunctionDecl(Context& context, .PopAndDiscardSoloNodeId(); // Process modifiers. - auto [enclosing_scope_inst_id, enclosing_scope_inst] = - context.name_scopes().GetInstIfValid(name_context.enclosing_scope_id); - auto modifiers = DiagnoseModifiers( - context, is_definition, enclosing_scope_inst_id, enclosing_scope_inst); + auto [parent_scope_inst_id, parent_scope_inst] = + context.name_scopes().GetInstIfValid(name_context.parent_scope_id); + auto modifiers = DiagnoseModifiers(context, is_definition, + parent_scope_inst_id, parent_scope_inst); if (modifiers.HasAnyOf(KeywordModifierSet::Access)) { context.TODO(context.decl_state_stack().innermost().modifier_node_id( ModifierOrder::Access), @@ -253,7 +253,7 @@ static auto BuildFunctionDecl(Context& context, SemIR::TypeId::Invalid, SemIR::FunctionId::Invalid, decl_block_id}; auto function_info = SemIR::Function{ .name_id = name_context.name_id_for_new_inst(), - .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(), + .parent_scope_id = name_context.parent_scope_id_for_new_inst(), .decl_id = context.AddPlaceholderInst( SemIR::LocIdAndInst(node_id, function_decl)), .implicit_param_refs_id = name.implicit_params_id, @@ -284,9 +284,9 @@ static auto BuildFunctionDecl(Context& context, // At interface scope, a function declaration introduces an associated // function. auto lookup_result_id = function_info.decl_id; - if (enclosing_scope_inst && !name_context.has_qualifiers) { + if (parent_scope_inst && !name_context.has_qualifiers) { if (auto interface_scope = - enclosing_scope_inst->TryAs()) { + parent_scope_inst->TryAs()) { lookup_result_id = BuildAssociatedEntity( context, interface_scope->interface_id, function_info.decl_id); } diff --git a/toolchain/check/handle_impl.cpp b/toolchain/check/handle_impl.cpp index fa73ef1cf2e5..251a6d8553dd 100644 --- a/toolchain/check/handle_impl.cpp +++ b/toolchain/check/handle_impl.cpp @@ -69,9 +69,9 @@ static auto TryAsClassScope(Context& context, SemIR::NameScopeId scope_id) } static auto GetDefaultSelfType(Context& context) -> SemIR::TypeId { - auto enclosing_scope_id = context.decl_name_stack().PeekEnclosingScopeId(); + auto parent_scope_id = context.decl_name_stack().PeekParentScopeId(); - if (auto class_decl = TryAsClassScope(context, enclosing_scope_id)) { + if (auto class_decl = TryAsClassScope(context, parent_scope_id)) { return context.classes().Get(class_decl->class_id).self_type_id; } @@ -91,7 +91,7 @@ auto HandleDefaultSelfImplAs(Context& context, } // There's no need to push `Self` into scope here, because we can find it in - // the enclosing class scope. + // the parent class scope. context.node_stack().Push(node_id, self_type_id); return true; } @@ -103,11 +103,11 @@ static auto ExtendImpl(Context& context, Parse::NodeId extend_node, Parse::NodeId self_type_node, SemIR::TypeId self_type_id, Parse::NodeId params_node, SemIR::TypeId constraint_id) -> void { - auto enclosing_scope_id = context.decl_name_stack().PeekEnclosingScopeId(); - auto& enclosing_scope = context.name_scopes().Get(enclosing_scope_id); + auto parent_scope_id = context.decl_name_stack().PeekParentScopeId(); + auto& parent_scope = context.name_scopes().Get(parent_scope_id); // TODO: This is also valid in a mixin. - if (!TryAsClassScope(context, enclosing_scope_id)) { + if (!TryAsClassScope(context, parent_scope_id)) { CARBON_DIAGNOSTIC(ExtendImplOutsideClass, Error, "`extend impl` can only be used in a class."); context.emitter().Emit(node_id, ExtendImplOutsideClass); @@ -118,7 +118,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); - enclosing_scope.has_error = true; + parent_scope.has_error = true; return; } @@ -131,7 +131,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(); - enclosing_scope.has_error = true; + parent_scope.has_error = true; return; } @@ -150,7 +150,7 @@ static auto ExtendImpl(Context& context, Parse::NodeId extend_node, context.types().TryGetAs(constraint_id); if (!interface_type) { context.TODO(node_id, "extending non-interface constraint"); - enclosing_scope.has_error = true; + parent_scope.has_error = true; return; } @@ -164,11 +164,11 @@ static auto ExtendImpl(Context& context, Parse::NodeId extend_node, constraint_id); context.NoteUndefinedInterface(interface_type->interface_id, diag); diag.Emit(); - enclosing_scope.has_error = true; + parent_scope.has_error = true; return; } - enclosing_scope.extended_scopes.push_back(interface.scope_id); + parent_scope.extended_scopes.push_back(interface.scope_id); } // Build an ImplDecl describing the signature of an impl. This handles the @@ -252,7 +252,7 @@ auto HandleImplDefinitionStart(Context& context, impl_info.definition_id = impl_decl_id; impl_info.scope_id = context.name_scopes().Add( impl_decl_id, SemIR::NameId::Invalid, - context.decl_name_stack().PeekEnclosingScopeId()); + context.decl_name_stack().PeekParentScopeId()); } context.scope_stack().Push(impl_decl_id, impl_info.scope_id); diff --git a/toolchain/check/handle_interface.cpp b/toolchain/check/handle_interface.cpp index be6c28ff8ebf..3b37cb7a6eb8 100644 --- a/toolchain/check/handle_interface.cpp +++ b/toolchain/check/handle_interface.cpp @@ -36,10 +36,10 @@ static auto BuildInterfaceDecl(Context& context, .PopAndDiscardSoloNodeId(); // Process modifiers. - auto [_, enclosing_scope_inst] = - context.name_scopes().GetInstIfValid(name_context.enclosing_scope_id); + auto [_, parent_scope_inst] = + context.name_scopes().GetInstIfValid(name_context.parent_scope_id); CheckAccessModifiersOnDecl(context, Lex::TokenKind::Interface, - enclosing_scope_inst); + parent_scope_inst); LimitModifiersOnDecl(context, KeywordModifierSet::Access, Lex::TokenKind::Interface); @@ -84,7 +84,7 @@ static auto BuildInterfaceDecl(Context& context, // invalid. interface_decl.interface_id = context.interfaces().Add( {.name_id = name_context.name_id_for_new_inst(), - .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst(), + .parent_scope_id = name_context.parent_scope_id_for_new_inst(), .decl_id = interface_decl_id}); } @@ -121,7 +121,7 @@ auto HandleInterfaceDefinitionStart(Context& context, interface_info.definition_id = interface_decl_id; interface_info.scope_id = context.name_scopes().Add(interface_decl_id, SemIR::NameId::Invalid, - interface_info.enclosing_scope_id); + interface_info.parent_scope_id); } // Enter the interface scope. @@ -145,7 +145,7 @@ auto HandleInterfaceDefinitionStart(Context& context, // the `value_id` on the `BindSymbolicName`. auto bind_name_id = context.bind_names().Add( {.name_id = SemIR::NameId::SelfType, - .enclosing_scope_id = interface_info.scope_id, + .parent_scope_id = interface_info.scope_id, .bind_index = context.scope_stack().AddCompileTimeBinding()}); interface_info.self_param_id = context.AddInst( SemIR::LocId::Invalid, {.type_id = self_type_id, diff --git a/toolchain/check/handle_let.cpp b/toolchain/check/handle_let.cpp index 397a651a390d..65ed717ca6fc 100644 --- a/toolchain/check/handle_let.cpp +++ b/toolchain/check/handle_let.cpp @@ -78,13 +78,12 @@ auto HandleLetDecl(Context& context, Parse::LetDeclId node_id) -> bool { // Process declaration modifiers. // TODO: For a qualified `let` declaration, this should use the target scope // of the name introduced in the declaration. See #2590. - auto [enclosing_scope_inst_id, enclosing_scope_inst] = + auto [parent_scope_inst_id, parent_scope_inst] = context.name_scopes().GetInstIfValid( context.scope_stack().PeekNameScopeId()); - CheckAccessModifiersOnDecl(context, Lex::TokenKind::Let, - enclosing_scope_inst); + CheckAccessModifiersOnDecl(context, Lex::TokenKind::Let, parent_scope_inst); RequireDefaultFinalOnlyInInterfaces(context, Lex::TokenKind::Let, - enclosing_scope_inst); + parent_scope_inst); LimitModifiersOnDecl( context, KeywordModifierSet::Access | KeywordModifierSet::Interface, Lex::TokenKind::Let); @@ -140,7 +139,7 @@ auto HandleLetDecl(Context& context, Parse::LetDeclId node_id) -> bool { // Add the name of the binding to the current scope. auto name_id = context.bind_names().Get(bind_name.bind_name_id).name_id; context.AddNameToLookup(name_id, pattern_id); - if (enclosing_scope_inst_id == SemIR::InstId::PackageNamespace) { + if (parent_scope_inst_id == SemIR::InstId::PackageNamespace) { context.AddExport(pattern_id); } return true; diff --git a/toolchain/check/handle_namespace.cpp b/toolchain/check/handle_namespace.cpp index 5d7dcbddb36d..4992052f16b8 100644 --- a/toolchain/check/handle_namespace.cpp +++ b/toolchain/check/handle_namespace.cpp @@ -31,7 +31,7 @@ auto HandleNamespace(Context& context, Parse::NamespaceId node_id) -> bool { context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst)); namespace_inst.name_scope_id = context.name_scopes().Add( namespace_id, name_context.name_id_for_new_inst(), - name_context.enclosing_scope_id_for_new_inst()); + name_context.parent_scope_id_for_new_inst()); context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst); auto existing_inst_id = diff --git a/toolchain/check/handle_variable.cpp b/toolchain/check/handle_variable.cpp index e8f092dce470..06eee71c61aa 100644 --- a/toolchain/check/handle_variable.cpp +++ b/toolchain/check/handle_variable.cpp @@ -98,10 +98,9 @@ auto HandleVariableDecl(Context& context, Parse::VariableDeclId node_id) // 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 [_, enclosing_scope_inst] = context.name_scopes().GetInstIfValid( + auto [_, parent_scope_inst] = context.name_scopes().GetInstIfValid( context.scope_stack().PeekNameScopeId()); - CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var, - enclosing_scope_inst); + CheckAccessModifiersOnDecl(context, Lex::TokenKind::Var, parent_scope_inst); LimitModifiersOnDecl(context, KeywordModifierSet::Access, Lex::TokenKind::Var); auto modifiers = context.decl_state_stack().innermost().modifier_set; diff --git a/toolchain/check/import.cpp b/toolchain/check/import.cpp index 43380721452f..b32c0ab3565e 100644 --- a/toolchain/check/import.cpp +++ b/toolchain/check/import.cpp @@ -31,29 +31,29 @@ static auto GetImportName(const SemIR::File& import_sem_ir, auto bind_inst = import_inst.As(); const auto& bind_name = import_sem_ir.bind_names().Get(bind_inst.bind_name_id); - return {bind_name.name_id, bind_name.enclosing_scope_id}; + return {bind_name.name_id, bind_name.parent_scope_id}; } case CARBON_KIND(SemIR::ClassDecl class_decl): { const auto& class_info = import_sem_ir.classes().Get(class_decl.class_id); - return {class_info.name_id, class_info.enclosing_scope_id}; + return {class_info.name_id, class_info.parent_scope_id}; } case CARBON_KIND(SemIR::FunctionDecl function_decl): { const auto& function = import_sem_ir.functions().Get(function_decl.function_id); - return {function.name_id, function.enclosing_scope_id}; + return {function.name_id, function.parent_scope_id}; } case CARBON_KIND(SemIR::InterfaceDecl interface_decl): { const auto& interface = import_sem_ir.interfaces().Get(interface_decl.interface_id); - return {interface.name_id, interface.enclosing_scope_id}; + return {interface.name_id, interface.parent_scope_id}; } case CARBON_KIND(SemIR::Namespace ns): { const auto& scope = import_sem_ir.name_scopes().Get(ns.name_scope_id); - return {scope.name_id, scope.enclosing_scope_id}; + return {scope.name_id, scope.parent_scope_id}; } default: @@ -81,12 +81,12 @@ static auto CopyNameFromImportIR(Context& context, static auto AddNamespace( Context& context, SemIR::TypeId namespace_type_id, Parse::ImportDeclId node_id, SemIR::NameId name_id, - SemIR::NameScopeId enclosing_scope_id, bool diagnose_duplicate_namespace, + SemIR::NameScopeId parent_scope_id, bool diagnose_duplicate_namespace, std::optional> make_import_id) -> std::tuple { - auto& enclosing_scope = context.name_scopes().Get(enclosing_scope_id); + auto& parent_scope = context.name_scopes().Get(parent_scope_id); auto [it, success] = - enclosing_scope.names.insert({name_id, SemIR::InstId::Invalid}); + parent_scope.names.insert({name_id, SemIR::InstId::Invalid}); if (!success) { if (auto namespace_inst = context.insts().TryGetAs(it->second)) { @@ -106,7 +106,7 @@ static auto AddNamespace( auto namespace_id = context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst)); namespace_inst.name_scope_id = - context.name_scopes().Add(namespace_id, name_id, enclosing_scope_id); + context.name_scopes().Add(namespace_id, name_id, parent_scope_id); context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst); // Diagnose if there's a name conflict, but still produce the namespace to @@ -138,13 +138,13 @@ static auto CopySingleNameScopeFromImportIR( Context& context, SemIR::TypeId namespace_type_id, llvm::DenseMap& copied_namespaces, SemIR::ImportIRId ir_id, SemIR::InstId import_inst_id, - SemIR::NameScopeId import_scope_id, SemIR::NameScopeId enclosing_scope_id, + SemIR::NameScopeId import_scope_id, SemIR::NameScopeId parent_scope_id, SemIR::NameId name_id) -> SemIR::NameScopeId { // Produce the namespace for the entry. auto make_import_id = [&]() { auto bind_name_id = context.bind_names().Add( {.name_id = name_id, - .enclosing_scope_id = enclosing_scope_id, + .parent_scope_id = parent_scope_id, .bind_index = SemIR::CompileTimeBindIndex::Invalid}); auto import_ir_inst_id = context.import_ir_insts().Add( {.ir_id = ir_id, .inst_id = import_inst_id}); @@ -153,10 +153,9 @@ static auto CopySingleNameScopeFromImportIR( .import_ir_inst_id = import_ir_inst_id, .bind_name_id = bind_name_id}); }; - auto [namespace_scope_id, namespace_const_id, _] = - AddNamespace(context, namespace_type_id, Parse::NodeId::Invalid, name_id, - enclosing_scope_id, /*diagnose_duplicate_namespace=*/false, - make_import_id); + auto [namespace_scope_id, namespace_const_id, _] = AddNamespace( + context, namespace_type_id, Parse::NodeId::Invalid, name_id, + parent_scope_id, /*diagnose_duplicate_namespace=*/false, make_import_id); context.import_ir_constant_values()[ir_id.index].Set(import_inst_id, namespace_const_id); @@ -165,29 +164,28 @@ static auto CopySingleNameScopeFromImportIR( return namespace_scope_id; } -// Copies enclosing name scopes from the import IR. Handles the parent -// traversal. Returns the NameScope corresponding to the copied -// import_enclosing_scope_id. -static auto CopyEnclosingNameScopesFromImportIR( +// Copies ancestor name scopes from the import IR. Handles the parent traversal. +// Returns the NameScope corresponding to the copied import_parent_scope_id. +static auto CopyAncestorNameScopesFromImportIR( Context& context, SemIR::TypeId namespace_type_id, const SemIR::File& import_sem_ir, SemIR::ImportIRId ir_id, - SemIR::NameScopeId import_enclosing_scope_id, + SemIR::NameScopeId import_parent_scope_id, llvm::DenseMap& copied_namespaces) -> SemIR::NameScopeId { // Package-level names don't need work. - if (import_enclosing_scope_id == SemIR::NameScopeId::Package) { - return import_enclosing_scope_id; + if (import_parent_scope_id == SemIR::NameScopeId::Package) { + return import_parent_scope_id; } // The scope to add namespaces to. Note this may change while looking at - // enclosing scopes, if we encounter a namespace that's already added. + // parent scopes, if we encounter a namespace that's already added. auto scope_cursor = SemIR::NameScopeId::Package; - // Build a stack of enclosing namespace names, with innermost first. + // Build a stack of ancestor namespace names, with the immediate parent first. llvm::SmallVector new_namespaces; - while (import_enclosing_scope_id != SemIR::NameScopeId::Package) { + while (import_parent_scope_id != SemIR::NameScopeId::Package) { // If the namespace was already copied, reuse the results. - if (auto it = copied_namespaces.find(import_enclosing_scope_id); + if (auto it = copied_namespaces.find(import_parent_scope_id); it != copied_namespaces.end()) { // We inject names at the provided scope, and don't need to keep // traversing parents. @@ -196,15 +194,14 @@ static auto CopyEnclosingNameScopesFromImportIR( } // The namespace hasn't been copied yet, so add it to our list. - const auto& scope = - import_sem_ir.name_scopes().Get(import_enclosing_scope_id); + const auto& scope = import_sem_ir.name_scopes().Get(import_parent_scope_id); auto scope_inst = import_sem_ir.insts().GetAs(scope.inst_id); new_namespaces.push_back(scope_inst.name_scope_id); - import_enclosing_scope_id = scope.enclosing_scope_id; + import_parent_scope_id = scope.parent_scope_id; } - // Add enclosing namespace names, starting with the outermost. + // Add ancestor namespace names, starting with the outermost. for (auto import_scope_id : llvm::reverse(new_namespaces)) { auto import_scope = import_sem_ir.name_scopes().Get(import_scope_id); auto name_id = @@ -221,15 +218,15 @@ static auto CopyEnclosingNameScopesFromImportIR( static auto AddImportRefOrMerge(Context& context, SemIR::ImportIRId ir_id, const SemIR::File& import_sem_ir, SemIR::InstId import_inst_id, - SemIR::NameScopeId enclosing_scope_id, + SemIR::NameScopeId parent_scope_id, SemIR::NameId name_id) -> void { // Leave a placeholder that the inst comes from the other IR. - auto& names = context.name_scopes().Get(enclosing_scope_id).names; + auto& names = context.name_scopes().Get(parent_scope_id).names; auto [it, success] = names.insert({name_id, SemIR::InstId::Invalid}); if (success) { auto bind_name_id = context.bind_names().Add( {.name_id = name_id, - .enclosing_scope_id = enclosing_scope_id, + .parent_scope_id = parent_scope_id, .bind_index = SemIR::CompileTimeBindIndex::Invalid}); it->second = AddImportRef( context, {.ir_id = ir_id, .inst_id = import_inst_id}, bind_name_id); @@ -255,17 +252,16 @@ auto ImportLibrariesFromCurrentPackage( for (const auto import_inst_id : import_ir.sem_ir->inst_blocks().Get(SemIR::InstBlockId::Exports)) { auto import_inst = import_ir.sem_ir->insts().Get(import_inst_id); - auto [import_name_id, import_enclosing_scope_id] = + auto [import_name_id, import_parent_scope_id] = GetImportName(*import_ir.sem_ir, import_inst); llvm::DenseMap copied_namespaces; auto name_id = CopyNameFromImportIR(context, *import_ir.sem_ir, import_name_id); - SemIR::NameScopeId enclosing_scope_id = - CopyEnclosingNameScopesFromImportIR( - context, namespace_type_id, *import_ir.sem_ir, ir_id, - import_enclosing_scope_id, copied_namespaces); + SemIR::NameScopeId parent_scope_id = CopyAncestorNameScopesFromImportIR( + context, namespace_type_id, *import_ir.sem_ir, ir_id, + import_parent_scope_id, copied_namespaces); if (auto import_namespace_inst = import_inst.TryAs()) { // Namespaces are always imported because they're essential for @@ -273,10 +269,10 @@ auto ImportLibrariesFromCurrentPackage( CopySingleNameScopeFromImportIR( context, namespace_type_id, copied_namespaces, ir_id, import_inst_id, import_namespace_inst->name_scope_id, - enclosing_scope_id, name_id); + parent_scope_id, name_id); } else { AddImportRefOrMerge(context, ir_id, *import_ir.sem_ir, import_inst_id, - enclosing_scope_id, name_id); + parent_scope_id, name_id); } } diff --git a/toolchain/check/import_ref.cpp b/toolchain/check/import_ref.cpp index a4d96c543258..45e628c99195 100644 --- a/toolchain/check/import_ref.cpp +++ b/toolchain/check/import_ref.cpp @@ -445,7 +445,7 @@ class ImportRefResolver { case SemIR::BindName::Kind: { auto bind_name_id = context_.bind_names().Add( {.name_id = name_id, - .enclosing_scope_id = SemIR::NameScopeId::Invalid, + .parent_scope_id = SemIR::NameScopeId::Invalid, .bind_index = SemIR::CompileTimeBindIndex::Invalid}); new_param_id = context_.AddInstInNoBlock( AddImportIRInst(bind_id), {.type_id = type_id, @@ -742,7 +742,7 @@ class ImportRefResolver { auto name_id = GetLocalNameId(import_bind_info.name_id); auto bind_name_id = context_.bind_names().Add( {.name_id = name_id, - .enclosing_scope_id = SemIR::NameScopeId::Invalid, + .parent_scope_id = SemIR::NameScopeId::Invalid, .bind_index = import_bind_info.bind_index}); auto new_bind_id = context_.AddInstInNoBlock( AddImportIRInst(import_inst_id), @@ -771,7 +771,7 @@ class ImportRefResolver { // generic class and can build the right constant value for it. // TODO: Add a better way to represent a generic `Class` prior to // importing the parameters. - .enclosing_scope_id = SemIR::NameScopeId::Invalid, + .parent_scope_id = SemIR::NameScopeId::Invalid, .implicit_param_refs_id = import_class.implicit_param_refs_id.is_valid() ? SemIR::InstBlockId::Empty : SemIR::InstBlockId::Invalid, @@ -804,9 +804,8 @@ class ImportRefResolver { new_class.object_repr_id = context_.GetTypeIdForTypeConstant(object_repr_const_id); - new_class.scope_id = - context_.name_scopes().Add(new_class.decl_id, SemIR::NameId::Invalid, - new_class.enclosing_scope_id); + new_class.scope_id = context_.name_scopes().Add( + new_class.decl_id, SemIR::NameId::Invalid, new_class.parent_scope_id); auto& new_scope = context_.name_scopes().Get(new_class.scope_id); const auto& import_scope = import_ir_.name_scopes().Get(import_class.scope_id); @@ -857,8 +856,7 @@ class ImportRefResolver { // Load constants for the definition. auto initial_work = work_stack_.size(); - auto enclosing_scope_id = - GetLocalNameScopeId(import_class.enclosing_scope_id); + auto parent_scope_id = GetLocalNameScopeId(import_class.parent_scope_id); llvm::SmallVector implicit_param_const_ids = GetLocalParamConstantIds(import_class.implicit_param_refs_id); llvm::SmallVector param_const_ids = @@ -877,7 +875,7 @@ class ImportRefResolver { } auto& new_class = context_.classes().Get(class_id); - new_class.enclosing_scope_id = enclosing_scope_id; + new_class.parent_scope_id = parent_scope_id; new_class.implicit_param_refs_id = GetLocalParamRefsId( import_class.implicit_param_refs_id, implicit_param_const_ids); new_class.param_refs_id = @@ -950,7 +948,7 @@ class ImportRefResolver { if (function.return_type_id.is_valid()) { return_type_const_id = GetLocalConstantId(function.return_type_id); } - auto enclosing_scope_id = GetLocalNameScopeId(function.enclosing_scope_id); + auto parent_scope_id = GetLocalNameScopeId(function.parent_scope_id); llvm::SmallVector implicit_param_const_ids = GetLocalParamConstantIds(function.implicit_param_refs_id); llvm::SmallVector param_const_ids = @@ -988,7 +986,7 @@ class ImportRefResolver { } function_decl.function_id = context_.functions().Add( {.name_id = GetLocalNameId(function.name_id), - .enclosing_scope_id = enclosing_scope_id, + .parent_scope_id = parent_scope_id, .decl_id = function_decl_id, .implicit_param_refs_id = GetLocalParamRefsId( function.implicit_param_refs_id, implicit_param_const_ids), @@ -1073,7 +1071,7 @@ class ImportRefResolver { SemIR::Interface new_interface = { .name_id = GetLocalNameId(import_interface.name_id), // Set in the second pass once we've imported it. - .enclosing_scope_id = SemIR::NameScopeId::Invalid, + .parent_scope_id = SemIR::NameScopeId::Invalid, .decl_id = interface_decl_id, }; @@ -1092,7 +1090,7 @@ class ImportRefResolver { SemIR::ConstantId self_param_id) -> void { new_interface.scope_id = context_.name_scopes().Add( new_interface.decl_id, SemIR::NameId::Invalid, - new_interface.enclosing_scope_id); + new_interface.parent_scope_id); auto& new_scope = context_.name_scopes().Get(new_interface.scope_id); const auto& import_scope = import_ir_.name_scopes().Get(import_interface.scope_id); @@ -1122,8 +1120,8 @@ class ImportRefResolver { auto initial_work = work_stack_.size(); - auto enclosing_scope_id = - GetLocalNameScopeId(import_interface.enclosing_scope_id); + auto parent_scope_id = + GetLocalNameScopeId(import_interface.parent_scope_id); auto self_param_id = GetLocalConstantId(import_interface.self_param_id); if (HasNewWork(initial_work)) { @@ -1134,7 +1132,7 @@ class ImportRefResolver { context_.insts() .GetAs(interface_const_id.inst_id()) .interface_id); - new_interface.enclosing_scope_id = enclosing_scope_id; + new_interface.parent_scope_id = parent_scope_id; if (import_interface.is_defined()) { AddInterfaceDefinition(import_interface, new_interface, self_param_id); diff --git a/toolchain/check/modifiers.cpp b/toolchain/check/modifiers.cpp index 1af512b2b7ff..ca9c7dc35895 100644 --- a/toolchain/check/modifiers.cpp +++ b/toolchain/check/modifiers.cpp @@ -62,10 +62,10 @@ auto ForbidModifiersOnDecl(Context& context, KeywordModifierSet forbidden, } auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind, - std::optional enclosing_scope_inst) + std::optional parent_scope_inst) -> void { - if (enclosing_scope_inst) { - if (enclosing_scope_inst->Is()) { + if (parent_scope_inst) { + if (parent_scope_inst->Is()) { // TODO: This assumes that namespaces can only be declared at file scope. // If we add support for non-file-scope namespaces, we will need to check // the parents of the target scope to determine whether we're at file @@ -76,7 +76,7 @@ auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind, return; } - if (enclosing_scope_inst->Is()) { + if (parent_scope_inst->Is()) { // Both `private` and `protected` allowed in a class definition. return; } @@ -91,24 +91,22 @@ auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind, } auto CheckMethodModifiersOnFunction( - Context& context, SemIR::InstId enclosing_scope_inst_id, - std::optional enclosing_scope_inst) -> void { + Context& context, SemIR::InstId parent_scope_inst_id, + std::optional parent_scope_inst) -> void { const Lex::TokenKind decl_kind = Lex::TokenKind::Fn; - if (enclosing_scope_inst) { - if (auto class_decl = enclosing_scope_inst->TryAs()) { + if (parent_scope_inst) { + if (auto class_decl = parent_scope_inst->TryAs()) { auto inheritance_kind = context.classes().Get(class_decl->class_id).inheritance_kind; if (inheritance_kind == SemIR::Class::Final) { - ForbidModifiersOnDecl( - context, KeywordModifierSet::Virtual, decl_kind, - " in a non-abstract non-base `class` definition", - context.insts().GetLocId(enclosing_scope_inst_id)); + ForbidModifiersOnDecl(context, KeywordModifierSet::Virtual, decl_kind, + " in a non-abstract non-base `class` definition", + context.insts().GetLocId(parent_scope_inst_id)); } if (inheritance_kind != SemIR::Class::Abstract) { - ForbidModifiersOnDecl( - context, KeywordModifierSet::Abstract, decl_kind, - " in a non-abstract `class` definition", - context.insts().GetLocId(enclosing_scope_inst_id)); + ForbidModifiersOnDecl(context, KeywordModifierSet::Abstract, decl_kind, + " in a non-abstract `class` definition", + context.insts().GetLocId(parent_scope_inst_id)); } return; } @@ -118,15 +116,14 @@ auto CheckMethodModifiersOnFunction( " outside of a class"); } -auto RestrictExternModifierOnDecl( - Context& context, Lex::TokenKind decl_kind, - std::optional enclosing_scope_inst, bool is_definition) - -> void { +auto RestrictExternModifierOnDecl(Context& context, Lex::TokenKind decl_kind, + std::optional parent_scope_inst, + bool is_definition) -> void { if (is_definition) { ForbidModifiersOnDecl(context, KeywordModifierSet::Extern, decl_kind, " that provides a definition"); } - if (enclosing_scope_inst && !enclosing_scope_inst->Is()) { + if (parent_scope_inst && !parent_scope_inst->Is()) { ForbidModifiersOnDecl(context, KeywordModifierSet::Extern, decl_kind, " that is a member"); } @@ -134,9 +131,8 @@ auto RestrictExternModifierOnDecl( auto RequireDefaultFinalOnlyInInterfaces( Context& context, Lex::TokenKind decl_kind, - std::optional enclosing_scope_inst) -> void { - if (enclosing_scope_inst && - enclosing_scope_inst->Is()) { + std::optional parent_scope_inst) -> void { + if (parent_scope_inst && parent_scope_inst->Is()) { // Both `default` and `final` allowed in an interface definition. return; } diff --git a/toolchain/check/modifiers.h b/toolchain/check/modifiers.h index 7c9b8bbc3440..3625d2f3489a 100644 --- a/toolchain/check/modifiers.h +++ b/toolchain/check/modifiers.h @@ -10,22 +10,22 @@ namespace Carbon::Check { // Reports a diagnostic if access control modifiers on this are not allowed for -// a declaration in `enclosing_scope_inst`, and updates the declaration state in +// a declaration in `parent_scope_inst`, and updates the declaration state in // `context`. // -// `enclosing_scope_inst` may be nullopt for a declaration in a block scope. +// `parent_scope_inst` may be nullopt for a declaration in a block scope. auto CheckAccessModifiersOnDecl(Context& context, Lex::TokenKind decl_kind, - std::optional enclosing_scope_inst) + std::optional parent_scope_inst) -> void; // Reports a diagnostic if the method function modifiers `abstract`, `virtual`, // or `impl` are present but not permitted on a function declaration in -// `enclosing_scope_inst`. +// `parent_scope_inst`. // -// `enclosing_scope_inst` may be nullopt for a declaration in a block scope. +// `parent_scope_inst` may be nullopt for a declaration in a block scope. auto CheckMethodModifiersOnFunction( - Context& context, SemIR::InstId enclosing_scope_inst_id, - std::optional enclosing_scope_inst) -> void; + Context& context, SemIR::InstId parent_scope_inst_id, + std::optional parent_scope_inst) -> void; // Like `LimitModifiersOnDecl`, except says which modifiers are forbidden, and a // `context_string` (and optional `context_loc_id`) specifying the context in @@ -50,20 +50,19 @@ inline auto LimitModifiersOnDecl(Context& context, KeywordModifierSet allowed, // - `extern` on a definition. // - `extern` on a scoped entity. // -// `enclosing_scope_inst` may be nullopt for a declaration in a block scope. -auto RestrictExternModifierOnDecl( - Context& context, Lex::TokenKind decl_kind, - std::optional enclosing_scope_inst, bool is_definition) - -> void; +// `parent_scope_inst` may be nullopt for a declaration in a block scope. +auto RestrictExternModifierOnDecl(Context& context, Lex::TokenKind decl_kind, + std::optional parent_scope_inst, + bool is_definition) -> void; // Report a diagonostic if `default` and `final` modifiers are used on // declarations where they are not allowed. Right now they are only allowed // inside interfaces. // -// `enclosing_scope_inst` may be nullopt for a declaration in a block scope. +// `parent_scope_inst` may be nullopt for a declaration in a block scope. auto RequireDefaultFinalOnlyInInterfaces( Context& context, Lex::TokenKind decl_kind, - std::optional enclosing_scope_inst) -> void; + std::optional parent_scope_inst) -> void; } // namespace Carbon::Check diff --git a/toolchain/check/scope_stack.cpp b/toolchain/check/scope_stack.cpp index 4eef2f1e349e..5f45ae102b7d 100644 --- a/toolchain/check/scope_stack.cpp +++ b/toolchain/check/scope_stack.cpp @@ -81,10 +81,10 @@ auto ScopeStack::LookupInCurrentScope(SemIR::NameId name_id) -> SemIR::InstId { return result.inst_id; } -auto ScopeStack::LookupInEnclosingScopes(SemIR::NameId name_id) +auto ScopeStack::LookupInLexicalScopes(SemIR::NameId name_id) -> std::pair> { - // Find the results from enclosing lexical scopes. These will be combined with - // results from non-lexical scopes such as namespaces and classes. + // Find the results from lexical scopes. These will be combined with results + // from non-lexical scopes such as namespaces and classes. llvm::ArrayRef lexical_results = lexical_lookup_.Get(name_id); diff --git a/toolchain/check/scope_stack.h b/toolchain/check/scope_stack.h index 0c615adbb59f..3857bea920d3 100644 --- a/toolchain/check/scope_stack.h +++ b/toolchain/check/scope_stack.h @@ -102,11 +102,11 @@ class ScopeStack { // lookup result, if any. auto LookupInCurrentScope(SemIR::NameId name_id) -> SemIR::InstId; - // Looks up the name `name_id` in the current scope and its enclosing scopes. - // Returns the innermost lexical lookup result, if any, along with a list of - // non-lexical scopes in which lookup should also be performed, ordered from - // outermost to innermost. - auto LookupInEnclosingScopes(SemIR::NameId name_id) + // Looks up the name `name_id` in the current scope and related lexical + // scopes. Returns the innermost lexical lookup result, if any, along with a + // list of non-lexical scopes in which lookup should also be performed, + // ordered from outermost to innermost. + auto LookupInLexicalScopes(SemIR::NameId name_id) -> std::pair>; // Looks up the name `name_id` in the current scope. Returns the existing @@ -159,7 +159,7 @@ class ScopeStack { // The next compile-time binding index to allocate in this scope. SemIR::CompileTimeBindIndex next_compile_time_bind_index; - // Whether lexical_lookup_ has load errors from this scope or an enclosing + // Whether lexical_lookup_ has load errors from this scope or an ancestor // scope. bool lexical_lookup_has_load_error; diff --git a/toolchain/check/testdata/basics/builtin_insts.carbon b/toolchain/check/testdata/basics/builtin_insts.carbon index 349978be107c..cfcac92bccf3 100644 --- a/toolchain/check/testdata/basics/builtin_insts.carbon +++ b/toolchain/check/testdata/basics/builtin_insts.carbon @@ -15,7 +15,7 @@ // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: import_irs_size: 1 // CHECK:STDOUT: name_scopes: -// CHECK:STDOUT: name_scope0: {inst: inst+0, enclosing_scope: name_scope, has_error: false, extended_scopes: [], names: {}} +// CHECK:STDOUT: name_scope0: {inst: inst+0, parent_scope: name_scope, has_error: false, extended_scopes: [], names: {}} // CHECK:STDOUT: bind_names: {} // CHECK:STDOUT: functions: {} // CHECK:STDOUT: classes: {} diff --git a/toolchain/check/testdata/basics/no_prelude/multifile_raw_and_textual_ir.carbon b/toolchain/check/testdata/basics/no_prelude/multifile_raw_and_textual_ir.carbon index 247df256fdf7..a954a6dea6d8 100644 --- a/toolchain/check/testdata/basics/no_prelude/multifile_raw_and_textual_ir.carbon +++ b/toolchain/check/testdata/basics/no_prelude/multifile_raw_and_textual_ir.carbon @@ -27,10 +27,10 @@ fn B() {} // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: import_irs_size: 1 // CHECK:STDOUT: name_scopes: -// CHECK:STDOUT: name_scope0: {inst: inst+0, enclosing_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} +// CHECK:STDOUT: name_scope0: {inst: inst+0, parent_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} // CHECK:STDOUT: bind_names: {} // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, enclosing_scope: name_scope0, param_refs: empty, body: [block3]} +// CHECK:STDOUT: function0: {name: name0, parent_scope: name_scope0, param_refs: empty, body: [block3]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {constant: template instNamespaceType, value_rep: {kind: copy, type: type0}} @@ -88,10 +88,10 @@ fn B() {} // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: import_irs_size: 1 // CHECK:STDOUT: name_scopes: -// CHECK:STDOUT: name_scope0: {inst: inst+0, enclosing_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} +// CHECK:STDOUT: name_scope0: {inst: inst+0, parent_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} // CHECK:STDOUT: bind_names: {} // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, enclosing_scope: name_scope0, param_refs: empty, body: [block3]} +// CHECK:STDOUT: function0: {name: name0, parent_scope: name_scope0, param_refs: empty, body: [block3]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {constant: template instNamespaceType, value_rep: {kind: copy, type: type0}} diff --git a/toolchain/check/testdata/basics/no_prelude/multifile_raw_ir.carbon b/toolchain/check/testdata/basics/no_prelude/multifile_raw_ir.carbon index 319293c5bce2..31a6f5d2d582 100644 --- a/toolchain/check/testdata/basics/no_prelude/multifile_raw_ir.carbon +++ b/toolchain/check/testdata/basics/no_prelude/multifile_raw_ir.carbon @@ -27,10 +27,10 @@ fn B() {} // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: import_irs_size: 1 // CHECK:STDOUT: name_scopes: -// CHECK:STDOUT: name_scope0: {inst: inst+0, enclosing_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} +// CHECK:STDOUT: name_scope0: {inst: inst+0, parent_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} // CHECK:STDOUT: bind_names: {} // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, enclosing_scope: name_scope0, param_refs: empty, body: [block3]} +// CHECK:STDOUT: function0: {name: name0, parent_scope: name_scope0, param_refs: empty, body: [block3]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {constant: template instNamespaceType, value_rep: {kind: copy, type: type0}} @@ -67,10 +67,10 @@ fn B() {} // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: import_irs_size: 1 // CHECK:STDOUT: name_scopes: -// CHECK:STDOUT: name_scope0: {inst: inst+0, enclosing_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} +// CHECK:STDOUT: name_scope0: {inst: inst+0, parent_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+1}} // CHECK:STDOUT: bind_names: {} // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, enclosing_scope: name_scope0, param_refs: empty, body: [block3]} +// CHECK:STDOUT: function0: {name: name0, parent_scope: name_scope0, param_refs: empty, body: [block3]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {constant: template instNamespaceType, value_rep: {kind: copy, type: type0}} diff --git a/toolchain/check/testdata/basics/no_prelude/raw_and_textual_ir.carbon b/toolchain/check/testdata/basics/no_prelude/raw_and_textual_ir.carbon index cccc03d7d7a6..a48fbbf784ab 100644 --- a/toolchain/check/testdata/basics/no_prelude/raw_and_textual_ir.carbon +++ b/toolchain/check/testdata/basics/no_prelude/raw_and_textual_ir.carbon @@ -21,11 +21,11 @@ fn Foo(n: ()) -> ((), ()) { // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: import_irs_size: 1 // CHECK:STDOUT: name_scopes: -// CHECK:STDOUT: name_scope0: {inst: inst+0, enclosing_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+14}} +// CHECK:STDOUT: name_scope0: {inst: inst+0, parent_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+14}} // CHECK:STDOUT: bind_names: -// CHECK:STDOUT: bindName0: {name: name1, enclosing_scope: name_scope, index: compTimeBind} +// CHECK:STDOUT: bindName0: {name: name1, parent_scope: name_scope, index: compTimeBind} // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, enclosing_scope: name_scope0, param_refs: block3, return_type: type2, return_storage: inst+13, return_slot: present, body: [block6]} +// CHECK:STDOUT: function0: {name: name0, parent_scope: name_scope0, param_refs: block3, return_type: type2, return_storage: inst+13, return_slot: present, body: [block6]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {constant: template instNamespaceType, value_rep: {kind: copy, type: type0}} diff --git a/toolchain/check/testdata/basics/no_prelude/raw_ir.carbon b/toolchain/check/testdata/basics/no_prelude/raw_ir.carbon index 7b1b4e623bd2..2f2678282b23 100644 --- a/toolchain/check/testdata/basics/no_prelude/raw_ir.carbon +++ b/toolchain/check/testdata/basics/no_prelude/raw_ir.carbon @@ -21,11 +21,11 @@ fn Foo(n: ()) -> ((), ()) { // CHECK:STDOUT: sem_ir: // CHECK:STDOUT: import_irs_size: 1 // CHECK:STDOUT: name_scopes: -// CHECK:STDOUT: name_scope0: {inst: inst+0, enclosing_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+14}} +// CHECK:STDOUT: name_scope0: {inst: inst+0, parent_scope: name_scope, has_error: false, extended_scopes: [], names: {name0: inst+14}} // CHECK:STDOUT: bind_names: -// CHECK:STDOUT: bindName0: {name: name1, enclosing_scope: name_scope, index: compTimeBind} +// CHECK:STDOUT: bindName0: {name: name1, parent_scope: name_scope, index: compTimeBind} // CHECK:STDOUT: functions: -// CHECK:STDOUT: function0: {name: name0, enclosing_scope: name_scope0, param_refs: block3, return_type: type2, return_storage: inst+13, return_slot: present, body: [block6]} +// CHECK:STDOUT: function0: {name: name0, parent_scope: name_scope0, param_refs: block3, return_type: type2, return_storage: inst+13, return_slot: present, body: [block6]} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: types: // CHECK:STDOUT: type0: {constant: template instNamespaceType, value_rep: {kind: copy, type: type0}} diff --git a/toolchain/lower/file_context.cpp b/toolchain/lower/file_context.cpp index 7644b8a748ba..30b713a357a7 100644 --- a/toolchain/lower/file_context.cpp +++ b/toolchain/lower/file_context.cpp @@ -123,7 +123,7 @@ auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id) // Don't lower associated functions. // TODO: We shouldn't lower any function that has generic parameters. if (sem_ir().insts().Is( - sem_ir().name_scopes().Get(function.enclosing_scope_id).inst_id)) { + sem_ir().name_scopes().Get(function.parent_scope_id).inst_id)) { return nullptr; } diff --git a/toolchain/parse/context.cpp b/toolchain/parse/context.cpp index 237008b6cd97..b79400e385f7 100644 --- a/toolchain/parse/context.cpp +++ b/toolchain/parse/context.cpp @@ -470,10 +470,9 @@ auto Context::AddFunctionDefinitionStart(Lex::TokenIndex token, int subtree_start, bool has_error) -> void { if (ParsingInDeferredDefinitionScope(*this)) { - enclosing_deferred_definition_stack_.push_back( - tree_->deferred_definitions_.Add( - {.start_id = FunctionDefinitionStartId( - NodeId(tree_->node_impls_.size()))})); + deferred_definition_stack_.push_back(tree_->deferred_definitions_.Add( + {.start_id = + FunctionDefinitionStartId(NodeId(tree_->node_impls_.size()))})); } AddNode(NodeKind::FunctionDefinitionStart, token, subtree_start, has_error); @@ -482,7 +481,7 @@ auto Context::AddFunctionDefinitionStart(Lex::TokenIndex token, auto Context::AddFunctionDefinition(Lex::TokenIndex token, int subtree_start, bool has_error) -> void { if (ParsingInDeferredDefinitionScope(*this)) { - auto definition_index = enclosing_deferred_definition_stack_.pop_back_val(); + auto definition_index = deferred_definition_stack_.pop_back_val(); auto& definition = tree_->deferred_definitions_.Get(definition_index); definition.definition_id = FunctionDefinitionId(NodeId(tree_->node_impls_.size())); diff --git a/toolchain/parse/context.h b/toolchain/parse/context.h index 09a147b0ff38..0ccf6f944bba 100644 --- a/toolchain/parse/context.h +++ b/toolchain/parse/context.h @@ -376,8 +376,7 @@ class Context { // The deferred definition indexes of functions whose definitions have begun // but not yet finished. - llvm::SmallVector - enclosing_deferred_definition_stack_; + llvm::SmallVector deferred_definition_stack_; // The current packaging state, whether `import`/`package` are allowed. PackagingState packaging_state_ = PackagingState::FileStart; diff --git a/toolchain/sem_ir/bind_name.h b/toolchain/sem_ir/bind_name.h index 53331c7eb3ef..a6b65a4212ce 100644 --- a/toolchain/sem_ir/bind_name.h +++ b/toolchain/sem_ir/bind_name.h @@ -13,14 +13,14 @@ namespace Carbon::SemIR { struct BindNameInfo : public Printable { auto Print(llvm::raw_ostream& out) const -> void { - out << "{name: " << name_id << ", enclosing_scope: " << enclosing_scope_id + out << "{name: " << name_id << ", parent_scope: " << parent_scope_id << ", index: " << bind_index << "}"; } // The name. NameId name_id; - // The enclosing scope. - NameScopeId enclosing_scope_id; + // The parent scope. + NameScopeId parent_scope_id; // The index for a compile-time binding. Invalid for a runtime binding. CompileTimeBindIndex bind_index; }; @@ -37,13 +37,13 @@ inline auto CarbonHashValue(const BindNameInfo& value, uint64_t seed) struct BindNameInfoDenseMapInfo { static auto getEmptyKey() -> BindNameInfo { return BindNameInfo{.name_id = NameId::Invalid, - .enclosing_scope_id = NameScopeId::Invalid, + .parent_scope_id = NameScopeId::Invalid, .bind_index = CompileTimeBindIndex( CompileTimeBindIndex::InvalidIndex - 1)}; } static auto getTombstoneKey() -> BindNameInfo { return BindNameInfo{.name_id = NameId::Invalid, - .enclosing_scope_id = NameScopeId::Invalid, + .parent_scope_id = NameScopeId::Invalid, .bind_index = CompileTimeBindIndex( CompileTimeBindIndex::InvalidIndex - 2)}; } diff --git a/toolchain/sem_ir/class.h b/toolchain/sem_ir/class.h index 6765c1a45ca7..79c4faa233c7 100644 --- a/toolchain/sem_ir/class.h +++ b/toolchain/sem_ir/class.h @@ -21,8 +21,7 @@ struct Class : public Printable { }; auto Print(llvm::raw_ostream& out) const -> void { - out << "{name: " << name_id << ", enclosing_scope: " << enclosing_scope_id - << "}"; + out << "{name: " << name_id << ", parent_scope: " << parent_scope_id << "}"; } // Determines whether this class has been fully defined. This is false until @@ -39,8 +38,8 @@ struct Class : public Printable { // The class name. NameId name_id; - // The enclosing scope. - NameScopeId enclosing_scope_id; + // The parent scope. + NameScopeId parent_scope_id; // A block containing a single reference instruction per implicit parameter. InstBlockId implicit_param_refs_id; // A block containing a single reference instruction per parameter. diff --git a/toolchain/sem_ir/function.h b/toolchain/sem_ir/function.h index 471691566fa8..ae42abbd1aa5 100644 --- a/toolchain/sem_ir/function.h +++ b/toolchain/sem_ir/function.h @@ -28,7 +28,7 @@ struct Function : public Printable { }; auto Print(llvm::raw_ostream& out) const -> void { - out << "{name: " << name_id << ", enclosing_scope: " << enclosing_scope_id + out << "{name: " << name_id << ", parent_scope: " << parent_scope_id << ", param_refs: " << param_refs_id; if (return_type_id.is_valid()) { out << ", return_type: " << return_type_id; @@ -79,8 +79,8 @@ struct Function : public Printable { // The function name. NameId name_id; - // The enclosing scope. - NameScopeId enclosing_scope_id; + // The parent scope. + NameScopeId parent_scope_id; // The first declaration of the function. This is a FunctionDecl. InstId decl_id; // A block containing a single reference instruction per implicit parameter. diff --git a/toolchain/sem_ir/interface.h b/toolchain/sem_ir/interface.h index ed3ba552a118..1fcee5f0cdf0 100644 --- a/toolchain/sem_ir/interface.h +++ b/toolchain/sem_ir/interface.h @@ -12,8 +12,7 @@ namespace Carbon::SemIR { // An interface. struct Interface : public Printable { auto Print(llvm::raw_ostream& out) const -> void { - out << "{name: " << name_id << ", enclosing_scope: " << enclosing_scope_id - << "}"; + out << "{name: " << name_id << ", parent_scope: " << parent_scope_id << "}"; } // Determines whether this interface has been fully defined. This is false @@ -31,8 +30,8 @@ struct Interface : public Printable { // The interface name. NameId name_id; - // The enclosing scope. - NameScopeId enclosing_scope_id; + // The parent scope. + NameScopeId parent_scope_id; // The first declaration of the interface. This is a InterfaceDecl. InstId decl_id; diff --git a/toolchain/sem_ir/name_scope.h b/toolchain/sem_ir/name_scope.h index 267d55ef4a82..b2e864cb8534 100644 --- a/toolchain/sem_ir/name_scope.h +++ b/toolchain/sem_ir/name_scope.h @@ -12,7 +12,7 @@ namespace Carbon::SemIR { struct NameScope : Printable { auto Print(llvm::raw_ostream& out) const -> void { - out << "{inst: " << inst_id << ", enclosing_scope: " << enclosing_scope_id + out << "{inst: " << inst_id << ", parent_scope: " << parent_scope_id << ", has_error: " << (has_error ? "true" : "false"); out << ", extended_scopes: ["; @@ -66,8 +66,8 @@ struct NameScope : Printable { // When the scope is a namespace, the name. Otherwise, invalid. NameId name_id; - // The scope enclosing this one. - NameScopeId enclosing_scope_id; + // The parent scope. + NameScopeId parent_scope_id; // Whether we have diagnosed an error in a construct that would have added // names to this scope. For example, this can happen if an `import` failed or @@ -91,11 +91,11 @@ class NameScopeStore { explicit NameScopeStore(InstStore* insts) : insts_(insts) {} // Adds a name scope, returning an ID to reference it. - auto Add(InstId inst_id, NameId name_id, NameScopeId enclosing_scope_id) + auto Add(InstId inst_id, NameId name_id, NameScopeId parent_scope_id) -> NameScopeId { return values_.Add({.inst_id = inst_id, .name_id = name_id, - .enclosing_scope_id = enclosing_scope_id}); + .parent_scope_id = parent_scope_id}); } // Returns the requested name scope.