value.h 13 KB

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