Skip to content

Commit 3e4dd43

Browse files
authored
Merge pull request #68278 from DougGregor/swift-int-type-bridging
2 parents 176bf34 + eaa856d commit 3e4dd43

File tree

10 files changed

+96
-33
lines changed

10 files changed

+96
-33
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@
2525
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2626
SWIFT_BEGIN_ASSUME_NONNULL
2727

28-
typedef long SwiftInt;
29-
typedef unsigned long SwiftUInt;
30-
3128
typedef struct {
3229
const unsigned char *_Nullable data;
33-
long length;
30+
SwiftInt length;
3431
} BridgedString;
3532

3633
typedef struct {
3734
const void *_Nullable data;
38-
long numElements;
35+
SwiftInt numElements;
3936
} BridgedArrayRef;
4037

4138
typedef struct BridgedASTContext {
@@ -70,7 +67,7 @@ typedef struct {
7067
BridgedSourceLoc TrailingCommaLoc;
7168
} BridgedTupleTypeElement;
7269

73-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : long {
70+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : SwiftInt {
7471
/// A type bound T : P, where T is a type that depends on a generic
7572
/// parameter and P is some type that should bound T, either as a concrete
7673
/// supertype or a protocol to which T must conform.
@@ -97,7 +94,7 @@ typedef struct {
9794
} BridgedRequirementRepr;
9895

9996
/// Diagnostic severity when reporting diagnostics.
100-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : long {
97+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : SwiftInt {
10198
BridgedFatalError,
10299
BridgedError,
103100
BridgedWarning,
@@ -113,7 +110,7 @@ typedef struct BridgedDiagnosticEngine {
113110
void *raw;
114111
} BridgedDiagnosticEngine;
115112

116-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long {
113+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : SwiftInt {
117114
/// An expanded macro.
118115
BridgedExpandedMacro = 0,
119116
/// An external macro, spelled with either the old spelling (Module.Type)
@@ -124,7 +121,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long {
124121
} BridgedMacroDefinitionKind;
125122

126123
/// Bridged parameter specifiers
127-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : long {
124+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : SwiftInt {
128125
BridgedAttributedTypeSpecifierInOut,
129126
BridgedAttributedTypeSpecifierBorrowing,
130127
BridgedAttributedTypeSpecifierConsuming,
@@ -135,7 +132,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : long
135132
} BridgedAttributedTypeSpecifier;
136133

137134
// Bridged type attribute kinds, which mirror TypeAttrKind exactly.
138-
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long {
135+
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : SwiftInt {
139136
BridgedTypeAttrKind_autoclosure,
140137
BridgedTypeAttrKind_convention,
141138
BridgedTypeAttrKind_noescape,
@@ -187,7 +184,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long {
187184
BridgedTypeAttrKind_Count
188185
} BridgedTypeAttrKind;
189186

190-
typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : long {
187+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : SwiftInt {
191188
ASTNodeKindExpr,
192189
ASTNodeKindStmt,
193190
ASTNodeKindDecl
@@ -307,7 +304,7 @@ void *IfStmt_create(BridgedASTContext cContext, BridgedSourceLoc cIfLoc,
307304
void *BraceStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLBLoc,
308305
BridgedArrayRef elements, BridgedSourceLoc cRBLoc);
309306

310-
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len);
307+
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, SwiftInt len);
311308

312309
void *ParamDecl_create(BridgedASTContext cContext, BridgedSourceLoc cLoc,
313310
BridgedSourceLoc cArgLoc, BridgedIdentifier argName,
@@ -357,7 +354,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
357354
BridgedDeclContext cDeclContext,
358355
BridgedIdentifier name,
359356
BridgedSourceLoc cNameLoc,
360-
BridgedSourceLoc cEachLoc, long index,
357+
BridgedSourceLoc cEachLoc, SwiftInt index,
361358
_Bool isParameterPack);
362359
void GenericTypeParamDecl_setInheritedType(BridgedASTContext cContext,
363360
void *Param, void *ty);

include/swift/Basic/CBasicBridging.h

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,69 @@
2121
// it causes importing the "Darwin"/"Glibc" overlay module. That violates
2222
// layering. i.e. Darwin overlay is created by Swift compiler.
2323

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__)
44+
#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
86+
2487
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2588

2689
#ifdef __cplusplus
@@ -40,7 +103,7 @@ SWIFT_BEGIN_ASSUME_NONNULL
40103

41104
typedef struct BridgedData {
42105
const char *_Nullable baseAddress;
43-
unsigned long size;
106+
SwiftUInt size;
44107
} BridgedData;
45108

46109
void BridgedData_free(BridgedData data);

lib/AST/CASTBridging.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void *ImportDecl_create(BridgedASTContext cContext,
208208
std::move(importPath).get());
209209
}
210210

211-
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len) {
211+
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, SwiftInt len) {
212212
SourceLoc loc = convertSourceLoc(cLoc).getAdvancedLoc(len);
213213
return {loc.getOpaquePointerValue()};
214214
}
@@ -768,7 +768,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
768768
BridgedDeclContext cDeclContext,
769769
BridgedIdentifier name,
770770
BridgedSourceLoc cNameLoc,
771-
BridgedSourceLoc cEachLoc, long index,
771+
BridgedSourceLoc cEachLoc, SwiftInt index,
772772
bool isParameterPack) {
773773
return GenericTypeParamDecl::createParsed(
774774
convertDeclContext(cDeclContext), convertIdentifier(name),
@@ -885,6 +885,6 @@ bool Plugin_waitForNextMessage(PluginHandle handle, BridgedData *out) {
885885
auto size = message.size();
886886
auto outPtr = malloc(size);
887887
memcpy(outPtr, message.data(), size);
888-
*out = BridgedData{(const char *)outPtr, (unsigned long)size};
888+
*out = BridgedData{(const char *)outPtr, (SwiftUInt)size};
889889
return false;
890890
}

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CASTBridging
2+
import CBasicBridging
23
import SwiftParser
34

45
// Needed to use SyntaxTransformVisitor's visit method.
@@ -8,7 +9,7 @@ import SwiftSyntax
89
extension Array {
910
public func withBridgedArrayRef<T>(_ c: (BridgedArrayRef) -> T) -> T {
1011
withUnsafeBytes { buf in
11-
c(BridgedArrayRef(data: buf.baseAddress!, numElements: count))
12+
c(BridgedArrayRef(data: buf.baseAddress!, numElements: SwiftInt(count)))
1213
}
1314
}
1415
}

lib/ASTGen/Sources/ASTGen/Generics.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CASTBridging
2+
import CBasicBridging
23
import SwiftParser
34

45
// Needed to use SyntaxTransformVisitor's visit method.
@@ -37,7 +38,7 @@ extension ASTGenVisitor {
3738

3839
return .decl(
3940
GenericTypeParamDecl_create(
40-
self.ctx, self.declContext, name, nameLoc, eachLoc, genericParameterIndex,
41+
self.ctx, self.declContext, name, nameLoc, eachLoc, SwiftInt(genericParameterIndex),
4142
eachLoc.raw != nil))
4243
}
4344
}

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func checkMacroDefinition(
286286
if module == "Builtin" {
287287
switch type {
288288
case "ExternalMacro":
289-
return BridgedMacroDefinitionKind.builtinExternalMacro.rawValue
289+
return Int(BridgedMacroDefinitionKind.builtinExternalMacro.rawValue)
290290

291291
default:
292292
// Warn about the unknown builtin.
@@ -331,7 +331,7 @@ func checkMacroDefinition(
331331
]
332332
)
333333
)
334-
return BridgedMacroDefinitionKind.externalMacro.rawValue
334+
return Int(BridgedMacroDefinitionKind.externalMacro.rawValue)
335335

336336
case let .expansion(expansionSyntax, replacements: _)
337337
where expansionSyntax.macroName.text == "externalMacro":
@@ -370,7 +370,7 @@ func checkMacroDefinition(
370370
// Form the "ModuleName.TypeName" result string.
371371
(externalMacroPointer.pointee, externalMacroLength.pointee) =
372372
allocateUTF8String("\(module).\(type)", nullTerminated: true)
373-
return BridgedMacroDefinitionKind.externalMacro.rawValue
373+
return Int(BridgedMacroDefinitionKind.externalMacro.rawValue)
374374

375375
case let .expansion(expansionSyntax, replacements: replacements):
376376
// Provide the expansion syntax.
@@ -381,7 +381,7 @@ func checkMacroDefinition(
381381

382382
// If there are no replacements, we're done.
383383
if replacements.isEmpty {
384-
return BridgedMacroDefinitionKind.expandedMacro.rawValue
384+
return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue)
385385
}
386386

387387
// The replacements are triples: (startOffset, endOffset, parameter index).
@@ -396,7 +396,7 @@ func checkMacroDefinition(
396396

397397
replacementsPtr.pointee = replacementBuffer.baseAddress
398398
numReplacementsPtr.pointee = replacements.count
399-
return BridgedMacroDefinitionKind.expandedMacro.rawValue
399+
return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue)
400400
}
401401
} catch let errDiags as DiagnosticsError {
402402
let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr)

lib/ASTGen/Sources/ASTGen/Misc.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CASTBridging
2+
import CBasicBridging
23
import SwiftParser
34

45
// Needed to use SyntaxTransformVisitor's visit method.
@@ -65,7 +66,7 @@ extension BridgedSourceLoc {
6566
) {
6667
if let start = buffer.baseAddress,
6768
position.utf8Offset >= 0 && position.utf8Offset < buffer.count {
68-
self = SourceLoc_advanced(BridgedSourceLoc(raw: start), position.utf8Offset)
69+
self = SourceLoc_advanced(BridgedSourceLoc(raw: start), SwiftInt(position.utf8Offset))
6970
} else {
7071
self = nil
7172
}
@@ -75,7 +76,7 @@ extension BridgedSourceLoc {
7576
extension String {
7677
mutating func withBridgedString<R>(_ body: (BridgedString) throws -> R) rethrows -> R {
7778
try withUTF8 { buffer in
78-
try body(BridgedString(data: buffer.baseAddress, length: buffer.count))
79+
try body(BridgedString(data: buffer.baseAddress, length: SwiftInt(buffer.count)))
7980
}
8081
}
8182
}

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: UInt(data.count)))
124+
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: SwiftUInt(data.count)))
125125
}
126126
if hadError {
127127
throw PluginError.failedToSendMessage
@@ -339,7 +339,7 @@ class PluginDiagnosticsEngine {
339339
guard let bufferBaseAddress = exportedSourceFile.pointee.buffer.baseAddress else {
340340
return nil
341341
}
342-
return SourceLoc_advanced(BridgedSourceLoc(raw: bufferBaseAddress), offset)
342+
return SourceLoc_advanced(BridgedSourceLoc(raw: bufferBaseAddress), SwiftInt(offset))
343343
}
344344

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

lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct LLVMJSON {
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: UInt(json.count))
44+
let data = BridgedData(baseAddress: json.baseAddress, size: SwiftUInt(json.count))
4545
let valuePtr = JSON_deserializedValue(data)
4646
defer { JSON_value_delete(valuePtr) }
4747

stdlib/public/SwiftShims/swift/shims/SwiftStdint.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ typedef unsigned __INT8_TYPE__ __swift_uint8_t;
7171
#define __swift_uintn_t(n) __swift_join3(__swift_uint, n, _t)
7272

7373
#if defined(_MSC_VER) && !defined(__clang__)
74-
#if defined(_WIN32)
75-
typedef __swift_int32_t __swift_intptr_t;
76-
typedef __swift_uint32_t __swift_uintptr_t;
77-
#elif defined(_WIN64)
74+
#if defined(_WIN64)
7875
typedef __swift_int64_t __swift_intptr_t;
7976
typedef __swift_uint64_t __swift_uintptr_t;
77+
#elif defined(_WIN32)
78+
typedef __swift_int32_t __swift_intptr_t;
79+
typedef __swift_uint32_t __swift_uintptr_t;
8080
#else
8181
#error unknown windows pointer width
8282
#endif

0 commit comments

Comments
 (0)