statement.h 8.3 KB

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