value.h 36 KB

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