statement.h 18 KB

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