statement.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/ast/static_scope.h"
  12. #include "executable_semantics/common/arena.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/Support/Compiler.h"
  15. namespace Carbon {
  16. class FunctionDeclaration;
  17. class StaticScope;
  18. class Statement : public virtual AstNode {
  19. public:
  20. ~Statement() override = 0;
  21. void Print(llvm::raw_ostream& out) const { PrintDepth(-1, out); }
  22. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  23. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  24. static auto classof(const AstNode* node) {
  25. return InheritsFromStatement(node->kind());
  26. }
  27. // Returns the enumerator corresponding to the most-derived type of this
  28. // object.
  29. auto kind() const -> StatementKind {
  30. return static_cast<StatementKind>(root_kind());
  31. }
  32. protected:
  33. Statement() = default;
  34. };
  35. class Block : public Statement {
  36. public:
  37. Block(SourceLocation source_loc, std::vector<Nonnull<Statement*>> statements)
  38. : AstNode(AstNodeKind::Block, source_loc), statements_(statements) {}
  39. static auto classof(const AstNode* node) -> bool {
  40. return InheritsFromBlock(node->kind());
  41. }
  42. auto statements() const -> llvm::ArrayRef<Nonnull<const Statement*>> {
  43. return statements_;
  44. }
  45. auto statements() -> llvm::MutableArrayRef<Nonnull<Statement*>> {
  46. return statements_;
  47. }
  48. auto static_scope() const -> const StaticScope& { return static_scope_; }
  49. auto static_scope() -> StaticScope& { return static_scope_; }
  50. private:
  51. std::vector<Nonnull<Statement*>> statements_;
  52. StaticScope static_scope_;
  53. };
  54. class ExpressionStatement : public Statement {
  55. public:
  56. ExpressionStatement(SourceLocation source_loc,
  57. Nonnull<Expression*> expression)
  58. : AstNode(AstNodeKind::ExpressionStatement, source_loc),
  59. expression_(expression) {}
  60. static auto classof(const AstNode* node) -> bool {
  61. return InheritsFromExpressionStatement(node->kind());
  62. }
  63. auto expression() const -> const Expression& { return *expression_; }
  64. auto expression() -> Expression& { return *expression_; }
  65. private:
  66. Nonnull<Expression*> expression_;
  67. };
  68. class Assign : public Statement {
  69. public:
  70. Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
  71. Nonnull<Expression*> rhs)
  72. : AstNode(AstNodeKind::Assign, source_loc), lhs_(lhs), rhs_(rhs) {}
  73. static auto classof(const AstNode* node) -> bool {
  74. return InheritsFromAssign(node->kind());
  75. }
  76. auto lhs() const -> const Expression& { return *lhs_; }
  77. auto lhs() -> Expression& { return *lhs_; }
  78. auto rhs() const -> const Expression& { return *rhs_; }
  79. auto rhs() -> Expression& { return *rhs_; }
  80. private:
  81. Nonnull<Expression*> lhs_;
  82. Nonnull<Expression*> rhs_;
  83. };
  84. class VariableDefinition : public Statement {
  85. public:
  86. VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
  87. Nonnull<Expression*> init)
  88. : AstNode(AstNodeKind::VariableDefinition, source_loc),
  89. pattern_(pattern),
  90. init_(init) {}
  91. static auto classof(const AstNode* node) -> bool {
  92. return InheritsFromVariableDefinition(node->kind());
  93. }
  94. auto pattern() const -> const Pattern& { return *pattern_; }
  95. auto pattern() -> Pattern& { return *pattern_; }
  96. auto init() const -> const Expression& { return *init_; }
  97. auto init() -> Expression& { return *init_; }
  98. private:
  99. Nonnull<Pattern*> pattern_;
  100. Nonnull<Expression*> init_;
  101. };
  102. class If : public Statement {
  103. public:
  104. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  105. Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
  106. : AstNode(AstNodeKind::If, source_loc),
  107. condition_(condition),
  108. then_block_(then_block),
  109. else_block_(else_block) {}
  110. static auto classof(const AstNode* node) -> bool {
  111. return InheritsFromIf(node->kind());
  112. }
  113. auto condition() const -> const Expression& { return *condition_; }
  114. auto condition() -> Expression& { return *condition_; }
  115. auto then_block() const -> const Block& { return *then_block_; }
  116. auto then_block() -> Block& { return *then_block_; }
  117. auto else_block() const -> std::optional<Nonnull<const Block*>> {
  118. return else_block_;
  119. }
  120. auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
  121. private:
  122. Nonnull<Expression*> condition_;
  123. Nonnull<Block*> then_block_;
  124. std::optional<Nonnull<Block*>> else_block_;
  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. : AstNode(AstNodeKind::Return, source_loc),
  133. expression_(expression),
  134. is_omitted_expression_(is_omitted_expression) {}
  135. static auto classof(const AstNode* node) -> bool {
  136. return InheritsFromReturn(node->kind());
  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. auto function() -> FunctionDeclaration& { return **function_; }
  149. // Can only be called once, by ResolveControlFlow.
  150. void set_function(Nonnull<FunctionDeclaration*> function) {
  151. CHECK(!function_.has_value());
  152. function_ = function;
  153. }
  154. private:
  155. Nonnull<Expression*> expression_;
  156. bool is_omitted_expression_;
  157. std::optional<Nonnull<FunctionDeclaration*>> function_;
  158. };
  159. class While : public Statement {
  160. public:
  161. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  162. Nonnull<Block*> body)
  163. : AstNode(AstNodeKind::While, source_loc),
  164. condition_(condition),
  165. body_(body) {}
  166. static auto classof(const AstNode* node) -> bool {
  167. return InheritsFromWhile(node->kind());
  168. }
  169. auto condition() const -> const Expression& { return *condition_; }
  170. auto condition() -> Expression& { return *condition_; }
  171. auto body() const -> const Block& { return *body_; }
  172. auto body() -> Block& { return *body_; }
  173. private:
  174. Nonnull<Expression*> condition_;
  175. Nonnull<Block*> body_;
  176. };
  177. class Break : public Statement {
  178. public:
  179. explicit Break(SourceLocation source_loc)
  180. : AstNode(AstNodeKind::Break, source_loc) {}
  181. static auto classof(const AstNode* node) -> bool {
  182. return InheritsFromBreak(node->kind());
  183. }
  184. // The AST node representing the loop this statement breaks out of.
  185. // Can only be called after ResolveControlFlow has visited this node.
  186. //
  187. // Note that this function does not represent an edge in the tree
  188. // structure of the AST: the return value is not a child of this node,
  189. // but an ancestor.
  190. auto loop() const -> const Statement& { return **loop_; }
  191. // Can only be called once, by ResolveControlFlow.
  192. void set_loop(Nonnull<const Statement*> loop) {
  193. CHECK(!loop_.has_value());
  194. loop_ = loop;
  195. }
  196. private:
  197. std::optional<Nonnull<const Statement*>> loop_;
  198. };
  199. class Continue : public Statement {
  200. public:
  201. explicit Continue(SourceLocation source_loc)
  202. : AstNode(AstNodeKind::Continue, source_loc) {}
  203. static auto classof(const AstNode* node) -> bool {
  204. return InheritsFromContinue(node->kind());
  205. }
  206. // The AST node representing the loop this statement continues.
  207. // Can only be called after ResolveControlFlow has visited this node.
  208. //
  209. // Note that this function does not represent an edge in the tree
  210. // structure of the AST: the return value is not a child of this node,
  211. // but an ancestor.
  212. auto loop() const -> const Statement& { return **loop_; }
  213. // Can only be called once, by ResolveControlFlow.
  214. void set_loop(Nonnull<const Statement*> loop) {
  215. CHECK(!loop_.has_value());
  216. loop_ = loop;
  217. }
  218. private:
  219. std::optional<Nonnull<const Statement*>> loop_;
  220. };
  221. class Match : public Statement {
  222. public:
  223. class Clause {
  224. public:
  225. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  226. : pattern_(pattern), statement_(statement) {}
  227. auto pattern() const -> const Pattern& { return *pattern_; }
  228. auto pattern() -> Pattern& { return *pattern_; }
  229. auto statement() const -> const Statement& { return *statement_; }
  230. auto statement() -> Statement& { return *statement_; }
  231. // Contains names for the pattern and statement. Note that when the
  232. // statement is a block, it gains its own scope.
  233. auto static_scope() const -> const StaticScope& { return static_scope_; }
  234. auto static_scope() -> StaticScope& { return static_scope_; }
  235. private:
  236. Nonnull<Pattern*> pattern_;
  237. Nonnull<Statement*> statement_;
  238. StaticScope static_scope_;
  239. };
  240. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  241. std::vector<Clause> clauses)
  242. : AstNode(AstNodeKind::Match, source_loc),
  243. expression_(expression),
  244. clauses_(std::move(clauses)) {}
  245. static auto classof(const AstNode* node) -> bool {
  246. return InheritsFromMatch(node->kind());
  247. }
  248. auto expression() const -> const Expression& { return *expression_; }
  249. auto expression() -> Expression& { return *expression_; }
  250. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  251. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  252. private:
  253. Nonnull<Expression*> expression_;
  254. std::vector<Clause> clauses_;
  255. };
  256. // A continuation statement.
  257. //
  258. // __continuation <continuation_variable> {
  259. // <body>
  260. // }
  261. class Continuation : public Statement, public NamedEntity {
  262. public:
  263. Continuation(SourceLocation source_loc, std::string continuation_variable,
  264. Nonnull<Block*> body)
  265. : AstNode(AstNodeKind::Continuation, source_loc),
  266. continuation_variable_(std::move(continuation_variable)),
  267. body_(body) {}
  268. static auto classof(const AstNode* node) -> bool {
  269. return InheritsFromContinuation(node->kind());
  270. }
  271. auto continuation_variable() const -> const std::string& {
  272. return continuation_variable_;
  273. }
  274. auto body() const -> const Block& { return *body_; }
  275. auto body() -> Block& { return *body_; }
  276. private:
  277. std::string continuation_variable_;
  278. Nonnull<Block*> body_;
  279. };
  280. // A run statement.
  281. //
  282. // __run <argument>;
  283. class Run : public Statement {
  284. public:
  285. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  286. : AstNode(AstNodeKind::Run, source_loc), argument_(argument) {}
  287. static auto classof(const AstNode* node) -> bool {
  288. return InheritsFromRun(node->kind());
  289. }
  290. auto argument() const -> const Expression& { return *argument_; }
  291. auto argument() -> Expression& { return *argument_; }
  292. private:
  293. Nonnull<Expression*> argument_;
  294. };
  295. // An await statement.
  296. //
  297. // __await;
  298. class Await : public Statement {
  299. public:
  300. explicit Await(SourceLocation source_loc)
  301. : AstNode(AstNodeKind::Await, source_loc) {}
  302. static auto classof(const AstNode* node) -> bool {
  303. return InheritsFromAwait(node->kind());
  304. }
  305. };
  306. } // namespace Carbon
  307. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_