node_id_traversal.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/node_id_traversal.h"
  5. #include "toolchain/check/handle.h"
  6. namespace Carbon::Check {
  7. NodeIdTraversal::NodeIdTraversal(Context& context,
  8. llvm::raw_ostream* vlog_stream)
  9. : context_(context),
  10. next_deferred_definition_(&context.parse_tree()),
  11. worklist_(vlog_stream) {
  12. auto range = context.parse_tree().postorder();
  13. chunks_.push_back(
  14. {.it = range.begin(),
  15. .end = range.end(),
  16. .next_definition = Parse::DeferredDefinitionIndex::Invalid});
  17. }
  18. auto NodeIdTraversal::Next() -> std::optional<Parse::NodeId> {
  19. while (true) {
  20. // If we're checking deferred definitions, find the next definition we
  21. // should check, restore its suspended state, and add a corresponding
  22. // `Chunk` to the top of the chunk list.
  23. if (chunks_.back().checking_deferred_definitions) {
  24. std::visit(
  25. [&](auto&& task) { PerformTask(std::forward<decltype(task)>(task)); },
  26. worklist_.Pop());
  27. continue;
  28. }
  29. // If we're not checking deferred definitions, produce the next parse node
  30. // for this chunk. If we've run out of parse nodes, we're done with this
  31. // chunk of the parse tree.
  32. if (chunks_.back().it == chunks_.back().end) {
  33. auto old_chunk = chunks_.pop_back_val();
  34. // If we're out of chunks, then we're done entirely.
  35. if (chunks_.empty()) {
  36. worklist_.VerifyEmpty();
  37. return std::nullopt;
  38. }
  39. next_deferred_definition_.SkipTo(old_chunk.next_definition);
  40. continue;
  41. }
  42. auto node_id = *chunks_.back().it;
  43. // If we've reached the start of a deferred definition, skip to the end of
  44. // it, and track that we need to check it later.
  45. if (node_id == next_deferred_definition_.start_id()) {
  46. const auto& definition_info =
  47. context_.parse_tree().deferred_definitions().Get(
  48. next_deferred_definition_.index());
  49. worklist_.SuspendFunctionAndPush(context_,
  50. next_deferred_definition_.index(),
  51. definition_info.start_id);
  52. // Continue type-checking the parse tree after the end of the definition.
  53. chunks_.back().it =
  54. Parse::Tree::PostorderIterator(definition_info.definition_id) + 1;
  55. next_deferred_definition_.SkipTo(definition_info.next_definition_index);
  56. continue;
  57. }
  58. ++chunks_.back().it;
  59. return node_id;
  60. }
  61. }
  62. // Determines whether this node kind is the start of a deferred definition
  63. // scope.
  64. static auto IsStartOfDeferredDefinitionScope(Parse::NodeKind kind) -> bool {
  65. switch (kind) {
  66. case Parse::NodeKind::ClassDefinitionStart:
  67. case Parse::NodeKind::ImplDefinitionStart:
  68. case Parse::NodeKind::InterfaceDefinitionStart:
  69. case Parse::NodeKind::NamedConstraintDefinitionStart:
  70. // TODO: Mixins.
  71. return true;
  72. default:
  73. return false;
  74. }
  75. }
  76. // Determines whether this node kind is the end of a deferred definition scope.
  77. static auto IsEndOfDeferredDefinitionScope(Parse::NodeKind kind) -> bool {
  78. switch (kind) {
  79. case Parse::NodeKind::ClassDefinition:
  80. case Parse::NodeKind::ImplDefinition:
  81. case Parse::NodeKind::InterfaceDefinition:
  82. case Parse::NodeKind::NamedConstraintDefinition:
  83. // TODO: Mixins.
  84. return true;
  85. default:
  86. return false;
  87. }
  88. }
  89. // TODO: Investigate factoring out `IsStartOfDeferredDefinitionScope` and
  90. // `IsEndOfDeferredDefinitionScope` in order to make `NodeIdTraversal`
  91. // reusable.
  92. auto NodeIdTraversal::Handle(Parse::NodeKind parse_kind) -> void {
  93. // When we reach the start of a deferred definition scope, add a task to the
  94. // worklist to check future skipped definitions in the new context.
  95. if (IsStartOfDeferredDefinitionScope(parse_kind)) {
  96. worklist_.PushEnterDeferredDefinitionScope(context_);
  97. }
  98. // When we reach the end of a deferred definition scope, add a task to the
  99. // worklist to leave the scope. If this is not a nested scope, start
  100. // checking the deferred definitions now.
  101. if (IsEndOfDeferredDefinitionScope(parse_kind)) {
  102. chunks_.back().checking_deferred_definitions =
  103. worklist_.SuspendFinishedScopeAndPush(context_);
  104. }
  105. }
  106. auto NodeIdTraversal::PerformTask(
  107. DeferredDefinitionWorklist::EnterDeferredDefinitionScope&& enter) -> void {
  108. CARBON_CHECK(enter.suspended_name,
  109. "Entering a scope with no suspension information.");
  110. context_.decl_name_stack().Restore(std::move(*enter.suspended_name));
  111. }
  112. auto NodeIdTraversal::PerformTask(
  113. DeferredDefinitionWorklist::LeaveDeferredDefinitionScope&& leave) -> void {
  114. if (!leave.in_deferred_definition_scope) {
  115. // We're done with checking deferred definitions.
  116. chunks_.back().checking_deferred_definitions = false;
  117. }
  118. context_.decl_name_stack().PopScope();
  119. }
  120. auto NodeIdTraversal::PerformTask(
  121. DeferredDefinitionWorklist::CheckSkippedDefinition&& parse_definition)
  122. -> void {
  123. auto& [definition_index, suspended_fn] = parse_definition;
  124. const auto& definition_info =
  125. context_.parse_tree().deferred_definitions().Get(definition_index);
  126. HandleFunctionDefinitionResume(context_, definition_info.start_id,
  127. std::move(suspended_fn));
  128. auto range = Parse::Tree::PostorderIterator::MakeRange(
  129. definition_info.start_id, definition_info.definition_id);
  130. chunks_.push_back({.it = range.begin() + 1,
  131. .end = range.end(),
  132. .next_definition = next_deferred_definition_.index()});
  133. ++definition_index.index;
  134. next_deferred_definition_.SkipTo(definition_index);
  135. }
  136. NodeIdTraversal::NextDeferredDefinitionCache::NextDeferredDefinitionCache(
  137. const Parse::Tree* tree)
  138. : tree_(tree) {
  139. SkipTo(Parse::DeferredDefinitionIndex(0));
  140. }
  141. // Set the specified deferred definition index as being the next one that
  142. // will be encountered.
  143. auto NodeIdTraversal::NextDeferredDefinitionCache::SkipTo(
  144. Parse::DeferredDefinitionIndex next_index) -> void {
  145. index_ = next_index;
  146. if (static_cast<size_t>(index_.index) ==
  147. tree_->deferred_definitions().size()) {
  148. start_id_ = Parse::NodeId::Invalid;
  149. } else {
  150. start_id_ = tree_->deferred_definitions().Get(index_).start_id;
  151. }
  152. }
  153. } // namespace Carbon::Check