Skip to content

Commit a6a330c

Browse files
authored
Merge pull request #41912 from hyp/unify2
Recommit: [cxx-interop] start to emitting a unified header file for a Swift module
2 parents a45940c + 80bc75a commit a6a330c

32 files changed

+182
-223
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ ERROR(error_mode_cannot_emit_dependencies,none,
118118
ERROR(error_mode_cannot_emit_reference_dependencies,none,
119119
"this mode does not support emitting reference dependency files", ())
120120
ERROR(error_mode_cannot_emit_header,none,
121-
"this mode does not support emitting Objective-C headers", ())
122-
ERROR(error_mode_cannot_emit_cxx_header,none,
123-
"this mode does not support emitting C++ headers", ())
121+
"this mode does not support emitting Objective-C or C++ headers", ())
124122
ERROR(error_mode_cannot_emit_loaded_module_trace,none,
125123
"this mode does not support emitting the loaded module trace", ())
126124
ERROR(error_mode_cannot_emit_module,none,

include/swift/Basic/FileTypes.def

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ TYPE("raw-sib", RawSIB, "sib", "")
5959
TYPE("llvm-ir", LLVM_IR, "ll", "")
6060
TYPE("llvm-bc", LLVM_BC, "bc", "")
6161
TYPE("diagnostics", SerializedDiagnostics, "dia", "")
62-
TYPE("objc-header", ObjCHeader, "h", "")
63-
TYPE("cxx-header", CXXHeader, "h", "")
62+
TYPE("clang-header", ClangHeader, "h", "")
6463
TYPE("swift-dependencies", SwiftDeps, "swiftdeps", "")
6564
TYPE("external-swift-dependencies", ExternalSwiftDeps, "swiftdeps.external", "")
6665
TYPE("remap", Remapping, "remap", "")

include/swift/Basic/SupplementaryOutputPaths.h

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,17 @@
2020

2121
namespace swift {
2222
struct SupplementaryOutputPaths {
23-
/// The path to which we should emit an Objective-C header for the module.
23+
/// The path to which we should emit a header file that exposes the Swift
24+
/// declarations to C, Objective-C and C++ clients for the module.
2425
///
2526
/// Currently only makes sense when the compiler has whole module knowledge.
2627
/// The modes for which it makes sense incuide both WMO and the "merge
2728
/// modules" job that happens after the normal compilation jobs. That's where
2829
/// the header is emitted in single-file mode, since it needs whole-module
2930
/// information.
3031
///
31-
/// \sa swift::printAsObjC
32-
std::string ObjCHeaderOutputPath;
33-
34-
/// The path to which we should emit a C++ header for the module.
35-
///
36-
/// Currently only makes sense when the compiler has whole module knowledge.
37-
/// The modes for which it makes sense include both WMO and the "merge
38-
/// modules" job that happens after the normal compilation jobs. That's where
39-
/// the header is emitted in single-file mode, since it needs whole-module
40-
/// information.
41-
///
42-
/// \sa swift::printAsCXX
43-
std::string CxxHeaderOutputPath;
32+
/// \sa swift::printAsClangHeader
33+
std::string ClangHeaderOutputPath;
4434

4535
/// The path to which we should emit a serialized module.
4636
/// It is valid whenever there are any inputs.
@@ -170,10 +160,8 @@ struct SupplementaryOutputPaths {
170160

171161
/// Apply a given function for each existing (non-empty string) supplementary output
172162
void forEachSetOutput(llvm::function_ref<void(const std::string&)> fn) const {
173-
if (!ObjCHeaderOutputPath.empty())
174-
fn(ObjCHeaderOutputPath);
175-
if (!CxxHeaderOutputPath.empty())
176-
fn(CxxHeaderOutputPath);
163+
if (!ClangHeaderOutputPath.empty())
164+
fn(ClangHeaderOutputPath);
177165
if (!ModuleOutputPath.empty())
178166
fn(ModuleOutputPath);
179167
if (!ModuleSourceInfoOutputPath.empty())
@@ -209,9 +197,8 @@ struct SupplementaryOutputPaths {
209197
}
210198

211199
bool empty() const {
212-
return ObjCHeaderOutputPath.empty() && CxxHeaderOutputPath.empty() &&
213-
ModuleOutputPath.empty() && ModuleDocOutputPath.empty() &&
214-
DependenciesFilePath.empty() &&
200+
return ClangHeaderOutputPath.empty() && ModuleOutputPath.empty() &&
201+
ModuleDocOutputPath.empty() && DependenciesFilePath.empty() &&
215202
ReferenceDependenciesFilePath.empty() &&
216203
SerializedDiagnosticsPath.empty() && LoadedModuleTracePath.empty() &&
217204
TBDPath.empty() && ModuleInterfaceOutputPath.empty() &&

include/swift/Frontend/Frontend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ class CompilerInvocation {
392392

393393
std::string getOutputFilenameForAtMostOnePrimary() const;
394394
std::string getMainInputFilenameForDebugInfoForAtMostOnePrimary() const;
395-
std::string getObjCHeaderOutputPathForAtMostOnePrimary() const;
396-
std::string getCxxHeaderOutputPathForAtMostOnePrimary() const;
395+
std::string getClangHeaderOutputPathForAtMostOnePrimary() const;
397396
std::string getModuleOutputPathForAtMostOnePrimary() const;
398397
std::string
399398
getReferenceDependenciesFilePathForPrimary(StringRef filename) const;

include/swift/Frontend/FrontendInputsAndOutputs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ class FrontendInputsAndOutputs {
249249

250250
bool hasDependenciesPath() const;
251251
bool hasReferenceDependenciesPath() const;
252-
bool hasObjCHeaderOutputPath() const;
253-
bool hasCxxHeaderOutputPath() const;
252+
bool hasClangHeaderOutputPath() const;
254253
bool hasLoadedModuleTracePath() const;
255254
bool hasModuleOutputPath() const;
256255
bool hasModuleDocOutputPath() const;

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ class FrontendOptions {
378378
/// '.../lib/swift', otherwise '.../lib/swift_static'.
379379
bool UseSharedResourceFolder = true;
380380

381+
/// Indicates whether to expose all public declarations in the generated clang
382+
/// header.
383+
bool ExposePublicDeclsInClangHeader = false;
384+
381385
/// \return true if the given action only parses without doing other compilation steps.
382386
static bool shouldActionOnlyParse(ActionType);
383387

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,4 +1031,9 @@ def skip_import_in_public_interface:
10311031
HelpText<"Skip the import statement corresponding to a module name "
10321032
"when printing the public interface.">;
10331033

1034+
def clang_header_expose_public_decls:
1035+
Flag<["-"], "clang-header-expose-public-decls">,
1036+
HelpText<"Expose all public declarations in the generated clang header">,
1037+
Flags<[FrontendOption, HelpHidden]>;
1038+
10341039
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Option/Options.td

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,11 @@ def emit_objc_header_path : Separate<["-"], "emit-objc-header-path">,
532532
SupplementaryOutput]>,
533533
MetaVarName<"<path>">, HelpText<"Emit an Objective-C header file to <path>">;
534534

535-
def emit_cxx_header : Flag<["-"], "emit-cxx-header">,
536-
Flags<[FrontendOption, NoInteractiveOption, SupplementaryOutput]>,
537-
HelpText<"Emit a C++ header file">;
538-
def emit_cxx_header_path : Separate<["-"], "emit-cxx-header-path">,
539-
Flags<[FrontendOption, NoInteractiveOption, ArgumentIsPath,
535+
def emit_clang_header_path : Separate<["-"], "emit-clang-header-path">,
536+
Flags<[FrontendOption, NoDriverOption, NoInteractiveOption, ArgumentIsPath,
540537
SupplementaryOutput]>,
541-
MetaVarName<"<path>">, HelpText<"Emit a C++ header file to <path>">;
538+
HelpText<"Emit an Objective-C and C++ header file to <path>">,
539+
Alias<emit_objc_header_path>;
542540

543541
def static : Flag<["-"], "static">,
544542
Flags<[FrontendOption, ModuleInterfaceOption, NoInteractiveOption]>,

include/swift/PrintAsClang/PrintAsClang.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ namespace swift {
2121
class ModuleDecl;
2222
class ValueDecl;
2323

24-
/// Print the Objective-C-compatible declarations in a module as a Clang
25-
/// header.
24+
/// Print the exposed declarations in a module into a Clang header.
2625
///
27-
/// Returns true on error.
28-
bool printAsObjC(raw_ostream &out, ModuleDecl *M, StringRef bridgingHeader);
29-
30-
/// Print the C++-compatible declarations in a module as a Clang header.
26+
/// The Objective-C compatible declarations are printed into a block that
27+
/// ensures that those declarations are only usable when the header is
28+
/// compiled in Objective-C mode.
29+
/// The C++ compatible declarations are printed into a block that ensures
30+
/// that those declarations are only usable when the header is compiled in
31+
/// C++ mode.
3132
///
3233
/// Returns true on error.
33-
bool printAsCXX(raw_ostream &os, ModuleDecl *M);
34+
bool printAsClangHeader(raw_ostream &out, ModuleDecl *M,
35+
StringRef bridgingHeader,
36+
bool ExposePublicDeclsInClangHeader);
3437
}
3538

3639
#endif

lib/Basic/FileTypes.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ bool file_types::isTextual(ID Id) {
7373
case file_types::TY_ASTDump:
7474
case file_types::TY_RawSIL:
7575
case file_types::TY_LLVM_IR:
76-
case file_types::TY_ObjCHeader:
77-
case file_types::TY_CXXHeader:
76+
case file_types::TY_ClangHeader:
7877
case file_types::TY_AutolinkFile:
7978
case file_types::TY_ImportedModules:
8079
case file_types::TY_TBD:
@@ -132,8 +131,7 @@ bool file_types::isAfterLLVM(ID Id) {
132131
case file_types::TY_Dependencies:
133132
case file_types::TY_ASTDump:
134133
case file_types::TY_RawSIL:
135-
case file_types::TY_ObjCHeader:
136-
case file_types::TY_CXXHeader:
134+
case file_types::TY_ClangHeader:
137135
case file_types::TY_AutolinkFile:
138136
case file_types::TY_Image:
139137
case file_types::TY_dSYM:
@@ -183,8 +181,7 @@ bool file_types::isPartOfSwiftCompilation(ID Id) {
183181
case file_types::TY_LLVM_BC:
184182
case file_types::TY_Object:
185183
case file_types::TY_Dependencies:
186-
case file_types::TY_ObjCHeader:
187-
case file_types::TY_CXXHeader:
184+
case file_types::TY_ClangHeader:
188185
case file_types::TY_AutolinkFile:
189186
case file_types::TY_PCH:
190187
case file_types::TY_ImportedModules:

lib/Driver/Driver.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,8 +1981,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
19811981
if (Arg *A = Args.getLastArg(options::OPT_import_objc_header)) {
19821982
StringRef Value = A->getValue();
19831983
auto Ty = TC.lookupTypeForExtension(llvm::sys::path::extension(Value));
1984-
if (Ty == file_types::TY_ObjCHeader ||
1985-
Ty == file_types::TY_CXXHeader) {
1984+
if (Ty == file_types::TY_ClangHeader) {
19861985
auto *HeaderInput = C.createAction<InputAction>(*A, Ty);
19871986
StringRef PersistentPCHDir;
19881987
if (const Arg *A = Args.getLastArg(options::OPT_pch_output_dir)) {
@@ -2065,8 +2064,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
20652064
case file_types::TY_LLVM_IR:
20662065
case file_types::TY_LLVM_BC:
20672066
case file_types::TY_SerializedDiagnostics:
2068-
case file_types::TY_ObjCHeader:
2069-
case file_types::TY_CXXHeader:
2067+
case file_types::TY_ClangHeader:
20702068
case file_types::TY_ClangModuleFile:
20712069
case file_types::TY_SwiftDeps:
20722070
case file_types::TY_ExternalSwiftDeps:
@@ -3480,12 +3478,12 @@ void Driver::chooseObjectiveCHeaderOutputPath(Compilation &C,
34803478
StringRef workingDirectory,
34813479
CommandOutput *Output) const {
34823480

3483-
if (hasExistingAdditionalOutput(*Output, file_types::TY_ObjCHeader))
3481+
if (hasExistingAdditionalOutput(*Output, file_types::TY_ClangHeader))
34843482
return;
34853483

34863484
StringRef ObjCHeaderPath;
34873485
if (OutputMap) {
3488-
auto iter = OutputMap->find(file_types::TY_ObjCHeader);
3486+
auto iter = OutputMap->find(file_types::TY_ClangHeader);
34893487
if (iter != OutputMap->end())
34903488
ObjCHeaderPath = iter->second;
34913489
}
@@ -3495,13 +3493,13 @@ void Driver::chooseObjectiveCHeaderOutputPath(Compilation &C,
34953493
ObjCHeaderPath = A->getValue();
34963494

34973495
if (!ObjCHeaderPath.empty()) {
3498-
Output->setAdditionalOutputForType(file_types::TY_ObjCHeader,
3496+
Output->setAdditionalOutputForType(file_types::TY_ClangHeader,
34993497
ObjCHeaderPath);
35003498
} else {
35013499
// Put the header next to the primary output file.
35023500
// FIXME: That's not correct if the user /just/ passed -emit-header
35033501
// and not -emit-module.
3504-
addAuxiliaryOutput(C, *Output, file_types::TY_ObjCHeader,
3502+
addAuxiliaryOutput(C, *Output, file_types::TY_ClangHeader,
35053503
/*output file map*/ nullptr, workingDirectory);
35063504
}
35073505
}

lib/Driver/ToolChains.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,7 @@ const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
675675
case file_types::TY_Dependencies:
676676
case file_types::TY_SwiftModuleDocFile:
677677
case file_types::TY_SerializedDiagnostics:
678-
case file_types::TY_ObjCHeader:
679-
case file_types::TY_CXXHeader:
678+
case file_types::TY_ClangHeader:
680679
case file_types::TY_Image:
681680
case file_types::TY_SwiftDeps:
682681
case file_types::TY_ExternalSwiftDeps:
@@ -815,7 +814,7 @@ void ToolChain::JobContext::addFrontendSupplementaryOutputArguments(
815814
file_types::TY_SerializedDiagnostics,
816815
"-serialize-diagnostics-path");
817816

818-
if (addOutputsOfType(arguments, Output, Args, file_types::ID::TY_ObjCHeader,
817+
if (addOutputsOfType(arguments, Output, Args, file_types::ID::TY_ClangHeader,
819818
"-emit-objc-header-path")) {
820819
assert(OI.CompilerMode == OutputInfo::Mode::SingleCompile &&
821820
"The Swift tool should only emit an Obj-C header in single compile"
@@ -936,8 +935,7 @@ ToolChain::constructInvocation(const BackendJobAction &job,
936935
case file_types::TY_Dependencies:
937936
case file_types::TY_SwiftModuleDocFile:
938937
case file_types::TY_SerializedDiagnostics:
939-
case file_types::TY_ObjCHeader:
940-
case file_types::TY_CXXHeader:
938+
case file_types::TY_ClangHeader:
941939
case file_types::TY_Image:
942940
case file_types::TY_SwiftDeps:
943941
case file_types::TY_ExternalSwiftDeps:
@@ -1100,7 +1098,7 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job,
11001098
file_types::TY_SerializedDiagnostics,
11011099
"-serialize-diagnostics-path");
11021100
addOutputsOfType(Arguments, context.Output, context.Args,
1103-
file_types::TY_ObjCHeader, "-emit-objc-header-path");
1101+
file_types::TY_ClangHeader, "-emit-objc-header-path");
11041102
addOutputsOfType(Arguments, context.Output, context.Args, file_types::TY_TBD,
11051103
"-emit-tbd-path");
11061104

@@ -1309,7 +1307,7 @@ ToolChain::constructInvocation(const GeneratePCHJobAction &job,
13091307
file_types::TY_SerializedDiagnostics,
13101308
"-serialize-diagnostics-path");
13111309

1312-
addInputsOfType(Arguments, context.InputActions, file_types::TY_ObjCHeader);
1310+
addInputsOfType(Arguments, context.InputActions, file_types::TY_ClangHeader);
13131311
context.Args.AddLastArg(Arguments, options::OPT_index_store_path);
13141312

13151313
if (job.isPersistentPCH()) {

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ bool ArgsToFrontendOptionsConverter::convert(
274274
Opts.EnableIncrementalDependencyVerifier |= Args.hasArg(OPT_verify_incremental_dependencies);
275275
Opts.UseSharedResourceFolder = !Args.hasArg(OPT_use_static_resource_dir);
276276
Opts.DisableBuildingInterface = Args.hasArg(OPT_disable_building_interface);
277+
Opts.ExposePublicDeclsInClangHeader =
278+
Args.hasArg(OPT_clang_header_expose_public_decls);
277279

278280
computeImportObjCHeaderOptions();
279281
computeImplicitImportModuleNames(OPT_import_module, /*isTestable=*/false);
@@ -625,15 +627,10 @@ bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
625627
return true;
626628
}
627629
if (!FrontendOptions::canActionEmitClangHeader(Opts.RequestedAction) &&
628-
Opts.InputsAndOutputs.hasObjCHeaderOutputPath()) {
630+
Opts.InputsAndOutputs.hasClangHeaderOutputPath()) {
629631
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_header);
630632
return true;
631633
}
632-
if (!FrontendOptions::canActionEmitClangHeader(Opts.RequestedAction) &&
633-
Opts.InputsAndOutputs.hasCxxHeaderOutputPath()) {
634-
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_cxx_header);
635-
return true;
636-
}
637634
if (!FrontendOptions::canActionEmitLoadedModuleTrace(Opts.RequestedAction) &&
638635
Opts.InputsAndOutputs.hasLoadedModuleTracePath()) {
639636
Diags.diagnose(SourceLoc(),

0 commit comments

Comments
 (0)