|
41 | 41 | #include "swift/AST/NameLookup.h" |
42 | 42 | #include "swift/AST/PackConformance.h" |
43 | 43 | #include "swift/AST/ParameterList.h" |
44 | | -#include "swift/AST/PluginRegistry.h" |
| 44 | +#include "swift/AST/PluginLoader.h" |
45 | 45 | #include "swift/AST/PrettyStackTrace.h" |
46 | 46 | #include "swift/AST/PropertyWrappers.h" |
47 | 47 | #include "swift/AST/ProtocolConformance.h" |
|
67 | 67 | #include "llvm/ADT/Statistic.h" |
68 | 68 | #include "llvm/ADT/StringMap.h" |
69 | 69 | #include "llvm/ADT/StringSwitch.h" |
70 | | -#include "llvm/Config/config.h" |
71 | 70 | #include "llvm/IR/LLVMContext.h" |
72 | 71 | #include "llvm/Support/Allocator.h" |
73 | 72 | #include "llvm/Support/Compiler.h" |
@@ -526,17 +525,8 @@ struct ASTContext::Implementation { |
526 | 525 |
|
527 | 526 | llvm::StringMap<OptionSet<SearchPathKind>> SearchPathsSet; |
528 | 527 |
|
529 | | - /// Plugin registry. Lazily populated by get/setPluginRegistry(). |
530 | | - /// NOTE: Do not reference this directly. Use ASTContext::getPluginRegistry(). |
531 | | - PluginRegistry *Plugins = nullptr; |
532 | | - |
533 | | - /// `Plugins` storage if this ASTContext owns it. |
534 | | - std::unique_ptr<PluginRegistry> OwnedPluginRegistry = nullptr; |
535 | | - |
536 | | - /// Map a module name to an executable plugin path that provides the module. |
537 | | - llvm::DenseMap<Identifier, StringRef> ExecutablePluginPaths; |
538 | | - |
539 | | - llvm::StringSet<> LoadedPluginLibraryPaths; |
| 528 | + /// Plugin loader. |
| 529 | + std::unique_ptr<swift::PluginLoader> Plugins; |
540 | 530 |
|
541 | 531 | /// The permanent arena. |
542 | 532 | Arena Permanent; |
@@ -712,8 +702,6 @@ ASTContext::ASTContext( |
712 | 702 | registerAccessRequestFunctions(evaluator); |
713 | 703 | registerNameLookupRequestFunctions(evaluator); |
714 | 704 |
|
715 | | - createModuleToExecutablePluginMap(); |
716 | | - |
717 | 705 | // Provide a default OnDiskOutputBackend if user didn't supply one. |
718 | 706 | if (!OutputBackend) |
719 | 707 | OutputBackend = llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>(); |
@@ -6281,34 +6269,33 @@ BuiltinTupleType *ASTContext::getBuiltinTupleType() { |
6281 | 6269 | return result; |
6282 | 6270 | } |
6283 | 6271 |
|
6284 | | -void ASTContext::setPluginRegistry(PluginRegistry *newValue) { |
6285 | | - assert(getImpl().Plugins == nullptr && |
6286 | | - "Too late to set a new plugin registry"); |
6287 | | - getImpl().Plugins = newValue; |
| 6272 | +void ASTContext::setPluginLoader(std::unique_ptr<PluginLoader> loader) { |
| 6273 | + getImpl().Plugins = std::move(loader); |
6288 | 6274 | } |
6289 | 6275 |
|
6290 | | -PluginRegistry *ASTContext::getPluginRegistry() const { |
6291 | | - PluginRegistry *®istry = getImpl().Plugins; |
| 6276 | +PluginLoader &ASTContext::getPluginLoader() { return *getImpl().Plugins; } |
6292 | 6277 |
|
6293 | | - // Create a new one if it hasn't been set. |
6294 | | - if (!registry) { |
6295 | | - registry = new PluginRegistry(); |
6296 | | - getImpl().OwnedPluginRegistry.reset(registry); |
6297 | | - } |
| 6278 | +Optional<std::string> |
| 6279 | +ASTContext::lookupLibraryPluginByModuleName(Identifier moduleName) { |
| 6280 | + return getImpl().Plugins->lookupLibraryPluginByModuleName(moduleName); |
| 6281 | +} |
6298 | 6282 |
|
6299 | | - assert(registry != nullptr); |
6300 | | - return registry; |
| 6283 | +Optional<StringRef> |
| 6284 | +ASTContext::lookupExecutablePluginByModuleName(Identifier moduleName) { |
| 6285 | + return getImpl().Plugins->lookupExecutablePluginByModuleName(moduleName); |
6301 | 6286 | } |
6302 | 6287 |
|
6303 | | -void ASTContext::createModuleToExecutablePluginMap() { |
6304 | | - for (auto &arg : SearchPathOpts.getCompilerPluginExecutablePaths()) { |
6305 | | - // Create a moduleName -> pluginPath mapping. |
6306 | | - assert(!arg.ExecutablePath.empty() && "empty plugin path"); |
6307 | | - auto pathStr = AllocateCopy(arg.ExecutablePath); |
6308 | | - for (auto moduleName : arg.ModuleNames) { |
6309 | | - getImpl().ExecutablePluginPaths[getIdentifier(moduleName)] = pathStr; |
6310 | | - } |
6311 | | - } |
| 6288 | +Optional<std::pair<std::string, std::string>> |
| 6289 | +ASTContext::lookupExternalLibraryPluginByModuleName(Identifier moduleName) { |
| 6290 | + return getImpl().Plugins->lookupExternalLibraryPluginByModuleName(moduleName); |
| 6291 | +} |
| 6292 | + |
| 6293 | +LoadedLibraryPlugin *ASTContext::loadLibraryPlugin(StringRef path) { |
| 6294 | + return getImpl().Plugins->loadLibraryPlugin(path); |
| 6295 | +} |
| 6296 | + |
| 6297 | +LoadedExecutablePlugin *ASTContext::loadExecutablePlugin(StringRef path) { |
| 6298 | + return getImpl().Plugins->loadExecutablePlugin(path); |
6312 | 6299 | } |
6313 | 6300 |
|
6314 | 6301 | Type ASTContext::getNamedSwiftType(ModuleDecl *module, StringRef name) { |
@@ -6346,105 +6333,6 @@ Type ASTContext::getNamedSwiftType(ModuleDecl *module, StringRef name) { |
6346 | 6333 | return decl->getDeclaredInterfaceType(); |
6347 | 6334 | } |
6348 | 6335 |
|
6349 | | -Optional<std::string> |
6350 | | -ASTContext::lookupLibraryPluginByModuleName(Identifier moduleName) { |
6351 | | - auto fs = SourceMgr.getFileSystem(); |
6352 | | - |
6353 | | - // Look for 'lib${module name}(.dylib|.so)'. |
6354 | | - SmallString<64> expectedBasename; |
6355 | | - expectedBasename.append("lib"); |
6356 | | - expectedBasename.append(moduleName.str()); |
6357 | | - expectedBasename.append(LTDL_SHLIB_EXT); |
6358 | | - |
6359 | | - // Try '-plugin-path'. |
6360 | | - for (const auto &searchPath : SearchPathOpts.PluginSearchPaths) { |
6361 | | - SmallString<128> fullPath(searchPath); |
6362 | | - llvm::sys::path::append(fullPath, expectedBasename); |
6363 | | - if (fs->exists(fullPath)) { |
6364 | | - return std::string(fullPath); |
6365 | | - } |
6366 | | - } |
6367 | | - |
6368 | | - // Try '-load-plugin-library'. |
6369 | | - for (const auto &libPath : SearchPathOpts.getCompilerPluginLibraryPaths()) { |
6370 | | - if (llvm::sys::path::filename(libPath) == expectedBasename) { |
6371 | | - return libPath; |
6372 | | - } |
6373 | | - } |
6374 | | - |
6375 | | - return None; |
6376 | | -} |
6377 | | - |
6378 | | -Optional<StringRef> |
6379 | | -ASTContext::lookupExecutablePluginByModuleName(Identifier moduleName) { |
6380 | | - auto &execPluginPaths = getImpl().ExecutablePluginPaths; |
6381 | | - auto found = execPluginPaths.find(moduleName); |
6382 | | - if (found == execPluginPaths.end()) |
6383 | | - return None; |
6384 | | - return found->second; |
6385 | | -} |
6386 | | - |
6387 | | -Optional<std::pair<std::string, std::string>> |
6388 | | -ASTContext::lookupExternalLibraryPluginByModuleName(Identifier moduleName) { |
6389 | | - auto fs = this->SourceMgr.getFileSystem(); |
6390 | | - for (auto &pair : SearchPathOpts.ExternalPluginSearchPaths) { |
6391 | | - SmallString<128> fullPath(pair.SearchPath); |
6392 | | - llvm::sys::path::append(fullPath, "lib" + moduleName.str() + LTDL_SHLIB_EXT); |
6393 | | - |
6394 | | - if (fs->exists(fullPath)) { |
6395 | | - return {{std::string(fullPath), pair.ServerPath}}; |
6396 | | - } |
6397 | | - } |
6398 | | - return None; |
6399 | | -} |
6400 | | - |
6401 | | -LoadedExecutablePlugin *ASTContext::loadExecutablePlugin(StringRef path) { |
6402 | | - SmallString<128> resolvedPath; |
6403 | | - auto fs = this->SourceMgr.getFileSystem(); |
6404 | | - if (auto err = fs->getRealPath(path, resolvedPath)) { |
6405 | | - Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path, |
6406 | | - err.message()); |
6407 | | - return nullptr; |
6408 | | - } |
6409 | | - |
6410 | | - // Load the plugin. |
6411 | | - auto plugin = getPluginRegistry()->loadExecutablePlugin(resolvedPath); |
6412 | | - if (!plugin) { |
6413 | | - Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path, |
6414 | | - llvm::toString(plugin.takeError())); |
6415 | | - return nullptr; |
6416 | | - } |
6417 | | - |
6418 | | - return plugin.get(); |
6419 | | -} |
6420 | | - |
6421 | | -LoadedLibraryPlugin *ASTContext::loadLibraryPlugin(StringRef path) { |
6422 | | - // Remember the path (even if it fails to load.) |
6423 | | - getImpl().LoadedPluginLibraryPaths.insert(path); |
6424 | | - |
6425 | | - SmallString<128> resolvedPath; |
6426 | | - auto fs = this->SourceMgr.getFileSystem(); |
6427 | | - if (auto err = fs->getRealPath(path, resolvedPath)) { |
6428 | | - Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path, |
6429 | | - err.message()); |
6430 | | - return nullptr; |
6431 | | - } |
6432 | | - |
6433 | | - // Load the plugin. |
6434 | | - auto plugin = getPluginRegistry()->loadLibraryPlugin(resolvedPath); |
6435 | | - if (!plugin) { |
6436 | | - Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path, |
6437 | | - llvm::toString(plugin.takeError())); |
6438 | | - return nullptr; |
6439 | | - } |
6440 | | - |
6441 | | - return plugin.get(); |
6442 | | -} |
6443 | | - |
6444 | | -const llvm::StringSet<> &ASTContext::getLoadedPluginLibraryPaths() const { |
6445 | | - return getImpl().LoadedPluginLibraryPaths; |
6446 | | -} |
6447 | | - |
6448 | 6336 | bool ASTContext::supportsMoveOnlyTypes() const { |
6449 | 6337 | // currently the only thing holding back whether the types can appear is this. |
6450 | 6338 | return SILOpts.LexicalLifetimes != LexicalLifetimesOption::Off; |
|
0 commit comments