declaration.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. namespace {
  221. // The deduced parameters of a function declaration.
  222. struct DeducedParameters {
  223. // The `me` parameter, if any.
  224. std::optional<Nonnull<Pattern*>> me_pattern;
  225. // All other deduced parameters.
  226. std::vector<Nonnull<GenericBinding*>> resolved_params;
  227. };
  228. // Split the `me` pattern (if any) out of `deduced_params`.
  229. auto SplitDeducedParameters(
  230. SourceLocation source_loc,
  231. const std::vector<Nonnull<AstNode*>>& deduced_params)
  232. -> ErrorOr<DeducedParameters> {
  233. DeducedParameters result;
  234. for (Nonnull<AstNode*> param : deduced_params) {
  235. switch (param->kind()) {
  236. case AstNodeKind::GenericBinding:
  237. result.resolved_params.push_back(&cast<GenericBinding>(*param));
  238. break;
  239. case AstNodeKind::BindingPattern: {
  240. Nonnull<BindingPattern*> binding = &cast<BindingPattern>(*param);
  241. if (binding->name() != "me") {
  242. return ProgramError(source_loc)
  243. << "illegal binding pattern in implicit parameter list";
  244. }
  245. if (result.me_pattern.has_value()) {
  246. return ProgramError(source_loc)
  247. << "parameter list cannot contain more than one `me` "
  248. "parameter";
  249. }
  250. result.me_pattern = binding;
  251. break;
  252. }
  253. case AstNodeKind::AddrPattern: {
  254. Nonnull<AddrPattern*> addr_pattern = &cast<AddrPattern>(*param);
  255. Nonnull<BindingPattern*> binding =
  256. &cast<BindingPattern>(addr_pattern->binding());
  257. if (binding->name() != "me") {
  258. return ProgramError(source_loc)
  259. << "illegal binding pattern in implicit parameter list";
  260. }
  261. if (result.me_pattern.has_value()) {
  262. return ProgramError(source_loc)
  263. << "parameter list cannot contain more than one `me` "
  264. "parameter";
  265. }
  266. result.me_pattern = addr_pattern;
  267. break;
  268. }
  269. default:
  270. return ProgramError(source_loc)
  271. << "illegal AST node in implicit parameter list";
  272. }
  273. }
  274. return result;
  275. }
  276. } // namespace
  277. auto DestructorDeclaration::CreateDestructor(
  278. Nonnull<Arena*> arena, SourceLocation source_loc,
  279. std::vector<Nonnull<AstNode*>> deduced_params,
  280. Nonnull<TuplePattern*> param_pattern, ReturnTerm return_term,
  281. std::optional<Nonnull<Block*>> body)
  282. -> ErrorOr<Nonnull<DestructorDeclaration*>> {
  283. DeducedParameters split_params;
  284. CARBON_ASSIGN_OR_RETURN(split_params,
  285. SplitDeducedParameters(source_loc, deduced_params));
  286. return arena->New<DestructorDeclaration>(
  287. source_loc, std::move(split_params.resolved_params),
  288. split_params.me_pattern, param_pattern, return_term, body);
  289. }
  290. auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
  291. SourceLocation source_loc, std::string name,
  292. std::vector<Nonnull<AstNode*>> deduced_params,
  293. Nonnull<TuplePattern*> param_pattern,
  294. ReturnTerm return_term,
  295. std::optional<Nonnull<Block*>> body)
  296. -> ErrorOr<Nonnull<FunctionDeclaration*>> {
  297. DeducedParameters split_params;
  298. CARBON_ASSIGN_OR_RETURN(split_params,
  299. SplitDeducedParameters(source_loc, deduced_params));
  300. return arena->New<FunctionDeclaration>(
  301. source_loc, name, std::move(split_params.resolved_params),
  302. split_params.me_pattern, param_pattern, return_term, body);
  303. }
  304. void CallableDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  305. out << "fn " << name_ << " ";
  306. if (!deduced_parameters_.empty()) {
  307. out << "[";
  308. llvm::ListSeparator sep;
  309. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  310. out << sep << *deduced;
  311. }
  312. out << "]";
  313. }
  314. out << *param_pattern_ << return_term_;
  315. if (body_) {
  316. out << " {\n";
  317. (*body_)->PrintDepth(depth, out);
  318. out << "\n}\n";
  319. } else {
  320. out << ";\n";
  321. }
  322. }
  323. auto ImplDeclaration::Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  324. ImplKind kind, Nonnull<Expression*> impl_type,
  325. Nonnull<Expression*> interface,
  326. std::vector<Nonnull<AstNode*>> deduced_params,
  327. std::vector<Nonnull<Declaration*>> members)
  328. -> ErrorOr<Nonnull<ImplDeclaration*>> {
  329. std::vector<Nonnull<GenericBinding*>> resolved_params;
  330. for (Nonnull<AstNode*> param : deduced_params) {
  331. switch (param->kind()) {
  332. case AstNodeKind::GenericBinding:
  333. resolved_params.push_back(&cast<GenericBinding>(*param));
  334. break;
  335. default:
  336. return ProgramError(source_loc)
  337. << "illegal AST node in implicit parameter list of impl";
  338. }
  339. }
  340. Nonnull<SelfDeclaration*> self_decl =
  341. arena->New<SelfDeclaration>(impl_type->source_loc());
  342. return arena->New<ImplDeclaration>(source_loc, kind, impl_type, self_decl,
  343. interface, resolved_params, members);
  344. }
  345. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  346. out << "alt " << name() << " " << signature();
  347. }
  348. void AlternativeSignature::PrintID(llvm::raw_ostream& out) const {
  349. out << name();
  350. }
  351. } // namespace Carbon