declaration.h 16 KB

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