declaration.h 7.3 KB

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