Skip to content

Commit bbb7024

Browse files
committed
[Macros] Use 'LibraryPluginProvider' in swift-plugin-server
Move 'PluginProvider' logic to 'SwiftLibraryPluginProvider' module so the compiler can reuse that logic for the actual in-process plugins. (cherry picked from commit 26dbe2f)
1 parent 280dfcc commit bbb7024

File tree

7 files changed

+5
-239
lines changed

7 files changed

+5
-239
lines changed

test/Macros/macro_plugin_server.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func testStringify(a: Int, b: Int) {
8787
let s3: String = #stringify(b + a).1
8888
print(s3)
8989

90-
// expected-error @+1 {{macro implementation type 'MacroDefinition.TypeDoesNotExist' could not be found in library plugin '}}
90+
// expected-error @+1 {{type 'MacroDefinition.TypeDoesNotExist' could not be found in library plugin '}}
9191
_ = #missing()
9292

9393
// expected-error @+1 {{type 'MacroDefinition.NotMacroStruct' is not a valid macro implementation type in library plugin '}}
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
if (SWIFT_BUILD_SWIFT_SYNTAX)
2-
# _swiftCSwiftPluginServer is just a C support library for swift-plugin-server
3-
# Don't bother to create '.a' for that.
4-
add_swift_host_library(_swiftCSwiftPluginServer STATIC
5-
Sources/CSwiftPluginServer/PluginServer.cpp
6-
LLVM_LINK_COMPONENTS support
7-
)
8-
target_link_libraries(_swiftCSwiftPluginServer PRIVATE
9-
swiftDemangling
10-
)
11-
target_include_directories(_swiftCSwiftPluginServer PUBLIC
12-
Sources/CSwiftPluginServer/include
13-
)
14-
152
add_pure_swift_host_tool(swift-plugin-server
163
Sources/swift-plugin-server/swift-plugin-server.swift
17-
DEPENDENCIES
18-
_swiftCSwiftPluginServer
194
SWIFT_COMPONENT
205
compiler
216
SWIFT_DEPENDENCIES
22-
SwiftSyntaxMacros
23-
SwiftSyntaxMacroExpansion
247
SwiftCompilerPluginMessageHandling
25-
)
26-
target_include_directories(swift-plugin-server PRIVATE
27-
Sources/CSwiftPluginServer/include
8+
SwiftLibraryPluginProvider
289
)
2910
endif()

tools/swift-plugin-server/Package.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,10 @@ let package = Package(
1111
.package(path: "../../../swift-syntax"),
1212
],
1313
targets: [
14-
.target(
15-
name: "CSwiftPluginServer",
16-
cxxSettings: [
17-
.unsafeFlags([
18-
"-I", "../../include",
19-
"-I", "../../../llvm-project/llvm/include",
20-
])
21-
]
22-
),
2314
.executableTarget(
2415
name: "swift-plugin-server",
2516
dependencies: [
2617
.product(name: "SwiftCompilerPluginMessageHandling", package: "swift-syntax"),
27-
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
28-
.product(name: "SwiftSyntax", package: "swift-syntax"),
29-
.product(name: "SwiftOperators", package: "swift-syntax"),
30-
.product(name: "SwiftParser", package: "swift-syntax"),
31-
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
32-
"CSwiftPluginServer"
3318
]
3419
),
3520
],

tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp

Lines changed: 0 additions & 77 deletions
This file was deleted.

tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

tools/swift-plugin-server/Sources/CSwiftPluginServer/include/module.modulemap

Lines changed: 0 additions & 4 deletions
This file was deleted.

tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,96 +11,16 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
@_spi(PluginMessage) import SwiftCompilerPluginMessageHandling
14-
import SwiftSyntaxMacros
15-
import CSwiftPluginServer
14+
@_spi(PluginMessage) import SwiftLibraryPluginProvider
1615

1716
@main
1817
final class SwiftPluginServer {
19-
struct MacroRef: Hashable {
20-
var moduleName: String
21-
var typeName: String
22-
init(_ moduleName: String, _ typeName: String) {
23-
self.moduleName = moduleName
24-
self.typeName = typeName
25-
}
26-
}
27-
28-
struct LoadedLibraryPlugin {
29-
var libraryPath: String
30-
var handle: UnsafeMutableRawPointer
31-
}
32-
33-
/// Loaded dylib handles associated with the module name.
34-
var loadedLibraryPlugins: [String: LoadedLibraryPlugin] = [:]
35-
36-
/// Resolved cached macros.
37-
var resolvedMacros: [MacroRef: Macro.Type] = [:]
38-
39-
/// @main entry point.
4018
static func main() throws {
4119
let connection = try StandardIOMessageConnection()
4220
let listener = CompilerPluginMessageListener(
4321
connection: connection,
44-
provider: self.init()
22+
provider: LibraryPluginProvider.shared
4523
)
46-
try listener.main()
47-
}
48-
}
49-
50-
extension SwiftPluginServer: PluginProvider {
51-
/// Load a macro implementation from the dynamic link library.
52-
func loadPluginLibrary(libraryPath: String, moduleName: String) throws {
53-
var errorMessage: UnsafePointer<CChar>?
54-
guard let dlHandle = PluginServer_load(libraryPath, &errorMessage) else {
55-
throw PluginServerError(message: "loader error: " + String(cString: errorMessage!))
56-
}
57-
loadedLibraryPlugins[moduleName] = LoadedLibraryPlugin(
58-
libraryPath: libraryPath,
59-
handle: dlHandle
60-
)
61-
}
62-
63-
/// Lookup a loaded macro by a pair of module name and type name.
64-
func resolveMacro(moduleName: String, typeName: String) throws -> Macro.Type {
65-
if let resolved = resolvedMacros[.init(moduleName, typeName)] {
66-
return resolved
67-
}
68-
69-
// Find 'dlopen'ed library for the module name.
70-
guard let plugin = loadedLibraryPlugins[moduleName] else {
71-
// NOTE: This should be unreachable. Compiler should not use this server
72-
// unless the plugin loading succeeded.
73-
throw PluginServerError(message: "(plugin-server) plugin not loaded for module '\(moduleName)'")
74-
}
75-
76-
// Lookup the type metadata.
77-
var errorMessage: UnsafePointer<CChar>?
78-
guard let macroTypePtr = PluginServer_lookupMacroTypeMetadataByExternalName(
79-
moduleName, typeName, plugin.handle, &errorMessage
80-
) else {
81-
throw PluginServerError(message: "macro implementation type '\(moduleName).\(typeName)' could not be found in library plugin '\(plugin.libraryPath)'")
82-
}
83-
84-
// THe type must be a 'Macro' type.
85-
let macroType = unsafeBitCast(macroTypePtr, to: Any.Type.self)
86-
guard let macro = macroType as? Macro.Type else {
87-
throw PluginServerError(message: "type '\(moduleName).\(typeName)' is not a valid macro implementation type in library plugin '\(plugin.libraryPath)'")
88-
}
89-
90-
// Cache the resolved type.
91-
resolvedMacros[.init(moduleName, typeName)] = macro
92-
return macro
93-
}
94-
95-
/// This 'PluginProvider' implements 'loadLibraryMacro()'.
96-
var features: [SwiftCompilerPluginMessageHandling.PluginFeature] {
97-
[.loadPluginLibrary]
98-
}
99-
}
100-
101-
struct PluginServerError: Error, CustomStringConvertible {
102-
var description: String
103-
init(message: String) {
104-
self.description = message
24+
listener.main()
10525
}
10626
}

0 commit comments

Comments
 (0)