Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Hacks and fun related to the code rewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 22a4efd | 2009-05-18 22:50:54 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTConsumers.h" |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 4478db9 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | ae43eb7 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Benjamin Kramer | 2ec1729 | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 28 | using namespace clang; |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 29 | using llvm::utostr; |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 30 | |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | namespace { |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 32 | class RewriteObjC : public ASTConsumer { |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 33 | Rewriter Rewrite; |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 34 | Diagnostic &Diags; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 35 | const LangOptions &LangOpts; |
Steve Naroff | 53b6f4c | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 36 | unsigned RewriteFailedDiag; |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 37 | unsigned TryFinallyContainsReturnDiag; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 38 | |
Chris Lattner | bf0bfa6 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 39 | ASTContext *Context; |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 40 | SourceManager *SM; |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 41 | TranslationUnitDecl *TUDecl; |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 42 | FileID MainFileID; |
Chris Lattner | ae43eb7 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 43 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 44 | SourceLocation LastIncLoc; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 45 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 46 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 47 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 48 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 49 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 50 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 51 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 52 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 53 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 54 | // Remember all the @protocol(<expr>) expressions. |
| 55 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 56 | |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 57 | unsigned NumObjCStringLiterals; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 58 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 59 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 60 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 61 | FunctionDecl *MsgSendStretFunctionDecl; |
| 62 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 63 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 64 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 65 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 66 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 67 | FunctionDecl *CFStringFunctionDecl; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 68 | FunctionDecl *SuperContructorFunctionDecl; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 69 | |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 70 | // ObjC string constant support. |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 71 | VarDecl *ConstantStringClassReference; |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 72 | RecordDecl *NSStringRecord; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 73 | |
Fariborz Jahanian | 2227742 | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 74 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 75 | int BcLabelCount; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 76 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 77 | // Needed for super. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 78 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 79 | RecordDecl *SuperStructDecl; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 80 | RecordDecl *ConstantStringDecl; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 81 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 82 | TypeDecl *ProtocolTypeDecl; |
| 83 | QualType getProtocolType(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 84 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 85 | // Needed for header files being rewritten |
| 86 | bool IsHeader; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 87 | |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 88 | std::string InFileName; |
Eli Friedman | a9c31084 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 89 | llvm::raw_ostream* OutFile; |
Eli Friedman | 35ba7a6 | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 90 | |
| 91 | bool SilenceRewriteMacroWarning; |
| 92 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 93 | std::string Preamble; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 94 | |
| 95 | // Block expressions. |
| 96 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 97 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 98 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 99 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 100 | // Block related declarations. |
| 101 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 102 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 103 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 104 | |
| 105 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 106 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 107 | // This maps a property to it's assignment statement. |
| 108 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 109 | // This maps a property to it's synthesied message expression. |
| 110 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 111 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 112 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 113 | // This maps an original source AST to it's rewritten form. This allows |
| 114 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 115 | // This is needed to support some of the exotic property rewriting. |
| 116 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 117 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 118 | FunctionDecl *CurFunctionDef; |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 119 | VarDecl *GlobalVarDecl; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 120 | |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 121 | bool DisableReplaceStmt; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 122 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 123 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 124 | public: |
Ted Kremenek | 79b50cb | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 125 | virtual void Initialize(ASTContext &context); |
| 126 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 127 | // Top Level Driver code. |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 128 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 129 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 130 | HandleTopLevelSingleDecl(*I); |
| 131 | } |
| 132 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 133 | void HandleDeclInMainFile(Decl *D); |
Eli Friedman | a9c31084 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 134 | RewriteObjC(std::string inFile, llvm::raw_ostream *OS, |
Eli Friedman | 35ba7a6 | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 135 | Diagnostic &D, const LangOptions &LOpts, |
| 136 | bool silenceMacroWarn); |
Ted Kremenek | cab0b0c | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 137 | |
| 138 | ~RewriteObjC() {} |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 139 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 140 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 141 | |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 142 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 143 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 144 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 145 | if (ReplacingStmt) |
| 146 | return; // We can't rewrite the same node twice. |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 147 | |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 148 | if (DisableReplaceStmt) |
| 149 | return; // Used when rewriting the assignment of a property setter. |
| 150 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 151 | // If replacement succeeded or warning disabled return with no warning. |
| 152 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 153 | ReplacedNodes[Old] = New; |
| 154 | return; |
| 155 | } |
| 156 | if (SilenceRewriteMacroWarning) |
| 157 | return; |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 158 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 159 | << Old->getSourceRange(); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 160 | } |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 161 | |
| 162 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 163 | // Measaure the old text. |
| 164 | int Size = Rewrite.getRangeSize(SrcRange); |
| 165 | if (Size == -1) { |
| 166 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 167 | << Old->getSourceRange(); |
| 168 | return; |
| 169 | } |
| 170 | // Get the new text. |
| 171 | std::string SStr; |
| 172 | llvm::raw_string_ostream S(SStr); |
Chris Lattner | 7099c78 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 173 | New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 174 | const std::string &Str = S.str(); |
| 175 | |
| 176 | // If replacement succeeded or warning disabled return with no warning. |
Daniel Dunbar | 60987c9 | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 177 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 178 | ReplacedNodes[Old] = New; |
| 179 | return; |
| 180 | } |
| 181 | if (SilenceRewriteMacroWarning) |
| 182 | return; |
| 183 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 184 | << Old->getSourceRange(); |
| 185 | } |
| 186 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 187 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 188 | bool InsertAfter = true) { |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 189 | // If insertion succeeded or warning disabled return with no warning. |
Daniel Dunbar | 60987c9 | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 190 | if (!Rewrite.InsertText(Loc, llvm::StringRef(StrData, StrLen), |
| 191 | InsertAfter) || |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 192 | SilenceRewriteMacroWarning) |
| 193 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 194 | |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 195 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 196 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 197 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 198 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 199 | // If removal succeeded or warning disabled return with no warning. |
| 200 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 201 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 202 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 203 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 204 | } |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 205 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 206 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 207 | const char *NewStr, unsigned NewLength) { |
| 208 | // If removal succeeded or warning disabled return with no warning. |
Daniel Dunbar | 60987c9 | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 209 | if (!Rewrite.ReplaceText(Start, OrigLength, |
| 210 | llvm::StringRef(NewStr, NewLength)) || |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 211 | SilenceRewriteMacroWarning) |
| 212 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 213 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 214 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 215 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 216 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 217 | // Syntactic Rewriting. |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 218 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 219 | void RewriteInclude(); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 220 | void RewriteTabs(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 221 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 222 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 223 | ObjCImplementationDecl *IMD, |
| 224 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 225 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 226 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 227 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 228 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 229 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 230 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 231 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 232 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 233 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 234 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 235 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 236 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 237 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 238 | QualType getSuperStructType(); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 239 | QualType getConstantStringStructType(); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 240 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 241 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 242 | // Expression Rewriting. |
Steve Naroff | 334fbc2 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 243 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 244 | void CollectPropertySetters(Stmt *S); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 245 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 246 | Stmt *CurrentBody; |
| 247 | ParentMap *PropParentMap; // created lazily. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 248 | |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 249 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 250 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 251 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 252 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 253 | SourceRange SrcRange); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 254 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 255 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 256 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 257 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 258 | void WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 259 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 260 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 261 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 262 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 263 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 2c02216 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 264 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 265 | SourceLocation OrigEnd); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 266 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 267 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 268 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 269 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 270 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 271 | void SynthCountByEnumWithState(std::string &buf); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 272 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 273 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 274 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 275 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 276 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 277 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 278 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 279 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 280 | void SynthSelGetUidFunctionDecl(); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 281 | void SynthSuperContructorFunctionDecl(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 282 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 283 | // Metadata emission. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 284 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 285 | std::string &Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 286 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 287 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 288 | std::string &Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 289 | |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 290 | template<typename MethodIterator> |
| 291 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 292 | MethodIterator MethodEnd, |
Fariborz Jahanian | a398637 | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 293 | bool IsInstanceMethod, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 294 | const char *prefix, |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 295 | const char *ClassName, |
| 296 | std::string &Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 297 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 298 | void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, |
| 299 | const char *prefix, |
| 300 | const char *ClassName, |
| 301 | std::string &Result); |
| 302 | void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 303 | const char *prefix, |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 304 | const char *ClassName, |
| 305 | std::string &Result); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 306 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 307 | std::string &Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 308 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 309 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 310 | std::string &Result); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 311 | void RewriteImplementations(); |
| 312 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 313 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 314 | // Block rewriting. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 315 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 316 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 317 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 318 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 319 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 320 | |
| 321 | // Block specific rewrite rules. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 322 | void RewriteBlockCall(CallExpr *Exp); |
| 323 | void RewriteBlockPointerDecl(NamedDecl *VD); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 324 | Stmt *RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 325 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 326 | |
| 327 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 328 | const char *funcName, std::string Tag); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 329 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 330 | const char *funcName, std::string Tag); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 331 | std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 332 | bool hasCopyDisposeHelpers); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 333 | Stmt *SynthesizeBlockCall(CallExpr *Exp); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 334 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 335 | const char *FunName); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 336 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 337 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 338 | void GetBlockCallExprs(Stmt *S); |
| 339 | void GetBlockDeclRefExprs(Stmt *S); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 340 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 341 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 342 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 343 | bool isTopLevelBlockPointerType(QualType T) { |
| 344 | return isa<BlockPointerType>(T); |
| 345 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 346 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 347 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 348 | bool isObjCType(QualType T) { |
| 349 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 350 | return false; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 351 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 352 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 353 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 354 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 355 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 356 | return true; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 357 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 358 | if (const PointerType *PT = OCT->getAs<PointerType>()) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 359 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 360 | PT->getPointeeType()->isObjCQualifiedIdType()) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 361 | return true; |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 366 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 367 | const char *&RParen); |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 368 | void RewriteCastExpr(CStyleCastExpr *CE); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 369 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 370 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 371 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 372 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 373 | void QuoteDoublequotes(std::string &From, std::string &To) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 374 | for (unsigned i = 0; i < From.length(); i++) { |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 375 | if (From[i] == '"') |
| 376 | To += "\\\""; |
| 377 | else |
| 378 | To += From[i]; |
| 379 | } |
| 380 | } |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 381 | }; |
| 382 | } |
| 383 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 384 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
| 385 | NamedDecl *D) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 386 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 387 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 388 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 389 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 390 | // All the args are checked/rewritten. Don't call twice! |
| 391 | RewriteBlockPointerDecl(D); |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 398 | const PointerType *PT = funcType->getAs<PointerType>(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 399 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 400 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 403 | static bool IsHeaderFile(const std::string &Filename) { |
| 404 | std::string::size_type DotPos = Filename.rfind('.'); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 405 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 406 | if (DotPos == std::string::npos) { |
| 407 | // no file extension |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 408 | return false; |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 409 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 410 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 411 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 412 | // C header: .h |
| 413 | // C++ header: .hh or .H; |
| 414 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 415 | } |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 416 | |
Eli Friedman | a9c31084 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 417 | RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, |
Eli Friedman | 35ba7a6 | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 418 | Diagnostic &D, const LangOptions &LOpts, |
| 419 | bool silenceMacroWarn) |
| 420 | : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), |
| 421 | SilenceRewriteMacroWarning(silenceMacroWarn) { |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 422 | IsHeader = IsHeaderFile(inFile); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 423 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 424 | "rewriting sub-expression within a macro (may not be correct)"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 425 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 426 | "rewriter doesn't support user-specified control flow semantics " |
| 427 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Eli Friedman | 848763f | 2009-05-18 22:29:17 +0000 | [diff] [blame] | 430 | ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, |
| 431 | llvm::raw_ostream* OS, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 432 | Diagnostic &Diags, |
Eli Friedman | 35ba7a6 | 2009-05-18 22:39:16 +0000 | [diff] [blame] | 433 | const LangOptions &LOpts, |
| 434 | bool SilenceRewriteMacroWarning) { |
| 435 | return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 436 | } |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 437 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 438 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 439 | Context = &context; |
| 440 | SM = &Context->getSourceManager(); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 441 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 442 | MsgSendFunctionDecl = 0; |
| 443 | MsgSendSuperFunctionDecl = 0; |
| 444 | MsgSendStretFunctionDecl = 0; |
| 445 | MsgSendSuperStretFunctionDecl = 0; |
| 446 | MsgSendFpretFunctionDecl = 0; |
| 447 | GetClassFunctionDecl = 0; |
| 448 | GetMetaClassFunctionDecl = 0; |
| 449 | SelGetUidFunctionDecl = 0; |
| 450 | CFStringFunctionDecl = 0; |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 451 | ConstantStringClassReference = 0; |
| 452 | NSStringRecord = 0; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 453 | CurMethodDef = 0; |
| 454 | CurFunctionDef = 0; |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 455 | GlobalVarDecl = 0; |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 456 | SuperStructDecl = 0; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 457 | ProtocolTypeDecl = 0; |
Steve Naroff | 053ae3f | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 458 | ConstantStringDecl = 0; |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 459 | BcLabelCount = 0; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 460 | SuperContructorFunctionDecl = 0; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 461 | NumObjCStringLiterals = 0; |
Steve Naroff | bad6f3b | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 462 | PropParentMap = 0; |
| 463 | CurrentBody = 0; |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 464 | DisableReplaceStmt = false; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 465 | |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 466 | // Get the ID and start/end of the main file. |
| 467 | MainFileID = SM->getMainFileID(); |
| 468 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 469 | MainFileStart = MainBuf->getBufferStart(); |
| 470 | MainFileEnd = MainBuf->getBufferEnd(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 471 | |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 472 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 473 | |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 474 | // declaring objc_selector outside the parameter list removes a silly |
| 475 | // scope related warning... |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 476 | if (IsHeader) |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 477 | Preamble = "#pragma once\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 478 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 479 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 480 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 481 | if (LangOpts.Microsoft) { |
| 482 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 483 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 484 | ": "; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 485 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 486 | } |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 487 | Preamble += "};\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 488 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 489 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 490 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 491 | Preamble += "#endif\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 492 | if (LangOpts.Microsoft) { |
| 493 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 494 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 495 | } else |
| 496 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 497 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 498 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 499 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 500 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 501 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 502 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 503 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 504 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 505 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 506 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 507 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 508 | Preamble += "(const char *);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 509 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 510 | Preamble += "(const char *);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 511 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 512 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 513 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 514 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 515 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 3e2c98c | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 516 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | cd25d78 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 517 | // @synchronized hooks. |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 518 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 519 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 520 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 521 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 522 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 523 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | c960e9d | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 524 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 525 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 526 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 527 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 528 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 529 | Preamble += "#endif\n"; |
| 530 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 531 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 532 | Preamble += " int *isa;\n"; |
| 533 | Preamble += " int flags;\n"; |
| 534 | Preamble += " char *str;\n"; |
| 535 | Preamble += " long length;\n"; |
| 536 | Preamble += "};\n"; |
Steve Naroff | c6e571e | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 537 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 538 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 539 | Preamble += "#else\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 540 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | c6e571e | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 541 | Preamble += "#endif\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 542 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 543 | Preamble += "#endif\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 544 | // Blocks preamble. |
| 545 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 546 | Preamble += "#define BLOCK_IMPL\n"; |
| 547 | Preamble += "struct __block_impl {\n"; |
| 548 | Preamble += " void *isa;\n"; |
| 549 | Preamble += " int Flags;\n"; |
| 550 | Preamble += " int Size;\n"; |
| 551 | Preamble += " void *FuncPtr;\n"; |
| 552 | Preamble += "};\n"; |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 553 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 554 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 555 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 556 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 557 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteStackBlock[32];\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 558 | Preamble += "#endif\n"; |
Steve Naroff | 39ba90e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 559 | if (LangOpts.Microsoft) { |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 560 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 561 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | 39ba90e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 562 | Preamble += "#define __attribute__(X)\n"; |
| 563 | } |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 567 | //===----------------------------------------------------------------------===// |
| 568 | // Top Level Driver Code |
| 569 | //===----------------------------------------------------------------------===// |
| 570 | |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 571 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 572 | // Two cases: either the decl could be in the main file, or it could be in a |
| 573 | // #included file. If the former, rewrite it now. If the later, check to see |
| 574 | // if we rewrote the #include/#import. |
| 575 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 576 | Loc = SM->getInstantiationLoc(Loc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 577 | |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 578 | // If this is for a builtin, ignore it. |
| 579 | if (Loc.isInvalid()) return; |
| 580 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 581 | // Look for built-in declarations that we need to refer during the rewrite. |
| 582 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 583 | RewriteFunctionDecl(FD); |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 584 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 585 | // declared in <Foundation/NSString.h> |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 586 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 587 | ConstantStringClassReference = FVD; |
| 588 | return; |
| 589 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 590 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 591 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 592 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 593 | RewriteCategoryDecl(CD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 594 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 595 | RewriteProtocolDecl(PD); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 596 | } else if (ObjCForwardProtocolDecl *FP = |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 597 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 598 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 599 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 600 | // Recurse into linkage specifications |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 601 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 602 | DIEnd = LSD->decls_end(); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 603 | DI != DIEnd; ++DI) |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 604 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 606 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | 2450c26 | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 607 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 608 | return HandleDeclInMainFile(D); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 611 | //===----------------------------------------------------------------------===// |
| 612 | // Syntactic (non-AST) Rewriting Code |
| 613 | //===----------------------------------------------------------------------===// |
| 614 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 615 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 616 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 617 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 618 | const char *MainBufStart = MainBuf.first; |
| 619 | const char *MainBufEnd = MainBuf.second; |
| 620 | size_t ImportLen = strlen("import"); |
| 621 | size_t IncludeLen = strlen("include"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 622 | |
Fariborz Jahanian | c81ed74 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 623 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 624 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 625 | if (*BufPtr == '#') { |
| 626 | if (++BufPtr == MainBufEnd) |
| 627 | return; |
| 628 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 629 | if (++BufPtr == MainBufEnd) |
| 630 | return; |
| 631 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 632 | // replace import with include |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 633 | SourceLocation ImportLoc = |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 634 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 635 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 636 | BufPtr += ImportLen; |
| 637 | } |
| 638 | } |
| 639 | } |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 642 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 643 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 644 | const char *MainBufStart = MainBuf.first; |
| 645 | const char *MainBufEnd = MainBuf.second; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 646 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 647 | // Loop over the whole file, looking for tabs. |
| 648 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 649 | if (*BufPtr != '\t') |
| 650 | continue; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 651 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 652 | // Okay, we found a tab. This tab will turn into at least one character, |
| 653 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 654 | unsigned VCol = 0; |
| 655 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 656 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 657 | ++VCol; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 658 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 659 | // Okay, now that we know the virtual column, we know how many spaces to |
| 660 | // insert. We assume 8-character tab-stops. |
| 661 | unsigned Spaces = 8-(VCol & 7); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 662 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 663 | // Get the location of the tab. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 664 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 665 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 666 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 667 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 668 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 669 | } |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 672 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 673 | ObjCIvarDecl *OID) { |
| 674 | std::string S; |
| 675 | S = "((struct "; |
| 676 | S += ClassDecl->getIdentifier()->getName(); |
| 677 | S += "_IMPL *)self)->"; |
| 678 | S += OID->getNameAsCString(); |
| 679 | return S; |
| 680 | } |
| 681 | |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 682 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 683 | ObjCImplementationDecl *IMD, |
| 684 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 685 | SourceLocation startLoc = PID->getLocStart(); |
| 686 | InsertText(startLoc, "// ", 3); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 687 | const char *startBuf = SM->getCharacterData(startLoc); |
| 688 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 689 | const char *semiBuf = strchr(startBuf, ';'); |
| 690 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 691 | SourceLocation onePastSemiLoc = |
| 692 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 693 | |
| 694 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 695 | return; // FIXME: is this correct? |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 696 | |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 697 | // Generate the 'getter' function. |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 698 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 699 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 700 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 701 | |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 702 | if (!OID) |
| 703 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 704 | |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 705 | std::string Getr; |
| 706 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 707 | Getr += "{ "; |
| 708 | // Synthesize an explicit cast to gain access to the ivar. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 709 | // FIXME: deal with code generation implications for various property |
| 710 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 11671e7 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 711 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 712 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 713 | Getr += "; }"; |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 714 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 715 | if (PD->isReadOnly()) |
| 716 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 717 | |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 718 | // Generate the 'setter' function. |
| 719 | std::string Setr; |
| 720 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 721 | Setr += "{ "; |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 722 | // Synthesize an explicit cast to initialize the ivar. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 723 | // FIXME: deal with code generation implications for various property |
| 724 | // attributes (copy, retain, nonatomic). |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 725 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 726 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 727 | Setr += PD->getNameAsCString(); |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 728 | Setr += "; }"; |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 729 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 730 | } |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 731 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 732 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 733 | // Get the start location and compute the semi location. |
| 734 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 735 | const char *startBuf = SM->getCharacterData(startLoc); |
| 736 | const char *semiPtr = strchr(startBuf, ';'); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 737 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 738 | // Translate to typedef's that forward reference structs with the same name |
| 739 | // as the class. As a convenience, we include the original declaration |
| 740 | // as a comment. |
| 741 | std::string typedefString; |
| 742 | typedefString += "// "; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 743 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 744 | typedefString += "\n"; |
Chris Lattner | 6a02874 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 745 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 746 | I != E; ++I) { |
| 747 | ObjCInterfaceDecl *ForwardDecl = *I; |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 748 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 749 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 750 | typedefString += "\n"; |
| 751 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 752 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 753 | typedefString += "\n"; |
Steve Naroff | 4242b97 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 754 | typedefString += "typedef struct objc_object "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 755 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 756 | typedefString += ";\n#endif\n"; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 757 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 758 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 759 | // Replace the @class with typedefs corresponding to the classes. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 760 | ReplaceText(startLoc, semiPtr-startBuf+1, |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 761 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 764 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 765 | SourceLocation LocStart = Method->getLocStart(); |
| 766 | SourceLocation LocEnd = Method->getLocEnd(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 767 | |
Chris Lattner | 2d89c56 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 768 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 769 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | 99f50fe | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 770 | InsertText(LocStart, "#if 0\n", 6); |
| 771 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 772 | } else { |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 773 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 777 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 778 | SourceLocation Loc = prop->getLocation(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 779 | |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 780 | ReplaceText(Loc, 0, "// ", 3); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 781 | |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 782 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | eca7fad | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 785 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 786 | SourceLocation LocStart = CatDecl->getLocStart(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 787 | |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 788 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 789 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 790 | |
| 791 | for (ObjCCategoryDecl::instmeth_iterator |
| 792 | I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 793 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 794 | RewriteMethodDeclaration(*I); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 795 | for (ObjCCategoryDecl::classmeth_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 796 | I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 797 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 798 | RewriteMethodDeclaration(*I); |
| 799 | |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 800 | // Lastly, comment out the @end. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 801 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 804 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 805 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 806 | |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 807 | SourceLocation LocStart = PDecl->getLocStart(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 808 | |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 809 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 810 | ReplaceText(LocStart, 0, "// ", 3); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 811 | |
| 812 | for (ObjCProtocolDecl::instmeth_iterator |
| 813 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 814 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 815 | RewriteMethodDeclaration(*I); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 816 | for (ObjCProtocolDecl::classmeth_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 817 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 818 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 819 | RewriteMethodDeclaration(*I); |
| 820 | |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 821 | // Lastly, comment out the @end. |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 822 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 823 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 824 | |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 825 | // Must comment out @optional/@required |
| 826 | const char *startBuf = SM->getCharacterData(LocStart); |
| 827 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 828 | for (const char *p = startBuf; p < endBuf; p++) { |
| 829 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 830 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 831 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 832 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 833 | CommentedOptional.c_str(), CommentedOptional.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 834 | |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 835 | } |
| 836 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 837 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 838 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 839 | ReplaceText(OptionalLoc, strlen("@required"), |
| 840 | CommentedRequired.c_str(), CommentedRequired.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 841 | |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 846 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 847 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | 0540f3f | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 848 | if (LocStart.isInvalid()) |
| 849 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 850 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 851 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 854 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 855 | std::string &ResultStr) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 856 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 857 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 858 | ResultStr += "\nstatic "; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 859 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 860 | ResultStr += "id"; |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 861 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 862 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 863 | // needs special handling, since pointer-to-functions have special |
| 864 | // syntax (where a decaration models use). |
| 865 | QualType retType = OMD->getResultType(); |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 866 | QualType PointeeTy; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 867 | if (const PointerType* PT = retType->getAs<PointerType>()) |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 868 | PointeeTy = PT->getPointeeType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 869 | else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 870 | PointeeTy = BPT->getPointeeType(); |
| 871 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 872 | ResultStr += FPRetType->getResultType().getAsString(); |
| 873 | ResultStr += "(*"; |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 874 | } |
| 875 | } else |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 876 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 7202d98 | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 877 | ResultStr += " "; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 878 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 879 | // Unique method name |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 880 | std::string NameStr; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 881 | |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 882 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 883 | NameStr += "_I_"; |
| 884 | else |
| 885 | NameStr += "_C_"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 886 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 887 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 888 | NameStr += "_"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 889 | |
| 890 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 438be77 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 891 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 892 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 893 | NameStr += "_"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 894 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 895 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 896 | { |
| 897 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 898 | int len = selString.size(); |
| 899 | for (int i = 0; i < len; i++) |
| 900 | if (selString[i] == ':') |
| 901 | selString[i] = '_'; |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 902 | NameStr += selString; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 903 | } |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 904 | // Remember this name for metadata emission |
| 905 | MethodInternalNames[OMD] = NameStr; |
| 906 | ResultStr += NameStr; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 907 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 908 | // Rewrite arguments |
| 909 | ResultStr += "("; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 910 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 911 | // invisible arguments |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 912 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 913 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 914 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 915 | if (!LangOpts.Microsoft) { |
| 916 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 917 | ResultStr += "struct "; |
| 918 | } |
| 919 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 920 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 921 | ResultStr += " *"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 922 | } |
| 923 | else |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 924 | ResultStr += Context->getObjCClassType().getAsString(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 925 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 926 | ResultStr += " self, "; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 927 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 928 | ResultStr += " _cmd"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 929 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 930 | // Method arguments. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 931 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 932 | E = OMD->param_end(); PI != E; ++PI) { |
| 933 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 934 | ResultStr += ", "; |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 935 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 936 | ResultStr += "id "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 937 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 938 | } else { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 939 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 940 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | 4e2ae51 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 941 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 942 | const BlockPointerType *BPT = PDecl->getType()->getAs<BlockPointerType>(); |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 943 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name, |
| 944 | Context->PrintingPolicy); |
Steve Naroff | 4e2ae51 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 945 | } else |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 946 | PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 947 | ResultStr += Name; |
| 948 | } |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 949 | } |
Fariborz Jahanian | 2fab94e | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 950 | if (OMD->isVariadic()) |
| 951 | ResultStr += ", ..."; |
Fariborz Jahanian | 7202d98 | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 952 | ResultStr += ") "; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 953 | |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 954 | if (FPRetType) { |
| 955 | ResultStr += ")"; // close the precedence "scope" for "*". |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 956 | |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 957 | // Now, emit the argument types (if any). |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 958 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 959 | ResultStr += "("; |
| 960 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 961 | if (i) ResultStr += ", "; |
| 962 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 963 | ResultStr += ParamStr; |
| 964 | } |
| 965 | if (FT->isVariadic()) { |
| 966 | if (FT->getNumArgs()) ResultStr += ", "; |
| 967 | ResultStr += "..."; |
| 968 | } |
| 969 | ResultStr += ")"; |
| 970 | } else { |
| 971 | ResultStr += "()"; |
| 972 | } |
| 973 | } |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 974 | } |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 975 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 976 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 977 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 978 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 979 | if (IMD) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 980 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 981 | else |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 982 | InsertText(CID->getLocStart(), "// ", 3); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 983 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 984 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 985 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 986 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 987 | I != E; ++I) { |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 988 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 989 | ObjCMethodDecl *OMD = *I; |
| 990 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 991 | SourceLocation LocStart = OMD->getLocStart(); |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 992 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 993 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 994 | const char *startBuf = SM->getCharacterData(LocStart); |
| 995 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 996 | ReplaceText(LocStart, endBuf-startBuf, |
| 997 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 998 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 999 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1000 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1001 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1002 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1003 | I != E; ++I) { |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1004 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1005 | ObjCMethodDecl *OMD = *I; |
| 1006 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1007 | SourceLocation LocStart = OMD->getLocStart(); |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1008 | SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1009 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1010 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1011 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1012 | ReplaceText(LocStart, endBuf-startBuf, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1013 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1014 | } |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1015 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1016 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1017 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1018 | I != E; ++I) { |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1019 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1022 | if (IMD) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1023 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1024 | else |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1025 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1028 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1029 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1030 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 77d081b | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1031 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 2adead7 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1032 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1033 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1034 | ResultStr += "\n"; |
| 1035 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1036 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1037 | ResultStr += "\n"; |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1038 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1039 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1040 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 77d081b | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1041 | // Mark this typedef as having been generated. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1042 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 77d081b | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1043 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1044 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1045 | |
| 1046 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1047 | E = ClassDecl->prop_end(); I != E; ++I) |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1048 | RewriteProperty(*I); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1049 | for (ObjCInterfaceDecl::instmeth_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1050 | I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1051 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1052 | RewriteMethodDeclaration(*I); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1053 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1054 | I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1055 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1056 | RewriteMethodDeclaration(*I); |
| 1057 | |
Steve Naroff | 1ccf463 | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1058 | // Lastly, comment out the @end. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1059 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1062 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1063 | SourceRange SrcRange) { |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1064 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1065 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1066 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1067 | ObjCMessageExpr *MsgExpr; |
| 1068 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1069 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1070 | ExprVec.push_back(newStmt); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1071 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1072 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1073 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1074 | if (PRE && PropGetters[PRE]) { |
| 1075 | // This allows us to handle chain/nested property getters. |
| 1076 | Receiver = PropGetters[PRE]; |
| 1077 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1078 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1079 | PDecl->getSetterName(), PDecl->getType(), |
| 1080 | PDecl->getSetterMethodDecl(), |
| 1081 | SourceLocation(), SourceLocation(), |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1082 | &ExprVec[0], 1); |
| 1083 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1084 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1085 | // Now do the actual rewrite. |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1086 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | 478f738 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1087 | //delete BinOp; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1088 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1089 | // to things that stay around. |
| 1090 | Context->Deallocate(MsgExpr); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1091 | return ReplacingStmt; |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1094 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1095 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1096 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1097 | ObjCMessageExpr *MsgExpr; |
| 1098 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1099 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1100 | Stmt *Receiver = PropRefExpr->getBase(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1101 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1102 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1103 | if (PRE && PropGetters[PRE]) { |
| 1104 | // This allows us to handle chain/nested property getters. |
| 1105 | Receiver = PropGetters[PRE]; |
| 1106 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1107 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
| 1108 | PDecl->getGetterName(), PDecl->getType(), |
| 1109 | PDecl->getGetterMethodDecl(), |
| 1110 | SourceLocation(), SourceLocation(), |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1111 | 0, 0); |
| 1112 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1113 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (!PropParentMap) |
| 1116 | PropParentMap = new ParentMap(CurrentBody); |
| 1117 | |
| 1118 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1119 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1120 | // We stash away the ReplacingStmt since actually doing the |
| 1121 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1122 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1123 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1124 | // to things that stay around. |
| 1125 | Context->Deallocate(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1126 | return PropRefExpr; // return the original... |
| 1127 | } else { |
| 1128 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1129 | // delete PropRefExpr; elsewhere... |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1130 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1131 | // to things that stay around. |
| 1132 | Context->Deallocate(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1133 | return ReplacingStmt; |
| 1134 | } |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1137 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1138 | SourceLocation OrigStart) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1139 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1140 | if (CurMethodDef) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1141 | if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1142 | ObjCInterfaceType *iFaceDecl = |
| 1143 | dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1144 | // lookup which class implements the instance variable. |
| 1145 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1146 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1147 | clsDeclared); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1148 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1149 | |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1150 | // Synthesize an explicit cast to gain access to the ivar. |
| 1151 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1152 | RecName += "_IMPL"; |
| 1153 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1154 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1155 | SourceLocation(), II); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1156 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1157 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1158 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1159 | CastExpr::CK_Unknown, |
| 1160 | IV->getBase(), |
| 1161 | castT,SourceLocation(), |
| 1162 | SourceLocation()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1163 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1164 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1165 | IV->getBase()->getLocEnd(), |
| 1166 | castExpr); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1167 | if (IV->isFreeIvar() && |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1168 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1169 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1170 | IV->getLocation(), |
| 1171 | D->getType()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1172 | ReplaceStmt(IV, ME); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1173 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1174 | return ME; |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1175 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1176 | |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1177 | ReplaceStmt(IV->getBase(), PE); |
| 1178 | // Cannot delete IV->getBase(), since PE points to it. |
| 1179 | // Replace the old base with the cast. This is important when doing |
| 1180 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1181 | IV->setBase(PE); |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1182 | return IV; |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1183 | } |
Steve Naroff | e615536 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1184 | } else { // we are outside a method. |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1185 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1186 | |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1187 | // Explicit ivar refs need to have a cast inserted. |
| 1188 | // FIXME: consider sharing some of this code with the code above. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1189 | if (const PointerType *pType = IV->getBase()->getType()->getAs<PointerType>()) { |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1190 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1191 | // lookup which class implements the instance variable. |
| 1192 | ObjCInterfaceDecl *clsDeclared = 0; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1193 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1194 | clsDeclared); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1195 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1196 | |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1197 | // Synthesize an explicit cast to gain access to the ivar. |
| 1198 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1199 | RecName += "_IMPL"; |
| 1200 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1201 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1202 | SourceLocation(), II); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1203 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1204 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1205 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1206 | CastExpr::CK_Unknown, |
| 1207 | IV->getBase(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1208 | castT, SourceLocation(), |
| 1209 | SourceLocation()); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1210 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1211 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 3cca661 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1212 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1213 | ReplaceStmt(IV->getBase(), PE); |
| 1214 | // Cannot delete IV->getBase(), since PE points to it. |
| 1215 | // Replace the old base with the cast. This is important when doing |
| 1216 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1217 | IV->setBase(PE); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1218 | return IV; |
| 1219 | } |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1220 | } |
Steve Naroff | e615536 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1221 | return IV; |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1224 | /// SynthCountByEnumWithState - To print: |
| 1225 | /// ((unsigned int (*) |
| 1226 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1227 | /// (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1228 | /// sel_registerName( |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1229 | /// "countByEnumeratingWithState:objects:count:"), |
| 1230 | /// &enumState, |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1231 | /// (id *)items, (unsigned int)16) |
| 1232 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1233 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1234 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1235 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1236 | buf += "\n\t\t"; |
| 1237 | buf += "((id)l_collection,\n\t\t"; |
| 1238 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1239 | buf += "\n\t\t"; |
| 1240 | buf += "&enumState, " |
| 1241 | "(id *)items, (unsigned int)16)"; |
| 1242 | } |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1243 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1244 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1245 | /// statement to exit to its outer synthesized loop. |
| 1246 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1247 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1248 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1249 | return S; |
| 1250 | // replace break with goto __break_label |
| 1251 | std::string buf; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1252 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1253 | SourceLocation startLoc = S->getLocStart(); |
| 1254 | buf = "goto __break_label_"; |
| 1255 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1256 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1262 | /// statement to continue with its inner synthesized loop. |
| 1263 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1264 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1265 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1266 | return S; |
| 1267 | // replace continue with goto __continue_label |
| 1268 | std::string buf; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1269 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1270 | SourceLocation startLoc = S->getLocStart(); |
| 1271 | buf = "goto __continue_label_"; |
| 1272 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1273 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1274 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
| 1277 | |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1278 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1279 | /// It rewrites: |
| 1280 | /// for ( type elem in collection) { stmts; } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1281 | |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1282 | /// Into: |
| 1283 | /// { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1284 | /// type elem; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1285 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1286 | /// id items[16]; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1287 | /// id l_collection = (id)collection; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1288 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1289 | /// objects:items count:16]; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1290 | /// if (limit) { |
| 1291 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1292 | /// do { |
| 1293 | /// unsigned long counter = 0; |
| 1294 | /// do { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1295 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1296 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1297 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1298 | /// stmts; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1299 | /// __continue_label: ; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1300 | /// } while (counter < limit); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1301 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1302 | /// objects:items count:16]); |
| 1303 | /// elem = nil; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1304 | /// __break_label: ; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1305 | /// } |
| 1306 | /// else |
| 1307 | /// elem = nil; |
| 1308 | /// } |
| 1309 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1310 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 2c02216 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1311 | SourceLocation OrigEnd) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1312 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1313 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1314 | "ObjCForCollectionStmt Statement stack mismatch"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1315 | assert(!ObjCBcLabelNo.empty() && |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1316 | "ObjCForCollectionStmt - Label No stack empty"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1317 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1318 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1319 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1320 | const char *elementName; |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1321 | std::string elementTypeAsString; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1322 | std::string buf; |
| 1323 | buf = "\n{\n\t"; |
| 1324 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1325 | // type elem; |
Chris Lattner | 4a9a85e | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1326 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 91a2bd3 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1327 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1328 | elementTypeAsString = ElementType.getAsString(); |
| 1329 | buf += elementTypeAsString; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1330 | buf += " "; |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1331 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1332 | buf += elementName; |
| 1333 | buf += ";\n\t"; |
| 1334 | } |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1335 | else { |
| 1336 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1337 | elementName = DR->getDecl()->getNameAsCString(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1338 | elementTypeAsString |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1339 | = cast<ValueDecl>(DR->getDecl())->getType().getAsString(); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1340 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1341 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1342 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1343 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1344 | // id items[16]; |
| 1345 | buf += "id items[16];\n\t"; |
| 1346 | // id l_collection = (id) |
| 1347 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1348 | // Find start location of 'collection' the hard way! |
| 1349 | const char *startCollectionBuf = startBuf; |
| 1350 | startCollectionBuf += 3; // skip 'for' |
| 1351 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1352 | startCollectionBuf++; // skip '(' |
| 1353 | // find 'in' and skip it. |
| 1354 | while (*startCollectionBuf != ' ' || |
| 1355 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1356 | (*(startCollectionBuf+3) != ' ' && |
| 1357 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1358 | startCollectionBuf++; |
| 1359 | startCollectionBuf += 3; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1360 | |
| 1361 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1362 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1363 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1364 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1365 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1366 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1367 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1368 | buf = ";\n\t"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1369 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1370 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1371 | // objects:items count:16]; |
| 1372 | // which is synthesized into: |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1373 | // unsigned int limit = |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1374 | // ((unsigned int (*) |
| 1375 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1376 | // (void *)objc_msgSend)((id)l_collection, |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1377 | // sel_registerName( |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1378 | // "countByEnumeratingWithState:objects:count:"), |
| 1379 | // (struct __objcFastEnumerationState *)&state, |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1380 | // (id *)items, (unsigned int)16); |
| 1381 | buf += "unsigned long limit =\n\t\t"; |
| 1382 | SynthCountByEnumWithState(buf); |
| 1383 | buf += ";\n\t"; |
| 1384 | /// if (limit) { |
| 1385 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1386 | /// do { |
| 1387 | /// unsigned long counter = 0; |
| 1388 | /// do { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1389 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1390 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1391 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1392 | buf += "if (limit) {\n\t"; |
| 1393 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1394 | buf += "do {\n\t\t"; |
| 1395 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1396 | buf += "do {\n\t\t\t"; |
| 1397 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1398 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1399 | buf += elementName; |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1400 | buf += " = ("; |
| 1401 | buf += elementTypeAsString; |
| 1402 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1403 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1404 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1405 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1406 | /// __continue_label: ; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1407 | /// } while (counter < limit); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1408 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1409 | /// objects:items count:16]); |
| 1410 | /// elem = nil; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1411 | /// __break_label: ; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1412 | /// } |
| 1413 | /// else |
| 1414 | /// elem = nil; |
| 1415 | /// } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1416 | /// |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1417 | buf = ";\n\t"; |
| 1418 | buf += "__continue_label_"; |
| 1419 | buf += utostr(ObjCBcLabelNo.back()); |
| 1420 | buf += ": ;"; |
| 1421 | buf += "\n\t\t"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1422 | buf += "} while (counter < limit);\n\t"; |
| 1423 | buf += "} while (limit = "; |
| 1424 | SynthCountByEnumWithState(buf); |
| 1425 | buf += ");\n\t"; |
| 1426 | buf += elementName; |
Steve Naroff | a9dc026 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1427 | buf += " = ((id)0);\n\t"; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1428 | buf += "__break_label_"; |
| 1429 | buf += utostr(ObjCBcLabelNo.back()); |
| 1430 | buf += ": ;\n\t"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1431 | buf += "}\n\t"; |
| 1432 | buf += "else\n\t\t"; |
| 1433 | buf += elementName; |
Steve Naroff | a9dc026 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1434 | buf += " = ((id)0);\n"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1435 | buf += "}\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1436 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1437 | // Insert all these *after* the statement body. |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 1438 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 5f238fe | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1439 | if (isa<CompoundStmt>(S->getBody())) { |
| 1440 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1441 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1442 | } else { |
| 1443 | /* Need to treat single statements specially. For example: |
| 1444 | * |
| 1445 | * for (A *a in b) if (stuff()) break; |
| 1446 | * for (A *a in b) xxxyy; |
| 1447 | * |
| 1448 | * The following code simply scans ahead to the semi to find the actual end. |
| 1449 | */ |
| 1450 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1451 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1452 | assert(semiBuf && "Can't find ';'"); |
| 1453 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1454 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1455 | } |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1456 | Stmts.pop_back(); |
| 1457 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1458 | return 0; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1461 | /// RewriteObjCSynchronizedStmt - |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1462 | /// This routine rewrites @synchronized(expr) stmt; |
| 1463 | /// into: |
| 1464 | /// objc_sync_enter(expr); |
| 1465 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1466 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1467 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1468 | // Get the start location and compute the semi location. |
| 1469 | SourceLocation startLoc = S->getLocStart(); |
| 1470 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1471 | |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1472 | assert((*startBuf == '@') && "bogus @synchronized location"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1473 | |
| 1474 | std::string buf; |
Steve Naroff | c1656a6 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1475 | buf = "objc_sync_enter((id)"; |
| 1476 | const char *lparenBuf = startBuf; |
| 1477 | while (*lparenBuf != '(') lparenBuf++; |
| 1478 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1479 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1480 | // the sync expression is typically a message expression that's already |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1481 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1482 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1483 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1484 | while (*endBuf != ')') endBuf--; |
| 1485 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1486 | buf = ");\n"; |
| 1487 | // declare a new scope with two variables, _stack and _rethrow. |
| 1488 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1489 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1490 | buf += "char *pointers[4];} _stack;\n"; |
| 1491 | buf += "id volatile _rethrow = 0;\n"; |
| 1492 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1493 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1494 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1495 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1496 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1497 | |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1498 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1499 | SourceLocation lastCurlyLoc = startLoc; |
| 1500 | buf = "}\nelse {\n"; |
| 1501 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 1502 | buf += "}\n"; |
| 1503 | buf += "{ /* implicit finally clause */\n"; |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1504 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1505 | buf += " objc_sync_exit("; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1506 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1507 | CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1508 | S->getSynchExpr(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1509 | Context->getObjCIdType(), |
| 1510 | SourceLocation(), |
| 1511 | SourceLocation()); |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1512 | std::string syncExprBufS; |
| 1513 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Chris Lattner | 7099c78 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1514 | syncExpr->printPretty(syncExprBuf, *Context, 0, |
| 1515 | PrintingPolicy(LangOpts)); |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1516 | buf += syncExprBuf.str(); |
| 1517 | buf += ");\n"; |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1518 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1519 | buf += "}\n"; |
| 1520 | buf += "}"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1521 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1522 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1523 | return 0; |
| 1524 | } |
| 1525 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1526 | void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) { |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1527 | // Perform a bottom up traversal of all children. |
| 1528 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1529 | CI != E; ++CI) |
| 1530 | if (*CI) |
| 1531 | WarnAboutReturnGotoContinueOrBreakStmts(*CI); |
| 1532 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1533 | if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1534 | isa<BreakStmt>(S) || isa<GotoStmt>(S)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1535 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1536 | TryFinallyContainsReturnDiag); |
| 1537 | } |
| 1538 | return; |
| 1539 | } |
| 1540 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1541 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1542 | // Get the start location and compute the semi location. |
| 1543 | SourceLocation startLoc = S->getLocStart(); |
| 1544 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1545 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1546 | assert((*startBuf == '@') && "bogus @try location"); |
| 1547 | |
| 1548 | std::string buf; |
| 1549 | // declare a new scope with two variables, _stack and _rethrow. |
| 1550 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1551 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1552 | buf += "char *pointers[4];} _stack;\n"; |
| 1553 | buf += "id volatile _rethrow = 0;\n"; |
| 1554 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1555 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1557 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1558 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1559 | startLoc = S->getTryBody()->getLocEnd(); |
| 1560 | startBuf = SM->getCharacterData(startLoc); |
| 1561 | |
| 1562 | assert((*startBuf == '}') && "bogus @try block"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1563 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1564 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1565 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1566 | if (catchList) { |
| 1567 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1568 | buf = " /* @catch begin */ else {\n"; |
| 1569 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1570 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1571 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1572 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1573 | buf += " else { /* @catch continue */"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1574 | |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1575 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 36269d2 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1576 | } else { /* no catch list */ |
| 1577 | buf = "}\nelse {\n"; |
| 1578 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1579 | buf += "}"; |
| 1580 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1581 | } |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1582 | bool sawIdTypedCatch = false; |
| 1583 | Stmt *lastCatchBody = 0; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1584 | while (catchList) { |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1585 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1586 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1587 | if (catchList == S->getCatchStmts()) |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1588 | buf = "if ("; // we are generating code for the first catch clause |
| 1589 | else |
| 1590 | buf = "else if ("; |
| 1591 | startLoc = catchList->getLocStart(); |
| 1592 | startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1593 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1594 | assert((*startBuf == '@') && "bogus @catch location"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1595 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1596 | const char *lParenLoc = strchr(startBuf, '('); |
| 1597 | |
Steve Naroff | 397c4ce | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1598 | if (catchList->hasEllipsis()) { |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1599 | // Now rewrite the body... |
| 1600 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1601 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1602 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1603 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1604 | "bogus @catch paren location"); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1605 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1606 | |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1607 | buf += "1) { id _tmp = _caught;"; |
Daniel Dunbar | 60987c9 | 2009-08-19 19:10:30 +0000 | [diff] [blame] | 1608 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1609 | } else if (catchDecl) { |
| 1610 | QualType t = catchDecl->getType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1611 | if (t == Context->getObjCIdType()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1612 | buf += "1) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1613 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1614 | sawIdTypedCatch = true; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1615 | } else if (const PointerType *pType = t->getAs<PointerType>()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1616 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1617 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1618 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1619 | if (cls) { |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1620 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1621 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1622 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1623 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | // Now rewrite the body... |
| 1627 | lastCatchBody = catchList->getCatchBody(); |
| 1628 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1629 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1630 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1631 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1632 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1633 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1634 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1635 | buf = " = _caught;"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1636 | // Here we replace ") {" with "= _caught;" (which initializes and |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1637 | // declares the @catch parameter). |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1638 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1639 | } else { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1640 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1641 | } |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1642 | // make sure all the catch bodies get rewritten! |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1643 | catchList = catchList->getNextCatchStmt(); |
| 1644 | } |
| 1645 | // Complete the catch list... |
| 1646 | if (lastCatchBody) { |
| 1647 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1648 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1649 | "bogus @catch body location"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1650 | |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1651 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1652 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1653 | buf = "} /* last catch end */\n"; |
| 1654 | buf += "else {\n"; |
| 1655 | buf += " _rethrow = _caught;\n"; |
| 1656 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1657 | buf += "} } /* @catch end */\n"; |
| 1658 | if (!S->getFinallyStmt()) |
| 1659 | buf += "}\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1660 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1661 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1662 | // Set lastCurlyLoc |
| 1663 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1664 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1665 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1666 | startLoc = finalStmt->getLocStart(); |
| 1667 | startBuf = SM->getCharacterData(startLoc); |
| 1668 | assert((*startBuf == '@') && "bogus @finally start"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1669 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1670 | buf = "/* @finally */"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1671 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1672 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1673 | Stmt *body = finalStmt->getFinallyBody(); |
| 1674 | SourceLocation startLoc = body->getLocStart(); |
| 1675 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1676 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1677 | "bogus @finally body location"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1678 | assert(*SM->getCharacterData(endLoc) == '}' && |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1679 | "bogus @finally body location"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1680 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1681 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1682 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1683 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1684 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1685 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1686 | InsertText(endLoc, buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1687 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1688 | // Set lastCurlyLoc |
| 1689 | lastCurlyLoc = body->getLocEnd(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1690 | |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1691 | // Now check for any return/continue/go statements within the @try. |
| 1692 | WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody()); |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1693 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1694 | buf = "{ /* implicit finally clause */\n"; |
| 1695 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1696 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1697 | buf += "}"; |
| 1698 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1699 | } |
| 1700 | // Now emit the final closing curly brace... |
| 1701 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1702 | buf = " } /* @try scope end */\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1703 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1704 | return 0; |
| 1705 | } |
| 1706 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1707 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1711 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
| 1714 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1715 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
| 1716 | // the throw expression is typically a message expression that's already |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1717 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1718 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1719 | // Get the start location and compute the semi location. |
| 1720 | SourceLocation startLoc = S->getLocStart(); |
| 1721 | const char *startBuf = SM->getCharacterData(startLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1722 | |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1723 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1724 | |
| 1725 | std::string buf; |
| 1726 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | be72efa | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1727 | if (S->getThrowExpr()) |
| 1728 | buf = "objc_exception_throw("; |
| 1729 | else // add an implicit argument |
| 1730 | buf = "objc_exception_throw(_caught"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1731 | |
Steve Naroff | 3de6eaa | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1732 | // handle "@ throw" correctly. |
| 1733 | const char *wBuf = strchr(startBuf, 'w'); |
| 1734 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1735 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1736 | |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1737 | const char *semiBuf = strchr(startBuf, ';'); |
| 1738 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1739 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1740 | buf = ");"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1741 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1744 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1745 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | bf0bfa6 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1746 | // Create a new string expression. |
| 1747 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1748 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1749 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1750 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1751 | StrEncoding.length(), false,StrType, |
| 1752 | SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1753 | ReplaceStmt(Exp, Replacement); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1754 | |
Chris Lattner | 4478db9 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1755 | // Replace this subexpr in the parent. |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1756 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1757 | return Replacement; |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1760 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 95f6bce | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1761 | if (!SelGetUidFunctionDecl) |
| 1762 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1763 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1764 | // Create a call to sel_registerName("selName"). |
| 1765 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1766 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1767 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1768 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1769 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1770 | false, argType, SourceLocation())); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1771 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1772 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1773 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1774 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1775 | return SelExp; |
| 1776 | } |
| 1777 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1778 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1779 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1780 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1781 | QualType msgSendType = FD->getType(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1782 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1783 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1784 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1785 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1786 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 1787 | QualType pToFunc = Context->getPointerType(msgSendType); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1788 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 1789 | CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1790 | DRE, |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1791 | /*isLvalue=*/false); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1792 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1793 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1794 | |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1795 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1796 | SourceLocation()); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1799 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1800 | const char *&startRef, const char *&endRef) { |
| 1801 | while (startBuf < endBuf) { |
| 1802 | if (*startBuf == '<') |
| 1803 | startRef = startBuf; // mark the start. |
| 1804 | if (*startBuf == '>') { |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1805 | if (startRef && *startRef == '<') { |
| 1806 | endRef = startBuf; // mark the end. |
| 1807 | return true; |
| 1808 | } |
| 1809 | return false; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1810 | } |
| 1811 | startBuf++; |
| 1812 | } |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1816 | static void scanToNextArgument(const char *&argRef) { |
| 1817 | int angle = 0; |
| 1818 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1819 | if (*argRef == '<') |
| 1820 | angle++; |
| 1821 | else if (*argRef == '>') |
| 1822 | angle--; |
| 1823 | argRef++; |
| 1824 | } |
| 1825 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1826 | } |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1827 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1828 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Steve Naroff | 77763c5 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1829 | return T->isObjCQualifiedIdType() || T->isObjCQualifiedInterfaceType(); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1832 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1833 | QualType Type = E->getType(); |
| 1834 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1835 | SourceLocation Loc, EndLoc; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1836 | |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1837 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1838 | Loc = ECE->getLParenLoc(); |
| 1839 | EndLoc = ECE->getRParenLoc(); |
| 1840 | } else { |
| 1841 | Loc = E->getLocStart(); |
| 1842 | EndLoc = E->getLocEnd(); |
| 1843 | } |
| 1844 | // This will defend against trying to rewrite synthesized expressions. |
| 1845 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1846 | return; |
| 1847 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1848 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1849 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1850 | const char *startRef = 0, *endRef = 0; |
| 1851 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1852 | // Get the locations of the startRef, endRef. |
| 1853 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1854 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1855 | // Comment out the protocol references. |
| 1856 | InsertText(LessLoc, "/*", 2); |
| 1857 | InsertText(GreaterLoc, "*/", 2); |
| 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1862 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1863 | SourceLocation Loc; |
| 1864 | QualType Type; |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1865 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1866 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1867 | Loc = VD->getLocation(); |
| 1868 | Type = VD->getType(); |
| 1869 | } |
| 1870 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1871 | Loc = FD->getLocation(); |
| 1872 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1873 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1874 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1875 | assert(funcType && "missing function type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1876 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1877 | if (!proto) |
| 1878 | return; |
| 1879 | Type = proto->getResultType(); |
| 1880 | } |
| 1881 | else |
| 1882 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1883 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1884 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1885 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1886 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1887 | const char *endBuf = SM->getCharacterData(Loc); |
| 1888 | const char *startBuf = endBuf; |
Steve Naroff | ff2fa19 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1889 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1890 | startBuf--; // scan backward (from the decl location) for return type. |
| 1891 | const char *startRef = 0, *endRef = 0; |
| 1892 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1893 | // Get the locations of the startRef, endRef. |
| 1894 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1895 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1896 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1897 | InsertText(LessLoc, "/*", 2); |
| 1898 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1899 | } |
| 1900 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1901 | if (!proto) |
| 1902 | return; // most likely, was a variable |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1903 | // Now check arguments. |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1904 | const char *startBuf = SM->getCharacterData(Loc); |
| 1905 | const char *startFuncBuf = startBuf; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1906 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1907 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1908 | // Since types are unique, we need to scan the buffer. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1909 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1910 | const char *endBuf = startBuf; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1911 | // scan forward (from the decl location) for argument types. |
| 1912 | scanToNextArgument(endBuf); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1913 | const char *startRef = 0, *endRef = 0; |
| 1914 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1915 | // Get the locations of the startRef, endRef. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1916 | SourceLocation LessLoc = |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1917 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1918 | SourceLocation GreaterLoc = |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1919 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1920 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1921 | InsertText(LessLoc, "/*", 2); |
| 1922 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1923 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1924 | startBuf = ++endBuf; |
| 1925 | } |
| 1926 | else { |
Steve Naroff | 6a1d88b | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1927 | // If the function name is derived from a macro expansion, then the |
| 1928 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1929 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1930 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1931 | startBuf++; |
| 1932 | } |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1933 | } |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1936 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1937 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1938 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1939 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1940 | ArgTys.push_back(Context->getPointerType( |
| 1941 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1942 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1943 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1944 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1945 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1946 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1947 | SelGetUidIdent, getFuncType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1948 | FunctionDecl::Extern, false); |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1951 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1952 | // declared in <objc/objc.h> |
Douglas Gregor | 7339c9e | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 1953 | if (FD->getIdentifier() && |
| 1954 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1955 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1956 | return; |
| 1957 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1958 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1961 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1962 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1963 | if (SuperContructorFunctionDecl) |
| 1964 | return; |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 1965 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1966 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1967 | QualType argT = Context->getObjCIdType(); |
| 1968 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1969 | ArgTys.push_back(argT); |
| 1970 | ArgTys.push_back(argT); |
| 1971 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1972 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1973 | false, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1974 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 1975 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1976 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1977 | FunctionDecl::Extern, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1980 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1981 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1982 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1983 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1984 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1985 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1986 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1987 | argT = Context->getObjCSelType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1988 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1989 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1990 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1991 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1992 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1993 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1994 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1995 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1996 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1999 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2000 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2001 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2002 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2003 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2004 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2005 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2006 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2007 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2008 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2009 | argT = Context->getObjCSelType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2010 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2011 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2012 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2013 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2014 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2015 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2016 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2017 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2018 | FunctionDecl::Extern, false); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2021 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2022 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2023 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2024 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2025 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2026 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2027 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2028 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2029 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2030 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2031 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2032 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2033 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2034 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2035 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2036 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2037 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2040 | // SynthMsgSendSuperStretFunctionDecl - |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2041 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2042 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2043 | IdentifierInfo *msgSendIdent = |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2044 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2045 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2046 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2047 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2048 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2049 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2050 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2051 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2052 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2053 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2054 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2055 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2056 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2057 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2058 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2059 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2060 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2061 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2064 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2065 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2066 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2067 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2068 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2069 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2070 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2071 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2072 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2073 | ArgTys.push_back(argT); |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2074 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2075 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2076 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2077 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2078 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2079 | msgSendIdent, msgSendType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2080 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2083 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2084 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2085 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2086 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2087 | ArgTys.push_back(Context->getPointerType( |
| 2088 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2089 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2090 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2091 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2092 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2093 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2094 | getClassIdent, getClassType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2095 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2098 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2099 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2100 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2101 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2102 | ArgTys.push_back(Context->getPointerType( |
| 2103 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2104 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2105 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2106 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2107 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2108 | SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2109 | getClassIdent, getClassType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2110 | FunctionDecl::Extern, false); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2113 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2114 | QualType strType = getConstantStringStructType(); |
| 2115 | |
| 2116 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a1f0099 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2117 | |
| 2118 | std::string tmpName = InFileName; |
| 2119 | unsigned i; |
| 2120 | for (i=0; i < tmpName.length(); i++) { |
| 2121 | char c = tmpName.at(i); |
| 2122 | // replace any non alphanumeric characters with '_'. |
| 2123 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2124 | tmpName[i] = '_'; |
| 2125 | } |
| 2126 | S += tmpName; |
| 2127 | S += "_"; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2128 | S += utostr(NumObjCStringLiterals++); |
| 2129 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2130 | Preamble += "static __NSConstantStringImpl " + S; |
| 2131 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2132 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2133 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2134 | std::string prettyBufS; |
| 2135 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Chris Lattner | 7099c78 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2136 | Exp->getString()->printPretty(prettyBuf, *Context, 0, |
| 2137 | PrintingPolicy(LangOpts)); |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2138 | Preamble += prettyBuf.str(); |
| 2139 | Preamble += ","; |
Steve Naroff | 64bce35 | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2140 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2141 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2142 | |
| 2143 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
| 2144 | &Context->Idents.get(S.c_str()), strType, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2145 | VarDecl::Static); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2146 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2147 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2148 | Context->getPointerType(DRE->getType()), |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2149 | SourceLocation()); |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2150 | // cast to NSConstantString * |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2151 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2152 | CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2153 | Unop, Exp->getType(), |
| 2154 | SourceLocation(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2155 | SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2156 | ReplaceStmt(Exp, cast); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2157 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2158 | return cast; |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2161 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2162 | // check if we are sending a message to 'super' |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2163 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2164 | |
Douglas Gregor | d860663 | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2165 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2166 | const ObjCObjectPointerType *OPT = |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2167 | Super->getType()->getAsObjCObjectPointerType(); |
| 2168 | assert(OPT); |
| 2169 | const ObjCInterfaceType *IT = OPT->getInterfaceType(); |
Chris Lattner | 4423d37 | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2170 | return IT->getDecl(); |
| 2171 | } |
| 2172 | return 0; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2176 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2177 | if (!SuperStructDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2178 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2179 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2180 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2181 | QualType FieldTypes[2]; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2182 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2183 | // struct objc_object *receiver; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2184 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2185 | // struct objc_class *super; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2186 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2187 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2188 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2189 | for (unsigned i = 0; i < 2; ++i) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2190 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
| 2191 | SourceLocation(), 0, |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2192 | FieldTypes[i], 0, |
| 2193 | /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2194 | /*Mutable=*/false)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2195 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2196 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2197 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2198 | } |
| 2199 | return Context->getTagDeclType(SuperStructDecl); |
| 2200 | } |
| 2201 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2202 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2203 | if (!ConstantStringDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2204 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2205 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2206 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2207 | QualType FieldTypes[4]; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2208 | |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2209 | // struct objc_object *receiver; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2210 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2211 | // int flags; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2212 | FieldTypes[1] = Context->IntTy; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2213 | // char *str; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2214 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2215 | // long length; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2216 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2217 | |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2218 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2219 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2220 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
| 2221 | ConstantStringDecl, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2222 | SourceLocation(), 0, |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2223 | FieldTypes[i], 0, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2224 | /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2225 | /*Mutable=*/true)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2229 | } |
| 2230 | return Context->getTagDeclType(ConstantStringDecl); |
| 2231 | } |
| 2232 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2233 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2234 | if (!SelGetUidFunctionDecl) |
| 2235 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2236 | if (!MsgSendFunctionDecl) |
| 2237 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2238 | if (!MsgSendSuperFunctionDecl) |
| 2239 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2240 | if (!MsgSendStretFunctionDecl) |
| 2241 | SynthMsgSendStretFunctionDecl(); |
| 2242 | if (!MsgSendSuperStretFunctionDecl) |
| 2243 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2244 | if (!MsgSendFpretFunctionDecl) |
| 2245 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2246 | if (!GetClassFunctionDecl) |
| 2247 | SynthGetClassFunctionDecl(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2248 | if (!GetMetaClassFunctionDecl) |
| 2249 | SynthGetMetaClassFunctionDecl(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2250 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2251 | // default to objc_msgSend(). |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2252 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2253 | // May need to use objc_msgSend_stret() as well. |
| 2254 | FunctionDecl *MsgSendStretFlavor = 0; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2255 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 2256 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2257 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2258 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2259 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2260 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2261 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2262 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2263 | // Synthesize a call to objc_msgSend(). |
| 2264 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2265 | IdentifierInfo *clsName = Exp->getClassName(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2266 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2267 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2268 | if (clsName) { // class message. |
Steve Naroff | 91a6920 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2269 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2270 | // the 'super' idiom within a class method. |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2271 | if (!strcmp(clsName->getName(), "super")) { |
| 2272 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2273 | if (MsgSendStretFlavor) |
| 2274 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2275 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2276 | |
| 2277 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2278 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2279 | |
| 2280 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2281 | |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2282 | // set the receiver to self, the first argument to all methods. |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2283 | InitExprs.push_back( |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2284 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2285 | CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2286 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2287 | Context->getObjCIdType(), |
| 2288 | SourceLocation()), |
| 2289 | Context->getObjCIdType(), |
| 2290 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
| 2291 | |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2292 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2293 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2294 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2295 | SuperDecl->getIdentifier()->getName(), |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2296 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2297 | false, argType, SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2298 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2299 | &ClsExprs[0], |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2300 | ClsExprs.size()); |
| 2301 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2302 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2303 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2304 | CastExpr::CK_Unknown, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2305 | Cls, Context->getObjCIdType(), |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2306 | SourceLocation(), SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2307 | // struct objc_super |
| 2308 | QualType superType = getSuperStructType(); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2309 | Expr *SuperRep; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2310 | |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2311 | if (LangOpts.Microsoft) { |
| 2312 | SynthSuperContructorFunctionDecl(); |
| 2313 | // Simulate a contructor call... |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2314 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2315 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2316 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2317 | InitExprs.size(), |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2318 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2319 | // The code for super is a little tricky to prevent collision with |
| 2320 | // the structure definition in the header. The rewriter has it's own |
| 2321 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2322 | // we need the cast below. For example: |
| 2323 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2324 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2325 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2326 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2327 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2328 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
| 2329 | CastExpr::CK_Unknown, SuperRep, |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2330 | Context->getPointerType(superType), |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2331 | SourceLocation(), SourceLocation()); |
| 2332 | } else { |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2333 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2334 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2335 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2336 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2337 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2338 | false); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2339 | // struct objc_super * |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2340 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2341 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2342 | SourceLocation()); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2343 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2344 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2345 | } else { |
| 2346 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2347 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2348 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2349 | clsName->getName(), |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2350 | clsName->getLength(), |
| 2351 | false, argType, |
| 2352 | SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2353 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2354 | &ClsExprs[0], |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2355 | ClsExprs.size()); |
| 2356 | MsgExprs.push_back(Cls); |
| 2357 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2358 | } else { // instance message. |
| 2359 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2360 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2361 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2362 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2363 | if (MsgSendStretFlavor) |
| 2364 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2365 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2366 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2367 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2368 | |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2369 | InitExprs.push_back( |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2370 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2371 | CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2372 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 2352861 | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2373 | Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2374 | SourceLocation()), |
| 2375 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2376 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2377 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2378 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2379 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2380 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2381 | SuperDecl->getIdentifier()->getName(), |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2382 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2383 | false, argType, SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2384 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2385 | &ClsExprs[0], |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2386 | ClsExprs.size()); |
Fariborz Jahanian | fabf3bf | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2387 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2388 | InitExprs.push_back( |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2389 | // set 'super class', using objc_getClass(). |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2390 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2391 | CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2392 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2393 | // struct objc_super |
| 2394 | QualType superType = getSuperStructType(); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2395 | Expr *SuperRep; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2396 | |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2397 | if (LangOpts.Microsoft) { |
| 2398 | SynthSuperContructorFunctionDecl(); |
| 2399 | // Simulate a contructor call... |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2400 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2401 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2402 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2403 | InitExprs.size(), |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2404 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2405 | // The code for super is a little tricky to prevent collision with |
| 2406 | // the structure definition in the header. The rewriter has it's own |
| 2407 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2408 | // we need the cast below. For example: |
| 2409 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2410 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2411 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2412 | Context->getPointerType(SuperRep->getType()), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2413 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2414 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2415 | CastExpr::CK_Unknown, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2416 | SuperRep, Context->getPointerType(superType), |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2417 | SourceLocation(), SourceLocation()); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2418 | } else { |
| 2419 | // (struct objc_super) { <exprs from above> } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2420 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
| 2421 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2422 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2423 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2424 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2425 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2426 | } else { |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2427 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2428 | // Foo<Proto> *. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2429 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2430 | recExpr = CE->getSubExpr(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2431 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2432 | CastExpr::CK_Unknown, recExpr, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2433 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2434 | SourceLocation(), SourceLocation()); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2435 | MsgExprs.push_back(recExpr); |
| 2436 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2437 | } |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2438 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2439 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2440 | QualType argType = Context->getPointerType(Context->CharTy); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2441 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2442 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2443 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2444 | false, argType, SourceLocation())); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2445 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2446 | &SelExprs[0], SelExprs.size()); |
| 2447 | MsgExprs.push_back(SelExp); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2448 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2449 | // Now push any user supplied arguments. |
| 2450 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2451 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2452 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2453 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2454 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2455 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2456 | ? Context->getObjCIdType() |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2457 | : ICE->getType(); |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2458 | userExpr = new (Context) CStyleCastExpr(type, CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2459 | userExpr, type, SourceLocation(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2460 | SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2461 | } |
| 2462 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2463 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2464 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2465 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2466 | userExpr = CE->getSubExpr(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2467 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2468 | CastExpr::CK_Unknown, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2469 | userExpr, Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2470 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2471 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2472 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2473 | MsgExprs.push_back(userExpr); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2474 | // We've transferred the ownership to MsgExprs. For now, we *don't* null |
| 2475 | // out the argument in the original expression (since we aren't deleting |
| 2476 | // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. |
| 2477 | //Exp->setArg(i, 0); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2478 | } |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2479 | // Generate the funky cast. |
| 2480 | CastExpr *cast; |
| 2481 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2482 | QualType returnType; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2483 | |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2484 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 0c04bb6 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2485 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2486 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2487 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2488 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2489 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2490 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2491 | // Push any user argument types. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2492 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2493 | E = OMD->param_end(); PI != E; ++PI) { |
| 2494 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2495 | ? Context->getObjCIdType() |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2496 | : (*PI)->getType(); |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2497 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2498 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2499 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2500 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2501 | } |
Steve Naroff | 4242b97 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2502 | ArgTypes.push_back(t); |
| 2503 | } |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2504 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2505 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2506 | } else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2507 | returnType = Context->getObjCIdType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2508 | } |
| 2509 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2510 | QualType msgSendType = MsgSendFlavor->getType(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2511 | |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2512 | // Create a reference to the objc_msgSend() declaration. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2513 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2514 | SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2515 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2516 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2517 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2518 | // xx.m:13: warning: function called through a non-compatible type |
| 2519 | // xx.m:13: note: if this code is reached, the program will abort |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2520 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2521 | CastExpr::CK_Unknown, DRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2522 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2523 | SourceLocation(), SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2524 | |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2525 | // Now do the "normal" pointer to function cast. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2526 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2527 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 3b8b4e3 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2528 | // If we don't have a method decl, force a variadic cast. |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2529 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2530 | castType = Context->getPointerType(castType); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2531 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, cast, |
| 2532 | castType, SourceLocation(), |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2533 | SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2534 | |
| 2535 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2536 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2537 | |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2538 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2539 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2540 | MsgExprs.size(), |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2541 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2542 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2543 | if (MsgSendStretFlavor) { |
| 2544 | // We have the method which returns a struct/union. Must also generate |
| 2545 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2546 | // expression which dictate which one to envoke depending on size of |
| 2547 | // method's return type. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2548 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2549 | // Create a reference to the objc_msgSend_stret() declaration. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2550 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2551 | SourceLocation()); |
| 2552 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2553 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), |
| 2554 | CastExpr::CK_Unknown, STDRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2555 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2556 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2557 | // Now do the "normal" pointer to function cast. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2558 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2559 | &ArgTypes[0], ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2560 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2561 | castType = Context->getPointerType(castType); |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2562 | cast = new (Context) CStyleCastExpr(castType, CastExpr::CK_Unknown, |
| 2563 | cast, castType, SourceLocation(), SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2564 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2565 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2566 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2567 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2568 | FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2569 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2570 | MsgExprs.size(), |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2571 | FT->getResultType(), SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2572 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2573 | // Build sizeof(returnType) |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2574 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2575 | returnType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2576 | Context->getSizeType(), |
| 2577 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2578 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2579 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2580 | // For X86 it is more complicated and some kind of target specific routine |
| 2581 | // is needed to decide what to do. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2582 | unsigned IntSize = |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2583 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2584 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2585 | Context->IntTy, |
| 2586 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2587 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
| 2588 | BinaryOperator::LE, |
| 2589 | Context->IntTy, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2590 | SourceLocation()); |
| 2591 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2592 | ConditionalOperator *CondExpr = |
Douglas Gregor | 3461987 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 2593 | new (Context) ConditionalOperator(lessThanExpr, |
| 2594 | SourceLocation(), CE, |
| 2595 | SourceLocation(), STCE, returnType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2596 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2597 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2598 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2599 | return ReplacingStmt; |
| 2600 | } |
| 2601 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2602 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2603 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2604 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2605 | // Now do the actual rewrite. |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2606 | ReplaceStmt(Exp, ReplacingStmt); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2607 | |
| 2608 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2609 | return ReplacingStmt; |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2612 | // typedef struct objc_object Protocol; |
| 2613 | QualType RewriteObjC::getProtocolType() { |
| 2614 | if (!ProtocolTypeDecl) { |
| 2615 | ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2616 | SourceLocation(), |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2617 | &Context->Idents.get("Protocol"), |
| 2618 | Context->getObjCIdType()); |
| 2619 | } |
| 2620 | return Context->getTypeDeclType(ProtocolTypeDecl); |
| 2621 | } |
| 2622 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2623 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2624 | /// a synthesized/forward data reference (to the protocol's metadata). |
| 2625 | /// The forward references (and metadata) are generated in |
| 2626 | /// RewriteObjC::HandleTranslationUnit(). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2627 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2628 | std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); |
| 2629 | IdentifierInfo *ID = &Context->Idents.get(Name); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2630 | VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2631 | ID, QualType()/*UNUSED*/, 0, VarDecl::Extern); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2632 | DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); |
| 2633 | Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2634 | Context->getPointerType(DRE->getType()), |
| 2635 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2636 | CastExpr *castExpr = new (Context) CStyleCastExpr(DerefExpr->getType(), |
| 2637 | CastExpr::CK_Unknown, |
| 2638 | DerefExpr, DerefExpr->getType(), |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2639 | SourceLocation(), SourceLocation()); |
| 2640 | ReplaceStmt(Exp, castExpr); |
| 2641 | ProtocolExprDecls.insert(Exp->getProtocol()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2642 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2643 | return castExpr; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2644 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2647 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2648 | const char *endBuf) { |
| 2649 | while (startBuf < endBuf) { |
| 2650 | if (*startBuf == '#') { |
| 2651 | // Skip whitespace. |
| 2652 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2653 | ; |
| 2654 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2655 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2656 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2657 | !strncmp(startBuf, "define", strlen("define")) || |
| 2658 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2659 | !strncmp(startBuf, "else", strlen("else")) || |
| 2660 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2661 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2662 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2663 | !strncmp(startBuf, "include", strlen("include")) || |
| 2664 | !strncmp(startBuf, "import", strlen("import")) || |
| 2665 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2666 | return true; |
| 2667 | } |
| 2668 | startBuf++; |
| 2669 | } |
| 2670 | return false; |
| 2671 | } |
| 2672 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2673 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2674 | /// an objective-c class with ivars. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2675 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2676 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2677 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2678 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2679 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2680 | // Do not synthesize more than once. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2681 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2682 | return; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2683 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2684 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2685 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2686 | SourceLocation LocEnd = CDecl->getLocEnd(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2687 | |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2688 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2689 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2690 | |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2691 | // If no ivars and no root or if its root, directly or indirectly, |
| 2692 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2693 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2694 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2695 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2696 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2697 | return; |
| 2698 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2699 | |
| 2700 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2701 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2702 | Result += "\nstruct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2703 | Result += CDecl->getNameAsString(); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2704 | if (LangOpts.Microsoft) |
| 2705 | Result += "_IMPL"; |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2706 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2707 | if (NumIvars > 0) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2708 | const char *cursor = strchr(startBuf, '{'); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2709 | assert((cursor && endBuf) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2710 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2711 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2712 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2713 | // NSURL.h, for example): |
| 2714 | // |
| 2715 | // #ifdef XYZ |
| 2716 | // @interface Foo : NSObject |
| 2717 | // #else |
| 2718 | // @interface FooBar : NSObject |
| 2719 | // #endif |
| 2720 | // { |
| 2721 | // int i; |
| 2722 | // } |
| 2723 | // @end |
| 2724 | // |
| 2725 | // This clause is segregated to avoid breaking the common case. |
| 2726 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2727 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2728 | CDecl->getClassLoc(); |
| 2729 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2730 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2731 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2732 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2733 | // advance to the end of the referenced protocols. |
| 2734 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2735 | endHeader++; |
| 2736 | } |
| 2737 | // rewrite the original header |
| 2738 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2739 | } else { |
| 2740 | // rewrite the original header *without* disturbing the '{' |
| 2741 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2742 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2743 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2744 | Result = "\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2745 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2746 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2747 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2748 | Result += "_IVARS;\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2749 | |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2750 | // insert the super class structure definition. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2751 | SourceLocation OnePastCurly = |
| 2752 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2753 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2754 | } |
| 2755 | cursor++; // past '{' |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2756 | |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2757 | // Now comment out any visibility specifiers. |
| 2758 | while (cursor < endBuf) { |
| 2759 | if (*cursor == '@') { |
| 2760 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f04ead5 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2761 | // Skip whitespace. |
| 2762 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2763 | /*scan*/; |
| 2764 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2765 | // FIXME: presence of @public, etc. inside comment results in |
| 2766 | // this transformation as well, which is still correct c-code. |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2767 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2768 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | a93924a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2769 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2770 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2771 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2772 | } |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2773 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2774 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2775 | // for type checking. |
| 2776 | else if (*cursor == '<') { |
| 2777 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2778 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2779 | cursor = strchr(cursor, '>'); |
| 2780 | cursor++; |
| 2781 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2782 | InsertText(atLoc, " */", 3); |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2783 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2784 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2785 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2786 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2787 | cursor++; |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2788 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2789 | // Don't forget to add a ';'!! |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2790 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2791 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2792 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2793 | Result += " {\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2794 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2795 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2796 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2797 | Result += "_IVARS;\n};\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2798 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2799 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2800 | // Mark this struct as having been generated. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2801 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2802 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2805 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2806 | /// class methods. |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2807 | template<typename MethodIterator> |
| 2808 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 2809 | MethodIterator MethodEnd, |
Fariborz Jahanian | a398637 | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2810 | bool IsInstanceMethod, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2811 | const char *prefix, |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2812 | const char *ClassName, |
| 2813 | std::string &Result) { |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2814 | if (MethodBegin == MethodEnd) return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2815 | |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2816 | static bool objc_impl_method = false; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2817 | if (!objc_impl_method) { |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2818 | /* struct _objc_method { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2819 | SEL _cmd; |
| 2820 | char *method_types; |
| 2821 | void *_imp; |
| 2822 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2823 | */ |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2824 | Result += "\nstruct _objc_method {\n"; |
| 2825 | Result += "\tSEL _cmd;\n"; |
| 2826 | Result += "\tchar *method_types;\n"; |
| 2827 | Result += "\tvoid *_imp;\n"; |
| 2828 | Result += "};\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2829 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2830 | objc_impl_method = true; |
Fariborz Jahanian | 96b55da | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2831 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2832 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2833 | // Build _objc_method_list for class's methods if needed |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2834 | |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2835 | /* struct { |
| 2836 | struct _objc_method_list *next_method; |
| 2837 | int method_count; |
| 2838 | struct _objc_method method_list[]; |
| 2839 | } |
| 2840 | */ |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2841 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2842 | Result += "\nstatic struct {\n"; |
| 2843 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2844 | Result += "\tint method_count;\n"; |
| 2845 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2846 | Result += utostr(NumMethods); |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2847 | Result += "];\n} _OBJC_"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2848 | Result += prefix; |
| 2849 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2850 | Result += "_METHODS_"; |
| 2851 | Result += ClassName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2852 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2853 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2854 | Result += "_meth\")))= "; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2855 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2856 | |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2857 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2858 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2859 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2860 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2861 | Result += "\", \""; |
| 2862 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2863 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2864 | Result += MethodInternalNames[*MethodBegin]; |
| 2865 | Result += "}\n"; |
| 2866 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2867 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2868 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2869 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2870 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2871 | Result += "\", \""; |
| 2872 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2873 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2874 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2875 | Result += "}\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2876 | } |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2877 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2880 | /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2881 | void RewriteObjC:: |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2882 | RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, |
| 2883 | const char *ClassName, std::string &Result) { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2884 | static bool objc_protocol_methods = false; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2885 | |
| 2886 | // Output struct protocol_methods holder of method selector and type. |
| 2887 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2888 | /* struct protocol_methods { |
| 2889 | SEL _cmd; |
| 2890 | char *method_types; |
| 2891 | } |
| 2892 | */ |
| 2893 | Result += "\nstruct _protocol_methods {\n"; |
| 2894 | Result += "\tstruct objc_selector *_cmd;\n"; |
| 2895 | Result += "\tchar *method_types;\n"; |
| 2896 | Result += "};\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2897 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2898 | objc_protocol_methods = true; |
| 2899 | } |
| 2900 | // Do not synthesize the protocol more than once. |
| 2901 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2902 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2903 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2904 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2905 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 2906 | PDecl->instmeth_end()); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2907 | /* struct _objc_protocol_method_list { |
| 2908 | int protocol_method_count; |
| 2909 | struct protocol_methods protocols[]; |
| 2910 | } |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2911 | */ |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2912 | Result += "\nstatic struct {\n"; |
| 2913 | Result += "\tint protocol_method_count;\n"; |
| 2914 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 2915 | Result += utostr(NumMethods); |
| 2916 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 2917 | Result += PDecl->getNameAsString(); |
| 2918 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2919 | "{\n\t" + utostr(NumMethods) + "\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2920 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2921 | // Output instance methods declared in this protocol. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2922 | for (ObjCProtocolDecl::instmeth_iterator |
| 2923 | I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2924 | I != E; ++I) { |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2925 | if (I == PDecl->instmeth_begin()) |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2926 | Result += "\t ,{{(struct objc_selector *)\""; |
| 2927 | else |
| 2928 | Result += "\t ,{(struct objc_selector *)\""; |
| 2929 | Result += (*I)->getSelector().getAsString().c_str(); |
| 2930 | std::string MethodTypeString; |
| 2931 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2932 | Result += "\", \""; |
| 2933 | Result += MethodTypeString; |
| 2934 | Result += "\"}\n"; |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2935 | } |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2936 | Result += "\t }\n};\n"; |
| 2937 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2938 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2939 | // Output class methods declared in this protocol. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2940 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 2941 | PDecl->classmeth_end()); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2942 | if (NumMethods > 0) { |
| 2943 | /* struct _objc_protocol_method_list { |
| 2944 | int protocol_method_count; |
| 2945 | struct protocol_methods protocols[]; |
| 2946 | } |
| 2947 | */ |
| 2948 | Result += "\nstatic struct {\n"; |
| 2949 | Result += "\tint protocol_method_count;\n"; |
| 2950 | Result += "\tstruct _protocol_methods protocol_methods["; |
| 2951 | Result += utostr(NumMethods); |
| 2952 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
| 2953 | Result += PDecl->getNameAsString(); |
| 2954 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2955 | "{\n\t"; |
| 2956 | Result += utostr(NumMethods); |
| 2957 | Result += "\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2958 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2959 | // Output instance methods declared in this protocol. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2960 | for (ObjCProtocolDecl::classmeth_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2961 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2962 | I != E; ++I) { |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2963 | if (I == PDecl->classmeth_begin()) |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2964 | Result += "\t ,{{(struct objc_selector *)\""; |
| 2965 | else |
| 2966 | Result += "\t ,{(struct objc_selector *)\""; |
| 2967 | Result += (*I)->getSelector().getAsString().c_str(); |
| 2968 | std::string MethodTypeString; |
| 2969 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2970 | Result += "\", \""; |
| 2971 | Result += MethodTypeString; |
| 2972 | Result += "\"}\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2973 | } |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2974 | Result += "\t }\n};\n"; |
| 2975 | } |
| 2976 | |
| 2977 | // Output: |
| 2978 | /* struct _objc_protocol { |
| 2979 | // Objective-C 1.0 extensions |
| 2980 | struct _objc_protocol_extension *isa; |
| 2981 | char *protocol_name; |
| 2982 | struct _objc_protocol **protocol_list; |
| 2983 | struct _objc_protocol_method_list *instance_methods; |
| 2984 | struct _objc_protocol_method_list *class_methods; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2985 | }; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2986 | */ |
| 2987 | static bool objc_protocol = false; |
| 2988 | if (!objc_protocol) { |
| 2989 | Result += "\nstruct _objc_protocol {\n"; |
| 2990 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2991 | Result += "\tchar *protocol_name;\n"; |
| 2992 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2993 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2994 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2995 | Result += "};\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2996 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 2997 | objc_protocol = true; |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2998 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 2999 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3000 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 3001 | Result += PDecl->getNameAsString(); |
| 3002 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 3003 | "{\n\t0, \""; |
| 3004 | Result += PDecl->getNameAsString(); |
| 3005 | Result += "\", 0, "; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3006 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3007 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 3008 | Result += PDecl->getNameAsString(); |
| 3009 | Result += ", "; |
| 3010 | } |
| 3011 | else |
| 3012 | Result += "0, "; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3013 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3014 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 3015 | Result += PDecl->getNameAsString(); |
| 3016 | Result += "\n"; |
| 3017 | } |
| 3018 | else |
| 3019 | Result += "0\n"; |
| 3020 | Result += "};\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3021 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3022 | // Mark this protocol as having been generated. |
| 3023 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3024 | assert(false && "protocol already synthesized"); |
| 3025 | |
| 3026 | } |
| 3027 | |
| 3028 | void RewriteObjC:: |
| 3029 | RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 3030 | const char *prefix, const char *ClassName, |
| 3031 | std::string &Result) { |
| 3032 | if (Protocols.empty()) return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3033 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3034 | for (unsigned i = 0; i != Protocols.size(); i++) |
| 3035 | RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); |
| 3036 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3037 | // Output the top lovel protocol meta-data for the class. |
| 3038 | /* struct _objc_protocol_list { |
| 3039 | struct _objc_protocol_list *next; |
| 3040 | int protocol_count; |
| 3041 | struct _objc_protocol *class_protocols[]; |
| 3042 | } |
| 3043 | */ |
| 3044 | Result += "\nstatic struct {\n"; |
| 3045 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3046 | Result += "\tint protocol_count;\n"; |
| 3047 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3048 | Result += utostr(Protocols.size()); |
| 3049 | Result += "];\n} _OBJC_"; |
| 3050 | Result += prefix; |
| 3051 | Result += "_PROTOCOLS_"; |
| 3052 | Result += ClassName; |
| 3053 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3054 | "{\n\t0, "; |
| 3055 | Result += utostr(Protocols.size()); |
| 3056 | Result += "\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3057 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3058 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3059 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3060 | Result += " \n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3061 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3062 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3063 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3064 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3065 | Result += "\n"; |
| 3066 | } |
| 3067 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3070 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3071 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3072 | /// implementation. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3073 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3074 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3075 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3076 | // Find category declaration for this implementation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3077 | ObjCCategoryDecl *CDecl; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3078 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3079 | CDecl = CDecl->getNextClassCategory()) |
| 3080 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3081 | break; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3082 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3083 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3084 | FullCategoryName += '_'; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3085 | FullCategoryName += IDecl->getNameAsString(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3086 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3087 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3088 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3089 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3090 | |
| 3091 | // If any of our property implementations have associated getters or |
| 3092 | // setters, produce metadata for them as well. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3093 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3094 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3095 | Prop != PropEnd; ++Prop) { |
| 3096 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3097 | continue; |
| 3098 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3099 | continue; |
| 3100 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3101 | if (!PD) |
| 3102 | continue; |
| 3103 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3104 | InstanceMethods.push_back(Getter); |
| 3105 | if (PD->isReadOnly()) |
| 3106 | continue; |
| 3107 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3108 | InstanceMethods.push_back(Setter); |
| 3109 | } |
| 3110 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3111 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3112 | Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3113 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3114 | // Build _objc_method_list for class's class methods if needed |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3115 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3116 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3117 | Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3118 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3119 | // Protocols referenced in class declaration? |
Fariborz Jahanian | d256e06 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3120 | // Null CDecl is case of a category implementation with no category interface |
| 3121 | if (CDecl) |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3122 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
| 3123 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3124 | /* struct _objc_category { |
| 3125 | char *category_name; |
| 3126 | char *class_name; |
| 3127 | struct _objc_method_list *instance_methods; |
| 3128 | struct _objc_method_list *class_methods; |
| 3129 | struct _objc_protocol_list *protocols; |
| 3130 | // Objective-C 1.0 extensions |
| 3131 | uint32_t size; // sizeof (struct _objc_category) |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3132 | struct _objc_property_list *instance_properties; // category's own |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3133 | // @property decl. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3134 | }; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3135 | */ |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3136 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3137 | static bool objc_category = false; |
| 3138 | if (!objc_category) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3139 | Result += "\nstruct _objc_category {\n"; |
| 3140 | Result += "\tchar *category_name;\n"; |
| 3141 | Result += "\tchar *class_name;\n"; |
| 3142 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3143 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3144 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3145 | Result += "\tunsigned int size;\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3146 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3147 | Result += "};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3148 | objc_category = true; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3149 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3150 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3151 | Result += FullCategoryName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3152 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3153 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3154 | Result += "\"\n\t, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3155 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3156 | Result += "\"\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3157 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3158 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3159 | Result += "\t, (struct _objc_method_list *)" |
| 3160 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3161 | Result += FullCategoryName; |
| 3162 | Result += "\n"; |
| 3163 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3164 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3165 | Result += "\t, 0\n"; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3166 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3167 | Result += "\t, (struct _objc_method_list *)" |
| 3168 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3169 | Result += FullCategoryName; |
| 3170 | Result += "\n"; |
| 3171 | } |
| 3172 | else |
| 3173 | Result += "\t, 0\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3174 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3175 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3176 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3177 | Result += FullCategoryName; |
| 3178 | Result += "\n"; |
| 3179 | } |
| 3180 | else |
| 3181 | Result += "\t, 0\n"; |
| 3182 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3185 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3186 | /// ivar offset. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3187 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 3188 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3189 | std::string &Result) { |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3190 | if (ivar->isBitField()) { |
| 3191 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3192 | // place all bitfields at offset 0. |
| 3193 | Result += "0"; |
| 3194 | } else { |
| 3195 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3196 | Result += IDecl->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3197 | if (LangOpts.Microsoft) |
| 3198 | Result += "_IMPL"; |
| 3199 | Result += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3200 | Result += ivar->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3201 | Result += ")"; |
| 3202 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3205 | //===----------------------------------------------------------------------===// |
| 3206 | // Meta Data Emission |
| 3207 | //===----------------------------------------------------------------------===// |
| 3208 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3209 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3210 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3211 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3212 | |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3213 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 7333b49 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3214 | if (CDecl->isImplicitInterfaceDecl()) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3215 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3216 | // produce correct synthesis as yet. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3217 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3218 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3219 | |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3220 | // Build _objc_ivar_list metadata for classes ivars if needed |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3221 | unsigned NumIvars = !IDecl->ivar_empty() |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3222 | ? IDecl->ivar_size() |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3223 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3224 | if (NumIvars > 0) { |
| 3225 | static bool objc_ivar = false; |
| 3226 | if (!objc_ivar) { |
| 3227 | /* struct _objc_ivar { |
| 3228 | char *ivar_name; |
| 3229 | char *ivar_type; |
| 3230 | int ivar_offset; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3231 | }; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3232 | */ |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3233 | Result += "\nstruct _objc_ivar {\n"; |
| 3234 | Result += "\tchar *ivar_name;\n"; |
| 3235 | Result += "\tchar *ivar_type;\n"; |
| 3236 | Result += "\tint ivar_offset;\n"; |
| 3237 | Result += "};\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3238 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3239 | objc_ivar = true; |
| 3240 | } |
| 3241 | |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3242 | /* struct { |
| 3243 | int ivar_count; |
| 3244 | struct _objc_ivar ivar_list[nIvars]; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3245 | }; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3246 | */ |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3247 | Result += "\nstatic struct {\n"; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3248 | Result += "\tint ivar_count;\n"; |
| 3249 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3250 | Result += utostr(NumIvars); |
| 3251 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3252 | Result += IDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3253 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3254 | "{\n\t"; |
| 3255 | Result += utostr(NumIvars); |
| 3256 | Result += "\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3257 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3258 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 087dbf3 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3259 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3260 | if (!IDecl->ivar_empty()) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3261 | for (ObjCImplementationDecl::ivar_iterator |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3262 | IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); |
Douglas Gregor | 087dbf3 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3263 | IV != IVEnd; ++IV) |
| 3264 | IVars.push_back(*IV); |
| 3265 | IVI = IVars.begin(); |
| 3266 | IVE = IVars.end(); |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3267 | } else { |
| 3268 | IVI = CDecl->ivar_begin(); |
| 3269 | IVE = CDecl->ivar_end(); |
| 3270 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3271 | Result += "\t,{{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3272 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3273 | Result += "\", \""; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3274 | std::string TmpString, StrEncoding; |
| 3275 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3276 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3277 | Result += StrEncoding; |
| 3278 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3279 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3280 | Result += "}\n"; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3281 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3282 | Result += "\t ,{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3283 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3284 | Result += "\", \""; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3285 | std::string TmpString, StrEncoding; |
| 3286 | Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); |
| 3287 | QuoteDoublequotes(TmpString, StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3288 | Result += StrEncoding; |
| 3289 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3290 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3291 | Result += "}\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3292 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3293 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3294 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3295 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3296 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3297 | // Build _objc_method_list for class's instance methods if needed |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3298 | llvm::SmallVector<ObjCMethodDecl *, 32> |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3299 | InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3300 | |
| 3301 | // If any of our property implementations have associated getters or |
| 3302 | // setters, produce metadata for them as well. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3303 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), |
| 3304 | PropEnd = IDecl->propimpl_end(); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3305 | Prop != PropEnd; ++Prop) { |
| 3306 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3307 | continue; |
| 3308 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3309 | continue; |
| 3310 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3311 | if (!PD) |
| 3312 | continue; |
| 3313 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3314 | InstanceMethods.push_back(Getter); |
| 3315 | if (PD->isReadOnly()) |
| 3316 | continue; |
| 3317 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3318 | InstanceMethods.push_back(Setter); |
| 3319 | } |
| 3320 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3321 | true, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3322 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3323 | // Build _objc_method_list for class's class methods if needed |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3324 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3325 | false, "", IDecl->getNameAsCString(), Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3326 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3327 | // Protocols referenced in class declaration? |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3328 | RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), |
| 3329 | "CLASS", CDecl->getNameAsCString(), Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3330 | |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3331 | // Declaration of class/meta-class metadata |
| 3332 | /* struct _objc_class { |
| 3333 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3334 | const char *super_class_name; |
| 3335 | char *name; |
| 3336 | long version; |
| 3337 | long info; |
| 3338 | long instance_size; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3339 | struct _objc_ivar_list *ivars; |
| 3340 | struct _objc_method_list *methods; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3341 | struct objc_cache *cache; |
| 3342 | struct objc_protocol_list *protocols; |
| 3343 | const char *ivar_layout; |
| 3344 | struct _objc_class_ext *ext; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3345 | }; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3346 | */ |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3347 | static bool objc_class = false; |
| 3348 | if (!objc_class) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3349 | Result += "\nstruct _objc_class {\n"; |
| 3350 | Result += "\tstruct _objc_class *isa;\n"; |
| 3351 | Result += "\tconst char *super_class_name;\n"; |
| 3352 | Result += "\tchar *name;\n"; |
| 3353 | Result += "\tlong version;\n"; |
| 3354 | Result += "\tlong info;\n"; |
| 3355 | Result += "\tlong instance_size;\n"; |
| 3356 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3357 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3358 | Result += "\tstruct objc_cache *cache;\n"; |
| 3359 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3360 | Result += "\tconst char *ivar_layout;\n"; |
| 3361 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3362 | Result += "};\n"; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3363 | objc_class = true; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3364 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3365 | |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3366 | // Meta-class metadata generation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3367 | ObjCInterfaceDecl *RootClass = 0; |
| 3368 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3369 | while (SuperClass) { |
| 3370 | RootClass = SuperClass; |
| 3371 | SuperClass = SuperClass->getSuperClass(); |
| 3372 | } |
| 3373 | SuperClass = CDecl->getSuperClass(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3374 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3375 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3376 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3377 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3378 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3379 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3380 | Result += "\""; |
| 3381 | |
| 3382 | if (SuperClass) { |
| 3383 | Result += ", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3384 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3385 | Result += "\", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3386 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3387 | Result += "\""; |
| 3388 | } |
| 3389 | else { |
| 3390 | Result += ", 0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3391 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3392 | Result += "\""; |
| 3393 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3394 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3395 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3396 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3397 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3398 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3399 | Result += IDecl->getNameAsString(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3400 | Result += "\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3401 | } |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3402 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3403 | Result += ", 0\n"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3404 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3405 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3406 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3407 | Result += ",0,0\n"; |
| 3408 | } |
Fariborz Jahanian | 0cb4d92 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3409 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3410 | Result += "\t,0,0,0,0\n"; |
| 3411 | Result += "};\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3412 | |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3413 | // class metadata generation. |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3414 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3415 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3416 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3417 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3418 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3419 | if (SuperClass) { |
| 3420 | Result += ", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3421 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3422 | Result += "\", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3423 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3424 | Result += "\""; |
| 3425 | } |
| 3426 | else { |
| 3427 | Result += ", 0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3428 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3429 | Result += "\""; |
| 3430 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3431 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3432 | Result += ", 0,1"; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3433 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3434 | Result += ",0"; |
| 3435 | else { |
| 3436 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3437 | Result += ",sizeof(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3438 | Result += CDecl->getNameAsString(); |
Steve Naroff | c302e5b | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3439 | if (LangOpts.Microsoft) |
| 3440 | Result += "_IMPL"; |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3441 | Result += ")"; |
| 3442 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3443 | if (NumIvars > 0) { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3444 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3445 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3446 | Result += "\n\t"; |
| 3447 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3448 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3449 | Result += ",0"; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3450 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3451 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3452 | Result += CDecl->getNameAsString(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3453 | Result += ", 0\n\t"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3454 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3455 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3456 | Result += ",0,0"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3457 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3458 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3459 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3460 | Result += ", 0,0\n"; |
| 3461 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3462 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3463 | Result += ",0,0,0\n"; |
| 3464 | Result += "};\n"; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3465 | } |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3466 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3467 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3468 | /// and emits meta-data. |
| 3469 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3470 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3471 | int ClsDefCount = ClassImplementation.size(); |
| 3472 | int CatDefCount = CategoryImplementation.size(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3473 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3474 | // Rewrite implemented methods |
| 3475 | for (int i = 0; i < ClsDefCount; i++) |
| 3476 | RewriteImplementationDecl(ClassImplementation[i]); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3477 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3478 | for (int i = 0; i < CatDefCount; i++) |
| 3479 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3480 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3481 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3482 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3483 | int ClsDefCount = ClassImplementation.size(); |
| 3484 | int CatDefCount = CategoryImplementation.size(); |
| 3485 | |
Steve Naroff | 5bf1022 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3486 | // This is needed for determining instance variable offsets. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3487 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3488 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3489 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3490 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3491 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3492 | // For each implemented category, write out all its meta data. |
| 3493 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3494 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3495 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3496 | // Write objc_symtab metadata |
| 3497 | /* |
| 3498 | struct _objc_symtab |
| 3499 | { |
| 3500 | long sel_ref_cnt; |
| 3501 | SEL *refs; |
| 3502 | short cls_def_cnt; |
| 3503 | short cat_def_cnt; |
| 3504 | void *defs[cls_def_cnt + cat_def_cnt]; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3505 | }; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3506 | */ |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3507 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3508 | Result += "\nstruct _objc_symtab {\n"; |
| 3509 | Result += "\tlong sel_ref_cnt;\n"; |
| 3510 | Result += "\tSEL *refs;\n"; |
| 3511 | Result += "\tshort cls_def_cnt;\n"; |
| 3512 | Result += "\tshort cat_def_cnt;\n"; |
| 3513 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3514 | Result += "};\n\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3515 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3516 | Result += "static struct _objc_symtab " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3517 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3518 | Result += "\t0, 0, " + utostr(ClsDefCount) |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3519 | + ", " + utostr(CatDefCount) + "\n"; |
| 3520 | for (int i = 0; i < ClsDefCount; i++) { |
| 3521 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3522 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3523 | Result += "\n"; |
| 3524 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3525 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3526 | for (int i = 0; i < CatDefCount; i++) { |
| 3527 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3528 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3529 | Result += "_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3530 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3531 | Result += "\n"; |
| 3532 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3533 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3534 | Result += "};\n\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3535 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3536 | // Write objc_module metadata |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3537 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3538 | /* |
| 3539 | struct _objc_module { |
| 3540 | long version; |
| 3541 | long size; |
| 3542 | const char *name; |
| 3543 | struct _objc_symtab *symtab; |
| 3544 | } |
| 3545 | */ |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3546 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3547 | Result += "\nstruct _objc_module {\n"; |
| 3548 | Result += "\tlong version;\n"; |
| 3549 | Result += "\tlong size;\n"; |
| 3550 | Result += "\tconst char *name;\n"; |
| 3551 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3552 | Result += "};\n\n"; |
| 3553 | Result += "static struct _objc_module " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3554 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3555 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3556 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3557 | Result += "};\n\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3558 | |
| 3559 | if (LangOpts.Microsoft) { |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3560 | if (ProtocolExprDecls.size()) { |
| 3561 | Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; |
| 3562 | Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3563 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3564 | E = ProtocolExprDecls.end(); I != E; ++I) { |
| 3565 | Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; |
| 3566 | Result += (*I)->getNameAsString(); |
| 3567 | Result += " = &_OBJC_PROTOCOL_"; |
| 3568 | Result += (*I)->getNameAsString(); |
| 3569 | Result += ";\n"; |
| 3570 | } |
| 3571 | Result += "#pragma data_seg(pop)\n\n"; |
| 3572 | } |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3573 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | bdd2dba | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3574 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3575 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3576 | Result += "&_OBJC_MODULES;\n"; |
| 3577 | Result += "#pragma data_seg(pop)\n\n"; |
| 3578 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3579 | } |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3580 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3581 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3582 | const char *funcName, |
| 3583 | std::string Tag) { |
| 3584 | const FunctionType *AFT = CE->getFunctionType(); |
| 3585 | QualType RT = AFT->getResultType(); |
| 3586 | std::string StructRef = "struct " + Tag; |
| 3587 | std::string S = "static " + RT.getAsString() + " __" + |
| 3588 | funcName + "_" + "block_func_" + utostr(i); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3589 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3590 | BlockDecl *BD = CE->getBlockDecl(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3591 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3592 | if (isa<FunctionNoProtoType>(AFT)) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3593 | // No user-supplied arguments. Still need to pass in a pointer to the |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3594 | // block (to reference imported block decl refs). |
| 3595 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3596 | } else if (BD->param_empty()) { |
| 3597 | S += "(" + StructRef + " *__cself)"; |
| 3598 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3599 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3600 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3601 | S += '('; |
| 3602 | // first add the implicit argument. |
| 3603 | S += StructRef + " *__cself, "; |
| 3604 | std::string ParamStr; |
| 3605 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3606 | E = BD->param_end(); AI != E; ++AI) { |
| 3607 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3608 | ParamStr = (*AI)->getNameAsString(); |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3609 | (*AI)->getType().getAsStringInternal(ParamStr, Context->PrintingPolicy); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3610 | S += ParamStr; |
| 3611 | } |
| 3612 | if (FT->isVariadic()) { |
| 3613 | if (!BD->param_empty()) S += ", "; |
| 3614 | S += "..."; |
| 3615 | } |
| 3616 | S += ')'; |
| 3617 | } |
| 3618 | S += " {\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3619 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3620 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3621 | // First, emit a declaration for all "by ref" decls. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3622 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3623 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3624 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3625 | std::string Name = (*I)->getNameAsString(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3626 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name, |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3627 | Context->PrintingPolicy); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3628 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3629 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3630 | // Next, emit a declaration for all "by copy" declarations. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3631 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3632 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3633 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3634 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3635 | // Handle nested closure invocation. For example: |
| 3636 | // |
| 3637 | // void (^myImportedClosure)(void); |
| 3638 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3639 | // |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3640 | // void (^anotherClosure)(void); |
| 3641 | // anotherClosure = ^(void) { |
| 3642 | // myImportedClosure(); // import and invoke the closure |
| 3643 | // }; |
| 3644 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3645 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3646 | S += "struct __block_impl *"; |
| 3647 | else |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3648 | (*I)->getType().getAsStringInternal(Name, Context->PrintingPolicy); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3649 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3650 | } |
| 3651 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3652 | const char *cstr = RewrittenStr.c_str(); |
| 3653 | while (*cstr++ != '{') ; |
| 3654 | S += cstr; |
| 3655 | S += "\n"; |
| 3656 | return S; |
| 3657 | } |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3658 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3659 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3660 | const char *funcName, |
| 3661 | std::string Tag) { |
| 3662 | std::string StructRef = "struct " + Tag; |
| 3663 | std::string S = "static void __"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3664 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3665 | S += funcName; |
| 3666 | S += "_block_copy_" + utostr(i); |
| 3667 | S += "(" + StructRef; |
| 3668 | S += "*dst, " + StructRef; |
| 3669 | S += "*src) {"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3670 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3671 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3672 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3673 | S += (*I)->getNameAsString(); |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3674 | S += ", (void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3675 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3676 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3677 | } |
| 3678 | S += "\nstatic void __"; |
| 3679 | S += funcName; |
| 3680 | S += "_block_dispose_" + utostr(i); |
| 3681 | S += "(" + StructRef; |
| 3682 | S += "*src) {"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3683 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3684 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3685 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3686 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3687 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3688 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3689 | S += "}\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3690 | return S; |
| 3691 | } |
| 3692 | |
| 3693 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3694 | bool hasCopyDisposeHelpers) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3695 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3696 | std::string Constructor = " " + Tag; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3697 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3698 | S += " {\n struct __block_impl impl;\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3699 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3700 | if (hasCopyDisposeHelpers) |
| 3701 | S += " void *copy;\n void *dispose;\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3702 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3703 | Constructor += "(void *fp"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3704 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3705 | if (hasCopyDisposeHelpers) |
| 3706 | Constructor += ", void *copyHelp, void *disposeHelp"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3707 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3708 | if (BlockDeclRefs.size()) { |
| 3709 | // Output all "by copy" declarations. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3710 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3711 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3712 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3713 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3714 | std::string ArgName = "_" + FieldName; |
| 3715 | // Handle nested closure invocation. For example: |
| 3716 | // |
| 3717 | // void (^myImportedBlock)(void); |
| 3718 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3719 | // |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3720 | // void (^anotherBlock)(void); |
| 3721 | // anotherBlock = ^(void) { |
| 3722 | // myImportedBlock(); // import and invoke the closure |
| 3723 | // }; |
| 3724 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3725 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3726 | S += "struct __block_impl *"; |
| 3727 | Constructor += ", void *" + ArgName; |
| 3728 | } else { |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3729 | (*I)->getType().getAsStringInternal(FieldName, Context->PrintingPolicy); |
| 3730 | (*I)->getType().getAsStringInternal(ArgName, Context->PrintingPolicy); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3731 | Constructor += ", " + ArgName; |
| 3732 | } |
| 3733 | S += FieldName + ";\n"; |
| 3734 | } |
| 3735 | // Output all "by ref" declarations. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3736 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3737 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3738 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3739 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3740 | std::string ArgName = "_" + FieldName; |
| 3741 | // Handle nested closure invocation. For example: |
| 3742 | // |
| 3743 | // void (^myImportedBlock)(void); |
| 3744 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3745 | // |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3746 | // void (^anotherBlock)(void); |
| 3747 | // anotherBlock = ^(void) { |
| 3748 | // myImportedBlock(); // import and invoke the closure |
| 3749 | // }; |
| 3750 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3751 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3752 | S += "struct __block_impl *"; |
| 3753 | Constructor += ", void *" + ArgName; |
| 3754 | } else { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3755 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName, |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3756 | Context->PrintingPolicy); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3757 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName, |
Douglas Gregor | 3bf3bbc | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 3758 | Context->PrintingPolicy); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3759 | Constructor += ", " + ArgName; |
| 3760 | } |
| 3761 | S += FieldName + "; // by ref\n"; |
| 3762 | } |
| 3763 | // Finish writing the constructor. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3764 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3765 | if (GlobalVarDecl) |
| 3766 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3767 | else |
| 3768 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3769 | Constructor += " impl.Size = sizeof("; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3770 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3771 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3772 | if (hasCopyDisposeHelpers) |
| 3773 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3774 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3775 | // Initialize all "by copy" arguments. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3776 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3777 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3778 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3779 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3780 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3781 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3782 | else |
| 3783 | Constructor += Name + " = _"; |
| 3784 | Constructor += Name + ";\n"; |
| 3785 | } |
| 3786 | // Initialize all "by ref" arguments. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3787 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3788 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3789 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3790 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3791 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3792 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3793 | else |
| 3794 | Constructor += Name + " = _"; |
| 3795 | Constructor += Name + ";\n"; |
| 3796 | } |
| 3797 | } else { |
| 3798 | // Finish writing the constructor. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3799 | Constructor += ", int flags=0) {\n"; |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3800 | if (GlobalVarDecl) |
| 3801 | Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; |
| 3802 | else |
| 3803 | Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; |
| 3804 | Constructor += " impl.Size = sizeof("; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3805 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3806 | if (hasCopyDisposeHelpers) |
| 3807 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3808 | } |
| 3809 | Constructor += " "; |
| 3810 | Constructor += "}\n"; |
| 3811 | S += Constructor; |
| 3812 | S += "};\n"; |
| 3813 | return S; |
| 3814 | } |
| 3815 | |
| 3816 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3817 | const char *FunName) { |
| 3818 | // Insert closures that were part of the function. |
| 3819 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3820 | |
| 3821 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3822 | |
| 3823 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3824 | |
| 3825 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3826 | ImportedBlockDecls.size() > 0); |
| 3827 | |
| 3828 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3829 | |
| 3830 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3831 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3832 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3833 | |
| 3834 | if (ImportedBlockDecls.size()) { |
| 3835 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3836 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3837 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3838 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3839 | BlockDeclRefs.clear(); |
| 3840 | BlockByRefDecls.clear(); |
| 3841 | BlockByCopyDecls.clear(); |
| 3842 | BlockCallExprs.clear(); |
| 3843 | ImportedBlockDecls.clear(); |
| 3844 | } |
| 3845 | Blocks.clear(); |
| 3846 | RewrittenBlockExprs.clear(); |
| 3847 | } |
| 3848 | |
| 3849 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3850 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3851 | const char *FuncName = FD->getNameAsCString(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3852 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3853 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3854 | } |
| 3855 | |
| 3856 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3857 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3858 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3859 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3860 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3861 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3862 | // Convert colons to underscores. |
| 3863 | std::string::size_type loc = 0; |
| 3864 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3865 | FuncName.replace(loc, 1, "_"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3866 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3867 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3868 | } |
| 3869 | |
| 3870 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3871 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3872 | CI != E; ++CI) |
| 3873 | if (*CI) { |
| 3874 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3875 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3876 | else |
| 3877 | GetBlockDeclRefExprs(*CI); |
| 3878 | } |
| 3879 | // Handle specific things. |
| 3880 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3881 | // FIXME: Handle enums. |
| 3882 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3883 | BlockDeclRefs.push_back(CDRE); |
| 3884 | return; |
| 3885 | } |
| 3886 | |
| 3887 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3888 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3889 | CI != E; ++CI) |
| 3890 | if (*CI) { |
| 3891 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3892 | GetBlockCallExprs(CBE->getBody()); |
| 3893 | else |
| 3894 | GetBlockCallExprs(*CI); |
| 3895 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3896 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3897 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3898 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3899 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3900 | } |
| 3901 | } |
| 3902 | return; |
| 3903 | } |
| 3904 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3905 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3906 | // Navigate to relevant type information. |
| 3907 | const char *closureName = 0; |
| 3908 | const BlockPointerType *CPT = 0; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3909 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3910 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3911 | closureName = DRE->getDecl()->getNameAsCString(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3912 | CPT = DRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3913 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3914 | closureName = CDRE->getDecl()->getNameAsCString(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3915 | CPT = CDRE->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3916 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3917 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3918 | CPT = MExpr->getType()->getAs<BlockPointerType>(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3919 | } else { |
| 3920 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3921 | } |
| 3922 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3923 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3924 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3925 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3926 | // FTP will be null for closures that don't take arguments. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3927 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3928 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3929 | SourceLocation(), |
| 3930 | &Context->Idents.get("__block_impl")); |
| 3931 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3932 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3933 | // Generate a funky cast. |
| 3934 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3935 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3936 | // Push the block argument type. |
| 3937 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3938 | if (FTP) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3939 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3940 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3941 | QualType t = *I; |
| 3942 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3943 | if (isTopLevelBlockPointerType(t)) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3944 | const BlockPointerType *BPT = t->getAs<BlockPointerType>(); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3945 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3946 | } |
| 3947 | ArgTypes.push_back(t); |
| 3948 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3949 | } |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3950 | // Now do the pointer to function cast. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3951 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3952 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3953 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3954 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3955 | |
| 3956 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 3957 | CastExpr::CK_Unknown, |
| 3958 | Exp->getCallee(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3959 | PtrBlock, SourceLocation(), |
| 3960 | SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3961 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3962 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3963 | BlkCast); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3964 | //PE->dump(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3965 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3966 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3967 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3968 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3969 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 3970 | FD->getType()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3971 | |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 3972 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, |
| 3973 | CastExpr::CK_Unknown, ME, |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3974 | PtrToFuncCastType, |
| 3975 | SourceLocation(), |
| 3976 | SourceLocation()); |
| 3977 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3978 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3979 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3980 | // Add the implicit argument. |
| 3981 | BlkExprs.push_back(BlkCast); |
| 3982 | // Add the user arguments. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 3983 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3984 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3985 | BlkExprs.push_back(*I); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3986 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3987 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3988 | BlkExprs.size(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3989 | Exp->getType(), SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3990 | return CE; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3994 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3995 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 3998 | // We need to return the rewritten expression to handle cases where the |
| 3999 | // BlockDeclRefExpr is embedded in another expression being rewritten. |
| 4000 | // For example: |
| 4001 | // |
| 4002 | // int main() { |
| 4003 | // __block Foo *f; |
| 4004 | // __block int i; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4005 | // |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4006 | // void (^myblock)() = ^() { |
| 4007 | // [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). |
| 4008 | // i = 77; |
| 4009 | // }; |
| 4010 | //} |
| 4011 | Stmt *RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4012 | // FIXME: Add more elaborate code generation required by the ABI. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4013 | Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4014 | Context->getPointerType(BDRE->getType()), |
| 4015 | SourceLocation()); |
| 4016 | // Need parens to enforce precedence. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4017 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 4018 | ReplaceStmt(BDRE, PE); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4019 | return PE; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4022 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 4023 | SourceLocation LocStart = CE->getLParenLoc(); |
| 4024 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4025 | |
| 4026 | // Need to avoid trying to rewrite synthesized casts. |
| 4027 | if (LocStart.isInvalid()) |
| 4028 | return; |
Steve Naroff | 1c53c02 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 4029 | // Need to avoid trying to rewrite casts contained in macros. |
| 4030 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 4031 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4032 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4033 | const char *startBuf = SM->getCharacterData(LocStart); |
| 4034 | const char *endBuf = SM->getCharacterData(LocEnd); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4035 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4036 | // advance the location to startArgList. |
| 4037 | const char *argPtr = startBuf; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4038 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4039 | while (*argPtr++ && (argPtr < endBuf)) { |
| 4040 | switch (*argPtr) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4041 | case '^': |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4042 | // Replace the '^' with '*'. |
| 4043 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 4044 | ReplaceText(LocStart, 1, "*", 1); |
| 4045 | break; |
| 4046 | } |
| 4047 | } |
| 4048 | return; |
| 4049 | } |
| 4050 | |
| 4051 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 4052 | SourceLocation DeclLoc = FD->getLocation(); |
| 4053 | unsigned parenCount = 0; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4054 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4055 | // We have 1 or more arguments that have closure pointers. |
| 4056 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4057 | const char *startArgList = strchr(startBuf, '('); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4058 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4059 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4060 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4061 | parenCount++; |
| 4062 | // advance the location to startArgList. |
| 4063 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 4064 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4065 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4066 | const char *argPtr = startArgList; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4067 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4068 | while (*argPtr++ && parenCount) { |
| 4069 | switch (*argPtr) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4070 | case '^': |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4071 | // Replace the '^' with '*'. |
| 4072 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 4073 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4074 | break; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4075 | case '(': |
| 4076 | parenCount++; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4077 | break; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4078 | case ')': |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4079 | parenCount--; |
| 4080 | break; |
| 4081 | } |
| 4082 | } |
| 4083 | return; |
| 4084 | } |
| 4085 | |
| 4086 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4087 | const FunctionProtoType *FTP; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4088 | const PointerType *PT = QT->getAs<PointerType>(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4089 | if (PT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4090 | FTP = PT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4091 | } else { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4092 | const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4093 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4094 | FTP = BPT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4095 | } |
| 4096 | if (FTP) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4097 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4098 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4099 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4100 | return true; |
| 4101 | } |
| 4102 | return false; |
| 4103 | } |
| 4104 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4105 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4106 | const char *&RParen) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4107 | const char *argPtr = strchr(Name, '('); |
| 4108 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4109 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4110 | LParen = argPtr; // output the start. |
| 4111 | argPtr++; // skip past the left paren. |
| 4112 | unsigned parenCount = 1; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4113 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4114 | while (*argPtr && parenCount) { |
| 4115 | switch (*argPtr) { |
| 4116 | case '(': parenCount++; break; |
| 4117 | case ')': parenCount--; break; |
| 4118 | default: break; |
| 4119 | } |
| 4120 | if (parenCount) argPtr++; |
| 4121 | } |
| 4122 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4123 | RParen = argPtr; // output the end |
| 4124 | } |
| 4125 | |
| 4126 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4127 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4128 | RewriteBlockPointerFunctionArgs(FD); |
| 4129 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4130 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4131 | // Handle Variables and Typedefs. |
| 4132 | SourceLocation DeclLoc = ND->getLocation(); |
| 4133 | QualType DeclT; |
| 4134 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4135 | DeclT = VD->getType(); |
| 4136 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4137 | DeclT = TDD->getUnderlyingType(); |
| 4138 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4139 | DeclT = FD->getType(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4140 | else |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4141 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4142 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4143 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4144 | const char *endBuf = startBuf; |
| 4145 | // scan backward (from the decl location) for the end of the previous decl. |
| 4146 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4147 | startBuf--; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4148 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4149 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4150 | // may take block argument types (which will be handled below). |
| 4151 | if (*startBuf == '^') { |
| 4152 | // Replace the '^' with '*', computing a negative offset. |
| 4153 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4154 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4155 | } |
| 4156 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4157 | // Replace the '^' with '*' for arguments. |
| 4158 | DeclLoc = ND->getLocation(); |
| 4159 | startBuf = SM->getCharacterData(DeclLoc); |
| 4160 | const char *argListBegin, *argListEnd; |
| 4161 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4162 | while (argListBegin < argListEnd) { |
| 4163 | if (*argListBegin == '^') { |
| 4164 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4165 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4166 | } |
| 4167 | argListBegin++; |
| 4168 | } |
| 4169 | } |
| 4170 | return; |
| 4171 | } |
| 4172 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4173 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4174 | // Add initializers for any closure decl refs. |
| 4175 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4176 | if (BlockDeclRefs.size()) { |
| 4177 | // Unique all "by copy" declarations. |
| 4178 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4179 | if (!BlockDeclRefs[i]->isByRef()) |
| 4180 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4181 | // Unique all "by ref" declarations. |
| 4182 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4183 | if (BlockDeclRefs[i]->isByRef()) { |
| 4184 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4185 | } |
| 4186 | // Find any imported blocks...they will need special attention. |
| 4187 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4188 | if (BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | e59c8b1 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4189 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4190 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4191 | } |
| 4192 | } |
| 4193 | } |
| 4194 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4195 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4196 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4197 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4198 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Argiris Kirtzidis | b17120c | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4199 | ID, FType, 0, FunctionDecl::Extern, false, |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4200 | false); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4203 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4204 | Blocks.push_back(Exp); |
| 4205 | |
| 4206 | CollectBlockDeclRefInfo(Exp); |
| 4207 | std::string FuncName; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4208 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4209 | if (CurFunctionDef) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4210 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4211 | else if (CurMethodDef) { |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4212 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4213 | // Convert colons to underscores. |
| 4214 | std::string::size_type loc = 0; |
| 4215 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4216 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4217 | } else if (GlobalVarDecl) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4218 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4219 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4220 | std::string BlockNumber = utostr(Blocks.size()-1); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4221 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4222 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4223 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4224 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4225 | // Get a pointer to the function type so we can cast appropriately. |
| 4226 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4227 | |
| 4228 | FunctionDecl *FD; |
| 4229 | Expr *NewRep; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4230 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4231 | // Simulate a contructor call... |
| 4232 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4233 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4234 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4235 | llvm::SmallVector<Expr*, 4> InitExprs; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4236 | |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4237 | // Initialize the block function. |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4238 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4239 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4240 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4241 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4242 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4243 | Context->VoidPtrTy, SourceLocation(), |
| 4244 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4245 | InitExprs.push_back(castExpr); |
| 4246 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4247 | if (ImportedBlockDecls.size()) { |
| 4248 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4249 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4250 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4251 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4252 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4253 | Context->VoidPtrTy, SourceLocation(), |
| 4254 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4255 | InitExprs.push_back(castExpr); |
| 4256 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4257 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4258 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4259 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4260 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4261 | CastExpr::CK_Unknown, Arg, |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4262 | Context->VoidPtrTy, SourceLocation(), |
| 4263 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4264 | InitExprs.push_back(castExpr); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4265 | } |
| 4266 | // Add initializers for any closure decl refs. |
| 4267 | if (BlockDeclRefs.size()) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4268 | Expr *Exp; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4269 | // Output all "by copy" declarations. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4270 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4271 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4272 | if (isObjCType((*I)->getType())) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4273 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4274 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4275 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4276 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4277 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4278 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4279 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, |
| 4280 | CastExpr::CK_Unknown, Arg, |
| 4281 | Context->VoidPtrTy, |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4282 | SourceLocation(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4283 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4284 | } else { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4285 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4286 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4287 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4288 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4289 | } |
| 4290 | // Output all "by ref" declarations. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4291 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4292 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4293 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4294 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4295 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4296 | Context->getPointerType(Exp->getType()), |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4297 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4298 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4299 | } |
| 4300 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4301 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4302 | FType, SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4303 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4304 | Context->getPointerType(NewRep->getType()), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4305 | SourceLocation()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4306 | NewRep = new (Context) CStyleCastExpr(FType, CastExpr::CK_Unknown, NewRep, |
Anders Carlsson | 7ef181c | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 4307 | FType, SourceLocation(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4308 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4309 | BlockDeclRefs.clear(); |
| 4310 | BlockByRefDecls.clear(); |
| 4311 | BlockByCopyDecls.clear(); |
| 4312 | ImportedBlockDecls.clear(); |
| 4313 | return NewRep; |
| 4314 | } |
| 4315 | |
| 4316 | //===----------------------------------------------------------------------===// |
| 4317 | // Function Body / Expression rewriting |
| 4318 | //===----------------------------------------------------------------------===// |
| 4319 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4320 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4321 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4322 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4323 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4324 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4325 | // we get this right. |
| 4326 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4327 | // Perform a bottom up traversal of all children. |
| 4328 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4329 | CI != E; ++CI) |
| 4330 | if (*CI) |
| 4331 | CollectPropertySetters(*CI); |
| 4332 | |
| 4333 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4334 | if (BinOp->isAssignmentOp()) { |
| 4335 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4336 | PropSetters[PRE] = BinOp; |
| 4337 | } |
| 4338 | } |
| 4339 | } |
| 4340 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4341 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4342 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4343 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4344 | Stmts.push_back(S); |
| 4345 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4346 | Stmts.push_back(S); |
| 4347 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4348 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4349 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4350 | SourceRange OrigStmtRange = S->getSourceRange(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4351 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4352 | // Perform a bottom up rewrite of all children. |
| 4353 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4354 | CI != E; ++CI) |
| 4355 | if (*CI) { |
| 4356 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4357 | if (newStmt) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4358 | *CI = newStmt; |
| 4359 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4360 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4361 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4362 | // Rewrite the block body in place. |
| 4363 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4364 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4365 | // Now we snarf the rewritten text and stash it away for later use. |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4366 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4367 | RewrittenBlockExprs[BE] = Str; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4368 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4369 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4370 | //blockTranscribed->dump(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4371 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4372 | return blockTranscribed; |
| 4373 | } |
| 4374 | // Handle specific things. |
| 4375 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4376 | return RewriteAtEncode(AtEncode); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4377 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4378 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4379 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4380 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4381 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4382 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4383 | if (BinOp) { |
| 4384 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4385 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4386 | DisableReplaceStmt = true; |
| 4387 | // Save the source range. Even if we disable the replacement, the |
| 4388 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4389 | // node is at the 'end', the rewriter will fail. Consider this: |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4390 | // self.errorHandler = handler ? handler : |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4391 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4392 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4393 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4394 | DisableReplaceStmt = false; |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4395 | // |
| 4396 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4397 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4398 | // to see the original expression). Consider this example: |
| 4399 | // |
| 4400 | // Foo *obj1, *obj2; |
| 4401 | // |
| 4402 | // obj1.i = [obj2 rrrr]; |
| 4403 | // |
| 4404 | // 'BinOp' for the previous expression looks like: |
| 4405 | // |
| 4406 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4407 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4408 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4409 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4410 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4411 | // |
| 4412 | // 'newStmt' represents the rewritten message expression. For example: |
| 4413 | // |
| 4414 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4415 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4416 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4417 | // (CStyleCastExpr 0x231d220 'void *' |
| 4418 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4419 | // |
| 4420 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4421 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4422 | // the original RHS (since we haven't altered BinOp). |
| 4423 | // |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4424 | // This implies the Rewrite* routines can no longer delete the original |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4425 | // node. As a result, we now leak the original AST nodes. |
| 4426 | // |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4427 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4428 | } else { |
| 4429 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4430 | } |
| 4431 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4432 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4433 | return RewriteAtSelector(AtSelector); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4434 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4435 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4436 | return RewriteObjCStringLiteral(AtString); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4437 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4438 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4439 | #if 0 |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4440 | // Before we rewrite it, put the original message expression in a comment. |
| 4441 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4442 | SourceLocation endLoc = MessExpr->getLocEnd(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4443 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4444 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4445 | const char *endBuf = SM->getCharacterData(endLoc); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4446 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4447 | std::string messString; |
| 4448 | messString += "// "; |
| 4449 | messString.append(startBuf, endBuf-startBuf+1); |
| 4450 | messString += "\n"; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4451 | |
| 4452 | // FIXME: Missing definition of |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4453 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4454 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4455 | // Tried this, but it didn't work either... |
| 4456 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4457 | #endif |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4458 | return RewriteMessageExpr(MessExpr); |
| 4459 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4460 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4461 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4462 | return RewriteObjCTryStmt(StmtTry); |
| 4463 | |
| 4464 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4465 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4466 | |
| 4467 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4468 | return RewriteObjCThrowStmt(StmtThrow); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4469 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4470 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4471 | return RewriteObjCProtocolExpr(ProtocolExp); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4472 | |
| 4473 | if (ObjCForCollectionStmt *StmtForCollection = |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4474 | dyn_cast<ObjCForCollectionStmt>(S)) |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4475 | return RewriteObjCForCollectionStmt(StmtForCollection, |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4476 | OrigStmtRange.getEnd()); |
| 4477 | if (BreakStmt *StmtBreakStmt = |
| 4478 | dyn_cast<BreakStmt>(S)) |
| 4479 | return RewriteBreakStmt(StmtBreakStmt); |
| 4480 | if (ContinueStmt *StmtContinueStmt = |
| 4481 | dyn_cast<ContinueStmt>(S)) |
| 4482 | return RewriteContinueStmt(StmtContinueStmt); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4483 | |
| 4484 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4485 | // and cast exprs. |
| 4486 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4487 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4488 | // precedes the first Decl. In the future the DeclGroup should have |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4489 | // a separate type-specifier that we can rewrite. |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4490 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4491 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4492 | // Blocks rewrite rules. |
| 4493 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4494 | DI != DE; ++DI) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4495 | Decl *SD = *DI; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4496 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4497 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4498 | RewriteBlockPointerDecl(ND); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4499 | else if (ND->getType()->isFunctionPointerType()) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4500 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4501 | } |
| 4502 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4503 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4504 | RewriteBlockPointerDecl(TD); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4505 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4506 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4507 | } |
| 4508 | } |
| 4509 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4510 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4511 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4512 | RewriteObjCQualifiedInterfaceTypes(CE); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4513 | |
| 4514 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4515 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4516 | assert(!Stmts.empty() && "Statement stack is empty"); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4517 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4518 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4519 | && "Statement stack mismatch"); |
| 4520 | Stmts.pop_back(); |
| 4521 | } |
| 4522 | // Handle blocks rewriting. |
| 4523 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4524 | if (BDRE->isByRef()) |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4525 | return RewriteBlockDeclRefExpr(BDRE); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4526 | } |
| 4527 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4528 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4529 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4530 | ReplaceStmt(S, BlockCall); |
| 4531 | return BlockCall; |
| 4532 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4533 | } |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4534 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4535 | RewriteCastExpr(CE); |
| 4536 | } |
| 4537 | #if 0 |
| 4538 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4539 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4540 | // Get the new text. |
| 4541 | std::string SStr; |
| 4542 | llvm::raw_string_ostream Buf(SStr); |
Eli Friedman | 011a736 | 2009-05-30 05:19:26 +0000 | [diff] [blame] | 4543 | Replacement->printPretty(Buf, *Context); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4544 | const std::string &Str = Buf.str(); |
| 4545 | |
| 4546 | printf("CAST = %s\n", &Str[0]); |
| 4547 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4548 | delete S; |
| 4549 | return Replacement; |
| 4550 | } |
| 4551 | #endif |
| 4552 | // Return this stmt unmodified. |
| 4553 | return S; |
| 4554 | } |
| 4555 | |
| 4556 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4557 | /// main file of the input. |
| 4558 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4559 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | ee8d497 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 4560 | if (FD->isOverloadedOperator()) |
| 4561 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4562 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4563 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4564 | // prototype. This enables us to rewrite function declarations and |
| 4565 | // definitions using the same code. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4566 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4567 | |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 4568 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4569 | if (CompoundStmt *Body = FD->getCompoundBody()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4570 | CurFunctionDef = FD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4571 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4572 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4573 | Body = |
| 4574 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4575 | FD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4576 | CurrentBody = 0; |
| 4577 | if (PropParentMap) { |
| 4578 | delete PropParentMap; |
| 4579 | PropParentMap = 0; |
| 4580 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4581 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4582 | // and any copy/dispose helper functions. |
| 4583 | InsertBlockLiteralsWithinFunction(FD); |
| 4584 | CurFunctionDef = 0; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4585 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4586 | return; |
| 4587 | } |
| 4588 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4589 | if (CompoundStmt *Body = MD->getCompoundBody()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4590 | CurMethodDef = MD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4591 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4592 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4593 | Body = |
| 4594 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4595 | MD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4596 | CurrentBody = 0; |
| 4597 | if (PropParentMap) { |
| 4598 | delete PropParentMap; |
| 4599 | PropParentMap = 0; |
| 4600 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4601 | InsertBlockLiteralsWithinMethod(MD); |
| 4602 | CurMethodDef = 0; |
| 4603 | } |
| 4604 | } |
| 4605 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4606 | ClassImplementation.push_back(CI); |
| 4607 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4608 | CategoryImplementation.push_back(CI); |
| 4609 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4610 | RewriteForwardClassDecl(CD); |
| 4611 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4612 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4613 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4614 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4615 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4616 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4617 | if (VD->getInit()) { |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4618 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4619 | RewriteCastExpr(CE); |
| 4620 | } |
| 4621 | } |
| 4622 | } |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4623 | if (VD->getInit()) { |
| 4624 | GlobalVarDecl = VD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4625 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4626 | CurrentBody = VD->getInit(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4627 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4628 | CurrentBody = 0; |
| 4629 | if (PropParentMap) { |
| 4630 | delete PropParentMap; |
| 4631 | PropParentMap = 0; |
| 4632 | } |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4633 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4634 | VD->getNameAsCString()); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4635 | GlobalVarDecl = 0; |
| 4636 | |
| 4637 | // This is needed for blocks. |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4638 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4639 | RewriteCastExpr(CE); |
| 4640 | } |
| 4641 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4642 | return; |
| 4643 | } |
| 4644 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4645 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4646 | RewriteBlockPointerDecl(TD); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4647 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4648 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4649 | return; |
| 4650 | } |
| 4651 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 4652 | if (RD->isDefinition()) { |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4653 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4654 | e = RD->field_end(); i != e; ++i) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4655 | FieldDecl *FD = *i; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4656 | if (isTopLevelBlockPointerType(FD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4657 | RewriteBlockPointerDecl(FD); |
| 4658 | } |
| 4659 | } |
| 4660 | return; |
| 4661 | } |
| 4662 | // Nothing yet. |
| 4663 | } |
| 4664 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4665 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4666 | // Get the top-level buffer that this corresponds to. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4667 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4668 | // Rewrite tabs if we care. |
| 4669 | //RewriteTabs(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4670 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4671 | if (Diags.hasErrorOccurred()) |
| 4672 | return; |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4673 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4674 | RewriteInclude(); |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4675 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4676 | // Here's a great place to add any extra declarations that may be needed. |
| 4677 | // Write out meta data for each @protocol(<expr>). |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4678 | for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4679 | E = ProtocolExprDecls.end(); I != E; ++I) |
| 4680 | RewriteObjCProtocolMetaData(*I, "", "", Preamble); |
| 4681 | |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4682 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4683 | Preamble.c_str(), Preamble.size(), false); |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4684 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4685 | RewriteImplementations(); |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4686 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4687 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4688 | // we are done. |
Mike Stump | 25cf760 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 4689 | if (const RewriteBuffer *RewriteBuf = |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4690 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4691 | //printf("Changed:\n"); |
| 4692 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4693 | } else { |
| 4694 | fprintf(stderr, "No changes\n"); |
| 4695 | } |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4696 | |
Steve Naroff | 450d2fc | 2009-04-29 16:37:50 +0000 | [diff] [blame] | 4697 | if (ClassImplementation.size() || CategoryImplementation.size() || |
| 4698 | ProtocolExprDecls.size()) { |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4699 | // Rewrite Objective-c meta data* |
| 4700 | std::string ResultStr; |
| 4701 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4702 | // Emit metadata. |
| 4703 | *OutFile << ResultStr; |
| 4704 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4705 | OutFile->flush(); |
| 4706 | } |
| 4707 | |