expression.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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/base/arena.h"
  20. #include "explorer/base/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(
  542. !fields_.empty(),
  543. "`{}` is represented as a StructLiteral, not a StructTypeLiteral.");
  544. }
  545. explicit StructTypeLiteral(CloneContext& context,
  546. const StructTypeLiteral& other)
  547. : ConstantValueLiteral(context, other),
  548. fields_(context.Clone(other.fields_)) {}
  549. static auto classof(const AstNode* node) -> bool {
  550. return InheritsFromStructTypeLiteral(node->kind());
  551. }
  552. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  553. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  554. private:
  555. std::vector<FieldInitializer> fields_;
  556. };
  557. class OperatorExpression : public RewritableMixin<Expression> {
  558. public:
  559. explicit OperatorExpression(SourceLocation source_loc, Operator op,
  560. std::vector<Nonnull<Expression*>> arguments)
  561. : RewritableMixin(AstNodeKind::OperatorExpression, source_loc),
  562. op_(op),
  563. arguments_(std::move(arguments)) {}
  564. explicit OperatorExpression(CloneContext& context,
  565. const OperatorExpression& other)
  566. : RewritableMixin(context, other),
  567. op_(other.op_),
  568. arguments_(context.Clone(other.arguments_)) {}
  569. static auto classof(const AstNode* node) -> bool {
  570. return InheritsFromOperatorExpression(node->kind());
  571. }
  572. auto op() const -> Operator { return op_; }
  573. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  574. return arguments_;
  575. }
  576. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  577. return arguments_;
  578. }
  579. private:
  580. Operator op_;
  581. std::vector<Nonnull<Expression*>> arguments_;
  582. };
  583. class CallExpression : public Expression {
  584. public:
  585. explicit CallExpression(SourceLocation source_loc,
  586. Nonnull<Expression*> function,
  587. Nonnull<Expression*> argument)
  588. : Expression(AstNodeKind::CallExpression, source_loc),
  589. function_(function),
  590. argument_(argument),
  591. bindings_({}, {}) {}
  592. explicit CallExpression(CloneContext& context, const CallExpression& other)
  593. : Expression(context, other),
  594. function_(context.Clone(other.function_)),
  595. argument_(context.Clone(other.argument_)),
  596. bindings_(context.Clone(other.bindings_)) {}
  597. static auto classof(const AstNode* node) -> bool {
  598. return InheritsFromCallExpression(node->kind());
  599. }
  600. auto function() const -> const Expression& { return *function_; }
  601. auto function() -> Expression& { return *function_; }
  602. auto argument() const -> const Expression& { return *argument_; }
  603. auto argument() -> Expression& { return *argument_; }
  604. auto bindings() -> const Bindings& { return bindings_; }
  605. // Can only be called once, during typechecking.
  606. void set_bindings(Bindings bindings) {
  607. CARBON_CHECK(bindings_.args().empty() && bindings_.witnesses().empty());
  608. bindings_ = std::move(bindings);
  609. }
  610. auto deduced_args() const -> const BindingMap& { return bindings_.args(); }
  611. // Maps each of `function`'s impl bindings to a witness.
  612. // Should not be called before typechecking, or if `function` is not
  613. // a generic function.
  614. auto witnesses() const -> const ImplWitnessMap& {
  615. return bindings_.witnesses();
  616. }
  617. // Can only be called by type-checking, if a conversion was required.
  618. void set_argument(Nonnull<Expression*> argument) { argument_ = argument; }
  619. private:
  620. Nonnull<Expression*> function_;
  621. Nonnull<Expression*> argument_;
  622. Bindings bindings_;
  623. };
  624. class FunctionTypeLiteral : public ConstantValueLiteral {
  625. public:
  626. explicit FunctionTypeLiteral(SourceLocation source_loc,
  627. Nonnull<TupleLiteral*> parameter,
  628. Nonnull<Expression*> return_type)
  629. : ConstantValueLiteral(AstNodeKind::FunctionTypeLiteral, source_loc),
  630. parameter_(parameter),
  631. return_type_(return_type) {}
  632. explicit FunctionTypeLiteral(CloneContext& context,
  633. const FunctionTypeLiteral& other)
  634. : ConstantValueLiteral(context, other),
  635. parameter_(context.Clone(other.parameter_)),
  636. return_type_(context.Clone(other.return_type_)) {}
  637. static auto classof(const AstNode* node) -> bool {
  638. return InheritsFromFunctionTypeLiteral(node->kind());
  639. }
  640. auto parameter() const -> const TupleLiteral& { return *parameter_; }
  641. auto parameter() -> TupleLiteral& { return *parameter_; }
  642. auto return_type() const -> const Expression& { return *return_type_; }
  643. auto return_type() -> Expression& { return *return_type_; }
  644. private:
  645. Nonnull<TupleLiteral*> parameter_;
  646. Nonnull<Expression*> return_type_;
  647. };
  648. class BoolTypeLiteral : public Expression {
  649. public:
  650. explicit BoolTypeLiteral(SourceLocation source_loc)
  651. : Expression(AstNodeKind::BoolTypeLiteral, source_loc) {}
  652. explicit BoolTypeLiteral(CloneContext& context, const BoolTypeLiteral& other)
  653. : Expression(context, other) {}
  654. static auto classof(const AstNode* node) -> bool {
  655. return InheritsFromBoolTypeLiteral(node->kind());
  656. }
  657. };
  658. class IntTypeLiteral : public Expression {
  659. public:
  660. explicit IntTypeLiteral(SourceLocation source_loc)
  661. : Expression(AstNodeKind::IntTypeLiteral, source_loc) {}
  662. explicit IntTypeLiteral(CloneContext& context, const IntTypeLiteral& other)
  663. : Expression(context, other) {}
  664. static auto classof(const AstNode* node) -> bool {
  665. return InheritsFromIntTypeLiteral(node->kind());
  666. }
  667. };
  668. class TypeTypeLiteral : public Expression {
  669. public:
  670. explicit TypeTypeLiteral(SourceLocation source_loc)
  671. : Expression(AstNodeKind::TypeTypeLiteral, source_loc) {}
  672. explicit TypeTypeLiteral(CloneContext& context, const TypeTypeLiteral& other)
  673. : Expression(context, other) {}
  674. static auto classof(const AstNode* node) -> bool {
  675. return InheritsFromTypeTypeLiteral(node->kind());
  676. }
  677. };
  678. // A literal value. This is used in desugaring, and can't be expressed in
  679. // source syntax.
  680. class ValueLiteral : public ConstantValueLiteral {
  681. public:
  682. // Value literals are created by type-checking, and so are created with their
  683. // type and value category already known.
  684. ValueLiteral(SourceLocation source_loc, Nonnull<const Value*> value,
  685. Nonnull<const Value*> type,
  686. ExpressionCategory expression_category)
  687. : ConstantValueLiteral(AstNodeKind::ValueLiteral, source_loc, value) {
  688. set_static_type(type);
  689. set_expression_category(expression_category);
  690. }
  691. explicit ValueLiteral(CloneContext& context, const ValueLiteral& other)
  692. : ConstantValueLiteral(context, other) {}
  693. static auto classof(const AstNode* node) -> bool {
  694. return InheritsFromValueLiteral(node->kind());
  695. }
  696. };
  697. class IntrinsicExpression : public RewritableMixin<Expression> {
  698. public:
  699. enum class Intrinsic {
  700. Print,
  701. Alloc,
  702. Dealloc,
  703. PrintAllocs,
  704. Rand,
  705. ImplicitAs,
  706. ImplicitAsConvert,
  707. IntEq,
  708. StrEq,
  709. StrCompare,
  710. IntCompare,
  711. IntBitAnd,
  712. IntBitOr,
  713. IntBitXor,
  714. IntBitComplement,
  715. IntLeftShift,
  716. IntRightShift,
  717. Assert,
  718. };
  719. // Returns the enumerator corresponding to the intrinsic named `name`,
  720. // or raises a fatal compile error if there is no such enumerator.
  721. static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
  722. -> ErrorOr<Intrinsic>;
  723. explicit IntrinsicExpression(Intrinsic intrinsic, Nonnull<TupleLiteral*> args,
  724. SourceLocation source_loc)
  725. : RewritableMixin(AstNodeKind::IntrinsicExpression, source_loc),
  726. intrinsic_(intrinsic),
  727. args_(args) {}
  728. explicit IntrinsicExpression(CloneContext& context,
  729. const IntrinsicExpression& other)
  730. : RewritableMixin(context, other),
  731. intrinsic_(other.intrinsic_),
  732. args_(context.Clone(other.args_)) {}
  733. static auto classof(const AstNode* node) -> bool {
  734. return InheritsFromIntrinsicExpression(node->kind());
  735. }
  736. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  737. auto name() const -> std::string_view;
  738. auto args() const -> const TupleLiteral& { return *args_; }
  739. auto args() -> TupleLiteral& { return *args_; }
  740. private:
  741. Intrinsic intrinsic_;
  742. Nonnull<TupleLiteral*> args_;
  743. };
  744. class IfExpression : public Expression {
  745. public:
  746. explicit IfExpression(SourceLocation source_loc,
  747. Nonnull<Expression*> condition,
  748. Nonnull<Expression*> then_expression,
  749. Nonnull<Expression*> else_expression)
  750. : Expression(AstNodeKind::IfExpression, source_loc),
  751. condition_(condition),
  752. then_expression_(then_expression),
  753. else_expression_(else_expression) {}
  754. explicit IfExpression(CloneContext& context, const IfExpression& other)
  755. : Expression(context, other),
  756. condition_(context.Clone(other.condition_)),
  757. then_expression_(context.Clone(other.then_expression_)),
  758. else_expression_(context.Clone(other.else_expression_)) {}
  759. static auto classof(const AstNode* node) -> bool {
  760. return InheritsFromIfExpression(node->kind());
  761. }
  762. auto condition() const -> const Expression& { return *condition_; }
  763. auto condition() -> Expression& { return *condition_; }
  764. auto then_expression() const -> const Expression& {
  765. return *then_expression_;
  766. }
  767. auto then_expression() -> Expression& { return *then_expression_; }
  768. auto else_expression() const -> const Expression& {
  769. return *else_expression_;
  770. }
  771. auto else_expression() -> Expression& { return *else_expression_; }
  772. // Can only be called by type-checking, if a conversion was required.
  773. void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
  774. private:
  775. Nonnull<Expression*> condition_;
  776. Nonnull<Expression*> then_expression_;
  777. Nonnull<Expression*> else_expression_;
  778. };
  779. // A clause appearing on the right-hand side of a `where` operator that forms a
  780. // more precise constraint from a more general one.
  781. class WhereClause : public AstNode {
  782. public:
  783. ~WhereClause() override = 0;
  784. void Print(llvm::raw_ostream& out) const override;
  785. void PrintID(llvm::raw_ostream& out) const override;
  786. static auto classof(const AstNode* node) {
  787. return InheritsFromWhereClause(node->kind());
  788. }
  789. auto kind() const -> WhereClauseKind {
  790. return static_cast<WhereClauseKind>(root_kind());
  791. }
  792. protected:
  793. explicit WhereClause(WhereClauseKind kind, SourceLocation source_loc)
  794. : AstNode(static_cast<AstNodeKind>(kind), source_loc) {}
  795. explicit WhereClause(CloneContext& context, const WhereClause& other)
  796. : AstNode(context, other) {}
  797. };
  798. // An `impls` where clause.
  799. //
  800. // For example, `ConstraintA where .Type impls ConstraintB` requires that the
  801. // associated type `.Type` implements the constraint `ConstraintB`.
  802. class ImplsWhereClause : public WhereClause {
  803. public:
  804. explicit ImplsWhereClause(SourceLocation source_loc,
  805. Nonnull<Expression*> type,
  806. Nonnull<Expression*> constraint)
  807. : WhereClause(WhereClauseKind::ImplsWhereClause, source_loc),
  808. type_(type),
  809. constraint_(constraint) {}
  810. explicit ImplsWhereClause(CloneContext& context,
  811. const ImplsWhereClause& other)
  812. : WhereClause(context, other),
  813. type_(context.Clone(other.type_)),
  814. constraint_(context.Clone(other.constraint_)) {}
  815. static auto classof(const AstNode* node) {
  816. return InheritsFromImplsWhereClause(node->kind());
  817. }
  818. auto type() const -> const Expression& { return *type_; }
  819. auto type() -> Expression& { return *type_; }
  820. auto constraint() const -> const Expression& { return *constraint_; }
  821. auto constraint() -> Expression& { return *constraint_; }
  822. private:
  823. Nonnull<Expression*> type_;
  824. Nonnull<Expression*> constraint_;
  825. };
  826. // An `==` where clause.
  827. //
  828. // For example, `Constraint where .Type == i32` requires that the associated
  829. // type `.Type` is `i32`.
  830. class EqualsWhereClause : public WhereClause {
  831. public:
  832. explicit EqualsWhereClause(SourceLocation source_loc,
  833. Nonnull<Expression*> lhs, Nonnull<Expression*> rhs)
  834. : WhereClause(WhereClauseKind::EqualsWhereClause, source_loc),
  835. lhs_(lhs),
  836. rhs_(rhs) {}
  837. explicit EqualsWhereClause(CloneContext& context,
  838. const EqualsWhereClause& other)
  839. : WhereClause(context, other),
  840. lhs_(context.Clone(other.lhs_)),
  841. rhs_(context.Clone(other.rhs_)) {}
  842. static auto classof(const AstNode* node) {
  843. return InheritsFromEqualsWhereClause(node->kind());
  844. }
  845. auto lhs() const -> const Expression& { return *lhs_; }
  846. auto lhs() -> Expression& { return *lhs_; }
  847. auto rhs() const -> const Expression& { return *rhs_; }
  848. auto rhs() -> Expression& { return *rhs_; }
  849. private:
  850. Nonnull<Expression*> lhs_;
  851. Nonnull<Expression*> rhs_;
  852. };
  853. // An `=` where clause.
  854. //
  855. // For example, `Constraint where .Type = i32` specifies that the associated
  856. // type `.Type` is rewritten to `i32` whenever used.
  857. class RewriteWhereClause : public WhereClause {
  858. public:
  859. explicit RewriteWhereClause(SourceLocation source_loc,
  860. std::string member_name,
  861. Nonnull<Expression*> replacement)
  862. : WhereClause(WhereClauseKind::RewriteWhereClause, source_loc),
  863. member_name_(std::move(member_name)),
  864. replacement_(replacement) {}
  865. explicit RewriteWhereClause(CloneContext& context,
  866. const RewriteWhereClause& other)
  867. : WhereClause(context, other),
  868. member_name_(other.member_name_),
  869. replacement_(context.Clone(other.replacement_)) {}
  870. static auto classof(const AstNode* node) {
  871. return InheritsFromRewriteWhereClause(node->kind());
  872. }
  873. auto member_name() const -> std::string_view { return member_name_; }
  874. auto replacement() const -> const Expression& { return *replacement_; }
  875. auto replacement() -> Expression& { return *replacement_; }
  876. private:
  877. std::string member_name_;
  878. Nonnull<Expression*> replacement_;
  879. };
  880. // A `where` expression: `AddableWith(i32) where .Result == i32`.
  881. //
  882. // The first operand is rewritten to a generic binding, for example
  883. // `.Self:! AddableWith(i32)`, which may be used in the clauses.
  884. class WhereExpression : public RewritableMixin<Expression> {
  885. public:
  886. explicit WhereExpression(SourceLocation source_loc,
  887. Nonnull<GenericBinding*> self_binding,
  888. std::vector<Nonnull<WhereClause*>> clauses)
  889. : RewritableMixin(AstNodeKind::WhereExpression, source_loc),
  890. self_binding_(self_binding),
  891. clauses_(std::move(clauses)) {}
  892. explicit WhereExpression(CloneContext& context, const WhereExpression& other);
  893. static auto classof(const AstNode* node) -> bool {
  894. return InheritsFromWhereExpression(node->kind());
  895. }
  896. auto self_binding() const -> const GenericBinding& { return *self_binding_; }
  897. auto self_binding() -> GenericBinding& { return *self_binding_; }
  898. auto enclosing_dot_self() const
  899. -> std::optional<Nonnull<const GenericBinding*>> {
  900. return enclosing_dot_self_;
  901. }
  902. // Sets the enclosing value of `.Self`. Can only be called during name
  903. // resolution.
  904. void set_enclosing_dot_self(Nonnull<const GenericBinding*> dot_self) {
  905. CARBON_CHECK(!enclosing_dot_self_ || enclosing_dot_self_ == dot_self);
  906. enclosing_dot_self_ = dot_self;
  907. }
  908. auto clauses() const -> llvm::ArrayRef<Nonnull<const WhereClause*>> {
  909. return clauses_;
  910. }
  911. auto clauses() -> llvm::ArrayRef<Nonnull<WhereClause*>> { return clauses_; }
  912. private:
  913. Nonnull<GenericBinding*> self_binding_;
  914. std::vector<Nonnull<WhereClause*>> clauses_;
  915. std::optional<Nonnull<const GenericBinding*>> enclosing_dot_self_;
  916. };
  917. // A builtin conversion to a type determined by type-checking. These are
  918. // created by type-checking when a type conversion is found to be necessary but
  919. // that conversion is implemented directly rather than by an `ImplicitAs`
  920. // implementation.
  921. class BuiltinConvertExpression : public RewritableMixin<Expression> {
  922. public:
  923. BuiltinConvertExpression(Nonnull<Expression*> source_expression)
  924. : RewritableMixin(AstNodeKind::BuiltinConvertExpression,
  925. source_expression->source_loc()),
  926. source_expression_(source_expression) {}
  927. explicit BuiltinConvertExpression(CloneContext& context,
  928. const BuiltinConvertExpression& other)
  929. : RewritableMixin(context, other),
  930. source_expression_(context.Clone(other.source_expression_)) {}
  931. static auto classof(const AstNode* node) -> bool {
  932. return InheritsFromBuiltinConvertExpression(node->kind());
  933. }
  934. auto source_expression() -> Nonnull<Expression*> {
  935. return source_expression_;
  936. }
  937. auto source_expression() const -> Nonnull<const Expression*> {
  938. return source_expression_;
  939. }
  940. private:
  941. Nonnull<Expression*> source_expression_;
  942. };
  943. // An expression whose semantics have not been implemented. This can be used
  944. // as a placeholder during development, in order to implement and test parsing
  945. // of a new expression syntax without having to implement its semantics.
  946. class UnimplementedExpression : public Expression {
  947. public:
  948. // Constructs an UnimplementedExpression with the given label and the given
  949. // children, which must all be convertible to Nonnull<AstNode*>. The label
  950. // should correspond roughly to the name of the class that will eventually
  951. // replace this usage of UnimplementedExpression.
  952. template <typename... Children>
  953. UnimplementedExpression(SourceLocation source_loc, std::string label,
  954. Children... children)
  955. : Expression(AstNodeKind::UnimplementedExpression, source_loc),
  956. label_(std::move(label)) {
  957. AddChildren(children...);
  958. }
  959. explicit UnimplementedExpression(CloneContext& context,
  960. const UnimplementedExpression& other)
  961. : Expression(context, other),
  962. label_(other.label_),
  963. children_(context.Clone(other.children_)) {}
  964. static auto classof(const AstNode* node) -> bool {
  965. return InheritsFromUnimplementedExpression(node->kind());
  966. }
  967. auto label() const -> std::string_view { return label_; }
  968. auto children() const -> llvm::ArrayRef<Nonnull<const AstNode*>> {
  969. return children_;
  970. }
  971. private:
  972. void AddChildren() {}
  973. template <typename... Children>
  974. void AddChildren(Nonnull<AstNode*> child, Children... children) {
  975. children_.push_back(child);
  976. AddChildren(children...);
  977. }
  978. std::string label_;
  979. std::vector<Nonnull<AstNode*>> children_;
  980. };
  981. // A literal representing a statically-sized array type.
  982. class ArrayTypeLiteral : public ConstantValueLiteral {
  983. public:
  984. // Constructs an array type literal which uses the given expressions to
  985. // represent the element type and size.
  986. explicit ArrayTypeLiteral(
  987. SourceLocation source_loc, Nonnull<Expression*> element_type_expression,
  988. std::optional<Nonnull<Expression*>> size_expression = std::nullopt)
  989. : ConstantValueLiteral(AstNodeKind::ArrayTypeLiteral, source_loc),
  990. element_type_expression_(element_type_expression),
  991. size_expression_(size_expression) {}
  992. explicit ArrayTypeLiteral(CloneContext& context,
  993. const ArrayTypeLiteral& other)
  994. : ConstantValueLiteral(context, other),
  995. element_type_expression_(context.Clone(other.element_type_expression_)),
  996. size_expression_(context.Clone(other.size_expression_)) {}
  997. static auto classof(const AstNode* node) -> bool {
  998. return InheritsFromArrayTypeLiteral(node->kind());
  999. }
  1000. auto element_type_expression() const -> const Expression& {
  1001. return *element_type_expression_;
  1002. }
  1003. auto element_type_expression() -> Expression& {
  1004. return *element_type_expression_;
  1005. }
  1006. auto has_size_expression() const -> bool {
  1007. return size_expression_.has_value();
  1008. }
  1009. auto size_expression() const -> const Expression& {
  1010. CARBON_CHECK(size_expression_.has_value());
  1011. return **size_expression_;
  1012. }
  1013. auto size_expression() -> Expression& {
  1014. CARBON_CHECK(size_expression_.has_value());
  1015. return **size_expression_;
  1016. }
  1017. private:
  1018. Nonnull<Expression*> element_type_expression_;
  1019. std::optional<Nonnull<Expression*>> size_expression_;
  1020. };
  1021. // Converts paren_contents to an Expression, interpreting the parentheses as
  1022. // grouping if their contents permit that interpretation, or as forming a
  1023. // tuple otherwise.
  1024. auto ExpressionFromParenContents(
  1025. Nonnull<Arena*> arena, SourceLocation source_loc,
  1026. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  1027. // Converts paren_contents to an Expression, interpreting the parentheses as
  1028. // forming a tuple.
  1029. auto TupleExpressionFromParenContents(
  1030. Nonnull<Arena*> arena, SourceLocation source_loc,
  1031. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  1032. } // namespace Carbon
  1033. #endif // CARBON_EXPLORER_AST_EXPRESSION_H_