action_stack.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "executable_semantics/interpreter/action_stack.h"
  5. #include "executable_semantics/interpreter/action.h"
  6. #include "llvm/ADT/StringExtras.h"
  7. #include "llvm/Support/Casting.h"
  8. namespace Carbon {
  9. void ActionStack::Print(llvm::raw_ostream& out) const {
  10. llvm::ListSeparator sep(" :: ");
  11. for (const std::unique_ptr<Action>& action : todo_) {
  12. out << sep << *action;
  13. }
  14. }
  15. void ActionStack::Start(std::unique_ptr<Action> action, Scope scope) {
  16. result_ = std::nullopt;
  17. todo_ = {};
  18. todo_.Push(std::make_unique<ScopeAction>(std::move(scope)));
  19. todo_.Push(std::move(action));
  20. }
  21. auto ActionStack::CurrentScope() const -> Scope& {
  22. for (const std::unique_ptr<Action>& action : todo_) {
  23. if (action->scope().has_value()) {
  24. return *action->scope();
  25. }
  26. }
  27. FATAL() << "No current scope";
  28. }
  29. void ActionStack::FinishAction() {
  30. std::unique_ptr<Action> act = todo_.Pop();
  31. switch (act->kind()) {
  32. case Action::Kind::ExpressionAction:
  33. case Action::Kind::LValAction:
  34. case Action::Kind::PatternAction:
  35. FATAL() << "This kind of action must produce a result.";
  36. case Action::Kind::ScopeAction:
  37. FATAL() << "ScopeAction at top of stack";
  38. case Action::Kind::StatementAction:
  39. PopScopes();
  40. CHECK(!IsEmpty());
  41. }
  42. }
  43. void ActionStack::FinishAction(Nonnull<const Value*> result) {
  44. std::unique_ptr<Action> act = todo_.Pop();
  45. switch (act->kind()) {
  46. case Action::Kind::StatementAction:
  47. FATAL() << "Statements cannot produce results.";
  48. case Action::Kind::ScopeAction:
  49. FATAL() << "ScopeAction at top of stack";
  50. case Action::Kind::ExpressionAction:
  51. case Action::Kind::LValAction:
  52. case Action::Kind::PatternAction:
  53. PopScopes();
  54. SetResult(result);
  55. }
  56. }
  57. void ActionStack::Spawn(std::unique_ptr<Action> child) {
  58. Action& action = *todo_.Top();
  59. action.set_pos(action.pos() + 1);
  60. todo_.Push(std::move(child));
  61. }
  62. void ActionStack::Spawn(std::unique_ptr<Action> child, Scope scope) {
  63. Action& action = *todo_.Top();
  64. action.set_pos(action.pos() + 1);
  65. todo_.Push(std::make_unique<ScopeAction>(std::move(scope)));
  66. todo_.Push(std::move(child));
  67. }
  68. void ActionStack::RunAgain() {
  69. Action& action = *todo_.Top();
  70. action.set_pos(action.pos() + 1);
  71. }
  72. void ActionStack::UnwindTo(Nonnull<const Statement*> ast_node) {
  73. while (true) {
  74. if (const auto* statement_action =
  75. llvm::dyn_cast<StatementAction>(todo_.Top().get());
  76. statement_action != nullptr &&
  77. &statement_action->statement() == ast_node) {
  78. break;
  79. }
  80. todo_.Pop();
  81. }
  82. }
  83. void ActionStack::UnwindPast(Nonnull<const Statement*> ast_node) {
  84. UnwindTo(ast_node);
  85. todo_.Pop();
  86. PopScopes();
  87. }
  88. void ActionStack::UnwindPast(Nonnull<const Statement*> ast_node,
  89. Nonnull<const Value*> result) {
  90. UnwindPast(ast_node);
  91. SetResult(result);
  92. }
  93. void ActionStack::Resume(Nonnull<const ContinuationValue*> continuation) {
  94. Action& action = *todo_.Top();
  95. action.set_pos(action.pos() + 1);
  96. continuation->stack().RestoreTo(todo_);
  97. }
  98. static auto IsRunAction(const Action& action) -> bool {
  99. const auto* statement = llvm::dyn_cast<StatementAction>(&action);
  100. return statement != nullptr && llvm::isa<Run>(statement->statement());
  101. }
  102. void ActionStack::Suspend() {
  103. // Pause the current continuation
  104. todo_.Pop();
  105. std::vector<std::unique_ptr<Action>> paused;
  106. while (!IsRunAction(*todo_.Top())) {
  107. paused.push_back(todo_.Pop());
  108. }
  109. const auto& continuation =
  110. llvm::cast<const ContinuationValue>(*todo_.Top()->results()[0]);
  111. // Update the continuation with the paused stack.
  112. continuation.stack().StoreReversed(std::move(paused));
  113. }
  114. void ActionStack::PopScopes() {
  115. while (!todo_.IsEmpty() && llvm::isa<ScopeAction>(*todo_.Top())) {
  116. todo_.Pop();
  117. }
  118. }
  119. void ActionStack::SetResult(Nonnull<const Value*> result) {
  120. if (todo_.IsEmpty()) {
  121. result_ = result;
  122. } else {
  123. todo_.Top()->AddResult(result);
  124. }
  125. }
  126. } // namespace Carbon