declaration.cpp 13 KB

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