deferred_definition_worklist.cpp 4.9 KB

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