@@ -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,13 +311,13 @@ 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.
@@ -370,21 +328,76 @@ resolveExecutableMacro(ASTContext &ctx, Identifier moduleName,
370
328
});
371
329
}
372
330
373
- // If this is a plugin server. Load the library in that process before
374
- // resolving the macro.
331
+ // If this is a plugin server, load the library.
375
332
if (!libraryPath.empty ()) {
376
333
llvm::SmallString<128 > resolvedLibraryPath;
377
334
auto fs = ctx.SourceMgr .getFileSystem ();
378
335
if (fs->getRealPath (libraryPath, resolvedLibraryPath)) {
379
- return None ;
336
+ return nullptr ;
380
337
}
381
338
bool loaded = swift_ASTGen_pluginServerLoadLibraryPlugin (
382
339
executablePlugin, resolvedLibraryPath.c_str (), moduleName.str ().data (),
383
340
&ctx.Diags );
384
341
if (!loaded)
385
- return None;
342
+ return nullptr ;
343
+ }
344
+
345
+ return executablePlugin;
346
+ }
347
+
348
+ LoadedCompilerPlugin
349
+ CompilerPluginLoadRequest::evaluate (Evaluator &evaluator, ASTContext *ctx,
350
+ Identifier moduleName) const {
351
+ auto fs = ctx->SourceMgr .getFileSystem ();
352
+ auto &searchPathOpts = ctx->SearchPathOpts ;
353
+ auto *registry = ctx->getPluginRegistry ();
354
+
355
+ // First, check '-plugin-path' paths.
356
+ for (const auto &path : searchPathOpts.PluginSearchPaths ) {
357
+ if (auto found =
358
+ loadLibraryPluginByName (path, moduleName.str (), *fs, registry))
359
+ return LoadedCompilerPlugin::inProcess (found);
386
360
}
387
361
362
+ // Fall back to executable plugins.
363
+ // i.e. '-external-plugin-path', and '-load-plugin-executable'.
364
+ if (auto *found = loadExecutablePluginByName (*ctx, moduleName)) {
365
+ return LoadedCompilerPlugin::executable (found);
366
+ }
367
+
368
+ return nullptr ;
369
+ }
370
+
371
+ static Optional<ExternalMacroDefinition>
372
+ resolveInProcessMacro (
373
+ ASTContext &ctx, Identifier moduleName, Identifier typeName,
374
+ void *libraryHint = nullptr
375
+ ) {
376
+ #if SWIFT_SWIFT_PARSER
377
+ // / Look for the type metadata given the external module and type names.
378
+ auto macroMetatype = lookupMacroTypeMetadataByExternalName (
379
+ ctx, moduleName.str (), typeName.str (), libraryHint);
380
+ if (macroMetatype) {
381
+ // Check whether the macro metatype is in-process.
382
+ if (auto inProcess = swift_ASTGen_resolveMacroType (macroMetatype)) {
383
+ // Make sure we clean up after the macro.
384
+ ctx.addCleanup ([inProcess]() {
385
+ swift_ASTGen_destroyMacro (inProcess);
386
+ });
387
+
388
+ return ExternalMacroDefinition{
389
+ ExternalMacroDefinition::PluginKind::InProcess, inProcess};
390
+ }
391
+ }
392
+ #endif
393
+ return None;
394
+ }
395
+
396
+ static Optional<ExternalMacroDefinition>
397
+ resolveExecutableMacro (ASTContext &ctx,
398
+ LoadedExecutablePlugin *executablePlugin,
399
+ Identifier moduleName, Identifier typeName) {
400
+ #if SWIFT_SWIFT_PARSER
388
401
if (auto *execMacro = swift_ASTGen_resolveExecutableMacro (
389
402
moduleName.str ().data (), moduleName.str ().size (),
390
403
typeName.str ().data (), typeName.str ().size (), executablePlugin)) {
@@ -395,7 +408,6 @@ resolveExecutableMacro(ASTContext &ctx, Identifier moduleName,
395
408
ExternalMacroDefinition::PluginKind::Executable, execMacro};
396
409
}
397
410
#endif
398
-
399
411
return None;
400
412
}
401
413
@@ -406,8 +418,9 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
406
418
// Try to load a plugin module from the plugin search paths. If it
407
419
// succeeds, resolve in-process from that plugin
408
420
CompilerPluginLoadRequest loadRequest{ctx, moduleName};
409
- if (auto loadedLibrary = evaluateOrDefault (
410
- evaluator, loadRequest, nullptr )) {
421
+ LoadedCompilerPlugin loaded =
422
+ evaluateOrDefault (evaluator, loadRequest, nullptr );
423
+ if (auto loadedLibrary = loaded.getAsInProcessPlugin ()) {
411
424
if (auto inProcess = resolveInProcessMacro (
412
425
*ctx, moduleName, typeName, loadedLibrary))
413
426
return *inProcess;
@@ -418,9 +431,11 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
418
431
return *inProcess;
419
432
420
433
// Try executable plugins.
421
- if (auto executableMacro =
422
- resolveExecutableMacro (*ctx, moduleName, typeName)) {
423
- return executableMacro;
434
+ if (auto *executablePlugin = loaded.getAsExecutablePlugin ()) {
435
+ if (auto executableMacro = resolveExecutableMacro (*ctx, executablePlugin,
436
+ moduleName, typeName)) {
437
+ return executableMacro;
438
+ }
424
439
}
425
440
426
441
return None;
0 commit comments