declaration.h 16 KB

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