Skip to content

[Swift bridging] Consistently use correctly-defined Swift(U)Int for bridging #68278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions include/swift/AST/CASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
SWIFT_BEGIN_ASSUME_NONNULL

typedef long SwiftInt;
typedef unsigned long SwiftUInt;

typedef struct {
const unsigned char *_Nullable data;
long length;
SwiftInt length;
} BridgedString;

typedef struct {
const void *_Nullable data;
long numElements;
SwiftInt numElements;
} BridgedArrayRef;

typedef struct BridgedASTContext {
Expand Down Expand Up @@ -70,7 +67,7 @@ typedef struct {
BridgedSourceLoc TrailingCommaLoc;
} BridgedTupleTypeElement;

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

/// Diagnostic severity when reporting diagnostics.
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : long {
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : SwiftInt {
BridgedFatalError,
BridgedError,
BridgedWarning,
Expand All @@ -113,7 +110,7 @@ typedef struct BridgedDiagnosticEngine {
void *raw;
} BridgedDiagnosticEngine;

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

/// Bridged parameter specifiers
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : long {
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : SwiftInt {
BridgedAttributedTypeSpecifierInOut,
BridgedAttributedTypeSpecifierBorrowing,
BridgedAttributedTypeSpecifierConsuming,
Expand All @@ -135,7 +132,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : long
} BridgedAttributedTypeSpecifier;

// Bridged type attribute kinds, which mirror TypeAttrKind exactly.
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long {
typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : SwiftInt {
BridgedTypeAttrKind_autoclosure,
BridgedTypeAttrKind_convention,
BridgedTypeAttrKind_noescape,
Expand Down Expand Up @@ -187,7 +184,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long {
BridgedTypeAttrKind_Count
} BridgedTypeAttrKind;

typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : long {
typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : SwiftInt {
ASTNodeKindExpr,
ASTNodeKindStmt,
ASTNodeKindDecl
Expand Down Expand Up @@ -307,7 +304,7 @@ void *IfStmt_create(BridgedASTContext cContext, BridgedSourceLoc cIfLoc,
void *BraceStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLBLoc,
BridgedArrayRef elements, BridgedSourceLoc cRBLoc);

BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len);
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, SwiftInt len);

void *ParamDecl_create(BridgedASTContext cContext, BridgedSourceLoc cLoc,
BridgedSourceLoc cArgLoc, BridgedIdentifier argName,
Expand Down Expand Up @@ -357,7 +354,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
BridgedDeclContext cDeclContext,
BridgedIdentifier name,
BridgedSourceLoc cNameLoc,
BridgedSourceLoc cEachLoc, long index,
BridgedSourceLoc cEachLoc, SwiftInt index,
_Bool isParameterPack);
void GenericTypeParamDecl_setInheritedType(BridgedASTContext cContext,
void *Param, void *ty);
Expand Down
65 changes: 64 additions & 1 deletion include/swift/Basic/CBasicBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,69 @@
// it causes importing the "Darwin"/"Glibc" overlay module. That violates
// layering. i.e. Darwin overlay is created by Swift compiler.

// 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 <stdint.h>
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

#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

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

#ifdef __cplusplus
Expand All @@ -40,7 +103,7 @@ SWIFT_BEGIN_ASSUME_NONNULL

typedef struct BridgedData {
const char *_Nullable baseAddress;
unsigned long size;
SwiftUInt size;
} BridgedData;

void BridgedData_free(BridgedData data);
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/CASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void *ImportDecl_create(BridgedASTContext cContext,
std::move(importPath).get());
}

BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len) {
BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, SwiftInt len) {
SourceLoc loc = convertSourceLoc(cLoc).getAdvancedLoc(len);
return {loc.getOpaquePointerValue()};
}
Expand Down Expand Up @@ -764,7 +764,7 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
BridgedDeclContext cDeclContext,
BridgedIdentifier name,
BridgedSourceLoc cNameLoc,
BridgedSourceLoc cEachLoc, long index,
BridgedSourceLoc cEachLoc, SwiftInt index,
bool isParameterPack) {
return GenericTypeParamDecl::createParsed(
convertDeclContext(cDeclContext), convertIdentifier(name),
Expand Down Expand Up @@ -881,6 +881,6 @@ bool Plugin_waitForNextMessage(PluginHandle handle, BridgedData *out) {
auto size = message.size();
auto outPtr = malloc(size);
memcpy(outPtr, message.data(), size);
*out = BridgedData{(const char *)outPtr, (unsigned long)size};
*out = BridgedData{(const char *)outPtr, (SwiftUInt)size};
return false;
}
3 changes: 2 additions & 1 deletion lib/ASTGen/Sources/ASTGen/ASTGen.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CASTBridging
import CBasicBridging
import SwiftParser

// Needed to use SyntaxTransformVisitor's visit method.
Expand All @@ -8,7 +9,7 @@ import SwiftSyntax
extension Array {
public func withBridgedArrayRef<T>(_ c: (BridgedArrayRef) -> T) -> T {
withUnsafeBytes { buf in
c(BridgedArrayRef(data: buf.baseAddress!, numElements: count))
c(BridgedArrayRef(data: buf.baseAddress!, numElements: SwiftInt(count)))
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/ASTGen/Sources/ASTGen/Generics.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CASTBridging
import CBasicBridging
import SwiftParser

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

return .decl(
GenericTypeParamDecl_create(
self.ctx, self.declContext, name, nameLoc, eachLoc, genericParameterIndex,
self.ctx, self.declContext, name, nameLoc, eachLoc, SwiftInt(genericParameterIndex),
eachLoc.raw != nil))
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func checkMacroDefinition(
if module == "Builtin" {
switch type {
case "ExternalMacro":
return BridgedMacroDefinitionKind.builtinExternalMacro.rawValue
return Int(BridgedMacroDefinitionKind.builtinExternalMacro.rawValue)

default:
// Warn about the unknown builtin.
Expand Down Expand Up @@ -326,7 +326,7 @@ func checkMacroDefinition(
]
)
)
return BridgedMacroDefinitionKind.externalMacro.rawValue
return Int(BridgedMacroDefinitionKind.externalMacro.rawValue)

case let .expansion(expansionSyntax, replacements: _)
where expansionSyntax.macroName.text == "externalMacro":
Expand Down Expand Up @@ -365,7 +365,7 @@ func checkMacroDefinition(
// Form the "ModuleName.TypeName" result string.
(externalMacroPointer.pointee, externalMacroLength.pointee) =
allocateUTF8String("\(module).\(type)", nullTerminated: true)
return BridgedMacroDefinitionKind.externalMacro.rawValue
return Int(BridgedMacroDefinitionKind.externalMacro.rawValue)

case let .expansion(expansionSyntax, replacements: replacements):
// Provide the expansion syntax.
Expand All @@ -376,7 +376,7 @@ func checkMacroDefinition(

// If there are no replacements, we're done.
if replacements.isEmpty {
return BridgedMacroDefinitionKind.expandedMacro.rawValue
return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue)
}

// The replacements are triples: (startOffset, endOffset, parameter index).
Expand All @@ -391,7 +391,7 @@ func checkMacroDefinition(

replacementsPtr.pointee = replacementBuffer.baseAddress
numReplacementsPtr.pointee = replacements.count
return BridgedMacroDefinitionKind.expandedMacro.rawValue
return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue)
}
} catch let errDiags as DiagnosticsError {
let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
Expand Down
5 changes: 3 additions & 2 deletions lib/ASTGen/Sources/ASTGen/Misc.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CASTBridging
import CBasicBridging
import SwiftParser

// Needed to use SyntaxTransformVisitor's visit method.
Expand Down Expand Up @@ -65,7 +66,7 @@ extension BridgedSourceLoc {
) {
if let start = buffer.baseAddress,
position.utf8Offset >= 0 && position.utf8Offset < buffer.count {
self = SourceLoc_advanced(BridgedSourceLoc(raw: start), position.utf8Offset)
self = SourceLoc_advanced(BridgedSourceLoc(raw: start), SwiftInt(position.utf8Offset))
} else {
self = nil
}
Expand All @@ -75,7 +76,7 @@ extension BridgedSourceLoc {
extension String {
mutating func withBridgedString<R>(_ body: (BridgedString) throws -> R) rethrows -> R {
try withUTF8 { buffer in
try body(BridgedString(data: buffer.baseAddress, length: buffer.count))
try body(BridgedString(data: buffer.baseAddress, length: SwiftInt(buffer.count)))
}
}
}
4 changes: 2 additions & 2 deletions lib/ASTGen/Sources/ASTGen/PluginHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ struct CompilerPlugin {

private func sendMessage(_ message: HostToPluginMessage) throws {
let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: UInt(data.count)))
return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: SwiftUInt(data.count)))
}
if hadError {
throw PluginError.failedToSendMessage
Expand Down Expand Up @@ -339,7 +339,7 @@ class PluginDiagnosticsEngine {
guard let bufferBaseAddress = exportedSourceFile.pointee.buffer.baseAddress else {
return nil
}
return SourceLoc_advanced(BridgedSourceLoc(raw: bufferBaseAddress), offset)
return SourceLoc_advanced(BridgedSourceLoc(raw: bufferBaseAddress), SwiftInt(offset))
}

/// C++ source location from a position value from a plugin.
Expand Down
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct LLVMJSON {

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

Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/SwiftShims/swift/shims/SwiftStdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ typedef unsigned __INT8_TYPE__ __swift_uint8_t;
#define __swift_uintn_t(n) __swift_join3(__swift_uint, n, _t)

#if defined(_MSC_VER) && !defined(__clang__)
#if defined(_WIN32)
typedef __swift_int32_t __swift_intptr_t;
typedef __swift_uint32_t __swift_uintptr_t;
#elif defined(_WIN64)
#if defined(_WIN64)
typedef __swift_int64_t __swift_intptr_t;
typedef __swift_uint64_t __swift_uintptr_t;
#elif defined(_WIN32)
typedef __swift_int32_t __swift_intptr_t;
typedef __swift_uint32_t __swift_uintptr_t;
#else
#error unknown windows pointer width
#endif
Expand Down