Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 1 | //===-- Transforms.h - Tranformations to ARC mode ---------------*- C++ -*-===// |
| 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 | |
| 10 | #ifndef LLVM_CLANG_LIB_ARCMIGRATE_TRANSFORMS_H |
| 11 | #define LLVM_CLANG_LIB_ARCMIGRATE_TRANSFORMS_H |
| 12 | |
| 13 | #include "clang/AST/RecursiveASTVisitor.h" |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 14 | #include "clang/AST/ParentMap.h" |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseSet.h" |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/SaveAndRestore.h" |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 17 | |
| 18 | namespace clang { |
| 19 | class Decl; |
| 20 | class Stmt; |
| 21 | class BlockDecl; |
| 22 | class ObjCMethodDecl; |
| 23 | class FunctionDecl; |
| 24 | |
| 25 | namespace arcmt { |
| 26 | class MigrationPass; |
| 27 | |
| 28 | namespace trans { |
| 29 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 30 | class MigrationContext; |
| 31 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Transformations. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | void rewriteAutoreleasePool(MigrationPass &pass); |
| 37 | void rewriteUnbridgedCasts(MigrationPass &pass); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 38 | void makeAssignARCSafe(MigrationPass &pass); |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame] | 39 | void removeRetainReleaseDeallocFinalize(MigrationPass &pass); |
| 40 | void removeZeroOutPropsInDeallocFinalize(MigrationPass &pass); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 41 | void rewriteUnusedInitDelegate(MigrationPass &pass); |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 42 | void checkAPIUses(MigrationPass &pass); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 43 | |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame] | 44 | void removeEmptyStatementsAndDeallocFinalize(MigrationPass &pass); |
Argyrios Kyrtzidis | fd3455a | 2011-06-21 20:20:42 +0000 | [diff] [blame] | 45 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 46 | class BodyContext { |
| 47 | MigrationContext &MigrateCtx; |
| 48 | ParentMap PMap; |
| 49 | Stmt *TopStmt; |
| 50 | |
| 51 | public: |
| 52 | BodyContext(MigrationContext &MigrateCtx, Stmt *S) |
| 53 | : MigrateCtx(MigrateCtx), PMap(S), TopStmt(S) {} |
| 54 | |
| 55 | MigrationContext &getMigrationContext() { return MigrateCtx; } |
| 56 | ParentMap &getParentMap() { return PMap; } |
| 57 | Stmt *getTopStmt() { return TopStmt; } |
| 58 | }; |
| 59 | |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 60 | class ObjCImplementationContext { |
| 61 | MigrationContext &MigrateCtx; |
| 62 | ObjCImplementationDecl *ImpD; |
| 63 | |
| 64 | public: |
| 65 | ObjCImplementationContext(MigrationContext &MigrateCtx, |
| 66 | ObjCImplementationDecl *D) |
| 67 | : MigrateCtx(MigrateCtx), ImpD(D) {} |
| 68 | |
| 69 | MigrationContext &getMigrationContext() { return MigrateCtx; } |
| 70 | ObjCImplementationDecl *getImplementationDecl() { return ImpD; } |
| 71 | }; |
| 72 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 73 | class ASTTraverser { |
| 74 | public: |
| 75 | virtual ~ASTTraverser(); |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 76 | virtual void traverseTU(MigrationContext &MigrateCtx) { } |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 77 | virtual void traverseBody(BodyContext &BodyCtx) { } |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 78 | virtual void traverseObjCImplementation(ObjCImplementationContext &ImplCtx) {} |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | class MigrationContext { |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 82 | std::vector<ASTTraverser *> Traversers; |
| 83 | |
| 84 | public: |
Argyrios Kyrtzidis | f38fa73 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 85 | MigrationPass &Pass; |
| 86 | |
| 87 | struct GCAttrOccurrence { |
| 88 | enum AttrKind { Weak, Strong } Kind; |
| 89 | SourceLocation Loc; |
| 90 | QualType ModifiedType; |
| 91 | Decl *Dcl; |
| 92 | /// \brief true if the attribute is owned, e.g. it is in a body and not just |
| 93 | /// in an interface. |
| 94 | bool FullyMigratable; |
| 95 | }; |
| 96 | std::vector<GCAttrOccurrence> GCAttrs; |
Argyrios Kyrtzidis | f38fa73 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 97 | llvm::DenseSet<unsigned> AttrSet; |
Argyrios Kyrtzidis | 7cfd7fe | 2011-11-08 02:02:38 +0000 | [diff] [blame] | 98 | llvm::DenseSet<unsigned> RemovedAttrSet; |
Argyrios Kyrtzidis | f38fa73 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 99 | |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 100 | /// \brief Set of raw '@' locations for 'assign' properties group that contain |
| 101 | /// GC __weak. |
| 102 | llvm::DenseSet<unsigned> AtPropsWeak; |
Argyrios Kyrtzidis | bf8455c | 2011-11-07 18:40:32 +0000 | [diff] [blame] | 103 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 104 | explicit MigrationContext(MigrationPass &pass) : Pass(pass) {} |
| 105 | ~MigrationContext(); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 106 | |
| 107 | typedef std::vector<ASTTraverser *>::iterator traverser_iterator; |
| 108 | traverser_iterator traversers_begin() { return Traversers.begin(); } |
| 109 | traverser_iterator traversers_end() { return Traversers.end(); } |
| 110 | |
| 111 | void addTraverser(ASTTraverser *traverser) { |
| 112 | Traversers.push_back(traverser); |
| 113 | } |
| 114 | |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame] | 115 | bool isGCOwnedNonObjC(QualType T); |
Argyrios Kyrtzidis | 6da4274 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 116 | bool removePropertyAttribute(StringRef fromAttr, SourceLocation atLoc) { |
| 117 | return rewritePropertyAttribute(fromAttr, StringRef(), atLoc); |
| 118 | } |
Argyrios Kyrtzidis | b0e1e12 | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 119 | bool rewritePropertyAttribute(StringRef fromAttr, StringRef toAttr, |
| 120 | SourceLocation atLoc); |
Argyrios Kyrtzidis | 6da4274 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 121 | bool addPropertyAttribute(StringRef attr, SourceLocation atLoc); |
Argyrios Kyrtzidis | 1fe4203 | 2011-11-04 23:43:03 +0000 | [diff] [blame] | 122 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 123 | void traverse(TranslationUnitDecl *TU); |
Argyrios Kyrtzidis | 17ac319 | 2011-11-06 18:58:17 +0000 | [diff] [blame] | 124 | |
| 125 | void dumpGCAttrs(); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Argyrios Kyrtzidis | b0d5db1 | 2011-11-06 18:57:57 +0000 | [diff] [blame] | 128 | class PropertyRewriteTraverser : public ASTTraverser { |
| 129 | public: |
| 130 | virtual void traverseObjCImplementation(ObjCImplementationContext &ImplCtx); |
| 131 | }; |
| 132 | |
Argyrios Kyrtzidis | 2a27818 | 2012-03-05 08:46:24 +0000 | [diff] [blame] | 133 | class BlockObjCVariableTraverser : public ASTTraverser { |
| 134 | public: |
| 135 | virtual void traverseBody(BodyContext &BodyCtx); |
| 136 | }; |
| 137 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 138 | // GC transformations |
| 139 | |
Argyrios Kyrtzidis | f38fa73 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 140 | class GCAttrsTraverser : public ASTTraverser { |
| 141 | public: |
| 142 | virtual void traverseTU(MigrationContext &MigrateCtx); |
| 143 | }; |
| 144 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 145 | class GCCollectableCallsTraverser : public ASTTraverser { |
| 146 | public: |
| 147 | virtual void traverseBody(BodyContext &BodyCtx); |
| 148 | }; |
| 149 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 150 | //===----------------------------------------------------------------------===// |
| 151 | // Helpers. |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | |
Argyrios Kyrtzidis | 86625b5 | 2011-07-12 22:05:17 +0000 | [diff] [blame] | 154 | /// \brief Determine whether we can add weak to the given type. |
Argyrios Kyrtzidis | 12192cf | 2011-11-07 18:40:29 +0000 | [diff] [blame] | 155 | bool canApplyWeak(ASTContext &Ctx, QualType type, |
| 156 | bool AllowOnUnknownClass = false); |
Argyrios Kyrtzidis | 86625b5 | 2011-07-12 22:05:17 +0000 | [diff] [blame] | 157 | |
Argyrios Kyrtzidis | 1b8fbd3 | 2012-05-23 21:50:04 +0000 | [diff] [blame] | 158 | bool isPlusOneAssign(const BinaryOperator *E); |
| 159 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 160 | /// \brief 'Loc' is the end of a statement range. This returns the location |
| 161 | /// immediately after the semicolon following the statement. |
| 162 | /// If no semicolon is found or the location is inside a macro, the returned |
| 163 | /// source location will be invalid. |
| 164 | SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx); |
| 165 | |
Argyrios Kyrtzidis | aec230d | 2011-09-01 20:53:18 +0000 | [diff] [blame] | 166 | /// \brief \arg Loc is the end of a statement range. This returns the location |
| 167 | /// of the semicolon following the statement. |
| 168 | /// If no semicolon is found or the location is inside a macro, the returned |
| 169 | /// source location will be invalid. |
| 170 | SourceLocation findSemiAfterLocation(SourceLocation loc, ASTContext &Ctx); |
| 171 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 172 | bool hasSideEffects(Expr *E, ASTContext &Ctx); |
Argyrios Kyrtzidis | 2c18ca0 | 2011-07-14 23:32:04 +0000 | [diff] [blame] | 173 | bool isGlobalVar(Expr *E); |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 174 | /// \brief Returns "nil" or "0" if 'nil' macro is not actually defined. |
| 175 | StringRef getNilString(ASTContext &Ctx); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 176 | |
| 177 | template <typename BODY_TRANS> |
| 178 | class BodyTransform : public RecursiveASTVisitor<BodyTransform<BODY_TRANS> > { |
| 179 | MigrationPass &Pass; |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 180 | Decl *ParentD; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 181 | |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 182 | typedef RecursiveASTVisitor<BodyTransform<BODY_TRANS> > base; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 183 | public: |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 184 | BodyTransform(MigrationPass &pass) : Pass(pass), ParentD(0) { } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 185 | |
Argyrios Kyrtzidis | b1094a0 | 2011-06-23 21:21:33 +0000 | [diff] [blame] | 186 | bool TraverseStmt(Stmt *rootS) { |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 187 | if (rootS) |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 188 | BODY_TRANS(Pass).transformBody(rootS, ParentD); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
Argyrios Kyrtzidis | 76a5245 | 2012-06-07 00:44:06 +0000 | [diff] [blame] | 191 | |
| 192 | bool TraverseObjCMethodDecl(ObjCMethodDecl *D) { |
| 193 | SaveAndRestore<Decl *> SetParent(ParentD, D); |
| 194 | return base::TraverseObjCMethodDecl(D); |
| 195 | } |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | typedef llvm::DenseSet<Expr *> ExprSet; |
| 199 | |
| 200 | void clearRefsIn(Stmt *S, ExprSet &refs); |
| 201 | template <typename iterator> |
| 202 | void clearRefsIn(iterator begin, iterator end, ExprSet &refs) { |
| 203 | for (; begin != end; ++begin) |
| 204 | clearRefsIn(*begin, refs); |
| 205 | } |
| 206 | |
| 207 | void collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs); |
| 208 | |
| 209 | void collectRemovables(Stmt *S, ExprSet &exprs); |
| 210 | |
| 211 | } // end namespace trans |
| 212 | |
| 213 | } // end namespace arcmt |
| 214 | |
| 215 | } // end namespace clang |
| 216 | |
| 217 | #endif |