declaration.h 28 KB

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