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, |
| 53 | PropAction_RetainToStrong, |
| 54 | PropAction_RetainRemoved, |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 55 | PropAction_AssignRemoved, |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 56 | PropAction_AssignRewritten, |
| 57 | PropAction_MaybeAddStrong, |
| 58 | PropAction_MaybeAddWeakOrUnsafe |
| 59 | }; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 60 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 61 | struct PropData { |
| 62 | ObjCPropertyDecl *PropD; |
| 63 | ObjCIvarDecl *IvarD; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 64 | ObjCPropertyImplDecl *ImplD; |
| 65 | |
| 66 | PropData(ObjCPropertyDecl *propD) : PropD(propD), IvarD(0), ImplD(0) { } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 69 | typedef SmallVector<PropData, 2> PropsTy; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 70 | typedef std::map<unsigned, PropsTy> AtPropDeclsTy; |
| 71 | AtPropDeclsTy AtProps; |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 72 | llvm::DenseMap<IdentifierInfo *, PropActionKind> ActionOnProp; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 73 | |
| 74 | public: |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 75 | explicit PropertiesRewriter(MigrationContext &MigrateCtx) |
| 76 | : MigrateCtx(MigrateCtx), Pass(MigrateCtx.Pass) { } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 77 | |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 78 | static void collectProperties(ObjCContainerDecl *D, AtPropDeclsTy &AtProps) { |
| 79 | for (ObjCInterfaceDecl::prop_iterator |
| 80 | propI = D->prop_begin(), |
| 81 | propE = D->prop_end(); propI != propE; ++propI) { |
| 82 | if (propI->getAtLoc().isInvalid()) |
| 83 | continue; |
| 84 | PropsTy &props = AtProps[propI->getAtLoc().getRawEncoding()]; |
| 85 | props.push_back(*propI); |
| 86 | } |
| 87 | } |
| 88 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 89 | void doTransform(ObjCImplementationDecl *D) { |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 90 | CurImplD = D; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 91 | ObjCInterfaceDecl *iface = D->getClassInterface(); |
| 92 | if (!iface) |
| 93 | return; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 94 | |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 95 | collectProperties(iface, AtProps); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 96 | |
| 97 | typedef DeclContext::specific_decl_iterator<ObjCPropertyImplDecl> |
| 98 | prop_impl_iterator; |
| 99 | for (prop_impl_iterator |
| 100 | I = prop_impl_iterator(D->decls_begin()), |
| 101 | E = prop_impl_iterator(D->decls_end()); I != E; ++I) { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 102 | ObjCPropertyImplDecl *implD = *I; |
| 103 | if (implD->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 104 | continue; |
| 105 | ObjCPropertyDecl *propD = implD->getPropertyDecl(); |
| 106 | if (!propD || propD->isInvalidDecl()) |
| 107 | continue; |
| 108 | ObjCIvarDecl *ivarD = implD->getPropertyIvarDecl(); |
| 109 | if (!ivarD || ivarD->isInvalidDecl()) |
| 110 | continue; |
| 111 | unsigned rawAtLoc = propD->getAtLoc().getRawEncoding(); |
| 112 | AtPropDeclsTy::iterator findAtLoc = AtProps.find(rawAtLoc); |
| 113 | if (findAtLoc == AtProps.end()) |
| 114 | continue; |
| 115 | |
| 116 | PropsTy &props = findAtLoc->second; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 117 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 118 | if (I->PropD == propD) { |
| 119 | I->IvarD = ivarD; |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 120 | I->ImplD = implD; |
| 121 | break; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 126 | for (AtPropDeclsTy::iterator |
| 127 | I = AtProps.begin(), E = AtProps.end(); I != E; ++I) { |
| 128 | SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first); |
| 129 | PropsTy &props = I->second; |
Argyrios Kyrtzidis | 1d5fb8f | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 130 | if (!getPropertyType(props)->isObjCRetainableType()) |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 131 | continue; |
Argyrios Kyrtzidis | bf8455c | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 132 | if (hasIvarWithExplicitARCOwnership(props)) |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 133 | continue; |
| 134 | |
| 135 | Transaction Trans(Pass.TA); |
| 136 | rewriteProperty(props, atLoc); |
| 137 | } |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 138 | |
| 139 | AtPropDeclsTy AtExtProps; |
| 140 | // Look through extensions. |
| 141 | for (ObjCCategoryDecl *Cat = iface->getCategoryList(); |
| 142 | Cat; Cat = Cat->getNextClassCategory()) |
| 143 | if (Cat->IsClassExtension()) |
| 144 | collectProperties(Cat, AtExtProps); |
| 145 | |
| 146 | for (AtPropDeclsTy::iterator |
| 147 | I = AtExtProps.begin(), E = AtExtProps.end(); I != E; ++I) { |
| 148 | SourceLocation atLoc = SourceLocation::getFromRawEncoding(I->first); |
| 149 | PropsTy &props = I->second; |
| 150 | Transaction Trans(Pass.TA); |
| 151 | doActionForExtensionProp(props, atLoc); |
| 152 | } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 156 | void doPropAction(PropActionKind kind, |
| 157 | PropsTy &props, SourceLocation atLoc, |
| 158 | bool markAction = true) { |
| 159 | if (markAction) |
| 160 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 161 | ActionOnProp[I->PropD->getIdentifier()] = kind; |
| 162 | |
| 163 | switch (kind) { |
| 164 | case PropAction_None: |
| 165 | return; |
| 166 | case PropAction_RetainToStrong: |
| 167 | rewriteAttribute("retain", "strong", atLoc); |
| 168 | return; |
| 169 | case PropAction_RetainRemoved: |
| 170 | removeAttribute("retain", atLoc); |
| 171 | return; |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 172 | case PropAction_AssignRemoved: |
| 173 | return removeAssignForDefaultStrong(props, atLoc); |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 174 | case PropAction_AssignRewritten: |
| 175 | return rewriteAssign(props, atLoc); |
| 176 | case PropAction_MaybeAddStrong: |
| 177 | return maybeAddStrongAttr(props, atLoc); |
| 178 | case PropAction_MaybeAddWeakOrUnsafe: |
| 179 | return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void doActionForExtensionProp(PropsTy &props, SourceLocation atLoc) { |
| 184 | llvm::DenseMap<IdentifierInfo *, PropActionKind>::iterator I; |
| 185 | I = ActionOnProp.find(props[0].PropD->getIdentifier()); |
| 186 | if (I == ActionOnProp.end()) |
| 187 | return; |
| 188 | |
| 189 | doPropAction(I->second, props, atLoc, false); |
| 190 | } |
| 191 | |
| 192 | void rewriteProperty(PropsTy &props, SourceLocation atLoc) { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 193 | ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props); |
| 194 | |
| 195 | if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy | |
| 196 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained | |
| 197 | ObjCPropertyDecl::OBJC_PR_strong | |
| 198 | ObjCPropertyDecl::OBJC_PR_weak)) |
| 199 | return; |
| 200 | |
| 201 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) { |
Argyrios Kyrtzidis | 01b2b9b | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 202 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_readonly) |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 203 | return doPropAction(PropAction_RetainToStrong, props, atLoc); |
Argyrios Kyrtzidis | 01b2b9b | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 204 | else |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 205 | // strong is the default. |
| 206 | return doPropAction(PropAction_RetainRemoved, props, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 209 | bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props); |
| 210 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 211 | if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 212 | if (HasIvarAssignedAPlusOneObject || |
| 213 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc))) { |
| 214 | return doPropAction(PropAction_AssignRemoved, props, atLoc); |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 215 | } |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 216 | return doPropAction(PropAction_AssignRewritten, props, atLoc); |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 219 | if (HasIvarAssignedAPlusOneObject || |
| 220 | (Pass.isGCMigration() && !hasGCWeak(props, atLoc))) |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 221 | return doPropAction(PropAction_MaybeAddStrong, props, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 222 | |
Argyrios Kyrtzidis | 4467901 | 2011-10-18 19:49:19 +0000 | [diff] [blame] | 223 | return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 226 | void removeAssignForDefaultStrong(PropsTy &props, |
| 227 | SourceLocation atLoc) const { |
| 228 | removeAttribute("retain", atLoc); |
| 229 | if (!removeAttribute("assign", atLoc)) |
| 230 | return; |
| 231 | |
| 232 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 233 | if (I->ImplD) |
| 234 | Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, |
| 235 | I->ImplD->getLocation()); |
| 236 | } |
| 237 | } |
| 238 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 239 | void rewriteAssign(PropsTy &props, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 240 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 241 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 242 | |
| 243 | bool rewroteAttr = rewriteAttribute("assign", |
| 244 | canUseWeak ? "weak" : "unsafe_unretained", |
| 245 | atLoc); |
| 246 | if (!rewroteAttr) |
| 247 | canUseWeak = false; |
| 248 | |
| 249 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 250 | if (isUserDeclared(I->IvarD)) |
| 251 | Pass.TA.insert(I->IvarD->getLocation(), |
| 252 | canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 253 | if (I->ImplD) |
| 254 | Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, |
| 255 | I->ImplD->getLocation()); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props, |
| 260 | SourceLocation atLoc) const { |
| 261 | ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 262 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 263 | bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props), |
| 264 | /*AllowOnUnknownClass=*/Pass.isGCMigration()); |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 265 | if (!(propAttrs & ObjCPropertyDecl::OBJC_PR_readonly) || |
| 266 | !hasAllIvarsBacked(props)) { |
| 267 | bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained", |
| 268 | atLoc); |
| 269 | if (!addedAttr) |
| 270 | canUseWeak = false; |
| 271 | } |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 272 | |
| 273 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 274 | if (isUserDeclared(I->IvarD)) |
| 275 | Pass.TA.insert(I->IvarD->getLocation(), |
| 276 | canUseWeak ? "__weak " : "__unsafe_unretained "); |
| 277 | if (I->ImplD) { |
| 278 | Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, |
| 279 | I->ImplD->getLocation()); |
| 280 | Pass.TA.clearDiagnostic( |
| 281 | diag::err_arc_objc_property_default_assign_on_object, |
| 282 | I->ImplD->getLocation()); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 287 | void maybeAddStrongAttr(PropsTy &props, SourceLocation atLoc) const { |
| 288 | ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props); |
| 289 | |
| 290 | if (!(propAttrs & ObjCPropertyDecl::OBJC_PR_readonly) || |
| 291 | !hasAllIvarsBacked(props)) { |
| 292 | addAttribute("strong", atLoc); |
| 293 | } |
| 294 | |
| 295 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 296 | if (I->ImplD) { |
| 297 | Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, |
| 298 | I->ImplD->getLocation()); |
| 299 | Pass.TA.clearDiagnostic( |
| 300 | diag::err_arc_objc_property_default_assign_on_object, |
| 301 | I->ImplD->getLocation()); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Argyrios Kyrtzidis | 01b2b9b | 2011-10-17 23:14:16 +0000 | [diff] [blame] | 306 | bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const { |
| 307 | return rewriteAttribute(fromAttr, StringRef(), atLoc); |
| 308 | } |
| 309 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 310 | bool rewriteAttribute(StringRef fromAttr, StringRef toAttr, |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 311 | SourceLocation atLoc) const { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 312 | return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 315 | bool addAttribute(StringRef attr, SourceLocation atLoc) const { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 316 | if (atLoc.isMacroID()) |
| 317 | return false; |
| 318 | |
| 319 | SourceManager &SM = Pass.Ctx.getSourceManager(); |
| 320 | |
| 321 | // Break down the source location. |
| 322 | std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc); |
| 323 | |
| 324 | // Try to load the file buffer. |
| 325 | bool invalidTemp = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 326 | StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 327 | if (invalidTemp) |
| 328 | return false; |
| 329 | |
| 330 | const char *tokenBegin = file.data() + locInfo.second; |
| 331 | |
| 332 | // Lex from the start of the given location. |
| 333 | Lexer lexer(SM.getLocForStartOfFile(locInfo.first), |
| 334 | Pass.Ctx.getLangOptions(), |
| 335 | file.begin(), tokenBegin, file.end()); |
| 336 | Token tok; |
| 337 | lexer.LexFromRawLexer(tok); |
| 338 | if (tok.isNot(tok::at)) return false; |
| 339 | lexer.LexFromRawLexer(tok); |
| 340 | if (tok.isNot(tok::raw_identifier)) return false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 341 | if (StringRef(tok.getRawIdentifierData(), tok.getLength()) |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 342 | != "property") |
| 343 | return false; |
| 344 | lexer.LexFromRawLexer(tok); |
| 345 | |
| 346 | if (tok.isNot(tok::l_paren)) { |
| 347 | Pass.TA.insert(tok.getLocation(), std::string("(") + attr.str() + ") "); |
| 348 | return true; |
| 349 | } |
| 350 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 351 | lexer.LexFromRawLexer(tok); |
| 352 | if (tok.is(tok::r_paren)) { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 353 | Pass.TA.insert(tok.getLocation(), attr); |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | if (tok.isNot(tok::raw_identifier)) return false; |
| 358 | |
| 359 | Pass.TA.insert(tok.getLocation(), std::string(attr) + ", "); |
| 360 | return true; |
| 361 | } |
| 362 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 363 | class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> { |
| 364 | ObjCIvarDecl *Ivar; |
| 365 | public: |
| 366 | PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {} |
| 367 | |
| 368 | bool VisitBinAssign(BinaryOperator *E) { |
| 369 | Expr *lhs = E->getLHS()->IgnoreParenImpCasts(); |
| 370 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) { |
| 371 | if (RE->getDecl() != Ivar) |
| 372 | return true; |
| 373 | |
Argyrios Kyrtzidis | 94a9016 | 2011-08-10 21:46:48 +0000 | [diff] [blame] | 374 | if (ObjCMessageExpr * |
| 375 | ME = dyn_cast<ObjCMessageExpr>(E->getRHS()->IgnoreParenCasts())) |
| 376 | if (ME->getMethodFamily() == OMF_retain) |
| 377 | return false; |
| 378 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 379 | ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(E->getRHS()); |
| 380 | while (implCE && implCE->getCastKind() == CK_BitCast) |
| 381 | implCE = dyn_cast<ImplicitCastExpr>(implCE->getSubExpr()); |
| 382 | |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 383 | if (implCE && implCE->getCastKind() == CK_ARCConsumeObject) |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
| 387 | return true; |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | bool hasIvarAssignedAPlusOneObject(PropsTy &props) const { |
| 392 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 393 | PlusOneAssign oneAssign(I->IvarD); |
| 394 | bool notFound = oneAssign.TraverseDecl(CurImplD); |
| 395 | if (!notFound) |
| 396 | return true; |
| 397 | } |
| 398 | |
| 399 | return false; |
| 400 | } |
| 401 | |
Argyrios Kyrtzidis | bf8455c | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 402 | bool hasIvarWithExplicitARCOwnership(PropsTy &props) const { |
| 403 | if (Pass.isGCMigration()) |
| 404 | return false; |
| 405 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 406 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { |
| 407 | if (isUserDeclared(I->IvarD)) { |
| 408 | if (isa<AttributedType>(I->IvarD->getType())) |
| 409 | return true; |
| 410 | if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime() |
| 411 | != Qualifiers::OCL_Strong) |
| 412 | return true; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 416 | return false; |
| 417 | } |
| 418 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 419 | bool hasAllIvarsBacked(PropsTy &props) const { |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 420 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 421 | if (!isUserDeclared(I->IvarD)) |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 422 | return false; |
| 423 | |
| 424 | return true; |
| 425 | } |
| 426 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 427 | // \brief Returns true if all declarations in the @property have GC __weak. |
| 428 | bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const { |
| 429 | if (!Pass.isGCMigration()) |
| 430 | return false; |
| 431 | if (props.empty()) |
| 432 | return false; |
| 433 | return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding()); |
| 434 | } |
| 435 | |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 436 | bool isUserDeclared(ObjCIvarDecl *ivarD) const { |
| 437 | return ivarD && !ivarD->getSynthesize(); |
| 438 | } |
| 439 | |
| 440 | QualType getPropertyType(PropsTy &props) const { |
| 441 | assert(!props.empty()); |
Argyrios Kyrtzidis | 1d5fb8f | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 442 | QualType ty = props[0].PropD->getType().getUnqualifiedType(); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 443 | |
| 444 | #ifndef NDEBUG |
| 445 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
Argyrios Kyrtzidis | 1d5fb8f | 2011-11-06 18:58:07 +0000 | [diff] [blame] | 446 | assert(ty == I->PropD->getType().getUnqualifiedType()); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 447 | #endif |
| 448 | |
| 449 | return ty; |
| 450 | } |
| 451 | |
| 452 | ObjCPropertyDecl::PropertyAttributeKind |
| 453 | getPropertyAttrs(PropsTy &props) const { |
| 454 | assert(!props.empty()); |
| 455 | ObjCPropertyDecl::PropertyAttributeKind |
| 456 | attrs = props[0].PropD->getPropertyAttributesAsWritten(); |
| 457 | |
| 458 | #ifndef NDEBUG |
| 459 | for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) |
| 460 | assert(attrs == I->PropD->getPropertyAttributesAsWritten()); |
| 461 | #endif |
| 462 | |
| 463 | return attrs; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 464 | } |
| 465 | }; |
| 466 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 467 | } // anonymous namespace |
| 468 | |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 469 | void PropertyRewriteTraverser::traverseObjCImplementation( |
| 470 | ObjCImplementationContext &ImplCtx) { |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 471 | PropertiesRewriter(ImplCtx.getMigrationContext()) |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 472 | .doTransform(ImplCtx.getImplementationDecl()); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 473 | } |