action.h 3.6 KB

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