statement.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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,
  53. Nonnull<Expression*> expression)
  54. : Statement(Kind::ExpressionStatement, source_loc),
  55. expression_(expression) {}
  56. static auto classof(const Statement* stmt) -> bool {
  57. return stmt->kind() == Kind::ExpressionStatement;
  58. }
  59. auto expression() const -> const Expression& { return *expression_; }
  60. auto expression() -> Expression& { return *expression_; }
  61. private:
  62. Nonnull<Expression*> expression_;
  63. };
  64. class Assign : public Statement {
  65. public:
  66. Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
  67. Nonnull<Expression*> rhs)
  68. : Statement(Kind::Assign, source_loc), lhs_(lhs), rhs_(rhs) {}
  69. static auto classof(const Statement* stmt) -> bool {
  70. return stmt->kind() == Kind::Assign;
  71. }
  72. auto lhs() const -> const Expression& { return *lhs_; }
  73. auto lhs() -> Expression& { return *lhs_; }
  74. auto rhs() const -> const Expression& { return *rhs_; }
  75. auto rhs() -> Expression& { return *rhs_; }
  76. private:
  77. Nonnull<Expression*> lhs_;
  78. Nonnull<Expression*> rhs_;
  79. };
  80. class VariableDefinition : public Statement {
  81. public:
  82. VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
  83. Nonnull<Expression*> init)
  84. : Statement(Kind::VariableDefinition, source_loc),
  85. pattern_(pattern),
  86. init_(init) {}
  87. static auto classof(const Statement* stmt) -> bool {
  88. return stmt->kind() == Kind::VariableDefinition;
  89. }
  90. auto pattern() const -> const Pattern& { return *pattern_; }
  91. auto pattern() -> Pattern& { return *pattern_; }
  92. auto init() const -> const Expression& { return *init_; }
  93. auto init() -> Expression& { return *init_; }
  94. private:
  95. Nonnull<Pattern*> pattern_;
  96. Nonnull<Expression*> init_;
  97. };
  98. class If : public Statement {
  99. public:
  100. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  101. Nonnull<Statement*> then_statement,
  102. std::optional<Nonnull<Statement*>> else_statement)
  103. : Statement(Kind::If, source_loc),
  104. condition_(condition),
  105. then_statement_(then_statement),
  106. else_statement_(else_statement) {}
  107. static auto classof(const Statement* stmt) -> bool {
  108. return stmt->kind() == Kind::If;
  109. }
  110. auto condition() const -> const Expression& { return *condition_; }
  111. auto condition() -> Expression& { return *condition_; }
  112. auto then_statement() const -> const Statement& { return *then_statement_; }
  113. auto then_statement() -> Statement& { return *then_statement_; }
  114. auto else_statement() const -> std::optional<Nonnull<const Statement*>> {
  115. return else_statement_;
  116. }
  117. auto else_statement() -> std::optional<Nonnull<Statement*>> {
  118. return else_statement_;
  119. }
  120. private:
  121. Nonnull<Expression*> condition_;
  122. Nonnull<Statement*> then_statement_;
  123. std::optional<Nonnull<Statement*>> else_statement_;
  124. };
  125. class Return : public Statement {
  126. public:
  127. Return(Nonnull<Arena*> arena, SourceLocation source_loc)
  128. : Return(source_loc, arena->New<TupleLiteral>(source_loc), true) {}
  129. Return(SourceLocation source_loc, Nonnull<Expression*> expression,
  130. bool is_omitted_expression)
  131. : Statement(Kind::Return, source_loc),
  132. expression_(expression),
  133. is_omitted_expression_(is_omitted_expression) {}
  134. static auto classof(const Statement* stmt) -> bool {
  135. return stmt->kind() == Kind::Return;
  136. }
  137. auto expression() const -> const Expression& { return *expression_; }
  138. auto expression() -> Expression& { return *expression_; }
  139. auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
  140. private:
  141. Nonnull<Expression*> expression_;
  142. bool is_omitted_expression_;
  143. };
  144. class Sequence : public Statement {
  145. public:
  146. Sequence(SourceLocation source_loc, Nonnull<Statement*> statement,
  147. std::optional<Nonnull<Statement*>> next)
  148. : Statement(Kind::Sequence, source_loc),
  149. statement_(statement),
  150. next_(next) {}
  151. static auto classof(const Statement* stmt) -> bool {
  152. return stmt->kind() == Kind::Sequence;
  153. }
  154. auto statement() const -> const Statement& { return *statement_; }
  155. auto statement() -> Statement& { return *statement_; }
  156. auto next() const -> std::optional<Nonnull<const Statement*>> {
  157. return next_;
  158. }
  159. auto next() -> std::optional<Nonnull<Statement*>> { return next_; }
  160. private:
  161. Nonnull<Statement*> statement_;
  162. std::optional<Nonnull<Statement*>> next_;
  163. };
  164. class Block : public Statement {
  165. public:
  166. Block(SourceLocation source_loc, std::optional<Nonnull<Statement*>> statement)
  167. : Statement(Kind::Block, source_loc), statement_(statement) {}
  168. static auto classof(const Statement* stmt) -> bool {
  169. return stmt->kind() == Kind::Block;
  170. }
  171. auto statement() const -> std::optional<Nonnull<const Statement*>> {
  172. return statement_;
  173. }
  174. auto statement() -> std::optional<Nonnull<Statement*>> { return statement_; }
  175. private:
  176. std::optional<Nonnull<Statement*>> statement_;
  177. };
  178. class While : public Statement {
  179. public:
  180. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  181. Nonnull<Statement*> body)
  182. : Statement(Kind::While, source_loc),
  183. condition_(condition),
  184. body_(body) {}
  185. static auto classof(const Statement* stmt) -> bool {
  186. return stmt->kind() == Kind::While;
  187. }
  188. auto condition() const -> const Expression& { return *condition_; }
  189. auto condition() -> Expression& { return *condition_; }
  190. auto body() const -> const Statement& { return *body_; }
  191. auto body() -> Statement& { return *body_; }
  192. private:
  193. Nonnull<Expression*> condition_;
  194. Nonnull<Statement*> body_;
  195. };
  196. class Break : public Statement {
  197. public:
  198. explicit Break(SourceLocation source_loc)
  199. : Statement(Kind::Break, source_loc) {}
  200. static auto classof(const Statement* stmt) -> bool {
  201. return stmt->kind() == Kind::Break;
  202. }
  203. };
  204. class Continue : public Statement {
  205. public:
  206. explicit Continue(SourceLocation source_loc)
  207. : Statement(Kind::Continue, source_loc) {}
  208. static auto classof(const Statement* stmt) -> bool {
  209. return stmt->kind() == Kind::Continue;
  210. }
  211. };
  212. class Match : public Statement {
  213. public:
  214. class Clause {
  215. public:
  216. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  217. : pattern_(pattern), statement_(statement) {}
  218. auto pattern() const -> const Pattern& { return *pattern_; }
  219. auto pattern() -> Pattern& { return *pattern_; }
  220. auto statement() const -> const Statement& { return *statement_; }
  221. auto statement() -> Statement& { return *statement_; }
  222. private:
  223. Nonnull<Pattern*> pattern_;
  224. Nonnull<Statement*> statement_;
  225. };
  226. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  227. std::vector<Clause> clauses)
  228. : Statement(Kind::Match, source_loc),
  229. expression_(expression),
  230. clauses_(std::move(clauses)) {}
  231. static auto classof(const Statement* stmt) -> bool {
  232. return stmt->kind() == Kind::Match;
  233. }
  234. auto expression() const -> const Expression& { return *expression_; }
  235. auto expression() -> Expression& { return *expression_; }
  236. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  237. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  238. private:
  239. Nonnull<Expression*> expression_;
  240. std::vector<Clause> clauses_;
  241. };
  242. // A continuation statement.
  243. //
  244. // __continuation <continuation_variable> {
  245. // <body>
  246. // }
  247. class Continuation : public Statement {
  248. public:
  249. Continuation(SourceLocation source_loc, std::string continuation_variable,
  250. Nonnull<Statement*> body)
  251. : Statement(Kind::Continuation, source_loc),
  252. continuation_variable_(std::move(continuation_variable)),
  253. body_(body) {}
  254. static auto classof(const Statement* stmt) -> bool {
  255. return stmt->kind() == Kind::Continuation;
  256. }
  257. auto continuation_variable() const -> const std::string& {
  258. return continuation_variable_;
  259. }
  260. auto body() const -> const Statement& { return *body_; }
  261. auto body() -> Statement& { return *body_; }
  262. private:
  263. std::string continuation_variable_;
  264. Nonnull<Statement*> body_;
  265. };
  266. // A run statement.
  267. //
  268. // __run <argument>;
  269. class Run : public Statement {
  270. public:
  271. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  272. : Statement(Kind::Run, source_loc), argument_(argument) {}
  273. static auto classof(const Statement* stmt) -> bool {
  274. return stmt->kind() == Kind::Run;
  275. }
  276. auto argument() const -> const Expression& { return *argument_; }
  277. auto argument() -> Expression& { return *argument_; }
  278. private:
  279. Nonnull<Expression*> argument_;
  280. };
  281. // An await statement.
  282. //
  283. // __await;
  284. class Await : public Statement {
  285. public:
  286. explicit Await(SourceLocation source_loc)
  287. : Statement(Kind::Await, source_loc) {}
  288. static auto classof(const Statement* stmt) -> bool {
  289. return stmt->kind() == Kind::Await;
  290. }
  291. };
  292. } // namespace Carbon
  293. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_