statement.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. enum DefinitionType {
  91. Var,
  92. Returned,
  93. };
  94. VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
  95. Nonnull<Expression*> init, ValueCategory value_category,
  96. DefinitionType def_type)
  97. : Statement(AstNodeKind::VariableDefinition, source_loc),
  98. pattern_(pattern),
  99. init_(init),
  100. value_category_(value_category),
  101. def_type_(def_type) {}
  102. static auto classof(const AstNode* node) -> bool {
  103. return InheritsFromVariableDefinition(node->kind());
  104. }
  105. auto pattern() const -> const Pattern& { return *pattern_; }
  106. auto pattern() -> Pattern& { return *pattern_; }
  107. auto init() const -> const Expression& { return *init_; }
  108. auto init() -> Expression& { return *init_; }
  109. auto value_category() const -> ValueCategory { return value_category_; }
  110. auto is_returned() const -> bool { return def_type_ == Returned; };
  111. // Can only be called by type-checking, if a conversion was required.
  112. void set_init(Nonnull<Expression*> init) { init_ = init; }
  113. private:
  114. Nonnull<Pattern*> pattern_;
  115. Nonnull<Expression*> init_;
  116. ValueCategory value_category_;
  117. const DefinitionType def_type_;
  118. };
  119. class If : public Statement {
  120. public:
  121. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  122. Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
  123. : Statement(AstNodeKind::If, source_loc),
  124. condition_(condition),
  125. then_block_(then_block),
  126. else_block_(else_block) {}
  127. static auto classof(const AstNode* node) -> bool {
  128. return InheritsFromIf(node->kind());
  129. }
  130. auto condition() const -> const Expression& { return *condition_; }
  131. auto condition() -> Expression& { return *condition_; }
  132. auto then_block() const -> const Block& { return *then_block_; }
  133. auto then_block() -> Block& { return *then_block_; }
  134. auto else_block() const -> std::optional<Nonnull<const Block*>> {
  135. return else_block_;
  136. }
  137. auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
  138. // Can only be called by type-checking, if a conversion was required.
  139. void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
  140. private:
  141. Nonnull<Expression*> condition_;
  142. Nonnull<Block*> then_block_;
  143. std::optional<Nonnull<Block*>> else_block_;
  144. };
  145. class Return : public Statement {
  146. public:
  147. static auto classof(const AstNode* node) -> bool {
  148. return InheritsFromReturn(node->kind());
  149. }
  150. // The AST node representing the function body this statement returns from.
  151. // Can only be called after ResolveControlFlow has visited this node.
  152. //
  153. // Note that this function does not represent an edge in the tree
  154. // structure of the AST: the return value is not a child of this node,
  155. // but an ancestor.
  156. auto function() const -> const FunctionDeclaration& { return **function_; }
  157. auto function() -> FunctionDeclaration& { return **function_; }
  158. // Can only be called once, by ResolveControlFlow.
  159. void set_function(Nonnull<FunctionDeclaration*> function) {
  160. CARBON_CHECK(!function_.has_value());
  161. function_ = function;
  162. }
  163. protected:
  164. Return(AstNodeKind node_kind, SourceLocation source_loc)
  165. : Statement(node_kind, source_loc) {}
  166. private:
  167. std::optional<Nonnull<FunctionDeclaration*>> function_;
  168. };
  169. class ReturnVar : public Return {
  170. public:
  171. explicit ReturnVar(SourceLocation source_loc)
  172. : Return(AstNodeKind::ReturnVar, source_loc) {}
  173. static auto classof(const AstNode* node) -> bool {
  174. return InheritsFromReturnVar(node->kind());
  175. }
  176. // Returns the value node of the BindingPattern of the returned var
  177. // definition. Cannot be called before name resolution.
  178. auto value_node() const -> const ValueNodeView& { return *value_node_; }
  179. // Can only be called once, by ResolveNames.
  180. void set_value_node(ValueNodeView value_node) {
  181. CARBON_CHECK(!value_node_.has_value());
  182. value_node_ = value_node;
  183. }
  184. private:
  185. // The value node of the BindingPattern of the returned var definition.
  186. std::optional<ValueNodeView> value_node_;
  187. };
  188. class ReturnExpression : public Return {
  189. public:
  190. ReturnExpression(Nonnull<Arena*> arena, SourceLocation source_loc)
  191. : ReturnExpression(source_loc, arena->New<TupleLiteral>(source_loc),
  192. true) {}
  193. ReturnExpression(SourceLocation source_loc, Nonnull<Expression*> expression,
  194. bool is_omitted_expression)
  195. : Return(AstNodeKind::ReturnExpression, source_loc),
  196. expression_(expression),
  197. is_omitted_expression_(is_omitted_expression) {}
  198. static auto classof(const AstNode* node) -> bool {
  199. return InheritsFromReturnExpression(node->kind());
  200. }
  201. auto expression() const -> const Expression& { return *expression_; }
  202. auto expression() -> Expression& { return *expression_; }
  203. auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
  204. // Can only be called by type-checking, if a conversion was required.
  205. void set_expression(Nonnull<Expression*> expression) {
  206. expression_ = expression;
  207. }
  208. private:
  209. Nonnull<Expression*> expression_;
  210. bool is_omitted_expression_;
  211. };
  212. class While : public Statement {
  213. public:
  214. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  215. Nonnull<Block*> body)
  216. : Statement(AstNodeKind::While, source_loc),
  217. condition_(condition),
  218. body_(body) {}
  219. static auto classof(const AstNode* node) -> bool {
  220. return InheritsFromWhile(node->kind());
  221. }
  222. auto condition() const -> const Expression& { return *condition_; }
  223. auto condition() -> Expression& { return *condition_; }
  224. auto body() const -> const Block& { return *body_; }
  225. auto body() -> Block& { return *body_; }
  226. // Can only be called by type-checking, if a conversion was required.
  227. void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
  228. private:
  229. Nonnull<Expression*> condition_;
  230. Nonnull<Block*> body_;
  231. };
  232. class Break : public Statement {
  233. public:
  234. explicit Break(SourceLocation source_loc)
  235. : Statement(AstNodeKind::Break, source_loc) {}
  236. static auto classof(const AstNode* node) -> bool {
  237. return InheritsFromBreak(node->kind());
  238. }
  239. // The AST node representing the loop this statement breaks out of.
  240. // Can only be called after ResolveControlFlow has visited this node.
  241. //
  242. // Note that this function does not represent an edge in the tree
  243. // structure of the AST: the return value is not a child of this node,
  244. // but an ancestor.
  245. auto loop() const -> const Statement& { return **loop_; }
  246. // Can only be called once, by ResolveControlFlow.
  247. void set_loop(Nonnull<const Statement*> loop) {
  248. CARBON_CHECK(!loop_.has_value());
  249. loop_ = loop;
  250. }
  251. private:
  252. std::optional<Nonnull<const Statement*>> loop_;
  253. };
  254. class Continue : public Statement {
  255. public:
  256. explicit Continue(SourceLocation source_loc)
  257. : Statement(AstNodeKind::Continue, source_loc) {}
  258. static auto classof(const AstNode* node) -> bool {
  259. return InheritsFromContinue(node->kind());
  260. }
  261. // The AST node representing the loop this statement continues.
  262. // Can only be called after ResolveControlFlow has visited this node.
  263. //
  264. // Note that this function does not represent an edge in the tree
  265. // structure of the AST: the return value is not a child of this node,
  266. // but an ancestor.
  267. auto loop() const -> const Statement& { return **loop_; }
  268. // Can only be called once, by ResolveControlFlow.
  269. void set_loop(Nonnull<const Statement*> loop) {
  270. CARBON_CHECK(!loop_.has_value());
  271. loop_ = loop;
  272. }
  273. private:
  274. std::optional<Nonnull<const Statement*>> loop_;
  275. };
  276. class Match : public Statement {
  277. public:
  278. class Clause {
  279. public:
  280. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  281. : pattern_(pattern), statement_(statement) {}
  282. auto pattern() const -> const Pattern& { return *pattern_; }
  283. auto pattern() -> Pattern& { return *pattern_; }
  284. auto statement() const -> const Statement& { return *statement_; }
  285. auto statement() -> Statement& { return *statement_; }
  286. private:
  287. Nonnull<Pattern*> pattern_;
  288. Nonnull<Statement*> statement_;
  289. };
  290. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  291. std::vector<Clause> clauses)
  292. : Statement(AstNodeKind::Match, source_loc),
  293. expression_(expression),
  294. clauses_(std::move(clauses)) {}
  295. static auto classof(const AstNode* node) -> bool {
  296. return InheritsFromMatch(node->kind());
  297. }
  298. auto expression() const -> const Expression& { return *expression_; }
  299. auto expression() -> Expression& { return *expression_; }
  300. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  301. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  302. // Can only be called by type-checking, if a conversion was required.
  303. void set_expression(Nonnull<Expression*> expression) {
  304. expression_ = expression;
  305. }
  306. private:
  307. Nonnull<Expression*> expression_;
  308. std::vector<Clause> clauses_;
  309. };
  310. // A continuation statement.
  311. //
  312. // __continuation <continuation_variable> {
  313. // <body>
  314. // }
  315. class Continuation : public Statement {
  316. public:
  317. using ImplementsCarbonValueNode = void;
  318. Continuation(SourceLocation source_loc, std::string name,
  319. Nonnull<Block*> body)
  320. : Statement(AstNodeKind::Continuation, source_loc),
  321. name_(std::move(name)),
  322. body_(body) {}
  323. static auto classof(const AstNode* node) -> bool {
  324. return InheritsFromContinuation(node->kind());
  325. }
  326. auto name() const -> const std::string& { return name_; }
  327. auto body() const -> const Block& { return *body_; }
  328. auto body() -> Block& { return *body_; }
  329. // The static type of the continuation. Cannot be called before typechecking.
  330. //
  331. // This will always be ContinuationType, but we must set it dynamically in
  332. // the typechecker because this code can't depend on ContinuationType.
  333. auto static_type() const -> const Value& { return **static_type_; }
  334. // Sets the static type of the continuation. Can only be called once,
  335. // during typechecking.
  336. void set_static_type(Nonnull<const Value*> type) {
  337. CARBON_CHECK(!static_type_.has_value());
  338. static_type_ = type;
  339. }
  340. auto value_category() const -> ValueCategory { return ValueCategory::Var; }
  341. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  342. return std::nullopt;
  343. }
  344. auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
  345. return std::nullopt;
  346. }
  347. private:
  348. std::string name_;
  349. Nonnull<Block*> body_;
  350. std::optional<Nonnull<const Value*>> static_type_;
  351. };
  352. // A run statement.
  353. //
  354. // __run <argument>;
  355. class Run : public Statement {
  356. public:
  357. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  358. : Statement(AstNodeKind::Run, source_loc), argument_(argument) {}
  359. static auto classof(const AstNode* node) -> bool {
  360. return InheritsFromRun(node->kind());
  361. }
  362. auto argument() const -> const Expression& { return *argument_; }
  363. auto argument() -> Expression& { return *argument_; }
  364. // Can only be called by type-checking, if a conversion was required.
  365. void set_argument(Nonnull<Expression*> argument) { argument_ = argument; }
  366. private:
  367. Nonnull<Expression*> argument_;
  368. };
  369. // An await statement.
  370. //
  371. // __await;
  372. class Await : public Statement {
  373. public:
  374. explicit Await(SourceLocation source_loc)
  375. : Statement(AstNodeKind::Await, source_loc) {}
  376. static auto classof(const AstNode* node) -> bool {
  377. return InheritsFromAwait(node->kind());
  378. }
  379. };
  380. } // namespace Carbon
  381. #endif // CARBON_EXPLORER_AST_STATEMENT_H_