value.h 15 KB

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