From 158ddd5d34fdf1c0b18cef6ece105bb3a0757d28 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sun, 28 Apr 2024 17:22:09 -0400 Subject: [PATCH 01/18] Rebase --- .../SwiftCompilerPlugin/CompilerPlugin.swift | 33 +++++++++++++++++ .../CompilerPluginMessageHandler.swift | 16 +++++++-- .../StandardIOMessageConnection.swift | 15 ++++++++ Sources/_SwiftSyntaxCShims/dummy.c | 1 - Sources/_SwiftSyntaxCShims/wasm_support.c | 35 +++++++++++++++++++ 5 files changed, 96 insertions(+), 4 deletions(-) delete mode 100644 Sources/_SwiftSyntaxCShims/dummy.c create mode 100644 Sources/_SwiftSyntaxCShims/wasm_support.c diff --git a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift index 431b8ad2f3b..98ee56822ee 100644 --- a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift +++ b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift @@ -112,6 +112,16 @@ extension CompilerPlugin { let connection = try StandardIOMessageConnection() let provider = MacroProviderAdapter(plugin: Self()) let impl = CompilerPluginMessageListener(connection: connection, provider: provider) + #if os(WASI) + // Rather than blocking on read(), let the host tell us when there's data. + readabilityHandler = { + do { + _ = try impl.handleNextMessage() + } catch { + internalError("\(error)") + } + } + #else do { try impl.main() } catch { @@ -119,5 +129,28 @@ extension CompilerPlugin { // and exit with an error code. fatalError("Internal Error: \(error)") } + #endif } } + +#if os(WASI) + +/// A callback invoked by the Wasm Host when new data is available on `stdin`. +/// +/// This is safe to access without serialization as Wasm plugins are single-threaded. +nonisolated(unsafe) private var readabilityHandler: () -> Void = { + internalError(""" + CompilerPlugin.main wasn't called. Did you annotate your plugin with '@main'? + """) +} + +// We can use @_expose(wasm, ...) with compiler >= 6.0 +// but it causes build errors with older compilers. +// Instead, we export from wasm_support.c and trampoline +// to this method. +@_cdecl("_swift_wasm_macro_pump") +func wasmPump() { + readabilityHandler() +} + +#endif diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 8051af7e549..4ed7324169f 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -86,10 +86,20 @@ public class CompilerPluginMessageListener Bool { + guard let message = try connection.waitForNextMessage(HostToPluginMessage.self) else { + return false } + let result = handler.handleMessage(message) + try connection.sendMessage(result) + return true } } diff --git a/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift b/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift index df5a10ecdd7..3ee08f70f17 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift @@ -48,6 +48,11 @@ public struct StandardIOMessageConnection: MessageConnection { /// - Duplicate the original `stdin` and `stdout` for use as messaging /// pipes, and are not directly used by the plugin logic public init() throws { + #if os(WASI) + // Wasm doesn't support dup{,2} so we use the original file descriptors. + let inputFD = fileno(_stdin) + let outputFD = fileno(_stdout) + #else // Duplicate the `stdin` file descriptor, which we will then use for // receiving messages from the plugin host. let inputFD = dup(fileno(_stdin)) @@ -80,6 +85,7 @@ public struct StandardIOMessageConnection: MessageConnection { _ = _setmode(inputFD, _O_BINARY) _ = _setmode(outputFD, _O_BINARY) #endif + #endif self.init(inputFileDescriptor: inputFD, outputFileDescriptor: outputFD) } @@ -146,6 +152,15 @@ public struct StandardIOMessageConnection: MessageConnection { } } +#if os(WASI) +// fatalError writes to stdout, which is the message +// output stream under WASI +public func internalError(_ message: String) -> Never { + fputs("Internal Error: \(message)\n", _stderr) + exit(1) +} +#endif + private enum IOError: Error, CustomStringConvertible { case readReachedEndOfInput case systemError(function: String, errno: CInt) diff --git a/Sources/_SwiftSyntaxCShims/dummy.c b/Sources/_SwiftSyntaxCShims/dummy.c deleted file mode 100644 index 8b137891791..00000000000 --- a/Sources/_SwiftSyntaxCShims/dummy.c +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Sources/_SwiftSyntaxCShims/wasm_support.c b/Sources/_SwiftSyntaxCShims/wasm_support.c new file mode 100644 index 00000000000..c3e52cbe74a --- /dev/null +++ b/Sources/_SwiftSyntaxCShims/wasm_support.c @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#if defined(__wasm32__) + +// uint32_t +#define SWIFT_WASM_MACRO_ABI 1 + +#define _STR(X) #X +#define STR(X) _STR(X) + +// LLVM has special-cased handling to map .custom_section.foo to +// Wasm Custom Section "foo". this must be a metadata section rather +// than a data section so we can't use __attribute__((section)) for it. +// See: https://reviews.llvm.org/D43097 +__asm__("\t.section .custom_section.swift_wasm_macro_abi,\"\",@\n\t.4byte " STR(SWIFT_WASM_MACRO_ABI) "\n"); + +// defined in CompilerPlugin.swift +void _swift_wasm_macro_pump(void); + +__attribute__((export_name("swift_wasm_macro_pump"))) +void swift_wasm_macro_pump(void) { + _swift_wasm_macro_pump(); +} + +#endif From 4dd83b36f19b602d1382bc643cc3a5199e91bde6 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 29 Apr 2024 23:16:16 -0400 Subject: [PATCH 02/18] cleanup --- Sources/SwiftCompilerPlugin/CompilerPlugin.swift | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift index a7301ff8c9d..40c9c12bbbf 100644 --- a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift +++ b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift @@ -114,13 +114,7 @@ extension CompilerPlugin { let impl = CompilerPluginMessageListener(connection: connection, provider: provider) #if os(WASI) // Rather than blocking on read(), let the host tell us when there's data. - readabilityHandler = { - do { - _ = try impl.handleNextMessage() - } catch { - internalError("\(error)") - } - } + readabilityHandler = { impl.handleNextMessage() } #else impl.main() #endif From 0b6a5a601b09289700326abe593959b0c11d3b87 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 29 Apr 2024 23:23:46 -0400 Subject: [PATCH 03/18] =?UTF-8?q?We=20don=E2=80=99t=20need=20internalError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/SwiftCompilerPlugin/CompilerPlugin.swift | 4 ++-- .../StandardIOMessageConnection.swift | 9 --------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift index 40c9c12bbbf..134baf1f365 100644 --- a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift +++ b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift @@ -114,7 +114,7 @@ extension CompilerPlugin { let impl = CompilerPluginMessageListener(connection: connection, provider: provider) #if os(WASI) // Rather than blocking on read(), let the host tell us when there's data. - readabilityHandler = { impl.handleNextMessage() } + readabilityHandler = { _ = impl.handleNextMessage() } #else impl.main() #endif @@ -127,7 +127,7 @@ extension CompilerPlugin { /// /// This is safe to access without serialization as Wasm plugins are single-threaded. nonisolated(unsafe) private var readabilityHandler: () -> Void = { - internalError(""" + fatalError(""" CompilerPlugin.main wasn't called. Did you annotate your plugin with '@main'? """) } diff --git a/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift b/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift index 3ee08f70f17..d9fa702ba0e 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift @@ -152,15 +152,6 @@ public struct StandardIOMessageConnection: MessageConnection { } } -#if os(WASI) -// fatalError writes to stdout, which is the message -// output stream under WASI -public func internalError(_ message: String) -> Never { - fputs("Internal Error: \(message)\n", _stderr) - exit(1) -} -#endif - private enum IOError: Error, CustomStringConvertible { case readReachedEndOfInput case systemError(function: String, errno: CInt) From 7a6b5761c434fb4c85592d50f3bb80a9f01643bf Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 29 Apr 2024 23:28:32 -0400 Subject: [PATCH 04/18] Comment --- Sources/_SwiftSyntaxCShims/wasm_support.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/_SwiftSyntaxCShims/wasm_support.c b/Sources/_SwiftSyntaxCShims/wasm_support.c index c3e52cbe74a..5e3ca38531e 100644 --- a/Sources/_SwiftSyntaxCShims/wasm_support.c +++ b/Sources/_SwiftSyntaxCShims/wasm_support.c @@ -18,6 +18,9 @@ #define _STR(X) #X #define STR(X) _STR(X) +// Add a `swift_wasm_macro_abi` section to the generated Wasm file +// to allow the runner to check our ABI version and act accordingly. +// // LLVM has special-cased handling to map .custom_section.foo to // Wasm Custom Section "foo". this must be a metadata section rather // than a data section so we can't use __attribute__((section)) for it. From f5d82ef37c45031847513f4a7667512f7694d91a Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 29 Apr 2024 23:44:42 -0400 Subject: [PATCH 05/18] Feedback --- .../StandardIOMessageConnection.swift | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift b/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift index d9fa702ba0e..a4c45112c36 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/StandardIOMessageConnection.swift @@ -40,6 +40,16 @@ public struct StandardIOMessageConnection: MessageConnection { self.outputFileDescriptor = outputFileDescriptor } + #if os(WASI) + /// Convenience initializer for Wasm executable plugins. Connects + /// directly to `stdin` and `stdout` as WASI doesn't support + /// `dup{,2}`. + public init() throws { + let inputFD = fileno(_stdin) + let outputFD = fileno(_stdout) + self.init(inputFileDescriptor: inputFD, outputFileDescriptor: outputFD) + } + #else /// Convenience initializer for normal executable plugins. Upon creation: /// - Redirect `stdout` to `stderr` so that print statements from the plugin /// are treated as plain-text output @@ -48,11 +58,6 @@ public struct StandardIOMessageConnection: MessageConnection { /// - Duplicate the original `stdin` and `stdout` for use as messaging /// pipes, and are not directly used by the plugin logic public init() throws { - #if os(WASI) - // Wasm doesn't support dup{,2} so we use the original file descriptors. - let inputFD = fileno(_stdin) - let outputFD = fileno(_stdout) - #else // Duplicate the `stdin` file descriptor, which we will then use for // receiving messages from the plugin host. let inputFD = dup(fileno(_stdin)) @@ -85,10 +90,10 @@ public struct StandardIOMessageConnection: MessageConnection { _ = _setmode(inputFD, _O_BINARY) _ = _setmode(outputFD, _O_BINARY) #endif - #endif self.init(inputFileDescriptor: inputFD, outputFileDescriptor: outputFD) } + #endif /// Write the buffer to the file descriptor. Throws an error on failure. private func _write(contentsOf buffer: UnsafeRawBufferPointer) throws { From 9f455cb608dbb54bf704b6dea67ef33db334d4c5 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 30 Apr 2024 00:08:52 -0400 Subject: [PATCH 06/18] More feedback --- .../SwiftCompilerPlugin/CompilerPlugin.swift | 27 ----------------- .../CompilerPluginMessageHandler.swift | 29 ++++++++++++++++++- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift index 134baf1f365..3ced3a43de1 100644 --- a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift +++ b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift @@ -112,33 +112,6 @@ extension CompilerPlugin { let connection = try StandardIOMessageConnection() let provider = MacroProviderAdapter(plugin: Self()) let impl = CompilerPluginMessageListener(connection: connection, provider: provider) - #if os(WASI) - // Rather than blocking on read(), let the host tell us when there's data. - readabilityHandler = { _ = impl.handleNextMessage() } - #else impl.main() - #endif } } - -#if os(WASI) - -/// A callback invoked by the Wasm Host when new data is available on `stdin`. -/// -/// This is safe to access without serialization as Wasm plugins are single-threaded. -nonisolated(unsafe) private var readabilityHandler: () -> Void = { - fatalError(""" - CompilerPlugin.main wasn't called. Did you annotate your plugin with '@main'? - """) -} - -// We can use @_expose(wasm, ...) with compiler >= 6.0 -// but it causes build errors with older compilers. -// Instead, we export from wasm_support.c and trampoline -// to this method. -@_cdecl("_swift_wasm_macro_pump") -func wasmPump() { - readabilityHandler() -} - -#endif diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 33b4dfd1f46..48e98a97f1d 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -89,10 +89,15 @@ public class CompilerPluginMessageListener Bool { + private func handleNextMessage() -> Bool { do { guard let message = try connection.waitForNextMessage(HostToPluginMessage.self) else { return false @@ -216,3 +221,25 @@ extension PluginProvider { throw UnimplementedError() } } + +#if os(WASI) + +/// A callback invoked by the Wasm Host when new data is available on `stdin`. +/// +/// This is safe to access without serialization as Wasm plugins are single-threaded. +nonisolated(unsafe) private var readabilityHandler: () -> Void = { + fatalError(""" + CompilerPlugin.main wasn't called. Did you annotate your plugin with '@main'? + """) +} + +// We can use @_expose(wasm, ...) with compiler >= 6.0 +// but it causes build errors with older compilers. +// Instead, we export from wasm_support.c and trampoline +// to this method. +@_cdecl("_swift_wasm_macro_pump") +func wasmPump() { + readabilityHandler() +} + +#endif From 18814f1bb17b1b75249833c5c4398e21cdd9fafa Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 30 Apr 2024 00:20:06 -0400 Subject: [PATCH 07/18] comment --- Sources/_SwiftSyntaxCShims/wasm_support.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_SwiftSyntaxCShims/wasm_support.c b/Sources/_SwiftSyntaxCShims/wasm_support.c index 5e3ca38531e..d075b44789e 100644 --- a/Sources/_SwiftSyntaxCShims/wasm_support.c +++ b/Sources/_SwiftSyntaxCShims/wasm_support.c @@ -27,7 +27,7 @@ // See: https://reviews.llvm.org/D43097 __asm__("\t.section .custom_section.swift_wasm_macro_abi,\"\",@\n\t.4byte " STR(SWIFT_WASM_MACRO_ABI) "\n"); -// defined in CompilerPlugin.swift +// defined in CompilerPluginMessageHandler.swift void _swift_wasm_macro_pump(void); __attribute__((export_name("swift_wasm_macro_pump"))) From a38baa435b2eff5ea21abe095591d5e59f3655a4 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 30 Apr 2024 00:25:57 -0400 Subject: [PATCH 08/18] comment --- .../CompilerPluginMessageHandler.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 48e98a97f1d..beae8fa79c7 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -97,6 +97,10 @@ public class CompilerPluginMessageListener Bool { do { guard let message = try connection.waitForNextMessage(HostToPluginMessage.self) else { From 85e03749d0d82561b3c37391e16d0d01c9808a98 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sun, 5 May 2024 13:57:49 -0500 Subject: [PATCH 09/18] Change wasm versioning approach --- Sources/_SwiftSyntaxCShims/wasm_support.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/Sources/_SwiftSyntaxCShims/wasm_support.c b/Sources/_SwiftSyntaxCShims/wasm_support.c index d075b44789e..687c2f795d7 100644 --- a/Sources/_SwiftSyntaxCShims/wasm_support.c +++ b/Sources/_SwiftSyntaxCShims/wasm_support.c @@ -12,25 +12,10 @@ #if defined(__wasm32__) -// uint32_t -#define SWIFT_WASM_MACRO_ABI 1 - -#define _STR(X) #X -#define STR(X) _STR(X) - -// Add a `swift_wasm_macro_abi` section to the generated Wasm file -// to allow the runner to check our ABI version and act accordingly. -// -// LLVM has special-cased handling to map .custom_section.foo to -// Wasm Custom Section "foo". this must be a metadata section rather -// than a data section so we can't use __attribute__((section)) for it. -// See: https://reviews.llvm.org/D43097 -__asm__("\t.section .custom_section.swift_wasm_macro_abi,\"\",@\n\t.4byte " STR(SWIFT_WASM_MACRO_ABI) "\n"); - // defined in CompilerPluginMessageHandler.swift void _swift_wasm_macro_pump(void); -__attribute__((export_name("swift_wasm_macro_pump"))) +__attribute__((export_name("swift_wasm_macro_v1_pump"))) void swift_wasm_macro_pump(void) { _swift_wasm_macro_pump(); } From d3495f2d47ec6fe3fb52895fe08c5473d3cde313 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sat, 25 May 2024 19:23:04 -0400 Subject: [PATCH 10/18] Create PluginMessageHandler --- Package.swift | 1 + .../CompilerPluginMessageHandler.swift | 32 +++++++++++++++---- .../Macros.swift | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Package.swift b/Package.swift index dde4f667dd2..4d6bbe1cb2c 100644 --- a/Package.swift +++ b/Package.swift @@ -18,6 +18,7 @@ let package = Package( .library(name: "SwiftCompilerPluginMessageHandling", targets: ["SwiftCompilerPluginMessageHandling"]), .library(name: "SwiftDiagnostics", targets: ["SwiftDiagnostics"]), .library(name: "SwiftIDEUtils", targets: ["SwiftIDEUtils"]), + .library(name: "SwiftLibraryPluginProvider", targets: ["SwiftLibraryPluginProvider"]), .library(name: "SwiftOperators", targets: ["SwiftOperators"]), .library(name: "SwiftParser", targets: ["SwiftParser"]), .library(name: "SwiftParserDiagnostics", targets: ["SwiftParserDiagnostics"]), diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index ca73845da6e..cdb39f182e5 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -74,15 +74,21 @@ struct HostCapability { /// /// The low level connection and the provider is injected by the client. @_spi(PluginMessage) -public class CompilerPluginMessageListener { +public class CompilerPluginMessageListener { /// Message channel for bidirectional communication with the plugin host. let connection: Connection - let handler: CompilerPluginMessageHandler + let handler: Handler - public init(connection: Connection, provider: Provider) { + public init(connection: Connection, messageHandler: Handler) { self.connection = connection - self.handler = CompilerPluginMessageHandler(provider: provider) + self.handler = messageHandler + } + + public init(connection: Connection, provider: Provider) + where Handler == PluginProviderMessageHandler { + self.connection = connection + self.handler = PluginProviderMessageHandler(provider: provider) } /// Run the main message listener loop. @@ -120,10 +126,18 @@ public class CompilerPluginMessageListener PluginToHostMessage +} + +/// A `PluginMessageHandler` that uses a `PluginProvider`. @_spi(PluginMessage) -public class CompilerPluginMessageHandler { +public class PluginProviderMessageHandler: PluginMessageHandler { /// Object to provide actual plugin functions. let provider: Provider @@ -210,6 +224,10 @@ public class CompilerPluginMessageHandler { } } +@_spi(PluginMessage) +@available(*, deprecated, renamed: "PluginProviderMessageHandler") +public typealias CompilerPluginMessageHandler = PluginProviderMessageHandler + struct UnimplementedError: Error, CustomStringConvertible { var description: String { "unimplemented" } } diff --git a/Sources/SwiftCompilerPluginMessageHandling/Macros.swift b/Sources/SwiftCompilerPluginMessageHandling/Macros.swift index e07e763862d..4b1f32bb41f 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/Macros.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/Macros.swift @@ -26,7 +26,7 @@ import SwiftSyntax @_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros #endif -extension CompilerPluginMessageHandler { +extension PluginProviderMessageHandler { /// Get concrete macro type from a pair of module name and type name. private func resolveMacro(_ ref: PluginMessage.MacroReference) throws -> Macro.Type { try provider.resolveMacro(moduleName: ref.moduleName, typeName: ref.typeName) From 76de84f471dcf502d2d2592076ed6b8fc2e05094 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sat, 25 May 2024 19:47:41 -0400 Subject: [PATCH 11/18] Make PluginMessage.Diagnostic.init SPI --- .../SwiftCompilerPluginMessageHandling/PluginMessages.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift b/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift index aeb8e9bd9ae..9bd4a7d5a9e 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift @@ -229,7 +229,8 @@ public enum PluginMessage { public var notes: [Note] public var fixIts: [FixIt] - internal init( + @_spi(PluginMessage) + public init( message: String, severity: Severity, position: Position, From 76f7d8f8b59d6bd893ca4ecc257600ba036b7603 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 11 Jun 2024 12:35:33 +0530 Subject: [PATCH 12/18] Use _expose --- .../CompilerPluginMessageHandler.swift | 17 ++++++++------ Sources/_SwiftSyntaxCShims/dummy.c | 1 + Sources/_SwiftSyntaxCShims/wasm_support.c | 23 ------------------- 3 files changed, 11 insertions(+), 30 deletions(-) create mode 100644 Sources/_SwiftSyntaxCShims/dummy.c delete mode 100644 Sources/_SwiftSyntaxCShims/wasm_support.c diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index cdb39f182e5..64611d7af76 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -246,7 +246,7 @@ extension PluginProvider { } } -#if os(WASI) +#if compiler(>=6) && os(WASI) /// A callback invoked by the Wasm Host when new data is available on `stdin`. /// @@ -257,13 +257,16 @@ nonisolated(unsafe) private var readabilityHandler: () -> Void = { """) } -// We can use @_expose(wasm, ...) with compiler >= 6.0 -// but it causes build errors with older compilers. -// Instead, we export from wasm_support.c and trampoline -// to this method. -@_cdecl("_swift_wasm_macro_pump") -func wasmPump() { +@_expose(wasm, "swift_wasm_macro_v1_pump") +func wasmPump() throws { readabilityHandler() } +// we can't nest the whole #if-#else in '#if os(WASI)' due to a bug where +// '#if compiler' directives have to be the top-level #if, otherwise +// the compiler doesn't skip unknown syntax. +#elseif os(WASI) + +#error("Building swift-syntax for WebAssembly requires compiler version 6.0 or higher.") + #endif diff --git a/Sources/_SwiftSyntaxCShims/dummy.c b/Sources/_SwiftSyntaxCShims/dummy.c new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/Sources/_SwiftSyntaxCShims/dummy.c @@ -0,0 +1 @@ + diff --git a/Sources/_SwiftSyntaxCShims/wasm_support.c b/Sources/_SwiftSyntaxCShims/wasm_support.c deleted file mode 100644 index 687c2f795d7..00000000000 --- a/Sources/_SwiftSyntaxCShims/wasm_support.c +++ /dev/null @@ -1,23 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift open source project -// -// Copyright (c) 2023 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See http://swift.org/LICENSE.txt for license information -// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#if defined(__wasm32__) - -// defined in CompilerPluginMessageHandler.swift -void _swift_wasm_macro_pump(void); - -__attribute__((export_name("swift_wasm_macro_v1_pump"))) -void swift_wasm_macro_pump(void) { - _swift_wasm_macro_pump(); -} - -#endif From cf060a4e71c3a1c8a304a2c082e09be0d7f95fd0 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Wed, 12 Jun 2024 14:55:37 +0530 Subject: [PATCH 13/18] Feedback --- .../CompilerPluginMessageHandler.swift | 4 ---- .../SwiftCompilerPluginMessageHandling/PluginMessages.swift | 1 - 2 files changed, 5 deletions(-) diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 64611d7af76..3b8a308d45b 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -224,10 +224,6 @@ public class PluginProviderMessageHandler: PluginMessa } } -@_spi(PluginMessage) -@available(*, deprecated, renamed: "PluginProviderMessageHandler") -public typealias CompilerPluginMessageHandler = PluginProviderMessageHandler - struct UnimplementedError: Error, CustomStringConvertible { var description: String { "unimplemented" } } diff --git a/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift b/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift index 9bd4a7d5a9e..37029ff6bd5 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift @@ -229,7 +229,6 @@ public enum PluginMessage { public var notes: [Note] public var fixIts: [FixIt] - @_spi(PluginMessage) public init( message: String, severity: Severity, From 4cd128c9fab6d1b0f887398b009126f523de0c65 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Wed, 12 Jun 2024 16:45:06 +0530 Subject: [PATCH 14/18] Fix calling convention --- .../CompilerPluginMessageHandler.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 3b8a308d45b..9e02f544238 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -254,7 +254,8 @@ nonisolated(unsafe) private var readabilityHandler: () -> Void = { } @_expose(wasm, "swift_wasm_macro_v1_pump") -func wasmPump() throws { +@_cdecl("swift_wasm_macro_v1_pump") +func wasmPump() { readabilityHandler() } From 84f323025b3bd4d841f892992fa0a901ad4d628c Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Thu, 13 Jun 2024 12:16:37 +0530 Subject: [PATCH 15/18] Update .spi.yml --- .spi.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.spi.yml b/.spi.yml index 5b8d735168a..3bcefa3f3fc 100644 --- a/.spi.yml +++ b/.spi.yml @@ -13,6 +13,7 @@ builder: - SwiftCompilerPluginMessageHandling - SwiftDiagnostics - SwiftIDEUtils + - SwiftLibraryPluginProvider - SwiftOperators - SwiftParser - SwiftParserDiagnostics From 2242b64c2ea03c331fe2744d80dc1819a0936e99 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Thu, 13 Jun 2024 21:29:55 +0530 Subject: [PATCH 16/18] Format --- .../CompilerPluginMessageHandler.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 9e02f544238..6675db24163 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -85,8 +85,8 @@ public class CompilerPluginMessageListener(connection: Connection, provider: Provider) - where Handler == PluginProviderMessageHandler { + public init(connection: Connection, provider: Provider) + where Handler == PluginProviderMessageHandler { self.connection = connection self.handler = PluginProviderMessageHandler(provider: provider) } @@ -248,12 +248,14 @@ extension PluginProvider { /// /// This is safe to access without serialization as Wasm plugins are single-threaded. nonisolated(unsafe) private var readabilityHandler: () -> Void = { - fatalError(""" - CompilerPlugin.main wasn't called. Did you annotate your plugin with '@main'? - """) + fatalError( + """ + CompilerPlugin.main wasn't called. Did you annotate your plugin with '@main'? + """ + ) } -@_expose(wasm, "swift_wasm_macro_v1_pump") +@_expose(wasm,"swift_wasm_macro_v1_pump") @_cdecl("swift_wasm_macro_v1_pump") func wasmPump() { readabilityHandler() From 595b0ae9262c0266d98bc8862d8dbf91a3ca98cd Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 18 Jun 2024 12:57:50 +0530 Subject: [PATCH 17/18] re-format --- .../CompilerPluginMessageHandler.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 6675db24163..303ff81f996 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -255,7 +255,7 @@ nonisolated(unsafe) private var readabilityHandler: () -> Void = { ) } -@_expose(wasm,"swift_wasm_macro_v1_pump") +@_expose(wasm, "swift_wasm_macro_v1_pump") @_cdecl("swift_wasm_macro_v1_pump") func wasmPump() { readabilityHandler() From 0e65034762b3930f0cb3db161278fa7bae2e64b0 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sat, 29 Jun 2024 20:34:42 +0530 Subject: [PATCH 18/18] Fix swiftlang/swift build --- .../CompilerPluginMessageHandler.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift index 9c0121e7dc1..6c78e03e845 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift +++ b/Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift @@ -228,6 +228,10 @@ public class PluginProviderMessageHandler: PluginMessa } } +@_spi(PluginMessage) +@available(*, deprecated, renamed: "PluginProviderMessageHandler") +public typealias CompilerPluginMessageHandler = PluginProviderMessageHandler + struct UnimplementedError: Error, CustomStringConvertible { var description: String { "unimplemented" } }