value.h 18 KB

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