From 517a416852c0f70111d8a2fe25a78035735ad93c Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Fri, 31 May 2024 16:23:57 -0700 Subject: [PATCH] Clean up some misc toolchain braced inits. (#4013) Following up on #4012 and #4009, clean scattered cases which could be making better use of designated initializers. --- toolchain/check/check.cpp | 4 +++- toolchain/check/context.cpp | 13 ++++++++----- toolchain/check/decl_name_stack.cpp | 3 ++- toolchain/check/handle_class.cpp | 7 ++++--- toolchain/check/handle_function.cpp | 4 +++- toolchain/check/handle_index.cpp | 2 +- toolchain/check/handle_interface.cpp | 8 ++++---- toolchain/check/handle_let.cpp | 5 +++-- toolchain/check/lexical_lookup.h | 6 ++++-- toolchain/check/node_stack.h | 4 ++-- toolchain/check/pending_block.h | 18 ++++++++++++------ toolchain/check/scope_stack.cpp | 10 ++++++---- toolchain/parse/handle_decl_scope_loop.cpp | 18 ++++++++++++------ toolchain/parse/tree.cpp | 19 +++++++++++-------- toolchain/sem_ir/file.cpp | 10 +++++----- toolchain/testing/yaml_test_helpers.cpp | 10 +++++----- 16 files changed, 85 insertions(+), 56 deletions(-) diff --git a/toolchain/check/check.cpp b/toolchain/check/check.cpp index 528ebc518f07..7ec6826d34a1 100644 --- a/toolchain/check/check.cpp +++ b/toolchain/check/check.cpp @@ -503,7 +503,9 @@ class DeferredDefinitionWorklist { enclosing_scopes_.push_back( {.worklist_start_index = worklist_.size(), .scope_index = context.scope_stack().PeekIndex()}); - worklist_.push_back(EnterDeferredDefinitionScope{std::nullopt, nested}); + worklist_.push_back( + EnterDeferredDefinitionScope{.suspended_name = std::nullopt, + .in_deferred_definition_scope = nested}); CARBON_VLOG() << VlogPrefix << "Push EnterDeferredDefinitionScope " << (nested ? "(nested)" : "(non-nested)") << "\n"; } diff --git a/toolchain/check/context.cpp b/toolchain/check/context.cpp index 33844c85f93c..784184bbbd45 100644 --- a/toolchain/check/context.cpp +++ b/toolchain/check/context.cpp @@ -672,7 +672,8 @@ class TypeCompleter { // Adds `type_id` to the work list, if it's not already complete. auto Push(SemIR::TypeId type_id) -> void { if (!context_.types().IsComplete(type_id)) { - work_list_.push_back({type_id, Phase::AddNestedIncompleteTypes}); + work_list_.push_back( + {.type_id = type_id, .phase = Phase::AddNestedIncompleteTypes}); } } @@ -712,7 +713,8 @@ class TypeCompleter { // should never fail: the value representation shouldn't require any // additional nested types to be complete. if (!context_.types().IsComplete(value_rep.type_id)) { - work_list_.push_back({value_rep.type_id, Phase::BuildValueRepr}); + work_list_.push_back( + {.type_id = value_rep.type_id, .phase = Phase::BuildValueRepr}); } // For a pointer representation, the pointee also needs to be complete. if (value_rep.kind == SemIR::ValueRepr::Pointer) { @@ -722,7 +724,8 @@ class TypeCompleter { auto pointee_type_id = context_.sem_ir().GetPointeeType(value_rep.type_id); if (!context_.types().IsComplete(pointee_type_id)) { - work_list_.push_back({pointee_type_id, Phase::BuildValueRepr}); + work_list_.push_back( + {.type_id = pointee_type_id, .phase = Phase::BuildValueRepr}); } } break; @@ -1050,9 +1053,9 @@ template static auto GetTypeImpl(Context& context, EachArgT... each_arg) -> SemIR::TypeId { // TODO: Remove inst_id parameter from TryEvalInst. + InstT inst = {SemIR::TypeId::TypeType, each_arg...}; return context.GetTypeIdForTypeConstant( - TryEvalInst(context, SemIR::InstId::Invalid, - InstT{SemIR::TypeId::TypeType, each_arg...})); + TryEvalInst(context, SemIR::InstId::Invalid, inst)); } // Gets or forms a type_id for a type, given the instruction kind and arguments, diff --git a/toolchain/check/decl_name_stack.cpp b/toolchain/check/decl_name_stack.cpp index fe5c713b0fa3..182ef1d313ea 100644 --- a/toolchain/check/decl_name_stack.cpp +++ b/toolchain/check/decl_name_stack.cpp @@ -86,7 +86,8 @@ auto DeclNameStack::PopScope() -> void { auto DeclNameStack::Suspend() -> SuspendedName { CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Finished) << "Missing call to FinishName before Suspend"; - SuspendedName result = {decl_name_stack_.pop_back_val(), {}}; + SuspendedName result = {.name_context = decl_name_stack_.pop_back_val(), + .scopes = {}}; auto enclosing_index = result.name_context.initial_scope_index; auto& scope_stack = context_->scope_stack(); while (scope_stack.PeekIndex() > enclosing_index) { diff --git a/toolchain/check/handle_class.cpp b/toolchain/check/handle_class.cpp index 677768867b4c..546a3504cc06 100644 --- a/toolchain/check/handle_class.cpp +++ b/toolchain/check/handle_class.cpp @@ -254,9 +254,10 @@ static auto BuildClassDecl(Context& context, Parse::AnyClassDeclId node_id, auto& class_info = context.classes().Get(class_decl.class_id); if (class_info.is_generic()) { // TODO: Pass in the generic arguments once we can represent them. - class_info.self_type_id = context.GetTypeIdForTypeConstant(TryEvalInst( - context, SemIR::InstId::Invalid, - SemIR::ClassType{SemIR::TypeId::TypeType, class_decl.class_id})); + class_info.self_type_id = context.GetTypeIdForTypeConstant( + TryEvalInst(context, SemIR::InstId::Invalid, + SemIR::ClassType{.type_id = SemIR::TypeId::TypeType, + .class_id = class_decl.class_id})); } else { class_info.self_type_id = context.GetTypeIdForTypeInst(class_decl_id); } diff --git a/toolchain/check/handle_function.cpp b/toolchain/check/handle_function.cpp index fa84c528ec49..e5c72d471b75 100644 --- a/toolchain/check/handle_function.cpp +++ b/toolchain/check/handle_function.cpp @@ -371,7 +371,9 @@ auto HandleFunctionDefinitionSuspend(Context& context, // Process the declaration portion of the function. auto [function_id, decl_id] = BuildFunctionDecl(context, node_id, /*is_definition=*/true); - return {function_id, decl_id, context.decl_name_stack().Suspend()}; + return {.function_id = function_id, + .decl_id = decl_id, + .saved_name_state = context.decl_name_stack().Suspend()}; } auto HandleFunctionDefinitionResume(Context& context, diff --git a/toolchain/check/handle_index.cpp b/toolchain/check/handle_index.cpp index b62d8ba9579a..0daa60cb0c56 100644 --- a/toolchain/check/handle_index.cpp +++ b/toolchain/check/handle_index.cpp @@ -28,7 +28,7 @@ static auto ValidateTupleIndex(Context& context, Parse::NodeId node_id, "Tuple element index `{0}` is past the end of type `{1}`.", TypedInt, SemIR::TypeId); context.emitter().Emit(node_id, TupleIndexOutOfBounds, - TypedInt{index_inst.type_id, index_val}, + {.type = index_inst.type_id, .value = index_val}, operand_inst.type_id()); return nullptr; } diff --git a/toolchain/check/handle_interface.cpp b/toolchain/check/handle_interface.cpp index 6b092a16112b..be6c28ff8ebf 100644 --- a/toolchain/check/handle_interface.cpp +++ b/toolchain/check/handle_interface.cpp @@ -147,10 +147,10 @@ auto HandleInterfaceDefinitionStart(Context& context, {.name_id = SemIR::NameId::SelfType, .enclosing_scope_id = interface_info.scope_id, .bind_index = context.scope_stack().AddCompileTimeBinding()}); - interface_info.self_param_id = - context.AddInst({Parse::NodeId::Invalid, - SemIR::BindSymbolicName{self_type_id, bind_name_id, - SemIR::InstId::Invalid}}); + interface_info.self_param_id = context.AddInst( + SemIR::LocId::Invalid, {.type_id = self_type_id, + .bind_name_id = bind_name_id, + .value_id = SemIR::InstId::Invalid}); context.name_scopes() .Get(interface_info.scope_id) .names.insert({SemIR::NameId::SelfType, interface_info.self_param_id}); diff --git a/toolchain/check/handle_let.cpp b/toolchain/check/handle_let.cpp index 7c4355d7a2e6..397a651a390d 100644 --- a/toolchain/check/handle_let.cpp +++ b/toolchain/check/handle_let.cpp @@ -47,8 +47,9 @@ static auto BuildAssociatedConstantDecl( auto name_id = context.bind_names().Get(binding_pattern->bind_name_id).name_id; context.ReplaceLocIdAndInstBeforeConstantUse( - pattern_id, {node_id, SemIR::AssociatedConstantDecl{ - binding_pattern->type_id, name_id}}); + pattern_id, + SemIR::LocIdAndInst(node_id, SemIR::AssociatedConstantDecl{ + binding_pattern->type_id, name_id})); auto decl_id = pattern_id; context.inst_block_stack().AddInstId(decl_id); diff --git a/toolchain/check/lexical_lookup.h b/toolchain/check/lexical_lookup.h index d76ee0516f27..2c92e714bc6c 100644 --- a/toolchain/check/lexical_lookup.h +++ b/toolchain/check/lexical_lookup.h @@ -61,12 +61,14 @@ class LexicalLookup { << "Suspending a nonexistent result for " << name_id << "."; CARBON_CHECK(index <= std::numeric_limits::max()) << "Unexpectedly large index " << index << " for name ID"; - return {static_cast(index), results.pop_back_val().inst_id}; + return {.index = static_cast(index), + .inst_id = results.pop_back_val().inst_id}; } // Restore a previously-suspended lookup result. auto Restore(SuspendedResult sus, ScopeIndex index) -> void { - lookup_[sus.index].push_back({sus.inst_id, index}); + lookup_[sus.index].push_back( + {.inst_id = sus.inst_id, .scope_index = index}); } private: diff --git a/toolchain/check/node_stack.h b/toolchain/check/node_stack.h index 1b4470ee3b04..989f964c4c06 100644 --- a/toolchain/check/node_stack.h +++ b/toolchain/check/node_stack.h @@ -84,7 +84,7 @@ class NodeStack { << " -> \n"; CARBON_CHECK(stack_.size() < (1 << 20)) << "Excessive stack size: likely infinite loop"; - stack_.push_back(Entry{node_id, Id()}); + stack_.push_back({.node_id = node_id, .id = Id()}); } // Pushes a parse tree node onto the stack with an ID. @@ -100,7 +100,7 @@ class NodeStack { << id << "\n"; CARBON_CHECK(stack_.size() < (1 << 20)) << "Excessive stack size: likely infinite loop"; - stack_.push_back(Entry{node_id, Id(id)}); + stack_.push_back({.node_id = node_id, .id = Id(id)}); } // Returns whether there is a node of the specified kind on top of the stack. diff --git a/toolchain/check/pending_block.h b/toolchain/check/pending_block.h index 9f7c3a9f1ad6..19ea39c684f6 100644 --- a/toolchain/check/pending_block.h +++ b/toolchain/check/pending_block.h @@ -7,6 +7,7 @@ #include "llvm/ADT/SmallVector.h" #include "toolchain/check/context.h" +#include "toolchain/sem_ir/inst.h" namespace Carbon::Check { @@ -65,9 +66,12 @@ class PendingBlock { // 1) The block is empty. Replace `target_id` with an empty splice // pointing at `value_id`. context_.ReplaceLocIdAndInstBeforeConstantUse( - target_id, {value.loc_id, - SemIR::SpliceBlock{value.inst.type_id(), - SemIR::InstBlockId::Empty, value_id}}); + target_id, + SemIR::LocIdAndInst( + value.loc_id, + SemIR::SpliceBlock{.type_id = value.inst.type_id(), + .block_id = SemIR::InstBlockId::Empty, + .result_id = value_id})); } else if (insts_.size() == 1 && insts_[0] == value_id) { // 2) The block is {value_id}. Replace `target_id` with the instruction // referred to by `value_id`. This is intended to be the common case. @@ -76,9 +80,11 @@ class PendingBlock { // 3) Anything else: splice it into the IR, replacing `target_id`. context_.ReplaceLocIdAndInstBeforeConstantUse( target_id, - {value.loc_id, - SemIR::SpliceBlock{value.inst.type_id(), - context_.inst_blocks().Add(insts_), value_id}}); + SemIR::LocIdAndInst( + value.loc_id, + SemIR::SpliceBlock{.type_id = value.inst.type_id(), + .block_id = context_.inst_blocks().Add(insts_), + .result_id = value_id})); } // Prepare to stash more pending instructions. diff --git a/toolchain/check/scope_stack.cpp b/toolchain/check/scope_stack.cpp index 0c7225c98bda..4eef2f1e349e 100644 --- a/toolchain/check/scope_stack.cpp +++ b/toolchain/check/scope_stack.cpp @@ -26,7 +26,8 @@ auto ScopeStack::Push(SemIR::InstId scope_inst_id, SemIR::NameScopeId scope_id, .lexical_lookup_has_load_error = LexicalLookupHasLoadError() || lexical_lookup_has_load_error}); if (scope_id.is_valid()) { - non_lexical_scope_stack_.push_back({next_scope_index_, scope_id}); + non_lexical_scope_stack_.push_back( + {.scope_index = next_scope_index_, .name_scope_id = scope_id}); } // TODO: Handle this case more gracefully. @@ -145,7 +146,8 @@ auto ScopeStack::SetReturnedVarOrGetExisting(SemIR::InstId inst_id) auto ScopeStack::Suspend() -> SuspendedScope { CARBON_CHECK(!scope_stack_.empty()) << "No scope to suspend"; - SuspendedScope result = {scope_stack_.pop_back_val(), {}}; + SuspendedScope result = {.entry = scope_stack_.pop_back_val(), + .suspended_lookups = {}}; if (result.entry.scope_id.is_valid()) { non_lexical_scope_stack_.pop_back(); } @@ -167,8 +169,8 @@ auto ScopeStack::Restore(SuspendedScope scope) -> void { lexical_lookup_.Restore(std::move(entry), scope.entry.index); } if (scope.entry.scope_id.is_valid()) { - non_lexical_scope_stack_.push_back( - {scope.entry.index, scope.entry.scope_id}); + non_lexical_scope_stack_.push_back({.scope_index = scope.entry.index, + .name_scope_id = scope.entry.scope_id}); } scope_stack_.push_back(std::move(scope.entry)); } diff --git a/toolchain/parse/handle_decl_scope_loop.cpp b/toolchain/parse/handle_decl_scope_loop.cpp index 1c6f1f4c9f7a..9fb20a148e92 100644 --- a/toolchain/parse/handle_decl_scope_loop.cpp +++ b/toolchain/parse/handle_decl_scope_loop.cpp @@ -59,18 +59,24 @@ struct DeclIntroducerInfo { static constexpr auto DeclIntroducers = [] { DeclIntroducerInfo introducers[] = { -#define CARBON_TOKEN(Name) \ - {DeclIntroducerKind::Unrecognized, NodeKind::InvalidParse, State::Invalid}, +#define CARBON_TOKEN(Name) \ + {.introducer_kind = DeclIntroducerKind::Unrecognized, \ + .node_kind = NodeKind::InvalidParse, \ + .state = State::Invalid}, #include "toolchain/lex/token_kind.def" }; auto set = [&](Lex::TokenKind token_kind, NodeKind node_kind, State state) { - introducers[token_kind.AsInt()] = {DeclIntroducerKind::NonPackagingDecl, - node_kind, state}; + introducers[token_kind.AsInt()] = { + .introducer_kind = DeclIntroducerKind::NonPackagingDecl, + .node_kind = node_kind, + .state = state}; }; auto set_packaging = [&](Lex::TokenKind token_kind, NodeKind node_kind, State state) { - introducers[token_kind.AsInt()] = {DeclIntroducerKind::PackagingDecl, - node_kind, state}; + introducers[token_kind.AsInt()] = { + .introducer_kind = DeclIntroducerKind::PackagingDecl, + .node_kind = node_kind, + .state = state}; }; set(Lex::TokenKind::Adapt, NodeKind::AdaptIntroducer, diff --git a/toolchain/parse/tree.cpp b/toolchain/parse/tree.cpp index f610f87cc6e7..7a1f248f54ed 100644 --- a/toolchain/parse/tree.cpp +++ b/toolchain/parse/tree.cpp @@ -15,8 +15,9 @@ namespace Carbon::Parse { auto Tree::postorder() const -> llvm::iterator_range { - return {PostorderIterator(NodeId(0)), - PostorderIterator(NodeId(node_impls_.size()))}; + return llvm::iterator_range( + PostorderIterator(NodeId(0)), + PostorderIterator(NodeId(node_impls_.size()))); } auto Tree::postorder(NodeId n) const @@ -26,21 +27,23 @@ auto Tree::postorder(NodeId n) const // its subtree. int end_index = n.index + 1; int start_index = end_index - node_impls_[n.index].subtree_size; - return {PostorderIterator(NodeId(start_index)), - PostorderIterator(NodeId(end_index))}; + return llvm::iterator_range( + PostorderIterator(NodeId(start_index)), + PostorderIterator(NodeId(end_index))); } auto Tree::children(NodeId n) const -> llvm::iterator_range { CARBON_CHECK(n.is_valid()); int end_index = n.index - node_impls_[n.index].subtree_size; - return {SiblingIterator(*this, NodeId(n.index - 1)), - SiblingIterator(*this, NodeId(end_index))}; + return llvm::iterator_range( + SiblingIterator(*this, NodeId(n.index - 1)), + SiblingIterator(*this, NodeId(end_index))); } auto Tree::roots() const -> llvm::iterator_range { - return { + return llvm::iterator_range( SiblingIterator(*this, NodeId(static_cast(node_impls_.size()) - 1)), - SiblingIterator(*this, NodeId(-1))}; + SiblingIterator(*this, NodeId(-1))); } auto Tree::node_has_error(NodeId n) const -> bool { diff --git a/toolchain/sem_ir/file.cpp b/toolchain/sem_ir/file.cpp index 337245798fde..f727f50dca78 100644 --- a/toolchain/sem_ir/file.cpp +++ b/toolchain/sem_ir/file.cpp @@ -76,11 +76,11 @@ File::File(CheckIRId check_ir_id, SharedValueStores& value_stores, // Error uses a self-referential type so that it's not accidentally treated as // a normal type. Every other builtin is a type, including the // self-referential TypeType. -#define CARBON_SEM_IR_BUILTIN_KIND(Name, ...) \ - insts_.AddInNoBlock(LocIdAndInst::NoLoc( \ - Builtin{BuiltinKind::Name == BuiltinKind::Error ? TypeId::Error \ - : TypeId::TypeType, \ - BuiltinKind::Name})); +#define CARBON_SEM_IR_BUILTIN_KIND(Name, ...) \ + insts_.AddInNoBlock(LocIdAndInst::NoLoc( \ + {.type_id = BuiltinKind::Name == BuiltinKind::Error ? TypeId::Error \ + : TypeId::TypeType, \ + .builtin_kind = BuiltinKind::Name})); #include "toolchain/sem_ir/builtin_kind.def" CARBON_CHECK(insts_.size() == BuiltinKind::ValidCount) << "Builtins should produce " << BuiltinKind::ValidCount diff --git a/toolchain/testing/yaml_test_helpers.cpp b/toolchain/testing/yaml_test_helpers.cpp index fa7943a9f302..4eff3f2a19e2 100644 --- a/toolchain/testing/yaml_test_helpers.cpp +++ b/toolchain/testing/yaml_test_helpers.cpp @@ -15,7 +15,7 @@ static auto Parse(llvm::yaml::Node* node) -> Value { // getType returns an unsigned int which should map to the enum. switch (static_cast(node->getType())) { case llvm::yaml::Node::NK_Null: - return Value{NullValue()}; + return Value(NullValue()); case llvm::yaml::Node::NK_Scalar: { llvm::SmallString<128> storage; @@ -35,7 +35,7 @@ static auto Parse(llvm::yaml::Node* node) -> Value { Value value = Parse(kv.getValue()); v.emplace_back(std::move(key), std::move(value)); } - return Value{std::move(v)}; + return Value(std::move(v)); } case llvm::yaml::Node::NK_Sequence: { @@ -43,11 +43,11 @@ static auto Parse(llvm::yaml::Node* node) -> Value { for (llvm::yaml::Node& n : llvm::cast(*node)) { v.push_back(Parse(&n)); } - return Value{std::move(v)}; + return Value(std::move(v)); } case llvm::yaml::Node::NK_Alias: - return Value{AliasValue()}; + return Value(AliasValue()); case llvm::yaml::Node::NK_KeyValue: llvm_unreachable("should only exist as child of mapping"); @@ -114,7 +114,7 @@ auto operator<<(std::ostream& os, const Value& v) -> std::ostream& { std::ostream& out; }; - std::visit(Printer{os}, v); + std::visit(Printer{.out = os}, v); return os; }