declaration.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include "explorer/ast/declaration.h"
  5. #include "llvm/ADT/StringExtras.h"
  6. #include "llvm/Support/Casting.h"
  7. namespace Carbon {
  8. using llvm::cast;
  9. Declaration::~Declaration() = default;
  10. void Declaration::Print(llvm::raw_ostream& out) const {
  11. switch (kind()) {
  12. case DeclarationKind::InterfaceDeclaration: {
  13. const auto& iface_decl = cast<InterfaceDeclaration>(*this);
  14. PrintID(out);
  15. out << " {\n";
  16. for (Nonnull<Declaration*> m : iface_decl.members()) {
  17. out << *m;
  18. }
  19. out << "}\n";
  20. break;
  21. }
  22. case DeclarationKind::ImplDeclaration: {
  23. const auto& impl_decl = cast<ImplDeclaration>(*this);
  24. PrintID(out);
  25. out << " {\n";
  26. for (Nonnull<Declaration*> m : impl_decl.members()) {
  27. out << *m;
  28. }
  29. out << "}\n";
  30. break;
  31. }
  32. case DeclarationKind::FunctionDeclaration:
  33. cast<FunctionDeclaration>(*this).PrintDepth(-1, out);
  34. break;
  35. case DeclarationKind::ClassDeclaration: {
  36. const auto& class_decl = cast<ClassDeclaration>(*this);
  37. PrintID(out);
  38. if (class_decl.type_params().has_value()) {
  39. out << **class_decl.type_params();
  40. }
  41. out << " {\n";
  42. for (Nonnull<Declaration*> m : class_decl.members()) {
  43. out << *m;
  44. }
  45. out << "}\n";
  46. break;
  47. }
  48. case DeclarationKind::ChoiceDeclaration: {
  49. const auto& choice = cast<ChoiceDeclaration>(*this);
  50. PrintID(out);
  51. out << " {\n";
  52. for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
  53. out << *alt << ";\n";
  54. }
  55. out << "}\n";
  56. break;
  57. }
  58. case DeclarationKind::VariableDeclaration: {
  59. const auto& var = cast<VariableDeclaration>(*this);
  60. PrintID(out);
  61. if (var.has_initializer()) {
  62. out << " = " << var.initializer();
  63. }
  64. out << ";\n";
  65. break;
  66. }
  67. case DeclarationKind::SelfDeclaration: {
  68. out << "Self";
  69. break;
  70. }
  71. }
  72. }
  73. void Declaration::PrintID(llvm::raw_ostream& out) const {
  74. switch (kind()) {
  75. case DeclarationKind::InterfaceDeclaration: {
  76. const auto& iface_decl = cast<InterfaceDeclaration>(*this);
  77. out << "interface " << iface_decl.name();
  78. break;
  79. }
  80. case DeclarationKind::ImplDeclaration: {
  81. const auto& impl_decl = cast<ImplDeclaration>(*this);
  82. switch (impl_decl.kind()) {
  83. case ImplKind::InternalImpl:
  84. break;
  85. case ImplKind::ExternalImpl:
  86. out << "external ";
  87. break;
  88. }
  89. out << "impl " << *impl_decl.impl_type() << " as "
  90. << impl_decl.interface();
  91. break;
  92. }
  93. case DeclarationKind::FunctionDeclaration:
  94. out << "fn " << cast<FunctionDeclaration>(*this).name();
  95. break;
  96. case DeclarationKind::ClassDeclaration: {
  97. const auto& class_decl = cast<ClassDeclaration>(*this);
  98. out << "class " << class_decl.name();
  99. break;
  100. }
  101. case DeclarationKind::ChoiceDeclaration: {
  102. const auto& choice = cast<ChoiceDeclaration>(*this);
  103. out << "choice " << choice.name();
  104. break;
  105. }
  106. case DeclarationKind::VariableDeclaration: {
  107. const auto& var = cast<VariableDeclaration>(*this);
  108. out << "var " << var.binding();
  109. break;
  110. }
  111. case DeclarationKind::SelfDeclaration: {
  112. out << "Self";
  113. break;
  114. }
  115. }
  116. }
  117. auto GetName(const Declaration& declaration) -> std::optional<std::string> {
  118. switch (declaration.kind()) {
  119. case DeclarationKind::FunctionDeclaration:
  120. return cast<FunctionDeclaration>(declaration).name();
  121. case DeclarationKind::ClassDeclaration:
  122. return cast<ClassDeclaration>(declaration).name();
  123. case DeclarationKind::ChoiceDeclaration:
  124. return cast<ChoiceDeclaration>(declaration).name();
  125. case DeclarationKind::InterfaceDeclaration:
  126. return cast<InterfaceDeclaration>(declaration).name();
  127. case DeclarationKind::VariableDeclaration:
  128. return cast<VariableDeclaration>(declaration).binding().name();
  129. case DeclarationKind::ImplDeclaration:
  130. return std::nullopt;
  131. case DeclarationKind::SelfDeclaration:
  132. return cast<SelfDeclaration>(declaration).name();
  133. }
  134. }
  135. void GenericBinding::Print(llvm::raw_ostream& out) const {
  136. out << name() << ":! " << type();
  137. }
  138. void GenericBinding::PrintID(llvm::raw_ostream& out) const { out << name(); }
  139. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  140. switch (kind_) {
  141. case ReturnKind::Omitted:
  142. return;
  143. case ReturnKind::Auto:
  144. out << "-> auto";
  145. return;
  146. case ReturnKind::Expression:
  147. CARBON_CHECK(type_expression_.has_value());
  148. out << "-> " << **type_expression_;
  149. return;
  150. }
  151. }
  152. auto FunctionDeclaration::Create(
  153. Nonnull<Arena*> arena, SourceLocation source_loc, std::string name,
  154. std::vector<Nonnull<AstNode*>> deduced_params,
  155. std::optional<Nonnull<BindingPattern*>> me_pattern,
  156. Nonnull<TuplePattern*> param_pattern, ReturnTerm return_term,
  157. std::optional<Nonnull<Block*>> body)
  158. -> ErrorOr<Nonnull<FunctionDeclaration*>> {
  159. std::vector<Nonnull<GenericBinding*>> resolved_params;
  160. // Look for the `me` parameter in the `deduced_parameters`
  161. // and put it in the `me_pattern`.
  162. for (Nonnull<AstNode*> param : deduced_params) {
  163. switch (param->kind()) {
  164. case AstNodeKind::GenericBinding:
  165. resolved_params.push_back(&cast<GenericBinding>(*param));
  166. break;
  167. case AstNodeKind::BindingPattern: {
  168. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
  169. if (me_pattern.has_value() || bp->name() != "me") {
  170. return CompilationError(source_loc)
  171. << "illegal binding pattern in implicit parameter list";
  172. }
  173. me_pattern = bp;
  174. break;
  175. }
  176. default:
  177. return CompilationError(source_loc)
  178. << "illegal AST node in implicit parameter list";
  179. }
  180. }
  181. return arena->New<FunctionDeclaration>(source_loc, name,
  182. std::move(resolved_params), me_pattern,
  183. param_pattern, return_term, body);
  184. }
  185. void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  186. out << "fn " << name_ << " ";
  187. if (!deduced_parameters_.empty()) {
  188. out << "[";
  189. llvm::ListSeparator sep;
  190. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  191. out << sep << *deduced;
  192. }
  193. out << "]";
  194. }
  195. out << *param_pattern_ << return_term_;
  196. if (body_) {
  197. out << " {\n";
  198. (*body_)->PrintDepth(depth, out);
  199. out << "\n}\n";
  200. } else {
  201. out << ";\n";
  202. }
  203. }
  204. auto ImplDeclaration::Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  205. ImplKind kind, Nonnull<Expression*> impl_type,
  206. Nonnull<Expression*> interface,
  207. std::vector<Nonnull<AstNode*>> deduced_params,
  208. std::vector<Nonnull<Declaration*>> members)
  209. -> ErrorOr<Nonnull<ImplDeclaration*>> {
  210. std::vector<Nonnull<GenericBinding*>> resolved_params;
  211. for (Nonnull<AstNode*> param : deduced_params) {
  212. switch (param->kind()) {
  213. case AstNodeKind::GenericBinding:
  214. resolved_params.push_back(&cast<GenericBinding>(*param));
  215. break;
  216. default:
  217. return CompilationError(source_loc)
  218. << "illegal AST node in implicit parameter list of impl";
  219. }
  220. }
  221. Nonnull<SelfDeclaration*> self_decl =
  222. arena->New<SelfDeclaration>(impl_type->source_loc());
  223. return arena->New<ImplDeclaration>(source_loc, kind, impl_type, self_decl,
  224. interface, resolved_params, members);
  225. }
  226. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  227. out << "alt " << name() << " " << signature();
  228. }
  229. void AlternativeSignature::PrintID(llvm::raw_ostream& out) const {
  230. out << name();
  231. }
  232. } // namespace Carbon