value.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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. // 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/interpreter/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 LValue representing the receiver as `me_value`,
  60. // 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 value.
  120. class FunctionValue : public Value {
  121. public:
  122. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration)
  123. : Value(Kind::FunctionValue), declaration_(declaration) {}
  124. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration,
  125. Nonnull<const Bindings*> bindings)
  126. : Value(Kind::FunctionValue),
  127. declaration_(declaration),
  128. bindings_(bindings) {}
  129. static auto classof(const Value* value) -> bool {
  130. return value->kind() == Kind::FunctionValue;
  131. }
  132. template <typename F>
  133. auto Decompose(F f) const {
  134. return f(declaration_, bindings_);
  135. }
  136. auto declaration() const -> const FunctionDeclaration& {
  137. return *declaration_;
  138. }
  139. auto bindings() const -> const Bindings& { return *bindings_; }
  140. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  141. auto witnesses() const -> const ImplWitnessMap& {
  142. return bindings_->witnesses();
  143. }
  144. private:
  145. Nonnull<const FunctionDeclaration*> declaration_;
  146. Nonnull<const Bindings*> bindings_ = Bindings::None();
  147. };
  148. // A destructor value.
  149. class DestructorValue : public Value {
  150. public:
  151. explicit DestructorValue(Nonnull<const DestructorDeclaration*> declaration)
  152. : Value(Kind::DestructorValue), declaration_(declaration) {}
  153. static auto classof(const Value* value) -> bool {
  154. return value->kind() == Kind::DestructorValue;
  155. }
  156. template <typename F>
  157. auto Decompose(F f) const {
  158. return f(declaration_);
  159. }
  160. auto declaration() const -> const DestructorDeclaration& {
  161. return *declaration_;
  162. }
  163. private:
  164. Nonnull<const DestructorDeclaration*> declaration_;
  165. };
  166. // A bound method value. It includes the receiver object.
  167. class BoundMethodValue : public Value {
  168. public:
  169. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  170. Nonnull<const Value*> receiver)
  171. : Value(Kind::BoundMethodValue),
  172. declaration_(declaration),
  173. receiver_(receiver) {}
  174. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  175. Nonnull<const Value*> receiver,
  176. Nonnull<const Bindings*> bindings)
  177. : Value(Kind::BoundMethodValue),
  178. declaration_(declaration),
  179. receiver_(receiver),
  180. bindings_(bindings) {}
  181. static auto classof(const Value* value) -> bool {
  182. return value->kind() == Kind::BoundMethodValue;
  183. }
  184. template <typename F>
  185. auto Decompose(F f) const {
  186. return f(declaration_, receiver_, bindings_);
  187. }
  188. auto declaration() const -> const FunctionDeclaration& {
  189. return *declaration_;
  190. }
  191. auto receiver() const -> Nonnull<const Value*> { return receiver_; }
  192. auto bindings() const -> const Bindings& { return *bindings_; }
  193. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  194. auto witnesses() const -> const ImplWitnessMap& {
  195. return bindings_->witnesses();
  196. }
  197. private:
  198. Nonnull<const FunctionDeclaration*> declaration_;
  199. Nonnull<const Value*> receiver_;
  200. Nonnull<const Bindings*> bindings_ = Bindings::None();
  201. };
  202. // The value of a location in memory.
  203. class LValue : public Value {
  204. public:
  205. explicit LValue(Address value)
  206. : Value(Kind::LValue), value_(std::move(value)) {}
  207. static auto classof(const Value* value) -> bool {
  208. return value->kind() == Kind::LValue;
  209. }
  210. template <typename F>
  211. auto Decompose(F f) const {
  212. return f(value_);
  213. }
  214. auto address() const -> const Address& { return value_; }
  215. private:
  216. Address value_;
  217. };
  218. // A pointer value
  219. class PointerValue : public Value {
  220. public:
  221. explicit PointerValue(Address value)
  222. : Value(Kind::PointerValue), value_(std::move(value)) {}
  223. static auto classof(const Value* value) -> bool {
  224. return value->kind() == Kind::PointerValue;
  225. }
  226. template <typename F>
  227. auto Decompose(F f) const {
  228. return f(value_);
  229. }
  230. auto address() const -> const Address& { return value_; }
  231. private:
  232. Address value_;
  233. };
  234. // A bool value.
  235. class BoolValue : public Value {
  236. public:
  237. explicit BoolValue(bool value) : Value(Kind::BoolValue), value_(value) {}
  238. static auto classof(const Value* value) -> bool {
  239. return value->kind() == Kind::BoolValue;
  240. }
  241. template <typename F>
  242. auto Decompose(F f) const {
  243. return f(value_);
  244. }
  245. auto value() const -> bool { return value_; }
  246. private:
  247. bool value_;
  248. };
  249. // A value of a struct type. Note that the expression `{}` is a value of type
  250. // `{} as type`; the former is a `StructValue` and the latter is a
  251. // `StructType`.
  252. class StructValue : public Value {
  253. public:
  254. explicit StructValue(std::vector<NamedValue> elements)
  255. : Value(Kind::StructValue), elements_(std::move(elements)) {}
  256. static auto classof(const Value* value) -> bool {
  257. return value->kind() == Kind::StructValue;
  258. }
  259. template <typename F>
  260. auto Decompose(F f) const {
  261. return f(elements_);
  262. }
  263. auto elements() const -> llvm::ArrayRef<NamedValue> { return elements_; }
  264. // Returns the value of the field named `name` in this struct, or
  265. // nullopt if there is no such field.
  266. auto FindField(std::string_view name) const
  267. -> std::optional<Nonnull<const Value*>>;
  268. private:
  269. std::vector<NamedValue> elements_;
  270. };
  271. // A value of a nominal class type, i.e., an object.
  272. class NominalClassValue : public Value {
  273. public:
  274. static constexpr llvm::StringLiteral BaseField{"base"};
  275. // Takes the class type, inits, an optional base, a pointer to a
  276. // NominalClassValue*, that must be common to all NominalClassValue of the
  277. // same object. The pointee is updated, when `NominalClassValue`s are
  278. // constructed, to point to the `NominalClassValue` corresponding to the
  279. // child-most class type.
  280. NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits,
  281. std::optional<Nonnull<const NominalClassValue*>> base,
  282. Nonnull<const NominalClassValue** const> class_value_ptr);
  283. static auto classof(const Value* value) -> bool {
  284. return value->kind() == Kind::NominalClassValue;
  285. }
  286. template <typename F>
  287. auto Decompose(F f) const {
  288. return f(type_, inits_, base_, class_value_ptr_);
  289. }
  290. auto type() const -> const Value& { return *type_; }
  291. auto inits() const -> const Value& { return *inits_; }
  292. auto base() const -> std::optional<Nonnull<const NominalClassValue*>> {
  293. return base_;
  294. }
  295. // Returns a pointer of pointer to the child-most class value.
  296. auto class_value_ptr() const -> Nonnull<const NominalClassValue** const> {
  297. return class_value_ptr_;
  298. }
  299. private:
  300. Nonnull<const Value*> type_;
  301. Nonnull<const Value*> inits_; // The initializing StructValue.
  302. std::optional<Nonnull<const NominalClassValue*>> base_;
  303. Nonnull<const NominalClassValue** const> class_value_ptr_;
  304. };
  305. // An alternative constructor value.
  306. class AlternativeConstructorValue : public Value {
  307. public:
  308. AlternativeConstructorValue(std::string_view alt_name,
  309. std::string_view choice_name)
  310. : Value(Kind::AlternativeConstructorValue),
  311. alt_name_(alt_name),
  312. choice_name_(choice_name) {}
  313. static auto classof(const Value* value) -> bool {
  314. return value->kind() == Kind::AlternativeConstructorValue;
  315. }
  316. template <typename F>
  317. auto Decompose(F f) const {
  318. return f(alt_name_, choice_name_);
  319. }
  320. auto alt_name() const -> const std::string& { return alt_name_; }
  321. auto choice_name() const -> const std::string& { return choice_name_; }
  322. private:
  323. std::string alt_name_;
  324. std::string choice_name_;
  325. };
  326. // An alternative value.
  327. class AlternativeValue : public Value {
  328. public:
  329. AlternativeValue(std::string_view alt_name, std::string_view choice_name,
  330. Nonnull<const Value*> argument)
  331. : Value(Kind::AlternativeValue),
  332. alt_name_(alt_name),
  333. choice_name_(choice_name),
  334. argument_(argument) {}
  335. static auto classof(const Value* value) -> bool {
  336. return value->kind() == Kind::AlternativeValue;
  337. }
  338. template <typename F>
  339. auto Decompose(F f) const {
  340. return f(alt_name_, choice_name_, argument_);
  341. }
  342. auto alt_name() const -> const std::string& { return alt_name_; }
  343. auto choice_name() const -> const std::string& { return choice_name_; }
  344. auto argument() const -> const Value& { return *argument_; }
  345. private:
  346. std::string alt_name_;
  347. std::string choice_name_;
  348. Nonnull<const Value*> 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. // Construct a non-generic class type.
  601. explicit NominalClassType(
  602. Nonnull<const ClassDeclaration*> declaration,
  603. std::optional<Nonnull<const NominalClassType*>> base, VTable class_vtable)
  604. : Value(Kind::NominalClassType),
  605. declaration_(declaration),
  606. base_(base),
  607. vtable_(std::move(class_vtable)),
  608. hierarchy_level_(base ? (*base)->hierarchy_level() + 1 : 0) {
  609. CARBON_CHECK(!declaration->type_params().has_value())
  610. << "missing arguments for parameterized class type";
  611. }
  612. // Construct a fully instantiated generic class type to represent the
  613. // run-time type of an object.
  614. explicit NominalClassType(
  615. Nonnull<const ClassDeclaration*> declaration,
  616. Nonnull<const Bindings*> bindings,
  617. std::optional<Nonnull<const NominalClassType*>> base, VTable class_vtable)
  618. : Value(Kind::NominalClassType),
  619. declaration_(declaration),
  620. bindings_(bindings),
  621. base_(base),
  622. vtable_(std::move(class_vtable)),
  623. hierarchy_level_(base ? (*base)->hierarchy_level() + 1 : 0) {}
  624. static auto classof(const Value* value) -> bool {
  625. return value->kind() == Kind::NominalClassType;
  626. }
  627. template <typename F>
  628. auto Decompose(F f) const {
  629. return f(declaration_, bindings_, base_, vtable_);
  630. }
  631. auto declaration() const -> const ClassDeclaration& { return *declaration_; }
  632. auto bindings() const -> const Bindings& { return *bindings_; }
  633. auto base() const -> std::optional<Nonnull<const NominalClassType*>> {
  634. return base_;
  635. }
  636. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  637. // Witnesses for each of the class's impl bindings.
  638. auto witnesses() const -> const ImplWitnessMap& {
  639. return bindings_->witnesses();
  640. }
  641. auto vtable() const -> const VTable& { return vtable_; }
  642. // Returns how many levels from the top ancestor class it is. i.e. a class
  643. // with no base returns `0`, while a class with a `.base` and `.base.base`
  644. // returns `2`.
  645. auto hierarchy_level() const -> int { return hierarchy_level_; }
  646. // Returns whether this a parameterized class. That is, a class with
  647. // parameters and no corresponding arguments.
  648. auto IsParameterized() const -> bool {
  649. return declaration_->type_params().has_value() && type_args().empty();
  650. }
  651. // Returns whether this class is, or inherits `other`.
  652. auto InheritsClass(Nonnull<const Value*> other) const -> bool;
  653. private:
  654. Nonnull<const ClassDeclaration*> declaration_;
  655. Nonnull<const Bindings*> bindings_ = Bindings::None();
  656. const std::optional<Nonnull<const NominalClassType*>> base_;
  657. const VTable vtable_;
  658. int hierarchy_level_;
  659. };
  660. class MixinPseudoType : public Value {
  661. public:
  662. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration)
  663. : Value(Kind::MixinPseudoType), declaration_(declaration) {
  664. CARBON_CHECK(!declaration->params().has_value())
  665. << "missing arguments for parameterized mixin type";
  666. }
  667. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration,
  668. Nonnull<const Bindings*> bindings)
  669. : Value(Kind::MixinPseudoType),
  670. declaration_(declaration),
  671. bindings_(bindings) {}
  672. static auto classof(const Value* value) -> bool {
  673. return value->kind() == Kind::MixinPseudoType;
  674. }
  675. template <typename F>
  676. auto Decompose(F f) const {
  677. return f(declaration_, bindings_);
  678. }
  679. auto declaration() const -> const MixinDeclaration& { return *declaration_; }
  680. auto bindings() const -> const Bindings& { return *bindings_; }
  681. auto args() const -> const BindingMap& { return bindings_->args(); }
  682. auto witnesses() const -> const ImplWitnessMap& {
  683. return bindings_->witnesses();
  684. }
  685. auto FindFunction(const std::string_view& name) const
  686. -> std::optional<Nonnull<const FunctionValue*>>;
  687. private:
  688. Nonnull<const MixinDeclaration*> declaration_;
  689. Nonnull<const Bindings*> bindings_ = Bindings::None();
  690. };
  691. // Returns the value of the function named `name` in this class, or
  692. // nullopt if there is no such function.
  693. auto FindFunction(std::string_view name,
  694. llvm::ArrayRef<Nonnull<Declaration*>> members)
  695. -> std::optional<Nonnull<const FunctionValue*>>;
  696. // Returns the value of the function named `name` in this class and its
  697. // parents, or nullopt if there is no such function.
  698. auto FindFunctionWithParents(std::string_view name,
  699. const ClassDeclaration& class_decl)
  700. -> std::optional<Nonnull<const FunctionValue*>>;
  701. // Return the declaration of the member with the given name.
  702. auto FindMember(std::string_view name,
  703. llvm::ArrayRef<Nonnull<Declaration*>> members)
  704. -> std::optional<Nonnull<const Declaration*>>;
  705. // An interface type.
  706. class InterfaceType : public Value {
  707. public:
  708. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
  709. : Value(Kind::InterfaceType), declaration_(declaration) {
  710. CARBON_CHECK(!declaration->params().has_value())
  711. << "missing arguments for parameterized interface type";
  712. }
  713. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
  714. Nonnull<const Bindings*> bindings)
  715. : Value(Kind::InterfaceType),
  716. declaration_(declaration),
  717. bindings_(bindings) {}
  718. static auto classof(const Value* value) -> bool {
  719. return value->kind() == Kind::InterfaceType;
  720. }
  721. template <typename F>
  722. auto Decompose(F f) const {
  723. return f(declaration_, bindings_);
  724. }
  725. auto declaration() const -> const InterfaceDeclaration& {
  726. return *declaration_;
  727. }
  728. auto bindings() const -> const Bindings& { return *bindings_; }
  729. auto args() const -> const BindingMap& { return bindings_->args(); }
  730. auto witnesses() const -> const ImplWitnessMap& {
  731. return bindings_->witnesses();
  732. }
  733. private:
  734. Nonnull<const InterfaceDeclaration*> declaration_;
  735. Nonnull<const Bindings*> bindings_ = Bindings::None();
  736. };
  737. // A named constraint type.
  738. class NamedConstraintType : public Value {
  739. public:
  740. explicit NamedConstraintType(
  741. Nonnull<const ConstraintDeclaration*> declaration,
  742. Nonnull<const Bindings*> bindings)
  743. : Value(Kind::NamedConstraintType),
  744. declaration_(declaration),
  745. bindings_(bindings) {}
  746. static auto classof(const Value* value) -> bool {
  747. return value->kind() == Kind::NamedConstraintType;
  748. }
  749. template <typename F>
  750. auto Decompose(F f) const {
  751. return f(declaration_, bindings_);
  752. }
  753. auto declaration() const -> const ConstraintDeclaration& {
  754. return *declaration_;
  755. }
  756. auto bindings() const -> const Bindings& { return *bindings_; }
  757. private:
  758. Nonnull<const ConstraintDeclaration*> declaration_;
  759. Nonnull<const Bindings*> bindings_ = Bindings::None();
  760. };
  761. // A constraint that requires implementation of an interface.
  762. struct ImplConstraint {
  763. // The type that is required to implement the interface.
  764. Nonnull<const Value*> type;
  765. // The interface that is required to be implemented.
  766. Nonnull<const InterfaceType*> interface;
  767. };
  768. // A constraint that a collection of values are known to be the same.
  769. struct EqualityConstraint {
  770. // Visit the values in this equality constraint that are a single step away
  771. // from the given value according to this equality constraint. That is: if
  772. // `value` is identical to a value in `values`, then call the visitor on all
  773. // values in `values` that are not identical to `value`. Otherwise, do not
  774. // call the visitor.
  775. //
  776. // Stops and returns `false` if any call to the visitor returns `false`,
  777. // otherwise returns `true`.
  778. auto VisitEqualValues(
  779. Nonnull<const Value*> value,
  780. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  781. std::vector<Nonnull<const Value*>> values;
  782. };
  783. // A constraint indicating that access to an associated constant should be
  784. // replaced by another value.
  785. struct RewriteConstraint {
  786. // The associated constant value that is rewritten.
  787. Nonnull<const AssociatedConstant*> constant;
  788. // The replacement in its original type.
  789. Nonnull<const Value*> unconverted_replacement;
  790. // The type of the replacement.
  791. Nonnull<const Value*> unconverted_replacement_type;
  792. // The replacement after conversion to the type of the associated constant.
  793. Nonnull<const Value*> converted_replacement;
  794. };
  795. // A context in which we might look up a name.
  796. struct LookupContext {
  797. Nonnull<const Value*> context;
  798. };
  799. // A type-of-type for an unknown constrained type.
  800. //
  801. // These types are formed by the `&` operator that combines constraints and by
  802. // `where` expressions.
  803. //
  804. // A constraint has three main properties:
  805. //
  806. // * A collection of (type, interface) pairs for interfaces that are known to
  807. // be implemented by a type satisfying the constraint.
  808. // * A collection of sets of values, typically associated constants, that are
  809. // known to be the same.
  810. // * A collection of contexts in which member name lookups will be performed
  811. // for a type variable whose type is this constraint.
  812. //
  813. // Within these properties, the constrained type can be referred to with a
  814. // `VariableType` naming the `self_binding`.
  815. class ConstraintType : public Value {
  816. public:
  817. explicit ConstraintType(Nonnull<const GenericBinding*> self_binding,
  818. std::vector<ImplConstraint> impl_constraints,
  819. std::vector<EqualityConstraint> equality_constraints,
  820. std::vector<RewriteConstraint> rewrite_constraints,
  821. std::vector<LookupContext> lookup_contexts)
  822. : Value(Kind::ConstraintType),
  823. self_binding_(self_binding),
  824. impl_constraints_(std::move(impl_constraints)),
  825. equality_constraints_(std::move(equality_constraints)),
  826. rewrite_constraints_(std::move(rewrite_constraints)),
  827. lookup_contexts_(std::move(lookup_contexts)) {}
  828. static auto classof(const Value* value) -> bool {
  829. return value->kind() == Kind::ConstraintType;
  830. }
  831. template <typename F>
  832. auto Decompose(F f) const {
  833. return f(self_binding_, impl_constraints_, equality_constraints_,
  834. rewrite_constraints_, lookup_contexts_);
  835. }
  836. auto self_binding() const -> Nonnull<const GenericBinding*> {
  837. return self_binding_;
  838. }
  839. auto impl_constraints() const -> llvm::ArrayRef<ImplConstraint> {
  840. return impl_constraints_;
  841. }
  842. auto equality_constraints() const -> llvm::ArrayRef<EqualityConstraint> {
  843. return equality_constraints_;
  844. }
  845. auto rewrite_constraints() const -> llvm::ArrayRef<RewriteConstraint> {
  846. return rewrite_constraints_;
  847. }
  848. auto lookup_contexts() const -> llvm::ArrayRef<LookupContext> {
  849. return lookup_contexts_;
  850. }
  851. // Visit the values in that are a single step away from the given value
  852. // according to equality constraints in this constraint type, that is, the
  853. // values `v` that are not identical to `value` but for which we have a
  854. // `value == v` equality constraint in this constraint type.
  855. //
  856. // Stops and returns `false` if any call to the visitor returns `false`,
  857. // otherwise returns `true`.
  858. auto VisitEqualValues(
  859. Nonnull<const Value*> value,
  860. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  861. private:
  862. Nonnull<const GenericBinding*> self_binding_;
  863. std::vector<ImplConstraint> impl_constraints_;
  864. std::vector<EqualityConstraint> equality_constraints_;
  865. std::vector<RewriteConstraint> rewrite_constraints_;
  866. std::vector<LookupContext> lookup_contexts_;
  867. };
  868. // A witness table.
  869. class Witness : public Value {
  870. protected:
  871. explicit Witness(Value::Kind kind) : Value(kind) {}
  872. public:
  873. static auto classof(const Value* value) -> bool {
  874. return value->kind() == Kind::ImplWitness ||
  875. value->kind() == Kind::BindingWitness ||
  876. value->kind() == Kind::ConstraintWitness ||
  877. value->kind() == Kind::ConstraintImplWitness;
  878. }
  879. };
  880. // The witness table for an impl.
  881. class ImplWitness : public Witness {
  882. public:
  883. // Construct a witness for an impl.
  884. explicit ImplWitness(Nonnull<const ImplDeclaration*> declaration,
  885. Nonnull<const Bindings*> bindings)
  886. : Witness(Kind::ImplWitness),
  887. declaration_(declaration),
  888. bindings_(bindings) {}
  889. static auto classof(const Value* value) -> bool {
  890. return value->kind() == Kind::ImplWitness;
  891. }
  892. template <typename F>
  893. auto Decompose(F f) const {
  894. return f(declaration_, bindings_);
  895. }
  896. auto declaration() const -> const ImplDeclaration& { return *declaration_; }
  897. auto bindings() const -> const Bindings& { return *bindings_; }
  898. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  899. auto witnesses() const -> const ImplWitnessMap& {
  900. return bindings_->witnesses();
  901. }
  902. private:
  903. Nonnull<const ImplDeclaration*> declaration_;
  904. Nonnull<const Bindings*> bindings_ = Bindings::None();
  905. };
  906. // The symbolic witness corresponding to an unresolved impl binding.
  907. class BindingWitness : public Witness {
  908. public:
  909. // Construct a witness for an impl binding.
  910. explicit BindingWitness(Nonnull<const ImplBinding*> binding)
  911. : Witness(Kind::BindingWitness), binding_(binding) {}
  912. static auto classof(const Value* value) -> bool {
  913. return value->kind() == Kind::BindingWitness;
  914. }
  915. template <typename F>
  916. auto Decompose(F f) const {
  917. return f(binding_);
  918. }
  919. auto binding() const -> Nonnull<const ImplBinding*> { return binding_; }
  920. private:
  921. Nonnull<const ImplBinding*> binding_;
  922. };
  923. // A witness for a constraint type, expressed as a tuple of witnesses for the
  924. // individual impl constraints in the constraint type.
  925. class ConstraintWitness : public Witness {
  926. public:
  927. explicit ConstraintWitness(std::vector<Nonnull<const Witness*>> witnesses)
  928. : Witness(Kind::ConstraintWitness), witnesses_(std::move(witnesses)) {}
  929. static auto classof(const Value* value) -> bool {
  930. return value->kind() == Kind::ConstraintWitness;
  931. }
  932. template <typename F>
  933. auto Decompose(F f) const {
  934. return f(witnesses_);
  935. }
  936. auto witnesses() const -> llvm::ArrayRef<Nonnull<const Witness*>> {
  937. return witnesses_;
  938. }
  939. private:
  940. std::vector<Nonnull<const Witness*>> witnesses_;
  941. };
  942. // A witness for an impl constraint in a constraint type, expressed in terms of
  943. // a symbolic witness for the constraint type.
  944. class ConstraintImplWitness : public Witness {
  945. public:
  946. // Make a witness for the given impl_constraint of the given `ConstraintType`
  947. // witness. If we're indexing into a known tuple of witnesses, pull out the
  948. // element.
  949. static auto Make(Nonnull<Arena*> arena, Nonnull<const Witness*> witness,
  950. int index) -> Nonnull<const Witness*> {
  951. CARBON_CHECK(!llvm::isa<ImplWitness>(witness))
  952. << "impl witness has no components to access";
  953. if (const auto* constraint_witness =
  954. llvm::dyn_cast<ConstraintWitness>(witness)) {
  955. return constraint_witness->witnesses()[index];
  956. }
  957. return arena->New<ConstraintImplWitness>(witness, index);
  958. }
  959. explicit ConstraintImplWitness(Nonnull<const Witness*> constraint_witness,
  960. int index)
  961. : Witness(Kind::ConstraintImplWitness),
  962. constraint_witness_(constraint_witness),
  963. index_(index) {
  964. CARBON_CHECK(!llvm::isa<ConstraintWitness>(constraint_witness))
  965. << "should have resolved element from constraint witness";
  966. }
  967. static auto classof(const Value* value) -> bool {
  968. return value->kind() == Kind::ConstraintImplWitness;
  969. }
  970. template <typename F>
  971. auto Decompose(F f) const {
  972. return f(constraint_witness_, index_);
  973. }
  974. // Get the witness for the complete `ConstraintType`.
  975. auto constraint_witness() const -> Nonnull<const Witness*> {
  976. return constraint_witness_;
  977. }
  978. // Get the index of the impl constraint within the constraint type.
  979. auto index() const -> int { return index_; }
  980. private:
  981. Nonnull<const Witness*> constraint_witness_;
  982. int index_;
  983. };
  984. // Allocate a `ConstraintImplWitness` using the custom `Make` function.
  985. template <>
  986. struct AllocateTrait<ConstraintImplWitness> {
  987. template <typename... Args>
  988. static auto New(Nonnull<Arena*> arena, Args&&... args)
  989. -> Nonnull<const Witness*> {
  990. return ConstraintImplWitness::Make(arena, std::forward<Args>(args)...);
  991. }
  992. };
  993. // A choice type.
  994. class ChoiceType : public Value {
  995. public:
  996. ChoiceType(Nonnull<const ChoiceDeclaration*> declaration,
  997. Nonnull<const Bindings*> bindings)
  998. : Value(Kind::ChoiceType),
  999. declaration_(declaration),
  1000. bindings_(bindings) {}
  1001. static auto classof(const Value* value) -> bool {
  1002. return value->kind() == Kind::ChoiceType;
  1003. }
  1004. template <typename F>
  1005. auto Decompose(F f) const {
  1006. return f(declaration_, bindings_);
  1007. }
  1008. auto name() const -> const std::string& { return declaration_->name(); }
  1009. // Returns the parameter types of the alternative with the given name,
  1010. // or nullopt if no such alternative is present.
  1011. auto FindAlternative(std::string_view name) const
  1012. -> std::optional<Nonnull<const Value*>>;
  1013. auto bindings() const -> const Bindings& { return *bindings_; }
  1014. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  1015. auto declaration() const -> const ChoiceDeclaration& { return *declaration_; }
  1016. auto IsParameterized() const -> bool {
  1017. return declaration_->type_params().has_value();
  1018. }
  1019. private:
  1020. Nonnull<const ChoiceDeclaration*> declaration_;
  1021. Nonnull<const Bindings*> bindings_;
  1022. };
  1023. // A continuation type.
  1024. class ContinuationType : public Value {
  1025. public:
  1026. ContinuationType() : Value(Kind::ContinuationType) {}
  1027. static auto classof(const Value* value) -> bool {
  1028. return value->kind() == Kind::ContinuationType;
  1029. }
  1030. template <typename F>
  1031. auto Decompose(F f) const {
  1032. return f();
  1033. }
  1034. };
  1035. // A variable type.
  1036. class VariableType : public Value {
  1037. public:
  1038. explicit VariableType(Nonnull<const GenericBinding*> binding)
  1039. : Value(Kind::VariableType), binding_(binding) {}
  1040. static auto classof(const Value* value) -> bool {
  1041. return value->kind() == Kind::VariableType;
  1042. }
  1043. template <typename F>
  1044. auto Decompose(F f) const {
  1045. return f(binding_);
  1046. }
  1047. auto binding() const -> const GenericBinding& { return *binding_; }
  1048. private:
  1049. Nonnull<const GenericBinding*> binding_;
  1050. };
  1051. // A name of an entity that has explicit parameters, such as a parameterized
  1052. // class or interface. When arguments for those parameters are provided in a
  1053. // call, the result will be a class type or interface type.
  1054. class ParameterizedEntityName : public Value {
  1055. public:
  1056. explicit ParameterizedEntityName(Nonnull<const Declaration*> declaration,
  1057. Nonnull<const TuplePattern*> params)
  1058. : Value(Kind::ParameterizedEntityName),
  1059. declaration_(declaration),
  1060. params_(params) {}
  1061. static auto classof(const Value* value) -> bool {
  1062. return value->kind() == Kind::ParameterizedEntityName;
  1063. }
  1064. template <typename F>
  1065. auto Decompose(F f) const {
  1066. return f(declaration_, params_);
  1067. }
  1068. auto declaration() const -> const Declaration& { return *declaration_; }
  1069. auto params() const -> const TuplePattern& { return *params_; }
  1070. private:
  1071. Nonnull<const Declaration*> declaration_;
  1072. Nonnull<const TuplePattern*> params_;
  1073. };
  1074. // The name of a member of a class or interface.
  1075. //
  1076. // These values are used to represent the second operand of a compound member
  1077. // access expression: `x.(A.B)`, and can also be the value of an alias
  1078. // declaration, but cannot be used in most other contexts.
  1079. class MemberName : public Value {
  1080. public:
  1081. MemberName(std::optional<Nonnull<const Value*>> base_type,
  1082. std::optional<Nonnull<const InterfaceType*>> interface,
  1083. NamedElement member)
  1084. : Value(Kind::MemberName),
  1085. base_type_(base_type),
  1086. interface_(interface),
  1087. member_(std::move(member)) {
  1088. CARBON_CHECK(base_type || interface)
  1089. << "member name must be in a type, an interface, or both";
  1090. }
  1091. static auto classof(const Value* value) -> bool {
  1092. return value->kind() == Kind::MemberName;
  1093. }
  1094. template <typename F>
  1095. auto Decompose(F f) const {
  1096. return f(base_type_, interface_, member_);
  1097. }
  1098. // Prints the member name or identifier.
  1099. void Print(llvm::raw_ostream& out) const { member_.Print(out); }
  1100. // The type for which `name` is a member or a member of an `impl`.
  1101. auto base_type() const -> std::optional<Nonnull<const Value*>> {
  1102. return base_type_;
  1103. }
  1104. // The interface for which `name` is a member, if any.
  1105. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  1106. return interface_;
  1107. }
  1108. // The member.
  1109. auto member() const -> const NamedElement& { return member_; }
  1110. // The name of the member.
  1111. auto name() const -> std::string_view { return member().name(); }
  1112. private:
  1113. std::optional<Nonnull<const Value*>> base_type_;
  1114. std::optional<Nonnull<const InterfaceType*>> interface_;
  1115. NamedElement member_;
  1116. };
  1117. // A symbolic value representing an associated constant.
  1118. //
  1119. // This is a value of the form `A.B` or `A.B.C` or similar, where `A` is a
  1120. // `VariableType`.
  1121. class AssociatedConstant : public Value {
  1122. public:
  1123. explicit AssociatedConstant(
  1124. Nonnull<const Value*> base, Nonnull<const InterfaceType*> interface,
  1125. Nonnull<const AssociatedConstantDeclaration*> constant,
  1126. Nonnull<const Witness*> witness)
  1127. : Value(Kind::AssociatedConstant),
  1128. base_(base),
  1129. interface_(interface),
  1130. constant_(constant),
  1131. witness_(witness) {}
  1132. static auto classof(const Value* value) -> bool {
  1133. return value->kind() == Kind::AssociatedConstant;
  1134. }
  1135. template <typename F>
  1136. auto Decompose(F f) const {
  1137. return f(base_, interface_, constant_, witness_);
  1138. }
  1139. // The type for which we denote an associated constant.
  1140. auto base() const -> const Value& { return *base_; }
  1141. // The interface within which the constant was declared.
  1142. auto interface() const -> const InterfaceType& { return *interface_; }
  1143. // The associated constant whose value is being denoted.
  1144. auto constant() const -> const AssociatedConstantDeclaration& {
  1145. return *constant_;
  1146. }
  1147. // Witness within which the constant's value can be found.
  1148. auto witness() const -> const Witness& { return *witness_; }
  1149. private:
  1150. Nonnull<const Value*> base_;
  1151. Nonnull<const InterfaceType*> interface_;
  1152. Nonnull<const AssociatedConstantDeclaration*> constant_;
  1153. Nonnull<const Witness*> witness_;
  1154. };
  1155. // A first-class continuation representation of a fragment of the stack.
  1156. // A continuation value behaves like a pointer to the underlying stack
  1157. // fragment, which is exposed by `Stack()`.
  1158. class ContinuationValue : public Value {
  1159. public:
  1160. class StackFragment {
  1161. public:
  1162. // Constructs an empty StackFragment.
  1163. StackFragment() = default;
  1164. // Requires *this to be empty, because by the time we're tearing down the
  1165. // Arena, it's no longer safe to invoke ~Action.
  1166. ~StackFragment();
  1167. StackFragment(StackFragment&&) = delete;
  1168. auto operator=(StackFragment&&) -> StackFragment& = delete;
  1169. // Store the given partial todo stack in *this, which must currently be
  1170. // empty. The stack is represented with the top of the stack at the
  1171. // beginning of the vector, the reverse of the usual order.
  1172. void StoreReversed(std::vector<std::unique_ptr<Action>> reversed_todo);
  1173. // Restore the currently stored stack fragment to the top of `todo`,
  1174. // leaving *this empty.
  1175. void RestoreTo(Stack<std::unique_ptr<Action>>& todo);
  1176. // Destroy the currently stored stack fragment.
  1177. void Clear();
  1178. void Print(llvm::raw_ostream& out) const;
  1179. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  1180. private:
  1181. // The todo stack of a suspended continuation, starting with the top
  1182. // Action.
  1183. std::vector<std::unique_ptr<Action>> reversed_todo_;
  1184. };
  1185. explicit ContinuationValue(Nonnull<StackFragment*> stack)
  1186. : Value(Kind::ContinuationValue), stack_(stack) {}
  1187. static auto classof(const Value* value) -> bool {
  1188. return value->kind() == Kind::ContinuationValue;
  1189. }
  1190. template <typename F>
  1191. auto Decompose(F f) const {
  1192. return f(stack_);
  1193. }
  1194. // The todo stack of the suspended continuation. Note that this provides
  1195. // mutable access, even when *this is const, because of the reference-like
  1196. // semantics of ContinuationValue.
  1197. auto stack() const -> StackFragment& { return *stack_; }
  1198. private:
  1199. Nonnull<StackFragment*> stack_;
  1200. };
  1201. // The String type.
  1202. class StringType : public Value {
  1203. public:
  1204. StringType() : Value(Kind::StringType) {}
  1205. static auto classof(const Value* value) -> bool {
  1206. return value->kind() == Kind::StringType;
  1207. }
  1208. template <typename F>
  1209. auto Decompose(F f) const {
  1210. return f();
  1211. }
  1212. };
  1213. // A string value.
  1214. class StringValue : public Value {
  1215. public:
  1216. explicit StringValue(std::string value)
  1217. : Value(Kind::StringValue), value_(std::move(value)) {}
  1218. static auto classof(const Value* value) -> bool {
  1219. return value->kind() == Kind::StringValue;
  1220. }
  1221. template <typename F>
  1222. auto Decompose(F f) const {
  1223. return f(value_);
  1224. }
  1225. auto value() const -> const std::string& { return value_; }
  1226. private:
  1227. std::string value_;
  1228. };
  1229. class TypeOfMixinPseudoType : public Value {
  1230. public:
  1231. explicit TypeOfMixinPseudoType(Nonnull<const MixinPseudoType*> class_type)
  1232. : Value(Kind::TypeOfMixinPseudoType), mixin_type_(class_type) {}
  1233. static auto classof(const Value* value) -> bool {
  1234. return value->kind() == Kind::TypeOfMixinPseudoType;
  1235. }
  1236. template <typename F>
  1237. auto Decompose(F f) const {
  1238. return f(mixin_type_);
  1239. }
  1240. auto mixin_type() const -> const MixinPseudoType& { return *mixin_type_; }
  1241. private:
  1242. Nonnull<const MixinPseudoType*> mixin_type_;
  1243. };
  1244. // The type of an expression whose value is the name of a parameterized entity.
  1245. // Such an expression can only be used as the operand of a call expression that
  1246. // provides arguments for the parameters.
  1247. class TypeOfParameterizedEntityName : public Value {
  1248. public:
  1249. explicit TypeOfParameterizedEntityName(
  1250. Nonnull<const ParameterizedEntityName*> name)
  1251. : Value(Kind::TypeOfParameterizedEntityName), name_(name) {}
  1252. static auto classof(const Value* value) -> bool {
  1253. return value->kind() == Kind::TypeOfParameterizedEntityName;
  1254. }
  1255. template <typename F>
  1256. auto Decompose(F f) const {
  1257. return f(name_);
  1258. }
  1259. auto name() const -> const ParameterizedEntityName& { return *name_; }
  1260. private:
  1261. Nonnull<const ParameterizedEntityName*> name_;
  1262. };
  1263. // The type of a member name expression.
  1264. //
  1265. // This is used for member names that don't denote a specific object or value
  1266. // until used on the right-hand side of a `.`, such as an instance method or
  1267. // field name, or any member function in an interface.
  1268. //
  1269. // Such expressions can appear only as the target of an `alias` declaration or
  1270. // as the member name in a compound member access.
  1271. class TypeOfMemberName : public Value {
  1272. public:
  1273. explicit TypeOfMemberName(NamedElement member)
  1274. : Value(Kind::TypeOfMemberName), member_(std::move(member)) {}
  1275. static auto classof(const Value* value) -> bool {
  1276. return value->kind() == Kind::TypeOfMemberName;
  1277. }
  1278. template <typename F>
  1279. auto Decompose(F f) const {
  1280. return f(member_);
  1281. }
  1282. // TODO: consider removing this or moving it elsewhere in the AST,
  1283. // since it's arguably part of the expression value rather than its type.
  1284. auto member() const -> NamedElement { return member_; }
  1285. private:
  1286. NamedElement member_;
  1287. };
  1288. // The type of a statically-sized array.
  1289. //
  1290. // Note that values of this type are represented as tuples.
  1291. class StaticArrayType : public Value {
  1292. public:
  1293. // Constructs a statically-sized array type with the given element type and
  1294. // size.
  1295. StaticArrayType(Nonnull<const Value*> element_type, size_t size)
  1296. : Value(Kind::StaticArrayType),
  1297. element_type_(element_type),
  1298. size_(size) {}
  1299. static auto classof(const Value* value) -> bool {
  1300. return value->kind() == Kind::StaticArrayType;
  1301. }
  1302. template <typename F>
  1303. auto Decompose(F f) const {
  1304. return f(element_type_, size_);
  1305. }
  1306. auto element_type() const -> const Value& { return *element_type_; }
  1307. auto size() const -> size_t { return size_; }
  1308. private:
  1309. Nonnull<const Value*> element_type_;
  1310. size_t size_;
  1311. };
  1312. template <typename R, typename F>
  1313. auto Value::Visit(F f) const -> R {
  1314. switch (kind()) {
  1315. #define CARBON_VALUE_KIND(kind) \
  1316. case Kind::kind: \
  1317. return f(static_cast<const kind*>(this));
  1318. #include "explorer/interpreter/value_kinds.def"
  1319. }
  1320. }
  1321. } // namespace Carbon
  1322. #endif // CARBON_EXPLORER_INTERPRETER_VALUE_H_