action_stack.cpp 7.4 KB

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