declaration.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/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 {
  28. public:
  29. enum class Kind {
  30. FunctionDeclaration,
  31. ClassDeclaration,
  32. ChoiceDeclaration,
  33. VariableDeclaration,
  34. };
  35. Declaration(const Member&) = delete;
  36. auto operator=(const Member&) -> Declaration& = delete;
  37. void Print(llvm::raw_ostream& out) const;
  38. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  39. // Returns the enumerator corresponding to the most-derived type of this
  40. // object.
  41. auto kind() const -> Kind { return kind_; }
  42. auto source_loc() const -> SourceLocation { return source_loc_; }
  43. // The static type of the declared entity. Cannot be called before
  44. // typechecking.
  45. auto static_type() const -> const Value& { return **static_type_; }
  46. // Sets the static type of the declared entity. Can only be called once,
  47. // during typechecking.
  48. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  49. // Returns whether the static type has been set. Should only be called
  50. // during typechecking: before typechecking it's guaranteed to be false,
  51. // and after typechecking it's guaranteed to be true.
  52. auto has_static_type() const -> bool { return static_type_.has_value(); }
  53. protected:
  54. // Constructs a Declaration representing syntax at the given line number.
  55. // `kind` must be the enumerator corresponding to the most-derived type being
  56. // constructed.
  57. Declaration(Kind kind, SourceLocation source_loc)
  58. : kind_(kind), source_loc_(source_loc) {}
  59. private:
  60. const Kind kind_;
  61. SourceLocation source_loc_;
  62. std::optional<Nonnull<const Value*>> static_type_;
  63. };
  64. // TODO: expand the kinds of things that can be deduced parameters.
  65. // For now, only generic parameters are supported.
  66. struct GenericBinding {
  67. std::string name;
  68. Nonnull<const Expression*> type;
  69. };
  70. class FunctionDeclaration : public Declaration {
  71. public:
  72. FunctionDeclaration(SourceLocation source_loc, std::string name,
  73. std::vector<GenericBinding> deduced_params,
  74. Nonnull<TuplePattern*> param_pattern,
  75. Nonnull<Pattern*> return_type,
  76. bool is_omitted_return_type,
  77. std::optional<Nonnull<Statement*>> body)
  78. : Declaration(Kind::FunctionDeclaration, source_loc),
  79. name_(std::move(name)),
  80. deduced_parameters_(std::move(deduced_params)),
  81. param_pattern_(param_pattern),
  82. return_type_(return_type),
  83. is_omitted_return_type_(is_omitted_return_type),
  84. body_(body) {}
  85. static auto classof(const Declaration* decl) -> bool {
  86. return decl->kind() == Kind::FunctionDeclaration;
  87. }
  88. void PrintDepth(int depth, llvm::raw_ostream& out) const;
  89. auto name() const -> const std::string& { return name_; }
  90. auto deduced_parameters() const -> llvm::ArrayRef<GenericBinding> {
  91. return deduced_parameters_;
  92. }
  93. auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
  94. auto param_pattern() -> TuplePattern& { return *param_pattern_; }
  95. auto return_type() const -> const Pattern& { return *return_type_; }
  96. auto return_type() -> Pattern& { return *return_type_; }
  97. auto is_omitted_return_type() const -> bool {
  98. return is_omitted_return_type_;
  99. }
  100. auto body() const -> std::optional<Nonnull<const Statement*>> {
  101. return body_;
  102. }
  103. auto body() -> std::optional<Nonnull<Statement*>> { return body_; }
  104. private:
  105. std::string name_;
  106. std::vector<GenericBinding> deduced_parameters_;
  107. Nonnull<TuplePattern*> param_pattern_;
  108. Nonnull<Pattern*> return_type_;
  109. bool is_omitted_return_type_;
  110. std::optional<Nonnull<Statement*>> body_;
  111. };
  112. class ClassDeclaration : public Declaration {
  113. public:
  114. ClassDeclaration(SourceLocation source_loc, std::string name,
  115. std::vector<Nonnull<Member*>> members)
  116. : Declaration(Kind::ClassDeclaration, source_loc),
  117. definition_(source_loc, std::move(name), std::move(members)) {}
  118. static auto classof(const Declaration* decl) -> bool {
  119. return decl->kind() == Kind::ClassDeclaration;
  120. }
  121. auto definition() const -> const ClassDefinition& { return definition_; }
  122. auto definition() -> ClassDefinition& { return definition_; }
  123. private:
  124. ClassDefinition definition_;
  125. };
  126. class ChoiceDeclaration : public Declaration {
  127. public:
  128. class Alternative {
  129. public:
  130. Alternative(std::string name, Nonnull<Expression*> signature)
  131. : name_(std::move(name)), signature_(signature) {}
  132. auto name() const -> const std::string& { return name_; }
  133. auto signature() const -> const Expression& { return *signature_; }
  134. private:
  135. std::string name_;
  136. Nonnull<Expression*> signature_;
  137. };
  138. ChoiceDeclaration(SourceLocation source_loc, std::string name,
  139. std::vector<Alternative> alternatives)
  140. : Declaration(Kind::ChoiceDeclaration, source_loc),
  141. name_(std::move(name)),
  142. alternatives_(std::move(alternatives)) {}
  143. static auto classof(const Declaration* decl) -> bool {
  144. return decl->kind() == Kind::ChoiceDeclaration;
  145. }
  146. auto name() const -> const std::string& { return name_; }
  147. auto alternatives() const -> llvm::ArrayRef<Alternative> {
  148. return alternatives_;
  149. }
  150. private:
  151. std::string name_;
  152. std::vector<Alternative> alternatives_;
  153. };
  154. // Global variable definition implements the Declaration concept.
  155. class VariableDeclaration : public Declaration {
  156. public:
  157. VariableDeclaration(SourceLocation source_loc,
  158. Nonnull<BindingPattern*> binding,
  159. Nonnull<Expression*> initializer)
  160. : Declaration(Kind::VariableDeclaration, source_loc),
  161. binding_(binding),
  162. initializer_(initializer) {}
  163. static auto classof(const Declaration* decl) -> bool {
  164. return decl->kind() == Kind::VariableDeclaration;
  165. }
  166. auto binding() const -> const BindingPattern& { return *binding_; }
  167. auto binding() -> BindingPattern& { return *binding_; }
  168. auto initializer() const -> const Expression& { return *initializer_; }
  169. auto initializer() -> Expression& { return *initializer_; }
  170. private:
  171. // TODO: split this into a non-optional name and a type, initialized by
  172. // a constructor that takes a BindingPattern and handles errors like a
  173. // missing name.
  174. Nonnull<BindingPattern*> binding_;
  175. Nonnull<Expression*> initializer_;
  176. };
  177. } // namespace Carbon
  178. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_