statement.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 FunctionDeclaration;
  16. class Statement {
  17. public:
  18. enum class Kind {
  19. ExpressionStatement,
  20. Assign,
  21. VariableDefinition,
  22. If,
  23. Return,
  24. Sequence,
  25. Block,
  26. While,
  27. Break,
  28. Continue,
  29. Match,
  30. Continuation, // Create a first-class continuation.
  31. Run, // Run a continuation to the next await or until it finishes.
  32. Await, // Pause execution of the continuation.
  33. };
  34. void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); }
  35. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  36. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  37. // Returns the enumerator corresponding to the most-derived type of this
  38. // object.
  39. auto kind() const -> Kind { return kind_; }
  40. auto source_loc() const -> SourceLocation { return source_loc_; }
  41. protected:
  42. // Constructs an Statement representing syntax at the given line number.
  43. // `kind` must be the enumerator corresponding to the most-derived type being
  44. // constructed.
  45. Statement(Kind kind, SourceLocation source_loc)
  46. : kind_(kind), source_loc_(source_loc) {}
  47. private:
  48. const Kind kind_;
  49. SourceLocation source_loc_;
  50. };
  51. class ExpressionStatement : public Statement {
  52. public:
  53. ExpressionStatement(SourceLocation source_loc,
  54. Nonnull<Expression*> expression)
  55. : Statement(Kind::ExpressionStatement, source_loc),
  56. expression_(expression) {}
  57. static auto classof(const Statement* stmt) -> bool {
  58. return stmt->kind() == Kind::ExpressionStatement;
  59. }
  60. auto expression() const -> const Expression& { return *expression_; }
  61. auto expression() -> Expression& { return *expression_; }
  62. private:
  63. Nonnull<Expression*> expression_;
  64. };
  65. class Assign : public Statement {
  66. public:
  67. Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
  68. Nonnull<Expression*> rhs)
  69. : Statement(Kind::Assign, source_loc), lhs_(lhs), rhs_(rhs) {}
  70. static auto classof(const Statement* stmt) -> bool {
  71. return stmt->kind() == Kind::Assign;
  72. }
  73. auto lhs() const -> const Expression& { return *lhs_; }
  74. auto lhs() -> Expression& { return *lhs_; }
  75. auto rhs() const -> const Expression& { return *rhs_; }
  76. auto rhs() -> Expression& { return *rhs_; }
  77. private:
  78. Nonnull<Expression*> lhs_;
  79. Nonnull<Expression*> rhs_;
  80. };
  81. class VariableDefinition : public Statement {
  82. public:
  83. VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
  84. Nonnull<Expression*> init)
  85. : Statement(Kind::VariableDefinition, source_loc),
  86. pattern_(pattern),
  87. init_(init) {}
  88. static auto classof(const Statement* stmt) -> bool {
  89. return stmt->kind() == Kind::VariableDefinition;
  90. }
  91. auto pattern() const -> const Pattern& { return *pattern_; }
  92. auto pattern() -> Pattern& { return *pattern_; }
  93. auto init() const -> const Expression& { return *init_; }
  94. auto init() -> Expression& { return *init_; }
  95. private:
  96. Nonnull<Pattern*> pattern_;
  97. Nonnull<Expression*> init_;
  98. };
  99. class If : public Statement {
  100. public:
  101. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  102. Nonnull<Statement*> then_statement,
  103. std::optional<Nonnull<Statement*>> else_statement)
  104. : Statement(Kind::If, source_loc),
  105. condition_(condition),
  106. then_statement_(then_statement),
  107. else_statement_(else_statement) {}
  108. static auto classof(const Statement* stmt) -> bool {
  109. return stmt->kind() == Kind::If;
  110. }
  111. auto condition() const -> const Expression& { return *condition_; }
  112. auto condition() -> Expression& { return *condition_; }
  113. auto then_statement() const -> const Statement& { return *then_statement_; }
  114. auto then_statement() -> Statement& { return *then_statement_; }
  115. auto else_statement() const -> std::optional<Nonnull<const Statement*>> {
  116. return else_statement_;
  117. }
  118. auto else_statement() -> std::optional<Nonnull<Statement*>> {
  119. return else_statement_;
  120. }
  121. private:
  122. Nonnull<Expression*> condition_;
  123. Nonnull<Statement*> then_statement_;
  124. std::optional<Nonnull<Statement*>> else_statement_;
  125. };
  126. class Return : public Statement {
  127. public:
  128. Return(Nonnull<Arena*> arena, SourceLocation source_loc)
  129. : Return(source_loc, arena->New<TupleLiteral>(source_loc), true) {}
  130. Return(SourceLocation source_loc, Nonnull<Expression*> expression,
  131. bool is_omitted_expression)
  132. : Statement(Kind::Return, source_loc),
  133. expression_(expression),
  134. is_omitted_expression_(is_omitted_expression) {}
  135. static auto classof(const Statement* stmt) -> bool {
  136. return stmt->kind() == Kind::Return;
  137. }
  138. auto expression() const -> const Expression& { return *expression_; }
  139. auto expression() -> Expression& { return *expression_; }
  140. auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
  141. // The AST node representing the function body this statement returns from.
  142. // Can only be called after ResolveControlFlow has visited this node.
  143. //
  144. // Note that this function does not represent an edge in the tree
  145. // structure of the AST: the return value is not a child of this node,
  146. // but an ancestor.
  147. auto function() const -> const FunctionDeclaration& { return **function_; }
  148. // Can only be called once, by ResolveControlFlow.
  149. void set_function(Nonnull<const FunctionDeclaration*> function) {
  150. CHECK(!function_.has_value());
  151. function_ = function;
  152. }
  153. private:
  154. Nonnull<Expression*> expression_;
  155. bool is_omitted_expression_;
  156. std::optional<Nonnull<const FunctionDeclaration*>> function_;
  157. };
  158. class Sequence : public Statement {
  159. public:
  160. Sequence(SourceLocation source_loc, Nonnull<Statement*> statement,
  161. std::optional<Nonnull<Statement*>> next)
  162. : Statement(Kind::Sequence, source_loc),
  163. statement_(statement),
  164. next_(next) {}
  165. static auto classof(const Statement* stmt) -> bool {
  166. return stmt->kind() == Kind::Sequence;
  167. }
  168. auto statement() const -> const Statement& { return *statement_; }
  169. auto statement() -> Statement& { return *statement_; }
  170. auto next() const -> std::optional<Nonnull<const Statement*>> {
  171. return next_;
  172. }
  173. auto next() -> std::optional<Nonnull<Statement*>> { return next_; }
  174. private:
  175. Nonnull<Statement*> statement_;
  176. std::optional<Nonnull<Statement*>> next_;
  177. };
  178. class Block : public Statement {
  179. public:
  180. Block(SourceLocation source_loc, std::optional<Nonnull<Statement*>> statement)
  181. : Statement(Kind::Block, source_loc), statement_(statement) {}
  182. static auto classof(const Statement* stmt) -> bool {
  183. return stmt->kind() == Kind::Block;
  184. }
  185. auto statement() const -> std::optional<Nonnull<const Statement*>> {
  186. return statement_;
  187. }
  188. auto statement() -> std::optional<Nonnull<Statement*>> { return statement_; }
  189. private:
  190. std::optional<Nonnull<Statement*>> statement_;
  191. };
  192. class While : public Statement {
  193. public:
  194. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  195. Nonnull<Statement*> body)
  196. : Statement(Kind::While, source_loc),
  197. condition_(condition),
  198. body_(body) {}
  199. static auto classof(const Statement* stmt) -> bool {
  200. return stmt->kind() == Kind::While;
  201. }
  202. auto condition() const -> const Expression& { return *condition_; }
  203. auto condition() -> Expression& { return *condition_; }
  204. auto body() const -> const Statement& { return *body_; }
  205. auto body() -> Statement& { return *body_; }
  206. private:
  207. Nonnull<Expression*> condition_;
  208. Nonnull<Statement*> body_;
  209. };
  210. class Break : public Statement {
  211. public:
  212. explicit Break(SourceLocation source_loc)
  213. : Statement(Kind::Break, source_loc) {}
  214. static auto classof(const Statement* stmt) -> bool {
  215. return stmt->kind() == Kind::Break;
  216. }
  217. // The AST node representing the loop this statement breaks out of.
  218. // Can only be called after ResolveControlFlow has visited this node.
  219. //
  220. // Note that this function does not represent an edge in the tree
  221. // structure of the AST: the return value is not a child of this node,
  222. // but an ancestor.
  223. auto loop() const -> const Statement& { return **loop_; }
  224. // Can only be called once, by ResolveControlFlow.
  225. void set_loop(Nonnull<const Statement*> loop) {
  226. CHECK(!loop_.has_value());
  227. loop_ = loop;
  228. }
  229. private:
  230. std::optional<Nonnull<const Statement*>> loop_;
  231. };
  232. class Continue : public Statement {
  233. public:
  234. explicit Continue(SourceLocation source_loc)
  235. : Statement(Kind::Continue, source_loc) {}
  236. static auto classof(const Statement* stmt) -> bool {
  237. return stmt->kind() == Kind::Continue;
  238. }
  239. // The AST node representing the loop this statement continues.
  240. // Can only be called after ResolveControlFlow has visited this node.
  241. //
  242. // Note that this function does not represent an edge in the tree
  243. // structure of the AST: the return value is not a child of this node,
  244. // but an ancestor.
  245. auto loop() const -> const Statement& { return **loop_; }
  246. // Can only be called once, by ResolveControlFlow.
  247. void set_loop(Nonnull<const Statement*> loop) {
  248. CHECK(!loop_.has_value());
  249. loop_ = loop;
  250. }
  251. private:
  252. std::optional<Nonnull<const Statement*>> loop_;
  253. };
  254. class Match : public Statement {
  255. public:
  256. class Clause {
  257. public:
  258. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  259. : pattern_(pattern), statement_(statement) {}
  260. auto pattern() const -> const Pattern& { return *pattern_; }
  261. auto pattern() -> Pattern& { return *pattern_; }
  262. auto statement() const -> const Statement& { return *statement_; }
  263. auto statement() -> Statement& { return *statement_; }
  264. private:
  265. Nonnull<Pattern*> pattern_;
  266. Nonnull<Statement*> statement_;
  267. };
  268. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  269. std::vector<Clause> clauses)
  270. : Statement(Kind::Match, source_loc),
  271. expression_(expression),
  272. clauses_(std::move(clauses)) {}
  273. static auto classof(const Statement* stmt) -> bool {
  274. return stmt->kind() == Kind::Match;
  275. }
  276. auto expression() const -> const Expression& { return *expression_; }
  277. auto expression() -> Expression& { return *expression_; }
  278. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  279. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  280. private:
  281. Nonnull<Expression*> expression_;
  282. std::vector<Clause> clauses_;
  283. };
  284. // A continuation statement.
  285. //
  286. // __continuation <continuation_variable> {
  287. // <body>
  288. // }
  289. class Continuation : public Statement {
  290. public:
  291. Continuation(SourceLocation source_loc, std::string continuation_variable,
  292. Nonnull<Statement*> body)
  293. : Statement(Kind::Continuation, source_loc),
  294. continuation_variable_(std::move(continuation_variable)),
  295. body_(body) {}
  296. static auto classof(const Statement* stmt) -> bool {
  297. return stmt->kind() == Kind::Continuation;
  298. }
  299. auto continuation_variable() const -> const std::string& {
  300. return continuation_variable_;
  301. }
  302. auto body() const -> const Statement& { return *body_; }
  303. auto body() -> Statement& { return *body_; }
  304. private:
  305. std::string continuation_variable_;
  306. Nonnull<Statement*> body_;
  307. };
  308. // A run statement.
  309. //
  310. // __run <argument>;
  311. class Run : public Statement {
  312. public:
  313. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  314. : Statement(Kind::Run, source_loc), argument_(argument) {}
  315. static auto classof(const Statement* stmt) -> bool {
  316. return stmt->kind() == Kind::Run;
  317. }
  318. auto argument() const -> const Expression& { return *argument_; }
  319. auto argument() -> Expression& { return *argument_; }
  320. private:
  321. Nonnull<Expression*> argument_;
  322. };
  323. // An await statement.
  324. //
  325. // __await;
  326. class Await : public Statement {
  327. public:
  328. explicit Await(SourceLocation source_loc)
  329. : Statement(Kind::Await, source_loc) {}
  330. static auto classof(const Statement* stmt) -> bool {
  331. return stmt->kind() == Kind::Await;
  332. }
  333. };
  334. } // namespace Carbon
  335. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_