value.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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. 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 name() const -> const std::string& { return declaration_->name(); }
  1022. // Returns the parameter types of the alternative with the given name,
  1023. // or nullopt if no such alternative is present.
  1024. auto FindAlternative(std::string_view name) const
  1025. -> std::optional<Nonnull<const Value*>>;
  1026. auto bindings() const -> const Bindings& { return *bindings_; }
  1027. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  1028. auto declaration() const -> const ChoiceDeclaration& { return *declaration_; }
  1029. auto IsParameterized() const -> bool {
  1030. return declaration_->type_params().has_value();
  1031. }
  1032. private:
  1033. Nonnull<const ChoiceDeclaration*> declaration_;
  1034. Nonnull<const Bindings*> bindings_;
  1035. };
  1036. // A continuation type.
  1037. class ContinuationType : public Value {
  1038. public:
  1039. ContinuationType() : Value(Kind::ContinuationType) {}
  1040. static auto classof(const Value* value) -> bool {
  1041. return value->kind() == Kind::ContinuationType;
  1042. }
  1043. template <typename F>
  1044. auto Decompose(F f) const {
  1045. return f();
  1046. }
  1047. };
  1048. // A variable type.
  1049. class VariableType : public Value {
  1050. public:
  1051. explicit VariableType(Nonnull<const GenericBinding*> binding)
  1052. : Value(Kind::VariableType), binding_(binding) {}
  1053. static auto classof(const Value* value) -> bool {
  1054. return value->kind() == Kind::VariableType;
  1055. }
  1056. template <typename F>
  1057. auto Decompose(F f) const {
  1058. return f(binding_);
  1059. }
  1060. auto binding() const -> const GenericBinding& { return *binding_; }
  1061. private:
  1062. Nonnull<const GenericBinding*> binding_;
  1063. };
  1064. // A name of an entity that has explicit parameters, such as a parameterized
  1065. // class or interface. When arguments for those parameters are provided in a
  1066. // call, the result will be a class type or interface type.
  1067. class ParameterizedEntityName : public Value {
  1068. public:
  1069. explicit ParameterizedEntityName(Nonnull<const Declaration*> declaration,
  1070. Nonnull<const TuplePattern*> params)
  1071. : Value(Kind::ParameterizedEntityName),
  1072. declaration_(declaration),
  1073. params_(params) {}
  1074. static auto classof(const Value* value) -> bool {
  1075. return value->kind() == Kind::ParameterizedEntityName;
  1076. }
  1077. template <typename F>
  1078. auto Decompose(F f) const {
  1079. return f(declaration_, params_);
  1080. }
  1081. auto declaration() const -> const Declaration& { return *declaration_; }
  1082. auto params() const -> const TuplePattern& { return *params_; }
  1083. private:
  1084. Nonnull<const Declaration*> declaration_;
  1085. Nonnull<const TuplePattern*> params_;
  1086. };
  1087. // The name of a member of a class or interface.
  1088. //
  1089. // These values are used to represent the second operand of a compound member
  1090. // access expression: `x.(A.B)`, and can also be the value of an alias
  1091. // declaration, but cannot be used in most other contexts.
  1092. class MemberName : public Value {
  1093. public:
  1094. MemberName(std::optional<Nonnull<const Value*>> base_type,
  1095. std::optional<Nonnull<const InterfaceType*>> interface,
  1096. NamedElement member)
  1097. : Value(Kind::MemberName),
  1098. base_type_(base_type),
  1099. interface_(interface),
  1100. member_(std::move(member)) {
  1101. CARBON_CHECK(base_type || interface)
  1102. << "member name must be in a type, an interface, or both";
  1103. }
  1104. static auto classof(const Value* value) -> bool {
  1105. return value->kind() == Kind::MemberName;
  1106. }
  1107. template <typename F>
  1108. auto Decompose(F f) const {
  1109. return f(base_type_, interface_, member_);
  1110. }
  1111. // Prints the member name or identifier.
  1112. void Print(llvm::raw_ostream& out) const { member_.Print(out); }
  1113. // The type for which `name` is a member or a member of an `impl`.
  1114. auto base_type() const -> std::optional<Nonnull<const Value*>> {
  1115. return base_type_;
  1116. }
  1117. // The interface for which `name` is a member, if any.
  1118. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  1119. return interface_;
  1120. }
  1121. // The member.
  1122. auto member() const -> const NamedElement& { return member_; }
  1123. // The name of the member.
  1124. auto name() const -> std::string_view { return member().name(); }
  1125. private:
  1126. std::optional<Nonnull<const Value*>> base_type_;
  1127. std::optional<Nonnull<const InterfaceType*>> interface_;
  1128. NamedElement member_;
  1129. };
  1130. // A symbolic value representing an associated constant.
  1131. //
  1132. // This is a value of the form `A.B` or `A.B.C` or similar, where `A` is a
  1133. // `VariableType`.
  1134. class AssociatedConstant : public Value {
  1135. public:
  1136. explicit AssociatedConstant(
  1137. Nonnull<const Value*> base, Nonnull<const InterfaceType*> interface,
  1138. Nonnull<const AssociatedConstantDeclaration*> constant,
  1139. Nonnull<const Witness*> witness)
  1140. : Value(Kind::AssociatedConstant),
  1141. base_(base),
  1142. interface_(interface),
  1143. constant_(constant),
  1144. witness_(witness) {}
  1145. static auto classof(const Value* value) -> bool {
  1146. return value->kind() == Kind::AssociatedConstant;
  1147. }
  1148. template <typename F>
  1149. auto Decompose(F f) const {
  1150. return f(base_, interface_, constant_, witness_);
  1151. }
  1152. // The type for which we denote an associated constant.
  1153. auto base() const -> const Value& { return *base_; }
  1154. // The interface within which the constant was declared.
  1155. auto interface() const -> const InterfaceType& { return *interface_; }
  1156. // The associated constant whose value is being denoted.
  1157. auto constant() const -> const AssociatedConstantDeclaration& {
  1158. return *constant_;
  1159. }
  1160. // Witness within which the constant's value can be found.
  1161. auto witness() const -> const Witness& { return *witness_; }
  1162. private:
  1163. Nonnull<const Value*> base_;
  1164. Nonnull<const InterfaceType*> interface_;
  1165. Nonnull<const AssociatedConstantDeclaration*> constant_;
  1166. Nonnull<const Witness*> witness_;
  1167. };
  1168. // A first-class continuation representation of a fragment of the stack.
  1169. // A continuation value behaves like a pointer to the underlying stack
  1170. // fragment, which is exposed by `Stack()`.
  1171. class ContinuationValue : public Value {
  1172. public:
  1173. class StackFragment {
  1174. public:
  1175. // Constructs an empty StackFragment.
  1176. StackFragment() = default;
  1177. // Requires *this to be empty, because by the time we're tearing down the
  1178. // Arena, it's no longer safe to invoke ~Action.
  1179. ~StackFragment();
  1180. StackFragment(StackFragment&&) = delete;
  1181. auto operator=(StackFragment&&) -> StackFragment& = delete;
  1182. // Store the given partial todo stack in *this, which must currently be
  1183. // empty. The stack is represented with the top of the stack at the
  1184. // beginning of the vector, the reverse of the usual order.
  1185. void StoreReversed(std::vector<std::unique_ptr<Action>> reversed_todo);
  1186. // Restore the currently stored stack fragment to the top of `todo`,
  1187. // leaving *this empty.
  1188. void RestoreTo(Stack<std::unique_ptr<Action>>& todo);
  1189. // Destroy the currently stored stack fragment.
  1190. void Clear();
  1191. void Print(llvm::raw_ostream& out) const;
  1192. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  1193. private:
  1194. // The todo stack of a suspended continuation, starting with the top
  1195. // Action.
  1196. std::vector<std::unique_ptr<Action>> reversed_todo_;
  1197. };
  1198. explicit ContinuationValue(Nonnull<StackFragment*> stack)
  1199. : Value(Kind::ContinuationValue), stack_(stack) {}
  1200. static auto classof(const Value* value) -> bool {
  1201. return value->kind() == Kind::ContinuationValue;
  1202. }
  1203. template <typename F>
  1204. auto Decompose(F f) const {
  1205. return f(stack_);
  1206. }
  1207. // The todo stack of the suspended continuation. Note that this provides
  1208. // mutable access, even when *this is const, because of the reference-like
  1209. // semantics of ContinuationValue.
  1210. auto stack() const -> StackFragment& { return *stack_; }
  1211. private:
  1212. Nonnull<StackFragment*> stack_;
  1213. };
  1214. // The String type.
  1215. class StringType : public Value {
  1216. public:
  1217. StringType() : Value(Kind::StringType) {}
  1218. static auto classof(const Value* value) -> bool {
  1219. return value->kind() == Kind::StringType;
  1220. }
  1221. template <typename F>
  1222. auto Decompose(F f) const {
  1223. return f();
  1224. }
  1225. };
  1226. // A string value.
  1227. class StringValue : public Value {
  1228. public:
  1229. explicit StringValue(std::string value)
  1230. : Value(Kind::StringValue), value_(std::move(value)) {}
  1231. static auto classof(const Value* value) -> bool {
  1232. return value->kind() == Kind::StringValue;
  1233. }
  1234. template <typename F>
  1235. auto Decompose(F f) const {
  1236. return f(value_);
  1237. }
  1238. auto value() const -> const std::string& { return value_; }
  1239. private:
  1240. std::string value_;
  1241. };
  1242. class TypeOfMixinPseudoType : public Value {
  1243. public:
  1244. explicit TypeOfMixinPseudoType(Nonnull<const MixinPseudoType*> class_type)
  1245. : Value(Kind::TypeOfMixinPseudoType), mixin_type_(class_type) {}
  1246. static auto classof(const Value* value) -> bool {
  1247. return value->kind() == Kind::TypeOfMixinPseudoType;
  1248. }
  1249. template <typename F>
  1250. auto Decompose(F f) const {
  1251. return f(mixin_type_);
  1252. }
  1253. auto mixin_type() const -> const MixinPseudoType& { return *mixin_type_; }
  1254. private:
  1255. Nonnull<const MixinPseudoType*> mixin_type_;
  1256. };
  1257. // The type of an expression whose value is the name of a parameterized entity.
  1258. // Such an expression can only be used as the operand of a call expression that
  1259. // provides arguments for the parameters.
  1260. class TypeOfParameterizedEntityName : public Value {
  1261. public:
  1262. explicit TypeOfParameterizedEntityName(
  1263. Nonnull<const ParameterizedEntityName*> name)
  1264. : Value(Kind::TypeOfParameterizedEntityName), name_(name) {}
  1265. static auto classof(const Value* value) -> bool {
  1266. return value->kind() == Kind::TypeOfParameterizedEntityName;
  1267. }
  1268. template <typename F>
  1269. auto Decompose(F f) const {
  1270. return f(name_);
  1271. }
  1272. auto name() const -> const ParameterizedEntityName& { return *name_; }
  1273. private:
  1274. Nonnull<const ParameterizedEntityName*> name_;
  1275. };
  1276. // The type of a member name expression.
  1277. //
  1278. // This is used for member names that don't denote a specific object or value
  1279. // until used on the right-hand side of a `.`, such as an instance method or
  1280. // field name, or any member function in an interface.
  1281. //
  1282. // Such expressions can appear only as the target of an `alias` declaration or
  1283. // as the member name in a compound member access.
  1284. class TypeOfMemberName : public Value {
  1285. public:
  1286. explicit TypeOfMemberName(NamedElement member)
  1287. : Value(Kind::TypeOfMemberName), member_(std::move(member)) {}
  1288. static auto classof(const Value* value) -> bool {
  1289. return value->kind() == Kind::TypeOfMemberName;
  1290. }
  1291. template <typename F>
  1292. auto Decompose(F f) const {
  1293. return f(member_);
  1294. }
  1295. // TODO: consider removing this or moving it elsewhere in the AST,
  1296. // since it's arguably part of the expression value rather than its type.
  1297. auto member() const -> NamedElement { return member_; }
  1298. private:
  1299. NamedElement member_;
  1300. };
  1301. // The type of a namespace name.
  1302. //
  1303. // Such expressions can appear only as the target of an `alias` declaration or
  1304. // as the left-hand side of a simple member access expression.
  1305. class TypeOfNamespaceName : public Value {
  1306. public:
  1307. explicit TypeOfNamespaceName(
  1308. Nonnull<const NamespaceDeclaration*> namespace_decl)
  1309. : Value(Kind::TypeOfNamespaceName), namespace_decl_(namespace_decl) {}
  1310. static auto classof(const Value* value) -> bool {
  1311. return value->kind() == Kind::TypeOfNamespaceName;
  1312. }
  1313. template <typename F>
  1314. auto Decompose(F f) const {
  1315. return f(namespace_decl_);
  1316. }
  1317. auto namespace_decl() const -> Nonnull<const NamespaceDeclaration*> {
  1318. return namespace_decl_;
  1319. }
  1320. private:
  1321. Nonnull<const NamespaceDeclaration*> namespace_decl_;
  1322. };
  1323. // The type of a statically-sized array.
  1324. //
  1325. // Note that values of this type are represented as tuples.
  1326. class StaticArrayType : public Value {
  1327. public:
  1328. // Constructs a statically-sized array type with the given element type and
  1329. // size.
  1330. StaticArrayType(Nonnull<const Value*> element_type, size_t size)
  1331. : Value(Kind::StaticArrayType),
  1332. element_type_(element_type),
  1333. size_(size) {}
  1334. static auto classof(const Value* value) -> bool {
  1335. return value->kind() == Kind::StaticArrayType;
  1336. }
  1337. template <typename F>
  1338. auto Decompose(F f) const {
  1339. return f(element_type_, size_);
  1340. }
  1341. auto element_type() const -> const Value& { return *element_type_; }
  1342. auto size() const -> size_t { return size_; }
  1343. private:
  1344. Nonnull<const Value*> element_type_;
  1345. size_t size_;
  1346. };
  1347. template <typename R, typename F>
  1348. auto Value::Visit(F f) const -> R {
  1349. switch (kind()) {
  1350. #define CARBON_VALUE_KIND(kind) \
  1351. case Kind::kind: \
  1352. return f(static_cast<const kind*>(this));
  1353. #include "explorer/interpreter/value_kinds.def"
  1354. }
  1355. }
  1356. } // namespace Carbon
  1357. #endif // CARBON_EXPLORER_INTERPRETER_VALUE_H_