|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#if swift(>=6.0) |
| 14 | +public import SwiftSyntaxMacros |
| 15 | +private import _SwiftSyntaxCShims |
| 16 | +#else |
| 17 | +import SwiftSyntaxMacros |
| 18 | +@_implementationOnly import _SwiftSyntaxCShims |
| 19 | +#endif |
| 20 | + |
| 21 | +/// Singleton 'PluginProvider' that can serve shared library plugins. |
| 22 | +@_spi(PluginMessage) |
| 23 | +public class LibraryPluginProvider: PluginProvider { |
| 24 | + struct LoadedLibraryPlugin { |
| 25 | + var libraryPath: String |
| 26 | + var handle: UnsafeMutableRawPointer |
| 27 | + } |
| 28 | + |
| 29 | + struct MacroRef: Hashable { |
| 30 | + var moduleName: String |
| 31 | + var typeName: String |
| 32 | + } |
| 33 | + |
| 34 | + /// Loaded dynamic link library handles associated with the module name. |
| 35 | + var loadedLibraryPlugins: [String: LoadedLibraryPlugin] = [:] |
| 36 | + |
| 37 | + /// Resolved macros cache. |
| 38 | + var resolvedMacros: [MacroRef: Macro.Type] = [:] |
| 39 | + |
| 40 | + private init() {} |
| 41 | + |
| 42 | + /// Singleton. |
| 43 | + @MainActor |
| 44 | + public static let shared: LibraryPluginProvider = LibraryPluginProvider() |
| 45 | + |
| 46 | + public var features: [PluginFeature] { |
| 47 | + [.loadPluginLibrary] |
| 48 | + } |
| 49 | + |
| 50 | + public func loadPluginLibrary(libraryPath: String, moduleName: String) throws { |
| 51 | + if let loaded = loadedLibraryPlugins[moduleName] { |
| 52 | + guard loaded.libraryPath == libraryPath else { |
| 53 | + // NOTE: Should be unreachable. Compiler should not load different |
| 54 | + // library for the same module name. |
| 55 | + throw LibraryPluginError( |
| 56 | + message: |
| 57 | + "library plugin for module '\(moduleName)' is already loaded from different path '\(loaded.libraryPath)'" |
| 58 | + ) |
| 59 | + } |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + let dlHandle = try _loadLibrary(libraryPath) |
| 64 | + |
| 65 | + loadedLibraryPlugins[moduleName] = LoadedLibraryPlugin( |
| 66 | + libraryPath: libraryPath, |
| 67 | + handle: dlHandle |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + public func resolveMacro(moduleName: String, typeName: String) throws -> SwiftSyntaxMacros.Macro.Type { |
| 72 | + let macroRef = MacroRef(moduleName: moduleName, typeName: typeName) |
| 73 | + if let resolved = resolvedMacros[macroRef] { |
| 74 | + return resolved |
| 75 | + } |
| 76 | + |
| 77 | + // Find 'dlopen'ed library for the module name. |
| 78 | + guard let plugin = loadedLibraryPlugins[moduleName] else { |
| 79 | + // NOTE: Should be unreachable. Compiler should not use this server |
| 80 | + // unless the plugin loading succeeded. |
| 81 | + throw LibraryPluginError(message: "plugin not loaded for module '\(moduleName)'") |
| 82 | + } |
| 83 | + |
| 84 | + // Lookup the type metadata. |
| 85 | + guard let type = _findAnyType(moduleName, typeName) else { |
| 86 | + throw LibraryPluginError( |
| 87 | + message: "type '\(moduleName).\(typeName)' could not be found in library plugin '\(plugin.libraryPath)'" |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + // The type must be a 'Macro' type. |
| 92 | + guard let macro = type as? Macro.Type else { |
| 93 | + throw LibraryPluginError( |
| 94 | + message: |
| 95 | + "type '\(moduleName).\(typeName)' is not a valid macro implementation type in library plugin '\(plugin.libraryPath)'" |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + // Cache the resolved type. |
| 100 | + resolvedMacros[macroRef] = macro |
| 101 | + return macro |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#if os(Windows) |
| 106 | +private func _loadLibrary(_ path: String) throws -> UnsafeMutableRawPointer { |
| 107 | + // Create NUL terminated UTF16 path. |
| 108 | + let utf16Path = UnsafeMutableBufferPointer<UInt16>.allocate(capacity: path.utf16.count + 1) |
| 109 | + defer { utf16Path.deallocate() } |
| 110 | + let end = utf16Path.initialize(fromContentsOf: path.utf16) |
| 111 | + utf16Path.initializeElement(at: end, to: 0) |
| 112 | + |
| 113 | + if let dlHandle = LoadLibraryW(utf16Path) { |
| 114 | + return dlHandle |
| 115 | + } |
| 116 | + // FIXME: Format the error code to string. |
| 117 | + throw LibraryPluginError(message: "loader error: \(GetLastError())") |
| 118 | +} |
| 119 | +#else |
| 120 | +private func _loadLibrary(_ path: String) throws -> UnsafeMutableRawPointer { |
| 121 | + if let dlHandle = dlopen(path, RTLD_LAZY | RTLD_LOCAL) { |
| 122 | + return dlHandle |
| 123 | + } |
| 124 | + throw LibraryPluginError(message: "loader error: \(String(cString: dlerror()))") |
| 125 | +} |
| 126 | +#endif |
| 127 | + |
| 128 | +private func _findAnyType(_ moduleName: String, _ typeName: String) -> Any.Type? { |
| 129 | + // Create a mangled name for struct, enum, and class. And use a runtime |
| 130 | + // function to find the type. Note that this simple mangling works even if the |
| 131 | + // actual symbol name doesn't match with it. i.e. We don't need to perform |
| 132 | + // punycode encodings or word substitutions. |
| 133 | + // FIXME: This is process global. Can we limit it to a specific .dylib ? |
| 134 | + for suffix in [ /*struct*/"V", /*enum*/ "O", /*class*/ "C"] { |
| 135 | + let mangled = "\(moduleName.utf8.count)\(moduleName)\(typeName.utf8.count)\(typeName)\(suffix)" |
| 136 | + if let type = _typeByName(mangled) { |
| 137 | + return type |
| 138 | + } |
| 139 | + } |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +private struct LibraryPluginError: Error, CustomStringConvertible { |
| 144 | + var description: String |
| 145 | + init(message: String) { |
| 146 | + self.description = message |
| 147 | + } |
| 148 | +} |
0 commit comments