@@ -46,7 +46,7 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
46
46
cxxDiagnosticEngine: UnsafeMutablePointer < UInt8 >
47
47
) -> Bool {
48
48
let plugin = CompilerPlugin ( opaqueHandle: opaqueHandle)
49
- assert ( plugin. capability. features? . contains ( " loadPluginLibrary " ) == true )
49
+ assert ( plugin. capability? . features. contains ( . loadPluginLibrary) == true )
50
50
let libraryPath = String ( cString: libraryPath)
51
51
let moduleName = String ( cString: moduleName)
52
52
let diagEngine = PluginDiagnosticsEngine ( cxxDiagnosticEngine: cxxDiagnosticEngine)
@@ -67,6 +67,24 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
67
67
}
68
68
69
69
struct CompilerPlugin {
70
+ struct Capability {
71
+ enum Feature : String {
72
+ case loadPluginLibrary = " load-plugin-library "
73
+ }
74
+
75
+ var protocolVersion : Int
76
+ var features : Set < Feature >
77
+
78
+ init ( _ message: PluginMessage . PluginCapability ) {
79
+ self . protocolVersion = message. protocolVersion
80
+ if let features = message. features {
81
+ self . features = Set ( features. compactMap ( Feature . init ( rawValue: ) ) )
82
+ } else {
83
+ self . features = [ ]
84
+ }
85
+ }
86
+ }
87
+
70
88
let opaqueHandle : UnsafeMutableRawPointer
71
89
72
90
private func withLock< R> ( _ body: ( ) throws -> R ) rethrows -> R {
@@ -111,26 +129,26 @@ struct CompilerPlugin {
111
129
}
112
130
113
131
func initialize( ) {
114
- self . withLock {
115
- // Get capability.
116
- let response : PluginToHostMessage
117
- do {
132
+ // Don't use `sendMessageAndWait` because we want to keep the lock until
133
+ // setting the returned value.
134
+ do {
135
+ try self . withLock {
136
+ // Get capability.
118
137
try self . sendMessage ( . getCapability)
119
- response = try self . waitForNextMessage ( )
120
- } catch {
121
- assertionFailure ( String ( describing: error) )
122
- return
123
- }
124
- switch response {
125
- case . getCapabilityResult( capability: let capability) :
126
- let ptr = UnsafeMutablePointer< PluginMessage . PluginCapability> . allocate( capacity: 1 )
127
- ptr. initialize ( to: capability)
138
+ let response = try self . waitForNextMessage ( )
139
+ guard case . getCapabilityResult( let capability) = response else {
140
+ throw PluginError . invalidReponseKind
141
+ }
142
+ let ptr = UnsafeMutablePointer< Capability> . allocate( capacity: 1 )
143
+ ptr. initialize ( to: . init( capability) )
128
144
Plugin_setCapability ( opaqueHandle, UnsafeRawPointer ( ptr) )
129
- default :
130
- assertionFailure ( " invalid response " )
131
145
}
146
+ } catch {
147
+ assertionFailure ( String ( describing: error) )
148
+ return
132
149
}
133
150
}
151
+
134
152
func deinitialize( ) {
135
153
self . withLock {
136
154
if let ptr = Plugin_getCapability ( opaqueHandle) {
@@ -142,11 +160,11 @@ struct CompilerPlugin {
142
160
}
143
161
}
144
162
145
- var capability : PluginMessage . PluginCapability {
163
+ var capability : Capability ? {
146
164
if let ptr = Plugin_getCapability ( opaqueHandle) {
147
- return ptr. assumingMemoryBound ( to: PluginMessage . PluginCapability . self) . pointee
165
+ return ptr. assumingMemoryBound ( to: Capability . self) . pointee
148
166
}
149
- return PluginMessage . PluginCapability ( protocolVersion : 0 )
167
+ return nil
150
168
}
151
169
}
152
170
0 commit comments