value.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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_VALUE_H_
  5. #define CARBON_EXPLORER_AST_VALUE_H_
  6. #include <optional>
  7. #include <string>
  8. #include <variant>
  9. #include <vector>
  10. #include "common/ostream.h"
  11. #include "explorer/ast/address.h"
  12. #include "explorer/ast/bindings.h"
  13. #include "explorer/ast/declaration.h"
  14. #include "explorer/ast/element.h"
  15. #include "explorer/ast/element_path.h"
  16. #include "explorer/ast/statement.h"
  17. #include "explorer/common/nonnull.h"
  18. #include "llvm/ADT/StringMap.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace Carbon {
  21. class AssociatedConstant;
  22. class ChoiceType;
  23. class TupleValue;
  24. // A trait type that describes how to allocate an instance of `T` in an arena.
  25. // Returns the created object, which is not required to be of type `T`.
  26. template <typename T>
  27. struct AllocateTrait {
  28. template <typename... Args>
  29. static auto New(Nonnull<Arena*> arena, Args&&... args) -> Nonnull<const T*> {
  30. return arena->New<T>(std::forward<Args>(args)...);
  31. }
  32. };
  33. using VTable =
  34. llvm::StringMap<std::pair<Nonnull<const CallableDeclaration*>, int>>;
  35. // Abstract base class of all AST nodes representing values.
  36. //
  37. // Value and its derived classes support LLVM-style RTTI, including
  38. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  39. // class derived from Value must provide a `classof` operation, and
  40. // every concrete derived class must have a corresponding enumerator
  41. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  42. // details.
  43. class Value {
  44. public:
  45. enum class Kind {
  46. #define CARBON_VALUE_KIND(kind) kind,
  47. #include "explorer/ast/value_kinds.def"
  48. };
  49. Value(const Value&) = delete;
  50. auto operator=(const Value&) -> Value& = delete;
  51. // Call `f` on this value, cast to its most-derived type. `R` specifies the
  52. // expected return type of `f`.
  53. template <typename R, typename F>
  54. auto Visit(F f) const -> R;
  55. void Print(llvm::raw_ostream& out) const;
  56. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  57. // Returns the sub-Value specified by `path`, which must be a valid element
  58. // path for *this. If the sub-Value is a method and its self_pattern is an
  59. // AddrPattern, then pass the LocationValue representing the receiver as
  60. // `me_value`, otherwise pass `*this`.
  61. auto GetElement(Nonnull<Arena*> arena, const ElementPath& path,
  62. SourceLocation source_loc,
  63. Nonnull<const Value*> me_value) const
  64. -> ErrorOr<Nonnull<const Value*>>;
  65. // Returns a copy of *this, but with the sub-Value specified by `path`
  66. // set to `field_value`. `path` must be a valid field path for *this.
  67. auto SetField(Nonnull<Arena*> arena, const ElementPath& path,
  68. Nonnull<const Value*> field_value,
  69. SourceLocation source_loc) const
  70. -> ErrorOr<Nonnull<const Value*>>;
  71. // Returns the enumerator corresponding to the most-derived type of this
  72. // object.
  73. auto kind() const -> Kind { return kind_; }
  74. protected:
  75. // Constructs a Value. `kind` must be the enumerator corresponding to the
  76. // most-derived type being constructed.
  77. explicit Value(Kind kind) : kind_(kind) {}
  78. private:
  79. const Kind kind_;
  80. };
  81. // Returns whether the fully-resolved kind that this value will eventually have
  82. // is currently unknown, because it depends on a generic parameter.
  83. inline auto IsValueKindDependent(Nonnull<const Value*> type) -> bool {
  84. return type->kind() == Value::Kind::VariableType ||
  85. type->kind() == Value::Kind::AssociatedConstant;
  86. }
  87. // Base class for types holding contextual information by which we can
  88. // determine whether values are equal.
  89. class EqualityContext {
  90. public:
  91. virtual auto VisitEqualValues(
  92. Nonnull<const Value*> value,
  93. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const
  94. -> bool = 0;
  95. protected:
  96. virtual ~EqualityContext() = default;
  97. };
  98. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2,
  99. std::optional<Nonnull<const EqualityContext*>> equality_ctx)
  100. -> bool;
  101. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
  102. std::optional<Nonnull<const EqualityContext*>> equality_ctx)
  103. -> bool;
  104. // An integer value.
  105. class IntValue : public Value {
  106. public:
  107. explicit IntValue(int value) : Value(Kind::IntValue), value_(value) {}
  108. static auto classof(const Value* value) -> bool {
  109. return value->kind() == Kind::IntValue;
  110. }
  111. template <typename F>
  112. auto Decompose(F f) const {
  113. return f(value_);
  114. }
  115. auto value() const -> int { return value_; }
  116. private:
  117. int value_;
  118. };
  119. // A function or bound method value.
  120. class FunctionOrMethodValue : public Value {
  121. public:
  122. explicit FunctionOrMethodValue(
  123. Kind kind, Nonnull<const FunctionDeclaration*> declaration,
  124. Nonnull<const Bindings*> bindings)
  125. : Value(kind), declaration_(declaration), bindings_(bindings) {}
  126. static auto classof(const Value* value) -> bool {
  127. return value->kind() == Kind::FunctionValue ||
  128. value->kind() == Kind::BoundMethodValue;
  129. }
  130. auto declaration() const -> const FunctionDeclaration& {
  131. return *declaration_;
  132. }
  133. auto bindings() const -> const Bindings& { return *bindings_; }
  134. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  135. auto witnesses() const -> const ImplWitnessMap& {
  136. return bindings_->witnesses();
  137. }
  138. private:
  139. Nonnull<const FunctionDeclaration*> declaration_;
  140. Nonnull<const Bindings*> bindings_;
  141. };
  142. // A function value.
  143. class FunctionValue : public FunctionOrMethodValue {
  144. public:
  145. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration,
  146. Nonnull<const Bindings*> bindings)
  147. : FunctionOrMethodValue(Kind::FunctionValue, declaration, bindings) {}
  148. static auto classof(const Value* value) -> bool {
  149. return value->kind() == Kind::FunctionValue;
  150. }
  151. template <typename F>
  152. auto Decompose(F f) const {
  153. return f(&declaration(), &bindings());
  154. }
  155. };
  156. // A bound method value. It includes the receiver object.
  157. class BoundMethodValue : public FunctionOrMethodValue {
  158. public:
  159. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  160. Nonnull<const Value*> receiver,
  161. Nonnull<const Bindings*> bindings)
  162. : FunctionOrMethodValue(Kind::BoundMethodValue, declaration, bindings),
  163. receiver_(receiver) {}
  164. static auto classof(const Value* value) -> bool {
  165. return value->kind() == Kind::BoundMethodValue;
  166. }
  167. template <typename F>
  168. auto Decompose(F f) const {
  169. return f(&declaration(), receiver_, &bindings());
  170. }
  171. auto receiver() const -> Nonnull<const Value*> { return receiver_; }
  172. private:
  173. Nonnull<const Value*> receiver_;
  174. };
  175. // A destructor value.
  176. class DestructorValue : public Value {
  177. public:
  178. explicit DestructorValue(Nonnull<const DestructorDeclaration*> declaration)
  179. : Value(Kind::DestructorValue), declaration_(declaration) {}
  180. static auto classof(const Value* value) -> bool {
  181. return value->kind() == Kind::DestructorValue;
  182. }
  183. template <typename F>
  184. auto Decompose(F f) const {
  185. return f(declaration_);
  186. }
  187. auto declaration() const -> const DestructorDeclaration& {
  188. return *declaration_;
  189. }
  190. private:
  191. Nonnull<const DestructorDeclaration*> declaration_;
  192. };
  193. // The value of a location in memory.
  194. class LocationValue : public Value {
  195. public:
  196. explicit LocationValue(Address value)
  197. : Value(Kind::LocationValue), value_(std::move(value)) {}
  198. static auto classof(const Value* value) -> bool {
  199. return value->kind() == Kind::LocationValue;
  200. }
  201. template <typename F>
  202. auto Decompose(F f) const {
  203. return f(value_);
  204. }
  205. auto address() const -> const Address& { return value_; }
  206. private:
  207. Address value_;
  208. };
  209. // A pointer value
  210. class PointerValue : public Value {
  211. public:
  212. explicit PointerValue(Address value)
  213. : Value(Kind::PointerValue), value_(std::move(value)) {}
  214. static auto classof(const Value* value) -> bool {
  215. return value->kind() == Kind::PointerValue;
  216. }
  217. template <typename F>
  218. auto Decompose(F f) const {
  219. return f(value_);
  220. }
  221. auto address() const -> const Address& { return value_; }
  222. private:
  223. Address value_;
  224. };
  225. // A bool value.
  226. class BoolValue : public Value {
  227. public:
  228. explicit BoolValue(bool value) : Value(Kind::BoolValue), value_(value) {}
  229. static auto classof(const Value* value) -> bool {
  230. return value->kind() == Kind::BoolValue;
  231. }
  232. template <typename F>
  233. auto Decompose(F f) const {
  234. return f(value_);
  235. }
  236. auto value() const -> bool { return value_; }
  237. private:
  238. bool value_;
  239. };
  240. // A value of a struct type. Note that the expression `{}` is a value of type
  241. // `{} as type`; the former is a `StructValue` and the latter is a
  242. // `StructType`.
  243. class StructValue : public Value {
  244. public:
  245. explicit StructValue(std::vector<NamedValue> elements)
  246. : Value(Kind::StructValue), elements_(std::move(elements)) {}
  247. static auto classof(const Value* value) -> bool {
  248. return value->kind() == Kind::StructValue;
  249. }
  250. template <typename F>
  251. auto Decompose(F f) const {
  252. return f(elements_);
  253. }
  254. auto elements() const -> llvm::ArrayRef<NamedValue> { return elements_; }
  255. // Returns the value of the field named `name` in this struct, or
  256. // nullopt if there is no such field.
  257. auto FindField(std::string_view name) const
  258. -> std::optional<Nonnull<const Value*>>;
  259. private:
  260. std::vector<NamedValue> elements_;
  261. };
  262. // A value of a nominal class type, i.e., an object.
  263. class NominalClassValue : public Value {
  264. public:
  265. static constexpr llvm::StringLiteral BaseField{"base"};
  266. // Takes the class type, inits, an optional base, a pointer to a
  267. // NominalClassValue*, that must be common to all NominalClassValue of the
  268. // same object. The pointee is updated, when `NominalClassValue`s are
  269. // constructed, to point to the `NominalClassValue` corresponding to the
  270. // child-most class type.
  271. NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits,
  272. std::optional<Nonnull<const NominalClassValue*>> base,
  273. Nonnull<const NominalClassValue** const> class_value_ptr);
  274. static auto classof(const Value* value) -> bool {
  275. return value->kind() == Kind::NominalClassValue;
  276. }
  277. template <typename F>
  278. auto Decompose(F f) const {
  279. return f(type_, inits_, base_, class_value_ptr_);
  280. }
  281. auto type() const -> const Value& { return *type_; }
  282. auto inits() const -> const Value& { return *inits_; }
  283. auto base() const -> std::optional<Nonnull<const NominalClassValue*>> {
  284. return base_;
  285. }
  286. // Returns a pointer of pointer to the child-most class value.
  287. auto class_value_ptr() const -> Nonnull<const NominalClassValue**> {
  288. return class_value_ptr_;
  289. }
  290. private:
  291. Nonnull<const Value*> type_;
  292. Nonnull<const Value*> inits_; // The initializing StructValue.
  293. std::optional<Nonnull<const NominalClassValue*>> base_;
  294. Nonnull<const NominalClassValue** const> class_value_ptr_;
  295. };
  296. // An alternative constructor value.
  297. class AlternativeConstructorValue : public Value {
  298. public:
  299. AlternativeConstructorValue(Nonnull<const ChoiceType*> choice,
  300. Nonnull<const AlternativeSignature*> alternative)
  301. : Value(Kind::AlternativeConstructorValue),
  302. choice_(choice),
  303. alternative_(alternative) {}
  304. static auto classof(const Value* value) -> bool {
  305. return value->kind() == Kind::AlternativeConstructorValue;
  306. }
  307. template <typename F>
  308. auto Decompose(F f) const {
  309. return f(&choice(), &alternative());
  310. }
  311. auto choice() const -> const ChoiceType& { return *choice_; }
  312. auto alternative() const -> const AlternativeSignature& {
  313. return *alternative_;
  314. }
  315. private:
  316. Nonnull<const ChoiceType*> choice_;
  317. Nonnull<const AlternativeSignature*> alternative_;
  318. };
  319. // An alternative value.
  320. class AlternativeValue : public Value {
  321. public:
  322. AlternativeValue(Nonnull<const ChoiceType*> choice,
  323. Nonnull<const AlternativeSignature*> alternative,
  324. std::optional<Nonnull<const TupleValue*>> argument)
  325. : Value(Kind::AlternativeValue),
  326. choice_(choice),
  327. alternative_(alternative),
  328. argument_(argument) {}
  329. static auto classof(const Value* value) -> bool {
  330. return value->kind() == Kind::AlternativeValue;
  331. }
  332. template <typename F>
  333. auto Decompose(F f) const {
  334. return f(&choice(), &alternative(), argument_);
  335. }
  336. auto choice() const -> const ChoiceType& { return *choice_; }
  337. auto alternative() const -> const AlternativeSignature& {
  338. return *alternative_;
  339. }
  340. auto argument() const -> std::optional<Nonnull<const TupleValue*>> {
  341. return argument_;
  342. }
  343. private:
  344. Nonnull<const ChoiceType*> choice_;
  345. Nonnull<const AlternativeSignature*> alternative_;
  346. std::optional<Nonnull<const TupleValue*>> argument_;
  347. };
  348. // Base class for tuple types and tuple values. These are the same other than
  349. // their type-of-type, but we separate them to make it easier to tell types and
  350. // values apart.
  351. class TupleValueBase : public Value {
  352. public:
  353. explicit TupleValueBase(Value::Kind kind,
  354. std::vector<Nonnull<const Value*>> elements)
  355. : Value(kind), elements_(std::move(elements)) {}
  356. auto elements() const -> llvm::ArrayRef<Nonnull<const Value*>> {
  357. return elements_;
  358. }
  359. static auto classof(const Value* value) -> bool {
  360. return value->kind() == Kind::TupleValue ||
  361. value->kind() == Kind::TupleType;
  362. }
  363. template <typename F>
  364. auto Decompose(F f) const {
  365. return f(elements_);
  366. }
  367. private:
  368. std::vector<Nonnull<const Value*>> elements_;
  369. };
  370. // A tuple value.
  371. class TupleValue : public TupleValueBase {
  372. public:
  373. // An empty tuple.
  374. static auto Empty() -> Nonnull<const TupleValue*> {
  375. static const TupleValue empty =
  376. TupleValue(std::vector<Nonnull<const Value*>>());
  377. return static_cast<Nonnull<const TupleValue*>>(&empty);
  378. }
  379. explicit TupleValue(std::vector<Nonnull<const Value*>> elements)
  380. : TupleValueBase(Kind::TupleValue, std::move(elements)) {}
  381. static auto classof(const Value* value) -> bool {
  382. return value->kind() == Kind::TupleValue;
  383. }
  384. };
  385. // A tuple type. These values are produced by converting a tuple value
  386. // containing only types to type `type`.
  387. class TupleType : public TupleValueBase {
  388. public:
  389. // The unit type.
  390. static auto Empty() -> Nonnull<const TupleType*> {
  391. static const TupleType empty =
  392. TupleType(std::vector<Nonnull<const Value*>>());
  393. return static_cast<Nonnull<const TupleType*>>(&empty);
  394. }
  395. explicit TupleType(std::vector<Nonnull<const Value*>> elements)
  396. : TupleValueBase(Kind::TupleType, std::move(elements)) {}
  397. static auto classof(const Value* value) -> bool {
  398. return value->kind() == Kind::TupleType;
  399. }
  400. };
  401. // A binding placeholder value.
  402. class BindingPlaceholderValue : public Value {
  403. public:
  404. // Represents the `_` placeholder.
  405. explicit BindingPlaceholderValue() : Value(Kind::BindingPlaceholderValue) {}
  406. // Represents a named placeholder.
  407. explicit BindingPlaceholderValue(ValueNodeView value_node)
  408. : Value(Kind::BindingPlaceholderValue),
  409. value_node_(std::move(value_node)) {}
  410. static auto classof(const Value* value) -> bool {
  411. return value->kind() == Kind::BindingPlaceholderValue;
  412. }
  413. template <typename F>
  414. auto Decompose(F f) const {
  415. return value_node_ ? f(*value_node_) : f();
  416. }
  417. auto value_node() const -> const std::optional<ValueNodeView>& {
  418. return value_node_;
  419. }
  420. private:
  421. std::optional<ValueNodeView> value_node_;
  422. };
  423. // Value for addr pattern
  424. class AddrValue : public Value {
  425. public:
  426. explicit AddrValue(Nonnull<const Value*> pattern)
  427. : Value(Kind::AddrValue), pattern_(pattern) {}
  428. static auto classof(const Value* value) -> bool {
  429. return value->kind() == Kind::AddrValue;
  430. }
  431. template <typename F>
  432. auto Decompose(F f) const {
  433. return f(pattern_);
  434. }
  435. auto pattern() const -> const Value& { return *pattern_; }
  436. private:
  437. Nonnull<const Value*> pattern_;
  438. };
  439. // Value for uninitialized local variables.
  440. class UninitializedValue : public Value {
  441. public:
  442. explicit UninitializedValue(Nonnull<const Value*> pattern)
  443. : Value(Kind::UninitializedValue), pattern_(pattern) {}
  444. static auto classof(const Value* value) -> bool {
  445. return value->kind() == Kind::UninitializedValue;
  446. }
  447. template <typename F>
  448. auto Decompose(F f) const {
  449. return f(pattern_);
  450. }
  451. auto pattern() const -> const Value& { return *pattern_; }
  452. private:
  453. Nonnull<const Value*> pattern_;
  454. };
  455. // The int type.
  456. class IntType : public Value {
  457. public:
  458. IntType() : Value(Kind::IntType) {}
  459. static auto classof(const Value* value) -> bool {
  460. return value->kind() == Kind::IntType;
  461. }
  462. template <typename F>
  463. auto Decompose(F f) const {
  464. return f();
  465. }
  466. };
  467. // The bool type.
  468. class BoolType : public Value {
  469. public:
  470. BoolType() : Value(Kind::BoolType) {}
  471. static auto classof(const Value* value) -> bool {
  472. return value->kind() == Kind::BoolType;
  473. }
  474. template <typename F>
  475. auto Decompose(F f) const {
  476. return f();
  477. }
  478. };
  479. // A type type.
  480. class TypeType : public Value {
  481. public:
  482. TypeType() : Value(Kind::TypeType) {}
  483. static auto classof(const Value* value) -> bool {
  484. return value->kind() == Kind::TypeType;
  485. }
  486. template <typename F>
  487. auto Decompose(F f) const {
  488. return f();
  489. }
  490. };
  491. // A function type.
  492. class FunctionType : public Value {
  493. public:
  494. // An explicit function parameter that is a `:!` binding:
  495. //
  496. // fn MakeEmptyVector(T:! type) -> Vector(T);
  497. struct GenericParameter {
  498. template <typename F>
  499. auto Decompose(F f) const {
  500. return f(index, binding);
  501. }
  502. size_t index;
  503. Nonnull<const GenericBinding*> binding;
  504. };
  505. FunctionType(Nonnull<const Value*> parameters,
  506. Nonnull<const Value*> return_type)
  507. : FunctionType(parameters, {}, return_type, {}, {}) {}
  508. FunctionType(Nonnull<const Value*> parameters,
  509. std::vector<GenericParameter> generic_parameters,
  510. Nonnull<const Value*> return_type,
  511. std::vector<Nonnull<const GenericBinding*>> deduced_bindings,
  512. std::vector<Nonnull<const ImplBinding*>> impl_bindings)
  513. : Value(Kind::FunctionType),
  514. parameters_(parameters),
  515. generic_parameters_(std::move(generic_parameters)),
  516. return_type_(return_type),
  517. deduced_bindings_(std::move(deduced_bindings)),
  518. impl_bindings_(std::move(impl_bindings)) {}
  519. static auto classof(const Value* value) -> bool {
  520. return value->kind() == Kind::FunctionType;
  521. }
  522. template <typename F>
  523. auto Decompose(F f) const {
  524. return f(parameters_, generic_parameters_, return_type_, deduced_bindings_,
  525. impl_bindings_);
  526. }
  527. // The type of the function parameter tuple.
  528. auto parameters() const -> const Value& { return *parameters_; }
  529. // Parameters that use a generic `:!` binding at the top level.
  530. auto generic_parameters() const -> llvm::ArrayRef<GenericParameter> {
  531. return generic_parameters_;
  532. }
  533. // The function return type.
  534. auto return_type() const -> const Value& { return *return_type_; }
  535. // All generic bindings in this function's signature that should be deduced
  536. // in a call. This excludes any generic parameters.
  537. auto deduced_bindings() const
  538. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  539. return deduced_bindings_;
  540. }
  541. // The bindings for the impl witness tables required by the
  542. // bounds on the type parameters of the generic function.
  543. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  544. return impl_bindings_;
  545. }
  546. private:
  547. Nonnull<const Value*> parameters_;
  548. std::vector<GenericParameter> generic_parameters_;
  549. Nonnull<const Value*> return_type_;
  550. std::vector<Nonnull<const GenericBinding*>> deduced_bindings_;
  551. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  552. };
  553. // A pointer type.
  554. class PointerType : public Value {
  555. public:
  556. // Constructs a pointer type with the given pointee type.
  557. explicit PointerType(Nonnull<const Value*> pointee_type)
  558. : Value(Kind::PointerType), pointee_type_(pointee_type) {}
  559. static auto classof(const Value* value) -> bool {
  560. return value->kind() == Kind::PointerType;
  561. }
  562. template <typename F>
  563. auto Decompose(F f) const {
  564. return f(pointee_type_);
  565. }
  566. auto pointee_type() const -> const Value& { return *pointee_type_; }
  567. private:
  568. Nonnull<const Value*> pointee_type_;
  569. };
  570. // The `auto` type.
  571. class AutoType : public Value {
  572. public:
  573. AutoType() : Value(Kind::AutoType) {}
  574. static auto classof(const Value* value) -> bool {
  575. return value->kind() == Kind::AutoType;
  576. }
  577. template <typename F>
  578. auto Decompose(F f) const {
  579. return f();
  580. }
  581. };
  582. // A struct type.
  583. class StructType : public Value {
  584. public:
  585. StructType() : StructType(std::vector<NamedValue>{}) {}
  586. explicit StructType(std::vector<NamedValue> fields)
  587. : Value(Kind::StructType), fields_(std::move(fields)) {}
  588. static auto classof(const Value* value) -> bool {
  589. return value->kind() == Kind::StructType;
  590. }
  591. template <typename F>
  592. auto Decompose(F f) const {
  593. return f(fields_);
  594. }
  595. auto fields() const -> llvm::ArrayRef<NamedValue> { return fields_; }
  596. private:
  597. std::vector<NamedValue> fields_;
  598. };
  599. // A class type.
  600. class NominalClassType : public Value {
  601. public:
  602. explicit NominalClassType(
  603. Nonnull<const ClassDeclaration*> declaration,
  604. Nonnull<const Bindings*> bindings,
  605. std::optional<Nonnull<const NominalClassType*>> base, VTable class_vtable)
  606. : Value(Kind::NominalClassType),
  607. declaration_(declaration),
  608. bindings_(bindings),
  609. base_(base),
  610. vtable_(std::move(class_vtable)),
  611. hierarchy_level_(base ? (*base)->hierarchy_level() + 1 : 0) {}
  612. static auto classof(const Value* value) -> bool {
  613. return value->kind() == Kind::NominalClassType;
  614. }
  615. template <typename F>
  616. auto Decompose(F f) const {
  617. return f(declaration_, bindings_, base_, vtable_);
  618. }
  619. auto declaration() const -> const ClassDeclaration& { return *declaration_; }
  620. auto bindings() const -> const Bindings& { return *bindings_; }
  621. auto base() const -> std::optional<Nonnull<const NominalClassType*>> {
  622. return base_;
  623. }
  624. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  625. // Witnesses for each of the class's impl bindings.
  626. auto witnesses() const -> const ImplWitnessMap& {
  627. return bindings_->witnesses();
  628. }
  629. auto vtable() const -> const VTable& { return vtable_; }
  630. // Returns how many levels from the top ancestor class it is. i.e. a class
  631. // with no base returns `0`, while a class with a `.base` and `.base.base`
  632. // returns `2`.
  633. auto hierarchy_level() const -> int { return hierarchy_level_; }
  634. // Returns whether this a parameterized class. That is, a class with
  635. // parameters and no corresponding arguments.
  636. auto IsParameterized() const -> bool {
  637. return declaration_->type_params().has_value() && type_args().empty();
  638. }
  639. // Returns whether this class is, or inherits `other`.
  640. auto InheritsClass(Nonnull<const Value*> other) const -> bool;
  641. private:
  642. Nonnull<const ClassDeclaration*> declaration_;
  643. Nonnull<const Bindings*> bindings_ = Bindings::None();
  644. const std::optional<Nonnull<const NominalClassType*>> base_;
  645. const VTable vtable_;
  646. int hierarchy_level_;
  647. };
  648. class MixinPseudoType : public Value {
  649. public:
  650. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration)
  651. : Value(Kind::MixinPseudoType), declaration_(declaration) {
  652. CARBON_CHECK(!declaration->params().has_value())
  653. << "missing arguments for parameterized mixin type";
  654. }
  655. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration,
  656. Nonnull<const Bindings*> bindings)
  657. : Value(Kind::MixinPseudoType),
  658. declaration_(declaration),
  659. bindings_(bindings) {}
  660. static auto classof(const Value* value) -> bool {
  661. return value->kind() == Kind::MixinPseudoType;
  662. }
  663. template <typename F>
  664. auto Decompose(F f) const {
  665. return f(declaration_, bindings_);
  666. }
  667. auto declaration() const -> const MixinDeclaration& { return *declaration_; }
  668. auto bindings() const -> const Bindings& { return *bindings_; }
  669. auto args() const -> const BindingMap& { return bindings_->args(); }
  670. auto witnesses() const -> const ImplWitnessMap& {
  671. return bindings_->witnesses();
  672. }
  673. auto FindFunction(const std::string_view& name) const
  674. -> std::optional<Nonnull<const FunctionValue*>>;
  675. private:
  676. Nonnull<const MixinDeclaration*> declaration_;
  677. Nonnull<const Bindings*> bindings_ = Bindings::None();
  678. };
  679. // Returns the value of the function named `name` in this class, or
  680. // nullopt if there is no such function.
  681. auto FindFunction(std::string_view name,
  682. llvm::ArrayRef<Nonnull<Declaration*>> members)
  683. -> std::optional<Nonnull<const FunctionValue*>>;
  684. // Returns the value of the function named `name` in this class and its
  685. // parents, or nullopt if there is no such function.
  686. auto FindFunctionWithParents(std::string_view name,
  687. const ClassDeclaration& class_decl)
  688. -> std::optional<Nonnull<const FunctionValue*>>;
  689. // Return the declaration of the member with the given name.
  690. auto FindMember(std::string_view name,
  691. llvm::ArrayRef<Nonnull<Declaration*>> members)
  692. -> std::optional<Nonnull<const Declaration*>>;
  693. // An interface type.
  694. class InterfaceType : public Value {
  695. public:
  696. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
  697. : Value(Kind::InterfaceType), declaration_(declaration) {
  698. CARBON_CHECK(!declaration->params().has_value())
  699. << "missing arguments for parameterized interface type";
  700. }
  701. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
  702. Nonnull<const Bindings*> bindings)
  703. : Value(Kind::InterfaceType),
  704. declaration_(declaration),
  705. bindings_(bindings) {}
  706. static auto classof(const Value* value) -> bool {
  707. return value->kind() == Kind::InterfaceType;
  708. }
  709. template <typename F>
  710. auto Decompose(F f) const {
  711. return f(declaration_, bindings_);
  712. }
  713. auto declaration() const -> const InterfaceDeclaration& {
  714. return *declaration_;
  715. }
  716. auto bindings() const -> const Bindings& { return *bindings_; }
  717. auto args() const -> const BindingMap& { return bindings_->args(); }
  718. auto witnesses() const -> const ImplWitnessMap& {
  719. return bindings_->witnesses();
  720. }
  721. private:
  722. Nonnull<const InterfaceDeclaration*> declaration_;
  723. Nonnull<const Bindings*> bindings_ = Bindings::None();
  724. };
  725. // A named constraint type.
  726. class NamedConstraintType : public Value {
  727. public:
  728. explicit NamedConstraintType(
  729. Nonnull<const ConstraintDeclaration*> declaration,
  730. Nonnull<const Bindings*> bindings)
  731. : Value(Kind::NamedConstraintType),
  732. declaration_(declaration),
  733. bindings_(bindings) {}
  734. static auto classof(const Value* value) -> bool {
  735. return value->kind() == Kind::NamedConstraintType;
  736. }
  737. template <typename F>
  738. auto Decompose(F f) const {
  739. return f(declaration_, bindings_);
  740. }
  741. auto declaration() const -> const ConstraintDeclaration& {
  742. return *declaration_;
  743. }
  744. auto bindings() const -> const Bindings& { return *bindings_; }
  745. private:
  746. Nonnull<const ConstraintDeclaration*> declaration_;
  747. Nonnull<const Bindings*> bindings_ = Bindings::None();
  748. };
  749. // A constraint that requires implementation of an interface.
  750. struct ImplsConstraint {
  751. template <typename F>
  752. auto Decompose(F f) const {
  753. return f(type, interface);
  754. }
  755. // The type that is required to implement the interface.
  756. Nonnull<const Value*> type;
  757. // The interface that is required to be implemented.
  758. Nonnull<const InterfaceType*> interface;
  759. };
  760. // A constraint that requires an intrinsic property of a type.
  761. struct IntrinsicConstraint {
  762. template <typename F>
  763. auto Decompose(F f) const {
  764. return f(type, kind, arguments);
  765. }
  766. // Print the intrinsic constraint.
  767. void Print(llvm::raw_ostream& out) const;
  768. // The type that is required to satisfy the intrinsic property.
  769. Nonnull<const Value*> type;
  770. // The kind of the intrinsic property.
  771. enum Kind {
  772. // `type` intrinsically implicitly converts to `parameters[0]`.
  773. // TODO: Split ImplicitAs into more specific constraints (such as
  774. // derived-to-base pointer conversions).
  775. ImplicitAs,
  776. };
  777. Kind kind;
  778. // Arguments for the intrinsic property. The meaning of these depends on
  779. // `kind`.
  780. std::vector<Nonnull<const Value*>> arguments;
  781. };
  782. // A constraint that a collection of values are known to be the same.
  783. struct EqualityConstraint {
  784. template <typename F>
  785. auto Decompose(F f) const {
  786. return f(values);
  787. }
  788. // Visit the values in this equality constraint that are a single step away
  789. // from the given value according to this equality constraint. That is: if
  790. // `value` is identical to a value in `values`, then call the visitor on all
  791. // values in `values` that are not identical to `value`. Otherwise, do not
  792. // call the visitor.
  793. //
  794. // Stops and returns `false` if any call to the visitor returns `false`,
  795. // otherwise returns `true`.
  796. auto VisitEqualValues(
  797. Nonnull<const Value*> value,
  798. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  799. std::vector<Nonnull<const Value*>> values;
  800. };
  801. // A constraint indicating that access to an associated constant should be
  802. // replaced by another value.
  803. struct RewriteConstraint {
  804. template <typename F>
  805. auto Decompose(F f) const {
  806. return f(constant, unconverted_replacement, unconverted_replacement_type,
  807. converted_replacement);
  808. }
  809. // The associated constant value that is rewritten.
  810. Nonnull<const AssociatedConstant*> constant;
  811. // The replacement in its original type.
  812. Nonnull<const Value*> unconverted_replacement;
  813. // The type of the replacement.
  814. Nonnull<const Value*> unconverted_replacement_type;
  815. // The replacement after conversion to the type of the associated constant.
  816. Nonnull<const Value*> converted_replacement;
  817. };
  818. // A context in which we might look up a name.
  819. struct LookupContext {
  820. template <typename F>
  821. auto Decompose(F f) const {
  822. return f(context);
  823. }
  824. Nonnull<const Value*> context;
  825. };
  826. // A type-of-type for an unknown constrained type.
  827. //
  828. // These types are formed by the `&` operator that combines constraints and by
  829. // `where` expressions.
  830. //
  831. // A constraint has three main properties:
  832. //
  833. // * A collection of (type, interface) pairs for interfaces that are known to
  834. // be implemented by a type satisfying the constraint.
  835. // * A collection of (type, intrinsic) pairs for intrinsic properties that are
  836. // known to be satisfied by a type satisfying the constraint.
  837. // * A collection of sets of values, typically associated constants, that are
  838. // known to be the same.
  839. // * A collection of contexts in which member name lookups will be performed
  840. // for a type variable whose type is this constraint.
  841. //
  842. // Within these properties, the constrained type can be referred to with a
  843. // `VariableType` naming the `self_binding`.
  844. class ConstraintType : public Value {
  845. public:
  846. explicit ConstraintType(
  847. Nonnull<const GenericBinding*> self_binding,
  848. std::vector<ImplsConstraint> impls_constraints,
  849. std::vector<IntrinsicConstraint> intrinsic_constraints,
  850. std::vector<EqualityConstraint> equality_constraints,
  851. std::vector<RewriteConstraint> rewrite_constraints,
  852. std::vector<LookupContext> lookup_contexts)
  853. : Value(Kind::ConstraintType),
  854. self_binding_(self_binding),
  855. impls_constraints_(std::move(impls_constraints)),
  856. intrinsic_constraints_(std::move(intrinsic_constraints)),
  857. equality_constraints_(std::move(equality_constraints)),
  858. rewrite_constraints_(std::move(rewrite_constraints)),
  859. lookup_contexts_(std::move(lookup_contexts)) {}
  860. static auto classof(const Value* value) -> bool {
  861. return value->kind() == Kind::ConstraintType;
  862. }
  863. template <typename F>
  864. auto Decompose(F f) const {
  865. return f(self_binding_, impls_constraints_, intrinsic_constraints_,
  866. equality_constraints_, rewrite_constraints_, lookup_contexts_);
  867. }
  868. auto self_binding() const -> Nonnull<const GenericBinding*> {
  869. return self_binding_;
  870. }
  871. auto impls_constraints() const -> llvm::ArrayRef<ImplsConstraint> {
  872. return impls_constraints_;
  873. }
  874. auto intrinsic_constraints() const -> llvm::ArrayRef<IntrinsicConstraint> {
  875. return intrinsic_constraints_;
  876. }
  877. auto equality_constraints() const -> llvm::ArrayRef<EqualityConstraint> {
  878. return equality_constraints_;
  879. }
  880. auto rewrite_constraints() const -> llvm::ArrayRef<RewriteConstraint> {
  881. return rewrite_constraints_;
  882. }
  883. auto lookup_contexts() const -> llvm::ArrayRef<LookupContext> {
  884. return lookup_contexts_;
  885. }
  886. // Visit the values in that are a single step away from the given value
  887. // according to equality constraints in this constraint type, that is, the
  888. // values `v` that are not identical to `value` but for which we have a
  889. // `value == v` equality constraint in this constraint type.
  890. //
  891. // Stops and returns `false` if any call to the visitor returns `false`,
  892. // otherwise returns `true`.
  893. auto VisitEqualValues(
  894. Nonnull<const Value*> value,
  895. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  896. private:
  897. Nonnull<const GenericBinding*> self_binding_;
  898. std::vector<ImplsConstraint> impls_constraints_;
  899. std::vector<IntrinsicConstraint> intrinsic_constraints_;
  900. std::vector<EqualityConstraint> equality_constraints_;
  901. std::vector<RewriteConstraint> rewrite_constraints_;
  902. std::vector<LookupContext> lookup_contexts_;
  903. };
  904. // A witness table.
  905. class Witness : public Value {
  906. protected:
  907. explicit Witness(Value::Kind kind) : Value(kind) {}
  908. public:
  909. static auto classof(const Value* value) -> bool {
  910. return value->kind() == Kind::ImplWitness ||
  911. value->kind() == Kind::BindingWitness ||
  912. value->kind() == Kind::ConstraintWitness ||
  913. value->kind() == Kind::ConstraintImplWitness;
  914. }
  915. };
  916. // The witness table for an impl.
  917. class ImplWitness : public Witness {
  918. public:
  919. // Construct a witness for an impl.
  920. explicit ImplWitness(Nonnull<const ImplDeclaration*> declaration,
  921. Nonnull<const Bindings*> bindings)
  922. : Witness(Kind::ImplWitness),
  923. declaration_(declaration),
  924. bindings_(bindings) {}
  925. static auto classof(const Value* value) -> bool {
  926. return value->kind() == Kind::ImplWitness;
  927. }
  928. template <typename F>
  929. auto Decompose(F f) const {
  930. return f(declaration_, bindings_);
  931. }
  932. auto declaration() const -> const ImplDeclaration& { return *declaration_; }
  933. auto bindings() const -> const Bindings& { return *bindings_; }
  934. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  935. auto witnesses() const -> const ImplWitnessMap& {
  936. return bindings_->witnesses();
  937. }
  938. private:
  939. Nonnull<const ImplDeclaration*> declaration_;
  940. Nonnull<const Bindings*> bindings_ = Bindings::None();
  941. };
  942. // The symbolic witness corresponding to an unresolved impl binding.
  943. class BindingWitness : public Witness {
  944. public:
  945. // Construct a witness for an impl binding.
  946. explicit BindingWitness(Nonnull<const ImplBinding*> binding)
  947. : Witness(Kind::BindingWitness), binding_(binding) {}
  948. static auto classof(const Value* value) -> bool {
  949. return value->kind() == Kind::BindingWitness;
  950. }
  951. template <typename F>
  952. auto Decompose(F f) const {
  953. return f(binding_);
  954. }
  955. auto binding() const -> Nonnull<const ImplBinding*> { return binding_; }
  956. private:
  957. Nonnull<const ImplBinding*> binding_;
  958. };
  959. // A witness for a constraint type, expressed as a tuple of witnesses for the
  960. // individual impls constraints in the constraint type.
  961. class ConstraintWitness : public Witness {
  962. public:
  963. explicit ConstraintWitness(std::vector<Nonnull<const Witness*>> witnesses)
  964. : Witness(Kind::ConstraintWitness), witnesses_(std::move(witnesses)) {}
  965. static auto classof(const Value* value) -> bool {
  966. return value->kind() == Kind::ConstraintWitness;
  967. }
  968. template <typename F>
  969. auto Decompose(F f) const {
  970. return f(witnesses_);
  971. }
  972. auto witnesses() const -> llvm::ArrayRef<Nonnull<const Witness*>> {
  973. return witnesses_;
  974. }
  975. private:
  976. std::vector<Nonnull<const Witness*>> witnesses_;
  977. };
  978. // A witness for an impls constraint in a constraint type, expressed in terms of
  979. // a symbolic witness for the constraint type.
  980. class ConstraintImplWitness : public Witness {
  981. public:
  982. // Make a witness for the given impls_constraint of the given `ConstraintType`
  983. // witness. If we're indexing into a known tuple of witnesses, pull out the
  984. // element.
  985. static auto Make(Nonnull<Arena*> arena, Nonnull<const Witness*> witness,
  986. int index) -> Nonnull<const Witness*> {
  987. CARBON_CHECK(!llvm::isa<ImplWitness>(witness))
  988. << "impl witness has no components to access";
  989. if (const auto* constraint_witness =
  990. llvm::dyn_cast<ConstraintWitness>(witness)) {
  991. return constraint_witness->witnesses()[index];
  992. }
  993. return arena->New<ConstraintImplWitness>(witness, index);
  994. }
  995. explicit ConstraintImplWitness(Nonnull<const Witness*> constraint_witness,
  996. int index)
  997. : Witness(Kind::ConstraintImplWitness),
  998. constraint_witness_(constraint_witness),
  999. index_(index) {
  1000. CARBON_CHECK(!llvm::isa<ConstraintWitness>(constraint_witness))
  1001. << "should have resolved element from constraint witness";
  1002. }
  1003. static auto classof(const Value* value) -> bool {
  1004. return value->kind() == Kind::ConstraintImplWitness;
  1005. }
  1006. template <typename F>
  1007. auto Decompose(F f) const {
  1008. return f(constraint_witness_, index_);
  1009. }
  1010. // Get the witness for the complete `ConstraintType`.
  1011. auto constraint_witness() const -> Nonnull<const Witness*> {
  1012. return constraint_witness_;
  1013. }
  1014. // Get the index of the impls constraint within the constraint type.
  1015. auto index() const -> int { return index_; }
  1016. private:
  1017. Nonnull<const Witness*> constraint_witness_;
  1018. int index_;
  1019. };
  1020. // Allocate a `ConstraintImplWitness` using the custom `Make` function.
  1021. template <>
  1022. struct AllocateTrait<ConstraintImplWitness> {
  1023. template <typename... Args>
  1024. static auto New(Nonnull<Arena*> arena, Args&&... args)
  1025. -> Nonnull<const Witness*> {
  1026. return ConstraintImplWitness::Make(arena, std::forward<Args>(args)...);
  1027. }
  1028. };
  1029. // A choice type.
  1030. class ChoiceType : public Value {
  1031. public:
  1032. ChoiceType(Nonnull<const ChoiceDeclaration*> declaration,
  1033. Nonnull<const Bindings*> bindings)
  1034. : Value(Kind::ChoiceType),
  1035. declaration_(declaration),
  1036. bindings_(bindings) {}
  1037. static auto classof(const Value* value) -> bool {
  1038. return value->kind() == Kind::ChoiceType;
  1039. }
  1040. template <typename F>
  1041. auto Decompose(F f) const {
  1042. return f(declaration_, bindings_);
  1043. }
  1044. auto bindings() const -> const Bindings& { return *bindings_; }
  1045. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  1046. auto declaration() const -> const ChoiceDeclaration& { return *declaration_; }
  1047. auto IsParameterized() const -> bool {
  1048. return declaration_->type_params().has_value();
  1049. }
  1050. private:
  1051. Nonnull<const ChoiceDeclaration*> declaration_;
  1052. Nonnull<const Bindings*> bindings_;
  1053. };
  1054. // A variable type.
  1055. class VariableType : public Value {
  1056. public:
  1057. explicit VariableType(Nonnull<const GenericBinding*> binding)
  1058. : Value(Kind::VariableType), binding_(binding) {}
  1059. static auto classof(const Value* value) -> bool {
  1060. return value->kind() == Kind::VariableType;
  1061. }
  1062. template <typename F>
  1063. auto Decompose(F f) const {
  1064. return f(binding_);
  1065. }
  1066. auto binding() const -> const GenericBinding& { return *binding_; }
  1067. private:
  1068. Nonnull<const GenericBinding*> binding_;
  1069. };
  1070. // A name of an entity that has explicit parameters, such as a parameterized
  1071. // class or interface. When arguments for those parameters are provided in a
  1072. // call, the result will be a class type or interface type.
  1073. class ParameterizedEntityName : public Value {
  1074. public:
  1075. explicit ParameterizedEntityName(Nonnull<const Declaration*> declaration,
  1076. Nonnull<const TuplePattern*> params)
  1077. : Value(Kind::ParameterizedEntityName),
  1078. declaration_(declaration),
  1079. params_(params) {}
  1080. static auto classof(const Value* value) -> bool {
  1081. return value->kind() == Kind::ParameterizedEntityName;
  1082. }
  1083. template <typename F>
  1084. auto Decompose(F f) const {
  1085. return f(declaration_, params_);
  1086. }
  1087. auto declaration() const -> const Declaration& { return *declaration_; }
  1088. auto params() const -> const TuplePattern& { return *params_; }
  1089. private:
  1090. Nonnull<const Declaration*> declaration_;
  1091. Nonnull<const TuplePattern*> params_;
  1092. };
  1093. // The name of a member of a class or interface.
  1094. //
  1095. // These values are used to represent the second operand of a compound member
  1096. // access expression: `x.(A.B)`, and can also be the value of an alias
  1097. // declaration, but cannot be used in most other contexts.
  1098. class MemberName : public Value {
  1099. public:
  1100. MemberName(std::optional<Nonnull<const Value*>> base_type,
  1101. std::optional<Nonnull<const InterfaceType*>> interface,
  1102. NamedElement member)
  1103. : Value(Kind::MemberName),
  1104. base_type_(base_type),
  1105. interface_(interface),
  1106. member_(std::move(member)) {
  1107. CARBON_CHECK(base_type || interface)
  1108. << "member name must be in a type, an interface, or both";
  1109. }
  1110. static auto classof(const Value* value) -> bool {
  1111. return value->kind() == Kind::MemberName;
  1112. }
  1113. template <typename F>
  1114. auto Decompose(F f) const {
  1115. return f(base_type_, interface_, member_);
  1116. }
  1117. // Prints the member name or identifier.
  1118. void Print(llvm::raw_ostream& out) const { member_.Print(out); }
  1119. // The type for which `name` is a member or a member of an `impl`.
  1120. auto base_type() const -> std::optional<Nonnull<const Value*>> {
  1121. return base_type_;
  1122. }
  1123. // The interface for which `name` is a member, if any.
  1124. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  1125. return interface_;
  1126. }
  1127. // The member.
  1128. auto member() const -> const NamedElement& { return member_; }
  1129. // The name of the member.
  1130. auto name() const -> std::string_view { return member().name(); }
  1131. private:
  1132. std::optional<Nonnull<const Value*>> base_type_;
  1133. std::optional<Nonnull<const InterfaceType*>> interface_;
  1134. NamedElement member_;
  1135. };
  1136. // A symbolic value representing an associated constant.
  1137. //
  1138. // This is a value of the form `A.B` or `A.B.C` or similar, where `A` is a
  1139. // `VariableType`.
  1140. class AssociatedConstant : public Value {
  1141. public:
  1142. explicit AssociatedConstant(
  1143. Nonnull<const Value*> base, Nonnull<const InterfaceType*> interface,
  1144. Nonnull<const AssociatedConstantDeclaration*> constant,
  1145. Nonnull<const Witness*> witness)
  1146. : Value(Kind::AssociatedConstant),
  1147. base_(base),
  1148. interface_(interface),
  1149. constant_(constant),
  1150. witness_(witness) {}
  1151. static auto classof(const Value* value) -> bool {
  1152. return value->kind() == Kind::AssociatedConstant;
  1153. }
  1154. template <typename F>
  1155. auto Decompose(F f) const {
  1156. return f(base_, interface_, constant_, witness_);
  1157. }
  1158. // The type for which we denote an associated constant.
  1159. auto base() const -> const Value& { return *base_; }
  1160. // The interface within which the constant was declared.
  1161. auto interface() const -> const InterfaceType& { return *interface_; }
  1162. // The associated constant whose value is being denoted.
  1163. auto constant() const -> const AssociatedConstantDeclaration& {
  1164. return *constant_;
  1165. }
  1166. // Witness within which the constant's value can be found.
  1167. auto witness() const -> const Witness& { return *witness_; }
  1168. private:
  1169. Nonnull<const Value*> base_;
  1170. Nonnull<const InterfaceType*> interface_;
  1171. Nonnull<const AssociatedConstantDeclaration*> constant_;
  1172. Nonnull<const Witness*> witness_;
  1173. };
  1174. // The String type.
  1175. class StringType : public Value {
  1176. public:
  1177. StringType() : Value(Kind::StringType) {}
  1178. static auto classof(const Value* value) -> bool {
  1179. return value->kind() == Kind::StringType;
  1180. }
  1181. template <typename F>
  1182. auto Decompose(F f) const {
  1183. return f();
  1184. }
  1185. };
  1186. // A string value.
  1187. class StringValue : public Value {
  1188. public:
  1189. explicit StringValue(std::string value)
  1190. : Value(Kind::StringValue), value_(std::move(value)) {}
  1191. static auto classof(const Value* value) -> bool {
  1192. return value->kind() == Kind::StringValue;
  1193. }
  1194. template <typename F>
  1195. auto Decompose(F f) const {
  1196. return f(value_);
  1197. }
  1198. auto value() const -> const std::string& { return value_; }
  1199. private:
  1200. std::string value_;
  1201. };
  1202. class TypeOfMixinPseudoType : public Value {
  1203. public:
  1204. explicit TypeOfMixinPseudoType(Nonnull<const MixinPseudoType*> class_type)
  1205. : Value(Kind::TypeOfMixinPseudoType), mixin_type_(class_type) {}
  1206. static auto classof(const Value* value) -> bool {
  1207. return value->kind() == Kind::TypeOfMixinPseudoType;
  1208. }
  1209. template <typename F>
  1210. auto Decompose(F f) const {
  1211. return f(mixin_type_);
  1212. }
  1213. auto mixin_type() const -> const MixinPseudoType& { return *mixin_type_; }
  1214. private:
  1215. Nonnull<const MixinPseudoType*> mixin_type_;
  1216. };
  1217. // The type of an expression whose value is the name of a parameterized entity.
  1218. // Such an expression can only be used as the operand of a call expression that
  1219. // provides arguments for the parameters.
  1220. class TypeOfParameterizedEntityName : public Value {
  1221. public:
  1222. explicit TypeOfParameterizedEntityName(
  1223. Nonnull<const ParameterizedEntityName*> name)
  1224. : Value(Kind::TypeOfParameterizedEntityName), name_(name) {}
  1225. static auto classof(const Value* value) -> bool {
  1226. return value->kind() == Kind::TypeOfParameterizedEntityName;
  1227. }
  1228. template <typename F>
  1229. auto Decompose(F f) const {
  1230. return f(name_);
  1231. }
  1232. auto name() const -> const ParameterizedEntityName& { return *name_; }
  1233. private:
  1234. Nonnull<const ParameterizedEntityName*> name_;
  1235. };
  1236. // The type of a member name expression.
  1237. //
  1238. // This is used for member names that don't denote a specific object or value
  1239. // until used on the right-hand side of a `.`, such as an instance method or
  1240. // field name, or any member function in an interface.
  1241. //
  1242. // Such expressions can appear only as the target of an `alias` declaration or
  1243. // as the member name in a compound member access.
  1244. class TypeOfMemberName : public Value {
  1245. public:
  1246. explicit TypeOfMemberName(NamedElement member)
  1247. : Value(Kind::TypeOfMemberName), member_(std::move(member)) {}
  1248. static auto classof(const Value* value) -> bool {
  1249. return value->kind() == Kind::TypeOfMemberName;
  1250. }
  1251. template <typename F>
  1252. auto Decompose(F f) const {
  1253. return f(member_);
  1254. }
  1255. // TODO: consider removing this or moving it elsewhere in the AST,
  1256. // since it's arguably part of the expression value rather than its type.
  1257. auto member() const -> NamedElement { return member_; }
  1258. private:
  1259. NamedElement member_;
  1260. };
  1261. // The type of a namespace name.
  1262. //
  1263. // Such expressions can appear only as the target of an `alias` declaration or
  1264. // as the left-hand side of a simple member access expression.
  1265. class TypeOfNamespaceName : public Value {
  1266. public:
  1267. explicit TypeOfNamespaceName(
  1268. Nonnull<const NamespaceDeclaration*> namespace_decl)
  1269. : Value(Kind::TypeOfNamespaceName), namespace_decl_(namespace_decl) {}
  1270. static auto classof(const Value* value) -> bool {
  1271. return value->kind() == Kind::TypeOfNamespaceName;
  1272. }
  1273. template <typename F>
  1274. auto Decompose(F f) const {
  1275. return f(namespace_decl_);
  1276. }
  1277. auto namespace_decl() const -> Nonnull<const NamespaceDeclaration*> {
  1278. return namespace_decl_;
  1279. }
  1280. private:
  1281. Nonnull<const NamespaceDeclaration*> namespace_decl_;
  1282. };
  1283. // The type of a statically-sized array.
  1284. //
  1285. // Note that values of this type are represented as tuples.
  1286. class StaticArrayType : public Value {
  1287. public:
  1288. // Constructs a statically-sized array type with the given element type and
  1289. // size.
  1290. StaticArrayType(Nonnull<const Value*> element_type,
  1291. std::optional<size_t> size)
  1292. : Value(Kind::StaticArrayType),
  1293. element_type_(element_type),
  1294. size_(size) {}
  1295. static auto classof(const Value* value) -> bool {
  1296. return value->kind() == Kind::StaticArrayType;
  1297. }
  1298. template <typename F>
  1299. auto Decompose(F f) const {
  1300. return f(element_type_, size_);
  1301. }
  1302. auto element_type() const -> const Value& { return *element_type_; }
  1303. auto size() const -> size_t {
  1304. CARBON_CHECK(has_size());
  1305. return *size_;
  1306. }
  1307. auto has_size() const -> bool { return size_.has_value(); }
  1308. private:
  1309. Nonnull<const Value*> element_type_;
  1310. std::optional<size_t> size_;
  1311. };
  1312. template <typename R, typename F>
  1313. auto Value::Visit(F f) const -> R {
  1314. switch (kind()) {
  1315. #define CARBON_VALUE_KIND(kind) \
  1316. case Kind::kind: \
  1317. return f(static_cast<const kind*>(this));
  1318. #include "explorer/ast/value_kinds.def"
  1319. }
  1320. }
  1321. } // namespace Carbon
  1322. #endif // CARBON_EXPLORER_AST_VALUE_H_