statement.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <utility>
  7. #include <vector>
  8. #include "common/ostream.h"
  9. #include "executable_semantics/ast/expression.h"
  10. #include "executable_semantics/ast/pattern.h"
  11. #include "executable_semantics/ast/source_location.h"
  12. #include "executable_semantics/ast/static_scope.h"
  13. #include "executable_semantics/common/arena.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/Support/Compiler.h"
  16. namespace Carbon {
  17. class FunctionDeclaration;
  18. class Statement : public 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(AstNodeKind kind, SourceLocation source_loc)
  33. : AstNode(kind, source_loc) {}
  34. };
  35. class Block : public Statement {
  36. public:
  37. Block(SourceLocation source_loc, std::vector<Nonnull<Statement*>> statements)
  38. : Statement(AstNodeKind::Block, source_loc),
  39. statements_(std::move(statements)) {}
  40. static auto classof(const AstNode* node) -> bool {
  41. return InheritsFromBlock(node->kind());
  42. }
  43. auto statements() const -> llvm::ArrayRef<Nonnull<const Statement*>> {
  44. return statements_;
  45. }
  46. auto statements() -> llvm::MutableArrayRef<Nonnull<Statement*>> {
  47. return statements_;
  48. }
  49. private:
  50. std::vector<Nonnull<Statement*>> statements_;
  51. };
  52. class ExpressionStatement : public Statement {
  53. public:
  54. ExpressionStatement(SourceLocation source_loc,
  55. Nonnull<Expression*> expression)
  56. : Statement(AstNodeKind::ExpressionStatement, source_loc),
  57. expression_(expression) {}
  58. static auto classof(const AstNode* node) -> bool {
  59. return InheritsFromExpressionStatement(node->kind());
  60. }
  61. auto expression() const -> const Expression& { return *expression_; }
  62. auto expression() -> Expression& { return *expression_; }
  63. private:
  64. Nonnull<Expression*> expression_;
  65. };
  66. class Assign : public Statement {
  67. public:
  68. Assign(SourceLocation source_loc, Nonnull<Expression*> lhs,
  69. Nonnull<Expression*> rhs)
  70. : Statement(AstNodeKind::Assign, source_loc), lhs_(lhs), rhs_(rhs) {}
  71. static auto classof(const AstNode* node) -> bool {
  72. return InheritsFromAssign(node->kind());
  73. }
  74. auto lhs() const -> const Expression& { return *lhs_; }
  75. auto lhs() -> Expression& { return *lhs_; }
  76. auto rhs() const -> const Expression& { return *rhs_; }
  77. auto rhs() -> Expression& { return *rhs_; }
  78. private:
  79. Nonnull<Expression*> lhs_;
  80. Nonnull<Expression*> rhs_;
  81. };
  82. class VariableDefinition : public Statement {
  83. public:
  84. VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
  85. Nonnull<Expression*> init)
  86. : Statement(AstNodeKind::VariableDefinition, source_loc),
  87. pattern_(pattern),
  88. init_(init) {}
  89. static auto classof(const AstNode* node) -> bool {
  90. return InheritsFromVariableDefinition(node->kind());
  91. }
  92. auto pattern() const -> const Pattern& { return *pattern_; }
  93. auto pattern() -> Pattern& { return *pattern_; }
  94. auto init() const -> const Expression& { return *init_; }
  95. auto init() -> Expression& { return *init_; }
  96. private:
  97. Nonnull<Pattern*> pattern_;
  98. Nonnull<Expression*> init_;
  99. };
  100. class If : public Statement {
  101. public:
  102. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  103. Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
  104. : Statement(AstNodeKind::If, source_loc),
  105. condition_(condition),
  106. then_block_(then_block),
  107. else_block_(else_block) {}
  108. static auto classof(const AstNode* node) -> bool {
  109. return InheritsFromIf(node->kind());
  110. }
  111. auto condition() const -> const Expression& { return *condition_; }
  112. auto condition() -> Expression& { return *condition_; }
  113. auto then_block() const -> const Block& { return *then_block_; }
  114. auto then_block() -> Block& { return *then_block_; }
  115. auto else_block() const -> std::optional<Nonnull<const Block*>> {
  116. return else_block_;
  117. }
  118. auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
  119. private:
  120. Nonnull<Expression*> condition_;
  121. Nonnull<Block*> then_block_;
  122. std::optional<Nonnull<Block*>> else_block_;
  123. };
  124. class Return : public Statement {
  125. public:
  126. Return(Nonnull<Arena*> arena, SourceLocation source_loc)
  127. : Return(source_loc, arena->New<TupleLiteral>(source_loc), true) {}
  128. Return(SourceLocation source_loc, Nonnull<Expression*> expression,
  129. bool is_omitted_expression)
  130. : Statement(AstNodeKind::Return, source_loc),
  131. expression_(expression),
  132. is_omitted_expression_(is_omitted_expression) {}
  133. static auto classof(const AstNode* node) -> bool {
  134. return InheritsFromReturn(node->kind());
  135. }
  136. auto expression() const -> const Expression& { return *expression_; }
  137. auto expression() -> Expression& { return *expression_; }
  138. auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
  139. // The AST node representing the function body this statement returns from.
  140. // Can only be called after ResolveControlFlow has visited this node.
  141. //
  142. // Note that this function does not represent an edge in the tree
  143. // structure of the AST: the return value is not a child of this node,
  144. // but an ancestor.
  145. auto function() const -> const FunctionDeclaration& { return **function_; }
  146. auto function() -> FunctionDeclaration& { return **function_; }
  147. // Can only be called once, by ResolveControlFlow.
  148. void set_function(Nonnull<FunctionDeclaration*> function) {
  149. CHECK(!function_.has_value());
  150. function_ = function;
  151. }
  152. private:
  153. Nonnull<Expression*> expression_;
  154. bool is_omitted_expression_;
  155. std::optional<Nonnull<FunctionDeclaration*>> function_;
  156. };
  157. class While : public Statement {
  158. public:
  159. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  160. Nonnull<Block*> body)
  161. : Statement(AstNodeKind::While, source_loc),
  162. condition_(condition),
  163. body_(body) {}
  164. static auto classof(const AstNode* node) -> bool {
  165. return InheritsFromWhile(node->kind());
  166. }
  167. auto condition() const -> const Expression& { return *condition_; }
  168. auto condition() -> Expression& { return *condition_; }
  169. auto body() const -> const Block& { return *body_; }
  170. auto body() -> Block& { return *body_; }
  171. private:
  172. Nonnull<Expression*> condition_;
  173. Nonnull<Block*> body_;
  174. };
  175. class Break : public Statement {
  176. public:
  177. explicit Break(SourceLocation source_loc)
  178. : Statement(AstNodeKind::Break, source_loc) {}
  179. static auto classof(const AstNode* node) -> bool {
  180. return InheritsFromBreak(node->kind());
  181. }
  182. // The AST node representing the loop this statement breaks out of.
  183. // Can only be called after ResolveControlFlow has visited this node.
  184. //
  185. // Note that this function does not represent an edge in the tree
  186. // structure of the AST: the return value is not a child of this node,
  187. // but an ancestor.
  188. auto loop() const -> const Statement& { return **loop_; }
  189. // Can only be called once, by ResolveControlFlow.
  190. void set_loop(Nonnull<const Statement*> loop) {
  191. CHECK(!loop_.has_value());
  192. loop_ = loop;
  193. }
  194. private:
  195. std::optional<Nonnull<const Statement*>> loop_;
  196. };
  197. class Continue : public Statement {
  198. public:
  199. explicit Continue(SourceLocation source_loc)
  200. : Statement(AstNodeKind::Continue, source_loc) {}
  201. static auto classof(const AstNode* node) -> bool {
  202. return InheritsFromContinue(node->kind());
  203. }
  204. // The AST node representing the loop this statement continues.
  205. // Can only be called after ResolveControlFlow has visited this node.
  206. //
  207. // Note that this function does not represent an edge in the tree
  208. // structure of the AST: the return value is not a child of this node,
  209. // but an ancestor.
  210. auto loop() const -> const Statement& { return **loop_; }
  211. // Can only be called once, by ResolveControlFlow.
  212. void set_loop(Nonnull<const Statement*> loop) {
  213. CHECK(!loop_.has_value());
  214. loop_ = loop;
  215. }
  216. private:
  217. std::optional<Nonnull<const Statement*>> loop_;
  218. };
  219. class Match : public Statement {
  220. public:
  221. class Clause {
  222. public:
  223. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  224. : pattern_(pattern), statement_(statement) {}
  225. auto pattern() const -> const Pattern& { return *pattern_; }
  226. auto pattern() -> Pattern& { return *pattern_; }
  227. auto statement() const -> const Statement& { return *statement_; }
  228. auto statement() -> Statement& { return *statement_; }
  229. private:
  230. Nonnull<Pattern*> pattern_;
  231. Nonnull<Statement*> statement_;
  232. };
  233. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  234. std::vector<Clause> clauses)
  235. : Statement(AstNodeKind::Match, source_loc),
  236. expression_(expression),
  237. clauses_(std::move(clauses)) {}
  238. static auto classof(const AstNode* node) -> bool {
  239. return InheritsFromMatch(node->kind());
  240. }
  241. auto expression() const -> const Expression& { return *expression_; }
  242. auto expression() -> Expression& { return *expression_; }
  243. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  244. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  245. private:
  246. Nonnull<Expression*> expression_;
  247. std::vector<Clause> clauses_;
  248. };
  249. // A continuation statement.
  250. //
  251. // __continuation <continuation_variable> {
  252. // <body>
  253. // }
  254. class Continuation : public Statement {
  255. public:
  256. using ImplementsCarbonNamedEntity = void;
  257. Continuation(SourceLocation source_loc, std::string continuation_variable,
  258. Nonnull<Block*> body)
  259. : Statement(AstNodeKind::Continuation, source_loc),
  260. continuation_variable_(std::move(continuation_variable)),
  261. body_(body) {}
  262. static auto classof(const AstNode* node) -> bool {
  263. return InheritsFromContinuation(node->kind());
  264. }
  265. auto continuation_variable() const -> const std::string& {
  266. return continuation_variable_;
  267. }
  268. auto body() const -> const Block& { return *body_; }
  269. auto body() -> Block& { return *body_; }
  270. // The static type of the continuation. Cannot be called before typechecking.
  271. //
  272. // This will always be ContinuationType, but we must set it dynamically in
  273. // the typechecker because this code can't depend on ContinuationType.
  274. auto static_type() const -> const Value& { return **static_type_; }
  275. // Sets the static type of the continuation. Can only be called once,
  276. // during typechecking.
  277. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  278. // Returns whether the static type has been set. Should only be called
  279. // during typechecking: before typechecking it's guaranteed to be false,
  280. // and after typechecking it's guaranteed to be true.
  281. auto has_static_type() const -> bool { return static_type_.has_value(); }
  282. private:
  283. std::string continuation_variable_;
  284. Nonnull<Block*> body_;
  285. std::optional<Nonnull<const Value*>> static_type_;
  286. };
  287. // A run statement.
  288. //
  289. // __run <argument>;
  290. class Run : public Statement {
  291. public:
  292. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  293. : Statement(AstNodeKind::Run, source_loc), argument_(argument) {}
  294. static auto classof(const AstNode* node) -> bool {
  295. return InheritsFromRun(node->kind());
  296. }
  297. auto argument() const -> const Expression& { return *argument_; }
  298. auto argument() -> Expression& { return *argument_; }
  299. private:
  300. Nonnull<Expression*> argument_;
  301. };
  302. // An await statement.
  303. //
  304. // __await;
  305. class Await : public Statement {
  306. public:
  307. explicit Await(SourceLocation source_loc)
  308. : Statement(AstNodeKind::Await, source_loc) {}
  309. static auto classof(const AstNode* node) -> bool {
  310. return InheritsFromAwait(node->kind());
  311. }
  312. };
  313. } // namespace Carbon
  314. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_