declaration.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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*>> base,
  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. base_expr_(base),
  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 base_expr() const -> std::optional<Nonnull<Expression*>> {
  234. return base_expr_;
  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 (const 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. auto base() const -> std::optional<Nonnull<const ClassDeclaration*>> {
  251. return base_;
  252. }
  253. void set_base(Nonnull<const ClassDeclaration*> base_decl) {
  254. base_ = base_decl;
  255. }
  256. private:
  257. std::string name_;
  258. ClassExtensibility extensibility_;
  259. Nonnull<SelfDeclaration*> self_decl_;
  260. std::optional<Nonnull<TuplePattern*>> type_params_;
  261. std::optional<Nonnull<Expression*>> base_expr_;
  262. std::vector<Nonnull<Declaration*>> members_;
  263. std::optional<Nonnull<FunctionDeclaration*>> destructor_;
  264. std::optional<Nonnull<const ClassDeclaration*>> base_;
  265. };
  266. // EXPERIMENTAL MIXIN FEATURE
  267. class MixinDeclaration : public Declaration {
  268. public:
  269. using ImplementsCarbonValueNode = void;
  270. MixinDeclaration(SourceLocation source_loc, std::string name,
  271. std::optional<Nonnull<TuplePattern*>> params,
  272. Nonnull<GenericBinding*> self,
  273. std::vector<Nonnull<Declaration*>> members)
  274. : Declaration(AstNodeKind::MixinDeclaration, source_loc),
  275. name_(std::move(name)),
  276. params_(params),
  277. self_(self),
  278. members_(std::move(members)) {}
  279. static auto classof(const AstNode* node) -> bool {
  280. return InheritsFromMixinDeclaration(node->kind());
  281. }
  282. auto name() const -> const std::string& { return name_; }
  283. auto params() const -> std::optional<Nonnull<const TuplePattern*>> {
  284. return params_;
  285. }
  286. auto params() -> std::optional<Nonnull<TuplePattern*>> { return params_; }
  287. auto self() const -> Nonnull<const GenericBinding*> { return self_; }
  288. auto self() -> Nonnull<GenericBinding*> { return self_; }
  289. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  290. return members_;
  291. }
  292. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  293. private:
  294. std::string name_;
  295. std::optional<Nonnull<TuplePattern*>> params_;
  296. Nonnull<GenericBinding*> self_;
  297. std::vector<Nonnull<Declaration*>> members_;
  298. };
  299. // EXPERIMENTAL MIXIN FEATURE
  300. class MixDeclaration : public Declaration {
  301. public:
  302. MixDeclaration(SourceLocation source_loc,
  303. std::optional<Nonnull<Expression*>> mixin_type)
  304. : Declaration(AstNodeKind::MixDeclaration, source_loc),
  305. mixin_(mixin_type) {}
  306. static auto classof(const AstNode* node) -> bool {
  307. return InheritsFromMixDeclaration(node->kind());
  308. }
  309. auto mixin() const -> const Expression& { return **mixin_; }
  310. auto mixin() -> Expression& { return **mixin_; }
  311. auto mixin_value() const -> const MixinPseudoType& { return *mixin_value_; }
  312. void set_mixin_value(Nonnull<const MixinPseudoType*> mixin_value) {
  313. mixin_value_ = mixin_value;
  314. }
  315. private:
  316. std::optional<Nonnull<Expression*>> mixin_;
  317. Nonnull<const MixinPseudoType*> mixin_value_;
  318. };
  319. class AlternativeSignature : public AstNode {
  320. public:
  321. AlternativeSignature(SourceLocation source_loc, std::string name,
  322. Nonnull<TupleLiteral*> signature)
  323. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  324. name_(std::move(name)),
  325. signature_(signature) {}
  326. void Print(llvm::raw_ostream& out) const override;
  327. void PrintID(llvm::raw_ostream& out) const override;
  328. static auto classof(const AstNode* node) -> bool {
  329. return InheritsFromAlternativeSignature(node->kind());
  330. }
  331. auto name() const -> const std::string& { return name_; }
  332. auto signature() const -> const TupleLiteral& { return *signature_; }
  333. auto signature() -> TupleLiteral& { return *signature_; }
  334. private:
  335. std::string name_;
  336. Nonnull<TupleLiteral*> signature_;
  337. };
  338. class ChoiceDeclaration : public Declaration {
  339. public:
  340. using ImplementsCarbonValueNode = void;
  341. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  342. std::optional<Nonnull<TuplePattern*>> type_params,
  343. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  344. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  345. name_(std::move(name)),
  346. type_params_(type_params),
  347. alternatives_(std::move(alternatives)) {}
  348. static auto classof(const AstNode* node) -> bool {
  349. return InheritsFromChoiceDeclaration(node->kind());
  350. }
  351. auto name() const -> const std::string& { return name_; }
  352. auto type_params() const -> std::optional<Nonnull<const TuplePattern*>> {
  353. return type_params_;
  354. }
  355. auto type_params() -> std::optional<Nonnull<TuplePattern*>> {
  356. return type_params_;
  357. }
  358. auto alternatives() const
  359. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  360. return alternatives_;
  361. }
  362. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  363. return alternatives_;
  364. }
  365. void set_members(const std::vector<NamedValue>& members) {
  366. members_ = members;
  367. }
  368. auto members() const -> std::vector<NamedValue> { return members_; }
  369. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  370. private:
  371. std::string name_;
  372. std::optional<Nonnull<TuplePattern*>> type_params_;
  373. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  374. std::vector<NamedValue> members_;
  375. };
  376. // Global variable definition implements the Declaration concept.
  377. class VariableDeclaration : public Declaration {
  378. public:
  379. VariableDeclaration(SourceLocation source_loc,
  380. Nonnull<BindingPattern*> binding,
  381. std::optional<Nonnull<Expression*>> initializer,
  382. ValueCategory value_category)
  383. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  384. binding_(binding),
  385. initializer_(initializer),
  386. value_category_(value_category) {}
  387. static auto classof(const AstNode* node) -> bool {
  388. return InheritsFromVariableDeclaration(node->kind());
  389. }
  390. auto binding() const -> const BindingPattern& { return *binding_; }
  391. auto binding() -> BindingPattern& { return *binding_; }
  392. auto initializer() const -> const Expression& { return **initializer_; }
  393. auto initializer() -> Expression& { return **initializer_; }
  394. auto value_category() const -> ValueCategory { return value_category_; }
  395. auto has_initializer() const -> bool { return initializer_.has_value(); }
  396. // Can only be called by type-checking, if a conversion was required.
  397. void set_initializer(Nonnull<Expression*> initializer) {
  398. CARBON_CHECK(has_initializer()) << "should not add a new initializer";
  399. initializer_ = initializer;
  400. }
  401. private:
  402. // TODO: split this into a non-optional name and a type, initialized by
  403. // a constructor that takes a BindingPattern and handles errors like a
  404. // missing name.
  405. Nonnull<BindingPattern*> binding_;
  406. std::optional<Nonnull<Expression*>> initializer_;
  407. ValueCategory value_category_;
  408. };
  409. class InterfaceDeclaration : public Declaration {
  410. public:
  411. using ImplementsCarbonValueNode = void;
  412. InterfaceDeclaration(Nonnull<Arena*> arena, SourceLocation source_loc,
  413. std::string name,
  414. std::optional<Nonnull<TuplePattern*>> params,
  415. std::vector<Nonnull<Declaration*>> members)
  416. : Declaration(AstNodeKind::InterfaceDeclaration, source_loc),
  417. name_(std::move(name)),
  418. params_(params),
  419. self_type_(arena->New<SelfDeclaration>(source_loc)),
  420. members_(std::move(members)) {
  421. // `interface X` has `Self:! X`.
  422. auto* self_type_ref = arena->New<IdentifierExpression>(source_loc, name);
  423. self_type_ref->set_value_node(self_type_);
  424. self_ = arena->New<GenericBinding>(source_loc, "Self", self_type_ref);
  425. }
  426. static auto classof(const AstNode* node) -> bool {
  427. return InheritsFromInterfaceDeclaration(node->kind());
  428. }
  429. auto name() const -> const std::string& { return name_; }
  430. auto params() const -> std::optional<Nonnull<const TuplePattern*>> {
  431. return params_;
  432. }
  433. auto params() -> std::optional<Nonnull<TuplePattern*>> { return params_; }
  434. // Get the type of `Self`, which is a reference to the interface itself, with
  435. // parameters mapped to their values. For example, in `interface X(T:!
  436. // Type)`, the self type is `X(T)`.
  437. auto self_type() const -> Nonnull<const SelfDeclaration*> {
  438. return self_type_;
  439. }
  440. auto self_type() -> Nonnull<SelfDeclaration*> { return self_type_; }
  441. auto self() const -> Nonnull<const GenericBinding*> { return self_; }
  442. auto self() -> Nonnull<GenericBinding*> { return self_; }
  443. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  444. return members_;
  445. }
  446. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  447. // Get the constraint type corresponding to this interface, or nullopt if
  448. // this interface is incomplete.
  449. auto constraint_type() const
  450. -> std::optional<Nonnull<const ConstraintType*>> {
  451. return constraint_type_;
  452. }
  453. // Set the constraint type corresponding to this interface. Can only be set
  454. // once, by type-checking.
  455. void set_constraint_type(Nonnull<const ConstraintType*> constraint_type) {
  456. CARBON_CHECK(!constraint_type_);
  457. constraint_type_ = constraint_type;
  458. }
  459. private:
  460. std::string name_;
  461. std::optional<Nonnull<TuplePattern*>> params_;
  462. Nonnull<SelfDeclaration*> self_type_;
  463. Nonnull<GenericBinding*> self_;
  464. std::vector<Nonnull<Declaration*>> members_;
  465. std::optional<Nonnull<const ConstraintType*>> constraint_type_;
  466. };
  467. // An `extends` declaration in an interface.
  468. class InterfaceExtendsDeclaration : public Declaration {
  469. public:
  470. InterfaceExtendsDeclaration(SourceLocation source_loc,
  471. Nonnull<Expression*> base)
  472. : Declaration(AstNodeKind::InterfaceExtendsDeclaration, source_loc),
  473. base_(base) {}
  474. static auto classof(const AstNode* node) -> bool {
  475. return InheritsFromInterfaceExtendsDeclaration(node->kind());
  476. }
  477. auto base() const -> const Expression* { return base_; }
  478. auto base() -> Expression* { return base_; }
  479. private:
  480. Nonnull<Expression*> base_;
  481. };
  482. // An `impl ... as` declaration in an interface.
  483. class InterfaceImplDeclaration : public Declaration {
  484. public:
  485. InterfaceImplDeclaration(SourceLocation source_loc,
  486. Nonnull<Expression*> impl_type,
  487. Nonnull<Expression*> constraint)
  488. : Declaration(AstNodeKind::InterfaceImplDeclaration, source_loc),
  489. impl_type_(impl_type),
  490. constraint_(constraint) {}
  491. static auto classof(const AstNode* node) -> bool {
  492. return InheritsFromInterfaceImplDeclaration(node->kind());
  493. }
  494. auto impl_type() const -> const Expression* { return impl_type_; }
  495. auto impl_type() -> Expression* { return impl_type_; }
  496. auto constraint() const -> const Expression* { return constraint_; }
  497. auto constraint() -> Expression* { return constraint_; }
  498. private:
  499. Nonnull<Expression*> impl_type_;
  500. Nonnull<Expression*> constraint_;
  501. };
  502. class AssociatedConstantDeclaration : public Declaration {
  503. public:
  504. AssociatedConstantDeclaration(SourceLocation source_loc,
  505. Nonnull<GenericBinding*> binding)
  506. : Declaration(AstNodeKind::AssociatedConstantDeclaration, source_loc),
  507. binding_(binding) {}
  508. static auto classof(const AstNode* node) -> bool {
  509. return InheritsFromAssociatedConstantDeclaration(node->kind());
  510. }
  511. auto binding() const -> const GenericBinding& { return *binding_; }
  512. auto binding() -> GenericBinding& { return *binding_; }
  513. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  514. private:
  515. Nonnull<GenericBinding*> binding_;
  516. };
  517. enum class ImplKind { InternalImpl, ExternalImpl };
  518. class ImplDeclaration : public Declaration {
  519. public:
  520. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  521. ImplKind kind, Nonnull<Expression*> impl_type,
  522. Nonnull<Expression*> interface,
  523. std::vector<Nonnull<AstNode*>> deduced_params,
  524. std::vector<Nonnull<Declaration*>> members)
  525. -> ErrorOr<Nonnull<ImplDeclaration*>>;
  526. // Use `Create` instead.
  527. ImplDeclaration(SourceLocation source_loc, ImplKind kind,
  528. Nonnull<Expression*> impl_type,
  529. Nonnull<SelfDeclaration*> self_decl,
  530. Nonnull<Expression*> interface,
  531. std::vector<Nonnull<GenericBinding*>> deduced_params,
  532. std::vector<Nonnull<Declaration*>> members)
  533. : Declaration(AstNodeKind::ImplDeclaration, source_loc),
  534. kind_(kind),
  535. impl_type_(impl_type),
  536. self_decl_(self_decl),
  537. interface_(interface),
  538. deduced_parameters_(std::move(deduced_params)),
  539. members_(std::move(members)) {}
  540. static auto classof(const AstNode* node) -> bool {
  541. return InheritsFromImplDeclaration(node->kind());
  542. }
  543. // Return whether this is an external or internal impl.
  544. auto kind() const -> ImplKind { return kind_; }
  545. // Return the type that is doing the implementing.
  546. auto impl_type() const -> Nonnull<Expression*> { return impl_type_; }
  547. // Return the interface that is being implemented.
  548. auto interface() const -> const Expression& { return *interface_; }
  549. auto interface() -> Expression& { return *interface_; }
  550. void set_constraint_type(Nonnull<const ConstraintType*> constraint_type) {
  551. constraint_type_ = constraint_type;
  552. }
  553. auto constraint_type() const -> Nonnull<const ConstraintType*> {
  554. return *constraint_type_;
  555. }
  556. // Returns the deduced parameters specified on the impl declaration. This
  557. // does not include any generic parameters from enclosing scopes.
  558. auto deduced_parameters() const
  559. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  560. return deduced_parameters_;
  561. }
  562. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  563. return deduced_parameters_;
  564. }
  565. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  566. return members_;
  567. }
  568. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  569. void set_impl_bindings(llvm::ArrayRef<Nonnull<const ImplBinding*>> imps) {
  570. impl_bindings_ = imps;
  571. }
  572. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  573. return impl_bindings_;
  574. }
  575. auto self() const -> Nonnull<const SelfDeclaration*> { return self_decl_; }
  576. auto self() -> Nonnull<SelfDeclaration*> { return self_decl_; }
  577. private:
  578. ImplKind kind_;
  579. Nonnull<Expression*> impl_type_;
  580. Nonnull<SelfDeclaration*> self_decl_;
  581. Nonnull<Expression*> interface_;
  582. std::optional<Nonnull<const ConstraintType*>> constraint_type_;
  583. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  584. std::vector<Nonnull<Declaration*>> members_;
  585. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  586. };
  587. class AliasDeclaration : public Declaration {
  588. public:
  589. using ImplementsCarbonValueNode = void;
  590. explicit AliasDeclaration(SourceLocation source_loc, std::string name,
  591. Nonnull<Expression*> target)
  592. : Declaration(AstNodeKind::AliasDeclaration, source_loc),
  593. name_(std::move(name)),
  594. target_(target) {}
  595. static auto classof(const AstNode* node) -> bool {
  596. return InheritsFromAliasDeclaration(node->kind());
  597. }
  598. auto name() const -> const std::string& { return name_; }
  599. auto target() const -> const Expression& { return *target_; }
  600. auto target() -> Expression& { return *target_; }
  601. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  602. private:
  603. std::string name_;
  604. Nonnull<Expression*> target_;
  605. };
  606. // Return the name of a declaration, if it has one.
  607. auto GetName(const Declaration&) -> std::optional<std::string_view>;
  608. } // namespace Carbon
  609. #endif // CARBON_EXPLORER_AST_DECLARATION_H_