From 8e05fb0fa61b5c9536b6e8890ad76b0be4969f2e Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 10:48:05 -0700 Subject: [PATCH 1/4] swift-plugin-server: remove standard headers Redefine the types rather than use the standard headers due to the circular dependency between Darwin and Swift. --- .../CSwiftPluginServer/include/PluginServer.h | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index 92e62c3aa7bc4..680c058b695f8 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -13,8 +13,48 @@ #ifndef SWIFT_PLUGINSERVER_PLUGINSERVER_H #define SWIFT_PLUGINSERVER_PLUGINSERVER_H -#include +// NOTE: Partially ported from SwiftShim's SwiftStdint.h. We cannot include +// that header here because it belongs to the runtime, but we need the same +// logic for interoperability with Swift code in the compiler itself. +// stdint.h is provided by Clang, but it dispatches to libc's stdint.h. As a +// result, using stdint.h here would pull in Darwin module (which includes +// libc). This creates a dependency cycle, so we can't use stdint.h in +// SwiftShims. +// On Linux, the story is different. We get the error message +// "/usr/include/x86_64-linux-gnu/sys/types.h:146:10: error: 'stddef.h' file not +// found" +// This is a known Clang/Ubuntu bug. + +// Clang has been defining __INTxx_TYPE__ macros for a long time. +// __UINTxx_TYPE__ are defined only since Clang 3.5. +#if defined(_MSC_VER) && !defined(__clang__) +typedef __int64 __swiftc_int64_t; +typedef unsigned __int64 __swiftc_uint64_t; +typedef int __swiftc_int32_t; +typedef unsigned int __swiftc_uint32_t; +#elif !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__) #include +typedef int64_t __swiftc_int64_t; +typedef uint64_t __swiftc_uint64_t; +typedef int32_t __swiftc_int32_t; +typedef uint32_t __swiftc_uint32_t; +typedef intptr_t __swiftc_intptr_t; +typedef uintptr_t __swiftc_uintptr_t; +#else +typedef __INT64_TYPE__ __swiftc_int64_t; +#ifdef __UINT64_TYPE__ +typedef __UINT64_TYPE__ __swiftc_uint64_t; +#else +typedef unsigned __INT64_TYPE__ __swiftc_uint64_t; +#endif + +typedef __INT32_TYPE__ __swiftc_int32_t; +#ifdef __UINT32_TYPE__ +typedef __UINT32_TYPE__ __swiftc_uint32_t; +#else +typedef unsigned __INT32_TYPE__ __swiftc_uint32_t; +#endif +#endif #ifdef __cplusplus extern "C" { From 7acf3e97c1e7e4784447c6427692e3e5c07a7518 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 10:56:48 -0700 Subject: [PATCH 2/4] Update PluginServer.h --- .../CSwiftPluginServer/include/PluginServer.h | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index 680c058b695f8..d6767940f1d89 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -56,6 +56,26 @@ typedef unsigned __INT32_TYPE__ __swiftc_uint32_t; #endif #endif +#define __swiftc_join3(a,b,c) a ## b ## c + +#define __swiftc_intn_t(n) __swiftc_join3(__swiftc_int, n, _t) +#define __swiftc_uintn_t(n) __swiftc_join3(__swiftc_uint, n, _t) + +#if defined(_MSC_VER) && !defined(__clang__) +#if defined(_WIN64) +typedef __swiftc_int64_t SwiftInt; +typedef __swiftc_uint64_t SwiftUInt; +#elif defined(_WIN32) +typedef __swiftc_int32_t SwiftInt; +typedef __swiftc_uint32_t SwiftUInt; +#else +#error unknown windows pointer width +#endif +#else +typedef __swiftc_intn_t(__INTPTR_WIDTH__) SwiftInt; +typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt; +#endif + #ifdef __cplusplus extern "C" { #endif From 239f779ba7e89493f6b1a814358ff03dbd5bb544 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 11:16:32 -0700 Subject: [PATCH 3/4] Update PluginServer.h --- .../Sources/CSwiftPluginServer/include/PluginServer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index d6767940f1d89..083194647d454 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -92,10 +92,10 @@ const void *PluginServer_createConnection(const char **errorMessage); void PluginServer_destroyConnection(const void *connHandle); /// Read bytes from the IPC communication handle. -size_t PluginServer_read(const void *connHandle, void *data, size_t nbyte); +SwiftUInt PluginServer_read(const void *connHandle, void *data, SwiftUInt nbyte); /// Write bytes to the IPC communication handle. -size_t PluginServer_write(const void *connHandle, const void *data, size_t nbyte); +SwiftUInt PluginServer_write(const void *connHandle, const void *data, SwiftUInt nbyte); //===----------------------------------------------------------------------===// // Dynamic link From 683b6d7cbef8be95e4e0399c0b43107dd72506d9 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 13:26:53 -0700 Subject: [PATCH 4/4] More SwiftInt conversions --- .../Sources/CSwiftPluginServer/PluginServer.cpp | 4 ++-- .../Sources/CSwiftPluginServer/include/PluginServer.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp b/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp index b84bdeaa173f7..a689f18d8ebc8 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp @@ -118,7 +118,7 @@ void PluginServer_destroyConnection(const void *server) { delete static_cast(server); } -size_t PluginServer_read(const void *server, void *data, size_t nbyte) { +SwiftInt PluginServer_read(const void *server, void *data, SwiftUInt nbyte) { const auto *connection = static_cast(server); #if defined(_WIN32) return _read(connection->inputFD, data, nbyte); @@ -127,7 +127,7 @@ size_t PluginServer_read(const void *server, void *data, size_t nbyte) { #endif } -size_t PluginServer_write(const void *server, const void *data, size_t nbyte) { +SwiftInt PluginServer_write(const void *server, const void *data, SwiftUInt nbyte) { const auto *connection = static_cast(server); #if defined(_WIN32) return _write(connection->outputFD, data, nbyte); diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index 083194647d454..d30c48b86dbc9 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -92,10 +92,10 @@ const void *PluginServer_createConnection(const char **errorMessage); void PluginServer_destroyConnection(const void *connHandle); /// Read bytes from the IPC communication handle. -SwiftUInt PluginServer_read(const void *connHandle, void *data, SwiftUInt nbyte); +SwiftInt PluginServer_read(const void *connHandle, void *data, SwiftUInt nbyte); /// Write bytes to the IPC communication handle. -SwiftUInt PluginServer_write(const void *connHandle, const void *data, SwiftUInt nbyte); +SwiftInt PluginServer_write(const void *connHandle, const void *data, SwiftUInt nbyte); //===----------------------------------------------------------------------===// // Dynamic link