statement.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. auto function() -> FunctionDeclaration& { return **function_; }
  166. // Can only be called once, by ResolveControlFlow.
  167. void set_function(Nonnull<FunctionDeclaration*> function) {
  168. CHECK(!function_.has_value());
  169. function_ = function;
  170. }
  171. private:
  172. Nonnull<Expression*> expression_;
  173. bool is_omitted_expression_;
  174. std::optional<Nonnull<FunctionDeclaration*>> function_;
  175. };
  176. class While : public Statement {
  177. public:
  178. While(SourceLocation source_loc, Nonnull<Expression*> condition,
  179. Nonnull<Block*> body)
  180. : Statement(Kind::While, source_loc),
  181. condition_(condition),
  182. body_(body) {}
  183. static auto classof(const Statement* stmt) -> bool {
  184. return stmt->kind() == Kind::While;
  185. }
  186. auto condition() const -> const Expression& { return *condition_; }
  187. auto condition() -> Expression& { return *condition_; }
  188. auto body() const -> const Block& { return *body_; }
  189. auto body() -> Block& { return *body_; }
  190. private:
  191. Nonnull<Expression*> condition_;
  192. Nonnull<Block*> body_;
  193. };
  194. class Break : public Statement {
  195. public:
  196. explicit Break(SourceLocation source_loc)
  197. : Statement(Kind::Break, source_loc) {}
  198. static auto classof(const Statement* stmt) -> bool {
  199. return stmt->kind() == Kind::Break;
  200. }
  201. // The AST node representing the loop this statement breaks out of.
  202. // Can only be called after ResolveControlFlow has visited this node.
  203. //
  204. // Note that this function does not represent an edge in the tree
  205. // structure of the AST: the return value is not a child of this node,
  206. // but an ancestor.
  207. auto loop() const -> const Statement& { return **loop_; }
  208. // Can only be called once, by ResolveControlFlow.
  209. void set_loop(Nonnull<const Statement*> loop) {
  210. CHECK(!loop_.has_value());
  211. loop_ = loop;
  212. }
  213. private:
  214. std::optional<Nonnull<const Statement*>> loop_;
  215. };
  216. class Continue : public Statement {
  217. public:
  218. explicit Continue(SourceLocation source_loc)
  219. : Statement(Kind::Continue, source_loc) {}
  220. static auto classof(const Statement* stmt) -> bool {
  221. return stmt->kind() == Kind::Continue;
  222. }
  223. // The AST node representing the loop this statement continues.
  224. // Can only be called after ResolveControlFlow has visited this node.
  225. //
  226. // Note that this function does not represent an edge in the tree
  227. // structure of the AST: the return value is not a child of this node,
  228. // but an ancestor.
  229. auto loop() const -> const Statement& { return **loop_; }
  230. // Can only be called once, by ResolveControlFlow.
  231. void set_loop(Nonnull<const Statement*> loop) {
  232. CHECK(!loop_.has_value());
  233. loop_ = loop;
  234. }
  235. private:
  236. std::optional<Nonnull<const Statement*>> loop_;
  237. };
  238. class Match : public Statement {
  239. public:
  240. class Clause {
  241. public:
  242. Clause(Nonnull<Pattern*> pattern, Nonnull<Statement*> statement)
  243. : pattern_(pattern), statement_(statement) {}
  244. auto pattern() const -> const Pattern& { return *pattern_; }
  245. auto pattern() -> Pattern& { return *pattern_; }
  246. auto statement() const -> const Statement& { return *statement_; }
  247. auto statement() -> Statement& { return *statement_; }
  248. // Contains names for the pattern and statement. Note that when the
  249. // statement is a block, it gains its own scope.
  250. auto static_scope() const -> const StaticScope& { return static_scope_; }
  251. auto static_scope() -> StaticScope& { return static_scope_; }
  252. private:
  253. Nonnull<Pattern*> pattern_;
  254. Nonnull<Statement*> statement_;
  255. StaticScope static_scope_;
  256. };
  257. Match(SourceLocation source_loc, Nonnull<Expression*> expression,
  258. std::vector<Clause> clauses)
  259. : Statement(Kind::Match, source_loc),
  260. expression_(expression),
  261. clauses_(std::move(clauses)) {}
  262. static auto classof(const Statement* stmt) -> bool {
  263. return stmt->kind() == Kind::Match;
  264. }
  265. auto expression() const -> const Expression& { return *expression_; }
  266. auto expression() -> Expression& { return *expression_; }
  267. auto clauses() const -> llvm::ArrayRef<Clause> { return clauses_; }
  268. auto clauses() -> llvm::MutableArrayRef<Clause> { return clauses_; }
  269. private:
  270. Nonnull<Expression*> expression_;
  271. std::vector<Clause> clauses_;
  272. };
  273. // A continuation statement.
  274. //
  275. // __continuation <continuation_variable> {
  276. // <body>
  277. // }
  278. class Continuation : public Statement, public NamedEntityInterface {
  279. public:
  280. Continuation(SourceLocation source_loc, std::string continuation_variable,
  281. Nonnull<Block*> body)
  282. : Statement(Kind::Continuation, source_loc),
  283. continuation_variable_(std::move(continuation_variable)),
  284. body_(body) {}
  285. auto named_entity_kind() const -> NamedEntityKind override {
  286. return NamedEntityKind::Continuation;
  287. }
  288. auto source_loc() const -> SourceLocation override {
  289. return Statement::source_loc();
  290. }
  291. static auto classof(const Statement* stmt) -> bool {
  292. return stmt->kind() == Kind::Continuation;
  293. }
  294. auto continuation_variable() const -> const std::string& {
  295. return continuation_variable_;
  296. }
  297. auto body() const -> const Block& { return *body_; }
  298. auto body() -> Block& { return *body_; }
  299. private:
  300. std::string continuation_variable_;
  301. Nonnull<Block*> body_;
  302. };
  303. // A run statement.
  304. //
  305. // __run <argument>;
  306. class Run : public Statement {
  307. public:
  308. Run(SourceLocation source_loc, Nonnull<Expression*> argument)
  309. : Statement(Kind::Run, source_loc), argument_(argument) {}
  310. static auto classof(const Statement* stmt) -> bool {
  311. return stmt->kind() == Kind::Run;
  312. }
  313. auto argument() const -> const Expression& { return *argument_; }
  314. auto argument() -> Expression& { return *argument_; }
  315. private:
  316. Nonnull<Expression*> argument_;
  317. };
  318. // An await statement.
  319. //
  320. // __await;
  321. class Await : public Statement {
  322. public:
  323. explicit Await(SourceLocation source_loc)
  324. : Statement(Kind::Await, source_loc) {}
  325. static auto classof(const Statement* stmt) -> bool {
  326. return stmt->kind() == Kind::Await;
  327. }
  328. };
  329. } // namespace Carbon
  330. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_