statement.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_AST_STATEMENT_H_
  5. #define EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
  6. #include <list>
  7. #include "common/ostream.h"
  8. #include "executable_semantics/ast/expression.h"
  9. #include "executable_semantics/ast/pattern.h"
  10. #include "executable_semantics/ast/source_location.h"
  11. #include "executable_semantics/common/arena.h"
  12. #include "llvm/Support/Compiler.h"
  13. namespace Carbon {
  14. class Statement {
  15. public:
  16. enum class Kind {
  17. ExpressionStatement,
  18. Assign,
  19. VariableDefinition,
  20. If,
  21. Return,
  22. Sequence,
  23. Block,
  24. While,
  25. Break,
  26. Continue,
  27. Match,
  28. Continuation, // Create a first-class continuation.
  29. Run, // Run a continuation to the next await or until it finishes.
  30. Await, // Pause execution of the continuation.
  31. };
  32. // Returns the enumerator corresponding to the most-derived type of this
  33. // object.
  34. auto Tag() const -> Kind { return tag; }
  35. auto SourceLoc() const -> SourceLocation { return loc; }
  36. void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); }
  37. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  38. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  39. protected:
  40. // Constructs an Statement representing syntax at the given line number.
  41. // `tag` must be the enumerator corresponding to the most-derived type being
  42. // constructed.
  43. Statement(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
  44. private:
  45. const Kind tag;
  46. SourceLocation loc;
  47. };
  48. class ExpressionStatement : public Statement {
  49. public:
  50. ExpressionStatement(SourceLocation loc, Ptr<const Expression> exp)
  51. : Statement(Kind::ExpressionStatement, loc), exp(exp) {}
  52. static auto classof(const Statement* stmt) -> bool {
  53. return stmt->Tag() == Kind::ExpressionStatement;
  54. }
  55. auto Exp() const -> Ptr<const Expression> { return exp; }
  56. private:
  57. Ptr<const Expression> exp;
  58. };
  59. class Assign : public Statement {
  60. public:
  61. Assign(SourceLocation loc, Ptr<const Expression> lhs,
  62. Ptr<const Expression> rhs)
  63. : Statement(Kind::Assign, loc), lhs(lhs), rhs(rhs) {}
  64. static auto classof(const Statement* stmt) -> bool {
  65. return stmt->Tag() == Kind::Assign;
  66. }
  67. auto Lhs() const -> Ptr<const Expression> { return lhs; }
  68. auto Rhs() const -> Ptr<const Expression> { return rhs; }
  69. private:
  70. Ptr<const Expression> lhs;
  71. Ptr<const Expression> rhs;
  72. };
  73. class VariableDefinition : public Statement {
  74. public:
  75. VariableDefinition(SourceLocation loc, Ptr<const Pattern> pat,
  76. Ptr<const Expression> init)
  77. : Statement(Kind::VariableDefinition, loc), pat(pat), init(init) {}
  78. static auto classof(const Statement* stmt) -> bool {
  79. return stmt->Tag() == Kind::VariableDefinition;
  80. }
  81. auto Pat() const -> Ptr<const Pattern> { return pat; }
  82. auto Init() const -> Ptr<const Expression> { return init; }
  83. private:
  84. Ptr<const Pattern> pat;
  85. Ptr<const Expression> init;
  86. };
  87. class If : public Statement {
  88. public:
  89. If(SourceLocation loc, Ptr<const Expression> cond, const Statement* then_stmt,
  90. const Statement* else_stmt)
  91. : Statement(Kind::If, loc),
  92. cond(cond),
  93. then_stmt(then_stmt),
  94. else_stmt(else_stmt) {}
  95. static auto classof(const Statement* stmt) -> bool {
  96. return stmt->Tag() == Kind::If;
  97. }
  98. auto Cond() const -> Ptr<const Expression> { return cond; }
  99. auto ThenStmt() const -> const Statement* { return then_stmt; }
  100. auto ElseStmt() const -> const Statement* { return else_stmt; }
  101. private:
  102. Ptr<const Expression> cond;
  103. const Statement* then_stmt;
  104. const Statement* else_stmt;
  105. };
  106. class Return : public Statement {
  107. public:
  108. explicit Return(SourceLocation loc)
  109. : Return(loc, global_arena->New<TupleLiteral>(loc), true) {}
  110. Return(SourceLocation loc, Ptr<const Expression> exp, bool is_omitted_exp)
  111. : Statement(Kind::Return, loc),
  112. exp(exp),
  113. is_omitted_exp(is_omitted_exp) {}
  114. static auto classof(const Statement* stmt) -> bool {
  115. return stmt->Tag() == Kind::Return;
  116. }
  117. auto Exp() const -> Ptr<const Expression> { return exp; }
  118. auto IsOmittedExp() const -> bool { return is_omitted_exp; }
  119. private:
  120. Ptr<const Expression> exp;
  121. bool is_omitted_exp;
  122. };
  123. class Sequence : public Statement {
  124. public:
  125. Sequence(SourceLocation loc, const Statement* stmt, const Statement* next)
  126. : Statement(Kind::Sequence, loc), stmt(stmt), next(next) {}
  127. static auto classof(const Statement* stmt) -> bool {
  128. return stmt->Tag() == Kind::Sequence;
  129. }
  130. auto Stmt() const -> const Statement* { return stmt; }
  131. auto Next() const -> const Statement* { return next; }
  132. private:
  133. const Statement* stmt;
  134. const Statement* next;
  135. };
  136. class Block : public Statement {
  137. public:
  138. Block(SourceLocation loc, const Statement* stmt)
  139. : Statement(Kind::Block, loc), stmt(stmt) {}
  140. static auto classof(const Statement* stmt) -> bool {
  141. return stmt->Tag() == Kind::Block;
  142. }
  143. auto Stmt() const -> const Statement* { return stmt; }
  144. private:
  145. const Statement* stmt;
  146. };
  147. class While : public Statement {
  148. public:
  149. While(SourceLocation loc, Ptr<const Expression> cond, const Statement* body)
  150. : Statement(Kind::While, loc), cond(cond), body(body) {}
  151. static auto classof(const Statement* stmt) -> bool {
  152. return stmt->Tag() == Kind::While;
  153. }
  154. auto Cond() const -> Ptr<const Expression> { return cond; }
  155. auto Body() const -> const Statement* { return body; }
  156. private:
  157. Ptr<const Expression> cond;
  158. const Statement* body;
  159. };
  160. class Break : public Statement {
  161. public:
  162. explicit Break(SourceLocation loc) : Statement(Kind::Break, loc) {}
  163. static auto classof(const Statement* stmt) -> bool {
  164. return stmt->Tag() == Kind::Break;
  165. }
  166. };
  167. class Continue : public Statement {
  168. public:
  169. explicit Continue(SourceLocation loc) : Statement(Kind::Continue, loc) {}
  170. static auto classof(const Statement* stmt) -> bool {
  171. return stmt->Tag() == Kind::Continue;
  172. }
  173. };
  174. class Match : public Statement {
  175. public:
  176. Match(SourceLocation loc, Ptr<const Expression> exp,
  177. std::list<std::pair<Ptr<const Pattern>, const Statement*>>* clauses)
  178. : Statement(Kind::Match, loc), exp(exp), clauses(clauses) {}
  179. static auto classof(const Statement* stmt) -> bool {
  180. return stmt->Tag() == Kind::Match;
  181. }
  182. auto Exp() const -> Ptr<const Expression> { return exp; }
  183. auto Clauses() const
  184. -> const std::list<std::pair<Ptr<const Pattern>, const Statement*>>* {
  185. return clauses;
  186. }
  187. private:
  188. Ptr<const Expression> exp;
  189. std::list<std::pair<Ptr<const Pattern>, const Statement*>>* clauses;
  190. };
  191. // A continuation statement.
  192. //
  193. // __continuation <continuation_variable> {
  194. // <body>
  195. // }
  196. class Continuation : public Statement {
  197. public:
  198. Continuation(SourceLocation loc, std::string continuation_variable,
  199. const Statement* body)
  200. : Statement(Kind::Continuation, loc),
  201. continuation_variable(std::move(continuation_variable)),
  202. body(body) {}
  203. static auto classof(const Statement* stmt) -> bool {
  204. return stmt->Tag() == Kind::Continuation;
  205. }
  206. auto ContinuationVariable() const -> const std::string& {
  207. return continuation_variable;
  208. }
  209. auto Body() const -> const Statement* { return body; }
  210. private:
  211. std::string continuation_variable;
  212. const Statement* body;
  213. };
  214. // A run statement.
  215. //
  216. // __run <argument>;
  217. class Run : public Statement {
  218. public:
  219. Run(SourceLocation loc, Ptr<const Expression> argument)
  220. : Statement(Kind::Run, loc), argument(argument) {}
  221. static auto classof(const Statement* stmt) -> bool {
  222. return stmt->Tag() == Kind::Run;
  223. }
  224. auto Argument() const -> Ptr<const Expression> { return argument; }
  225. private:
  226. Ptr<const Expression> argument;
  227. };
  228. // An await statement.
  229. //
  230. // __await;
  231. class Await : public Statement {
  232. public:
  233. explicit Await(SourceLocation loc) : Statement(Kind::Await, loc) {}
  234. static auto classof(const Statement* stmt) -> bool {
  235. return stmt->Tag() == Kind::Await;
  236. }
  237. };
  238. } // namespace Carbon
  239. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_