Преглед изворни кода

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.
Jon Ross-Perkins пре 1 година
родитељ
комит
517a416852

+ 3 - 1
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";
   }

+ 8 - 5
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 <typename InstT, typename... EachArgT>
 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,

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

+ 4 - 3
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);
     }

+ 3 - 1
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,

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

+ 4 - 4
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::BindSymbolicName>(
+        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});

+ 3 - 2
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);
 

+ 4 - 2
toolchain/check/lexical_lookup.h

@@ -61,12 +61,14 @@ class LexicalLookup {
         << "Suspending a nonexistent result for " << name_id << ".";
     CARBON_CHECK(index <= std::numeric_limits<uint32_t>::max())
         << "Unexpectedly large index " << index << " for name ID";
-    return {static_cast<uint32_t>(index), results.pop_back_val().inst_id};
+    return {.index = static_cast<uint32_t>(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:

+ 2 - 2
toolchain/check/node_stack.h

@@ -84,7 +84,7 @@ class NodeStack {
                   << " -> <none>\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.

+ 12 - 6
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.

+ 6 - 4
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));
 }

+ 12 - 6
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,

+ 11 - 8
toolchain/parse/tree.cpp

@@ -15,8 +15,9 @@
 namespace Carbon::Parse {
 
 auto Tree::postorder() const -> llvm::iterator_range<PostorderIterator> {
-  return {PostorderIterator(NodeId(0)),
-          PostorderIterator(NodeId(node_impls_.size()))};
+  return llvm::iterator_range<PostorderIterator>(
+      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>(
+      PostorderIterator(NodeId(start_index)),
+      PostorderIterator(NodeId(end_index)));
 }
 
 auto Tree::children(NodeId n) const -> llvm::iterator_range<SiblingIterator> {
   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>(
+      SiblingIterator(*this, NodeId(n.index - 1)),
+      SiblingIterator(*this, NodeId(end_index)));
 }
 
 auto Tree::roots() const -> llvm::iterator_range<SiblingIterator> {
-  return {
+  return llvm::iterator_range<SiblingIterator>(
       SiblingIterator(*this, NodeId(static_cast<int>(node_impls_.size()) - 1)),
-      SiblingIterator(*this, NodeId(-1))};
+      SiblingIterator(*this, NodeId(-1)));
 }
 
 auto Tree::node_has_error(NodeId n) const -> bool {

+ 5 - 5
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<Builtin>(                           \
+      {.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

+ 5 - 5
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<llvm::yaml::Node::NodeKind>(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<llvm::yaml::SequenceNode>(*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;
 }