declaration.h 12 KB

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