value.h 12 KB

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