statement.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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/clone_context.h"
  11. #include "explorer/ast/expression.h"
  12. #include "explorer/ast/expression_category.h"
  13. #include "explorer/ast/pattern.h"
  14. #include "explorer/ast/return_term.h"
  15. #include "explorer/ast/value_node.h"
  16. #include "explorer/base/arena.h"
  17. #include "explorer/base/source_location.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace Carbon {
  21. class CallableDeclaration;
  22. class Statement : public AstNode {
  23. public:
  24. ~Statement() override = 0;
  25. void Print(llvm::raw_ostream& out) const override { PrintIndent(0, out); }
  26. void PrintID(llvm::raw_ostream& out) const override;
  27. void PrintIndent(int indent_num_spaces, llvm::raw_ostream& out) const;
  28. static auto classof(const AstNode* node) {
  29. return InheritsFromStatement(node->kind());
  30. }
  31. // Returns the enumerator corresponding to the most-derived type of this
  32. // object.
  33. auto kind() const -> StatementKind {
  34. return static_cast<StatementKind>(root_kind());
  35. }
  36. protected:
  37. explicit Statement(AstNodeKind kind, SourceLocation source_loc)
  38. : AstNode(kind, source_loc) {}
  39. explicit Statement(CloneContext& context, const Statement& other)
  40. : AstNode(context, other) {}
  41. };
  42. class Block : public Statement {
  43. public:
  44. Block(SourceLocation source_loc, std::vector<Nonnull<Statement*>> statements)
  45. : Statement(AstNodeKind::Block, source_loc),
  46. statements_(std::move(statements)) {}
  47. explicit Block(CloneContext& context, const Block& other)
  48. : Statement(context, other),
  49. statements_(context.Clone(other.statements_)) {}
  50. static auto classof(const AstNode* node) -> bool {
  51. return InheritsFromBlock(node->kind());
  52. }
  53. auto statements() const -> llvm::ArrayRef<Nonnull<const Statement*>> {
  54. return statements_;
  55. }
  56. auto statements() -> llvm::MutableArrayRef<Nonnull<Statement*>> {
  57. return statements_;
  58. }
  59. private:
  60. std::vector<Nonnull<Statement*>> statements_;
  61. };
  62. class ExpressionStatement : public Statement {
  63. public:
  64. ExpressionStatement(SourceLocation source_loc,
  65. Nonnull<Expression*> expression)
  66. : Statement(AstNodeKind::ExpressionStatement, source_loc),
  67. expression_(expression) {}
  68. explicit ExpressionStatement(CloneContext& context,
  69. const ExpressionStatement& other)
  70. : Statement(context, other),
  71. expression_(context.Clone(other.expression_)) {}
  72. static auto classof(const AstNode* node) -> bool {
  73. return InheritsFromExpressionStatement(node->kind());
  74. }
  75. auto expression() const -> const Expression& { return *expression_; }
  76. auto expression() -> Expression& { return *expression_; }
  77. private:
  78. Nonnull<Expression*> expression_;
  79. };
  80. enum class AssignOperator {
  81. Plain,
  82. Add,
  83. Div,
  84. Mul,
  85. Mod,
  86. Sub,
  87. And,
  88. Or,
  89. Xor,
  90. ShiftLeft,
  91. ShiftRight,
  92. };
  93. // Returns the spelling of this assignment operator token.
  94. auto AssignOperatorToString(AssignOperator op) -> std::string_view;
  95. class Assign : public Statement {
  96. public:
  97. Assign(SourceLocation source_loc, Nonnull<Expression*> lhs, AssignOperator op,
  98. Nonnull<Expression*> rhs)
  99. : Statement(AstNodeKind::Assign, source_loc),
  100. lhs_(lhs),
  101. rhs_(rhs),
  102. op_(op) {}
  103. explicit Assign(CloneContext& context, const Assign& other)
  104. : Statement(context, other),
  105. lhs_(context.Clone(other.lhs_)),
  106. rhs_(context.Clone(other.rhs_)),
  107. op_(other.op_),
  108. rewritten_form_(context.Clone(other.rewritten_form_)) {}
  109. static auto classof(const AstNode* node) -> bool {
  110. return InheritsFromAssign(node->kind());
  111. }
  112. auto lhs() const -> const Expression& { return *lhs_; }
  113. auto lhs() -> Expression& { return *lhs_; }
  114. auto rhs() const -> const Expression& { return *rhs_; }
  115. auto rhs() -> Expression& { return *rhs_; }
  116. auto op() const -> AssignOperator { return op_; }
  117. // Can only be called by type-checking, if a conversion was required.
  118. void set_rhs(Nonnull<Expression*> rhs) { rhs_ = rhs; }
  119. // Set the rewritten form of this statement. Can only be called during type
  120. // checking.
  121. auto set_rewritten_form(Nonnull<const Expression*> rewritten_form) -> void {
  122. CARBON_CHECK(!rewritten_form_.has_value(), "rewritten form set twice");
  123. rewritten_form_ = rewritten_form;
  124. }
  125. // Get the rewritten form of this statement. A rewritten form is used when
  126. // the statement is rewritten as a function call on an interface. A
  127. // rewritten form is not used when providing built-in operator semantics for
  128. // a plain assignment.
  129. auto rewritten_form() const -> std::optional<Nonnull<const Expression*>> {
  130. return rewritten_form_;
  131. }
  132. private:
  133. Nonnull<Expression*> lhs_;
  134. Nonnull<Expression*> rhs_;
  135. AssignOperator op_;
  136. std::optional<Nonnull<const Expression*>> rewritten_form_;
  137. };
  138. class IncrementDecrement : public Statement {
  139. public:
  140. IncrementDecrement(SourceLocation source_loc, Nonnull<Expression*> argument,
  141. bool is_increment)
  142. : Statement(AstNodeKind::IncrementDecrement, source_loc),
  143. argument_(argument),
  144. is_increment_(is_increment) {}
  145. explicit IncrementDecrement(CloneContext& context,
  146. const IncrementDecrement& other)
  147. : Statement(context, other),
  148. argument_(context.Clone(other.argument_)),
  149. is_increment_(other.is_increment_),
  150. rewritten_form_(context.Clone(other.rewritten_form_)) {}
  151. static auto classof(const AstNode* node) -> bool {
  152. return InheritsFromIncrementDecrement(node->kind());
  153. }
  154. auto argument() const -> const Expression& { return *argument_; }
  155. auto argument() -> Expression& { return *argument_; }
  156. auto is_increment() const -> bool { return is_increment_; }
  157. // Set the rewritten form of this statement. Can only be called during type
  158. // checking.
  159. auto set_rewritten_form(Nonnull<const Expression*> rewritten_form) -> void {
  160. CARBON_CHECK(!rewritten_form_.has_value(), "rewritten form set twice");
  161. rewritten_form_ = rewritten_form;
  162. }
  163. // Get the rewritten form of this statement.
  164. auto rewritten_form() const -> std::optional<Nonnull<const Expression*>> {
  165. return rewritten_form_;
  166. }
  167. private:
  168. Nonnull<Expression*> argument_;
  169. bool is_increment_;
  170. std::optional<Nonnull<const Expression*>> rewritten_form_;
  171. };
  172. class VariableDefinition : public Statement {
  173. public:
  174. enum DefinitionType {
  175. Var,
  176. Returned,
  177. };
  178. VariableDefinition(SourceLocation source_loc, Nonnull<Pattern*> pattern,
  179. std::optional<Nonnull<Expression*>> init,
  180. ExpressionCategory expression_category,
  181. DefinitionType def_type)
  182. : Statement(AstNodeKind::VariableDefinition, source_loc),
  183. pattern_(pattern),
  184. init_(init),
  185. expression_category_(expression_category),
  186. def_type_(def_type) {}
  187. explicit VariableDefinition(CloneContext& context,
  188. const VariableDefinition& other)
  189. : Statement(context, other),
  190. pattern_(context.Clone(other.pattern_)),
  191. init_(context.Clone(other.init_)),
  192. expression_category_(other.expression_category_),
  193. def_type_(other.def_type_) {}
  194. static auto classof(const AstNode* node) -> bool {
  195. return InheritsFromVariableDefinition(node->kind());
  196. }
  197. auto pattern() const -> const Pattern& { return *pattern_; }
  198. auto pattern() -> Pattern& { return *pattern_; }
  199. auto init() const -> const Expression& {
  200. CARBON_CHECK(has_init());
  201. return **init_;
  202. }
  203. auto init() -> Expression& {
  204. CARBON_CHECK(has_init());
  205. return **init_;
  206. }
  207. auto has_init() const -> bool { return init_.has_value(); }
  208. // Can only be called by type-checking, if a conversion was required.
  209. void set_init(Nonnull<Expression*> init) {
  210. CARBON_CHECK(has_init(), "should not add a new initializer");
  211. init_ = init;
  212. }
  213. auto expression_category() const -> ExpressionCategory {
  214. return expression_category_;
  215. }
  216. auto is_returned() const -> bool { return def_type_ == Returned; }
  217. private:
  218. Nonnull<Pattern*> pattern_;
  219. std::optional<Nonnull<Expression*>> init_;
  220. ExpressionCategory expression_category_;
  221. const DefinitionType def_type_;
  222. };
  223. class If : public Statement {
  224. public:
  225. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  226. Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
  227. : Statement(AstNodeKind::If, source_loc),
  228. condition_(condition),
  229. then_block_(then_block),
  230. else_block_(else_block) {}
  231. explicit If(CloneContext& context, const If& other)
  232. : Statement(context, other),
  233. condition_(context.Clone(other.condition_)),
  234. then_block_(context.Clone(other.then_block_)),
  235. else_block_(context.Clone(other.else_block_)) {}
  236. static auto classof(const AstNode* node) -> bool {
  237. return InheritsFromIf(node->kind());
  238. }
  239. auto condition() const -> const Expression& { return *condition_; }
  240. auto condition() -> Expression& { return *condition_; }
  241. auto then_block() const -> const Block& { return *then_block_; }
  242. auto then_block() -> Block& { return *then_block_; }
  243. auto else_block() const -> std::optional<Nonnull<const Block*>> {
  244. return else_block_;
  245. }
  246. auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
  247. // Can only be called by type-checking, if a conversion was required.
  248. void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
  249. private:
  250. Nonnull<Expression*> condition_;
  251. Nonnull<Block*> then_block_;
  252. std::optional<Nonnull<Block*>> else_block_;
  253. };
  254. class Return : public Statement {
  255. public:
  256. static auto classof(const AstNode* node) -> bool {
  257. return InheritsFromReturn(node->kind());
  258. }
  259. // The AST node representing the function body this statement returns from.
  260. // Can only be called after ResolveControlFlow has visited this node.
  261. //
  262. // Note that this function does not represent an edge in the tree
  263. // structure of the AST: the return value is not a child of this node,
  264. // but an ancestor.
  265. auto function() const -> const CallableDeclaration& { return **function_; }
  266. auto function() -> CallableDeclaration& { return **function_; }
  267. // Can only be called once, by ResolveControlFlow.
  268. void set_function(Nonnull<CallableDeclaration*> function) {
  269. CARBON_CHECK(!function_.has_value());
  270. function_ = function;
  271. }
  272. protected:
  273. Return(AstNodeKind node_kind, SourceLocation source_loc)
  274. : Statement(node_kind, source_loc) {}
  275. explicit Return(CloneContext& context, const Return& other);
  276. private:
  277. std::optional<Nonnull<CallableDeclaration*>> function_;
  278. };
  279. class ReturnVar : public Return {
  280. public:
  281. explicit ReturnVar(SourceLocation source_loc)
  282. : Return(AstNodeKind::ReturnVar, source_loc) {}
  283. explicit ReturnVar(CloneContext& context, const ReturnVar& other)
  284. : Return(context, other), value_node_(context.Clone(other.value_node_)) {}
  285. static auto classof(const AstNode* node) -> bool {
  286. return InheritsFromReturnVar(node->kind());
  287. }
  288. // Returns the value node of the BindingPattern of the returned var
  289. // definition. Cannot be called before name resolution.
  290. auto value_node() const -> const ValueNodeView& { return *value_node_; }
  291. // Can only be called once, by ResolveNames.
  292. void set_value_node(ValueNodeView value_node) {
  293. CARBON_CHECK(!value_node_.has_value());
  294. value_node_ = value_node;
  295. }
  296. private:
  297. // The value node of the BindingPattern of the returned var definition.
  298. std::optional<ValueNodeView> value_node_;
  299. };
  300. class ReturnExpression : public Return {
  301. public:
  302. ReturnExpression(Nonnull<Arena*> arena, SourceLocation source_loc)
  303. : ReturnExpression(source_loc, arena->New<TupleLiteral>(source_loc),
  304. true) {}
  305. ReturnExpression(SourceLocation source_loc, Nonnull<Expression*> expression,
  306. bool is_omitted_expression)
  307. : Return(AstNodeKind::ReturnExpression, source_loc),
  308. expression_(expression),
  309. is_omitted_expression_(is_omitted_expression) {}
  310. explicit ReturnExpression(CloneContext& context,
  311. const ReturnExpression& other)
  312. : Return(context, other),
  313. expression_(context.Clone(other.expression_)),
  314. is_omitted_expression_(other.is_omitted_expression_) {}
  315. static auto classof(const AstNode* node) -> bool {
  316. return InheritsFromReturnExpression(node->kind());
  317. }
  318. auto expression() const -> const Expression& { return *expression_; }
  319. auto expression() -> Expression& { return *expression_; }
  320. auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
  321. // Can only be called by type-checking, if a conversion was required.
  322. void set_expression(Nonnull<Expression*> expression) {
  323. expression_ = expression;
  324. }
  325. private:
  326. Nonnull<Expression*> expression_;
  327. bool is_omitted_expression_;
  328. };
  329. class While : public Statement {
  330. public:
  331. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  332. Nonnull<Block*> body)
  333. : Statement(AstNodeKind::While, source_loc),
  334. condition_(condition),
  335. body_(body) {}
  336. explicit While(CloneContext& context, const While& other)
  337. : Statement(context, other),
  338. condition_(context.Clone(other.condition_)),
  339. body_(context.Clone(other.body_)) {}
  340. static auto classof(const AstNode* node) -> bool {
  341. return InheritsFromWhile(node->kind());
  342. }
  343. auto condition() const -> const Expression& { return *condition_; }
  344. auto condition() -> Expression& { return *condition_; }
  345. auto body() const -> const Block& { return *body_; }
  346. auto body() -> Block& { return *body_; }
  347. // Can only be called by type-checking, if a conversion was required.
  348. void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
  349. private:
  350. Nonnull<Expression*> condition_;
  351. Nonnull<Block*> body_;
  352. };
  353. class For : public Statement {
  354. public:
  355. For(SourceLocation source_loc, Nonnull<BindingPattern*> variable_declaration,
  356. Nonnull<Expression*> loop_target, Nonnull<Block*> body)
  357. : Statement(AstNodeKind::For, source_loc),
  358. variable_declaration_(variable_declaration),
  359. loop_target_(loop_target),
  360. body_(body) {}
  361. explicit For(CloneContext& context, const For& other)
  362. : Statement(context, other),
  363. variable_declaration_(context.Clone(other.variable_declaration_)),
  364. loop_target_(context.Clone(other.loop_target_)),
  365. body_(context.Clone(other.body_)) {}
  366. static auto classof(const AstNode* node) -> bool {
  367. return InheritsFromFor(node->kind());
  368. }
  369. auto variable_declaration() const -> const BindingPattern& {
  370. return *variable_declaration_;
  371. }
  372. auto variable_declaration() -> BindingPattern& {
  373. return *variable_declaration_;
  374. }
  375. auto loop_target() const -> const Expression& { return *loop_target_; }
  376. auto loop_target() -> Expression& { return *loop_target_; }
  377. auto body() const -> const Block& { return *body_; }
  378. auto body() -> Block& { return *body_; }
  379. private:
  380. Nonnull<BindingPattern*> variable_declaration_;
  381. Nonnull<Expression*> loop_target_;
  382. Nonnull<Block*> body_;
  383. };
  384. class Break : public Statement {
  385. public:
  386. explicit Break(SourceLocation source_loc)
  387. : Statement(AstNodeKind::Break, source_loc) {}
  388. explicit Break(CloneContext& context, const Break& other)
  389. : Statement(context, other), loop_(context.Clone(other.loop_)) {}
  390. static auto classof(const AstNode* node) -> bool {
  391. return InheritsFromBreak(node->kind());
  392. }
  393. // The AST node representing the loop this statement breaks out of.
  394. // Can only be called after ResolveControlFlow has visited this node.
  395. //
  396. // Note that this function does not represent an edge in the tree
  397. // structure of the AST: the return value is not a child of this node,
  398. // but an ancestor.
  399. auto loop() const -> const Statement& { return **loop_; }
  400. // Can only be called once, by ResolveControlFlow.
  401. void set_loop(Nonnull<const Statement*> loop) {
  402. CARBON_CHECK(!loop_.has_value());
  403. loop_ = loop;
  404. }
  405. private:
  406. std::optional<Nonnull<const Statement*>> loop_;
  407. };
  408. class Continue : public Statement {
  409. public:
  410. explicit Continue(SourceLocation source_loc)
  411. : Statement(AstNodeKind::Continue, source_loc) {}
  412. explicit Continue(CloneContext& context, const Continue& other)
  413. : Statement(context, other), loop_(context.Clone(other.loop_)) {}
  414. static auto classof(const AstNode* node) -> bool {
  415. return InheritsFromContinue(node->kind());
  416. }
  417. // The AST node representing the loop this statement continues.
  418. // Can only be called after ResolveControlFlow has visited this node.
  419. //
  420. // Note that this function does not represent an edge in the tree
  421. // structure of the AST: the return value is not a child of this node,
  422. // but an ancestor.
  423. auto loop() const -> const Statement& { return **loop_; }
  424. // Can only be called once, by ResolveControlFlow.
  425. void set_loop(Nonnull<const Statement*> loop) {
  426. CARBON_CHECK(!loop_.has_value());
  427. loop_ = loop;
  428. }
  429. private:
  430. std::optional<Nonnull<const Statement*>> loop_;
  431. };
  432. class Match : public Statement {
  433. public:
  434. class Clause {
  435. public:
  436. explicit Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  437. : pattern_(pattern), statement_(statement) {}
  438. explicit Clause(CloneContext& context, const Clause& other)
  439. : pattern_(context.Clone(other.pattern_)),
  440. statement_(context.Clone(other.statement_)) {}
  441. auto pattern() const -> const Pattern& { return *pattern_; }
  442. auto pattern() -> Pattern& { return *pattern_; }
  443. auto statement() const -> const Statement& { return *statement_; }
  444. auto statement() -> Statement& { return *statement_; }
  445. private:
  446. Nonnull<Pattern*> pattern_;
  447. Nonnull<Statement*> statement_;
  448. };
  449. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  450. std::vector<Clause> clauses)
  451. : Statement(AstNodeKind::Match, source_loc),
  452. expression_(expression),
  453. clauses_(std::move(clauses)) {}
  454. explicit Match(CloneContext& context, const Match& other)
  455. : Statement(context, other),
  456. expression_(context.Clone(other.expression_)),
  457. clauses_(context.Clone(other.clauses_)) {}
  458. static auto classof(const AstNode* node) -> bool {
  459. return InheritsFromMatch(node->kind());
  460. }
  461. auto expression() const -> const Expression& { return *expression_; }
  462. auto expression() -> Expression& { return *expression_; }
  463. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  464. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  465. // Can only be called by type-checking, if a conversion was required.
  466. void set_expression(Nonnull<Expression*> expression) {
  467. expression_ = expression;
  468. }
  469. private:
  470. Nonnull<Expression*> expression_;
  471. std::vector<Clause> clauses_;
  472. };
  473. } // namespace Carbon
  474. #endif // CARBON_EXPLORER_AST_STATEMENT_H_