declaration.h 17 KB

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