declaration.cpp 13 KB

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