expression.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_EXPLORER_AST_EXPRESSION_H_
  5. #define CARBON_EXPLORER_AST_EXPRESSION_H_
  6. #include <map>
  7. #include <optional>
  8. #include <string>
  9. #include <variant>
  10. #include <vector>
  11. #include "common/ostream.h"
  12. #include "explorer/ast/ast_node.h"
  13. #include "explorer/ast/bindings.h"
  14. #include "explorer/ast/member.h"
  15. #include "explorer/ast/paren_contents.h"
  16. #include "explorer/ast/static_scope.h"
  17. #include "explorer/ast/value_category.h"
  18. #include "explorer/common/arena.h"
  19. #include "explorer/common/source_location.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/Support/Compiler.h"
  22. namespace Carbon {
  23. class Value;
  24. class MemberName;
  25. class VariableType;
  26. class InterfaceType;
  27. class ImplBinding;
  28. class GenericBinding;
  29. class Expression : public AstNode {
  30. public:
  31. ~Expression() override = 0;
  32. void Print(llvm::raw_ostream& out) const override;
  33. void PrintID(llvm::raw_ostream& out) const override;
  34. static auto classof(const AstNode* node) {
  35. return InheritsFromExpression(node->kind());
  36. }
  37. // Returns the enumerator corresponding to the most-derived type of this
  38. // object.
  39. auto kind() const -> ExpressionKind {
  40. return static_cast<ExpressionKind>(root_kind());
  41. }
  42. // The static type of this expression. Cannot be called before typechecking.
  43. auto static_type() const -> const Value& {
  44. CARBON_CHECK(static_type_.has_value());
  45. return **static_type_;
  46. }
  47. // Sets the static type of this expression. Can only be called once, during
  48. // typechecking.
  49. void set_static_type(Nonnull<const Value*> type) {
  50. CARBON_CHECK(!static_type_.has_value());
  51. static_type_ = type;
  52. }
  53. // The value category of this expression. Cannot be called before
  54. // typechecking.
  55. auto value_category() const -> ValueCategory { return *value_category_; }
  56. // Sets the value category of this expression. Can be called multiple times,
  57. // but the argument must have the same value each time.
  58. void set_value_category(ValueCategory value_category) {
  59. CARBON_CHECK(!value_category_.has_value() ||
  60. value_category == *value_category_);
  61. value_category_ = value_category;
  62. }
  63. // Determines whether the expression has already been type-checked. Should
  64. // only be used by type-checking.
  65. auto is_type_checked() -> bool {
  66. return static_type_.has_value() && value_category_.has_value();
  67. }
  68. protected:
  69. // Constructs an Expression representing syntax at the given line number.
  70. // `kind` must be the enumerator corresponding to the most-derived type being
  71. // constructed.
  72. Expression(AstNodeKind kind, SourceLocation source_loc)
  73. : AstNode(kind, source_loc) {}
  74. private:
  75. std::optional<Nonnull<const Value*>> static_type_;
  76. std::optional<ValueCategory> value_category_;
  77. };
  78. // A FieldInitializer represents the initialization of a single struct field.
  79. class FieldInitializer {
  80. public:
  81. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  82. : name_(std::move(name)), expression_(expression) {}
  83. auto name() const -> const std::string& { return name_; }
  84. auto expression() const -> const Expression& { return *expression_; }
  85. auto expression() -> Expression& { return *expression_; }
  86. private:
  87. // The field name. Cannot be empty.
  88. std::string name_;
  89. // The expression that initializes the field.
  90. Nonnull<Expression*> expression_;
  91. };
  92. enum class Operator {
  93. Add,
  94. AddressOf,
  95. And,
  96. As,
  97. Combine,
  98. Deref,
  99. Eq,
  100. Mul,
  101. Neg,
  102. Not,
  103. Or,
  104. Sub,
  105. Ptr,
  106. };
  107. // Returns the lexical representation of `op`, such as "+" for `Add`.
  108. auto ToString(Operator op) -> std::string_view;
  109. class IdentifierExpression : public Expression {
  110. public:
  111. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  112. : Expression(AstNodeKind::IdentifierExpression, source_loc),
  113. name_(std::move(name)) {}
  114. static auto classof(const AstNode* node) -> bool {
  115. return InheritsFromIdentifierExpression(node->kind());
  116. }
  117. auto name() const -> const std::string& { return name_; }
  118. // Returns the ValueNodeView this identifier refers to. Cannot be called
  119. // before name resolution.
  120. auto value_node() const -> const ValueNodeView& { return *value_node_; }
  121. // Sets the value returned by value_node. Can be called only during name
  122. // resolution.
  123. void set_value_node(ValueNodeView value_node) {
  124. CARBON_CHECK(!value_node_.has_value() || value_node_ == value_node);
  125. value_node_ = std::move(value_node);
  126. }
  127. private:
  128. std::string name_;
  129. std::optional<ValueNodeView> value_node_;
  130. };
  131. // A `.Self` expression within either a `:!` binding or a standalone `where`
  132. // expression.
  133. //
  134. // In a `:!` binding, the type of `.Self` is always `Type`. For example, in
  135. // `A:! AddableWith(.Self)`, the expression `.Self` refers to the same type as
  136. // `A`, but with type `Type`.
  137. //
  138. // In a `where` binding, the type of `.Self` is the constraint preceding the
  139. // `where` keyword. For example, in `Foo where .Result is Bar(.Self)`, the type
  140. // of `.Self` is `Foo`.
  141. class DotSelfExpression : public Expression {
  142. public:
  143. explicit DotSelfExpression(SourceLocation source_loc)
  144. : Expression(AstNodeKind::DotSelfExpression, source_loc) {}
  145. static auto classof(const AstNode* node) -> bool {
  146. return InheritsFromDotSelfExpression(node->kind());
  147. }
  148. // The self binding. Cannot be called before name resolution.
  149. auto self_binding() const -> const GenericBinding& { return **self_binding_; }
  150. auto self_binding() -> GenericBinding& { return **self_binding_; }
  151. // Sets the self binding. Called only during name resolution.
  152. void set_self_binding(Nonnull<GenericBinding*> self_binding) {
  153. CARBON_CHECK(!self_binding_.has_value() || self_binding_ == self_binding);
  154. self_binding_ = self_binding;
  155. }
  156. private:
  157. std::string name_;
  158. std::optional<Nonnull<GenericBinding*>> self_binding_;
  159. };
  160. class SimpleMemberAccessExpression : public Expression {
  161. public:
  162. explicit SimpleMemberAccessExpression(SourceLocation source_loc,
  163. Nonnull<Expression*> object,
  164. std::string member_name)
  165. : Expression(AstNodeKind::SimpleMemberAccessExpression, source_loc),
  166. object_(object),
  167. member_name_(std::move(member_name)) {}
  168. static auto classof(const AstNode* node) -> bool {
  169. return InheritsFromSimpleMemberAccessExpression(node->kind());
  170. }
  171. auto object() const -> const Expression& { return *object_; }
  172. auto object() -> Expression& { return *object_; }
  173. auto member_name() const -> const std::string& { return member_name_; }
  174. // Returns the `Member` that the member name resolved to.
  175. // Should not be called before typechecking.
  176. auto member() const -> const Member& {
  177. CARBON_CHECK(member_.has_value());
  178. return *member_;
  179. }
  180. // Can only be called once, during typechecking.
  181. void set_member(Member member) {
  182. CARBON_CHECK(!member_.has_value());
  183. member_ = member;
  184. }
  185. // Returns true if the field is a method that has a "me" declaration in an
  186. // AddrPattern.
  187. auto is_field_addr_me_method() const -> bool {
  188. return is_field_addr_me_method_;
  189. }
  190. // Can only be called once, during typechecking.
  191. void set_is_field_addr_me_method() { is_field_addr_me_method_ = true; }
  192. // If `object` has a generic type, returns the `ImplBinding` that
  193. // identifies its witness table. Otherwise, returns `std::nullopt`. Should not
  194. // be called before typechecking.
  195. auto impl() const -> std::optional<Nonnull<const Expression*>> {
  196. return impl_;
  197. }
  198. // Can only be called once, during typechecking.
  199. void set_impl(Nonnull<const Expression*> impl) {
  200. CARBON_CHECK(!impl_.has_value());
  201. impl_ = impl;
  202. }
  203. // If `object` is a constrained type parameter and `member` was found in an
  204. // interface, returns that interface. Should not be called before
  205. // typechecking.
  206. auto found_in_interface() const
  207. -> std::optional<Nonnull<const InterfaceType*>> {
  208. return found_in_interface_;
  209. }
  210. // Can only be called once, during typechecking.
  211. void set_found_in_interface(Nonnull<const InterfaceType*> interface) {
  212. CARBON_CHECK(!found_in_interface_.has_value());
  213. found_in_interface_ = interface;
  214. }
  215. private:
  216. Nonnull<Expression*> object_;
  217. std::string member_name_;
  218. std::optional<Member> member_;
  219. bool is_field_addr_me_method_ = false;
  220. std::optional<Nonnull<const Expression*>> impl_;
  221. std::optional<Nonnull<const InterfaceType*>> found_in_interface_;
  222. };
  223. // A compound member access expression of the form `object.(path)`.
  224. //
  225. // `path` is required to have `TypeOfMemberName` type, and describes the member
  226. // being accessed, which is one of:
  227. //
  228. // - An instance member of a type: `object.(Type.member)`.
  229. // - A non-instance member of an interface: `Type.(Interface.member)` or
  230. // `object.(Interface.member)`.
  231. // - An instance member of an interface: `object.(Interface.member)` or
  232. // `object.(Type.(Interface.member))`.
  233. //
  234. // Note that the `path` is evaluated during type-checking, not at runtime, so
  235. // the corresponding `member` is determined statically.
  236. class CompoundMemberAccessExpression : public Expression {
  237. public:
  238. explicit CompoundMemberAccessExpression(SourceLocation source_loc,
  239. Nonnull<Expression*> object,
  240. Nonnull<Expression*> path)
  241. : Expression(AstNodeKind::CompoundMemberAccessExpression, source_loc),
  242. object_(object),
  243. path_(path) {}
  244. static auto classof(const AstNode* node) -> bool {
  245. return InheritsFromCompoundMemberAccessExpression(node->kind());
  246. }
  247. auto object() const -> const Expression& { return *object_; }
  248. auto object() -> Expression& { return *object_; }
  249. auto path() const -> const Expression& { return *path_; }
  250. auto path() -> Expression& { return *path_; }
  251. // Returns the `MemberName` value that evaluation of the path produced.
  252. // Should not be called before typechecking.
  253. auto member() const -> const MemberName& {
  254. CARBON_CHECK(member_.has_value());
  255. return **member_;
  256. }
  257. // Can only be called once, during typechecking.
  258. void set_member(Nonnull<const MemberName*> member) {
  259. CARBON_CHECK(!member_.has_value());
  260. member_ = member;
  261. }
  262. // Returns the expression to use to compute the witness table, if this
  263. // expression names an interface member.
  264. auto impl() const -> std::optional<Nonnull<const Expression*>> {
  265. return impl_;
  266. }
  267. // Can only be called once, during typechecking.
  268. void set_impl(Nonnull<const Expression*> impl) {
  269. CARBON_CHECK(!impl_.has_value());
  270. impl_ = impl;
  271. }
  272. // Can only be called by type-checking, if a conversion was required.
  273. void set_object(Nonnull<Expression*> object) { object_ = object; }
  274. private:
  275. Nonnull<Expression*> object_;
  276. Nonnull<Expression*> path_;
  277. std::optional<Nonnull<const MemberName*>> member_;
  278. std::optional<Nonnull<const Expression*>> impl_;
  279. };
  280. class IndexExpression : public Expression {
  281. public:
  282. explicit IndexExpression(SourceLocation source_loc,
  283. Nonnull<Expression*> object,
  284. Nonnull<Expression*> offset)
  285. : Expression(AstNodeKind::IndexExpression, source_loc),
  286. object_(object),
  287. offset_(offset) {}
  288. static auto classof(const AstNode* node) -> bool {
  289. return InheritsFromIndexExpression(node->kind());
  290. }
  291. auto object() const -> const Expression& { return *object_; }
  292. auto object() -> Expression& { return *object_; }
  293. auto offset() const -> const Expression& { return *offset_; }
  294. auto offset() -> Expression& { return *offset_; }
  295. private:
  296. Nonnull<Expression*> object_;
  297. Nonnull<Expression*> offset_;
  298. };
  299. class IntLiteral : public Expression {
  300. public:
  301. explicit IntLiteral(SourceLocation source_loc, int value)
  302. : Expression(AstNodeKind::IntLiteral, source_loc), value_(value) {}
  303. static auto classof(const AstNode* node) -> bool {
  304. return InheritsFromIntLiteral(node->kind());
  305. }
  306. auto value() const -> int { return value_; }
  307. private:
  308. int value_;
  309. };
  310. class BoolLiteral : public Expression {
  311. public:
  312. explicit BoolLiteral(SourceLocation source_loc, bool value)
  313. : Expression(AstNodeKind::BoolLiteral, source_loc), value_(value) {}
  314. static auto classof(const AstNode* node) -> bool {
  315. return InheritsFromBoolLiteral(node->kind());
  316. }
  317. auto value() const -> bool { return value_; }
  318. private:
  319. bool value_;
  320. };
  321. class StringLiteral : public Expression {
  322. public:
  323. explicit StringLiteral(SourceLocation source_loc, std::string value)
  324. : Expression(AstNodeKind::StringLiteral, source_loc),
  325. value_(std::move(value)) {}
  326. static auto classof(const AstNode* node) -> bool {
  327. return InheritsFromStringLiteral(node->kind());
  328. }
  329. auto value() const -> const std::string& { return value_; }
  330. private:
  331. std::string value_;
  332. };
  333. class StringTypeLiteral : public Expression {
  334. public:
  335. explicit StringTypeLiteral(SourceLocation source_loc)
  336. : Expression(AstNodeKind::StringTypeLiteral, source_loc) {}
  337. static auto classof(const AstNode* node) -> bool {
  338. return InheritsFromStringTypeLiteral(node->kind());
  339. }
  340. };
  341. class TupleLiteral : public Expression {
  342. public:
  343. explicit TupleLiteral(SourceLocation source_loc)
  344. : TupleLiteral(source_loc, {}) {}
  345. explicit TupleLiteral(SourceLocation source_loc,
  346. std::vector<Nonnull<Expression*>> fields)
  347. : Expression(AstNodeKind::TupleLiteral, source_loc),
  348. fields_(std::move(fields)) {}
  349. static auto classof(const AstNode* node) -> bool {
  350. return InheritsFromTupleLiteral(node->kind());
  351. }
  352. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  353. return fields_;
  354. }
  355. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  356. private:
  357. std::vector<Nonnull<Expression*>> fields_;
  358. };
  359. // A non-empty literal value of a struct type.
  360. //
  361. // It can't be empty because the syntax `{}` is a struct type literal as well
  362. // as a literal value of that type, so for consistency we always represent it
  363. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  364. // the two.
  365. class StructLiteral : public Expression {
  366. public:
  367. explicit StructLiteral(SourceLocation loc,
  368. std::vector<FieldInitializer> fields)
  369. : Expression(AstNodeKind::StructLiteral, loc),
  370. fields_(std::move(fields)) {
  371. CARBON_CHECK(!fields_.empty())
  372. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  373. }
  374. static auto classof(const AstNode* node) -> bool {
  375. return InheritsFromStructLiteral(node->kind());
  376. }
  377. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  378. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  379. private:
  380. std::vector<FieldInitializer> fields_;
  381. };
  382. // A literal representing a struct type.
  383. //
  384. // Code that handles this type may sometimes need to have special-case handling
  385. // for `{}`, which is a struct value in addition to being a struct type.
  386. class StructTypeLiteral : public Expression {
  387. public:
  388. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  389. explicit StructTypeLiteral(SourceLocation loc,
  390. std::vector<FieldInitializer> fields)
  391. : Expression(AstNodeKind::StructTypeLiteral, loc),
  392. fields_(std::move(fields)) {}
  393. static auto classof(const AstNode* node) -> bool {
  394. return InheritsFromStructTypeLiteral(node->kind());
  395. }
  396. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  397. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  398. private:
  399. std::vector<FieldInitializer> fields_;
  400. };
  401. class PrimitiveOperatorExpression : public Expression {
  402. public:
  403. explicit PrimitiveOperatorExpression(
  404. SourceLocation source_loc, Operator op,
  405. std::vector<Nonnull<Expression*>> arguments)
  406. : Expression(AstNodeKind::PrimitiveOperatorExpression, source_loc),
  407. op_(op),
  408. arguments_(std::move(arguments)) {}
  409. static auto classof(const AstNode* node) -> bool {
  410. return InheritsFromPrimitiveOperatorExpression(node->kind());
  411. }
  412. auto op() const -> Operator { return op_; }
  413. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  414. return arguments_;
  415. }
  416. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  417. return arguments_;
  418. }
  419. // Set the rewritten form of this expression. Can only be called during type
  420. // checking.
  421. auto set_rewritten_form(const Expression* rewritten_form) -> void {
  422. CARBON_CHECK(!rewritten_form_.has_value()) << "rewritten form set twice";
  423. rewritten_form_ = rewritten_form;
  424. set_static_type(&rewritten_form->static_type());
  425. set_value_category(rewritten_form->value_category());
  426. }
  427. // Get the rewritten form of this expression. A rewritten form is used when
  428. // the expression is rewritten as a function call on an interface. A
  429. // rewritten form is not used when providing built-in operator semantics.
  430. auto rewritten_form() const -> std::optional<Nonnull<const Expression*>> {
  431. return rewritten_form_;
  432. }
  433. private:
  434. Operator op_;
  435. std::vector<Nonnull<Expression*>> arguments_;
  436. std::optional<Nonnull<const Expression*>> rewritten_form_;
  437. };
  438. using ImplExpMap = std::map<Nonnull<const ImplBinding*>, Nonnull<Expression*>>;
  439. class CallExpression : public Expression {
  440. public:
  441. explicit CallExpression(SourceLocation source_loc,
  442. Nonnull<Expression*> function,
  443. Nonnull<Expression*> argument)
  444. : Expression(AstNodeKind::CallExpression, source_loc),
  445. function_(function),
  446. argument_(argument) {}
  447. static auto classof(const AstNode* node) -> bool {
  448. return InheritsFromCallExpression(node->kind());
  449. }
  450. auto function() const -> const Expression& { return *function_; }
  451. auto function() -> Expression& { return *function_; }
  452. auto argument() const -> const Expression& { return *argument_; }
  453. auto argument() -> Expression& { return *argument_; }
  454. // Maps each of `function`'s impl bindings to an expression
  455. // that constructs a witness table.
  456. // Should not be called before typechecking, or if `function` is not
  457. // a generic function.
  458. auto impls() const -> const ImplExpMap& { return impls_; }
  459. // Can only be called once, during typechecking.
  460. void set_impls(const ImplExpMap& impls) {
  461. CARBON_CHECK(impls_.empty());
  462. impls_ = impls;
  463. }
  464. auto deduced_args() const -> const BindingMap& { return deduced_args_; }
  465. void set_deduced_args(const BindingMap& deduced_args) {
  466. deduced_args_ = deduced_args;
  467. }
  468. // Can only be called by type-checking, if a conversion was required.
  469. void set_argument(Nonnull<Expression*> argument) { argument_ = argument; }
  470. private:
  471. Nonnull<Expression*> function_;
  472. Nonnull<Expression*> argument_;
  473. ImplExpMap impls_;
  474. BindingMap deduced_args_;
  475. };
  476. class FunctionTypeLiteral : public Expression {
  477. public:
  478. explicit FunctionTypeLiteral(SourceLocation source_loc,
  479. Nonnull<TupleLiteral*> parameter,
  480. Nonnull<Expression*> return_type)
  481. : Expression(AstNodeKind::FunctionTypeLiteral, source_loc),
  482. parameter_(parameter),
  483. return_type_(return_type) {}
  484. static auto classof(const AstNode* node) -> bool {
  485. return InheritsFromFunctionTypeLiteral(node->kind());
  486. }
  487. auto parameter() const -> const TupleLiteral& { return *parameter_; }
  488. auto parameter() -> TupleLiteral& { return *parameter_; }
  489. auto return_type() const -> const Expression& { return *return_type_; }
  490. auto return_type() -> Expression& { return *return_type_; }
  491. private:
  492. Nonnull<TupleLiteral*> parameter_;
  493. Nonnull<Expression*> return_type_;
  494. };
  495. class BoolTypeLiteral : public Expression {
  496. public:
  497. explicit BoolTypeLiteral(SourceLocation source_loc)
  498. : Expression(AstNodeKind::BoolTypeLiteral, source_loc) {}
  499. static auto classof(const AstNode* node) -> bool {
  500. return InheritsFromBoolTypeLiteral(node->kind());
  501. }
  502. };
  503. class IntTypeLiteral : public Expression {
  504. public:
  505. explicit IntTypeLiteral(SourceLocation source_loc)
  506. : Expression(AstNodeKind::IntTypeLiteral, source_loc) {}
  507. static auto classof(const AstNode* node) -> bool {
  508. return InheritsFromIntTypeLiteral(node->kind());
  509. }
  510. };
  511. class ContinuationTypeLiteral : public Expression {
  512. public:
  513. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  514. : Expression(AstNodeKind::ContinuationTypeLiteral, source_loc) {}
  515. static auto classof(const AstNode* node) -> bool {
  516. return InheritsFromContinuationTypeLiteral(node->kind());
  517. }
  518. };
  519. class TypeTypeLiteral : public Expression {
  520. public:
  521. explicit TypeTypeLiteral(SourceLocation source_loc)
  522. : Expression(AstNodeKind::TypeTypeLiteral, source_loc) {}
  523. static auto classof(const AstNode* node) -> bool {
  524. return InheritsFromTypeTypeLiteral(node->kind());
  525. }
  526. };
  527. // A literal value. This is used in desugaring, and can't be expressed in
  528. // source syntax.
  529. class ValueLiteral : public Expression {
  530. public:
  531. // Value literals are created by type-checking, and so are created with their
  532. // type and value category already known.
  533. ValueLiteral(SourceLocation source_loc, Nonnull<const Value*> value,
  534. Nonnull<const Value*> type, ValueCategory value_category)
  535. : Expression(AstNodeKind::ValueLiteral, source_loc), value_(value) {
  536. set_static_type(type);
  537. set_value_category(value_category);
  538. }
  539. static auto classof(const AstNode* node) -> bool {
  540. return InheritsFromValueLiteral(node->kind());
  541. }
  542. auto value() const -> const Value& { return *value_; }
  543. private:
  544. Nonnull<const Value*> value_;
  545. };
  546. class IntrinsicExpression : public Expression {
  547. public:
  548. enum class Intrinsic { Print, Alloc, Dealloc };
  549. // Returns the enumerator corresponding to the intrinsic named `name`,
  550. // or raises a fatal compile error if there is no such enumerator.
  551. static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
  552. -> ErrorOr<Intrinsic>;
  553. explicit IntrinsicExpression(Intrinsic intrinsic, Nonnull<TupleLiteral*> args,
  554. SourceLocation source_loc)
  555. : Expression(AstNodeKind::IntrinsicExpression, source_loc),
  556. intrinsic_(intrinsic),
  557. args_(args) {}
  558. static auto classof(const AstNode* node) -> bool {
  559. return InheritsFromIntrinsicExpression(node->kind());
  560. }
  561. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  562. auto args() const -> const TupleLiteral& { return *args_; }
  563. auto args() -> TupleLiteral& { return *args_; }
  564. private:
  565. Intrinsic intrinsic_;
  566. Nonnull<TupleLiteral*> args_;
  567. };
  568. class IfExpression : public Expression {
  569. public:
  570. explicit IfExpression(SourceLocation source_loc,
  571. Nonnull<Expression*> condition,
  572. Nonnull<Expression*> then_expression,
  573. Nonnull<Expression*> else_expression)
  574. : Expression(AstNodeKind::IfExpression, source_loc),
  575. condition_(condition),
  576. then_expression_(then_expression),
  577. else_expression_(else_expression) {}
  578. static auto classof(const AstNode* node) -> bool {
  579. return InheritsFromIfExpression(node->kind());
  580. }
  581. auto condition() const -> const Expression& { return *condition_; }
  582. auto condition() -> Expression& { return *condition_; }
  583. auto then_expression() const -> const Expression& {
  584. return *then_expression_;
  585. }
  586. auto then_expression() -> Expression& { return *then_expression_; }
  587. auto else_expression() const -> const Expression& {
  588. return *else_expression_;
  589. }
  590. auto else_expression() -> Expression& { return *else_expression_; }
  591. // Can only be called by type-checking, if a conversion was required.
  592. void set_condition(Nonnull<Expression*> condition) { condition_ = condition; }
  593. private:
  594. Nonnull<Expression*> condition_;
  595. Nonnull<Expression*> then_expression_;
  596. Nonnull<Expression*> else_expression_;
  597. };
  598. // A clause appearing on the right-hand side of a `where` operator that forms a
  599. // more precise constraint from a more general one.
  600. class WhereClause : public AstNode {
  601. public:
  602. ~WhereClause() override = 0;
  603. void Print(llvm::raw_ostream& out) const override;
  604. void PrintID(llvm::raw_ostream& out) const override;
  605. static auto classof(const AstNode* node) {
  606. return InheritsFromWhereClause(node->kind());
  607. }
  608. auto kind() const -> WhereClauseKind {
  609. return static_cast<WhereClauseKind>(root_kind());
  610. }
  611. protected:
  612. WhereClause(WhereClauseKind kind, SourceLocation source_loc)
  613. : AstNode(static_cast<AstNodeKind>(kind), source_loc) {}
  614. };
  615. // An `is` where clause.
  616. //
  617. // For example, `ConstraintA where .Type is ConstraintB` requires that the
  618. // associated type `.Type` implements the constraint `ConstraintB`.
  619. class IsWhereClause : public WhereClause {
  620. public:
  621. explicit IsWhereClause(SourceLocation source_loc, Nonnull<Expression*> type,
  622. Nonnull<Expression*> constraint)
  623. : WhereClause(WhereClauseKind::IsWhereClause, source_loc),
  624. type_(type),
  625. constraint_(constraint) {}
  626. static auto classof(const AstNode* node) {
  627. return InheritsFromIsWhereClause(node->kind());
  628. }
  629. auto type() const -> const Expression& { return *type_; }
  630. auto type() -> Expression& { return *type_; }
  631. auto constraint() const -> const Expression& { return *constraint_; }
  632. auto constraint() -> Expression& { return *constraint_; }
  633. private:
  634. Nonnull<Expression*> type_;
  635. Nonnull<Expression*> constraint_;
  636. };
  637. // An `==` where clause.
  638. //
  639. // For example, `Constraint where .Type == i32` requires that the associated
  640. // type `.Type` is `i32`.
  641. class EqualsWhereClause : public WhereClause {
  642. public:
  643. explicit EqualsWhereClause(SourceLocation source_loc,
  644. Nonnull<Expression*> lhs, Nonnull<Expression*> rhs)
  645. : WhereClause(WhereClauseKind::EqualsWhereClause, source_loc),
  646. lhs_(lhs),
  647. rhs_(rhs) {}
  648. static auto classof(const AstNode* node) {
  649. return InheritsFromEqualsWhereClause(node->kind());
  650. }
  651. auto lhs() const -> const Expression& { return *lhs_; }
  652. auto lhs() -> Expression& { return *lhs_; }
  653. auto rhs() const -> const Expression& { return *rhs_; }
  654. auto rhs() -> Expression& { return *rhs_; }
  655. private:
  656. Nonnull<Expression*> lhs_;
  657. Nonnull<Expression*> rhs_;
  658. };
  659. // A `where` expression: `AddableWith(i32) where .Result == i32`.
  660. //
  661. // The first operand is rewritten to a generic binding, for example
  662. // `.Self:! AddableWith(i32)`, which may be used in the clauses.
  663. class WhereExpression : public Expression {
  664. public:
  665. explicit WhereExpression(SourceLocation source_loc,
  666. Nonnull<GenericBinding*> self_binding,
  667. std::vector<Nonnull<WhereClause*>> clauses)
  668. : Expression(AstNodeKind::WhereExpression, source_loc),
  669. self_binding_(self_binding),
  670. clauses_(std::move(clauses)) {}
  671. static auto classof(const AstNode* node) -> bool {
  672. return InheritsFromWhereExpression(node->kind());
  673. }
  674. auto self_binding() const -> const GenericBinding& { return *self_binding_; }
  675. auto self_binding() -> GenericBinding& { return *self_binding_; }
  676. auto clauses() const -> llvm::ArrayRef<Nonnull<const WhereClause*>> {
  677. return clauses_;
  678. }
  679. auto clauses() -> llvm::ArrayRef<Nonnull<WhereClause*>> { return clauses_; }
  680. private:
  681. Nonnull<GenericBinding*> self_binding_;
  682. std::vector<Nonnull<WhereClause*>> clauses_;
  683. };
  684. // Instantiate a generic impl.
  685. class InstantiateImpl : public Expression {
  686. public:
  687. using ImplementsCarbonValueNode = void;
  688. explicit InstantiateImpl(SourceLocation source_loc,
  689. Nonnull<Expression*> generic_impl,
  690. const BindingMap& type_args, const ImplExpMap& impls)
  691. : Expression(AstNodeKind::InstantiateImpl, source_loc),
  692. generic_impl_(generic_impl),
  693. type_args_(type_args),
  694. impls_(impls) {}
  695. static auto classof(const AstNode* node) -> bool {
  696. return InheritsFromInstantiateImpl(node->kind());
  697. }
  698. auto generic_impl() const -> Nonnull<Expression*> { return generic_impl_; }
  699. auto type_args() const -> const BindingMap& { return type_args_; }
  700. // Maps each of the impl bindings to an expression that constructs
  701. // the witness table for that impl.
  702. auto impls() const -> const ImplExpMap& { return impls_; }
  703. private:
  704. Nonnull<Expression*> generic_impl_;
  705. BindingMap type_args_;
  706. ImplExpMap impls_;
  707. };
  708. // An expression whose semantics have not been implemented. This can be used
  709. // as a placeholder during development, in order to implement and test parsing
  710. // of a new expression syntax without having to implement its semantics.
  711. class UnimplementedExpression : public Expression {
  712. public:
  713. // Constructs an UnimplementedExpression with the given label and the given
  714. // children, which must all be convertible to Nonnull<AstNode*>. The label
  715. // should correspond roughly to the name of the class that will eventually
  716. // replace this usage of UnimplementedExpression.
  717. template <typename... Children>
  718. UnimplementedExpression(SourceLocation source_loc, std::string label,
  719. Children... children)
  720. : Expression(AstNodeKind::UnimplementedExpression, source_loc),
  721. label_(std::move(label)) {
  722. AddChildren(children...);
  723. }
  724. static auto classof(const AstNode* node) -> bool {
  725. return InheritsFromUnimplementedExpression(node->kind());
  726. }
  727. auto label() const -> std::string_view { return label_; }
  728. auto children() const -> llvm::ArrayRef<Nonnull<const AstNode*>> {
  729. return children_;
  730. }
  731. private:
  732. void AddChildren() {}
  733. template <typename... Children>
  734. void AddChildren(Nonnull<AstNode*> child, Children... children) {
  735. children_.push_back(child);
  736. AddChildren(children...);
  737. }
  738. std::string label_;
  739. std::vector<Nonnull<AstNode*>> children_;
  740. };
  741. // A literal representing a statically-sized array type.
  742. class ArrayTypeLiteral : public Expression {
  743. public:
  744. // Constructs an array type literal which uses the given expressions to
  745. // represent the element type and size.
  746. ArrayTypeLiteral(SourceLocation source_loc,
  747. Nonnull<Expression*> element_type_expression,
  748. Nonnull<Expression*> size_expression)
  749. : Expression(AstNodeKind::ArrayTypeLiteral, source_loc),
  750. element_type_expression_(element_type_expression),
  751. size_expression_(size_expression) {}
  752. static auto classof(const AstNode* node) -> bool {
  753. return InheritsFromArrayTypeLiteral(node->kind());
  754. }
  755. auto element_type_expression() const -> const Expression& {
  756. return *element_type_expression_;
  757. }
  758. auto element_type_expression() -> Expression& {
  759. return *element_type_expression_;
  760. }
  761. auto size_expression() const -> const Expression& {
  762. return *size_expression_;
  763. }
  764. auto size_expression() -> Expression& { return *size_expression_; }
  765. private:
  766. Nonnull<Expression*> element_type_expression_;
  767. Nonnull<Expression*> size_expression_;
  768. };
  769. // Converts paren_contents to an Expression, interpreting the parentheses as
  770. // grouping if their contents permit that interpretation, or as forming a
  771. // tuple otherwise.
  772. auto ExpressionFromParenContents(
  773. Nonnull<Arena*> arena, SourceLocation source_loc,
  774. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  775. // Converts paren_contents to an Expression, interpreting the parentheses as
  776. // forming a tuple.
  777. auto TupleExpressionFromParenContents(
  778. Nonnull<Arena*> arena, SourceLocation source_loc,
  779. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  780. } // namespace Carbon
  781. #endif // CARBON_EXPLORER_AST_EXPRESSION_H_