expression.h 30 KB

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