Skip to content

Commit 3d8a296

Browse files
committed
[Macros] Introduce CompilerPluginMessageListener
Separate message listening logic from 'CompilerPluginMessgeHandler' to 'CompilerPluginMessageListener', so 'CompilerPluginMessageHandler' can be used independently without 'MessageConnection' 'CompilerPluginMessageHandler.handleMessage()' now returns a response object instead of sending it to the peer.
1 parent 02a1330 commit 3d8a296

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ extension CompilerPlugin {
143143
// Handle messages from the host until the input stream is closed,
144144
// indicating that we're done.
145145
let provider = MacroProviderAdapter(plugin: Self())
146-
let impl = CompilerPluginMessageHandler(connection: connection, provider: provider)
146+
let impl = CompilerPluginMessageListener(connection: connection, provider: provider)
147147
do {
148148
try impl.main()
149149
} catch {

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,22 @@ struct HostCapability {
5757
var hasExpandMacroResult: Bool { protocolVersion >= 5 }
5858
}
5959

60-
/// 'CompilerPluginMessageHandler' is a type that listens to the message
61-
/// connection and dispatches them to the actual plugin provider, then send back
60+
/// 'CompilerPluginMessageListener' is a type that listens to the message
61+
/// connection, delegate them to the message handler, then send back
6262
/// the response.
6363
///
64-
/// The low level connection and the provider is injected by the client.
65-
public class CompilerPluginMessageHandler<Connection: MessageConnection, Provider: PluginProvider> {
64+
/// The low level connection and the plugin provider is injected by the client.
65+
public class CompilerPluginMessageListener<Connection: MessageConnection, Provider: PluginProvider> {
6666
/// Message channel for bidirectional communication with the plugin host.
6767
let connection: Connection
6868

69-
/// Object to provide actual plugin functions.
70-
let provider: Provider
71-
72-
/// Plugin host capability
73-
var hostCapability: HostCapability
69+
let handler: CompilerPluginMessageHandler<Provider>
7470

7571
public init(connection: Connection, provider: Provider) {
7672
self.connection = connection
77-
self.provider = provider
78-
self.hostCapability = HostCapability()
73+
self.handler = CompilerPluginMessageHandler(provider: provider)
7974
}
80-
}
8175

82-
extension CompilerPluginMessageHandler {
8376
func sendMessage(_ message: PluginToHostMessage) throws {
8477
try connection.sendMessage(message)
8578
}
@@ -94,12 +87,31 @@ extension CompilerPluginMessageHandler {
9487
/// to serialize/deserialize the message.
9588
public func main() throws {
9689
while let message = try self.waitForNextMessage() {
97-
try handleMessage(message)
90+
let result = handler.handleMessage(message)
91+
try connection.sendMessage(result)
9892
}
9993
}
94+
}
95+
96+
/// 'CompilerPluginMessageHandler' is a type that handle a message and do the
97+
/// corresponding operation.
98+
@_spi(Compiler)
99+
public class CompilerPluginMessageHandler<Provider: PluginProvider> {
100+
/// Object to provide actual plugin functions.
101+
let provider: Provider
102+
103+
/// Plugin host capability
104+
var hostCapability: HostCapability
105+
106+
public init(provider: Provider) {
107+
self.provider = provider
108+
self.hostCapability = HostCapability()
109+
}
110+
}
100111

112+
extension CompilerPluginMessageHandler {
101113
/// Handles a single message received from the plugin host.
102-
fileprivate func handleMessage(_ message: HostToPluginMessage) throws {
114+
public func handleMessage(_ message: HostToPluginMessage) -> PluginToHostMessage {
103115
switch message {
104116
case .getCapability(let hostCapability):
105117
// Remember the peer capability if provided.
@@ -112,10 +124,10 @@ extension CompilerPluginMessageHandler {
112124
protocolVersion: PluginMessage.PROTOCOL_VERSION_NUMBER,
113125
features: provider.features.map({ $0.rawValue })
114126
)
115-
try self.sendMessage(.getCapabilityResult(capability: capability))
127+
return .getCapabilityResult(capability: capability)
116128

117129
case .expandFreestandingMacro(let macro, let macroRole, let discriminator, let expandingSyntax):
118-
try expandFreestandingMacro(
130+
return expandFreestandingMacro(
119131
macro: macro,
120132
macroRole: macroRole,
121133
discriminator: discriminator,
@@ -132,7 +144,7 @@ extension CompilerPluginMessageHandler {
132144
let extendedTypeSyntax,
133145
let conformanceListSyntax
134146
):
135-
try expandAttachedMacro(
147+
return expandAttachedMacro(
136148
macro: macro,
137149
macroRole: macroRole,
138150
discriminator: discriminator,
@@ -159,7 +171,7 @@ extension CompilerPluginMessageHandler {
159171
)
160172
)
161173
}
162-
try self.sendMessage(.loadPluginLibraryResult(loaded: diags.isEmpty, diagnostics: diags));
174+
return .loadPluginLibraryResult(loaded: diags.isEmpty, diagnostics: diags)
163175
}
164176
}
165177
}

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension CompilerPluginMessageHandler {
2929
macroRole pluginMacroRole: PluginMessage.MacroRole?,
3030
discriminator: String,
3131
expandingSyntax: PluginMessage.Syntax
32-
) throws {
32+
) -> PluginToHostMessage {
3333
let sourceManager = SourceManager()
3434
let syntax = sourceManager.add(expandingSyntax, foldingWith: .standardOperators)
3535

@@ -73,7 +73,7 @@ extension CompilerPluginMessageHandler {
7373
// TODO: Remove this when all compilers have 'hasExpandMacroResult'.
7474
response = .expandFreestandingMacroResult(expandedSource: expandedSource, diagnostics: diagnostics)
7575
}
76-
try self.sendMessage(response)
76+
return response
7777
}
7878

7979
/// Expand `@attached(XXX)` macros.
@@ -86,7 +86,7 @@ extension CompilerPluginMessageHandler {
8686
parentDeclSyntax: PluginMessage.Syntax?,
8787
extendedTypeSyntax: PluginMessage.Syntax?,
8888
conformanceListSyntax: PluginMessage.Syntax?
89-
) throws {
89+
) -> PluginToHostMessage {
9090
let sourceManager = SourceManager()
9191
let context = PluginMacroExpansionContext(
9292
sourceManager: sourceManager,
@@ -150,7 +150,7 @@ extension CompilerPluginMessageHandler {
150150
} else {
151151
response = .expandAttachedMacroResult(expandedSources: expandedSources, diagnostics: diagnostics)
152152
}
153-
try self.sendMessage(response)
153+
return response
154154
}
155155
}
156156

0 commit comments

Comments
 (0)