Skip to content

Commit 9d71639

Browse files
committed
Add an interface mode getter/setter in PrintOptions and ModuleInterfaceOptions
Update args parsing
1 parent e5ca8e5 commit 9d71639

File tree

7 files changed

+62
-43
lines changed

7 files changed

+62
-43
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,6 @@ struct ShouldPrintChecker {
118118
virtual ~ShouldPrintChecker() = default;
119119
};
120120

121-
enum class InterfaceMode : uint8_t {
122-
Public, // prints public/inlinable decls
123-
Private, // prints SPI and public/inlinable decls
124-
Package // prints package, SPI, and public/inlinable decls
125-
};
126-
127121
/// Options for printing AST nodes.
128122
///
129123
/// A default-constructed PrintOptions is suitable for printing to users;
@@ -189,6 +183,25 @@ struct PrintOptions {
189183
/// Whether to print enum raw value expressions.
190184
EnumRawValueMode EnumRawValues = EnumRawValueMode::Skip;
191185

186+
enum class InterfaceMode : uint8_t {
187+
Public, // prints public/inlinable decls
188+
Private, // prints SPI and public/inlinable decls
189+
Package // prints package, SPI, and public/inlinable decls
190+
};
191+
192+
InterfaceMode InterfaceContentKind;
193+
194+
bool printPublicInterface() const {
195+
return InterfaceContentKind == InterfaceMode::Public;
196+
}
197+
bool printPackageInterface() const {
198+
return InterfaceContentKind == InterfaceMode::Package;
199+
}
200+
201+
void setInterfaceMode(InterfaceMode mode) {
202+
InterfaceContentKind = mode;
203+
}
204+
192205
/// Whether to prefer printing TypeReprs instead of Types,
193206
/// if a TypeRepr is available. This allows us to print the original
194207
/// spelling of the type name.
@@ -305,8 +318,6 @@ struct PrintOptions {
305318
/// Whether to skip keywords with a prefix of underscore such as __consuming.
306319
bool SkipUnderscoredKeywords = false;
307320

308-
InterfaceMode InterfaceContentMode;
309-
310321
/// Prints type variables and unresolved types in an expanded notation suitable
311322
/// for debugging.
312323
bool PrintTypesForDebugging = false;
@@ -689,7 +700,7 @@ struct PrintOptions {
689700
static PrintOptions printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
690701
bool preferTypeRepr,
691702
bool printFullConvention,
692-
InterfaceMode interfaceContentMode,
703+
InterfaceMode interfaceMode,
693704
bool useExportedModuleNames,
694705
bool aliasModuleNames,
695706
llvm::SmallSet<StringRef, 4>

include/swift/Frontend/ModuleInterfaceSupport.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ struct ModuleInterfaceOptions {
5555
/// e.g. -package-name PACKAGE_ID
5656
std::string IgnorablePrivateFlags;
5757

58-
/// Prints package, SPIs, or public/inlinable decls depending on the mode.
59-
InterfaceMode InterfaceContentMode = InterfaceMode::Public;
60-
6158
/// Print imports with both @_implementationOnly and @_spi, only applies
6259
/// when PrintSPIs is true.
6360
bool ExperimentalSPIImports = false;
@@ -70,6 +67,18 @@ struct ModuleInterfaceOptions {
7067

7168
/// A list of modules we shouldn't import in the public interfaces.
7269
std::vector<std::string> ModulesToSkipInPublicInterface;
70+
71+
/// A mode which decides whether the printed interface contains package, SPIs, or public/inlinable declarations.
72+
PrintOptions::InterfaceMode InterfaceContentMode = PrintOptions::InterfaceMode::Public;
73+
bool printPublicInterface() const {
74+
return InterfaceContentMode == PrintOptions::InterfaceMode::Public;
75+
}
76+
bool printPackageInterface() const {
77+
return InterfaceContentMode == PrintOptions::InterfaceMode::Package;
78+
}
79+
void setInterfaceMode(PrintOptions::InterfaceMode mode) {
80+
InterfaceContentMode = mode;
81+
}
7382
};
7483

7584
extern version::Version InterfaceFormatVersion;

lib/AST/ASTPrinter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static bool shouldTypeCheck(const PrintOptions &options) {
174174
PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
175175
bool preferTypeRepr,
176176
bool printFullConvention,
177-
InterfaceMode interfaceContentMode,
177+
InterfaceMode interfaceMode,
178178
bool useExportedModuleNames,
179179
bool aliasModuleNames,
180180
llvm::SmallSet<StringRef, 4>
@@ -206,7 +206,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
206206
result.PrintFunctionRepresentationAttrs =
207207
PrintOptions::FunctionRepresentationMode::Full;
208208
result.AlwaysTryPrintParameterLabels = true;
209-
result.InterfaceContentMode = interfaceContentMode;
209+
result.InterfaceContentKind = interfaceMode;
210210
result.DesugarExistentialConstraint = true;
211211

212212
// We should print __consuming, __owned, etc for the module interface file.
@@ -238,7 +238,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
238238
return false;
239239

240240
// Skip SPI decls if `PrintSPIs`.
241-
if (options.InterfaceContentMode == InterfaceMode::Public && D->isSPI())
241+
if (options.printPublicInterface() && D->isSPI())
242242
return false;
243243

244244
if (auto *VD = dyn_cast<ValueDecl>(D)) {
@@ -252,7 +252,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
252252
if (contributesToParentTypeStorage(ASD))
253253
return true;
254254

255-
if (options.InterfaceContentMode != InterfaceMode::Package || !isPackage(VD))
255+
if (!options.printPackageInterface() || !isPackage(VD))
256256
return false;
257257
}
258258

@@ -288,7 +288,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
288288

289289
for (const Requirement &req : ED->getGenericRequirements()) {
290290
if (!isPublicOrUsableFromInline(req.getFirstType())) {
291-
if (options.InterfaceContentMode != InterfaceMode::Package || !isPackage(req.getSecondType()))
291+
if (!options.printPackageInterface() || !isPackage(req.getSecondType()))
292292
return false;
293293
}
294294

@@ -297,7 +297,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
297297
case RequirementKind::Superclass:
298298
case RequirementKind::SameType:
299299
if (!isPublicOrUsableFromInline(req.getSecondType())) {
300-
if (options.InterfaceContentMode != InterfaceMode::Package || !isPackage(req.getSecondType()))
300+
if (!options.printPackageInterface() || !isPackage(req.getSecondType()))
301301
return false;
302302
}
303303
break;
@@ -1205,7 +1205,7 @@ void PrintAST::printAttributes(const Decl *D) {
12051205
}
12061206

12071207
// Add SPIs to both private and package interfaces
1208-
if (Options.InterfaceContentMode != InterfaceMode::Public &&
1208+
if (!Options.printPublicInterface() &&
12091209
DeclAttribute::canAttributeAppearOnDeclKind(
12101210
DAK_SPIAccessControl, D->getKind())) {
12111211
interleave(D->getSPIGroups(),
@@ -1294,7 +1294,7 @@ static bool mustPrintPropertyName(VarDecl *decl, const PrintOptions &opts) {
12941294
if (contributesToParentTypeStorage(decl)) return true;
12951295

12961296
// Print a package decl if in print package mode (for .package.swiftinterface),
1297-
if (opts.InterfaceContentMode == InterfaceMode::Package && isPackage(decl))
1297+
if (opts.printPackageInterface() && isPackage(decl))
12981298
return true;
12991299

13001300
// If it's public or @usableFromInline, we must print the name because it's a
@@ -6007,10 +6007,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60076007
Filter |= ModuleDecl::ImportFilterKind::Default;
60086008

60096009
// For private or package swiftinterfaces, also look through @_spiOnly imports.
6010-
if (Options.InterfaceContentMode != InterfaceMode::Public)
6010+
if (!Options.printPublicInterface())
60116011
Filter |= ModuleDecl::ImportFilterKind::SPIOnly;
60126012
// Consider package import for package interface
6013-
if (Options.InterfaceContentMode == InterfaceMode::Package)
6013+
if (Options.printPackageInterface())
60146014
Filter |= ModuleDecl::ImportFilterKind::PackageOnly;
60156015

60166016
SmallVector<ImportedModule, 4> Imports;

lib/AST/Attr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
10481048
return true;
10491049

10501050
case DAK_SPIAccessControl: {
1051-
if (Options.InterfaceContentMode == InterfaceMode::Public) return false;
1051+
if (Options.printPublicInterface()) return false;
10521052

10531053
auto spiAttr = static_cast<const SPIAccessControlAttr*>(this);
10541054
interleave(spiAttr->getSPIGroups(),
@@ -1100,7 +1100,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
11001100
auto Attr = cast<AvailableAttr>(this);
11011101
if (Options.SuppressNoAsyncAvailabilityAttr && Attr->isNoAsync())
11021102
return false;
1103-
if (Options.InterfaceContentMode == InterfaceMode::Public && Attr->IsSPI) {
1103+
if (Options.printPublicInterface() && Attr->IsSPI) {
11041104
assert(Attr->hasPlatform());
11051105
assert(Attr->Introduced.has_value());
11061106
Printer.printAttrName("@available");
@@ -1196,7 +1196,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
11961196
auto *attr = cast<SpecializeAttr>(this);
11971197
// Don't print the _specialize attribute if it is marked spi and we are
11981198
// asked to skip SPI.
1199-
if (Options.InterfaceContentMode == InterfaceMode::Public && !attr->getSPIGroups().empty())
1199+
if (Options.printPublicInterface() && !attr->getSPIGroups().empty())
12001200
return false;
12011201

12021202
// Don't print the _specialize attribute if we are asked to skip the ones

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
417417
if (const Arg *A = Args.getLastArg(OPT_library_level)) {
418418
StringRef contents = A->getValue();
419419
if (contents == "spi") {
420-
Opts.InterfaceContentMode = InterfaceMode::Private;
420+
Opts.setInterfaceMode(PrintOptions::InterfaceMode::Private);
421421
}
422422
}
423423
for (auto val: Args.getAllArgValues(OPT_skip_import_in_public_interface)) {
@@ -622,11 +622,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
622622
Opts.EnableAccessControl
623623
= A->getOption().matches(OPT_enable_access_control);
624624
}
625-
if (auto A = Args.getLastArg(OPT_experimental_package_interface_load,
626-
OPT_experimental_package_interface_load)) {
627-
Opts.EnablePackageInterfaceLoad
628-
= A->getOption().matches(OPT_experimental_package_interface_load);
629-
}
630625

631626
Opts.ForceWorkaroundBrokenModules
632627
|= Args.hasArg(OPT_force_workaround_broken_modules);
@@ -638,6 +633,10 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
638633
Opts.EnableExperimentalStringProcessing = true;
639634
}
640635

636+
if (Args.hasArg(OPT_experimental_package_interface_load)) {
637+
Opts.EnablePackageInterfaceLoad = true;
638+
}
639+
641640
// Experimental string processing.
642641
if (auto A = Args.getLastArg(OPT_enable_experimental_string_processing,
643642
OPT_disable_experimental_string_processing)) {

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
7171

7272
ModuleDecl::ImportFilter filter = {ModuleDecl::ImportFilterKind::Default,
7373
ModuleDecl::ImportFilterKind::Exported};
74-
if (Opts.InterfaceContentMode != InterfaceMode::Public)
74+
if (!Opts.printPublicInterface())
7575
filter |= ModuleDecl::ImportFilterKind::SPIOnly;
76-
if (Opts.InterfaceContentMode == InterfaceMode::Package)
76+
if (Opts.printPackageInterface())
7777
filter |= ModuleDecl::ImportFilterKind::PackageOnly;
7878

7979
SmallVector<ImportedModule> imports;
@@ -101,7 +101,7 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
101101
<< Opts.IgnorableFlags << "\n";
102102
}
103103

104-
auto hasPrivateIgnorableFlags = Opts.InterfaceContentMode != InterfaceMode::Public && !Opts.IgnorablePrivateFlags.empty();
104+
auto hasPrivateIgnorableFlags = !Opts.printPublicInterface() && !Opts.IgnorablePrivateFlags.empty();
105105
if (hasPrivateIgnorableFlags) {
106106
out << "// " SWIFT_MODULE_FLAGS_IGNORABLE_PRIVATE_KEY ": "
107107
<< Opts.IgnorablePrivateFlags << "\n";
@@ -247,7 +247,7 @@ static void printImports(raw_ostream &out,
247247
// imports only if they are also SPI. First, list all implementation-only imports and
248248
// filter them later.
249249
llvm::SmallSet<ImportedModule, 4, ImportedModule::Order> ioiImportSet;
250-
if (Opts.InterfaceContentMode != InterfaceMode::Public && Opts.ExperimentalSPIImports) {
250+
if (!Opts.printPublicInterface() && Opts.ExperimentalSPIImports) {
251251

252252
SmallVector<ImportedModule, 4> ioiImports, allImports;
253253
M->getImportedModules(ioiImports,
@@ -268,7 +268,7 @@ static void printImports(raw_ostream &out,
268268

269269
/// Collect @_spiOnly imports that are not imported elsewhere publicly.
270270
llvm::SmallSet<ImportedModule, 4, ImportedModule::Order> spiOnlyImportSet;
271-
if (Opts.InterfaceContentMode != InterfaceMode::Public) {
271+
if (!Opts.printPublicInterface()) {
272272
SmallVector<ImportedModule, 4> spiOnlyImports, otherImports;
273273
M->getImportedModules(spiOnlyImports,
274274
ModuleDecl::ImportFilterKind::SPIOnly);
@@ -335,7 +335,7 @@ static void printImports(raw_ostream &out,
335335
if (publicImportSet.count(import))
336336
out << "@_exported ";
337337

338-
if (Opts.InterfaceContentMode != InterfaceMode::Public) {
338+
if (!Opts.printPublicInterface()) {
339339
// An import visible in the private or package swiftinterface only.
340340
//
341341
// In the long term, we want to print this attribute for consistency and
@@ -623,7 +623,7 @@ class InheritedProtocolCollector {
623623
return;
624624

625625
// Skip SPI extensions in the public interface.
626-
if (printOptions.InterfaceContentMode == InterfaceMode::Public && extension->isSPI())
626+
if (printOptions.printPublicInterface() && extension->isSPI())
627627
return;
628628

629629
const NominalTypeDecl *nominal = extension->getExtendedNominal();
@@ -704,7 +704,7 @@ class InheritedProtocolCollector {
704704
inherited->isSpecificProtocol(KnownProtocolKind::Actor))
705705
return TypeWalker::Action::SkipChildren;
706706

707-
if (inherited->isSPI() && printOptions.InterfaceContentMode == InterfaceMode::Public)
707+
if (inherited->isSPI() && printOptions.printPublicInterface())
708708
return TypeWalker::Action::Continue;
709709

710710
if (isPublicOrUsableFromInline(inherited) &&
@@ -784,7 +784,7 @@ class InheritedProtocolCollector {
784784
return false;
785785
assert(nominal->isGenericContext());
786786

787-
if (printOptions.InterfaceContentMode != InterfaceMode::Public)
787+
if (!printOptions.printPublicInterface())
788788
out << "@_spi(" << DummyProtocolName << ")\n";
789789
out << "@available(*, unavailable)\nextension ";
790790
nominal->getDeclaredType().print(out, printOptions);
@@ -825,7 +825,7 @@ bool swift::emitSwiftInterface(raw_ostream &out,
825825

826826
printImports(out, Opts, M, aliasModuleNamesTargets);
827827

828-
bool useExportedModuleNames = Opts.InterfaceContentMode == InterfaceMode::Public;
828+
bool useExportedModuleNames = Opts.printPublicInterface();
829829

830830
const PrintOptions printOptions = PrintOptions::printSwiftInterfaceFile(
831831
M, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention,

lib/FrontendTool/FrontendTool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
987987
if (opts.InputsAndOutputs.hasPrivateModuleInterfaceOutputPath()) {
988988
// Copy the settings from the module interface to add SPI printing.
989989
ModuleInterfaceOptions privOpts = Invocation.getModuleInterfaceOptions();
990-
privOpts.InterfaceContentMode = InterfaceMode::Private;
990+
privOpts.setInterfaceMode(PrintOptions::InterfaceMode::Private);
991991
privOpts.ModulesToSkipInPublicInterface.clear();
992992

993993
hadAnyError |= printModuleInterfaceIfNeeded(
@@ -1000,7 +1000,7 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
10001000
if (opts.InputsAndOutputs.hasPackageModuleInterfaceOutputPath()) {
10011001
// Copy the settings from the module interface to add package decl printing.
10021002
ModuleInterfaceOptions pkgOpts = Invocation.getModuleInterfaceOptions();
1003-
pkgOpts.InterfaceContentMode = InterfaceMode::Package;
1003+
pkgOpts.setInterfaceMode(PrintOptions::InterfaceMode::Package);
10041004
pkgOpts.ModulesToSkipInPublicInterface.clear();
10051005

10061006
hadAnyError |= printModuleInterfaceIfNeeded(

0 commit comments

Comments
 (0)