action_stack.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "common/error.h"
  6. #include "explorer/interpreter/action.h"
  7. #include "llvm/ADT/StringExtras.h"
  8. #include "llvm/Support/Casting.h"
  9. #include "llvm/Support/Error.h"
  10. namespace Carbon {
  11. void ActionStack::Print(llvm::raw_ostream& out) const {
  12. llvm::ListSeparator sep(" ## ");
  13. for (const std::unique_ptr<Action>& action : todo_) {
  14. out << sep << *action;
  15. }
  16. }
  17. // OBSOLETE
  18. void ActionStack::PrintScopes(llvm::raw_ostream& out) const {
  19. llvm::ListSeparator sep(" ## ");
  20. for (const std::unique_ptr<Action>& action : todo_) {
  21. if (action->scope().has_value()) {
  22. out << sep << *action->scope();
  23. }
  24. }
  25. if (globals_.has_value()) {
  26. out << sep << *globals_;
  27. }
  28. // TODO: should we print constants as well?
  29. }
  30. void ActionStack::Start(std::unique_ptr<Action> action) {
  31. result_ = std::nullopt;
  32. CARBON_CHECK(todo_.IsEmpty());
  33. todo_.Push(std::move(action));
  34. }
  35. void ActionStack::Initialize(ValueNodeView value_node,
  36. Nonnull<const Value*> value) {
  37. for (const std::unique_ptr<Action>& action : todo_) {
  38. if (action->scope().has_value()) {
  39. action->scope()->Initialize(value_node, value);
  40. return;
  41. }
  42. }
  43. globals_->Initialize(value_node, value);
  44. }
  45. auto ActionStack::ValueOfNode(ValueNodeView value_node,
  46. SourceLocation source_loc) const
  47. -> ErrorOr<Nonnull<const Value*>> {
  48. std::optional<const Value*> constant_value = value_node.constant_value();
  49. if (constant_value.has_value()) {
  50. return *constant_value;
  51. }
  52. for (const std::unique_ptr<Action>& action : todo_) {
  53. // TODO: have static name resolution identify the scope of value_node
  54. // as an AstNode, and then perform lookup _only_ on the Action associated
  55. // with that node. This will help keep unwanted dynamic-scoping behavior
  56. // from sneaking in.
  57. if (action->scope().has_value()) {
  58. std::optional<Nonnull<const Value*>> result =
  59. action->scope()->Get(value_node);
  60. if (result.has_value()) {
  61. return *result;
  62. }
  63. }
  64. }
  65. if (globals_.has_value()) {
  66. std::optional<Nonnull<const Value*>> result = globals_->Get(value_node);
  67. if (result.has_value()) {
  68. return *result;
  69. }
  70. }
  71. // We don't know the value of this node, but at compile time we may still be
  72. // able to form a symbolic value for it. For example, in
  73. //
  74. // fn F[T:! Type](x: T) {}
  75. //
  76. // ... we don't know the value of `T` but can still symbolically evaluate it
  77. // to a `VariableType`. At runtime we need actual values.
  78. if (phase_ == Phase::CompileTime) {
  79. std::optional<const Value*> symbolic_identity =
  80. value_node.symbolic_identity();
  81. if (symbolic_identity.has_value()) {
  82. return *symbolic_identity;
  83. }
  84. }
  85. // TODO: Move these errors to compile time and explain them more clearly.
  86. return RuntimeError(source_loc)
  87. << "could not find `" << value_node.base() << "`";
  88. }
  89. void ActionStack::MergeScope(RuntimeScope scope) {
  90. for (const std::unique_ptr<Action>& action : todo_) {
  91. if (action->scope().has_value()) {
  92. action->scope()->Merge(std::move(scope));
  93. return;
  94. }
  95. }
  96. if (globals_.has_value()) {
  97. globals_->Merge(std::move(scope));
  98. return;
  99. }
  100. CARBON_FATAL() << "No current scope";
  101. }
  102. void ActionStack::InitializeFragment(ContinuationValue::StackFragment& fragment,
  103. Nonnull<const Statement*> body) {
  104. std::vector<Nonnull<const RuntimeScope*>> scopes;
  105. for (const std::unique_ptr<Action>& action : todo_) {
  106. if (action->scope().has_value()) {
  107. scopes.push_back(&*action->scope());
  108. }
  109. }
  110. // We don't capture globals_ or constants_ because they're global.
  111. std::vector<std::unique_ptr<Action>> reversed_todo;
  112. reversed_todo.push_back(std::make_unique<StatementAction>(body));
  113. reversed_todo.push_back(
  114. std::make_unique<ScopeAction>(RuntimeScope::Capture(scopes)));
  115. fragment.StoreReversed(std::move(reversed_todo));
  116. }
  117. auto ActionStack::FinishAction() -> ErrorOr<Success> {
  118. std::stack<std::unique_ptr<Action>> scopes_to_destroy;
  119. std::unique_ptr<Action> act = todo_.Pop();
  120. switch (act->kind()) {
  121. case Action::Kind::CleanUpAction:
  122. case Action::Kind::ExpressionAction:
  123. case Action::Kind::LValAction:
  124. case Action::Kind::PatternAction:
  125. CARBON_FATAL() << "This kind of action must produce a result: " << *act;
  126. case Action::Kind::ScopeAction:
  127. CARBON_FATAL() << "ScopeAction at top of stack";
  128. case Action::Kind::StatementAction:
  129. case Action::Kind::DeclarationAction:
  130. case Action::Kind::RecursiveAction: {
  131. PopScopes(scopes_to_destroy);
  132. break;
  133. }
  134. }
  135. PushCleanUpAction(std::move(act));
  136. PushCleanUpActions(std::move(scopes_to_destroy));
  137. return Success();
  138. }
  139. auto ActionStack::FinishAction(Nonnull<const Value*> result)
  140. -> ErrorOr<Success> {
  141. std::stack<std::unique_ptr<Action>> scopes_to_destroy;
  142. std::unique_ptr<Action> act = todo_.Pop();
  143. switch (act->kind()) {
  144. case Action::Kind::CleanUpAction:
  145. case Action::Kind::StatementAction:
  146. case Action::Kind::DeclarationAction:
  147. case Action::Kind::RecursiveAction:
  148. CARBON_FATAL() << "This kind of Action cannot produce results: " << *act;
  149. case Action::Kind::ScopeAction:
  150. CARBON_FATAL() << "ScopeAction at top of stack";
  151. case Action::Kind::ExpressionAction:
  152. case Action::Kind::LValAction:
  153. case Action::Kind::PatternAction:
  154. PopScopes(scopes_to_destroy);
  155. SetResult(result);
  156. break;
  157. }
  158. PushCleanUpAction(std::move(act));
  159. PushCleanUpActions(std::move(scopes_to_destroy));
  160. return Success();
  161. }
  162. auto ActionStack::Spawn(std::unique_ptr<Action> child) -> ErrorOr<Success> {
  163. Action& action = *todo_.Top();
  164. action.set_pos(action.pos() + 1);
  165. todo_.Push(std::move(child));
  166. return Success();
  167. }
  168. auto ActionStack::Spawn(std::unique_ptr<Action> child, RuntimeScope scope)
  169. -> ErrorOr<Success> {
  170. Action& action = *todo_.Top();
  171. action.set_pos(action.pos() + 1);
  172. todo_.Push(std::make_unique<ScopeAction>(std::move(scope)));
  173. todo_.Push(std::move(child));
  174. return Success();
  175. }
  176. auto ActionStack::ReplaceWith(std::unique_ptr<Action> replacement)
  177. -> ErrorOr<Success> {
  178. std::unique_ptr<Action> old = todo_.Pop();
  179. CARBON_CHECK(replacement->kind() == old->kind())
  180. << "ReplaceWith can't change action kind";
  181. todo_.Push(std::move(replacement));
  182. return Success();
  183. }
  184. auto ActionStack::RunAgain() -> ErrorOr<Success> {
  185. Action& action = *todo_.Top();
  186. action.set_pos(action.pos() + 1);
  187. return Success();
  188. }
  189. auto ActionStack::UnwindToWithCaptureScopesToDestroy(
  190. Nonnull<const Statement*> ast_node) -> std::stack<std::unique_ptr<Action>> {
  191. std::stack<std::unique_ptr<Action>> scopes_to_destroy;
  192. while (true) {
  193. if (const auto* statement_action =
  194. llvm::dyn_cast<StatementAction>(todo_.Top().get());
  195. statement_action != nullptr &&
  196. &statement_action->statement() == ast_node) {
  197. break;
  198. }
  199. auto item = todo_.Pop();
  200. auto& scope = item->scope();
  201. if (scope && item->kind() != Action::Kind::CleanUpAction) {
  202. std::unique_ptr<Action> cleanup_action =
  203. std::make_unique<CleanupAction>(std::move(*scope));
  204. scopes_to_destroy.push(std::move(cleanup_action));
  205. }
  206. }
  207. return scopes_to_destroy;
  208. }
  209. auto ActionStack::UnwindTo(Nonnull<const Statement*> ast_node)
  210. -> ErrorOr<Success> {
  211. std::stack<std::unique_ptr<Action>> scopes_to_destroy =
  212. UnwindToWithCaptureScopesToDestroy(ast_node);
  213. PushCleanUpActions(std::move(scopes_to_destroy));
  214. return Success();
  215. }
  216. auto ActionStack::UnwindPast(Nonnull<const Statement*> ast_node)
  217. -> ErrorOr<Success> {
  218. std::stack<std::unique_ptr<Action>> scopes_to_destroy =
  219. UnwindPastWithCaptureScopesToDestroy(ast_node);
  220. PushCleanUpActions(std::move(scopes_to_destroy));
  221. return Success();
  222. }
  223. auto ActionStack::UnwindPastWithCaptureScopesToDestroy(
  224. Nonnull<const Statement*> ast_node) -> std::stack<std::unique_ptr<Action>> {
  225. std::stack<std::unique_ptr<Action>> scopes_to_destroy =
  226. UnwindToWithCaptureScopesToDestroy(ast_node);
  227. auto item = todo_.Pop();
  228. scopes_to_destroy.push(std::move(item));
  229. PopScopes(scopes_to_destroy);
  230. return scopes_to_destroy;
  231. }
  232. auto ActionStack::UnwindPast(Nonnull<const Statement*> ast_node,
  233. Nonnull<const Value*> result) -> ErrorOr<Success> {
  234. std::stack<std::unique_ptr<Action>> scopes_to_destroy =
  235. UnwindPastWithCaptureScopesToDestroy(ast_node);
  236. SetResult(result);
  237. PushCleanUpActions(std::move(scopes_to_destroy));
  238. return Success();
  239. }
  240. auto ActionStack::Resume(Nonnull<const ContinuationValue*> continuation)
  241. -> ErrorOr<Success> {
  242. Action& action = *todo_.Top();
  243. action.set_pos(action.pos() + 1);
  244. continuation->stack().RestoreTo(todo_);
  245. return Success();
  246. }
  247. static auto IsRunAction(const Action& action) -> bool {
  248. const auto* statement = llvm::dyn_cast<StatementAction>(&action);
  249. return statement != nullptr && llvm::isa<Run>(statement->statement());
  250. }
  251. auto ActionStack::Suspend() -> ErrorOr<Success> {
  252. // Pause the current continuation
  253. todo_.Pop();
  254. std::vector<std::unique_ptr<Action>> paused;
  255. while (!IsRunAction(*todo_.Top())) {
  256. paused.push_back(todo_.Pop());
  257. }
  258. const auto& continuation =
  259. llvm::cast<const ContinuationValue>(*todo_.Top()->results()[0]);
  260. // Update the continuation with the paused stack.
  261. continuation.stack().StoreReversed(std::move(paused));
  262. return Success();
  263. }
  264. void ActionStack::PopScopes(
  265. std::stack<std::unique_ptr<Action>>& cleanup_stack) {
  266. while (!todo_.IsEmpty() && llvm::isa<ScopeAction>(*todo_.Top())) {
  267. auto act = todo_.Pop();
  268. if (act->scope()) {
  269. if ((*act->scope()).DestructionState() <
  270. RuntimeScope::State::CleanUpped) {
  271. (*act->scope()).TransitState();
  272. cleanup_stack.push(std::move(act));
  273. }
  274. }
  275. }
  276. }
  277. void ActionStack::SetResult(Nonnull<const Value*> result) {
  278. if (todo_.IsEmpty()) {
  279. result_ = result;
  280. } else {
  281. todo_.Top()->AddResult(result);
  282. }
  283. }
  284. void ActionStack::PushCleanUpActions(
  285. std::stack<std::unique_ptr<Action>> actions) {
  286. while (!actions.empty()) {
  287. auto& act = actions.top();
  288. if (act->scope()) {
  289. std::unique_ptr<Action> cleanup_action =
  290. std::make_unique<CleanupAction>(std::move(*act->scope()));
  291. todo_.Push(std::move(cleanup_action));
  292. }
  293. actions.pop();
  294. }
  295. }
  296. void ActionStack::PushCleanUpAction(std::unique_ptr<Action> act) {
  297. auto& scope = act->scope();
  298. if (scope && act->kind() != Action::Kind::CleanUpAction) {
  299. std::unique_ptr<Action> cleanup_action =
  300. std::make_unique<CleanupAction>(std::move(*scope));
  301. todo_.Push(std::move(cleanup_action));
  302. }
  303. }
  304. } // namespace Carbon