action_stack.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef CARBON_EXPLORER_INTERPRETER_ACTION_STACK_H_
  5. #define CARBON_EXPLORER_INTERPRETER_ACTION_STACK_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <stack>
  9. #include "common/ostream.h"
  10. #include "explorer/ast/statement.h"
  11. #include "explorer/interpreter/action.h"
  12. #include "explorer/interpreter/value.h"
  13. namespace Carbon {
  14. // Selects between compile-time and run-time behavior.
  15. enum class Phase { CompileTime, RunTime };
  16. // The stack of Actions currently being executed by the interpreter.
  17. class ActionStack {
  18. public:
  19. // Constructs an empty compile-time ActionStack.
  20. ActionStack() : phase_(Phase::CompileTime) {}
  21. // Constructs an empty run-time ActionStack that allocates global variables
  22. // on `heap`.
  23. explicit ActionStack(Nonnull<HeapAllocationInterface*> heap)
  24. : globals_(RuntimeScope(heap)), phase_(Phase::RunTime) {}
  25. void Print(llvm::raw_ostream& out) const;
  26. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  27. // TODO: consider unifying with Print.
  28. void PrintScopes(llvm::raw_ostream& out) const;
  29. // Starts execution with `action` at the top of the stack. Cannot be called
  30. // when IsEmpty() is false.
  31. void Start(std::unique_ptr<Action> action);
  32. // True if the stack is empty.
  33. auto IsEmpty() const -> bool { return todo_.IsEmpty(); }
  34. // The Action currently at the top of the stack. This will never be a
  35. // ScopeAction.
  36. auto CurrentAction() -> Action& { return *todo_.Top(); }
  37. // Allocates storage for `value_node`, and initializes it to `value`.
  38. void Initialize(ValueNodeView value_node, Nonnull<const Value*> value);
  39. // Returns the value bound to `value_node`. If `value_node` is a local
  40. // variable, this will be an LValue.
  41. auto ValueOfNode(ValueNodeView value_node, SourceLocation source_loc) const
  42. -> ErrorOr<Nonnull<const Value*>>;
  43. // Merges `scope` into the innermost scope currently on the stack.
  44. void MergeScope(RuntimeScope scope);
  45. // Initializes `fragment` so that, when resumed, it begins execution of
  46. // `body`.
  47. void InitializeFragment(ContinuationValue::StackFragment& fragment,
  48. Nonnull<const Statement*> body);
  49. // The result produced by the `action` argument of the most recent
  50. // Start call. Cannot be called if IsEmpty() is false, or if `action`
  51. // was an action that doesn't produce results.
  52. auto result() const -> Nonnull<const Value*> { return *result_; }
  53. // The following methods, called "transition methods", update the state of
  54. // the ActionStack and/or the current Action to reflect the effects of
  55. // executing a step of that Action. Execution of an Action step should always
  56. // invoke exactly one transition method, as the very last operation. This is a
  57. // matter of safety as well as convention: most transition methods modify the
  58. // state of the current action, and some of them destroy it. To help enforce
  59. // this requirement, we have a convention of making these methods return an
  60. // ErrorOr<Success> even when a method can't actually fail, and calling the
  61. // methods as part of return statements, e.g. `return todo_.FinishAction()`.
  62. // Finishes execution of the current Action. If `result` is specified, it
  63. // represents the result of that Action.
  64. auto FinishAction() -> ErrorOr<Success>;
  65. auto FinishAction(Nonnull<const Value*> result) -> ErrorOr<Success>;
  66. // Advances the current action one step, and push `child` onto the stack.
  67. // If `scope` is specified, `child` will be executed in that scope.
  68. auto Spawn(std::unique_ptr<Action> child) -> ErrorOr<Success>;
  69. auto Spawn(std::unique_ptr<Action> child, RuntimeScope scope)
  70. -> ErrorOr<Success>;
  71. // Replace the current action with another action that produces the same kind
  72. // of result and run it next.
  73. auto ReplaceWith(std::unique_ptr<Action> replacement) -> ErrorOr<Success>;
  74. // Start a new recursive action.
  75. auto BeginRecursiveAction() {
  76. todo_.Push(std::make_unique<RecursiveAction>());
  77. }
  78. // Advances the current action one step.
  79. auto RunAgain() -> ErrorOr<Success>;
  80. // Unwinds Actions from the stack until the StatementAction associated with
  81. // `ast_node` is at the top of the stack.
  82. auto UnwindTo(Nonnull<const Statement*> ast_node) -> ErrorOr<Success>;
  83. // Unwinds Actions from the stack until the StatementAction associated with
  84. // `ast_node` has been removed from the stack. If `result` is specified,
  85. // it represents the result of that Action (StatementActions normally cannot
  86. // produce results, but the body of a function can).
  87. auto UnwindPast(Nonnull<const Statement*> ast_node) -> ErrorOr<Success>;
  88. auto UnwindPast(Nonnull<const Statement*> ast_node,
  89. Nonnull<const Value*> result) -> ErrorOr<Success>;
  90. // Resumes execution of a suspended continuation.
  91. auto Resume(Nonnull<const ContinuationValue*> continuation)
  92. -> ErrorOr<Success>;
  93. // Suspends execution of the currently-executing continuation.
  94. auto Suspend() -> ErrorOr<Success>;
  95. void Pop() { todo_.Pop(); }
  96. private:
  97. // Pop any ScopeActions from the top of the stack, propagating results as
  98. // needed, to restore the invariant that todo_.Top() is not a ScopeAction.
  99. // Store the popped scope action into cleanup_stack, so that the destructor
  100. // can be called for the variables
  101. void PopScopes(std::stack<std::unique_ptr<Action>>& cleanup_stack);
  102. // Set `result` as the result of the Action most recently removed from the
  103. // stack.
  104. void SetResult(Nonnull<const Value*> result);
  105. auto UnwindToWithCaptureScopesToDestroy(Nonnull<const Statement*> ast_node)
  106. -> std::stack<std::unique_ptr<Action>>;
  107. auto UnwindPastWithCaptureScopesToDestroy(Nonnull<const Statement*> ast_node)
  108. -> std::stack<std::unique_ptr<Action>>;
  109. // Create CleanUpActions for all actions
  110. void PushCleanUpActions(std::stack<std::unique_ptr<Action>> actions);
  111. // Create and push a CleanUpAction on the stack
  112. void PushCleanUpAction(std::unique_ptr<Action> act);
  113. // TODO: consider defining a non-nullable unique_ptr-like type to use here.
  114. Stack<std::unique_ptr<Action>> todo_;
  115. std::optional<Nonnull<const Value*>> result_;
  116. std::optional<RuntimeScope> globals_;
  117. Phase phase_;
  118. };
  119. } // namespace Carbon
  120. #endif // CARBON_EXPLORER_INTERPRETER_ACTION_STACK_H_