declaration.h 12 KB

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