declaration.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. class FunctionDeclaration : public Declaration {
  87. public:
  88. FunctionDeclaration(SourceLocation source_loc, std::string name,
  89. std::vector<GenericBinding> deduced_params,
  90. Nonnull<TuplePattern*> param_pattern,
  91. Nonnull<Pattern*> return_type,
  92. bool is_omitted_return_type,
  93. std::optional<Nonnull<Block*>> body)
  94. : Declaration(Kind::FunctionDeclaration, source_loc),
  95. name_(std::move(name)),
  96. deduced_parameters_(std::move(deduced_params)),
  97. param_pattern_(param_pattern),
  98. return_type_(return_type),
  99. is_omitted_return_type_(is_omitted_return_type),
  100. body_(body) {}
  101. static auto classof(const Declaration* decl) -> bool {
  102. return decl->kind() == Kind::FunctionDeclaration;
  103. }
  104. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  105. auto name() const -> const std::string& { return name_; }
  106. auto deduced_parameters() const -> llvm::ArrayRef<GenericBinding> {
  107. return deduced_parameters_;
  108. }
  109. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  110. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  111. auto return_type() const -> const Pattern& { return *return_type_; }
  112. auto return_type() -> Pattern& { return *return_type_; }
  113. auto is_omitted_return_type() const -> bool {
  114. return is_omitted_return_type_;
  115. }
  116. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  117. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  118. // Only contains function parameters. Scoped variables are in the body.
  119. auto static_scope() const -> const StaticScope& { return static_scope_; }
  120. auto static_scope() -> StaticScope& { return static_scope_; }
  121. private:
  122. std::string name_;
  123. std::vector<GenericBinding> deduced_parameters_;
  124. Nonnull<TuplePattern*> param_pattern_;
  125. Nonnull<Pattern*> return_type_;
  126. bool is_omitted_return_type_;
  127. std::optional<Nonnull<Block*>> body_;
  128. StaticScope static_scope_;
  129. };
  130. class ClassDeclaration : public Declaration {
  131. public:
  132. ClassDeclaration(SourceLocation source_loc, std::string name,
  133. std::vector<Nonnull<Member*>> members)
  134. : Declaration(Kind::ClassDeclaration, source_loc),
  135. name_(std::move(name)),
  136. members_(std::move(members)) {}
  137. static auto classof(const Declaration* decl) -> bool {
  138. return decl->kind() == Kind::ClassDeclaration;
  139. }
  140. auto name() const -> const std::string& { return name_; }
  141. auto members() const -> llvm::ArrayRef<Nonnull<Member*>> { return members_; }
  142. // Contains class members. Scoped variables are in the body.
  143. auto static_scope() const -> const StaticScope& { return static_scope_; }
  144. auto static_scope() -> StaticScope& { return static_scope_; }
  145. private:
  146. std::string name_;
  147. std::vector<Nonnull<Member*>> members_;
  148. StaticScope static_scope_;
  149. };
  150. class ChoiceDeclaration : public Declaration {
  151. public:
  152. class Alternative : public NamedEntityInterface {
  153. public:
  154. Alternative(SourceLocation source_loc, std::string name,
  155. Nonnull<Expression*> signature)
  156. : source_loc_(source_loc),
  157. name_(std::move(name)),
  158. signature_(signature) {}
  159. auto named_entity_kind() const -> NamedEntityKind override {
  160. return NamedEntityKind::ChoiceDeclarationAlternative;
  161. }
  162. auto source_loc() const -> SourceLocation override { return source_loc_; }
  163. auto name() const -> const std::string& { return name_; }
  164. auto signature() const -> const Expression& { return *signature_; }
  165. private:
  166. SourceLocation source_loc_;
  167. std::string name_;
  168. Nonnull<Expression*> signature_;
  169. };
  170. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  171. std::vector<Alternative> alternatives)
  172. : Declaration(Kind::ChoiceDeclaration, source_loc),
  173. name_(std::move(name)),
  174. alternatives_(std::move(alternatives)) {}
  175. static auto classof(const Declaration* decl) -> bool {
  176. return decl->kind() == Kind::ChoiceDeclaration;
  177. }
  178. auto name() const -> const std::string& { return name_; }
  179. auto alternatives() const -> llvm::ArrayRef<Alternative> {
  180. return alternatives_;
  181. }
  182. // Contains the alternatives.
  183. auto static_scope() const -> const StaticScope& { return static_scope_; }
  184. auto static_scope() -> StaticScope& { return static_scope_; }
  185. private:
  186. std::string name_;
  187. std::vector<Alternative> alternatives_;
  188. StaticScope static_scope_;
  189. };
  190. // Global variable definition implements the Declaration concept.
  191. class VariableDeclaration : public Declaration {
  192. public:
  193. VariableDeclaration(SourceLocation source_loc,
  194. Nonnull<BindingPattern*> binding,
  195. Nonnull<Expression*> initializer)
  196. : Declaration(Kind::VariableDeclaration, source_loc),
  197. binding_(binding),
  198. initializer_(initializer) {}
  199. static auto classof(const Declaration* decl) -> bool {
  200. return decl->kind() == Kind::VariableDeclaration;
  201. }
  202. auto binding() const -> const BindingPattern& { return *binding_; }
  203. auto binding() -> BindingPattern& { return *binding_; }
  204. auto initializer() const -> const Expression& { return *initializer_; }
  205. auto initializer() -> Expression& { return *initializer_; }
  206. private:
  207. // TODO: split this into a non-optional name and a type, initialized by
  208. // a constructor that takes a BindingPattern and handles errors like a
  209. // missing name.
  210. Nonnull<BindingPattern*> binding_;
  211. Nonnull<Expression*> initializer_;
  212. };
  213. } // namespace Carbon
  214. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_