declaration.h 12 KB

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