1 //===--- TransBlockObjCVariable.cpp - Tranformations to ARC mode ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // rewriteBlockObjCVariable:
12 // Adding __block to an obj-c variable could be either because the the variable
13 // is used for output storage or the user wanted to break a retain cycle.
14 // This transformation checks whether a reference of the variable for the block
15 // is actually needed (it is assigned to or its address is taken) or not.
16 // If the reference is not needed it will assume __block was added to break a
17 // cycle so it will remove '__block' and add __weak/__unsafe_unretained.
21 // bar(^ { [x cake]; });
24 // bar(^ { [x cake]; });
26 //===----------------------------------------------------------------------===//
28 #include "Transforms.h"
29 #include "Internals.h"
30 #include "clang/Basic/SourceManager.h"
32 using namespace clang;
33 using namespace arcmt;
34 using namespace trans;
38 class RootBlockObjCVarRewriter :
39 public RecursiveASTVisitor<RootBlockObjCVarRewriter> {
41 llvm::DenseSet<VarDecl *> CheckedVars;
43 class BlockVarChecker : public RecursiveASTVisitor<BlockVarChecker> {
46 typedef RecursiveASTVisitor<BlockVarChecker> base;
48 BlockVarChecker(VarDecl *var) : Var(var) { }
50 bool TraverseImplicitCastExpr(ImplicitCastExpr *castE) {
51 if (BlockDeclRefExpr *
52 ref = dyn_cast<BlockDeclRefExpr>(castE->getSubExpr())) {
53 if (ref->getDecl() == Var) {
54 if (castE->getCastKind() == CK_LValueToRValue)
55 return true; // Using the value of the variable.
56 if (castE->getCastKind() == CK_NoOp && castE->isLValue() &&
57 Var->getASTContext().getLangOptions().CPlusPlus)
58 return true; // Binding to const C++ reference.
62 return base::TraverseImplicitCastExpr(castE);
65 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
66 if (E->getDecl() == Var)
67 return false; // The reference of the variable, and not just its value,
74 RootBlockObjCVarRewriter(MigrationPass &pass) : Pass(pass) { }
76 bool VisitBlockDecl(BlockDecl *block) {
77 SmallVector<VarDecl *, 4> BlockVars;
79 for (BlockDecl::capture_iterator
80 I = block->capture_begin(), E = block->capture_end(); I != E; ++I) {
81 VarDecl *var = I->getVariable();
83 !isAlreadyChecked(var) &&
84 var->getType()->isObjCObjectPointerType() &&
85 isImplicitStrong(var->getType())) {
86 BlockVars.push_back(var);
90 for (unsigned i = 0, e = BlockVars.size(); i != e; ++i) {
91 VarDecl *var = BlockVars[i];
92 CheckedVars.insert(var);
94 BlockVarChecker checker(var);
95 bool onlyValueOfVarIsNeeded = checker.TraverseStmt(block->getBody());
96 if (onlyValueOfVarIsNeeded) {
97 BlocksAttr *attr = var->getAttr<BlocksAttr>();
100 bool useWeak = canApplyWeak(Pass.Ctx, var->getType());
101 SourceManager &SM = Pass.Ctx.getSourceManager();
102 Transaction Trans(Pass.TA);
103 Pass.TA.replaceText(SM.getExpansionLoc(attr->getLocation()),
105 useWeak ? "__weak" : "__unsafe_unretained");
114 bool isAlreadyChecked(VarDecl *VD) {
115 return CheckedVars.count(VD);
118 bool isImplicitStrong(QualType ty) {
119 if (isa<AttributedType>(ty.getTypePtr()))
121 return ty.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong;
125 class BlockObjCVarRewriter : public RecursiveASTVisitor<BlockObjCVarRewriter> {
129 BlockObjCVarRewriter(MigrationPass &pass) : Pass(pass) { }
131 bool TraverseBlockDecl(BlockDecl *block) {
132 RootBlockObjCVarRewriter(Pass).TraverseDecl(block);
137 } // anonymous namespace
139 void trans::rewriteBlockObjCVariable(MigrationPass &pass) {
140 BlockObjCVarRewriter trans(pass);
141 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());