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