declaration.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 virtual 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() = default;
  56. private:
  57. std::optional<Nonnull<const Value*>> static_type_;
  58. };
  59. // TODO: expand the kinds of things that can be deduced parameters.
  60. // For now, only generic parameters are supported.
  61. struct GenericBinding : public virtual AstNode, public NamedEntity {
  62. public:
  63. GenericBinding(SourceLocation source_loc, std::string name,
  64. Nonnull<Expression*> type)
  65. : AstNode(AstNodeKind::GenericBinding, source_loc),
  66. name_(std::move(name)),
  67. type_(type) {}
  68. void Print(llvm::raw_ostream& out) const override;
  69. static auto classof(const AstNode* node) -> bool {
  70. return InheritsFromGenericBinding(node->kind());
  71. }
  72. auto name() const -> const std::string& { return name_; }
  73. auto type() const -> const Expression& { return *type_; }
  74. auto type() -> Expression& { return *type_; }
  75. // The static type of the binding. Cannot be called before typechecking.
  76. auto static_type() const -> const Value& { return **static_type_; }
  77. // Sets the static type of the binding. Can only be called once, during
  78. // typechecking.
  79. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  80. // Returns whether the static type has been set. Should only be called
  81. // during typechecking: before typechecking it's guaranteed to be false,
  82. // and after typechecking it's guaranteed to be true.
  83. auto has_static_type() const -> bool { return static_type_.has_value(); }
  84. private:
  85. std::string name_;
  86. Nonnull<Expression*> type_;
  87. std::optional<Nonnull<const Value*>> static_type_;
  88. };
  89. // The syntactic representation of a function declaration's return type.
  90. // This syntax can take one of three forms:
  91. // - An _explicit_ term consists of `->` followed by a type expression.
  92. // - An _auto_ term consists of `-> auto`.
  93. // - An _omitted_ term consists of no tokens at all.
  94. // Each of these forms has a corresponding factory function.
  95. class ReturnTerm {
  96. public:
  97. ReturnTerm(const ReturnTerm&) = default;
  98. auto operator=(const ReturnTerm&) -> ReturnTerm& = default;
  99. // Represents an omitted return term at `source_loc`.
  100. static auto Omitted(SourceLocation source_loc) -> ReturnTerm {
  101. return ReturnTerm(ReturnKind::Omitted, source_loc);
  102. }
  103. // Represents an auto return term at `source_loc`.
  104. static auto Auto(SourceLocation source_loc) -> ReturnTerm {
  105. return ReturnTerm(ReturnKind::Auto, source_loc);
  106. }
  107. // Represents an explicit return term with the given type expression.
  108. static auto Explicit(Nonnull<Expression*> type_expression) -> ReturnTerm {
  109. return ReturnTerm(type_expression);
  110. }
  111. // Returns true if this represents an omitted return term.
  112. auto is_omitted() const -> bool { return kind_ == ReturnKind::Omitted; }
  113. // Returns true if this represents an auto return term.
  114. auto is_auto() const -> bool { return kind_ == ReturnKind::Auto; }
  115. // If this represents an explicit return term, returns the type expression.
  116. // Otherwise, returns nullopt.
  117. auto type_expression() const -> std::optional<Nonnull<const Expression*>> {
  118. return type_expression_;
  119. }
  120. auto type_expression() -> std::optional<Nonnull<Expression*>> {
  121. return type_expression_;
  122. }
  123. // The static return type this term resolves to. Cannot be called before
  124. // typechecking.
  125. auto static_type() const -> const Value& { return **static_type_; }
  126. // Sets the value of static_type(). Can only be called once, during
  127. // typechecking.
  128. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  129. // Returns whether static_type() has been set. Should only be called
  130. // during typechecking: before typechecking it's guaranteed to be false,
  131. // and after typechecking it's guaranteed to be true.
  132. auto has_static_type() const -> bool { return static_type_.has_value(); }
  133. auto source_loc() const -> SourceLocation { return source_loc_; }
  134. void Print(llvm::raw_ostream& out) const;
  135. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  136. private:
  137. enum class ReturnKind { Omitted, Auto, Expression };
  138. explicit ReturnTerm(ReturnKind kind, SourceLocation source_loc)
  139. : kind_(kind), source_loc_(source_loc) {
  140. CHECK(kind != ReturnKind::Expression);
  141. }
  142. explicit ReturnTerm(Nonnull<Expression*> type_expression)
  143. : kind_(ReturnKind::Expression),
  144. type_expression_(type_expression),
  145. source_loc_(type_expression->source_loc()) {}
  146. ReturnKind kind_;
  147. std::optional<Nonnull<Expression*>> type_expression_;
  148. std::optional<Nonnull<const Value*>> static_type_;
  149. SourceLocation source_loc_;
  150. };
  151. class FunctionDeclaration : public Declaration, public NamedEntity {
  152. public:
  153. FunctionDeclaration(SourceLocation source_loc, std::string name,
  154. std::vector<Nonnull<GenericBinding*>> deduced_params,
  155. Nonnull<TuplePattern*> param_pattern,
  156. ReturnTerm return_term,
  157. std::optional<Nonnull<Block*>> body)
  158. : AstNode(AstNodeKind::FunctionDeclaration, source_loc),
  159. name_(std::move(name)),
  160. deduced_parameters_(std::move(deduced_params)),
  161. param_pattern_(param_pattern),
  162. return_term_(return_term),
  163. body_(body) {}
  164. static auto classof(const AstNode* node) -> bool {
  165. return InheritsFromFunctionDeclaration(node->kind());
  166. }
  167. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  168. auto name() const -> const std::string& { return name_; }
  169. auto deduced_parameters() const
  170. -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
  171. return deduced_parameters_;
  172. }
  173. auto deduced_parameters() -> llvm::ArrayRef<Nonnull<GenericBinding*>> {
  174. return deduced_parameters_;
  175. }
  176. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  177. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  178. auto return_term() const -> const ReturnTerm& { return return_term_; }
  179. auto return_term() -> ReturnTerm& { return return_term_; }
  180. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  181. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  182. private:
  183. std::string name_;
  184. std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
  185. Nonnull<TuplePattern*> param_pattern_;
  186. ReturnTerm return_term_;
  187. std::optional<Nonnull<Block*>> body_;
  188. };
  189. class ClassDeclaration : public Declaration, public NamedEntity {
  190. public:
  191. ClassDeclaration(SourceLocation source_loc, std::string name,
  192. std::vector<Nonnull<Member*>> members)
  193. : AstNode(AstNodeKind::ClassDeclaration, source_loc),
  194. name_(std::move(name)),
  195. members_(std::move(members)) {}
  196. static auto classof(const AstNode* node) -> bool {
  197. return InheritsFromClassDeclaration(node->kind());
  198. }
  199. auto name() const -> const std::string& { return name_; }
  200. auto members() const -> llvm::ArrayRef<Nonnull<Member*>> { return members_; }
  201. private:
  202. std::string name_;
  203. std::vector<Nonnull<Member*>> members_;
  204. };
  205. class AlternativeSignature : public virtual AstNode {
  206. public:
  207. AlternativeSignature(SourceLocation source_loc, std::string name,
  208. Nonnull<Expression*> signature)
  209. : AstNode(AstNodeKind::AlternativeSignature, source_loc),
  210. name_(std::move(name)),
  211. signature_(signature) {}
  212. void Print(llvm::raw_ostream& out) const override;
  213. static auto classof(const AstNode* node) -> bool {
  214. return InheritsFromAlternativeSignature(node->kind());
  215. }
  216. auto name() const -> const std::string& { return name_; }
  217. auto signature() const -> const Expression& { return *signature_; }
  218. auto signature() -> Expression& { return *signature_; }
  219. private:
  220. std::string name_;
  221. Nonnull<Expression*> signature_;
  222. };
  223. class ChoiceDeclaration : public Declaration, public NamedEntity {
  224. public:
  225. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  226. std::vector<Nonnull<AlternativeSignature*>> alternatives)
  227. : AstNode(AstNodeKind::ChoiceDeclaration, source_loc),
  228. name_(std::move(name)),
  229. alternatives_(std::move(alternatives)) {}
  230. static auto classof(const AstNode* node) -> bool {
  231. return InheritsFromChoiceDeclaration(node->kind());
  232. }
  233. auto name() const -> const std::string& { return name_; }
  234. auto alternatives() const
  235. -> llvm::ArrayRef<Nonnull<const AlternativeSignature*>> {
  236. return alternatives_;
  237. }
  238. auto alternatives() -> llvm::ArrayRef<Nonnull<AlternativeSignature*>> {
  239. return alternatives_;
  240. }
  241. private:
  242. std::string name_;
  243. std::vector<Nonnull<AlternativeSignature*>> alternatives_;
  244. };
  245. // Global variable definition implements the Declaration concept.
  246. class VariableDeclaration : public Declaration {
  247. public:
  248. VariableDeclaration(SourceLocation source_loc,
  249. Nonnull<BindingPattern*> binding,
  250. Nonnull<Expression*> initializer)
  251. : AstNode(AstNodeKind::VariableDeclaration, source_loc),
  252. binding_(binding),
  253. initializer_(initializer) {}
  254. static auto classof(const AstNode* node) -> bool {
  255. return InheritsFromVariableDeclaration(node->kind());
  256. }
  257. auto binding() const -> const BindingPattern& { return *binding_; }
  258. auto binding() -> BindingPattern& { return *binding_; }
  259. auto initializer() const -> const Expression& { return *initializer_; }
  260. auto initializer() -> Expression& { return *initializer_; }
  261. private:
  262. // TODO: split this into a non-optional name and a type, initialized by
  263. // a constructor that takes a BindingPattern and handles errors like a
  264. // missing name.
  265. Nonnull<BindingPattern*> binding_;
  266. Nonnull<Expression*> initializer_;
  267. };
  268. } // namespace Carbon
  269. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_