statement.h 12 KB

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