13
13
#include " swift/PrintAsClang/PrintAsClang.h"
14
14
15
15
#include " ModuleContentsWriter.h"
16
+ #include " OutputLanguageMode.h"
16
17
17
18
#include " swift/AST/ASTContext.h"
18
19
#include " swift/AST/Module.h"
27
28
using namespace swift ;
28
29
29
30
static void writePrologue (raw_ostream &out, ASTContext &ctx,
30
- StringRef macroGuard) {
31
- out << " // Generated by " << version::getSwiftFullVersion (
32
- ctx.LangOpts .EffectiveLanguageVersion ) << " \n "
33
- // Guard against recursive definition.
34
- << " #ifndef " << macroGuard << " \n "
35
- << " #define " << macroGuard << " \n "
31
+ StringRef macroGuard, OutputLanguageMode Lang) {
32
+ out << " // Generated by "
33
+ << version::getSwiftFullVersion (ctx.LangOpts .EffectiveLanguageVersion )
34
+ << " \n "
35
+ // Guard against recursive definition.
36
+ << " #ifndef " << macroGuard << " \n "
37
+ << " #define " << macroGuard
38
+ << " \n "
36
39
" #pragma clang diagnostic push\n "
37
40
" #pragma clang diagnostic ignored \" -Wgcc-compat\"\n "
38
41
" \n "
@@ -53,12 +56,18 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
53
56
" # include <swift/objc-prologue.h>\n "
54
57
" #endif\n "
55
58
" \n "
56
- " #pragma clang diagnostic ignored \" -Wauto-import\"\n "
57
- " #include <Foundation/Foundation.h>\n "
58
- " #include <stdint.h>\n "
59
- " #include <stddef.h>\n "
60
- " #include <stdbool.h>\n "
61
- " \n "
59
+ " #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
+ }
70
+ out << " \n "
62
71
" #if !defined(SWIFT_TYPEDEFS)\n "
63
72
" # define SWIFT_TYPEDEFS 1\n "
64
73
" # if __has_include(<uchar.h>)\n "
@@ -95,19 +104,19 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
95
104
" \n "
96
105
" #if __has_attribute(objc_runtime_name)\n "
97
106
" # define SWIFT_RUNTIME_NAME(X) "
98
- " __attribute__((objc_runtime_name(X)))\n "
107
+ " __attribute__((objc_runtime_name(X)))\n "
99
108
" #else\n "
100
109
" # define SWIFT_RUNTIME_NAME(X)\n "
101
110
" #endif\n "
102
111
" #if __has_attribute(swift_name)\n "
103
112
" # define SWIFT_COMPILE_NAME(X) "
104
- " __attribute__((swift_name(X)))\n "
113
+ " __attribute__((swift_name(X)))\n "
105
114
" #else\n "
106
115
" # define SWIFT_COMPILE_NAME(X)\n "
107
116
" #endif\n "
108
117
" #if __has_attribute(objc_method_family)\n "
109
118
" # define SWIFT_METHOD_FAMILY(X) "
110
- " __attribute__((objc_method_family(X)))\n "
119
+ " __attribute__((objc_method_family(X)))\n "
111
120
" #else\n "
112
121
" # define SWIFT_METHOD_FAMILY(X)\n "
113
122
" #endif\n "
@@ -122,7 +131,8 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
122
131
" # define SWIFT_RELEASES_ARGUMENT\n "
123
132
" #endif\n "
124
133
" #if __has_attribute(warn_unused_result)\n "
125
- " # define SWIFT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n "
134
+ " # define SWIFT_WARN_UNUSED_RESULT "
135
+ " __attribute__((warn_unused_result))\n "
126
136
" #else\n "
127
137
" # define SWIFT_WARN_UNUSED_RESULT\n "
128
138
" #endif\n "
@@ -143,41 +153,41 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
143
153
" #if !defined(SWIFT_CLASS)\n "
144
154
" # if __has_attribute(objc_subclassing_restricted)\n "
145
155
" # define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) "
146
- " __attribute__((objc_subclassing_restricted)) "
147
- " SWIFT_CLASS_EXTRA\n "
156
+ " __attribute__((objc_subclassing_restricted)) "
157
+ " SWIFT_CLASS_EXTRA\n "
148
158
" # define SWIFT_CLASS_NAMED(SWIFT_NAME) "
149
- " __attribute__((objc_subclassing_restricted)) "
150
- " SWIFT_COMPILE_NAME(SWIFT_NAME) "
151
- " SWIFT_CLASS_EXTRA\n "
159
+ " __attribute__((objc_subclassing_restricted)) "
160
+ " SWIFT_COMPILE_NAME(SWIFT_NAME) "
161
+ " SWIFT_CLASS_EXTRA\n "
152
162
" # else\n "
153
163
" # define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) "
154
- " SWIFT_CLASS_EXTRA\n "
164
+ " SWIFT_CLASS_EXTRA\n "
155
165
" # define SWIFT_CLASS_NAMED(SWIFT_NAME) "
156
- " SWIFT_COMPILE_NAME(SWIFT_NAME) "
157
- " SWIFT_CLASS_EXTRA\n "
166
+ " SWIFT_COMPILE_NAME(SWIFT_NAME) "
167
+ " SWIFT_CLASS_EXTRA\n "
158
168
" # endif\n "
159
169
" #endif\n "
160
170
" #if !defined(SWIFT_RESILIENT_CLASS)\n "
161
171
" # if __has_attribute(objc_class_stub)\n "
162
172
" # define SWIFT_RESILIENT_CLASS(SWIFT_NAME) SWIFT_CLASS(SWIFT_NAME) "
163
- " __attribute__((objc_class_stub))\n "
173
+ " __attribute__((objc_class_stub))\n "
164
174
" # define SWIFT_RESILIENT_CLASS_NAMED(SWIFT_NAME) "
165
- " __attribute__((objc_class_stub)) "
166
- " SWIFT_CLASS_NAMED(SWIFT_NAME)\n "
175
+ " __attribute__((objc_class_stub)) "
176
+ " SWIFT_CLASS_NAMED(SWIFT_NAME)\n "
167
177
" # else\n "
168
178
" # define SWIFT_RESILIENT_CLASS(SWIFT_NAME) "
169
- " SWIFT_CLASS(SWIFT_NAME)\n "
179
+ " SWIFT_CLASS(SWIFT_NAME)\n "
170
180
" # define SWIFT_RESILIENT_CLASS_NAMED(SWIFT_NAME) "
171
- " SWIFT_CLASS_NAMED(SWIFT_NAME)\n "
181
+ " SWIFT_CLASS_NAMED(SWIFT_NAME)\n "
172
182
" # endif\n "
173
183
" #endif\n "
174
184
" \n "
175
185
" #if !defined(SWIFT_PROTOCOL)\n "
176
186
" # define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) "
177
- " SWIFT_PROTOCOL_EXTRA\n "
187
+ " SWIFT_PROTOCOL_EXTRA\n "
178
188
" # define SWIFT_PROTOCOL_NAMED(SWIFT_NAME) "
179
- " SWIFT_COMPILE_NAME(SWIFT_NAME) "
180
- " SWIFT_PROTOCOL_EXTRA\n "
189
+ " SWIFT_COMPILE_NAME(SWIFT_NAME) "
190
+ " SWIFT_PROTOCOL_EXTRA\n "
181
191
" #endif\n "
182
192
" \n "
183
193
" #if !defined(SWIFT_EXTENSION)\n "
@@ -187,44 +197,46 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
187
197
" #if !defined(OBJC_DESIGNATED_INITIALIZER)\n "
188
198
" # if __has_attribute(objc_designated_initializer)\n "
189
199
" # define OBJC_DESIGNATED_INITIALIZER "
190
- " __attribute__((objc_designated_initializer))\n "
200
+ " __attribute__((objc_designated_initializer))\n "
191
201
" # else\n "
192
202
" # define OBJC_DESIGNATED_INITIALIZER\n "
193
203
" # endif\n "
194
204
" #endif\n "
195
205
" #if !defined(SWIFT_ENUM_ATTR)\n "
196
206
" # if defined(__has_attribute) && "
197
- " __has_attribute(enum_extensibility)\n "
207
+ " __has_attribute(enum_extensibility)\n "
198
208
" # define SWIFT_ENUM_ATTR(_extensibility) "
199
- " __attribute__((enum_extensibility(_extensibility)))\n "
209
+ " __attribute__((enum_extensibility(_extensibility)))\n "
200
210
" # else\n "
201
211
" # define SWIFT_ENUM_ATTR(_extensibility)\n "
202
212
" # endif\n "
203
213
" #endif\n "
204
214
" #if !defined(SWIFT_ENUM)\n "
205
215
" # define SWIFT_ENUM(_type, _name, _extensibility) "
206
- " enum _name : _type _name; "
207
- " enum SWIFT_ENUM_ATTR(_extensibility) SWIFT_ENUM_EXTRA "
208
- " _name : _type\n "
216
+ " enum _name : _type _name; "
217
+ " enum SWIFT_ENUM_ATTR(_extensibility) SWIFT_ENUM_EXTRA "
218
+ " _name : _type\n "
209
219
" # if __has_feature(generalized_swift_name)\n "
210
220
" # define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME, "
211
- " _extensibility) "
212
- " enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_NAME); "
213
- " enum SWIFT_COMPILE_NAME(SWIFT_NAME) "
214
- " SWIFT_ENUM_ATTR(_extensibility) SWIFT_ENUM_EXTRA _name : _type\n "
221
+ " _extensibility) "
222
+ " enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_NAME); "
223
+ " enum SWIFT_COMPILE_NAME(SWIFT_NAME) "
224
+ " SWIFT_ENUM_ATTR(_extensibility) SWIFT_ENUM_EXTRA _name : _type\n "
215
225
" # else\n "
216
226
" # define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME, "
217
- " _extensibility) SWIFT_ENUM(_type, _name, _extensibility)\n "
227
+ " _extensibility) SWIFT_ENUM(_type, _name, _extensibility)\n "
218
228
" # endif\n "
219
229
" #endif\n "
220
230
" #if !defined(SWIFT_UNAVAILABLE)\n "
221
231
" # define SWIFT_UNAVAILABLE __attribute__((unavailable))\n "
222
232
" #endif\n "
223
233
" #if !defined(SWIFT_UNAVAILABLE_MSG)\n "
224
- " # define SWIFT_UNAVAILABLE_MSG(msg) __attribute__((unavailable(msg)))\n "
234
+ " # define SWIFT_UNAVAILABLE_MSG(msg) "
235
+ " __attribute__((unavailable(msg)))\n "
225
236
" #endif\n "
226
237
" #if !defined(SWIFT_AVAILABILITY)\n "
227
- " # define SWIFT_AVAILABILITY(plat, ...) __attribute__((availability(plat, __VA_ARGS__)))\n "
238
+ " # define SWIFT_AVAILABILITY(plat, ...) "
239
+ " __attribute__((availability(plat, __VA_ARGS__)))\n "
228
240
" #endif\n "
229
241
" #if !defined(SWIFT_WEAK_IMPORT)\n "
230
242
" # define SWIFT_WEAK_IMPORT __attribute__((weak_import))\n "
@@ -233,24 +245,27 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
233
245
" # define SWIFT_DEPRECATED __attribute__((deprecated))\n "
234
246
" #endif\n "
235
247
" #if !defined(SWIFT_DEPRECATED_MSG)\n "
236
- " # define SWIFT_DEPRECATED_MSG(...) __attribute__((deprecated(__VA_ARGS__)))\n "
248
+ " # define SWIFT_DEPRECATED_MSG(...) "
249
+ " __attribute__((deprecated(__VA_ARGS__)))\n "
237
250
" #endif\n "
238
251
" #if __has_feature(attribute_diagnose_if_objc)\n "
239
- " # define SWIFT_DEPRECATED_OBJC(Msg) __attribute__((diagnose_if(1, Msg, \" warning\" )))\n "
252
+ " # define SWIFT_DEPRECATED_OBJC(Msg) __attribute__((diagnose_if(1, "
253
+ " Msg, \" warning\" )))\n "
240
254
" #else\n "
241
255
" # define SWIFT_DEPRECATED_OBJC(Msg) SWIFT_DEPRECATED_MSG(Msg)\n "
242
- " #endif\n "
243
- " #if !defined(IBSegueAction)\n "
244
- " # define IBSegueAction\n "
245
- " #endif\n "
246
- " #if !defined(SWIFT_EXTERN)\n "
256
+ " #endif\n " ;
257
+ if (Lang == OutputLanguageMode::ObjC) {
258
+ out << " #if !defined(IBSegueAction)\n "
259
+ " # define IBSegueAction\n "
260
+ " #endif\n " ;
261
+ }
262
+ out << " #if !defined(SWIFT_EXTERN)\n "
247
263
" # if defined(__cplusplus)\n "
248
264
" # define SWIFT_EXTERN extern \" C\"\n "
249
265
" # else\n "
250
266
" # define SWIFT_EXTERN extern\n "
251
267
" # endif\n "
252
- " #endif\n "
253
- ;
268
+ " #endif\n " ;
254
269
static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
255
270
" need to add SIMD typedefs here if max elements is increased" );
256
271
}
@@ -401,7 +416,8 @@ bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
401
416
std::string moduleContentsBuf;
402
417
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
403
418
printModuleContentsAsObjC (moduleContents, imports, *M);
404
- writePrologue (os, M->getASTContext (), computeMacroGuard (M));
419
+ writePrologue (os, M->getASTContext (), computeMacroGuard (M),
420
+ OutputLanguageMode::ObjC);
405
421
writeImports (os, imports, *M, bridgingHeader);
406
422
writePostImportPrologue (os, *M);
407
423
os << moduleContents.str ();
@@ -413,9 +429,14 @@ bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
413
429
bool swift::printAsCXX (raw_ostream &os, ModuleDecl *M) {
414
430
llvm::PrettyStackTraceString trace (" While generating C++ header" );
415
431
416
- writePrologue (os, M->getASTContext (), computeMacroGuard (M));
432
+ SmallPtrSet<ImportModuleTy, 8 > imports;
433
+ std::string moduleContentsBuf;
434
+ llvm::raw_string_ostream moduleContents{moduleContentsBuf};
435
+ printModuleContentsAsCxx (moduleContents, imports, *M);
436
+ writePrologue (os, M->getASTContext (), computeMacroGuard (M),
437
+ OutputLanguageMode::Cxx);
417
438
writePostImportPrologue (os, *M);
418
- // TODO (Alex): emit module contents.
439
+ os << moduleContents. str ();
419
440
writeEpilogue (os);
420
441
421
442
return false ;
0 commit comments