Skip to content

Commit ad50214

Browse files
author
Gabor Horvath
committed
[cxx-interop] Make reverse interop header compile in embedded mode
These are the minimal changes to make the reverse interop header compile when embedded Swift is used. Previously, the _SwiftStdlibCxxOverlay.h and the generated interop header disagreed on the mangling of a symbol. This is not sufficient yet to use the embedded Swift standard library from reverse interop, there are some missing symbols while linking. But this PR enables doing reverse interop without using the stdlib types like swift::String in C++. Follow-up PRs will fix the rest of the issues. rdar://154740225
1 parent 299f173 commit ad50214

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/AST/SwiftNameTranslation.h"
3030
#include "swift/AST/TypeDeclFinder.h"
3131
#include "swift/Basic/Assertions.h"
32+
#include "swift/Basic/Feature.h"
3233
#include "swift/Basic/SourceManager.h"
3334
#include "swift/ClangImporter/ClangImporter.h"
3435
#include "swift/SIL/SILInstruction.h"
@@ -1183,13 +1184,14 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
11831184
std::string modulePrologueBuf;
11841185
llvm::raw_string_ostream prologueOS{modulePrologueBuf};
11851186
EmittedClangHeaderDependencyInfo info;
1187+
auto &context = M.getASTContext();
11861188

11871189
// Define the `SWIFT_SYMBOL` macro.
11881190
os << "#ifdef SWIFT_SYMBOL\n";
11891191
os << "#undef SWIFT_SYMBOL\n";
11901192
os << "#endif\n";
11911193
os << "#define SWIFT_SYMBOL(usrValue) SWIFT_SYMBOL_MODULE_USR(\"";
1192-
ClangSyntaxPrinter(M.getASTContext(), os).printBaseName(&M);
1194+
ClangSyntaxPrinter(context, os).printBaseName(&M);
11931195
os << "\", usrValue)\n";
11941196

11951197
// FIXME: Use getRequiredAccess once @expose is supported.
@@ -1204,17 +1206,19 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
12041206
os << "#include <string>\n";
12051207
os << "#endif\n";
12061208
os << "#include <new>\n";
1209+
if (context.LangOpts.hasFeature(Feature::Embedded))
1210+
os << "#define __EmbeddedSwift__\n";
12071211
// Embed an overlay for the standard library.
1208-
ClangSyntaxPrinter(M.getASTContext(), moduleOS).printIncludeForShimHeader(
1209-
"_SwiftStdlibCxxOverlay.h");
1212+
ClangSyntaxPrinter(context, moduleOS)
1213+
.printIncludeForShimHeader("_SwiftStdlibCxxOverlay.h");
12101214
// Ignore typos in Swift stdlib doc comments.
12111215
os << "#pragma clang diagnostic push\n";
12121216
os << "#pragma clang diagnostic ignored \"-Wdocumentation\"\n";
12131217
}
12141218

12151219
os << "#ifndef SWIFT_PRINTED_CORE\n";
12161220
os << "#define SWIFT_PRINTED_CORE\n";
1217-
printSwiftToClangCoreScaffold(interopContext, M.getASTContext(),
1221+
printSwiftToClangCoreScaffold(interopContext, context,
12181222
writer.getTypeMapping(), os);
12191223
os << "#endif\n";
12201224

@@ -1226,9 +1230,9 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
12261230
os << "#endif\n";
12271231
os << "#ifdef __cplusplus\n";
12281232
os << "namespace ";
1229-
ClangSyntaxPrinter(M.getASTContext(), os).printBaseName(&M);
1233+
ClangSyntaxPrinter(context, os).printBaseName(&M);
12301234
os << " SWIFT_PRIVATE_ATTR";
1231-
ClangSyntaxPrinter(M.getASTContext(), os).printSymbolUSRAttribute(&M);
1235+
ClangSyntaxPrinter(context, os).printSymbolUSRAttribute(&M);
12321236
os << " {\n";
12331237
os << "namespace " << cxx_synthesis::getCxxImplNamespaceName() << " {\n";
12341238
os << "extern \"C\" {\n";
@@ -1246,10 +1250,13 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
12461250
os << "#pragma clang diagnostic push\n";
12471251
os << "#pragma clang diagnostic ignored \"-Wreserved-identifier\"\n";
12481252
// Construct a C++ namespace for the module.
1249-
ClangSyntaxPrinter(M.getASTContext(), os).printNamespace(
1250-
[&](raw_ostream &os) { ClangSyntaxPrinter(M.getASTContext(), os).printBaseName(&M); },
1251-
[&](raw_ostream &os) { os << moduleOS.str(); },
1252-
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate, &M);
1253+
ClangSyntaxPrinter(context, os)
1254+
.printNamespace(
1255+
[&](raw_ostream &os) {
1256+
ClangSyntaxPrinter(context, os).printBaseName(&M);
1257+
},
1258+
[&](raw_ostream &os) { os << moduleOS.str(); },
1259+
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate, &M);
12531260
os << "#pragma clang diagnostic pop\n";
12541261

12551262
if (M.isStdlibModule()) {

lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ SWIFT_INLINE_THUNK T_0_0 get() const
3838
/// Constructs a Swift string from a C string.
3939
SWIFT_INLINE_THUNK String(const char *cString) noexcept {
4040
if (!cString) {
41+
#ifdef __EmbeddedSwift__
42+
auto res = _impl::$eS2SycfC();
43+
#else
4144
auto res = _impl::$sS2SycfC();
45+
#endif
4246
memcpy(_getOpaquePointer(), &res, sizeof(res));
4347
return;
4448
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend %S/embedded-swift.swift -target arm64-apple-macosx15.0 -module-name Core -swift-version 6 -enable-experimental-feature Embedded -clang-header-expose-decls=all-public -typecheck -verify -emit-clang-header-path %t/core.h
4+
5+
// RUN: %target-interop-build-clangxx -c %s -I %t -o %t/swift-embedded-execution.o -fno-exceptions -fno-rtti
6+
// RUN: %target-interop-build-swift -target arm64-apple-macosx15.0 -wmo %S/embedded-swift.swift -o %t/swift-embedded-execution -Xlinker %t/swift-embedded-execution.o -module-name Core -Xfrontend -entry-point-function-name -Xfrontend swiftMain -enable-experimental-feature Embedded -Xcc -fno-rtti -Xcc -fno-exceptions -Xlinker %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswiftUnicodeDataTables.a
7+
8+
// RUN: %target-codesign %t/swift-embedded-execution
9+
// RUN: %target-run %t/swift-embedded-execution
10+
11+
// REQUIRES: OS=macosx
12+
// REQUIRES: embedded_stdlib
13+
// REQUIRES: swift_feature_Embedded
14+
// REQUIRES: executable_test
15+
// REQUIRES: CPU=arm64e || CPU=arm64
16+
17+
#include <assert.h>
18+
#include "core.h"
19+
20+
int main() {
21+
assert(Core::id(5) == 5);
22+
return 0;
23+
}

0 commit comments

Comments
 (0)