declaration.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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::DestructorDeclaration:
  36. cast<DestructorDeclaration>(*this).PrintDepth(-1, out);
  37. break;
  38. case DeclarationKind::ClassDeclaration: {
  39. const auto& class_decl = cast<ClassDeclaration>(*this);
  40. PrintID(out);
  41. if (class_decl.type_params().has_value()) {
  42. out << **class_decl.type_params();
  43. }
  44. out << " {\n";
  45. for (Nonnull<Declaration*> m : class_decl.members()) {
  46. out << *m;
  47. }
  48. out << "}\n";
  49. break;
  50. }
  51. case DeclarationKind::MixinDeclaration: {
  52. const auto& mixin_decl = cast<MixinDeclaration>(*this);
  53. PrintID(out);
  54. out << "{\n";
  55. for (Nonnull<Declaration*> m : mixin_decl.members()) {
  56. out << *m;
  57. }
  58. out << "}\n";
  59. break;
  60. }
  61. case DeclarationKind::MixDeclaration: {
  62. const auto& mix_decl = cast<MixDeclaration>(*this);
  63. PrintID(out);
  64. out << mix_decl.mixin() << ";";
  65. break;
  66. }
  67. case DeclarationKind::ChoiceDeclaration: {
  68. const auto& choice = cast<ChoiceDeclaration>(*this);
  69. PrintID(out);
  70. out << " {\n";
  71. for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
  72. out << *alt << ";\n";
  73. }
  74. out << "}\n";
  75. break;
  76. }
  77. case DeclarationKind::VariableDeclaration: {
  78. const auto& var = cast<VariableDeclaration>(*this);
  79. PrintID(out);
  80. if (var.has_initializer()) {
  81. out << " = " << var.initializer();
  82. }
  83. out << ";\n";
  84. break;
  85. }
  86. case DeclarationKind::AssociatedConstantDeclaration:
  87. PrintID(out);
  88. out << ";\n";
  89. break;
  90. case DeclarationKind::SelfDeclaration: {
  91. out << "Self";
  92. break;
  93. }
  94. case DeclarationKind::AliasDeclaration: {
  95. const auto& alias = cast<AliasDeclaration>(*this);
  96. PrintID(out);
  97. out << " = " << alias.target() << ";\n";
  98. break;
  99. }
  100. }
  101. }
  102. void Declaration::PrintID(llvm::raw_ostream& out) const {
  103. switch (kind()) {
  104. case DeclarationKind::InterfaceDeclaration: {
  105. const auto& iface_decl = cast<InterfaceDeclaration>(*this);
  106. out << "interface " << iface_decl.name();
  107. break;
  108. }
  109. case DeclarationKind::ImplDeclaration: {
  110. const auto& impl_decl = cast<ImplDeclaration>(*this);
  111. switch (impl_decl.kind()) {
  112. case ImplKind::InternalImpl:
  113. break;
  114. case ImplKind::ExternalImpl:
  115. out << "external ";
  116. break;
  117. }
  118. out << "impl " << *impl_decl.impl_type() << " as "
  119. << impl_decl.interface();
  120. break;
  121. }
  122. case DeclarationKind::FunctionDeclaration:
  123. out << "fn " << cast<FunctionDeclaration>(*this).name();
  124. break;
  125. case DeclarationKind::DestructorDeclaration:
  126. out << cast<DestructorDeclaration>(*this).name();
  127. break;
  128. case DeclarationKind::ClassDeclaration: {
  129. const auto& class_decl = cast<ClassDeclaration>(*this);
  130. out << "class " << class_decl.name();
  131. break;
  132. }
  133. case DeclarationKind::MixinDeclaration: {
  134. const auto& mixin_decl = cast<MixinDeclaration>(*this);
  135. out << "__mixin " << mixin_decl.name();
  136. if (mixin_decl.self()->type().kind() != ExpressionKind::TypeTypeLiteral) {
  137. out << " for " << mixin_decl.self()->type();
  138. }
  139. break;
  140. }
  141. case DeclarationKind::MixDeclaration: {
  142. out << "__mix ";
  143. break;
  144. }
  145. case DeclarationKind::ChoiceDeclaration: {
  146. const auto& choice = cast<ChoiceDeclaration>(*this);
  147. out << "choice " << choice.name();
  148. break;
  149. }
  150. case DeclarationKind::VariableDeclaration: {
  151. const auto& var = cast<VariableDeclaration>(*this);
  152. out << "var " << var.binding();
  153. break;
  154. }
  155. case DeclarationKind::AssociatedConstantDeclaration: {
  156. const auto& let = cast<AssociatedConstantDeclaration>(*this);
  157. out << "let " << let.binding();
  158. break;
  159. }
  160. case DeclarationKind::SelfDeclaration: {
  161. out << "Self";
  162. break;
  163. }
  164. case DeclarationKind::AliasDeclaration: {
  165. const auto& alias = cast<AliasDeclaration>(*this);
  166. out << "alias " << alias.name();
  167. break;
  168. }
  169. }
  170. }
  171. auto GetName(const Declaration& declaration)
  172. -> std::optional<std::string_view> {
  173. switch (declaration.kind()) {
  174. case DeclarationKind::FunctionDeclaration:
  175. return cast<FunctionDeclaration>(declaration).name();
  176. case DeclarationKind::DestructorDeclaration:
  177. return cast<DestructorDeclaration>(declaration).name();
  178. case DeclarationKind::ClassDeclaration:
  179. return cast<ClassDeclaration>(declaration).name();
  180. case DeclarationKind::MixinDeclaration: {
  181. return cast<MixinDeclaration>(declaration).name();
  182. }
  183. case DeclarationKind::MixDeclaration: {
  184. return std::nullopt;
  185. }
  186. case DeclarationKind::ChoiceDeclaration:
  187. return cast<ChoiceDeclaration>(declaration).name();
  188. case DeclarationKind::InterfaceDeclaration:
  189. return cast<InterfaceDeclaration>(declaration).name();
  190. case DeclarationKind::VariableDeclaration:
  191. return cast<VariableDeclaration>(declaration).binding().name();
  192. case DeclarationKind::AssociatedConstantDeclaration:
  193. return cast<AssociatedConstantDeclaration>(declaration).binding().name();
  194. case DeclarationKind::ImplDeclaration:
  195. return std::nullopt;
  196. case DeclarationKind::SelfDeclaration:
  197. return cast<SelfDeclaration>(declaration).name();
  198. case DeclarationKind::AliasDeclaration: {
  199. return cast<AliasDeclaration>(declaration).name();
  200. }
  201. }
  202. }
  203. void GenericBinding::Print(llvm::raw_ostream& out) const {
  204. out << name() << ":! " << type();
  205. }
  206. void GenericBinding::PrintID(llvm::raw_ostream& out) const { out << name(); }
  207. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  208. switch (kind_) {
  209. case ReturnKind::Omitted:
  210. return;
  211. case ReturnKind::Auto:
  212. out << "-> auto";
  213. return;
  214. case ReturnKind::Expression:
  215. CARBON_CHECK(type_expression_.has_value());
  216. out << "-> " << **type_expression_;
  217. return;
  218. }
  219. }
  220. // Look for the `me` parameter in the `deduced_parameters`
  221. // and put it in the `me_pattern`.
  222. static auto MoveMeParameterToPattern(
  223. SourceLocation source_loc, std::optional<Nonnull<Pattern*>>& me_pattern,
  224. const std::vector<Nonnull<AstNode*>>& deduced_params)
  225. -> ErrorOr<std::vector<Nonnull<GenericBinding*>>> {
  226. std::vector<Nonnull<GenericBinding*>> resolved_params;
  227. for (Nonnull<AstNode*> param : deduced_params) {
  228. switch (param->kind()) {
  229. case AstNodeKind::GenericBinding:
  230. resolved_params.push_back(&cast<GenericBinding>(*param));
  231. break;
  232. case AstNodeKind::BindingPattern: {
  233. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(*param);
  234. if (me_pattern.has_value() || bp->name() != "me") {
  235. return CompilationError(source_loc)
  236. << "illegal binding pattern in implicit parameter list";
  237. }
  238. me_pattern = bp;
  239. break;
  240. }
  241. case AstNodeKind::AddrPattern: {
  242. Nonnull<AddrPattern*> abp = &cast<AddrPattern>(*param);
  243. Nonnull<BindingPattern*> bp = &cast<BindingPattern>(abp->binding());
  244. if (me_pattern.has_value() || bp->name() != "me") {
  245. return CompilationError(source_loc)
  246. << "illegal binding pattern in implicit parameter list";
  247. }
  248. me_pattern = abp;
  249. break;
  250. }
  251. default:
  252. return CompilationError(source_loc)
  253. << "illegal AST node in implicit parameter list";
  254. }
  255. }
  256. return resolved_params;
  257. }
  258. auto DestructorDeclaration::CreateDestructor(
  259. Nonnull<Arena*> arena, SourceLocation source_loc,
  260. std::vector<Nonnull<AstNode*>> deduced_params,
  261. Nonnull<TuplePattern*> param_pattern, ReturnTerm return_term,
  262. std::optional<Nonnull<Block*>> body)
  263. -> ErrorOr<Nonnull<DestructorDeclaration*>> {
  264. std::vector<Nonnull<GenericBinding*>> resolved_params;
  265. std::optional<Nonnull<Pattern*>> me_pattern;
  266. CARBON_ASSIGN_OR_RETURN(
  267. resolved_params,
  268. MoveMeParameterToPattern(source_loc, me_pattern, deduced_params));
  269. return arena->New<DestructorDeclaration>(
  270. source_loc, std::move(resolved_params), me_pattern, param_pattern,
  271. return_term, body);
  272. }
  273. auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
  274. SourceLocation source_loc, std::string name,
  275. std::vector<Nonnull<AstNode*>> deduced_params,
  276. std::optional<Nonnull<Pattern*>> me_pattern,
  277. Nonnull<TuplePattern*> param_pattern,
  278. ReturnTerm return_term,
  279. std::optional<Nonnull<Block*>> body)
  280. -> ErrorOr<Nonnull<FunctionDeclaration*>> {
  281. std::vector<Nonnull<GenericBinding*>> resolved_params;
  282. CARBON_ASSIGN_OR_RETURN(
  283. resolved_params,
  284. MoveMeParameterToPattern(source_loc, me_pattern, deduced_params));
  285. return arena->New<FunctionDeclaration>(source_loc, name,
  286. std::move(resolved_params), me_pattern,
  287. param_pattern, return_term, body);
  288. }
  289. void CallableDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  290. out << "fn " << name_ << " ";
  291. if (!deduced_parameters_.empty()) {
  292. out << "[";
  293. llvm::ListSeparator sep;
  294. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  295. out << sep << *deduced;
  296. }
  297. out << "]";
  298. }
  299. out << *param_pattern_ << return_term_;
  300. if (body_) {
  301. out << " {\n";
  302. (*body_)->PrintDepth(depth, out);
  303. out << "\n}\n";
  304. } else {
  305. out << ";\n";
  306. }
  307. }
  308. auto ImplDeclaration::Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  309. ImplKind kind, Nonnull<Expression*> impl_type,
  310. Nonnull<Expression*> interface,
  311. std::vector<Nonnull<AstNode*>> deduced_params,
  312. std::vector<Nonnull<Declaration*>> members)
  313. -> ErrorOr<Nonnull<ImplDeclaration*>> {
  314. std::vector<Nonnull<GenericBinding*>> resolved_params;
  315. for (Nonnull<AstNode*> param : deduced_params) {
  316. switch (param->kind()) {
  317. case AstNodeKind::GenericBinding:
  318. resolved_params.push_back(&cast<GenericBinding>(*param));
  319. break;
  320. default:
  321. return CompilationError(source_loc)
  322. << "illegal AST node in implicit parameter list of impl";
  323. }
  324. }
  325. Nonnull<SelfDeclaration*> self_decl =
  326. arena->New<SelfDeclaration>(impl_type->source_loc());
  327. return arena->New<ImplDeclaration>(source_loc, kind, impl_type, self_decl,
  328. interface, resolved_params, members);
  329. }
  330. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  331. out << "alt " << name() << " " << signature();
  332. }
  333. void AlternativeSignature::PrintID(llvm::raw_ostream& out) const {
  334. out << name();
  335. }
  336. } // namespace Carbon