value.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. NominalClassValue,
  36. AlternativeValue,
  37. TupleValue,
  38. IntType,
  39. BoolType,
  40. TypeType,
  41. FunctionType,
  42. PointerType,
  43. AutoType,
  44. StructType,
  45. NominalClassType,
  46. ChoiceType,
  47. ContinuationType, // The type of a continuation.
  48. VariableType, // e.g., generic type parameters.
  49. BindingPlaceholderValue,
  50. AlternativeConstructorValue,
  51. ContinuationValue, // A first-class continuation value.
  52. StringType,
  53. StringValue,
  54. };
  55. Value(const Value&) = delete;
  56. Value& operator=(const Value&) = delete;
  57. void Print(llvm::raw_ostream& out) const;
  58. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  59. // Returns the sub-Value specified by `path`, which must be a valid field
  60. // path for *this.
  61. auto GetField(Nonnull<Arena*> arena, const FieldPath& path,
  62. SourceLocation source_loc) const -> Nonnull<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(Nonnull<Arena*> arena, const FieldPath& path,
  66. Nonnull<const Value*> field_value,
  67. SourceLocation source_loc) const -> Nonnull<const Value*>;
  68. // Returns the enumerator corresponding to the most-derived type of this
  69. // object.
  70. auto kind() const -> Kind { return kind_; }
  71. protected:
  72. // Constructs a Value. `kind` must be the enumerator corresponding to the
  73. // most-derived type being constructed.
  74. explicit Value(Kind kind) : kind_(kind) {}
  75. private:
  76. const Kind kind_;
  77. };
  78. using VarValues = std::vector<std::pair<std::string, Nonnull<const Value*>>>;
  79. auto FindInVarValues(const std::string& field, const VarValues& inits)
  80. -> std::optional<Nonnull<const Value*>>;
  81. auto FieldsEqual(const VarValues& ts1, const VarValues& ts2) -> bool;
  82. // A TupleElement represents the value of a single tuple or struct field.
  83. //
  84. // TODO(geoffromer): Rename this, and look for ways to eliminate duplication
  85. // among TupleElement, VarValues::value_type, FieldInitializer,
  86. // TuplePattern::Field, and any similar types.
  87. struct TupleElement {
  88. // The field name.
  89. std::string name;
  90. // The field's value.
  91. Nonnull<const Value*> value;
  92. };
  93. struct Frame; // Used by continuation.
  94. // An integer value.
  95. class IntValue : public Value {
  96. public:
  97. explicit IntValue(int val) : Value(Kind::IntValue), val(val) {}
  98. static auto classof(const Value* value) -> bool {
  99. return value->kind() == Kind::IntValue;
  100. }
  101. auto Val() const -> int { return val; }
  102. private:
  103. int val;
  104. };
  105. // A function value.
  106. class FunctionValue : public Value {
  107. public:
  108. FunctionValue(std::string name, Nonnull<const Value*> param,
  109. std::optional<Nonnull<const Statement*>> body)
  110. : Value(Kind::FunctionValue),
  111. name(std::move(name)),
  112. param(param),
  113. body(body) {}
  114. static auto classof(const Value* value) -> bool {
  115. return value->kind() == Kind::FunctionValue;
  116. }
  117. auto Name() const -> const std::string& { return name; }
  118. auto Param() const -> Nonnull<const Value*> { return param; }
  119. auto Body() const -> std::optional<Nonnull<const Statement*>> { return body; }
  120. private:
  121. std::string name;
  122. Nonnull<const Value*> param;
  123. std::optional<Nonnull<const Statement*>> body;
  124. };
  125. // A pointer value.
  126. class PointerValue : public Value {
  127. public:
  128. explicit PointerValue(Address val)
  129. : Value(Kind::PointerValue), val(std::move(val)) {}
  130. static auto classof(const Value* value) -> bool {
  131. return value->kind() == Kind::PointerValue;
  132. }
  133. auto Val() const -> const Address& { return val; }
  134. private:
  135. Address val;
  136. };
  137. // A bool value.
  138. class BoolValue : public Value {
  139. public:
  140. explicit BoolValue(bool val) : Value(Kind::BoolValue), val(val) {}
  141. static auto classof(const Value* value) -> bool {
  142. return value->kind() == Kind::BoolValue;
  143. }
  144. auto Val() const -> bool { return val; }
  145. private:
  146. bool val;
  147. };
  148. // A non-empty value of a struct type.
  149. //
  150. // It can't be empty because `{}` is a struct type as well as a value of that
  151. // type, so for consistency we always represent it as a StructType rather than
  152. // let it oscillate unpredictably between the two. However, this means code
  153. // that handles StructValue instances may also need to be able to handle
  154. // StructType instances.
  155. class StructValue : public Value {
  156. public:
  157. explicit StructValue(std::vector<TupleElement> elements)
  158. : Value(Kind::StructValue), elements_(std::move(elements)) {
  159. CHECK(!elements_.empty())
  160. << "`{}` is represented as a StructType, not a StructValue.";
  161. }
  162. static auto classof(const Value* value) -> bool {
  163. return value->kind() == Kind::StructValue;
  164. }
  165. auto elements() const -> const std::vector<TupleElement>& {
  166. return elements_;
  167. }
  168. // Returns the value of the field named `name` in this struct, or
  169. // nullopt if there is no such field.
  170. auto FindField(const std::string& name) const
  171. -> std::optional<Nonnull<const Value*>>;
  172. private:
  173. std::vector<TupleElement> elements_;
  174. };
  175. // A value of a nominal class type.
  176. class NominalClassValue : public Value {
  177. public:
  178. NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits)
  179. : Value(Kind::NominalClassValue), type(type), inits(inits) {}
  180. static auto classof(const Value* value) -> bool {
  181. return value->kind() == Kind::NominalClassValue;
  182. }
  183. auto Type() const -> Nonnull<const Value*> { return type; }
  184. auto Inits() const -> Nonnull<const Value*> { return inits; }
  185. private:
  186. Nonnull<const Value*> type;
  187. Nonnull<const Value*> inits;
  188. };
  189. // An alternative constructor value.
  190. class AlternativeConstructorValue : public Value {
  191. public:
  192. AlternativeConstructorValue(std::string alt_name, std::string choice_name)
  193. : Value(Kind::AlternativeConstructorValue),
  194. alt_name(std::move(alt_name)),
  195. choice_name(std::move(choice_name)) {}
  196. static auto classof(const Value* value) -> bool {
  197. return value->kind() == Kind::AlternativeConstructorValue;
  198. }
  199. auto AltName() const -> const std::string& { return alt_name; }
  200. auto ChoiceName() const -> const std::string& { return choice_name; }
  201. private:
  202. std::string alt_name;
  203. std::string choice_name;
  204. };
  205. // An alternative value.
  206. class AlternativeValue : public Value {
  207. public:
  208. AlternativeValue(std::string alt_name, std::string choice_name,
  209. Nonnull<const Value*> argument)
  210. : Value(Kind::AlternativeValue),
  211. alt_name(std::move(alt_name)),
  212. choice_name(std::move(choice_name)),
  213. argument(argument) {}
  214. static auto classof(const Value* value) -> bool {
  215. return value->kind() == Kind::AlternativeValue;
  216. }
  217. auto AltName() const -> const std::string& { return alt_name; }
  218. auto ChoiceName() const -> const std::string& { return choice_name; }
  219. auto Argument() const -> Nonnull<const Value*> { return argument; }
  220. private:
  221. std::string alt_name;
  222. std::string choice_name;
  223. Nonnull<const Value*> argument;
  224. };
  225. // A function value.
  226. class TupleValue : public Value {
  227. public:
  228. // An empty tuple, also known as the unit type.
  229. static Nonnull<const TupleValue*> Empty() {
  230. static const TupleValue empty = TupleValue(std::vector<TupleElement>());
  231. return Nonnull<const TupleValue*>(&empty);
  232. }
  233. explicit TupleValue(std::vector<TupleElement> elements)
  234. : Value(Kind::TupleValue), elements(std::move(elements)) {}
  235. static auto classof(const Value* value) -> bool {
  236. return value->kind() == Kind::TupleValue;
  237. }
  238. auto Elements() const -> const std::vector<TupleElement>& { return elements; }
  239. // Returns the value of the field named `name` in this tuple, or
  240. // nullopt if there is no such field.
  241. auto FindField(const std::string& name) const
  242. -> std::optional<Nonnull<const Value*>>;
  243. private:
  244. std::vector<TupleElement> elements;
  245. };
  246. // A binding placeholder value.
  247. class BindingPlaceholderValue : public Value {
  248. public:
  249. // nullopt represents the `_` placeholder.
  250. BindingPlaceholderValue(std::optional<std::string> name,
  251. Nonnull<const Value*> type)
  252. : Value(Kind::BindingPlaceholderValue),
  253. name(std::move(name)),
  254. type(type) {}
  255. static auto classof(const Value* value) -> bool {
  256. return value->kind() == Kind::BindingPlaceholderValue;
  257. }
  258. auto Name() const -> const std::optional<std::string>& { return name; }
  259. auto Type() const -> Nonnull<const Value*> { return type; }
  260. private:
  261. std::optional<std::string> name;
  262. Nonnull<const Value*> type;
  263. };
  264. // The int type.
  265. class IntType : public Value {
  266. public:
  267. IntType() : Value(Kind::IntType) {}
  268. static auto classof(const Value* value) -> bool {
  269. return value->kind() == Kind::IntType;
  270. }
  271. };
  272. // The bool type.
  273. class BoolType : public Value {
  274. public:
  275. BoolType() : Value(Kind::BoolType) {}
  276. static auto classof(const Value* value) -> bool {
  277. return value->kind() == Kind::BoolType;
  278. }
  279. };
  280. // A type type.
  281. class TypeType : public Value {
  282. public:
  283. TypeType() : Value(Kind::TypeType) {}
  284. static auto classof(const Value* value) -> bool {
  285. return value->kind() == Kind::TypeType;
  286. }
  287. };
  288. // A function type.
  289. class FunctionType : public Value {
  290. public:
  291. FunctionType(std::vector<GenericBinding> deduced, Nonnull<const Value*> param,
  292. Nonnull<const Value*> ret)
  293. : Value(Kind::FunctionType),
  294. deduced(std::move(deduced)),
  295. param(param),
  296. ret(ret) {}
  297. static auto classof(const Value* value) -> bool {
  298. return value->kind() == Kind::FunctionType;
  299. }
  300. auto Deduced() const -> const std::vector<GenericBinding>& { return deduced; }
  301. auto Param() const -> Nonnull<const Value*> { return param; }
  302. auto Ret() const -> Nonnull<const Value*> { return ret; }
  303. private:
  304. std::vector<GenericBinding> deduced;
  305. Nonnull<const Value*> param;
  306. Nonnull<const Value*> ret;
  307. };
  308. // A pointer type.
  309. class PointerType : public Value {
  310. public:
  311. explicit PointerType(Nonnull<const Value*> type)
  312. : Value(Kind::PointerType), type(type) {}
  313. static auto classof(const Value* value) -> bool {
  314. return value->kind() == Kind::PointerType;
  315. }
  316. auto Type() const -> Nonnull<const Value*> { return type; }
  317. private:
  318. Nonnull<const Value*> type;
  319. };
  320. // The `auto` type.
  321. class AutoType : public Value {
  322. public:
  323. AutoType() : Value(Kind::AutoType) {}
  324. static auto classof(const Value* value) -> bool {
  325. return value->kind() == Kind::AutoType;
  326. }
  327. };
  328. // A struct type.
  329. //
  330. // Code that handles this type may sometimes need to have special-case handling
  331. // for `{}`, which is a struct value in addition to being a struct type.
  332. class StructType : public Value {
  333. public:
  334. StructType() : StructType(VarValues{}) {}
  335. explicit StructType(VarValues fields)
  336. : Value(Kind::StructType), fields_(std::move(fields)) {}
  337. static auto classof(const Value* value) -> bool {
  338. return value->kind() == Kind::StructType;
  339. }
  340. auto fields() const -> const VarValues& { return fields_; }
  341. private:
  342. VarValues fields_;
  343. };
  344. // A class type.
  345. class NominalClassType : public Value {
  346. public:
  347. NominalClassType(std::string name, VarValues fields, VarValues methods)
  348. : Value(Kind::NominalClassType),
  349. name(std::move(name)),
  350. fields(std::move(fields)),
  351. methods(std::move(methods)) {}
  352. static auto classof(const Value* value) -> bool {
  353. return value->kind() == Kind::NominalClassType;
  354. }
  355. auto Name() const -> const std::string& { return name; }
  356. auto Fields() const -> const VarValues& { return fields; }
  357. auto Methods() const -> const VarValues& { return methods; }
  358. private:
  359. std::string name;
  360. VarValues fields;
  361. VarValues methods;
  362. };
  363. // A choice type.
  364. class ChoiceType : public Value {
  365. public:
  366. ChoiceType(std::string name, VarValues alternatives)
  367. : Value(Kind::ChoiceType),
  368. name(std::move(name)),
  369. alternatives(std::move(alternatives)) {}
  370. static auto classof(const Value* value) -> bool {
  371. return value->kind() == Kind::ChoiceType;
  372. }
  373. auto Name() const -> const std::string& { return name; }
  374. auto Alternatives() const -> const VarValues& { return alternatives; }
  375. private:
  376. std::string name;
  377. VarValues alternatives;
  378. };
  379. // A continuation type.
  380. class ContinuationType : public Value {
  381. public:
  382. ContinuationType() : Value(Kind::ContinuationType) {}
  383. static auto classof(const Value* value) -> bool {
  384. return value->kind() == Kind::ContinuationType;
  385. }
  386. };
  387. // A variable type.
  388. class VariableType : public Value {
  389. public:
  390. explicit VariableType(std::string name)
  391. : Value(Kind::VariableType), name(std::move(name)) {}
  392. static auto classof(const Value* value) -> bool {
  393. return value->kind() == Kind::VariableType;
  394. }
  395. auto Name() const -> const std::string& { return name; }
  396. private:
  397. std::string name;
  398. };
  399. // A first-class continuation representation of a fragment of the stack.
  400. class ContinuationValue : public Value {
  401. public:
  402. explicit ContinuationValue(std::vector<Nonnull<Frame*>> stack)
  403. : Value(Kind::ContinuationValue), stack(std::move(stack)) {}
  404. static auto classof(const Value* value) -> bool {
  405. return value->kind() == Kind::ContinuationValue;
  406. }
  407. auto Stack() const -> const std::vector<Nonnull<Frame*>>& { return stack; }
  408. private:
  409. std::vector<Nonnull<Frame*>> stack;
  410. };
  411. // The String type.
  412. class StringType : public Value {
  413. public:
  414. StringType() : Value(Kind::StringType) {}
  415. static auto classof(const Value* value) -> bool {
  416. return value->kind() == Kind::StringType;
  417. }
  418. };
  419. // A string value.
  420. class StringValue : public Value {
  421. public:
  422. explicit StringValue(std::string val)
  423. : Value(Kind::StringValue), val(std::move(val)) {}
  424. static auto classof(const Value* value) -> bool {
  425. return value->kind() == Kind::StringValue;
  426. }
  427. auto Val() const -> const std::string& { return val; }
  428. private:
  429. std::string val;
  430. };
  431. auto CopyVal(Nonnull<Arena*> arena, Nonnull<const Value*> val,
  432. SourceLocation source_loc) -> Nonnull<const Value*>;
  433. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool;
  434. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
  435. SourceLocation source_loc) -> bool;
  436. } // namespace Carbon
  437. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_