value.h 29 KB

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