statement.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <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/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, Nonnull<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 -> Nonnull<const Expression*> { return exp; }
  56. private:
  57. Nonnull<const Expression*> exp;
  58. };
  59. class Assign : public Statement {
  60. public:
  61. Assign(SourceLocation loc, Nonnull<const Expression*> lhs,
  62. Nonnull<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 -> Nonnull<const Expression*> { return lhs; }
  68. auto Rhs() const -> Nonnull<const Expression*> { return rhs; }
  69. private:
  70. Nonnull<const Expression*> lhs;
  71. Nonnull<const Expression*> rhs;
  72. };
  73. class VariableDefinition : public Statement {
  74. public:
  75. VariableDefinition(SourceLocation loc, Nonnull<const Pattern*> pat,
  76. Nonnull<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 -> Nonnull<const Pattern*> { return pat; }
  82. auto Init() const -> Nonnull<const Expression*> { return init; }
  83. private:
  84. Nonnull<const Pattern*> pat;
  85. Nonnull<const Expression*> init;
  86. };
  87. class If : public Statement {
  88. public:
  89. If(SourceLocation loc, Nonnull<const Expression*> cond,
  90. Nonnull<const Statement*> then_stmt,
  91. std::optional<Nonnull<const Statement*>> else_stmt)
  92. : Statement(Kind::If, loc),
  93. cond(cond),
  94. then_stmt(then_stmt),
  95. else_stmt(else_stmt) {}
  96. static auto classof(const Statement* stmt) -> bool {
  97. return stmt->Tag() == Kind::If;
  98. }
  99. auto Cond() const -> Nonnull<const Expression*> { return cond; }
  100. auto ThenStmt() const -> Nonnull<const Statement*> { return then_stmt; }
  101. auto ElseStmt() const -> std::optional<Nonnull<const Statement*>> {
  102. return else_stmt;
  103. }
  104. private:
  105. Nonnull<const Expression*> cond;
  106. Nonnull<const Statement*> then_stmt;
  107. std::optional<Nonnull<const Statement*>> else_stmt;
  108. };
  109. class Return : public Statement {
  110. public:
  111. Return(Nonnull<Arena*> arena, SourceLocation loc)
  112. : Return(loc, arena->New<TupleLiteral>(loc), true) {}
  113. Return(SourceLocation loc, Nonnull<const Expression*> exp,
  114. bool is_omitted_exp)
  115. : Statement(Kind::Return, loc),
  116. exp(exp),
  117. is_omitted_exp(is_omitted_exp) {}
  118. static auto classof(const Statement* stmt) -> bool {
  119. return stmt->Tag() == Kind::Return;
  120. }
  121. auto Exp() const -> Nonnull<const Expression*> { return exp; }
  122. auto IsOmittedExp() const -> bool { return is_omitted_exp; }
  123. private:
  124. Nonnull<const Expression*> exp;
  125. bool is_omitted_exp;
  126. };
  127. class Sequence : public Statement {
  128. public:
  129. Sequence(SourceLocation loc, Nonnull<const Statement*> stmt,
  130. std::optional<Nonnull<const Statement*>> next)
  131. : Statement(Kind::Sequence, loc), stmt(stmt), next(next) {}
  132. static auto classof(const Statement* stmt) -> bool {
  133. return stmt->Tag() == Kind::Sequence;
  134. }
  135. auto Stmt() const -> Nonnull<const Statement*> { return stmt; }
  136. auto Next() const -> std::optional<Nonnull<const Statement*>> { return next; }
  137. private:
  138. Nonnull<const Statement*> stmt;
  139. std::optional<Nonnull<const Statement*>> next;
  140. };
  141. class Block : public Statement {
  142. public:
  143. Block(SourceLocation loc, std::optional<Nonnull<const Statement*>> stmt)
  144. : Statement(Kind::Block, loc), stmt(stmt) {}
  145. static auto classof(const Statement* stmt) -> bool {
  146. return stmt->Tag() == Kind::Block;
  147. }
  148. auto Stmt() const -> std::optional<Nonnull<const Statement*>> { return stmt; }
  149. private:
  150. std::optional<Nonnull<const Statement*>> stmt;
  151. };
  152. class While : public Statement {
  153. public:
  154. While(SourceLocation loc, Nonnull<const Expression*> cond,
  155. Nonnull<const Statement*> body)
  156. : Statement(Kind::While, loc), cond(cond), body(body) {}
  157. static auto classof(const Statement* stmt) -> bool {
  158. return stmt->Tag() == Kind::While;
  159. }
  160. auto Cond() const -> Nonnull<const Expression*> { return cond; }
  161. auto Body() const -> Nonnull<const Statement*> { return body; }
  162. private:
  163. Nonnull<const Expression*> cond;
  164. Nonnull<const Statement*> body;
  165. };
  166. class Break : public Statement {
  167. public:
  168. explicit Break(SourceLocation loc) : Statement(Kind::Break, loc) {}
  169. static auto classof(const Statement* stmt) -> bool {
  170. return stmt->Tag() == Kind::Break;
  171. }
  172. };
  173. class Continue : public Statement {
  174. public:
  175. explicit Continue(SourceLocation loc) : Statement(Kind::Continue, loc) {}
  176. static auto classof(const Statement* stmt) -> bool {
  177. return stmt->Tag() == Kind::Continue;
  178. }
  179. };
  180. class Match : public Statement {
  181. public:
  182. Match(
  183. SourceLocation loc, Nonnull<const Expression*> exp,
  184. std::vector<std::pair<Nonnull<const Pattern*>, Nonnull<const Statement*>>>
  185. clauses)
  186. : Statement(Kind::Match, loc), exp(exp), clauses(std::move(clauses)) {}
  187. static auto classof(const Statement* stmt) -> bool {
  188. return stmt->Tag() == Kind::Match;
  189. }
  190. auto Exp() const -> Nonnull<const Expression*> { return exp; }
  191. auto Clauses() const -> const std::vector<
  192. std::pair<Nonnull<const Pattern*>, Nonnull<const Statement*>>>& {
  193. return clauses;
  194. }
  195. private:
  196. Nonnull<const Expression*> exp;
  197. std::vector<std::pair<Nonnull<const Pattern*>, Nonnull<const Statement*>>>
  198. clauses;
  199. };
  200. // A continuation statement.
  201. //
  202. // __continuation <continuation_variable> {
  203. // <body>
  204. // }
  205. class Continuation : public Statement {
  206. public:
  207. Continuation(SourceLocation loc, std::string continuation_variable,
  208. Nonnull<const Statement*> body)
  209. : Statement(Kind::Continuation, loc),
  210. continuation_variable(std::move(continuation_variable)),
  211. body(body) {}
  212. static auto classof(const Statement* stmt) -> bool {
  213. return stmt->Tag() == Kind::Continuation;
  214. }
  215. auto ContinuationVariable() const -> const std::string& {
  216. return continuation_variable;
  217. }
  218. auto Body() const -> Nonnull<const Statement*> { return body; }
  219. private:
  220. std::string continuation_variable;
  221. Nonnull<const Statement*> body;
  222. };
  223. // A run statement.
  224. //
  225. // __run <argument>;
  226. class Run : public Statement {
  227. public:
  228. Run(SourceLocation loc, Nonnull<const Expression*> argument)
  229. : Statement(Kind::Run, loc), argument(argument) {}
  230. static auto classof(const Statement* stmt) -> bool {
  231. return stmt->Tag() == Kind::Run;
  232. }
  233. auto Argument() const -> Nonnull<const Expression*> { return argument; }
  234. private:
  235. Nonnull<const Expression*> argument;
  236. };
  237. // An await statement.
  238. //
  239. // __await;
  240. class Await : public Statement {
  241. public:
  242. explicit Await(SourceLocation loc) : Statement(Kind::Await, loc) {}
  243. static auto classof(const Statement* stmt) -> bool {
  244. return stmt->Tag() == Kind::Await;
  245. }
  246. };
  247. } // namespace Carbon
  248. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_