1111//===----------------------------------------------------------------------===//
1212
1313#if swift(>=6)
14+ private import _SwiftSyntaxCShims
1415public import SwiftSyntaxMacros
1516#else
17+ @_implementationOnly import _SwiftSyntaxCShims
1618import SwiftSyntaxMacros
1719#endif
1820
@@ -23,6 +25,8 @@ public enum PluginFeature: String {
2325}
2426
2527/// A type that provides the actual plugin functions.
28+ ///
29+ /// Note that it's an implementation's responsibility to cache the API results as needed.
2630@_spi ( PluginMessage)
2731public protocol PluginProvider {
2832 /// Resolve macro type by the module name and the type name.
@@ -64,50 +68,60 @@ struct HostCapability {
6468 var hasExpandMacroResult : Bool { protocolVersion >= 5 }
6569}
6670
67- /// 'CompilerPluginMessageHandler ' is a type that listens to the message
68- /// connection and dispatches them to the actual plugin provider , then send back
71+ /// 'CompilerPluginMessageListener ' is a type that listens to the message
72+ /// connection, delegate them to the message handler , then send back
6973/// the response.
7074///
7175/// The low level connection and the provider is injected by the client.
7276@_spi ( PluginMessage)
73- public class CompilerPluginMessageHandler < Connection: MessageConnection , Provider: PluginProvider > {
77+ public class CompilerPluginMessageListener < Connection: MessageConnection , Provider: PluginProvider > {
7478 /// Message channel for bidirectional communication with the plugin host.
7579 let connection : Connection
7680
77- /// Object to provide actual plugin functions.
78- let provider : Provider
79-
80- /// Plugin host capability
81- var hostCapability : HostCapability
81+ let handler : CompilerPluginMessageHandler < Provider >
8282
8383 public init ( connection: Connection , provider: Provider ) {
8484 self . connection = connection
85- self . provider = provider
86- self . hostCapability = HostCapability ( )
87- }
88- }
89-
90- extension CompilerPluginMessageHandler {
91- func sendMessage( _ message: PluginToHostMessage ) throws {
92- try connection. sendMessage ( message)
93- }
94-
95- func waitForNextMessage( ) throws -> HostToPluginMessage ? {
96- try connection. waitForNextMessage ( HostToPluginMessage . self)
85+ self . handler = CompilerPluginMessageHandler ( provider: provider)
9786 }
9887
9988 /// Run the main message listener loop.
10089 /// Returns when the message connection was closed.
101- /// Throws an error when it failed to send/receive the message, or failed
102- /// to serialize/deserialize the message.
103- public func main( ) throws {
104- while let message = try self . waitForNextMessage ( ) {
105- try handleMessage ( message)
90+ ///
91+ /// On internal errors, such as I/O errors or JSON serialization errors, print
92+ /// an error message and `exit(1)`
93+ public func main( ) {
94+ do {
95+ while let message = try connection. waitForNextMessage ( HostToPluginMessage . self) {
96+ let result = handler. handleMessage ( message)
97+ try connection. sendMessage ( result)
98+ }
99+ } catch {
100+ // Emit a diagnostic and indicate failure to the plugin host,
101+ // and exit with an error code.
102+ fputs ( " Internal Error: \( error) \n " , _stderr)
103+ exit ( 1 )
106104 }
107105 }
106+ }
107+
108+ /// 'CompilerPluginMessageHandler' is a type that handle a message and do the
109+ /// corresponding operation.
110+ @_spi ( PluginMessage)
111+ public class CompilerPluginMessageHandler < Provider: PluginProvider > {
112+ /// Object to provide actual plugin functions.
113+ let provider : Provider
114+
115+ /// Plugin host capability
116+ var hostCapability : HostCapability
117+
118+ public init ( provider: Provider ) {
119+ self . provider = provider
120+ self . hostCapability = HostCapability ( )
121+ }
108122
109123 /// Handles a single message received from the plugin host.
110- fileprivate func handleMessage( _ message: HostToPluginMessage ) throws {
124+ public func handleMessage( _ message: HostToPluginMessage ) -> PluginToHostMessage {
111125 switch message {
112126 case . getCapability( let hostCapability) :
113127 // Remember the peer capability if provided.
@@ -120,7 +134,7 @@ extension CompilerPluginMessageHandler {
120134 protocolVersion: PluginMessage . PROTOCOL_VERSION_NUMBER,
121135 features: provider. features. map ( { $0. rawValue } )
122136 )
123- try self . sendMessage ( . getCapabilityResult( capability: capability) )
137+ return . getCapabilityResult( capability: capability)
124138
125139 case . expandFreestandingMacro(
126140 let macro,
@@ -129,7 +143,7 @@ extension CompilerPluginMessageHandler {
129143 let expandingSyntax,
130144 let lexicalContext
131145 ) :
132- try expandFreestandingMacro (
146+ return expandFreestandingMacro (
133147 macro: macro,
134148 macroRole: macroRole,
135149 discriminator: discriminator,
@@ -148,7 +162,7 @@ extension CompilerPluginMessageHandler {
148162 let conformanceListSyntax,
149163 let lexicalContext
150164 ) :
151- try expandAttachedMacro (
165+ return expandAttachedMacro (
152166 macro: macro,
153167 macroRole: macroRole,
154168 discriminator: discriminator,
@@ -176,7 +190,7 @@ extension CompilerPluginMessageHandler {
176190 )
177191 )
178192 }
179- try self . sendMessage ( . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags) ) ;
193+ return . loadPluginLibraryResult( loaded: diags. isEmpty, diagnostics: diags)
180194 }
181195 }
182196}
0 commit comments