value.h 26 KB

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