Skip to content

Commit 37f5b43

Browse files
authored
Merge pull request #68710 from rintaro/macros-darwin-swift
[ASTGen] Use standard headers in bridging interface
2 parents d1436b3 + 7ac5d65 commit 37f5b43

File tree

13 files changed

+65
-187
lines changed

13 files changed

+65
-187
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "swift/Basic/Compiler.h"
1818
#include "swift/Basic/Nullability.h"
1919

20+
#include <stddef.h>
21+
2022
// NOTE: DO NOT #include any stdlib headers here. e.g. <stdint.h>. Those are
2123
// part of "Darwin"/"Glibc" module, so when a Swift file imports this header,
2224
// it causes importing the "Darwin"/"Glibc" overlay module. That violates
@@ -33,12 +35,12 @@ SWIFT_BEGIN_ASSUME_NONNULL
3335

3436
typedef struct {
3537
const unsigned char *_Nullable data;
36-
SwiftInt length;
38+
size_t length;
3739
} BridgedString;
3840

3941
typedef struct {
4042
const void *_Nullable data;
41-
SwiftInt numElements;
43+
size_t numElements;
4244
} BridgedArrayRef;
4345

4446
typedef struct BridgedASTContext {
@@ -64,7 +66,7 @@ typedef struct BridgedIdentifier {
6466

6567
typedef struct {
6668
void *start;
67-
SwiftInt byteLength;
69+
size_t byteLength;
6870
} BridgedCharSourceRange;
6971

7072
typedef struct {
@@ -78,7 +80,7 @@ typedef struct {
7880
BridgedSourceLoc TrailingCommaLoc;
7981
} BridgedTupleTypeElement;
8082

81-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : SwiftInt {
83+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : size_t {
8284
/// A type bound T : P, where T is a type that depends on a generic
8385
/// parameter and P is some type that should bound T, either as a concrete
8486
/// supertype or a protocol to which T must conform.
@@ -105,7 +107,7 @@ typedef struct {
105107
} BridgedRequirementRepr;
106108

107109
/// Diagnostic severity when reporting diagnostics.
108-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : SwiftInt {
110+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : size_t {
109111
BridgedFatalError,
110112
BridgedError,
111113
BridgedWarning,
@@ -121,7 +123,7 @@ typedef struct BridgedDiagnosticEngine {
121123
void *raw;
122124
} BridgedDiagnosticEngine;
123125

124-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : SwiftInt {
126+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : size_t {
125127
/// An expanded macro.
126128
BridgedExpandedMacro = 0,
127129
/// An external macro, spelled with either the old spelling (Module.Type)
@@ -132,7 +134,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : SwiftInt
132134
} BridgedMacroDefinitionKind;
133135

134136
/// Bridged parameter specifiers
135-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : SwiftInt {
137+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : size_t {
136138
BridgedAttributedTypeSpecifierInOut,
137139
BridgedAttributedTypeSpecifierBorrowing,
138140
BridgedAttributedTypeSpecifierConsuming,
@@ -143,7 +145,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : Swif
143145
} BridgedAttributedTypeSpecifier;
144146

145147
// Bridged type attribute kinds, which mirror TypeAttrKind exactly.
146-
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : SwiftInt {
148+
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : size_t {
147149
BridgedTypeAttrKind_autoclosure,
148150
BridgedTypeAttrKind_convention,
149151
BridgedTypeAttrKind_noescape,
@@ -195,7 +197,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : SwiftInt {
195197
BridgedTypeAttrKind_Count
196198
} BridgedTypeAttrKind;
197199

198-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : SwiftInt {
200+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : size_t {
199201
ASTNodeKindExpr,
200202
ASTNodeKindStmt,
201203
ASTNodeKindDecl
@@ -325,7 +327,7 @@ void *IfStmt_create(BridgedASTContext cContext, BridgedSourceLoc cIfLoc,
325327
void *BraceStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLBLoc,
326328
BridgedArrayRef elements, BridgedSourceLoc cRBLoc);
327329

328-
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, SwiftInt len);
330+
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, size_t len);
329331

330332
SWIFT_NAME("ParamDecl_create(astContext:declContext:specifierLoc:firstName:"
331333
"firstNameLoc:secondName:secondNameLoc:type:defaultValue:)")
@@ -545,7 +547,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
545547
BridgedIdentifier cName,
546548
BridgedSourceLoc cNameLoc,
547549
void *_Nullable opaqueInheritedType,
548-
SwiftInt index);
550+
size_t index);
549551

550552
SWIFT_NAME(
551553
"TrailingWhereClause_create(astContext:whereKeywordLoc:requirements:)")

include/swift/Basic/CBasicBridging.h

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,8 @@
1616
#include "swift/Basic/Compiler.h"
1717
#include "swift/Basic/Nullability.h"
1818

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

8722
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
8823

@@ -103,7 +38,7 @@ SWIFT_BEGIN_ASSUME_NONNULL
10338

10439
typedef struct BridgedData {
10540
const char *_Nullable baseAddress;
106-
SwiftUInt size;
41+
size_t size;
10742
} BridgedData;
10843

10944
void BridgedData_free(BridgedData data);
@@ -132,31 +67,31 @@ _Bool JSON_value_getAsNull(void *valuePtr);
13267
_Bool JSON_value_getAsBoolean(void *valuePtr, _Bool *result);
13368
_Bool JSON_value_getAsString(void *valuePtr, BridgedData *result);
13469
_Bool JSON_value_getAsDouble(void *valuePtr, double *result);
135-
_Bool JSON_value_getAsInteger(void *valuePtr, long long *result);
70+
_Bool JSON_value_getAsInteger(void *valuePtr, int64_t *result);
13671
_Bool JSON_value_getAsObject(void *valuePtr, void *_Nullable *_Nonnull result);
13772
_Bool JSON_value_getAsArray(void *valuePtr, void *_Nullable *_Nonnull result);
13873

139-
unsigned long JSON_object_getSize(void *objectPtr);
140-
BridgedData JSON_object_getKey(void *objectPtr, unsigned long i);
74+
size_t JSON_object_getSize(void *objectPtr);
75+
BridgedData JSON_object_getKey(void *objectPtr, size_t i);
14176
_Bool JSON_object_hasKey(void *objectPtr, const char *key);
14277
void *JSON_object_getValue(void *objectPtr, const char *key);
14378

144-
long long JSON_array_getSize(void *arrayPtr);
145-
void *JSON_array_getValue(void *arrayPtr, long long index);
79+
size_t JSON_array_getSize(void *arrayPtr);
80+
void *JSON_array_getValue(void *arrayPtr, size_t index);
14681

14782
void JSON_value_emplaceNull(void *valuePtr);
14883
void JSON_value_emplaceBoolean(void *valuePtr, _Bool value);
14984
void JSON_value_emplaceString(void *valuePtr, const char *value);
15085
void JSON_value_emplaceDouble(void *valuePtr, double value);
151-
void JSON_value_emplaceInteger(void *valuePtr, long long value);
86+
void JSON_value_emplaceInteger(void *valuePtr, int64_t value);
15287
void *JSON_value_emplaceNewObject(void *valuePtr);
15388
void *JSON_value_emplaceNewArray(void *valuePtr);
15489

15590
void JSON_object_setNull(void *objectPtr, const char *key);
15691
void JSON_object_setBoolean(void *objectPtr, const char *key, _Bool value);
15792
void JSON_object_setString(void *objectPtr, const char *key, const char *value);
15893
void JSON_object_setDouble(void *objectPtr, const char *key, double value);
159-
void JSON_object_setInteger(void *objectPtr, const char *key, long long value);
94+
void JSON_object_setInteger(void *objectPtr, const char *key, int64_t value);
16095
void *JSON_object_setNewObject(void *objectPtr, const char *key);
16196
void *JSON_object_setNewArray(void *objectPtr, const char *key);
16297
void *JSON_object_setNewValue(void *objectPtr, const char *key);
@@ -165,7 +100,7 @@ void JSON_array_pushNull(void *arrayPtr);
165100
void JSON_array_pushBoolean(void *arrayPtr, _Bool value);
166101
void JSON_array_pushString(void *arrayPtr, const char *value);
167102
void JSON_array_pushDouble(void *arrayPtr, double value);
168-
void JSON_array_pushInteger(void *arrayPtr, long long value);
103+
void JSON_array_pushInteger(void *arrayPtr, int64_t value);
169104
void *JSON_array_pushNewObject(void *arrayPtr);
170105
void *JSON_array_pushNewArray(void *arrayPtr);
171106
void *JSON_array_pushNewValue(void *arrayPtr);

lib/AST/CASTBridging.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ bool ASTContext_langOptsHasFeature(BridgedASTContext cContext,
192192
return convertASTContext(cContext).LangOpts.hasFeature((Feature)feature);
193193
}
194194

195-
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, SwiftInt len) {
195+
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, size_t len) {
196196
SourceLoc loc = convertSourceLoc(cLoc).getAdvancedLoc(len);
197197
return {loc.getOpaquePointerValue()};
198198
}
@@ -1120,7 +1120,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
11201120
BridgedIdentifier cName,
11211121
BridgedSourceLoc cNameLoc,
11221122
void *_Nullable opaqueInheritedType,
1123-
SwiftInt index) {
1123+
size_t index) {
11241124
auto eachLoc = convertSourceLoc(cEachLoc);
11251125
auto *decl = GenericTypeParamDecl::createParsed(
11261126
convertDeclContext(cDeclContext), convertIdentifier(cName),
@@ -1265,6 +1265,6 @@ bool Plugin_waitForNextMessage(PluginHandle handle, BridgedData *out) {
12651265
auto size = message.size();
12661266
auto outPtr = malloc(size);
12671267
memcpy(outPtr, message.data(), size);
1268-
*out = BridgedData{(const char *)outPtr, (SwiftUInt)size};
1268+
*out = BridgedData{(const char *)outPtr, size};
12691269
return false;
12701270
}

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ extension Collection {
412412
}
413413
}
414414

415-
return .init(data: baseAddress, numElements: SwiftInt(self.count))
415+
return .init(data: baseAddress, numElements: self.count)
416416
}
417417
}
418418

@@ -426,7 +426,7 @@ extension LazyCollectionProtocol {
426426
let buffer = astgen.allocator.allocate(Element.self, count: self.count)
427427
_ = buffer.initialize(from: self)
428428

429-
return .init(data: buffer.baseAddress, numElements: SwiftInt(self.count))
429+
return .init(data: buffer.baseAddress, numElements: self.count)
430430
}
431431
}
432432

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension BridgedSourceLoc {
2121
in buffer: UnsafeBufferPointer<UInt8>
2222
) {
2323
precondition(position.utf8Offset >= 0 && position.utf8Offset <= buffer.count)
24-
self = SourceLoc_advanced(BridgedSourceLoc(raw: buffer.baseAddress!), SwiftInt(position.utf8Offset))
24+
self = SourceLoc_advanced(BridgedSourceLoc(raw: buffer.baseAddress!), position.utf8Offset)
2525
}
2626
}
2727

@@ -35,7 +35,7 @@ extension BridgedSourceRange {
3535
extension String {
3636
mutating func withBridgedString<R>(_ body: (BridgedString) throws -> R) rethrows -> R {
3737
try withUTF8 { buffer in
38-
try body(BridgedString(data: buffer.baseAddress, length: SwiftInt(buffer.count)))
38+
try body(BridgedString(data: buffer.baseAddress, length: buffer.count))
3939
}
4040
}
4141
}

lib/ASTGen/Sources/ASTGen/Generics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension ASTGenVisitor {
3737
name: name,
3838
nameLoc: nameLoc,
3939
inheritedType: self.generate(node.inheritedType)?.rawValue,
40-
index: SwiftInt(genericParameterIndex)
40+
index: genericParameterIndex
4141
)
4242
)
4343
}

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct CompilerPlugin {
121121

122122
private func sendMessage(_ message: HostToPluginMessage) throws {
123123
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
124-
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: SwiftUInt(data.count)))
124+
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: data.count))
125125
}
126126
if hadError {
127127
throw PluginError.failedToSendMessage
@@ -341,7 +341,7 @@ class PluginDiagnosticsEngine {
341341
guard let bufferBaseAddress = exportedSourceFile.pointee.buffer.baseAddress else {
342342
return nil
343343
}
344-
return SourceLoc_advanced(BridgedSourceLoc(raw: bufferBaseAddress), SwiftInt(offset))
344+
return SourceLoc_advanced(BridgedSourceLoc(raw: bufferBaseAddress), offset)
345345
}
346346

347347
/// C++ source location from a position value from a plugin.

lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public struct LLVMJSON {
3535
JSON_value_serialize(valuePtr, &data)
3636
assert(data.baseAddress != nil)
3737
defer { BridgedData_free(data) }
38-
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: Int(data.size))
38+
let buffer = UnsafeBufferPointer(start: data.baseAddress, count: data.size)
3939
return try body(buffer)
4040
}
4141

4242
/// Decode a JSON data to a Swift value.
4343
public static func decode<T: Decodable>(_ type: T.Type, from json: UnsafeBufferPointer<Int8>) throws -> T {
44-
let data = BridgedData(baseAddress: json.baseAddress, size: SwiftUInt(json.count))
44+
let data = BridgedData(baseAddress: json.baseAddress, size: json.count)
4545
let valuePtr = JSON_deserializedValue(data)
4646
defer { JSON_value_delete(valuePtr) }
4747

@@ -209,7 +209,7 @@ extension LLVMJSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
209209
var allKeys: [Key] {
210210
var keys: [Key] = []
211211
let size = JSON_object_getSize(objectPtr)
212-
keys.reserveCapacity(Int(size))
212+
keys.reserveCapacity(size)
213213
for i in 0 ..< size {
214214
let keyData = JSON_object_getKey(objectPtr, i)
215215
if let key = Key(stringValue: String(keyData)) {

0 commit comments

Comments
 (0)