value.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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(std::string name, Nonnull<const Value*> parameters,
  108. std::optional<Nonnull<const Statement*>> body)
  109. : Value(Kind::FunctionValue),
  110. name_(std::move(name)),
  111. parameters_(parameters),
  112. body_(body) {}
  113. static auto classof(const Value* value) -> bool {
  114. return value->kind() == Kind::FunctionValue;
  115. }
  116. auto name() const -> const std::string& { return name_; }
  117. auto parameters() const -> const Value& { return *parameters_; }
  118. auto body() const -> std::optional<Nonnull<const Statement*>> {
  119. return body_;
  120. }
  121. private:
  122. std::string name_;
  123. Nonnull<const Value*> parameters_;
  124. std::optional<Nonnull<const Statement*>> body_;
  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 value() 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<StructElement> 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 -> const std::vector<StructElement>& {
  167. return elements_;
  168. }
  169. // Returns the value of the field named `name` in this struct, or
  170. // nullopt if there is no such field.
  171. auto FindField(const std::string& name) const
  172. -> std::optional<Nonnull<const Value*>>;
  173. private:
  174. std::vector<StructElement> elements_;
  175. };
  176. // A value of a nominal class type.
  177. class NominalClassValue : public Value {
  178. public:
  179. NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits)
  180. : Value(Kind::NominalClassValue), type_(type), inits_(inits) {}
  181. static auto classof(const Value* value) -> bool {
  182. return value->kind() == Kind::NominalClassValue;
  183. }
  184. auto type() const -> const Value& { return *type_; }
  185. auto inits() const -> const Value& { return *inits_; }
  186. private:
  187. Nonnull<const Value*> type_;
  188. Nonnull<const Value*> inits_;
  189. };
  190. // An alternative constructor value.
  191. class AlternativeConstructorValue : public Value {
  192. public:
  193. AlternativeConstructorValue(std::string alt_name, std::string choice_name)
  194. : Value(Kind::AlternativeConstructorValue),
  195. alt_name_(std::move(alt_name)),
  196. choice_name_(std::move(choice_name)) {}
  197. static auto classof(const Value* value) -> bool {
  198. return value->kind() == Kind::AlternativeConstructorValue;
  199. }
  200. auto alt_name() const -> const std::string& { return alt_name_; }
  201. auto choice_name() const -> const std::string& { return choice_name_; }
  202. private:
  203. std::string alt_name_;
  204. std::string choice_name_;
  205. };
  206. // An alternative value.
  207. class AlternativeValue : public Value {
  208. public:
  209. AlternativeValue(std::string alt_name, std::string choice_name,
  210. Nonnull<const Value*> argument)
  211. : Value(Kind::AlternativeValue),
  212. alt_name_(std::move(alt_name)),
  213. choice_name_(std::move(choice_name)),
  214. argument_(argument) {}
  215. static auto classof(const Value* value) -> bool {
  216. return value->kind() == Kind::AlternativeValue;
  217. }
  218. auto alt_name() const -> const std::string& { return alt_name_; }
  219. auto choice_name() const -> const std::string& { return choice_name_; }
  220. auto argument() const -> const Value& { return *argument_; }
  221. private:
  222. std::string alt_name_;
  223. std::string choice_name_;
  224. Nonnull<const Value*> argument_;
  225. };
  226. // A function value.
  227. class TupleValue : public Value {
  228. public:
  229. // An empty tuple, also known as the unit type.
  230. static auto Empty() -> Nonnull<const TupleValue*> {
  231. static const TupleValue empty =
  232. TupleValue(std::vector<Nonnull<const Value*>>());
  233. return Nonnull<const TupleValue*>(&empty);
  234. }
  235. explicit TupleValue(std::vector<Nonnull<const Value*>> elements)
  236. : Value(Kind::TupleValue), elements_(std::move(elements)) {}
  237. static auto classof(const Value* value) -> bool {
  238. return value->kind() == Kind::TupleValue;
  239. }
  240. auto elements() const -> llvm::ArrayRef<Nonnull<const Value*>> {
  241. return elements_;
  242. }
  243. private:
  244. std::vector<Nonnull<const Value*>> elements_;
  245. };
  246. // A binding placeholder value.
  247. class BindingPlaceholderValue : public Value {
  248. public:
  249. // nullopt represents the `_` placeholder.
  250. BindingPlaceholderValue(std::optional<std::string> name,
  251. Nonnull<const Value*> type)
  252. : Value(Kind::BindingPlaceholderValue),
  253. name_(std::move(name)),
  254. type_(type) {}
  255. static auto classof(const Value* value) -> bool {
  256. return value->kind() == Kind::BindingPlaceholderValue;
  257. }
  258. auto name() const -> const std::optional<std::string>& { return name_; }
  259. auto type() const -> const Value& { return *type_; }
  260. private:
  261. std::optional<std::string> name_;
  262. Nonnull<const Value*> type_;
  263. };
  264. // The int type.
  265. class IntType : public Value {
  266. public:
  267. IntType() : Value(Kind::IntType) {}
  268. static auto classof(const Value* value) -> bool {
  269. return value->kind() == Kind::IntType;
  270. }
  271. };
  272. // The bool type.
  273. class BoolType : public Value {
  274. public:
  275. BoolType() : Value(Kind::BoolType) {}
  276. static auto classof(const Value* value) -> bool {
  277. return value->kind() == Kind::BoolType;
  278. }
  279. };
  280. // A type type.
  281. class TypeType : public Value {
  282. public:
  283. TypeType() : Value(Kind::TypeType) {}
  284. static auto classof(const Value* value) -> bool {
  285. return value->kind() == Kind::TypeType;
  286. }
  287. };
  288. // A function type.
  289. class FunctionType : public Value {
  290. public:
  291. FunctionType(std::vector<GenericBinding> deduced,
  292. Nonnull<const Value*> parameters,
  293. Nonnull<const Value*> return_type)
  294. : Value(Kind::FunctionType),
  295. deduced_(std::move(deduced)),
  296. parameters_(parameters),
  297. return_type_(return_type) {}
  298. static auto classof(const Value* value) -> bool {
  299. return value->kind() == Kind::FunctionType;
  300. }
  301. auto deduced() const -> llvm::ArrayRef<GenericBinding> { return deduced_; }
  302. auto parameters() const -> const Value& { return *parameters_; }
  303. auto return_type() const -> const Value& { return *return_type_; }
  304. private:
  305. std::vector<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(VarValues{}) {}
  336. explicit StructType(VarValues 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 -> const VarValues& { return fields_; }
  342. private:
  343. VarValues fields_;
  344. };
  345. // A class type.
  346. class NominalClassType : public Value {
  347. public:
  348. NominalClassType(std::string name, VarValues fields, VarValues methods)
  349. : Value(Kind::NominalClassType),
  350. name_(std::move(name)),
  351. fields_(std::move(fields)),
  352. methods_(std::move(methods)) {}
  353. static auto classof(const Value* value) -> bool {
  354. return value->kind() == Kind::NominalClassType;
  355. }
  356. auto name() const -> const std::string& { return name_; }
  357. auto fields() const -> const VarValues& { return fields_; }
  358. auto methods() const -> const VarValues& { return methods_; }
  359. private:
  360. std::string name_;
  361. VarValues fields_;
  362. VarValues methods_;
  363. };
  364. // A choice type.
  365. class ChoiceType : public Value {
  366. public:
  367. ChoiceType(std::string name, VarValues alternatives)
  368. : Value(Kind::ChoiceType),
  369. name_(std::move(name)),
  370. alternatives_(std::move(alternatives)) {}
  371. static auto classof(const Value* value) -> bool {
  372. return value->kind() == Kind::ChoiceType;
  373. }
  374. auto name() const -> const std::string& { return name_; }
  375. auto alternatives() const -> const VarValues& { return alternatives_; }
  376. private:
  377. std::string name_;
  378. VarValues alternatives_;
  379. };
  380. // A continuation type.
  381. class ContinuationType : public Value {
  382. public:
  383. ContinuationType() : Value(Kind::ContinuationType) {}
  384. static auto classof(const Value* value) -> bool {
  385. return value->kind() == Kind::ContinuationType;
  386. }
  387. };
  388. // A variable type.
  389. class VariableType : public Value {
  390. public:
  391. explicit VariableType(std::string name)
  392. : Value(Kind::VariableType), name_(std::move(name)) {}
  393. static auto classof(const Value* value) -> bool {
  394. return value->kind() == Kind::VariableType;
  395. }
  396. auto name() const -> const std::string& { return name_; }
  397. private:
  398. std::string name_;
  399. };
  400. // A first-class continuation representation of a fragment of the stack.
  401. // A continuation value behaves like a pointer to the underlying stack
  402. // fragment, which is exposed by `Stack()`.
  403. class ContinuationValue : public Value {
  404. public:
  405. explicit ContinuationValue(Nonnull<std::vector<Nonnull<Frame*>>*> stack)
  406. : Value(Kind::ContinuationValue), stack_(stack) {}
  407. static auto classof(const Value* value) -> bool {
  408. return value->kind() == Kind::ContinuationValue;
  409. }
  410. // The call stack of the suspended continuation, starting with the top
  411. // frame (the reverse of the usual order). Note that this provides mutable
  412. // access, even when *this is const, because of the reference-like semantics
  413. // of ContinuationValue.
  414. auto stack() const -> std::vector<Nonnull<Frame*>>& { return *stack_; }
  415. private:
  416. Nonnull<std::vector<Nonnull<Frame*>>*> stack_;
  417. };
  418. // The String type.
  419. class StringType : public Value {
  420. public:
  421. StringType() : Value(Kind::StringType) {}
  422. static auto classof(const Value* value) -> bool {
  423. return value->kind() == Kind::StringType;
  424. }
  425. };
  426. // A string value.
  427. class StringValue : public Value {
  428. public:
  429. explicit StringValue(std::string value)
  430. : Value(Kind::StringValue), value_(std::move(value)) {}
  431. static auto classof(const Value* value) -> bool {
  432. return value->kind() == Kind::StringValue;
  433. }
  434. auto value() const -> const std::string& { return value_; }
  435. private:
  436. std::string value_;
  437. };
  438. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool;
  439. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
  440. SourceLocation source_loc) -> bool;
  441. } // namespace Carbon
  442. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_