Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 1 | //===--- TransProperties.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 | // |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 10 | // rewriteProperties: |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 11 | // |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 12 | // - Adds strong/weak/unsafe_unretained ownership specifier to properties that |
| 13 | // are missing one. |
| 14 | // - Migrates properties from (retain) to (strong) and (assign) to |
| 15 | // (unsafe_unretained/weak). |
| 16 | // - If a property is synthesized, adds the ownership specifier in the ivar |
| 17 | // backing the property. |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 18 | // |
| 19 | // @interface Foo : NSObject { |
| 20 | // NSObject *x; |
| 21 | // } |
| 22 | // @property (assign) id x; |
| 23 | // @end |
| 24 | // ----> |
| 25 | // @interface Foo : NSObject { |
| 26 | // NSObject *__weak x; |
| 27 | // } |
| 28 | // @property (weak) id x; |
| 29 | // @end |
| 30 | // |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | #include "Transforms.h" |
| 34 | #include "Internals.h" |
| 35 | #include "clang/Sema/SemaDiagnostic.h" |
| 36 | #include "clang/Basic/SourceManager.h" |
| 37 | #include "clang/Lex/Lexer.h" |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 38 | #include <map> |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace clang; |
| 41 | using namespace arcmt; |
| 42 | using namespace trans; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 43 | |
| 44 | namespace { |
| 45 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 46 | class PropertiesRewriter { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 47 | MigrationContext &MigrateCtx; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 48 | MigrationPass &Pass; |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 49 | ObjCImplementationDecl *CurImplD; |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 50 | |
| 51 | enum PropActionKind { |
| 52 | PropAction_None, |
Fariborz Jahanian | 86f9601 | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 53 | PropAction_RetainReplacedWithStrong, |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 54 | PropAction_AssignRemoved, |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 55 | PropAction_AssignRewritten, |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 56 | PropAction_MaybeAddWeakOrUnsafe |
| 57 | }; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 58 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 59 | struct PropData { |
| 60 | ObjCPropertyDecl *PropD; |
| 61 | ObjCIvarDecl *IvarD; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 62 | ObjCPropertyImplDecl *ImplD; |
| 63 | |
| 64 | PropData(ObjCPropertyDecl *propD) : PropD(propD), IvarD(0), ImplD(0) { } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | typedef SmallVector<PropData, 2> PropsTy; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 68 | typedef std::map<unsigned, PropsTy> AtPropDeclsTy; |
| 69 | AtPropDeclsTy AtProps; |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 70 | llvm::DenseMap<IdentifierInfo *, PropActionKind> ActionOnProp; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 71 | |
| 72 | public: |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 73 | explicit PropertiesRewriter(MigrationContext &MigrateCtx) |
| 74 | : MigrateCtx(MigrateCtx), Pass(MigrateCtx.Pass) { } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 75 | |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 76 | static void collectProperties(ObjCContainerDecl *D, AtPropDeclsTy &AtProps) { |
| 77 | for (ObjCInterfaceDecl::prop_iterator |
| 78 | propI = D->prop_begin(), |
| 79 | propE = D->prop_end(); propI != propE; ++propI) { |
| 80 | if (propI->getAtLoc().isInvalid()) |
| 81 | continue; |
| 82 | PropsTy &props = AtProps[propI->getAtLoc().getRawEncoding()]; |
| 83 | props.push_back(*propI); |
| 84 | } |
| 85 | } |
| 86 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 87 | void doTransform(ObjCImplementationDecl *D) { |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 88 | CurImplD = D; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 89 | ObjCInterfaceDecl *iface = D->getClassInterface(); |
| 90 | if (!iface) |
| 91 | return; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 92 | |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 93 | collectProperties(iface, AtProps); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 94 | |
| 95 | typedef DeclContext::specific_decl_iterator<ObjCPropertyImplDecl> |
| 96 | prop_impl_iterator; |
| 97 | for (prop_impl_iterator |
| 98 | I = prop_impl_iterator(D->decls_begin()), |
| 99 | E = prop_impl_iterator(D->decls_end()); I != E; ++I) { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 100 | ObjCPropertyImplDecl *implD = *I; |
| 101 | if (implD->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 102 | continue; |
| 103 | ObjCPropertyDecl *propD = implD->getPropertyDecl(); |
| 104 | if (!propD || propD->isInvalidDecl()) |
| 105 | continue; |
| 106 | ObjCIvarDecl *ivarD = implD->getPropertyIvarDecl(); |
| 107 | if (!ivarD || ivarD->isInvalidDecl()) |
| 108 | continue; |
| 109 | unsigned rawAtLoc = propD->getAtLoc().getRawEncoding(); |
| 110 | AtPropDeclsTy::iterator findAtLoc = AtProps.find(rawAtLoc); |
| 111 | if (findAtLoc == AtProps.end()) |
| 112 | continue; |
| 113 | |
| 114 | PropsTy &props = findAtLoc->second; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 115 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 116 | if (I->PropD == propD) { |
| 117 | I->IvarD = ivarD; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 118 | I->ImplD = implD; |
| 119 | break; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 124 | for (AtPropDeclsTy::iterator |
| 125 | I = AtProps.begin(), E = AtProps.end(); I != E; ++I) { |
| 126 | SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first); |
| 127 | PropsTy &props = I->second; |
Argyrios Kyrtzidis | 1d5fb8f | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 128 | if (!getPropertyType(props)->isObjCRetainableType()) |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 129 | continue; |
Argyrios Kyrtzidis | bf8455c | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 130 | if (hasIvarWithExplicitARCOwnership(props)) |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 131 | continue; |
| 132 | |
| 133 | Transaction Trans(Pass.TA); |
| 134 | rewriteProperty(props, atLoc); |
| 135 | } |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 136 | |
| 137 | AtPropDeclsTy AtExtProps; |
| 138 | // Look through extensions. |
| 139 | for (ObjCCategoryDecl *Cat = iface->getCategoryList(); |
| 140 | Cat; Cat = Cat->getNextClassCategory()) |
| 141 | if (Cat->IsClassExtension()) |
| 142 | collectProperties(Cat, AtExtProps); |
| 143 | |
| 144 | for (AtPropDeclsTy::iterator |
| 145 | I = AtExtProps.begin(), E = AtExtProps.end(); I != E; ++I) { |
| 146 | SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first); |
| 147 | PropsTy &props = I->second; |
| 148 | Transaction Trans(Pass.TA); |
| 149 | doActionForExtensionProp(props, atLoc); |
| 150 | } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private: |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 154 | void doPropAction(PropActionKind kind, |
| 155 | PropsTy &props, SourceLocation atLoc, |
| 156 | bool markAction = true) { |
| 157 | if (markAction) |
| 158 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 159 | ActionOnProp[I->PropD->getIdentifier()] = kind; |
| 160 | |
| 161 | switch (kind) { |
| 162 | case PropAction_None: |
| 163 | return; |
Fariborz Jahanian | 86f9601 | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 164 | case PropAction_RetainReplacedWithStrong: { |
| 165 | StringRef toAttr = "strong"; |
| 166 | MigrateCtx.rewritePropertyAttribute("retain", toAttr, atLoc); |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 167 | return; |
Fariborz Jahanian | 86f9601 | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 168 | } |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 169 | case PropAction_AssignRemoved: |
| 170 | return removeAssignForDefaultStrong(props, atLoc); |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 171 | case PropAction_AssignRewritten: |
| 172 | return rewriteAssign(props, atLoc); |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 173 | case PropAction_MaybeAddWeakOrUnsafe: |
| 174 | return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void doActionForExtensionProp(PropsTy &props, SourceLocation atLoc) { |
| 179 | llvm::DenseMap<IdentifierInfo *, PropActionKind>::iterator I; |
| 180 | I = ActionOnProp.find(props[0].PropD->getIdentifier()); |
| 181 | if (I == ActionOnProp.end()) |
| 182 | return; |
| 183 | |
| 184 | doPropAction(I->second, props, atLoc, false); |
| 185 | } |
| 186 | |
| 187 | void rewriteProperty(PropsTy &props, SourceLocation atLoc) { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 188 | ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props); |
| 189 | |
| 190 | if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy | |
| 191 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained | |
| 192 | ObjCPropertyDecl::OBJC_PR_strong | |
| 193 | ObjCPropertyDecl::OBJC_PR_weak)) |
| 194 | return; |
| 195 | |
| 196 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) { |
Argyrios Kyrtzidis | 8b08eb3 | 2011-11-08 23:09:34 +0000 | [diff] [blame] | 197 | // strong is the default. |
Fariborz Jahanian | 86f9601 | 2012-01-20 19:15:02 +0000 | [diff] [blame] | 198 | return doPropAction(PropAction_RetainReplacedWithStrong, props, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 201 | bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props); |
| 202 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 203 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) { |
Fariborz Jahanian | 2f72ec9 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 204 | if (HasIvarAssignedAPlusOneObject) |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 205 | return doPropAction(PropAction_AssignRemoved, props, atLoc); |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 206 | return doPropAction(PropAction_AssignRewritten, props, atLoc); |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 209 | if (HasIvarAssignedAPlusOneObject || |
| 210 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc))) |
Argyrios Kyrtzidis | af9b5e9 | 2011-11-08 22:10:58 +0000 | [diff] [blame] | 211 | return; // 'strong' by default. |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 212 | |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 213 | return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 216 | void removeAssignForDefaultStrong(PropsTy &props, |
| 217 | SourceLocation atLoc) const { |
| 218 | removeAttribute("retain", atLoc); |
| 219 | if (!removeAttribute("assign", atLoc)) |
| 220 | return; |
| 221 | |
| 222 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 223 | if (I->ImplD) |
| 224 | Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, |
| 225 | I->ImplD->getLocation()); |
| 226 | } |
| 227 | } |
| 228 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 229 | void rewriteAssign(PropsTy &props, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 230 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 231 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Fariborz Jahanian | 2f72ec9 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 232 | const char *toWhich = |
| 233 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "strong" : |
| 234 | (canUseWeak ? "weak" : "unsafe_unretained"); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 235 | |
Fariborz Jahanian | 2f72ec9 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 236 | bool rewroteAttr = rewriteAttribute("assign", toWhich, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 237 | if (!rewroteAttr) |
| 238 | canUseWeak = false; |
| 239 | |
| 240 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 6d7d16d | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 241 | if (isUserDeclared(I->IvarD)) { |
| 242 | if (I->IvarD && |
Fariborz Jahanian | 2f72ec9 | 2012-01-21 00:43:53 +0000 | [diff] [blame] | 243 | I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) { |
| 244 | const char *toWhich = |
| 245 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc)) ? "__strong " : |
| 246 | (canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 247 | Pass.TA.insert(I->IvarD->getLocation(), toWhich); |
| 248 | } |
Argyrios Kyrtzidis | 6d7d16d | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 249 | } |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 250 | if (I->ImplD) |
| 251 | Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, |
| 252 | I->ImplD->getLocation()); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props, |
| 257 | SourceLocation atLoc) const { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 258 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 259 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Argyrios Kyrtzidis | 8b08eb3 | 2011-11-08 23:09:34 +0000 | [diff] [blame] | 260 | |
| 261 | bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained", |
| 262 | atLoc); |
| 263 | if (!addedAttr) |
| 264 | canUseWeak = false; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 265 | |
| 266 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 6d7d16d | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 267 | if (isUserDeclared(I->IvarD)) { |
| 268 | if (I->IvarD && |
| 269 | I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak) |
| 270 | Pass.TA.insert(I->IvarD->getLocation(), |
| 271 | canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 272 | } |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 273 | if (I->ImplD) { |
| 274 | Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, |
| 275 | I->ImplD->getLocation()); |
| 276 | Pass.TA.clearDiagnostic( |
| 277 | diag::err_arc_objc_property_default_assign_on_object, |
| 278 | I->ImplD->getLocation()); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Argyrios Kyrtzidis | 01b2b9b | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 283 | bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 6da4274 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 284 | return MigrateCtx.removePropertyAttribute(fromAttr, atLoc); |
Argyrios Kyrtzidis | 01b2b9b | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 287 | bool rewriteAttribute(StringRef fromAttr, StringRef toAttr, |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 288 | SourceLocation atLoc) const { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 289 | return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 292 | bool addAttribute(StringRef attr, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 6da4274 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 293 | return MigrateCtx.addPropertyAttribute(attr, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 296 | class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> { |
| 297 | ObjCIvarDecl *Ivar; |
| 298 | public: |
| 299 | PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {} |
| 300 | |
| 301 | bool VisitBinAssign(BinaryOperator *E) { |
| 302 | Expr *lhs = E->getLHS()->IgnoreParenImpCasts(); |
| 303 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) { |
| 304 | if (RE->getDecl() != Ivar) |
| 305 | return true; |
| 306 | |
Argyrios Kyrtzidis | 94a9016 | 2011-08-10 21:46:48 +0000 | [diff] [blame] | 307 | if (ObjCMessageExpr * |
| 308 | ME = dyn_cast<ObjCMessageExpr>(E->getRHS()->IgnoreParenCasts())) |
| 309 | if (ME->getMethodFamily() == OMF_retain) |
| 310 | return false; |
| 311 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 312 | ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(E->getRHS()); |
| 313 | while (implCE && implCE->getCastKind() == CK_BitCast) |
| 314 | implCE = dyn_cast<ImplicitCastExpr>(implCE->getSubExpr()); |
| 315 | |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 316 | if (implCE && implCE->getCastKind() == CK_ARCConsumeObject) |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return true; |
| 321 | } |
| 322 | }; |
| 323 | |
| 324 | bool hasIvarAssignedAPlusOneObject(PropsTy &props) const { |
| 325 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 326 | PlusOneAssign oneAssign(I->IvarD); |
| 327 | bool notFound = oneAssign.TraverseDecl(CurImplD); |
| 328 | if (!notFound) |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | return false; |
| 333 | } |
| 334 | |
Argyrios Kyrtzidis | bf8455c | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 335 | bool hasIvarWithExplicitARCOwnership(PropsTy &props) const { |
| 336 | if (Pass.isGCMigration()) |
| 337 | return false; |
| 338 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 339 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 340 | if (isUserDeclared(I->IvarD)) { |
| 341 | if (isa<AttributedType>(I->IvarD->getType())) |
| 342 | return true; |
| 343 | if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime() |
| 344 | != Qualifiers::OCL_Strong) |
| 345 | return true; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 352 | bool hasAllIvarsBacked(PropsTy &props) const { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 353 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 354 | if (!isUserDeclared(I->IvarD)) |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 355 | return false; |
| 356 | |
| 357 | return true; |
| 358 | } |
| 359 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 360 | // \brief Returns true if all declarations in the @property have GC __weak. |
| 361 | bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const { |
| 362 | if (!Pass.isGCMigration()) |
| 363 | return false; |
| 364 | if (props.empty()) |
| 365 | return false; |
| 366 | return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding()); |
| 367 | } |
| 368 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 369 | bool isUserDeclared(ObjCIvarDecl *ivarD) const { |
| 370 | return ivarD && !ivarD->getSynthesize(); |
| 371 | } |
| 372 | |
| 373 | QualType getPropertyType(PropsTy &props) const { |
| 374 | assert(!props.empty()); |
Argyrios Kyrtzidis | 1d5fb8f | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 375 | QualType ty = props[0].PropD->getType().getUnqualifiedType(); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 376 | |
| 377 | #ifndef NDEBUG |
| 378 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
Argyrios Kyrtzidis | 1d5fb8f | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 379 | assert(ty == I->PropD->getType().getUnqualifiedType()); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 380 | #endif |
| 381 | |
| 382 | return ty; |
| 383 | } |
| 384 | |
| 385 | ObjCPropertyDecl::PropertyAttributeKind |
| 386 | getPropertyAttrs(PropsTy &props) const { |
| 387 | assert(!props.empty()); |
| 388 | ObjCPropertyDecl::PropertyAttributeKind |
| 389 | attrs = props[0].PropD->getPropertyAttributesAsWritten(); |
| 390 | |
| 391 | #ifndef NDEBUG |
| 392 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 393 | assert(attrs == I->PropD->getPropertyAttributesAsWritten()); |
| 394 | #endif |
| 395 | |
| 396 | return attrs; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 397 | } |
| 398 | }; |
| 399 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 400 | } // anonymous namespace |
| 401 | |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 402 | void PropertyRewriteTraverser::traverseObjCImplementation( |
| 403 | ObjCImplementationContext &ImplCtx) { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 404 | PropertiesRewriter(ImplCtx.getMigrationContext()) |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 405 | .doTransform(ImplCtx.getImplementationDecl()); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 406 | } |