@@ -61,21 +61,27 @@ static bool isGlobalOrStaticVar(VarDecl *VD) {
61
61
return VD->isStatic () || VD->getDeclContext ()->isModuleScopeContext ();
62
62
}
63
63
64
+ TBDGenVisitor::TBDGenVisitor (TBDGenDescriptor desc,
65
+ SymbolCallbackFn symbolCallback)
66
+ : TBDGenVisitor(desc.getTarget(), desc.getDataLayout(),
67
+ desc.getParentModule(), desc.getOptions(),
68
+ symbolCallback) {}
69
+
64
70
void TBDGenVisitor::addSymbolInternal (StringRef name,
65
71
llvm::MachO::SymbolKind kind,
66
72
bool isLinkerDirective) {
67
73
if (!isLinkerDirective && Opts.LinkerDirectivesOnly )
68
74
return ;
69
- Symbols.addSymbol (kind, name, Targets);
70
- if (kind == SymbolKind::GlobalSymbol) {
71
- StringSymbols.push_back (name);
75
+
72
76
#ifndef NDEBUG
77
+ if (kind == SymbolKind::GlobalSymbol) {
73
78
if (!DuplicateSymbolChecker.insert (name).second ) {
74
79
llvm::dbgs () << " TBDGen duplicate symbol: " << name << ' \n ' ;
75
80
assert (false && " TBDGen symbol appears twice" );
76
81
}
77
- #endif
78
82
}
83
+ #endif
84
+ SymbolCallback (name, kind);
79
85
}
80
86
81
87
static std::vector<OriginallyDefinedInAttr::ActiveVersion>
@@ -1053,6 +1059,63 @@ void TBDGenVisitor::visit(Decl *D) {
1053
1059
ASTVisitor::visit (D);
1054
1060
}
1055
1061
1062
+ static bool hasLinkerDirective (Decl *D) {
1063
+ return !getAllMovedPlatformVersions (D).empty ();
1064
+ }
1065
+
1066
+ void TBDGenVisitor::visitFile (FileUnit *file) {
1067
+ if (file == SwiftModule->getFiles ()[0 ])
1068
+ addFirstFileSymbols ();
1069
+
1070
+ SmallVector<Decl *, 16 > decls;
1071
+ file->getTopLevelDecls (decls);
1072
+
1073
+ addMainIfNecessary (file);
1074
+
1075
+ for (auto d : decls) {
1076
+ if (Opts.LinkerDirectivesOnly && !hasLinkerDirective (d))
1077
+ continue ;
1078
+ visit (d);
1079
+ }
1080
+ }
1081
+
1082
+ void TBDGenVisitor::visit (const TBDGenDescriptor &desc) {
1083
+ if (auto *singleFile = desc.getSingleFile ()) {
1084
+ assert (SwiftModule == singleFile->getParentModule () &&
1085
+ " mismatched file and module" );
1086
+ visitFile (singleFile);
1087
+
1088
+ // Visit synthesized file, if it exists.
1089
+ if (auto *SF = dyn_cast<SourceFile>(singleFile)) {
1090
+ if (auto *synthesizedFile = SF->getSynthesizedFile ())
1091
+ visitFile (synthesizedFile);
1092
+ }
1093
+ return ;
1094
+ }
1095
+
1096
+ llvm::SmallVector<ModuleDecl*, 4 > Modules;
1097
+ Modules.push_back (SwiftModule);
1098
+
1099
+ auto &ctx = SwiftModule->getASTContext ();
1100
+ for (auto Name: Opts.embedSymbolsFromModules ) {
1101
+ if (auto *MD = ctx.getModuleByName (Name)) {
1102
+ // If it is a clang module, the symbols should be collected by TAPI.
1103
+ if (!MD->isNonSwiftModule ()) {
1104
+ Modules.push_back (MD);
1105
+ continue ;
1106
+ }
1107
+ }
1108
+ // Diagnose module name that cannot be found
1109
+ ctx.Diags .diagnose (SourceLoc (), diag::unknown_swift_module_name, Name);
1110
+ }
1111
+ // Collect symbols in each module.
1112
+ llvm::for_each (Modules, [&](ModuleDecl *M) {
1113
+ for (auto *file : M->getFiles ()) {
1114
+ visitFile (file);
1115
+ }
1116
+ });
1117
+ }
1118
+
1056
1119
// / The kind of version being parsed, used for diagnostics.
1057
1120
// / Note: Must match the order in DiagnosticsFrontend.def
1058
1121
enum DylibVersionKind_t: unsigned {
@@ -1100,25 +1163,15 @@ static bool isApplicationExtensionSafe(const LangOptions &LangOpts) {
1100
1163
llvm::sys::Process::GetEnv (" LD_APPLICATION_EXTENSION_SAFE" );
1101
1164
}
1102
1165
1103
- static bool hasLinkerDirective (Decl *D) {
1104
- return !getAllMovedPlatformVersions (D).empty ();
1105
- }
1106
-
1107
- TBDFileAndSymbols
1108
- GenerateTBDRequest::evaluate (Evaluator &evaluator,
1109
- TBDGenDescriptor desc) const {
1166
+ TBDFile GenerateTBDRequest::evaluate (Evaluator &evaluator,
1167
+ TBDGenDescriptor desc) const {
1110
1168
auto *M = desc.getParentModule ();
1111
1169
auto &opts = desc.getOptions ();
1112
-
1113
1170
auto &ctx = M->getASTContext ();
1114
- const auto &triple = ctx.LangOpts .Target ;
1115
- UniversalLinkageInfo linkInfo (triple, opts.HasMultipleIGMs ,
1116
- /* forcePublicDecls*/ false );
1117
1171
1118
1172
llvm::MachO::InterfaceFile file;
1119
1173
file.setFileType (llvm::MachO::FileType::TBD_V4);
1120
- file.setApplicationExtensionSafe (
1121
- isApplicationExtensionSafe (M->getASTContext ().LangOpts ));
1174
+ file.setApplicationExtensionSafe (isApplicationExtensionSafe (ctx.LangOpts ));
1122
1175
file.setInstallName (opts.InstallName );
1123
1176
file.setTwoLevelNamespace ();
1124
1177
file.setSwiftABIVersion (irgen::getSwiftABIVersion ());
@@ -1134,75 +1187,47 @@ GenerateTBDRequest::evaluate(Evaluator &evaluator,
1134
1187
file.setCompatibilityVersion (*packed);
1135
1188
}
1136
1189
1137
- llvm::MachO::Target target (triple );
1190
+ llvm::MachO::Target target (ctx. LangOpts . Target );
1138
1191
file.addTarget (target);
1139
1192
// Add target variant
1140
1193
if (ctx.LangOpts .TargetVariant .hasValue ()) {
1141
1194
llvm::MachO::Target targetVar (*ctx.LangOpts .TargetVariant );
1142
1195
file.addTarget (targetVar);
1143
1196
}
1144
- auto *clang = static_cast <ClangImporter *>(ctx.getClangModuleLoader ());
1145
- TBDGenVisitor visitor (file, {target}, clang->getTargetInfo ().getDataLayout (),
1146
- linkInfo, M, opts);
1147
1197
1148
- auto visitFile = [&](FileUnit *file) {
1149
- if (file == M->getFiles ()[0 ]) {
1150
- visitor.addFirstFileSymbols ();
1151
- }
1152
-
1153
- SmallVector<Decl *, 16 > decls;
1154
- file->getTopLevelDecls (decls);
1198
+ llvm::MachO::TargetList targets{target};
1199
+ auto addSymbol = [&](StringRef symbol, SymbolKind kind) {
1200
+ file.addSymbol (kind, symbol, targets);
1201
+ };
1155
1202
1156
- visitor.addMainIfNecessary (file);
1203
+ TBDGenVisitor visitor (desc, addSymbol);
1204
+ visitor.visit (desc);
1205
+ return file;
1206
+ }
1157
1207
1158
- for (auto d : decls) {
1159
- if (opts.LinkerDirectivesOnly && !hasLinkerDirective (d))
1160
- continue ;
1161
- visitor.visit (d);
1162
- }
1208
+ std::vector<std::string>
1209
+ PublicSymbolsRequest::evaluate (Evaluator &evaluator,
1210
+ TBDGenDescriptor desc) const {
1211
+ std::vector<std::string> symbols;
1212
+ auto addSymbol = [&](StringRef symbol, SymbolKind kind) {
1213
+ if (kind == SymbolKind::GlobalSymbol)
1214
+ symbols.push_back (symbol.str ());
1163
1215
};
1164
1216
1165
- if (auto *singleFile = desc.getSingleFile ()) {
1166
- assert (M == singleFile->getParentModule () && " mismatched file and module" );
1167
- visitFile (singleFile);
1168
- // Visit synthesized file, if it exists.
1169
- if (auto *SF = dyn_cast<SourceFile>(singleFile))
1170
- if (auto *synthesizedFile = SF->getSynthesizedFile ())
1171
- visitFile (synthesizedFile);
1172
- } else {
1173
- llvm::SmallVector<ModuleDecl*, 4 > Modules;
1174
- Modules.push_back (M);
1175
- for (auto Name: opts.embedSymbolsFromModules ) {
1176
- if (auto *MD = ctx.getModuleByName (Name)) {
1177
- // If it is a clang module, the symbols should be collected by TAPI.
1178
- if (!MD->isNonSwiftModule ()) {
1179
- Modules.push_back (MD);
1180
- continue ;
1181
- }
1182
- }
1183
- // Diagnose module name that cannot be found
1184
- ctx.Diags .diagnose (SourceLoc (), diag::unknown_swift_module_name, Name);
1185
- }
1186
- // Collect symbols in each module.
1187
- llvm::for_each (Modules, [&](ModuleDecl *M) {
1188
- for (auto *file : M->getFiles ()) {
1189
- visitFile (file);
1190
- }
1191
- });
1192
- }
1193
-
1194
- return std::make_pair (std::move (file), std::move (visitor.StringSymbols ));
1217
+ TBDGenVisitor visitor (desc, addSymbol);
1218
+ visitor.visit (desc);
1219
+ return symbols;
1195
1220
}
1196
1221
1197
1222
std::vector<std::string> swift::getPublicSymbols (TBDGenDescriptor desc) {
1198
1223
auto &evaluator = desc.getParentModule ()->getASTContext ().evaluator ;
1199
- return llvm::cantFail (evaluator (GenerateTBDRequest {desc})). second ;
1224
+ return llvm::cantFail (evaluator (PublicSymbolsRequest {desc}));
1200
1225
}
1201
1226
void swift::writeTBDFile (ModuleDecl *M, llvm::raw_ostream &os,
1202
1227
const TBDGenOptions &opts) {
1203
1228
auto &evaluator = M->getASTContext ().evaluator ;
1204
1229
auto desc = TBDGenDescriptor::forModule (M, opts);
1205
- auto file = llvm::cantFail (evaluator (GenerateTBDRequest{desc})). first ;
1230
+ auto file = llvm::cantFail (evaluator (GenerateTBDRequest{desc}));
1206
1231
llvm::cantFail (llvm::MachO::TextAPIWriter::writeToStream (os, file),
1207
1232
" YAML writing should be error-free" );
1208
1233
}
0 commit comments