statement.h 12 KB

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