declaration.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/class_definition.h"
  11. #include "executable_semantics/ast/member.h"
  12. #include "executable_semantics/ast/pattern.h"
  13. #include "executable_semantics/ast/source_location.h"
  14. #include "executable_semantics/ast/statement.h"
  15. #include "executable_semantics/ast/static_scope.h"
  16. #include "executable_semantics/common/nonnull.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/Support/Compiler.h"
  19. namespace Carbon {
  20. class StaticScope;
  21. // Abstract base class of all AST nodes representing patterns.
  22. //
  23. // Declaration and its derived classes support LLVM-style RTTI, including
  24. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  25. // class derived from Declaration must provide a `classof` operation, and
  26. // every concrete derived class must have a corresponding enumerator
  27. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  28. // details.
  29. class Declaration : public NamedEntityInterface {
  30. public:
  31. enum class Kind {
  32. FunctionDeclaration,
  33. ClassDeclaration,
  34. ChoiceDeclaration,
  35. VariableDeclaration,
  36. };
  37. Declaration(const Member&) = delete;
  38. auto operator=(const Member&) -> Declaration& = delete;
  39. void Print(llvm::raw_ostream& out) const;
  40. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  41. // Returns the enumerator corresponding to the most-derived type of this
  42. // object.
  43. auto kind() const -> Kind { return kind_; }
  44. auto named_entity_kind() const -> NamedEntityKind override {
  45. return NamedEntityKind::Declaration;
  46. }
  47. auto source_loc() const -> SourceLocation override { return source_loc_; }
  48. // The static type of the declared entity. Cannot be called before
  49. // typechecking.
  50. auto static_type() const -> const Value& { return **static_type_; }
  51. // Sets the static type of the declared entity. Can only be called once,
  52. // during typechecking.
  53. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  54. // Returns whether the static type has been set. Should only be called
  55. // during typechecking: before typechecking it's guaranteed to be false,
  56. // and after typechecking it's guaranteed to be true.
  57. auto has_static_type() const -> bool { return static_type_.has_value(); }
  58. protected:
  59. // Constructs a Declaration representing syntax at the given line number.
  60. // `kind` must be the enumerator corresponding to the most-derived type being
  61. // constructed.
  62. Declaration(Kind kind, SourceLocation source_loc)
  63. : kind_(kind), source_loc_(source_loc) {}
  64. private:
  65. const Kind kind_;
  66. SourceLocation source_loc_;
  67. std::optional<Nonnull<const Value*>> static_type_;
  68. };
  69. // TODO: expand the kinds of things that can be deduced parameters.
  70. // For now, only generic parameters are supported.
  71. struct GenericBinding : public NamedEntityInterface {
  72. public:
  73. GenericBinding(SourceLocation source_loc, std::string name,
  74. Nonnull<Expression*> type)
  75. : source_loc_(source_loc), name_(std::move(name)), type_(type) {}
  76. auto named_entity_kind() const -> NamedEntityKind override {
  77. return NamedEntityKind::GenericBinding;
  78. }
  79. auto source_loc() const -> SourceLocation override { return source_loc_; }
  80. auto name() const -> const std::string& { return name_; }
  81. auto type() const -> const Expression& { return *type_; }
  82. private:
  83. SourceLocation source_loc_;
  84. std::string name_;
  85. Nonnull<Expression*> type_;
  86. };
  87. class FunctionDeclaration : public Declaration {
  88. public:
  89. FunctionDeclaration(SourceLocation source_loc, std::string name,
  90. std::vector<GenericBinding> deduced_params,
  91. Nonnull<TuplePattern*> param_pattern,
  92. Nonnull<Pattern*> return_type,
  93. bool is_omitted_return_type,
  94. std::optional<Nonnull<Block*>> body)
  95. : Declaration(Kind::FunctionDeclaration, source_loc),
  96. name_(std::move(name)),
  97. deduced_parameters_(std::move(deduced_params)),
  98. param_pattern_(param_pattern),
  99. return_type_(return_type),
  100. is_omitted_return_type_(is_omitted_return_type),
  101. body_(body) {}
  102. static auto classof(const Declaration* decl) -> bool {
  103. return decl->kind() == Kind::FunctionDeclaration;
  104. }
  105. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  106. auto name() const -> const std::string& { return name_; }
  107. auto deduced_parameters() const -> llvm::ArrayRef<GenericBinding> {
  108. return deduced_parameters_;
  109. }
  110. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  111. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  112. auto return_type() const -> const Pattern& { return *return_type_; }
  113. auto return_type() -> Pattern& { return *return_type_; }
  114. auto is_omitted_return_type() const -> bool {
  115. return is_omitted_return_type_;
  116. }
  117. auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
  118. auto body() -> std::optional<Nonnull<Block*>> { return body_; }
  119. // Only contains function parameters. Scoped variables are in the body.
  120. auto static_scope() const -> const StaticScope& { return static_scope_; }
  121. auto static_scope() -> StaticScope& { return static_scope_; }
  122. private:
  123. std::string name_;
  124. std::vector<GenericBinding> deduced_parameters_;
  125. Nonnull<TuplePattern*> param_pattern_;
  126. Nonnull<Pattern*> return_type_;
  127. bool is_omitted_return_type_;
  128. std::optional<Nonnull<Block*>> body_;
  129. StaticScope static_scope_;
  130. };
  131. class ClassDeclaration : public Declaration {
  132. public:
  133. ClassDeclaration(SourceLocation source_loc, std::string name,
  134. std::vector<Nonnull<Member*>> members)
  135. : Declaration(Kind::ClassDeclaration, source_loc),
  136. definition_(source_loc, std::move(name), std::move(members)) {}
  137. static auto classof(const Declaration* decl) -> bool {
  138. return decl->kind() == Kind::ClassDeclaration;
  139. }
  140. auto definition() const -> const ClassDefinition& { return definition_; }
  141. auto definition() -> ClassDefinition& { return definition_; }
  142. private:
  143. ClassDefinition definition_;
  144. };
  145. class ChoiceDeclaration : public Declaration {
  146. public:
  147. class Alternative : public NamedEntityInterface {
  148. public:
  149. Alternative(SourceLocation source_loc, std::string name,
  150. Nonnull<Expression*> signature)
  151. : source_loc_(source_loc),
  152. name_(std::move(name)),
  153. signature_(signature) {}
  154. auto named_entity_kind() const -> NamedEntityKind override {
  155. return NamedEntityKind::ChoiceDeclarationAlternative;
  156. }
  157. auto source_loc() const -> SourceLocation override { return source_loc_; }
  158. auto name() const -> const std::string& { return name_; }
  159. auto signature() const -> const Expression& { return *signature_; }
  160. private:
  161. SourceLocation source_loc_;
  162. std::string name_;
  163. Nonnull<Expression*> signature_;
  164. };
  165. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  166. std::vector<Alternative> alternatives)
  167. : Declaration(Kind::ChoiceDeclaration, source_loc),
  168. name_(std::move(name)),
  169. alternatives_(std::move(alternatives)) {}
  170. static auto classof(const Declaration* decl) -> bool {
  171. return decl->kind() == Kind::ChoiceDeclaration;
  172. }
  173. auto name() const -> const std::string& { return name_; }
  174. auto alternatives() const -> llvm::ArrayRef<Alternative> {
  175. return alternatives_;
  176. }
  177. // Contains the alternatives.
  178. auto static_scope() const -> const StaticScope& { return static_scope_; }
  179. auto static_scope() -> StaticScope& { return static_scope_; }
  180. private:
  181. std::string name_;
  182. std::vector<Alternative> alternatives_;
  183. StaticScope static_scope_;
  184. };
  185. // Global variable definition implements the Declaration concept.
  186. class VariableDeclaration : public Declaration {
  187. public:
  188. VariableDeclaration(SourceLocation source_loc,
  189. Nonnull<BindingPattern*> binding,
  190. Nonnull<Expression*> initializer)
  191. : Declaration(Kind::VariableDeclaration, source_loc),
  192. binding_(binding),
  193. initializer_(initializer) {}
  194. static auto classof(const Declaration* decl) -> bool {
  195. return decl->kind() == Kind::VariableDeclaration;
  196. }
  197. auto binding() const -> const BindingPattern& { return *binding_; }
  198. auto binding() -> BindingPattern& { return *binding_; }
  199. auto initializer() const -> const Expression& { return *initializer_; }
  200. auto initializer() -> Expression& { return *initializer_; }
  201. private:
  202. // TODO: split this into a non-optional name and a type, initialized by
  203. // a constructor that takes a BindingPattern and handles errors like a
  204. // missing name.
  205. Nonnull<BindingPattern*> binding_;
  206. Nonnull<Expression*> initializer_;
  207. };
  208. } // namespace Carbon
  209. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_