expression.h 30 KB

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