action_stack.h 5.2 KB

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