expression.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 CARBON_EXPLORER_AST_EXPRESSION_H_
  5. #define CARBON_EXPLORER_AST_EXPRESSION_H_
  6. #include <map>
  7. #include <optional>
  8. #include <string>
  9. #include <variant>
  10. #include <vector>
  11. #include "common/ostream.h"
  12. #include "explorer/ast/ast_node.h"
  13. #include "explorer/ast/paren_contents.h"
  14. #include "explorer/ast/static_scope.h"
  15. #include "explorer/ast/value_category.h"
  16. #include "explorer/common/arena.h"
  17. #include "explorer/common/source_location.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace Carbon {
  21. class Value;
  22. class MemberName;
  23. class VariableType;
  24. class ImplBinding;
  25. class Expression : public AstNode {
  26. public:
  27. ~Expression() override = 0;
  28. void Print(llvm::raw_ostream& out) const override;
  29. void PrintID(llvm::raw_ostream& out) const override;
  30. static auto classof(const AstNode* node) {
  31. return InheritsFromExpression(node->kind());
  32. }
  33. // Returns the enumerator corresponding to the most-derived type of this
  34. // object.
  35. auto kind() const -> ExpressionKind {
  36. return static_cast<ExpressionKind>(root_kind());
  37. }
  38. // The static type of this expression. Cannot be called before typechecking.
  39. auto static_type() const -> const Value& {
  40. CARBON_CHECK(static_type_.has_value());
  41. return **static_type_;
  42. }
  43. // Sets the static type of this expression. Can only be called once, during
  44. // typechecking.
  45. void set_static_type(Nonnull<const Value*> type) {
  46. CARBON_CHECK(!static_type_.has_value());
  47. static_type_ = type;
  48. }
  49. // The value category of this expression. Cannot be called before
  50. // typechecking.
  51. auto value_category() const -> ValueCategory { return *value_category_; }
  52. // Sets the value category of this expression. Can be called multiple times,
  53. // but the argument must have the same value each time.
  54. void set_value_category(ValueCategory value_category) {
  55. CARBON_CHECK(!value_category_.has_value() ||
  56. value_category == *value_category_);
  57. value_category_ = value_category;
  58. }
  59. protected:
  60. // Constructs an Expression representing syntax at the given line number.
  61. // `kind` must be the enumerator corresponding to the most-derived type being
  62. // constructed.
  63. Expression(AstNodeKind kind, SourceLocation source_loc)
  64. : AstNode(kind, source_loc) {}
  65. private:
  66. std::optional<Nonnull<const Value*>> static_type_;
  67. std::optional<ValueCategory> value_category_;
  68. };
  69. // A FieldInitializer represents the initialization of a single struct field.
  70. class FieldInitializer {
  71. public:
  72. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  73. : name_(std::move(name)), expression_(expression) {}
  74. auto name() const -> const std::string& { return name_; }
  75. auto expression() const -> const Expression& { return *expression_; }
  76. auto expression() -> Expression& { return *expression_; }
  77. private:
  78. // The field name. Cannot be empty.
  79. std::string name_;
  80. // The expression that initializes the field.
  81. Nonnull<Expression*> expression_;
  82. };
  83. enum class Operator {
  84. Add,
  85. AddressOf,
  86. And,
  87. Deref,
  88. Eq,
  89. Mul,
  90. Neg,
  91. Not,
  92. Or,
  93. Sub,
  94. Ptr,
  95. };
  96. // Returns the lexical representation of `op`, such as "+" for `Add`.
  97. auto ToString(Operator op) -> std::string_view;
  98. class IdentifierExpression : public Expression {
  99. public:
  100. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  101. : Expression(AstNodeKind::IdentifierExpression, source_loc),
  102. name_(std::move(name)) {}
  103. static auto classof(const AstNode* node) -> bool {
  104. return InheritsFromIdentifierExpression(node->kind());
  105. }
  106. auto name() const -> const std::string& { return name_; }
  107. // Returns the ValueNodeView this identifier refers to. Cannot be called
  108. // before name resolution.
  109. auto value_node() const -> const ValueNodeView& { return *value_node_; }
  110. // Sets the value returned by value_node. Can be called only once,
  111. // during name resolution.
  112. void set_value_node(ValueNodeView value_node) {
  113. CARBON_CHECK(!value_node_.has_value());
  114. value_node_ = std::move(value_node);
  115. }
  116. private:
  117. std::string name_;
  118. std::optional<ValueNodeView> value_node_;
  119. };
  120. class FieldAccessExpression : public Expression {
  121. public:
  122. explicit FieldAccessExpression(SourceLocation source_loc,
  123. Nonnull<Expression*> aggregate,
  124. std::string field)
  125. : Expression(AstNodeKind::FieldAccessExpression, source_loc),
  126. aggregate_(aggregate),
  127. field_(std::move(field)) {}
  128. static auto classof(const AstNode* node) -> bool {
  129. return InheritsFromFieldAccessExpression(node->kind());
  130. }
  131. auto aggregate() const -> const Expression& { return *aggregate_; }
  132. auto aggregate() -> Expression& { return *aggregate_; }
  133. auto field() const -> const std::string& { return field_; }
  134. // If `aggregate` has a generic type, returns the `ImplBinding` that
  135. // identifies its witness table. Otherwise, returns `std::nullopt`. Should not
  136. // be called before typechecking.
  137. auto impl() const -> std::optional<Nonnull<const ImplBinding*>> {
  138. return impl_;
  139. }
  140. // Can only be called once, during typechecking.
  141. void set_impl(Nonnull<const ImplBinding*> impl) {
  142. CARBON_CHECK(!impl_.has_value());
  143. impl_ = impl;
  144. }
  145. private:
  146. Nonnull<Expression*> aggregate_;
  147. std::string field_;
  148. std::optional<Nonnull<const ImplBinding*>> impl_;
  149. };
  150. // A compound member access expression of the form `object.(path)`.
  151. //
  152. // `path` is required to have `TypeOfMemberName` type, and describes the member
  153. // being accessed, which is one of:
  154. //
  155. // - An instance member of a type: `object.(Type.member)`.
  156. // - A non-instance member of an interface: `Type.(Interface.member)` or
  157. // `object.(Interface.member)`.
  158. // - An instance member of an interface: `object.(Interface.member)` or
  159. // `object.(Type.(Interface.member))`.
  160. //
  161. // Note that the `path` is evaluated during type-checking, not at runtime, so
  162. // the corresponding `member` is determined statically.
  163. class CompoundFieldAccessExpression : public Expression {
  164. public:
  165. explicit CompoundFieldAccessExpression(SourceLocation source_loc,
  166. Nonnull<Expression*> object,
  167. Nonnull<Expression*> path)
  168. : Expression(AstNodeKind::CompoundFieldAccessExpression, source_loc),
  169. object_(object),
  170. path_(path) {}
  171. static auto classof(const AstNode* node) -> bool {
  172. return InheritsFromCompoundFieldAccessExpression(node->kind());
  173. }
  174. auto object() const -> const Expression& { return *object_; }
  175. auto object() -> Expression& { return *object_; }
  176. auto path() const -> const Expression& { return *path_; }
  177. auto path() -> Expression& { return *path_; }
  178. // Returns the `MemberName` value that evaluation of the path produced.
  179. // Should not be called before typechecking.
  180. auto member() const -> const MemberName& {
  181. CARBON_CHECK(member_.has_value());
  182. return **member_;
  183. }
  184. // Can only be called once, during typechecking.
  185. void set_member(Nonnull<const MemberName*> member) {
  186. CARBON_CHECK(!member_.has_value());
  187. member_ = member;
  188. }
  189. // Returns the expression to use to compute the witness table, if this
  190. // expression names an interface member.
  191. auto impl() const -> std::optional<Nonnull<const Expression*>> {
  192. return impl_;
  193. }
  194. // Can only be called once, during typechecking.
  195. void set_impl(Nonnull<const Expression*> impl) {
  196. CARBON_CHECK(!impl_.has_value());
  197. impl_ = impl;
  198. }
  199. private:
  200. Nonnull<Expression*> object_;
  201. Nonnull<Expression*> path_;
  202. std::optional<Nonnull<const MemberName*>> member_;
  203. std::optional<Nonnull<const Expression*>> impl_;
  204. };
  205. class IndexExpression : public Expression {
  206. public:
  207. explicit IndexExpression(SourceLocation source_loc,
  208. Nonnull<Expression*> aggregate,
  209. Nonnull<Expression*> offset)
  210. : Expression(AstNodeKind::IndexExpression, source_loc),
  211. aggregate_(aggregate),
  212. offset_(offset) {}
  213. static auto classof(const AstNode* node) -> bool {
  214. return InheritsFromIndexExpression(node->kind());
  215. }
  216. auto aggregate() const -> const Expression& { return *aggregate_; }
  217. auto aggregate() -> Expression& { return *aggregate_; }
  218. auto offset() const -> const Expression& { return *offset_; }
  219. auto offset() -> Expression& { return *offset_; }
  220. private:
  221. Nonnull<Expression*> aggregate_;
  222. Nonnull<Expression*> offset_;
  223. };
  224. class IntLiteral : public Expression {
  225. public:
  226. explicit IntLiteral(SourceLocation source_loc, int value)
  227. : Expression(AstNodeKind::IntLiteral, source_loc), value_(value) {}
  228. static auto classof(const AstNode* node) -> bool {
  229. return InheritsFromIntLiteral(node->kind());
  230. }
  231. auto value() const -> int { return value_; }
  232. private:
  233. int value_;
  234. };
  235. class BoolLiteral : public Expression {
  236. public:
  237. explicit BoolLiteral(SourceLocation source_loc, bool value)
  238. : Expression(AstNodeKind::BoolLiteral, source_loc), value_(value) {}
  239. static auto classof(const AstNode* node) -> bool {
  240. return InheritsFromBoolLiteral(node->kind());
  241. }
  242. auto value() const -> bool { return value_; }
  243. private:
  244. bool value_;
  245. };
  246. class StringLiteral : public Expression {
  247. public:
  248. explicit StringLiteral(SourceLocation source_loc, std::string value)
  249. : Expression(AstNodeKind::StringLiteral, source_loc),
  250. value_(std::move(value)) {}
  251. static auto classof(const AstNode* node) -> bool {
  252. return InheritsFromStringLiteral(node->kind());
  253. }
  254. auto value() const -> const std::string& { return value_; }
  255. private:
  256. std::string value_;
  257. };
  258. class StringTypeLiteral : public Expression {
  259. public:
  260. explicit StringTypeLiteral(SourceLocation source_loc)
  261. : Expression(AstNodeKind::StringTypeLiteral, source_loc) {}
  262. static auto classof(const AstNode* node) -> bool {
  263. return InheritsFromStringTypeLiteral(node->kind());
  264. }
  265. };
  266. class TupleLiteral : public Expression {
  267. public:
  268. explicit TupleLiteral(SourceLocation source_loc)
  269. : TupleLiteral(source_loc, {}) {}
  270. explicit TupleLiteral(SourceLocation source_loc,
  271. std::vector<Nonnull<Expression*>> fields)
  272. : Expression(AstNodeKind::TupleLiteral, source_loc),
  273. fields_(std::move(fields)) {}
  274. static auto classof(const AstNode* node) -> bool {
  275. return InheritsFromTupleLiteral(node->kind());
  276. }
  277. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  278. return fields_;
  279. }
  280. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  281. private:
  282. std::vector<Nonnull<Expression*>> fields_;
  283. };
  284. // A non-empty literal value of a struct type.
  285. //
  286. // It can't be empty because the syntax `{}` is a struct type literal as well
  287. // as a literal value of that type, so for consistency we always represent it
  288. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  289. // the two.
  290. class StructLiteral : public Expression {
  291. public:
  292. explicit StructLiteral(SourceLocation loc,
  293. std::vector<FieldInitializer> fields)
  294. : Expression(AstNodeKind::StructLiteral, loc),
  295. fields_(std::move(fields)) {
  296. CARBON_CHECK(!fields_.empty())
  297. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  298. }
  299. static auto classof(const AstNode* node) -> bool {
  300. return InheritsFromStructLiteral(node->kind());
  301. }
  302. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  303. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  304. private:
  305. std::vector<FieldInitializer> fields_;
  306. };
  307. // A literal representing a struct type.
  308. //
  309. // Code that handles this type may sometimes need to have special-case handling
  310. // for `{}`, which is a struct value in addition to being a struct type.
  311. class StructTypeLiteral : public Expression {
  312. public:
  313. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  314. explicit StructTypeLiteral(SourceLocation loc,
  315. std::vector<FieldInitializer> fields)
  316. : Expression(AstNodeKind::StructTypeLiteral, loc),
  317. fields_(std::move(fields)) {}
  318. static auto classof(const AstNode* node) -> bool {
  319. return InheritsFromStructTypeLiteral(node->kind());
  320. }
  321. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  322. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  323. private:
  324. std::vector<FieldInitializer> fields_;
  325. };
  326. class PrimitiveOperatorExpression : public Expression {
  327. public:
  328. explicit PrimitiveOperatorExpression(
  329. SourceLocation source_loc, Operator op,
  330. std::vector<Nonnull<Expression*>> arguments)
  331. : Expression(AstNodeKind::PrimitiveOperatorExpression, source_loc),
  332. op_(op),
  333. arguments_(std::move(arguments)) {}
  334. static auto classof(const AstNode* node) -> bool {
  335. return InheritsFromPrimitiveOperatorExpression(node->kind());
  336. }
  337. auto op() const -> Operator { return op_; }
  338. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  339. return arguments_;
  340. }
  341. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  342. return arguments_;
  343. }
  344. private:
  345. Operator op_;
  346. std::vector<Nonnull<Expression*>> arguments_;
  347. };
  348. class GenericBinding;
  349. using BindingMap =
  350. std::map<Nonnull<const GenericBinding*>, Nonnull<const Value*>>;
  351. using ImplExpMap = std::map<Nonnull<const ImplBinding*>, Nonnull<Expression*>>;
  352. class CallExpression : public Expression {
  353. public:
  354. explicit CallExpression(SourceLocation source_loc,
  355. Nonnull<Expression*> function,
  356. Nonnull<Expression*> argument)
  357. : Expression(AstNodeKind::CallExpression, source_loc),
  358. function_(function),
  359. argument_(argument) {}
  360. static auto classof(const AstNode* node) -> bool {
  361. return InheritsFromCallExpression(node->kind());
  362. }
  363. auto function() const -> const Expression& { return *function_; }
  364. auto function() -> Expression& { return *function_; }
  365. auto argument() const -> const Expression& { return *argument_; }
  366. auto argument() -> Expression& { return *argument_; }
  367. // Maps each of `function`'s impl bindings to an expression
  368. // that constructs a witness table.
  369. // Should not be called before typechecking, or if `function` is not
  370. // a generic function.
  371. auto impls() const -> const ImplExpMap& { return impls_; }
  372. // Can only be called once, during typechecking.
  373. void set_impls(const ImplExpMap& impls) {
  374. CARBON_CHECK(impls_.empty());
  375. impls_ = impls;
  376. }
  377. auto deduced_args() const -> const BindingMap& { return deduced_args_; }
  378. void set_deduced_args(const BindingMap& deduced_args) {
  379. deduced_args_ = deduced_args;
  380. }
  381. private:
  382. Nonnull<Expression*> function_;
  383. Nonnull<Expression*> argument_;
  384. ImplExpMap impls_;
  385. BindingMap deduced_args_;
  386. };
  387. class FunctionTypeLiteral : public Expression {
  388. public:
  389. explicit FunctionTypeLiteral(SourceLocation source_loc,
  390. Nonnull<Expression*> parameter,
  391. Nonnull<Expression*> return_type)
  392. : Expression(AstNodeKind::FunctionTypeLiteral, source_loc),
  393. parameter_(parameter),
  394. return_type_(return_type) {}
  395. static auto classof(const AstNode* node) -> bool {
  396. return InheritsFromFunctionTypeLiteral(node->kind());
  397. }
  398. auto parameter() const -> const Expression& { return *parameter_; }
  399. auto parameter() -> Expression& { return *parameter_; }
  400. auto return_type() const -> const Expression& { return *return_type_; }
  401. auto return_type() -> Expression& { return *return_type_; }
  402. private:
  403. Nonnull<Expression*> parameter_;
  404. Nonnull<Expression*> return_type_;
  405. };
  406. class BoolTypeLiteral : public Expression {
  407. public:
  408. explicit BoolTypeLiteral(SourceLocation source_loc)
  409. : Expression(AstNodeKind::BoolTypeLiteral, source_loc) {}
  410. static auto classof(const AstNode* node) -> bool {
  411. return InheritsFromBoolTypeLiteral(node->kind());
  412. }
  413. };
  414. class IntTypeLiteral : public Expression {
  415. public:
  416. explicit IntTypeLiteral(SourceLocation source_loc)
  417. : Expression(AstNodeKind::IntTypeLiteral, source_loc) {}
  418. static auto classof(const AstNode* node) -> bool {
  419. return InheritsFromIntTypeLiteral(node->kind());
  420. }
  421. };
  422. class ContinuationTypeLiteral : public Expression {
  423. public:
  424. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  425. : Expression(AstNodeKind::ContinuationTypeLiteral, source_loc) {}
  426. static auto classof(const AstNode* node) -> bool {
  427. return InheritsFromContinuationTypeLiteral(node->kind());
  428. }
  429. };
  430. class TypeTypeLiteral : public Expression {
  431. public:
  432. explicit TypeTypeLiteral(SourceLocation source_loc)
  433. : Expression(AstNodeKind::TypeTypeLiteral, source_loc) {}
  434. static auto classof(const AstNode* node) -> bool {
  435. return InheritsFromTypeTypeLiteral(node->kind());
  436. }
  437. };
  438. class IntrinsicExpression : public Expression {
  439. public:
  440. enum class Intrinsic {
  441. Print,
  442. };
  443. // Returns the enumerator corresponding to the intrinsic named `name`,
  444. // or raises a fatal compile error if there is no such enumerator.
  445. static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
  446. -> ErrorOr<Intrinsic>;
  447. explicit IntrinsicExpression(Intrinsic intrinsic, Nonnull<TupleLiteral*> args,
  448. SourceLocation source_loc)
  449. : Expression(AstNodeKind::IntrinsicExpression, source_loc),
  450. intrinsic_(intrinsic),
  451. args_(args) {}
  452. static auto classof(const AstNode* node) -> bool {
  453. return InheritsFromIntrinsicExpression(node->kind());
  454. }
  455. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  456. auto args() const -> const TupleLiteral& { return *args_; }
  457. auto args() -> TupleLiteral& { return *args_; }
  458. private:
  459. Intrinsic intrinsic_;
  460. Nonnull<TupleLiteral*> args_;
  461. };
  462. class IfExpression : public Expression {
  463. public:
  464. explicit IfExpression(SourceLocation source_loc,
  465. Nonnull<Expression*> condition,
  466. Nonnull<Expression*> then_expression,
  467. Nonnull<Expression*> else_expression)
  468. : Expression(AstNodeKind::IfExpression, source_loc),
  469. condition_(condition),
  470. then_expression_(then_expression),
  471. else_expression_(else_expression) {}
  472. static auto classof(const AstNode* node) -> bool {
  473. return InheritsFromIfExpression(node->kind());
  474. }
  475. auto condition() const -> const Expression& { return *condition_; }
  476. auto condition() -> Expression& { return *condition_; }
  477. auto then_expression() const -> const Expression& {
  478. return *then_expression_;
  479. }
  480. auto then_expression() -> Expression& { return *then_expression_; }
  481. auto else_expression() const -> const Expression& {
  482. return *else_expression_;
  483. }
  484. auto else_expression() -> Expression& { return *else_expression_; }
  485. private:
  486. Nonnull<Expression*> condition_;
  487. Nonnull<Expression*> then_expression_;
  488. Nonnull<Expression*> else_expression_;
  489. };
  490. // Instantiate a generic impl.
  491. class InstantiateImpl : public Expression {
  492. public:
  493. using ImplementsCarbonValueNode = void;
  494. explicit InstantiateImpl(SourceLocation source_loc,
  495. Nonnull<Expression*> generic_impl,
  496. const BindingMap& type_args, const ImplExpMap& impls)
  497. : Expression(AstNodeKind::InstantiateImpl, source_loc),
  498. generic_impl_(generic_impl),
  499. type_args_(type_args),
  500. impls_(impls) {}
  501. static auto classof(const AstNode* node) -> bool {
  502. return InheritsFromInstantiateImpl(node->kind());
  503. }
  504. auto generic_impl() const -> Nonnull<Expression*> { return generic_impl_; }
  505. auto type_args() const -> const BindingMap& { return type_args_; }
  506. // Maps each of the impl bindings to an expression that constructs
  507. // the witness table for that impl.
  508. auto impls() const -> const ImplExpMap& { return impls_; }
  509. private:
  510. Nonnull<Expression*> generic_impl_;
  511. BindingMap type_args_;
  512. ImplExpMap impls_;
  513. };
  514. // An expression whose semantics have not been implemented. This can be used
  515. // as a placeholder during development, in order to implement and test parsing
  516. // of a new expression syntax without having to implement its semantics.
  517. class UnimplementedExpression : public Expression {
  518. public:
  519. // Constructs an UnimplementedExpression with the given label and the given
  520. // children, which must all be convertible to Nonnull<AstNode*>. The label
  521. // should correspond roughly to the name of the class that will eventually
  522. // replace this usage of UnimplementedExpression.
  523. template <typename... Children>
  524. UnimplementedExpression(SourceLocation source_loc, std::string label,
  525. Children... children)
  526. : Expression(AstNodeKind::UnimplementedExpression, source_loc),
  527. label_(std::move(label)) {
  528. AddChildren(children...);
  529. }
  530. static auto classof(const AstNode* node) -> bool {
  531. return InheritsFromUnimplementedExpression(node->kind());
  532. }
  533. auto label() const -> std::string_view { return label_; }
  534. auto children() const -> llvm::ArrayRef<Nonnull<const AstNode*>> {
  535. return children_;
  536. }
  537. private:
  538. void AddChildren() {}
  539. template <typename... Children>
  540. void AddChildren(Nonnull<AstNode*> child, Children... children) {
  541. children_.push_back(child);
  542. AddChildren(children...);
  543. }
  544. std::string label_;
  545. std::vector<Nonnull<AstNode*>> children_;
  546. };
  547. // A literal representing a statically-sized array type.
  548. class ArrayTypeLiteral : public Expression {
  549. public:
  550. // Constructs an array type literal which uses the given expressions to
  551. // represent the element type and size.
  552. ArrayTypeLiteral(SourceLocation source_loc,
  553. Nonnull<Expression*> element_type_expression,
  554. Nonnull<Expression*> size_expression)
  555. : Expression(AstNodeKind::ArrayTypeLiteral, source_loc),
  556. element_type_expression_(element_type_expression),
  557. size_expression_(size_expression) {}
  558. static auto classof(const AstNode* node) -> bool {
  559. return InheritsFromArrayTypeLiteral(node->kind());
  560. }
  561. auto element_type_expression() const -> const Expression& {
  562. return *element_type_expression_;
  563. }
  564. auto element_type_expression() -> Expression& {
  565. return *element_type_expression_;
  566. }
  567. auto size_expression() const -> const Expression& {
  568. return *size_expression_;
  569. }
  570. auto size_expression() -> Expression& { return *size_expression_; }
  571. private:
  572. Nonnull<Expression*> element_type_expression_;
  573. Nonnull<Expression*> size_expression_;
  574. };
  575. // Converts paren_contents to an Expression, interpreting the parentheses as
  576. // grouping if their contents permit that interpretation, or as forming a
  577. // tuple otherwise.
  578. auto ExpressionFromParenContents(
  579. Nonnull<Arena*> arena, SourceLocation source_loc,
  580. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  581. // Converts paren_contents to an Expression, interpreting the parentheses as
  582. // forming a tuple.
  583. auto TupleExpressionFromParenContents(
  584. Nonnull<Arena*> arena, SourceLocation source_loc,
  585. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  586. } // namespace Carbon
  587. #endif // CARBON_EXPLORER_AST_EXPRESSION_H_