@@ -286,10 +286,9 @@ MacroDefinition MacroDefinitionRequest::evaluate(
286286}
287287
288288// / 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) {
293292 SmallString<128 > fullPath (searchPath);
294293 llvm::sys::path::append (fullPath, " lib" + moduleName + LTDL_SHLIB_EXT);
295294 if (fs.getRealPath (fullPath, fullPath))
@@ -298,53 +297,12 @@ static void *loadPluginByName(StringRef searchPath,
298297 return loadResult ? *loadResult : nullptr ;
299298}
300299
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.
345303 std::string libraryPath;
304+ std::string executablePluginPath;
346305
347- // Find macros in executable plugins.
348306 if (auto found = ctx.lookupExternalLibraryPluginByModuleName (moduleName)) {
349307 // Found in '-external-plugin-path'.
350308 std::tie (libraryPath, executablePluginPath) = found.value ();
@@ -353,38 +311,97 @@ resolveExecutableMacro(ASTContext &ctx, Identifier moduleName,
353311 executablePluginPath = found->str ();
354312 }
355313 if (executablePluginPath.empty ())
356- return None ;
314+ return nullptr ;
357315
358316 // Launch the plugin.
359317 LoadedExecutablePlugin *executablePlugin =
360318 ctx.loadExecutablePlugin (executablePluginPath);
361319 if (!executablePlugin)
362- return None ;
320+ return nullptr ;
363321
364322 // FIXME: Ideally this should be done right after invoking the plugin.
365323 // But plugin loading is in libAST and it can't link ASTGen symbols.
366324 if (!executablePlugin->isInitialized ()) {
325+ #if SWIFT_SWIFT_PARSER
367326 swift_ASTGen_initializePlugin (executablePlugin);
368327 executablePlugin->setCleanup ([executablePlugin] {
369328 swift_ASTGen_deinitializePlugin (executablePlugin);
370329 });
330+ #endif
371331 }
372332
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.
375334 if (!libraryPath.empty ()) {
335+ #if SWIFT_SWIFT_PARSER
376336 llvm::SmallString<128 > resolvedLibraryPath;
377337 auto fs = ctx.SourceMgr .getFileSystem ();
378338 if (fs->getRealPath (libraryPath, resolvedLibraryPath)) {
379- return None ;
339+ return nullptr ;
380340 }
381341 bool loaded = swift_ASTGen_pluginServerLoadLibraryPlugin (
382342 executablePlugin, resolvedLibraryPath.c_str (), moduleName.str ().data (),
383343 &ctx.Diags );
384344 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);
386370 }
387371
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
388405 if (auto *execMacro = swift_ASTGen_resolveExecutableMacro (
389406 moduleName.str ().data (), moduleName.str ().size (),
390407 typeName.str ().data (), typeName.str ().size (), executablePlugin)) {
@@ -395,7 +412,6 @@ resolveExecutableMacro(ASTContext &ctx, Identifier moduleName,
395412 ExternalMacroDefinition::PluginKind::Executable, execMacro};
396413 }
397414#endif
398-
399415 return None;
400416}
401417
@@ -406,8 +422,9 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
406422 // Try to load a plugin module from the plugin search paths. If it
407423 // succeeds, resolve in-process from that plugin
408424 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 ()) {
411428 if (auto inProcess = resolveInProcessMacro (
412429 *ctx, moduleName, typeName, loadedLibrary))
413430 return *inProcess;
@@ -418,9 +435,11 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
418435 return *inProcess;
419436
420437 // 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+ }
424443 }
425444
426445 return None;
0 commit comments