action.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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<const Value*>& { return results; }
  34. void IncrementPos() { ++pos; }
  35. void AddResult(const Value* result) { results.push_back(result); }
  36. void Clear() {
  37. pos = 0;
  38. results.clear();
  39. }
  40. // Returns the enumerator corresponding to the most-derived type of this
  41. // object.
  42. auto Tag() const -> Kind { return tag; }
  43. static void PrintList(const Stack<Ptr<Action>>& ls, llvm::raw_ostream& out);
  44. void Print(llvm::raw_ostream& out) const;
  45. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  46. protected:
  47. // Constructs an Action. `tag` must be the enumerator corresponding to the
  48. // most-derived type being constructed.
  49. explicit Action(Kind tag) : tag(tag) {}
  50. private:
  51. int pos = 0;
  52. std::vector<const Value*> results;
  53. const Kind tag;
  54. };
  55. class LValAction : public Action {
  56. public:
  57. explicit LValAction(const Expression* exp)
  58. : Action(Kind::LValAction), exp(exp) {}
  59. static auto classof(const Action* action) -> bool {
  60. return action->Tag() == Kind::LValAction;
  61. }
  62. auto Exp() const -> const Expression* { return exp; }
  63. private:
  64. const Expression* exp;
  65. };
  66. class ExpressionAction : public Action {
  67. public:
  68. explicit ExpressionAction(const Expression* exp)
  69. : Action(Kind::ExpressionAction), exp(exp) {}
  70. static auto classof(const Action* action) -> bool {
  71. return action->Tag() == Kind::ExpressionAction;
  72. }
  73. auto Exp() const -> const Expression* { return exp; }
  74. private:
  75. const Expression* exp;
  76. };
  77. class PatternAction : public Action {
  78. public:
  79. explicit PatternAction(const Pattern* pat)
  80. : Action(Kind::PatternAction), pat(pat) {}
  81. static auto classof(const Action* action) -> bool {
  82. return action->Tag() == Kind::PatternAction;
  83. }
  84. auto Pat() const -> const Pattern* { return pat; }
  85. private:
  86. const Pattern* pat;
  87. };
  88. class StatementAction : public Action {
  89. public:
  90. explicit StatementAction(const Statement* stmt)
  91. : Action(Kind::StatementAction), stmt(stmt) {}
  92. static auto classof(const Action* action) -> bool {
  93. return action->Tag() == Kind::StatementAction;
  94. }
  95. auto Stmt() const -> const Statement* { return stmt; }
  96. private:
  97. const Statement* stmt;
  98. };
  99. } // namespace Carbon
  100. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_