Procházet zdrojové kódy

`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 <josh11b@users.noreply.github.com>
josh11b před 1 rokem
rodič
revize
01ca9f05dd

+ 1 - 1
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);

+ 1 - 1
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()) {

+ 1 - 1
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);

+ 7 - 1
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()) {

+ 1 - 1
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();
   }
 };
 

+ 1 - 1
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();
   }
 };