Explorar el Código

Change `PendingBlock.context_` from reference to pointer (#5209)

Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
Boaz Brickner hace 1 año
padre
commit
9d3664baa9
Se han modificado 2 ficheros con 12 adiciones y 11 borrados
  1. 3 3
      toolchain/check/convert.cpp
  2. 9 8
      toolchain/check/pending_block.h

+ 3 - 3
toolchain/check/convert.cpp

@@ -286,7 +286,7 @@ static auto ConvertTupleToArray(Context& context, SemIR::TupleType tuple_type,
     return SemIR::ErrorInst::SingletonInstId;
   }
 
-  PendingBlock target_block_storage(context);
+  PendingBlock target_block_storage(&context);
   PendingBlock* target_block =
       target.init_block ? target.init_block : &target_block_storage;
 
@@ -604,7 +604,7 @@ static auto ConvertStructToClass(Context& context, SemIR::StructType src_type,
                                  SemIR::ClassType dest_type,
                                  SemIR::InstId value_id,
                                  ConversionTarget target) -> SemIR::InstId {
-  PendingBlock target_block(context);
+  PendingBlock target_block(&context);
   auto& dest_class_info = context.classes().Get(dest_type.class_id);
   CARBON_CHECK(dest_class_info.inheritance_kind != SemIR::Class::Abstract);
   auto object_repr_id =
@@ -1396,7 +1396,7 @@ auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
 
 auto Initialize(Context& context, SemIR::LocId loc_id, SemIR::InstId target_id,
                 SemIR::InstId value_id) -> SemIR::InstId {
-  PendingBlock target_block(context);
+  PendingBlock target_block(&context);
   return Convert(context, loc_id, value_id,
                  {.kind = ConversionTarget::Initializer,
                   .type_id = context.insts().Get(target_id).type_id(),

+ 9 - 8
toolchain/check/pending_block.h

@@ -17,7 +17,8 @@ namespace Carbon::Check {
 // that haven't been inserted yet.
 class PendingBlock {
  public:
-  explicit PendingBlock(Context& context) : context_(context) {}
+  // `context` must not be null.
+  explicit PendingBlock(Context* context) : context_(context) {}
 
   PendingBlock(const PendingBlock&) = delete;
   auto operator=(const PendingBlock&) -> PendingBlock& = delete;
@@ -44,14 +45,14 @@ class PendingBlock {
 
   template <typename InstT, typename LocT>
   auto AddInst(LocT loc_id, InstT inst) -> SemIR::InstId {
-    auto inst_id = AddInstInNoBlock(context_, loc_id, inst);
+    auto inst_id = AddInstInNoBlock(*context_, loc_id, inst);
     insts_.push_back(inst_id);
     return inst_id;
   }
 
   template <typename InstT, typename LocT>
   auto AddInstWithCleanup(LocT loc_id, InstT inst) -> SemIR::InstId {
-    auto inst_id = AddInstWithCleanupInNoBlock(context_, loc_id, inst);
+    auto inst_id = AddInstWithCleanupInNoBlock(*context_, loc_id, inst);
     insts_.push_back(inst_id);
     return inst_id;
   }
@@ -59,7 +60,7 @@ class PendingBlock {
   // Insert the pending block of code at the current position.
   auto InsertHere() -> void {
     for (auto id : insts_) {
-      context_.inst_block_stack().AddInstId(id);
+      context_->inst_block_stack().AddInstId(id);
     }
     insts_.clear();
   }
@@ -67,7 +68,7 @@ class PendingBlock {
   // Replace the instruction at target_id with the instructions in this block.
   // The new value for target_id should be value_id.
   auto MergeReplacing(SemIR::InstId target_id, SemIR::InstId value_id) -> void {
-    SemIR::LocIdAndInst value = context_.insts().GetWithLocId(value_id);
+    SemIR::LocIdAndInst value = context_->insts().GetWithLocId(value_id);
 
     if (insts_.size() == 1 && insts_[0] == value_id) {
       // The block is {value_id}. Replace `target_id` with the instruction
@@ -77,18 +78,18 @@ class PendingBlock {
       // includes empty blocks, which `Add` handles.
       value.inst =
           SemIR::SpliceBlock{.type_id = value.inst.type_id(),
-                             .block_id = context_.inst_blocks().Add(insts_),
+                             .block_id = context_->inst_blocks().Add(insts_),
                              .result_id = value_id};
     }
 
-    ReplaceLocIdAndInstBeforeConstantUse(context_, target_id, value);
+    ReplaceLocIdAndInstBeforeConstantUse(*context_, target_id, value);
 
     // Prepare to stash more pending instructions.
     insts_.clear();
   }
 
  private:
-  Context& context_;
+  Context* context_;
   llvm::SmallVector<SemIR::InstId> insts_;
 };