Skip to content

Commit

Permalink
has_definition_started accessor for entities (#4730)
Browse files Browse the repository at this point in the history
Note that I left some calls to `is_defined()` where I thought they were
interchangeable.

---------

Co-authored-by: Josh L <[email protected]>
  • Loading branch information
josh11b and josh11b authored Dec 20, 2024
1 parent 9b72b0e commit 01ca9f0
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion toolchain/check/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion toolchain/check/handle_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion toolchain/check/handle_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion toolchain/sem_ir/entity_with_params_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion toolchain/sem_ir/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

Expand Down
2 changes: 1 addition & 1 deletion toolchain/sem_ir/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

Expand Down

0 comments on commit 01ca9f0

Please sign in to comment.