Skip to content

Commit 435d0a6

Browse files
committed
[interop][SwiftToCxx] ensure swift::Int and swift::UInt are usable in generic context
Fixes #63452 (cherry picked from commit 85a431e)
1 parent 5550394 commit 435d0a6

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

lib/PrintAsClang/PrintSwiftToClangCoreScaffold.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/Type.h"
2020
#include "swift/IRGen/IRABIDetailsProvider.h"
2121
#include "swift/IRGen/Linking.h"
22+
#include "clang/Basic/TargetInfo.h"
2223
#include "llvm/ADT/STLExtras.h"
2324

2425
using namespace swift;
@@ -164,9 +165,26 @@ void printPrimitiveGenericTypeTraits(raw_ostream &os, ASTContext &astContext,
164165

165166
// Pointer types.
166167
// FIXME: support raw pointers?
167-
astContext.getOpaquePointerType()};
168-
169-
for (Type type : llvm::makeArrayRef(supportedPrimitiveTypes)) {
168+
astContext.getOpaquePointerType(),
169+
170+
astContext.getIntType(), astContext.getUIntType()};
171+
172+
auto primTypesArray = llvm::makeArrayRef(supportedPrimitiveTypes);
173+
174+
// Ensure that `long` and `unsigned long` are treated as valid
175+
// generic Swift types (`Int` and `UInt`) on platforms
176+
// that do define `Int`/`ptrdiff_t` as `long` and don't define `int64_t` to be
177+
// `long`.
178+
auto &clangTI =
179+
astContext.getClangModuleLoader()->getClangASTContext().getTargetInfo();
180+
bool isSwiftIntLong =
181+
clangTI.getPtrDiffType(0) == clang::TransferrableTargetInfo::SignedLong;
182+
bool isInt64Long =
183+
clangTI.getInt64Type() == clang::TransferrableTargetInfo::SignedLong;
184+
if (!(isSwiftIntLong && !isInt64Long))
185+
primTypesArray = primTypesArray.drop_back(2);
186+
187+
for (Type type : primTypesArray) {
170188
auto typeInfo = *typeMapping.getKnownCxxTypeInfo(
171189
type->getNominalOrBoundGenericNominal());
172190

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Core -clang-header-expose-decls=all-public -emit-clang-header-path %t/core.h
3+
// RUN: %FileCheck %s < %t/core.h
4+
5+
// RUN: %check-interop-cxx-header-in-clang(%t/core.h)
6+
7+
// REQUIRES: PTRSIZE=64
8+
// REQUIRES: OS=macosx
9+
10+
// CHECK: // type metadata address for Bool.
11+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $sSbN;
12+
// CHECK-NEXT: // type metadata address for Int8.
13+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss4Int8VN;
14+
// CHECK-NEXT: // type metadata address for UInt8.
15+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss5UInt8VN;
16+
// CHECK-NEXT: // type metadata address for Int16.
17+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss5Int16VN;
18+
// CHECK-NEXT: // type metadata address for UInt16.
19+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss6UInt16VN;
20+
// CHECK-NEXT: // type metadata address for Int32.
21+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss5Int32VN;
22+
// CHECK-NEXT: // type metadata address for UInt32.
23+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss6UInt32VN;
24+
// CHECK-NEXT: // type metadata address for Int64.
25+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss5Int64VN;
26+
// CHECK-NEXT: // type metadata address for UInt64.
27+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss6UInt64VN;
28+
// CHECK-NEXT: // type metadata address for Float.
29+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $sSfN;
30+
// CHECK-NEXT: // type metadata address for Double.
31+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $sSdN;
32+
// CHECK-NEXT: // type metadata address for OpaquePointer.
33+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss13OpaquePointerVN;
34+
// CHECK-NEXT: // type metadata address for Int.
35+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $sSiN;
36+
// CHECK-NEXT: // type metadata address for UInt.
37+
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $sSuN;
38+
// CHECK-EMPTY:
39+
// CHECK-NEXT: #ifdef __cplusplus
40+
// CHECK-NEXT: }
41+
// CHECK-NEXT: #endif
42+
43+
// CHECK: static inline const constexpr bool isUsableInGenericContext<void *> = true;
44+
// CHECK-EMPTY:
45+
// CHECK-NEXT: template<>
46+
// CHECK-NEXT: struct TypeMetadataTrait<void *> {
47+
// CHECK-NEXT: static SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() {
48+
// CHECK-NEXT: return &_impl::$ss13OpaquePointerVN;
49+
// CHECK-NEXT: }
50+
// CHECK-NEXT: };
51+
// CHECK-EMPTY:
52+
// CHECK-NEXT: template<>
53+
// CHECK-NEXT: static inline const constexpr bool isUsableInGenericContext<swift::Int> = true;
54+
// CHECK-EMPTY:
55+
// CHECK-NEXT: template<>
56+
// CHECK-NEXT: struct TypeMetadataTrait<swift::Int> {
57+
// CHECK-NEXT: static SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() {
58+
// CHECK-NEXT: return &_impl::$sSiN;
59+
// CHECK-NEXT: }
60+
// CHECK-NEXT: };
61+
// CHECK-EMPTY:
62+
// CHECK-NEXT: template<>
63+
// CHECK-NEXT: static inline const constexpr bool isUsableInGenericContext<swift::UInt> = true;
64+
// CHECK-EMPTY:
65+
// CHECK-NEXT: template<>
66+
// CHECK-NEXT: struct TypeMetadataTrait<swift::UInt> {
67+
// CHECK-NEXT: static SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() {
68+
// CHECK-NEXT: return &_impl::$sSuN;
69+
// CHECK-NEXT: }
70+
// CHECK-NEXT: };
71+
// CHECK-EMPTY:
72+
// CHECK-NEXT: #pragma clang diagnostic pop
73+
// CHECK-EMPTY:
74+
// CHECK-NEXT: } // namespace swift
75+

test/Interop/SwiftToCxx/core/swift-impl-defs-in-cxx.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@
9393
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $sSdN;
9494
// CHECK-NEXT: // type metadata address for OpaquePointer.
9595
// CHECK-NEXT: SWIFT_IMPORT_STDLIB_SYMBOL extern size_t $ss13OpaquePointerVN;
96-
// CHECK-EMPTY:
97-
// CHECK-NEXT: #ifdef __cplusplus
96+
// CHECK: #ifdef __cplusplus
9897
// CHECK-NEXT: }
9998
// CHECK-NEXT: #endif
10099
// CHECK-EMPTY:
@@ -224,7 +223,7 @@
224223
// CHECK-NEXT: }
225224
// CHECK-NEXT: };
226225
// CHECK-EMPTY:
227-
// CHECK-NEXT: #pragma clang diagnostic pop
226+
// CHECK: #pragma clang diagnostic pop
228227
// CHECK-EMPTY:
229228
// CHECK-NEXT: } // namespace swift
230229
// CHECK-EMPTY:

test/Interop/SwiftToCxx/stdlib/stdlib-dep-inline-in-cxx.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ public func test() -> String {
1717
return ""
1818
}
1919

20+
@_expose(Cxx)
21+
public func testIntArray() -> [Int] {
22+
return []
23+
}
24+
2025
// CHECK: namespace swift SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("swift") {
2126
// CHECK: class SWIFT_SYMBOL("{{.*}}") String final {

0 commit comments

Comments
 (0)