value.h 33 KB

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