statement.h 15 KB

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