declaration.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 ConstraintType;
  25. // Abstract base class of all AST nodes representing patterns.
  26. //
  27. // Declaration and its derived classes support LLVM-style RTTI, including
  28. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  29. // class derived from Declaration must provide a `classof` operation, and
  30. // every concrete derived class must have a corresponding enumerator
  31. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  32. // details.
  33. class Declaration : public AstNode {
  34. public:
  35. ~Declaration() override = 0;
  36. Declaration(const Declaration&) = delete;
  37. auto operator=(const Declaration&) -> Declaration& = delete;
  38. void Print(llvm::raw_ostream& out) const override;
  39. void PrintID(llvm::raw_ostream& out) const override;
  40. static auto classof(const AstNode* node) -> bool {
  41. return InheritsFromDeclaration(node->kind());
  42. }
  43. // Returns the enumerator corresponding to the most-derived type of this
  44. // object.
  45. auto kind() const -> DeclarationKind {
  46. return static_cast<DeclarationKind>(root_kind());
  47. }
  48. // The static type of the declared entity. Cannot be called before
  49. // typechecking.
  50. auto static_type() const -> const Value& { return **static_type_; }
  51. // Sets the static type of the declared entity. Can only be called once,
  52. // during typechecking.
  53. void set_static_type(Nonnull<const Value*> type) {
  54. CARBON_CHECK(!static_type_.has_value());
  55. static_type_ = type;
  56. }
  57. // Returns whether the static type has been set. Should only be called
  58. // during typechecking: before typechecking it's guaranteed to be false,
  59. // and after typechecking it's guaranteed to be true.
  60. auto has_static_type() const -> bool { return static_type_.has_value(); }
  61. // Sets the value returned by constant_value(). Can only be called once,
  62. // during typechecking.
  63. void set_constant_value(Nonnull<const Value*> value) {
  64. CARBON_CHECK(!constant_value_.has_value());
  65. constant_value_ = value;
  66. }
  67. // See static_scope.h for API.
  68. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  69. return constant_value_;
  70. }
  71. // See static_scope.h for API.
  72. auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
  73. return constant_value_;
  74. }
  75. protected:
  76. // Constructs a Declaration representing syntax at the given line number.
  77. // `kind` must be the enumerator corresponding to the most-derived type being
  78. // constructed.
  79. Declaration(AstNodeKind kind, SourceLocation source_loc)
  80. : AstNode(kind, source_loc) {}
  81. private:
  82. std::optional<Nonnull<const Value*>> static_type_;
  83. std::optional<Nonnull<const Value*>> constant_value_;
  84. };
  85. class FunctionDeclaration : public Declaration {
  86. public:
  87. using ImplementsCarbonValueNode = void;
  88. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  89. std::string name,
  90. std::vector<Nonnull<AstNode*>> deduced_params,
  91. std::optional<Nonnull<Pattern*>> me_pattern,
  92. Nonnull<TuplePattern*> param_pattern,
  93. ReturnTerm return_term,
  94. std::optional<Nonnull<Block*>> body)
  95. -> ErrorOr<Nonnull<FunctionDeclaration*>>;
  96. // Use `Create()` instead. This is public only so Arena::New() can call it.
  97. FunctionDeclaration(SourceLocation source_loc, std::string name,
  98. std::vector<Nonnull<GenericBinding*>> deduced_params,
  99. std::optional<Nonnull<Pattern*>> me_pattern,
  100. Nonnull<TuplePattern*> param_pattern,
  101. ReturnTerm return_term,
  102. std::optional<Nonnull<Block*>> body)
  103. : Declaration(AstNodeKind::FunctionDeclaration, source_loc),
  104. name_(std::move(name)),
  105. deduced_parameters_(std::move(deduced_params)),
  106. me_pattern_(me_pattern),
  107. param_pattern_(param_pattern),
  108. return_term_(return_term),
  109. body_(body) {}
  110. static auto classof(const AstNode* node) -> bool {
  111. return InheritsFromFunctionDeclaration(node->kind());
  112. }
  113. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  114. auto name() const -> const std::string& { return name_; }
  115. auto deduced_parameters() const
  116. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  117. return deduced_parameters_;
  118. }
  119. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  120. return deduced_parameters_;
  121. }
  122. auto me_pattern() const -> const Pattern& { return **me_pattern_; }
  123. auto me_pattern() -> Pattern& { return **me_pattern_; }
  124. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  125. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  126. auto return_term() const -> const ReturnTerm& { return return_term_; }
  127. auto return_term() -> ReturnTerm& { return return_term_; }
  128. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  129. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  130. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  131. auto is_method() const -> bool { return me_pattern_.has_value(); }
  132. private:
  133. std::string name_;
  134. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  135. std::optional<Nonnull<Pattern*>> me_pattern_;
  136. Nonnull<TuplePattern*> param_pattern_;
  137. ReturnTerm return_term_;
  138. std::optional<Nonnull<Block*>> body_;
  139. };
  140. class SelfDeclaration : public Declaration {
  141. public:
  142. using ImplementsCarbonValueNode = void;
  143. explicit SelfDeclaration(SourceLocation source_loc)
  144. : Declaration(AstNodeKind::SelfDeclaration, source_loc) {}
  145. static auto classof(const AstNode* node) -> bool {
  146. return InheritsFromSelfDeclaration(node->kind());
  147. }
  148. static auto name() -> std::string_view { return "Self"; }
  149. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  150. };
  151. enum class ClassExtensibility { None, Base, Abstract };
  152. class ClassDeclaration : public Declaration {
  153. public:
  154. using ImplementsCarbonValueNode = void;
  155. ClassDeclaration(SourceLocation source_loc, std::string name,
  156. Nonnull<SelfDeclaration*> self_decl,
  157. ClassExtensibility extensibility,
  158. std::optional<Nonnull<TuplePattern*>> type_params,
  159. std::optional<Nonnull<Expression*>> extends,
  160. std::vector<Nonnull<Declaration*>> members)
  161. : Declaration(AstNodeKind::ClassDeclaration, source_loc),
  162. name_(std::move(name)),
  163. extensibility_(extensibility),
  164. self_decl_(self_decl),
  165. type_params_(type_params),
  166. extends_(extends),
  167. members_(std::move(members)) {}
  168. static auto classof(const AstNode* node) -> bool {
  169. return InheritsFromClassDeclaration(node->kind());
  170. }
  171. auto name() const -> const std::string& { return name_; }
  172. auto extensibility() const -> ClassExtensibility { return extensibility_; }
  173. auto type_params() const -> std::optional<Nonnull<const TuplePattern*>> {
  174. return type_params_;
  175. }
  176. auto type_params() -> std::optional<Nonnull<TuplePattern*>> {
  177. return type_params_;
  178. }
  179. auto extends() const -> std::optional<Nonnull<Expression*>> {
  180. return extends_;
  181. }
  182. auto self() const -> Nonnull<const SelfDeclaration*> { return self_decl_; }
  183. auto self() -> Nonnull<SelfDeclaration*> { return self_decl_; }
  184. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  185. return members_;
  186. }
  187. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  188. private:
  189. std::string name_;
  190. ClassExtensibility extensibility_;
  191. Nonnull<SelfDeclaration*> self_decl_;
  192. std::optional<Nonnull<TuplePattern*>> type_params_;
  193. std::optional<Nonnull<Expression*>> extends_;
  194. std::vector<Nonnull<Declaration*>> members_;
  195. };
  196. class AlternativeSignature : public AstNode {
  197. public:
  198. AlternativeSignature(SourceLocation source_loc, std::string name,
  199. Nonnull<TupleLiteral*> signature)
  200. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  201. name_(std::move(name)),
  202. signature_(signature) {}
  203. void Print(llvm::raw_ostream& out) const override;
  204. void PrintID(llvm::raw_ostream& out) const override;
  205. static auto classof(const AstNode* node) -> bool {
  206. return InheritsFromAlternativeSignature(node->kind());
  207. }
  208. auto name() const -> const std::string& { return name_; }
  209. auto signature() const -> const TupleLiteral& { return *signature_; }
  210. auto signature() -> TupleLiteral& { return *signature_; }
  211. private:
  212. std::string name_;
  213. Nonnull<TupleLiteral*> signature_;
  214. };
  215. class ChoiceDeclaration : public Declaration {
  216. public:
  217. using ImplementsCarbonValueNode = void;
  218. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  219. std::optional<Nonnull<TuplePattern*>> type_params,
  220. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  221. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  222. name_(std::move(name)),
  223. type_params_(type_params),
  224. alternatives_(std::move(alternatives)) {}
  225. static auto classof(const AstNode* node) -> bool {
  226. return InheritsFromChoiceDeclaration(node->kind());
  227. }
  228. auto name() const -> const std::string& { return name_; }
  229. auto type_params() const -> std::optional<Nonnull<const TuplePattern*>> {
  230. return type_params_;
  231. }
  232. auto type_params() -> std::optional<Nonnull<TuplePattern*>> {
  233. return type_params_;
  234. }
  235. auto alternatives() const
  236. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  237. return alternatives_;
  238. }
  239. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  240. return alternatives_;
  241. }
  242. void set_members(const std::vector<NamedValue>& members) {
  243. members_ = members;
  244. }
  245. auto members() const -> std::vector<NamedValue> { return members_; }
  246. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  247. private:
  248. std::string name_;
  249. std::optional<Nonnull<TuplePattern*>> type_params_;
  250. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  251. std::vector<NamedValue> members_;
  252. };
  253. // Global variable definition implements the Declaration concept.
  254. class VariableDeclaration : public Declaration {
  255. public:
  256. VariableDeclaration(SourceLocation source_loc,
  257. Nonnull<BindingPattern*> binding,
  258. std::optional<Nonnull<Expression*>> initializer,
  259. ValueCategory value_category)
  260. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  261. binding_(binding),
  262. initializer_(initializer),
  263. value_category_(value_category) {}
  264. static auto classof(const AstNode* node) -> bool {
  265. return InheritsFromVariableDeclaration(node->kind());
  266. }
  267. auto binding() const -> const BindingPattern& { return *binding_; }
  268. auto binding() -> BindingPattern& { return *binding_; }
  269. auto initializer() const -> const Expression& { return **initializer_; }
  270. auto initializer() -> Expression& { return **initializer_; }
  271. auto value_category() const -> ValueCategory { return value_category_; }
  272. auto has_initializer() const -> bool { return initializer_.has_value(); }
  273. // Can only be called by type-checking, if a conversion was required.
  274. void set_initializer(Nonnull<Expression*> initializer) {
  275. CARBON_CHECK(has_initializer()) << "should not add a new initializer";
  276. initializer_ = initializer;
  277. }
  278. private:
  279. // TODO: split this into a non-optional name and a type, initialized by
  280. // a constructor that takes a BindingPattern and handles errors like a
  281. // missing name.
  282. Nonnull<BindingPattern*> binding_;
  283. std::optional<Nonnull<Expression*>> initializer_;
  284. ValueCategory value_category_;
  285. };
  286. class InterfaceDeclaration : public Declaration {
  287. public:
  288. using ImplementsCarbonValueNode = void;
  289. InterfaceDeclaration(SourceLocation source_loc, std::string name,
  290. std::optional<Nonnull<TuplePattern*>> params,
  291. Nonnull<GenericBinding*> self,
  292. std::vector<Nonnull<Declaration*>> members)
  293. : Declaration(AstNodeKind::InterfaceDeclaration, source_loc),
  294. name_(std::move(name)),
  295. params_(std::move(params)),
  296. self_(self),
  297. members_(std::move(members)) {}
  298. static auto classof(const AstNode* node) -> bool {
  299. return InheritsFromInterfaceDeclaration(node->kind());
  300. }
  301. auto name() const -> const std::string& { return name_; }
  302. auto params() const -> std::optional<Nonnull<const TuplePattern*>> {
  303. return params_;
  304. }
  305. auto params() -> std::optional<Nonnull<TuplePattern*>> { return params_; }
  306. auto self() const -> Nonnull<const GenericBinding*> { return self_; }
  307. auto self() -> Nonnull<GenericBinding*> { return self_; }
  308. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  309. return members_;
  310. }
  311. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  312. private:
  313. std::string name_;
  314. std::optional<Nonnull<TuplePattern*>> params_;
  315. Nonnull<GenericBinding*> self_;
  316. std::vector<Nonnull<Declaration*>> members_;
  317. };
  318. class AssociatedConstantDeclaration : public Declaration {
  319. public:
  320. AssociatedConstantDeclaration(SourceLocation source_loc,
  321. Nonnull<GenericBinding*> binding)
  322. : Declaration(AstNodeKind::AssociatedConstantDeclaration, source_loc),
  323. binding_(binding) {}
  324. static auto classof(const AstNode* node) -> bool {
  325. return InheritsFromAssociatedConstantDeclaration(node->kind());
  326. }
  327. auto binding() const -> const GenericBinding& { return *binding_; }
  328. auto binding() -> GenericBinding& { return *binding_; }
  329. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  330. private:
  331. Nonnull<GenericBinding*> binding_;
  332. };
  333. enum class ImplKind { InternalImpl, ExternalImpl };
  334. class ImplDeclaration : public Declaration {
  335. public:
  336. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  337. ImplKind kind, Nonnull<Expression*> impl_type,
  338. Nonnull<Expression*> interface,
  339. std::vector<Nonnull<AstNode*>> deduced_params,
  340. std::vector<Nonnull<Declaration*>> members)
  341. -> ErrorOr<Nonnull<ImplDeclaration*>>;
  342. // Use `Create` instead.
  343. ImplDeclaration(SourceLocation source_loc, ImplKind kind,
  344. Nonnull<Expression*> impl_type,
  345. Nonnull<SelfDeclaration*> self_decl,
  346. Nonnull<Expression*> interface,
  347. std::vector<Nonnull<GenericBinding*>> deduced_params,
  348. std::vector<Nonnull<Declaration*>> members)
  349. : Declaration(AstNodeKind::ImplDeclaration, source_loc),
  350. kind_(kind),
  351. impl_type_(impl_type),
  352. self_decl_(self_decl),
  353. interface_(interface),
  354. deduced_parameters_(std::move(deduced_params)),
  355. members_(std::move(members)) {}
  356. static auto classof(const AstNode* node) -> bool {
  357. return InheritsFromImplDeclaration(node->kind());
  358. }
  359. // Return whether this is an external or internal impl.
  360. auto kind() const -> ImplKind { return kind_; }
  361. // Return the type that is doing the implementing.
  362. auto impl_type() const -> Nonnull<Expression*> { return impl_type_; }
  363. // Return the interface that is being implemented.
  364. auto interface() const -> const Expression& { return *interface_; }
  365. auto interface() -> Expression& { return *interface_; }
  366. void set_constraint_type(Nonnull<const ConstraintType*> constraint_type) {
  367. constraint_type_ = constraint_type;
  368. }
  369. auto constraint_type() const -> Nonnull<const ConstraintType*> {
  370. return *constraint_type_;
  371. }
  372. auto deduced_parameters() const
  373. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  374. return deduced_parameters_;
  375. }
  376. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  377. return deduced_parameters_;
  378. }
  379. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  380. return members_;
  381. }
  382. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  383. void set_impl_bindings(llvm::ArrayRef<Nonnull<const ImplBinding*>> imps) {
  384. impl_bindings_ = imps;
  385. }
  386. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  387. return impl_bindings_;
  388. }
  389. auto self() const -> Nonnull<const SelfDeclaration*> { return self_decl_; }
  390. auto self() -> Nonnull<SelfDeclaration*> { return self_decl_; }
  391. private:
  392. ImplKind kind_;
  393. Nonnull<Expression*> impl_type_;
  394. Nonnull<SelfDeclaration*> self_decl_;
  395. Nonnull<Expression*> interface_;
  396. std::optional<Nonnull<const ConstraintType*>> constraint_type_;
  397. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  398. std::vector<Nonnull<Declaration*>> members_;
  399. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  400. };
  401. class AliasDeclaration : public Declaration {
  402. public:
  403. using ImplementsCarbonValueNode = void;
  404. explicit AliasDeclaration(SourceLocation source_loc, const std::string& name,
  405. Nonnull<Expression*> target)
  406. : Declaration(AstNodeKind::AliasDeclaration, source_loc),
  407. name_(name),
  408. target_(target) {}
  409. static auto classof(const AstNode* node) -> bool {
  410. return InheritsFromAliasDeclaration(node->kind());
  411. }
  412. auto name() const -> const std::string& { return name_; }
  413. auto target() const -> const Expression& { return *target_; }
  414. auto target() -> Expression& { return *target_; }
  415. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  416. private:
  417. std::string name_;
  418. Nonnull<Expression*> target_;
  419. };
  420. // Return the name of a declaration, if it has one.
  421. auto GetName(const Declaration&) -> std::optional<std::string_view>;
  422. } // namespace Carbon
  423. #endif // CARBON_EXPLORER_AST_DECLARATION_H_