declaration.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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_AST_DECLARATION_H_
  5. #define CARBON_EXPLORER_AST_DECLARATION_H_
  6. #include <string>
  7. #include <string_view>
  8. #include <utility>
  9. #include <vector>
  10. #include "common/check.h"
  11. #include "common/ostream.h"
  12. #include "explorer/ast/ast_node.h"
  13. #include "explorer/ast/impl_binding.h"
  14. #include "explorer/ast/pattern.h"
  15. #include "explorer/ast/return_term.h"
  16. #include "explorer/ast/statement.h"
  17. #include "explorer/ast/static_scope.h"
  18. #include "explorer/ast/value_category.h"
  19. #include "explorer/common/nonnull.h"
  20. #include "explorer/common/source_location.h"
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/Support/Compiler.h"
  23. namespace Carbon {
  24. class MixinPseudoType;
  25. class ConstraintType;
  26. // Abstract base class of all AST nodes representing patterns.
  27. //
  28. // Declaration and its derived classes support LLVM-style RTTI, including
  29. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  30. // class derived from Declaration must provide a `classof` operation, and
  31. // every concrete derived class must have a corresponding enumerator
  32. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  33. // details.
  34. class Declaration : public AstNode {
  35. public:
  36. ~Declaration() override = 0;
  37. Declaration(const Declaration&) = delete;
  38. auto operator=(const Declaration&) -> Declaration& = delete;
  39. void Print(llvm::raw_ostream& out) const override;
  40. void PrintID(llvm::raw_ostream& out) const override;
  41. static auto classof(const AstNode* node) -> bool {
  42. return InheritsFromDeclaration(node->kind());
  43. }
  44. // Returns the enumerator corresponding to the most-derived type of this
  45. // object.
  46. auto kind() const -> DeclarationKind {
  47. return static_cast<DeclarationKind>(root_kind());
  48. }
  49. // The static type of the declared entity. Cannot be called before
  50. // typechecking.
  51. auto static_type() const -> const Value& { return **static_type_; }
  52. // Sets the static type of the declared entity. Can only be called once,
  53. // during typechecking.
  54. void set_static_type(Nonnull<const Value*> type) {
  55. CARBON_CHECK(!static_type_.has_value());
  56. static_type_ = type;
  57. }
  58. // Returns whether the static type has been set. Should only be called
  59. // during typechecking: before typechecking it's guaranteed to be false,
  60. // and after typechecking it's guaranteed to be true.
  61. auto has_static_type() const -> bool { return static_type_.has_value(); }
  62. // Sets the value returned by constant_value(). Can only be called once,
  63. // during typechecking.
  64. void set_constant_value(Nonnull<const Value*> value) {
  65. CARBON_CHECK(!constant_value_.has_value());
  66. constant_value_ = value;
  67. }
  68. // See static_scope.h for API.
  69. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  70. return constant_value_;
  71. }
  72. // See static_scope.h for API.
  73. auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
  74. return constant_value_;
  75. }
  76. // Returns whether this node has been declared.
  77. auto is_declared() const -> bool { return is_declared_; }
  78. // Set that this node is declared. Should only be called once, by the
  79. // type-checker, once the node is ready to be named and used.
  80. void set_is_declared() {
  81. CARBON_CHECK(!is_declared_) << "should not be declared twice";
  82. is_declared_ = true;
  83. }
  84. // Returns whether this node has been fully type-checked.
  85. auto is_type_checked() const -> bool { return is_type_checked_; }
  86. // Set that this node is type-checked. Should only be called once, by the
  87. // type-checker, once full type-checking is complete.
  88. void set_is_type_checked() {
  89. CARBON_CHECK(!is_type_checked_) << "should not be type-checked twice";
  90. is_type_checked_ = true;
  91. }
  92. protected:
  93. // Constructs a Declaration representing syntax at the given line number.
  94. // `kind` must be the enumerator corresponding to the most-derived type being
  95. // constructed.
  96. Declaration(AstNodeKind kind, SourceLocation source_loc)
  97. : AstNode(kind, source_loc) {}
  98. private:
  99. std::optional<Nonnull<const Value*>> static_type_;
  100. std::optional<Nonnull<const Value*>> constant_value_;
  101. bool is_declared_ = false;
  102. bool is_type_checked_ = false;
  103. };
  104. class CallableDeclaration : public Declaration {
  105. public:
  106. CallableDeclaration(AstNodeKind kind, SourceLocation loc, std::string name,
  107. std::vector<Nonnull<GenericBinding*>> deduced_params,
  108. std::optional<Nonnull<Pattern*>> me_pattern,
  109. Nonnull<TuplePattern*> param_pattern,
  110. ReturnTerm return_term,
  111. std::optional<Nonnull<Block*>> body)
  112. : Declaration(kind, loc),
  113. name_(std::move(name)),
  114. deduced_parameters_(std::move(deduced_params)),
  115. me_pattern_(me_pattern),
  116. param_pattern_(param_pattern),
  117. return_term_(return_term),
  118. body_(body) {}
  119. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  120. // TODO: Move name() and name_ to FunctionDeclaration
  121. auto name() const -> const std::string& { return name_; }
  122. auto deduced_parameters() const
  123. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  124. return deduced_parameters_;
  125. }
  126. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  127. return deduced_parameters_;
  128. }
  129. auto me_pattern() const -> const Pattern& { return **me_pattern_; }
  130. auto me_pattern() -> Pattern& { return **me_pattern_; }
  131. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  132. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  133. auto return_term() const -> const ReturnTerm& { return return_term_; }
  134. auto return_term() -> ReturnTerm& { return return_term_; }
  135. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  136. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  137. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  138. auto is_method() const -> bool { return me_pattern_.has_value(); }
  139. private:
  140. std::string name_;
  141. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  142. std::optional<Nonnull<Pattern*>> me_pattern_;
  143. Nonnull<TuplePattern*> param_pattern_;
  144. ReturnTerm return_term_;
  145. std::optional<Nonnull<Block*>> body_;
  146. };
  147. class FunctionDeclaration : public CallableDeclaration {
  148. public:
  149. using ImplementsCarbonValueNode = void;
  150. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  151. std::string name,
  152. std::vector<Nonnull<AstNode*>> deduced_params,
  153. Nonnull<TuplePattern*> param_pattern,
  154. ReturnTerm return_term,
  155. std::optional<Nonnull<Block*>> body)
  156. -> ErrorOr<Nonnull<FunctionDeclaration*>>;
  157. // Use `Create()` instead. This is public only so Arena::New() can call it.
  158. FunctionDeclaration(SourceLocation source_loc, std::string name,
  159. std::vector<Nonnull<GenericBinding*>> deduced_params,
  160. std::optional<Nonnull<Pattern*>> me_pattern,
  161. Nonnull<TuplePattern*> param_pattern,
  162. ReturnTerm return_term,
  163. std::optional<Nonnull<Block*>> body)
  164. : CallableDeclaration(AstNodeKind::FunctionDeclaration, source_loc,
  165. std::move(name), std::move(deduced_params),
  166. me_pattern, param_pattern, return_term, body) {}
  167. static auto classof(const AstNode* node) -> bool {
  168. return InheritsFromFunctionDeclaration(node->kind());
  169. }
  170. };
  171. class DestructorDeclaration : public CallableDeclaration {
  172. public:
  173. using ImplementsCarbonValueNode = void;
  174. static auto CreateDestructor(Nonnull<Arena*> arena, SourceLocation source_loc,
  175. std::vector<Nonnull<AstNode*>> deduced_params,
  176. Nonnull<TuplePattern*> param_pattern,
  177. ReturnTerm return_term,
  178. std::optional<Nonnull<Block*>> body)
  179. -> ErrorOr<Nonnull<DestructorDeclaration*>>;
  180. // Use `Create()` instead. This is public only so Arena::New() can call it.
  181. DestructorDeclaration(SourceLocation source_loc,
  182. std::vector<Nonnull<GenericBinding*>> deduced_params,
  183. std::optional<Nonnull<Pattern*>> me_pattern,
  184. Nonnull<TuplePattern*> param_pattern,
  185. ReturnTerm return_term,
  186. std::optional<Nonnull<Block*>> body)
  187. : CallableDeclaration(AstNodeKind::DestructorDeclaration, source_loc,
  188. "destructor", std::move(deduced_params), me_pattern,
  189. param_pattern, return_term, body) {}
  190. static auto classof(const AstNode* node) -> bool {
  191. return InheritsFromDestructorDeclaration(node->kind());
  192. }
  193. };
  194. class SelfDeclaration : public Declaration {
  195. public:
  196. using ImplementsCarbonValueNode = void;
  197. explicit SelfDeclaration(SourceLocation source_loc)
  198. : Declaration(AstNodeKind::SelfDeclaration, source_loc) {}
  199. static auto classof(const AstNode* node) -> bool {
  200. return InheritsFromSelfDeclaration(node->kind());
  201. }
  202. static auto name() -> std::string_view { return "Self"; }
  203. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  204. };
  205. enum class ClassExtensibility { None, Base, Abstract };
  206. class ClassDeclaration : public Declaration {
  207. public:
  208. using ImplementsCarbonValueNode = void;
  209. ClassDeclaration(SourceLocation source_loc, std::string name,
  210. Nonnull<SelfDeclaration*> self_decl,
  211. ClassExtensibility extensibility,
  212. std::optional<Nonnull<TuplePattern*>> type_params,
  213. std::optional<Nonnull<Expression*>> extends,
  214. std::vector<Nonnull<Declaration*>> members)
  215. : Declaration(AstNodeKind::ClassDeclaration, source_loc),
  216. name_(std::move(name)),
  217. extensibility_(extensibility),
  218. self_decl_(self_decl),
  219. type_params_(type_params),
  220. extends_(extends),
  221. members_(std::move(members)) {}
  222. static auto classof(const AstNode* node) -> bool {
  223. return InheritsFromClassDeclaration(node->kind());
  224. }
  225. auto name() const -> const std::string& { return name_; }
  226. auto extensibility() const -> ClassExtensibility { return extensibility_; }
  227. auto type_params() const -> std::optional<Nonnull<const TuplePattern*>> {
  228. return type_params_;
  229. }
  230. auto type_params() -> std::optional<Nonnull<TuplePattern*>> {
  231. return type_params_;
  232. }
  233. auto extends() const -> std::optional<Nonnull<Expression*>> {
  234. return extends_;
  235. }
  236. auto self() const -> Nonnull<const SelfDeclaration*> { return self_decl_; }
  237. auto self() -> Nonnull<SelfDeclaration*> { return self_decl_; }
  238. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  239. return members_;
  240. }
  241. auto destructor() const -> std::optional<Nonnull<DestructorDeclaration*>> {
  242. for (auto& x : members_) {
  243. if (x->kind() == DeclarationKind::DestructorDeclaration) {
  244. return llvm::cast<DestructorDeclaration>(x);
  245. }
  246. }
  247. return std::nullopt;
  248. }
  249. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  250. private:
  251. std::string name_;
  252. ClassExtensibility extensibility_;
  253. Nonnull<SelfDeclaration*> self_decl_;
  254. std::optional<Nonnull<TuplePattern*>> type_params_;
  255. std::optional<Nonnull<Expression*>> extends_;
  256. std::vector<Nonnull<Declaration*>> members_;
  257. std::optional<Nonnull<FunctionDeclaration*>> destructor_;
  258. };
  259. // EXPERIMENTAL MIXIN FEATURE
  260. class MixinDeclaration : public Declaration {
  261. public:
  262. using ImplementsCarbonValueNode = void;
  263. MixinDeclaration(SourceLocation source_loc, std::string name,
  264. std::optional<Nonnull<TuplePattern*>> params,
  265. Nonnull<GenericBinding*> self,
  266. std::vector<Nonnull<Declaration*>> members)
  267. : Declaration(AstNodeKind::MixinDeclaration, source_loc),
  268. name_(std::move(name)),
  269. params_(params),
  270. self_(self),
  271. members_(std::move(members)) {}
  272. static auto classof(const AstNode* node) -> bool {
  273. return InheritsFromMixinDeclaration(node->kind());
  274. }
  275. auto name() const -> const std::string& { return name_; }
  276. auto params() const -> std::optional<Nonnull<const TuplePattern*>> {
  277. return params_;
  278. }
  279. auto params() -> std::optional<Nonnull<TuplePattern*>> { return params_; }
  280. auto self() const -> Nonnull<const GenericBinding*> { return self_; }
  281. auto self() -> Nonnull<GenericBinding*> { return self_; }
  282. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  283. return members_;
  284. }
  285. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  286. private:
  287. std::string name_;
  288. std::optional<Nonnull<TuplePattern*>> params_;
  289. Nonnull<GenericBinding*> self_;
  290. std::vector<Nonnull<Declaration*>> members_;
  291. };
  292. // EXPERIMENTAL MIXIN FEATURE
  293. class MixDeclaration : public Declaration {
  294. public:
  295. MixDeclaration(SourceLocation source_loc,
  296. std::optional<Nonnull<Expression*>> mixin_type)
  297. : Declaration(AstNodeKind::MixDeclaration, source_loc),
  298. mixin_(mixin_type) {}
  299. static auto classof(const AstNode* node) -> bool {
  300. return InheritsFromMixDeclaration(node->kind());
  301. }
  302. auto mixin() const -> const Expression& { return **mixin_; }
  303. auto mixin() -> Expression& { return **mixin_; }
  304. auto mixin_value() const -> const MixinPseudoType& { return *mixin_value_; }
  305. void set_mixin_value(Nonnull<const MixinPseudoType*> mixin_value) {
  306. mixin_value_ = mixin_value;
  307. }
  308. private:
  309. std::optional<Nonnull<Expression*>> mixin_;
  310. Nonnull<const MixinPseudoType*> mixin_value_;
  311. };
  312. class AlternativeSignature : public AstNode {
  313. public:
  314. AlternativeSignature(SourceLocation source_loc, std::string name,
  315. Nonnull<TupleLiteral*> signature)
  316. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  317. name_(std::move(name)),
  318. signature_(signature) {}
  319. void Print(llvm::raw_ostream& out) const override;
  320. void PrintID(llvm::raw_ostream& out) const override;
  321. static auto classof(const AstNode* node) -> bool {
  322. return InheritsFromAlternativeSignature(node->kind());
  323. }
  324. auto name() const -> const std::string& { return name_; }
  325. auto signature() const -> const TupleLiteral& { return *signature_; }
  326. auto signature() -> TupleLiteral& { return *signature_; }
  327. private:
  328. std::string name_;
  329. Nonnull<TupleLiteral*> signature_;
  330. };
  331. class ChoiceDeclaration : public Declaration {
  332. public:
  333. using ImplementsCarbonValueNode = void;
  334. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  335. std::optional<Nonnull<TuplePattern*>> type_params,
  336. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  337. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  338. name_(std::move(name)),
  339. type_params_(type_params),
  340. alternatives_(std::move(alternatives)) {}
  341. static auto classof(const AstNode* node) -> bool {
  342. return InheritsFromChoiceDeclaration(node->kind());
  343. }
  344. auto name() const -> const std::string& { return name_; }
  345. auto type_params() const -> std::optional<Nonnull<const TuplePattern*>> {
  346. return type_params_;
  347. }
  348. auto type_params() -> std::optional<Nonnull<TuplePattern*>> {
  349. return type_params_;
  350. }
  351. auto alternatives() const
  352. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  353. return alternatives_;
  354. }
  355. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  356. return alternatives_;
  357. }
  358. void set_members(const std::vector<NamedValue>& members) {
  359. members_ = members;
  360. }
  361. auto members() const -> std::vector<NamedValue> { return members_; }
  362. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  363. private:
  364. std::string name_;
  365. std::optional<Nonnull<TuplePattern*>> type_params_;
  366. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  367. std::vector<NamedValue> members_;
  368. };
  369. // Global variable definition implements the Declaration concept.
  370. class VariableDeclaration : public Declaration {
  371. public:
  372. VariableDeclaration(SourceLocation source_loc,
  373. Nonnull<BindingPattern*> binding,
  374. std::optional<Nonnull<Expression*>> initializer,
  375. ValueCategory value_category)
  376. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  377. binding_(binding),
  378. initializer_(initializer),
  379. value_category_(value_category) {}
  380. static auto classof(const AstNode* node) -> bool {
  381. return InheritsFromVariableDeclaration(node->kind());
  382. }
  383. auto binding() const -> const BindingPattern& { return *binding_; }
  384. auto binding() -> BindingPattern& { return *binding_; }
  385. auto initializer() const -> const Expression& { return **initializer_; }
  386. auto initializer() -> Expression& { return **initializer_; }
  387. auto value_category() const -> ValueCategory { return value_category_; }
  388. auto has_initializer() const -> bool { return initializer_.has_value(); }
  389. // Can only be called by type-checking, if a conversion was required.
  390. void set_initializer(Nonnull<Expression*> initializer) {
  391. CARBON_CHECK(has_initializer()) << "should not add a new initializer";
  392. initializer_ = initializer;
  393. }
  394. private:
  395. // TODO: split this into a non-optional name and a type, initialized by
  396. // a constructor that takes a BindingPattern and handles errors like a
  397. // missing name.
  398. Nonnull<BindingPattern*> binding_;
  399. std::optional<Nonnull<Expression*>> initializer_;
  400. ValueCategory value_category_;
  401. };
  402. class InterfaceDeclaration : public Declaration {
  403. public:
  404. using ImplementsCarbonValueNode = void;
  405. InterfaceDeclaration(Nonnull<Arena*> arena, SourceLocation source_loc,
  406. std::string name,
  407. std::optional<Nonnull<TuplePattern*>> params,
  408. std::vector<Nonnull<Declaration*>> members)
  409. : Declaration(AstNodeKind::InterfaceDeclaration, source_loc),
  410. name_(std::move(name)),
  411. params_(params),
  412. self_type_(arena->New<SelfDeclaration>(source_loc)),
  413. members_(std::move(members)) {
  414. // `interface X` has `Self:! X`.
  415. auto self_type_ref = arena->New<IdentifierExpression>(source_loc, name);
  416. self_type_ref->set_value_node(self_type_);
  417. self_ = arena->New<GenericBinding>(source_loc, "Self", self_type_ref);
  418. }
  419. static auto classof(const AstNode* node) -> bool {
  420. return InheritsFromInterfaceDeclaration(node->kind());
  421. }
  422. auto name() const -> const std::string& { return name_; }
  423. auto params() const -> std::optional<Nonnull<const TuplePattern*>> {
  424. return params_;
  425. }
  426. auto params() -> std::optional<Nonnull<TuplePattern*>> { return params_; }
  427. // Get the type of `Self`, which is a reference to the interface itself, with
  428. // parameters mapped to their values. For example, in `interface X(T:!
  429. // Type)`, the self type is `X(T)`.
  430. auto self_type() const -> Nonnull<const SelfDeclaration*> {
  431. return self_type_;
  432. }
  433. auto self_type() -> Nonnull<SelfDeclaration*> { return self_type_; }
  434. auto self() const -> Nonnull<const GenericBinding*> { return self_; }
  435. auto self() -> Nonnull<GenericBinding*> { return self_; }
  436. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  437. return members_;
  438. }
  439. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  440. // Get the constraint type corresponding to this interface, or nullopt if
  441. // this interface is incomplete.
  442. auto constraint_type() const
  443. -> std::optional<Nonnull<const ConstraintType*>> {
  444. return constraint_type_;
  445. }
  446. // Set the constraint type corresponding to this interface. Can only be set
  447. // once, by type-checking.
  448. void set_constraint_type(Nonnull<const ConstraintType*> constraint_type) {
  449. CARBON_CHECK(!constraint_type_);
  450. constraint_type_ = constraint_type;
  451. }
  452. private:
  453. std::string name_;
  454. std::optional<Nonnull<TuplePattern*>> params_;
  455. Nonnull<SelfDeclaration*> self_type_;
  456. Nonnull<GenericBinding*> self_;
  457. std::vector<Nonnull<Declaration*>> members_;
  458. std::optional<Nonnull<const ConstraintType*>> constraint_type_;
  459. };
  460. // An `extends` declaration in an interface.
  461. class InterfaceExtendsDeclaration : public Declaration {
  462. public:
  463. InterfaceExtendsDeclaration(SourceLocation source_loc,
  464. Nonnull<Expression*> base)
  465. : Declaration(AstNodeKind::InterfaceExtendsDeclaration, source_loc),
  466. base_(base) {}
  467. static auto classof(const AstNode* node) -> bool {
  468. return InheritsFromInterfaceExtendsDeclaration(node->kind());
  469. }
  470. auto base() const -> const Expression* { return base_; }
  471. auto base() -> Expression* { return base_; }
  472. private:
  473. Nonnull<Expression*> base_;
  474. };
  475. // An `impl ... as` declaration in an interface.
  476. class InterfaceImplDeclaration : public Declaration {
  477. public:
  478. InterfaceImplDeclaration(SourceLocation source_loc,
  479. Nonnull<Expression*> impl_type,
  480. Nonnull<Expression*> constraint)
  481. : Declaration(AstNodeKind::InterfaceImplDeclaration, source_loc),
  482. impl_type_(impl_type),
  483. constraint_(constraint) {}
  484. static auto classof(const AstNode* node) -> bool {
  485. return InheritsFromInterfaceImplDeclaration(node->kind());
  486. }
  487. auto impl_type() const -> const Expression* { return impl_type_; }
  488. auto impl_type() -> Expression* { return impl_type_; }
  489. auto constraint() const -> const Expression* { return constraint_; }
  490. auto constraint() -> Expression* { return constraint_; }
  491. private:
  492. Nonnull<Expression*> impl_type_;
  493. Nonnull<Expression*> constraint_;
  494. };
  495. class AssociatedConstantDeclaration : public Declaration {
  496. public:
  497. AssociatedConstantDeclaration(SourceLocation source_loc,
  498. Nonnull<GenericBinding*> binding)
  499. : Declaration(AstNodeKind::AssociatedConstantDeclaration, source_loc),
  500. binding_(binding) {}
  501. static auto classof(const AstNode* node) -> bool {
  502. return InheritsFromAssociatedConstantDeclaration(node->kind());
  503. }
  504. auto binding() const -> const GenericBinding& { return *binding_; }
  505. auto binding() -> GenericBinding& { return *binding_; }
  506. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  507. private:
  508. Nonnull<GenericBinding*> binding_;
  509. };
  510. enum class ImplKind { InternalImpl, ExternalImpl };
  511. class ImplDeclaration : public Declaration {
  512. public:
  513. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  514. ImplKind kind, Nonnull<Expression*> impl_type,
  515. Nonnull<Expression*> interface,
  516. std::vector<Nonnull<AstNode*>> deduced_params,
  517. std::vector<Nonnull<Declaration*>> members)
  518. -> ErrorOr<Nonnull<ImplDeclaration*>>;
  519. // Use `Create` instead.
  520. ImplDeclaration(SourceLocation source_loc, ImplKind kind,
  521. Nonnull<Expression*> impl_type,
  522. Nonnull<SelfDeclaration*> self_decl,
  523. Nonnull<Expression*> interface,
  524. std::vector<Nonnull<GenericBinding*>> deduced_params,
  525. std::vector<Nonnull<Declaration*>> members)
  526. : Declaration(AstNodeKind::ImplDeclaration, source_loc),
  527. kind_(kind),
  528. impl_type_(impl_type),
  529. self_decl_(self_decl),
  530. interface_(interface),
  531. deduced_parameters_(std::move(deduced_params)),
  532. members_(std::move(members)) {}
  533. static auto classof(const AstNode* node) -> bool {
  534. return InheritsFromImplDeclaration(node->kind());
  535. }
  536. // Return whether this is an external or internal impl.
  537. auto kind() const -> ImplKind { return kind_; }
  538. // Return the type that is doing the implementing.
  539. auto impl_type() const -> Nonnull<Expression*> { return impl_type_; }
  540. // Return the interface that is being implemented.
  541. auto interface() const -> const Expression& { return *interface_; }
  542. auto interface() -> Expression& { return *interface_; }
  543. void set_constraint_type(Nonnull<const ConstraintType*> constraint_type) {
  544. constraint_type_ = constraint_type;
  545. }
  546. auto constraint_type() const -> Nonnull<const ConstraintType*> {
  547. return *constraint_type_;
  548. }
  549. // Returns the deduced parameters specified on the impl declaration. This
  550. // does not include any generic parameters from enclosing scopes.
  551. auto deduced_parameters() const
  552. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  553. return deduced_parameters_;
  554. }
  555. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  556. return deduced_parameters_;
  557. }
  558. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  559. return members_;
  560. }
  561. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  562. void set_impl_bindings(llvm::ArrayRef<Nonnull<const ImplBinding*>> imps) {
  563. impl_bindings_ = imps;
  564. }
  565. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  566. return impl_bindings_;
  567. }
  568. auto self() const -> Nonnull<const SelfDeclaration*> { return self_decl_; }
  569. auto self() -> Nonnull<SelfDeclaration*> { return self_decl_; }
  570. private:
  571. ImplKind kind_;
  572. Nonnull<Expression*> impl_type_;
  573. Nonnull<SelfDeclaration*> self_decl_;
  574. Nonnull<Expression*> interface_;
  575. std::optional<Nonnull<const ConstraintType*>> constraint_type_;
  576. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  577. std::vector<Nonnull<Declaration*>> members_;
  578. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  579. };
  580. class AliasDeclaration : public Declaration {
  581. public:
  582. using ImplementsCarbonValueNode = void;
  583. explicit AliasDeclaration(SourceLocation source_loc, std::string name,
  584. Nonnull<Expression*> target)
  585. : Declaration(AstNodeKind::AliasDeclaration, source_loc),
  586. name_(std::move(name)),
  587. target_(target) {}
  588. static auto classof(const AstNode* node) -> bool {
  589. return InheritsFromAliasDeclaration(node->kind());
  590. }
  591. auto name() const -> const std::string& { return name_; }
  592. auto target() const -> const Expression& { return *target_; }
  593. auto target() -> Expression& { return *target_; }
  594. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  595. private:
  596. std::string name_;
  597. Nonnull<Expression*> target_;
  598. };
  599. // Return the name of a declaration, if it has one.
  600. auto GetName(const Declaration&) -> std::optional<std::string_view>;
  601. } // namespace Carbon
  602. #endif // CARBON_EXPLORER_AST_DECLARATION_H_