13
13
#include " swift/PrintAsClang/PrintAsClang.h"
14
14
15
15
#include " ModuleContentsWriter.h"
16
- #include " OutputLanguageMode.h"
17
16
18
17
#include " swift/AST/ASTContext.h"
19
18
#include " swift/AST/Module.h"
28
27
using namespace swift ;
29
28
30
29
static 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
+
32
52
out << " // Generated by "
33
53
<< version::getSwiftFullVersion (ctx.LangOpts .EffectiveLanguageVersion )
34
54
<< " \n "
@@ -57,16 +77,18 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
57
77
" #endif\n "
58
78
" \n "
59
79
" #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
+ });
70
92
out << " \n "
71
93
" #if !defined(SWIFT_TYPEDEFS)\n "
72
94
" # define SWIFT_TYPEDEFS 1\n "
@@ -254,30 +276,27 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
254
276
" #else\n "
255
277
" # define SWIFT_DEPRECATED_OBJC(Msg) SWIFT_DEPRECATED_MSG(Msg)\n "
256
278
" #endif\n " ;
257
- if (Lang == OutputLanguageMode::ObjC) {
279
+ emitObjCConditional ([&] {
258
280
out << " #if !defined(IBSegueAction)\n "
259
281
" # define IBSegueAction\n "
260
282
" #endif\n " ;
261
- }
283
+ });
262
284
out << " #if !defined(SWIFT_EXTERN)\n "
263
285
" # if defined(__cplusplus)\n "
264
286
" # define SWIFT_EXTERN extern \" C\"\n "
265
287
" # else\n "
266
288
" # define SWIFT_EXTERN extern\n "
267
289
" # endif\n "
268
290
" #endif\n " ;
269
- auto emitMacro = [&](StringRef name, StringRef value) {
291
+ auto emitMacro = [&](StringRef name, StringRef value = " " ) {
270
292
out << " #if !defined(" << name << " )\n " ;
271
293
out << " # define " << name << " " << value << " \n " ;
272
294
out << " #endif\n " ;
273
295
};
274
296
emitMacro (" SWIFT_CALL" , " __attribute__((swiftcall))" );
275
297
// 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" ); });
281
300
static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
282
301
" need to add SIMD typedefs here if max elements is increased" );
283
302
}
@@ -431,8 +450,7 @@ bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
431
450
std::string moduleContentsBuf;
432
451
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
433
452
printModuleContentsAsObjC (moduleContents, imports, *M);
434
- writePrologue (os, M->getASTContext (), computeMacroGuard (M),
435
- OutputLanguageMode::ObjC);
453
+ writePrologue (os, M->getASTContext (), computeMacroGuard (M));
436
454
writeImports (os, imports, *M, bridgingHeader);
437
455
writePostImportPrologue (os, *M);
438
456
os << moduleContents.str ();
@@ -448,8 +466,7 @@ bool swift::printAsCXX(raw_ostream &os, ModuleDecl *M) {
448
466
std::string moduleContentsBuf;
449
467
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
450
468
printModuleContentsAsCxx (moduleContents, imports, *M);
451
- writePrologue (os, M->getASTContext (), computeMacroGuard (M),
452
- OutputLanguageMode::Cxx);
469
+ writePrologue (os, M->getASTContext (), computeMacroGuard (M));
453
470
writePostImportPrologue (os, *M);
454
471
os << moduleContents.str ();
455
472
writeEpilogue (os);
0 commit comments