value.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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 GenericBinding*>> generic_bindings,
  351. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings)
  352. : Value(Kind::FunctionType),
  353. deduced_(deduced),
  354. parameters_(parameters),
  355. return_type_(return_type),
  356. generic_bindings_(generic_bindings),
  357. impl_bindings_(impl_bindings) {}
  358. static auto classof(const Value* value) -> bool {
  359. return value->kind() == Kind::FunctionType;
  360. }
  361. auto deduced() const -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  362. return deduced_;
  363. }
  364. auto parameters() const -> const Value& { return *parameters_; }
  365. auto return_type() const -> const Value& { return *return_type_; }
  366. // All generic bindings in this function's signature.
  367. auto generic_bindings() const
  368. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  369. return generic_bindings_;
  370. }
  371. // The bindings for the witness tables (impls) required by the
  372. // bounds on the type parameters of the generic function.
  373. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  374. return impl_bindings_;
  375. }
  376. private:
  377. std::vector<Nonnull<const GenericBinding*>> deduced_;
  378. Nonnull<const Value*> parameters_;
  379. Nonnull<const Value*> return_type_;
  380. std::vector<Nonnull<const GenericBinding*>> generic_bindings_;
  381. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  382. };
  383. // A pointer type.
  384. class PointerType : public Value {
  385. public:
  386. explicit PointerType(Nonnull<const Value*> type)
  387. : Value(Kind::PointerType), type_(type) {}
  388. static auto classof(const Value* value) -> bool {
  389. return value->kind() == Kind::PointerType;
  390. }
  391. auto type() const -> const Value& { return *type_; }
  392. private:
  393. Nonnull<const Value*> type_;
  394. };
  395. // The `auto` type.
  396. class AutoType : public Value {
  397. public:
  398. AutoType() : Value(Kind::AutoType) {}
  399. static auto classof(const Value* value) -> bool {
  400. return value->kind() == Kind::AutoType;
  401. }
  402. };
  403. // A struct type.
  404. //
  405. // Code that handles this type may sometimes need to have special-case handling
  406. // for `{}`, which is a struct value in addition to being a struct type.
  407. class StructType : public Value {
  408. public:
  409. StructType() : StructType(std::vector<NamedValue>{}) {}
  410. explicit StructType(std::vector<NamedValue> fields)
  411. : Value(Kind::StructType), fields_(std::move(fields)) {}
  412. static auto classof(const Value* value) -> bool {
  413. return value->kind() == Kind::StructType;
  414. }
  415. auto fields() const -> llvm::ArrayRef<NamedValue> { return fields_; }
  416. private:
  417. std::vector<NamedValue> fields_;
  418. };
  419. // A class type.
  420. // TODO: Consider splitting this class into several classes.
  421. class NominalClassType : public Value {
  422. public:
  423. // Construct a non-generic class type.
  424. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration)
  425. : Value(Kind::NominalClassType), declaration_(declaration) {
  426. CARBON_CHECK(!declaration->type_params().has_value())
  427. << "missing arguments for parameterized class type";
  428. }
  429. // Construct a class type that represents the result of applying the
  430. // given generic class to the `type_args`.
  431. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration,
  432. const BindingMap& type_args)
  433. : Value(Kind::NominalClassType),
  434. declaration_(declaration),
  435. type_args_(type_args) {}
  436. // Construct a class type that represents the result of applying the
  437. // given generic class to the `type_args` and that records the result of the
  438. // compile-time search for any required impls.
  439. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration,
  440. const BindingMap& type_args,
  441. const ImplExpMap& impls)
  442. : Value(Kind::NominalClassType),
  443. declaration_(declaration),
  444. type_args_(type_args),
  445. impls_(impls) {}
  446. // Construct a fully instantiated generic class type to represent the
  447. // run-time type of an object.
  448. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration,
  449. const BindingMap& type_args,
  450. const std::map<Nonnull<const ImplBinding*>,
  451. Nonnull<const Witness*>>& wits)
  452. : Value(Kind::NominalClassType),
  453. declaration_(declaration),
  454. type_args_(type_args),
  455. witnesses_(wits) {}
  456. static auto classof(const Value* value) -> bool {
  457. return value->kind() == Kind::NominalClassType;
  458. }
  459. auto declaration() const -> const ClassDeclaration& { return *declaration_; }
  460. auto type_args() const -> const BindingMap& { return type_args_; }
  461. // Maps each of an instantiated generic class's impl bindings to an
  462. // expression that constructs the witness table for the corresponding
  463. // argument. Should not be called on 1) a non-generic class, 2) a
  464. // generic-class that is not instantiated, or 3) a fully
  465. // instantiated runtime type of a generic class.
  466. auto impls() const -> const ImplExpMap& { return impls_; }
  467. // Maps each of the class's impl bindings to the witness table
  468. // for the corresponding argument. Should only be called on a fully
  469. // instantiated runtime type of a generic class.
  470. auto witnesses() const -> const ImplWitnessMap& { return witnesses_; }
  471. // Returns whether this a parameterized class. That is, a class with
  472. // parameters and no corresponding arguments.
  473. auto IsParameterized() const -> bool {
  474. return declaration_->type_params().has_value() && type_args_.empty();
  475. }
  476. // Returns the value of the function named `name` in this class, or
  477. // nullopt if there is no such function.
  478. auto FindFunction(const std::string& name) const
  479. -> std::optional<Nonnull<const FunctionValue*>>;
  480. private:
  481. Nonnull<const ClassDeclaration*> declaration_;
  482. BindingMap type_args_;
  483. ImplExpMap impls_;
  484. ImplWitnessMap witnesses_;
  485. };
  486. // Return the declaration of the member with the given name.
  487. auto FindMember(const std::string& name,
  488. llvm::ArrayRef<Nonnull<Declaration*>> members)
  489. -> std::optional<Nonnull<const Declaration*>>;
  490. // An interface type.
  491. class InterfaceType : public Value {
  492. public:
  493. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
  494. : Value(Kind::InterfaceType), declaration_(declaration) {
  495. CARBON_CHECK(!declaration->params().has_value())
  496. << "missing arguments for parameterized interface type";
  497. }
  498. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
  499. const BindingMap& args)
  500. : Value(Kind::InterfaceType), declaration_(declaration), args_(args) {}
  501. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
  502. const BindingMap& args, const ImplExpMap& impls)
  503. : Value(Kind::InterfaceType),
  504. declaration_(declaration),
  505. args_(args),
  506. impls_(impls) {}
  507. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
  508. const BindingMap& args, const ImplWitnessMap& wits)
  509. : Value(Kind::InterfaceType),
  510. declaration_(declaration),
  511. args_(args),
  512. witnesses_(wits) {}
  513. static auto classof(const Value* value) -> bool {
  514. return value->kind() == Kind::InterfaceType;
  515. }
  516. auto declaration() const -> const InterfaceDeclaration& {
  517. return *declaration_;
  518. }
  519. auto args() const -> const BindingMap& { return args_; }
  520. // FIXME: These aren't used for anything yet.
  521. auto impls() const -> const ImplExpMap& { return impls_; }
  522. auto witnesses() const -> const ImplWitnessMap& { return witnesses_; }
  523. private:
  524. Nonnull<const InterfaceDeclaration*> declaration_;
  525. BindingMap args_;
  526. ImplExpMap impls_;
  527. ImplWitnessMap witnesses_;
  528. };
  529. // The witness table for an impl.
  530. class Witness : public Value {
  531. public:
  532. // Construct a witness for
  533. // 1) a non-generic impl, or
  534. // 2) a generic impl that has not yet been applied to type arguments.
  535. explicit Witness(Nonnull<const ImplDeclaration*> declaration)
  536. : Value(Kind::Witness), declaration_(declaration) {}
  537. // Construct an instantiated generic impl.
  538. explicit Witness(Nonnull<const ImplDeclaration*> declaration,
  539. const BindingMap& type_args, const ImplWitnessMap& wits)
  540. : Value(Kind::Witness),
  541. declaration_(declaration),
  542. type_args_(type_args),
  543. witnesses_(wits) {}
  544. static auto classof(const Value* value) -> bool {
  545. return value->kind() == Kind::Witness;
  546. }
  547. auto declaration() const -> const ImplDeclaration& { return *declaration_; }
  548. auto type_args() const -> const BindingMap& { return type_args_; }
  549. // Maps each of the impl's impl bindings to the witness table
  550. // for the corresponding argument. Should only be called on a fully
  551. // instantiated runtime type of a generic class.
  552. auto witnesses() const -> const ImplWitnessMap& { return witnesses_; }
  553. private:
  554. Nonnull<const ImplDeclaration*> declaration_;
  555. BindingMap type_args_;
  556. ImplWitnessMap witnesses_;
  557. };
  558. // A choice type.
  559. class ChoiceType : public Value {
  560. public:
  561. ChoiceType(std::string name, std::vector<NamedValue> alternatives)
  562. : Value(Kind::ChoiceType),
  563. name_(std::move(name)),
  564. alternatives_(std::move(alternatives)) {}
  565. static auto classof(const Value* value) -> bool {
  566. return value->kind() == Kind::ChoiceType;
  567. }
  568. auto name() const -> const std::string& { return name_; }
  569. // Returns the parameter types of the alternative with the given name,
  570. // or nullopt if no such alternative is present.
  571. auto FindAlternative(std::string_view name) const
  572. -> std::optional<Nonnull<const Value*>>;
  573. private:
  574. std::string name_;
  575. std::vector<NamedValue> alternatives_;
  576. };
  577. // A continuation type.
  578. class ContinuationType : public Value {
  579. public:
  580. ContinuationType() : Value(Kind::ContinuationType) {}
  581. static auto classof(const Value* value) -> bool {
  582. return value->kind() == Kind::ContinuationType;
  583. }
  584. };
  585. // A variable type.
  586. class VariableType : public Value {
  587. public:
  588. explicit VariableType(Nonnull<const GenericBinding*> binding)
  589. : Value(Kind::VariableType), binding_(binding) {}
  590. static auto classof(const Value* value) -> bool {
  591. return value->kind() == Kind::VariableType;
  592. }
  593. auto binding() const -> const GenericBinding& { return *binding_; }
  594. private:
  595. Nonnull<const GenericBinding*> binding_;
  596. };
  597. // A name of an entity that has explicit parameters, such as a parameterized
  598. // class or interface. When arguments for those parameters are provided in a
  599. // call, the result will be a class type or interface type.
  600. class ParameterizedEntityName : public Value {
  601. public:
  602. explicit ParameterizedEntityName(Nonnull<const Declaration*> declaration,
  603. Nonnull<const TuplePattern*> params)
  604. : Value(Kind::ParameterizedEntityName),
  605. declaration_(declaration),
  606. params_(params) {}
  607. static auto classof(const Value* value) -> bool {
  608. return value->kind() == Kind::ParameterizedEntityName;
  609. }
  610. auto declaration() const -> const Declaration& { return *declaration_; }
  611. auto params() const -> const TuplePattern& { return *params_; }
  612. private:
  613. Nonnull<const Declaration*> declaration_;
  614. Nonnull<const TuplePattern*> params_;
  615. };
  616. // A first-class continuation representation of a fragment of the stack.
  617. // A continuation value behaves like a pointer to the underlying stack
  618. // fragment, which is exposed by `Stack()`.
  619. class ContinuationValue : public Value {
  620. public:
  621. class StackFragment {
  622. public:
  623. // Constructs an empty StackFragment.
  624. StackFragment() = default;
  625. // Requires *this to be empty, because by the time we're tearing down the
  626. // Arena, it's no longer safe to invoke ~Action.
  627. ~StackFragment();
  628. StackFragment(StackFragment&&) = delete;
  629. auto operator=(StackFragment&&) -> StackFragment& = delete;
  630. // Store the given partial todo stack in *this, which must currently be
  631. // empty. The stack is represented with the top of the stack at the
  632. // beginning of the vector, the reverse of the usual order.
  633. void StoreReversed(std::vector<std::unique_ptr<Action>> reversed_todo);
  634. // Restore the currently stored stack fragment to the top of `todo`,
  635. // leaving *this empty.
  636. void RestoreTo(Stack<std::unique_ptr<Action>>& todo);
  637. // Destroy the currently stored stack fragment.
  638. void Clear();
  639. void Print(llvm::raw_ostream& out) const;
  640. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  641. private:
  642. // The todo stack of a suspended continuation, starting with the top
  643. // Action.
  644. std::vector<std::unique_ptr<Action>> reversed_todo_;
  645. };
  646. explicit ContinuationValue(Nonnull<StackFragment*> stack)
  647. : Value(Kind::ContinuationValue), stack_(stack) {}
  648. static auto classof(const Value* value) -> bool {
  649. return value->kind() == Kind::ContinuationValue;
  650. }
  651. // The todo stack of the suspended continuation. Note that this provides
  652. // mutable access, even when *this is const, because of the reference-like
  653. // semantics of ContinuationValue.
  654. auto stack() const -> StackFragment& { return *stack_; }
  655. private:
  656. Nonnull<StackFragment*> stack_;
  657. };
  658. // The String type.
  659. class StringType : public Value {
  660. public:
  661. StringType() : Value(Kind::StringType) {}
  662. static auto classof(const Value* value) -> bool {
  663. return value->kind() == Kind::StringType;
  664. }
  665. };
  666. // A string value.
  667. class StringValue : public Value {
  668. public:
  669. explicit StringValue(std::string value)
  670. : Value(Kind::StringValue), value_(std::move(value)) {}
  671. static auto classof(const Value* value) -> bool {
  672. return value->kind() == Kind::StringValue;
  673. }
  674. auto value() const -> const std::string& { return value_; }
  675. private:
  676. std::string value_;
  677. };
  678. // The type of an expression whose value is a class type. Currently there is no
  679. // way to explicitly name such a type in Carbon code, but we are tentatively
  680. // using `typeof(ClassName)` as the debug-printing format, in anticipation of
  681. // something like that becoming valid Carbon syntax.
  682. class TypeOfClassType : public Value {
  683. public:
  684. explicit TypeOfClassType(Nonnull<const NominalClassType*> class_type)
  685. : Value(Kind::TypeOfClassType), class_type_(class_type) {}
  686. static auto classof(const Value* value) -> bool {
  687. return value->kind() == Kind::TypeOfClassType;
  688. }
  689. auto class_type() const -> const NominalClassType& { return *class_type_; }
  690. private:
  691. Nonnull<const NominalClassType*> class_type_;
  692. };
  693. class TypeOfInterfaceType : public Value {
  694. public:
  695. explicit TypeOfInterfaceType(Nonnull<const InterfaceType*> iface_type)
  696. : Value(Kind::TypeOfInterfaceType), iface_type_(iface_type) {}
  697. static auto classof(const Value* value) -> bool {
  698. return value->kind() == Kind::TypeOfInterfaceType;
  699. }
  700. auto interface_type() const -> const InterfaceType& { return *iface_type_; }
  701. private:
  702. Nonnull<const InterfaceType*> iface_type_;
  703. };
  704. // The type of an expression whose value is a choice type. Currently there is no
  705. // way to explicitly name such a type in Carbon code, but we are tentatively
  706. // using `typeof(ChoiceName)` as the debug-printing format, in anticipation of
  707. // something like that becoming valid Carbon syntax.
  708. class TypeOfChoiceType : public Value {
  709. public:
  710. explicit TypeOfChoiceType(Nonnull<const ChoiceType*> choice_type)
  711. : Value(Kind::TypeOfChoiceType), choice_type_(choice_type) {}
  712. static auto classof(const Value* value) -> bool {
  713. return value->kind() == Kind::TypeOfChoiceType;
  714. }
  715. auto choice_type() const -> const ChoiceType& { return *choice_type_; }
  716. private:
  717. Nonnull<const ChoiceType*> choice_type_;
  718. };
  719. // The type of an expression whose value is the name of a parameterized entity.
  720. // Such an expression can only be used as the operand of a call expression that
  721. // provides arguments for the parameters.
  722. class TypeOfParameterizedEntityName : public Value {
  723. public:
  724. explicit TypeOfParameterizedEntityName(
  725. Nonnull<const ParameterizedEntityName*> name)
  726. : Value(Kind::TypeOfParameterizedEntityName), name_(name) {}
  727. static auto classof(const Value* value) -> bool {
  728. return value->kind() == Kind::TypeOfParameterizedEntityName;
  729. }
  730. auto name() const -> const ParameterizedEntityName& { return *name_; }
  731. private:
  732. Nonnull<const ParameterizedEntityName*> name_;
  733. };
  734. // The type of a statically-sized array.
  735. //
  736. // Note that values of this type are represented as tuples.
  737. class StaticArrayType : public Value {
  738. public:
  739. // Constructs a statically-sized array type with the given element type and
  740. // size.
  741. StaticArrayType(Nonnull<const Value*> element_type, size_t size)
  742. : Value(Kind::StaticArrayType),
  743. element_type_(element_type),
  744. size_(size) {}
  745. static auto classof(const Value* value) -> bool {
  746. return value->kind() == Kind::StaticArrayType;
  747. }
  748. auto element_type() const -> const Value& { return *element_type_; }
  749. auto size() const -> size_t { return size_; }
  750. private:
  751. Nonnull<const Value*> element_type_;
  752. size_t size_;
  753. };
  754. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool;
  755. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool;
  756. } // namespace Carbon
  757. #endif // EXPLORER_INTERPRETER_VALUE_H_