value.h 37 KB

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