action.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. enum class ActionKind {
  16. LValAction,
  17. ExpressionAction,
  18. PatternAction,
  19. StatementAction,
  20. ValAction,
  21. };
  22. struct LValAction {
  23. static constexpr ActionKind Kind = ActionKind::LValAction;
  24. const Expression* exp;
  25. };
  26. struct ExpressionAction {
  27. static constexpr ActionKind Kind = ActionKind::ExpressionAction;
  28. const Expression* exp;
  29. };
  30. struct PatternAction {
  31. static constexpr ActionKind Kind = ActionKind::PatternAction;
  32. const Pattern* pattern;
  33. };
  34. struct StatementAction {
  35. static constexpr ActionKind Kind = ActionKind::StatementAction;
  36. const Statement* stmt;
  37. };
  38. struct ValAction {
  39. static constexpr ActionKind Kind = ActionKind::ValAction;
  40. const Value* val;
  41. };
  42. struct Action {
  43. static auto MakeLValAction(const Expression* e) -> Action*;
  44. static auto MakeExpressionAction(const Expression* e) -> Action*;
  45. static auto MakePatternAction(const Pattern* p) -> Action*;
  46. static auto MakeStatementAction(const Statement* s) -> Action*;
  47. static auto MakeValAction(const Value* v) -> Action*;
  48. static void PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out);
  49. auto GetLValAction() const -> const LValAction&;
  50. auto GetExpressionAction() const -> const ExpressionAction&;
  51. auto GetPatternAction() const -> const PatternAction&;
  52. auto GetStatementAction() const -> const StatementAction&;
  53. auto GetValAction() const -> const ValAction&;
  54. void Print(llvm::raw_ostream& out) const;
  55. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  56. inline auto tag() const -> ActionKind {
  57. return std::visit([](const auto& t) { return t.Kind; }, value);
  58. }
  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. int pos = 0;
  66. // Results from a subexpression.
  67. std::vector<const Value*> results;
  68. private:
  69. std::variant<LValAction, ExpressionAction, PatternAction, StatementAction,
  70. ValAction>
  71. value;
  72. };
  73. } // namespace Carbon
  74. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_