|
| 1 | +//=== ShouldPrintForParseableInterface.cpp - Parseable Interface filtering ===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "ShouldPrintForParseableInterface.h" |
| 14 | +#include "swift/AST/ASTWalker.h" |
| 15 | +#include "swift/AST/TypeWalker.h" |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/AST/Module.h" |
| 18 | +#include "swift/AST/Type.h" |
| 19 | + |
| 20 | +using namespace swift; |
| 21 | + |
| 22 | +bool swift::isPublicOrUsableFromInline(const ValueDecl *VD) { |
| 23 | + AccessScope scope = |
| 24 | + VD->getFormalAccessScope(/*useDC*/nullptr, |
| 25 | + /*treatUsableFromInlineAsPublic*/true); |
| 26 | + return scope.isPublic(); |
| 27 | +} |
| 28 | + |
| 29 | +bool swift::contributesToParentTypeStorage(const AbstractStorageDecl *ASD) { |
| 30 | + auto *DC = ASD->getDeclContext()->getAsDecl(); |
| 31 | + if (!DC) return false; |
| 32 | + auto *ND = dyn_cast<NominalTypeDecl>(DC); |
| 33 | + if (!ND) return false; |
| 34 | + return !ND->isResilient() && ASD->hasStorage() && !ASD->isStatic(); |
| 35 | +} |
| 36 | + |
| 37 | +/// Finds type decls that need to be printed to ensure the layout of public |
| 38 | +/// types are correct for non-resilient types. |
| 39 | +/// |
| 40 | +/// For example: |
| 41 | +/// |
| 42 | +/// ``` |
| 43 | +/// private struct B {} |
| 44 | +/// public struct A { |
| 45 | +/// internal let b: B |
| 46 | +/// } |
| 47 | +/// ``` |
| 48 | +/// Since `A`'s size depends on `B`, we need to print `B` even though it's |
| 49 | +/// private. `B`'s children will also be checked for types that contribute to |
| 50 | +/// its layout, and so on. |
| 51 | +class FindReferencedNonPublicTypeDecls: public ASTWalker { |
| 52 | + using TypeDecls = SmallPtrSetImpl<TypeDecl *>; |
| 53 | + |
| 54 | + /// Finds all nominal types or typealiases referenced in the type being |
| 55 | + /// walked. |
| 56 | + class FindTypeDecls: public TypeWalker { |
| 57 | + TypeDecls &foundDecls; |
| 58 | + public: |
| 59 | + FindTypeDecls(TypeDecls &foundDecls): foundDecls(foundDecls) {} |
| 60 | + |
| 61 | + void addIfNonPublic(TypeDecl *typeDecl) { |
| 62 | + if (isPublicOrUsableFromInline(typeDecl)) |
| 63 | + return; |
| 64 | + foundDecls.insert(typeDecl); |
| 65 | + } |
| 66 | + |
| 67 | + /// Finds all nominal types or typealiases referenced by the |
| 68 | + Action walkToTypePre(Type type) override { |
| 69 | + if (auto nominal = |
| 70 | + dyn_cast<NominalOrBoundGenericNominalType>(type.getPointer())) |
| 71 | + addIfNonPublic(nominal->getDecl()); |
| 72 | + if (auto alias = dyn_cast<NameAliasType>(type.getPointer())) { |
| 73 | + // Add the typealias to the found decls |
| 74 | + auto decl = alias->getDecl(); |
| 75 | + addIfNonPublic(decl); |
| 76 | + |
| 77 | + // Also walk into the RHS of the typealias to find extra types we need. |
| 78 | + decl->getUnderlyingTypeLoc().getType().walk(*this); |
| 79 | + } |
| 80 | + return Action::Continue; |
| 81 | + } |
| 82 | + }; |
| 83 | + FindTypeDecls findTypeDecls; |
| 84 | +public: |
| 85 | + FindReferencedNonPublicTypeDecls(TypeDecls &foundDecls): |
| 86 | + findTypeDecls(foundDecls) {} |
| 87 | + bool walkToDeclPre(Decl *decl) { |
| 88 | + auto asd = dyn_cast<AbstractStorageDecl>(decl); |
| 89 | + if (!asd) return true; |
| 90 | + if (contributesToParentTypeStorage(asd)) |
| 91 | + asd->getInterfaceType().walk(findTypeDecls); |
| 92 | + return true; |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +std::shared_ptr<ShouldPrintForParseableInterface> |
| 97 | +ShouldPrintForParseableInterface::create(ModuleDecl *module) { |
| 98 | + auto shouldPrint = std::make_shared<ShouldPrintForParseableInterface>(); |
| 99 | + |
| 100 | + // Walk the module to find stored properties whose type is non-public but |
| 101 | + // required to reconstruct the layout of their containing type. |
| 102 | + // We need to keep track of these type declarations, so we make sure to |
| 103 | + // print them. |
| 104 | + auto findTypeDecls = FindReferencedNonPublicTypeDecls( |
| 105 | + shouldPrint->referencedNonPublicTypeDecls); |
| 106 | + module->walk(findTypeDecls); |
| 107 | + return shouldPrint; |
| 108 | +} |
| 109 | + |
| 110 | +bool ShouldPrintForParseableInterface::shouldPrint( |
| 111 | + const Decl *D, const PrintOptions &options) { |
| 112 | + // Skip anything that isn't 'public' or '@usableFromInline'. |
| 113 | + if (auto *VD = dyn_cast<ValueDecl>(D)) { |
| 114 | + if (!isPublicOrUsableFromInline(VD)) { |
| 115 | + // We do want to print private stored properties, without their |
| 116 | + // original names present. |
| 117 | + if (auto *ASD = dyn_cast<AbstractStorageDecl>(VD)) |
| 118 | + if (contributesToParentTypeStorage(ASD)) |
| 119 | + return true; |
| 120 | + |
| 121 | + // If this is a type that contributes to the layout of any |
| 122 | + // types in this module, then we should print it. |
| 123 | + if (auto typeDecl = dyn_cast<TypeDecl>(VD)) |
| 124 | + return referencedNonPublicTypeDecls.count(typeDecl) != 0; |
| 125 | + return false; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Skip extensions that extend things we wouldn't print. |
| 130 | + if (auto *ED = dyn_cast<ExtensionDecl>(D)) { |
| 131 | + if (!shouldPrint(ED->getExtendedNominal(), options)) |
| 132 | + return false; |
| 133 | + // FIXME: We also need to check the generic signature for constraints |
| 134 | + // that we can't reference. |
| 135 | + } |
| 136 | + |
| 137 | + // Skip typealiases that just redeclare generic parameters. |
| 138 | + if (auto *alias = dyn_cast<TypeAliasDecl>(D)) { |
| 139 | + if (alias->isImplicit()) { |
| 140 | + const Decl *parent = |
| 141 | + D->getDeclContext()->getAsDecl(); |
| 142 | + if (auto *genericCtx = parent->getAsGenericContext()) { |
| 143 | + bool matchesGenericParam = |
| 144 | + llvm::any_of(genericCtx->getInnermostGenericParamTypes(), |
| 145 | + [alias](const GenericTypeParamType *param) { |
| 146 | + return param->getName() == alias->getName(); |
| 147 | + }); |
| 148 | + if (matchesGenericParam) |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return ShouldPrintChecker::shouldPrint(D, options); |
| 155 | +} |
0 commit comments