action.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_
  6. #include <map>
  7. #include <vector>
  8. #include "common/ostream.h"
  9. #include "executable_semantics/ast/expression.h"
  10. #include "executable_semantics/ast/pattern.h"
  11. #include "executable_semantics/ast/statement.h"
  12. #include "executable_semantics/interpreter/dictionary.h"
  13. #include "executable_semantics/interpreter/heap_allocation_interface.h"
  14. #include "executable_semantics/interpreter/stack.h"
  15. #include "executable_semantics/interpreter/value.h"
  16. #include "llvm/Support/Compiler.h"
  17. namespace Carbon {
  18. // A RuntimeScope manages and provides access to the storage for names that are
  19. // not compile-time constants.
  20. class RuntimeScope {
  21. public:
  22. // Returns a RuntimeScope whose Get() operation for a given name returns the
  23. // storage owned by the first entry in `scopes` that defines that name. This
  24. // behavior is closely analogous to a `[&]` capture in C++, hence the name.
  25. // `scopes` must contain at least one entry, and all entries must be backed
  26. // by the same Heap.
  27. static auto Capture(const std::vector<Nonnull<const RuntimeScope*>>& scopes)
  28. -> RuntimeScope;
  29. // Constructs a RuntimeScope that allocates storage in `heap`.
  30. explicit RuntimeScope(Nonnull<HeapAllocationInterface*> heap) : heap_(heap) {}
  31. // Moving a RuntimeScope transfers ownership of its allocations.
  32. RuntimeScope(RuntimeScope&&) noexcept;
  33. auto operator=(RuntimeScope&&) noexcept -> RuntimeScope&;
  34. // Deallocates any allocations in this scope from `heap`.
  35. ~RuntimeScope();
  36. void Print(llvm::raw_ostream& out) const;
  37. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  38. // Allocates storage for `named_entity` in `heap`, and initializes it with
  39. // `value`.
  40. void Initialize(NamedEntityView named_entity, Nonnull<const Value*> value);
  41. // Transfers the names and allocations from `other` into *this. The two
  42. // scopes must not define the same name, and must be backed by the same Heap.
  43. void Merge(RuntimeScope other);
  44. // Returns the local storage for named_entity, if it has storage local to
  45. // this scope.
  46. auto Get(NamedEntityView named_entity) const
  47. -> std::optional<Nonnull<const LValue*>>;
  48. private:
  49. std::map<NamedEntityView, Nonnull<const LValue*>> locals_;
  50. std::vector<AllocationId> allocations_;
  51. Nonnull<HeapAllocationInterface*> heap_;
  52. };
  53. // An Action represents the current state of a self-contained computation,
  54. // usually associated with some AST node, such as evaluation of an expression or
  55. // execution of a statement. Execution of an action is divided into a series of
  56. // steps, and the `pos` field typically counts the number of steps executed.
  57. //
  58. // They should be destroyed as soon as they are done executing, in order to
  59. // clean up the associated Carbon scope, and consequently they should not be
  60. // allocated on an Arena. Actions are typically owned by the ActionStack.
  61. //
  62. // The actual behavior of an Action step is defined by Interpreter::Step, not by
  63. // Action or its subclasses.
  64. // TODO: consider moving this logic to a virtual method `Step`.
  65. class Action {
  66. public:
  67. enum class Kind {
  68. LValAction,
  69. ExpressionAction,
  70. PatternAction,
  71. StatementAction,
  72. DeclarationAction,
  73. ScopeAction,
  74. };
  75. Action(const Value&) = delete;
  76. auto operator=(const Value&) -> Action& = delete;
  77. virtual ~Action() = default;
  78. void Print(llvm::raw_ostream& out) const;
  79. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  80. // Resets this Action to its initial state.
  81. void Clear() {
  82. CHECK(!scope_.has_value());
  83. pos_ = 0;
  84. results_.clear();
  85. }
  86. // Returns the enumerator corresponding to the most-derived type of this
  87. // object.
  88. auto kind() const -> Kind { return kind_; }
  89. // The position or state of the action. Starts at 0 and is typically
  90. // incremented after each step.
  91. auto pos() const -> int { return pos_; }
  92. void set_pos(int pos) { this->pos_ = pos; }
  93. // The results of any Actions spawned by this Action.
  94. auto results() const -> const std::vector<Nonnull<const Value*>>& {
  95. return results_;
  96. }
  97. // Appends `result` to `results`.
  98. void AddResult(Nonnull<const Value*> result) { results_.push_back(result); }
  99. // Returns the scope associated with this Action, if any.
  100. auto scope() -> std::optional<RuntimeScope>& { return scope_; }
  101. // Associates this action with a new scope, with initial state `scope`.
  102. // Values that are local to this scope will be deallocated when this
  103. // Action is completed or unwound. Can only be called once on a given
  104. // Action.
  105. void StartScope(RuntimeScope scope) {
  106. CHECK(!scope_.has_value());
  107. scope_ = std::move(scope);
  108. }
  109. protected:
  110. // Constructs an Action. `kind` must be the enumerator corresponding to the
  111. // most-derived type being constructed.
  112. explicit Action(Kind kind) : kind_(kind) {}
  113. private:
  114. int pos_ = 0;
  115. std::vector<Nonnull<const Value*>> results_;
  116. std::optional<RuntimeScope> scope_;
  117. const Kind kind_;
  118. };
  119. // An Action which implements evaluation of an Expression to produce an
  120. // LValue.
  121. class LValAction : public Action {
  122. public:
  123. explicit LValAction(Nonnull<const Expression*> expression)
  124. : Action(Kind::LValAction), expression_(expression) {}
  125. static auto classof(const Action* action) -> bool {
  126. return action->kind() == Kind::LValAction;
  127. }
  128. // The Expression this Action evaluates.
  129. auto expression() const -> const Expression& { return *expression_; }
  130. private:
  131. Nonnull<const Expression*> expression_;
  132. };
  133. // An Action which implements evaluation of an Expression to produce an
  134. // rvalue. The result is expressed as a Value.
  135. class ExpressionAction : public Action {
  136. public:
  137. explicit ExpressionAction(Nonnull<const Expression*> expression)
  138. : Action(Kind::ExpressionAction), expression_(expression) {}
  139. static auto classof(const Action* action) -> bool {
  140. return action->kind() == Kind::ExpressionAction;
  141. }
  142. // The Expression this Action evaluates.
  143. auto expression() const -> const Expression& { return *expression_; }
  144. private:
  145. Nonnull<const Expression*> expression_;
  146. };
  147. // An Action which implements evaluation of a Pattern. The result is expressed
  148. // as a Value.
  149. class PatternAction : public Action {
  150. public:
  151. explicit PatternAction(Nonnull<const Pattern*> pattern)
  152. : Action(Kind::PatternAction), pattern_(pattern) {}
  153. static auto classof(const Action* action) -> bool {
  154. return action->kind() == Kind::PatternAction;
  155. }
  156. // The Pattern this Action evaluates.
  157. auto pattern() const -> const Pattern& { return *pattern_; }
  158. private:
  159. Nonnull<const Pattern*> pattern_;
  160. };
  161. // An Action which implements execution of a Statement. Does not produce a
  162. // result.
  163. class StatementAction : public Action {
  164. public:
  165. explicit StatementAction(Nonnull<const Statement*> statement)
  166. : Action(Kind::StatementAction), statement_(statement) {}
  167. static auto classof(const Action* action) -> bool {
  168. return action->kind() == Kind::StatementAction;
  169. }
  170. // The Statement this Action executes.
  171. auto statement() const -> const Statement& { return *statement_; }
  172. private:
  173. Nonnull<const Statement*> statement_;
  174. };
  175. // Action which implements the run-time effects of executing a Declaration.
  176. // Does not produce a result.
  177. class DeclarationAction : public Action {
  178. public:
  179. explicit DeclarationAction(Nonnull<const Declaration*> declaration)
  180. : Action(Kind::DeclarationAction), declaration_(declaration) {}
  181. static auto classof(const Action* action) -> bool {
  182. return action->kind() == Kind::DeclarationAction;
  183. }
  184. // The Declaration this Action executes.
  185. auto declaration() const -> const Declaration& { return *declaration_; }
  186. private:
  187. Nonnull<const Declaration*> declaration_;
  188. };
  189. // Action which does nothing except introduce a new scope into the action
  190. // stack. This is useful when a distinct scope doesn't otherwise have an
  191. // Action it can naturally be associated with. ScopeActions are not associated
  192. // with AST nodes.
  193. class ScopeAction : public Action {
  194. public:
  195. explicit ScopeAction(RuntimeScope scope) : Action(Kind::ScopeAction) {
  196. StartScope(std::move(scope));
  197. }
  198. static auto classof(const Action* action) -> bool {
  199. return action->kind() == Kind::ScopeAction;
  200. }
  201. };
  202. } // namespace Carbon
  203. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_