value.h 34 KB

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