Skip to content

Commit cc8ef9d

Browse files
authored
Merge pull request #14575 from troughton/win64-int-import
[stdlib] Correctly import C Int types on Windows x64
2 parents db2de58 + 457ac78 commit cc8ef9d

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4400,6 +4400,13 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
44004400
#define MAP_BUILTIN_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
44014401
addTrivial(getIdentifier(#SWIFT_TYPE_NAME), stdlib);
44024402
#include "swift/ClangImporter/BuiltinMappedTypes.def"
4403+
4404+
// Even though we may never import types directly as Int or UInt
4405+
// (e.g. on 64-bit Windows, where CLong maps to Int32 and
4406+
// CLongLong to Int64), it's always possible to convert an Int
4407+
// or UInt to a C type.
4408+
addTrivial(getIdentifier("Int"), stdlib);
4409+
addTrivial(getIdentifier("UInt"), stdlib);
44034410
}
44044411

44054412
if (auto darwin = getLoadedModule(Id_Darwin)) {

lib/IRGen/GenClangType.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,21 @@ ClangTypeConverter::reverseBuiltinTypeMapping(IRGenModule &IGM,
315315
cacheStdlibType(#SWIFT_TYPE_NAME, clang::BuiltinType::CLANG_BUILTIN_KIND);
316316
#include "swift/ClangImporter/BuiltinMappedTypes.def"
317317

318+
// On 64-bit Windows, no C type is imported as an Int or UInt; CLong is
319+
// imported as an Int32 and CLongLong as an Int64. Therefore, manually
320+
// add mappings to C for Int and UInt.
321+
if (IGM.Triple.isOSWindows() && IGM.Triple.isArch64Bit()) {
322+
// Map UInt to uintptr_t
323+
auto swiftUIntType = getNamedSwiftType(stdlib, "UInt");
324+
auto clangUIntPtrType = ctx.getCanonicalType(ctx.getUIntPtrType());
325+
Cache.insert({swiftUIntType, clangUIntPtrType});
326+
327+
// Map Int to intptr_t
328+
auto swiftIntType = getNamedSwiftType(stdlib, "Int");
329+
auto clangIntPtrType = ctx.getCanonicalType(ctx.getIntPtrType());
330+
Cache.insert({swiftIntType, clangIntPtrType});
331+
}
332+
318333
// The above code sets up a bunch of mappings in the cache; just
319334
// assume that we hit one of them.
320335
auto it = Cache.find(type);

stdlib/public/core/CTypes.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public typealias CUnsignedShort = UInt16
2828
public typealias CUnsignedInt = UInt32
2929

3030
/// The C 'unsigned long' type.
31+
#if os(Windows) && arch(x86_64)
32+
public typealias CUnsignedLong = UInt32
33+
#else
3134
public typealias CUnsignedLong = UInt
35+
#endif
3236

3337
/// The C 'unsigned long long' type.
3438
public typealias CUnsignedLongLong = UInt64
@@ -42,21 +46,15 @@ public typealias CShort = Int16
4246
/// The C 'int' type.
4347
public typealias CInt = Int32
4448

45-
#if os(Windows) && arch(x86_64)
4649
/// The C 'long' type.
50+
#if os(Windows) && arch(x86_64)
4751
public typealias CLong = Int32
4852
#else
49-
/// The C 'long' type.
5053
public typealias CLong = Int
5154
#endif
5255

53-
#if os(Windows) && arch(x86_64)
54-
/// The C 'long long' type.
55-
public typealias CLongLong = Int
56-
#else
5756
/// The C 'long long' type.
5857
public typealias CLongLong = Int64
59-
#endif
6058

6159
/// The C 'float' type.
6260
public typealias CFloat = Float

0 commit comments

Comments
 (0)