value.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. // A function value.
  108. class FunctionValue : public Value {
  109. public:
  110. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration)
  111. : Value(Kind::FunctionValue), declaration_(declaration) {}
  112. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration,
  113. const BindingMap& type_args,
  114. const std::map<Nonnull<const ImplBinding*>,
  115. Nonnull<const Witness*>>& wits)
  116. : Value(Kind::FunctionValue),
  117. declaration_(declaration),
  118. type_args_(type_args),
  119. witnesses_(wits) {}
  120. static auto classof(const Value* value) -> bool {
  121. return value->kind() == Kind::FunctionValue;
  122. }
  123. auto declaration() const -> const FunctionDeclaration& {
  124. return *declaration_;
  125. }
  126. auto type_args() const -> const BindingMap& { return type_args_; }
  127. auto witnesses() const
  128. -> const std::map<Nonnull<const ImplBinding*>, const Witness*>& {
  129. return witnesses_;
  130. }
  131. private:
  132. Nonnull<const FunctionDeclaration*> declaration_;
  133. BindingMap type_args_;
  134. std::map<Nonnull<const ImplBinding*>, Nonnull<const Witness*>> witnesses_;
  135. };
  136. // A bound method value. It includes the receiver object.
  137. class BoundMethodValue : public Value {
  138. public:
  139. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  140. Nonnull<const Value*> receiver)
  141. : Value(Kind::BoundMethodValue),
  142. declaration_(declaration),
  143. receiver_(receiver) {}
  144. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  145. Nonnull<const Value*> receiver,
  146. const BindingMap& type_args,
  147. const std::map<Nonnull<const ImplBinding*>,
  148. Nonnull<const Witness*>>& wits)
  149. : Value(Kind::BoundMethodValue),
  150. declaration_(declaration),
  151. receiver_(receiver),
  152. type_args_(type_args),
  153. witnesses_(wits) {}
  154. static auto classof(const Value* value) -> bool {
  155. return value->kind() == Kind::BoundMethodValue;
  156. }
  157. auto declaration() const -> const FunctionDeclaration& {
  158. return *declaration_;
  159. }
  160. auto receiver() const -> Nonnull<const Value*> { return receiver_; }
  161. auto type_args() const -> const BindingMap& { return type_args_; }
  162. auto witnesses() const
  163. -> const std::map<Nonnull<const ImplBinding*>, Nonnull<const Witness*>>& {
  164. return witnesses_;
  165. }
  166. private:
  167. Nonnull<const FunctionDeclaration*> declaration_;
  168. Nonnull<const Value*> receiver_;
  169. BindingMap type_args_;
  170. std::map<Nonnull<const ImplBinding*>, Nonnull<const Witness*>> 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. 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 function value.
  283. class TupleValue : public Value {
  284. public:
  285. // An empty tuple, also known as the unit type.
  286. static auto Empty() -> Nonnull<const TupleValue*> {
  287. static const TupleValue empty =
  288. TupleValue(std::vector<Nonnull<const Value*>>());
  289. return Nonnull<const TupleValue*>(&empty);
  290. }
  291. explicit TupleValue(std::vector<Nonnull<const Value*>> elements)
  292. : Value(Kind::TupleValue), elements_(std::move(elements)) {}
  293. static auto classof(const Value* value) -> bool {
  294. return value->kind() == Kind::TupleValue;
  295. }
  296. auto elements() const -> llvm::ArrayRef<Nonnull<const Value*>> {
  297. return elements_;
  298. }
  299. private:
  300. std::vector<Nonnull<const Value*>> elements_;
  301. };
  302. // A binding placeholder value.
  303. class BindingPlaceholderValue : public Value {
  304. public:
  305. // Represents the `_` placeholder.
  306. explicit BindingPlaceholderValue() : Value(Kind::BindingPlaceholderValue) {}
  307. // Represents a named placeholder.
  308. explicit BindingPlaceholderValue(ValueNodeView value_node)
  309. : Value(Kind::BindingPlaceholderValue),
  310. value_node_(std::move(value_node)) {}
  311. static auto classof(const Value* value) -> bool {
  312. return value->kind() == Kind::BindingPlaceholderValue;
  313. }
  314. auto value_node() const -> const std::optional<ValueNodeView>& {
  315. return value_node_;
  316. }
  317. private:
  318. std::optional<ValueNodeView> value_node_;
  319. };
  320. // The int type.
  321. class IntType : public Value {
  322. public:
  323. IntType() : Value(Kind::IntType) {}
  324. static auto classof(const Value* value) -> bool {
  325. return value->kind() == Kind::IntType;
  326. }
  327. };
  328. // The bool type.
  329. class BoolType : public Value {
  330. public:
  331. BoolType() : Value(Kind::BoolType) {}
  332. static auto classof(const Value* value) -> bool {
  333. return value->kind() == Kind::BoolType;
  334. }
  335. };
  336. // A type type.
  337. class TypeType : public Value {
  338. public:
  339. TypeType() : Value(Kind::TypeType) {}
  340. static auto classof(const Value* value) -> bool {
  341. return value->kind() == Kind::TypeType;
  342. }
  343. };
  344. // A function type.
  345. class FunctionType : public Value {
  346. public:
  347. FunctionType(llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  348. Nonnull<const Value*> parameters,
  349. Nonnull<const Value*> return_type,
  350. llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings)
  351. : Value(Kind::FunctionType),
  352. deduced_(deduced),
  353. parameters_(parameters),
  354. return_type_(return_type),
  355. impl_bindings_(impl_bindings) {}
  356. static auto classof(const Value* value) -> bool {
  357. return value->kind() == Kind::FunctionType;
  358. }
  359. auto deduced() const -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  360. return deduced_;
  361. }
  362. auto parameters() const -> const Value& { return *parameters_; }
  363. auto return_type() const -> const Value& { return *return_type_; }
  364. // The bindings for the witness tables (impls) required by the
  365. // bounds on the type parameters of the generic function.
  366. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  367. return impl_bindings_;
  368. }
  369. private:
  370. std::vector<Nonnull<const GenericBinding*>> deduced_;
  371. Nonnull<const Value*> parameters_;
  372. Nonnull<const Value*> return_type_;
  373. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  374. };
  375. // A pointer type.
  376. class PointerType : public Value {
  377. public:
  378. explicit PointerType(Nonnull<const Value*> type)
  379. : Value(Kind::PointerType), type_(type) {}
  380. static auto classof(const Value* value) -> bool {
  381. return value->kind() == Kind::PointerType;
  382. }
  383. auto type() const -> const Value& { return *type_; }
  384. private:
  385. Nonnull<const Value*> type_;
  386. };
  387. // The `auto` type.
  388. class AutoType : public Value {
  389. public:
  390. AutoType() : Value(Kind::AutoType) {}
  391. static auto classof(const Value* value) -> bool {
  392. return value->kind() == Kind::AutoType;
  393. }
  394. };
  395. // A struct type.
  396. //
  397. // Code that handles this type may sometimes need to have special-case handling
  398. // for `{}`, which is a struct value in addition to being a struct type.
  399. class StructType : public Value {
  400. public:
  401. StructType() : StructType(std::vector<NamedValue>{}) {}
  402. explicit StructType(std::vector<NamedValue> fields)
  403. : Value(Kind::StructType), fields_(std::move(fields)) {}
  404. static auto classof(const Value* value) -> bool {
  405. return value->kind() == Kind::StructType;
  406. }
  407. auto fields() const -> llvm::ArrayRef<NamedValue> { return fields_; }
  408. private:
  409. std::vector<NamedValue> fields_;
  410. };
  411. // A class type.
  412. // TODO: Consider splitting this class into several classes.
  413. class NominalClassType : public Value {
  414. public:
  415. // Construct a non-generic class type or a generic class type that has
  416. // not yet been applied to type arguments.
  417. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration)
  418. : Value(Kind::NominalClassType), declaration_(declaration) {}
  419. // Construct a class type that represents the result of applying the
  420. // given generic class to the `type_args`.
  421. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration,
  422. const BindingMap& type_args)
  423. : Value(Kind::NominalClassType),
  424. declaration_(declaration),
  425. type_args_(type_args) {}
  426. // Construct a class type that represents the result of applying the
  427. // given generic class to the `type_args` and that records the result of the
  428. // compile-time search for any required impls.
  429. explicit NominalClassType(
  430. Nonnull<const ClassDeclaration*> declaration, const BindingMap& type_args,
  431. const std::map<Nonnull<const ImplBinding*>, ValueNodeView>& impls)
  432. : Value(Kind::NominalClassType),
  433. declaration_(declaration),
  434. type_args_(type_args),
  435. impls_(impls) {}
  436. // Construct a fully instantiated generic class type to represent the
  437. // run-time type of an object.
  438. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration,
  439. const BindingMap& type_args,
  440. const std::map<Nonnull<const ImplBinding*>,
  441. Nonnull<const Witness*>>& wits)
  442. : Value(Kind::NominalClassType),
  443. declaration_(declaration),
  444. type_args_(type_args),
  445. witnesses_(wits) {}
  446. static auto classof(const Value* value) -> bool {
  447. return value->kind() == Kind::NominalClassType;
  448. }
  449. auto declaration() const -> const ClassDeclaration& { return *declaration_; }
  450. auto type_args() const -> const BindingMap& { return type_args_; }
  451. // Maps each of the class's generic parameters to the AST node that
  452. // identifies the witness table for the corresponding argument.
  453. // Should not be called on 1) a non-generic class, 2) a generic-class
  454. // that is not instantiated, or 3) a fully instantiated runtime type
  455. // of a generic class.
  456. auto impls() const
  457. -> const std::map<Nonnull<const ImplBinding*>, ValueNodeView>& {
  458. return impls_;
  459. }
  460. // Maps each of the class's generic parameters to the witness table
  461. // for the corresponding argument. Should only be called on a fully
  462. // instantiated runtime type of a generic class.
  463. auto witnesses() const
  464. -> const std::map<Nonnull<const ImplBinding*>, Nonnull<const Witness*>>& {
  465. return witnesses_;
  466. }
  467. // Returns the value of the function named `name` in this class, or
  468. // nullopt if there is no such function.
  469. auto FindFunction(const std::string& name) const
  470. -> std::optional<Nonnull<const FunctionValue*>>;
  471. private:
  472. Nonnull<const ClassDeclaration*> declaration_;
  473. BindingMap type_args_;
  474. std::map<Nonnull<const ImplBinding*>, ValueNodeView> impls_;
  475. std::map<Nonnull<const ImplBinding*>, Nonnull<const Witness*>> witnesses_;
  476. };
  477. // Return the declaration of the member with the given name.
  478. auto FindMember(const std::string& name,
  479. llvm::ArrayRef<Nonnull<Declaration*>> members)
  480. -> std::optional<Nonnull<const Declaration*>>;
  481. // An interface type.
  482. class InterfaceType : public Value {
  483. public:
  484. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
  485. : Value(Kind::InterfaceType), declaration_(declaration) {}
  486. static auto classof(const Value* value) -> bool {
  487. return value->kind() == Kind::InterfaceType;
  488. }
  489. auto declaration() const -> const InterfaceDeclaration& {
  490. return *declaration_;
  491. }
  492. private:
  493. Nonnull<const InterfaceDeclaration*> declaration_;
  494. };
  495. // The witness table for an impl.
  496. class Witness : public Value {
  497. public:
  498. explicit Witness(Nonnull<const ImplDeclaration*> declaration)
  499. : Value(Kind::Witness), declaration_(declaration) {}
  500. static auto classof(const Value* value) -> bool {
  501. return value->kind() == Kind::Witness;
  502. }
  503. auto declaration() const -> const ImplDeclaration& { return *declaration_; }
  504. private:
  505. Nonnull<const ImplDeclaration*> declaration_;
  506. };
  507. // A choice type.
  508. class ChoiceType : public Value {
  509. public:
  510. ChoiceType(std::string name, std::vector<NamedValue> alternatives)
  511. : Value(Kind::ChoiceType),
  512. name_(std::move(name)),
  513. alternatives_(std::move(alternatives)) {}
  514. static auto classof(const Value* value) -> bool {
  515. return value->kind() == Kind::ChoiceType;
  516. }
  517. auto name() const -> const std::string& { return name_; }
  518. // Returns the parameter types of the alternative with the given name,
  519. // or nullopt if no such alternative is present.
  520. auto FindAlternative(std::string_view name) const
  521. -> std::optional<Nonnull<const Value*>>;
  522. private:
  523. std::string name_;
  524. std::vector<NamedValue> alternatives_;
  525. };
  526. // A continuation type.
  527. class ContinuationType : public Value {
  528. public:
  529. ContinuationType() : Value(Kind::ContinuationType) {}
  530. static auto classof(const Value* value) -> bool {
  531. return value->kind() == Kind::ContinuationType;
  532. }
  533. };
  534. // A variable type.
  535. class VariableType : public Value {
  536. public:
  537. explicit VariableType(Nonnull<const GenericBinding*> binding)
  538. : Value(Kind::VariableType), binding_(binding) {}
  539. static auto classof(const Value* value) -> bool {
  540. return value->kind() == Kind::VariableType;
  541. }
  542. auto binding() const -> const GenericBinding& { return *binding_; }
  543. private:
  544. Nonnull<const GenericBinding*> binding_;
  545. };
  546. // A first-class continuation representation of a fragment of the stack.
  547. // A continuation value behaves like a pointer to the underlying stack
  548. // fragment, which is exposed by `Stack()`.
  549. class ContinuationValue : public Value {
  550. public:
  551. class StackFragment {
  552. public:
  553. // Constructs an empty StackFragment.
  554. StackFragment() = default;
  555. // Requires *this to be empty, because by the time we're tearing down the
  556. // Arena, it's no longer safe to invoke ~Action.
  557. ~StackFragment();
  558. StackFragment(StackFragment&&) = delete;
  559. auto operator=(StackFragment&&) -> StackFragment& = delete;
  560. // Store the given partial todo stack in *this, which must currently be
  561. // empty. The stack is represented with the top of the stack at the
  562. // beginning of the vector, the reverse of the usual order.
  563. void StoreReversed(std::vector<std::unique_ptr<Action>> reversed_todo);
  564. // Restore the currently stored stack fragment to the top of `todo`,
  565. // leaving *this empty.
  566. void RestoreTo(Stack<std::unique_ptr<Action>>& todo);
  567. // Destroy the currently stored stack fragment.
  568. void Clear();
  569. void Print(llvm::raw_ostream& out) const;
  570. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  571. private:
  572. // The todo stack of a suspended continuation, starting with the top
  573. // Action.
  574. std::vector<std::unique_ptr<Action>> reversed_todo_;
  575. };
  576. explicit ContinuationValue(Nonnull<StackFragment*> stack)
  577. : Value(Kind::ContinuationValue), stack_(stack) {}
  578. static auto classof(const Value* value) -> bool {
  579. return value->kind() == Kind::ContinuationValue;
  580. }
  581. // The todo stack of the suspended continuation. Note that this provides
  582. // mutable access, even when *this is const, because of the reference-like
  583. // semantics of ContinuationValue.
  584. auto stack() const -> StackFragment& { return *stack_; }
  585. private:
  586. Nonnull<StackFragment*> stack_;
  587. };
  588. // The String type.
  589. class StringType : public Value {
  590. public:
  591. StringType() : Value(Kind::StringType) {}
  592. static auto classof(const Value* value) -> bool {
  593. return value->kind() == Kind::StringType;
  594. }
  595. };
  596. // A string value.
  597. class StringValue : public Value {
  598. public:
  599. explicit StringValue(std::string value)
  600. : Value(Kind::StringValue), value_(std::move(value)) {}
  601. static auto classof(const Value* value) -> bool {
  602. return value->kind() == Kind::StringValue;
  603. }
  604. auto value() const -> const std::string& { return value_; }
  605. private:
  606. std::string value_;
  607. };
  608. // The type of an expression whose value is a class type. Currently there is no
  609. // way to explicitly name such a type in Carbon code, but we are tentatively
  610. // using `typeof(ClassName)` as the debug-printing format, in anticipation of
  611. // something like that becoming valid Carbon syntax.
  612. class TypeOfClassType : public Value {
  613. public:
  614. explicit TypeOfClassType(Nonnull<const NominalClassType*> class_type)
  615. : Value(Kind::TypeOfClassType), class_type_(class_type) {}
  616. static auto classof(const Value* value) -> bool {
  617. return value->kind() == Kind::TypeOfClassType;
  618. }
  619. auto class_type() const -> const NominalClassType& { return *class_type_; }
  620. private:
  621. Nonnull<const NominalClassType*> class_type_;
  622. };
  623. class TypeOfInterfaceType : public Value {
  624. public:
  625. explicit TypeOfInterfaceType(Nonnull<const InterfaceType*> iface_type)
  626. : Value(Kind::TypeOfInterfaceType), iface_type_(iface_type) {}
  627. static auto classof(const Value* value) -> bool {
  628. return value->kind() == Kind::TypeOfInterfaceType;
  629. }
  630. auto interface_type() const -> const InterfaceType& { return *iface_type_; }
  631. private:
  632. Nonnull<const InterfaceType*> iface_type_;
  633. };
  634. // The type of an expression whose value is a choice type. Currently there is no
  635. // way to explicitly name such a type in Carbon code, but we are tentatively
  636. // using `typeof(ChoiceName)` as the debug-printing format, in anticipation of
  637. // something like that becoming valid Carbon syntax.
  638. class TypeOfChoiceType : public Value {
  639. public:
  640. explicit TypeOfChoiceType(Nonnull<const ChoiceType*> choice_type)
  641. : Value(Kind::TypeOfChoiceType), choice_type_(choice_type) {}
  642. static auto classof(const Value* value) -> bool {
  643. return value->kind() == Kind::TypeOfChoiceType;
  644. }
  645. auto choice_type() const -> const ChoiceType& { return *choice_type_; }
  646. private:
  647. Nonnull<const ChoiceType*> choice_type_;
  648. };
  649. // The type of a statically-sized array.
  650. //
  651. // Note that values of this type are represented as tuples.
  652. class StaticArrayType : public Value {
  653. public:
  654. // Constructs a statically-sized array type with the given element type and
  655. // size.
  656. StaticArrayType(Nonnull<const Value*> element_type, size_t size)
  657. : Value(Kind::StaticArrayType),
  658. element_type_(element_type),
  659. size_(size) {}
  660. static auto classof(const Value* value) -> bool {
  661. return value->kind() == Kind::StaticArrayType;
  662. }
  663. auto element_type() const -> const Value& { return *element_type_; }
  664. auto size() const -> size_t { return size_; }
  665. private:
  666. Nonnull<const Value*> element_type_;
  667. size_t size_;
  668. };
  669. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool;
  670. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool;
  671. } // namespace Carbon
  672. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_