blob: f8bc8a3c5feb22cd04cc020664724dd323d219ea [file] [log] [blame]
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +00001//===--- 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 Kyrtzidis14c4b442011-07-13 19:22:00 +000010// rewriteProperties:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000011//
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +000012// - 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 Kyrtzidis7196d062011-06-21 20:20:39 +000018//
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 Kyrtzidis14c4b442011-07-13 19:22:00 +000038#include <map>
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000039
40using namespace clang;
41using namespace arcmt;
42using namespace trans;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000043
44namespace {
45
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +000046class PropertiesRewriter {
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +000047 MigrationContext &MigrateCtx;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000048 MigrationPass &Pass;
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000049 ObjCImplementationDecl *CurImplD;
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +000050
51 enum PropActionKind {
52 PropAction_None,
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +000053 PropAction_RetainRemoved,
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +000054 PropAction_AssignRemoved,
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +000055 PropAction_AssignRewritten,
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +000056 PropAction_MaybeAddWeakOrUnsafe
57 };
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +000058
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000059 struct PropData {
60 ObjCPropertyDecl *PropD;
61 ObjCIvarDecl *IvarD;
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +000062 ObjCPropertyImplDecl *ImplD;
63
64 PropData(ObjCPropertyDecl *propD) : PropD(propD), IvarD(0), ImplD(0) { }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000065 };
66
Chris Lattner5f9e2722011-07-23 10:55:15 +000067 typedef SmallVector<PropData, 2> PropsTy;
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +000068 typedef std::map<unsigned, PropsTy> AtPropDeclsTy;
69 AtPropDeclsTy AtProps;
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +000070 llvm::DenseMap<IdentifierInfo *, PropActionKind> ActionOnProp;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000071
72public:
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +000073 explicit PropertiesRewriter(MigrationContext &MigrateCtx)
74 : MigrateCtx(MigrateCtx), Pass(MigrateCtx.Pass) { }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000075
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +000076 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 Kyrtzidis7196d062011-06-21 20:20:39 +000087 void doTransform(ObjCImplementationDecl *D) {
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +000088 CurImplD = D;
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +000089 ObjCInterfaceDecl *iface = D->getClassInterface();
90 if (!iface)
91 return;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000092
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +000093 collectProperties(iface, AtProps);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000094
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 Kyrtzidis14c4b442011-07-13 19:22:00 +0000100 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 Kyrtzidis7196d062011-06-21 20:20:39 +0000115 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
116 if (I->PropD == propD) {
117 I->IvarD = ivarD;
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000118 I->ImplD = implD;
119 break;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000120 }
121 }
122 }
123
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000124 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 Kyrtzidis1d5fb8f2011-11-06 18:58:07 +0000128 if (!getPropertyType(props)->isObjCRetainableType())
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000129 continue;
Argyrios Kyrtzidisbf8455c2011-11-07 18:40:32 +0000130 if (hasIvarWithExplicitARCOwnership(props))
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000131 continue;
132
133 Transaction Trans(Pass.TA);
134 rewriteProperty(props, atLoc);
135 }
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +0000136
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 Kyrtzidis7196d062011-06-21 20:20:39 +0000151 }
152
153private:
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +0000154 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;
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +0000164 case PropAction_RetainRemoved:
165 removeAttribute("retain", atLoc);
166 return;
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000167 case PropAction_AssignRemoved:
168 return removeAssignForDefaultStrong(props, atLoc);
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +0000169 case PropAction_AssignRewritten:
170 return rewriteAssign(props, atLoc);
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +0000171 case PropAction_MaybeAddWeakOrUnsafe:
172 return maybeAddWeakOrUnsafeUnretainedAttr(props, atLoc);
173 }
174 }
175
176 void doActionForExtensionProp(PropsTy &props, SourceLocation atLoc) {
177 llvm::DenseMap<IdentifierInfo *, PropActionKind>::iterator I;
178 I = ActionOnProp.find(props[0].PropD->getIdentifier());
179 if (I == ActionOnProp.end())
180 return;
181
182 doPropAction(I->second, props, atLoc, false);
183 }
184
185 void rewriteProperty(PropsTy &props, SourceLocation atLoc) {
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000186 ObjCPropertyDecl::PropertyAttributeKind propAttrs = getPropertyAttrs(props);
187
188 if (propAttrs & (ObjCPropertyDecl::OBJC_PR_copy |
189 ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
190 ObjCPropertyDecl::OBJC_PR_strong |
191 ObjCPropertyDecl::OBJC_PR_weak))
192 return;
193
194 if (propAttrs & ObjCPropertyDecl::OBJC_PR_retain) {
Argyrios Kyrtzidis8b08eb32011-11-08 23:09:34 +0000195 // strong is the default.
196 return doPropAction(PropAction_RetainRemoved, props, atLoc);
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000197 }
198
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000199 bool HasIvarAssignedAPlusOneObject = hasIvarAssignedAPlusOneObject(props);
200
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000201 if (propAttrs & ObjCPropertyDecl::OBJC_PR_assign) {
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000202 if (HasIvarAssignedAPlusOneObject ||
203 (Pass.isGCMigration() && !hasGCWeak(props, atLoc))) {
204 return doPropAction(PropAction_AssignRemoved, props, atLoc);
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000205 }
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +0000206 return doPropAction(PropAction_AssignRewritten, props, atLoc);
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000207 }
208
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000209 if (HasIvarAssignedAPlusOneObject ||
210 (Pass.isGCMigration() && !hasGCWeak(props, atLoc)))
Argyrios Kyrtzidisaf9b5e92011-11-08 22:10:58 +0000211 return; // 'strong' by default.
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000212
Argyrios Kyrtzidis44679012011-10-18 19:49:19 +0000213 return doPropAction(PropAction_MaybeAddWeakOrUnsafe, props, atLoc);
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000214 }
215
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000216 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 Kyrtzidis14c4b442011-07-13 19:22:00 +0000229 void rewriteAssign(PropsTy &props, SourceLocation atLoc) const {
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000230 bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
231 /*AllowOnUnknownClass=*/Pass.isGCMigration());
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000232
233 bool rewroteAttr = rewriteAttribute("assign",
234 canUseWeak ? "weak" : "unsafe_unretained",
235 atLoc);
236 if (!rewroteAttr)
237 canUseWeak = false;
238
239 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
Argyrios Kyrtzidis6d7d16d2011-11-28 00:23:12 +0000240 if (isUserDeclared(I->IvarD)) {
241 if (I->IvarD &&
242 I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
243 Pass.TA.insert(I->IvarD->getLocation(),
244 canUseWeak ? "__weak " : "__unsafe_unretained ");
245 }
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000246 if (I->ImplD)
247 Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership,
248 I->ImplD->getLocation());
249 }
250 }
251
252 void maybeAddWeakOrUnsafeUnretainedAttr(PropsTy &props,
253 SourceLocation atLoc) const {
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000254 bool canUseWeak = canApplyWeak(Pass.Ctx, getPropertyType(props),
255 /*AllowOnUnknownClass=*/Pass.isGCMigration());
Argyrios Kyrtzidis8b08eb32011-11-08 23:09:34 +0000256
257 bool addedAttr = addAttribute(canUseWeak ? "weak" : "unsafe_unretained",
258 atLoc);
259 if (!addedAttr)
260 canUseWeak = false;
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000261
262 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
Argyrios Kyrtzidis6d7d16d2011-11-28 00:23:12 +0000263 if (isUserDeclared(I->IvarD)) {
264 if (I->IvarD &&
265 I->IvarD->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
266 Pass.TA.insert(I->IvarD->getLocation(),
267 canUseWeak ? "__weak " : "__unsafe_unretained ");
268 }
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000269 if (I->ImplD) {
270 Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership,
271 I->ImplD->getLocation());
272 Pass.TA.clearDiagnostic(
273 diag::err_arc_objc_property_default_assign_on_object,
274 I->ImplD->getLocation());
275 }
276 }
277 }
278
Argyrios Kyrtzidis01b2b9b2011-10-17 23:14:16 +0000279 bool removeAttribute(StringRef fromAttr, SourceLocation atLoc) const {
Argyrios Kyrtzidis6da42742011-11-28 02:04:36 +0000280 return MigrateCtx.removePropertyAttribute(fromAttr, atLoc);
Argyrios Kyrtzidis01b2b9b2011-10-17 23:14:16 +0000281 }
282
Chris Lattner5f9e2722011-07-23 10:55:15 +0000283 bool rewriteAttribute(StringRef fromAttr, StringRef toAttr,
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000284 SourceLocation atLoc) const {
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000285 return MigrateCtx.rewritePropertyAttribute(fromAttr, toAttr, atLoc);
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000286 }
287
Chris Lattner5f9e2722011-07-23 10:55:15 +0000288 bool addAttribute(StringRef attr, SourceLocation atLoc) const {
Argyrios Kyrtzidis6da42742011-11-28 02:04:36 +0000289 return MigrateCtx.addPropertyAttribute(attr, atLoc);
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000290 }
291
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000292 class PlusOneAssign : public RecursiveASTVisitor<PlusOneAssign> {
293 ObjCIvarDecl *Ivar;
294 public:
295 PlusOneAssign(ObjCIvarDecl *D) : Ivar(D) {}
296
297 bool VisitBinAssign(BinaryOperator *E) {
298 Expr *lhs = E->getLHS()->IgnoreParenImpCasts();
299 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(lhs)) {
300 if (RE->getDecl() != Ivar)
301 return true;
302
Argyrios Kyrtzidis94a90162011-08-10 21:46:48 +0000303 if (ObjCMessageExpr *
304 ME = dyn_cast<ObjCMessageExpr>(E->getRHS()->IgnoreParenCasts()))
305 if (ME->getMethodFamily() == OMF_retain)
306 return false;
307
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000308 ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(E->getRHS());
309 while (implCE && implCE->getCastKind() == CK_BitCast)
310 implCE = dyn_cast<ImplicitCastExpr>(implCE->getSubExpr());
311
John McCall33e56f32011-09-10 06:18:15 +0000312 if (implCE && implCE->getCastKind() == CK_ARCConsumeObject)
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000313 return false;
314 }
315
316 return true;
317 }
318 };
319
320 bool hasIvarAssignedAPlusOneObject(PropsTy &props) const {
321 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
322 PlusOneAssign oneAssign(I->IvarD);
323 bool notFound = oneAssign.TraverseDecl(CurImplD);
324 if (!notFound)
325 return true;
326 }
327
328 return false;
329 }
330
Argyrios Kyrtzidisbf8455c2011-11-07 18:40:32 +0000331 bool hasIvarWithExplicitARCOwnership(PropsTy &props) const {
332 if (Pass.isGCMigration())
333 return false;
334
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000335 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
336 if (isUserDeclared(I->IvarD)) {
337 if (isa<AttributedType>(I->IvarD->getType()))
338 return true;
339 if (I->IvarD->getType().getLocalQualifiers().getObjCLifetime()
340 != Qualifiers::OCL_Strong)
341 return true;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000342 }
343 }
344
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000345 return false;
346 }
347
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000348 bool hasAllIvarsBacked(PropsTy &props) const {
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000349 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000350 if (!isUserDeclared(I->IvarD))
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000351 return false;
352
353 return true;
354 }
355
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000356 // \brief Returns true if all declarations in the @property have GC __weak.
357 bool hasGCWeak(PropsTy &props, SourceLocation atLoc) const {
358 if (!Pass.isGCMigration())
359 return false;
360 if (props.empty())
361 return false;
362 return MigrateCtx.AtPropsWeak.count(atLoc.getRawEncoding());
363 }
364
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000365 bool isUserDeclared(ObjCIvarDecl *ivarD) const {
366 return ivarD && !ivarD->getSynthesize();
367 }
368
369 QualType getPropertyType(PropsTy &props) const {
370 assert(!props.empty());
Argyrios Kyrtzidis1d5fb8f2011-11-06 18:58:07 +0000371 QualType ty = props[0].PropD->getType().getUnqualifiedType();
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000372
373#ifndef NDEBUG
374 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
Argyrios Kyrtzidis1d5fb8f2011-11-06 18:58:07 +0000375 assert(ty == I->PropD->getType().getUnqualifiedType());
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000376#endif
377
378 return ty;
379 }
380
381 ObjCPropertyDecl::PropertyAttributeKind
382 getPropertyAttrs(PropsTy &props) const {
383 assert(!props.empty());
384 ObjCPropertyDecl::PropertyAttributeKind
385 attrs = props[0].PropD->getPropertyAttributesAsWritten();
386
387#ifndef NDEBUG
388 for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I)
389 assert(attrs == I->PropD->getPropertyAttributesAsWritten());
390#endif
391
392 return attrs;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000393 }
394};
395
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000396} // anonymous namespace
397
Argyrios Kyrtzidisb0d5db12011-11-06 18:57:57 +0000398void PropertyRewriteTraverser::traverseObjCImplementation(
399 ObjCImplementationContext &ImplCtx) {
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000400 PropertiesRewriter(ImplCtx.getMigrationContext())
Argyrios Kyrtzidisb0d5db12011-11-06 18:57:57 +0000401 .doTransform(ImplCtx.getImplementationDecl());
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000402}