declaration.cpp 16 KB

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