value.h 13 KB

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