declaration.cpp 9.3 KB

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