|
28 | 28 | #include "swift/SIL/TypeLowering.h"
|
29 | 29 | #include "llvm/ADT/StringSet.h"
|
30 | 30 |
|
| 31 | +#include "TBDGenVisitor.h" |
| 32 | + |
31 | 33 | using namespace swift;
|
32 | 34 | using namespace swift::irgen;
|
| 35 | +using namespace swift::tbdgen; |
33 | 36 | using StringSet = llvm::StringSet<>;
|
34 | 37 |
|
35 | 38 | static bool isPrivateDecl(ValueDecl *VD) {
|
36 | 39 | return getDeclLinkage(VD) != FormalLinkage::PublicUnique;
|
37 | 40 | }
|
38 | 41 |
|
39 |
| -namespace { |
40 |
| -class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> { |
41 |
| - StringSet &Symbols; |
42 |
| - const UniversalLinkageInfo &UniversalLinkInfo; |
43 |
| - ModuleDecl *SwiftModule; |
44 |
| - bool FileHasEntryPoint; |
45 |
| - bool SILSerializeWitnessTables; |
46 |
| - |
47 |
| - bool InsideAbstractStorageDecl = false; |
48 |
| - |
49 |
| - void addSymbol(StringRef name) { |
50 |
| - auto isNewValue = Symbols.insert(name).second; |
51 |
| - (void)isNewValue; |
52 |
| - assert(isNewValue && "already inserted"); |
53 |
| - } |
54 |
| - |
55 |
| - void addSymbol(SILDeclRef declRef); |
56 |
| - |
57 |
| - void addSymbol(LinkEntity entity) { |
58 |
| - auto linkage = |
59 |
| - LinkInfo::get(UniversalLinkInfo, SwiftModule, entity, ForDefinition); |
60 |
| - |
61 |
| - auto externallyVisible = |
62 |
| - llvm::GlobalValue::isExternalLinkage(linkage.getLinkage()) && |
63 |
| - linkage.getVisibility() != llvm::GlobalValue::HiddenVisibility; |
64 |
| - |
65 |
| - if (externallyVisible) |
66 |
| - addSymbol(linkage.getName()); |
67 |
| - } |
68 |
| - |
69 |
| - void addConformances(DeclContext *DC); |
70 |
| - |
71 |
| -public: |
72 |
| - TBDGenVisitor(StringSet &symbols, |
73 |
| - const UniversalLinkageInfo &universalLinkInfo, |
74 |
| - ModuleDecl *swiftModule, bool fileHasEntryPoint, |
75 |
| - bool silSerializeWitnessTables) |
76 |
| - : Symbols(symbols), UniversalLinkInfo(universalLinkInfo), |
77 |
| - SwiftModule(swiftModule), FileHasEntryPoint(fileHasEntryPoint), |
78 |
| - SILSerializeWitnessTables(silSerializeWitnessTables) {} |
79 |
| - |
80 |
| - void visitMembers(Decl *D) { |
81 |
| - SmallVector<Decl *, 4> members; |
82 |
| - auto addMembers = [&](DeclRange range) { |
83 |
| - for (auto member : range) |
84 |
| - members.push_back(member); |
85 |
| - }; |
86 |
| - if (auto ED = dyn_cast<ExtensionDecl>(D)) |
87 |
| - addMembers(ED->getMembers()); |
88 |
| - else if (auto NTD = dyn_cast<NominalTypeDecl>(D)) |
89 |
| - addMembers(NTD->getMembers()); |
90 |
| - else if (auto ASD = dyn_cast<AbstractStorageDecl>(D)) |
91 |
| - ASD->getAllAccessorFunctions(members); |
92 |
| - |
93 |
| - for (auto member : members) { |
94 |
| - ASTVisitor::visit(member); |
95 |
| - } |
96 |
| - } |
97 |
| - |
98 |
| - void visitPatternBindingDecl(PatternBindingDecl *PBD); |
99 |
| - |
100 |
| - void visitValueDecl(ValueDecl *VD); |
101 |
| - |
102 |
| - void visitAbstractFunctionDecl(AbstractFunctionDecl *AFD); |
103 |
| - |
104 |
| - void visitTypeAliasDecl(TypeAliasDecl *TAD) { |
105 |
| - // any information here is encoded elsewhere |
106 |
| - } |
107 |
| - |
108 |
| - void visitNominalTypeDecl(NominalTypeDecl *NTD); |
109 |
| - |
110 |
| - void visitClassDecl(ClassDecl *CD); |
111 |
| - |
112 |
| - void visitConstructorDecl(ConstructorDecl *CD); |
113 |
| - |
114 |
| - void visitExtensionDecl(ExtensionDecl *ED); |
115 |
| - |
116 |
| - void visitProtocolDecl(ProtocolDecl *PD); |
117 |
| - |
118 |
| - void visitAbstractStorageDecl(AbstractStorageDecl *ASD); |
119 |
| - |
120 |
| - void visitVarDecl(VarDecl *VD); |
121 |
| - |
122 |
| - void visitDecl(Decl *D) { visitMembers(D); } |
123 |
| -}; |
124 |
| -} // end anonymous namespace |
125 |
| - |
126 | 42 | static bool isGlobalOrStaticVar(VarDecl *VD) {
|
127 | 43 | return VD->isStatic() || VD->getDeclContext()->isModuleScopeContext();
|
128 | 44 | }
|
@@ -410,23 +326,70 @@ void TBDGenVisitor::visitProtocolDecl(ProtocolDecl *PD) {
|
410 | 326 | #endif
|
411 | 327 | }
|
412 | 328 |
|
| 329 | +static void enumeratePublicSymbolsAndWrite(ModuleDecl *M, FileUnit *singleFile, |
| 330 | + StringSet &symbols, |
| 331 | + bool hasMultipleIRGenThreads, |
| 332 | + bool silSerializeWitnessTables, |
| 333 | + llvm::raw_ostream *os, |
| 334 | + StringRef installName) { |
| 335 | + auto isWholeModule = singleFile == nullptr; |
| 336 | + const auto &target = M->getASTContext().LangOpts.Target; |
| 337 | + UniversalLinkageInfo linkInfo(target, hasMultipleIRGenThreads, isWholeModule); |
| 338 | + |
| 339 | + TBDGenVisitor visitor(symbols, target, linkInfo, M, silSerializeWitnessTables, |
| 340 | + installName); |
| 341 | + |
| 342 | + auto visitFile = [&](FileUnit *file) { |
| 343 | + SmallVector<Decl *, 16> decls; |
| 344 | + file->getTopLevelDecls(decls); |
| 345 | + |
| 346 | + visitor.setFileHasEntryPoint(file->hasEntryPoint()); |
| 347 | + |
| 348 | + for (auto d : decls) |
| 349 | + visitor.visit(d); |
| 350 | + }; |
| 351 | + |
| 352 | + if (singleFile) { |
| 353 | + assert(M == singleFile->getParentModule() && "mismatched file and module"); |
| 354 | + visitFile(singleFile); |
| 355 | + } else { |
| 356 | + for (auto *file : M->getFiles()) { |
| 357 | + visitFile(file); |
| 358 | + } |
| 359 | + } |
| 360 | + |
| 361 | + if (os) { |
| 362 | + // The correct TBD formatting code is temporarily non-open source, so this |
| 363 | + // is just a list of the symbols. |
| 364 | + std::vector<StringRef> sorted; |
| 365 | + for (auto &symbol : symbols) |
| 366 | + sorted.push_back(symbol.getKey()); |
| 367 | + std::sort(sorted.begin(), sorted.end()); |
| 368 | + for (const auto &symbol : sorted) { |
| 369 | + *os << symbol << "\n"; |
| 370 | + } |
| 371 | + } |
| 372 | +} |
| 373 | + |
413 | 374 | void swift::enumeratePublicSymbols(FileUnit *file, StringSet &symbols,
|
414 | 375 | bool hasMultipleIRGenThreads,
|
415 |
| - bool isWholeModule, |
416 | 376 | bool silSerializeWitnessTables) {
|
417 |
| - UniversalLinkageInfo linkInfo(file->getASTContext().LangOpts.Target, |
418 |
| - hasMultipleIRGenThreads, isWholeModule); |
419 |
| - |
420 |
| - SmallVector<Decl *, 16> decls; |
421 |
| - file->getTopLevelDecls(decls); |
422 |
| - |
423 |
| - auto hasEntryPoint = file->hasEntryPoint(); |
424 |
| - |
425 |
| - TBDGenVisitor visitor(symbols, linkInfo, file->getParentModule(), |
426 |
| - hasEntryPoint, silSerializeWitnessTables); |
427 |
| - for (auto d : decls) |
428 |
| - visitor.visit(d); |
429 |
| - |
430 |
| - if (hasEntryPoint) |
431 |
| - symbols.insert("main"); |
| 377 | + enumeratePublicSymbolsAndWrite( |
| 378 | + file->getParentModule(), file, symbols, hasMultipleIRGenThreads, |
| 379 | + silSerializeWitnessTables, nullptr, StringRef()); |
| 380 | +} |
| 381 | +void swift::enumeratePublicSymbols(ModuleDecl *M, StringSet &symbols, |
| 382 | + bool hasMultipleIRGenThreads, |
| 383 | + bool silSerializeWitnessTables) { |
| 384 | + enumeratePublicSymbolsAndWrite(M, nullptr, symbols, hasMultipleIRGenThreads, |
| 385 | + silSerializeWitnessTables, nullptr, |
| 386 | + StringRef()); |
| 387 | +} |
| 388 | +void swift::writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os, |
| 389 | + bool hasMultipleIRGenThreads, |
| 390 | + bool silSerializeWitnessTables, |
| 391 | + StringRef installName) { |
| 392 | + StringSet symbols; |
| 393 | + enumeratePublicSymbolsAndWrite(M, nullptr, symbols, hasMultipleIRGenThreads, |
| 394 | + silSerializeWitnessTables, &os, installName); |
432 | 395 | }
|
0 commit comments