value.h 13 KB

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