action_stack.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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::PrintScopes(llvm::raw_ostream& out) const {
  16. llvm::ListSeparator sep(" :: ");
  17. for (const std::unique_ptr<Action>& action : todo_) {
  18. if (action->scope().has_value()) {
  19. out << sep << *action->scope();
  20. }
  21. }
  22. if (globals_.has_value()) {
  23. out << sep << *globals_;
  24. }
  25. // TODO: should we print constants as well?
  26. }
  27. void ActionStack::Start(std::unique_ptr<Action> action) {
  28. result_ = std::nullopt;
  29. CHECK(todo_.IsEmpty());
  30. todo_.Push(std::move(action));
  31. }
  32. void ActionStack::Initialize(ValueNodeView value_node,
  33. Nonnull<const Value*> value) {
  34. for (const std::unique_ptr<Action>& action : todo_) {
  35. if (action->scope().has_value()) {
  36. action->scope()->Initialize(value_node, value);
  37. return;
  38. }
  39. }
  40. globals_->Initialize(value_node, value);
  41. }
  42. auto ActionStack::ValueOfNode(ValueNodeView value_node,
  43. SourceLocation source_loc) const
  44. -> Nonnull<const Value*> {
  45. if (std::optional<Nonnull<const Value*>> constant_value =
  46. value_node.constant_value();
  47. constant_value.has_value()) {
  48. return *constant_value;
  49. }
  50. for (const std::unique_ptr<Action>& action : todo_) {
  51. // TODO: have static name resolution identify the scope of value_node
  52. // as an AstNode, and then perform lookup _only_ on the Action associated
  53. // with that node. This will help keep unwanted dynamic-scoping behavior
  54. // from sneaking in.
  55. if (action->scope().has_value()) {
  56. std::optional<Nonnull<const Value*>> result =
  57. action->scope()->Get(value_node);
  58. if (result.has_value()) {
  59. return *result;
  60. }
  61. }
  62. }
  63. if (globals_.has_value()) {
  64. std::optional<Nonnull<const Value*>> result = globals_->Get(value_node);
  65. if (result.has_value()) {
  66. return *result;
  67. }
  68. }
  69. // TODO: Move these errors to name resolution and explain them more clearly.
  70. FATAL_RUNTIME_ERROR(source_loc)
  71. << "could not find `" << value_node.base() << "`";
  72. }
  73. void ActionStack::MergeScope(RuntimeScope scope) {
  74. for (const std::unique_ptr<Action>& action : todo_) {
  75. if (action->scope().has_value()) {
  76. action->scope()->Merge(std::move(scope));
  77. return;
  78. }
  79. }
  80. if (globals_.has_value()) {
  81. globals_->Merge(std::move(scope));
  82. return;
  83. }
  84. FATAL() << "No current scope";
  85. }
  86. void ActionStack::InitializeFragment(ContinuationValue::StackFragment& fragment,
  87. Nonnull<const Statement*> body) {
  88. std::vector<Nonnull<const RuntimeScope*>> scopes;
  89. for (const std::unique_ptr<Action>& action : todo_) {
  90. if (action->scope().has_value()) {
  91. scopes.push_back(&*action->scope());
  92. }
  93. }
  94. // We don't capture globals_ or constants_ because they're global.
  95. std::vector<std::unique_ptr<Action>> reversed_todo;
  96. reversed_todo.push_back(std::make_unique<StatementAction>(body));
  97. reversed_todo.push_back(
  98. std::make_unique<ScopeAction>(RuntimeScope::Capture(scopes)));
  99. fragment.StoreReversed(std::move(reversed_todo));
  100. }
  101. void ActionStack::FinishAction() {
  102. std::unique_ptr<Action> act = todo_.Pop();
  103. switch (act->kind()) {
  104. case Action::Kind::ExpressionAction:
  105. case Action::Kind::LValAction:
  106. case Action::Kind::PatternAction:
  107. FATAL() << "This kind of action must produce a result: " << *act;
  108. case Action::Kind::ScopeAction:
  109. FATAL() << "ScopeAction at top of stack";
  110. case Action::Kind::StatementAction:
  111. case Action::Kind::DeclarationAction:
  112. PopScopes();
  113. }
  114. }
  115. void ActionStack::FinishAction(Nonnull<const Value*> result) {
  116. std::unique_ptr<Action> act = todo_.Pop();
  117. switch (act->kind()) {
  118. case Action::Kind::StatementAction:
  119. case Action::Kind::DeclarationAction:
  120. FATAL() << "This kind of Action cannot produce results: " << *act;
  121. case Action::Kind::ScopeAction:
  122. FATAL() << "ScopeAction at top of stack";
  123. case Action::Kind::ExpressionAction:
  124. case Action::Kind::LValAction:
  125. case Action::Kind::PatternAction:
  126. PopScopes();
  127. SetResult(result);
  128. }
  129. }
  130. void ActionStack::Spawn(std::unique_ptr<Action> child) {
  131. Action& action = *todo_.Top();
  132. action.set_pos(action.pos() + 1);
  133. todo_.Push(std::move(child));
  134. }
  135. void ActionStack::Spawn(std::unique_ptr<Action> child, RuntimeScope scope) {
  136. Action& action = *todo_.Top();
  137. action.set_pos(action.pos() + 1);
  138. todo_.Push(std::make_unique<ScopeAction>(std::move(scope)));
  139. todo_.Push(std::move(child));
  140. }
  141. void ActionStack::RunAgain() {
  142. Action& action = *todo_.Top();
  143. action.set_pos(action.pos() + 1);
  144. }
  145. void ActionStack::UnwindTo(Nonnull<const Statement*> ast_node) {
  146. while (true) {
  147. if (const auto* statement_action =
  148. llvm::dyn_cast<StatementAction>(todo_.Top().get());
  149. statement_action != nullptr &&
  150. &statement_action->statement() == ast_node) {
  151. break;
  152. }
  153. todo_.Pop();
  154. }
  155. }
  156. void ActionStack::UnwindPast(Nonnull<const Statement*> ast_node) {
  157. UnwindTo(ast_node);
  158. todo_.Pop();
  159. PopScopes();
  160. }
  161. void ActionStack::UnwindPast(Nonnull<const Statement*> ast_node,
  162. Nonnull<const Value*> result) {
  163. UnwindPast(ast_node);
  164. SetResult(result);
  165. }
  166. void ActionStack::Resume(Nonnull<const ContinuationValue*> continuation) {
  167. Action& action = *todo_.Top();
  168. action.set_pos(action.pos() + 1);
  169. continuation->stack().RestoreTo(todo_);
  170. }
  171. static auto IsRunAction(const Action& action) -> bool {
  172. const auto* statement = llvm::dyn_cast<StatementAction>(&action);
  173. return statement != nullptr && llvm::isa<Run>(statement->statement());
  174. }
  175. void ActionStack::Suspend() {
  176. // Pause the current continuation
  177. todo_.Pop();
  178. std::vector<std::unique_ptr<Action>> paused;
  179. while (!IsRunAction(*todo_.Top())) {
  180. paused.push_back(todo_.Pop());
  181. }
  182. const auto& continuation =
  183. llvm::cast<const ContinuationValue>(*todo_.Top()->results()[0]);
  184. // Update the continuation with the paused stack.
  185. continuation.stack().StoreReversed(std::move(paused));
  186. }
  187. void ActionStack::PopScopes() {
  188. while (!todo_.IsEmpty() && llvm::isa<ScopeAction>(*todo_.Top())) {
  189. todo_.Pop();
  190. }
  191. }
  192. void ActionStack::SetResult(Nonnull<const Value*> result) {
  193. if (todo_.IsEmpty()) {
  194. result_ = result;
  195. } else {
  196. todo_.Top()->AddResult(result);
  197. }
  198. }
  199. } // namespace Carbon