value.h 50 KB

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