value.h 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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, size_t size)
  1291. : Value(Kind::StaticArrayType),
  1292. element_type_(element_type),
  1293. size_(size) {}
  1294. static auto classof(const Value* value) -> bool {
  1295. return value->kind() == Kind::StaticArrayType;
  1296. }
  1297. template <typename F>
  1298. auto Decompose(F f) const {
  1299. return f(element_type_, size_);
  1300. }
  1301. auto element_type() const -> const Value& { return *element_type_; }
  1302. auto size() const -> size_t { return size_; }
  1303. private:
  1304. Nonnull<const Value*> element_type_;
  1305. size_t size_;
  1306. };
  1307. template <typename R, typename F>
  1308. auto Value::Visit(F f) const -> R {
  1309. switch (kind()) {
  1310. #define CARBON_VALUE_KIND(kind) \
  1311. case Kind::kind: \
  1312. return f(static_cast<const kind*>(this));
  1313. #include "explorer/ast/value_kinds.def"
  1314. }
  1315. }
  1316. } // namespace Carbon
  1317. #endif // CARBON_EXPLORER_AST_VALUE_H_