value.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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 CARBON_EXPLORER_INTERPRETER_VALUE_H_
  5. #define CARBON_EXPLORER_INTERPRETER_VALUE_H_
  6. #include <optional>
  7. #include <string>
  8. #include <variant>
  9. #include <vector>
  10. #include "common/ostream.h"
  11. #include "explorer/ast/bindings.h"
  12. #include "explorer/ast/declaration.h"
  13. #include "explorer/ast/member.h"
  14. #include "explorer/ast/statement.h"
  15. #include "explorer/common/nonnull.h"
  16. #include "explorer/interpreter/address.h"
  17. #include "explorer/interpreter/field_path.h"
  18. #include "explorer/interpreter/stack.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace Carbon {
  21. class Action;
  22. class AssociatedConstant;
  23. // Abstract base class of all AST nodes representing values.
  24. //
  25. // Value and its derived classes support LLVM-style RTTI, including
  26. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  27. // class derived from Value must provide a `classof` operation, and
  28. // every concrete derived class must have a corresponding enumerator
  29. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  30. // details.
  31. class Value {
  32. public:
  33. enum class Kind {
  34. IntValue,
  35. FunctionValue,
  36. DestructorValue,
  37. BoundMethodValue,
  38. PointerValue,
  39. LValue,
  40. BoolValue,
  41. StructValue,
  42. NominalClassValue,
  43. AlternativeValue,
  44. TupleValue,
  45. UninitializedValue,
  46. ImplWitness,
  47. BindingWitness,
  48. ConstraintWitness,
  49. ConstraintImplWitness,
  50. IntType,
  51. BoolType,
  52. TypeType,
  53. FunctionType,
  54. PointerType,
  55. AutoType,
  56. StructType,
  57. NominalClassType,
  58. TupleType,
  59. MixinPseudoType,
  60. InterfaceType,
  61. ConstraintType,
  62. ChoiceType,
  63. ContinuationType, // The type of a continuation.
  64. VariableType, // e.g., generic type parameters.
  65. AssociatedConstant,
  66. ParameterizedEntityName,
  67. MemberName,
  68. BindingPlaceholderValue,
  69. AddrValue,
  70. AlternativeConstructorValue,
  71. ContinuationValue, // A first-class continuation value.
  72. StringType,
  73. StringValue,
  74. TypeOfMixinPseudoType,
  75. TypeOfParameterizedEntityName,
  76. TypeOfMemberName,
  77. StaticArrayType,
  78. };
  79. Value(const Value&) = delete;
  80. auto operator=(const Value&) -> Value& = delete;
  81. void Print(llvm::raw_ostream& out) const;
  82. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  83. // Returns the sub-Value specified by `path`, which must be a valid field
  84. // path for *this. If the sub-Value is a method and its me_pattern is an
  85. // AddrPattern, then pass the LValue representing the receiver as `me_value`,
  86. // otherwise pass `*this`.
  87. auto GetMember(Nonnull<Arena*> arena, const FieldPath& path,
  88. SourceLocation source_loc,
  89. Nonnull<const Value*> me_value) const
  90. -> ErrorOr<Nonnull<const Value*>>;
  91. // Returns a copy of *this, but with the sub-Value specified by `path`
  92. // set to `field_value`. `path` must be a valid field path for *this.
  93. auto SetField(Nonnull<Arena*> arena, const FieldPath& path,
  94. Nonnull<const Value*> field_value,
  95. SourceLocation source_loc) const
  96. -> ErrorOr<Nonnull<const Value*>>;
  97. // Returns the enumerator corresponding to the most-derived type of this
  98. // object.
  99. auto kind() const -> Kind { return kind_; }
  100. protected:
  101. // Constructs a Value. `kind` must be the enumerator corresponding to the
  102. // most-derived type being constructed.
  103. explicit Value(Kind kind) : kind_(kind) {}
  104. private:
  105. const Kind kind_;
  106. };
  107. // Returns whether the fully-resolved kind that this value will eventually have
  108. // is currently unknown, because it depends on a generic parameter.
  109. inline auto IsValueKindDependent(Nonnull<const Value*> type) -> bool {
  110. return type->kind() == Value::Kind::VariableType ||
  111. type->kind() == Value::Kind::AssociatedConstant;
  112. }
  113. // Base class for types holding contextual information by which we can
  114. // determine whether values are equal.
  115. class EqualityContext {
  116. public:
  117. virtual auto VisitEqualValues(
  118. Nonnull<const Value*> value,
  119. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const
  120. -> bool = 0;
  121. protected:
  122. virtual ~EqualityContext() = default;
  123. };
  124. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2,
  125. std::optional<Nonnull<const EqualityContext*>> equality_ctx)
  126. -> bool;
  127. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
  128. std::optional<Nonnull<const EqualityContext*>> equality_ctx)
  129. -> bool;
  130. // An integer value.
  131. class IntValue : public Value {
  132. public:
  133. explicit IntValue(int value) : Value(Kind::IntValue), value_(value) {}
  134. static auto classof(const Value* value) -> bool {
  135. return value->kind() == Kind::IntValue;
  136. }
  137. auto value() const -> int { return value_; }
  138. private:
  139. int value_;
  140. };
  141. // A function value.
  142. class FunctionValue : public Value {
  143. public:
  144. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration)
  145. : Value(Kind::FunctionValue), declaration_(declaration) {}
  146. explicit FunctionValue(Nonnull<const FunctionDeclaration*> declaration,
  147. Nonnull<const Bindings*> bindings)
  148. : Value(Kind::FunctionValue),
  149. declaration_(declaration),
  150. bindings_(bindings) {}
  151. static auto classof(const Value* value) -> bool {
  152. return value->kind() == Kind::FunctionValue;
  153. }
  154. auto declaration() const -> const FunctionDeclaration& {
  155. return *declaration_;
  156. }
  157. auto bindings() const -> const Bindings& { return *bindings_; }
  158. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  159. auto witnesses() const -> const ImplWitnessMap& {
  160. return bindings_->witnesses();
  161. }
  162. private:
  163. Nonnull<const FunctionDeclaration*> declaration_;
  164. Nonnull<const Bindings*> bindings_ = Bindings::None();
  165. };
  166. // A destructor value.
  167. class DestructorValue : public Value {
  168. public:
  169. explicit DestructorValue(Nonnull<const DestructorDeclaration*> declaration)
  170. : Value(Kind::DestructorValue), declaration_(declaration) {}
  171. static auto classof(const Value* value) -> bool {
  172. return value->kind() == Kind::DestructorValue;
  173. }
  174. auto declaration() const -> const DestructorDeclaration& {
  175. return *declaration_;
  176. }
  177. private:
  178. Nonnull<const DestructorDeclaration*> declaration_;
  179. };
  180. // A bound method value. It includes the receiver object.
  181. class BoundMethodValue : public Value {
  182. public:
  183. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  184. Nonnull<const Value*> receiver)
  185. : Value(Kind::BoundMethodValue),
  186. declaration_(declaration),
  187. receiver_(receiver) {}
  188. explicit BoundMethodValue(Nonnull<const FunctionDeclaration*> declaration,
  189. Nonnull<const Value*> receiver,
  190. Nonnull<const Bindings*> bindings)
  191. : Value(Kind::BoundMethodValue),
  192. declaration_(declaration),
  193. receiver_(receiver),
  194. bindings_(bindings) {}
  195. static auto classof(const Value* value) -> bool {
  196. return value->kind() == Kind::BoundMethodValue;
  197. }
  198. auto declaration() const -> const FunctionDeclaration& {
  199. return *declaration_;
  200. }
  201. auto receiver() const -> Nonnull<const Value*> { return receiver_; }
  202. auto bindings() const -> const Bindings& { return *bindings_; }
  203. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  204. auto witnesses() const -> const ImplWitnessMap& {
  205. return bindings_->witnesses();
  206. }
  207. private:
  208. Nonnull<const FunctionDeclaration*> declaration_;
  209. Nonnull<const Value*> receiver_;
  210. Nonnull<const Bindings*> bindings_ = Bindings::None();
  211. };
  212. // The value of a location in memory.
  213. class LValue : public Value {
  214. public:
  215. explicit LValue(Address value)
  216. : Value(Kind::LValue), value_(std::move(value)) {}
  217. static auto classof(const Value* value) -> bool {
  218. return value->kind() == Kind::LValue;
  219. }
  220. auto address() const -> const Address& { return value_; }
  221. private:
  222. Address value_;
  223. };
  224. // A pointer value
  225. class PointerValue : public Value {
  226. public:
  227. explicit PointerValue(Address value)
  228. : Value(Kind::PointerValue), value_(std::move(value)) {}
  229. static auto classof(const Value* value) -> bool {
  230. return value->kind() == Kind::PointerValue;
  231. }
  232. auto address() const -> const Address& { return value_; }
  233. private:
  234. Address value_;
  235. };
  236. // A bool value.
  237. class BoolValue : public Value {
  238. public:
  239. explicit BoolValue(bool value) : Value(Kind::BoolValue), value_(value) {}
  240. static auto classof(const Value* value) -> bool {
  241. return value->kind() == Kind::BoolValue;
  242. }
  243. auto value() const -> bool { return value_; }
  244. private:
  245. bool value_;
  246. };
  247. // A value of a struct type. Note that the expression `{}` is a value of type
  248. // `{} as Type`; the former is a `StructValue` and the latter is a
  249. // `StructType`.
  250. class StructValue : public Value {
  251. public:
  252. explicit StructValue(std::vector<NamedValue> elements)
  253. : Value(Kind::StructValue), elements_(std::move(elements)) {}
  254. static auto classof(const Value* value) -> bool {
  255. return value->kind() == Kind::StructValue;
  256. }
  257. auto elements() const -> llvm::ArrayRef<NamedValue> { return elements_; }
  258. // Returns the value of the field named `name` in this struct, or
  259. // nullopt if there is no such field.
  260. auto FindField(std::string_view name) const
  261. -> std::optional<Nonnull<const Value*>>;
  262. private:
  263. std::vector<NamedValue> elements_;
  264. };
  265. // A value of a nominal class type, i.e., an object.
  266. class NominalClassValue : public Value {
  267. public:
  268. NominalClassValue(Nonnull<const Value*> type, Nonnull<const Value*> inits)
  269. : Value(Kind::NominalClassValue), type_(type), inits_(inits) {}
  270. static auto classof(const Value* value) -> bool {
  271. return value->kind() == Kind::NominalClassValue;
  272. }
  273. auto type() const -> const Value& { return *type_; }
  274. auto inits() const -> const Value& { return *inits_; }
  275. private:
  276. Nonnull<const Value*> type_;
  277. Nonnull<const Value*> inits_; // The initializing StructValue.
  278. };
  279. // An alternative constructor value.
  280. class AlternativeConstructorValue : public Value {
  281. public:
  282. AlternativeConstructorValue(std::string_view alt_name,
  283. std::string_view choice_name)
  284. : Value(Kind::AlternativeConstructorValue),
  285. alt_name_(alt_name),
  286. choice_name_(choice_name) {}
  287. static auto classof(const Value* value) -> bool {
  288. return value->kind() == Kind::AlternativeConstructorValue;
  289. }
  290. auto alt_name() const -> const std::string& { return alt_name_; }
  291. auto choice_name() const -> const std::string& { return choice_name_; }
  292. private:
  293. std::string alt_name_;
  294. std::string choice_name_;
  295. };
  296. // An alternative value.
  297. class AlternativeValue : public Value {
  298. public:
  299. AlternativeValue(std::string_view alt_name, std::string_view choice_name,
  300. Nonnull<const Value*> argument)
  301. : Value(Kind::AlternativeValue),
  302. alt_name_(alt_name),
  303. choice_name_(choice_name),
  304. argument_(argument) {}
  305. static auto classof(const Value* value) -> bool {
  306. return value->kind() == Kind::AlternativeValue;
  307. }
  308. auto alt_name() const -> const std::string& { return alt_name_; }
  309. auto choice_name() const -> const std::string& { return choice_name_; }
  310. auto argument() const -> const Value& { return *argument_; }
  311. private:
  312. std::string alt_name_;
  313. std::string choice_name_;
  314. Nonnull<const Value*> argument_;
  315. };
  316. // Base class for tuple types and tuple values. These are the same other than
  317. // their type-of-type, but we separate them to make it easier to tell types and
  318. // values apart.
  319. class TupleValueBase : public Value {
  320. public:
  321. explicit TupleValueBase(Value::Kind kind,
  322. std::vector<Nonnull<const Value*>> elements)
  323. : Value(kind), elements_(std::move(elements)) {}
  324. auto elements() const -> llvm::ArrayRef<Nonnull<const Value*>> {
  325. return elements_;
  326. }
  327. static auto classof(const Value* value) -> bool {
  328. return value->kind() == Kind::TupleValue ||
  329. value->kind() == Kind::TupleType;
  330. }
  331. private:
  332. std::vector<Nonnull<const Value*>> elements_;
  333. };
  334. // A tuple value.
  335. class TupleValue : public TupleValueBase {
  336. public:
  337. // An empty tuple.
  338. static auto Empty() -> Nonnull<const TupleValue*> {
  339. static const TupleValue empty =
  340. TupleValue(std::vector<Nonnull<const Value*>>());
  341. return static_cast<Nonnull<const TupleValue*>>(&empty);
  342. }
  343. explicit TupleValue(std::vector<Nonnull<const Value*>> elements)
  344. : TupleValueBase(Kind::TupleValue, std::move(elements)) {}
  345. static auto classof(const Value* value) -> bool {
  346. return value->kind() == Kind::TupleValue;
  347. }
  348. };
  349. // A tuple type. This is the result of converting a tuple value containing
  350. // only types to type Type.
  351. class TupleType : public TupleValueBase {
  352. public:
  353. // The unit type.
  354. static auto Empty() -> Nonnull<const TupleType*> {
  355. static const TupleType empty =
  356. TupleType(std::vector<Nonnull<const Value*>>());
  357. return static_cast<Nonnull<const TupleType*>>(&empty);
  358. }
  359. explicit TupleType(std::vector<Nonnull<const Value*>> elements)
  360. : TupleValueBase(Kind::TupleType, std::move(elements)) {}
  361. static auto classof(const Value* value) -> bool {
  362. return value->kind() == Kind::TupleType;
  363. }
  364. };
  365. // A binding placeholder value.
  366. class BindingPlaceholderValue : public Value {
  367. public:
  368. // Represents the `_` placeholder.
  369. explicit BindingPlaceholderValue() : Value(Kind::BindingPlaceholderValue) {}
  370. // Represents a named placeholder.
  371. explicit BindingPlaceholderValue(ValueNodeView value_node)
  372. : Value(Kind::BindingPlaceholderValue),
  373. value_node_(std::move(value_node)) {}
  374. static auto classof(const Value* value) -> bool {
  375. return value->kind() == Kind::BindingPlaceholderValue;
  376. }
  377. auto value_node() const -> const std::optional<ValueNodeView>& {
  378. return value_node_;
  379. }
  380. private:
  381. std::optional<ValueNodeView> value_node_;
  382. };
  383. // Value for addr pattern
  384. class AddrValue : public Value {
  385. public:
  386. explicit AddrValue(Nonnull<const Value*> pattern)
  387. : Value(Kind::AddrValue), pattern_(pattern) {}
  388. static auto classof(const Value* value) -> bool {
  389. return value->kind() == Kind::AddrValue;
  390. }
  391. auto pattern() const -> const Value& { return *pattern_; }
  392. private:
  393. Nonnull<const Value*> pattern_;
  394. };
  395. // Value for uninitialized local variables.
  396. class UninitializedValue : public Value {
  397. public:
  398. explicit UninitializedValue(Nonnull<const Value*> pattern)
  399. : Value(Kind::UninitializedValue), pattern_(pattern) {}
  400. static auto classof(const Value* value) -> bool {
  401. return value->kind() == Kind::UninitializedValue;
  402. }
  403. auto pattern() const -> const Value& { return *pattern_; }
  404. private:
  405. Nonnull<const Value*> pattern_;
  406. };
  407. // The int type.
  408. class IntType : public Value {
  409. public:
  410. IntType() : Value(Kind::IntType) {}
  411. static auto classof(const Value* value) -> bool {
  412. return value->kind() == Kind::IntType;
  413. }
  414. };
  415. // The bool type.
  416. class BoolType : public Value {
  417. public:
  418. BoolType() : Value(Kind::BoolType) {}
  419. static auto classof(const Value* value) -> bool {
  420. return value->kind() == Kind::BoolType;
  421. }
  422. };
  423. // A type type.
  424. class TypeType : public Value {
  425. public:
  426. TypeType() : Value(Kind::TypeType) {}
  427. static auto classof(const Value* value) -> bool {
  428. return value->kind() == Kind::TypeType;
  429. }
  430. };
  431. // A function type.
  432. class FunctionType : public Value {
  433. public:
  434. // An explicit function parameter that is a `:!` binding:
  435. //
  436. // fn MakeEmptyVector(T:! Type) -> Vector(T);
  437. struct GenericParameter {
  438. size_t index;
  439. Nonnull<const GenericBinding*> binding;
  440. };
  441. FunctionType(Nonnull<const Value*> parameters,
  442. Nonnull<const Value*> return_type)
  443. : FunctionType(parameters, {}, return_type, {}, {}) {}
  444. FunctionType(Nonnull<const Value*> parameters,
  445. std::vector<GenericParameter> generic_parameters,
  446. Nonnull<const Value*> return_type,
  447. std::vector<Nonnull<const GenericBinding*>> deduced_bindings,
  448. std::vector<Nonnull<const ImplBinding*>> impl_bindings)
  449. : Value(Kind::FunctionType),
  450. parameters_(parameters),
  451. generic_parameters_(std::move(generic_parameters)),
  452. return_type_(return_type),
  453. deduced_bindings_(std::move(deduced_bindings)),
  454. impl_bindings_(std::move(impl_bindings)) {}
  455. static auto classof(const Value* value) -> bool {
  456. return value->kind() == Kind::FunctionType;
  457. }
  458. // The type of the function parameter tuple.
  459. auto parameters() const -> const Value& { return *parameters_; }
  460. // Parameters that use a generic `:!` binding at the top level.
  461. auto generic_parameters() const -> llvm::ArrayRef<GenericParameter> {
  462. return generic_parameters_;
  463. }
  464. // The function return type.
  465. auto return_type() const -> const Value& { return *return_type_; }
  466. // All generic bindings in this function's signature that should be deduced
  467. // in a call. This excludes any generic parameters.
  468. auto deduced_bindings() const
  469. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  470. return deduced_bindings_;
  471. }
  472. // The bindings for the witness tables (impls) required by the
  473. // bounds on the type parameters of the generic function.
  474. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  475. return impl_bindings_;
  476. }
  477. private:
  478. Nonnull<const Value*> parameters_;
  479. std::vector<GenericParameter> generic_parameters_;
  480. Nonnull<const Value*> return_type_;
  481. std::vector<Nonnull<const GenericBinding*>> deduced_bindings_;
  482. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  483. };
  484. // A pointer type.
  485. class PointerType : public Value {
  486. public:
  487. explicit PointerType(Nonnull<const Value*> type)
  488. : Value(Kind::PointerType), type_(type) {}
  489. static auto classof(const Value* value) -> bool {
  490. return value->kind() == Kind::PointerType;
  491. }
  492. auto type() const -> const Value& { return *type_; }
  493. private:
  494. Nonnull<const Value*> type_;
  495. };
  496. // The `auto` type.
  497. class AutoType : public Value {
  498. public:
  499. AutoType() : Value(Kind::AutoType) {}
  500. static auto classof(const Value* value) -> bool {
  501. return value->kind() == Kind::AutoType;
  502. }
  503. };
  504. // A struct type.
  505. class StructType : public Value {
  506. public:
  507. StructType() : StructType(std::vector<NamedValue>{}) {}
  508. explicit StructType(std::vector<NamedValue> fields)
  509. : Value(Kind::StructType), fields_(std::move(fields)) {}
  510. static auto classof(const Value* value) -> bool {
  511. return value->kind() == Kind::StructType;
  512. }
  513. auto fields() const -> llvm::ArrayRef<NamedValue> { return fields_; }
  514. private:
  515. std::vector<NamedValue> fields_;
  516. };
  517. // A class type.
  518. // TODO: Consider splitting this class into several classes.
  519. class NominalClassType : public Value {
  520. public:
  521. // Construct a non-generic class type.
  522. explicit NominalClassType(Nonnull<const ClassDeclaration*> declaration)
  523. : Value(Kind::NominalClassType), declaration_(declaration) {
  524. CARBON_CHECK(!declaration->type_params().has_value())
  525. << "missing arguments for parameterized class type";
  526. }
  527. // Construct a fully instantiated generic class type to represent the
  528. // run-time type of an object.
  529. explicit NominalClassType(
  530. Nonnull<const ClassDeclaration*> declaration,
  531. Nonnull<const Bindings*> bindings,
  532. std::optional<Nonnull<const NominalClassType*>> base = std::nullopt)
  533. : Value(Kind::NominalClassType),
  534. declaration_(declaration),
  535. bindings_(bindings),
  536. base_(base) {}
  537. static auto classof(const Value* value) -> bool {
  538. return value->kind() == Kind::NominalClassType;
  539. }
  540. auto declaration() const -> const ClassDeclaration& { return *declaration_; }
  541. auto bindings() const -> const Bindings& { return *bindings_; }
  542. auto base() const -> std::optional<Nonnull<const NominalClassType*>> {
  543. return base_;
  544. }
  545. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  546. // Witnesses for each of the class's impl bindings.
  547. auto witnesses() const -> const ImplWitnessMap& {
  548. return bindings_->witnesses();
  549. }
  550. // Returns whether this a parameterized class. That is, a class with
  551. // parameters and no corresponding arguments.
  552. auto IsParameterized() const -> bool {
  553. return declaration_->type_params().has_value() && type_args().empty();
  554. }
  555. private:
  556. Nonnull<const ClassDeclaration*> declaration_;
  557. Nonnull<const Bindings*> bindings_ = Bindings::None();
  558. std::optional<Nonnull<const NominalClassType*>> base_;
  559. };
  560. class MixinPseudoType : public Value {
  561. public:
  562. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration)
  563. : Value(Kind::MixinPseudoType), declaration_(declaration) {
  564. CARBON_CHECK(!declaration->params().has_value())
  565. << "missing arguments for parameterized mixin type";
  566. }
  567. explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration,
  568. Nonnull<const Bindings*> bindings)
  569. : Value(Kind::MixinPseudoType),
  570. declaration_(declaration),
  571. bindings_(bindings) {}
  572. static auto classof(const Value* value) -> bool {
  573. return value->kind() == Kind::MixinPseudoType;
  574. }
  575. auto declaration() const -> const MixinDeclaration& { return *declaration_; }
  576. auto bindings() const -> const Bindings& { return *bindings_; }
  577. auto args() const -> const BindingMap& { return bindings_->args(); }
  578. auto witnesses() const -> const ImplWitnessMap& {
  579. return bindings_->witnesses();
  580. }
  581. auto FindFunction(const std::string_view& name) const
  582. -> std::optional<Nonnull<const FunctionValue*>>;
  583. private:
  584. Nonnull<const MixinDeclaration*> declaration_;
  585. Nonnull<const Bindings*> bindings_ = Bindings::None();
  586. };
  587. // Returns the value of the function named `name` in this class, or
  588. // nullopt if there is no such function.
  589. auto FindFunction(std::string_view name,
  590. llvm::ArrayRef<Nonnull<Declaration*>> members)
  591. -> std::optional<Nonnull<const FunctionValue*>>;
  592. // Returns the value of the function named `name` in this class and its
  593. // parents, or nullopt if there is no such function.
  594. auto FindFunctionWithParents(std::string_view name,
  595. const ClassDeclaration& class_decl)
  596. -> std::optional<Nonnull<const FunctionValue*>>;
  597. // Return the declaration of the member with the given name.
  598. auto FindMember(std::string_view name,
  599. llvm::ArrayRef<Nonnull<Declaration*>> members)
  600. -> std::optional<Nonnull<const Declaration*>>;
  601. // An interface type.
  602. class InterfaceType : public Value {
  603. public:
  604. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
  605. : Value(Kind::InterfaceType), declaration_(declaration) {
  606. CARBON_CHECK(!declaration->params().has_value())
  607. << "missing arguments for parameterized interface type";
  608. }
  609. explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
  610. Nonnull<const Bindings*> bindings)
  611. : Value(Kind::InterfaceType),
  612. declaration_(declaration),
  613. bindings_(bindings) {}
  614. static auto classof(const Value* value) -> bool {
  615. return value->kind() == Kind::InterfaceType;
  616. }
  617. auto declaration() const -> const InterfaceDeclaration& {
  618. return *declaration_;
  619. }
  620. auto bindings() const -> const Bindings& { return *bindings_; }
  621. auto args() const -> const BindingMap& { return bindings_->args(); }
  622. auto witnesses() const -> const ImplWitnessMap& {
  623. return bindings_->witnesses();
  624. }
  625. private:
  626. Nonnull<const InterfaceDeclaration*> declaration_;
  627. Nonnull<const Bindings*> bindings_ = Bindings::None();
  628. };
  629. // A constraint that requires implementation of an interface.
  630. struct ImplConstraint {
  631. // The type that is required to implement the interface.
  632. Nonnull<const Value*> type;
  633. // The interface that is required to be implemented.
  634. Nonnull<const InterfaceType*> interface;
  635. };
  636. // A constraint that a collection of values are known to be the same.
  637. struct EqualityConstraint {
  638. // Visit the values in this equality constraint that are a single step away
  639. // from the given value according to this equality constraint. That is: if
  640. // `value` is identical to a value in `values`, then call the visitor on all
  641. // values in `values` that are not identical to `value`. Otherwise, do not
  642. // call the visitor.
  643. //
  644. // Stops and returns `false` if any call to the visitor returns `false`,
  645. // otherwise returns `true`.
  646. auto VisitEqualValues(
  647. Nonnull<const Value*> value,
  648. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  649. std::vector<Nonnull<const Value*>> values;
  650. };
  651. // A constraint indicating that access to an associated constant should be
  652. // replaced by another value.
  653. struct RewriteConstraint {
  654. // The associated constant value that is rewritten.
  655. Nonnull<const AssociatedConstant*> constant;
  656. // The replacement in its original type.
  657. Nonnull<const Value*> unconverted_replacement;
  658. // The type of the replacement.
  659. Nonnull<const Value*> unconverted_replacement_type;
  660. // The replacement after conversion to the type of the associated constant.
  661. Nonnull<const Value*> converted_replacement;
  662. };
  663. // A context in which we might look up a name.
  664. struct LookupContext {
  665. Nonnull<const Value*> context;
  666. };
  667. // A type-of-type for an unknown constrained type.
  668. //
  669. // These types are formed by the `&` operator that combines constraints and by
  670. // `where` expressions.
  671. //
  672. // A constraint has three main properties:
  673. //
  674. // * A collection of (type, interface) pairs for interfaces that are known to
  675. // be implemented by a type satisfying the constraint.
  676. // * A collection of sets of values, typically associated constants, that are
  677. // known to be the same.
  678. // * A collection of contexts in which member name lookups will be performed
  679. // for a type variable whose type is this constraint.
  680. //
  681. // Within these properties, the constrained type can be referred to with a
  682. // `VariableType` naming the `self_binding`.
  683. class ConstraintType : public Value {
  684. public:
  685. explicit ConstraintType(Nonnull<const GenericBinding*> self_binding,
  686. std::vector<ImplConstraint> impl_constraints,
  687. std::vector<EqualityConstraint> equality_constraints,
  688. std::vector<RewriteConstraint> rewrite_constraints,
  689. std::vector<LookupContext> lookup_contexts)
  690. : Value(Kind::ConstraintType),
  691. self_binding_(self_binding),
  692. impl_constraints_(std::move(impl_constraints)),
  693. equality_constraints_(std::move(equality_constraints)),
  694. rewrite_constraints_(std::move(rewrite_constraints)),
  695. lookup_contexts_(std::move(lookup_contexts)) {}
  696. static auto classof(const Value* value) -> bool {
  697. return value->kind() == Kind::ConstraintType;
  698. }
  699. auto self_binding() const -> Nonnull<const GenericBinding*> {
  700. return self_binding_;
  701. }
  702. auto impl_constraints() const -> llvm::ArrayRef<ImplConstraint> {
  703. return impl_constraints_;
  704. }
  705. auto equality_constraints() const -> llvm::ArrayRef<EqualityConstraint> {
  706. return equality_constraints_;
  707. }
  708. auto rewrite_constraints() const -> llvm::ArrayRef<RewriteConstraint> {
  709. return rewrite_constraints_;
  710. }
  711. auto lookup_contexts() const -> llvm::ArrayRef<LookupContext> {
  712. return lookup_contexts_;
  713. }
  714. // Visit the values in that are a single step away from the given value
  715. // according to equality constraints in this constraint type, that is, the
  716. // values `v` that are not identical to `value` but for which we have a
  717. // `value == v` equality constraint in this constraint type.
  718. //
  719. // Stops and returns `false` if any call to the visitor returns `false`,
  720. // otherwise returns `true`.
  721. auto VisitEqualValues(
  722. Nonnull<const Value*> value,
  723. llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool;
  724. private:
  725. Nonnull<const GenericBinding*> self_binding_;
  726. std::vector<ImplConstraint> impl_constraints_;
  727. std::vector<EqualityConstraint> equality_constraints_;
  728. std::vector<RewriteConstraint> rewrite_constraints_;
  729. std::vector<LookupContext> lookup_contexts_;
  730. };
  731. // A witness table.
  732. class Witness : public Value {
  733. protected:
  734. explicit Witness(Value::Kind kind) : Value(kind) {}
  735. public:
  736. static auto classof(const Value* value) -> bool {
  737. return value->kind() == Kind::ImplWitness ||
  738. value->kind() == Kind::BindingWitness ||
  739. value->kind() == Kind::ConstraintWitness ||
  740. value->kind() == Kind::ConstraintImplWitness;
  741. }
  742. };
  743. // The witness table for an impl.
  744. class ImplWitness : public Witness {
  745. public:
  746. // Construct a witness for an impl.
  747. explicit ImplWitness(Nonnull<const ImplDeclaration*> declaration,
  748. Nonnull<const Bindings*> bindings)
  749. : Witness(Kind::ImplWitness),
  750. declaration_(declaration),
  751. bindings_(bindings) {}
  752. static auto classof(const Value* value) -> bool {
  753. return value->kind() == Kind::ImplWitness;
  754. }
  755. auto declaration() const -> const ImplDeclaration& { return *declaration_; }
  756. auto bindings() const -> const Bindings& { return *bindings_; }
  757. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  758. auto witnesses() const -> const ImplWitnessMap& {
  759. return bindings_->witnesses();
  760. }
  761. private:
  762. Nonnull<const ImplDeclaration*> declaration_;
  763. Nonnull<const Bindings*> bindings_ = Bindings::None();
  764. };
  765. // The symbolic witness corresponding to an unresolved impl binding.
  766. class BindingWitness : public Witness {
  767. public:
  768. // Construct a witness for an impl binding.
  769. explicit BindingWitness(Nonnull<const ImplBinding*> binding)
  770. : Witness(Kind::BindingWitness), binding_(binding) {}
  771. static auto classof(const Value* value) -> bool {
  772. return value->kind() == Kind::BindingWitness;
  773. }
  774. auto binding() const -> Nonnull<const ImplBinding*> { return binding_; }
  775. private:
  776. Nonnull<const ImplBinding*> binding_;
  777. };
  778. // A witness for a constraint type, expressed as a tuple of witnesses for the
  779. // individual impl constraints in the constraint type.
  780. class ConstraintWitness : public Witness {
  781. public:
  782. explicit ConstraintWitness(std::vector<Nonnull<const Witness*>> witnesses)
  783. : Witness(Kind::ConstraintWitness), witnesses_(std::move(witnesses)) {}
  784. static auto classof(const Value* value) -> bool {
  785. return value->kind() == Kind::ConstraintWitness;
  786. }
  787. auto witnesses() const -> llvm::ArrayRef<Nonnull<const Witness*>> {
  788. return witnesses_;
  789. }
  790. private:
  791. std::vector<Nonnull<const Witness*>> witnesses_;
  792. };
  793. // A witness for an impl constraint in a constraint type, expressed in terms of
  794. // a symbolic witness for the constraint type.
  795. class ConstraintImplWitness : public Witness {
  796. public:
  797. // Make a witness for the given impl_constraint of the given `ConstraintType`
  798. // witness. If we're indexing into a known tuple of witnesses, pull out the
  799. // element.
  800. static auto Make(Nonnull<Arena*> arena, Nonnull<const Witness*> witness,
  801. int index) -> Nonnull<const Witness*> {
  802. CARBON_CHECK(!llvm::isa<ImplWitness>(witness))
  803. << "impl witness has no components to access";
  804. if (const auto* constraint_witness =
  805. llvm::dyn_cast<ConstraintWitness>(witness)) {
  806. return constraint_witness->witnesses()[index];
  807. }
  808. return arena->New<ConstraintImplWitness>(witness, index);
  809. }
  810. explicit ConstraintImplWitness(Nonnull<const Witness*> constraint_witness,
  811. int index)
  812. : Witness(Kind::ConstraintImplWitness),
  813. constraint_witness_(constraint_witness),
  814. index_(index) {
  815. CARBON_CHECK(!llvm::isa<ConstraintWitness>(constraint_witness))
  816. << "should have resolved element from constraint witness";
  817. }
  818. static auto classof(const Value* value) -> bool {
  819. return value->kind() == Kind::ConstraintImplWitness;
  820. }
  821. // Get the witness for the complete `ConstraintType`.
  822. auto constraint_witness() const -> Nonnull<const Witness*> {
  823. return constraint_witness_;
  824. }
  825. // Get the index of the impl constraint within the constraint type.
  826. auto index() const -> int { return index_; }
  827. private:
  828. Nonnull<const Witness*> constraint_witness_;
  829. int index_;
  830. };
  831. // A choice type.
  832. class ChoiceType : public Value {
  833. public:
  834. ChoiceType(Nonnull<const ChoiceDeclaration*> declaration,
  835. Nonnull<const Bindings*> bindings)
  836. : Value(Kind::ChoiceType),
  837. declaration_(declaration),
  838. bindings_(bindings) {}
  839. static auto classof(const Value* value) -> bool {
  840. return value->kind() == Kind::ChoiceType;
  841. }
  842. auto name() const -> const std::string& { return declaration_->name(); }
  843. // Returns the parameter types of the alternative with the given name,
  844. // or nullopt if no such alternative is present.
  845. auto FindAlternative(std::string_view name) const
  846. -> std::optional<Nonnull<const Value*>>;
  847. auto bindings() const -> const Bindings& { return *bindings_; }
  848. auto type_args() const -> const BindingMap& { return bindings_->args(); }
  849. auto declaration() const -> const ChoiceDeclaration& { return *declaration_; }
  850. auto IsParameterized() const -> bool {
  851. return declaration_->type_params().has_value();
  852. }
  853. private:
  854. Nonnull<const ChoiceDeclaration*> declaration_;
  855. Nonnull<const Bindings*> bindings_;
  856. };
  857. // A continuation type.
  858. class ContinuationType : public Value {
  859. public:
  860. ContinuationType() : Value(Kind::ContinuationType) {}
  861. static auto classof(const Value* value) -> bool {
  862. return value->kind() == Kind::ContinuationType;
  863. }
  864. };
  865. // A variable type.
  866. class VariableType : public Value {
  867. public:
  868. explicit VariableType(Nonnull<const GenericBinding*> binding)
  869. : Value(Kind::VariableType), binding_(binding) {}
  870. static auto classof(const Value* value) -> bool {
  871. return value->kind() == Kind::VariableType;
  872. }
  873. auto binding() const -> const GenericBinding& { return *binding_; }
  874. private:
  875. Nonnull<const GenericBinding*> binding_;
  876. };
  877. // A name of an entity that has explicit parameters, such as a parameterized
  878. // class or interface. When arguments for those parameters are provided in a
  879. // call, the result will be a class type or interface type.
  880. class ParameterizedEntityName : public Value {
  881. public:
  882. explicit ParameterizedEntityName(Nonnull<const Declaration*> declaration,
  883. Nonnull<const TuplePattern*> params)
  884. : Value(Kind::ParameterizedEntityName),
  885. declaration_(declaration),
  886. params_(params) {}
  887. static auto classof(const Value* value) -> bool {
  888. return value->kind() == Kind::ParameterizedEntityName;
  889. }
  890. auto declaration() const -> const Declaration& { return *declaration_; }
  891. auto params() const -> const TuplePattern& { return *params_; }
  892. private:
  893. Nonnull<const Declaration*> declaration_;
  894. Nonnull<const TuplePattern*> params_;
  895. };
  896. // The name of a member of a class or interface.
  897. //
  898. // These values are used to represent the second operand of a compound member
  899. // access expression: `x.(A.B)`, and can also be the value of an alias
  900. // declaration, but cannot be used in most other contexts.
  901. class MemberName : public Value {
  902. public:
  903. MemberName(std::optional<Nonnull<const Value*>> base_type,
  904. std::optional<Nonnull<const InterfaceType*>> interface,
  905. Member member)
  906. : Value(Kind::MemberName),
  907. base_type_(base_type),
  908. interface_(interface),
  909. member_(member) {
  910. CARBON_CHECK(base_type || interface)
  911. << "member name must be in a type, an interface, or both";
  912. }
  913. static auto classof(const Value* value) -> bool {
  914. return value->kind() == Kind::MemberName;
  915. }
  916. // The type for which `name` is a member or a member of an `impl`.
  917. auto base_type() const -> std::optional<Nonnull<const Value*>> {
  918. return base_type_;
  919. }
  920. // The interface for which `name` is a member, if any.
  921. auto interface() const -> std::optional<Nonnull<const InterfaceType*>> {
  922. return interface_;
  923. }
  924. // The member.
  925. auto member() const -> Member { return member_; }
  926. // The name of the member.
  927. auto name() const -> std::string_view { return member().name(); }
  928. private:
  929. std::optional<Nonnull<const Value*>> base_type_;
  930. std::optional<Nonnull<const InterfaceType*>> interface_;
  931. Member member_;
  932. };
  933. // A symbolic value representing an associated constant.
  934. //
  935. // This is a value of the form `A.B` or `A.B.C` or similar, where `A` is a
  936. // `VariableType`.
  937. class AssociatedConstant : public Value {
  938. public:
  939. explicit AssociatedConstant(
  940. Nonnull<const Value*> base, Nonnull<const InterfaceType*> interface,
  941. Nonnull<const AssociatedConstantDeclaration*> constant,
  942. Nonnull<const Witness*> witness)
  943. : Value(Kind::AssociatedConstant),
  944. base_(base),
  945. interface_(interface),
  946. constant_(constant),
  947. witness_(witness) {}
  948. static auto classof(const Value* value) -> bool {
  949. return value->kind() == Kind::AssociatedConstant;
  950. }
  951. // The type for which we denote an associated constant.
  952. auto base() const -> const Value& { return *base_; }
  953. // The interface within which the constant was declared.
  954. auto interface() const -> const InterfaceType& { return *interface_; }
  955. // The associated constant whose value is being denoted.
  956. auto constant() const -> const AssociatedConstantDeclaration& {
  957. return *constant_;
  958. }
  959. // Witness within which the constant's value can be found.
  960. auto witness() const -> const Witness& { return *witness_; }
  961. private:
  962. Nonnull<const Value*> base_;
  963. Nonnull<const InterfaceType*> interface_;
  964. Nonnull<const AssociatedConstantDeclaration*> constant_;
  965. Nonnull<const Witness*> witness_;
  966. };
  967. // A first-class continuation representation of a fragment of the stack.
  968. // A continuation value behaves like a pointer to the underlying stack
  969. // fragment, which is exposed by `Stack()`.
  970. class ContinuationValue : public Value {
  971. public:
  972. class StackFragment {
  973. public:
  974. // Constructs an empty StackFragment.
  975. StackFragment() = default;
  976. // Requires *this to be empty, because by the time we're tearing down the
  977. // Arena, it's no longer safe to invoke ~Action.
  978. ~StackFragment();
  979. StackFragment(StackFragment&&) = delete;
  980. auto operator=(StackFragment&&) -> StackFragment& = delete;
  981. // Store the given partial todo stack in *this, which must currently be
  982. // empty. The stack is represented with the top of the stack at the
  983. // beginning of the vector, the reverse of the usual order.
  984. void StoreReversed(std::vector<std::unique_ptr<Action>> reversed_todo);
  985. // Restore the currently stored stack fragment to the top of `todo`,
  986. // leaving *this empty.
  987. void RestoreTo(Stack<std::unique_ptr<Action>>& todo);
  988. // Destroy the currently stored stack fragment.
  989. void Clear();
  990. void Print(llvm::raw_ostream& out) const;
  991. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  992. private:
  993. // The todo stack of a suspended continuation, starting with the top
  994. // Action.
  995. std::vector<std::unique_ptr<Action>> reversed_todo_;
  996. };
  997. explicit ContinuationValue(Nonnull<StackFragment*> stack)
  998. : Value(Kind::ContinuationValue), stack_(stack) {}
  999. static auto classof(const Value* value) -> bool {
  1000. return value->kind() == Kind::ContinuationValue;
  1001. }
  1002. // The todo stack of the suspended continuation. Note that this provides
  1003. // mutable access, even when *this is const, because of the reference-like
  1004. // semantics of ContinuationValue.
  1005. auto stack() const -> StackFragment& { return *stack_; }
  1006. private:
  1007. Nonnull<StackFragment*> stack_;
  1008. };
  1009. // The String type.
  1010. class StringType : public Value {
  1011. public:
  1012. StringType() : Value(Kind::StringType) {}
  1013. static auto classof(const Value* value) -> bool {
  1014. return value->kind() == Kind::StringType;
  1015. }
  1016. };
  1017. // A string value.
  1018. class StringValue : public Value {
  1019. public:
  1020. explicit StringValue(std::string value)
  1021. : Value(Kind::StringValue), value_(std::move(value)) {}
  1022. static auto classof(const Value* value) -> bool {
  1023. return value->kind() == Kind::StringValue;
  1024. }
  1025. auto value() const -> const std::string& { return value_; }
  1026. private:
  1027. std::string value_;
  1028. };
  1029. class TypeOfMixinPseudoType : public Value {
  1030. public:
  1031. explicit TypeOfMixinPseudoType(Nonnull<const MixinPseudoType*> class_type)
  1032. : Value(Kind::TypeOfMixinPseudoType), mixin_type_(class_type) {}
  1033. static auto classof(const Value* value) -> bool {
  1034. return value->kind() == Kind::TypeOfMixinPseudoType;
  1035. }
  1036. auto mixin_type() const -> const MixinPseudoType& { return *mixin_type_; }
  1037. private:
  1038. Nonnull<const MixinPseudoType*> mixin_type_;
  1039. };
  1040. // The type of an expression whose value is the name of a parameterized entity.
  1041. // Such an expression can only be used as the operand of a call expression that
  1042. // provides arguments for the parameters.
  1043. class TypeOfParameterizedEntityName : public Value {
  1044. public:
  1045. explicit TypeOfParameterizedEntityName(
  1046. Nonnull<const ParameterizedEntityName*> name)
  1047. : Value(Kind::TypeOfParameterizedEntityName), name_(name) {}
  1048. static auto classof(const Value* value) -> bool {
  1049. return value->kind() == Kind::TypeOfParameterizedEntityName;
  1050. }
  1051. auto name() const -> const ParameterizedEntityName& { return *name_; }
  1052. private:
  1053. Nonnull<const ParameterizedEntityName*> name_;
  1054. };
  1055. // The type of a member name expression.
  1056. //
  1057. // This is used for member names that don't denote a specific object or value
  1058. // until used on the right-hand side of a `.`, such as an instance method or
  1059. // field name, or any member function in an interface.
  1060. //
  1061. // Such expressions can appear only as the target of an `alias` declaration or
  1062. // as the member name in a compound member access.
  1063. class TypeOfMemberName : public Value {
  1064. public:
  1065. explicit TypeOfMemberName(Member member)
  1066. : Value(Kind::TypeOfMemberName), member_(member) {}
  1067. static auto classof(const Value* value) -> bool {
  1068. return value->kind() == Kind::TypeOfMemberName;
  1069. }
  1070. // TODO: consider removing this or moving it elsewhere in the AST,
  1071. // since it's arguably part of the expression value rather than its type.
  1072. auto member() const -> Member { return member_; }
  1073. private:
  1074. Member member_;
  1075. };
  1076. // The type of a statically-sized array.
  1077. //
  1078. // Note that values of this type are represented as tuples.
  1079. class StaticArrayType : public Value {
  1080. public:
  1081. // Constructs a statically-sized array type with the given element type and
  1082. // size.
  1083. StaticArrayType(Nonnull<const Value*> element_type, size_t size)
  1084. : Value(Kind::StaticArrayType),
  1085. element_type_(element_type),
  1086. size_(size) {}
  1087. static auto classof(const Value* value) -> bool {
  1088. return value->kind() == Kind::StaticArrayType;
  1089. }
  1090. auto element_type() const -> const Value& { return *element_type_; }
  1091. auto size() const -> size_t { return size_; }
  1092. private:
  1093. Nonnull<const Value*> element_type_;
  1094. size_t size_;
  1095. };
  1096. } // namespace Carbon
  1097. #endif // CARBON_EXPLORER_INTERPRETER_VALUE_H_