Browse Source

Change `NodeIdTraversal.context_` from reference to pointer (#5210)

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 1 năm trước cách đây
mục cha
commit
3acca8402f

+ 1 - 1
toolchain/check/check_unit.cpp

@@ -351,7 +351,7 @@ auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
 // for example if an unrecoverable state is encountered.
 // for example if an unrecoverable state is encountered.
 // NOLINTNEXTLINE(readability-function-size)
 // NOLINTNEXTLINE(readability-function-size)
 auto CheckUnit::ProcessNodeIds() -> bool {
 auto CheckUnit::ProcessNodeIds() -> bool {
-  NodeIdTraversal traversal(context_, vlog_stream_);
+  NodeIdTraversal traversal(&context_, vlog_stream_);
 
 
   Parse::NodeId node_id = Parse::NodeId::None;
   Parse::NodeId node_id = Parse::NodeId::None;
 
 

+ 11 - 11
toolchain/check/node_id_traversal.cpp

@@ -8,12 +8,12 @@
 
 
 namespace Carbon::Check {
 namespace Carbon::Check {
 
 
-NodeIdTraversal::NodeIdTraversal(Context& context,
+NodeIdTraversal::NodeIdTraversal(Context* context,
                                  llvm::raw_ostream* vlog_stream)
                                  llvm::raw_ostream* vlog_stream)
     : context_(context),
     : context_(context),
-      next_deferred_definition_(&context.parse_tree()),
+      next_deferred_definition_(&context->parse_tree()),
       worklist_(vlog_stream) {
       worklist_(vlog_stream) {
-  auto range = context.parse_tree().postorder();
+  auto range = context->parse_tree().postorder();
   chunks_.push_back({.it = range.begin(),
   chunks_.push_back({.it = range.begin(),
                      .end = range.end(),
                      .end = range.end(),
                      .next_definition = Parse::DeferredDefinitionIndex::None});
                      .next_definition = Parse::DeferredDefinitionIndex::None});
@@ -53,9 +53,9 @@ auto NodeIdTraversal::Next() -> std::optional<Parse::NodeId> {
     // it, and track that we need to check it later.
     // it, and track that we need to check it later.
     if (node_id == next_deferred_definition_.start_id()) {
     if (node_id == next_deferred_definition_.start_id()) {
       const auto& definition_info =
       const auto& definition_info =
-          context_.parse_tree().deferred_definitions().Get(
+          context_->parse_tree().deferred_definitions().Get(
               next_deferred_definition_.index());
               next_deferred_definition_.index());
-      worklist_.SuspendFunctionAndPush(context_,
+      worklist_.SuspendFunctionAndPush(*context_,
                                        next_deferred_definition_.index(),
                                        next_deferred_definition_.index(),
                                        definition_info.start_id);
                                        definition_info.start_id);
 
 
@@ -107,7 +107,7 @@ auto NodeIdTraversal::Handle(Parse::NodeKind parse_kind) -> void {
   // When we reach the start of a deferred definition scope, add a task to the
   // When we reach the start of a deferred definition scope, add a task to the
   // worklist to check future skipped definitions in the new context.
   // worklist to check future skipped definitions in the new context.
   if (IsStartOfDeferredDefinitionScope(parse_kind)) {
   if (IsStartOfDeferredDefinitionScope(parse_kind)) {
-    worklist_.PushEnterDeferredDefinitionScope(context_);
+    worklist_.PushEnterDeferredDefinitionScope(*context_);
   }
   }
 
 
   // When we reach the end of a deferred definition scope, add a task to the
   // When we reach the end of a deferred definition scope, add a task to the
@@ -115,7 +115,7 @@ auto NodeIdTraversal::Handle(Parse::NodeKind parse_kind) -> void {
   // checking the deferred definitions now.
   // checking the deferred definitions now.
   if (IsEndOfDeferredDefinitionScope(parse_kind)) {
   if (IsEndOfDeferredDefinitionScope(parse_kind)) {
     chunks_.back().checking_deferred_definitions =
     chunks_.back().checking_deferred_definitions =
-        worklist_.SuspendFinishedScopeAndPush(context_);
+        worklist_.SuspendFinishedScopeAndPush(*context_);
   }
   }
 }
 }
 
 
@@ -123,7 +123,7 @@ auto NodeIdTraversal::PerformTask(
     DeferredDefinitionWorklist::EnterDeferredDefinitionScope&& enter) -> void {
     DeferredDefinitionWorklist::EnterDeferredDefinitionScope&& enter) -> void {
   CARBON_CHECK(enter.suspended_name,
   CARBON_CHECK(enter.suspended_name,
                "Entering a scope with no suspension information.");
                "Entering a scope with no suspension information.");
-  context_.decl_name_stack().Restore(std::move(*enter.suspended_name));
+  context_->decl_name_stack().Restore(std::move(*enter.suspended_name));
 }
 }
 
 
 auto NodeIdTraversal::PerformTask(
 auto NodeIdTraversal::PerformTask(
@@ -132,7 +132,7 @@ auto NodeIdTraversal::PerformTask(
     // We're done with checking deferred definitions.
     // We're done with checking deferred definitions.
     chunks_.back().checking_deferred_definitions = false;
     chunks_.back().checking_deferred_definitions = false;
   }
   }
-  context_.decl_name_stack().PopScope();
+  context_->decl_name_stack().PopScope();
 }
 }
 
 
 auto NodeIdTraversal::PerformTask(
 auto NodeIdTraversal::PerformTask(
@@ -140,8 +140,8 @@ auto NodeIdTraversal::PerformTask(
     -> void {
     -> void {
   auto& [definition_index, suspended_fn] = parse_definition;
   auto& [definition_index, suspended_fn] = parse_definition;
   const auto& definition_info =
   const auto& definition_info =
-      context_.parse_tree().deferred_definitions().Get(definition_index);
-  HandleFunctionDefinitionResume(context_, definition_info.start_id,
+      context_->parse_tree().deferred_definitions().Get(definition_index);
+  HandleFunctionDefinitionResume(*context_, definition_info.start_id,
                                  std::move(suspended_fn));
                                  std::move(suspended_fn));
   auto range = Parse::Tree::PostorderIterator::MakeRange(
   auto range = Parse::Tree::PostorderIterator::MakeRange(
       definition_info.start_id, definition_info.definition_id);
       definition_info.start_id, definition_info.definition_id);

+ 3 - 2
toolchain/check/node_id_traversal.h

@@ -17,7 +17,8 @@ namespace Carbon::Check {
 // to check them.
 // to check them.
 class NodeIdTraversal {
 class NodeIdTraversal {
  public:
  public:
-  explicit NodeIdTraversal(Context& context, llvm::raw_ostream* vlog_stream);
+  // `context` must not be null.
+  explicit NodeIdTraversal(Context* context, llvm::raw_ostream* vlog_stream);
 
 
   // Finds the next `NodeId` to type-check. Returns nullopt if the traversal is
   // Finds the next `NodeId` to type-check. Returns nullopt if the traversal is
   // complete.
   // complete.
@@ -76,7 +77,7 @@ class NodeIdTraversal {
       DeferredDefinitionWorklist::CheckSkippedDefinition&& parse_definition)
       DeferredDefinitionWorklist::CheckSkippedDefinition&& parse_definition)
       -> void;
       -> void;
 
 
-  Context& context_;
+  Context* context_;
   NextDeferredDefinitionCache next_deferred_definition_;
   NextDeferredDefinitionCache next_deferred_definition_;
   DeferredDefinitionWorklist worklist_;
   DeferredDefinitionWorklist worklist_;
   llvm::SmallVector<Chunk> chunks_;
   llvm::SmallVector<Chunk> chunks_;