Skip to content

Commit f4a6af7

Browse files
committed
Add -enable-cxx-interop flag and support for extern "C" {}
- Updated some swift headers to build with c++ and c. - Added -enable-cxx-interop flag. - Added support for 'extern "C"'.
1 parent 9902d20 commit f4a6af7

File tree

9 files changed

+60
-19
lines changed

9 files changed

+60
-19
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ namespace swift {
139139
/// configuration options.
140140
bool EnableObjCInterop = true;
141141

142+
/// Enable C++ interop code generation and build configuration
143+
/// options.
144+
/// FIXME: Disabled by default until this is fully baked.
145+
bool EnableCXXInterop = false;
146+
142147
/// On Darwin platforms, use the pre-stable ABI's mark bit for Swift
143148
/// classes instead of the stable ABI's bit. This is needed when
144149
/// targeting OSes prior to macOS 10.14.4 and iOS 12.2, where

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,11 @@ def enable_private_imports : Flag<["-"], "enable-private-imports">,
841841
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
842842
HelpText<"Allows this module's internal and private API to be accessed">;
843843

844+
def enable_cxx_interop :
845+
Flag<["-"], "enable-cxx-interop">,
846+
HelpText<"Enable C++ interop code generation and config directives">,
847+
Flags<[FrontendOption]>;
848+
844849
def sanitize_EQ : CommaJoined<["-"], "sanitize=">,
845850
Flags<[FrontendOption, NoInteractiveOption]>, MetaVarName<"<check>">,
846851
HelpText<"Turn on runtime checks for erroneous behavior.">;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,12 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
518518
});
519519

520520
} else {
521-
invocationArgStrs.insert(invocationArgStrs.end(), {"-x", "c", "-std=gnu11"});
521+
bool EnableCXXInterop = LangOpts.EnableCXXInterop;
522+
invocationArgStrs.insert(invocationArgStrs.end(), {
523+
"-x",
524+
EnableCXXInterop ? "c++" : "c",
525+
EnableCXXInterop ? "-std=c++17" : "-std=gnu11"
526+
});
522527
}
523528

524529
// Set C language options.

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class SwiftLookupTableWriter : public clang::ModuleFileExtensionWriter {
8181
llvm::BitstreamWriter &stream) override;
8282

8383
void populateTable(SwiftLookupTable &table, NameImporter &);
84+
85+
void populateTableWithDecl(SwiftLookupTable &table,
86+
NameImporter &nameImporter,
87+
clang::Decl* decl);
8488
};
8589

8690
/// Module file extension reader for the Swift lookup tables.
@@ -1793,21 +1797,35 @@ void importer::finalizeLookupTable(SwiftLookupTable &table,
17931797
}
17941798
}
17951799

1800+
void SwiftLookupTableWriter::populateTableWithDecl(SwiftLookupTable &table,
1801+
NameImporter &nameImporter,
1802+
clang::Decl* decl) {
1803+
// Skip anything from an AST file.
1804+
if (decl->isFromASTFile())
1805+
return;
1806+
1807+
// Iterate into extern "C" {} type declarations.
1808+
if (auto linkageDecl = dyn_cast<clang::LinkageSpecDecl>(decl)) {
1809+
for (auto* decl : linkageDecl->noload_decls()) {
1810+
populateTableWithDecl(table, nameImporter, decl);
1811+
}
1812+
return;
1813+
}
1814+
1815+
// Skip non-named declarations.
1816+
auto named = dyn_cast<clang::NamedDecl>(decl);
1817+
if (!named)
1818+
return;
1819+
1820+
// Add this entry to the lookup table.
1821+
addEntryToLookupTable(table, named, nameImporter);
1822+
}
1823+
17961824
void SwiftLookupTableWriter::populateTable(SwiftLookupTable &table,
17971825
NameImporter &nameImporter) {
17981826
auto &sema = nameImporter.getClangSema();
17991827
for (auto decl : sema.Context.getTranslationUnitDecl()->noload_decls()) {
1800-
// Skip anything from an AST file.
1801-
if (decl->isFromASTFile())
1802-
continue;
1803-
1804-
// Skip non-named declarations.
1805-
auto named = dyn_cast<clang::NamedDecl>(decl);
1806-
if (!named)
1807-
continue;
1808-
1809-
// Add this entry to the lookup table.
1810-
addEntryToLookupTable(table, named, nameImporter);
1828+
populateTableWithDecl(table, nameImporter, decl);
18111829
}
18121830

18131831
// Add macros to the lookup table.

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
188188
inputArgs.AddLastArg(arguments, options::OPT_enable_library_evolution);
189189
inputArgs.AddLastArg(arguments, options::OPT_enable_testing);
190190
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
191+
inputArgs.AddLastArg(arguments, options::OPT_enable_cxx_interop);
191192
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
192193
inputArgs.AddLastArg(arguments, options::OPT_debug_info_format);
193194
inputArgs.AddLastArg(arguments, options::OPT_import_underlying_module);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
470470
TargetArg = A->getValue();
471471
}
472472

473+
Opts.EnableCXXInterop |= Args.hasArg(OPT_enable_cxx_interop);
473474
Opts.EnableObjCInterop =
474475
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,
475476
Target.isOSDarwin());

stdlib/public/SwiftShims/GlobalObjects.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
#include "Visibility.h"
2525

2626
#ifdef __cplusplus
27-
namespace swift { extern "C" {
27+
#ifndef __swift__
28+
namespace swift {
29+
#endif
30+
extern "C" {
2831
#endif
2932

3033
struct _SwiftArrayBodyStorage {
@@ -102,7 +105,10 @@ static_assert(
102105
4 * sizeof(__swift_intptr_t) + sizeof(__swift_int64_t),
103106
"_SwiftSetBodyStorage has unexpected size");
104107

105-
}} // extern "C", namespace swift
108+
} // extern "C"
109+
#ifndef __swift__
110+
} // namespace swift
111+
#endif
106112
#endif
107113

108114
#endif

stdlib/public/SwiftShims/HeapObject.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define SWIFT_ABI_HEAP_OBJECT_HEADER_SIZE_64 16
2121
#define SWIFT_ABI_HEAP_OBJECT_HEADER_SIZE_32 8
2222

23-
#ifdef __cplusplus
23+
#ifndef __swift__
2424
#include <type_traits>
2525
#include "swift/Basic/type_traits.h"
2626

@@ -48,7 +48,7 @@ struct HeapObject {
4848

4949
SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;
5050

51-
#ifdef __cplusplus
51+
#ifndef __swift__
5252
HeapObject() = default;
5353

5454
// Initialize a HeapObject header as appropriate for a newly-allocated object.
@@ -68,7 +68,7 @@ struct HeapObject {
6868
void dump() const LLVM_ATTRIBUTE_USED;
6969
#endif
7070

71-
#endif // __cplusplus
71+
#endif // __swift__
7272
};
7373

7474
#ifdef __cplusplus
@@ -92,7 +92,7 @@ __swift_size_t swift_weakRetainCount(HeapObject *obj);
9292
} // extern "C"
9393
#endif
9494

95-
#ifdef __cplusplus
95+
#ifndef __swift__
9696
static_assert(std::is_trivially_destructible<HeapObject>::value,
9797
"HeapObject must be trivially destructible");
9898

stdlib/public/SwiftShims/RefCount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct {
2121
__swift_uintptr_t refCounts SWIFT_ATTRIBUTE_UNAVAILABLE;
2222
} InlineRefCountsPlaceholder;
2323

24-
#if !defined(__cplusplus)
24+
#if defined(__swift__)
2525

2626
typedef InlineRefCountsPlaceholder InlineRefCounts;
2727

0 commit comments

Comments
 (0)