@@ -531,9 +531,6 @@ struct ASTContext::Implementation {
531531 // / `Plugins` storage if this ASTContext owns it.
532532 std::unique_ptr<PluginRegistry> OwnedPluginRegistry = nullptr ;
533533
534- // / Cache of loaded symbols.
535- llvm::StringMap<void *> LoadedSymbols;
536-
537534 // / Map a module name to an executable plugin path that provides the module.
538535 llvm::DenseMap<Identifier, StringRef> ExecutablePluginPaths;
539536
@@ -707,8 +704,7 @@ ASTContext::ASTContext(
707704 registerAccessRequestFunctions (evaluator);
708705 registerNameLookupRequestFunctions (evaluator);
709706
710- // FIXME: Delay this so the client e.g. SourceKit can inject plugin registry.
711- loadCompilerPlugins ();
707+ createModuleToExecutablePluginMap ();
712708}
713709
714710ASTContext::~ASTContext () {
@@ -6264,66 +6260,17 @@ PluginRegistry *ASTContext::getPluginRegistry() const {
62646260 return registry;
62656261}
62666262
6267- void ASTContext::loadCompilerPlugins () {
6268- auto fs = this ->SourceMgr .getFileSystem ();
6269- for (auto &path : SearchPathOpts.getCompilerPluginLibraryPaths ()) {
6270- SmallString<128 > resolvedPath;
6271- if (auto err = fs->getRealPath (path, resolvedPath)) {
6272- Diags.diagnose (SourceLoc (), diag::compiler_plugin_not_loaded, path,
6273- err.message ());
6274- continue ;
6275- }
6276- auto loaded = getPluginRegistry ()->loadLibraryPlugin (resolvedPath);
6277- if (!loaded) {
6278- Diags.diagnose (SourceLoc (), diag::compiler_plugin_not_loaded, path,
6279- llvm::toString (loaded.takeError ()));
6280- }
6281- }
6282-
6263+ void ASTContext::createModuleToExecutablePluginMap () {
62836264 for (auto &arg : SearchPathOpts.getCompilerPluginExecutablePaths ()) {
6284- // 'arg' is '<path to executable>#<module names>' where the module names are
6285- // comma separated.
6286-
62876265 // Create a moduleName -> pluginPath mapping.
6288- StringRef path;
6289- StringRef modulesStr;
6290- std::tie (path, modulesStr) = StringRef (arg).rsplit (' #' );
6291- SmallVector<StringRef, 1 > modules;
6292- modulesStr.split (modules, ' ,' );
6293-
6294- if (modules.empty () || path.empty ()) {
6295- // TODO: Error messsage.
6296- Diags.diagnose (SourceLoc (), diag::compiler_plugin_not_loaded, arg, " " );
6297- }
6298- auto pathStr = AllocateCopy (path);
6299- for (auto moduleName : modules) {
6266+ assert (!arg.ExecutablePath .empty () && " empty plugin path" );
6267+ auto pathStr = AllocateCopy (arg.ExecutablePath );
6268+ for (auto moduleName : arg.ModuleNames ) {
63006269 getImpl ().ExecutablePluginPaths [getIdentifier (moduleName)] = pathStr;
63016270 }
63026271 }
63036272}
63046273
6305- void *ASTContext::getAddressOfSymbol (const char *name,
6306- void *libraryHandleHint) {
6307- auto lookup = getImpl ().LoadedSymbols .try_emplace (name, nullptr );
6308- void *&address = lookup.first ->getValue ();
6309- #if !defined(_WIN32)
6310- if (lookup.second ) {
6311- auto *handle = libraryHandleHint ? libraryHandleHint : RTLD_DEFAULT;
6312- address = dlsym (handle, name);
6313-
6314- // If we didn't know where to look, look specifically in each plugin.
6315- if (!address && !libraryHandleHint) {
6316- for (const auto &plugin : getPluginRegistry ()->getLoadedLibraryPlugins ()) {
6317- address = dlsym (plugin.second , name);
6318- if (address)
6319- break ;
6320- }
6321- }
6322- }
6323- #endif
6324- return address;
6325- }
6326-
63276274Type ASTContext::getNamedSwiftType (ModuleDecl *module , StringRef name) {
63286275 if (!module )
63296276 return Type ();
@@ -6359,6 +6306,35 @@ Type ASTContext::getNamedSwiftType(ModuleDecl *module, StringRef name) {
63596306 return decl->getDeclaredInterfaceType ();
63606307}
63616308
6309+ Optional<std::string>
6310+ ASTContext::lookupLibraryPluginByModuleName (Identifier moduleName) {
6311+ auto fs = SourceMgr.getFileSystem ();
6312+
6313+ // Look for 'lib${module name}(.dylib|.so)'.
6314+ SmallString<64 > expectedBasename;
6315+ expectedBasename.append (" lib" );
6316+ expectedBasename.append (moduleName.str ());
6317+ expectedBasename.append (LTDL_SHLIB_EXT);
6318+
6319+ // Try '-plugin-path'.
6320+ for (const auto &searchPath : SearchPathOpts.PluginSearchPaths ) {
6321+ SmallString<128 > fullPath (searchPath);
6322+ llvm::sys::path::append (fullPath, expectedBasename);
6323+ if (fs->exists (fullPath)) {
6324+ return std::string (fullPath);
6325+ }
6326+ }
6327+
6328+ // Try '-load-plugin-library'.
6329+ for (const auto &libPath : SearchPathOpts.getCompilerPluginLibraryPaths ()) {
6330+ if (llvm::sys::path::filename (libPath) == expectedBasename) {
6331+ return libPath;
6332+ }
6333+ }
6334+
6335+ return None;
6336+ }
6337+
63626338Optional<StringRef>
63636339ASTContext::lookupExecutablePluginByModuleName (Identifier moduleName) {
63646340 auto &execPluginPaths = getImpl ().ExecutablePluginPaths ;
@@ -6401,6 +6377,25 @@ LoadedExecutablePlugin *ASTContext::loadExecutablePlugin(StringRef path) {
64016377 return plugin.get ();
64026378}
64036379
6380+ void *ASTContext::loadLibraryPlugin (StringRef path) {
6381+ SmallString<128 > resolvedPath;
6382+ auto fs = this ->SourceMgr .getFileSystem ();
6383+ if (auto err = fs->getRealPath (path, resolvedPath)) {
6384+ Diags.diagnose (SourceLoc (), diag::compiler_plugin_not_loaded, path,
6385+ err.message ());
6386+ return nullptr ;
6387+ }
6388+
6389+ // Load the plugin.
6390+ auto plugin = getPluginRegistry ()->loadLibraryPlugin (resolvedPath);
6391+ if (!plugin) {
6392+ Diags.diagnose (SourceLoc (), diag::compiler_plugin_not_loaded, path,
6393+ llvm::toString (plugin.takeError ()));
6394+ }
6395+
6396+ return plugin.get ();
6397+ }
6398+
64046399bool ASTContext::supportsMoveOnlyTypes () const {
64056400 // currently the only thing holding back whether the types can appear is this.
64066401 return SILOpts.LexicalLifetimes != LexicalLifetimesOption::Off;
0 commit comments