Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 1 | //===--- TransZeroOutPropsInDealloc.cpp - Tranformations to ARC mode ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // removeZeroOutPropsInDealloc: |
| 11 | // |
| 12 | // Removes zero'ing out "strong" @synthesized properties in a -dealloc method. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "Transforms.h" |
| 17 | #include "Internals.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | using namespace arcmt; |
| 21 | using namespace trans; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class ZeroOutInDeallocRemover : |
| 26 | public RecursiveASTVisitor<ZeroOutInDeallocRemover> { |
| 27 | typedef RecursiveASTVisitor<ZeroOutInDeallocRemover> base; |
| 28 | |
| 29 | MigrationPass &Pass; |
| 30 | |
| 31 | llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*> SynthesizedProperties; |
| 32 | ImplicitParamDecl *SelfD; |
| 33 | ExprSet Removables; |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame] | 34 | Selector FinalizeSel; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 35 | |
| 36 | public: |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame] | 37 | ZeroOutInDeallocRemover(MigrationPass &pass) : Pass(pass), SelfD(0) { |
| 38 | FinalizeSel = |
| 39 | Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize")); |
| 40 | } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 41 | |
| 42 | bool VisitObjCMessageExpr(ObjCMessageExpr *ME) { |
| 43 | ASTContext &Ctx = Pass.Ctx; |
| 44 | TransformActions &TA = Pass.TA; |
| 45 | |
| 46 | if (ME->getReceiverKind() != ObjCMessageExpr::Instance) |
| 47 | return true; |
| 48 | Expr *receiver = ME->getInstanceReceiver(); |
| 49 | if (!receiver) |
| 50 | return true; |
| 51 | |
| 52 | DeclRefExpr *refE = dyn_cast<DeclRefExpr>(receiver->IgnoreParenCasts()); |
| 53 | if (!refE || refE->getDecl() != SelfD) |
| 54 | return true; |
| 55 | |
| 56 | bool BackedBySynthesizeSetter = false; |
| 57 | for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator |
| 58 | P = SynthesizedProperties.begin(), |
| 59 | E = SynthesizedProperties.end(); P != E; ++P) { |
| 60 | ObjCPropertyDecl *PropDecl = P->first; |
| 61 | if (PropDecl->getSetterName() == ME->getSelector()) { |
| 62 | BackedBySynthesizeSetter = true; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | if (!BackedBySynthesizeSetter) |
| 67 | return true; |
| 68 | |
| 69 | // Remove the setter message if RHS is null |
| 70 | Transaction Trans(TA); |
| 71 | Expr *RHS = ME->getArg(0); |
| 72 | bool RHSIsNull = |
| 73 | RHS->isNullPointerConstant(Ctx, |
| 74 | Expr::NPC_ValueDependentIsNull); |
| 75 | if (RHSIsNull && isRemovable(ME)) |
| 76 | TA.removeStmt(ME); |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 81 | bool VisitPseudoObjectExpr(PseudoObjectExpr *POE) { |
| 82 | if (isZeroingPropIvar(POE) && isRemovable(POE)) { |
| 83 | Transaction Trans(Pass.TA); |
| 84 | Pass.TA.removeStmt(POE); |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 90 | bool VisitBinaryOperator(BinaryOperator *BOE) { |
| 91 | if (isZeroingPropIvar(BOE) && isRemovable(BOE)) { |
| 92 | Transaction Trans(Pass.TA); |
| 93 | Pass.TA.removeStmt(BOE); |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | bool TraverseObjCMethodDecl(ObjCMethodDecl *D) { |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame] | 100 | if (D->getMethodFamily() != OMF_dealloc && |
| 101 | !(D->isInstanceMethod() && D->getSelector() == FinalizeSel)) |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 102 | return true; |
| 103 | if (!D->hasBody()) |
| 104 | return true; |
| 105 | |
| 106 | ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(D->getDeclContext()); |
| 107 | if (!IMD) |
| 108 | return true; |
| 109 | |
| 110 | SelfD = D->getSelfDecl(); |
| 111 | collectRemovables(D->getBody(), Removables); |
| 112 | |
| 113 | // For a 'dealloc' method use, find all property implementations in |
| 114 | // this class implementation. |
| 115 | for (ObjCImplDecl::propimpl_iterator |
| 116 | I = IMD->propimpl_begin(), EI = IMD->propimpl_end(); I != EI; ++I) { |
| 117 | ObjCPropertyImplDecl *PID = *I; |
| 118 | if (PID->getPropertyImplementation() == |
| 119 | ObjCPropertyImplDecl::Synthesize) { |
| 120 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 121 | ObjCMethodDecl *setterM = PD->getSetterMethodDecl(); |
| 122 | if (!(setterM && setterM->isDefined())) { |
| 123 | ObjCPropertyDecl::PropertyAttributeKind AttrKind = |
| 124 | PD->getPropertyAttributes(); |
| 125 | if (AttrKind & |
| 126 | (ObjCPropertyDecl::OBJC_PR_retain | |
| 127 | ObjCPropertyDecl::OBJC_PR_copy | |
| 128 | ObjCPropertyDecl::OBJC_PR_strong)) |
| 129 | SynthesizedProperties[PD] = PID; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Now, remove all zeroing of ivars etc. |
| 135 | base::TraverseObjCMethodDecl(D); |
| 136 | |
| 137 | // clear out for next method. |
| 138 | SynthesizedProperties.clear(); |
| 139 | SelfD = 0; |
| 140 | Removables.clear(); |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | bool TraverseFunctionDecl(FunctionDecl *D) { return true; } |
| 145 | bool TraverseBlockDecl(BlockDecl *block) { return true; } |
| 146 | bool TraverseBlockExpr(BlockExpr *block) { return true; } |
| 147 | |
| 148 | private: |
| 149 | bool isRemovable(Expr *E) const { |
| 150 | return Removables.count(E); |
| 151 | } |
| 152 | |
| 153 | bool isZeroingPropIvar(Expr *E) { |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 154 | E = E->IgnoreParens(); |
| 155 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) |
| 156 | return isZeroingPropIvar(BO); |
| 157 | if (PseudoObjectExpr *PO = dyn_cast<PseudoObjectExpr>(E)) |
| 158 | return isZeroingPropIvar(PO); |
| 159 | return false; |
| 160 | } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 161 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 162 | bool isZeroingPropIvar(BinaryOperator *BOE) { |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 163 | if (BOE->getOpcode() == BO_Comma) |
| 164 | return isZeroingPropIvar(BOE->getLHS()) && |
| 165 | isZeroingPropIvar(BOE->getRHS()); |
| 166 | |
| 167 | if (BOE->getOpcode() != BO_Assign) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 168 | return false; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 169 | |
| 170 | Expr *LHS = BOE->getLHS(); |
| 171 | if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(LHS)) { |
| 172 | ObjCIvarDecl *IVDecl = IV->getDecl(); |
| 173 | if (!IVDecl->getType()->isObjCObjectPointerType()) |
| 174 | return false; |
| 175 | bool IvarBacksPropertySynthesis = false; |
| 176 | for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator |
| 177 | P = SynthesizedProperties.begin(), |
| 178 | E = SynthesizedProperties.end(); P != E; ++P) { |
| 179 | ObjCPropertyImplDecl *PropImpDecl = P->second; |
| 180 | if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) { |
| 181 | IvarBacksPropertySynthesis = true; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | if (!IvarBacksPropertySynthesis) |
| 186 | return false; |
| 187 | } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 188 | else |
| 189 | return false; |
| 190 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 191 | return isZero(BOE->getRHS()); |
| 192 | } |
| 193 | |
| 194 | bool isZeroingPropIvar(PseudoObjectExpr *PO) { |
| 195 | BinaryOperator *BO = dyn_cast<BinaryOperator>(PO->getSyntacticForm()); |
| 196 | if (!BO) return false; |
| 197 | if (BO->getOpcode() != BO_Assign) return false; |
| 198 | |
| 199 | ObjCPropertyRefExpr *PropRefExp = |
| 200 | dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParens()); |
| 201 | if (!PropRefExp) return false; |
| 202 | |
| 203 | // TODO: Using implicit property decl. |
| 204 | if (PropRefExp->isImplicitProperty()) |
| 205 | return false; |
| 206 | |
| 207 | if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) { |
| 208 | if (!SynthesizedProperties.count(PDecl)) |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | return isZero(cast<OpaqueValueExpr>(BO->getRHS())->getSourceExpr()); |
| 213 | } |
| 214 | |
| 215 | bool isZero(Expr *E) { |
| 216 | if (E->isNullPointerConstant(Pass.Ctx, Expr::NPC_ValueDependentIsNull)) |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 217 | return true; |
| 218 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 219 | return isZeroingPropIvar(E); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 220 | } |
| 221 | }; |
| 222 | |
| 223 | } // anonymous namespace |
| 224 | |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame] | 225 | void trans::removeZeroOutPropsInDeallocFinalize(MigrationPass &pass) { |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 226 | ZeroOutInDeallocRemover trans(pass); |
| 227 | trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl()); |
| 228 | } |