@@ -286,10 +286,9 @@ MacroDefinition MacroDefinitionRequest::evaluate(
286
286
}
287
287
288
288
// / Load a plugin library based on a module name.
289
- static void *loadPluginByName (StringRef searchPath,
290
- StringRef moduleName,
291
- llvm::vfs::FileSystem &fs,
292
- PluginRegistry *registry) {
289
+ static void *loadLibraryPluginByName (StringRef searchPath, StringRef moduleName,
290
+ llvm::vfs::FileSystem &fs,
291
+ PluginRegistry *registry) {
293
292
SmallString<128 > fullPath (searchPath);
294
293
llvm::sys::path::append (fullPath, " lib" + moduleName + LTDL_SHLIB_EXT);
295
294
if (fs.getRealPath (fullPath, fullPath))
@@ -298,53 +297,12 @@ static void *loadPluginByName(StringRef searchPath,
298
297
return loadResult ? *loadResult : nullptr ;
299
298
}
300
299
301
- void *CompilerPluginLoadRequest::evaluate (
302
- Evaluator &evaluator, ASTContext *ctx, Identifier moduleName
303
- ) const {
304
- auto fs = ctx->SourceMgr .getFileSystem ();
305
- auto &searchPathOpts = ctx->SearchPathOpts ;
306
- auto *registry = ctx->getPluginRegistry ();
307
- for (const auto &path : searchPathOpts.PluginSearchPaths ) {
308
- if (auto found = loadPluginByName (path, moduleName.str (), *fs, registry))
309
- return found;
310
- }
311
-
312
- return nullptr ;
313
- }
314
-
315
- static Optional<ExternalMacroDefinition>
316
- resolveInProcessMacro (
317
- ASTContext &ctx, Identifier moduleName, Identifier typeName,
318
- void *libraryHint = nullptr
319
- ) {
320
- #if SWIFT_SWIFT_PARSER
321
- // / Look for the type metadata given the external module and type names.
322
- auto macroMetatype = lookupMacroTypeMetadataByExternalName (
323
- ctx, moduleName.str (), typeName.str (), libraryHint);
324
- if (macroMetatype) {
325
- // Check whether the macro metatype is in-process.
326
- if (auto inProcess = swift_ASTGen_resolveMacroType (macroMetatype)) {
327
- // Make sure we clean up after the macro.
328
- ctx.addCleanup ([inProcess]() {
329
- swift_ASTGen_destroyMacro (inProcess);
330
- });
331
-
332
- return ExternalMacroDefinition{
333
- ExternalMacroDefinition::PluginKind::InProcess, inProcess};
334
- }
335
- }
336
- #endif
337
- return None;
338
- }
339
-
340
- static Optional<ExternalMacroDefinition>
341
- resolveExecutableMacro (ASTContext &ctx, Identifier moduleName,
342
- Identifier typeName) {
343
- #if SWIFT_SWIFT_PARSER
344
- std::string executablePluginPath;
300
+ static LoadedExecutablePlugin *
301
+ loadExecutablePluginByName (ASTContext &ctx, Identifier moduleName) {
302
+ // Find an executable plugin.
345
303
std::string libraryPath;
304
+ std::string executablePluginPath;
346
305
347
- // Find macros in executable plugins.
348
306
if (auto found = ctx.lookupExternalLibraryPluginByModuleName (moduleName)) {
349
307
// Found in '-external-plugin-path'.
350
308
std::tie (libraryPath, executablePluginPath) = found.value ();
@@ -353,38 +311,97 @@ resolveExecutableMacro(ASTContext &ctx, Identifier moduleName,
353
311
executablePluginPath = found->str ();
354
312
}
355
313
if (executablePluginPath.empty ())
356
- return None ;
314
+ return nullptr ;
357
315
358
316
// Launch the plugin.
359
317
LoadedExecutablePlugin *executablePlugin =
360
318
ctx.loadExecutablePlugin (executablePluginPath);
361
319
if (!executablePlugin)
362
- return None ;
320
+ return nullptr ;
363
321
364
322
// FIXME: Ideally this should be done right after invoking the plugin.
365
323
// But plugin loading is in libAST and it can't link ASTGen symbols.
366
324
if (!executablePlugin->isInitialized ()) {
325
+ #if SWIFT_SWIFT_PARSER
367
326
swift_ASTGen_initializePlugin (executablePlugin);
368
327
executablePlugin->setCleanup ([executablePlugin] {
369
328
swift_ASTGen_deinitializePlugin (executablePlugin);
370
329
});
330
+ #endif
371
331
}
372
332
373
- // If this is a plugin server. Load the library in that process before
374
- // resolving the macro.
333
+ // If this is a plugin server, load the library.
375
334
if (!libraryPath.empty ()) {
335
+ #if SWIFT_SWIFT_PARSER
376
336
llvm::SmallString<128 > resolvedLibraryPath;
377
337
auto fs = ctx.SourceMgr .getFileSystem ();
378
338
if (fs->getRealPath (libraryPath, resolvedLibraryPath)) {
379
- return None ;
339
+ return nullptr ;
380
340
}
381
341
bool loaded = swift_ASTGen_pluginServerLoadLibraryPlugin (
382
342
executablePlugin, resolvedLibraryPath.c_str (), moduleName.str ().data (),
383
343
&ctx.Diags );
384
344
if (!loaded)
385
- return None;
345
+ return nullptr ;
346
+ #endif
347
+ }
348
+
349
+ return executablePlugin;
350
+ }
351
+
352
+ LoadedCompilerPlugin
353
+ CompilerPluginLoadRequest::evaluate (Evaluator &evaluator, ASTContext *ctx,
354
+ Identifier moduleName) const {
355
+ auto fs = ctx->SourceMgr .getFileSystem ();
356
+ auto &searchPathOpts = ctx->SearchPathOpts ;
357
+ auto *registry = ctx->getPluginRegistry ();
358
+
359
+ // First, check '-plugin-path' paths.
360
+ for (const auto &path : searchPathOpts.PluginSearchPaths ) {
361
+ if (auto found =
362
+ loadLibraryPluginByName (path, moduleName.str (), *fs, registry))
363
+ return LoadedCompilerPlugin::inProcess (found);
364
+ }
365
+
366
+ // Fall back to executable plugins.
367
+ // i.e. '-external-plugin-path', and '-load-plugin-executable'.
368
+ if (auto *found = loadExecutablePluginByName (*ctx, moduleName)) {
369
+ return LoadedCompilerPlugin::executable (found);
386
370
}
387
371
372
+ return nullptr ;
373
+ }
374
+
375
+ static Optional<ExternalMacroDefinition>
376
+ resolveInProcessMacro (
377
+ ASTContext &ctx, Identifier moduleName, Identifier typeName,
378
+ void *libraryHint = nullptr
379
+ ) {
380
+ #if SWIFT_SWIFT_PARSER
381
+ // / Look for the type metadata given the external module and type names.
382
+ auto macroMetatype = lookupMacroTypeMetadataByExternalName (
383
+ ctx, moduleName.str (), typeName.str (), libraryHint);
384
+ if (macroMetatype) {
385
+ // Check whether the macro metatype is in-process.
386
+ if (auto inProcess = swift_ASTGen_resolveMacroType (macroMetatype)) {
387
+ // Make sure we clean up after the macro.
388
+ ctx.addCleanup ([inProcess]() {
389
+ swift_ASTGen_destroyMacro (inProcess);
390
+ });
391
+
392
+ return ExternalMacroDefinition{
393
+ ExternalMacroDefinition::PluginKind::InProcess, inProcess};
394
+ }
395
+ }
396
+ #endif
397
+ return None;
398
+ }
399
+
400
+ static Optional<ExternalMacroDefinition>
401
+ resolveExecutableMacro (ASTContext &ctx,
402
+ LoadedExecutablePlugin *executablePlugin,
403
+ Identifier moduleName, Identifier typeName) {
404
+ #if SWIFT_SWIFT_PARSER
388
405
if (auto *execMacro = swift_ASTGen_resolveExecutableMacro (
389
406
moduleName.str ().data (), moduleName.str ().size (),
390
407
typeName.str ().data (), typeName.str ().size (), executablePlugin)) {
@@ -395,7 +412,6 @@ resolveExecutableMacro(ASTContext &ctx, Identifier moduleName,
395
412
ExternalMacroDefinition::PluginKind::Executable, execMacro};
396
413
}
397
414
#endif
398
-
399
415
return None;
400
416
}
401
417
@@ -406,8 +422,9 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
406
422
// Try to load a plugin module from the plugin search paths. If it
407
423
// succeeds, resolve in-process from that plugin
408
424
CompilerPluginLoadRequest loadRequest{ctx, moduleName};
409
- if (auto loadedLibrary = evaluateOrDefault (
410
- evaluator, loadRequest, nullptr )) {
425
+ LoadedCompilerPlugin loaded =
426
+ evaluateOrDefault (evaluator, loadRequest, nullptr );
427
+ if (auto loadedLibrary = loaded.getAsInProcessPlugin ()) {
411
428
if (auto inProcess = resolveInProcessMacro (
412
429
*ctx, moduleName, typeName, loadedLibrary))
413
430
return *inProcess;
@@ -418,9 +435,11 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
418
435
return *inProcess;
419
436
420
437
// Try executable plugins.
421
- if (auto executableMacro =
422
- resolveExecutableMacro (*ctx, moduleName, typeName)) {
423
- return executableMacro;
438
+ if (auto *executablePlugin = loaded.getAsExecutablePlugin ()) {
439
+ if (auto executableMacro = resolveExecutableMacro (*ctx, executablePlugin,
440
+ moduleName, typeName)) {
441
+ return executableMacro;
442
+ }
424
443
}
425
444
426
445
return None;
0 commit comments