action_stack.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 ActionStack
  17. ActionStack() = default;
  18. void Print(llvm::raw_ostream& out) const;
  19. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  20. // TODO: consider unifying with Print.
  21. void PrintScopes(llvm::raw_ostream& out) const;
  22. // Sets the heap that variables will be allocated on. Cannot be called at
  23. // run time, or when IsEmpty() is false, and marks the start of run time.
  24. void SetHeap(Nonnull<HeapAllocationInterface*> heap) {
  25. CHECK(todo_.IsEmpty());
  26. CHECK(!globals_.has_value());
  27. globals_ = RuntimeScope(heap);
  28. }
  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 `named_entity`, and initializes it to `value`.
  38. void Initialize(NamedEntityView named_entity, Nonnull<const Value*> value);
  39. // Returns the value bound to `named_entity`. If `named_entity` is a local
  40. // variable, this will be an LValue.
  41. auto ValueOfName(NamedEntityView named_entity,
  42. SourceLocation source_loc) const -> 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 calling these methods as part of
  60. // return statements, e.g. `return todo_.FinishAction()`, even though they
  61. // return void.
  62. // Finishes execution of the current Action. If `result` is specified, it
  63. // represents the result of that Action.
  64. void FinishAction();
  65. void FinishAction(Nonnull<const Value*> result);
  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. void Spawn(std::unique_ptr<Action> child);
  69. void Spawn(std::unique_ptr<Action> child, RuntimeScope scope);
  70. // Advances the current action one step.
  71. void RunAgain();
  72. // Unwinds Actions from the stack until the StatementAction associated with
  73. // `ast_node` is at the top of the stack.
  74. void UnwindTo(Nonnull<const Statement*> ast_node);
  75. // Unwinds Actions from the stack until the StatementAction associated with
  76. // `ast_node` has been removed from the stack. If `result` is specified,
  77. // it represents the result of that Action (StatementActions normally cannot
  78. // produce results, but the body of a function can).
  79. void UnwindPast(Nonnull<const Statement*> ast_node);
  80. void UnwindPast(Nonnull<const Statement*> ast_node,
  81. Nonnull<const Value*> result);
  82. // Resumes execution of a suspended continuation.
  83. void Resume(Nonnull<const ContinuationValue*> continuation);
  84. // Suspends execution of the currently-executing continuation.
  85. void Suspend();
  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. void PopScopes();
  90. // Set `result` as the result of the Action most recently removed from the
  91. // stack.
  92. void SetResult(Nonnull<const Value*> result);
  93. // TODO: consider defining a non-nullable unique_ptr-like type to use here.
  94. Stack<std::unique_ptr<Action>> todo_;
  95. std::optional<Nonnull<const Value*>> result_;
  96. std::optional<RuntimeScope> globals_;
  97. };
  98. } // namespace Carbon
  99. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_STACK_H_