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 | |
| 14 | #include "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" |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | ae43eb7 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Steve Naroff | 1c46cf1 | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 6da1510 | 2008-05-21 20:19:16 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 673f2bd | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 30 | #include "llvm/System/Path.h" |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 31 | using namespace clang; |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 32 | using llvm::utostr; |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 33 | |
Steve Naroff | 1c46cf1 | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 34 | static llvm::cl::opt<bool> |
| 35 | SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false), |
| 36 | llvm::cl::desc("Silence ObjC rewriting warnings")); |
| 37 | |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 38 | namespace { |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 39 | class RewriteObjC : public ASTConsumer { |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 40 | Rewriter Rewrite; |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 41 | Diagnostic &Diags; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 42 | const LangOptions &LangOpts; |
Steve Naroff | 53b6f4c | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 43 | unsigned RewriteFailedDiag; |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 44 | unsigned TryFinallyContainsReturnDiag; |
| 45 | |
Chris Lattner | bf0bfa6 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 46 | ASTContext *Context; |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 47 | SourceManager *SM; |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 48 | TranslationUnitDecl *TUDecl; |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 49 | FileID MainFileID; |
Chris Lattner | ae43eb7 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 50 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 51 | SourceLocation LastIncLoc; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 53 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 54 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 55 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 56 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 57 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 58 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 59 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 60 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 61 | |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 62 | unsigned NumObjCStringLiterals; |
| 63 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 64 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 65 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 66 | FunctionDecl *MsgSendStretFunctionDecl; |
| 67 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 68 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 69 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 70 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 71 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 72 | FunctionDecl *CFStringFunctionDecl; |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 73 | FunctionDecl *GetProtocolFunctionDecl; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 74 | FunctionDecl *SuperContructorFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 75 | |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 76 | // ObjC string constant support. |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 77 | VarDecl *ConstantStringClassReference; |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 78 | RecordDecl *NSStringRecord; |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 79 | |
Fariborz Jahanian | 2227742 | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 80 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 81 | int BcLabelCount; |
| 82 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 83 | // Needed for super. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 84 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 85 | RecordDecl *SuperStructDecl; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 86 | RecordDecl *ConstantStringDecl; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 87 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 88 | // Needed for header files being rewritten |
| 89 | bool IsHeader; |
| 90 | |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 91 | std::string InFileName; |
| 92 | std::string OutFileName; |
| 93 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 94 | std::string Preamble; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 95 | |
| 96 | // Block expressions. |
| 97 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 98 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 99 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 100 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 101 | // Block related declarations. |
| 102 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 103 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 104 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 105 | |
| 106 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 107 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 108 | // This maps a property to it's assignment statement. |
| 109 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 110 | // This maps a property to it's synthesied message expression. |
| 111 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 112 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
| 113 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 114 | // This maps an original source AST to it's rewritten form. This allows |
| 115 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 116 | // This is needed to support some of the exotic property rewriting. |
| 117 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 118 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 119 | FunctionDecl *CurFunctionDef; |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 120 | VarDecl *GlobalVarDecl; |
| 121 | |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 122 | bool DisableReplaceStmt; |
| 123 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 124 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 125 | public: |
Ted Kremenek | 79b50cb | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 126 | virtual void Initialize(ASTContext &context); |
| 127 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 128 | // Top Level Driver code. |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 129 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 130 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 131 | HandleTopLevelSingleDecl(*I); |
| 132 | } |
| 133 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 134 | void HandleDeclInMainFile(Decl *D); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 135 | RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 136 | Diagnostic &D, const LangOptions &LOpts); |
Ted Kremenek | cab0b0c | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 137 | |
| 138 | ~RewriteObjC() {} |
| 139 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 140 | virtual void HandleTranslationUnit(ASTContext &C); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 141 | |
| 142 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 143 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 144 | |
| 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); |
| 173 | New->printPretty(S); |
| 174 | const std::string &Str = S.str(); |
| 175 | |
| 176 | // If replacement succeeded or warning disabled return with no warning. |
| 177 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, &Str[0], Str.size())) { |
| 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. |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 190 | if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) || |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 191 | SilenceRewriteMacroWarning) |
| 192 | return; |
| 193 | |
| 194 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 195 | } |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 196 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 197 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 198 | // If removal succeeded or warning disabled return with no warning. |
| 199 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 200 | return; |
| 201 | |
| 202 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 203 | } |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 204 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 205 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 206 | const char *NewStr, unsigned NewLength) { |
| 207 | // If removal succeeded or warning disabled return with no warning. |
| 208 | if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) || |
| 209 | SilenceRewriteMacroWarning) |
| 210 | return; |
| 211 | |
| 212 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 213 | } |
| 214 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 215 | // Syntactic Rewriting. |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 216 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 217 | void RewriteInclude(); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 218 | void RewriteTabs(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 219 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 220 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 221 | ObjCImplementationDecl *IMD, |
| 222 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 223 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 224 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 225 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 226 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 227 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 228 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 229 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 230 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 231 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 232 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 233 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 234 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 235 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 236 | QualType getSuperStructType(); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 237 | QualType getConstantStringStructType(); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 238 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 239 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 240 | // Expression Rewriting. |
Steve Naroff | 334fbc2 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 241 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 242 | void CollectPropertySetters(Stmt *S); |
| 243 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 244 | Stmt *CurrentBody; |
| 245 | ParentMap *PropParentMap; // created lazily. |
| 246 | |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 247 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 248 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 249 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 250 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 251 | SourceRange SrcRange); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 252 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 253 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 254 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 255 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 256 | void WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 257 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 258 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 259 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 260 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 261 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 2c02216 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 262 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 263 | SourceLocation OrigEnd); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 264 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 265 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 266 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 267 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 268 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 269 | void SynthCountByEnumWithState(std::string &buf); |
| 270 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 271 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 272 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 273 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 274 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 275 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 276 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 277 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 278 | void SynthSelGetUidFunctionDecl(); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 279 | void SynthGetProtocolFunctionDecl(); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 280 | void SynthSuperContructorFunctionDecl(); |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 281 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 282 | // Metadata emission. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 283 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 284 | std::string &Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 286 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 287 | std::string &Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 289 | template<typename MethodIterator> |
| 290 | void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 291 | MethodIterator MethodEnd, |
Fariborz Jahanian | a398637 | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 292 | bool IsInstanceMethod, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 293 | const char *prefix, |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 294 | const char *ClassName, |
| 295 | std::string &Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 297 | void RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> |
| 298 | &Protocols, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 299 | const char *prefix, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 300 | const char *ClassName, |
| 301 | std::string &Result); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 302 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 303 | std::string &Result); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 304 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 305 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 306 | std::string &Result); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 307 | void RewriteImplementations(); |
| 308 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 309 | |
| 310 | // Block rewriting. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 311 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 312 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 313 | |
| 314 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 315 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 316 | |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 317 | // Block specific rewrite rules. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 318 | void RewriteBlockCall(CallExpr *Exp); |
| 319 | void RewriteBlockPointerDecl(NamedDecl *VD); |
| 320 | void RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
| 321 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 322 | |
| 323 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 324 | const char *funcName, std::string Tag); |
| 325 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 326 | const char *funcName, std::string Tag); |
| 327 | std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 328 | bool hasCopyDisposeHelpers); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 329 | Stmt *SynthesizeBlockCall(CallExpr *Exp); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 330 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 331 | const char *FunName); |
| 332 | |
| 333 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 334 | void GetBlockCallExprs(Stmt *S); |
| 335 | void GetBlockDeclRefExprs(Stmt *S); |
| 336 | |
| 337 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 338 | // 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] | 339 | bool isTopLevelBlockPointerType(QualType T) { |
| 340 | return isa<BlockPointerType>(T); |
| 341 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 342 | |
| 343 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 344 | bool isObjCType(QualType T) { |
| 345 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 346 | return false; |
| 347 | |
| 348 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 349 | |
| 350 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 351 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 352 | return true; |
| 353 | |
| 354 | if (const PointerType *PT = OCT->getAsPointerType()) { |
| 355 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 356 | isa<ObjCQualifiedIdType>(PT->getPointeeType())) |
| 357 | return true; |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 362 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 363 | const char *&RParen); |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 364 | void RewriteCastExpr(CStyleCastExpr *CE); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 365 | |
| 366 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 367 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 368 | }; |
| 369 | } |
| 370 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 371 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 372 | NamedDecl *D) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 373 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
| 374 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 375 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 376 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 377 | // All the args are checked/rewritten. Don't call twice! |
| 378 | RewriteBlockPointerDecl(D); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 385 | const PointerType *PT = funcType->getAsPointerType(); |
| 386 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 387 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 390 | static bool IsHeaderFile(const std::string &Filename) { |
| 391 | std::string::size_type DotPos = Filename.rfind('.'); |
| 392 | |
| 393 | if (DotPos == std::string::npos) { |
| 394 | // no file extension |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 399 | // C header: .h |
| 400 | // C++ header: .hh or .H; |
| 401 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 402 | } |
| 403 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 404 | RewriteObjC::RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 405 | Diagnostic &D, const LangOptions &LOpts) |
| 406 | : Diags(D), LangOpts(LOpts) { |
| 407 | IsHeader = IsHeaderFile(inFile); |
| 408 | InFileName = inFile; |
| 409 | OutFileName = outFile; |
| 410 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 411 | "rewriting sub-expression within a macro (may not be correct)"); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 412 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 413 | "rewriter doesn't support user-specified control flow semantics " |
| 414 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 417 | ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, |
Chris Lattner | 673f2bd | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 418 | const std::string& OutFile, |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 419 | Diagnostic &Diags, |
| 420 | const LangOptions &LOpts) { |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 421 | return new RewriteObjC(InFile, OutFile, Diags, LOpts); |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 422 | } |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 423 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 424 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 425 | Context = &context; |
| 426 | SM = &Context->getSourceManager(); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 427 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 428 | MsgSendFunctionDecl = 0; |
| 429 | MsgSendSuperFunctionDecl = 0; |
| 430 | MsgSendStretFunctionDecl = 0; |
| 431 | MsgSendSuperStretFunctionDecl = 0; |
| 432 | MsgSendFpretFunctionDecl = 0; |
| 433 | GetClassFunctionDecl = 0; |
| 434 | GetMetaClassFunctionDecl = 0; |
| 435 | SelGetUidFunctionDecl = 0; |
| 436 | CFStringFunctionDecl = 0; |
| 437 | GetProtocolFunctionDecl = 0; |
| 438 | ConstantStringClassReference = 0; |
| 439 | NSStringRecord = 0; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 440 | CurMethodDef = 0; |
| 441 | CurFunctionDef = 0; |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 442 | GlobalVarDecl = 0; |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 443 | SuperStructDecl = 0; |
Steve Naroff | 053ae3f | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 444 | ConstantStringDecl = 0; |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 445 | BcLabelCount = 0; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 446 | SuperContructorFunctionDecl = 0; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 447 | NumObjCStringLiterals = 0; |
Steve Naroff | bad6f3b | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 448 | PropParentMap = 0; |
| 449 | CurrentBody = 0; |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 450 | DisableReplaceStmt = false; |
| 451 | |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 452 | // Get the ID and start/end of the main file. |
| 453 | MainFileID = SM->getMainFileID(); |
| 454 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 455 | MainFileStart = MainBuf->getBufferStart(); |
| 456 | MainFileEnd = MainBuf->getBufferEnd(); |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 457 | |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 458 | Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 460 | // declaring objc_selector outside the parameter list removes a silly |
| 461 | // scope related warning... |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 462 | if (IsHeader) |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 463 | Preamble = "#pragma once\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 464 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 465 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 466 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 467 | if (LangOpts.Microsoft) { |
| 468 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 469 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 470 | ": "; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 471 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 472 | } |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 473 | Preamble += "};\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 474 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 475 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 476 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 477 | Preamble += "#endif\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 478 | if (LangOpts.Microsoft) { |
| 479 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 480 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 481 | } else |
| 482 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 483 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 484 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 485 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 486 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 487 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 488 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 489 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 490 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 491 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 492 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 493 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 494 | Preamble += "(const char *);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 495 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 496 | Preamble += "(const char *);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 497 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 498 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 499 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 500 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 501 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 3e2c98c | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 502 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | cd25d78 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 503 | // @synchronized hooks. |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 504 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 505 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 506 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 507 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 508 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 509 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | c960e9d | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 510 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 511 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 512 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 513 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 514 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 515 | Preamble += "#endif\n"; |
| 516 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 517 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 518 | Preamble += " int *isa;\n"; |
| 519 | Preamble += " int flags;\n"; |
| 520 | Preamble += " char *str;\n"; |
| 521 | Preamble += " long length;\n"; |
| 522 | Preamble += "};\n"; |
Steve Naroff | c6e571e | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 523 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 524 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 525 | Preamble += "#else\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 526 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | c6e571e | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 527 | Preamble += "#endif\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 528 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 529 | Preamble += "#endif\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 530 | // Blocks preamble. |
| 531 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 532 | Preamble += "#define BLOCK_IMPL\n"; |
| 533 | Preamble += "struct __block_impl {\n"; |
| 534 | Preamble += " void *isa;\n"; |
| 535 | Preamble += " int Flags;\n"; |
| 536 | Preamble += " int Size;\n"; |
| 537 | Preamble += " void *FuncPtr;\n"; |
| 538 | Preamble += "};\n"; |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 539 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 540 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 541 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 542 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 543 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteStackBlock[32];\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 544 | Preamble += "#endif\n"; |
Steve Naroff | 39ba90e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 545 | if (LangOpts.Microsoft) { |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 546 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 547 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | 39ba90e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 548 | Preamble += "#define __attribute__(X)\n"; |
| 549 | } |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 553 | //===----------------------------------------------------------------------===// |
| 554 | // Top Level Driver Code |
| 555 | //===----------------------------------------------------------------------===// |
| 556 | |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 557 | void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 558 | // Two cases: either the decl could be in the main file, or it could be in a |
| 559 | // #included file. If the former, rewrite it now. If the later, check to see |
| 560 | // if we rewrote the #include/#import. |
| 561 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 562 | Loc = SM->getInstantiationLoc(Loc); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 563 | |
| 564 | // If this is for a builtin, ignore it. |
| 565 | if (Loc.isInvalid()) return; |
| 566 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 567 | // Look for built-in declarations that we need to refer during the rewrite. |
| 568 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 569 | RewriteFunctionDecl(FD); |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 570 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 571 | // declared in <Foundation/NSString.h> |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 572 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 573 | ConstantStringClassReference = FVD; |
| 574 | return; |
| 575 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 576 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 577 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 578 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 579 | RewriteCategoryDecl(CD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 580 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 581 | RewriteProtocolDecl(PD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 582 | } else if (ObjCForwardProtocolDecl *FP = |
| 583 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 584 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 585 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 586 | // Recurse into linkage specifications |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 587 | for (DeclContext::decl_iterator DI = LSD->decls_begin(*Context), |
| 588 | DIEnd = LSD->decls_end(*Context); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 589 | DI != DIEnd; ++DI) |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 590 | HandleTopLevelSingleDecl(*DI); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 591 | } |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 592 | // 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] | 593 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 594 | return HandleDeclInMainFile(D); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 597 | //===----------------------------------------------------------------------===// |
| 598 | // Syntactic (non-AST) Rewriting Code |
| 599 | //===----------------------------------------------------------------------===// |
| 600 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 601 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 602 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 603 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 604 | const char *MainBufStart = MainBuf.first; |
| 605 | const char *MainBufEnd = MainBuf.second; |
| 606 | size_t ImportLen = strlen("import"); |
| 607 | size_t IncludeLen = strlen("include"); |
| 608 | |
Fariborz Jahanian | c81ed74 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 609 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 610 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 611 | if (*BufPtr == '#') { |
| 612 | if (++BufPtr == MainBufEnd) |
| 613 | return; |
| 614 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 615 | if (++BufPtr == MainBufEnd) |
| 616 | return; |
| 617 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 618 | // replace import with include |
| 619 | SourceLocation ImportLoc = |
| 620 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 621 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 622 | BufPtr += ImportLen; |
| 623 | } |
| 624 | } |
| 625 | } |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 628 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 629 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 630 | const char *MainBufStart = MainBuf.first; |
| 631 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 632 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 633 | // Loop over the whole file, looking for tabs. |
| 634 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 635 | if (*BufPtr != '\t') |
| 636 | continue; |
| 637 | |
| 638 | // Okay, we found a tab. This tab will turn into at least one character, |
| 639 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 640 | unsigned VCol = 0; |
| 641 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 642 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 643 | ++VCol; |
| 644 | |
| 645 | // Okay, now that we know the virtual column, we know how many spaces to |
| 646 | // insert. We assume 8-character tab-stops. |
| 647 | unsigned Spaces = 8-(VCol & 7); |
| 648 | |
| 649 | // Get the location of the tab. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 650 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 651 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 652 | |
| 653 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 654 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 655 | } |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 658 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 659 | ObjCIvarDecl *OID) { |
| 660 | std::string S; |
| 661 | S = "((struct "; |
| 662 | S += ClassDecl->getIdentifier()->getName(); |
| 663 | S += "_IMPL *)self)->"; |
| 664 | S += OID->getNameAsCString(); |
| 665 | return S; |
| 666 | } |
| 667 | |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 668 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 669 | ObjCImplementationDecl *IMD, |
| 670 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 671 | SourceLocation startLoc = PID->getLocStart(); |
| 672 | InsertText(startLoc, "// ", 3); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 673 | const char *startBuf = SM->getCharacterData(startLoc); |
| 674 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 675 | const char *semiBuf = strchr(startBuf, ';'); |
| 676 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 677 | SourceLocation onePastSemiLoc = |
| 678 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 679 | |
| 680 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 681 | return; // FIXME: is this correct? |
| 682 | |
| 683 | // Generate the 'getter' function. |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 684 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 685 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 686 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 687 | |
| 688 | if (!OID) |
| 689 | return; |
| 690 | |
| 691 | std::string Getr; |
| 692 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 693 | Getr += "{ "; |
| 694 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 11671e7 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 695 | // FIXME: deal with code generation implications for various property |
| 696 | // attributes (copy, retain, nonatomic). |
| 697 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 698 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 699 | Getr += "; }"; |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 700 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 701 | |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 702 | if (PD->isReadOnly()) |
| 703 | return; |
| 704 | |
| 705 | // Generate the 'setter' function. |
| 706 | std::string Setr; |
| 707 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 708 | Setr += "{ "; |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 709 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 11671e7 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 710 | // FIXME: deal with code generation implications for various property |
| 711 | // attributes (copy, retain, nonatomic). |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 712 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 713 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 714 | Setr += PD->getNameAsCString(); |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 715 | Setr += "; }"; |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 716 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 717 | } |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 718 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 719 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 720 | // Get the start location and compute the semi location. |
| 721 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 722 | const char *startBuf = SM->getCharacterData(startLoc); |
| 723 | const char *semiPtr = strchr(startBuf, ';'); |
| 724 | |
| 725 | // Translate to typedef's that forward reference structs with the same name |
| 726 | // as the class. As a convenience, we include the original declaration |
| 727 | // as a comment. |
| 728 | std::string typedefString; |
| 729 | typedefString += "// "; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 730 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 731 | typedefString += "\n"; |
Chris Lattner | 6a02874 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 732 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 733 | I != E; ++I) { |
| 734 | ObjCInterfaceDecl *ForwardDecl = *I; |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 735 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 736 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 737 | typedefString += "\n"; |
| 738 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 739 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 740 | typedefString += "\n"; |
Steve Naroff | 4242b97 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 741 | typedefString += "typedef struct objc_object "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 742 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 743 | typedefString += ";\n#endif\n"; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 747 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 748 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 751 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 752 | SourceLocation LocStart = Method->getLocStart(); |
| 753 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 2d89c56 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 755 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 756 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | 99f50fe | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 757 | InsertText(LocStart, "#if 0\n", 6); |
| 758 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 759 | } else { |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 760 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 764 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) |
Fariborz Jahanian | eca7fad | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 765 | { |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 766 | SourceLocation Loc = prop->getLocation(); |
| 767 | |
| 768 | ReplaceText(Loc, 0, "// ", 3); |
| 769 | |
| 770 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | eca7fad | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 773 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 774 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 775 | |
| 776 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 777 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 779 | for (ObjCCategoryDecl::instmeth_iterator |
| 780 | I = CatDecl->instmeth_begin(*Context), |
| 781 | E = CatDecl->instmeth_end(*Context); |
| 782 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 783 | RewriteMethodDeclaration(*I); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 784 | for (ObjCCategoryDecl::classmeth_iterator |
| 785 | I = CatDecl->classmeth_begin(*Context), |
| 786 | E = CatDecl->classmeth_end(*Context); |
| 787 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 788 | RewriteMethodDeclaration(*I); |
| 789 | |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 790 | // Lastly, comment out the @end. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 791 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 794 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 795 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 796 | |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 797 | SourceLocation LocStart = PDecl->getLocStart(); |
| 798 | |
| 799 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 800 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 802 | for (ObjCProtocolDecl::instmeth_iterator |
| 803 | I = PDecl->instmeth_begin(*Context), |
| 804 | E = PDecl->instmeth_end(*Context); |
| 805 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 806 | RewriteMethodDeclaration(*I); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 807 | for (ObjCProtocolDecl::classmeth_iterator |
| 808 | I = PDecl->classmeth_begin(*Context), |
| 809 | E = PDecl->classmeth_end(*Context); |
| 810 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 811 | RewriteMethodDeclaration(*I); |
| 812 | |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 813 | // Lastly, comment out the @end. |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 814 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 815 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 816 | |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 817 | // Must comment out @optional/@required |
| 818 | const char *startBuf = SM->getCharacterData(LocStart); |
| 819 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 820 | for (const char *p = startBuf; p < endBuf; p++) { |
| 821 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 822 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 823 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 824 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 825 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 826 | |
| 827 | } |
| 828 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 829 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 830 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 831 | ReplaceText(OptionalLoc, strlen("@required"), |
| 832 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 833 | |
| 834 | } |
| 835 | } |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 838 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 839 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | 0540f3f | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 840 | if (LocStart.isInvalid()) |
| 841 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 842 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 843 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 846 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 847 | std::string &ResultStr) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 848 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 849 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 850 | ResultStr += "\nstatic "; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 851 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 852 | ResultStr += "id"; |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 853 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 854 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 855 | // needs special handling, since pointer-to-functions have special |
| 856 | // syntax (where a decaration models use). |
| 857 | QualType retType = OMD->getResultType(); |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 858 | QualType PointeeTy; |
| 859 | if (const PointerType* PT = retType->getAsPointerType()) |
| 860 | PointeeTy = PT->getPointeeType(); |
| 861 | else if (const BlockPointerType *BPT = retType->getAsBlockPointerType()) |
| 862 | PointeeTy = BPT->getPointeeType(); |
| 863 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 864 | ResultStr += FPRetType->getResultType().getAsString(); |
| 865 | ResultStr += "(*"; |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 866 | } |
| 867 | } else |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 868 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 7202d98 | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 869 | ResultStr += " "; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 870 | |
| 871 | // Unique method name |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 872 | std::string NameStr; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 874 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 875 | NameStr += "_I_"; |
| 876 | else |
| 877 | NameStr += "_C_"; |
| 878 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 879 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 880 | NameStr += "_"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 881 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 882 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 438be77 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 883 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 884 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 885 | NameStr += "_"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 886 | } |
Steve Naroff | 5cb1e6e | 2008-04-04 22:23:44 +0000 | [diff] [blame] | 887 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 888 | { |
| 889 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 890 | int len = selString.size(); |
| 891 | for (int i = 0; i < len; i++) |
| 892 | if (selString[i] == ':') |
| 893 | selString[i] = '_'; |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 894 | NameStr += selString; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 895 | } |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 896 | // Remember this name for metadata emission |
| 897 | MethodInternalNames[OMD] = NameStr; |
| 898 | ResultStr += NameStr; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 899 | |
| 900 | // Rewrite arguments |
| 901 | ResultStr += "("; |
| 902 | |
| 903 | // invisible arguments |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 904 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 905 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 906 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 907 | if (!LangOpts.Microsoft) { |
| 908 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 909 | ResultStr += "struct "; |
| 910 | } |
| 911 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 912 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 913 | ResultStr += " *"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 914 | } |
| 915 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 916 | ResultStr += Context->getObjCIdType().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 917 | |
| 918 | ResultStr += " self, "; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 919 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 920 | ResultStr += " _cmd"; |
| 921 | |
| 922 | // Method arguments. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 923 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 924 | E = OMD->param_end(); PI != E; ++PI) { |
| 925 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 926 | ResultStr += ", "; |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 927 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 928 | ResultStr += "id "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 929 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 930 | } else { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 931 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 932 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | 4e2ae51 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 933 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 934 | const BlockPointerType *BPT = PDecl->getType()->getAsBlockPointerType(); |
| 935 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name); |
| 936 | } else |
| 937 | PDecl->getType().getAsStringInternal(Name); |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 938 | ResultStr += Name; |
| 939 | } |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 940 | } |
Fariborz Jahanian | 2fab94e | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 941 | if (OMD->isVariadic()) |
| 942 | ResultStr += ", ..."; |
Fariborz Jahanian | 7202d98 | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 943 | ResultStr += ") "; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 944 | |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 945 | if (FPRetType) { |
| 946 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 947 | |
| 948 | // Now, emit the argument types (if any). |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 949 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 950 | ResultStr += "("; |
| 951 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 952 | if (i) ResultStr += ", "; |
| 953 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 954 | ResultStr += ParamStr; |
| 955 | } |
| 956 | if (FT->isVariadic()) { |
| 957 | if (FT->getNumArgs()) ResultStr += ", "; |
| 958 | ResultStr += "..."; |
| 959 | } |
| 960 | ResultStr += ")"; |
| 961 | } else { |
| 962 | ResultStr += "()"; |
| 963 | } |
| 964 | } |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 965 | } |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 966 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 967 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 968 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 969 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 970 | if (IMD) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 971 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 972 | else |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 973 | InsertText(CID->getLocStart(), "// ", 3); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 974 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 975 | for (ObjCCategoryImplDecl::instmeth_iterator |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 976 | I = IMD ? IMD->instmeth_begin(*Context) : CID->instmeth_begin(*Context), |
| 977 | E = IMD ? IMD->instmeth_end(*Context) : CID->instmeth_end(*Context); |
| 978 | I != E; ++I) { |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 979 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 980 | ObjCMethodDecl *OMD = *I; |
| 981 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 982 | SourceLocation LocStart = OMD->getLocStart(); |
Douglas Gregor | e3241e9 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 983 | SourceLocation LocEnd = OMD->getBody(*Context)->getLocStart(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 984 | |
| 985 | const char *startBuf = SM->getCharacterData(LocStart); |
| 986 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 987 | ReplaceText(LocStart, endBuf-startBuf, |
| 988 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 991 | for (ObjCCategoryImplDecl::classmeth_iterator |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 992 | I = IMD ? IMD->classmeth_begin(*Context) : CID->classmeth_begin(*Context), |
| 993 | E = IMD ? IMD->classmeth_end(*Context) : CID->classmeth_end(*Context); |
| 994 | I != E; ++I) { |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 995 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 996 | ObjCMethodDecl *OMD = *I; |
| 997 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 998 | SourceLocation LocStart = OMD->getLocStart(); |
Douglas Gregor | e3241e9 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 999 | SourceLocation LocEnd = OMD->getBody(*Context)->getLocStart(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1000 | |
| 1001 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1002 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1003 | ReplaceText(LocStart, endBuf-startBuf, |
| 1004 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1005 | } |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1006 | for (ObjCCategoryImplDecl::propimpl_iterator |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 1007 | I = IMD ? IMD->propimpl_begin(*Context) : CID->propimpl_begin(*Context), |
| 1008 | E = IMD ? IMD->propimpl_end(*Context) : CID->propimpl_end(*Context); |
| 1009 | I != E; ++I) { |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1010 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1013 | if (IMD) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1014 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1015 | else |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1016 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1019 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1020 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1021 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 77d081b | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1022 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 2adead7 | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1023 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1024 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1025 | ResultStr += "\n"; |
| 1026 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1027 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1028 | ResultStr += "\n"; |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1029 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1030 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1031 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 77d081b | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1032 | // Mark this typedef as having been generated. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1033 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 77d081b | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1034 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1035 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1036 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1037 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(*Context), |
| 1038 | E = ClassDecl->prop_end(*Context); I != E; ++I) |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1039 | RewriteProperty(*I); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1040 | for (ObjCInterfaceDecl::instmeth_iterator |
| 1041 | I = ClassDecl->instmeth_begin(*Context), |
| 1042 | E = ClassDecl->instmeth_end(*Context); |
| 1043 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1044 | RewriteMethodDeclaration(*I); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1045 | for (ObjCInterfaceDecl::classmeth_iterator |
| 1046 | I = ClassDecl->classmeth_begin(*Context), |
| 1047 | E = ClassDecl->classmeth_end(*Context); |
| 1048 | I != E; ++I) |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1049 | RewriteMethodDeclaration(*I); |
| 1050 | |
Steve Naroff | 1ccf463 | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1051 | // Lastly, comment out the @end. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1052 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1055 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1056 | SourceRange SrcRange) { |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1057 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1058 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1059 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1060 | ObjCMessageExpr *MsgExpr; |
| 1061 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1062 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1063 | ExprVec.push_back(newStmt); |
| 1064 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1065 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1066 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1067 | if (PRE && PropGetters[PRE]) { |
| 1068 | // This allows us to handle chain/nested property getters. |
| 1069 | Receiver = PropGetters[PRE]; |
| 1070 | } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1071 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1072 | PDecl->getSetterName(), PDecl->getType(), |
| 1073 | PDecl->getSetterMethodDecl(), |
| 1074 | SourceLocation(), SourceLocation(), |
| 1075 | &ExprVec[0], 1); |
| 1076 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
| 1077 | |
| 1078 | // Now do the actual rewrite. |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1079 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | 478f738 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1080 | //delete BinOp; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1081 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1082 | // to things that stay around. |
| 1083 | Context->Deallocate(MsgExpr); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1084 | return ReplacingStmt; |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1087 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1088 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1089 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1090 | ObjCMessageExpr *MsgExpr; |
| 1091 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1092 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1093 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1094 | |
| 1095 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1096 | if (PRE && PropGetters[PRE]) { |
| 1097 | // This allows us to handle chain/nested property getters. |
| 1098 | Receiver = PropGetters[PRE]; |
| 1099 | } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1100 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1101 | PDecl->getGetterName(), PDecl->getType(), |
| 1102 | PDecl->getGetterMethodDecl(), |
| 1103 | SourceLocation(), SourceLocation(), |
| 1104 | 0, 0); |
| 1105 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1106 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (!PropParentMap) |
| 1109 | PropParentMap = new ParentMap(CurrentBody); |
| 1110 | |
| 1111 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1112 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1113 | // We stash away the ReplacingStmt since actually doing the |
| 1114 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1115 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1116 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1117 | // to things that stay around. |
| 1118 | Context->Deallocate(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1119 | return PropRefExpr; // return the original... |
| 1120 | } else { |
| 1121 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1122 | // delete PropRefExpr; elsewhere... |
| 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 ReplacingStmt; |
| 1127 | } |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1130 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
| 1131 | SourceLocation OrigStart) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1132 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1133 | if (CurMethodDef) { |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1134 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1135 | ObjCInterfaceType *iFaceDecl = |
| 1136 | dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1137 | // lookup which class implements the instance variable. |
| 1138 | ObjCInterfaceDecl *clsDeclared = 0; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1139 | iFaceDecl->getDecl()->lookupInstanceVariable(*Context, |
| 1140 | D->getIdentifier(), |
| 1141 | clsDeclared); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1142 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1143 | |
| 1144 | // Synthesize an explicit cast to gain access to the ivar. |
| 1145 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1146 | RecName += "_IMPL"; |
| 1147 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1148 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1149 | SourceLocation(), II); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1150 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1151 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1152 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), |
| 1153 | castT,SourceLocation(), |
| 1154 | SourceLocation()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1155 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1156 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1157 | IV->getBase()->getLocEnd(), |
| 1158 | castExpr); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1159 | if (IV->isFreeIvar() && |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1160 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1161 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1162 | IV->getLocation(), |
| 1163 | D->getType()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1164 | ReplaceStmt(IV, ME); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1165 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1166 | return ME; |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1167 | } |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1168 | |
| 1169 | ReplaceStmt(IV->getBase(), PE); |
| 1170 | // Cannot delete IV->getBase(), since PE points to it. |
| 1171 | // Replace the old base with the cast. This is important when doing |
| 1172 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1173 | IV->setBase(PE); |
| 1174 | return IV; |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1175 | } |
Steve Naroff | e615536 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1176 | } else { // we are outside a method. |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1177 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 1178 | |
| 1179 | // Explicit ivar refs need to have a cast inserted. |
| 1180 | // FIXME: consider sharing some of this code with the code above. |
| 1181 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1182 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1183 | // lookup which class implements the instance variable. |
| 1184 | ObjCInterfaceDecl *clsDeclared = 0; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1185 | iFaceDecl->getDecl()->lookupInstanceVariable(*Context, |
| 1186 | D->getIdentifier(), |
| 1187 | clsDeclared); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1188 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1189 | |
| 1190 | // Synthesize an explicit cast to gain access to the ivar. |
| 1191 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1192 | RecName += "_IMPL"; |
| 1193 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1194 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1195 | SourceLocation(), II); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1196 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1197 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1198 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), |
| 1199 | castT, SourceLocation(), |
| 1200 | SourceLocation()); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1201 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1202 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 3cca661 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1203 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1204 | ReplaceStmt(IV->getBase(), PE); |
| 1205 | // Cannot delete IV->getBase(), since PE points to it. |
| 1206 | // Replace the old base with the cast. This is important when doing |
| 1207 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1208 | IV->setBase(PE); |
| 1209 | return IV; |
| 1210 | } |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1211 | } |
Steve Naroff | e615536 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1212 | return IV; |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1215 | /// SynthCountByEnumWithState - To print: |
| 1216 | /// ((unsigned int (*) |
| 1217 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1218 | /// (void *)objc_msgSend)((id)l_collection, |
| 1219 | /// sel_registerName( |
| 1220 | /// "countByEnumeratingWithState:objects:count:"), |
| 1221 | /// &enumState, |
| 1222 | /// (id *)items, (unsigned int)16) |
| 1223 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1224 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1225 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1226 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1227 | buf += "\n\t\t"; |
| 1228 | buf += "((id)l_collection,\n\t\t"; |
| 1229 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1230 | buf += "\n\t\t"; |
| 1231 | buf += "&enumState, " |
| 1232 | "(id *)items, (unsigned int)16)"; |
| 1233 | } |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1234 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1235 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1236 | /// statement to exit to its outer synthesized loop. |
| 1237 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1238 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1239 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1240 | return S; |
| 1241 | // replace break with goto __break_label |
| 1242 | std::string buf; |
| 1243 | |
| 1244 | SourceLocation startLoc = S->getLocStart(); |
| 1245 | buf = "goto __break_label_"; |
| 1246 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1247 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1253 | /// statement to continue with its inner synthesized loop. |
| 1254 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1255 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1256 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1257 | return S; |
| 1258 | // replace continue with goto __continue_label |
| 1259 | std::string buf; |
| 1260 | |
| 1261 | SourceLocation startLoc = S->getLocStart(); |
| 1262 | buf = "goto __continue_label_"; |
| 1263 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1264 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1269 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1270 | /// It rewrites: |
| 1271 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 1272 | |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1273 | /// Into: |
| 1274 | /// { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1275 | /// type elem; |
| 1276 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1277 | /// id items[16]; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1278 | /// id l_collection = (id)collection; |
| 1279 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1280 | /// objects:items count:16]; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1281 | /// if (limit) { |
| 1282 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1283 | /// do { |
| 1284 | /// unsigned long counter = 0; |
| 1285 | /// do { |
| 1286 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1287 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1288 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1289 | /// stmts; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1290 | /// __continue_label: ; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1291 | /// } while (counter < limit); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1292 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1293 | /// objects:items count:16]); |
| 1294 | /// elem = nil; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1295 | /// __break_label: ; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1296 | /// } |
| 1297 | /// else |
| 1298 | /// elem = nil; |
| 1299 | /// } |
| 1300 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1301 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 2c02216 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1302 | SourceLocation OrigEnd) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1303 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1304 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1305 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1306 | assert(!ObjCBcLabelNo.empty() && |
| 1307 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1308 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1309 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1310 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1311 | const char *elementName; |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1312 | std::string elementTypeAsString; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1313 | std::string buf; |
| 1314 | buf = "\n{\n\t"; |
| 1315 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1316 | // type elem; |
Chris Lattner | 4a9a85e | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1317 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 91a2bd3 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1318 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1319 | elementTypeAsString = ElementType.getAsString(); |
| 1320 | buf += elementTypeAsString; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1321 | buf += " "; |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1322 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1323 | buf += elementName; |
| 1324 | buf += ";\n\t"; |
| 1325 | } |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1326 | else { |
| 1327 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1328 | elementName = DR->getDecl()->getNameAsCString(); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1329 | elementTypeAsString |
| 1330 | = cast<ValueDecl>(DR->getDecl())->getType().getAsString(); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1331 | } |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1332 | |
| 1333 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1334 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1335 | // id items[16]; |
| 1336 | buf += "id items[16];\n\t"; |
| 1337 | // id l_collection = (id) |
| 1338 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1339 | // Find start location of 'collection' the hard way! |
| 1340 | const char *startCollectionBuf = startBuf; |
| 1341 | startCollectionBuf += 3; // skip 'for' |
| 1342 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1343 | startCollectionBuf++; // skip '(' |
| 1344 | // find 'in' and skip it. |
| 1345 | while (*startCollectionBuf != ' ' || |
| 1346 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1347 | (*(startCollectionBuf+3) != ' ' && |
| 1348 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1349 | startCollectionBuf++; |
| 1350 | startCollectionBuf += 3; |
| 1351 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1352 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1353 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1354 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1355 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1356 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1357 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1358 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1359 | buf = ";\n\t"; |
| 1360 | |
| 1361 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1362 | // objects:items count:16]; |
| 1363 | // which is synthesized into: |
| 1364 | // unsigned int limit = |
| 1365 | // ((unsigned int (*) |
| 1366 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1367 | // (void *)objc_msgSend)((id)l_collection, |
| 1368 | // sel_registerName( |
| 1369 | // "countByEnumeratingWithState:objects:count:"), |
| 1370 | // (struct __objcFastEnumerationState *)&state, |
| 1371 | // (id *)items, (unsigned int)16); |
| 1372 | buf += "unsigned long limit =\n\t\t"; |
| 1373 | SynthCountByEnumWithState(buf); |
| 1374 | buf += ";\n\t"; |
| 1375 | /// if (limit) { |
| 1376 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1377 | /// do { |
| 1378 | /// unsigned long counter = 0; |
| 1379 | /// do { |
| 1380 | /// if (startMutations != *enumState.mutationsPtr) |
| 1381 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1382 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1383 | buf += "if (limit) {\n\t"; |
| 1384 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1385 | buf += "do {\n\t\t"; |
| 1386 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1387 | buf += "do {\n\t\t\t"; |
| 1388 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1389 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1390 | buf += elementName; |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1391 | buf += " = ("; |
| 1392 | buf += elementTypeAsString; |
| 1393 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1394 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1395 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1396 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1397 | /// __continue_label: ; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1398 | /// } while (counter < limit); |
| 1399 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1400 | /// objects:items count:16]); |
| 1401 | /// elem = nil; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1402 | /// __break_label: ; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1403 | /// } |
| 1404 | /// else |
| 1405 | /// elem = nil; |
| 1406 | /// } |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1407 | /// |
| 1408 | buf = ";\n\t"; |
| 1409 | buf += "__continue_label_"; |
| 1410 | buf += utostr(ObjCBcLabelNo.back()); |
| 1411 | buf += ": ;"; |
| 1412 | buf += "\n\t\t"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1413 | buf += "} while (counter < limit);\n\t"; |
| 1414 | buf += "} while (limit = "; |
| 1415 | SynthCountByEnumWithState(buf); |
| 1416 | buf += ");\n\t"; |
| 1417 | buf += elementName; |
Steve Naroff | a9dc026 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1418 | buf += " = ((id)0);\n\t"; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1419 | buf += "__break_label_"; |
| 1420 | buf += utostr(ObjCBcLabelNo.back()); |
| 1421 | buf += ": ;\n\t"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1422 | buf += "}\n\t"; |
| 1423 | buf += "else\n\t\t"; |
| 1424 | buf += elementName; |
Steve Naroff | a9dc026 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1425 | buf += " = ((id)0);\n"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1426 | buf += "}\n"; |
Steve Naroff | 5f238fe | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1427 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1428 | // Insert all these *after* the statement body. |
Steve Naroff | 5f238fe | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1429 | if (isa<CompoundStmt>(S->getBody())) { |
| 1430 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1431 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1432 | } else { |
| 1433 | /* Need to treat single statements specially. For example: |
| 1434 | * |
| 1435 | * for (A *a in b) if (stuff()) break; |
| 1436 | * for (A *a in b) xxxyy; |
| 1437 | * |
| 1438 | * The following code simply scans ahead to the semi to find the actual end. |
| 1439 | */ |
| 1440 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1441 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1442 | assert(semiBuf && "Can't find ';'"); |
| 1443 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1444 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1445 | } |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1446 | Stmts.pop_back(); |
| 1447 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1448 | return 0; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1451 | /// RewriteObjCSynchronizedStmt - |
| 1452 | /// This routine rewrites @synchronized(expr) stmt; |
| 1453 | /// into: |
| 1454 | /// objc_sync_enter(expr); |
| 1455 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1456 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1457 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1458 | // Get the start location and compute the semi location. |
| 1459 | SourceLocation startLoc = S->getLocStart(); |
| 1460 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1461 | |
| 1462 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1463 | |
| 1464 | std::string buf; |
Steve Naroff | c1656a6 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1465 | buf = "objc_sync_enter((id)"; |
| 1466 | const char *lparenBuf = startBuf; |
| 1467 | while (*lparenBuf != '(') lparenBuf++; |
| 1468 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1469 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1470 | // the sync expression is typically a message expression that's already |
| 1471 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1472 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1473 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1474 | while (*endBuf != ')') endBuf--; |
| 1475 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1476 | buf = ");\n"; |
| 1477 | // declare a new scope with two variables, _stack and _rethrow. |
| 1478 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1479 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1480 | buf += "char *pointers[4];} _stack;\n"; |
| 1481 | buf += "id volatile _rethrow = 0;\n"; |
| 1482 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1483 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1484 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1485 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1486 | startBuf = SM->getCharacterData(startLoc); |
| 1487 | |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1488 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1489 | SourceLocation lastCurlyLoc = startLoc; |
| 1490 | buf = "}\nelse {\n"; |
| 1491 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1492 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1493 | buf += " objc_sync_exit("; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1494 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 1495 | S->getSynchExpr(), |
| 1496 | Context->getObjCIdType(), |
| 1497 | SourceLocation(), |
| 1498 | SourceLocation()); |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1499 | std::string syncExprBufS; |
| 1500 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Steve Naroff | c1656a6 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1501 | syncExpr->printPretty(syncExprBuf); |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1502 | buf += syncExprBuf.str(); |
| 1503 | buf += ");\n"; |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1504 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1505 | buf += "}\n"; |
| 1506 | buf += "}"; |
| 1507 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1508 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1509 | return 0; |
| 1510 | } |
| 1511 | |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1512 | void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) { |
| 1513 | // Perform a bottom up traversal of all children. |
| 1514 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1515 | CI != E; ++CI) |
| 1516 | if (*CI) |
| 1517 | WarnAboutReturnGotoContinueOrBreakStmts(*CI); |
| 1518 | |
| 1519 | if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || |
| 1520 | isa<BreakStmt>(S) || isa<GotoStmt>(S)) { |
| 1521 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1522 | TryFinallyContainsReturnDiag); |
| 1523 | } |
| 1524 | return; |
| 1525 | } |
| 1526 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1527 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1528 | // Get the start location and compute the semi location. |
| 1529 | SourceLocation startLoc = S->getLocStart(); |
| 1530 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1531 | |
| 1532 | assert((*startBuf == '@') && "bogus @try location"); |
| 1533 | |
| 1534 | std::string buf; |
| 1535 | // declare a new scope with two variables, _stack and _rethrow. |
| 1536 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1537 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1538 | buf += "char *pointers[4];} _stack;\n"; |
| 1539 | buf += "id volatile _rethrow = 0;\n"; |
| 1540 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1541 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1543 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1544 | |
| 1545 | startLoc = S->getTryBody()->getLocEnd(); |
| 1546 | startBuf = SM->getCharacterData(startLoc); |
| 1547 | |
| 1548 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1549 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1550 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1551 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1552 | if (catchList) { |
| 1553 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1554 | buf = " /* @catch begin */ else {\n"; |
| 1555 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1556 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1557 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1558 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1559 | buf += " else { /* @catch continue */"; |
| 1560 | |
| 1561 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 36269d2 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1562 | } else { /* no catch list */ |
| 1563 | buf = "}\nelse {\n"; |
| 1564 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1565 | buf += "}"; |
| 1566 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1567 | } |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1568 | bool sawIdTypedCatch = false; |
| 1569 | Stmt *lastCatchBody = 0; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1570 | while (catchList) { |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1571 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1572 | |
| 1573 | if (catchList == S->getCatchStmts()) |
| 1574 | buf = "if ("; // we are generating code for the first catch clause |
| 1575 | else |
| 1576 | buf = "else if ("; |
| 1577 | startLoc = catchList->getLocStart(); |
| 1578 | startBuf = SM->getCharacterData(startLoc); |
| 1579 | |
| 1580 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1581 | |
| 1582 | const char *lParenLoc = strchr(startBuf, '('); |
| 1583 | |
Steve Naroff | 397c4ce | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1584 | if (catchList->hasEllipsis()) { |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1585 | // Now rewrite the body... |
| 1586 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1587 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1588 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1589 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1590 | "bogus @catch paren location"); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1591 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1592 | |
| 1593 | buf += "1) { id _tmp = _caught;"; |
| 1594 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1595 | buf.c_str(), buf.size()); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1596 | } else if (catchDecl) { |
| 1597 | QualType t = catchDecl->getType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1598 | if (t == Context->getObjCIdType()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1599 | buf += "1) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1600 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1601 | sawIdTypedCatch = true; |
| 1602 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1603 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1604 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1605 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1606 | if (cls) { |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1607 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1608 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1609 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1610 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | // Now rewrite the body... |
| 1614 | lastCatchBody = catchList->getCatchBody(); |
| 1615 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1616 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1617 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1618 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1619 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1620 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1621 | |
| 1622 | buf = " = _caught;"; |
| 1623 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1624 | // declares the @catch parameter). |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1625 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1626 | } else { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1627 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1628 | } |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1629 | // make sure all the catch bodies get rewritten! |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1630 | catchList = catchList->getNextCatchStmt(); |
| 1631 | } |
| 1632 | // Complete the catch list... |
| 1633 | if (lastCatchBody) { |
| 1634 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1635 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1636 | "bogus @catch body location"); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1637 | |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1638 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1639 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1640 | buf = "} /* last catch end */\n"; |
| 1641 | buf += "else {\n"; |
| 1642 | buf += " _rethrow = _caught;\n"; |
| 1643 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1644 | buf += "} } /* @catch end */\n"; |
| 1645 | if (!S->getFinallyStmt()) |
| 1646 | buf += "}\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1647 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1648 | |
| 1649 | // Set lastCurlyLoc |
| 1650 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1651 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1652 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1653 | startLoc = finalStmt->getLocStart(); |
| 1654 | startBuf = SM->getCharacterData(startLoc); |
| 1655 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1656 | |
| 1657 | buf = "/* @finally */"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1658 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1659 | |
| 1660 | Stmt *body = finalStmt->getFinallyBody(); |
| 1661 | SourceLocation startLoc = body->getLocStart(); |
| 1662 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1663 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1664 | "bogus @finally body location"); |
| 1665 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1666 | "bogus @finally body location"); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1667 | |
| 1668 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1669 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1670 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1671 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1672 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1673 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1674 | |
| 1675 | // Set lastCurlyLoc |
| 1676 | lastCurlyLoc = body->getLocEnd(); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Now check for any return/continue/go statements within the @try. |
| 1679 | WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody()); |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1680 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1681 | buf = "{ /* implicit finally clause */\n"; |
| 1682 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1683 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1684 | buf += "}"; |
| 1685 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1686 | } |
| 1687 | // Now emit the final closing curly brace... |
| 1688 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1689 | buf = " } /* @try scope end */\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1690 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1694 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1695 | return 0; |
| 1696 | } |
| 1697 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1698 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1699 | return 0; |
| 1700 | } |
| 1701 | |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1702 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1703 | // the throw expression is typically a message expression that's already |
| 1704 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1705 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1706 | // Get the start location and compute the semi location. |
| 1707 | SourceLocation startLoc = S->getLocStart(); |
| 1708 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1709 | |
| 1710 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1711 | |
| 1712 | std::string buf; |
| 1713 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | be72efa | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1714 | if (S->getThrowExpr()) |
| 1715 | buf = "objc_exception_throw("; |
| 1716 | else // add an implicit argument |
| 1717 | buf = "objc_exception_throw(_caught"; |
Steve Naroff | 3de6eaa | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1718 | |
| 1719 | // handle "@ throw" correctly. |
| 1720 | const char *wBuf = strchr(startBuf, 'w'); |
| 1721 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1722 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
| 1723 | |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1724 | const char *semiBuf = strchr(startBuf, ';'); |
| 1725 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1726 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1727 | buf = ");"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1728 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1729 | return 0; |
| 1730 | } |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1731 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1732 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | bf0bfa6 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1733 | // Create a new string expression. |
| 1734 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1735 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1736 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1737 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1738 | StrEncoding.length(), false,StrType, |
| 1739 | SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1740 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 4478db9 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1742 | // Replace this subexpr in the parent. |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1743 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1744 | return Replacement; |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1747 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 95f6bce | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1748 | if (!SelGetUidFunctionDecl) |
| 1749 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1750 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1751 | // Create a call to sel_registerName("selName"). |
| 1752 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1753 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1754 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1755 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1756 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1757 | false, argType, SourceLocation())); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1758 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1759 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1760 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1761 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1762 | return SelExp; |
| 1763 | } |
| 1764 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1765 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1766 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1767 | // 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] | 1768 | QualType msgSendType = FD->getType(); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1769 | |
| 1770 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1771 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1772 | |
| 1773 | // 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] | 1774 | QualType pToFunc = Context->getPointerType(msgSendType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1775 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, DRE, |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1776 | /*isLvalue=*/false); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1777 | |
| 1778 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1779 | |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1780 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1781 | SourceLocation()); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1784 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1785 | const char *&startRef, const char *&endRef) { |
| 1786 | while (startBuf < endBuf) { |
| 1787 | if (*startBuf == '<') |
| 1788 | startRef = startBuf; // mark the start. |
| 1789 | if (*startBuf == '>') { |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1790 | if (startRef && *startRef == '<') { |
| 1791 | endRef = startBuf; // mark the end. |
| 1792 | return true; |
| 1793 | } |
| 1794 | return false; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1795 | } |
| 1796 | startBuf++; |
| 1797 | } |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1801 | static void scanToNextArgument(const char *&argRef) { |
| 1802 | int angle = 0; |
| 1803 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1804 | if (*argRef == '<') |
| 1805 | angle++; |
| 1806 | else if (*argRef == '>') |
| 1807 | angle--; |
| 1808 | argRef++; |
| 1809 | } |
| 1810 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1811 | } |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1812 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1813 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1814 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1815 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1816 | return true; |
| 1817 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1818 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1819 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1820 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1821 | return true; // we have "Class <Protocol> *". |
| 1822 | } |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1823 | return false; |
| 1824 | } |
| 1825 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1826 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1827 | QualType Type = E->getType(); |
| 1828 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1829 | SourceLocation Loc, EndLoc; |
| 1830 | |
| 1831 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1832 | Loc = ECE->getLParenLoc(); |
| 1833 | EndLoc = ECE->getRParenLoc(); |
| 1834 | } else { |
| 1835 | Loc = E->getLocStart(); |
| 1836 | EndLoc = E->getLocEnd(); |
| 1837 | } |
| 1838 | // This will defend against trying to rewrite synthesized expressions. |
| 1839 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1840 | return; |
| 1841 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1842 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1843 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1844 | const char *startRef = 0, *endRef = 0; |
| 1845 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1846 | // Get the locations of the startRef, endRef. |
| 1847 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1848 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1849 | // Comment out the protocol references. |
| 1850 | InsertText(LessLoc, "/*", 2); |
| 1851 | InsertText(GreaterLoc, "*/", 2); |
| 1852 | } |
| 1853 | } |
| 1854 | } |
| 1855 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1856 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1857 | SourceLocation Loc; |
| 1858 | QualType Type; |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1859 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1860 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1861 | Loc = VD->getLocation(); |
| 1862 | Type = VD->getType(); |
| 1863 | } |
| 1864 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1865 | Loc = FD->getLocation(); |
| 1866 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1867 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1868 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1869 | assert(funcType && "missing function type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1870 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1871 | if (!proto) |
| 1872 | return; |
| 1873 | Type = proto->getResultType(); |
| 1874 | } |
| 1875 | else |
| 1876 | return; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1877 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1878 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1879 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1880 | |
| 1881 | const char *endBuf = SM->getCharacterData(Loc); |
| 1882 | const char *startBuf = endBuf; |
Steve Naroff | ff2fa19 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1883 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1884 | startBuf--; // scan backward (from the decl location) for return type. |
| 1885 | const char *startRef = 0, *endRef = 0; |
| 1886 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1887 | // Get the locations of the startRef, endRef. |
| 1888 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1889 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1890 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1891 | InsertText(LessLoc, "/*", 2); |
| 1892 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1895 | if (!proto) |
| 1896 | return; // most likely, was a variable |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1897 | // Now check arguments. |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1898 | const char *startBuf = SM->getCharacterData(Loc); |
| 1899 | const char *startFuncBuf = startBuf; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1900 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1901 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1902 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1903 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1904 | const char *endBuf = startBuf; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1905 | // scan forward (from the decl location) for argument types. |
| 1906 | scanToNextArgument(endBuf); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1907 | const char *startRef = 0, *endRef = 0; |
| 1908 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1909 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1910 | SourceLocation LessLoc = |
| 1911 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1912 | SourceLocation GreaterLoc = |
| 1913 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1914 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1915 | InsertText(LessLoc, "/*", 2); |
| 1916 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1917 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1918 | startBuf = ++endBuf; |
| 1919 | } |
| 1920 | else { |
Steve Naroff | 6a1d88b | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1921 | // If the function name is derived from a macro expansion, then the |
| 1922 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1923 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1924 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1925 | startBuf++; |
| 1926 | } |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1927 | } |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1930 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1931 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1932 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1933 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1934 | ArgTys.push_back(Context->getPointerType( |
| 1935 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1936 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1937 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1938 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1939 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1940 | SourceLocation(), |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1941 | SelGetUidIdent, getFuncType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1942 | FunctionDecl::Extern, false); |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1945 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1946 | void RewriteObjC::SynthGetProtocolFunctionDecl() { |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1947 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1948 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1949 | ArgTys.push_back(Context->getPointerType( |
| 1950 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1951 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1952 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1953 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1954 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1955 | SourceLocation(), |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1956 | SelGetProtoIdent, getFuncType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1957 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1960 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1961 | // declared in <objc/objc.h> |
Douglas Gregor | 7339c9e | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 1962 | if (FD->getIdentifier() && |
| 1963 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1964 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1965 | return; |
| 1966 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1967 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1970 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1971 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1972 | if (SuperContructorFunctionDecl) |
| 1973 | return; |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 1974 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1975 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1976 | QualType argT = Context->getObjCIdType(); |
| 1977 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1978 | ArgTys.push_back(argT); |
| 1979 | ArgTys.push_back(argT); |
| 1980 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1981 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1982 | false, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1983 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1984 | SourceLocation(), |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1985 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1986 | FunctionDecl::Extern, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1989 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1990 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1991 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1992 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1993 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1994 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1995 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1996 | argT = Context->getObjCSelType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1997 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1998 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1999 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2000 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2001 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2002 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2003 | SourceLocation(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2004 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2005 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2008 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2009 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2010 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2011 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2012 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2013 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2014 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2015 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2016 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2017 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2018 | argT = Context->getObjCSelType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2019 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2020 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2021 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2022 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2023 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2024 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2025 | SourceLocation(), |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2026 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2027 | FunctionDecl::Extern, false); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2030 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2031 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2032 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2033 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2034 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2035 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2036 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2037 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2038 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2039 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2040 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2041 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2042 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2043 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2044 | SourceLocation(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2045 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2046 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | // SynthMsgSendSuperStretFunctionDecl - |
| 2050 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2051 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2052 | IdentifierInfo *msgSendIdent = |
| 2053 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2054 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2055 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2056 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2057 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2058 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2059 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2060 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2061 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2062 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2063 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2064 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2065 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2066 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2067 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 2068 | SourceLocation(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2069 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2070 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2073 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2074 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2075 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2076 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2077 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2078 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2079 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2080 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2081 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2082 | ArgTys.push_back(argT); |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2083 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2084 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2085 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2086 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2087 | SourceLocation(), |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2088 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2089 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2092 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2093 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2094 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2095 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2096 | ArgTys.push_back(Context->getPointerType( |
| 2097 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2098 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2099 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2100 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2101 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2102 | SourceLocation(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2103 | getClassIdent, getClassType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2104 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2107 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2108 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2109 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2110 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2111 | ArgTys.push_back(Context->getPointerType( |
| 2112 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2113 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2114 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2115 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2116 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2117 | SourceLocation(), |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2118 | getClassIdent, getClassType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2119 | FunctionDecl::Extern, false); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2122 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2123 | QualType strType = getConstantStringStructType(); |
| 2124 | |
| 2125 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a1f0099 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2126 | |
| 2127 | std::string tmpName = InFileName; |
| 2128 | unsigned i; |
| 2129 | for (i=0; i < tmpName.length(); i++) { |
| 2130 | char c = tmpName.at(i); |
| 2131 | // replace any non alphanumeric characters with '_'. |
| 2132 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2133 | tmpName[i] = '_'; |
| 2134 | } |
| 2135 | S += tmpName; |
| 2136 | S += "_"; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2137 | S += utostr(NumObjCStringLiterals++); |
| 2138 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2139 | Preamble += "static __NSConstantStringImpl " + S; |
| 2140 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2141 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2142 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2143 | std::string prettyBufS; |
| 2144 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2145 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2146 | Preamble += prettyBuf.str(); |
| 2147 | Preamble += ","; |
Steve Naroff | 64bce35 | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2148 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2149 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2150 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2151 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2152 | &Context->Idents.get(S.c_str()), strType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2153 | VarDecl::Static); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2154 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2155 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2156 | Context->getPointerType(DRE->getType()), |
| 2157 | SourceLocation()); |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2158 | // cast to NSConstantString * |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2159 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), Unop, |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2160 | Exp->getType(), SourceLocation(), SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2161 | ReplaceStmt(Exp, cast); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2162 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2163 | return cast; |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2166 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2167 | // check if we are sending a message to 'super' |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2168 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | d860663 | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2170 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
| 2171 | const PointerType *PT = Super->getType()->getAsPointerType(); |
Chris Lattner | 4423d37 | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2172 | assert(PT); |
| 2173 | ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 2174 | return IT->getDecl(); |
| 2175 | } |
| 2176 | return 0; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2180 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2181 | if (!SuperStructDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2182 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2183 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2184 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2185 | QualType FieldTypes[2]; |
| 2186 | |
| 2187 | // struct objc_object *receiver; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2188 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2189 | // struct objc_class *super; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2190 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2191 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2192 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2193 | for (unsigned i = 0; i < 2; ++i) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2194 | SuperStructDecl->addDecl(*Context, |
| 2195 | FieldDecl::Create(*Context, SuperStructDecl, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2196 | SourceLocation(), 0, |
| 2197 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2198 | /*Mutable=*/false)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2199 | } |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2200 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2201 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2202 | } |
| 2203 | return Context->getTagDeclType(SuperStructDecl); |
| 2204 | } |
| 2205 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2206 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2207 | if (!ConstantStringDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2208 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2209 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2210 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2211 | QualType FieldTypes[4]; |
| 2212 | |
| 2213 | // struct objc_object *receiver; |
| 2214 | FieldTypes[0] = Context->getObjCIdType(); |
| 2215 | // int flags; |
| 2216 | FieldTypes[1] = Context->IntTy; |
| 2217 | // char *str; |
| 2218 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2219 | // long length; |
| 2220 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2221 | |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2222 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2223 | for (unsigned i = 0; i < 4; ++i) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2224 | ConstantStringDecl->addDecl(*Context, |
| 2225 | FieldDecl::Create(*Context, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2226 | ConstantStringDecl, |
| 2227 | SourceLocation(), 0, |
| 2228 | FieldTypes[i], |
| 2229 | /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2230 | /*Mutable=*/true)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2234 | } |
| 2235 | return Context->getTagDeclType(ConstantStringDecl); |
| 2236 | } |
| 2237 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2238 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2239 | if (!SelGetUidFunctionDecl) |
| 2240 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2241 | if (!MsgSendFunctionDecl) |
| 2242 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2243 | if (!MsgSendSuperFunctionDecl) |
| 2244 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2245 | if (!MsgSendStretFunctionDecl) |
| 2246 | SynthMsgSendStretFunctionDecl(); |
| 2247 | if (!MsgSendSuperStretFunctionDecl) |
| 2248 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2249 | if (!MsgSendFpretFunctionDecl) |
| 2250 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2251 | if (!GetClassFunctionDecl) |
| 2252 | SynthGetClassFunctionDecl(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2253 | if (!GetMetaClassFunctionDecl) |
| 2254 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2255 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2256 | // default to objc_msgSend(). |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2257 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2258 | // May need to use objc_msgSend_stret() as well. |
| 2259 | FunctionDecl *MsgSendStretFlavor = 0; |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2260 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 2261 | QualType resultType = OMD->getResultType(); |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2262 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2263 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2264 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2265 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2266 | } |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2267 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2268 | // Synthesize a call to objc_msgSend(). |
| 2269 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2270 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2271 | |
| 2272 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2273 | if (clsName) { // class message. |
Steve Naroff | 91a6920 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2274 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2275 | // the 'super' idiom within a class method. |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2276 | if (!strcmp(clsName->getName(), "super")) { |
| 2277 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2278 | if (MsgSendStretFlavor) |
| 2279 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2280 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2281 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2282 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2283 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2284 | |
| 2285 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2286 | |
| 2287 | // set the receiver to self, the first argument to all methods. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2288 | InitExprs.push_back(new (Context) DeclRefExpr( |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2289 | CurMethodDef->getSelfDecl(), |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2290 | Context->getObjCIdType(), |
| 2291 | SourceLocation())); |
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, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2295 | SuperDecl->getIdentifier()->getName(), |
| 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, |
| 2299 | &ClsExprs[0], |
| 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(). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2303 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2304 | Cls, Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2305 | SourceLocation(), SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2306 | // struct objc_super |
| 2307 | QualType superType = getSuperStructType(); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2308 | Expr *SuperRep; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2309 | |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2310 | if (LangOpts.Microsoft) { |
| 2311 | SynthSuperContructorFunctionDecl(); |
| 2312 | // Simulate a contructor call... |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2313 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2314 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2315 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2316 | InitExprs.size(), |
| 2317 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2318 | // The code for super is a little tricky to prevent collision with |
| 2319 | // the structure definition in the header. The rewriter has it's own |
| 2320 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2321 | // we need the cast below. For example: |
| 2322 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2323 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2324 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2325 | Context->getPointerType(SuperRep->getType()), |
| 2326 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2327 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2328 | SuperRep, Context->getPointerType(superType), |
| 2329 | SourceLocation(), SourceLocation()); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2330 | } else { |
| 2331 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2332 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2333 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2334 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2335 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2336 | false); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2337 | // struct objc_super * |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2338 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2339 | Context->getPointerType(SuperRep->getType()), |
| 2340 | SourceLocation()); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2341 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2342 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2343 | } else { |
| 2344 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2345 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2346 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2347 | clsName->getName(), |
| 2348 | clsName->getLength(), |
| 2349 | false, argType, |
| 2350 | SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2351 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2352 | &ClsExprs[0], |
| 2353 | ClsExprs.size()); |
| 2354 | MsgExprs.push_back(Cls); |
| 2355 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2356 | } else { // instance message. |
| 2357 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2358 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2359 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2360 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2361 | if (MsgSendStretFlavor) |
| 2362 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2363 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2364 | |
| 2365 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2366 | |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2367 | InitExprs.push_back( |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2368 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 2369 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 2352861 | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2370 | Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2371 | SourceLocation()), |
| 2372 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2373 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2374 | |
| 2375 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2376 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2377 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2378 | SuperDecl->getIdentifier()->getName(), |
| 2379 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2380 | false, argType, SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2381 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2382 | &ClsExprs[0], |
| 2383 | ClsExprs.size()); |
Fariborz Jahanian | fabf3bf | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2384 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2385 | InitExprs.push_back( |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2386 | // set 'super class', using objc_getClass(). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2387 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2388 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2389 | // struct objc_super |
| 2390 | QualType superType = getSuperStructType(); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2391 | Expr *SuperRep; |
| 2392 | |
| 2393 | if (LangOpts.Microsoft) { |
| 2394 | SynthSuperContructorFunctionDecl(); |
| 2395 | // Simulate a contructor call... |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2396 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2397 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2398 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2399 | InitExprs.size(), |
| 2400 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2401 | // The code for super is a little tricky to prevent collision with |
| 2402 | // the structure definition in the header. The rewriter has it's own |
| 2403 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2404 | // we need the cast below. For example: |
| 2405 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2406 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2407 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2408 | Context->getPointerType(SuperRep->getType()), |
| 2409 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2410 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2411 | SuperRep, Context->getPointerType(superType), |
| 2412 | SourceLocation(), SourceLocation()); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2413 | } else { |
| 2414 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2415 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2416 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2417 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2418 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2419 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2420 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2421 | } else { |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2422 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2423 | // Foo<Proto> *. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2424 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2425 | recExpr = CE->getSubExpr(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2426 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), recExpr, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2427 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2428 | SourceLocation(), SourceLocation()); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2429 | MsgExprs.push_back(recExpr); |
| 2430 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2431 | } |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2432 | // 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] | 2433 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2434 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2435 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2436 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2437 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2438 | false, argType, SourceLocation())); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2439 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2440 | &SelExprs[0], SelExprs.size()); |
| 2441 | MsgExprs.push_back(SelExp); |
| 2442 | |
| 2443 | // Now push any user supplied arguments. |
| 2444 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2445 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2446 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2447 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2448 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2449 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2450 | ? Context->getObjCIdType() |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2451 | : ICE->getType(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2452 | userExpr = new (Context) CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2453 | } |
| 2454 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2455 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2456 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2457 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2458 | userExpr = CE->getSubExpr(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2459 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2460 | userExpr, Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2461 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2462 | } |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2463 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2464 | MsgExprs.push_back(userExpr); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2465 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2466 | // the original expression, since we will delete it below. |
| 2467 | Exp->setArg(i, 0); |
| 2468 | } |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2469 | // Generate the funky cast. |
| 2470 | CastExpr *cast; |
| 2471 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2472 | QualType returnType; |
| 2473 | |
| 2474 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 0c04bb6 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2475 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2476 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2477 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2478 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2479 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2480 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2481 | // Push any user argument types. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2482 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2483 | E = OMD->param_end(); PI != E; ++PI) { |
| 2484 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2485 | ? Context->getObjCIdType() |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2486 | : (*PI)->getType(); |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2487 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2488 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2489 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 2490 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2491 | } |
Steve Naroff | 4242b97 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2492 | ArgTypes.push_back(t); |
| 2493 | } |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2494 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2495 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2496 | } else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2497 | returnType = Context->getObjCIdType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2498 | } |
| 2499 | // 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] | 2500 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2501 | |
| 2502 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2503 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2504 | SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2505 | |
| 2506 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2507 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2508 | // xx.m:13: warning: function called through a non-compatible type |
| 2509 | // xx.m:13: note: if this code is reached, the program will abort |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2510 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2511 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2512 | SourceLocation(), SourceLocation()); |
Steve Naroff | 29fe746 | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2513 | |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2514 | // Now do the "normal" pointer to function cast. |
| 2515 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2516 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 3b8b4e3 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2517 | // If we don't have a method decl, force a variadic cast. |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2518 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2519 | castType = Context->getPointerType(castType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2520 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2521 | |
| 2522 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2523 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2524 | |
| 2525 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2526 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2527 | MsgExprs.size(), |
| 2528 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2529 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2530 | if (MsgSendStretFlavor) { |
| 2531 | // We have the method which returns a struct/union. Must also generate |
| 2532 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2533 | // expression which dictate which one to envoke depending on size of |
| 2534 | // method's return type. |
| 2535 | |
| 2536 | // Create a reference to the objc_msgSend_stret() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2537 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2538 | SourceLocation()); |
| 2539 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2540 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2541 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2542 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2543 | // Now do the "normal" pointer to function cast. |
| 2544 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2545 | &ArgTypes[0], ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2546 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2547 | castType = Context->getPointerType(castType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2548 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2549 | |
| 2550 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2551 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2552 | |
| 2553 | FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2554 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2555 | MsgExprs.size(), |
| 2556 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2557 | |
| 2558 | // Build sizeof(returnType) |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2559 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
| 2560 | returnType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2561 | Context->getSizeType(), |
| 2562 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2563 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2564 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2565 | // For X86 it is more complicated and some kind of target specific routine |
| 2566 | // is needed to decide what to do. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2567 | unsigned IntSize = |
| 2568 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2569 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2570 | Context->IntTy, |
| 2571 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2572 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2573 | BinaryOperator::LE, |
| 2574 | Context->IntTy, |
| 2575 | SourceLocation()); |
| 2576 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2577 | ConditionalOperator *CondExpr = |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2578 | new (Context) ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
| 2579 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2580 | } |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2581 | return ReplacingStmt; |
| 2582 | } |
| 2583 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2584 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2585 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | e8efe89 | 2008-10-27 20:54:44 +0000 | [diff] [blame] | 2586 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 2587 | //ReplacingStmt->dump(); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2588 | // Now do the actual rewrite. |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2589 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2590 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2591 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2592 | return ReplacingStmt; |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2595 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2596 | /// call to objc_getProtocol("proto-name"). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2597 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2598 | if (!GetProtocolFunctionDecl) |
| 2599 | SynthGetProtocolFunctionDecl(); |
| 2600 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2601 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2602 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2603 | ProtoExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2604 | Exp->getProtocol()->getNameAsCString(), |
| 2605 | strlen(Exp->getProtocol()->getNameAsCString()), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2606 | false, argType, SourceLocation())); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2607 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2608 | &ProtoExprs[0], |
| 2609 | ProtoExprs.size()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2610 | ReplaceStmt(Exp, ProtoExp); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2611 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2612 | return ProtoExp; |
| 2613 | |
| 2614 | } |
| 2615 | |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2616 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2617 | const char *endBuf) { |
| 2618 | while (startBuf < endBuf) { |
| 2619 | if (*startBuf == '#') { |
| 2620 | // Skip whitespace. |
| 2621 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2622 | ; |
| 2623 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2624 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2625 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2626 | !strncmp(startBuf, "define", strlen("define")) || |
| 2627 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2628 | !strncmp(startBuf, "else", strlen("else")) || |
| 2629 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2630 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2631 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2632 | !strncmp(startBuf, "include", strlen("include")) || |
| 2633 | !strncmp(startBuf, "import", strlen("import")) || |
| 2634 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2635 | return true; |
| 2636 | } |
| 2637 | startBuf++; |
| 2638 | } |
| 2639 | return false; |
| 2640 | } |
| 2641 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2642 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2643 | /// an objective-c class with ivars. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2644 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2645 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2646 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2647 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2648 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2649 | // Do not synthesize more than once. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2650 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2651 | return; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2652 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2653 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2654 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2655 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2656 | |
| 2657 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2658 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2659 | |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2660 | // If no ivars and no root or if its root, directly or indirectly, |
| 2661 | // 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] | 2662 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2663 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2664 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2665 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2666 | return; |
| 2667 | } |
| 2668 | |
| 2669 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2670 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2671 | Result += "\nstruct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2672 | Result += CDecl->getNameAsString(); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2673 | if (LangOpts.Microsoft) |
| 2674 | Result += "_IMPL"; |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2675 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2676 | if (NumIvars > 0) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2677 | const char *cursor = strchr(startBuf, '{'); |
| 2678 | assert((cursor && endBuf) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2679 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2680 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2681 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2682 | // NSURL.h, for example): |
| 2683 | // |
| 2684 | // #ifdef XYZ |
| 2685 | // @interface Foo : NSObject |
| 2686 | // #else |
| 2687 | // @interface FooBar : NSObject |
| 2688 | // #endif |
| 2689 | // { |
| 2690 | // int i; |
| 2691 | // } |
| 2692 | // @end |
| 2693 | // |
| 2694 | // This clause is segregated to avoid breaking the common case. |
| 2695 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2696 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2697 | CDecl->getClassLoc(); |
| 2698 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2699 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2700 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2701 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2702 | // advance to the end of the referenced protocols. |
| 2703 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2704 | endHeader++; |
| 2705 | } |
| 2706 | // rewrite the original header |
| 2707 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2708 | } else { |
| 2709 | // rewrite the original header *without* disturbing the '{' |
| 2710 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2711 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2712 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2713 | Result = "\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2714 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2715 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2716 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2717 | Result += "_IVARS;\n"; |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2718 | |
| 2719 | // insert the super class structure definition. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2720 | SourceLocation OnePastCurly = |
| 2721 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2722 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2723 | } |
| 2724 | cursor++; // past '{' |
| 2725 | |
| 2726 | // Now comment out any visibility specifiers. |
| 2727 | while (cursor < endBuf) { |
| 2728 | if (*cursor == '@') { |
| 2729 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f04ead5 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2730 | // Skip whitespace. |
| 2731 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2732 | /*scan*/; |
| 2733 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2734 | // FIXME: presence of @public, etc. inside comment results in |
| 2735 | // this transformation as well, which is still correct c-code. |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2736 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2737 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | a93924a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2738 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2739 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2740 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2741 | } |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2742 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2743 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2744 | // for type checking. |
| 2745 | else if (*cursor == '<') { |
| 2746 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2747 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2748 | cursor = strchr(cursor, '>'); |
| 2749 | cursor++; |
| 2750 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2751 | InsertText(atLoc, " */", 3); |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2752 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2753 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2754 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2755 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2756 | cursor++; |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2757 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2758 | // Don't forget to add a ';'!! |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2759 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2760 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2761 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2762 | Result += " {\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2763 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2764 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2765 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2766 | Result += "_IVARS;\n};\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2767 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2768 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2769 | // Mark this struct as having been generated. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2770 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2771 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2774 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2775 | /// class methods. |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 2776 | template<typename MethodIterator> |
| 2777 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 2778 | MethodIterator MethodEnd, |
Fariborz Jahanian | a398637 | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2779 | bool IsInstanceMethod, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2780 | const char *prefix, |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2781 | const char *ClassName, |
| 2782 | std::string &Result) { |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2783 | if (MethodBegin == MethodEnd) return; |
| 2784 | |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2785 | static bool objc_impl_method = false; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2786 | if (!objc_impl_method) { |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2787 | /* struct _objc_method { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2788 | SEL _cmd; |
| 2789 | char *method_types; |
| 2790 | void *_imp; |
| 2791 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2792 | */ |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2793 | Result += "\nstruct _objc_method {\n"; |
| 2794 | Result += "\tSEL _cmd;\n"; |
| 2795 | Result += "\tchar *method_types;\n"; |
| 2796 | Result += "\tvoid *_imp;\n"; |
| 2797 | Result += "};\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2798 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2799 | objc_impl_method = true; |
Fariborz Jahanian | 96b55da | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2800 | } |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2801 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2802 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2803 | |
| 2804 | /* struct { |
| 2805 | struct _objc_method_list *next_method; |
| 2806 | int method_count; |
| 2807 | struct _objc_method method_list[]; |
| 2808 | } |
| 2809 | */ |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 2810 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2811 | Result += "\nstatic struct {\n"; |
| 2812 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2813 | Result += "\tint method_count;\n"; |
| 2814 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 2815 | Result += utostr(NumMethods); |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2816 | Result += "];\n} _OBJC_"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2817 | Result += prefix; |
| 2818 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2819 | Result += "_METHODS_"; |
| 2820 | Result += ClassName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2821 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2822 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2823 | Result += "_meth\")))= "; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 2824 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2825 | |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2826 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2827 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2828 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2829 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2830 | Result += "\", \""; |
| 2831 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2832 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2833 | Result += MethodInternalNames[*MethodBegin]; |
| 2834 | Result += "}\n"; |
| 2835 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2836 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2837 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2838 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2839 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2840 | Result += "\", \""; |
| 2841 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2842 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2843 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2844 | Result += "}\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2845 | } |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2846 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2849 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2850 | void RewriteObjC:: |
| 2851 | RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 2852 | const char *prefix, |
| 2853 | const char *ClassName, |
| 2854 | std::string &Result) { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2855 | static bool objc_protocol_methods = false; |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2856 | if (Protocols.empty()) return; |
| 2857 | |
| 2858 | for (unsigned i = 0; i != Protocols.size(); i++) { |
| 2859 | ObjCProtocolDecl *PDecl = Protocols[i]; |
| 2860 | // Output struct protocol_methods holder of method selector and type. |
| 2861 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2862 | /* struct protocol_methods { |
| 2863 | SEL _cmd; |
| 2864 | char *method_types; |
| 2865 | } |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2866 | */ |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2867 | Result += "\nstruct protocol_methods {\n"; |
| 2868 | Result += "\tSEL _cmd;\n"; |
| 2869 | Result += "\tchar *method_types;\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2870 | Result += "};\n"; |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2871 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2872 | objc_protocol_methods = true; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2873 | } |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2874 | // Do not synthesize the protocol more than once. |
| 2875 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2876 | continue; |
| 2877 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2878 | if (PDecl->instmeth_begin(*Context) != PDecl->instmeth_end(*Context)) { |
| 2879 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(*Context), |
| 2880 | PDecl->instmeth_end(*Context)); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2881 | /* struct _objc_protocol_method_list { |
| 2882 | int protocol_method_count; |
| 2883 | struct protocol_methods protocols[]; |
| 2884 | } |
| 2885 | */ |
| 2886 | Result += "\nstatic struct {\n"; |
| 2887 | Result += "\tint protocol_method_count;\n"; |
| 2888 | Result += "\tstruct protocol_methods protocols["; |
| 2889 | Result += utostr(NumMethods); |
| 2890 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2891 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2892 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2893 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2894 | |
| 2895 | // Output instance methods declared in this protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2896 | for (ObjCProtocolDecl::instmeth_iterator |
| 2897 | I = PDecl->instmeth_begin(*Context), |
| 2898 | E = PDecl->instmeth_end(*Context); |
| 2899 | I != E; ++I) { |
| 2900 | if (I == PDecl->instmeth_begin(*Context)) |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2901 | Result += "\t ,{{(SEL)\""; |
| 2902 | else |
| 2903 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2904 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2905 | std::string MethodTypeString; |
| 2906 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2907 | Result += "\", \""; |
| 2908 | Result += MethodTypeString; |
| 2909 | Result += "\"}\n"; |
| 2910 | } |
| 2911 | Result += "\t }\n};\n"; |
| 2912 | } |
| 2913 | |
| 2914 | // Output class methods declared in this protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2915 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(*Context), |
| 2916 | PDecl->classmeth_end(*Context)); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2917 | if (NumMethods > 0) { |
| 2918 | /* struct _objc_protocol_method_list { |
| 2919 | int protocol_method_count; |
| 2920 | struct protocol_methods protocols[]; |
| 2921 | } |
| 2922 | */ |
| 2923 | Result += "\nstatic struct {\n"; |
| 2924 | Result += "\tint protocol_method_count;\n"; |
| 2925 | Result += "\tstruct protocol_methods protocols["; |
| 2926 | Result += utostr(NumMethods); |
| 2927 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2928 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2929 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2930 | "{\n\t"; |
| 2931 | Result += utostr(NumMethods); |
| 2932 | Result += "\n"; |
| 2933 | |
| 2934 | // Output instance methods declared in this protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2935 | for (ObjCProtocolDecl::classmeth_iterator |
| 2936 | I = PDecl->classmeth_begin(*Context), |
| 2937 | E = PDecl->classmeth_end(*Context); |
| 2938 | I != E; ++I) { |
| 2939 | if (I == PDecl->classmeth_begin(*Context)) |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2940 | Result += "\t ,{{(SEL)\""; |
| 2941 | else |
| 2942 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2943 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2944 | std::string MethodTypeString; |
| 2945 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2946 | Result += "\", \""; |
| 2947 | Result += MethodTypeString; |
| 2948 | Result += "\"}\n"; |
| 2949 | } |
| 2950 | Result += "\t }\n};\n"; |
| 2951 | } |
| 2952 | |
| 2953 | // Output: |
| 2954 | /* struct _objc_protocol { |
| 2955 | // Objective-C 1.0 extensions |
| 2956 | struct _objc_protocol_extension *isa; |
| 2957 | char *protocol_name; |
| 2958 | struct _objc_protocol **protocol_list; |
| 2959 | struct _objc_protocol_method_list *instance_methods; |
| 2960 | struct _objc_protocol_method_list *class_methods; |
| 2961 | }; |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2962 | */ |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2963 | static bool objc_protocol = false; |
| 2964 | if (!objc_protocol) { |
| 2965 | Result += "\nstruct _objc_protocol {\n"; |
| 2966 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2967 | Result += "\tchar *protocol_name;\n"; |
| 2968 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2969 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2970 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2971 | Result += "};\n"; |
| 2972 | |
| 2973 | objc_protocol = true; |
| 2974 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2975 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2976 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2977 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2978 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 2979 | "{\n\t0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2980 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2981 | Result += "\", 0, "; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2982 | if (PDecl->instmeth_begin(*Context) != PDecl->instmeth_end(*Context)) { |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2983 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2984 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2985 | Result += ", "; |
| 2986 | } |
| 2987 | else |
| 2988 | Result += "0, "; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2989 | if (PDecl->classmeth_begin(*Context) != PDecl->classmeth_end(*Context)) { |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2990 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2991 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2992 | Result += "\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2993 | } |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2994 | else |
| 2995 | Result += "0\n"; |
| 2996 | Result += "};\n"; |
| 2997 | |
| 2998 | // Mark this protocol as having been generated. |
| 2999 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3000 | assert(false && "protocol already synthesized"); |
| 3001 | } |
| 3002 | // Output the top lovel protocol meta-data for the class. |
| 3003 | /* struct _objc_protocol_list { |
| 3004 | struct _objc_protocol_list *next; |
| 3005 | int protocol_count; |
| 3006 | struct _objc_protocol *class_protocols[]; |
| 3007 | } |
| 3008 | */ |
| 3009 | Result += "\nstatic struct {\n"; |
| 3010 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3011 | Result += "\tint protocol_count;\n"; |
| 3012 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3013 | Result += utostr(Protocols.size()); |
| 3014 | Result += "];\n} _OBJC_"; |
| 3015 | Result += prefix; |
| 3016 | Result += "_PROTOCOLS_"; |
| 3017 | Result += ClassName; |
| 3018 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3019 | "{\n\t0, "; |
| 3020 | Result += utostr(Protocols.size()); |
| 3021 | Result += "\n"; |
| 3022 | |
| 3023 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3024 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3025 | Result += " \n"; |
| 3026 | |
| 3027 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3028 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3029 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3030 | Result += "\n"; |
| 3031 | } |
| 3032 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3035 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3036 | /// implementation. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3037 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3038 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3039 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3040 | // Find category declaration for this implementation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3041 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3042 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 3043 | CDecl = CDecl->getNextClassCategory()) |
| 3044 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3045 | break; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3046 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3047 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3048 | FullCategoryName += '_'; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3049 | FullCategoryName += IDecl->getNameAsString(); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3050 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3051 | // Build _objc_method_list for class's instance methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3052 | llvm::SmallVector<ObjCMethodDecl *, 32> |
| 3053 | InstanceMethods(IDecl->instmeth_begin(*Context), |
| 3054 | IDecl->instmeth_end(*Context)); |
| 3055 | |
| 3056 | // If any of our property implementations have associated getters or |
| 3057 | // setters, produce metadata for them as well. |
| 3058 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(*Context), |
| 3059 | PropEnd = IDecl->propimpl_end(*Context); |
| 3060 | Prop != PropEnd; ++Prop) { |
| 3061 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3062 | continue; |
| 3063 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3064 | continue; |
| 3065 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3066 | if (!PD) |
| 3067 | continue; |
| 3068 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3069 | InstanceMethods.push_back(Getter); |
| 3070 | if (PD->isReadOnly()) |
| 3071 | continue; |
| 3072 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3073 | InstanceMethods.push_back(Setter); |
| 3074 | } |
| 3075 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3076 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3077 | Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3078 | |
| 3079 | // Build _objc_method_list for class's class methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3080 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(*Context), |
| 3081 | IDecl->classmeth_end(*Context), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3082 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3083 | Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3084 | |
| 3085 | // Protocols referenced in class declaration? |
Fariborz Jahanian | d256e06 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3086 | // Null CDecl is case of a category implementation with no category interface |
| 3087 | if (CDecl) |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3088 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3089 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3090 | |
| 3091 | /* struct _objc_category { |
| 3092 | char *category_name; |
| 3093 | char *class_name; |
| 3094 | struct _objc_method_list *instance_methods; |
| 3095 | struct _objc_method_list *class_methods; |
| 3096 | struct _objc_protocol_list *protocols; |
| 3097 | // Objective-C 1.0 extensions |
| 3098 | uint32_t size; // sizeof (struct _objc_category) |
| 3099 | struct _objc_property_list *instance_properties; // category's own |
| 3100 | // @property decl. |
| 3101 | }; |
| 3102 | */ |
| 3103 | |
| 3104 | static bool objc_category = false; |
| 3105 | if (!objc_category) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3106 | Result += "\nstruct _objc_category {\n"; |
| 3107 | Result += "\tchar *category_name;\n"; |
| 3108 | Result += "\tchar *class_name;\n"; |
| 3109 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3110 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3111 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3112 | Result += "\tunsigned int size;\n"; |
| 3113 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3114 | Result += "};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3115 | objc_category = true; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3116 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3117 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3118 | Result += FullCategoryName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3119 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3120 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3121 | Result += "\"\n\t, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3122 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3123 | Result += "\"\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3124 | |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3125 | if (IDecl->instmeth_begin(*Context) != IDecl->instmeth_end(*Context)) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3126 | Result += "\t, (struct _objc_method_list *)" |
| 3127 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3128 | Result += FullCategoryName; |
| 3129 | Result += "\n"; |
| 3130 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3131 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3132 | Result += "\t, 0\n"; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3133 | if (IDecl->classmeth_begin(*Context) != IDecl->classmeth_end(*Context)) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3134 | Result += "\t, (struct _objc_method_list *)" |
| 3135 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3136 | Result += FullCategoryName; |
| 3137 | Result += "\n"; |
| 3138 | } |
| 3139 | else |
| 3140 | Result += "\t, 0\n"; |
| 3141 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3142 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3143 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 3144 | Result += FullCategoryName; |
| 3145 | Result += "\n"; |
| 3146 | } |
| 3147 | else |
| 3148 | Result += "\t, 0\n"; |
| 3149 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3152 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3153 | /// ivar offset. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3154 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3155 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3156 | std::string &Result) { |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3157 | if (ivar->isBitField()) { |
| 3158 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3159 | // place all bitfields at offset 0. |
| 3160 | Result += "0"; |
| 3161 | } else { |
| 3162 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3163 | Result += IDecl->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3164 | if (LangOpts.Microsoft) |
| 3165 | Result += "_IMPL"; |
| 3166 | Result += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3167 | Result += ivar->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3168 | Result += ")"; |
| 3169 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3172 | //===----------------------------------------------------------------------===// |
| 3173 | // Meta Data Emission |
| 3174 | //===----------------------------------------------------------------------===// |
| 3175 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3176 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3177 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3178 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3179 | |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3180 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 7333b49 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3181 | if (CDecl->isImplicitInterfaceDecl()) { |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3182 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 3183 | // produce correct synthesis as yet. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3184 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3185 | } |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3186 | |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3187 | // Build _objc_ivar_list metadata for classes ivars if needed |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3188 | unsigned NumIvars = !IDecl->ivar_empty() |
| 3189 | ? IDecl->ivar_size() |
| 3190 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3191 | if (NumIvars > 0) { |
| 3192 | static bool objc_ivar = false; |
| 3193 | if (!objc_ivar) { |
| 3194 | /* struct _objc_ivar { |
| 3195 | char *ivar_name; |
| 3196 | char *ivar_type; |
| 3197 | int ivar_offset; |
| 3198 | }; |
| 3199 | */ |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3200 | Result += "\nstruct _objc_ivar {\n"; |
| 3201 | Result += "\tchar *ivar_name;\n"; |
| 3202 | Result += "\tchar *ivar_type;\n"; |
| 3203 | Result += "\tint ivar_offset;\n"; |
| 3204 | Result += "};\n"; |
| 3205 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3206 | objc_ivar = true; |
| 3207 | } |
| 3208 | |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3209 | /* struct { |
| 3210 | int ivar_count; |
| 3211 | struct _objc_ivar ivar_list[nIvars]; |
| 3212 | }; |
| 3213 | */ |
| 3214 | Result += "\nstatic struct {\n"; |
| 3215 | Result += "\tint ivar_count;\n"; |
| 3216 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3217 | Result += utostr(NumIvars); |
| 3218 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3219 | Result += IDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3220 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3221 | "{\n\t"; |
| 3222 | Result += utostr(NumIvars); |
| 3223 | Result += "\n"; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3224 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3225 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3226 | if (!IDecl->ivar_empty()) { |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3227 | IVI = IDecl->ivar_begin(); |
| 3228 | IVE = IDecl->ivar_end(); |
| 3229 | } else { |
| 3230 | IVI = CDecl->ivar_begin(); |
| 3231 | IVE = CDecl->ivar_end(); |
| 3232 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3233 | Result += "\t,{{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3234 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3235 | Result += "\", \""; |
| 3236 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3237 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3238 | Result += StrEncoding; |
| 3239 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3240 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3241 | Result += "}\n"; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3242 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3243 | Result += "\t ,{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3244 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3245 | Result += "\", \""; |
| 3246 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3247 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3248 | Result += StrEncoding; |
| 3249 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3250 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3251 | Result += "}\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | // Build _objc_method_list for class's instance methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3258 | llvm::SmallVector<ObjCMethodDecl *, 32> |
| 3259 | InstanceMethods(IDecl->instmeth_begin(*Context), |
| 3260 | IDecl->instmeth_end(*Context)); |
| 3261 | |
| 3262 | // If any of our property implementations have associated getters or |
| 3263 | // setters, produce metadata for them as well. |
| 3264 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(*Context), |
| 3265 | PropEnd = IDecl->propimpl_end(*Context); |
| 3266 | Prop != PropEnd; ++Prop) { |
| 3267 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3268 | continue; |
| 3269 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3270 | continue; |
| 3271 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3272 | if (!PD) |
| 3273 | continue; |
| 3274 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3275 | InstanceMethods.push_back(Getter); |
| 3276 | if (PD->isReadOnly()) |
| 3277 | continue; |
| 3278 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3279 | InstanceMethods.push_back(Setter); |
| 3280 | } |
| 3281 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3282 | true, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3283 | |
| 3284 | // Build _objc_method_list for class's class methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3285 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(*Context), |
| 3286 | IDecl->classmeth_end(*Context), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3287 | false, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3288 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3289 | // Protocols referenced in class declaration? |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3290 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3291 | "CLASS", CDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3292 | |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3293 | |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3294 | // Declaration of class/meta-class metadata |
| 3295 | /* struct _objc_class { |
| 3296 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3297 | const char *super_class_name; |
| 3298 | char *name; |
| 3299 | long version; |
| 3300 | long info; |
| 3301 | long instance_size; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3302 | struct _objc_ivar_list *ivars; |
| 3303 | struct _objc_method_list *methods; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3304 | struct objc_cache *cache; |
| 3305 | struct objc_protocol_list *protocols; |
| 3306 | const char *ivar_layout; |
| 3307 | struct _objc_class_ext *ext; |
| 3308 | }; |
| 3309 | */ |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3310 | static bool objc_class = false; |
| 3311 | if (!objc_class) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3312 | Result += "\nstruct _objc_class {\n"; |
| 3313 | Result += "\tstruct _objc_class *isa;\n"; |
| 3314 | Result += "\tconst char *super_class_name;\n"; |
| 3315 | Result += "\tchar *name;\n"; |
| 3316 | Result += "\tlong version;\n"; |
| 3317 | Result += "\tlong info;\n"; |
| 3318 | Result += "\tlong instance_size;\n"; |
| 3319 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3320 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3321 | Result += "\tstruct objc_cache *cache;\n"; |
| 3322 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3323 | Result += "\tconst char *ivar_layout;\n"; |
| 3324 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3325 | Result += "};\n"; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3326 | objc_class = true; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | // Meta-class metadata generation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3330 | ObjCInterfaceDecl *RootClass = 0; |
| 3331 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3332 | while (SuperClass) { |
| 3333 | RootClass = SuperClass; |
| 3334 | SuperClass = SuperClass->getSuperClass(); |
| 3335 | } |
| 3336 | SuperClass = CDecl->getSuperClass(); |
| 3337 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3338 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3339 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3340 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3341 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3342 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3343 | Result += "\""; |
| 3344 | |
| 3345 | if (SuperClass) { |
| 3346 | Result += ", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3347 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3348 | Result += "\", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3349 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3350 | Result += "\""; |
| 3351 | } |
| 3352 | else { |
| 3353 | Result += ", 0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3354 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3355 | Result += "\""; |
| 3356 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3357 | // 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] | 3358 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3359 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3360 | if (IDecl->classmeth_begin(*Context) != IDecl->classmeth_end(*Context)) { |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3361 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3362 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3363 | Result += "\n"; |
| 3364 | } |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3365 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3366 | Result += ", 0\n"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3367 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3368 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3369 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3370 | Result += ",0,0\n"; |
| 3371 | } |
Fariborz Jahanian | 0cb4d92 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3372 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3373 | Result += "\t,0,0,0,0\n"; |
| 3374 | Result += "};\n"; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3375 | |
| 3376 | // class metadata generation. |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3377 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3378 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3379 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3380 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3381 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 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 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3394 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3395 | Result += ", 0,1"; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3396 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3397 | Result += ",0"; |
| 3398 | else { |
| 3399 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3400 | Result += ",sizeof(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3401 | Result += CDecl->getNameAsString(); |
Steve Naroff | c302e5b | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3402 | if (LangOpts.Microsoft) |
| 3403 | Result += "_IMPL"; |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3404 | Result += ")"; |
| 3405 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3406 | if (NumIvars > 0) { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3407 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3408 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3409 | Result += "\n\t"; |
| 3410 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3411 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3412 | Result += ",0"; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame^] | 3413 | if (IDecl->instmeth_begin(*Context) != IDecl->instmeth_end(*Context)) { |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3414 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3415 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3416 | Result += ", 0\n\t"; |
| 3417 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3418 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3419 | Result += ",0,0"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3420 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3421 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3422 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3423 | Result += ", 0,0\n"; |
| 3424 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3425 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3426 | Result += ",0,0,0\n"; |
| 3427 | Result += "};\n"; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3428 | } |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3429 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3430 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3431 | /// and emits meta-data. |
| 3432 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3433 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3434 | int ClsDefCount = ClassImplementation.size(); |
| 3435 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3436 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3437 | // Rewrite implemented methods |
| 3438 | for (int i = 0; i < ClsDefCount; i++) |
| 3439 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3440 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3441 | for (int i = 0; i < CatDefCount; i++) |
| 3442 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3443 | } |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3444 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3445 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3446 | int ClsDefCount = ClassImplementation.size(); |
| 3447 | int CatDefCount = CategoryImplementation.size(); |
| 3448 | |
Steve Naroff | 5bf1022 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3449 | // This is needed for determining instance variable offsets. |
Steve Naroff | 314c1a6 | 2008-08-05 18:47:23 +0000 | [diff] [blame] | 3450 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3451 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3452 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3453 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3454 | |
| 3455 | // For each implemented category, write out all its meta data. |
| 3456 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3457 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3458 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3459 | // Write objc_symtab metadata |
| 3460 | /* |
| 3461 | struct _objc_symtab |
| 3462 | { |
| 3463 | long sel_ref_cnt; |
| 3464 | SEL *refs; |
| 3465 | short cls_def_cnt; |
| 3466 | short cat_def_cnt; |
| 3467 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3468 | }; |
| 3469 | */ |
| 3470 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3471 | Result += "\nstruct _objc_symtab {\n"; |
| 3472 | Result += "\tlong sel_ref_cnt;\n"; |
| 3473 | Result += "\tSEL *refs;\n"; |
| 3474 | Result += "\tshort cls_def_cnt;\n"; |
| 3475 | Result += "\tshort cat_def_cnt;\n"; |
| 3476 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3477 | Result += "};\n\n"; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3478 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3479 | Result += "static struct _objc_symtab " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3480 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3481 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3482 | + ", " + utostr(CatDefCount) + "\n"; |
| 3483 | for (int i = 0; i < ClsDefCount; i++) { |
| 3484 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3485 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3486 | Result += "\n"; |
| 3487 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3488 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3489 | for (int i = 0; i < CatDefCount; i++) { |
| 3490 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3491 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3492 | Result += "_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3493 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3494 | Result += "\n"; |
| 3495 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3496 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3497 | Result += "};\n\n"; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3498 | |
| 3499 | // Write objc_module metadata |
| 3500 | |
| 3501 | /* |
| 3502 | struct _objc_module { |
| 3503 | long version; |
| 3504 | long size; |
| 3505 | const char *name; |
| 3506 | struct _objc_symtab *symtab; |
| 3507 | } |
| 3508 | */ |
| 3509 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3510 | Result += "\nstruct _objc_module {\n"; |
| 3511 | Result += "\tlong version;\n"; |
| 3512 | Result += "\tlong size;\n"; |
| 3513 | Result += "\tconst char *name;\n"; |
| 3514 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3515 | Result += "};\n\n"; |
| 3516 | Result += "static struct _objc_module " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3517 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3518 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3519 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3520 | Result += "};\n\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3521 | |
| 3522 | if (LangOpts.Microsoft) { |
| 3523 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | bdd2dba | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3524 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3525 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3526 | Result += "&_OBJC_MODULES;\n"; |
| 3527 | Result += "#pragma data_seg(pop)\n\n"; |
| 3528 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3529 | } |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3530 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3531 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3532 | const char *funcName, |
| 3533 | std::string Tag) { |
| 3534 | const FunctionType *AFT = CE->getFunctionType(); |
| 3535 | QualType RT = AFT->getResultType(); |
| 3536 | std::string StructRef = "struct " + Tag; |
| 3537 | std::string S = "static " + RT.getAsString() + " __" + |
| 3538 | funcName + "_" + "block_func_" + utostr(i); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3539 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3540 | BlockDecl *BD = CE->getBlockDecl(); |
| 3541 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3542 | if (isa<FunctionNoProtoType>(AFT)) { |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3543 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3544 | // block (to reference imported block decl refs). |
| 3545 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3546 | } else if (BD->param_empty()) { |
| 3547 | S += "(" + StructRef + " *__cself)"; |
| 3548 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3549 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3550 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3551 | S += '('; |
| 3552 | // first add the implicit argument. |
| 3553 | S += StructRef + " *__cself, "; |
| 3554 | std::string ParamStr; |
| 3555 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3556 | E = BD->param_end(); AI != E; ++AI) { |
| 3557 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3558 | ParamStr = (*AI)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3559 | (*AI)->getType().getAsStringInternal(ParamStr); |
| 3560 | S += ParamStr; |
| 3561 | } |
| 3562 | if (FT->isVariadic()) { |
| 3563 | if (!BD->param_empty()) S += ", "; |
| 3564 | S += "..."; |
| 3565 | } |
| 3566 | S += ')'; |
| 3567 | } |
| 3568 | S += " {\n"; |
| 3569 | |
| 3570 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3571 | // First, emit a declaration for all "by ref" decls. |
| 3572 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3573 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3574 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3575 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3576 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3577 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3578 | } |
| 3579 | // Next, emit a declaration for all "by copy" declarations. |
| 3580 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3581 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3582 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3583 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3584 | // Handle nested closure invocation. For example: |
| 3585 | // |
| 3586 | // void (^myImportedClosure)(void); |
| 3587 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3588 | // |
| 3589 | // void (^anotherClosure)(void); |
| 3590 | // anotherClosure = ^(void) { |
| 3591 | // myImportedClosure(); // import and invoke the closure |
| 3592 | // }; |
| 3593 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3594 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3595 | S += "struct __block_impl *"; |
| 3596 | else |
| 3597 | (*I)->getType().getAsStringInternal(Name); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3598 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3599 | } |
| 3600 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3601 | const char *cstr = RewrittenStr.c_str(); |
| 3602 | while (*cstr++ != '{') ; |
| 3603 | S += cstr; |
| 3604 | S += "\n"; |
| 3605 | return S; |
| 3606 | } |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3607 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3608 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3609 | const char *funcName, |
| 3610 | std::string Tag) { |
| 3611 | std::string StructRef = "struct " + Tag; |
| 3612 | std::string S = "static void __"; |
| 3613 | |
| 3614 | S += funcName; |
| 3615 | S += "_block_copy_" + utostr(i); |
| 3616 | S += "(" + StructRef; |
| 3617 | S += "*dst, " + StructRef; |
| 3618 | S += "*src) {"; |
| 3619 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3620 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3621 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3622 | S += (*I)->getNameAsString(); |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3623 | S += ", (void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3624 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3625 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3626 | } |
| 3627 | S += "\nstatic void __"; |
| 3628 | S += funcName; |
| 3629 | S += "_block_dispose_" + utostr(i); |
| 3630 | S += "(" + StructRef; |
| 3631 | S += "*src) {"; |
| 3632 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3633 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3634 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3635 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3636 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3637 | } |
| 3638 | S += "}\n"; |
| 3639 | return S; |
| 3640 | } |
| 3641 | |
| 3642 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3643 | bool hasCopyDisposeHelpers) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3644 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3645 | std::string Constructor = " " + Tag; |
| 3646 | |
| 3647 | S += " {\n struct __block_impl impl;\n"; |
| 3648 | |
| 3649 | if (hasCopyDisposeHelpers) |
| 3650 | S += " void *copy;\n void *dispose;\n"; |
| 3651 | |
| 3652 | Constructor += "(void *fp"; |
| 3653 | |
| 3654 | if (hasCopyDisposeHelpers) |
| 3655 | Constructor += ", void *copyHelp, void *disposeHelp"; |
| 3656 | |
| 3657 | if (BlockDeclRefs.size()) { |
| 3658 | // Output all "by copy" declarations. |
| 3659 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3660 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3661 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3662 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3663 | std::string ArgName = "_" + FieldName; |
| 3664 | // Handle nested closure invocation. For example: |
| 3665 | // |
| 3666 | // void (^myImportedBlock)(void); |
| 3667 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3668 | // |
| 3669 | // void (^anotherBlock)(void); |
| 3670 | // anotherBlock = ^(void) { |
| 3671 | // myImportedBlock(); // import and invoke the closure |
| 3672 | // }; |
| 3673 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3674 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3675 | S += "struct __block_impl *"; |
| 3676 | Constructor += ", void *" + ArgName; |
| 3677 | } else { |
| 3678 | (*I)->getType().getAsStringInternal(FieldName); |
| 3679 | (*I)->getType().getAsStringInternal(ArgName); |
| 3680 | Constructor += ", " + ArgName; |
| 3681 | } |
| 3682 | S += FieldName + ";\n"; |
| 3683 | } |
| 3684 | // Output all "by ref" declarations. |
| 3685 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3686 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3687 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3688 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3689 | std::string ArgName = "_" + FieldName; |
| 3690 | // Handle nested closure invocation. For example: |
| 3691 | // |
| 3692 | // void (^myImportedBlock)(void); |
| 3693 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3694 | // |
| 3695 | // void (^anotherBlock)(void); |
| 3696 | // anotherBlock = ^(void) { |
| 3697 | // myImportedBlock(); // import and invoke the closure |
| 3698 | // }; |
| 3699 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3700 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3701 | S += "struct __block_impl *"; |
| 3702 | Constructor += ", void *" + ArgName; |
| 3703 | } else { |
| 3704 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName); |
| 3705 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName); |
| 3706 | Constructor += ", " + ArgName; |
| 3707 | } |
| 3708 | S += FieldName + "; // by ref\n"; |
| 3709 | } |
| 3710 | // Finish writing the constructor. |
| 3711 | // FIXME: handle NSConcreteGlobalBlock. |
| 3712 | Constructor += ", int flags=0) {\n"; |
| 3713 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3714 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3715 | |
| 3716 | if (hasCopyDisposeHelpers) |
| 3717 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3718 | |
| 3719 | // Initialize all "by copy" arguments. |
| 3720 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3721 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3722 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3723 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3724 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3725 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3726 | else |
| 3727 | Constructor += Name + " = _"; |
| 3728 | Constructor += Name + ";\n"; |
| 3729 | } |
| 3730 | // Initialize all "by ref" arguments. |
| 3731 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3732 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3733 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3734 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3735 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3736 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3737 | else |
| 3738 | Constructor += Name + " = _"; |
| 3739 | Constructor += Name + ";\n"; |
| 3740 | } |
| 3741 | } else { |
| 3742 | // Finish writing the constructor. |
| 3743 | // FIXME: handle NSConcreteGlobalBlock. |
| 3744 | Constructor += ", int flags=0) {\n"; |
| 3745 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3746 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3747 | if (hasCopyDisposeHelpers) |
| 3748 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3749 | } |
| 3750 | Constructor += " "; |
| 3751 | Constructor += "}\n"; |
| 3752 | S += Constructor; |
| 3753 | S += "};\n"; |
| 3754 | return S; |
| 3755 | } |
| 3756 | |
| 3757 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3758 | const char *FunName) { |
| 3759 | // Insert closures that were part of the function. |
| 3760 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3761 | |
| 3762 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3763 | |
| 3764 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3765 | |
| 3766 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
| 3767 | ImportedBlockDecls.size() > 0); |
| 3768 | |
| 3769 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3770 | |
| 3771 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
| 3772 | |
| 3773 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3774 | |
| 3775 | if (ImportedBlockDecls.size()) { |
| 3776 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3777 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3778 | } |
| 3779 | |
| 3780 | BlockDeclRefs.clear(); |
| 3781 | BlockByRefDecls.clear(); |
| 3782 | BlockByCopyDecls.clear(); |
| 3783 | BlockCallExprs.clear(); |
| 3784 | ImportedBlockDecls.clear(); |
| 3785 | } |
| 3786 | Blocks.clear(); |
| 3787 | RewrittenBlockExprs.clear(); |
| 3788 | } |
| 3789 | |
| 3790 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3791 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3792 | const char *FuncName = FD->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3793 | |
| 3794 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3795 | } |
| 3796 | |
| 3797 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3798 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3799 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3800 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3801 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3802 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3803 | // Convert colons to underscores. |
| 3804 | std::string::size_type loc = 0; |
| 3805 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3806 | FuncName.replace(loc, 1, "_"); |
| 3807 | |
| 3808 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3809 | } |
| 3810 | |
| 3811 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3812 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3813 | CI != E; ++CI) |
| 3814 | if (*CI) { |
| 3815 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3816 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3817 | else |
| 3818 | GetBlockDeclRefExprs(*CI); |
| 3819 | } |
| 3820 | // Handle specific things. |
| 3821 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3822 | // FIXME: Handle enums. |
| 3823 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3824 | BlockDeclRefs.push_back(CDRE); |
| 3825 | return; |
| 3826 | } |
| 3827 | |
| 3828 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3829 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3830 | CI != E; ++CI) |
| 3831 | if (*CI) { |
| 3832 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3833 | GetBlockCallExprs(CBE->getBody()); |
| 3834 | else |
| 3835 | GetBlockCallExprs(*CI); |
| 3836 | } |
| 3837 | |
| 3838 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3839 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3840 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3841 | } |
| 3842 | } |
| 3843 | return; |
| 3844 | } |
| 3845 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3846 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3847 | // Navigate to relevant type information. |
| 3848 | const char *closureName = 0; |
| 3849 | const BlockPointerType *CPT = 0; |
| 3850 | |
| 3851 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3852 | closureName = DRE->getDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3853 | CPT = DRE->getType()->getAsBlockPointerType(); |
| 3854 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3855 | closureName = CDRE->getDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3856 | CPT = CDRE->getType()->getAsBlockPointerType(); |
| 3857 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3858 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3859 | CPT = MExpr->getType()->getAsBlockPointerType(); |
| 3860 | } else { |
| 3861 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3862 | } |
| 3863 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3864 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3865 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3866 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3867 | // FTP will be null for closures that don't take arguments. |
| 3868 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3869 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3870 | SourceLocation(), |
| 3871 | &Context->Idents.get("__block_impl")); |
| 3872 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3873 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3874 | // Generate a funky cast. |
| 3875 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3876 | |
| 3877 | // Push the block argument type. |
| 3878 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3879 | if (FTP) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3880 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3881 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3882 | QualType t = *I; |
| 3883 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3884 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3885 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 3886 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3887 | } |
| 3888 | ArgTypes.push_back(t); |
| 3889 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3890 | } |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3891 | // Now do the pointer to function cast. |
| 3892 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
| 3893 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
| 3894 | |
| 3895 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3896 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3897 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, Exp->getCallee(), |
| 3898 | PtrBlock, SourceLocation(), |
| 3899 | SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3900 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3901 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3902 | BlkCast); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3903 | //PE->dump(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3905 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3906 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3907 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3908 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 3909 | FD->getType()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3910 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3911 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, ME, |
| 3912 | PtrToFuncCastType, |
| 3913 | SourceLocation(), |
| 3914 | SourceLocation()); |
| 3915 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3916 | |
| 3917 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3918 | // Add the implicit argument. |
| 3919 | BlkExprs.push_back(BlkCast); |
| 3920 | // Add the user arguments. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3921 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3922 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3923 | BlkExprs.push_back(*I); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3924 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3925 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3926 | BlkExprs.size(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3927 | Exp->getType(), SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3928 | return CE; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3932 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3933 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | void RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
| 3937 | // FIXME: Add more elaborate code generation required by the ABI. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3938 | Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3939 | Context->getPointerType(BDRE->getType()), |
| 3940 | SourceLocation()); |
| 3941 | // Need parens to enforce precedence. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3942 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3943 | ReplaceStmt(BDRE, PE); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3946 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3947 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3948 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3949 | |
| 3950 | // Need to avoid trying to rewrite synthesized casts. |
| 3951 | if (LocStart.isInvalid()) |
| 3952 | return; |
Steve Naroff | 1c53c02 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3953 | // Need to avoid trying to rewrite casts contained in macros. |
| 3954 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3955 | return; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3956 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3957 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3958 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 3959 | |
| 3960 | // advance the location to startArgList. |
| 3961 | const char *argPtr = startBuf; |
| 3962 | |
| 3963 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3964 | switch (*argPtr) { |
| 3965 | case '^': |
| 3966 | // Replace the '^' with '*'. |
| 3967 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 3968 | ReplaceText(LocStart, 1, "*", 1); |
| 3969 | break; |
| 3970 | } |
| 3971 | } |
| 3972 | return; |
| 3973 | } |
| 3974 | |
| 3975 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3976 | SourceLocation DeclLoc = FD->getLocation(); |
| 3977 | unsigned parenCount = 0; |
| 3978 | |
| 3979 | // We have 1 or more arguments that have closure pointers. |
| 3980 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3981 | const char *startArgList = strchr(startBuf, '('); |
| 3982 | |
| 3983 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 3984 | |
| 3985 | parenCount++; |
| 3986 | // advance the location to startArgList. |
| 3987 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 3988 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 3989 | |
| 3990 | const char *argPtr = startArgList; |
| 3991 | |
| 3992 | while (*argPtr++ && parenCount) { |
| 3993 | switch (*argPtr) { |
| 3994 | case '^': |
| 3995 | // Replace the '^' with '*'. |
| 3996 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 3997 | ReplaceText(DeclLoc, 1, "*", 1); |
| 3998 | break; |
| 3999 | case '(': |
| 4000 | parenCount++; |
| 4001 | break; |
| 4002 | case ')': |
| 4003 | parenCount--; |
| 4004 | break; |
| 4005 | } |
| 4006 | } |
| 4007 | return; |
| 4008 | } |
| 4009 | |
| 4010 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4011 | const FunctionProtoType *FTP; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4012 | const PointerType *PT = QT->getAsPointerType(); |
| 4013 | if (PT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4014 | FTP = PT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4015 | } else { |
| 4016 | const BlockPointerType *BPT = QT->getAsBlockPointerType(); |
| 4017 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4018 | FTP = BPT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4019 | } |
| 4020 | if (FTP) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4021 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4022 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4023 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4024 | return true; |
| 4025 | } |
| 4026 | return false; |
| 4027 | } |
| 4028 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4029 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4030 | const char *&RParen) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4031 | const char *argPtr = strchr(Name, '('); |
| 4032 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 4033 | |
| 4034 | LParen = argPtr; // output the start. |
| 4035 | argPtr++; // skip past the left paren. |
| 4036 | unsigned parenCount = 1; |
| 4037 | |
| 4038 | while (*argPtr && parenCount) { |
| 4039 | switch (*argPtr) { |
| 4040 | case '(': parenCount++; break; |
| 4041 | case ')': parenCount--; break; |
| 4042 | default: break; |
| 4043 | } |
| 4044 | if (parenCount) argPtr++; |
| 4045 | } |
| 4046 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4047 | RParen = argPtr; // output the end |
| 4048 | } |
| 4049 | |
| 4050 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4051 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4052 | RewriteBlockPointerFunctionArgs(FD); |
| 4053 | return; |
| 4054 | } |
| 4055 | // Handle Variables and Typedefs. |
| 4056 | SourceLocation DeclLoc = ND->getLocation(); |
| 4057 | QualType DeclT; |
| 4058 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4059 | DeclT = VD->getType(); |
| 4060 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4061 | DeclT = TDD->getUnderlyingType(); |
| 4062 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4063 | DeclT = FD->getType(); |
| 4064 | else |
| 4065 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4066 | |
| 4067 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4068 | const char *endBuf = startBuf; |
| 4069 | // scan backward (from the decl location) for the end of the previous decl. |
| 4070 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4071 | startBuf--; |
| 4072 | |
| 4073 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4074 | // may take block argument types (which will be handled below). |
| 4075 | if (*startBuf == '^') { |
| 4076 | // Replace the '^' with '*', computing a negative offset. |
| 4077 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4078 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4079 | } |
| 4080 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4081 | // Replace the '^' with '*' for arguments. |
| 4082 | DeclLoc = ND->getLocation(); |
| 4083 | startBuf = SM->getCharacterData(DeclLoc); |
| 4084 | const char *argListBegin, *argListEnd; |
| 4085 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4086 | while (argListBegin < argListEnd) { |
| 4087 | if (*argListBegin == '^') { |
| 4088 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4089 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4090 | } |
| 4091 | argListBegin++; |
| 4092 | } |
| 4093 | } |
| 4094 | return; |
| 4095 | } |
| 4096 | |
| 4097 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 4098 | // Add initializers for any closure decl refs. |
| 4099 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4100 | if (BlockDeclRefs.size()) { |
| 4101 | // Unique all "by copy" declarations. |
| 4102 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4103 | if (!BlockDeclRefs[i]->isByRef()) |
| 4104 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4105 | // Unique all "by ref" declarations. |
| 4106 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4107 | if (BlockDeclRefs[i]->isByRef()) { |
| 4108 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4109 | } |
| 4110 | // Find any imported blocks...they will need special attention. |
| 4111 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4112 | if (BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | e59c8b1 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4113 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4114 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4115 | } |
| 4116 | } |
| 4117 | } |
| 4118 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4119 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4120 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4121 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4122 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4123 | ID, FType, FunctionDecl::Extern, false, |
| 4124 | false); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4127 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4128 | Blocks.push_back(Exp); |
| 4129 | |
| 4130 | CollectBlockDeclRefInfo(Exp); |
| 4131 | std::string FuncName; |
| 4132 | |
| 4133 | if (CurFunctionDef) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4134 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4135 | else if (CurMethodDef) { |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4136 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4137 | // Convert colons to underscores. |
| 4138 | std::string::size_type loc = 0; |
| 4139 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4140 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4141 | } else if (GlobalVarDecl) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4142 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4143 | |
| 4144 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4145 | |
| 4146 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4147 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4148 | |
| 4149 | // Get a pointer to the function type so we can cast appropriately. |
| 4150 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4151 | |
| 4152 | FunctionDecl *FD; |
| 4153 | Expr *NewRep; |
| 4154 | |
| 4155 | // Simulate a contructor call... |
| 4156 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4157 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4158 | |
| 4159 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 4160 | |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4161 | // Initialize the block function. |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4162 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4163 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4164 | SourceLocation()); |
| 4165 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4166 | Context->VoidPtrTy, SourceLocation(), |
| 4167 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4168 | InitExprs.push_back(castExpr); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4169 | |
| 4170 | if (ImportedBlockDecls.size()) { |
| 4171 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4172 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4173 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4174 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4175 | Context->VoidPtrTy, SourceLocation(), |
| 4176 | SourceLocation()); |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4177 | InitExprs.push_back(castExpr); |
| 4178 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4179 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4180 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4181 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4182 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4183 | Context->VoidPtrTy, SourceLocation(), |
| 4184 | SourceLocation()); |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4185 | InitExprs.push_back(castExpr); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4186 | } |
| 4187 | // Add initializers for any closure decl refs. |
| 4188 | if (BlockDeclRefs.size()) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4189 | Expr *Exp; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4190 | // Output all "by copy" declarations. |
| 4191 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4192 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4193 | if (isObjCType((*I)->getType())) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4194 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4195 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4196 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4197 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4198 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4199 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4200 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4201 | Context->VoidPtrTy, SourceLocation(), |
| 4202 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4203 | } else { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4204 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4205 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4206 | } |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4207 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4208 | } |
| 4209 | // Output all "by ref" declarations. |
| 4210 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4211 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4212 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4213 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4214 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4215 | Context->getPointerType(Exp->getType()), |
| 4216 | SourceLocation()); |
Steve Naroff | 362c756 | 2008-11-14 21:36:12 +0000 | [diff] [blame] | 4217 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4218 | } |
| 4219 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4220 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4221 | FType, SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4222 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4223 | Context->getPointerType(NewRep->getType()), |
| 4224 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4225 | NewRep = new (Context) CStyleCastExpr(FType, NewRep, FType, SourceLocation(), |
| 4226 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4227 | BlockDeclRefs.clear(); |
| 4228 | BlockByRefDecls.clear(); |
| 4229 | BlockByCopyDecls.clear(); |
| 4230 | ImportedBlockDecls.clear(); |
| 4231 | return NewRep; |
| 4232 | } |
| 4233 | |
| 4234 | //===----------------------------------------------------------------------===// |
| 4235 | // Function Body / Expression rewriting |
| 4236 | //===----------------------------------------------------------------------===// |
| 4237 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4238 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4239 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4240 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4241 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4242 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4243 | // we get this right. |
| 4244 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4245 | // Perform a bottom up traversal of all children. |
| 4246 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4247 | CI != E; ++CI) |
| 4248 | if (*CI) |
| 4249 | CollectPropertySetters(*CI); |
| 4250 | |
| 4251 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4252 | if (BinOp->isAssignmentOp()) { |
| 4253 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4254 | PropSetters[PRE] = BinOp; |
| 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4259 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4260 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4261 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4262 | Stmts.push_back(S); |
| 4263 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4264 | Stmts.push_back(S); |
| 4265 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4266 | } |
| 4267 | |
| 4268 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4269 | |
| 4270 | // Perform a bottom up rewrite of all children. |
| 4271 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4272 | CI != E; ++CI) |
| 4273 | if (*CI) { |
| 4274 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
| 4275 | if (newStmt) |
| 4276 | *CI = newStmt; |
| 4277 | } |
| 4278 | |
| 4279 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4280 | // Rewrite the block body in place. |
| 4281 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4282 | |
| 4283 | // 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] | 4284 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4285 | RewrittenBlockExprs[BE] = Str; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4286 | |
| 4287 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4288 | //blockTranscribed->dump(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4289 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4290 | return blockTranscribed; |
| 4291 | } |
| 4292 | // Handle specific things. |
| 4293 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4294 | return RewriteAtEncode(AtEncode); |
| 4295 | |
| 4296 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4297 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4298 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4299 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4300 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4301 | if (BinOp) { |
| 4302 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4303 | // 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] | 4304 | DisableReplaceStmt = true; |
| 4305 | // Save the source range. Even if we disable the replacement, the |
| 4306 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4307 | // node is at the 'end', the rewriter will fail. Consider this: |
| 4308 | // self.errorHandler = handler ? handler : |
| 4309 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4310 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4311 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4312 | DisableReplaceStmt = false; |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4313 | // |
| 4314 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4315 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4316 | // to see the original expression). Consider this example: |
| 4317 | // |
| 4318 | // Foo *obj1, *obj2; |
| 4319 | // |
| 4320 | // obj1.i = [obj2 rrrr]; |
| 4321 | // |
| 4322 | // 'BinOp' for the previous expression looks like: |
| 4323 | // |
| 4324 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4325 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4326 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4327 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4328 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4329 | // |
| 4330 | // 'newStmt' represents the rewritten message expression. For example: |
| 4331 | // |
| 4332 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4333 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4334 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4335 | // (CStyleCastExpr 0x231d220 'void *' |
| 4336 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4337 | // |
| 4338 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4339 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4340 | // the original RHS (since we haven't altered BinOp). |
| 4341 | // |
| 4342 | // This implies the Rewrite* routines can no longer delete the original |
| 4343 | // node. As a result, we now leak the original AST nodes. |
| 4344 | // |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4345 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4346 | } else { |
| 4347 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4348 | } |
| 4349 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4350 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4351 | return RewriteAtSelector(AtSelector); |
| 4352 | |
| 4353 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4354 | return RewriteObjCStringLiteral(AtString); |
| 4355 | |
| 4356 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4357 | #if 0 |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4358 | // Before we rewrite it, put the original message expression in a comment. |
| 4359 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4360 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4361 | |
| 4362 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4363 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4364 | |
| 4365 | std::string messString; |
| 4366 | messString += "// "; |
| 4367 | messString.append(startBuf, endBuf-startBuf+1); |
| 4368 | messString += "\n"; |
| 4369 | |
| 4370 | // FIXME: Missing definition of |
| 4371 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4372 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4373 | // Tried this, but it didn't work either... |
| 4374 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4375 | #endif |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4376 | return RewriteMessageExpr(MessExpr); |
| 4377 | } |
| 4378 | |
| 4379 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4380 | return RewriteObjCTryStmt(StmtTry); |
| 4381 | |
| 4382 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4383 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4384 | |
| 4385 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4386 | return RewriteObjCThrowStmt(StmtThrow); |
| 4387 | |
| 4388 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4389 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4390 | |
| 4391 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4392 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4393 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4394 | OrigStmtRange.getEnd()); |
| 4395 | if (BreakStmt *StmtBreakStmt = |
| 4396 | dyn_cast<BreakStmt>(S)) |
| 4397 | return RewriteBreakStmt(StmtBreakStmt); |
| 4398 | if (ContinueStmt *StmtContinueStmt = |
| 4399 | dyn_cast<ContinueStmt>(S)) |
| 4400 | return RewriteContinueStmt(StmtContinueStmt); |
| 4401 | |
| 4402 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4403 | // and cast exprs. |
| 4404 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4405 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4406 | // precedes the first Decl. In the future the DeclGroup should have |
| 4407 | // a separate type-specifier that we can rewrite. |
| 4408 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4409 | |
| 4410 | // Blocks rewrite rules. |
| 4411 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4412 | DI != DE; ++DI) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4413 | Decl *SD = *DI; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4414 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4415 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4416 | RewriteBlockPointerDecl(ND); |
| 4417 | else if (ND->getType()->isFunctionPointerType()) |
| 4418 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4419 | } |
| 4420 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4421 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4422 | RewriteBlockPointerDecl(TD); |
| 4423 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4424 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4425 | } |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4430 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4431 | |
| 4432 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4433 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4434 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4435 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4436 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4437 | && "Statement stack mismatch"); |
| 4438 | Stmts.pop_back(); |
| 4439 | } |
| 4440 | // Handle blocks rewriting. |
| 4441 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4442 | if (BDRE->isByRef()) |
| 4443 | RewriteBlockDeclRefExpr(BDRE); |
| 4444 | } |
| 4445 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4446 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4447 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4448 | ReplaceStmt(S, BlockCall); |
| 4449 | return BlockCall; |
| 4450 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4451 | } |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4452 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4453 | RewriteCastExpr(CE); |
| 4454 | } |
| 4455 | #if 0 |
| 4456 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4457 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4458 | // Get the new text. |
| 4459 | std::string SStr; |
| 4460 | llvm::raw_string_ostream Buf(SStr); |
| 4461 | Replacement->printPretty(Buf); |
| 4462 | const std::string &Str = Buf.str(); |
| 4463 | |
| 4464 | printf("CAST = %s\n", &Str[0]); |
| 4465 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4466 | delete S; |
| 4467 | return Replacement; |
| 4468 | } |
| 4469 | #endif |
| 4470 | // Return this stmt unmodified. |
| 4471 | return S; |
| 4472 | } |
| 4473 | |
| 4474 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4475 | /// main file of the input. |
| 4476 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4477 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | ee8d497 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 4478 | if (FD->isOverloadedOperator()) |
| 4479 | return; |
| 4480 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4481 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4482 | // prototype. This enables us to rewrite function declarations and |
| 4483 | // definitions using the same code. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4484 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4485 | |
Douglas Gregor | e3241e9 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 4486 | if (CompoundStmt *Body = FD->getBody(*Context)) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4487 | CurFunctionDef = FD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4488 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4489 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4490 | Body = |
| 4491 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4492 | FD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4493 | CurrentBody = 0; |
| 4494 | if (PropParentMap) { |
| 4495 | delete PropParentMap; |
| 4496 | PropParentMap = 0; |
| 4497 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4498 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4499 | // and any copy/dispose helper functions. |
| 4500 | InsertBlockLiteralsWithinFunction(FD); |
| 4501 | CurFunctionDef = 0; |
| 4502 | } |
| 4503 | return; |
| 4504 | } |
| 4505 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4506 | if (CompoundStmt *Body = MD->getBody()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4507 | CurMethodDef = MD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4508 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4509 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4510 | Body = |
| 4511 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4512 | MD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4513 | CurrentBody = 0; |
| 4514 | if (PropParentMap) { |
| 4515 | delete PropParentMap; |
| 4516 | PropParentMap = 0; |
| 4517 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4518 | InsertBlockLiteralsWithinMethod(MD); |
| 4519 | CurMethodDef = 0; |
| 4520 | } |
| 4521 | } |
| 4522 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4523 | ClassImplementation.push_back(CI); |
| 4524 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4525 | CategoryImplementation.push_back(CI); |
| 4526 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4527 | RewriteForwardClassDecl(CD); |
| 4528 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4529 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4530 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4531 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4532 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4533 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4534 | if (VD->getInit()) { |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4535 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4536 | RewriteCastExpr(CE); |
| 4537 | } |
| 4538 | } |
| 4539 | } |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4540 | if (VD->getInit()) { |
| 4541 | GlobalVarDecl = VD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4542 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4543 | CurrentBody = VD->getInit(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4544 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4545 | CurrentBody = 0; |
| 4546 | if (PropParentMap) { |
| 4547 | delete PropParentMap; |
| 4548 | PropParentMap = 0; |
| 4549 | } |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 4550 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4551 | VD->getNameAsCString()); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4552 | GlobalVarDecl = 0; |
| 4553 | |
| 4554 | // This is needed for blocks. |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4555 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4556 | RewriteCastExpr(CE); |
| 4557 | } |
| 4558 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4559 | return; |
| 4560 | } |
| 4561 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4562 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4563 | RewriteBlockPointerDecl(TD); |
| 4564 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4565 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4566 | return; |
| 4567 | } |
| 4568 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 4569 | if (RD->isDefinition()) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4570 | for (RecordDecl::field_iterator i = RD->field_begin(*Context), |
| 4571 | e = RD->field_end(*Context); i != e; ++i) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4572 | FieldDecl *FD = *i; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4573 | if (isTopLevelBlockPointerType(FD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4574 | RewriteBlockPointerDecl(FD); |
| 4575 | } |
| 4576 | } |
| 4577 | return; |
| 4578 | } |
| 4579 | // Nothing yet. |
| 4580 | } |
| 4581 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4582 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4583 | // Get the top-level buffer that this corresponds to. |
| 4584 | |
| 4585 | // Rewrite tabs if we care. |
| 4586 | //RewriteTabs(); |
| 4587 | |
| 4588 | if (Diags.hasErrorOccurred()) |
| 4589 | return; |
| 4590 | |
| 4591 | // Create the output file. |
| 4592 | |
| 4593 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 4594 | llvm::raw_ostream *OutFile; |
| 4595 | if (OutFileName == "-") { |
| 4596 | OutFile = &llvm::outs(); |
| 4597 | } else if (!OutFileName.empty()) { |
| 4598 | std::string Err; |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 4599 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), |
| 4600 | // set binary mode (critical for Windoze) |
| 4601 | true, |
| 4602 | Err); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4603 | OwnedStream.reset(OutFile); |
| 4604 | } else if (InFileName == "-") { |
| 4605 | OutFile = &llvm::outs(); |
| 4606 | } else { |
| 4607 | llvm::sys::Path Path(InFileName); |
| 4608 | Path.eraseSuffix(); |
| 4609 | Path.appendSuffix("cpp"); |
| 4610 | std::string Err; |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 4611 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), |
| 4612 | // set binary mode (critical for Windoze) |
| 4613 | true, |
| 4614 | Err); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4615 | OwnedStream.reset(OutFile); |
| 4616 | } |
| 4617 | |
| 4618 | RewriteInclude(); |
| 4619 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 4620 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4621 | Preamble.c_str(), Preamble.size(), false); |
| 4622 | |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4623 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4624 | RewriteImplementations(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4625 | |
| 4626 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4627 | // we are done. |
| 4628 | if (const RewriteBuffer *RewriteBuf = |
| 4629 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4630 | //printf("Changed:\n"); |
| 4631 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4632 | } else { |
| 4633 | fprintf(stderr, "No changes\n"); |
| 4634 | } |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4635 | |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4636 | if (ClassImplementation.size() || CategoryImplementation.size()) { |
| 4637 | // Rewrite Objective-c meta data* |
| 4638 | std::string ResultStr; |
| 4639 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4640 | // Emit metadata. |
| 4641 | *OutFile << ResultStr; |
| 4642 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4643 | OutFile->flush(); |
| 4644 | } |
| 4645 | |