value.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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, 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 -> const Value* { return param; }
  112. auto Body() const -> const Statement* { return body; }
  113. private:
  114. std::string name;
  115. const Value* param;
  116. 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(const Value* type, 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 -> const Value* { return type; }
  150. auto Inits() const -> const Value* { return inits; }
  151. private:
  152. const Value* type;
  153. 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. 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 -> const Value* { return argument; }
  186. private:
  187. std::string alt_name;
  188. std::string choice_name;
  189. 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 const TupleValue& Empty() {
  196. static const TupleValue empty = TupleValue(std::vector<TupleElement>());
  197. return 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. // null if there is no such field.
  207. auto FindField(const std::string& name) const -> const Value*;
  208. private:
  209. std::vector<TupleElement> elements;
  210. };
  211. // A binding placeholder value.
  212. class BindingPlaceholderValue : public Value {
  213. public:
  214. // nullopt represents the `_` placeholder.
  215. BindingPlaceholderValue(std::optional<std::string> name, const Value* type)
  216. : Value(Kind::BindingPlaceholderValue),
  217. name(std::move(name)),
  218. type(type) {}
  219. static auto classof(const Value* value) -> bool {
  220. return value->Tag() == Kind::BindingPlaceholderValue;
  221. }
  222. auto Name() const -> const std::optional<std::string>& { return name; }
  223. auto Type() const -> const Value* { return type; }
  224. private:
  225. std::optional<std::string> name;
  226. const Value* type;
  227. };
  228. // The int type.
  229. class IntType : public Value {
  230. public:
  231. IntType() : Value(Kind::IntType) {}
  232. static auto classof(const Value* value) -> bool {
  233. return value->Tag() == Kind::IntType;
  234. }
  235. };
  236. // The bool type.
  237. class BoolType : public Value {
  238. public:
  239. BoolType() : Value(Kind::BoolType) {}
  240. static auto classof(const Value* value) -> bool {
  241. return value->Tag() == Kind::BoolType;
  242. }
  243. };
  244. // A type type.
  245. class TypeType : public Value {
  246. public:
  247. TypeType() : Value(Kind::TypeType) {}
  248. static auto classof(const Value* value) -> bool {
  249. return value->Tag() == Kind::TypeType;
  250. }
  251. };
  252. // A function type.
  253. class FunctionType : public Value {
  254. public:
  255. FunctionType(std::vector<GenericBinding> deduced, const Value* param,
  256. const Value* ret)
  257. : Value(Kind::FunctionType),
  258. deduced(std::move(deduced)),
  259. param(param),
  260. ret(ret) {}
  261. static auto classof(const Value* value) -> bool {
  262. return value->Tag() == Kind::FunctionType;
  263. }
  264. auto Deduced() const -> const std::vector<GenericBinding>& { return deduced; }
  265. auto Param() const -> const Value* { return param; }
  266. auto Ret() const -> const Value* { return ret; }
  267. private:
  268. std::vector<GenericBinding> deduced;
  269. const Value* param;
  270. const Value* ret;
  271. };
  272. // A pointer type.
  273. class PointerType : public Value {
  274. public:
  275. explicit PointerType(const Value* type)
  276. : Value(Kind::PointerType), type(type) {}
  277. static auto classof(const Value* value) -> bool {
  278. return value->Tag() == Kind::PointerType;
  279. }
  280. auto Type() const -> const Value* { return type; }
  281. private:
  282. const Value* type;
  283. };
  284. // The `auto` type.
  285. class AutoType : public Value {
  286. public:
  287. AutoType() : Value(Kind::AutoType) {}
  288. static auto classof(const Value* value) -> bool {
  289. return value->Tag() == Kind::AutoType;
  290. }
  291. };
  292. // A struct type.
  293. class ClassType : public Value {
  294. public:
  295. ClassType(std::string name, VarValues fields, VarValues methods)
  296. : Value(Kind::ClassType),
  297. name(std::move(name)),
  298. fields(std::move(fields)),
  299. methods(std::move(methods)) {}
  300. static auto classof(const Value* value) -> bool {
  301. return value->Tag() == Kind::ClassType;
  302. }
  303. auto Name() const -> const std::string& { return name; }
  304. auto Fields() const -> const VarValues& { return fields; }
  305. auto Methods() const -> const VarValues& { return methods; }
  306. private:
  307. std::string name;
  308. VarValues fields;
  309. VarValues methods;
  310. };
  311. // A choice type.
  312. class ChoiceType : public Value {
  313. public:
  314. ChoiceType(std::string name, VarValues alternatives)
  315. : Value(Kind::ChoiceType),
  316. name(std::move(name)),
  317. alternatives(std::move(alternatives)) {}
  318. static auto classof(const Value* value) -> bool {
  319. return value->Tag() == Kind::ChoiceType;
  320. }
  321. auto Name() const -> const std::string& { return name; }
  322. auto Alternatives() const -> const VarValues& { return alternatives; }
  323. private:
  324. std::string name;
  325. VarValues alternatives;
  326. };
  327. // A continuation type.
  328. class ContinuationType : public Value {
  329. public:
  330. ContinuationType() : Value(Kind::ContinuationType) {}
  331. static auto classof(const Value* value) -> bool {
  332. return value->Tag() == Kind::ContinuationType;
  333. }
  334. };
  335. // A variable type.
  336. class VariableType : public Value {
  337. public:
  338. explicit VariableType(std::string name)
  339. : Value(Kind::VariableType), name(std::move(name)) {}
  340. static auto classof(const Value* value) -> bool {
  341. return value->Tag() == Kind::VariableType;
  342. }
  343. auto Name() const -> const std::string& { return name; }
  344. private:
  345. std::string name;
  346. };
  347. // A first-class continuation representation of a fragment of the stack.
  348. class ContinuationValue : public Value {
  349. public:
  350. explicit ContinuationValue(std::vector<Ptr<Frame>> stack)
  351. : Value(Kind::ContinuationValue), stack(std::move(stack)) {}
  352. static auto classof(const Value* value) -> bool {
  353. return value->Tag() == Kind::ContinuationValue;
  354. }
  355. auto Stack() const -> const std::vector<Ptr<Frame>>& { return stack; }
  356. private:
  357. std::vector<Ptr<Frame>> stack;
  358. };
  359. // The String type.
  360. class StringType : public Value {
  361. public:
  362. StringType() : Value(Kind::StringType) {}
  363. static auto classof(const Value* value) -> bool {
  364. return value->Tag() == Kind::StringType;
  365. }
  366. };
  367. // A string value.
  368. class StringValue : public Value {
  369. public:
  370. explicit StringValue(std::string val)
  371. : Value(Kind::StringValue), val(std::move(val)) {}
  372. static auto classof(const Value* value) -> bool {
  373. return value->Tag() == Kind::StringValue;
  374. }
  375. auto Val() const -> const std::string& { return val; }
  376. private:
  377. std::string val;
  378. };
  379. auto CopyVal(const Value* val, SourceLocation loc) -> const Value*;
  380. auto TypeEqual(const Value* t1, const Value* t2) -> bool;
  381. auto ValueEqual(const Value* v1, const Value* v2, SourceLocation loc) -> bool;
  382. } // namespace Carbon
  383. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_