declaration.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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::vector<Nonnull<AlternativeSignature*>> alternatives)
  220. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  221. name_(std::move(name)),
  222. alternatives_(std::move(alternatives)) {}
  223. static auto classof(const AstNode* node) -> bool {
  224. return InheritsFromChoiceDeclaration(node->kind());
  225. }
  226. auto name() const -> const std::string& { return name_; }
  227. auto alternatives() const
  228. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  229. return alternatives_;
  230. }
  231. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  232. return alternatives_;
  233. }
  234. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  235. private:
  236. std::string name_;
  237. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  238. };
  239. // Global variable definition implements the Declaration concept.
  240. class VariableDeclaration : public Declaration {
  241. public:
  242. VariableDeclaration(SourceLocation source_loc,
  243. Nonnull<BindingPattern*> binding,
  244. std::optional<Nonnull<Expression*>> initializer,
  245. ValueCategory value_category)
  246. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  247. binding_(binding),
  248. initializer_(initializer),
  249. value_category_(value_category) {}
  250. static auto classof(const AstNode* node) -> bool {
  251. return InheritsFromVariableDeclaration(node->kind());
  252. }
  253. auto binding() const -> const BindingPattern& { return *binding_; }
  254. auto binding() -> BindingPattern& { return *binding_; }
  255. auto initializer() const -> const Expression& { return **initializer_; }
  256. auto initializer() -> Expression& { return **initializer_; }
  257. auto value_category() const -> ValueCategory { return value_category_; }
  258. auto has_initializer() const -> bool { return initializer_.has_value(); }
  259. // Can only be called by type-checking, if a conversion was required.
  260. void set_initializer(Nonnull<Expression*> initializer) {
  261. CARBON_CHECK(has_initializer()) << "should not add a new initializer";
  262. initializer_ = initializer;
  263. }
  264. private:
  265. // TODO: split this into a non-optional name and a type, initialized by
  266. // a constructor that takes a BindingPattern and handles errors like a
  267. // missing name.
  268. Nonnull<BindingPattern*> binding_;
  269. std::optional<Nonnull<Expression*>> initializer_;
  270. ValueCategory value_category_;
  271. };
  272. class InterfaceDeclaration : public Declaration {
  273. public:
  274. using ImplementsCarbonValueNode = void;
  275. InterfaceDeclaration(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::InterfaceDeclaration, source_loc),
  280. name_(std::move(name)),
  281. params_(std::move(params)),
  282. self_(self),
  283. members_(std::move(members)) {}
  284. static auto classof(const AstNode* node) -> bool {
  285. return InheritsFromInterfaceDeclaration(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. class AssociatedConstantDeclaration : public Declaration {
  305. public:
  306. AssociatedConstantDeclaration(SourceLocation source_loc,
  307. Nonnull<GenericBinding*> binding)
  308. : Declaration(AstNodeKind::AssociatedConstantDeclaration, source_loc),
  309. binding_(binding) {}
  310. static auto classof(const AstNode* node) -> bool {
  311. return InheritsFromAssociatedConstantDeclaration(node->kind());
  312. }
  313. auto binding() const -> const GenericBinding& { return *binding_; }
  314. auto binding() -> GenericBinding& { return *binding_; }
  315. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  316. private:
  317. Nonnull<GenericBinding*> binding_;
  318. };
  319. enum class ImplKind { InternalImpl, ExternalImpl };
  320. class ImplDeclaration : public Declaration {
  321. public:
  322. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  323. ImplKind kind, Nonnull<Expression*> impl_type,
  324. Nonnull<Expression*> interface,
  325. std::vector<Nonnull<AstNode*>> deduced_params,
  326. std::vector<Nonnull<Declaration*>> members)
  327. -> ErrorOr<Nonnull<ImplDeclaration*>>;
  328. // Use `Create` instead.
  329. ImplDeclaration(SourceLocation source_loc, ImplKind kind,
  330. Nonnull<Expression*> impl_type,
  331. Nonnull<SelfDeclaration*> self_decl,
  332. Nonnull<Expression*> interface,
  333. std::vector<Nonnull<GenericBinding*>> deduced_params,
  334. std::vector<Nonnull<Declaration*>> members)
  335. : Declaration(AstNodeKind::ImplDeclaration, source_loc),
  336. kind_(kind),
  337. impl_type_(impl_type),
  338. self_decl_(self_decl),
  339. interface_(interface),
  340. deduced_parameters_(std::move(deduced_params)),
  341. members_(std::move(members)) {}
  342. static auto classof(const AstNode* node) -> bool {
  343. return InheritsFromImplDeclaration(node->kind());
  344. }
  345. // Return whether this is an external or internal impl.
  346. auto kind() const -> ImplKind { return kind_; }
  347. // Return the type that is doing the implementing.
  348. auto impl_type() const -> Nonnull<Expression*> { return impl_type_; }
  349. // Return the interface that is being implemented.
  350. auto interface() const -> const Expression& { return *interface_; }
  351. auto interface() -> Expression& { return *interface_; }
  352. void set_constraint_type(Nonnull<const ConstraintType*> constraint_type) {
  353. constraint_type_ = constraint_type;
  354. }
  355. auto constraint_type() const -> Nonnull<const ConstraintType*> {
  356. return *constraint_type_;
  357. }
  358. auto deduced_parameters() const
  359. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  360. return deduced_parameters_;
  361. }
  362. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  363. return deduced_parameters_;
  364. }
  365. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  366. return members_;
  367. }
  368. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  369. void set_impl_bindings(llvm::ArrayRef<Nonnull<const ImplBinding*>> imps) {
  370. impl_bindings_ = imps;
  371. }
  372. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  373. return impl_bindings_;
  374. }
  375. auto self() const -> Nonnull<const SelfDeclaration*> { return self_decl_; }
  376. auto self() -> Nonnull<SelfDeclaration*> { return self_decl_; }
  377. private:
  378. ImplKind kind_;
  379. Nonnull<Expression*> impl_type_;
  380. Nonnull<SelfDeclaration*> self_decl_;
  381. Nonnull<Expression*> interface_;
  382. std::optional<Nonnull<const ConstraintType*>> constraint_type_;
  383. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  384. std::vector<Nonnull<Declaration*>> members_;
  385. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  386. };
  387. class AliasDeclaration : public Declaration {
  388. public:
  389. using ImplementsCarbonValueNode = void;
  390. explicit AliasDeclaration(SourceLocation source_loc, const std::string& name,
  391. Nonnull<Expression*> target)
  392. : Declaration(AstNodeKind::AliasDeclaration, source_loc),
  393. name_(name),
  394. target_(target) {}
  395. static auto classof(const AstNode* node) -> bool {
  396. return InheritsFromAliasDeclaration(node->kind());
  397. }
  398. auto name() const -> const std::string& { return name_; }
  399. auto target() const -> const Expression& { return *target_; }
  400. auto target() -> Expression& { return *target_; }
  401. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  402. private:
  403. std::string name_;
  404. Nonnull<Expression*> target_;
  405. };
  406. // Return the name of a declaration, if it has one.
  407. auto GetName(const Declaration&) -> std::optional<std::string_view>;
  408. } // namespace Carbon
  409. #endif // CARBON_EXPLORER_AST_DECLARATION_H_