action_stack.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 `named_entity`, and initializes it to `value`.
  35. void Initialize(NamedEntityView named_entity, Nonnull<const Value*> value);
  36. // Returns the value bound to `named_entity`. If `named_entity` is a local
  37. // variable, this will be an LValue.
  38. auto ValueOfName(NamedEntityView named_entity,
  39. SourceLocation source_loc) const -> 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 calling these methods as part of
  57. // return statements, e.g. `return todo_.FinishAction()`, even though they
  58. // return void.
  59. // Finishes execution of the current Action. If `result` is specified, it
  60. // represents the result of that Action.
  61. void FinishAction();
  62. void FinishAction(Nonnull<const Value*> result);
  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. void Spawn(std::unique_ptr<Action> child);
  66. void Spawn(std::unique_ptr<Action> child, RuntimeScope scope);
  67. // Advances the current action one step.
  68. void RunAgain();
  69. // Unwinds Actions from the stack until the StatementAction associated with
  70. // `ast_node` is at the top of the stack.
  71. void UnwindTo(Nonnull<const Statement*> ast_node);
  72. // Unwinds Actions from the stack until the StatementAction associated with
  73. // `ast_node` has been removed from the stack. If `result` is specified,
  74. // it represents the result of that Action (StatementActions normally cannot
  75. // produce results, but the body of a function can).
  76. void UnwindPast(Nonnull<const Statement*> ast_node);
  77. void UnwindPast(Nonnull<const Statement*> ast_node,
  78. Nonnull<const Value*> result);
  79. // Resumes execution of a suspended continuation.
  80. void Resume(Nonnull<const ContinuationValue*> continuation);
  81. // Suspends execution of the currently-executing continuation.
  82. void Suspend();
  83. private:
  84. // Pop any ScopeActions from the top of the stack, propagating results as
  85. // needed, to restore the invariant that todo_.Top() is not a ScopeAction.
  86. void PopScopes();
  87. // Set `result` as the result of the Action most recently removed from the
  88. // stack.
  89. void SetResult(Nonnull<const Value*> result);
  90. // TODO: consider defining a non-nullable unique_ptr-like type to use here.
  91. Stack<std::unique_ptr<Action>> todo_;
  92. std::optional<Nonnull<const Value*>> result_;
  93. std::optional<RuntimeScope> globals_;
  94. };
  95. } // namespace Carbon
  96. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_STACK_H_