statement.h 13 KB

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