value.h 38 KB

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