statement.h 12 KB

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