expression.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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 <utility>
  10. #include <variant>
  11. #include <vector>
  12. #include "common/ostream.h"
  13. #include "explorer/ast/ast_node.h"
  14. #include "explorer/ast/bindings.h"
  15. #include "explorer/ast/element.h"
  16. #include "explorer/ast/expression_category.h"
  17. #include "explorer/ast/paren_contents.h"
  18. #include "explorer/ast/value_node.h"
  19. #include "explorer/common/arena.h"
  20. #include "explorer/common/source_location.h"
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/Support/Compiler.h"
  23. namespace Carbon {
  24. class Value;
  25. class Witness;
  26. class MemberName;
  27. class VariableType;
  28. class InterfaceType;
  29. class ImplBinding;
  30. class GenericBinding;
  31. class Expression : public AstNode {
  32. public:
  33. ~Expression() override = 0;
  34. void Print(llvm::raw_ostream& out) const override;
  35. void PrintID(llvm::raw_ostream& out) const override;
  36. static auto classof(const AstNode* node) {
  37. return InheritsFromExpression(node->kind());
  38. }
  39. // Returns the enumerator corresponding to the most-derived type of this
  40. // object.
  41. auto kind() const -> ExpressionKind {
  42. return static_cast<ExpressionKind>(root_kind());
  43. }
  44. // The static type of this expression. Cannot be called before typechecking.
  45. auto static_type() const -> const Value& {
  46. CARBON_CHECK(static_type_.has_value());
  47. return **static_type_;
  48. }
  49. // Sets the static type of this expression. Can only be called once, during
  50. // typechecking.
  51. void set_static_type(Nonnull<const Value*> type) {
  52. CARBON_CHECK(!static_type_.has_value());
  53. static_type_ = type;
  54. }
  55. // The value category of this expression. Cannot be called before
  56. // typechecking.
  57. auto expression_category() const -> ExpressionCategory {
  58. return *expression_category_;
  59. }
  60. // Sets the value category of this expression. Can be called multiple times,
  61. // but the argument must have the same value each time.
  62. void set_expression_category(ExpressionCategory expression_category) {
  63. CARBON_CHECK(!expression_category_.has_value() ||
  64. expression_category == *expression_category_);
  65. expression_category_ = expression_category;
  66. }
  67. // Determines whether the expression has already been type-checked. Should
  68. // only be used by type-checking.
  69. auto is_type_checked() const -> bool {
  70. return static_type_.has_value() && expression_category_.has_value();
  71. }
  72. protected:
  73. // Constructs an Expression representing syntax at the given line number.
  74. // `kind` must be the enumerator corresponding to the most-derived type being
  75. // constructed.
  76. Expression(AstNodeKind kind, SourceLocation source_loc)
  77. : AstNode(kind, source_loc) {}
  78. explicit Expression(CloneContext& context, const Expression& other)
  79. : AstNode(context, other) {}
  80. private:
  81. std::optional<Nonnull<const Value*>> static_type_;
  82. std::optional<ExpressionCategory> expression_category_;
  83. };
  84. // A mixin for expressions that can be rewritten to a different expression by
  85. // type-checking.
  86. template <typename Base>
  87. class RewritableMixin : public Base {
  88. public:
  89. using Base::Base;
  90. explicit RewritableMixin(CloneContext& context, const RewritableMixin& other)
  91. : Base(context, other),
  92. rewritten_form_(context.Clone(other.rewritten_form_)) {}
  93. // Set the rewritten form of this expression. Can only be called during type
  94. // checking.
  95. auto set_rewritten_form(Nonnull<const Expression*> rewritten_form) -> void {
  96. CARBON_CHECK(!rewritten_form_.has_value()) << "rewritten form set twice";
  97. rewritten_form_ = rewritten_form;
  98. this->set_static_type(&rewritten_form->static_type());
  99. this->set_expression_category(rewritten_form->expression_category());
  100. }
  101. // Get the rewritten form of this expression. A rewritten form is used when
  102. // the expression is rewritten as a function call on an interface. A
  103. // rewritten form is not used when providing built-in operator semantics.
  104. auto rewritten_form() const -> std::optional<Nonnull<const Expression*>> {
  105. return rewritten_form_;
  106. }
  107. private:
  108. std::optional<Nonnull<const Expression*>> rewritten_form_;
  109. };
  110. // A FieldInitializer represents the initialization of a single struct field.
  111. class FieldInitializer {
  112. public:
  113. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  114. : name_(std::move(name)), expression_(expression) {}
  115. explicit FieldInitializer(CloneContext& context,
  116. const FieldInitializer& other)
  117. : name_(other.name_), expression_(context.Clone(other.expression_)) {}
  118. auto name() const -> const std::string& { return name_; }
  119. auto expression() const -> const Expression& { return *expression_; }
  120. auto expression() -> Expression& { return *expression_; }
  121. private:
  122. // The field name. Cannot be empty.
  123. std::string name_;
  124. // The expression that initializes the field.
  125. Nonnull<Expression*> expression_;
  126. };
  127. enum class Operator {
  128. Add,
  129. AddressOf,
  130. And,
  131. As,
  132. BitwiseAnd,
  133. BitwiseOr,
  134. BitwiseXor,
  135. BitShiftLeft,
  136. BitShiftRight,
  137. Complement,
  138. Deref,
  139. Div,
  140. Eq,
  141. Less,
  142. LessEq,
  143. Greater,
  144. GreaterEq,
  145. Mul,
  146. Mod,
  147. Neg,
  148. Not,
  149. NotEq,
  150. Or,
  151. Sub,
  152. Ptr,
  153. };
  154. // Returns the lexical representation of `op`, such as "+" for `Add`.
  155. auto OperatorToString(Operator op) -> std::string_view;
  156. class IdentifierExpression : public Expression {
  157. public:
  158. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  159. : Expression(AstNodeKind::IdentifierExpression, source_loc),
  160. name_(std::move(name)) {}
  161. explicit IdentifierExpression(CloneContext& context,
  162. const IdentifierExpression& other)
  163. : Expression(context, other),
  164. name_(other.name_),
  165. value_node_(context.Clone(other.value_node_)) {}
  166. static auto classof(const AstNode* node) -> bool {
  167. return InheritsFromIdentifierExpression(node->kind());
  168. }
  169. auto name() const -> const std::string& { return name_; }
  170. // Returns the ValueNodeView this identifier refers to. Cannot be called
  171. // before name resolution.
  172. auto value_node() const -> const ValueNodeView& { return *value_node_; }
  173. // Sets the value returned by value_node. Can be called only during name
  174. // resolution.
  175. void set_value_node(ValueNodeView value_node) {
  176. CARBON_CHECK(!value_node_.has_value() || value_node_ == value_node);
  177. value_node_ = std::move(value_node);
  178. }
  179. private:
  180. std::string name_;
  181. std::optional<ValueNodeView> value_node_;
  182. };
  183. // A `.Self` expression within either a `:!` binding or a standalone `where`
  184. // expression.
  185. //
  186. // In a `:!` binding, the type of `.Self` is always `type`. For example, in
  187. // `A:! AddableWith(.Self)`, the expression `.Self` refers to the same type as
  188. // `A`, but with type `type`.
  189. //
  190. // In a `where` binding, the type of `.Self` is the constraint preceding the
  191. // `where` keyword. For example, in `Foo where .Result impls Bar(.Self)`, the
  192. // type of `.Self` is `Foo`.
  193. class DotSelfExpression : public Expression {
  194. public:
  195. explicit DotSelfExpression(SourceLocation source_loc)
  196. : Expression(AstNodeKind::DotSelfExpression, source_loc) {}
  197. explicit DotSelfExpression(CloneContext& context,
  198. const DotSelfExpression& other);
  199. static auto classof(const AstNode* node) -> bool {
  200. return InheritsFromDotSelfExpression(node->kind());
  201. }
  202. // The self binding. Cannot be called before name resolution.
  203. auto self_binding() const -> const GenericBinding& { return **self_binding_; }
  204. auto self_binding() -> GenericBinding& { return **self_binding_; }
  205. // Sets the self binding. Called only during name resolution.
  206. void set_self_binding(Nonnull<GenericBinding*> self_binding) {
  207. CARBON_CHECK(!self_binding_.has_value() || self_binding_ == self_binding);
  208. self_binding_ = self_binding;
  209. }
  210. private:
  211. std::string name_;
  212. std::optional<Nonnull<GenericBinding*>> self_binding_;
  213. };
  214. class MemberAccessExpression : public Expression {
  215. public:
  216. explicit MemberAccessExpression(AstNodeKind kind, SourceLocation source_loc,
  217. Nonnull<Expression*> object)
  218. : Expression(kind, source_loc), object_(object) {}
  219. explicit MemberAccessExpression(CloneContext& context,
  220. const MemberAccessExpression& other);
  221. static auto classof(const AstNode* node) -> bool {
  222. return InheritsFromMemberAccessExpression(node->kind());
  223. }
  224. auto object() const -> const Expression& { return *object_; }
  225. auto object() -> Expression& { return *object_; }
  226. // Can only be called by type-checking, if a conversion was required.
  227. void set_object(Nonnull<Expression*> object) { object_ = object; }
  228. // Returns true if this is an access of a member of the type of the object,
  229. // rather than an access of a member of the object itself. In this case, the
  230. // value of the object expression is ignored, and the type is accessed
  231. // instead.
  232. //
  233. // For example, given `x: Class`, `x.StaticFunction` is a type access
  234. // equivalent to `T.StaticFunction`, and given `T:! Interface` and `y: T`,
  235. // `y.AssociatedConstant` is a type access equivalent to
  236. // `T.AssociatedConstant`.
  237. auto is_type_access() const -> bool { return is_type_access_; }
  238. // Can only be called once, during typechecking.
  239. void set_is_type_access(bool type_access) { is_type_access_ = type_access; }
  240. // Returns true if the member is a method that has a "self" declaration in an
  241. // AddrPattern.
  242. auto is_addr_me_method() const -> bool { return is_addr_me_method_; }
  243. // Can only be called once, during typechecking.
  244. void set_is_addr_me_method() { is_addr_me_method_ = true; }
  245. // If `object` has a generic type, returns the witness value, which might be
  246. // either concrete or symbolic. Otherwise, returns `std::nullopt`. Should not
  247. // be called before typechecking.
  248. auto impl() const -> std::optional<Nonnull<const Witness*>> { return impl_; }
  249. // Can only be called once, during typechecking.
  250. void set_impl(Nonnull<const Witness*> impl) {
  251. CARBON_CHECK(!impl_.has_value());
  252. impl_ = impl;
  253. }
  254. // Returns the constant value of this expression, if one has been set. This
  255. // value will be used instead of accessing a member. Even if this is present,
  256. // the operand of the member access expression must still be evaluated, in
  257. // case it has side effects.
  258. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  259. return constant_value_;
  260. }
  261. // Sets the value returned by constant_value(). Can only be called once,
  262. // during typechecking.
  263. void set_constant_value(Nonnull<const Value*> value) {
  264. CARBON_CHECK(!constant_value_.has_value());
  265. constant_value_ = value;
  266. }
  267. private:
  268. Nonnull<Expression*> object_;
  269. bool is_type_access_ = false;
  270. bool is_addr_me_method_ = false;
  271. std::optional<Nonnull<const Witness*>> impl_;
  272. std::optional<Nonnull<const Value*>> constant_value_;
  273. };
  274. class SimpleMemberAccessExpression
  275. : public RewritableMixin<MemberAccessExpression> {
  276. public:
  277. explicit SimpleMemberAccessExpression(SourceLocation source_loc,
  278. Nonnull<Expression*> object,
  279. std::string member_name)
  280. : RewritableMixin(AstNodeKind::SimpleMemberAccessExpression, source_loc,
  281. object),
  282. member_name_(std::move(member_name)) {}
  283. explicit SimpleMemberAccessExpression(
  284. CloneContext& context, const SimpleMemberAccessExpression& other);
  285. static auto classof(const AstNode* node) -> bool {
  286. return InheritsFromSimpleMemberAccessExpression(node->kind());
  287. }
  288. auto member_name() const -> const std::string& { return member_name_; }
  289. // Returns the `NamedElement` that the member name resolved to.
  290. // Should not be called before typechecking.
  291. auto member() const -> const NamedElement& {
  292. CARBON_CHECK(member_.has_value());
  293. return *member_.value();
  294. }
  295. // Can only be called once, during typechecking.
  296. void set_member(Nonnull<const NamedElement*> member) { member_ = member; }
  297. // If `object` is a constrained type parameter and `member` was found in an
  298. // interface, returns that interface. Should not be called before
  299. // typechecking.
  300. auto found_in_interface() const
  301. -> std::optional<Nonnull<const InterfaceType*>> {
  302. return found_in_interface_;
  303. }
  304. // Can only be called once, during typechecking.
  305. void set_found_in_interface(Nonnull<const InterfaceType*> interface) {
  306. CARBON_CHECK(!found_in_interface_.has_value());
  307. found_in_interface_ = interface;
  308. }
  309. // Returns the ValueNodeView this identifier refers to, if this was
  310. // determined by name resolution. Cannot be called before name resolution.
  311. auto value_node() const -> std::optional<ValueNodeView> {
  312. return value_node_;
  313. }
  314. // Sets the value returned by value_node. Can be called only during name
  315. // resolution.
  316. void set_value_node(ValueNodeView value_node) {
  317. CARBON_CHECK(!value_node_.has_value() || value_node_ == value_node);
  318. value_node_ = std::move(value_node);
  319. }
  320. private:
  321. std::string member_name_;
  322. std::optional<Nonnull<const NamedElement*>> member_;
  323. std::optional<Nonnull<const InterfaceType*>> found_in_interface_;
  324. std::optional<ValueNodeView> value_node_;
  325. };
  326. // A compound member access expression of the form `object.(path)`.
  327. //
  328. // `path` is required to have `TypeOfMemberName` type, and describes the member
  329. // being accessed, which is one of:
  330. //
  331. // - An instance member of a type: `object.(Type.member)`.
  332. // - A non-instance member of an interface: `Type.(Interface.member)` or
  333. // `object.(Interface.member)`.
  334. // - An instance member of an interface: `object.(Interface.member)` or
  335. // `object.(Type.(Interface.member))`.
  336. //
  337. // Note that the `path` is evaluated during type-checking, not at runtime, so
  338. // the corresponding `member` is determined statically.
  339. class CompoundMemberAccessExpression : public MemberAccessExpression {
  340. public:
  341. explicit CompoundMemberAccessExpression(SourceLocation source_loc,
  342. Nonnull<Expression*> object,
  343. Nonnull<Expression*> path)
  344. : MemberAccessExpression(AstNodeKind::CompoundMemberAccessExpression,
  345. source_loc, object),
  346. path_(path) {}
  347. explicit CompoundMemberAccessExpression(
  348. CloneContext& context, const CompoundMemberAccessExpression& other);
  349. static auto classof(const AstNode* node) -> bool {
  350. return InheritsFromCompoundMemberAccessExpression(node->kind());
  351. }
  352. auto path() const -> const Expression& { return *path_; }
  353. auto path() -> Expression& { return *path_; }
  354. // Returns the `MemberName` value that evaluation of the path produced.
  355. // Should not be called before typechecking.
  356. auto member() const -> const MemberName& {
  357. CARBON_CHECK(member_.has_value());
  358. return **member_;
  359. }
  360. // Can only be called once, during typechecking.
  361. void set_member(Nonnull<const MemberName*> member) {
  362. CARBON_CHECK(!member_.has_value());
  363. member_ = member;
  364. }
  365. private:
  366. Nonnull<Expression*> path_;
  367. std::optional<Nonnull<const MemberName*>> member_;
  368. };
  369. class IndexExpression : public Expression {
  370. public:
  371. explicit IndexExpression(SourceLocation source_loc,
  372. Nonnull<Expression*> object,
  373. Nonnull<Expression*> offset)
  374. : Expression(AstNodeKind::IndexExpression, source_loc),
  375. object_(object),
  376. offset_(offset) {}
  377. explicit IndexExpression(CloneContext& context, const IndexExpression& other)
  378. : Expression(context, other),
  379. object_(context.Clone(other.object_)),
  380. offset_(context.Clone(other.offset_)) {}
  381. static auto classof(const AstNode* node) -> bool {
  382. return InheritsFromIndexExpression(node->kind());
  383. }
  384. auto object() const -> const Expression& { return *object_; }
  385. auto object() -> Expression& { return *object_; }
  386. auto offset() const -> const Expression& { return *offset_; }
  387. auto offset() -> Expression& { return *offset_; }
  388. private:
  389. Nonnull<Expression*> object_;
  390. Nonnull<Expression*> offset_;
  391. };
  392. class BaseAccessExpression : public MemberAccessExpression {
  393. public:
  394. explicit BaseAccessExpression(SourceLocation source_loc,
  395. Nonnull<Expression*> object,
  396. Nonnull<const BaseElement*> base)
  397. : MemberAccessExpression(AstNodeKind::BaseAccessExpression, source_loc,
  398. object),
  399. base_(base) {
  400. set_static_type(&base->type());
  401. set_expression_category(ExpressionCategory::Value);
  402. }
  403. explicit BaseAccessExpression(CloneContext& context,
  404. const BaseAccessExpression& other)
  405. : MemberAccessExpression(context, other),
  406. base_(context.Clone(other.base_)) {}
  407. static auto classof(const AstNode* node) -> bool {
  408. return InheritsFromBaseAccessExpression(node->kind());
  409. }
  410. auto element() const -> const BaseElement& { return *base_; }
  411. private:
  412. const Nonnull<const BaseElement*> base_;
  413. };
  414. class IntLiteral : public Expression {
  415. public:
  416. explicit IntLiteral(SourceLocation source_loc, int value)
  417. : Expression(AstNodeKind::IntLiteral, source_loc), value_(value) {}
  418. explicit IntLiteral(CloneContext& context, const IntLiteral& other)
  419. : Expression(context, other), value_(other.value_) {}
  420. static auto classof(const AstNode* node) -> bool {
  421. return InheritsFromIntLiteral(node->kind());
  422. }
  423. auto value() const -> int { return value_; }
  424. private:
  425. int value_;
  426. };
  427. class BoolLiteral : public Expression {
  428. public:
  429. explicit BoolLiteral(SourceLocation source_loc, bool value)
  430. : Expression(AstNodeKind::BoolLiteral, source_loc), value_(value) {}
  431. explicit BoolLiteral(CloneContext& context, const BoolLiteral& other)
  432. : Expression(context, other), value_(other.value_) {}
  433. static auto classof(const AstNode* node) -> bool {
  434. return InheritsFromBoolLiteral(node->kind());
  435. }
  436. auto value() const -> bool { return value_; }
  437. private:
  438. bool value_;
  439. };
  440. class StringLiteral : public Expression {
  441. public:
  442. explicit StringLiteral(SourceLocation source_loc, std::string value)
  443. : Expression(AstNodeKind::StringLiteral, source_loc),
  444. value_(std::move(value)) {}
  445. explicit StringLiteral(CloneContext& context, const StringLiteral& other)
  446. : Expression(context, other), value_(other.value_) {}
  447. static auto classof(const AstNode* node) -> bool {
  448. return InheritsFromStringLiteral(node->kind());
  449. }
  450. auto value() const -> const std::string& { return value_; }
  451. private:
  452. std::string value_;
  453. };
  454. class StringTypeLiteral : public Expression {
  455. public:
  456. explicit StringTypeLiteral(SourceLocation source_loc)
  457. : Expression(AstNodeKind::StringTypeLiteral, source_loc) {}
  458. explicit StringTypeLiteral(CloneContext& context,
  459. const StringTypeLiteral& other)
  460. : Expression(context, other) {}
  461. static auto classof(const AstNode* node) -> bool {
  462. return InheritsFromStringTypeLiteral(node->kind());
  463. }
  464. };
  465. class TupleLiteral : public Expression {
  466. public:
  467. explicit TupleLiteral(SourceLocation source_loc)
  468. : TupleLiteral(source_loc, {}) {}
  469. explicit TupleLiteral(SourceLocation source_loc,
  470. std::vector<Nonnull<Expression*>> fields)
  471. : Expression(AstNodeKind::TupleLiteral, source_loc),
  472. fields_(std::move(fields)) {}
  473. explicit TupleLiteral(CloneContext& context, const TupleLiteral& other)
  474. : Expression(context, other), fields_(context.Clone(other.fields_)) {}
  475. static auto classof(const AstNode* node) -> bool {
  476. return InheritsFromTupleLiteral(node->kind());
  477. }
  478. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  479. return fields_;
  480. }
  481. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  482. private:
  483. std::vector<Nonnull<Expression*>> fields_;
  484. };
  485. // A literal value of a struct type.
  486. class StructLiteral : public Expression {
  487. public:
  488. explicit StructLiteral(SourceLocation loc) : StructLiteral(loc, {}) {}
  489. explicit StructLiteral(SourceLocation loc,
  490. std::vector<FieldInitializer> fields)
  491. : Expression(AstNodeKind::StructLiteral, loc),
  492. fields_(std::move(fields)) {}
  493. explicit StructLiteral(CloneContext& context, const StructLiteral& other)
  494. : Expression(context, other), fields_(context.Clone(other.fields_)) {}
  495. static auto classof(const AstNode* node) -> bool {
  496. return InheritsFromStructLiteral(node->kind());
  497. }
  498. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  499. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  500. private:
  501. std::vector<FieldInitializer> fields_;
  502. };
  503. // A base class for literals with a constant value determined by type-checking.
  504. class ConstantValueLiteral : public Expression {
  505. public:
  506. explicit ConstantValueLiteral(
  507. AstNodeKind kind, SourceLocation source_loc,
  508. std::optional<Nonnull<const Value*>> constant_value = std::nullopt)
  509. : Expression(kind, source_loc), constant_value_(constant_value) {}
  510. explicit ConstantValueLiteral(CloneContext& context,
  511. const ConstantValueLiteral& other)
  512. : Expression(context, other),
  513. constant_value_(context.Clone(other.constant_value_)) {}
  514. static auto classof(const AstNode* node) -> bool {
  515. return InheritsFromConstantValueLiteral(node->kind());
  516. }
  517. // Returns the constant value of this expression.
  518. auto constant_value() const -> const Value& {
  519. CARBON_CHECK(constant_value_);
  520. return **constant_value_;
  521. }
  522. // Sets the value returned by constant_value(). Can only be called once,
  523. // during typechecking.
  524. void set_constant_value(Nonnull<const Value*> value) {
  525. CARBON_CHECK(!constant_value_.has_value());
  526. constant_value_ = value;
  527. }
  528. private:
  529. std::optional<Nonnull<const Value*>> constant_value_;
  530. };
  531. // A literal representing a struct type.
  532. //
  533. // Note that a struct type literal can't be empty because `{}` is a struct
  534. // value. However, that value implicitly converts to a type.
  535. class StructTypeLiteral : public ConstantValueLiteral {
  536. public:
  537. explicit StructTypeLiteral(SourceLocation loc,
  538. std::vector<FieldInitializer> fields)
  539. : ConstantValueLiteral(AstNodeKind::StructTypeLiteral, loc),
  540. fields_(std::move(fields)) {
  541. CARBON_CHECK(!fields_.empty())
  542. << "`{}` is represented as a StructLiteral, not a StructTypeLiteral.";
  543. }
  544. explicit StructTypeLiteral(CloneContext& context,
  545. const StructTypeLiteral& other)
  546. : ConstantValueLiteral(context, other),
  547. fields_(context.Clone(other.fields_)) {}
  548. static auto classof(const AstNode* node) -> bool {
  549. return InheritsFromStructTypeLiteral(node->kind());
  550. }
  551. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  552. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  553. private:
  554. std::vector<FieldInitializer> fields_;
  555. };
  556. class OperatorExpression : public RewritableMixin<Expression> {
  557. public:
  558. explicit OperatorExpression(SourceLocation source_loc, Operator op,
  559. std::vector<Nonnull<Expression*>> arguments)
  560. : RewritableMixin(AstNodeKind::OperatorExpression, source_loc),
  561. op_(op),
  562. arguments_(std::move(arguments)) {}
  563. explicit OperatorExpression(CloneContext& context,
  564. const OperatorExpression& other)
  565. : RewritableMixin(context, other),
  566. op_(other.op_),
  567. arguments_(context.Clone(other.arguments_)) {}
  568. static auto classof(const AstNode* node) -> bool {
  569. return InheritsFromOperatorExpression(node->kind());
  570. }
  571. auto op() const -> Operator { return op_; }
  572. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  573. return arguments_;
  574. }
  575. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  576. return arguments_;
  577. }
  578. private:
  579. Operator op_;
  580. std::vector<Nonnull<Expression*>> arguments_;
  581. };
  582. class CallExpression : public Expression {
  583. public:
  584. explicit CallExpression(SourceLocation source_loc,
  585. Nonnull<Expression*> function,
  586. Nonnull<Expression*> argument)
  587. : Expression(AstNodeKind::CallExpression, source_loc),
  588. function_(function),
  589. argument_(argument),
  590. bindings_({}, {}) {}
  591. explicit CallExpression(CloneContext& context, const CallExpression& other)
  592. : Expression(context, other),
  593. function_(context.Clone(other.function_)),
  594. argument_(context.Clone(other.argument_)),
  595. bindings_(context.Clone(other.bindings_)) {}
  596. static auto classof(const AstNode* node) -> bool {
  597. return InheritsFromCallExpression(node->kind());
  598. }
  599. auto function() const -> const Expression& { return *function_; }
  600. auto function() -> Expression& { return *function_; }
  601. auto argument() const -> const Expression& { return *argument_; }
  602. auto argument() -> Expression& { return *argument_; }
  603. auto bindings() -> const Bindings& { return bindings_; }
  604. // Can only be called once, during typechecking.
  605. void set_bindings(Bindings bindings) {
  606. CARBON_CHECK(bindings_.args().empty() && bindings_.witnesses().empty());
  607. bindings_ = std::move(bindings);
  608. }
  609. auto deduced_args() const -> const BindingMap& { return bindings_.args(); }
  610. // Maps each of `function`'s impl bindings to a witness.
  611. // Should not be called before typechecking, or if `function` is not
  612. // a generic function.
  613. auto witnesses() const -> const ImplWitnessMap& {
  614. return bindings_.witnesses();
  615. }
  616. // Can only be called by type-checking, if a conversion was required.
  617. void set_argument(Nonnull<Expression*> argument) { argument_ = argument; }
  618. private:
  619. Nonnull<Expression*> function_;
  620. Nonnull<Expression*> argument_;
  621. Bindings bindings_;
  622. };
  623. class FunctionTypeLiteral : public ConstantValueLiteral {
  624. public:
  625. explicit FunctionTypeLiteral(SourceLocation source_loc,
  626. Nonnull<TupleLiteral*> parameter,
  627. Nonnull<Expression*> return_type)
  628. : ConstantValueLiteral(AstNodeKind::FunctionTypeLiteral, source_loc),
  629. parameter_(parameter),
  630. return_type_(return_type) {}
  631. explicit FunctionTypeLiteral(CloneContext& context,
  632. const FunctionTypeLiteral& other)
  633. : ConstantValueLiteral(context, other),
  634. parameter_(context.Clone(other.parameter_)),
  635. return_type_(context.Clone(other.return_type_)) {}
  636. static auto classof(const AstNode* node) -> bool {
  637. return InheritsFromFunctionTypeLiteral(node->kind());
  638. }
  639. auto parameter() const -> const TupleLiteral& { return *parameter_; }
  640. auto parameter() -> TupleLiteral& { return *parameter_; }
  641. auto return_type() const -> const Expression& { return *return_type_; }
  642. auto return_type() -> Expression& { return *return_type_; }
  643. private:
  644. Nonnull<TupleLiteral*> parameter_;
  645. Nonnull<Expression*> return_type_;
  646. };
  647. class BoolTypeLiteral : public Expression {
  648. public:
  649. explicit BoolTypeLiteral(SourceLocation source_loc)
  650. : Expression(AstNodeKind::BoolTypeLiteral, source_loc) {}
  651. explicit BoolTypeLiteral(CloneContext& context, const BoolTypeLiteral& other)
  652. : Expression(context, other) {}
  653. static auto classof(const AstNode* node) -> bool {
  654. return InheritsFromBoolTypeLiteral(node->kind());
  655. }
  656. };
  657. class IntTypeLiteral : public Expression {
  658. public:
  659. explicit IntTypeLiteral(SourceLocation source_loc)
  660. : Expression(AstNodeKind::IntTypeLiteral, source_loc) {}
  661. explicit IntTypeLiteral(CloneContext& context, const IntTypeLiteral& other)
  662. : Expression(context, other) {}
  663. static auto classof(const AstNode* node) -> bool {
  664. return InheritsFromIntTypeLiteral(node->kind());
  665. }
  666. };
  667. class TypeTypeLiteral : public Expression {
  668. public:
  669. explicit TypeTypeLiteral(SourceLocation source_loc)
  670. : Expression(AstNodeKind::TypeTypeLiteral, source_loc) {}
  671. explicit TypeTypeLiteral(CloneContext& context, const TypeTypeLiteral& other)
  672. : Expression(context, other) {}
  673. static auto classof(const AstNode* node) -> bool {
  674. return InheritsFromTypeTypeLiteral(node->kind());
  675. }
  676. };
  677. // A literal value. This is used in desugaring, and can't be expressed in
  678. // source syntax.
  679. class ValueLiteral : public ConstantValueLiteral {
  680. public:
  681. // Value literals are created by type-checking, and so are created with their
  682. // type and value category already known.
  683. ValueLiteral(SourceLocation source_loc, Nonnull<const Value*> value,
  684. Nonnull<const Value*> type,
  685. ExpressionCategory expression_category)
  686. : ConstantValueLiteral(AstNodeKind::ValueLiteral, source_loc, value) {
  687. set_static_type(type);
  688. set_expression_category(expression_category);
  689. }
  690. explicit ValueLiteral(CloneContext& context, const ValueLiteral& other)
  691. : ConstantValueLiteral(context, other) {}
  692. static auto classof(const AstNode* node) -> bool {
  693. return InheritsFromValueLiteral(node->kind());
  694. }
  695. };
  696. class IntrinsicExpression : public Expression {
  697. public:
  698. enum class Intrinsic {
  699. Print,
  700. Alloc,
  701. Dealloc,
  702. Rand,
  703. ImplicitAs,
  704. ImplicitAsConvert,
  705. IntEq,
  706. StrEq,
  707. StrCompare,
  708. IntCompare,
  709. IntBitAnd,
  710. IntBitOr,
  711. IntBitXor,
  712. IntBitComplement,
  713. IntLeftShift,
  714. IntRightShift,
  715. Assert,
  716. };
  717. // Returns the enumerator corresponding to the intrinsic named `name`,
  718. // or raises a fatal compile error if there is no such enumerator.
  719. static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
  720. -> ErrorOr<Intrinsic>;
  721. explicit IntrinsicExpression(Intrinsic intrinsic, Nonnull<TupleLiteral*> args,
  722. SourceLocation source_loc)
  723. : Expression(AstNodeKind::IntrinsicExpression, source_loc),
  724. intrinsic_(intrinsic),
  725. args_(args) {}
  726. explicit IntrinsicExpression(CloneContext& context,
  727. const IntrinsicExpression& other)
  728. : Expression(context, other),
  729. intrinsic_(other.intrinsic_),
  730. args_(context.Clone(other.args_)) {}
  731. static auto classof(const AstNode* node) -> bool {
  732. return InheritsFromIntrinsicExpression(node->kind());
  733. }
  734. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  735. auto name() const -> std::string_view;
  736. auto args() const -> const TupleLiteral& { return *args_; }
  737. auto args() -> TupleLiteral& { return *args_; }
  738. private:
  739. Intrinsic intrinsic_;
  740. Nonnull<TupleLiteral*> args_;
  741. };
  742. class IfExpression : public Expression {
  743. public:
  744. explicit IfExpression(SourceLocation source_loc,
  745. Nonnull<Expression*> condition,
  746. Nonnull<Expression*> then_expression,
  747. Nonnull<Expression*> else_expression)
  748. : Expression(AstNodeKind::IfExpression, source_loc),
  749. condition_(condition),
  750. then_expression_(then_expression),
  751. else_expression_(else_expression) {}
  752. explicit IfExpression(CloneContext& context, const IfExpression& other)
  753. : Expression(context, other),
  754. condition_(context.Clone(other.condition_)),
  755. then_expression_(context.Clone(other.then_expression_)),
  756. else_expression_(context.Clone(other.else_expression_)) {}
  757. static auto classof(const AstNode* node) -> bool {
  758. return InheritsFromIfExpression(node->kind());
  759. }
  760. auto condition() const -> const Expression& { return *condition_; }
  761. auto condition() -> Expression& { return *condition_; }
  762. auto then_expression() const -> const Expression& {
  763. return *then_expression_;
  764. }
  765. auto then_expression() -> Expression& { return *then_expression_; }
  766. auto else_expression() const -> const Expression& {
  767. return *else_expression_;
  768. }
  769. auto else_expression() -> Expression& { return *else_expression_; }
  770. // Can only be called by type-checking, if a conversion was required.
  771. void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
  772. private:
  773. Nonnull<Expression*> condition_;
  774. Nonnull<Expression*> then_expression_;
  775. Nonnull<Expression*> else_expression_;
  776. };
  777. // A clause appearing on the right-hand side of a `where` operator that forms a
  778. // more precise constraint from a more general one.
  779. class WhereClause : public AstNode {
  780. public:
  781. ~WhereClause() override = 0;
  782. void Print(llvm::raw_ostream& out) const override;
  783. void PrintID(llvm::raw_ostream& out) const override;
  784. static auto classof(const AstNode* node) {
  785. return InheritsFromWhereClause(node->kind());
  786. }
  787. auto kind() const -> WhereClauseKind {
  788. return static_cast<WhereClauseKind>(root_kind());
  789. }
  790. protected:
  791. explicit WhereClause(WhereClauseKind kind, SourceLocation source_loc)
  792. : AstNode(static_cast<AstNodeKind>(kind), source_loc) {}
  793. explicit WhereClause(CloneContext& context, const WhereClause& other)
  794. : AstNode(context, other) {}
  795. };
  796. // An `impls` where clause.
  797. //
  798. // For example, `ConstraintA where .Type impls ConstraintB` requires that the
  799. // associated type `.Type` implements the constraint `ConstraintB`.
  800. class ImplsWhereClause : public WhereClause {
  801. public:
  802. explicit ImplsWhereClause(SourceLocation source_loc,
  803. Nonnull<Expression*> type,
  804. Nonnull<Expression*> constraint)
  805. : WhereClause(WhereClauseKind::ImplsWhereClause, source_loc),
  806. type_(type),
  807. constraint_(constraint) {}
  808. explicit ImplsWhereClause(CloneContext& context,
  809. const ImplsWhereClause& other)
  810. : WhereClause(context, other),
  811. type_(context.Clone(other.type_)),
  812. constraint_(context.Clone(other.constraint_)) {}
  813. static auto classof(const AstNode* node) {
  814. return InheritsFromImplsWhereClause(node->kind());
  815. }
  816. auto type() const -> const Expression& { return *type_; }
  817. auto type() -> Expression& { return *type_; }
  818. auto constraint() const -> const Expression& { return *constraint_; }
  819. auto constraint() -> Expression& { return *constraint_; }
  820. private:
  821. Nonnull<Expression*> type_;
  822. Nonnull<Expression*> constraint_;
  823. };
  824. // An `==` where clause.
  825. //
  826. // For example, `Constraint where .Type == i32` requires that the associated
  827. // type `.Type` is `i32`.
  828. class EqualsWhereClause : public WhereClause {
  829. public:
  830. explicit EqualsWhereClause(SourceLocation source_loc,
  831. Nonnull<Expression*> lhs, Nonnull<Expression*> rhs)
  832. : WhereClause(WhereClauseKind::EqualsWhereClause, source_loc),
  833. lhs_(lhs),
  834. rhs_(rhs) {}
  835. explicit EqualsWhereClause(CloneContext& context,
  836. const EqualsWhereClause& other)
  837. : WhereClause(context, other),
  838. lhs_(context.Clone(other.lhs_)),
  839. rhs_(context.Clone(other.rhs_)) {}
  840. static auto classof(const AstNode* node) {
  841. return InheritsFromEqualsWhereClause(node->kind());
  842. }
  843. auto lhs() const -> const Expression& { return *lhs_; }
  844. auto lhs() -> Expression& { return *lhs_; }
  845. auto rhs() const -> const Expression& { return *rhs_; }
  846. auto rhs() -> Expression& { return *rhs_; }
  847. private:
  848. Nonnull<Expression*> lhs_;
  849. Nonnull<Expression*> rhs_;
  850. };
  851. // An `=` where clause.
  852. //
  853. // For example, `Constraint where .Type = i32` specifies that the associated
  854. // type `.Type` is rewritten to `i32` whenever used.
  855. class RewriteWhereClause : public WhereClause {
  856. public:
  857. explicit RewriteWhereClause(SourceLocation source_loc,
  858. std::string member_name,
  859. Nonnull<Expression*> replacement)
  860. : WhereClause(WhereClauseKind::RewriteWhereClause, source_loc),
  861. member_name_(std::move(member_name)),
  862. replacement_(replacement) {}
  863. explicit RewriteWhereClause(CloneContext& context,
  864. const RewriteWhereClause& other)
  865. : WhereClause(context, other),
  866. member_name_(other.member_name_),
  867. replacement_(context.Clone(other.replacement_)) {}
  868. static auto classof(const AstNode* node) {
  869. return InheritsFromRewriteWhereClause(node->kind());
  870. }
  871. auto member_name() const -> std::string_view { return member_name_; }
  872. auto replacement() const -> const Expression& { return *replacement_; }
  873. auto replacement() -> Expression& { return *replacement_; }
  874. private:
  875. std::string member_name_;
  876. Nonnull<Expression*> replacement_;
  877. };
  878. // A `where` expression: `AddableWith(i32) where .Result == i32`.
  879. //
  880. // The first operand is rewritten to a generic binding, for example
  881. // `.Self:! AddableWith(i32)`, which may be used in the clauses.
  882. class WhereExpression : public RewritableMixin<Expression> {
  883. public:
  884. explicit WhereExpression(SourceLocation source_loc,
  885. Nonnull<GenericBinding*> self_binding,
  886. std::vector<Nonnull<WhereClause*>> clauses)
  887. : RewritableMixin(AstNodeKind::WhereExpression, source_loc),
  888. self_binding_(self_binding),
  889. clauses_(std::move(clauses)) {}
  890. explicit WhereExpression(CloneContext& context, const WhereExpression& other);
  891. static auto classof(const AstNode* node) -> bool {
  892. return InheritsFromWhereExpression(node->kind());
  893. }
  894. auto self_binding() const -> const GenericBinding& { return *self_binding_; }
  895. auto self_binding() -> GenericBinding& { return *self_binding_; }
  896. auto enclosing_dot_self() const
  897. -> std::optional<Nonnull<const GenericBinding*>> {
  898. return enclosing_dot_self_;
  899. }
  900. // Sets the enclosing value of `.Self`. Can only be called during name
  901. // resolution.
  902. void set_enclosing_dot_self(Nonnull<const GenericBinding*> dot_self) {
  903. CARBON_CHECK(!enclosing_dot_self_ || enclosing_dot_self_ == dot_self);
  904. enclosing_dot_self_ = dot_self;
  905. }
  906. auto clauses() const -> llvm::ArrayRef<Nonnull<const WhereClause*>> {
  907. return clauses_;
  908. }
  909. auto clauses() -> llvm::ArrayRef<Nonnull<WhereClause*>> { return clauses_; }
  910. private:
  911. Nonnull<GenericBinding*> self_binding_;
  912. std::vector<Nonnull<WhereClause*>> clauses_;
  913. std::optional<Nonnull<const GenericBinding*>> enclosing_dot_self_;
  914. };
  915. // A builtin conversion to a type determined by type-checking. These are
  916. // created by type-checking when a type conversion is found to be necessary but
  917. // that conversion is implemented directly rather than by an `ImplicitAs`
  918. // implementation.
  919. class BuiltinConvertExpression : public Expression {
  920. public:
  921. BuiltinConvertExpression(Nonnull<Expression*> source_expression,
  922. Nonnull<const Value*> destination_type)
  923. : Expression(AstNodeKind::BuiltinConvertExpression,
  924. source_expression->source_loc()),
  925. source_expression_(source_expression) {
  926. set_static_type(destination_type);
  927. set_expression_category(ExpressionCategory::Value);
  928. }
  929. explicit BuiltinConvertExpression(CloneContext& context,
  930. const BuiltinConvertExpression& other)
  931. : Expression(context, other),
  932. source_expression_(context.Clone(other.source_expression_)),
  933. rewritten_form_(context.Clone(other.rewritten_form_)) {}
  934. static auto classof(const AstNode* node) -> bool {
  935. return InheritsFromBuiltinConvertExpression(node->kind());
  936. }
  937. auto source_expression() -> Nonnull<Expression*> {
  938. return source_expression_;
  939. }
  940. auto source_expression() const -> Nonnull<const Expression*> {
  941. return source_expression_;
  942. }
  943. // Set the rewritten form of this expression. Can only be called during type
  944. // checking.
  945. auto set_rewritten_form(Nonnull<const Expression*> rewritten_form) -> void {
  946. CARBON_CHECK(!rewritten_form_.has_value()) << "rewritten form set twice";
  947. rewritten_form_ = rewritten_form;
  948. }
  949. // Get the rewritten form of this expression. A rewritten form can be used to
  950. // prepare the conversion during type checking.
  951. auto rewritten_form() const -> std::optional<Nonnull<const Expression*>> {
  952. return rewritten_form_;
  953. }
  954. private:
  955. Nonnull<Expression*> source_expression_;
  956. std::optional<Nonnull<const Expression*>> rewritten_form_;
  957. };
  958. // An expression whose semantics have not been implemented. This can be used
  959. // as a placeholder during development, in order to implement and test parsing
  960. // of a new expression syntax without having to implement its semantics.
  961. class UnimplementedExpression : public Expression {
  962. public:
  963. // Constructs an UnimplementedExpression with the given label and the given
  964. // children, which must all be convertible to Nonnull<AstNode*>. The label
  965. // should correspond roughly to the name of the class that will eventually
  966. // replace this usage of UnimplementedExpression.
  967. template <typename... Children>
  968. UnimplementedExpression(SourceLocation source_loc, std::string label,
  969. Children... children)
  970. : Expression(AstNodeKind::UnimplementedExpression, source_loc),
  971. label_(std::move(label)) {
  972. AddChildren(children...);
  973. }
  974. explicit UnimplementedExpression(CloneContext& context,
  975. const UnimplementedExpression& other)
  976. : Expression(context, other),
  977. label_(other.label_),
  978. children_(context.Clone(other.children_)) {}
  979. static auto classof(const AstNode* node) -> bool {
  980. return InheritsFromUnimplementedExpression(node->kind());
  981. }
  982. auto label() const -> std::string_view { return label_; }
  983. auto children() const -> llvm::ArrayRef<Nonnull<const AstNode*>> {
  984. return children_;
  985. }
  986. private:
  987. void AddChildren() {}
  988. template <typename... Children>
  989. void AddChildren(Nonnull<AstNode*> child, Children... children) {
  990. children_.push_back(child);
  991. AddChildren(children...);
  992. }
  993. std::string label_;
  994. std::vector<Nonnull<AstNode*>> children_;
  995. };
  996. // A literal representing a statically-sized array type.
  997. class ArrayTypeLiteral : public ConstantValueLiteral {
  998. public:
  999. // Constructs an array type literal which uses the given expressions to
  1000. // represent the element type and size.
  1001. explicit ArrayTypeLiteral(SourceLocation source_loc,
  1002. Nonnull<Expression*> element_type_expression,
  1003. Nonnull<Expression*> size_expression)
  1004. : ConstantValueLiteral(AstNodeKind::ArrayTypeLiteral, source_loc),
  1005. element_type_expression_(element_type_expression),
  1006. size_expression_(size_expression) {}
  1007. explicit ArrayTypeLiteral(CloneContext& context,
  1008. const ArrayTypeLiteral& other)
  1009. : ConstantValueLiteral(context, other),
  1010. element_type_expression_(context.Clone(other.element_type_expression_)),
  1011. size_expression_(context.Clone(other.size_expression_)) {}
  1012. static auto classof(const AstNode* node) -> bool {
  1013. return InheritsFromArrayTypeLiteral(node->kind());
  1014. }
  1015. auto element_type_expression() const -> const Expression& {
  1016. return *element_type_expression_;
  1017. }
  1018. auto element_type_expression() -> Expression& {
  1019. return *element_type_expression_;
  1020. }
  1021. auto size_expression() const -> const Expression& {
  1022. return *size_expression_;
  1023. }
  1024. auto size_expression() -> Expression& { return *size_expression_; }
  1025. private:
  1026. Nonnull<Expression*> element_type_expression_;
  1027. Nonnull<Expression*> size_expression_;
  1028. };
  1029. // Converts paren_contents to an Expression, interpreting the parentheses as
  1030. // grouping if their contents permit that interpretation, or as forming a
  1031. // tuple otherwise.
  1032. auto ExpressionFromParenContents(
  1033. Nonnull<Arena*> arena, SourceLocation source_loc,
  1034. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  1035. // Converts paren_contents to an Expression, interpreting the parentheses as
  1036. // forming a tuple.
  1037. auto TupleExpressionFromParenContents(
  1038. Nonnull<Arena*> arena, SourceLocation source_loc,
  1039. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  1040. } // namespace Carbon
  1041. #endif // CARBON_EXPLORER_AST_EXPRESSION_H_