statement.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. 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 Block : public Statement {
  51. public:
  52. Block(SourceLocation source_loc, std::vector<Nonnull<Statement*>> statements)
  53. : Statement(Kind::Block, source_loc), statements_(statements) {}
  54. static auto classof(const Statement* stmt) -> bool {
  55. return stmt->kind() == Kind::Block;
  56. }
  57. auto statements() const -> llvm::ArrayRef<Nonnull<const Statement*>> {
  58. return statements_;
  59. }
  60. auto statements() -> llvm::MutableArrayRef<Nonnull<Statement*>> {
  61. return statements_;
  62. }
  63. private:
  64. std::vector<Nonnull<Statement*>> statements_;
  65. };
  66. class ExpressionStatement : public Statement {
  67. public:
  68. ExpressionStatement(SourceLocation source_loc,
  69. Nonnull<Expression*> expression)
  70. : Statement(Kind::ExpressionStatement, source_loc),
  71. expression_(expression) {}
  72. static auto classof(const Statement* stmt) -> bool {
  73. return stmt->kind() == Kind::ExpressionStatement;
  74. }
  75. auto expression() const -> const Expression& { return *expression_; }
  76. auto expression() -> Expression& { return *expression_; }
  77. private:
  78. Nonnull<Expression*> expression_;
  79. };
  80. class Assign : public Statement {
  81. public:
  82. Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
  83. Nonnull<Expression*> rhs)
  84. : Statement(Kind::Assign, source_loc), lhs_(lhs), rhs_(rhs) {}
  85. static auto classof(const Statement* stmt) -> bool {
  86. return stmt->kind() == Kind::Assign;
  87. }
  88. auto lhs() const -> const Expression& { return *lhs_; }
  89. auto lhs() -> Expression& { return *lhs_; }
  90. auto rhs() const -> const Expression& { return *rhs_; }
  91. auto rhs() -> Expression& { return *rhs_; }
  92. private:
  93. Nonnull<Expression*> lhs_;
  94. Nonnull<Expression*> rhs_;
  95. };
  96. class VariableDefinition : public Statement {
  97. public:
  98. VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
  99. Nonnull<Expression*> init)
  100. : Statement(Kind::VariableDefinition, source_loc),
  101. pattern_(pattern),
  102. init_(init) {}
  103. static auto classof(const Statement* stmt) -> bool {
  104. return stmt->kind() == Kind::VariableDefinition;
  105. }
  106. auto pattern() const -> const Pattern& { return *pattern_; }
  107. auto pattern() -> Pattern& { return *pattern_; }
  108. auto init() const -> const Expression& { return *init_; }
  109. auto init() -> Expression& { return *init_; }
  110. private:
  111. Nonnull<Pattern*> pattern_;
  112. Nonnull<Expression*> init_;
  113. };
  114. class If : public Statement {
  115. public:
  116. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  117. Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
  118. : Statement(Kind::If, source_loc),
  119. condition_(condition),
  120. then_block_(then_block),
  121. else_block_(else_block) {}
  122. static auto classof(const Statement* stmt) -> bool {
  123. return stmt->kind() == Kind::If;
  124. }
  125. auto condition() const -> const Expression& { return *condition_; }
  126. auto condition() -> Expression& { return *condition_; }
  127. auto then_block() const -> const Block& { return *then_block_; }
  128. auto then_block() -> Block& { return *then_block_; }
  129. auto else_block() const -> std::optional<Nonnull<const Block*>> {
  130. return else_block_;
  131. }
  132. auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
  133. private:
  134. Nonnull<Expression*> condition_;
  135. Nonnull<Block*> then_block_;
  136. std::optional<Nonnull<Block*>> else_block_;
  137. };
  138. class Return : public Statement {
  139. public:
  140. Return(Nonnull<Arena*> arena, SourceLocation source_loc)
  141. : Return(source_loc, arena->New<TupleLiteral>(source_loc), true) {}
  142. Return(SourceLocation source_loc, Nonnull<Expression*> expression,
  143. bool is_omitted_expression)
  144. : Statement(Kind::Return, source_loc),
  145. expression_(expression),
  146. is_omitted_expression_(is_omitted_expression) {}
  147. static auto classof(const Statement* stmt) -> bool {
  148. return stmt->kind() == Kind::Return;
  149. }
  150. auto expression() const -> const Expression& { return *expression_; }
  151. auto expression() -> Expression& { return *expression_; }
  152. auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
  153. // The AST node representing the function body this statement returns from.
  154. // Can only be called after ResolveControlFlow has visited this node.
  155. //
  156. // Note that this function does not represent an edge in the tree
  157. // structure of the AST: the return value is not a child of this node,
  158. // but an ancestor.
  159. auto function() const -> const FunctionDeclaration& { return **function_; }
  160. // Can only be called once, by ResolveControlFlow.
  161. void set_function(Nonnull<const FunctionDeclaration*> function) {
  162. CHECK(!function_.has_value());
  163. function_ = function;
  164. }
  165. private:
  166. Nonnull<Expression*> expression_;
  167. bool is_omitted_expression_;
  168. std::optional<Nonnull<const FunctionDeclaration*>> function_;
  169. };
  170. class While : public Statement {
  171. public:
  172. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  173. Nonnull<Block*> body)
  174. : Statement(Kind::While, source_loc),
  175. condition_(condition),
  176. body_(body) {}
  177. static auto classof(const Statement* stmt) -> bool {
  178. return stmt->kind() == Kind::While;
  179. }
  180. auto condition() const -> const Expression& { return *condition_; }
  181. auto condition() -> Expression& { return *condition_; }
  182. auto body() const -> const Block& { return *body_; }
  183. auto body() -> Block& { return *body_; }
  184. private:
  185. Nonnull<Expression*> condition_;
  186. Nonnull<Block*> body_;
  187. };
  188. class Break : public Statement {
  189. public:
  190. explicit Break(SourceLocation source_loc)
  191. : Statement(Kind::Break, source_loc) {}
  192. static auto classof(const Statement* stmt) -> bool {
  193. return stmt->kind() == Kind::Break;
  194. }
  195. // The AST node representing the loop this statement breaks out of.
  196. // Can only be called after ResolveControlFlow has visited this node.
  197. //
  198. // Note that this function does not represent an edge in the tree
  199. // structure of the AST: the return value is not a child of this node,
  200. // but an ancestor.
  201. auto loop() const -> const Statement& { return **loop_; }
  202. // Can only be called once, by ResolveControlFlow.
  203. void set_loop(Nonnull<const Statement*> loop) {
  204. CHECK(!loop_.has_value());
  205. loop_ = loop;
  206. }
  207. private:
  208. std::optional<Nonnull<const Statement*>> loop_;
  209. };
  210. class Continue : public Statement {
  211. public:
  212. explicit Continue(SourceLocation source_loc)
  213. : Statement(Kind::Continue, source_loc) {}
  214. static auto classof(const Statement* stmt) -> bool {
  215. return stmt->kind() == Kind::Continue;
  216. }
  217. // The AST node representing the loop this statement continues.
  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 Match : public Statement {
  233. public:
  234. class Clause {
  235. public:
  236. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  237. : pattern_(pattern), statement_(statement) {}
  238. auto pattern() const -> const Pattern& { return *pattern_; }
  239. auto pattern() -> Pattern& { return *pattern_; }
  240. auto statement() const -> const Statement& { return *statement_; }
  241. auto statement() -> Statement& { return *statement_; }
  242. private:
  243. Nonnull<Pattern*> pattern_;
  244. Nonnull<Statement*> statement_;
  245. };
  246. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  247. std::vector<Clause> clauses)
  248. : Statement(Kind::Match, source_loc),
  249. expression_(expression),
  250. clauses_(std::move(clauses)) {}
  251. static auto classof(const Statement* stmt) -> bool {
  252. return stmt->kind() == Kind::Match;
  253. }
  254. auto expression() const -> const Expression& { return *expression_; }
  255. auto expression() -> Expression& { return *expression_; }
  256. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  257. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  258. private:
  259. Nonnull<Expression*> expression_;
  260. std::vector<Clause> clauses_;
  261. };
  262. // A continuation statement.
  263. //
  264. // __continuation <continuation_variable> {
  265. // <body>
  266. // }
  267. class Continuation : public Statement {
  268. public:
  269. Continuation(SourceLocation source_loc, std::string continuation_variable,
  270. Nonnull<Block*> body)
  271. : Statement(Kind::Continuation, source_loc),
  272. continuation_variable_(std::move(continuation_variable)),
  273. body_(body) {}
  274. static auto classof(const Statement* stmt) -> bool {
  275. return stmt->kind() == Kind::Continuation;
  276. }
  277. auto continuation_variable() const -> const std::string& {
  278. return continuation_variable_;
  279. }
  280. auto body() const -> const Block& { return *body_; }
  281. auto body() -> Block& { return *body_; }
  282. private:
  283. std::string continuation_variable_;
  284. Nonnull<Block*> body_;
  285. };
  286. // A run statement.
  287. //
  288. // __run <argument>;
  289. class Run : public Statement {
  290. public:
  291. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  292. : Statement(Kind::Run, source_loc), argument_(argument) {}
  293. static auto classof(const Statement* stmt) -> bool {
  294. return stmt->kind() == Kind::Run;
  295. }
  296. auto argument() const -> const Expression& { return *argument_; }
  297. auto argument() -> Expression& { return *argument_; }
  298. private:
  299. Nonnull<Expression*> argument_;
  300. };
  301. // An await statement.
  302. //
  303. // __await;
  304. class Await : public Statement {
  305. public:
  306. explicit Await(SourceLocation source_loc)
  307. : Statement(Kind::Await, source_loc) {}
  308. static auto classof(const Statement* stmt) -> bool {
  309. return stmt->kind() == Kind::Await;
  310. }
  311. };
  312. } // namespace Carbon
  313. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_