Skip to content

Commit 49bc58f

Browse files
committed
swift-plugin-server: remove standard headers
Redefine the types rather than use the standard headers due to the circular dependency between Darwin and Swift.
1 parent 667a1f8 commit 49bc58f

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void PluginServer_destroyConnection(const void *server) {
118118
delete static_cast<const ConnectionHandle *>(server);
119119
}
120120

121-
size_t PluginServer_read(const void *server, void *data, size_t nbyte) {
121+
SwiftInt PluginServer_read(const void *server, void *data, SwiftUInt nbyte) {
122122
const auto *connection = static_cast<const ConnectionHandle *>(server);
123123
#if defined(_WIN32)
124124
return _read(connection->inputFD, data, nbyte);
@@ -127,7 +127,7 @@ size_t PluginServer_read(const void *server, void *data, size_t nbyte) {
127127
#endif
128128
}
129129

130-
size_t PluginServer_write(const void *server, const void *data, size_t nbyte) {
130+
SwiftInt PluginServer_write(const void *server, const void *data, SwiftUInt nbyte) {
131131
const auto *connection = static_cast<const ConnectionHandle *>(server);
132132
#if defined(_WIN32)
133133
return _write(connection->outputFD, data, nbyte);

tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,68 @@
1313
#ifndef SWIFT_PLUGINSERVER_PLUGINSERVER_H
1414
#define SWIFT_PLUGINSERVER_PLUGINSERVER_H
1515

16-
#include <stddef.h>
16+
// NOTE: Partially ported from SwiftShim's SwiftStdint.h. We cannot include
17+
// that header here because it belongs to the runtime, but we need the same
18+
// logic for interoperability with Swift code in the compiler itself.
19+
// stdint.h is provided by Clang, but it dispatches to libc's stdint.h. As a
20+
// result, using stdint.h here would pull in Darwin module (which includes
21+
// libc). This creates a dependency cycle, so we can't use stdint.h in
22+
// SwiftShims.
23+
// On Linux, the story is different. We get the error message
24+
// "/usr/include/x86_64-linux-gnu/sys/types.h:146:10: error: 'stddef.h' file not
25+
// found"
26+
// This is a known Clang/Ubuntu bug.
27+
28+
// Clang has been defining __INTxx_TYPE__ macros for a long time.
29+
// __UINTxx_TYPE__ are defined only since Clang 3.5.
30+
#if defined(_MSC_VER) && !defined(__clang__)
31+
typedef __int64 __swiftc_int64_t;
32+
typedef unsigned __int64 __swiftc_uint64_t;
33+
typedef int __swiftc_int32_t;
34+
typedef unsigned int __swiftc_uint32_t;
35+
#elif !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__)
1736
#include <stdint.h>
37+
typedef int64_t __swiftc_int64_t;
38+
typedef uint64_t __swiftc_uint64_t;
39+
typedef int32_t __swiftc_int32_t;
40+
typedef uint32_t __swiftc_uint32_t;
41+
typedef intptr_t __swiftc_intptr_t;
42+
typedef uintptr_t __swiftc_uintptr_t;
43+
#else
44+
typedef __INT64_TYPE__ __swiftc_int64_t;
45+
#ifdef __UINT64_TYPE__
46+
typedef __UINT64_TYPE__ __swiftc_uint64_t;
47+
#else
48+
typedef unsigned __INT64_TYPE__ __swiftc_uint64_t;
49+
#endif
50+
51+
typedef __INT32_TYPE__ __swiftc_int32_t;
52+
#ifdef __UINT32_TYPE__
53+
typedef __UINT32_TYPE__ __swiftc_uint32_t;
54+
#else
55+
typedef unsigned __INT32_TYPE__ __swiftc_uint32_t;
56+
#endif
57+
#endif
58+
59+
#define __swiftc_join3(a,b,c) a ## b ## c
60+
61+
#define __swiftc_intn_t(n) __swiftc_join3(__swiftc_int, n, _t)
62+
#define __swiftc_uintn_t(n) __swiftc_join3(__swiftc_uint, n, _t)
63+
64+
#if defined(_MSC_VER) && !defined(__clang__)
65+
#if defined(_WIN64)
66+
typedef __swiftc_int64_t SwiftInt;
67+
typedef __swiftc_uint64_t SwiftUInt;
68+
#elif defined(_WIN32)
69+
typedef __swiftc_int32_t SwiftInt;
70+
typedef __swiftc_uint32_t SwiftUInt;
71+
#else
72+
#error unknown windows pointer width
73+
#endif
74+
#else
75+
typedef __swiftc_intn_t(__INTPTR_WIDTH__) SwiftInt;
76+
typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt;
77+
#endif
1878

1979
#ifdef __cplusplus
2080
extern "C" {
@@ -32,10 +92,10 @@ const void *PluginServer_createConnection(const char **errorMessage);
3292
void PluginServer_destroyConnection(const void *connHandle);
3393

3494
/// Read bytes from the IPC communication handle.
35-
size_t PluginServer_read(const void *connHandle, void *data, size_t nbyte);
95+
SwiftInt PluginServer_read(const void *connHandle, void *data, SwiftUInt nbyte);
3696

3797
/// Write bytes to the IPC communication handle.
38-
size_t PluginServer_write(const void *connHandle, const void *data, size_t nbyte);
98+
SwiftInt PluginServer_write(const void *connHandle, const void *data, SwiftUInt nbyte);
3999

40100
//===----------------------------------------------------------------------===//
41101
// Dynamic link

tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ final class PluginHostConnection: MessageConnection {
172172
var ptr = buffer.baseAddress!
173173

174174
while (bytesToWrite > 0) {
175-
let writtenSize = PluginServer_write(handle, ptr, Int(bytesToWrite))
175+
let writtenSize = PluginServer_write(handle, ptr, SwiftUInt(bytesToWrite))
176176
if (writtenSize <= 0) {
177177
// error e.g. broken pipe.
178178
break
179179
}
180-
ptr = ptr.advanced(by: writtenSize)
181-
bytesToWrite -= writtenSize
180+
ptr = ptr.advanced(by: Int(writtenSize))
181+
bytesToWrite -= Int(writtenSize)
182182
}
183183
return buffer.count - bytesToWrite
184184
}
@@ -193,13 +193,13 @@ final class PluginHostConnection: MessageConnection {
193193
var ptr = buffer.baseAddress!
194194

195195
while bytesToRead > 0 {
196-
let readSize = PluginServer_read(handle, ptr, Int(bytesToRead))
196+
let readSize = PluginServer_read(handle, ptr, SwiftUInt(bytesToRead))
197197
if (readSize <= 0) {
198198
// 0: EOF (the host closed), -1: Broken pipe (the host crashed?)
199199
break;
200200
}
201-
ptr = ptr.advanced(by: readSize)
202-
bytesToRead -= readSize
201+
ptr = ptr.advanced(by: Int(readSize))
202+
bytesToRead -= Int(readSize)
203203
}
204204
return buffer.count - bytesToRead
205205
}

0 commit comments

Comments
 (0)