statement.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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)
  89. : Statement(AstNodeKind::VariableDefinition, source_loc),
  90. pattern_(pattern),
  91. init_(init) {}
  92. static auto classof(const AstNode* node) -> bool {
  93. return InheritsFromVariableDefinition(node->kind());
  94. }
  95. auto pattern() const -> const Pattern& { return *pattern_; }
  96. auto pattern() -> Pattern& { return *pattern_; }
  97. auto init() const -> const Expression& { return *init_; }
  98. auto init() -> Expression& { return *init_; }
  99. private:
  100. Nonnull<Pattern*> pattern_;
  101. Nonnull<Expression*> init_;
  102. };
  103. class If : public Statement {
  104. public:
  105. If(SourceLocation source_loc, Nonnull<Expression*> condition,
  106. Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
  107. : Statement(AstNodeKind::If, source_loc),
  108. condition_(condition),
  109. then_block_(then_block),
  110. else_block_(else_block) {}
  111. static auto classof(const AstNode* node) -> bool {
  112. return InheritsFromIf(node->kind());
  113. }
  114. auto condition() const -> const Expression& { return *condition_; }
  115. auto condition() -> Expression& { return *condition_; }
  116. auto then_block() const -> const Block& { return *then_block_; }
  117. auto then_block() -> Block& { return *then_block_; }
  118. auto else_block() const -> std::optional<Nonnull<const Block*>> {
  119. return else_block_;
  120. }
  121. auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
  122. private:
  123. Nonnull<Expression*> condition_;
  124. Nonnull<Block*> then_block_;
  125. std::optional<Nonnull<Block*>> else_block_;
  126. };
  127. class Return : public Statement {
  128. public:
  129. Return(Nonnull<Arena*> arena, SourceLocation source_loc)
  130. : Return(source_loc, arena->New<TupleLiteral>(source_loc), true) {}
  131. Return(SourceLocation source_loc, Nonnull<Expression*> expression,
  132. bool is_omitted_expression)
  133. : Statement(AstNodeKind::Return, source_loc),
  134. expression_(expression),
  135. is_omitted_expression_(is_omitted_expression) {}
  136. static auto classof(const AstNode* node) -> bool {
  137. return InheritsFromReturn(node->kind());
  138. }
  139. auto expression() const -> const Expression& { return *expression_; }
  140. auto expression() -> Expression& { return *expression_; }
  141. auto is_omitted_expression() const -> bool { return is_omitted_expression_; }
  142. // The AST node representing the function body this statement returns from.
  143. // Can only be called after ResolveControlFlow has visited this node.
  144. //
  145. // Note that this function does not represent an edge in the tree
  146. // structure of the AST: the return value is not a child of this node,
  147. // but an ancestor.
  148. auto function() const -> const FunctionDeclaration& { return **function_; }
  149. auto function() -> FunctionDeclaration& { return **function_; }
  150. // Can only be called once, by ResolveControlFlow.
  151. void set_function(Nonnull<FunctionDeclaration*> function) {
  152. CHECK(!function_.has_value());
  153. function_ = function;
  154. }
  155. private:
  156. Nonnull<Expression*> expression_;
  157. bool is_omitted_expression_;
  158. std::optional<Nonnull<FunctionDeclaration*>> function_;
  159. };
  160. class While : public Statement {
  161. public:
  162. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  163. Nonnull<Block*> body)
  164. : Statement(AstNodeKind::While, source_loc),
  165. condition_(condition),
  166. body_(body) {}
  167. static auto classof(const AstNode* node) -> bool {
  168. return InheritsFromWhile(node->kind());
  169. }
  170. auto condition() const -> const Expression& { return *condition_; }
  171. auto condition() -> Expression& { return *condition_; }
  172. auto body() const -> const Block& { return *body_; }
  173. auto body() -> Block& { return *body_; }
  174. private:
  175. Nonnull<Expression*> condition_;
  176. Nonnull<Block*> body_;
  177. };
  178. class Break : public Statement {
  179. public:
  180. explicit Break(SourceLocation source_loc)
  181. : Statement(AstNodeKind::Break, source_loc) {}
  182. static auto classof(const AstNode* node) -> bool {
  183. return InheritsFromBreak(node->kind());
  184. }
  185. // The AST node representing the loop this statement breaks out of.
  186. // Can only be called after ResolveControlFlow has visited this node.
  187. //
  188. // Note that this function does not represent an edge in the tree
  189. // structure of the AST: the return value is not a child of this node,
  190. // but an ancestor.
  191. auto loop() const -> const Statement& { return **loop_; }
  192. // Can only be called once, by ResolveControlFlow.
  193. void set_loop(Nonnull<const Statement*> loop) {
  194. CHECK(!loop_.has_value());
  195. loop_ = loop;
  196. }
  197. private:
  198. std::optional<Nonnull<const Statement*>> loop_;
  199. };
  200. class Continue : public Statement {
  201. public:
  202. explicit Continue(SourceLocation source_loc)
  203. : Statement(AstNodeKind::Continue, source_loc) {}
  204. static auto classof(const AstNode* node) -> bool {
  205. return InheritsFromContinue(node->kind());
  206. }
  207. // The AST node representing the loop this statement continues.
  208. // Can only be called after ResolveControlFlow has visited this node.
  209. //
  210. // Note that this function does not represent an edge in the tree
  211. // structure of the AST: the return value is not a child of this node,
  212. // but an ancestor.
  213. auto loop() const -> const Statement& { return **loop_; }
  214. // Can only be called once, by ResolveControlFlow.
  215. void set_loop(Nonnull<const Statement*> loop) {
  216. CHECK(!loop_.has_value());
  217. loop_ = loop;
  218. }
  219. private:
  220. std::optional<Nonnull<const Statement*>> loop_;
  221. };
  222. class Match : public Statement {
  223. public:
  224. class Clause {
  225. public:
  226. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  227. : pattern_(pattern), statement_(statement) {}
  228. auto pattern() const -> const Pattern& { return *pattern_; }
  229. auto pattern() -> Pattern& { return *pattern_; }
  230. auto statement() const -> const Statement& { return *statement_; }
  231. auto statement() -> Statement& { return *statement_; }
  232. private:
  233. Nonnull<Pattern*> pattern_;
  234. Nonnull<Statement*> statement_;
  235. };
  236. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  237. std::vector<Clause> clauses)
  238. : Statement(AstNodeKind::Match, source_loc),
  239. expression_(expression),
  240. clauses_(std::move(clauses)) {}
  241. static auto classof(const AstNode* node) -> bool {
  242. return InheritsFromMatch(node->kind());
  243. }
  244. auto expression() const -> const Expression& { return *expression_; }
  245. auto expression() -> Expression& { return *expression_; }
  246. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  247. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  248. private:
  249. Nonnull<Expression*> expression_;
  250. std::vector<Clause> clauses_;
  251. };
  252. // A continuation statement.
  253. //
  254. // __continuation <continuation_variable> {
  255. // <body>
  256. // }
  257. class Continuation : public Statement {
  258. public:
  259. using ImplementsCarbonNamedEntity = void;
  260. Continuation(SourceLocation source_loc, std::string name,
  261. Nonnull<Block*> body)
  262. : Statement(AstNodeKind::Continuation, source_loc),
  263. name_(std::move(name)),
  264. body_(body) {}
  265. static auto classof(const AstNode* node) -> bool {
  266. return InheritsFromContinuation(node->kind());
  267. }
  268. auto name() const -> const std::string& { return name_; }
  269. auto body() const -> const Block& { return *body_; }
  270. auto body() -> Block& { return *body_; }
  271. // The static type of the continuation. Cannot be called before typechecking.
  272. //
  273. // This will always be ContinuationType, but we must set it dynamically in
  274. // the typechecker because this code can't depend on ContinuationType.
  275. auto static_type() const -> const Value& { return **static_type_; }
  276. // Sets the static type of the continuation. Can only be called once,
  277. // during typechecking.
  278. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  279. // Returns whether the static type has been set. Should only be called
  280. // during typechecking: before typechecking it's guaranteed to be false,
  281. // and after typechecking it's guaranteed to be true.
  282. auto has_static_type() const -> bool { return static_type_.has_value(); }
  283. auto value_category() const -> ValueCategory { return ValueCategory::Var; }
  284. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  285. return std::nullopt;
  286. }
  287. private:
  288. std::string name_;
  289. Nonnull<Block*> body_;
  290. std::optional<Nonnull<const Value*>> static_type_;
  291. };
  292. // A run statement.
  293. //
  294. // __run <argument>;
  295. class Run : public Statement {
  296. public:
  297. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  298. : Statement(AstNodeKind::Run, source_loc), argument_(argument) {}
  299. static auto classof(const AstNode* node) -> bool {
  300. return InheritsFromRun(node->kind());
  301. }
  302. auto argument() const -> const Expression& { return *argument_; }
  303. auto argument() -> Expression& { return *argument_; }
  304. private:
  305. Nonnull<Expression*> argument_;
  306. };
  307. // An await statement.
  308. //
  309. // __await;
  310. class Await : public Statement {
  311. public:
  312. explicit Await(SourceLocation source_loc)
  313. : Statement(AstNodeKind::Await, source_loc) {}
  314. static auto classof(const AstNode* node) -> bool {
  315. return InheritsFromAwait(node->kind());
  316. }
  317. };
  318. } // namespace Carbon
  319. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_