deferred_definition_worklist.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/deferred_definition_worklist.h"
  5. #include <algorithm>
  6. #include <optional>
  7. #include "common/variant_helpers.h"
  8. #include "common/vlog.h"
  9. #include "toolchain/check/handle.h"
  10. namespace Carbon::Check {
  11. static constexpr llvm::StringLiteral VlogPrefix = "DeferredDefinitionWorklist ";
  12. DeferredDefinitionWorklist::DeferredDefinitionWorklist(
  13. llvm::raw_ostream* vlog_stream)
  14. : vlog_stream_(vlog_stream) {
  15. // See declaration of `worklist_`.
  16. worklist_.reserve(64);
  17. }
  18. auto DeferredDefinitionWorklist::SuspendFunctionAndPush(
  19. Context& context, Parse::DeferredDefinitionIndex index,
  20. Parse::FunctionDefinitionStartId node_id) -> void {
  21. // TODO: Investigate factoring out `HandleFunctionDefinitionSuspend` to make
  22. // `DeferredDefinitionWorklist` reusable.
  23. worklist_.push_back(CheckSkippedDefinition{
  24. index, HandleFunctionDefinitionSuspend(context, node_id)});
  25. CARBON_VLOG("{0}Push CheckSkippedDefinition {1}\n", VlogPrefix, index.index);
  26. }
  27. auto DeferredDefinitionWorklist::PushEnterDeferredDefinitionScope(
  28. Context& context) -> void {
  29. bool nested = !entered_scopes_.empty() &&
  30. entered_scopes_.back().scope_index ==
  31. context.decl_name_stack().PeekInitialScopeIndex();
  32. entered_scopes_.push_back({.worklist_start_index = worklist_.size(),
  33. .scope_index = context.scope_stack().PeekIndex()});
  34. worklist_.push_back(EnterDeferredDefinitionScope{
  35. .suspended_name = std::nullopt, .in_deferred_definition_scope = nested});
  36. CARBON_VLOG("{0}Push EnterDeferredDefinitionScope {1}\n", VlogPrefix,
  37. nested ? "(nested)" : "(non-nested)");
  38. }
  39. auto DeferredDefinitionWorklist::SuspendFinishedScopeAndPush(Context& context)
  40. -> bool {
  41. auto start_index = entered_scopes_.pop_back_val().worklist_start_index;
  42. // If we've not found any deferred definitions in this scope, clean up the
  43. // stack.
  44. if (start_index == worklist_.size() - 1) {
  45. context.decl_name_stack().PopScope();
  46. worklist_.pop_back();
  47. CARBON_VLOG("{0}Pop EnterDeferredDefinitionScope (empty)\n", VlogPrefix);
  48. return false;
  49. }
  50. // If we're finishing a nested deferred definition scope, keep track of that
  51. // but don't type-check deferred definitions now.
  52. auto& enter_scope = get<EnterDeferredDefinitionScope>(worklist_[start_index]);
  53. if (enter_scope.in_deferred_definition_scope) {
  54. // This is a nested deferred definition scope. Suspend the inner scope so we
  55. // can restore it when we come to type-check the deferred definitions.
  56. enter_scope.suspended_name = context.decl_name_stack().Suspend();
  57. // Enqueue a task to leave the nested scope.
  58. worklist_.push_back(
  59. LeaveDeferredDefinitionScope{.in_deferred_definition_scope = true});
  60. CARBON_VLOG("{0}Push LeaveDeferredDefinitionScope (nested)\n", VlogPrefix);
  61. return false;
  62. }
  63. // We're at the end of a non-nested deferred definition scope. Prepare to
  64. // start checking deferred definitions. Enqueue a task to leave this outer
  65. // scope and end checking deferred definitions.
  66. worklist_.push_back(
  67. LeaveDeferredDefinitionScope{.in_deferred_definition_scope = false});
  68. CARBON_VLOG("{0}Push LeaveDeferredDefinitionScope (non-nested)\n",
  69. VlogPrefix);
  70. // We'll process the worklist in reverse index order, so reverse the part of
  71. // it we're about to execute so we run our tasks in the order in which they
  72. // were pushed.
  73. std::reverse(worklist_.begin() + start_index, worklist_.end());
  74. // Pop the `EnterDeferredDefinitionScope` that's now on the end of the
  75. // worklist. We stay in that scope rather than suspending then immediately
  76. // resuming it.
  77. CARBON_CHECK(
  78. holds_alternative<EnterDeferredDefinitionScope>(worklist_.back()),
  79. "Unexpected task in worklist.");
  80. worklist_.pop_back();
  81. CARBON_VLOG("{0}Handle EnterDeferredDefinitionScope (non-nested)\n",
  82. VlogPrefix);
  83. return true;
  84. }
  85. auto DeferredDefinitionWorklist::Pop() -> Task {
  86. if (vlog_stream_) {
  87. VariantMatch(
  88. worklist_.back(),
  89. [&](CheckSkippedDefinition& definition) {
  90. CARBON_VLOG("{0}Handle CheckSkippedDefinition {1}\n", VlogPrefix,
  91. definition.definition_index.index);
  92. },
  93. [&](EnterDeferredDefinitionScope& enter) {
  94. CARBON_CHECK(enter.in_deferred_definition_scope);
  95. CARBON_VLOG("{0}Handle EnterDeferredDefinitionScope (nested)\n",
  96. VlogPrefix);
  97. },
  98. [&](LeaveDeferredDefinitionScope& leave) {
  99. bool nested = leave.in_deferred_definition_scope;
  100. CARBON_VLOG("{0}Handle LeaveDeferredDefinitionScope {1}\n",
  101. VlogPrefix, nested ? "(nested)" : "(non-nested)");
  102. });
  103. }
  104. return worklist_.pop_back_val();
  105. }
  106. } // namespace Carbon::Check