value.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/function_definition.h"
  12. #include "executable_semantics/ast/statement.h"
  13. #include "executable_semantics/common/ptr.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. AlternativeValue,
  36. TupleValue,
  37. IntType,
  38. BoolType,
  39. TypeType,
  40. FunctionType,
  41. PointerType,
  42. AutoType,
  43. ClassType,
  44. ChoiceType,
  45. ContinuationType, // The type of a continuation.
  46. VariableType, // e.g., generic type parameters.
  47. BindingPlaceholderValue,
  48. AlternativeConstructorValue,
  49. ContinuationValue, // A first-class continuation value.
  50. StringType,
  51. StringValue,
  52. };
  53. Value(const Value&) = delete;
  54. Value& operator=(const Value&) = delete;
  55. // Returns the enumerator corresponding to the most-derived type of this
  56. // object.
  57. auto Tag() const -> Kind { return tag; }
  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(const FieldPath& path, SourceLocation loc) const
  63. -> Ptr<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(const FieldPath& path, Ptr<const Value> field_value,
  67. SourceLocation loc) const -> Ptr<const Value>;
  68. protected:
  69. // Constructs a Value. `tag` must be the enumerator corresponding to the
  70. // most-derived type being constructed.
  71. explicit Value(Kind tag) : tag(tag) {}
  72. private:
  73. const Kind tag;
  74. };
  75. using VarValues = std::vector<std::pair<std::string, Ptr<const Value>>>;
  76. auto FindInVarValues(const std::string& field, const VarValues& inits)
  77. -> std::optional<Ptr<const Value>>;
  78. auto FieldsEqual(const VarValues& ts1, const VarValues& ts2) -> bool;
  79. // A TupleElement represents the value of a single tuple field.
  80. struct TupleElement {
  81. // The field name.
  82. std::string name;
  83. // The field's value.
  84. Ptr<const Value> value;
  85. };
  86. struct Frame; // Used by continuation.
  87. // An integer value.
  88. class IntValue : public Value {
  89. public:
  90. explicit IntValue(int val) : Value(Kind::IntValue), val(val) {}
  91. static auto classof(const Value* value) -> bool {
  92. return value->Tag() == Kind::IntValue;
  93. }
  94. auto Val() const -> int { return val; }
  95. private:
  96. int val;
  97. };
  98. // A function value.
  99. class FunctionValue : public Value {
  100. public:
  101. FunctionValue(std::string name, Ptr<const Value> param,
  102. std::optional<Ptr<const Statement>> body)
  103. : Value(Kind::FunctionValue),
  104. name(std::move(name)),
  105. param(param),
  106. body(body) {}
  107. static auto classof(const Value* value) -> bool {
  108. return value->Tag() == Kind::FunctionValue;
  109. }
  110. auto Name() const -> const std::string& { return name; }
  111. auto Param() const -> Ptr<const Value> { return param; }
  112. auto Body() const -> std::optional<Ptr<const Statement>> { return body; }
  113. private:
  114. std::string name;
  115. Ptr<const Value> param;
  116. std::optional<Ptr<const Statement>> body;
  117. };
  118. // A pointer value.
  119. class PointerValue : public Value {
  120. public:
  121. explicit PointerValue(Address val)
  122. : Value(Kind::PointerValue), val(std::move(val)) {}
  123. static auto classof(const Value* value) -> bool {
  124. return value->Tag() == Kind::PointerValue;
  125. }
  126. auto Val() const -> const Address& { return val; }
  127. private:
  128. Address val;
  129. };
  130. // A bool value.
  131. class BoolValue : public Value {
  132. public:
  133. explicit BoolValue(bool val) : Value(Kind::BoolValue), val(val) {}
  134. static auto classof(const Value* value) -> bool {
  135. return value->Tag() == Kind::BoolValue;
  136. }
  137. auto Val() const -> bool { return val; }
  138. private:
  139. bool val;
  140. };
  141. // A function value.
  142. class StructValue : public Value {
  143. public:
  144. StructValue(Ptr<const Value> type, Ptr<const Value> inits)
  145. : Value(Kind::StructValue), type(type), inits(inits) {}
  146. static auto classof(const Value* value) -> bool {
  147. return value->Tag() == Kind::StructValue;
  148. }
  149. auto Type() const -> Ptr<const Value> { return type; }
  150. auto Inits() const -> Ptr<const Value> { return inits; }
  151. private:
  152. Ptr<const Value> type;
  153. Ptr<const Value> inits;
  154. };
  155. // An alternative constructor value.
  156. class AlternativeConstructorValue : public Value {
  157. public:
  158. AlternativeConstructorValue(std::string alt_name, std::string choice_name)
  159. : Value(Kind::AlternativeConstructorValue),
  160. alt_name(std::move(alt_name)),
  161. choice_name(std::move(choice_name)) {}
  162. static auto classof(const Value* value) -> bool {
  163. return value->Tag() == Kind::AlternativeConstructorValue;
  164. }
  165. auto AltName() const -> const std::string& { return alt_name; }
  166. auto ChoiceName() const -> const std::string& { return choice_name; }
  167. private:
  168. std::string alt_name;
  169. std::string choice_name;
  170. };
  171. // An alternative value.
  172. class AlternativeValue : public Value {
  173. public:
  174. AlternativeValue(std::string alt_name, std::string choice_name,
  175. Ptr<const Value> argument)
  176. : Value(Kind::AlternativeValue),
  177. alt_name(std::move(alt_name)),
  178. choice_name(std::move(choice_name)),
  179. argument(argument) {}
  180. static auto classof(const Value* value) -> bool {
  181. return value->Tag() == Kind::AlternativeValue;
  182. }
  183. auto AltName() const -> const std::string& { return alt_name; }
  184. auto ChoiceName() const -> const std::string& { return choice_name; }
  185. auto Argument() const -> Ptr<const Value> { return argument; }
  186. private:
  187. std::string alt_name;
  188. std::string choice_name;
  189. Ptr<const Value> argument;
  190. };
  191. // A function value.
  192. class TupleValue : public Value {
  193. public:
  194. // An empty tuple, also known as the unit type.
  195. static Ptr<const TupleValue> Empty() {
  196. static const TupleValue empty = TupleValue(std::vector<TupleElement>());
  197. return Ptr<const TupleValue>(&empty);
  198. }
  199. explicit TupleValue(std::vector<TupleElement> elements)
  200. : Value(Kind::TupleValue), elements(std::move(elements)) {}
  201. static auto classof(const Value* value) -> bool {
  202. return value->Tag() == Kind::TupleValue;
  203. }
  204. auto Elements() const -> const std::vector<TupleElement>& { return elements; }
  205. // Returns the value of the field named `name` in this tuple, or
  206. // nullopt if there is no such field.
  207. auto FindField(const std::string& name) const
  208. -> std::optional<Ptr<const Value>>;
  209. private:
  210. std::vector<TupleElement> elements;
  211. };
  212. // A binding placeholder value.
  213. class BindingPlaceholderValue : public Value {
  214. public:
  215. // nullopt represents the `_` placeholder.
  216. BindingPlaceholderValue(std::optional<std::string> name,
  217. Ptr<const Value> type)
  218. : Value(Kind::BindingPlaceholderValue),
  219. name(std::move(name)),
  220. type(type) {}
  221. static auto classof(const Value* value) -> bool {
  222. return value->Tag() == Kind::BindingPlaceholderValue;
  223. }
  224. auto Name() const -> const std::optional<std::string>& { return name; }
  225. auto Type() const -> Ptr<const Value> { return type; }
  226. private:
  227. std::optional<std::string> name;
  228. Ptr<const Value> type;
  229. };
  230. // The int type.
  231. class IntType : public Value {
  232. public:
  233. IntType() : Value(Kind::IntType) {}
  234. static auto classof(const Value* value) -> bool {
  235. return value->Tag() == Kind::IntType;
  236. }
  237. };
  238. // The bool type.
  239. class BoolType : public Value {
  240. public:
  241. BoolType() : Value(Kind::BoolType) {}
  242. static auto classof(const Value* value) -> bool {
  243. return value->Tag() == Kind::BoolType;
  244. }
  245. };
  246. // A type type.
  247. class TypeType : public Value {
  248. public:
  249. TypeType() : Value(Kind::TypeType) {}
  250. static auto classof(const Value* value) -> bool {
  251. return value->Tag() == Kind::TypeType;
  252. }
  253. };
  254. // A function type.
  255. class FunctionType : public Value {
  256. public:
  257. FunctionType(std::vector<GenericBinding> deduced, Ptr<const Value> param,
  258. Ptr<const Value> ret)
  259. : Value(Kind::FunctionType),
  260. deduced(std::move(deduced)),
  261. param(param),
  262. ret(ret) {}
  263. static auto classof(const Value* value) -> bool {
  264. return value->Tag() == Kind::FunctionType;
  265. }
  266. auto Deduced() const -> const std::vector<GenericBinding>& { return deduced; }
  267. auto Param() const -> Ptr<const Value> { return param; }
  268. auto Ret() const -> Ptr<const Value> { return ret; }
  269. private:
  270. std::vector<GenericBinding> deduced;
  271. Ptr<const Value> param;
  272. Ptr<const Value> ret;
  273. };
  274. // A pointer type.
  275. class PointerType : public Value {
  276. public:
  277. explicit PointerType(Ptr<const Value> type)
  278. : Value(Kind::PointerType), type(type) {}
  279. static auto classof(const Value* value) -> bool {
  280. return value->Tag() == Kind::PointerType;
  281. }
  282. auto Type() const -> Ptr<const Value> { return type; }
  283. private:
  284. Ptr<const Value> type;
  285. };
  286. // The `auto` type.
  287. class AutoType : public Value {
  288. public:
  289. AutoType() : Value(Kind::AutoType) {}
  290. static auto classof(const Value* value) -> bool {
  291. return value->Tag() == Kind::AutoType;
  292. }
  293. };
  294. // A struct type.
  295. class ClassType : public Value {
  296. public:
  297. ClassType(std::string name, VarValues fields, VarValues methods)
  298. : Value(Kind::ClassType),
  299. name(std::move(name)),
  300. fields(std::move(fields)),
  301. methods(std::move(methods)) {}
  302. static auto classof(const Value* value) -> bool {
  303. return value->Tag() == Kind::ClassType;
  304. }
  305. auto Name() const -> const std::string& { return name; }
  306. auto Fields() const -> const VarValues& { return fields; }
  307. auto Methods() const -> const VarValues& { return methods; }
  308. private:
  309. std::string name;
  310. VarValues fields;
  311. VarValues methods;
  312. };
  313. // A choice type.
  314. class ChoiceType : public Value {
  315. public:
  316. ChoiceType(std::string name, VarValues alternatives)
  317. : Value(Kind::ChoiceType),
  318. name(std::move(name)),
  319. alternatives(std::move(alternatives)) {}
  320. static auto classof(const Value* value) -> bool {
  321. return value->Tag() == Kind::ChoiceType;
  322. }
  323. auto Name() const -> const std::string& { return name; }
  324. auto Alternatives() const -> const VarValues& { return alternatives; }
  325. private:
  326. std::string name;
  327. VarValues alternatives;
  328. };
  329. // A continuation type.
  330. class ContinuationType : public Value {
  331. public:
  332. ContinuationType() : Value(Kind::ContinuationType) {}
  333. static auto classof(const Value* value) -> bool {
  334. return value->Tag() == Kind::ContinuationType;
  335. }
  336. };
  337. // A variable type.
  338. class VariableType : public Value {
  339. public:
  340. explicit VariableType(std::string name)
  341. : Value(Kind::VariableType), name(std::move(name)) {}
  342. static auto classof(const Value* value) -> bool {
  343. return value->Tag() == Kind::VariableType;
  344. }
  345. auto Name() const -> const std::string& { return name; }
  346. private:
  347. std::string name;
  348. };
  349. // A first-class continuation representation of a fragment of the stack.
  350. class ContinuationValue : public Value {
  351. public:
  352. explicit ContinuationValue(std::vector<Ptr<Frame>> stack)
  353. : Value(Kind::ContinuationValue), stack(std::move(stack)) {}
  354. static auto classof(const Value* value) -> bool {
  355. return value->Tag() == Kind::ContinuationValue;
  356. }
  357. auto Stack() const -> const std::vector<Ptr<Frame>>& { return stack; }
  358. private:
  359. std::vector<Ptr<Frame>> stack;
  360. };
  361. // The String type.
  362. class StringType : public Value {
  363. public:
  364. StringType() : Value(Kind::StringType) {}
  365. static auto classof(const Value* value) -> bool {
  366. return value->Tag() == Kind::StringType;
  367. }
  368. };
  369. // A string value.
  370. class StringValue : public Value {
  371. public:
  372. explicit StringValue(std::string val)
  373. : Value(Kind::StringValue), val(std::move(val)) {}
  374. static auto classof(const Value* value) -> bool {
  375. return value->Tag() == Kind::StringValue;
  376. }
  377. auto Val() const -> const std::string& { return val; }
  378. private:
  379. std::string val;
  380. };
  381. auto CopyVal(Ptr<const Value> val, SourceLocation loc) -> Ptr<const Value>;
  382. auto TypeEqual(Ptr<const Value> t1, Ptr<const Value> t2) -> bool;
  383. auto ValueEqual(Ptr<const Value> v1, Ptr<const Value> v2, SourceLocation loc)
  384. -> bool;
  385. } // namespace Carbon
  386. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_