action_stack.h 5.8 KB

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