action.h 7.9 KB

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