24
24
#include " swift/AST/ASTContext.h"
25
25
#include " swift/AST/Decl.h"
26
26
#include " swift/AST/GenericSignature.h"
27
+ #include " swift/AST/GenericSignatureBuilder.h"
27
28
#include " swift/AST/Module.h"
28
29
#include " swift/AST/NameLookup.h"
29
30
#include " swift/AST/Type.h"
@@ -424,6 +425,55 @@ ASTBuilder::getForeignModuleKind(const Demangle::NodePointer &node) {
424
425
.Default (None);
425
426
}
426
427
428
+ CanGenericSignature ASTBuilder::demangleGenericSignature (
429
+ NominalTypeDecl *nominalDecl,
430
+ const Demangle::NodePointer &node) {
431
+ GenericSignatureBuilder builder (Ctx);
432
+ builder.addGenericSignature (nominalDecl->getGenericSignature ());
433
+
434
+ for (auto &child : *node) {
435
+ if (child->getKind () ==
436
+ Demangle::Node::Kind::DependentGenericParamCount)
437
+ continue ;
438
+
439
+ if (child->getNumChildren () != 2 )
440
+ return CanGenericSignature ();
441
+ auto subjectType = swift::Demangle::decodeMangledType (
442
+ *this , child->getChild (0 ));
443
+ auto constraintType = swift::Demangle::decodeMangledType (
444
+ *this , child->getChild (1 ));
445
+ if (!subjectType || !constraintType)
446
+ return CanGenericSignature ();
447
+
448
+ auto source =
449
+ GenericSignatureBuilder::FloatingRequirementSource::forAbstract ();
450
+
451
+ switch (child->getKind ()) {
452
+ case Demangle::Node::Kind::DependentGenericConformanceRequirement: {
453
+ builder.addRequirement (
454
+ Requirement (constraintType->isExistentialType ()
455
+ ? RequirementKind::Conformance
456
+ : RequirementKind::Superclass,
457
+ subjectType, constraintType),
458
+ source, nullptr );
459
+ break ;
460
+ }
461
+ case Demangle::Node::Kind::DependentGenericSameTypeRequirement: {
462
+ builder.addRequirement (
463
+ Requirement (RequirementKind::SameType,
464
+ subjectType, constraintType),
465
+ source, nullptr );
466
+ break ;
467
+ }
468
+ default :
469
+ return CanGenericSignature ();
470
+ }
471
+ }
472
+
473
+ return std::move (builder).computeGenericSignature (SourceLoc ())
474
+ ->getCanonicalSignature ();
475
+ }
476
+
427
477
DeclContext *
428
478
ASTBuilder::findDeclContext (const Demangle::NodePointer &node) {
429
479
switch (node->getKind ()) {
@@ -500,9 +550,41 @@ ASTBuilder::findDeclContext(const Demangle::NodePointer &node) {
500
550
case Demangle::Node::Kind::Global:
501
551
return findDeclContext (node->getChild (0 ));
502
552
553
+ case Demangle::Node::Kind::Extension: {
554
+ auto *moduleDecl = dyn_cast_or_null<ModuleDecl>(
555
+ findDeclContext (node->getChild (0 )));
556
+ if (!moduleDecl)
557
+ return nullptr ;
558
+
559
+ auto *nominalDecl = dyn_cast_or_null<NominalTypeDecl>(
560
+ findDeclContext (node->getChild (1 )));
561
+ if (!nominalDecl)
562
+ return nullptr ;
563
+
564
+ CanGenericSignature genericSig;
565
+ if (node->getNumChildren () > 2 )
566
+ genericSig = demangleGenericSignature (nominalDecl, node->getChild (2 ));
567
+
568
+ for (auto *ext : nominalDecl->getExtensions ()) {
569
+ if (ext->getParentModule () != moduleDecl)
570
+ continue ;
571
+
572
+ if (!ext->isConstrainedExtension ()) {
573
+ if (!genericSig)
574
+ return ext;
575
+ continue ;
576
+ }
577
+
578
+ if (ext->getGenericSignature ()->getCanonicalSignature ()
579
+ == genericSig) {
580
+ return ext;
581
+ }
582
+ }
583
+
584
+ return nullptr ;
585
+ }
586
+
503
587
// Bail out on other kinds of contexts.
504
- // TODO: extensions
505
- // TODO: local contexts
506
588
default :
507
589
return nullptr ;
508
590
}
@@ -515,6 +597,12 @@ ASTBuilder::findNominalTypeDecl(DeclContext *dc,
515
597
Demangle::Node::Kind kind) {
516
598
auto module = dc->getParentModule ();
517
599
600
+ // When looking into an extension, look into the nominal instead; the
601
+ // important thing is that the module, obtained above, is the module
602
+ // containing the extension and not the module containing the nominal
603
+ if (isa<ExtensionDecl>(dc))
604
+ dc = dc->getSelfNominalTypeDecl ();
605
+
518
606
SmallVector<ValueDecl *, 4 > lookupResults;
519
607
module ->lookupMember (lookupResults, dc, name, privateDiscriminator);
520
608
0 commit comments