value.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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/member.h"
  14. #include "explorer/ast/statement.h"
  15. #include "explorer/common/nonnull.h"
  16. #include "explorer/interpreter/address.h"
  17. #include "explorer/interpreter/field_path.h"
  18. #include "explorer/interpreter/stack.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace Carbon {
  21. class Action;
  22. class ImplScope;
  23. // Abstract base class of all AST nodes representing values.
  24. //
  25. // Value and its derived classes support LLVM-style RTTI, including
  26. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  27. // class derived from Value must provide a `classof` operation, and
  28. // every concrete derived class must have a corresponding enumerator
  29. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  30. // details.
  31. class Value {
  32. public:
  33. enum class Kind {
  34. IntValue,
  35. FunctionValue,
  36. DestructorValue,
  37. BoundMethodValue,
  38. PointerValue,
  39. LValue,
  40. BoolValue,
  41. StructValue,
  42. NominalClassValue,
  43. AlternativeValue,
  44. TupleValue,
  45. UninitializedValue,
  46. ImplWitness,
  47. BindingWitness,
  48. ConstraintWitness,
  49. ConstraintImplWitness,
  50. IntType,
  51. BoolType,
  52. TypeType,
  53. FunctionType,
  54. PointerType,
  55. AutoType,
  56. StructType,
  57. NominalClassType,
  58. MixinPseudoType,
  59. InterfaceType,
  60. ConstraintType,
  61. ChoiceType,
  62. ContinuationType, // The type of a continuation.
  63. VariableType, // e.g., generic type parameters.
  64. AssociatedConstant,
  65. ParameterizedEntityName,
  66. MemberName,
  67. BindingPlaceholderValue,
  68. AddrValue,
  69. AlternativeConstructorValue,
  70. ContinuationValue, // A first-class continuation value.
  71. StringType,
  72. StringValue,
  73. TypeOfMixinPseudoType,
  74. TypeOfParameterizedEntityName,
  75. TypeOfMemberName,
  76. StaticArrayType,
  77. };
  78. Value(const Value&) = delete;
  79. auto operator=(const Value&) -> Value& = delete;
  80. void Print(llvm::raw_ostream& out) const;
  81. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  82. // Returns the sub-Value specified by `path`, which must be a valid field
  83. // path for *this. If the sub-Value is a method and its me_pattern is an
  84. // AddrPattern, then pass the LValue representing the receiver as `me_value`,
  85. // otherwise pass `*this`.
  86. auto GetMember(Nonnull<Arena*> arena, const FieldPath& path,
  87. SourceLocation source_loc,
  88. Nonnull<const Value*> me_value) const
  89. -> ErrorOr<Nonnull<const Value*>>;
  90. // Returns a copy of *this, but with the sub-Value specified by `path`
  91. // set to `field_value`. `path` must be a valid field path for *this.
  92. auto SetField(Nonnull<Arena*> arena, const FieldPath& path,
  93. Nonnull<const Value*> field_value,
  94. SourceLocation source_loc) const
  95. -> ErrorOr<Nonnull<const Value*>>;
  96. // Returns the enumerator corresponding to the most-derived type of this
  97. // object.
  98. auto kind() const -> Kind { return kind_; }
  99. protected:
  100. // Constructs a Value. `kind` must be the enumerator corresponding to the
  101. // most-derived type being constructed.
  102. explicit Value(Kind kind) : kind_(kind) {}
  103. private:
  104. const Kind kind_;
  105. };
  106. // Returns whether the fully-resolved kind that this value will eventually have
  107. // is currently unknown, because it depends on a generic parameter.
  108. inline bool IsValueKindDependent(Nonnull<const Value*> type) {
  109. return type->kind() == Value::Kind::VariableType ||
  110. type->kind() == Value::Kind::AssociatedConstant;
  111. }
  112. // Base class for types holding contextual information by which we can
  113. // determine whether values are equal.
  114. class EqualityContext {
  115. public:
  116. virtual auto VisitEqualValues(
  117. Nonnull<const Value*> value,
  118. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const
  119. -> bool = 0;
  120. protected:
  121. virtual ~EqualityContext() = default;
  122. };
  123. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2,
  124. std::optional<Nonnull<const EqualityContext*>> equality_ctx)
  125. -> bool;
  126. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
  127. std::optional<Nonnull<const EqualityContext*>> equality_ctx)
  128. -> bool;
  129. // An integer value.
  130. class IntValue : public Value {
  131. public:
  132. explicit IntValue(int value) : Value(Kind::IntValue), value_(value) {}
  133. static auto classof(const Value* value) -> bool {
  134. return value->kind() == Kind::IntValue;
  135. }
  136. auto value() const -> int { return value_; }
  137. private:
  138. int value_;
  139. };
  140. // A function value.
  141. class FunctionValue : public Value {
  142. public:
  143. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration)
  144. : Value(Kind::FunctionValue), declaration_(declaration) {}
  145. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration,
  146. Nonnull<const Bindings*> bindings)
  147. : Value(Kind::FunctionValue),
  148. declaration_(declaration),
  149. bindings_(bindings) {}
  150. static auto classof(const Value* value) -> bool {
  151. return value->kind() == Kind::FunctionValue;
  152. }
  153. auto declaration() const -> const FunctionDeclaration& {
  154. return *declaration_;
  155. }
  156. auto bindings() const -> const Bindings& { return *bindings_; }
  157. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  158. auto witnesses() const -> const ImplWitnessMap& {
  159. return bindings_->witnesses();
  160. }
  161. private:
  162. Nonnull<const FunctionDeclaration*> declaration_;
  163. Nonnull<const Bindings*> bindings_ = Bindings::None();
  164. };
  165. // A destructor value.
  166. class DestructorValue : public Value {
  167. public:
  168. explicit DestructorValue(Nonnull<const DestructorDeclaration*> declaration)
  169. : Value(Kind::DestructorValue), declaration_(declaration) {}
  170. static auto classof(const Value* value) -> bool {
  171. return value->kind() == Kind::DestructorValue;
  172. }
  173. auto declaration() const -> const DestructorDeclaration& {
  174. return *declaration_;
  175. }
  176. private:
  177. Nonnull<const DestructorDeclaration*> declaration_;
  178. };
  179. // A bound method value. It includes the receiver object.
  180. class BoundMethodValue : public Value {
  181. public:
  182. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  183. Nonnull<const Value*> receiver)
  184. : Value(Kind::BoundMethodValue),
  185. declaration_(declaration),
  186. receiver_(receiver) {}
  187. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  188. Nonnull<const Value*> receiver,
  189. Nonnull<const Bindings*> bindings)
  190. : Value(Kind::BoundMethodValue),
  191. declaration_(declaration),
  192. receiver_(receiver),
  193. bindings_(bindings) {}
  194. static auto classof(const Value* value) -> bool {
  195. return value->kind() == Kind::BoundMethodValue;
  196. }
  197. auto declaration() const -> const FunctionDeclaration& {
  198. return *declaration_;
  199. }
  200. auto receiver() const -> Nonnull<const Value*> { return receiver_; }
  201. auto bindings() const -> const Bindings& { return *bindings_; }
  202. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  203. auto witnesses() const -> const ImplWitnessMap& {
  204. return bindings_->witnesses();
  205. }
  206. private:
  207. Nonnull<const FunctionDeclaration*> declaration_;
  208. Nonnull<const Value*> receiver_;
  209. Nonnull<const Bindings*> bindings_ = Bindings::None();
  210. };
  211. // The value of a location in memory.
  212. class LValue : public Value {
  213. public:
  214. explicit LValue(Address value)
  215. : Value(Kind::LValue), value_(std::move(value)) {}
  216. static auto classof(const Value* value) -> bool {
  217. return value->kind() == Kind::LValue;
  218. }
  219. auto address() const -> const Address& { return value_; }
  220. private:
  221. Address value_;
  222. };
  223. // A pointer value
  224. class PointerValue : public Value {
  225. public:
  226. explicit PointerValue(Address value)
  227. : Value(Kind::PointerValue), value_(std::move(value)) {}
  228. static auto classof(const Value* value) -> bool {
  229. return value->kind() == Kind::PointerValue;
  230. }
  231. auto address() const -> const Address& { return value_; }
  232. private:
  233. Address value_;
  234. };
  235. // A bool value.
  236. class BoolValue : public Value {
  237. public:
  238. explicit BoolValue(bool value) : Value(Kind::BoolValue), value_(value) {}
  239. static auto classof(const Value* value) -> bool {
  240. return value->kind() == Kind::BoolValue;
  241. }
  242. auto value() const -> bool { return value_; }
  243. private:
  244. bool value_;
  245. };
  246. // A non-empty value of a struct type.
  247. //
  248. // It can't be empty because `{}` is a struct type as well as a value of that
  249. // type, so for consistency we always represent it as a StructType rather than
  250. // let it oscillate unpredictably between the two. However, this means code
  251. // that handles StructValue instances may also need to be able to handle
  252. // StructType instances.
  253. class StructValue : public Value {
  254. public:
  255. explicit StructValue(std::vector<NamedValue> elements)
  256. : Value(Kind::StructValue), elements_(std::move(elements)) {
  257. CARBON_CHECK(!elements_.empty())
  258. << "`{}` is represented as a StructType, not a StructValue.";
  259. }
  260. static auto classof(const Value* value) -> bool {
  261. return value->kind() == Kind::StructValue;
  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. NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits)
  275. : Value(Kind::NominalClassValue), type_(type), inits_(inits) {}
  276. static auto classof(const Value* value) -> bool {
  277. return value->kind() == Kind::NominalClassValue;
  278. }
  279. auto type() const -> const Value& { return *type_; }
  280. auto inits() const -> const Value& { return *inits_; }
  281. private:
  282. Nonnull<const Value*> type_;
  283. Nonnull<const Value*> inits_; // The initializing StructValue.
  284. };
  285. // An alternative constructor value.
  286. class AlternativeConstructorValue : public Value {
  287. public:
  288. AlternativeConstructorValue(std::string_view alt_name,
  289. std::string_view choice_name)
  290. : Value(Kind::AlternativeConstructorValue),
  291. alt_name_(std::move(alt_name)),
  292. choice_name_(std::move(choice_name)) {}
  293. static auto classof(const Value* value) -> bool {
  294. return value->kind() == Kind::AlternativeConstructorValue;
  295. }
  296. auto alt_name() const -> const std::string& { return alt_name_; }
  297. auto choice_name() const -> const std::string& { return choice_name_; }
  298. private:
  299. std::string alt_name_;
  300. std::string choice_name_;
  301. };
  302. // An alternative value.
  303. class AlternativeValue : public Value {
  304. public:
  305. AlternativeValue(std::string_view alt_name, std::string_view choice_name,
  306. Nonnull<const Value*> argument)
  307. : Value(Kind::AlternativeValue),
  308. alt_name_(std::move(alt_name)),
  309. choice_name_(std::move(choice_name)),
  310. argument_(argument) {}
  311. static auto classof(const Value* value) -> bool {
  312. return value->kind() == Kind::AlternativeValue;
  313. }
  314. auto alt_name() const -> const std::string& { return alt_name_; }
  315. auto choice_name() const -> const std::string& { return choice_name_; }
  316. auto argument() const -> const Value& { return *argument_; }
  317. private:
  318. std::string alt_name_;
  319. std::string choice_name_;
  320. Nonnull<const Value*> argument_;
  321. };
  322. // A tuple value.
  323. class TupleValue : public Value {
  324. public:
  325. // An empty tuple, also known as the unit type.
  326. static auto Empty() -> Nonnull<const TupleValue*> {
  327. static const TupleValue empty =
  328. TupleValue(std::vector<Nonnull<const Value*>>());
  329. return Nonnull<const TupleValue*>(&empty);
  330. }
  331. explicit TupleValue(std::vector<Nonnull<const Value*>> elements)
  332. : Value(Kind::TupleValue), elements_(std::move(elements)) {}
  333. static auto classof(const Value* value) -> bool {
  334. return value->kind() == Kind::TupleValue;
  335. }
  336. auto elements() const -> llvm::ArrayRef<Nonnull<const Value*>> {
  337. return elements_;
  338. }
  339. private:
  340. std::vector<Nonnull<const Value*>> elements_;
  341. };
  342. // A binding placeholder value.
  343. class BindingPlaceholderValue : public Value {
  344. public:
  345. // Represents the `_` placeholder.
  346. explicit BindingPlaceholderValue() : Value(Kind::BindingPlaceholderValue) {}
  347. // Represents a named placeholder.
  348. explicit BindingPlaceholderValue(ValueNodeView value_node)
  349. : Value(Kind::BindingPlaceholderValue),
  350. value_node_(std::move(value_node)) {}
  351. static auto classof(const Value* value) -> bool {
  352. return value->kind() == Kind::BindingPlaceholderValue;
  353. }
  354. auto value_node() const -> const std::optional<ValueNodeView>& {
  355. return value_node_;
  356. }
  357. private:
  358. std::optional<ValueNodeView> value_node_;
  359. };
  360. // Value for addr pattern
  361. class AddrValue : public Value {
  362. public:
  363. explicit AddrValue(Nonnull<const Value*> pattern)
  364. : Value(Kind::AddrValue), pattern_(pattern) {}
  365. static auto classof(const Value* value) -> bool {
  366. return value->kind() == Kind::AddrValue;
  367. }
  368. auto pattern() const -> const Value& { return *pattern_; }
  369. private:
  370. Nonnull<const Value*> pattern_;
  371. };
  372. // Value for uninitialized local variables.
  373. class UninitializedValue : public Value {
  374. public:
  375. explicit UninitializedValue(Nonnull<const Value*> pattern)
  376. : Value(Kind::UninitializedValue), pattern_(pattern) {}
  377. static auto classof(const Value* value) -> bool {
  378. return value->kind() == Kind::UninitializedValue;
  379. }
  380. auto pattern() const -> const Value& { return *pattern_; }
  381. private:
  382. Nonnull<const Value*> pattern_;
  383. };
  384. // The int type.
  385. class IntType : public Value {
  386. public:
  387. IntType() : Value(Kind::IntType) {}
  388. static auto classof(const Value* value) -> bool {
  389. return value->kind() == Kind::IntType;
  390. }
  391. };
  392. // The bool type.
  393. class BoolType : public Value {
  394. public:
  395. BoolType() : Value(Kind::BoolType) {}
  396. static auto classof(const Value* value) -> bool {
  397. return value->kind() == Kind::BoolType;
  398. }
  399. };
  400. // A type type.
  401. class TypeType : public Value {
  402. public:
  403. TypeType() : Value(Kind::TypeType) {}
  404. static auto classof(const Value* value) -> bool {
  405. return value->kind() == Kind::TypeType;
  406. }
  407. };
  408. // A function type.
  409. class FunctionType : public Value {
  410. public:
  411. // An explicit function parameter that is a `:!` binding:
  412. //
  413. // fn MakeEmptyVector(T:! Type) -> Vector(T);
  414. struct GenericParameter {
  415. size_t index;
  416. Nonnull<const GenericBinding*> binding;
  417. };
  418. FunctionType(Nonnull<const Value*> parameters,
  419. Nonnull<const Value*> return_type)
  420. : FunctionType(parameters, {}, return_type, {}, {}) {}
  421. FunctionType(Nonnull<const Value*> parameters,
  422. std::vector<GenericParameter> generic_parameters,
  423. Nonnull<const Value*> return_type,
  424. std::vector<Nonnull<const GenericBinding*>> deduced_bindings,
  425. std::vector<Nonnull<const ImplBinding*>> impl_bindings)
  426. : Value(Kind::FunctionType),
  427. parameters_(parameters),
  428. generic_parameters_(std::move(generic_parameters)),
  429. return_type_(return_type),
  430. deduced_bindings_(std::move(deduced_bindings)),
  431. impl_bindings_(std::move(impl_bindings)) {}
  432. static auto classof(const Value* value) -> bool {
  433. return value->kind() == Kind::FunctionType;
  434. }
  435. // The type of the function parameter tuple.
  436. auto parameters() const -> const Value& { return *parameters_; }
  437. // Parameters that use a generic `:!` binding at the top level.
  438. auto generic_parameters() const -> llvm::ArrayRef<GenericParameter> {
  439. return generic_parameters_;
  440. }
  441. // The function return type.
  442. auto return_type() const -> const Value& { return *return_type_; }
  443. // All generic bindings in this function's signature that should be deduced
  444. // in a call. This excludes any generic parameters.
  445. auto deduced_bindings() const
  446. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  447. return deduced_bindings_;
  448. }
  449. // The bindings for the witness tables (impls) required by the
  450. // bounds on the type parameters of the generic function.
  451. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  452. return impl_bindings_;
  453. }
  454. private:
  455. Nonnull<const Value*> parameters_;
  456. std::vector<GenericParameter> generic_parameters_;
  457. Nonnull<const Value*> return_type_;
  458. std::vector<Nonnull<const GenericBinding*>> deduced_bindings_;
  459. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  460. };
  461. // A pointer type.
  462. class PointerType : public Value {
  463. public:
  464. explicit PointerType(Nonnull<const Value*> type)
  465. : Value(Kind::PointerType), type_(type) {}
  466. static auto classof(const Value* value) -> bool {
  467. return value->kind() == Kind::PointerType;
  468. }
  469. auto type() const -> const Value& { return *type_; }
  470. private:
  471. Nonnull<const Value*> type_;
  472. };
  473. // The `auto` type.
  474. class AutoType : public Value {
  475. public:
  476. AutoType() : Value(Kind::AutoType) {}
  477. static auto classof(const Value* value) -> bool {
  478. return value->kind() == Kind::AutoType;
  479. }
  480. };
  481. // A struct type.
  482. //
  483. // Code that handles this type may sometimes need to have special-case handling
  484. // for `{}`, which is a struct value in addition to being a struct type.
  485. class StructType : public Value {
  486. public:
  487. StructType() : StructType(std::vector<NamedValue>{}) {}
  488. explicit StructType(std::vector<NamedValue> fields)
  489. : Value(Kind::StructType), fields_(std::move(fields)) {}
  490. static auto classof(const Value* value) -> bool {
  491. return value->kind() == Kind::StructType;
  492. }
  493. auto fields() const -> llvm::ArrayRef<NamedValue> { return fields_; }
  494. private:
  495. std::vector<NamedValue> fields_;
  496. };
  497. // A class type.
  498. // TODO: Consider splitting this class into several classes.
  499. class NominalClassType : public Value {
  500. public:
  501. // Construct a non-generic class type.
  502. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration)
  503. : Value(Kind::NominalClassType), declaration_(declaration) {
  504. CARBON_CHECK(!declaration->type_params().has_value())
  505. << "missing arguments for parameterized class type";
  506. }
  507. // Construct a fully instantiated generic class type to represent the
  508. // run-time type of an object.
  509. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration,
  510. Nonnull<const Bindings*> bindings)
  511. : Value(Kind::NominalClassType),
  512. declaration_(declaration),
  513. bindings_(bindings) {}
  514. static auto classof(const Value* value) -> bool {
  515. return value->kind() == Kind::NominalClassType;
  516. }
  517. auto declaration() const -> const ClassDeclaration& { return *declaration_; }
  518. auto bindings() const -> const Bindings& { return *bindings_; }
  519. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  520. // Witnesses for each of the class's impl bindings.
  521. auto witnesses() const -> const ImplWitnessMap& {
  522. return bindings_->witnesses();
  523. }
  524. // Returns whether this a parameterized class. That is, a class with
  525. // parameters and no corresponding arguments.
  526. auto IsParameterized() const -> bool {
  527. return declaration_->type_params().has_value() && type_args().empty();
  528. }
  529. // Returns the value of the function named `name` in this class, or
  530. // nullopt if there is no such function.
  531. auto FindFunction(std::string_view name) const
  532. -> std::optional<Nonnull<const FunctionValue*>>;
  533. private:
  534. Nonnull<const ClassDeclaration*> declaration_;
  535. Nonnull<const Bindings*> bindings_ = Bindings::None();
  536. };
  537. class MixinPseudoType : public Value {
  538. public:
  539. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration)
  540. : Value(Kind::MixinPseudoType), declaration_(declaration) {
  541. CARBON_CHECK(!declaration->params().has_value())
  542. << "missing arguments for parameterized mixin type";
  543. }
  544. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration,
  545. Nonnull<const Bindings*> bindings)
  546. : Value(Kind::MixinPseudoType),
  547. declaration_(declaration),
  548. bindings_(bindings) {}
  549. static auto classof(const Value* value) -> bool {
  550. return value->kind() == Kind::MixinPseudoType;
  551. }
  552. auto declaration() const -> const MixinDeclaration& { return *declaration_; }
  553. auto bindings() const -> const Bindings& { return *bindings_; }
  554. auto args() const -> const BindingMap& { return bindings_->args(); }
  555. auto witnesses() const -> const ImplWitnessMap& {
  556. return bindings_->witnesses();
  557. }
  558. auto FindFunction(const std::string_view& name) const
  559. -> std::optional<Nonnull<const FunctionValue*>>;
  560. private:
  561. Nonnull<const MixinDeclaration*> declaration_;
  562. Nonnull<const Bindings*> bindings_ = Bindings::None();
  563. };
  564. // Return the declaration of the member with the given name.
  565. auto FindMember(std::string_view name,
  566. llvm::ArrayRef<Nonnull<Declaration*>> members)
  567. -> std::optional<Nonnull<const Declaration*>>;
  568. // An interface type.
  569. class InterfaceType : public Value {
  570. public:
  571. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
  572. : Value(Kind::InterfaceType), declaration_(declaration) {
  573. CARBON_CHECK(!declaration->params().has_value())
  574. << "missing arguments for parameterized interface type";
  575. }
  576. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
  577. Nonnull<const Bindings*> bindings)
  578. : Value(Kind::InterfaceType),
  579. declaration_(declaration),
  580. bindings_(bindings) {}
  581. static auto classof(const Value* value) -> bool {
  582. return value->kind() == Kind::InterfaceType;
  583. }
  584. auto declaration() const -> const InterfaceDeclaration& {
  585. return *declaration_;
  586. }
  587. auto bindings() const -> const Bindings& { return *bindings_; }
  588. auto args() const -> const BindingMap& { return bindings_->args(); }
  589. auto witnesses() const -> const ImplWitnessMap& {
  590. return bindings_->witnesses();
  591. }
  592. private:
  593. Nonnull<const InterfaceDeclaration*> declaration_;
  594. Nonnull<const Bindings*> bindings_ = Bindings::None();
  595. };
  596. // A collection of values that are known to be the same.
  597. struct EqualityConstraint {
  598. // Visit the values in this equality constraint that are a single step away
  599. // from the given value according to this equality constraint. That is: if
  600. // `value` is identical to a value in `values`, then call the visitor on all
  601. // values in `values` that are not identical to `value`. Otherwise, do not
  602. // call the visitor.
  603. //
  604. // Stops and returns `false` if any call to the visitor returns `false`,
  605. // otherwise returns `true`.
  606. auto VisitEqualValues(
  607. Nonnull<const Value*> value,
  608. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  609. std::vector<Nonnull<const Value*>> values;
  610. };
  611. // A type-of-type for an unknown constrained type.
  612. //
  613. // These types are formed by the `&` operator that combines constraints and by
  614. // `where` expressions.
  615. //
  616. // A constraint has three main properties:
  617. //
  618. // * A collection of (type, interface) pairs for interfaces that are known to
  619. // be implemented by a type satisfying the constraint.
  620. // * A collection of sets of values, typically associated constants, that are
  621. // known to be the same.
  622. // * A collection of contexts in which member name lookups will be performed
  623. // for a type variable whose type is this constraint.
  624. //
  625. // Within these properties, the constrained type can be referred to with a
  626. // `VariableType` naming the `self_binding`.
  627. class ConstraintType : public Value {
  628. public:
  629. // A required implementation of an interface.
  630. struct ImplConstraint {
  631. Nonnull<const Value*> type;
  632. Nonnull<const InterfaceType*> interface;
  633. };
  634. using EqualityConstraint = Carbon::EqualityConstraint;
  635. // A constraint indicating that access to an associated constant should be
  636. // replaced by another value.
  637. struct RewriteConstraint {
  638. Nonnull<const InterfaceType*> interface;
  639. Nonnull<const AssociatedConstantDeclaration*> constant;
  640. Nonnull<const ValueLiteral*> replacement;
  641. };
  642. // A context in which we might look up a name.
  643. struct LookupContext {
  644. Nonnull<const Value*> context;
  645. };
  646. public:
  647. explicit ConstraintType(Nonnull<const GenericBinding*> self_binding,
  648. std::vector<ImplConstraint> impl_constraints,
  649. std::vector<EqualityConstraint> equality_constraints,
  650. std::vector<RewriteConstraint> rewrite_constraints,
  651. std::vector<LookupContext> lookup_contexts)
  652. : Value(Kind::ConstraintType),
  653. self_binding_(self_binding),
  654. impl_constraints_(std::move(impl_constraints)),
  655. equality_constraints_(std::move(equality_constraints)),
  656. rewrite_constraints_(std::move(rewrite_constraints)),
  657. lookup_contexts_(std::move(lookup_contexts)) {}
  658. static auto classof(const Value* value) -> bool {
  659. return value->kind() == Kind::ConstraintType;
  660. }
  661. auto self_binding() const -> Nonnull<const GenericBinding*> {
  662. return self_binding_;
  663. }
  664. auto impl_constraints() const -> llvm::ArrayRef<ImplConstraint> {
  665. return impl_constraints_;
  666. }
  667. auto equality_constraints() const -> llvm::ArrayRef<EqualityConstraint> {
  668. return equality_constraints_;
  669. }
  670. auto rewrite_constraints() const -> llvm::ArrayRef<RewriteConstraint> {
  671. return rewrite_constraints_;
  672. }
  673. auto lookup_contexts() const -> llvm::ArrayRef<LookupContext> {
  674. return lookup_contexts_;
  675. }
  676. // Visit the values in that are a single step away from the given value
  677. // according to equality constraints in this constraint type, that is, the
  678. // values `v` that are not identical to `value` but for which we have a
  679. // `value == v` equality constraint in this constraint type.
  680. //
  681. // Stops and returns `false` if any call to the visitor returns `false`,
  682. // otherwise returns `true`.
  683. auto VisitEqualValues(
  684. Nonnull<const Value*> value,
  685. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  686. private:
  687. Nonnull<const GenericBinding*> self_binding_;
  688. std::vector<ImplConstraint> impl_constraints_;
  689. std::vector<EqualityConstraint> equality_constraints_;
  690. std::vector<RewriteConstraint> rewrite_constraints_;
  691. std::vector<LookupContext> lookup_contexts_;
  692. };
  693. // A witness table.
  694. class Witness : public Value {
  695. protected:
  696. explicit Witness(Value::Kind kind) : Value(kind) {}
  697. public:
  698. static auto classof(const Value* value) -> bool {
  699. return value->kind() == Kind::ImplWitness ||
  700. value->kind() == Kind::BindingWitness ||
  701. value->kind() == Kind::ConstraintWitness ||
  702. value->kind() == Kind::ConstraintImplWitness;
  703. }
  704. };
  705. // The witness table for an impl.
  706. class ImplWitness : public Witness {
  707. public:
  708. // Construct a witness for an impl.
  709. explicit ImplWitness(Nonnull<const ImplDeclaration*> declaration,
  710. Nonnull<const Bindings*> bindings)
  711. : Witness(Kind::ImplWitness),
  712. declaration_(declaration),
  713. bindings_(bindings) {}
  714. static auto classof(const Value* value) -> bool {
  715. return value->kind() == Kind::ImplWitness;
  716. }
  717. auto declaration() const -> const ImplDeclaration& { return *declaration_; }
  718. auto bindings() const -> const Bindings& { return *bindings_; }
  719. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  720. auto witnesses() const -> const ImplWitnessMap& {
  721. return bindings_->witnesses();
  722. }
  723. private:
  724. Nonnull<const ImplDeclaration*> declaration_;
  725. Nonnull<const Bindings*> bindings_ = Bindings::None();
  726. };
  727. // The symbolic witness corresponding to an unresolved impl binding.
  728. class BindingWitness : public Witness {
  729. public:
  730. // Construct a witness for an impl binding.
  731. explicit BindingWitness(Nonnull<const ImplBinding*> binding)
  732. : Witness(Kind::BindingWitness), binding_(binding) {}
  733. static auto classof(const Value* value) -> bool {
  734. return value->kind() == Kind::BindingWitness;
  735. }
  736. auto binding() const -> Nonnull<const ImplBinding*> { return binding_; }
  737. private:
  738. Nonnull<const ImplBinding*> binding_;
  739. };
  740. // A witness for a constraint type, expressed as a tuple of witnesses for the
  741. // individual impl constraints in the constraint type.
  742. class ConstraintWitness : public Witness {
  743. public:
  744. explicit ConstraintWitness(std::vector<Nonnull<const Witness*>> witnesses)
  745. : Witness(Kind::ConstraintWitness), witnesses_(std::move(witnesses)) {}
  746. static auto classof(const Value* value) -> bool {
  747. return value->kind() == Kind::ConstraintWitness;
  748. }
  749. auto witnesses() const -> llvm::ArrayRef<Nonnull<const Witness*>> {
  750. return witnesses_;
  751. }
  752. private:
  753. std::vector<Nonnull<const Witness*>> witnesses_;
  754. };
  755. // A witness for an impl constraint in a constraint type, expressed in terms of
  756. // a symbolic witness for the constraint type.
  757. class ConstraintImplWitness : public Witness {
  758. public:
  759. // Make a witness for the given impl_constraint of the given `ConstraintType`
  760. // witness. If we're indexing into a known tuple of witnesses, pull out the
  761. // element.
  762. static auto Make(Nonnull<Arena*> arena, Nonnull<const Witness*> witness,
  763. int index) -> Nonnull<const Witness*> {
  764. CARBON_CHECK(!llvm::isa<ImplWitness>(witness))
  765. << "impl witness has no components to access";
  766. if (auto* constraint_witness = llvm::dyn_cast<ConstraintWitness>(witness)) {
  767. return constraint_witness->witnesses()[index];
  768. }
  769. return arena->New<ConstraintImplWitness>(witness, index);
  770. }
  771. explicit ConstraintImplWitness(Nonnull<const Witness*> constraint_witness,
  772. int index)
  773. : Witness(Kind::ConstraintImplWitness),
  774. constraint_witness_(constraint_witness),
  775. index_(index) {
  776. CARBON_CHECK(!llvm::isa<ConstraintWitness>(constraint_witness))
  777. << "should have resolved element from constraint witness";
  778. }
  779. static auto classof(const Value* value) -> bool {
  780. return value->kind() == Kind::ConstraintImplWitness;
  781. }
  782. // Get the witness for the complete `ConstraintType`.
  783. auto constraint_witness() const -> Nonnull<const Witness*> {
  784. return constraint_witness_;
  785. }
  786. // Get the index of the impl constraint within the constraint type.
  787. auto index() const -> int { return index_; }
  788. private:
  789. Nonnull<const Witness*> constraint_witness_;
  790. int index_;
  791. };
  792. // A choice type.
  793. class ChoiceType : public Value {
  794. public:
  795. ChoiceType(Nonnull<const ChoiceDeclaration*> declaration,
  796. Nonnull<const Bindings*> bindings)
  797. : Value(Kind::ChoiceType),
  798. declaration_(declaration),
  799. bindings_(bindings) {}
  800. static auto classof(const Value* value) -> bool {
  801. return value->kind() == Kind::ChoiceType;
  802. }
  803. auto name() const -> const std::string& { return declaration_->name(); }
  804. // Returns the parameter types of the alternative with the given name,
  805. // or nullopt if no such alternative is present.
  806. auto FindAlternative(std::string_view name) const
  807. -> std::optional<Nonnull<const Value*>>;
  808. auto bindings() const -> const Bindings& { return *bindings_; }
  809. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  810. auto declaration() const -> const ChoiceDeclaration& { return *declaration_; }
  811. auto IsParameterized() const -> bool {
  812. return declaration_->type_params().has_value();
  813. }
  814. private:
  815. Nonnull<const ChoiceDeclaration*> declaration_;
  816. Nonnull<const Bindings*> bindings_;
  817. };
  818. // A continuation type.
  819. class ContinuationType : public Value {
  820. public:
  821. ContinuationType() : Value(Kind::ContinuationType) {}
  822. static auto classof(const Value* value) -> bool {
  823. return value->kind() == Kind::ContinuationType;
  824. }
  825. };
  826. // A variable type.
  827. class VariableType : public Value {
  828. public:
  829. explicit VariableType(Nonnull<const GenericBinding*> binding)
  830. : Value(Kind::VariableType), binding_(binding) {}
  831. static auto classof(const Value* value) -> bool {
  832. return value->kind() == Kind::VariableType;
  833. }
  834. auto binding() const -> const GenericBinding& { return *binding_; }
  835. private:
  836. Nonnull<const GenericBinding*> binding_;
  837. };
  838. // A name of an entity that has explicit parameters, such as a parameterized
  839. // class or interface. When arguments for those parameters are provided in a
  840. // call, the result will be a class type or interface type.
  841. class ParameterizedEntityName : public Value {
  842. public:
  843. explicit ParameterizedEntityName(Nonnull<const Declaration*> declaration,
  844. Nonnull<const TuplePattern*> params)
  845. : Value(Kind::ParameterizedEntityName),
  846. declaration_(declaration),
  847. params_(params) {}
  848. static auto classof(const Value* value) -> bool {
  849. return value->kind() == Kind::ParameterizedEntityName;
  850. }
  851. auto declaration() const -> const Declaration& { return *declaration_; }
  852. auto params() const -> const TuplePattern& { return *params_; }
  853. private:
  854. Nonnull<const Declaration*> declaration_;
  855. Nonnull<const TuplePattern*> params_;
  856. };
  857. // The name of a member of a class or interface.
  858. //
  859. // These values are used to represent the second operand of a compound member
  860. // access expression: `x.(A.B)`, and can also be the value of an alias
  861. // declaration, but cannot be used in most other contexts.
  862. class MemberName : public Value {
  863. public:
  864. MemberName(std::optional<Nonnull<const Value*>> base_type,
  865. std::optional<Nonnull<const InterfaceType*>> interface,
  866. Member member)
  867. : Value(Kind::MemberName),
  868. base_type_(base_type),
  869. interface_(interface),
  870. member_(member) {
  871. CARBON_CHECK(base_type || interface)
  872. << "member name must be in a type, an interface, or both";
  873. }
  874. static auto classof(const Value* value) -> bool {
  875. return value->kind() == Kind::MemberName;
  876. }
  877. // The type for which `name` is a member or a member of an `impl`.
  878. auto base_type() const -> std::optional<Nonnull<const Value*>> {
  879. return base_type_;
  880. }
  881. // The interface for which `name` is a member, if any.
  882. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  883. return interface_;
  884. }
  885. // The member.
  886. auto member() const -> Member { return member_; }
  887. // The name of the member.
  888. auto name() const -> std::string_view { return member().name(); }
  889. private:
  890. std::optional<Nonnull<const Value*>> base_type_;
  891. std::optional<Nonnull<const InterfaceType*>> interface_;
  892. Member member_;
  893. };
  894. // A symbolic value representing an associated constant.
  895. //
  896. // This is a value of the form `A.B` or `A.B.C` or similar, where `A` is a
  897. // `VariableType`.
  898. class AssociatedConstant : public Value {
  899. public:
  900. explicit AssociatedConstant(
  901. Nonnull<const Value*> base, Nonnull<const InterfaceType*> interface,
  902. Nonnull<const AssociatedConstantDeclaration*> constant,
  903. Nonnull<const Witness*> witness)
  904. : Value(Kind::AssociatedConstant),
  905. base_(base),
  906. interface_(interface),
  907. constant_(constant),
  908. witness_(witness) {}
  909. static auto classof(const Value* value) -> bool {
  910. return value->kind() == Kind::AssociatedConstant;
  911. }
  912. // The type for which we denote an associated constant.
  913. auto base() const -> const Value& { return *base_; }
  914. // The interface within which the constant was declared.
  915. auto interface() const -> const InterfaceType& { return *interface_; }
  916. // The associated constant whose value is being denoted.
  917. auto constant() const -> const AssociatedConstantDeclaration& {
  918. return *constant_;
  919. }
  920. // Witness within which the constant's value can be found.
  921. auto witness() const -> const Witness& { return *witness_; }
  922. private:
  923. Nonnull<const Value*> base_;
  924. Nonnull<const InterfaceType*> interface_;
  925. Nonnull<const AssociatedConstantDeclaration*> constant_;
  926. Nonnull<const Witness*> witness_;
  927. };
  928. // A first-class continuation representation of a fragment of the stack.
  929. // A continuation value behaves like a pointer to the underlying stack
  930. // fragment, which is exposed by `Stack()`.
  931. class ContinuationValue : public Value {
  932. public:
  933. class StackFragment {
  934. public:
  935. // Constructs an empty StackFragment.
  936. StackFragment() = default;
  937. // Requires *this to be empty, because by the time we're tearing down the
  938. // Arena, it's no longer safe to invoke ~Action.
  939. ~StackFragment();
  940. StackFragment(StackFragment&&) = delete;
  941. auto operator=(StackFragment&&) -> StackFragment& = delete;
  942. // Store the given partial todo stack in *this, which must currently be
  943. // empty. The stack is represented with the top of the stack at the
  944. // beginning of the vector, the reverse of the usual order.
  945. void StoreReversed(std::vector<std::unique_ptr<Action>> reversed_todo);
  946. // Restore the currently stored stack fragment to the top of `todo`,
  947. // leaving *this empty.
  948. void RestoreTo(Stack<std::unique_ptr<Action>>& todo);
  949. // Destroy the currently stored stack fragment.
  950. void Clear();
  951. void Print(llvm::raw_ostream& out) const;
  952. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  953. private:
  954. // The todo stack of a suspended continuation, starting with the top
  955. // Action.
  956. std::vector<std::unique_ptr<Action>> reversed_todo_;
  957. };
  958. explicit ContinuationValue(Nonnull<StackFragment*> stack)
  959. : Value(Kind::ContinuationValue), stack_(stack) {}
  960. static auto classof(const Value* value) -> bool {
  961. return value->kind() == Kind::ContinuationValue;
  962. }
  963. // The todo stack of the suspended continuation. Note that this provides
  964. // mutable access, even when *this is const, because of the reference-like
  965. // semantics of ContinuationValue.
  966. auto stack() const -> StackFragment& { return *stack_; }
  967. private:
  968. Nonnull<StackFragment*> stack_;
  969. };
  970. // The String type.
  971. class StringType : public Value {
  972. public:
  973. StringType() : Value(Kind::StringType) {}
  974. static auto classof(const Value* value) -> bool {
  975. return value->kind() == Kind::StringType;
  976. }
  977. };
  978. // A string value.
  979. class StringValue : public Value {
  980. public:
  981. explicit StringValue(std::string value)
  982. : Value(Kind::StringValue), value_(std::move(value)) {}
  983. static auto classof(const Value* value) -> bool {
  984. return value->kind() == Kind::StringValue;
  985. }
  986. auto value() const -> const std::string& { return value_; }
  987. private:
  988. std::string value_;
  989. };
  990. class TypeOfMixinPseudoType : public Value {
  991. public:
  992. explicit TypeOfMixinPseudoType(Nonnull<const MixinPseudoType*> class_type)
  993. : Value(Kind::TypeOfMixinPseudoType), mixin_type_(class_type) {}
  994. static auto classof(const Value* value) -> bool {
  995. return value->kind() == Kind::TypeOfMixinPseudoType;
  996. }
  997. auto mixin_type() const -> const MixinPseudoType& { return *mixin_type_; }
  998. private:
  999. Nonnull<const MixinPseudoType*> mixin_type_;
  1000. };
  1001. // The type of an expression whose value is the name of a parameterized entity.
  1002. // Such an expression can only be used as the operand of a call expression that
  1003. // provides arguments for the parameters.
  1004. class TypeOfParameterizedEntityName : public Value {
  1005. public:
  1006. explicit TypeOfParameterizedEntityName(
  1007. Nonnull<const ParameterizedEntityName*> name)
  1008. : Value(Kind::TypeOfParameterizedEntityName), name_(name) {}
  1009. static auto classof(const Value* value) -> bool {
  1010. return value->kind() == Kind::TypeOfParameterizedEntityName;
  1011. }
  1012. auto name() const -> const ParameterizedEntityName& { return *name_; }
  1013. private:
  1014. Nonnull<const ParameterizedEntityName*> name_;
  1015. };
  1016. // The type of a member name expression.
  1017. //
  1018. // This is used for member names that don't denote a specific object or value
  1019. // until used on the right-hand side of a `.`, such as an instance method or
  1020. // field name, or any member function in an interface.
  1021. //
  1022. // Such expressions can appear only as the target of an `alias` declaration or
  1023. // as the member name in a compound member access.
  1024. class TypeOfMemberName : public Value {
  1025. public:
  1026. explicit TypeOfMemberName(Member member)
  1027. : Value(Kind::TypeOfMemberName), member_(member) {}
  1028. static auto classof(const Value* value) -> bool {
  1029. return value->kind() == Kind::TypeOfMemberName;
  1030. }
  1031. // TODO: consider removing this or moving it elsewhere in the AST,
  1032. // since it's arguably part of the expression value rather than its type.
  1033. auto member() const -> Member { return member_; }
  1034. private:
  1035. Member member_;
  1036. };
  1037. // The type of a statically-sized array.
  1038. //
  1039. // Note that values of this type are represented as tuples.
  1040. class StaticArrayType : public Value {
  1041. public:
  1042. // Constructs a statically-sized array type with the given element type and
  1043. // size.
  1044. StaticArrayType(Nonnull<const Value*> element_type, size_t size)
  1045. : Value(Kind::StaticArrayType),
  1046. element_type_(element_type),
  1047. size_(size) {}
  1048. static auto classof(const Value* value) -> bool {
  1049. return value->kind() == Kind::StaticArrayType;
  1050. }
  1051. auto element_type() const -> const Value& { return *element_type_; }
  1052. auto size() const -> size_t { return size_; }
  1053. private:
  1054. Nonnull<const Value*> element_type_;
  1055. size_t size_;
  1056. };
  1057. } // namespace Carbon
  1058. #endif // CARBON_EXPLORER_INTERPRETER_VALUE_H_