1313#include " swift/PrintAsClang/PrintAsClang.h"
1414
1515#include " ModuleContentsWriter.h"
16- #include " OutputLanguageMode.h"
1716
1817#include " swift/AST/ASTContext.h"
1918#include " swift/AST/Module.h"
2827using namespace swift ;
2928
3029static void writePrologue (raw_ostream &out, ASTContext &ctx,
31- StringRef macroGuard, OutputLanguageMode Lang) {
30+ StringRef macroGuard) {
31+ auto emitCxxConditional = [&](llvm::function_ref<void ()> cxxCase,
32+ llvm::function_ref<void ()> cCase = {}) {
33+ out << " #if defined(__cplusplus)\n " ;
34+ cxxCase ();
35+ if (cCase) {
36+ out << " #else\n " ;
37+ cCase ();
38+ }
39+ out << " #endif\n " ;
40+ };
41+ auto emitObjCConditional = [&](llvm::function_ref<void ()> objcCase,
42+ llvm::function_ref<void ()> nonObjCCase = {}) {
43+ out << " #if defined(__OBJC__)\n " ;
44+ objcCase ();
45+ if (nonObjCCase) {
46+ out << " #else\n " ;
47+ nonObjCCase ();
48+ }
49+ out << " #endif\n " ;
50+ };
51+
3252 out << " // Generated by "
3353 << version::getSwiftFullVersion (ctx.LangOpts .EffectiveLanguageVersion )
3454 << " \n "
@@ -57,16 +77,18 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
5777 " #endif\n "
5878 " \n "
5979 " #pragma clang diagnostic ignored \" -Wauto-import\"\n " ;
60- if (Lang == OutputLanguageMode::Cxx) {
61- out << " #include <cstdint>\n "
62- " #include <cstddef>\n "
63- " #include <cstdbool>\n " ;
64- } else {
65- out << " #include <Foundation/Foundation.h>\n "
66- " #include <stdint.h>\n "
67- " #include <stddef.h>\n "
68- " #include <stdbool.h>\n " ;
69- }
80+ emitObjCConditional ([&] { out << " #include <Foundation/Foundation.h>\n " ; });
81+ emitCxxConditional (
82+ [&] {
83+ out << " #include <cstdint>\n "
84+ " #include <cstddef>\n "
85+ " #include <cstdbool>\n " ;
86+ },
87+ [&] {
88+ out << " #include <stdint.h>\n "
89+ " #include <stddef.h>\n "
90+ " #include <stdbool.h>\n " ;
91+ });
7092 out << " \n "
7193 " #if !defined(SWIFT_TYPEDEFS)\n "
7294 " # define SWIFT_TYPEDEFS 1\n "
@@ -254,30 +276,27 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
254276 " #else\n "
255277 " # define SWIFT_DEPRECATED_OBJC(Msg) SWIFT_DEPRECATED_MSG(Msg)\n "
256278 " #endif\n " ;
257- if (Lang == OutputLanguageMode::ObjC) {
279+ emitObjCConditional ([&] {
258280 out << " #if !defined(IBSegueAction)\n "
259281 " # define IBSegueAction\n "
260282 " #endif\n " ;
261- }
283+ });
262284 out << " #if !defined(SWIFT_EXTERN)\n "
263285 " # if defined(__cplusplus)\n "
264286 " # define SWIFT_EXTERN extern \" C\"\n "
265287 " # else\n "
266288 " # define SWIFT_EXTERN extern\n "
267289 " # endif\n "
268290 " #endif\n " ;
269- auto emitMacro = [&](StringRef name, StringRef value) {
291+ auto emitMacro = [&](StringRef name, StringRef value = " " ) {
270292 out << " #if !defined(" << name << " )\n " ;
271293 out << " # define " << name << " " << value << " \n " ;
272294 out << " #endif\n " ;
273295 };
274296 emitMacro (" SWIFT_CALL" , " __attribute__((swiftcall))" );
275297 // SWIFT_NOEXCEPT applies 'noexcept' in C++ mode only.
276- out << " #if defined(__cplusplus)\n " ;
277- emitMacro (" SWIFT_NOEXCEPT" , " noexcept" );
278- out << " #else\n " ;
279- emitMacro (" SWIFT_NOEXCEPT" , " " );
280- out << " #endif\n " ;
298+ emitCxxConditional ([&] { emitMacro (" SWIFT_NOEXCEPT" , " noexcept" ); },
299+ [&] { emitMacro (" SWIFT_NOEXCEPT" ); });
281300 static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
282301 " need to add SIMD typedefs here if max elements is increased" );
283302}
@@ -431,8 +450,7 @@ bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
431450 std::string moduleContentsBuf;
432451 llvm::raw_string_ostream moduleContents{moduleContentsBuf};
433452 printModuleContentsAsObjC (moduleContents, imports, *M);
434- writePrologue (os, M->getASTContext (), computeMacroGuard (M),
435- OutputLanguageMode::ObjC);
453+ writePrologue (os, M->getASTContext (), computeMacroGuard (M));
436454 writeImports (os, imports, *M, bridgingHeader);
437455 writePostImportPrologue (os, *M);
438456 os << moduleContents.str ();
@@ -448,8 +466,7 @@ bool swift::printAsCXX(raw_ostream &os, ModuleDecl *M) {
448466 std::string moduleContentsBuf;
449467 llvm::raw_string_ostream moduleContents{moduleContentsBuf};
450468 printModuleContentsAsCxx (moduleContents, imports, *M);
451- writePrologue (os, M->getASTContext (), computeMacroGuard (M),
452- OutputLanguageMode::Cxx);
469+ writePrologue (os, M->getASTContext (), computeMacroGuard (M));
453470 writePostImportPrologue (os, *M);
454471 os << moduleContents.str ();
455472 writeEpilogue (os);
0 commit comments