Skip to content

Commit 3b7c920

Browse files
authored
Merge pull request #64265 from hyp/eng/swift-stdlib-is-swift-whoda-thunk-it
[interop][SwiftToCxx] emit Swift's stdlib inside of 'swift' namespace
2 parents 52ca885 + 0dc90d3 commit 3b7c920

39 files changed

+148
-136
lines changed

lib/AST/SwiftNameTranslation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
163163
if (customNamesOnly)
164164
return StringRef();
165165

166+
if (auto *mod = dyn_cast<ModuleDecl>(VD)) {
167+
if (mod->isStdlibModule())
168+
return "swift";
169+
}
166170
if (VD->getModuleContext()->isStdlibModule()) {
167171
// Incorporate argument labels into Stdlib API names.
168172
// FIXME: This should be done more broadly.

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void ClangSyntaxPrinter::printModuleNamespaceStart(
142142
const ModuleDecl &moduleContext) const {
143143
os << "namespace ";
144144
printBaseName(&moduleContext);
145-
os << " __attribute__((swift_private))";
145+
os << " SWIFT_PRIVATE_ATTR";
146146
printSymbolUSRAttribute(&moduleContext);
147147
os << " {\n";
148148
}
@@ -155,7 +155,7 @@ void ClangSyntaxPrinter::printNamespace(
155155
os << "namespace ";
156156
namePrinter(os);
157157
if (trivia == NamespaceTrivia::AttributeSwiftPrivate)
158-
os << " __attribute__((swift_private))";
158+
os << " SWIFT_PRIVATE_ATTR";
159159
if (moduleContext)
160160
printSymbolUSRAttribute(moduleContext);
161161
os << " {\n\n";

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,8 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
835835
os << "#endif\n";
836836
os << "#ifdef __cplusplus\n";
837837
os << "namespace ";
838-
M.ValueDecl::getName().print(os);
839-
os << " __attribute__((swift_private))";
838+
ClangSyntaxPrinter(os).printBaseName(&M);
839+
os << " SWIFT_PRIVATE_ATTR";
840840
ClangSyntaxPrinter(os).printSymbolUSRAttribute(&M);
841841
os << " {\n";
842842
os << "namespace " << cxx_synthesis::getCxxImplNamespaceName() << " {\n";
@@ -854,7 +854,7 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
854854

855855
// Construct a C++ namespace for the module.
856856
ClangSyntaxPrinter(os).printNamespace(
857-
[&](raw_ostream &os) { M.ValueDecl::getName().print(os); },
857+
[&](raw_ostream &os) { ClangSyntaxPrinter(os).printBaseName(&M); },
858858
[&](raw_ostream &os) { os << moduleOS.str(); },
859859
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate, &M);
860860

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "PrintClangValueType.h"
2020
#include "SwiftToClangInteropContext.h"
2121
#include "swift/ABI/MetadataValues.h"
22+
#include "swift/AST/ASTContext.h"
2223
#include "swift/AST/Decl.h"
2324
#include "swift/AST/GenericParamList.h"
2425
#include "swift/AST/Module.h"
@@ -218,7 +219,8 @@ class CFunctionSignatureTypePrinter
218219
llvm::function_ref<void()> body) {
219220
if (!optionalKind || optionalKind == OTK_None)
220221
return body();
221-
os << "Swift::Optional<";
222+
printBaseName(moduleContext->getASTContext().getStdlibModule());
223+
os << "::Optional<";
222224
body();
223225
os << '>';
224226
}
@@ -718,7 +720,7 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
718720

719721
// Print out the return type.
720722
if (FD->hasThrows() && outputLang == OutputLanguageMode::Cxx)
721-
os << "Swift::ThrowingResult<";
723+
os << "swift::ThrowingResult<";
722724
if (kind == FunctionSignatureKind::CFunctionProto) {
723725
// First, verify that the C++ return type is representable.
724726
{
@@ -1330,25 +1332,25 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
13301332
if (hasThrows) {
13311333
os << " if (opaqueError != nullptr)\n";
13321334
os << "#ifdef __cpp_exceptions\n";
1333-
os << " throw (Swift::Error(opaqueError));\n";
1335+
os << " throw (swift::Error(opaqueError));\n";
13341336
os << "#else\n";
13351337
if (resultTy->isVoid()) {
1336-
os << " return Swift::Expected<void>(Swift::Error(opaqueError));\n";
1338+
os << " return swift::Expected<void>(swift::Error(opaqueError));\n";
13371339
os << "#endif\n";
13381340
} else {
13391341
auto directResultType = signature.getDirectResultType();
13401342
printDirectReturnOrParamCType(
13411343
*directResultType, resultTy, moduleContext, os, cPrologueOS,
13421344
typeMapping, interopContext, [&]() {
1343-
os << " return Swift::Expected<";
1345+
os << " return swift::Expected<";
13441346
OptionalTypeKind retKind;
13451347
Type objTy;
13461348
std::tie(objTy, retKind) =
13471349
DeclAndTypePrinter::getObjectTypeAndOptionality(FD, resultTy);
13481350

13491351
auto s = printClangFunctionReturnType(objTy, retKind, const_cast<ModuleDecl *>(moduleContext),
13501352
OutputLanguageMode::Cxx);
1351-
os << ">(Swift::Error(opaqueError));\n";
1353+
os << ">(swift::Error(opaqueError));\n";
13521354
os << "#endif\n";
13531355

13541356
// Return the function result value if it doesn't throw.

lib/PrintAsClang/PrintSwiftToClangCoreScaffold.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,27 @@ void swift::printSwiftToClangCoreScaffold(SwiftToClangInteropContext &ctx,
200200
PrimitiveTypeMapping &typeMapping,
201201
raw_ostream &os) {
202202
ClangSyntaxPrinter printer(os);
203-
printer.printNamespace("swift", [&](raw_ostream &) {
204-
printer.printNamespace(
205-
cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &) {
206-
printer.printExternC([&](raw_ostream &os) {
207-
printTypeMetadataResponseType(ctx, typeMapping, os);
208-
os << "\n";
209-
printValueWitnessTable(os);
210-
os << "\n";
211-
printPrimitiveGenericTypeTraits(os, astContext, typeMapping,
212-
/*isCForwardDefinition=*/true);
213-
});
214-
os << "\n";
203+
printer.printNamespace(
204+
cxx_synthesis::getCxxSwiftNamespaceName(),
205+
[&](raw_ostream &) {
206+
printer.printNamespace(
207+
cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &) {
208+
printer.printExternC([&](raw_ostream &os) {
209+
printTypeMetadataResponseType(ctx, typeMapping, os);
210+
os << "\n";
211+
printValueWitnessTable(os);
212+
os << "\n";
213+
printPrimitiveGenericTypeTraits(os, astContext, typeMapping,
214+
/*isCForwardDefinition=*/true);
215+
});
216+
os << "\n";
217+
});
218+
os << "\n";
219+
// C++ only supports inline variables from C++17.
220+
ClangSyntaxPrinter(os).printIgnoredCxx17ExtensionDiagnosticBlock([&]() {
221+
printPrimitiveGenericTypeTraits(os, astContext, typeMapping,
222+
/*isCForwardDefinition=*/false);
215223
});
216-
os << "\n";
217-
// C++ only supports inline variables from C++17.
218-
ClangSyntaxPrinter(os).printIgnoredCxx17ExtensionDiagnosticBlock([&]() {
219-
printPrimitiveGenericTypeTraits(os, astContext, typeMapping,
220-
/*isCForwardDefinition=*/false);
221-
});
222-
});
224+
},
225+
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate);
223226
}

lib/PrintAsClang/_SwiftCxxInteroperability.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@
7171
#define SWIFT_SYMBOL_MODULE(moduleValue)
7272
#endif
7373

74-
namespace swift {
74+
#if __has_attribute(swift_private)
75+
#define SWIFT_PRIVATE_ATTR __attribute__((swift_private))
76+
#else
77+
#define SWIFT_PRIVATE_ATTR
78+
#endif
79+
80+
namespace swift SWIFT_PRIVATE_ATTR {
7581
namespace _impl {
7682

7783
extern "C" void *_Nonnull swift_retain(void *_Nonnull) noexcept;
@@ -243,7 +249,7 @@ SWIFT_INLINE_THUNK void *_Nonnull getOpaquePointer(T &value) {
243249

244250
#pragma clang diagnostic pop
245251

246-
} // namespace swift
252+
} // namespace swift SWIFT_PRIVATE_ATTR
247253
#endif
248254

249255
#endif // SWIFT_CXX_INTEROPERABILITY_H

lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Error {
189189
opaqueValue = other.opaqueValue;
190190
}
191191

192-
template <class T> SWIFT_INLINE_THUNK Swift::Optional<T> as() {
192+
template <class T> SWIFT_INLINE_THUNK swift::Optional<T> as() {
193193
alignas(alignof(T)) char buffer[sizeof(T)];
194194
const void *em = getErrorMetadata();
195195
void *ep = getPointerToOpaquePointer();
@@ -207,10 +207,10 @@ class Error {
207207
swift::_impl::implClassFor<T>::type::initializeWithTake(dest,
208208
buffer);
209209
});
210-
return Swift::Optional<T>::init(result);
210+
return swift::Optional<T>::init(result);
211211
}
212212

213-
return Swift::Optional<T>::none();
213+
return swift::Optional<T>::none();
214214
}
215215

216216
private:
@@ -236,7 +236,7 @@ class Expected {
236236
has_val = false;
237237
}
238238

239-
constexpr Expected(const Swift::Error& error_val) noexcept {
239+
constexpr Expected(const swift::Error &error_val) noexcept {
240240
new (&buffer) Error(error_val);
241241
has_val = false;
242242
}
@@ -264,7 +264,7 @@ class Expected {
264264
if (has_value())
265265
reinterpret_cast<const T *>(buffer)->~T();
266266
else
267-
reinterpret_cast<Swift::Error *>(buffer)->~Error();
267+
reinterpret_cast<swift::Error *>(buffer)->~Error();
268268
}
269269

270270
/// assignment
@@ -313,22 +313,23 @@ class Expected {
313313
}
314314

315315
// Get error
316-
constexpr Swift::Error const& error() const& {
316+
constexpr swift::Error const &error() const & {
317317
if (has_value())
318318
abort();
319-
return reinterpret_cast<const Swift::Error&>(buffer);
319+
return reinterpret_cast<const swift::Error &>(buffer);
320320
}
321321

322-
constexpr Swift::Error& error() & {
322+
constexpr swift::Error &error() & {
323323
if (has_value())
324324
abort();
325-
return reinterpret_cast<Swift::Error&>(buffer);
325+
return reinterpret_cast<swift::Error &>(buffer);
326326
}
327327

328328
constexpr bool has_value() const noexcept { return has_val; }
329329

330330
private:
331-
alignas(_impl::max(alignof(T), alignof(Swift::Error))) char buffer[_impl::max(sizeof(T), sizeof(Swift::Error))];
331+
alignas(_impl::max(alignof(T), alignof(swift::Error))) char buffer[_impl::max(
332+
sizeof(T), sizeof(swift::Error))];
332333
bool has_val;
333334
};
334335

@@ -341,12 +342,11 @@ class Expected<void> {
341342
has_val = false;
342343
}
343344

344-
Expected(const Swift::Error& error_val) noexcept {
345+
Expected(const swift::Error &error_val) noexcept {
345346
new (&buffer) Error(error_val);
346347
has_val = false;
347348
}
348349

349-
350350
/// Copy
351351
Expected(Expected const& other) noexcept {
352352
if (other.has_value())
@@ -361,9 +361,7 @@ class Expected<void> {
361361
// FIXME: Implement move semantics when move swift values is possible
362362
[[noreturn]] Expected(Expected&&) noexcept { abort(); }
363363

364-
~Expected() noexcept {
365-
reinterpret_cast<Swift::Error *>(buffer)->~Error();
366-
}
364+
~Expected() noexcept { reinterpret_cast<swift::Error *>(buffer)->~Error(); }
367365

368366
/// assignment
369367
constexpr auto operator=(Expected&& other) noexcept = delete;
@@ -373,21 +371,21 @@ class Expected<void> {
373371
constexpr explicit operator bool() const noexcept { return has_value(); }
374372

375373
// Get error
376-
constexpr Swift::Error const& error() const& {
374+
constexpr swift::Error const &error() const & {
377375
if (has_value())
378376
abort();
379-
return reinterpret_cast<const Swift::Error&>(buffer);
377+
return reinterpret_cast<const swift::Error &>(buffer);
380378
}
381379

382-
constexpr Swift::Error& error() & {
380+
constexpr swift::Error &error() & {
383381
if (has_value())
384382
abort();
385-
return reinterpret_cast<Swift::Error&>(buffer);
383+
return reinterpret_cast<swift::Error &>(buffer);
386384
}
387385

388386
constexpr bool has_value() const noexcept { return has_val; }
389387
private:
390-
alignas(alignof(Swift::Error)) char buffer[sizeof(Swift::Error)];
388+
alignas(alignof(swift::Error)) char buffer[sizeof(swift::Error)];
391389
bool has_val;
392390
};
393391

@@ -400,10 +398,9 @@ using ThrowingResult = T;
400398

401399
#else
402400

403-
template<class T>
404-
using ThrowingResult = Swift::Expected<T>;
401+
template <class T> using ThrowingResult = swift::Expected<T>;
405402

406-
#define SWIFT_RETURN_THUNK(T, v) Swift::Expected<T>(v)
403+
#define SWIFT_RETURN_THUNK(T, v) swift::Expected<T>(v)
407404

408405
#endif
409406

test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public func takeTrivialInout(_ x: inout Trivial) {
192192
// CHECK-NEXT: #pragma clang diagnostic pop
193193
// CHECK-NEXT: } // namespace swift
194194
// CHECK-EMPTY:
195-
// CHECK-NEXT: namespace UseCxxTy __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("UseCxxTy") {
195+
// CHECK-NEXT: namespace UseCxxTy SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("UseCxxTy") {
196196

197197
// CHECK: SWIFT_INLINE_THUNK ns::NonTrivialTemplate<int> retNonTrivial() noexcept SWIFT_SYMBOL({{.*}}) SWIFT_WARN_UNUSED_RESULT {
198198
// CHECK-NEXT: alignas(alignof(ns::NonTrivialTemplate<int>)) char storage[sizeof(ns::NonTrivialTemplate<int>)];
@@ -231,7 +231,7 @@ public func takeTrivialInout(_ x: inout Trivial) {
231231
// CHECK-NEXT: #pragma clang diagnostic pop
232232
// CHECK-NEXT: } // namespace swift
233233
// CHECK-EMPTY:
234-
// CHECK-NEXT: namespace UseCxxTy __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("UseCxxTy") {
234+
// CHECK-NEXT: namespace UseCxxTy SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("UseCxxTy") {
235235
// CHECK-EMPTY:
236236
// CHECK-NEXT: SWIFT_INLINE_THUNK ns::NonTrivialTemplate<ns::TrivialinNS> retNonTrivial2() noexcept SWIFT_SYMBOL({{.*}}) SWIFT_WARN_UNUSED_RESULT {
237237
// CHECK-NEXT: alignas(alignof(ns::NonTrivialTemplate<ns::TrivialinNS>)) char storage[sizeof(ns::NonTrivialTemplate<ns::TrivialinNS>)];

test/Interop/SwiftToCxx/class/swift-actor-in-cxx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final actor ActorWithField {
3434
}
3535
}
3636

37-
// CHECK: namespace Actor __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Actor") {
37+
// CHECK: namespace Actor SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Actor") {
3838
// CHECK: SWIFT_EXTERN void * _Nonnull $s5Actor0A9WithFieldCACycfC(SWIFT_CONTEXT void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // init()
3939
// CHECK: SWIFT_EXTERN void $s5Actor0A9WithFieldC6methodyyF(SWIFT_CONTEXT void * _Nonnull _self) SWIFT_NOEXCEPT SWIFT_CALL; // method()
4040

test/Interop/SwiftToCxx/class/swift-class-in-cxx.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public final class ClassWithIntField {
2121
}
2222
}
2323

24-
// CHECK: namespace Class __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Class") {
24+
// CHECK: namespace Class SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Class") {
2525

2626
// CHECK: SWIFT_EXTERN void * _Nonnull $s5Class011passThroughA12WithIntFieldyAA0adeF0CADF(void * _Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // passThroughClassWithIntField(_:)
2727
// CHECK-NEXT: SWIFT_EXTERN void * _Nonnull $s5Class06returnA12WithIntFieldAA0acdE0CyF(void) SWIFT_NOEXCEPT SWIFT_CALL; // returnClassWithIntField()
2828
// CHECK-NEXT: SWIFT_EXTERN void $s5Class04takeA12WithIntFieldyyAA0acdE0CF(void * _Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // takeClassWithIntField(_:)
2929
// CHECK-NEXT: SWIFT_EXTERN void $s5Class04takeA17WithIntFieldInoutyyAA0acdE0CzF(void * _Nonnull * _Nonnull x) SWIFT_NOEXCEPT SWIFT_CALL; // takeClassWithIntFieldInout(_:)
3030

31-
// CHECK: namespace Class __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Class") {
31+
// CHECK: namespace Class SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Class") {
3232

3333
// CHECK: class SWIFT_SYMBOL("s:5Class0A12WithIntFieldC") ClassWithIntField;
3434
// CHECK-NEXT: } // end namespace
@@ -43,7 +43,7 @@ public final class ClassWithIntField {
4343
// CHECK-NEXT: } // namespace swift
4444

4545
// CHECK: namespace
46-
// CHECK-SAME: Class __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Class") {
46+
// CHECK-SAME: Class SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Class") {
4747

4848
// CHECK: namespace
4949
// CHECK-SAME: _impl {
@@ -100,7 +100,7 @@ public final class ClassWithIntField {
100100
// CHECK-NEXT: #pragma clang diagnostic pop
101101
// CHECK-NEXT: } // namespace swift
102102
// CHECK-EMPTY:
103-
// CHECK-NEXT: namespace Class __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Class") {
103+
// CHECK-NEXT: namespace Class SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Class") {
104104

105105
// CHECK: SWIFT_INLINE_THUNK ClassWithIntField passThroughClassWithIntField(const ClassWithIntField& x) noexcept SWIFT_SYMBOL("s:5Class011passThroughA12WithIntFieldyAA0adeF0CADF") SWIFT_WARN_UNUSED_RESULT {
106106
// CHECK-NEXT: return _impl::_impl_ClassWithIntField::makeRetained(_impl::$s5Class011passThroughA12WithIntFieldyAA0adeF0CADF(::swift::_impl::_impl_RefCountedClass::getOpaquePointer(x)));

0 commit comments

Comments
 (0)