3232#include " swift/Demangling/ManglingMacros.h"
3333#include " swift/Parse/Lexer.h"
3434#include " swift/Subsystems.h"
35+ #include " llvm/Config/config.h"
36+
37+ #if defined(_WIN32)
38+ #define WIN32_LEAN_AND_MEAN
39+ #define NOMINMAX
40+ #include < windows.h>
41+ #else
42+ #include < dlfcn.h>
43+ #endif
3544
3645using namespace swift ;
3746
@@ -100,7 +109,9 @@ static std::string mangledNameForTypeMetadataAccessor(
100109#if SWIFT_SWIFT_PARSER
101110// / Look for macro's type metadata given its external module and type name.
102111static void const *lookupMacroTypeMetadataByExternalName (
103- ASTContext &ctx, StringRef moduleName, StringRef typeName) {
112+ ASTContext &ctx, StringRef moduleName, StringRef typeName,
113+ void *libraryHint = nullptr
114+ ) {
104115 // Look up the type metadata accessor as a struct, enum, or class.
105116 const Demangle::Node::Kind typeKinds[] = {
106117 Demangle::Node::Kind::Structure,
@@ -111,8 +122,8 @@ static void const *lookupMacroTypeMetadataByExternalName(
111122 void *accessorAddr = nullptr ;
112123 for (auto typeKind : typeKinds) {
113124 auto symbolName = mangledNameForTypeMetadataAccessor (
114- moduleName, typeName, typeKind);
115- accessorAddr = ctx.getAddressOfSymbol (symbolName.c_str ());
125+ moduleName, typeName, typeKind);
126+ accessorAddr = ctx.getAddressOfSymbol (symbolName.c_str (), libraryHint );
116127 if (accessorAddr)
117128 break ;
118129 }
@@ -289,20 +300,43 @@ MacroDefinition MacroDefinitionRequest::evaluate(
289300 return MacroDefinition::forExternal (*moduleName, *typeName);
290301}
291302
292- ExternalMacroDefinition
293- ExternalMacroDefinitionRequest::evaluate (
294- Evaluator &evaluator, ASTContext *ctx,
295- Identifier moduleName, Identifier typeName
303+ // / Load a plugin library based on a module name.
304+ static void *loadPluginByName (StringRef searchPath, StringRef moduleName) {
305+ SmallString<128 > fullPath (searchPath);
306+ llvm::sys::path::append (fullPath, " lib" + moduleName + LTDL_SHLIB_EXT);
307+ #if defined(_WIN32)
308+ return LoadLibraryA (fullPath.c_str ());
309+ #else
310+ return dlopen (fullPath.c_str (), RTLD_LAZY);
311+ #endif
312+ }
313+
314+ void *CompilerPluginLoadRequest::evaluate (
315+ Evaluator &evaluator, ASTContext *ctx, Identifier moduleName
296316) const {
317+ auto &searchPathOpts = ctx->SearchPathOpts ;
318+ for (const auto &path : searchPathOpts.PluginSearchPaths ) {
319+ if (auto found = loadPluginByName (path, moduleName.str ()))
320+ return found;
321+ }
322+
323+ return nullptr ;
324+ }
325+
326+ static Optional<ExternalMacroDefinition>
327+ resolveInProcessMacro (
328+ ASTContext &ctx, Identifier moduleName, Identifier typeName,
329+ void *libraryHint = nullptr
330+ ) {
297331#if SWIFT_SWIFT_PARSER
298332 // / Look for the type metadata given the external module and type names.
299333 auto macroMetatype = lookupMacroTypeMetadataByExternalName (
300- * ctx, moduleName.str (), typeName.str ());
334+ ctx, moduleName.str (), typeName.str (), libraryHint );
301335 if (macroMetatype) {
302336 // Check whether the macro metatype is in-process.
303337 if (auto inProcess = swift_ASTGen_resolveMacroType (macroMetatype)) {
304338 // Make sure we clean up after the macro.
305- ctx-> addCleanup ([inProcess]() {
339+ ctx. addCleanup ([inProcess]() {
306340 swift_ASTGen_destroyMacro (inProcess);
307341 });
308342
@@ -311,6 +345,28 @@ ExternalMacroDefinitionRequest::evaluate(
311345 }
312346#endif
313347
348+ return None;
349+ }
350+
351+ ExternalMacroDefinition
352+ ExternalMacroDefinitionRequest::evaluate (
353+ Evaluator &evaluator, ASTContext *ctx,
354+ Identifier moduleName, Identifier typeName
355+ ) const {
356+ // Try to load a plugin module from the plugin search paths. If it
357+ // succeeds, resolve in-process from that plugin
358+ CompilerPluginLoadRequest loadRequest{ctx, moduleName};
359+ if (auto loadedLibrary = evaluateOrDefault (
360+ evaluator, loadRequest, nullptr )) {
361+ if (auto inProcess = resolveInProcessMacro (
362+ *ctx, moduleName, typeName, loadedLibrary))
363+ return *inProcess;
364+ }
365+
366+ // Try to resolve in-process.
367+ if (auto inProcess = resolveInProcessMacro (*ctx, moduleName, typeName))
368+ return *inProcess;
369+
314370 return ExternalMacroDefinition{nullptr };
315371}
316372
0 commit comments