action.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/stack.h"
  13. #include "executable_semantics/interpreter/value.h"
  14. #include "llvm/Support/Compiler.h"
  15. namespace Carbon {
  16. using Env = Dictionary<std::string, AllocationId>;
  17. struct Scope {
  18. explicit Scope(Env values) : Scope(values, std::vector<std::string>()) {}
  19. Scope(Env values, std::vector<std::string> l)
  20. : values(values), locals(std::move(l)) {}
  21. Env values;
  22. std::vector<std::string> locals;
  23. bool deallocated = false;
  24. };
  25. class Action {
  26. public:
  27. enum class Kind {
  28. LValAction,
  29. ExpressionAction,
  30. PatternAction,
  31. StatementAction,
  32. ScopeAction,
  33. };
  34. Action(const Value&) = delete;
  35. auto operator=(const Value&) -> Action& = delete;
  36. void AddResult(Nonnull<const Value*> result) { results_.push_back(result); }
  37. void Clear() {
  38. CHECK(!scope_.has_value());
  39. pos_ = 0;
  40. results_.clear();
  41. }
  42. // Associates this action with a new scope, with initial state `scope`.
  43. // Values that are local to this scope will be deallocated when this
  44. // Action is completed or unwound. Can only be called once on a given
  45. // Action.
  46. void StartScope(Scope scope) {
  47. CHECK(!scope_.has_value());
  48. scope_ = std::move(scope);
  49. }
  50. // Returns the scope associated with this Action, if any.
  51. auto scope() -> std::optional<Scope>& { return scope_; }
  52. static void PrintList(const Stack<Nonnull<Action*>>& ls,
  53. llvm::raw_ostream& out);
  54. void Print(llvm::raw_ostream& out) const;
  55. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  56. // Returns the enumerator corresponding to the most-derived type of this
  57. // object.
  58. auto kind() const -> Kind { return kind_; }
  59. // The position or state of the action. Starts at 0 and goes up to the number
  60. // of subexpressions.
  61. //
  62. // pos indicates how many of the entries in the following `results` vector
  63. // will be filled in the next time this action is active.
  64. // For each i < pos, results[i] contains a pointer to a Value.
  65. auto pos() const -> int { return pos_; }
  66. void set_pos(int pos) { this->pos_ = pos; }
  67. // Results from a subexpression.
  68. auto results() const -> const std::vector<Nonnull<const Value*>>& {
  69. return results_;
  70. }
  71. protected:
  72. // Constructs an Action. `kind` must be the enumerator corresponding to the
  73. // most-derived type being constructed.
  74. explicit Action(Kind kind) : kind_(kind) {}
  75. private:
  76. int pos_ = 0;
  77. std::vector<Nonnull<const Value*>> results_;
  78. std::optional<Scope> scope_;
  79. const Kind kind_;
  80. };
  81. // An Action which implements evaluation of an Expression to produce an
  82. // lvalue. The result be expressed as a PointerValue which points to the
  83. // Expression's value.
  84. class LValAction : public Action {
  85. public:
  86. explicit LValAction(Nonnull<const Expression*> expression)
  87. : Action(Kind::LValAction), expression_(expression) {}
  88. static auto classof(const Action* action) -> bool {
  89. return action->kind() == Kind::LValAction;
  90. }
  91. // The Expression this Action evaluates.
  92. auto expression() const -> const Expression& { return *expression_; }
  93. private:
  94. Nonnull<const Expression*> expression_;
  95. };
  96. // An Action which implements evaluation of an Expression to produce an
  97. // rvalue. The result is expressed as a Value.
  98. class ExpressionAction : public Action {
  99. public:
  100. explicit ExpressionAction(Nonnull<const Expression*> expression)
  101. : Action(Kind::ExpressionAction), expression_(expression) {}
  102. static auto classof(const Action* action) -> bool {
  103. return action->kind() == Kind::ExpressionAction;
  104. }
  105. // The Expression this Action evaluates.
  106. auto expression() const -> const Expression& { return *expression_; }
  107. private:
  108. Nonnull<const Expression*> expression_;
  109. };
  110. // An Action which implements evaluation of a Pattern. The result is expressed
  111. // as a Value.
  112. class PatternAction : public Action {
  113. public:
  114. explicit PatternAction(Nonnull<const Pattern*> pattern)
  115. : Action(Kind::PatternAction), pattern_(pattern) {}
  116. static auto classof(const Action* action) -> bool {
  117. return action->kind() == Kind::PatternAction;
  118. }
  119. // The Pattern this Action evaluates.
  120. auto pattern() const -> const Pattern& { return *pattern_; }
  121. private:
  122. Nonnull<const Pattern*> pattern_;
  123. };
  124. // An Action which implements execution of a Statement. Does not produce a
  125. // result.
  126. class StatementAction : public Action {
  127. public:
  128. explicit StatementAction(Nonnull<const Statement*> statement)
  129. : Action(Kind::StatementAction), statement_(statement) {}
  130. static auto classof(const Action* action) -> bool {
  131. return action->kind() == Kind::StatementAction;
  132. }
  133. // The Statement this Action executes.
  134. auto statement() const -> const Statement& { return *statement_; }
  135. private:
  136. Nonnull<const Statement*> statement_;
  137. };
  138. // Action which does nothing except introduce a new scope into the action
  139. // stack. This is useful when a distinct scope doesn't otherwise have an
  140. // Action it can naturally be associated with. ScopeActions are not associated
  141. // with AST nodes.
  142. class ScopeAction : public Action {
  143. public:
  144. ScopeAction(Scope scope) : Action(Kind::ScopeAction) {
  145. StartScope(std::move(scope));
  146. }
  147. static auto classof(const Action* action) -> bool {
  148. return action->kind() == Kind::ScopeAction;
  149. }
  150. };
  151. } // namespace Carbon
  152. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_