value.h 18 KB

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