value.h 25 KB

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