statement.h 9.3 KB

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