declaration.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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::NamespaceDeclaration:
  13. PrintID(out);
  14. out << ";";
  15. break;
  16. case DeclarationKind::InterfaceDeclaration:
  17. case DeclarationKind::ConstraintDeclaration: {
  18. const auto& iface_decl = cast<ConstraintTypeDeclaration>(*this);
  19. PrintID(out);
  20. out << " {\n";
  21. for (Nonnull<Declaration*> m : iface_decl.members()) {
  22. out << *m;
  23. }
  24. out << "}\n";
  25. break;
  26. }
  27. case DeclarationKind::ImplDeclaration: {
  28. const auto& impl_decl = cast<ImplDeclaration>(*this);
  29. PrintID(out);
  30. out << " {\n";
  31. for (Nonnull<Declaration*> m : impl_decl.members()) {
  32. out << *m;
  33. }
  34. out << "}\n";
  35. break;
  36. }
  37. case DeclarationKind::MatchFirstDeclaration: {
  38. const auto& match_first_decl = cast<MatchFirstDeclaration>(*this);
  39. PrintID(out);
  40. out << " {\n";
  41. for (Nonnull<const ImplDeclaration*> m : match_first_decl.impls()) {
  42. out << *m;
  43. }
  44. out << "}\n";
  45. break;
  46. }
  47. case DeclarationKind::FunctionDeclaration:
  48. cast<FunctionDeclaration>(*this).PrintDepth(-1, out);
  49. break;
  50. case DeclarationKind::DestructorDeclaration:
  51. cast<DestructorDeclaration>(*this).PrintDepth(-1, out);
  52. break;
  53. case DeclarationKind::ClassDeclaration: {
  54. const auto& class_decl = cast<ClassDeclaration>(*this);
  55. PrintID(out);
  56. if (class_decl.type_params().has_value()) {
  57. out << **class_decl.type_params();
  58. }
  59. out << " {\n";
  60. for (Nonnull<Declaration*> m : class_decl.members()) {
  61. out << *m;
  62. }
  63. out << "}\n";
  64. break;
  65. }
  66. case DeclarationKind::MixinDeclaration: {
  67. const auto& mixin_decl = cast<MixinDeclaration>(*this);
  68. PrintID(out);
  69. out << "{\n";
  70. for (Nonnull<Declaration*> m : mixin_decl.members()) {
  71. out << *m;
  72. }
  73. out << "}\n";
  74. break;
  75. }
  76. case DeclarationKind::MixDeclaration: {
  77. const auto& mix_decl = cast<MixDeclaration>(*this);
  78. PrintID(out);
  79. out << mix_decl.mixin() << ";";
  80. break;
  81. }
  82. case DeclarationKind::ChoiceDeclaration: {
  83. const auto& choice = cast<ChoiceDeclaration>(*this);
  84. PrintID(out);
  85. out << " {\n";
  86. for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
  87. out << *alt << ";\n";
  88. }
  89. out << "}\n";
  90. break;
  91. }
  92. case DeclarationKind::VariableDeclaration: {
  93. const auto& var = cast<VariableDeclaration>(*this);
  94. PrintID(out);
  95. if (var.has_initializer()) {
  96. out << " = " << var.initializer();
  97. }
  98. out << ";\n";
  99. break;
  100. }
  101. case DeclarationKind::InterfaceExtendsDeclaration:
  102. case DeclarationKind::InterfaceImplDeclaration:
  103. case DeclarationKind::AssociatedConstantDeclaration: {
  104. PrintID(out);
  105. out << ";\n";
  106. break;
  107. }
  108. case DeclarationKind::SelfDeclaration: {
  109. out << "Self";
  110. break;
  111. }
  112. case DeclarationKind::AliasDeclaration: {
  113. const auto& alias = cast<AliasDeclaration>(*this);
  114. PrintID(out);
  115. out << " = " << alias.target() << ";\n";
  116. break;
  117. }
  118. }
  119. }
  120. void Declaration::PrintID(llvm::raw_ostream& out) const {
  121. switch (kind()) {
  122. case DeclarationKind::NamespaceDeclaration:
  123. out << "namespace " << cast<NamespaceDeclaration>(*this).name();
  124. break;
  125. case DeclarationKind::InterfaceDeclaration: {
  126. const auto& iface_decl = cast<InterfaceDeclaration>(*this);
  127. out << "interface " << iface_decl.name();
  128. break;
  129. }
  130. case DeclarationKind::ConstraintDeclaration: {
  131. const auto& constraint_decl = cast<ConstraintDeclaration>(*this);
  132. out << "constraint " << constraint_decl.name();
  133. break;
  134. }
  135. case DeclarationKind::ImplDeclaration: {
  136. const auto& impl_decl = cast<ImplDeclaration>(*this);
  137. switch (impl_decl.kind()) {
  138. case ImplKind::InternalImpl:
  139. break;
  140. case ImplKind::ExternalImpl:
  141. out << "external ";
  142. break;
  143. }
  144. out << "impl " << *impl_decl.impl_type() << " as "
  145. << impl_decl.interface();
  146. break;
  147. }
  148. case DeclarationKind::MatchFirstDeclaration:
  149. out << "match_first";
  150. break;
  151. case DeclarationKind::FunctionDeclaration:
  152. out << "fn " << cast<FunctionDeclaration>(*this).name();
  153. break;
  154. case DeclarationKind::DestructorDeclaration:
  155. out << *GetName(*this);
  156. break;
  157. case DeclarationKind::ClassDeclaration: {
  158. const auto& class_decl = cast<ClassDeclaration>(*this);
  159. out << "class " << class_decl.name();
  160. break;
  161. }
  162. case DeclarationKind::MixinDeclaration: {
  163. const auto& mixin_decl = cast<MixinDeclaration>(*this);
  164. out << "__mixin " << mixin_decl.name();
  165. if (mixin_decl.self()->type().kind() != ExpressionKind::TypeTypeLiteral) {
  166. out << " for " << mixin_decl.self()->type();
  167. }
  168. break;
  169. }
  170. case DeclarationKind::MixDeclaration: {
  171. out << "__mix ";
  172. break;
  173. }
  174. case DeclarationKind::ChoiceDeclaration: {
  175. const auto& choice = cast<ChoiceDeclaration>(*this);
  176. out << "choice " << choice.name();
  177. break;
  178. }
  179. case DeclarationKind::VariableDeclaration: {
  180. const auto& var = cast<VariableDeclaration>(*this);
  181. out << "var " << var.binding();
  182. break;
  183. }
  184. case DeclarationKind::InterfaceExtendsDeclaration: {
  185. const auto& extends = cast<InterfaceExtendsDeclaration>(*this);
  186. out << "extends " << *extends.base();
  187. break;
  188. }
  189. case DeclarationKind::InterfaceImplDeclaration: {
  190. const auto& impl = cast<InterfaceImplDeclaration>(*this);
  191. out << "impl " << *impl.impl_type() << " as " << *impl.constraint();
  192. break;
  193. }
  194. case DeclarationKind::AssociatedConstantDeclaration: {
  195. const auto& let = cast<AssociatedConstantDeclaration>(*this);
  196. out << "let " << let.binding();
  197. break;
  198. }
  199. case DeclarationKind::SelfDeclaration: {
  200. out << "Self";
  201. break;
  202. }
  203. case DeclarationKind::AliasDeclaration: {
  204. const auto& alias = cast<AliasDeclaration>(*this);
  205. out << "alias " << alias.name();
  206. break;
  207. }
  208. }
  209. }
  210. void DeclaredName::Print(llvm::raw_ostream& out) const {
  211. for (const auto& [loc, name] : qualifiers()) {
  212. out << name << ".";
  213. }
  214. out << inner_name();
  215. }
  216. auto GetName(const Declaration& declaration)
  217. -> std::optional<std::string_view> {
  218. switch (declaration.kind()) {
  219. case DeclarationKind::NamespaceDeclaration:
  220. return cast<NamespaceDeclaration>(declaration).name();
  221. case DeclarationKind::FunctionDeclaration:
  222. return cast<FunctionDeclaration>(declaration).name().inner_name();
  223. case DeclarationKind::DestructorDeclaration:
  224. return "destructor";
  225. case DeclarationKind::ClassDeclaration:
  226. return cast<ClassDeclaration>(declaration).name();
  227. case DeclarationKind::MixinDeclaration: {
  228. return cast<MixinDeclaration>(declaration).name();
  229. }
  230. case DeclarationKind::MixDeclaration: {
  231. return std::nullopt;
  232. }
  233. case DeclarationKind::ChoiceDeclaration:
  234. return cast<ChoiceDeclaration>(declaration).name();
  235. case DeclarationKind::InterfaceDeclaration:
  236. case DeclarationKind::ConstraintDeclaration:
  237. return cast<ConstraintTypeDeclaration>(declaration).name();
  238. case DeclarationKind::VariableDeclaration:
  239. return cast<VariableDeclaration>(declaration).binding().name();
  240. case DeclarationKind::AssociatedConstantDeclaration:
  241. return cast<AssociatedConstantDeclaration>(declaration).binding().name();
  242. case DeclarationKind::InterfaceExtendsDeclaration:
  243. case DeclarationKind::InterfaceImplDeclaration:
  244. case DeclarationKind::ImplDeclaration:
  245. case DeclarationKind::MatchFirstDeclaration:
  246. return std::nullopt;
  247. case DeclarationKind::SelfDeclaration:
  248. return SelfDeclaration::name();
  249. case DeclarationKind::AliasDeclaration: {
  250. return cast<AliasDeclaration>(declaration).name();
  251. }
  252. }
  253. }
  254. void GenericBinding::Print(llvm::raw_ostream& out) const {
  255. out << name() << ":! " << type();
  256. }
  257. void GenericBinding::PrintID(llvm::raw_ostream& out) const { out << name(); }
  258. void ReturnTerm::Print(llvm::raw_ostream& out) const {
  259. switch (kind_) {
  260. case ReturnKind::Omitted:
  261. return;
  262. case ReturnKind::Auto:
  263. out << "-> auto";
  264. return;
  265. case ReturnKind::Expression:
  266. CARBON_CHECK(type_expression_.has_value());
  267. out << "-> " << **type_expression_;
  268. return;
  269. }
  270. }
  271. namespace {
  272. // The deduced parameters of a function declaration.
  273. struct DeducedParameters {
  274. // The `self` parameter, if any.
  275. std::optional<Nonnull<Pattern*>> self_pattern;
  276. // All other deduced parameters.
  277. std::vector<Nonnull<GenericBinding*>> resolved_params;
  278. };
  279. // Split the `self` pattern (if any) out of `deduced_params`.
  280. auto SplitDeducedParameters(
  281. SourceLocation source_loc,
  282. const std::vector<Nonnull<AstNode*>>& deduced_params)
  283. -> ErrorOr<DeducedParameters> {
  284. DeducedParameters result;
  285. for (Nonnull<AstNode*> param : deduced_params) {
  286. switch (param->kind()) {
  287. case AstNodeKind::GenericBinding:
  288. result.resolved_params.push_back(&cast<GenericBinding>(*param));
  289. break;
  290. case AstNodeKind::BindingPattern: {
  291. Nonnull<BindingPattern*> binding = &cast<BindingPattern>(*param);
  292. if (binding->name() != "self") {
  293. return ProgramError(source_loc)
  294. << "illegal binding pattern in implicit parameter list";
  295. }
  296. if (result.self_pattern.has_value()) {
  297. return ProgramError(source_loc)
  298. << "parameter list cannot contain more than one `self` "
  299. "parameter";
  300. }
  301. result.self_pattern = binding;
  302. break;
  303. }
  304. case AstNodeKind::AddrPattern: {
  305. Nonnull<AddrPattern*> addr_pattern = &cast<AddrPattern>(*param);
  306. Nonnull<BindingPattern*> binding =
  307. &cast<BindingPattern>(addr_pattern->binding());
  308. if (binding->name() != "self") {
  309. return ProgramError(source_loc)
  310. << "illegal binding pattern in implicit parameter list";
  311. }
  312. if (result.self_pattern.has_value()) {
  313. return ProgramError(source_loc)
  314. << "parameter list cannot contain more than one `self` "
  315. "parameter";
  316. }
  317. result.self_pattern = addr_pattern;
  318. break;
  319. }
  320. default:
  321. return ProgramError(source_loc)
  322. << "illegal AST node in implicit parameter list";
  323. }
  324. }
  325. return result;
  326. }
  327. } // namespace
  328. auto DestructorDeclaration::CreateDestructor(
  329. Nonnull<Arena*> arena, SourceLocation source_loc,
  330. std::vector<Nonnull<AstNode*>> deduced_params,
  331. Nonnull<TuplePattern*> param_pattern, ReturnTerm return_term,
  332. std::optional<Nonnull<Block*>> body)
  333. -> ErrorOr<Nonnull<DestructorDeclaration*>> {
  334. DeducedParameters split_params;
  335. CARBON_ASSIGN_OR_RETURN(split_params,
  336. SplitDeducedParameters(source_loc, deduced_params));
  337. return arena->New<DestructorDeclaration>(
  338. source_loc, std::move(split_params.resolved_params),
  339. split_params.self_pattern, param_pattern, return_term, body);
  340. }
  341. auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
  342. SourceLocation source_loc, DeclaredName name,
  343. std::vector<Nonnull<AstNode*>> deduced_params,
  344. Nonnull<TuplePattern*> param_pattern,
  345. ReturnTerm return_term,
  346. std::optional<Nonnull<Block*>> body,
  347. VirtualOverride virt_override)
  348. -> ErrorOr<Nonnull<FunctionDeclaration*>> {
  349. DeducedParameters split_params;
  350. CARBON_ASSIGN_OR_RETURN(split_params,
  351. SplitDeducedParameters(source_loc, deduced_params));
  352. return arena->New<FunctionDeclaration>(
  353. source_loc, std::move(name), std::move(split_params.resolved_params),
  354. split_params.self_pattern, param_pattern, return_term, body,
  355. virt_override);
  356. }
  357. void CallableDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
  358. auto name = GetName(*this);
  359. CARBON_CHECK(name) << "Unexpected missing name for `" << *this << "`.";
  360. out << "fn " << *name << " ";
  361. if (!deduced_parameters_.empty()) {
  362. out << "[";
  363. llvm::ListSeparator sep;
  364. for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
  365. out << sep << *deduced;
  366. }
  367. out << "]";
  368. }
  369. out << *param_pattern_ << return_term_;
  370. if (body_) {
  371. out << " {\n";
  372. (*body_)->PrintDepth(depth, out);
  373. out << "\n}\n";
  374. } else {
  375. out << ";\n";
  376. }
  377. }
  378. auto ImplDeclaration::Create(Nonnull<Arena*> arena, SourceLocation source_loc,
  379. ImplKind kind, Nonnull<Expression*> impl_type,
  380. Nonnull<Expression*> interface,
  381. std::vector<Nonnull<AstNode*>> deduced_params,
  382. std::vector<Nonnull<Declaration*>> members)
  383. -> ErrorOr<Nonnull<ImplDeclaration*>> {
  384. std::vector<Nonnull<GenericBinding*>> resolved_params;
  385. for (Nonnull<AstNode*> param : deduced_params) {
  386. switch (param->kind()) {
  387. case AstNodeKind::GenericBinding:
  388. resolved_params.push_back(&cast<GenericBinding>(*param));
  389. break;
  390. default:
  391. return ProgramError(source_loc)
  392. << "illegal AST node in implicit parameter list of impl";
  393. }
  394. }
  395. Nonnull<SelfDeclaration*> self_decl =
  396. arena->New<SelfDeclaration>(impl_type->source_loc());
  397. return arena->New<ImplDeclaration>(source_loc, kind, impl_type, self_decl,
  398. interface, resolved_params, members);
  399. }
  400. void AlternativeSignature::Print(llvm::raw_ostream& out) const {
  401. out << "alt " << name() << " " << signature();
  402. }
  403. void AlternativeSignature::PrintID(llvm::raw_ostream& out) const {
  404. out << name();
  405. }
  406. } // namespace Carbon