[Module interface] Use features in module interface generation.

When generating a module interface, emit `#if` around any declarations
that are tied to specific, named language features. This allows module
interfaces to be processed by older Swift compilers that do not
support these newer features, such as async/await or actors.

The amount of effort required to correctly handle a new kind of
feature varies somewhat drastically based on the feature itself. The
"simple" case is where a particular declaration can only exist if a
feature is available. For example, and `async` declaration is fairly
easy to handle; a `@_marker` protocol's conformances are not.

Fixes rdar://73326633.
This commit is contained in:
Doug Gregor
2021-02-06 00:13:19 -08:00
parent 09f0838466
commit aa139a101c
9 changed files with 323 additions and 11 deletions

View File

@@ -248,6 +248,23 @@ class InheritedProtocolCollector {
return cache.getValue();
}
static bool canPrintProtocolTypeNormally(Type type, const Decl *D) {
if (!isPublicOrUsableFromInline(type))
return false;
// Extensions can print marker protocols.
if (isa<ExtensionDecl>(D))
return true;
ExistentialLayout layout = type->getExistentialLayout();
for (ProtocolType *protoTy : layout.getProtocols()) {
if (protoTy->getDecl()->isMarkerProtocol())
return false;
}
return true;
}
/// For each type in \p directlyInherited, classify the protocols it refers to
/// as included for printing or not, and record them in the appropriate
/// vectors.
@@ -259,7 +276,7 @@ class InheritedProtocolCollector {
if (!inheritedTy || !inheritedTy->isExistentialType())
continue;
bool canPrintNormally = isPublicOrUsableFromInline(inheritedTy);
bool canPrintNormally = canPrintProtocolTypeNormally(inheritedTy, D);
ExistentialLayout layout = inheritedTy->getExistentialLayout();
for (ProtocolType *protoTy : layout.getProtocols()) {
if (canPrintNormally)
@@ -452,6 +469,14 @@ public:
inherited->isSpecificProtocol(KnownProtocolKind::Actor))
return TypeWalker::Action::Continue;
#if false
// If the protocol is a marker protocol, print it separately.
if (inherited->isMarkerProtocol()) {
protocolsToPrint.push_back({inherited, protoAndAvailability.second});
return TypeWalker::Action::SkipChildren;
}
#endif
if (inherited->isSPI() && !printOptions.PrintSPIs)
return TypeWalker::Action::Continue;
@@ -471,6 +496,9 @@ public:
StreamPrinter printer(out);
ProtocolDecl *proto = protoAndAvailability.first;
bool haveFeatureChecks = printOptions.PrintCompatibilityFeatureChecks &&
printCompatibilityFeatureChecksPre(printer, proto);
// FIXME: Shouldn't this be an implicit conversion?
TinyPtrVector<const DeclAttribute *> attrs;
attrs.insert(attrs.end(), protoAndAvailability.second.begin(),
@@ -485,7 +513,12 @@ public:
proto->getDeclaredInterfaceType()->print(printer, printOptions);
printer << " {}\n";
printer << " {}";
if (haveFeatureChecks)
printCompatibilityFeatureChecksPost(printer);
else
printer << "\n";
}
}