[InterfaceGen] Print property initializers in resilient, fixed-layout types (#19619)

Augment the ASTPrinter to print the name and text of initializer expressions if
a property has an initializer and the type is @_fixed_layout and resides in a resilient module, and serialize the text for partial modules.

With this change, all .swiftinterface files in the project (except for SwiftLang) compile to swiftmodules on macOS.

rdar://43774580
rdar://43812188
This commit is contained in:
Harlan
2018-10-05 18:21:46 -07:00
committed by GitHub
parent 690c6c374f
commit 2c86e3249c
10 changed files with 262 additions and 59 deletions

View File

@@ -1181,18 +1181,19 @@ unsigned PatternBindingDecl::getPatternEntryIndexForVarDecl(const VarDecl *VD) c
}
SourceRange PatternBindingEntry::getOrigInitRange() const {
auto Init = InitAndFlags.getPointer();
auto Init = getInitAsWritten();
return Init ? Init->getSourceRange() : SourceRange();
}
void PatternBindingEntry::setInit(Expr *E) {
auto F = InitAndFlags.getInt();
auto F = PatternAndFlags.getInt();
if (E) {
InitAndFlags.setInt(F - Flags::Removed);
InitAndFlags.setPointer(E);
PatternAndFlags.setInt(F - Flags::Removed);
} else {
InitAndFlags.setInt(F | Flags::Removed);
PatternAndFlags.setInt(F | Flags::Removed);
}
InitExpr.Node = E;
InitContextAndIsText.setInt(false);
}
VarDecl *PatternBindingEntry::getAnchoringVarDecl() const {
@@ -1225,6 +1226,25 @@ SourceRange PatternBindingEntry::getSourceRange(bool omitAccessors) const {
return SourceRange(startLoc, endLoc);
}
bool PatternBindingEntry::hasInitStringRepresentation() const {
if (InitContextAndIsText.getInt())
return !InitStringRepresentation.empty();
return getInit() && getInit()->getSourceRange().isValid();
}
StringRef PatternBindingEntry::getInitStringRepresentation(
SmallVectorImpl<char> &scratch) const {
assert(hasInitStringRepresentation() &&
"must check if pattern has string representation");
if (InitContextAndIsText.getInt() && !InitStringRepresentation.empty())
return InitStringRepresentation;
auto &sourceMgr = getAnchoringVarDecl()->getASTContext().SourceMgr;
auto init = getInit();
return extractInlinableText(sourceMgr, init, scratch);
}
SourceRange PatternBindingDecl::getSourceRange() const {
SourceLoc startLoc = getStartLoc();
SourceLoc endLoc = getPatternList().back().getSourceRange().End;
@@ -1279,6 +1299,19 @@ VarDecl *PatternBindingDecl::getSingleVar() const {
return nullptr;
}
bool VarDecl::isInitExposedToClients() const {
auto parent = dyn_cast<NominalTypeDecl>(getDeclContext());
if (!parent) return false;
if (!hasInitialValue())
return false;
if (isStatic())
return false;
if (!parent->getAttrs().hasAttribute<FixedLayoutAttr>())
return false;
auto *module = parent->getModuleContext();
return module->getResilienceStrategy() == ResilienceStrategy::Resilient;
}
/// Check whether the given type representation will be
/// default-initializable.
static bool isDefaultInitializable(const TypeRepr *typeRepr) {