declaration.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/member.h"
  11. #include "executable_semantics/ast/pattern.h"
  12. #include "executable_semantics/ast/source_location.h"
  13. #include "executable_semantics/ast/statement.h"
  14. #include "executable_semantics/ast/static_scope.h"
  15. #include "executable_semantics/common/nonnull.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/Support/Compiler.h"
  18. namespace Carbon {
  19. // Abstract base class of all AST nodes representing patterns.
  20. //
  21. // Declaration and its derived classes support LLVM-style RTTI, including
  22. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  23. // class derived from Declaration must provide a `classof` operation, and
  24. // every concrete derived class must have a corresponding enumerator
  25. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  26. // details.
  27. class Declaration : public AstNode {
  28. public:
  29. ~Declaration() override = 0;
  30. Declaration(const Member&) = delete;
  31. auto operator=(const Member&) -> Declaration& = delete;
  32. void Print(llvm::raw_ostream& out) const override;
  33. static auto classof(const AstNode* node) -> bool {
  34. return InheritsFromDeclaration(node->kind());
  35. }
  36. // Returns the enumerator corresponding to the most-derived type of this
  37. // object.
  38. auto kind() const -> DeclarationKind {
  39. return static_cast<DeclarationKind>(root_kind());
  40. }
  41. // The static type of the declared entity. Cannot be called before
  42. // typechecking.
  43. auto static_type() const -> const Value& { return **static_type_; }
  44. // Sets the static type of the declared entity. Can only be called once,
  45. // during typechecking.
  46. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  47. // Returns whether the static type has been set. Should only be called
  48. // during typechecking: before typechecking it's guaranteed to be false,
  49. // and after typechecking it's guaranteed to be true.
  50. auto has_static_type() const -> bool { return static_type_.has_value(); }
  51. protected:
  52. // Constructs a Declaration representing syntax at the given line number.
  53. // `kind` must be the enumerator corresponding to the most-derived type being
  54. // constructed.
  55. Declaration(AstNodeKind kind, SourceLocation source_loc)
  56. : AstNode(kind, source_loc) {}
  57. private:
  58. std::optional<Nonnull<const Value*>> static_type_;
  59. };
  60. // TODO: expand the kinds of things that can be deduced parameters.
  61. // For now, only generic parameters are supported.
  62. class GenericBinding : public AstNode {
  63. public:
  64. using ImplementsCarbonNamedEntity = void;
  65. GenericBinding(SourceLocation source_loc, std::string name,
  66. Nonnull<Expression*> type)
  67. : AstNode(AstNodeKind::GenericBinding, source_loc),
  68. name_(std::move(name)),
  69. type_(type) {}
  70. void Print(llvm::raw_ostream& out) const override;
  71. static auto classof(const AstNode* node) -> bool {
  72. return InheritsFromGenericBinding(node->kind());
  73. }
  74. auto name() const -> const std::string& { return name_; }
  75. auto type() const -> const Expression& { return *type_; }
  76. auto type() -> Expression& { return *type_; }
  77. // The static type of the binding. Cannot be called before typechecking.
  78. auto static_type() const -> const Value& { return **static_type_; }
  79. // Sets the static type of the binding. Can only be called once, during
  80. // typechecking.
  81. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  82. // Returns whether the static type has been set. Should only be called
  83. // during typechecking: before typechecking it's guaranteed to be false,
  84. // and after typechecking it's guaranteed to be true.
  85. auto has_static_type() const -> bool { return static_type_.has_value(); }
  86. private:
  87. std::string name_;
  88. Nonnull<Expression*> type_;
  89. std::optional<Nonnull<const Value*>> static_type_;
  90. };
  91. // The syntactic representation of a function declaration's return type.
  92. // This syntax can take one of three forms:
  93. // - An _explicit_ term consists of `->` followed by a type expression.
  94. // - An _auto_ term consists of `-> auto`.
  95. // - An _omitted_ term consists of no tokens at all.
  96. // Each of these forms has a corresponding factory function.
  97. class ReturnTerm {
  98. public:
  99. ReturnTerm(const ReturnTerm&) = default;
  100. auto operator=(const ReturnTerm&) -> ReturnTerm& = default;
  101. // Represents an omitted return term at `source_loc`.
  102. static auto Omitted(SourceLocation source_loc) -> ReturnTerm {
  103. return ReturnTerm(ReturnKind::Omitted, source_loc);
  104. }
  105. // Represents an auto return term at `source_loc`.
  106. static auto Auto(SourceLocation source_loc) -> ReturnTerm {
  107. return ReturnTerm(ReturnKind::Auto, source_loc);
  108. }
  109. // Represents an explicit return term with the given type expression.
  110. static auto Explicit(Nonnull<Expression*> type_expression) -> ReturnTerm {
  111. return ReturnTerm(type_expression);
  112. }
  113. // Returns true if this represents an omitted return term.
  114. auto is_omitted() const -> bool { return kind_ == ReturnKind::Omitted; }
  115. // Returns true if this represents an auto return term.
  116. auto is_auto() const -> bool { return kind_ == ReturnKind::Auto; }
  117. // If this represents an explicit return term, returns the type expression.
  118. // Otherwise, returns nullopt.
  119. auto type_expression() const -> std::optional<Nonnull<const Expression*>> {
  120. return type_expression_;
  121. }
  122. auto type_expression() -> std::optional<Nonnull<Expression*>> {
  123. return type_expression_;
  124. }
  125. // The static return type this term resolves to. Cannot be called before
  126. // typechecking.
  127. auto static_type() const -> const Value& { return **static_type_; }
  128. // Sets the value of static_type(). Can only be called once, during
  129. // typechecking.
  130. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  131. // Returns whether static_type() has been set. Should only be called
  132. // during typechecking: before typechecking it's guaranteed to be false,
  133. // and after typechecking it's guaranteed to be true.
  134. auto has_static_type() const -> bool { return static_type_.has_value(); }
  135. auto source_loc() const -> SourceLocation { return source_loc_; }
  136. void Print(llvm::raw_ostream& out) const;
  137. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  138. private:
  139. enum class ReturnKind { Omitted, Auto, Expression };
  140. explicit ReturnTerm(ReturnKind kind, SourceLocation source_loc)
  141. : kind_(kind), source_loc_(source_loc) {
  142. CHECK(kind != ReturnKind::Expression);
  143. }
  144. explicit ReturnTerm(Nonnull<Expression*> type_expression)
  145. : kind_(ReturnKind::Expression),
  146. type_expression_(type_expression),
  147. source_loc_(type_expression->source_loc()) {}
  148. ReturnKind kind_;
  149. std::optional<Nonnull<Expression*>> type_expression_;
  150. std::optional<Nonnull<const Value*>> static_type_;
  151. SourceLocation source_loc_;
  152. };
  153. class FunctionDeclaration : public Declaration {
  154. public:
  155. using ImplementsCarbonNamedEntity = void;
  156. FunctionDeclaration(SourceLocation source_loc, std::string name,
  157. std::vector<Nonnull<GenericBinding*>> deduced_params,
  158. Nonnull<TuplePattern*> param_pattern,
  159. ReturnTerm return_term,
  160. std::optional<Nonnull<Block*>> body)
  161. : Declaration(AstNodeKind::FunctionDeclaration, source_loc),
  162. name_(std::move(name)),
  163. deduced_parameters_(std::move(deduced_params)),
  164. param_pattern_(param_pattern),
  165. return_term_(return_term),
  166. body_(body) {}
  167. static auto classof(const AstNode* node) -> bool {
  168. return InheritsFromFunctionDeclaration(node->kind());
  169. }
  170. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  171. auto name() const -> const std::string& { return name_; }
  172. auto deduced_parameters() const
  173. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  174. return deduced_parameters_;
  175. }
  176. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  177. return deduced_parameters_;
  178. }
  179. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  180. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  181. auto return_term() const -> const ReturnTerm& { return return_term_; }
  182. auto return_term() -> ReturnTerm& { return return_term_; }
  183. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  184. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  185. private:
  186. std::string name_;
  187. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  188. Nonnull<TuplePattern*> param_pattern_;
  189. ReturnTerm return_term_;
  190. std::optional<Nonnull<Block*>> body_;
  191. };
  192. class ClassDeclaration : public Declaration {
  193. public:
  194. using ImplementsCarbonNamedEntity = void;
  195. ClassDeclaration(SourceLocation source_loc, std::string name,
  196. std::vector<Nonnull<Member*>> members)
  197. : Declaration(AstNodeKind::ClassDeclaration, source_loc),
  198. name_(std::move(name)),
  199. members_(std::move(members)) {}
  200. static auto classof(const AstNode* node) -> bool {
  201. return InheritsFromClassDeclaration(node->kind());
  202. }
  203. auto name() const -> const std::string& { return name_; }
  204. auto members() const -> llvm::ArrayRef<Nonnull<Member*>> { return members_; }
  205. private:
  206. std::string name_;
  207. std::vector<Nonnull<Member*>> members_;
  208. };
  209. class AlternativeSignature : public AstNode {
  210. public:
  211. AlternativeSignature(SourceLocation source_loc, std::string name,
  212. Nonnull<Expression*> signature)
  213. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  214. name_(std::move(name)),
  215. signature_(signature) {}
  216. void Print(llvm::raw_ostream& out) const override;
  217. static auto classof(const AstNode* node) -> bool {
  218. return InheritsFromAlternativeSignature(node->kind());
  219. }
  220. auto name() const -> const std::string& { return name_; }
  221. auto signature() const -> const Expression& { return *signature_; }
  222. auto signature() -> Expression& { return *signature_; }
  223. private:
  224. std::string name_;
  225. Nonnull<Expression*> signature_;
  226. };
  227. class ChoiceDeclaration : public Declaration {
  228. public:
  229. using ImplementsCarbonNamedEntity = void;
  230. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  231. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  232. : Declaration(AstNodeKind::ChoiceDeclaration, source_loc),
  233. name_(std::move(name)),
  234. alternatives_(std::move(alternatives)) {}
  235. static auto classof(const AstNode* node) -> bool {
  236. return InheritsFromChoiceDeclaration(node->kind());
  237. }
  238. auto name() const -> const std::string& { return name_; }
  239. auto alternatives() const
  240. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  241. return alternatives_;
  242. }
  243. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  244. return alternatives_;
  245. }
  246. private:
  247. std::string name_;
  248. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  249. };
  250. // Global variable definition implements the Declaration concept.
  251. class VariableDeclaration : public Declaration {
  252. public:
  253. VariableDeclaration(SourceLocation source_loc,
  254. Nonnull<BindingPattern*> binding,
  255. Nonnull<Expression*> initializer)
  256. : Declaration(AstNodeKind::VariableDeclaration, source_loc),
  257. binding_(binding),
  258. initializer_(initializer) {}
  259. static auto classof(const AstNode* node) -> bool {
  260. return InheritsFromVariableDeclaration(node->kind());
  261. }
  262. auto binding() const -> const BindingPattern& { return *binding_; }
  263. auto binding() -> BindingPattern& { return *binding_; }
  264. auto initializer() const -> const Expression& { return *initializer_; }
  265. auto initializer() -> Expression& { return *initializer_; }
  266. private:
  267. // TODO: split this into a non-optional name and a type, initialized by
  268. // a constructor that takes a BindingPattern and handles errors like a
  269. // missing name.
  270. Nonnull<BindingPattern*> binding_;
  271. Nonnull<Expression*> initializer_;
  272. };
  273. } // namespace Carbon
  274. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_