@@ -26,8 +26,13 @@ final class SwiftPluginServer {
26
26
}
27
27
}
28
28
29
+ struct LoadedLibraryPlugin {
30
+ var libraryPath : String
31
+ var handle : UnsafeMutableRawPointer
32
+ }
33
+
29
34
/// Loaded dylib handles associated with the module name.
30
- var loadedLibraryPlugins : [ String : UnsafeMutableRawPointer ] = [ : ]
35
+ var loadedLibraryPlugins : [ String : LoadedLibraryPlugin ] = [ : ]
31
36
32
37
/// Resolved cached macros.
33
38
var resolvedMacros : [ MacroRef : Macro . Type ] = [ : ]
@@ -50,33 +55,37 @@ extension SwiftPluginServer: PluginProvider {
50
55
guard let dlHandle = PluginServer_load ( libraryPath, & errorMessage) else {
51
56
throw PluginServerError ( message: String ( cString: errorMessage!) )
52
57
}
53
- loadedLibraryPlugins [ moduleName] = dlHandle
58
+ loadedLibraryPlugins [ moduleName] = LoadedLibraryPlugin (
59
+ libraryPath: libraryPath,
60
+ handle: dlHandle
61
+ )
54
62
}
55
63
56
64
/// Lookup a loaded macro by a pair of module name and type name.
57
- func resolveMacro( moduleName: String , typeName: String ) -> Macro . Type ? {
65
+ func resolveMacro( moduleName: String , typeName: String ) throws -> Macro . Type {
58
66
if let resolved = resolvedMacros [ . init( moduleName, typeName) ] {
59
67
return resolved
60
68
}
61
69
62
70
// Find 'dlopen'ed library for the module name.
63
- guard let dlHandle = loadedLibraryPlugins [ moduleName] else {
64
- return nil
71
+ guard let plugin = loadedLibraryPlugins [ moduleName] else {
72
+ // NOTE: This should be unreachable. Compiler should not use this server
73
+ // unless the plugin loading succeeded.
74
+ throw PluginServerError ( message: " (plugin-server) plugin not loaded for module ' \( moduleName) ' " )
65
75
}
66
76
67
77
// Lookup the type metadata.
68
78
var errorMessage : UnsafePointer < CChar > ?
69
79
guard let macroTypePtr = PluginServer_lookupMacroTypeMetadataByExternalName (
70
- moduleName, typeName, dlHandle , & errorMessage
80
+ moduleName, typeName, plugin . handle , & errorMessage
71
81
) else {
72
- // FIXME: Propagate error message?
73
- return nil
82
+ throw PluginServerError ( message: " macro implementation type ' \( moduleName) . \( typeName) ' could not be found in library plugin ' \( plugin. libraryPath) ' " )
74
83
}
75
84
76
85
// THe type must be a 'Macro' type.
77
86
let macroType = unsafeBitCast ( macroTypePtr, to: Any . Type. self)
78
87
guard let macro = macroType as? Macro . Type else {
79
- return nil
88
+ throw PluginServerError ( message : " type ' \( moduleName ) . \( typeName ) ' is not a valid macro implementation type in library plugin ' \( plugin . libraryPath ) ' " )
80
89
}
81
90
82
91
// Cache the resolved type.
0 commit comments