Skip to content

Commit e240870

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 e240870

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
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: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,49 +57,53 @@ 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()
79-
}
80-
}
81-
82-
extension CompilerPluginMessageHandler {
83-
func sendMessage(_ message: PluginToHostMessage) throws {
84-
try connection.sendMessage(message)
85-
}
86-
87-
func waitForNextMessage() throws -> HostToPluginMessage? {
88-
try connection.waitForNextMessage(HostToPluginMessage.self)
73+
self.handler = CompilerPluginMessageHandler(provider: provider)
8974
}
9075

9176
/// Run the main message listener loop.
9277
/// Returns when the message connection was closed.
9378
/// Throws an error when it failed to send/receive the message, or failed
9479
/// to serialize/deserialize the message.
9580
public func main() throws {
96-
while let message = try self.waitForNextMessage() {
97-
try handleMessage(message)
81+
while let message = try connection.waitForNextMessage(HostToPluginMessage.self) {
82+
let result = handler.handleMessage(message)
83+
try connection.sendMessage(result)
9884
}
9985
}
86+
}
87+
88+
/// 'CompilerPluginMessageHandler' is a type that handle a message and do the
89+
/// corresponding operation.
90+
@_spi(Compiler)
91+
public class CompilerPluginMessageHandler<Provider: PluginProvider> {
92+
/// Object to provide actual plugin functions.
93+
let provider: Provider
94+
95+
/// Plugin host capability
96+
var hostCapability: HostCapability
10097

98+
public init(provider: Provider) {
99+
self.provider = provider
100+
self.hostCapability = HostCapability()
101+
}
102+
}
103+
104+
extension CompilerPluginMessageHandler {
101105
/// Handles a single message received from the plugin host.
102-
fileprivate func handleMessage(_ message: HostToPluginMessage) throws {
106+
public func handleMessage(_ message: HostToPluginMessage) -> PluginToHostMessage {
103107
switch message {
104108
case .getCapability(let hostCapability):
105109
// Remember the peer capability if provided.
@@ -112,10 +116,10 @@ extension CompilerPluginMessageHandler {
112116
protocolVersion: PluginMessage.PROTOCOL_VERSION_NUMBER,
113117
features: provider.features.map({ $0.rawValue })
114118
)
115-
try self.sendMessage(.getCapabilityResult(capability: capability))
119+
return .getCapabilityResult(capability: capability)
116120

117121
case .expandFreestandingMacro(let macro, let macroRole, let discriminator, let expandingSyntax):
118-
try expandFreestandingMacro(
122+
return expandFreestandingMacro(
119123
macro: macro,
120124
macroRole: macroRole,
121125
discriminator: discriminator,
@@ -132,7 +136,7 @@ extension CompilerPluginMessageHandler {
132136
let extendedTypeSyntax,
133137
let conformanceListSyntax
134138
):
135-
try expandAttachedMacro(
139+
return expandAttachedMacro(
136140
macro: macro,
137141
macroRole: macroRole,
138142
discriminator: discriminator,
@@ -159,7 +163,7 @@ extension CompilerPluginMessageHandler {
159163
)
160164
)
161165
}
162-
try self.sendMessage(.loadPluginLibraryResult(loaded: diags.isEmpty, diagnostics: diags));
166+
return .loadPluginLibraryResult(loaded: diags.isEmpty, diagnostics: diags)
163167
}
164168
}
165169
}

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)