statement.h 12 KB

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