value.h 13 KB

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