declaration.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. case DeclarationKind::AliasDeclaration: {
  72. const auto& alias = cast<AliasDeclaration>(*this);
  73. PrintID(out);
  74. out << " = " << alias.target() << ";\n";
  75. break;
  76. }
  77. }
  78. }
  79. void Declaration::PrintID(llvm::raw_ostream& out) const {
  80. switch (kind()) {
  81. case DeclarationKind::InterfaceDeclaration: {
  82. const auto& iface_decl = cast<InterfaceDeclaration>(*this);
  83. out << "interface " << iface_decl.name();
  84. break;
  85. }
  86. case DeclarationKind::ImplDeclaration: {
  87. const auto& impl_decl = cast<ImplDeclaration>(*this);
  88. switch (impl_decl.kind()) {
  89. case ImplKind::InternalImpl:
  90. break;
  91. case ImplKind::ExternalImpl:
  92. out << "external ";
  93. break;
  94. }
  95. out << "impl " << *impl_decl.impl_type() << " as "
  96. << impl_decl.interface();
  97. break;
  98. }
  99. case DeclarationKind::FunctionDeclaration:
  100. out << "fn " << cast<FunctionDeclaration>(*this).name();
  101. break;
  102. case DeclarationKind::ClassDeclaration: {
  103. const auto& class_decl = cast<ClassDeclaration>(*this);
  104. out << "class " << class_decl.name();
  105. break;
  106. }
  107. case DeclarationKind::ChoiceDeclaration: {
  108. const auto& choice = cast<ChoiceDeclaration>(*this);
  109. out << "choice " << choice.name();
  110. break;
  111. }
  112. case DeclarationKind::VariableDeclaration: {
  113. const auto& var = cast<VariableDeclaration>(*this);
  114. out << "var " << var.binding();
  115. break;
  116. }
  117. case DeclarationKind::SelfDeclaration: {
  118. out << "Self";
  119. break;
  120. }
  121. case DeclarationKind::AliasDeclaration: {
  122. const auto& alias = cast<AliasDeclaration>(*this);
  123. out << "alias " << alias.name();
  124. break;
  125. }
  126. }
  127. }
  128. auto GetName(const Declaration& declaration) -> std::optional<std::string> {
  129. switch (declaration.kind()) {
  130. case DeclarationKind::FunctionDeclaration:
  131. return cast<FunctionDeclaration>(declaration).name();
  132. case DeclarationKind::ClassDeclaration:
  133. return cast<ClassDeclaration>(declaration).name();
  134. case DeclarationKind::ChoiceDeclaration:
  135. return cast<ChoiceDeclaration>(declaration).name();
  136. case DeclarationKind::InterfaceDeclaration:
  137. return cast<InterfaceDeclaration>(declaration).name();
  138. case DeclarationKind::VariableDeclaration:
  139. return cast<VariableDeclaration>(declaration).binding().name();
  140. case DeclarationKind::ImplDeclaration:
  141. return std::nullopt;
  142. case DeclarationKind::SelfDeclaration:
  143. return cast<SelfDeclaration>(declaration).name();
  144. case DeclarationKind::AliasDeclaration: {
  145. return cast<AliasDeclaration>(declaration).name();
  146. }
  147. }
  148. }
  149. void GenericBinding::Print(llvm::raw_ostream& out) const {
  150. out << name() << ":! " << type();
  151. }
  152. void GenericBinding::PrintID(llvm::raw_ostream& out) const { out << name(); }
  153. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  154. switch (kind_) {
  155. case ReturnKind::Omitted:
  156. return;
  157. case ReturnKind::Auto:
  158. out << "-> auto";
  159. return;
  160. case ReturnKind::Expression:
  161. CARBON_CHECK(type_expression_.has_value());
  162. out << "-> " << **type_expression_;
  163. return;
  164. }
  165. }
  166. auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
  167. SourceLocation source_loc, std::string name,
  168. std::vector<Nonnull<AstNode*>> deduced_params,
  169. std::optional<Nonnull<Pattern*>> me_pattern,
  170. Nonnull<TuplePattern*> param_pattern,
  171. ReturnTerm return_term,
  172. std::optional<Nonnull<Block*>> body)
  173. -> ErrorOr<Nonnull<FunctionDeclaration*>> {
  174. std::vector<Nonnull<GenericBinding*>> resolved_params;
  175. // Look for the `me` parameter in the `deduced_parameters`
  176. // and put it in the `me_pattern`.
  177. for (Nonnull<AstNode*> param : deduced_params) {
  178. switch (param->kind()) {
  179. case AstNodeKind::GenericBinding:
  180. resolved_params.push_back(&cast<GenericBinding>(*param));
  181. break;
  182. case AstNodeKind::BindingPattern: {
  183. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
  184. if (me_pattern.has_value() || bp->name() != "me") {
  185. return CompilationError(source_loc)
  186. << "illegal binding pattern in implicit parameter list";
  187. }
  188. me_pattern = bp;
  189. break;
  190. }
  191. case AstNodeKind::AddrPattern: {
  192. Nonnull<AddrPattern*> abp = &cast<AddrPattern>(*param);
  193. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(abp->binding());
  194. if (me_pattern.has_value() || bp->name() != "me") {
  195. return CompilationError(source_loc)
  196. << "illegal binding pattern in implicit parameter list";
  197. }
  198. me_pattern = abp;
  199. break;
  200. }
  201. default:
  202. return CompilationError(source_loc)
  203. << "illegal AST node in implicit parameter list";
  204. }
  205. }
  206. return arena->New<FunctionDeclaration>(source_loc, name,
  207. std::move(resolved_params), me_pattern,
  208. param_pattern, return_term, body);
  209. }
  210. void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  211. out << "fn " << name_ << " ";
  212. if (!deduced_parameters_.empty()) {
  213. out << "[";
  214. llvm::ListSeparator sep;
  215. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  216. out << sep << *deduced;
  217. }
  218. out << "]";
  219. }
  220. out << *param_pattern_ << return_term_;
  221. if (body_) {
  222. out << " {\n";
  223. (*body_)->PrintDepth(depth, out);
  224. out << "\n}\n";
  225. } else {
  226. out << ";\n";
  227. }
  228. }
  229. auto ImplDeclaration::Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  230. ImplKind kind, Nonnull<Expression*> impl_type,
  231. Nonnull<Expression*> interface,
  232. std::vector<Nonnull<AstNode*>> deduced_params,
  233. std::vector<Nonnull<Declaration*>> members)
  234. -> ErrorOr<Nonnull<ImplDeclaration*>> {
  235. std::vector<Nonnull<GenericBinding*>> resolved_params;
  236. for (Nonnull<AstNode*> param : deduced_params) {
  237. switch (param->kind()) {
  238. case AstNodeKind::GenericBinding:
  239. resolved_params.push_back(&cast<GenericBinding>(*param));
  240. break;
  241. default:
  242. return CompilationError(source_loc)
  243. << "illegal AST node in implicit parameter list of impl";
  244. }
  245. }
  246. Nonnull<SelfDeclaration*> self_decl =
  247. arena->New<SelfDeclaration>(impl_type->source_loc());
  248. return arena->New<ImplDeclaration>(source_loc, kind, impl_type, self_decl,
  249. interface, resolved_params, members);
  250. }
  251. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  252. out << "alt " << name() << " " << signature();
  253. }
  254. void AlternativeSignature::PrintID(llvm::raw_ostream& out) const {
  255. out << name();
  256. }
  257. } // namespace Carbon