value.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. IntType,
  42. BoolType,
  43. TypeType,
  44. FunctionType,
  45. PointerType,
  46. AutoType,
  47. StructType,
  48. NominalClassType,
  49. ChoiceType,
  50. ContinuationType, // The type of a continuation.
  51. VariableType, // e.g., generic type parameters.
  52. BindingPlaceholderValue,
  53. AlternativeConstructorValue,
  54. ContinuationValue, // A first-class continuation value.
  55. StringType,
  56. StringValue,
  57. TypeOfClassType,
  58. TypeOfChoiceType,
  59. };
  60. Value(const Value&) = delete;
  61. auto operator=(const Value&) -> Value& = delete;
  62. void Print(llvm::raw_ostream& out) const;
  63. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  64. // Returns the sub-Value specified by `path`, which must be a valid field
  65. // path for *this.
  66. auto GetField(Nonnull<Arena*> arena, const FieldPath& path,
  67. SourceLocation source_loc) const -> Nonnull<const Value*>;
  68. // Returns a copy of *this, but with the sub-Value specified by `path`
  69. // set to `field_value`. `path` must be a valid field path for *this.
  70. auto SetField(Nonnull<Arena*> arena, const FieldPath& path,
  71. Nonnull<const Value*> field_value,
  72. SourceLocation source_loc) const -> Nonnull<const Value*>;
  73. // Returns the enumerator corresponding to the most-derived type of this
  74. // object.
  75. auto kind() const -> Kind { return kind_; }
  76. protected:
  77. // Constructs a Value. `kind` must be the enumerator corresponding to the
  78. // most-derived type being constructed.
  79. explicit Value(Kind kind) : kind_(kind) {}
  80. private:
  81. const Kind kind_;
  82. };
  83. // A NamedValue represents a value with a name, such as a single struct field.
  84. struct NamedValue {
  85. // The field name.
  86. std::string name;
  87. // The field's value.
  88. Nonnull<const Value*> value;
  89. };
  90. // An integer value.
  91. class IntValue : public Value {
  92. public:
  93. explicit IntValue(int value) : Value(Kind::IntValue), value_(value) {}
  94. static auto classof(const Value* value) -> bool {
  95. return value->kind() == Kind::IntValue;
  96. }
  97. auto value() const -> int { return value_; }
  98. private:
  99. int value_;
  100. };
  101. // A function value.
  102. class FunctionValue : public Value {
  103. public:
  104. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration)
  105. : Value(Kind::FunctionValue), declaration_(declaration) {}
  106. static auto classof(const Value* value) -> bool {
  107. return value->kind() == Kind::FunctionValue;
  108. }
  109. auto declaration() const -> const FunctionDeclaration& {
  110. return *declaration_;
  111. }
  112. private:
  113. Nonnull<const FunctionDeclaration*> declaration_;
  114. };
  115. // A bound method value. It includes the receiver object.
  116. class BoundMethodValue : public Value {
  117. public:
  118. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  119. Nonnull<const Value*> receiver)
  120. : Value(Kind::BoundMethodValue),
  121. declaration_(declaration),
  122. receiver_(receiver) {}
  123. static auto classof(const Value* value) -> bool {
  124. return value->kind() == Kind::BoundMethodValue;
  125. }
  126. auto declaration() const -> const FunctionDeclaration& {
  127. return *declaration_;
  128. }
  129. auto receiver() const -> Nonnull<const Value*> { return receiver_; }
  130. private:
  131. Nonnull<const FunctionDeclaration*> declaration_;
  132. Nonnull<const Value*> receiver_;
  133. };
  134. // The value of a location in memory.
  135. class LValue : public Value {
  136. public:
  137. explicit LValue(Address value)
  138. : Value(Kind::LValue), value_(std::move(value)) {}
  139. static auto classof(const Value* value) -> bool {
  140. return value->kind() == Kind::LValue;
  141. }
  142. auto address() const -> const Address& { return value_; }
  143. private:
  144. Address value_;
  145. };
  146. // A pointer value
  147. class PointerValue : public Value {
  148. public:
  149. explicit PointerValue(Address value)
  150. : Value(Kind::PointerValue), value_(std::move(value)) {}
  151. static auto classof(const Value* value) -> bool {
  152. return value->kind() == Kind::PointerValue;
  153. }
  154. auto address() const -> const Address& { return value_; }
  155. private:
  156. Address value_;
  157. };
  158. // A bool value.
  159. class BoolValue : public Value {
  160. public:
  161. explicit BoolValue(bool value) : Value(Kind::BoolValue), value_(value) {}
  162. static auto classof(const Value* value) -> bool {
  163. return value->kind() == Kind::BoolValue;
  164. }
  165. auto value() const -> bool { return value_; }
  166. private:
  167. bool value_;
  168. };
  169. // A non-empty value of a struct type.
  170. //
  171. // It can't be empty because `{}` is a struct type as well as a value of that
  172. // type, so for consistency we always represent it as a StructType rather than
  173. // let it oscillate unpredictably between the two. However, this means code
  174. // that handles StructValue instances may also need to be able to handle
  175. // StructType instances.
  176. class StructValue : public Value {
  177. public:
  178. explicit StructValue(std::vector<NamedValue> elements)
  179. : Value(Kind::StructValue), elements_(std::move(elements)) {
  180. CHECK(!elements_.empty())
  181. << "`{}` is represented as a StructType, not a StructValue.";
  182. }
  183. static auto classof(const Value* value) -> bool {
  184. return value->kind() == Kind::StructValue;
  185. }
  186. auto elements() const -> llvm::ArrayRef<NamedValue> { return elements_; }
  187. // Returns the value of the field named `name` in this struct, or
  188. // nullopt if there is no such field.
  189. auto FindField(const std::string& name) const
  190. -> std::optional<Nonnull<const Value*>>;
  191. private:
  192. std::vector<NamedValue> elements_;
  193. };
  194. // A value of a nominal class type, i.e., an object.
  195. class NominalClassValue : public Value {
  196. public:
  197. NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits)
  198. : Value(Kind::NominalClassValue), type_(type), inits_(inits) {}
  199. static auto classof(const Value* value) -> bool {
  200. return value->kind() == Kind::NominalClassValue;
  201. }
  202. auto type() const -> const Value& { return *type_; }
  203. auto inits() const -> const Value& { return *inits_; }
  204. private:
  205. Nonnull<const Value*> type_;
  206. Nonnull<const Value*> inits_; // The initializing StructValue.
  207. };
  208. // An alternative constructor value.
  209. class AlternativeConstructorValue : public Value {
  210. public:
  211. AlternativeConstructorValue(std::string alt_name, std::string choice_name)
  212. : Value(Kind::AlternativeConstructorValue),
  213. alt_name_(std::move(alt_name)),
  214. choice_name_(std::move(choice_name)) {}
  215. static auto classof(const Value* value) -> bool {
  216. return value->kind() == Kind::AlternativeConstructorValue;
  217. }
  218. auto alt_name() const -> const std::string& { return alt_name_; }
  219. auto choice_name() const -> const std::string& { return choice_name_; }
  220. private:
  221. std::string alt_name_;
  222. std::string choice_name_;
  223. };
  224. // An alternative value.
  225. class AlternativeValue : public Value {
  226. public:
  227. AlternativeValue(std::string alt_name, std::string choice_name,
  228. Nonnull<const Value*> argument)
  229. : Value(Kind::AlternativeValue),
  230. alt_name_(std::move(alt_name)),
  231. choice_name_(std::move(choice_name)),
  232. argument_(argument) {}
  233. static auto classof(const Value* value) -> bool {
  234. return value->kind() == Kind::AlternativeValue;
  235. }
  236. auto alt_name() const -> const std::string& { return alt_name_; }
  237. auto choice_name() const -> const std::string& { return choice_name_; }
  238. auto argument() const -> const Value& { return *argument_; }
  239. private:
  240. std::string alt_name_;
  241. std::string choice_name_;
  242. Nonnull<const Value*> argument_;
  243. };
  244. // A function value.
  245. class TupleValue : public Value {
  246. public:
  247. // An empty tuple, also known as the unit type.
  248. static auto Empty() -> Nonnull<const TupleValue*> {
  249. static const TupleValue empty =
  250. TupleValue(std::vector<Nonnull<const Value*>>());
  251. return Nonnull<const TupleValue*>(&empty);
  252. }
  253. explicit TupleValue(std::vector<Nonnull<const Value*>> elements)
  254. : Value(Kind::TupleValue), elements_(std::move(elements)) {}
  255. static auto classof(const Value* value) -> bool {
  256. return value->kind() == Kind::TupleValue;
  257. }
  258. auto elements() const -> llvm::ArrayRef<Nonnull<const Value*>> {
  259. return elements_;
  260. }
  261. private:
  262. std::vector<Nonnull<const Value*>> elements_;
  263. };
  264. // A binding placeholder value.
  265. class BindingPlaceholderValue : public Value {
  266. public:
  267. // Represents the `_` placeholder.
  268. explicit BindingPlaceholderValue() : Value(Kind::BindingPlaceholderValue) {}
  269. // Represents a named placeholder.
  270. explicit BindingPlaceholderValue(NamedEntityView named_entity)
  271. : Value(Kind::BindingPlaceholderValue),
  272. named_entity_(std::move(named_entity)) {}
  273. static auto classof(const Value* value) -> bool {
  274. return value->kind() == Kind::BindingPlaceholderValue;
  275. }
  276. auto named_entity() const -> const std::optional<NamedEntityView>& {
  277. return named_entity_;
  278. }
  279. private:
  280. std::optional<NamedEntityView> named_entity_;
  281. };
  282. // The int type.
  283. class IntType : public Value {
  284. public:
  285. IntType() : Value(Kind::IntType) {}
  286. static auto classof(const Value* value) -> bool {
  287. return value->kind() == Kind::IntType;
  288. }
  289. };
  290. // The bool type.
  291. class BoolType : public Value {
  292. public:
  293. BoolType() : Value(Kind::BoolType) {}
  294. static auto classof(const Value* value) -> bool {
  295. return value->kind() == Kind::BoolType;
  296. }
  297. };
  298. // A type type.
  299. class TypeType : public Value {
  300. public:
  301. TypeType() : Value(Kind::TypeType) {}
  302. static auto classof(const Value* value) -> bool {
  303. return value->kind() == Kind::TypeType;
  304. }
  305. };
  306. // A function type.
  307. class FunctionType : public Value {
  308. public:
  309. FunctionType(llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
  310. Nonnull<const Value*> parameters,
  311. Nonnull<const Value*> return_type)
  312. : Value(Kind::FunctionType),
  313. deduced_(deduced),
  314. parameters_(parameters),
  315. return_type_(return_type) {}
  316. static auto classof(const Value* value) -> bool {
  317. return value->kind() == Kind::FunctionType;
  318. }
  319. auto deduced() const -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  320. return deduced_;
  321. }
  322. auto parameters() const -> const Value& { return *parameters_; }
  323. auto return_type() const -> const Value& { return *return_type_; }
  324. private:
  325. std::vector<Nonnull<const GenericBinding*>> deduced_;
  326. Nonnull<const Value*> parameters_;
  327. Nonnull<const Value*> return_type_;
  328. };
  329. // A pointer type.
  330. class PointerType : public Value {
  331. public:
  332. explicit PointerType(Nonnull<const Value*> type)
  333. : Value(Kind::PointerType), type_(type) {}
  334. static auto classof(const Value* value) -> bool {
  335. return value->kind() == Kind::PointerType;
  336. }
  337. auto type() const -> const Value& { return *type_; }
  338. private:
  339. Nonnull<const Value*> type_;
  340. };
  341. // The `auto` type.
  342. class AutoType : public Value {
  343. public:
  344. AutoType() : Value(Kind::AutoType) {}
  345. static auto classof(const Value* value) -> bool {
  346. return value->kind() == Kind::AutoType;
  347. }
  348. };
  349. // A struct type.
  350. //
  351. // Code that handles this type may sometimes need to have special-case handling
  352. // for `{}`, which is a struct value in addition to being a struct type.
  353. class StructType : public Value {
  354. public:
  355. StructType() : StructType(std::vector<NamedValue>{}) {}
  356. explicit StructType(std::vector<NamedValue> fields)
  357. : Value(Kind::StructType), fields_(std::move(fields)) {}
  358. static auto classof(const Value* value) -> bool {
  359. return value->kind() == Kind::StructType;
  360. }
  361. auto fields() const -> llvm::ArrayRef<NamedValue> { return fields_; }
  362. private:
  363. std::vector<NamedValue> fields_;
  364. };
  365. // A class type.
  366. class NominalClassType : public Value {
  367. public:
  368. NominalClassType(Nonnull<const ClassDeclaration*> declaration)
  369. : Value(Kind::NominalClassType), declaration_(declaration) {}
  370. static auto classof(const Value* value) -> bool {
  371. return value->kind() == Kind::NominalClassType;
  372. }
  373. auto declaration() const -> const ClassDeclaration& { return *declaration_; }
  374. // Return the declaration of the member with the given name.
  375. auto FindMember(const std::string& name) const
  376. -> std::optional<Nonnull<const Declaration*>>;
  377. // Returns the value of the function named `name` in this class, or
  378. // nullopt if there is no such function.
  379. auto FindFunction(const std::string& name) const
  380. -> std::optional<Nonnull<const FunctionValue*>>;
  381. private:
  382. Nonnull<const ClassDeclaration*> declaration_;
  383. };
  384. auto FieldTypes(const NominalClassType&) -> std::vector<NamedValue>;
  385. // A choice type.
  386. class ChoiceType : public Value {
  387. public:
  388. ChoiceType(std::string name, std::vector<NamedValue> alternatives)
  389. : Value(Kind::ChoiceType),
  390. name_(std::move(name)),
  391. alternatives_(std::move(alternatives)) {}
  392. static auto classof(const Value* value) -> bool {
  393. return value->kind() == Kind::ChoiceType;
  394. }
  395. auto name() const -> const std::string& { return name_; }
  396. // Returns the parameter types of the alternative with the given name,
  397. // or nullopt if no such alternative is present.
  398. auto FindAlternative(std::string_view name) const
  399. -> std::optional<Nonnull<const Value*>>;
  400. private:
  401. std::string name_;
  402. std::vector<NamedValue> alternatives_;
  403. };
  404. // A continuation type.
  405. class ContinuationType : public Value {
  406. public:
  407. ContinuationType() : Value(Kind::ContinuationType) {}
  408. static auto classof(const Value* value) -> bool {
  409. return value->kind() == Kind::ContinuationType;
  410. }
  411. };
  412. // A variable type.
  413. class VariableType : public Value {
  414. public:
  415. explicit VariableType(Nonnull<const GenericBinding*> binding)
  416. : Value(Kind::VariableType), binding_(binding) {}
  417. static auto classof(const Value* value) -> bool {
  418. return value->kind() == Kind::VariableType;
  419. }
  420. auto binding() const -> const GenericBinding& { return *binding_; }
  421. private:
  422. Nonnull<const GenericBinding*> binding_;
  423. };
  424. // A first-class continuation representation of a fragment of the stack.
  425. // A continuation value behaves like a pointer to the underlying stack
  426. // fragment, which is exposed by `Stack()`.
  427. class ContinuationValue : public Value {
  428. public:
  429. class StackFragment {
  430. public:
  431. // Constructs an empty StackFragment.
  432. StackFragment() = default;
  433. // Requires *this to be empty, because by the time we're tearing down the
  434. // Arena, it's no longer safe to invoke ~Action.
  435. ~StackFragment();
  436. StackFragment(StackFragment&&) = delete;
  437. auto operator=(StackFragment&&) -> StackFragment& = delete;
  438. // Store the given partial todo stack in *this, which must currently be
  439. // empty. The stack is represented with the top of the stack at the
  440. // beginning of the vector, the reverse of the usual order.
  441. void StoreReversed(std::vector<std::unique_ptr<Action>> reversed_todo);
  442. // Restore the currently stored stack fragment to the top of `todo`,
  443. // leaving *this empty.
  444. void RestoreTo(Stack<std::unique_ptr<Action>>& todo);
  445. // Destroy the currently stored stack fragment.
  446. void Clear();
  447. void Print(llvm::raw_ostream& out) const;
  448. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  449. private:
  450. // The todo stack of a suspended continuation, starting with the top
  451. // Action.
  452. std::vector<std::unique_ptr<Action>> reversed_todo_;
  453. };
  454. explicit ContinuationValue(Nonnull<StackFragment*> stack)
  455. : Value(Kind::ContinuationValue), stack_(stack) {}
  456. static auto classof(const Value* value) -> bool {
  457. return value->kind() == Kind::ContinuationValue;
  458. }
  459. // The todo stack of the suspended continuation. Note that this provides
  460. // mutable access, even when *this is const, because of the reference-like
  461. // semantics of ContinuationValue.
  462. auto stack() const -> StackFragment& { return *stack_; }
  463. private:
  464. Nonnull<StackFragment*> stack_;
  465. };
  466. // The String type.
  467. class StringType : public Value {
  468. public:
  469. StringType() : Value(Kind::StringType) {}
  470. static auto classof(const Value* value) -> bool {
  471. return value->kind() == Kind::StringType;
  472. }
  473. };
  474. // A string value.
  475. class StringValue : public Value {
  476. public:
  477. explicit StringValue(std::string value)
  478. : Value(Kind::StringValue), value_(std::move(value)) {}
  479. static auto classof(const Value* value) -> bool {
  480. return value->kind() == Kind::StringValue;
  481. }
  482. auto value() const -> const std::string& { return value_; }
  483. private:
  484. std::string value_;
  485. };
  486. // The type of an expression whose value is a class type. Currently there is no
  487. // way to explicitly name such a type in Carbon code, but we are tentatively
  488. // using `typeof(ClassName)` as the debug-printing format, in anticipation of
  489. // something like that becoming valid Carbon syntax.
  490. class TypeOfClassType : public Value {
  491. public:
  492. explicit TypeOfClassType(Nonnull<const NominalClassType*> class_type)
  493. : Value(Kind::TypeOfClassType), class_type_(class_type) {}
  494. static auto classof(const Value* value) -> bool {
  495. return value->kind() == Kind::TypeOfClassType;
  496. }
  497. auto class_type() const -> const NominalClassType& { return *class_type_; }
  498. private:
  499. Nonnull<const NominalClassType*> class_type_;
  500. };
  501. // The type of an expression whose value is a choice type. Currently there is no
  502. // way to explicitly name such a type in Carbon code, but we are tentatively
  503. // using `typeof(ChoiceName)` as the debug-printing format, in anticipation of
  504. // something like that becoming valid Carbon syntax.
  505. class TypeOfChoiceType : public Value {
  506. public:
  507. explicit TypeOfChoiceType(Nonnull<const ChoiceType*> choice_type)
  508. : Value(Kind::TypeOfChoiceType), choice_type_(choice_type) {}
  509. static auto classof(const Value* value) -> bool {
  510. return value->kind() == Kind::TypeOfChoiceType;
  511. }
  512. auto choice_type() const -> const ChoiceType& { return *choice_type_; }
  513. private:
  514. Nonnull<const ChoiceType*> choice_type_;
  515. };
  516. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool;
  517. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool;
  518. } // namespace Carbon
  519. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_