value.h 15 KB

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