@@ -53,14 +53,21 @@ class NominalTypeConformanceCollector : public ASTWalker {
53
53
}
54
54
55
55
PreWalkAction walkToDeclPre (Decl *D) override {
56
- if (auto *NTD = llvm::dyn_cast<NominalTypeDecl>(D))
57
- if (!isa<ProtocolDecl>(NTD))
56
+ auto *NTD = llvm::dyn_cast<NominalTypeDecl>(D);
57
+ if (!NTD)
58
+ if (auto *ETD = dyn_cast<ExtensionDecl>(D))
59
+ NTD = ETD->getExtendedNominal ();
60
+ if (NTD)
61
+ if (!isa<ProtocolDecl>(NTD) && CheckedDecls.insert (NTD).second )
58
62
for (auto &Protocol : NTD->getAllProtocols ())
59
63
if (Protocol->getAttrs ().hasAttribute <ExtractConstantsFromMembersAttr>() ||
60
64
Protocols.count (Protocol->getName ().str ().str ()) != 0 )
61
65
ConformanceTypeDecls.push_back (NTD);
62
66
return Action::Continue ();
63
67
}
68
+
69
+ private:
70
+ std::unordered_set<NominalTypeDecl *> CheckedDecls;
64
71
};
65
72
66
73
std::string toFullyQualifiedTypeNameString (const swift::Type &Type) {
@@ -463,37 +470,52 @@ extractEnumCases(NominalTypeDecl *Decl) {
463
470
return llvm::None;
464
471
}
465
472
466
- ConstValueTypeInfo
467
- ConstantValueInfoRequest::evaluate (Evaluator &Evaluator,
468
- NominalTypeDecl *Decl) const {
469
- // Use 'getStoredProperties' to get lowered lazy and wrapped properties
470
- auto StoredProperties = Decl->getStoredProperties ();
471
- std::unordered_set<VarDecl *> StoredPropertiesSet (StoredProperties.begin (),
472
- StoredProperties.end ());
473
+ ConstValueTypeInfo ConstantValueInfoRequest::evaluate (
474
+ Evaluator &Evaluator, NominalTypeDecl *Decl,
475
+ llvm::PointerUnion<const SourceFile *, ModuleDecl *> extractionScope)
476
+ const {
477
+
478
+ auto shouldExtract = [&](DeclContext *decl) {
479
+ if (auto SF = extractionScope.dyn_cast <const SourceFile *>())
480
+ return decl->getOutermostParentSourceFile () == SF;
481
+ return decl->getParentModule () == extractionScope.get <ModuleDecl *>();
482
+ };
473
483
474
484
std::vector<ConstValueTypePropertyInfo> Properties;
475
- for (auto Property : StoredProperties) {
476
- Properties.push_back (extractTypePropertyInfo (Property));
477
- }
485
+ llvm::Optional<std::vector<EnumElementDeclValue>> EnumCases;
478
486
479
- for (auto Member : Decl->getMembers ()) {
480
- auto *VD = dyn_cast<VarDecl>(Member);
481
- // Ignore plain stored properties collected above,
482
- // instead gather up remaining static and computed properties.
483
- if (!VD || StoredPropertiesSet.count (VD))
484
- continue ;
485
- Properties.push_back (extractTypePropertyInfo (VD));
487
+ if (shouldExtract (Decl)) {
488
+ // Use 'getStoredProperties' to get lowered lazy and wrapped properties
489
+ auto StoredProperties = Decl->getStoredProperties ();
490
+ std::unordered_set<VarDecl *> StoredPropertiesSet (StoredProperties.begin (),
491
+ StoredProperties.end ());
492
+ for (auto Property : StoredProperties) {
493
+ Properties.push_back (extractTypePropertyInfo (Property));
494
+ }
495
+
496
+ for (auto Member : Decl->getMembers ()) {
497
+ auto *VD = dyn_cast<VarDecl>(Member);
498
+ // Ignore plain stored properties collected above,
499
+ // instead gather up remaining static and computed properties.
500
+ if (!VD || StoredPropertiesSet.count (VD))
501
+ continue ;
502
+ Properties.push_back (extractTypePropertyInfo (VD));
503
+ }
504
+
505
+ EnumCases = extractEnumCases (Decl);
486
506
}
487
507
488
508
for (auto Extension: Decl->getExtensions ()) {
489
- for (auto Member : Extension->getMembers ()) {
490
- if (auto *VD = dyn_cast<VarDecl>(Member)) {
491
- Properties.push_back (extractTypePropertyInfo (VD));
509
+ if (shouldExtract (Extension)) {
510
+ for (auto Member : Extension->getMembers ()) {
511
+ if (auto *VD = dyn_cast<VarDecl>(Member)) {
512
+ Properties.push_back (extractTypePropertyInfo (VD));
513
+ }
492
514
}
493
515
}
494
516
}
495
517
496
- return ConstValueTypeInfo{Decl, Properties, extractEnumCases (Decl) };
518
+ return ConstValueTypeInfo{Decl, Properties, EnumCases };
497
519
}
498
520
499
521
std::vector<ConstValueTypeInfo>
@@ -507,7 +529,8 @@ gatherConstValuesForModule(const std::unordered_set<std::string> &Protocols,
507
529
Module->walk (ConformanceCollector);
508
530
for (auto *CD : ConformanceDecls)
509
531
Result.emplace_back (evaluateOrDefault (CD->getASTContext ().evaluator ,
510
- ConstantValueInfoRequest{CD}, {}));
532
+ ConstantValueInfoRequest{CD, Module},
533
+ {}));
511
534
return Result;
512
535
}
513
536
@@ -523,8 +546,8 @@ gatherConstValuesForPrimary(const std::unordered_set<std::string> &Protocols,
523
546
D->walk (ConformanceCollector);
524
547
525
548
for (auto *CD : ConformanceDecls)
526
- Result.emplace_back (evaluateOrDefault (CD-> getASTContext (). evaluator ,
527
- ConstantValueInfoRequest{CD}, {}));
549
+ Result.emplace_back (evaluateOrDefault (
550
+ CD-> getASTContext (). evaluator , ConstantValueInfoRequest{CD, SF }, {}));
528
551
return Result;
529
552
}
530
553
0 commit comments