action.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/stack.h"
  12. #include "executable_semantics/interpreter/value.h"
  13. #include "llvm/Support/Compiler.h"
  14. namespace Carbon {
  15. class Action {
  16. public:
  17. enum class Kind {
  18. LValAction,
  19. ExpressionAction,
  20. PatternAction,
  21. StatementAction,
  22. ValAction,
  23. };
  24. Action(const Value&) = delete;
  25. Action& operator=(const Value&) = delete;
  26. // The position or state of the action. Starts at 0 and goes up to the number
  27. // of subexpressions.
  28. //
  29. // pos indicates how many of the entries in the following `results` vector
  30. // will be filled in the next time this action is active.
  31. // For each i < pos, results[i] contains a pointer to a Value.
  32. auto Pos() const -> int { return pos; }
  33. // Results from a subexpression.
  34. auto Results() const -> const std::vector<const Value*>& { return results; }
  35. void IncrementPos() { ++pos; }
  36. void AddResult(const Value* result) { results.push_back(result); }
  37. void Clear() {
  38. pos = 0;
  39. results.clear();
  40. }
  41. // Returns the enumerator corresponding to the most-derived type of this
  42. // object.
  43. auto Tag() const -> Kind { return tag; }
  44. static void PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out);
  45. void Print(llvm::raw_ostream& out) const;
  46. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  47. protected:
  48. // Constructs an Action. `tag` must be the enumerator corresponding to the
  49. // most-derived type being constructed.
  50. explicit Action(Kind tag) : tag(tag) {}
  51. private:
  52. int pos = 0;
  53. std::vector<const Value*> results;
  54. const Kind tag;
  55. };
  56. class LValAction : public Action {
  57. public:
  58. explicit LValAction(const Expression* exp)
  59. : Action(Kind::LValAction), exp(exp) {}
  60. static auto classof(const Action* action) -> bool {
  61. return action->Tag() == Kind::LValAction;
  62. }
  63. auto Exp() const -> const Expression* { return exp; }
  64. private:
  65. const Expression* exp;
  66. };
  67. class ExpressionAction : public Action {
  68. public:
  69. explicit ExpressionAction(const Expression* exp)
  70. : Action(Kind::ExpressionAction), exp(exp) {}
  71. static auto classof(const Action* action) -> bool {
  72. return action->Tag() == Kind::ExpressionAction;
  73. }
  74. auto Exp() const -> const Expression* { return exp; }
  75. private:
  76. const Expression* exp;
  77. };
  78. class PatternAction : public Action {
  79. public:
  80. explicit PatternAction(const Pattern* pat)
  81. : Action(Kind::PatternAction), pat(pat) {}
  82. static auto classof(const Action* action) -> bool {
  83. return action->Tag() == Kind::PatternAction;
  84. }
  85. auto Pat() const -> const Pattern* { return pat; }
  86. private:
  87. const Pattern* pat;
  88. };
  89. class StatementAction : public Action {
  90. public:
  91. explicit StatementAction(const Statement* stmt)
  92. : Action(Kind::StatementAction), stmt(stmt) {}
  93. static auto classof(const Action* action) -> bool {
  94. return action->Tag() == Kind::StatementAction;
  95. }
  96. auto Stmt() const -> const Statement* { return stmt; }
  97. private:
  98. const Statement* stmt;
  99. };
  100. class ValAction : public Action {
  101. public:
  102. explicit ValAction(const Value* val) : Action(Kind::ValAction), val(val) {}
  103. static auto classof(const Action* action) -> bool {
  104. return action->Tag() == Kind::ValAction;
  105. }
  106. auto Val() const -> const Value* { return val; }
  107. private:
  108. const Value* val;
  109. };
  110. } // namespace Carbon
  111. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_