declaration.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
  5. #define EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/ostream.h"
  10. #include "executable_semantics/ast/ast_node.h"
  11. #include "executable_semantics/ast/impl_binding.h"
  12. #include "executable_semantics/ast/pattern.h"
  13. #include "executable_semantics/ast/return_term.h"
  14. #include "executable_semantics/ast/statement.h"
  15. #include "executable_semantics/ast/static_scope.h"
  16. #include "executable_semantics/ast/value_category.h"
  17. #include "executable_semantics/common/nonnull.h"
  18. #include "executable_semantics/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. 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. 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 ClassDeclaration : public Declaration {
  138. public:
  139. using ImplementsCarbonValueNode = void;
  140. ClassDeclaration(SourceLocation source_loc, std::string name,
  141. std::optional<Nonnull<TuplePattern*>> type_params,
  142. std::vector<Nonnull<Declaration*>> members)
  143. : Declaration(AstNodeKind::ClassDeclaration, source_loc),
  144. name_(std::move(name)),
  145. type_params_(type_params),
  146. members_(std::move(members)) {}
  147. static auto classof(const AstNode* node) -> bool {
  148. return InheritsFromClassDeclaration(node->kind());
  149. }
  150. auto name() const -> const std::string& { return name_; }
  151. auto type_params() const -> std::optional<Nonnull<const TuplePattern*>> {
  152. return type_params_;
  153. }
  154. auto type_params() -> std::optional<Nonnull<TuplePattern*>> {
  155. return type_params_;
  156. }
  157. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  158. return members_;
  159. }
  160. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  161. private:
  162. std::string name_;
  163. std::optional<Nonnull<TuplePattern*>> type_params_;
  164. std::vector<Nonnull<Declaration*>> members_;
  165. };
  166. class AlternativeSignature : public AstNode {
  167. public:
  168. AlternativeSignature(SourceLocation source_loc, std::string name,
  169. Nonnull<Expression*> signature)
  170. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  171. name_(std::move(name)),
  172. signature_(signature) {}
  173. void Print(llvm::raw_ostream& out) const override;
  174. void PrintID(llvm::raw_ostream& out) const override;
  175. static auto classof(const AstNode* node) -> bool {
  176. return InheritsFromAlternativeSignature(node->kind());
  177. }
  178. auto name() const -> const std::string& { return name_; }
  179. auto signature() const -> const Expression& { return *signature_; }
  180. auto signature() -> Expression& { return *signature_; }
  181. private:
  182. std::string name_;
  183. Nonnull<Expression*> signature_;
  184. };
  185. class ChoiceDeclaration : public Declaration {
  186. public:
  187. using ImplementsCarbonValueNode = void;
  188. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  189. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  190. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  191. name_(std::move(name)),
  192. alternatives_(std::move(alternatives)) {}
  193. static auto classof(const AstNode* node) -> bool {
  194. return InheritsFromChoiceDeclaration(node->kind());
  195. }
  196. auto name() const -> const std::string& { return name_; }
  197. auto alternatives() const
  198. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  199. return alternatives_;
  200. }
  201. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  202. return alternatives_;
  203. }
  204. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  205. private:
  206. std::string name_;
  207. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  208. };
  209. // Global variable definition implements the Declaration concept.
  210. class VariableDeclaration : public Declaration {
  211. public:
  212. VariableDeclaration(SourceLocation source_loc,
  213. Nonnull<BindingPattern*> binding,
  214. std::optional<Nonnull<Expression*>> initializer,
  215. ValueCategory value_category)
  216. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  217. binding_(binding),
  218. initializer_(initializer),
  219. value_category_(value_category) {}
  220. static auto classof(const AstNode* node) -> bool {
  221. return InheritsFromVariableDeclaration(node->kind());
  222. }
  223. auto binding() const -> const BindingPattern& { return *binding_; }
  224. auto binding() -> BindingPattern& { return *binding_; }
  225. auto initializer() const -> const Expression& { return **initializer_; }
  226. auto initializer() -> Expression& { return **initializer_; }
  227. auto value_category() const -> ValueCategory { return value_category_; }
  228. auto has_initializer() const -> bool { return initializer_.has_value(); }
  229. private:
  230. // TODO: split this into a non-optional name and a type, initialized by
  231. // a constructor that takes a BindingPattern and handles errors like a
  232. // missing name.
  233. Nonnull<BindingPattern*> binding_;
  234. std::optional<Nonnull<Expression*>> initializer_;
  235. ValueCategory value_category_;
  236. };
  237. class InterfaceDeclaration : public Declaration {
  238. public:
  239. using ImplementsCarbonValueNode = void;
  240. InterfaceDeclaration(SourceLocation source_loc, std::string name,
  241. Nonnull<GenericBinding*> self,
  242. std::vector<Nonnull<Declaration*>> members)
  243. : Declaration(AstNodeKind::InterfaceDeclaration, source_loc),
  244. name_(std::move(name)),
  245. members_(std::move(members)),
  246. self_(self) {}
  247. static auto classof(const AstNode* node) -> bool {
  248. return InheritsFromInterfaceDeclaration(node->kind());
  249. }
  250. auto name() const -> const std::string& { return name_; }
  251. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  252. return members_;
  253. }
  254. auto self() const -> Nonnull<const GenericBinding*> { return self_; }
  255. auto self() -> Nonnull<GenericBinding*> { return self_; }
  256. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  257. private:
  258. std::string name_;
  259. std::vector<Nonnull<Declaration*>> members_;
  260. Nonnull<GenericBinding*> self_;
  261. };
  262. enum class ImplKind { InternalImpl, ExternalImpl };
  263. class ImplDeclaration : public Declaration {
  264. public:
  265. using ImplementsCarbonValueNode = void;
  266. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  267. ImplKind kind, Nonnull<Expression*> impl_type,
  268. Nonnull<Expression*> interface,
  269. std::vector<Nonnull<AstNode*>> deduced_params,
  270. std::vector<Nonnull<Declaration*>> members)
  271. -> ErrorOr<Nonnull<ImplDeclaration*>>;
  272. // Use `Create` instead.
  273. ImplDeclaration(SourceLocation source_loc, ImplKind kind,
  274. Nonnull<Expression*> impl_type,
  275. Nonnull<Expression*> interface,
  276. std::vector<Nonnull<GenericBinding*>> deduced_params,
  277. std::vector<Nonnull<Declaration*>> members)
  278. : Declaration(AstNodeKind::ImplDeclaration, source_loc),
  279. kind_(kind),
  280. impl_type_(impl_type),
  281. interface_(interface),
  282. deduced_parameters_(std::move(deduced_params)),
  283. members_(std::move(members)) {}
  284. static auto classof(const AstNode* node) -> bool {
  285. return InheritsFromImplDeclaration(node->kind());
  286. }
  287. // Return whether this is an external or internal impl.
  288. auto kind() const -> ImplKind { return kind_; }
  289. // Return the type that is doing the implementing.
  290. auto impl_type() const -> Nonnull<Expression*> { return impl_type_; }
  291. // Return the interface that is being implemented.
  292. auto interface() const -> const Expression& { return *interface_; }
  293. auto interface() -> Expression& { return *interface_; }
  294. void set_interface_type(Nonnull<const Value*> iface_type) {
  295. interface_type_ = iface_type;
  296. }
  297. auto interface_type() const -> Nonnull<const Value*> {
  298. return *interface_type_;
  299. }
  300. auto deduced_parameters() const
  301. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  302. return deduced_parameters_;
  303. }
  304. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  305. return deduced_parameters_;
  306. }
  307. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  308. return members_;
  309. }
  310. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  311. void set_impl_bindings(llvm::ArrayRef<Nonnull<const ImplBinding*>> imps) {
  312. impl_bindings_ = imps;
  313. }
  314. auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
  315. return impl_bindings_;
  316. }
  317. private:
  318. ImplKind kind_;
  319. Nonnull<Expression*> impl_type_; // TODO: make this optional
  320. Nonnull<Expression*> interface_;
  321. std::optional<Nonnull<const Value*>> interface_type_;
  322. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  323. std::vector<Nonnull<Declaration*>> members_;
  324. std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
  325. };
  326. // Return the name of a declaration, if it has one.
  327. auto GetName(const Declaration&) -> std::optional<std::string>;
  328. } // namespace Carbon
  329. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_