action_stack.cpp 7.2 KB

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