statement.h 12 KB

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