2424#include " swift/Parse/Lexer.h"
2525#include " swift/Subsystems.h"
2626#include " llvm/Config/config.h"
27- #include " llvm/Support/DynamicLibrary.h"
2827#include < cstdlib>
2928
29+ #if !defined(_WIN32)
30+ #include < dlfcn.h>
31+ #endif
32+
3033using namespace swift ;
3134
3235#define ALLMACROS_PROPERTY_NAME " allMacros"
@@ -56,8 +59,7 @@ void swift_ASTGen_getMacroTypes(const void *getter,
5659#endif
5760
5861static const void *
59- getMacroRegistrationPropertyGetter (llvm::sys::DynamicLibrary library,
60- StringRef moduleName,
62+ getMacroRegistrationPropertyGetter (void *library, StringRef moduleName,
6163 ASTContext &ctx) {
6264 assert (!moduleName.empty ());
6365 // TODO: Consider using runtime lookup to get all types that conform to the
@@ -147,23 +149,28 @@ getMacroRegistrationPropertyGetter(llvm::sys::DynamicLibrary library,
147149 assert (mangleResult.isSuccess ());
148150 name = mangleResult.result ();
149151 }
150- return ctx.getAddressOfSymbol (name, & library);
152+ return ctx.getAddressOfSymbol (name. c_str (), library);
151153}
152154
153155void ASTContext::loadCompilerPlugins () {
154- for (StringRef path : SearchPathOpts.getCompilerPluginLibraryPaths ()) {
155- std::string errorMsg;
156- auto lib = llvm::sys::DynamicLibrary::getPermanentLibrary (
157- path.data (), &errorMsg);
158- if (!lib.isValid ()) {
156+ for (auto &path : SearchPathOpts.getCompilerPluginLibraryPaths ()) {
157+ void *lib = nullptr ;
158+ #if !defined(_WIN32)
159+ lib = dlopen (path.c_str (), RTLD_LAZY|RTLD_LOCAL);
160+ #endif
161+ if (!lib) {
162+ const char *errorMsg = " Unsupported platform" ;
163+ #if !defined(_WIN32)
164+ errorMsg = dlerror ();
165+ #endif
159166 Diags.diagnose (SourceLoc (), diag::compiler_plugin_not_loaded, path,
160167 errorMsg);
161168 continue ;
162169 }
163170 auto moduleName = llvm::sys::path::filename (path);
164- #if !defined(_WIN32)
171+ #if !defined(_WIN32)
165172 moduleName.consume_front (" lib" );
166- #endif
173+ #endif
167174 moduleName.consume_back (LTDL_SHLIB_EXT);
168175 auto *getter = getMacroRegistrationPropertyGetter (lib, moduleName, *this );
169176 if (!getter) {
@@ -175,7 +182,7 @@ void ASTContext::loadCompilerPlugins() {
175182 // Note: We don't currently have a way to poke at the contents of a Swift
176183 // array `[Any.Type]` from C++. But this should not be an issue for release
177184 // toolchains where user-defined macros will be used.
178- #if SWIFT_SWIFT_PARSER
185+ #if SWIFT_SWIFT_PARSER
179186 const void *const *metatypesAddress;
180187 ptrdiff_t metatypeCount;
181188 swift_ASTGen_getMacroTypes (getter, &metatypesAddress, &metatypeCount);
@@ -186,7 +193,7 @@ void ASTContext::loadCompilerPlugins() {
186193 LoadedPlugins.try_emplace (name, std::move (plugin));
187194 }
188195 free (const_cast <void *>((const void *)metatypes.data ()));
189- #endif
196+ #endif // SWIFT_SWIFT_PARSER
190197 }
191198}
192199
@@ -197,8 +204,7 @@ using WitnessTableLookupFn = const void *(const void *type,
197204extern " C" WitnessTableLookupFn swift_conformsToProtocol;
198205#endif
199206
200- CompilerPlugin::CompilerPlugin (const void *metadata,
201- llvm::sys::DynamicLibrary parentLibrary,
207+ CompilerPlugin::CompilerPlugin (const void *metadata, void *parentLibrary,
202208 ASTContext &ctx)
203209 : metadata(metadata), parentLibrary(parentLibrary)
204210{
@@ -208,6 +214,8 @@ CompilerPlugin::CompilerPlugin(const void *metadata,
208214#endif
209215 void *protocolDescriptor =
210216 ctx.getAddressOfSymbol (COMPILER_PLUGIN_PROTOCOL_DESCRIPTOR);
217+ assert (swift_conformsToProtocol);
218+ assert (protocolDescriptor);
211219 witnessTable = swift_conformsToProtocol (metadata, protocolDescriptor);
212220 assert (witnessTable && " Type does not conform to _CompilerPlugin" );
213221 auto returnedName = invokeName ();
@@ -216,6 +224,12 @@ CompilerPlugin::CompilerPlugin(const void *metadata,
216224 kind = invokeKind ();
217225}
218226
227+ CompilerPlugin::~CompilerPlugin () {
228+ #if !defined(_WIN32)
229+ dlclose (parentLibrary);
230+ #endif
231+ }
232+
219233namespace {
220234struct CharBuffer {
221235 const char *data;
@@ -280,3 +294,28 @@ CompilerPlugin::invokeRewrite(StringRef targetModuleName,
280294 llvm_unreachable (" Incompatible host compiler" );
281295#endif
282296}
297+
298+ Optional<StringRef>
299+ CompilerPlugin::invokeGenericSignature () const {
300+ #if __clang__
301+ using Method = SWIFT_CC CharBuffer (
302+ SWIFT_CONTEXT const void *, const void *, const void *);
303+ auto method = getWitnessMethodUnsafe<Method>(
304+ WitnessTableEntry::GenericSignature);
305+ return method (metadata, metadata, witnessTable).str ();
306+ #else
307+ llvm_unreachable (" Incompatible host compiler" );
308+ #endif
309+ }
310+
311+ StringRef CompilerPlugin::invokeTypeSignature () const {
312+ #if __clang__
313+ using Method = SWIFT_CC CharBuffer (
314+ SWIFT_CONTEXT const void *, const void *, const void *);
315+ auto method = getWitnessMethodUnsafe<Method>(
316+ WitnessTableEntry::TypeSignature);
317+ return method (metadata, metadata, witnessTable).str ();
318+ #else
319+ llvm_unreachable (" Incompatible host compiler" );
320+ #endif
321+ }
0 commit comments