declaration.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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)
  129. -> std::optional<std::string_view> {
  130. switch (declaration.kind()) {
  131. case DeclarationKind::FunctionDeclaration:
  132. return cast<FunctionDeclaration>(declaration).name();
  133. case DeclarationKind::ClassDeclaration:
  134. return cast<ClassDeclaration>(declaration).name();
  135. case DeclarationKind::ChoiceDeclaration:
  136. return cast<ChoiceDeclaration>(declaration).name();
  137. case DeclarationKind::InterfaceDeclaration:
  138. return cast<InterfaceDeclaration>(declaration).name();
  139. case DeclarationKind::VariableDeclaration:
  140. return cast<VariableDeclaration>(declaration).binding().name();
  141. case DeclarationKind::ImplDeclaration:
  142. return std::nullopt;
  143. case DeclarationKind::SelfDeclaration:
  144. return cast<SelfDeclaration>(declaration).name();
  145. case DeclarationKind::AliasDeclaration: {
  146. return cast<AliasDeclaration>(declaration).name();
  147. }
  148. }
  149. }
  150. void GenericBinding::Print(llvm::raw_ostream& out) const {
  151. out << name() << ":! " << type();
  152. }
  153. void GenericBinding::PrintID(llvm::raw_ostream& out) const { out << name(); }
  154. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  155. switch (kind_) {
  156. case ReturnKind::Omitted:
  157. return;
  158. case ReturnKind::Auto:
  159. out << "-> auto";
  160. return;
  161. case ReturnKind::Expression:
  162. CARBON_CHECK(type_expression_.has_value());
  163. out << "-> " << **type_expression_;
  164. return;
  165. }
  166. }
  167. auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
  168. SourceLocation source_loc, std::string name,
  169. std::vector<Nonnull<AstNode*>> deduced_params,
  170. std::optional<Nonnull<Pattern*>> me_pattern,
  171. Nonnull<TuplePattern*> param_pattern,
  172. ReturnTerm return_term,
  173. std::optional<Nonnull<Block*>> body)
  174. -> ErrorOr<Nonnull<FunctionDeclaration*>> {
  175. std::vector<Nonnull<GenericBinding*>> resolved_params;
  176. // Look for the `me` parameter in the `deduced_parameters`
  177. // and put it in the `me_pattern`.
  178. for (Nonnull<AstNode*> param : deduced_params) {
  179. switch (param->kind()) {
  180. case AstNodeKind::GenericBinding:
  181. resolved_params.push_back(&cast<GenericBinding>(*param));
  182. break;
  183. case AstNodeKind::BindingPattern: {
  184. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
  185. if (me_pattern.has_value() || bp->name() != "me") {
  186. return CompilationError(source_loc)
  187. << "illegal binding pattern in implicit parameter list";
  188. }
  189. me_pattern = bp;
  190. break;
  191. }
  192. case AstNodeKind::AddrPattern: {
  193. Nonnull<AddrPattern*> abp = &cast<AddrPattern>(*param);
  194. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(abp->binding());
  195. if (me_pattern.has_value() || bp->name() != "me") {
  196. return CompilationError(source_loc)
  197. << "illegal binding pattern in implicit parameter list";
  198. }
  199. me_pattern = abp;
  200. break;
  201. }
  202. default:
  203. return CompilationError(source_loc)
  204. << "illegal AST node in implicit parameter list";
  205. }
  206. }
  207. return arena->New<FunctionDeclaration>(source_loc, name,
  208. std::move(resolved_params), me_pattern,
  209. param_pattern, return_term, body);
  210. }
  211. void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  212. out << "fn " << name_ << " ";
  213. if (!deduced_parameters_.empty()) {
  214. out << "[";
  215. llvm::ListSeparator sep;
  216. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  217. out << sep << *deduced;
  218. }
  219. out << "]";
  220. }
  221. out << *param_pattern_ << return_term_;
  222. if (body_) {
  223. out << " {\n";
  224. (*body_)->PrintDepth(depth, out);
  225. out << "\n}\n";
  226. } else {
  227. out << ";\n";
  228. }
  229. }
  230. auto ImplDeclaration::Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  231. ImplKind kind, Nonnull<Expression*> impl_type,
  232. Nonnull<Expression*> interface,
  233. std::vector<Nonnull<AstNode*>> deduced_params,
  234. std::vector<Nonnull<Declaration*>> members)
  235. -> ErrorOr<Nonnull<ImplDeclaration*>> {
  236. std::vector<Nonnull<GenericBinding*>> resolved_params;
  237. for (Nonnull<AstNode*> param : deduced_params) {
  238. switch (param->kind()) {
  239. case AstNodeKind::GenericBinding:
  240. resolved_params.push_back(&cast<GenericBinding>(*param));
  241. break;
  242. default:
  243. return CompilationError(source_loc)
  244. << "illegal AST node in implicit parameter list of impl";
  245. }
  246. }
  247. Nonnull<SelfDeclaration*> self_decl =
  248. arena->New<SelfDeclaration>(impl_type->source_loc());
  249. return arena->New<ImplDeclaration>(source_loc, kind, impl_type, self_decl,
  250. interface, resolved_params, members);
  251. }
  252. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  253. out << "alt " << name() << " " << signature();
  254. }
  255. void AlternativeSignature::PrintID(llvm::raw_ostream& out) const {
  256. out << name();
  257. }
  258. } // namespace Carbon