value.h 15 KB

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