declaration.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/generic_binding.h"
  12. #include "executable_semantics/ast/pattern.h"
  13. #include "executable_semantics/ast/return_term.h"
  14. #include "executable_semantics/ast/source_location.h"
  15. #include "executable_semantics/ast/statement.h"
  16. #include "executable_semantics/ast/static_scope.h"
  17. #include "executable_semantics/ast/value_category.h"
  18. #include "executable_semantics/common/nonnull.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. static auto classof(const AstNode* node) -> bool {
  37. return InheritsFromDeclaration(node->kind());
  38. }
  39. // Returns the enumerator corresponding to the most-derived type of this
  40. // object.
  41. auto kind() const -> DeclarationKind {
  42. return static_cast<DeclarationKind>(root_kind());
  43. }
  44. // The static type of the declared entity. Cannot be called before
  45. // typechecking.
  46. auto static_type() const -> const Value& { return **static_type_; }
  47. // Sets the static type of the declared entity. Can only be called once,
  48. // during typechecking.
  49. void set_static_type(Nonnull<const Value*> type) {
  50. CHECK(!static_type_.has_value());
  51. static_type_ = type;
  52. }
  53. // Returns whether the static type has been set. Should only be called
  54. // during typechecking: before typechecking it's guaranteed to be false,
  55. // and after typechecking it's guaranteed to be true.
  56. auto has_static_type() const -> bool { return static_type_.has_value(); }
  57. protected:
  58. // Constructs a Declaration representing syntax at the given line number.
  59. // `kind` must be the enumerator corresponding to the most-derived type being
  60. // constructed.
  61. Declaration(AstNodeKind kind, SourceLocation source_loc)
  62. : AstNode(kind, source_loc) {}
  63. private:
  64. std::optional<Nonnull<const Value*>> static_type_;
  65. };
  66. class FunctionDeclaration : public Declaration {
  67. public:
  68. using ImplementsCarbonValueNode = void;
  69. static auto Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  70. std::string name,
  71. std::vector<Nonnull<AstNode*>> deduced_params,
  72. std::optional<Nonnull<BindingPattern*>> me_pattern,
  73. Nonnull<TuplePattern*> param_pattern,
  74. ReturnTerm return_term,
  75. std::optional<Nonnull<Block*>> body)
  76. -> ErrorOr<Nonnull<FunctionDeclaration*>>;
  77. // Use `Create()` instead. This is public only so Arena::New() can call it.
  78. FunctionDeclaration(SourceLocation source_loc, std::string name,
  79. std::vector<Nonnull<GenericBinding*>> deduced_params,
  80. std::optional<Nonnull<BindingPattern*>> me_pattern,
  81. Nonnull<TuplePattern*> param_pattern,
  82. ReturnTerm return_term,
  83. std::optional<Nonnull<Block*>> body)
  84. : Declaration(AstNodeKind::FunctionDeclaration, source_loc),
  85. name_(std::move(name)),
  86. deduced_parameters_(std::move(deduced_params)),
  87. me_pattern_(me_pattern),
  88. param_pattern_(param_pattern),
  89. return_term_(return_term),
  90. body_(body) {}
  91. static auto classof(const AstNode* node) -> bool {
  92. return InheritsFromFunctionDeclaration(node->kind());
  93. }
  94. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  95. auto name() const -> const std::string& { return name_; }
  96. auto deduced_parameters() const
  97. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  98. return deduced_parameters_;
  99. }
  100. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  101. return deduced_parameters_;
  102. }
  103. auto me_pattern() const -> const BindingPattern& { return **me_pattern_; }
  104. auto me_pattern() -> BindingPattern& { return **me_pattern_; }
  105. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  106. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  107. auto return_term() const -> const ReturnTerm& { return return_term_; }
  108. auto return_term() -> ReturnTerm& { return return_term_; }
  109. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  110. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  111. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  112. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  113. return constant_value_;
  114. }
  115. // Sets the value returned by constant_value(). Can only be called once,
  116. // during typechecking.
  117. void set_constant_value(Nonnull<const Value*> value) {
  118. CHECK(!constant_value_.has_value());
  119. constant_value_ = value;
  120. }
  121. auto is_method() const -> bool { return me_pattern_.has_value(); }
  122. private:
  123. std::string name_;
  124. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  125. std::optional<Nonnull<BindingPattern*>> me_pattern_;
  126. Nonnull<TuplePattern*> param_pattern_;
  127. ReturnTerm return_term_;
  128. std::optional<Nonnull<Block*>> body_;
  129. std::optional<Nonnull<const Value*>> constant_value_;
  130. };
  131. class ClassDeclaration : public Declaration {
  132. public:
  133. using ImplementsCarbonValueNode = void;
  134. ClassDeclaration(SourceLocation source_loc, std::string name,
  135. std::vector<Nonnull<Declaration*>> members)
  136. : Declaration(AstNodeKind::ClassDeclaration, source_loc),
  137. name_(std::move(name)),
  138. members_(std::move(members)) {}
  139. static auto classof(const AstNode* node) -> bool {
  140. return InheritsFromClassDeclaration(node->kind());
  141. }
  142. auto name() const -> const std::string& { return name_; }
  143. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  144. return members_;
  145. }
  146. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  147. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  148. return constant_value_;
  149. }
  150. // Sets the value returned by constant_value(). Can only be called once,
  151. // during typechecking.
  152. void set_constant_value(Nonnull<const Value*> value) {
  153. CHECK(!constant_value_.has_value());
  154. constant_value_ = value;
  155. }
  156. private:
  157. std::string name_;
  158. std::vector<Nonnull<Declaration*>> members_;
  159. std::optional<Nonnull<const Value*>> constant_value_;
  160. };
  161. class AlternativeSignature : public AstNode {
  162. public:
  163. AlternativeSignature(SourceLocation source_loc, std::string name,
  164. Nonnull<Expression*> signature)
  165. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  166. name_(std::move(name)),
  167. signature_(signature) {}
  168. void Print(llvm::raw_ostream& out) const override;
  169. static auto classof(const AstNode* node) -> bool {
  170. return InheritsFromAlternativeSignature(node->kind());
  171. }
  172. auto name() const -> const std::string& { return name_; }
  173. auto signature() const -> const Expression& { return *signature_; }
  174. auto signature() -> Expression& { return *signature_; }
  175. private:
  176. std::string name_;
  177. Nonnull<Expression*> signature_;
  178. };
  179. class ChoiceDeclaration : public Declaration {
  180. public:
  181. using ImplementsCarbonValueNode = void;
  182. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  183. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  184. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  185. name_(std::move(name)),
  186. alternatives_(std::move(alternatives)) {}
  187. static auto classof(const AstNode* node) -> bool {
  188. return InheritsFromChoiceDeclaration(node->kind());
  189. }
  190. auto name() const -> const std::string& { return name_; }
  191. auto alternatives() const
  192. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  193. return alternatives_;
  194. }
  195. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  196. return alternatives_;
  197. }
  198. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  199. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  200. return constant_value_;
  201. }
  202. // Sets the value returned by constant_value(). Can only be called once,
  203. // during typechecking.
  204. void set_constant_value(Nonnull<const Value*> value) {
  205. CHECK(!constant_value_.has_value());
  206. constant_value_ = value;
  207. }
  208. private:
  209. std::string name_;
  210. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  211. std::optional<Nonnull<const Value*>> constant_value_;
  212. };
  213. // Global variable definition implements the Declaration concept.
  214. class VariableDeclaration : public Declaration {
  215. public:
  216. VariableDeclaration(SourceLocation source_loc,
  217. Nonnull<BindingPattern*> binding,
  218. std::optional<Nonnull<Expression*>> initializer,
  219. ValueCategory value_category)
  220. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  221. binding_(binding),
  222. initializer_(initializer),
  223. value_category_(value_category) {}
  224. static auto classof(const AstNode* node) -> bool {
  225. return InheritsFromVariableDeclaration(node->kind());
  226. }
  227. auto binding() const -> const BindingPattern& { return *binding_; }
  228. auto binding() -> BindingPattern& { return *binding_; }
  229. auto initializer() const -> const Expression& { return **initializer_; }
  230. auto initializer() -> Expression& { return **initializer_; }
  231. auto value_category() const -> ValueCategory { return value_category_; }
  232. auto has_initializer() const -> bool { return initializer_.has_value(); }
  233. private:
  234. // TODO: split this into a non-optional name and a type, initialized by
  235. // a constructor that takes a BindingPattern and handles errors like a
  236. // missing name.
  237. Nonnull<BindingPattern*> binding_;
  238. std::optional<Nonnull<Expression*>> initializer_;
  239. ValueCategory value_category_;
  240. };
  241. class InterfaceDeclaration : public Declaration {
  242. public:
  243. using ImplementsCarbonValueNode = void;
  244. InterfaceDeclaration(SourceLocation source_loc, std::string name,
  245. Nonnull<GenericBinding*> self,
  246. std::vector<Nonnull<Declaration*>> members)
  247. : Declaration(AstNodeKind::InterfaceDeclaration, source_loc),
  248. name_(std::move(name)),
  249. members_(std::move(members)),
  250. self_(self) {}
  251. static auto classof(const AstNode* node) -> bool {
  252. return InheritsFromInterfaceDeclaration(node->kind());
  253. }
  254. auto name() const -> const std::string& { return name_; }
  255. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  256. return members_;
  257. }
  258. auto self() const -> Nonnull<const GenericBinding*> { return self_; }
  259. auto self() -> Nonnull<GenericBinding*> { return self_; }
  260. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  261. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  262. return constant_value_;
  263. }
  264. // Sets the value returned by constant_value(). Can only be called once,
  265. // during typechecking.
  266. void set_constant_value(Nonnull<const Value*> value) {
  267. CHECK(!constant_value_.has_value());
  268. constant_value_ = value;
  269. }
  270. private:
  271. std::string name_;
  272. std::vector<Nonnull<Declaration*>> members_;
  273. std::optional<Nonnull<const Value*>> constant_value_;
  274. Nonnull<GenericBinding*> self_;
  275. };
  276. enum class ImplKind { InternalImpl, ExternalImpl };
  277. class ImplDeclaration : public Declaration {
  278. public:
  279. using ImplementsCarbonValueNode = void;
  280. ImplDeclaration(SourceLocation source_loc, ImplKind kind,
  281. Nonnull<Expression*> impl_type,
  282. Nonnull<Expression*> interface,
  283. std::vector<Nonnull<Declaration*>> members)
  284. : Declaration(AstNodeKind::ImplDeclaration, source_loc),
  285. kind_(kind),
  286. impl_type_(impl_type),
  287. interface_(interface),
  288. members_(std::move(members)) {}
  289. static auto classof(const AstNode* node) -> bool {
  290. return InheritsFromImplDeclaration(node->kind());
  291. }
  292. // Return whether this is an external or internal impl.
  293. auto kind() const -> ImplKind { return kind_; }
  294. // Return the type that is doing the implementing.
  295. auto impl_type() const -> Nonnull<Expression*> { return impl_type_; }
  296. // Return the interface that is being implemented.
  297. auto interface() const -> const Expression& { return *interface_; }
  298. auto interface() -> Expression& { return *interface_; }
  299. void set_interface_type(Nonnull<const Value*> iface_type) {
  300. interface_type_ = iface_type;
  301. }
  302. auto interface_type() const -> Nonnull<const Value*> {
  303. return *interface_type_;
  304. }
  305. auto members() const -> llvm::ArrayRef<Nonnull<Declaration*>> {
  306. return members_;
  307. }
  308. // Return the witness table for this impl.
  309. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  310. return constant_value_;
  311. }
  312. void set_constant_value(Nonnull<const Value*> value) {
  313. CHECK(!constant_value_.has_value());
  314. constant_value_ = value;
  315. }
  316. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  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<Declaration*>> members_;
  323. std::optional<Nonnull<const Value*>> constant_value_;
  324. };
  325. // Return the name of a declaration, if it has one.
  326. auto GetName(const Declaration&) -> std::optional<std::string>;
  327. } // namespace Carbon
  328. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_