statement.h 10 KB

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