statement.h 12 KB

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