Skip to content

Commit d68eae6

Browse files
committed
[PrintAsObjC] Fix caching of various special names
All persistent info should live in the parent DeclAndTypePrinter, not in the Implementation helper class. We can move the rarely-used members out too even if they're not lazily-populated.
1 parent 6058797 commit d68eae6

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,18 @@ class DeclAndTypePrinter::Implementation
9999
friend ASTVisitor;
100100
friend TypeVisitor;
101101

102+
// These first two members are accessible through 'owningPrinter',
103+
// but it makes the code simpler to have them here too.
102104
ModuleDecl &M;
103105
raw_ostream &os;
104-
const DelayedMemberSet &delayedMembers;
105-
AccessLevel minRequiredAccess;
106+
DeclAndTypePrinter &owningPrinter;
106107

107108
SmallVector<const FunctionType *, 4> openFunctionTypes;
108109

109-
using NameAndOptional = std::pair<StringRef, bool>;
110-
llvm::DenseMap<std::pair<Identifier, Identifier>, NameAndOptional>
111-
specialNames;
112-
113-
// Cached for convenience.
114-
Identifier ID_CFTypeRef;
115-
Optional<Type> NSCopyingType;
116-
117110
public:
118111
explicit Implementation(ModuleDecl &mod, raw_ostream &out,
119-
const DelayedMemberSet &delayed, AccessLevel access)
120-
: M(mod), os(out), delayedMembers(delayed), minRequiredAccess(access) {}
112+
DeclAndTypePrinter &owner)
113+
: M(mod), os(out), owningPrinter(owner) {}
121114

122115
void print(const Decl *D) {
123116
PrettyStackTraceDecl trace("printing", D);
@@ -156,8 +149,7 @@ class DeclAndTypePrinter::Implementation
156149
}
157150

158151
bool shouldInclude(const ValueDecl *VD) {
159-
return isVisibleToObjC(VD, minRequiredAccess) &&
160-
!VD->getAttrs().hasAttribute<ImplementationOnlyAttr>();
152+
return owningPrinter.shouldInclude(VD);
161153
}
162154

163155
private:
@@ -195,7 +187,7 @@ class DeclAndTypePrinter::Implementation
195187
continue;
196188
if (isa<AccessorDecl>(VD))
197189
continue;
198-
if (!AllowDelayed && delayedMembers.count(VD)) {
190+
if (!AllowDelayed && owningPrinter.delayedMembers.count(VD)) {
199191
os << "// '" << VD->getFullName() << "' below\n";
200192
continue;
201193
}
@@ -619,7 +611,7 @@ class DeclAndTypePrinter::Implementation
619611
// Swift designated initializers are Objective-C designated initializers.
620612
if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
621613
if (ctor->hasStubImplementation()
622-
|| ctor->getFormalAccess() < minRequiredAccess) {
614+
|| ctor->getFormalAccess() < owningPrinter.minRequiredAccess) {
623615
// This will only be reached if the overridden initializer has the
624616
// required access
625617
os << " SWIFT_UNAVAILABLE";
@@ -1062,9 +1054,9 @@ class DeclAndTypePrinter::Implementation
10621054
if (!TAD || !TAD->hasClangNode())
10631055
return false;
10641056

1065-
if (ID_CFTypeRef.empty())
1066-
ID_CFTypeRef = M.getASTContext().getIdentifier("CFTypeRef");
1067-
return TAD->getName() == ID_CFTypeRef;
1057+
if (owningPrinter.ID_CFTypeRef.empty())
1058+
owningPrinter.ID_CFTypeRef = M.getASTContext().getIdentifier("CFTypeRef");
1059+
return TAD->getName() == owningPrinter.ID_CFTypeRef;
10681060
}
10691061

10701062
/// Returns true if \p ty can be used with Objective-C reference-counting
@@ -1105,8 +1097,10 @@ class DeclAndTypePrinter::Implementation
11051097

11061098
ASTContext &ctx = M.getASTContext();
11071099
bool isSettable = VD->isSettable(nullptr);
1108-
if (isSettable && !ctx.isAccessControlDisabled())
1109-
isSettable = (VD->getSetterFormalAccess() >= minRequiredAccess);
1100+
if (isSettable && !ctx.isAccessControlDisabled()) {
1101+
isSettable =
1102+
(VD->getSetterFormalAccess() >= owningPrinter.minRequiredAccess);
1103+
}
11101104
if (!isSettable)
11111105
os << ", readonly";
11121106

@@ -1393,19 +1387,19 @@ class DeclAndTypePrinter::Implementation
13931387
else if (swiftNominal == ctx.getDictionaryDecl() &&
13941388
isNSObjectOrAnyHashable(ctx, typeArgs[0])) {
13951389
if (ModuleDecl *M = ctx.getLoadedModule(ctx.Id_Foundation)) {
1396-
if (!NSCopyingType) {
1390+
if (!owningPrinter.NSCopyingType) {
13971391
SmallVector<ValueDecl *, 1> decls;
13981392
M->lookupQualified(M, ctx.getIdentifier("NSCopying"),
13991393
NL_OnlyTypes, decls);
14001394
if (decls.size() == 1 && isa<ProtocolDecl>(decls[0])) {
1401-
NSCopyingType = cast<ProtocolDecl>(decls[0])
1395+
owningPrinter.NSCopyingType = cast<ProtocolDecl>(decls[0])
14021396
->getDeclaredInterfaceType();
14031397
} else {
1404-
NSCopyingType = Type();
1398+
owningPrinter.NSCopyingType = Type();
14051399
}
14061400
}
1407-
if (*NSCopyingType) {
1408-
rewrittenArgsBuf[0] = *NSCopyingType;
1401+
if (*owningPrinter.NSCopyingType) {
1402+
rewrittenArgsBuf[0] = *owningPrinter.NSCopyingType;
14091403
rewrittenArgsBuf[1] = typeArgs[1];
14101404
typeArgs = rewrittenArgsBuf;
14111405
}
@@ -1452,6 +1446,7 @@ class DeclAndTypePrinter::Implementation
14521446
///
14531447
/// Returns null if the name is not one of these known types.
14541448
const NameAndOptional *getKnownTypeInfo(const TypeDecl *typeDecl) {
1449+
auto &specialNames = owningPrinter.specialNames;
14551450
if (specialNames.empty()) {
14561451
ASTContext &ctx = M.getASTContext();
14571452
#define MAP(SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY) \
@@ -2035,11 +2030,12 @@ class DeclAndTypePrinter::Implementation
20352030
};
20362031

20372032
auto DeclAndTypePrinter::getImpl() -> Implementation {
2038-
return Implementation(M, os, delayedMembers, minRequiredAccess);
2033+
return Implementation(M, os, *this);
20392034
}
20402035

20412036
bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
2042-
return getImpl().shouldInclude(VD);
2037+
return isVisibleToObjC(VD, minRequiredAccess) &&
2038+
!VD->getAttrs().hasAttribute<ImplementationOnlyAttr>();
20432039
}
20442040

20452041
void DeclAndTypePrinter::print(const Decl *D) {

lib/PrintAsObjC/DeclAndTypePrinter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,21 @@ class DeclAndTypePrinter {
3131

3232
private:
3333
class Implementation;
34+
friend class Implementation;
3435

3536
ModuleDecl &M;
3637
raw_ostream &os;
3738
const DelayedMemberSet &delayedMembers;
3839
AccessLevel minRequiredAccess;
3940

41+
using NameAndOptional = std::pair<StringRef, bool>;
42+
llvm::DenseMap<std::pair<Identifier, Identifier>, NameAndOptional>
43+
specialNames;
44+
45+
// Cached for convenience.
46+
Identifier ID_CFTypeRef;
47+
Optional<Type> NSCopyingType;
48+
4049
Implementation getImpl();
4150

4251
public:

0 commit comments

Comments
 (0)