Skip to content

Commit 6a889c8

Browse files
authored
Merge pull request #60285 from hyp/eng/swift-shims-header
[interop][SwiftToCxx] add _SwiftCxxInteroperability.h shims header and import it for C++ header mode
2 parents d422b5c + 567bb85 commit 6a889c8

File tree

8 files changed

+74
-52
lines changed

8 files changed

+74
-52
lines changed

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
105105
"#include <cstring>\n";
106106
out << "#include <stdlib.h>\n";
107107
out << "#include <new>\n";
108-
out << "#if defined(_WIN32)\n#include <malloc.h>\n#endif\n";
108+
out << "#if __has_include(<shims/_SwiftCxxInteroperability.h>)\n";
109+
out << "#include <shims/_SwiftCxxInteroperability.h>\n";
110+
out << "#endif\n";
109111
},
110112
[&] {
111113
out << "#include <stdint.h>\n"

lib/PrintAsClang/PrintSwiftToClangCoreScaffold.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -154,28 +154,6 @@ static void printTypeMetadataResponseType(SwiftToClangInteropContext &ctx,
154154
funcSig.parameterTypes[0]);
155155
}
156156

157-
static void printOpaqueAllocFee(raw_ostream &os) {
158-
os << R"text(inline void * _Nonnull opaqueAlloc(size_t size, size_t align) noexcept {
159-
#if defined(_WIN32)
160-
void *r = _aligned_malloc(size, align);
161-
#else
162-
if (align < sizeof(void *)) align = sizeof(void *);
163-
void *r = nullptr;
164-
int res = posix_memalign(&r, align, size);
165-
(void)res;
166-
#endif
167-
return r;
168-
}
169-
inline void opaqueFree(void * _Nonnull p) noexcept {
170-
#if defined(_WIN32)
171-
_aligned_free(p);
172-
#else
173-
free(p);
174-
#endif
175-
}
176-
)text";
177-
}
178-
179157
static void printSwiftResilientStorageClass(raw_ostream &os) {
180158
auto name = cxx_synthesis::getCxxOpaqueStorageClassName();
181159
static_assert(TargetValueWitnessFlags<uint64_t>::AlignmentMask ==
@@ -302,8 +280,6 @@ void swift::printSwiftToClangCoreScaffold(SwiftToClangInteropContext &ctx,
302280
/*isCForwardDefinition=*/true);
303281
});
304282
os << "\n";
305-
printOpaqueAllocFee(os);
306-
os << "\n";
307283
printSwiftResilientStorageClass(os);
308284
printCxxNaiveException(os);
309285
});

stdlib/public/SwiftShims/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(sources
2222
UnicodeData.h
2323
Visibility.h
2424
_SwiftConcurrency.h
25+
_SwiftCxxInteroperability.h
2526
_SwiftDistributed.h
2627

2728
module.modulemap
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===--- _SwiftCxxInteroperability.h - C++ Interop support ------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Defines types and support functions required by C++ bindings generated
14+
// by the Swift compiler that allow C++ code to call Swift APIs.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
#ifndef SWIFT_CXX_INTEROPERABILITY_H
18+
#define SWIFT_CXX_INTEROPERABILITY_H
19+
20+
#ifdef __cplusplus
21+
22+
#include <cstdint>
23+
#include <stdlib.h>
24+
#if defined(_WIN32)
25+
#include <malloc.h>
26+
#endif
27+
28+
namespace swift {
29+
namespace _impl {
30+
31+
inline void *_Nonnull opaqueAlloc(size_t size, size_t align) noexcept {
32+
#if defined(_WIN32)
33+
void *r = _aligned_malloc(size, align);
34+
#else
35+
if (align < sizeof(void *))
36+
align = sizeof(void *);
37+
void *r = nullptr;
38+
int res = posix_memalign(&r, align, size);
39+
(void)res;
40+
#endif
41+
return r;
42+
}
43+
44+
inline void opaqueFree(void *_Nonnull p) noexcept {
45+
#if defined(_WIN32)
46+
_aligned_free(p);
47+
#else
48+
free(p);
49+
#endif
50+
}
51+
52+
} // namespace _impl
53+
} // namespace swift
54+
#endif
55+
56+
#endif // SWIFT_CXX_INTEROPERABILITY_H

stdlib/public/SwiftShims/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ module SwiftOverlayShims {
3030
header "LibcOverlayShims.h"
3131
export *
3232
}
33+
34+
module _SwiftCxxInteropShims {
35+
header "_SwiftCxxInteroperability.h"
36+
export *
37+
}

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,6 @@
9696
// CHECK-NEXT: }
9797
// CHECK-NEXT: #endif
9898
// CHECK-EMPTY:
99-
// CHECK-NEXT: inline void * _Nonnull opaqueAlloc(size_t size, size_t align) noexcept {
100-
// CHECK-NEXT: #if defined(_WIN32)
101-
// CHECK-NEXT: void *r = _aligned_malloc(size, align);
102-
// CHECK-NEXT: #else
103-
// CHECK-NEXT: if (align < sizeof(void *)) align = sizeof(void *);
104-
// CHECK-NEXT: void *r = nullptr;
105-
// CHECK-NEXT: int res = posix_memalign(&r, align, size);
106-
// CHECK-NEXT: (void)res;
107-
// CHECK-NEXT: #endif
108-
// CHECK-NEXT: return r;
109-
// CHECK-NEXT: }
110-
// CHECK-NEXT: inline void opaqueFree(void * _Nonnull p) noexcept {
111-
// CHECK-NEXT: #if defined(_WIN32)
112-
// CHECK-NEXT: _aligned_free(p);
113-
// CHECK-NEXT: #else
114-
// CHECK-NEXT: free(p);
115-
// CHECK-NEXT: #endif
116-
// CHECK-NEXT: }
117-
// CHECK-EMPTY:
11899
// CHECK-NEXT: /// Container for an opaque Swift value, like resilient struct.
119100
// CHECK-NEXT: class OpaqueStorage {
120101
// CHECK-NEXT: public:

test/Interop/lit.local.cfg

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ def get_target_os():
66
(run_cpu, run_vendor, run_os, run_version) = re.match('([^-]+)-([^-]+)-([^0-9]+)(.*)', config.variant_triple).groups()
77
return run_os
88

9-
clang_opt = ''
9+
clang_compile_opt = '-I' + config.swiftlib_dir + ' '
10+
clang_opt = clang_compile_opt
1011

1112
if get_target_os() in ['windows-msvc']:
1213
config.substitutions.insert(0, ('%target-abi', 'WIN'))
1314
# Clang should build object files with link settings equivalent to -libc MD
1415
# when building for the MSVC target.
15-
clang_opt = '-D_MT -D_DLL -Xclang --dependent-lib=msvcrt -Xclang --dependent-lib=oldnames '
16+
clang_opt = clang_compile_opt + '-D_MT -D_DLL -Xclang --dependent-lib=msvcrt -Xclang --dependent-lib=oldnames '
1617
else:
1718
# FIXME(compnerd) do all the targets we currently support use SysV ABI?
1819
config.substitutions.insert(0, ('%target-abi', 'SYSV'))
@@ -29,13 +30,13 @@ config.substitutions.insert(0, ('%target-interop-build-clangxx', '%target-clangx
2930

3031
# Test parsing of the generated C++ header in different C++ language modes.
3132
config.substitutions.insert(0, ('%check-interop-cxx-header-in-clang\(([^)]+)\)',
32-
SubstituteCaptures(r'%check-cxx-header-in-clang -std=c++14 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB \1 && '
33-
r'%check-cxx-header-in-clang -std=c++17 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB \1 && '
34-
r'%check-cxx-header-in-clang -std=c++20 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB \1')))
33+
SubstituteCaptures(r'%check-cxx-header-in-clang -std=c++14 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB ' + clang_compile_opt + r'\1 && '
34+
r'%check-cxx-header-in-clang -std=c++17 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB ' + clang_compile_opt + r'\1 && '
35+
r'%check-cxx-header-in-clang -std=c++20 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB ' + clang_compile_opt + r'\1')))
3536

3637
# Test parsing of the generated C++ header in different C++ language modes (with C++20 concepts support).
3738
config.substitutions.insert(0, ('%check-generic-interop-cxx-header-in-clang\(([^)]+)\)',
38-
SubstituteCaptures(r'%check-cxx-header-in-clang -std=c++20 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB \1')))
39+
SubstituteCaptures(r'%check-cxx-header-in-clang -std=c++20 -Wno-padded -Wno-c11-extensions -D_LIBCPP_CSTDLIB ' + clang_compile_opt + r'\1')))
3940

4041
# Test parsing of the generated C header in different C language modes.
4142
config.substitutions.insert(0, ('%check-interop-c-header-in-clang\(([^)]+)\)',

test/PrintAsCxx/empty.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
// CHECK-NEXT: #include <cstring>
3232
// CHECK-NEXT: #include <stdlib.h>
3333
// CHECK-NEXT: #include <new>
34-
// CHECK-NEXT: #if defined(_WIN32)
35-
// CHECK-NEXT: #include <malloc.h>
34+
// CHECK-NEXT: #if __has_include(<shims/_SwiftCxxInteroperability.h>)
35+
// CHECK-NEXT: #include <shims/_SwiftCxxInteroperability.h>
3636
// CHECK-NEXT: #endif
3737
// CHECK-NEXT: #else
3838
// CHECK-NEXT: #include <stdint.h>

0 commit comments

Comments
 (0)