From 01ca9f05ddc42888af5017834690e5323d1ac0ac Mon Sep 17 00:00:00 2001 From: josh11b <15258583+josh11b@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:29:50 -0800 Subject: [PATCH] `has_definition_started` accessor for entities (#4730) Note that I left some calls to `is_defined()` where I thought they were interchangeable. --------- Co-authored-by: Josh L --- toolchain/check/context.cpp | 2 +- toolchain/check/handle_function.cpp | 2 +- toolchain/check/handle_impl.cpp | 2 +- toolchain/sem_ir/entity_with_params_base.h | 8 +++++++- toolchain/sem_ir/impl.h | 2 +- toolchain/sem_ir/interface.h | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/toolchain/check/context.cpp b/toolchain/check/context.cpp index 193cb52e999f..5250697f77da 100644 --- a/toolchain/check/context.cpp +++ b/toolchain/check/context.cpp @@ -277,7 +277,7 @@ auto Context::NoteIncompleteClass(SemIR::ClassId class_id, DiagnosticBuilder& builder) -> void { const auto& class_info = classes().Get(class_id); CARBON_CHECK(!class_info.is_defined(), "Class is not incomplete"); - if (class_info.definition_id.is_valid()) { + if (class_info.has_definition_started()) { CARBON_DIAGNOSTIC(ClassIncompleteWithinDefinition, Note, "class is incomplete within its definition"); builder.Note(class_info.definition_id, ClassIncompleteWithinDefinition); diff --git a/toolchain/check/handle_function.cpp b/toolchain/check/handle_function.cpp index 0c8cd63b7646..8e88fd697c80 100644 --- a/toolchain/check/handle_function.cpp +++ b/toolchain/check/handle_function.cpp @@ -93,7 +93,7 @@ static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc, CheckIsAllowedRedecl(context, Lex::TokenKind::Fn, prev_function.name_id, RedeclInfo(new_function, new_loc, new_is_definition), RedeclInfo(prev_function, prev_function.latest_decl_id(), - prev_function.definition_id.is_valid()), + prev_function.has_definition_started()), prev_import_ir_id); if (!prev_function.first_owning_decl_id.is_valid()) { diff --git a/toolchain/check/handle_impl.cpp b/toolchain/check/handle_impl.cpp index 0b90fa073955..9d257526a8c9 100644 --- a/toolchain/check/handle_impl.cpp +++ b/toolchain/check/handle_impl.cpp @@ -389,7 +389,7 @@ auto HandleParseNode(Context& context, Parse::ImplDefinitionStartId node_id) BuildImplDecl(context, node_id, /*is_definition=*/true); auto& impl_info = context.impls().Get(impl_id); - if (impl_info.is_defined()) { + if (impl_info.has_definition_started()) { CARBON_DIAGNOSTIC(ImplRedefinition, Error, "redefinition of `impl {0} as {1}`", InstIdAsRawType, InstIdAsRawType); diff --git a/toolchain/sem_ir/entity_with_params_base.h b/toolchain/sem_ir/entity_with_params_base.h index 606b405e1abc..a4fa9713fa2d 100644 --- a/toolchain/sem_ir/entity_with_params_base.h +++ b/toolchain/sem_ir/entity_with_params_base.h @@ -51,6 +51,12 @@ struct EntityWithParamsBase { definition_id = definition.definition_id; } + // Determines whether the definition of this entity has begun. This is false + // until we reach the `{` of the definition. + auto has_definition_started() const -> bool { + return definition_id.is_valid(); + } + // Returns the instruction for the first declaration. auto first_decl_id() const -> SemIR::InstId { if (non_owning_decl_id.is_valid()) { @@ -62,7 +68,7 @@ struct EntityWithParamsBase { // Returns the instruction for the latest declaration. auto latest_decl_id() const -> SemIR::InstId { - if (definition_id.is_valid()) { + if (has_definition_started()) { return definition_id; } if (first_owning_decl_id.is_valid()) { diff --git a/toolchain/sem_ir/impl.h b/toolchain/sem_ir/impl.h index d07c86512440..911dd541b4bb 100644 --- a/toolchain/sem_ir/impl.h +++ b/toolchain/sem_ir/impl.h @@ -51,7 +51,7 @@ struct Impl : public EntityWithParamsBase, // Determines whether this impl's definition has begun but not yet ended. auto is_being_defined() const -> bool { - return definition_id.is_valid() && !is_defined(); + return has_definition_started() && !is_defined(); } }; diff --git a/toolchain/sem_ir/interface.h b/toolchain/sem_ir/interface.h index 5d967b0b40d9..6fdbe8029ce0 100644 --- a/toolchain/sem_ir/interface.h +++ b/toolchain/sem_ir/interface.h @@ -43,7 +43,7 @@ struct Interface : public EntityWithParamsBase, // Determines whether we're currently defining the interface. This is true // between the braces of the interface. auto is_being_defined() const -> bool { - return definition_id.is_valid() && !is_defined(); + return has_definition_started() && !is_defined(); } };