statement.h 12 KB

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