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(); |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame^] | 983 | SourceLocation LocEnd = OMD->getCompoundBody(*Context)->getLocStart(); |
| 984 | |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 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(); |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame^] | 999 | SourceLocation LocEnd = OMD->getCompoundBody(*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. |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame^] | 1429 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
Steve Naroff | 5f238fe | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1430 | if (isa<CompoundStmt>(S->getBody())) { |
| 1431 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1432 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1433 | } else { |
| 1434 | /* Need to treat single statements specially. For example: |
| 1435 | * |
| 1436 | * for (A *a in b) if (stuff()) break; |
| 1437 | * for (A *a in b) xxxyy; |
| 1438 | * |
| 1439 | * The following code simply scans ahead to the semi to find the actual end. |
| 1440 | */ |
| 1441 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1442 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1443 | assert(semiBuf && "Can't find ';'"); |
| 1444 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1445 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1446 | } |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1447 | Stmts.pop_back(); |
| 1448 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1449 | return 0; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1452 | /// RewriteObjCSynchronizedStmt - |
| 1453 | /// This routine rewrites @synchronized(expr) stmt; |
| 1454 | /// into: |
| 1455 | /// objc_sync_enter(expr); |
| 1456 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1457 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1458 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1459 | // Get the start location and compute the semi location. |
| 1460 | SourceLocation startLoc = S->getLocStart(); |
| 1461 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1462 | |
| 1463 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1464 | |
| 1465 | std::string buf; |
Steve Naroff | c1656a6 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1466 | buf = "objc_sync_enter((id)"; |
| 1467 | const char *lparenBuf = startBuf; |
| 1468 | while (*lparenBuf != '(') lparenBuf++; |
| 1469 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1470 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1471 | // the sync expression is typically a message expression that's already |
| 1472 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1473 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1474 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1475 | while (*endBuf != ')') endBuf--; |
| 1476 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1477 | buf = ");\n"; |
| 1478 | // declare a new scope with two variables, _stack and _rethrow. |
| 1479 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1480 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1481 | buf += "char *pointers[4];} _stack;\n"; |
| 1482 | buf += "id volatile _rethrow = 0;\n"; |
| 1483 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1484 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1485 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1486 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1487 | startBuf = SM->getCharacterData(startLoc); |
| 1488 | |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1489 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1490 | SourceLocation lastCurlyLoc = startLoc; |
| 1491 | buf = "}\nelse {\n"; |
| 1492 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1493 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1494 | buf += " objc_sync_exit("; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1495 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 1496 | S->getSynchExpr(), |
| 1497 | Context->getObjCIdType(), |
| 1498 | SourceLocation(), |
| 1499 | SourceLocation()); |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1500 | std::string syncExprBufS; |
| 1501 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Steve Naroff | c1656a6 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1502 | syncExpr->printPretty(syncExprBuf); |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1503 | buf += syncExprBuf.str(); |
| 1504 | buf += ");\n"; |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1505 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1506 | buf += "}\n"; |
| 1507 | buf += "}"; |
| 1508 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1509 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1510 | return 0; |
| 1511 | } |
| 1512 | |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1513 | void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) { |
| 1514 | // Perform a bottom up traversal of all children. |
| 1515 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1516 | CI != E; ++CI) |
| 1517 | if (*CI) |
| 1518 | WarnAboutReturnGotoContinueOrBreakStmts(*CI); |
| 1519 | |
| 1520 | if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || |
| 1521 | isa<BreakStmt>(S) || isa<GotoStmt>(S)) { |
| 1522 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1523 | TryFinallyContainsReturnDiag); |
| 1524 | } |
| 1525 | return; |
| 1526 | } |
| 1527 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1528 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1529 | // Get the start location and compute the semi location. |
| 1530 | SourceLocation startLoc = S->getLocStart(); |
| 1531 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1532 | |
| 1533 | assert((*startBuf == '@') && "bogus @try location"); |
| 1534 | |
| 1535 | std::string buf; |
| 1536 | // declare a new scope with two variables, _stack and _rethrow. |
| 1537 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1538 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1539 | buf += "char *pointers[4];} _stack;\n"; |
| 1540 | buf += "id volatile _rethrow = 0;\n"; |
| 1541 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1542 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1543 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1544 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1545 | |
| 1546 | startLoc = S->getTryBody()->getLocEnd(); |
| 1547 | startBuf = SM->getCharacterData(startLoc); |
| 1548 | |
| 1549 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1550 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1551 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1552 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1553 | if (catchList) { |
| 1554 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1555 | buf = " /* @catch begin */ else {\n"; |
| 1556 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1557 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1558 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1559 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1560 | buf += " else { /* @catch continue */"; |
| 1561 | |
| 1562 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 36269d2 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1563 | } else { /* no catch list */ |
| 1564 | buf = "}\nelse {\n"; |
| 1565 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1566 | buf += "}"; |
| 1567 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1568 | } |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1569 | bool sawIdTypedCatch = false; |
| 1570 | Stmt *lastCatchBody = 0; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1571 | while (catchList) { |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1572 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1573 | |
| 1574 | if (catchList == S->getCatchStmts()) |
| 1575 | buf = "if ("; // we are generating code for the first catch clause |
| 1576 | else |
| 1577 | buf = "else if ("; |
| 1578 | startLoc = catchList->getLocStart(); |
| 1579 | startBuf = SM->getCharacterData(startLoc); |
| 1580 | |
| 1581 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1582 | |
| 1583 | const char *lParenLoc = strchr(startBuf, '('); |
| 1584 | |
Steve Naroff | 397c4ce | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1585 | if (catchList->hasEllipsis()) { |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1586 | // Now rewrite the body... |
| 1587 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1588 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1589 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1590 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1591 | "bogus @catch paren location"); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1592 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1593 | |
| 1594 | buf += "1) { id _tmp = _caught;"; |
| 1595 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1596 | buf.c_str(), buf.size()); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1597 | } else if (catchDecl) { |
| 1598 | QualType t = catchDecl->getType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1599 | if (t == Context->getObjCIdType()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1600 | buf += "1) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1601 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1602 | sawIdTypedCatch = true; |
| 1603 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1604 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1605 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1606 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1607 | if (cls) { |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1608 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1609 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1610 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1611 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | // Now rewrite the body... |
| 1615 | lastCatchBody = catchList->getCatchBody(); |
| 1616 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1617 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1618 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1619 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1620 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1621 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1622 | |
| 1623 | buf = " = _caught;"; |
| 1624 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1625 | // declares the @catch parameter). |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1626 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1627 | } else { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1628 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1629 | } |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1630 | // make sure all the catch bodies get rewritten! |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1631 | catchList = catchList->getNextCatchStmt(); |
| 1632 | } |
| 1633 | // Complete the catch list... |
| 1634 | if (lastCatchBody) { |
| 1635 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1636 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1637 | "bogus @catch body location"); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1638 | |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1639 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1640 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1641 | buf = "} /* last catch end */\n"; |
| 1642 | buf += "else {\n"; |
| 1643 | buf += " _rethrow = _caught;\n"; |
| 1644 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1645 | buf += "} } /* @catch end */\n"; |
| 1646 | if (!S->getFinallyStmt()) |
| 1647 | buf += "}\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1648 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1649 | |
| 1650 | // Set lastCurlyLoc |
| 1651 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1652 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1653 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1654 | startLoc = finalStmt->getLocStart(); |
| 1655 | startBuf = SM->getCharacterData(startLoc); |
| 1656 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1657 | |
| 1658 | buf = "/* @finally */"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1659 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1660 | |
| 1661 | Stmt *body = finalStmt->getFinallyBody(); |
| 1662 | SourceLocation startLoc = body->getLocStart(); |
| 1663 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1664 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1665 | "bogus @finally body location"); |
| 1666 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1667 | "bogus @finally body location"); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1668 | |
| 1669 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1670 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1671 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1672 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1673 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1674 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Set lastCurlyLoc |
| 1677 | lastCurlyLoc = body->getLocEnd(); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1678 | |
| 1679 | // Now check for any return/continue/go statements within the @try. |
| 1680 | WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody()); |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1681 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1682 | buf = "{ /* implicit finally clause */\n"; |
| 1683 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1684 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1685 | buf += "}"; |
| 1686 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1687 | } |
| 1688 | // Now emit the final closing curly brace... |
| 1689 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1690 | buf = " } /* @try scope end */\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1691 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1692 | return 0; |
| 1693 | } |
| 1694 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1695 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1696 | return 0; |
| 1697 | } |
| 1698 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1699 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1703 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1704 | // the throw expression is typically a message expression that's already |
| 1705 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1706 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1707 | // Get the start location and compute the semi location. |
| 1708 | SourceLocation startLoc = S->getLocStart(); |
| 1709 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1710 | |
| 1711 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1712 | |
| 1713 | std::string buf; |
| 1714 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | be72efa | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1715 | if (S->getThrowExpr()) |
| 1716 | buf = "objc_exception_throw("; |
| 1717 | else // add an implicit argument |
| 1718 | buf = "objc_exception_throw(_caught"; |
Steve Naroff | 3de6eaa | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1719 | |
| 1720 | // handle "@ throw" correctly. |
| 1721 | const char *wBuf = strchr(startBuf, 'w'); |
| 1722 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1723 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
| 1724 | |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1725 | const char *semiBuf = strchr(startBuf, ';'); |
| 1726 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1727 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1728 | buf = ");"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1729 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1730 | return 0; |
| 1731 | } |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1732 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1733 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | bf0bfa6 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1734 | // Create a new string expression. |
| 1735 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1736 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1737 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1738 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1739 | StrEncoding.length(), false,StrType, |
| 1740 | SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1741 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1742 | |
Chris Lattner | 4478db9 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1743 | // Replace this subexpr in the parent. |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1744 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1745 | return Replacement; |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1748 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 95f6bce | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1749 | if (!SelGetUidFunctionDecl) |
| 1750 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1751 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1752 | // Create a call to sel_registerName("selName"). |
| 1753 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1754 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1755 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1756 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1757 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1758 | false, argType, SourceLocation())); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1759 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1760 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1761 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1762 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1763 | return SelExp; |
| 1764 | } |
| 1765 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1766 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1767 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1768 | // 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] | 1769 | QualType msgSendType = FD->getType(); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1772 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1773 | |
| 1774 | // 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] | 1775 | QualType pToFunc = Context->getPointerType(msgSendType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1776 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, DRE, |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1777 | /*isLvalue=*/false); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1778 | |
| 1779 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1780 | |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1781 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1782 | SourceLocation()); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1785 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1786 | const char *&startRef, const char *&endRef) { |
| 1787 | while (startBuf < endBuf) { |
| 1788 | if (*startBuf == '<') |
| 1789 | startRef = startBuf; // mark the start. |
| 1790 | if (*startBuf == '>') { |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1791 | if (startRef && *startRef == '<') { |
| 1792 | endRef = startBuf; // mark the end. |
| 1793 | return true; |
| 1794 | } |
| 1795 | return false; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1796 | } |
| 1797 | startBuf++; |
| 1798 | } |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1802 | static void scanToNextArgument(const char *&argRef) { |
| 1803 | int angle = 0; |
| 1804 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1805 | if (*argRef == '<') |
| 1806 | angle++; |
| 1807 | else if (*argRef == '>') |
| 1808 | angle--; |
| 1809 | argRef++; |
| 1810 | } |
| 1811 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1812 | } |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1813 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1814 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1815 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1816 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1817 | return true; |
| 1818 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1819 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1820 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1821 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1822 | return true; // we have "Class <Protocol> *". |
| 1823 | } |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1824 | return false; |
| 1825 | } |
| 1826 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1827 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1828 | QualType Type = E->getType(); |
| 1829 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1830 | SourceLocation Loc, EndLoc; |
| 1831 | |
| 1832 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1833 | Loc = ECE->getLParenLoc(); |
| 1834 | EndLoc = ECE->getRParenLoc(); |
| 1835 | } else { |
| 1836 | Loc = E->getLocStart(); |
| 1837 | EndLoc = E->getLocEnd(); |
| 1838 | } |
| 1839 | // This will defend against trying to rewrite synthesized expressions. |
| 1840 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1841 | return; |
| 1842 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1843 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1844 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1845 | const char *startRef = 0, *endRef = 0; |
| 1846 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1847 | // Get the locations of the startRef, endRef. |
| 1848 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1849 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1850 | // Comment out the protocol references. |
| 1851 | InsertText(LessLoc, "/*", 2); |
| 1852 | InsertText(GreaterLoc, "*/", 2); |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1857 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1858 | SourceLocation Loc; |
| 1859 | QualType Type; |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1860 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1861 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1862 | Loc = VD->getLocation(); |
| 1863 | Type = VD->getType(); |
| 1864 | } |
| 1865 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1866 | Loc = FD->getLocation(); |
| 1867 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1868 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1869 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1870 | assert(funcType && "missing function type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1871 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1872 | if (!proto) |
| 1873 | return; |
| 1874 | Type = proto->getResultType(); |
| 1875 | } |
| 1876 | else |
| 1877 | return; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1878 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1879 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1880 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1881 | |
| 1882 | const char *endBuf = SM->getCharacterData(Loc); |
| 1883 | const char *startBuf = endBuf; |
Steve Naroff | ff2fa19 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1884 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1885 | startBuf--; // scan backward (from the decl location) for return type. |
| 1886 | const char *startRef = 0, *endRef = 0; |
| 1887 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1888 | // Get the locations of the startRef, endRef. |
| 1889 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1890 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1891 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1892 | InsertText(LessLoc, "/*", 2); |
| 1893 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1896 | if (!proto) |
| 1897 | return; // most likely, was a variable |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1898 | // Now check arguments. |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1899 | const char *startBuf = SM->getCharacterData(Loc); |
| 1900 | const char *startFuncBuf = startBuf; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1901 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1902 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1903 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1904 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1905 | const char *endBuf = startBuf; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1906 | // scan forward (from the decl location) for argument types. |
| 1907 | scanToNextArgument(endBuf); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1908 | const char *startRef = 0, *endRef = 0; |
| 1909 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1910 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1911 | SourceLocation LessLoc = |
| 1912 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1913 | SourceLocation GreaterLoc = |
| 1914 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1915 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1916 | InsertText(LessLoc, "/*", 2); |
| 1917 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1918 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1919 | startBuf = ++endBuf; |
| 1920 | } |
| 1921 | else { |
Steve Naroff | 6a1d88b | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1922 | // If the function name is derived from a macro expansion, then the |
| 1923 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1924 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1925 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1926 | startBuf++; |
| 1927 | } |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1928 | } |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1931 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1932 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1933 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1934 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1935 | ArgTys.push_back(Context->getPointerType( |
| 1936 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1937 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1938 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1939 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1940 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1941 | SourceLocation(), |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1942 | SelGetUidIdent, getFuncType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1943 | FunctionDecl::Extern, false); |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1946 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1947 | void RewriteObjC::SynthGetProtocolFunctionDecl() { |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1948 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1949 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1950 | ArgTys.push_back(Context->getPointerType( |
| 1951 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1952 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1953 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1954 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1955 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1956 | SourceLocation(), |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1957 | SelGetProtoIdent, getFuncType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1958 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1961 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1962 | // declared in <objc/objc.h> |
Douglas Gregor | 7339c9e | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 1963 | if (FD->getIdentifier() && |
| 1964 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1965 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1966 | return; |
| 1967 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1968 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1971 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1972 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1973 | if (SuperContructorFunctionDecl) |
| 1974 | return; |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 1975 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1976 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1977 | QualType argT = Context->getObjCIdType(); |
| 1978 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1979 | ArgTys.push_back(argT); |
| 1980 | ArgTys.push_back(argT); |
| 1981 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1982 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1983 | false, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1984 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1985 | SourceLocation(), |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1986 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1987 | FunctionDecl::Extern, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1990 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1991 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1992 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1993 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1994 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1995 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1996 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1997 | argT = Context->getObjCSelType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1998 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1999 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2000 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2001 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2002 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2003 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2004 | SourceLocation(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2005 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2006 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2009 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2010 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2011 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2012 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2013 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2014 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2015 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2016 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2017 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2018 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2019 | argT = Context->getObjCSelType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2020 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2021 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2022 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2023 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2024 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2025 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2026 | SourceLocation(), |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2027 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2028 | FunctionDecl::Extern, false); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2031 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2032 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2033 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2034 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2035 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2036 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2037 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2038 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2039 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2040 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2041 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2042 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2043 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2044 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2045 | SourceLocation(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2046 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2047 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | // SynthMsgSendSuperStretFunctionDecl - |
| 2051 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2052 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2053 | IdentifierInfo *msgSendIdent = |
| 2054 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2055 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2056 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2057 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2058 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2059 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2060 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2061 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2062 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2063 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2064 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2065 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2066 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2067 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2068 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 2069 | SourceLocation(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2070 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2071 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2074 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2075 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2076 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2077 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2078 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2079 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2080 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2081 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2082 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2083 | ArgTys.push_back(argT); |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2084 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2085 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2086 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2087 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2088 | SourceLocation(), |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2089 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2090 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2093 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2094 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2095 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2096 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2097 | ArgTys.push_back(Context->getPointerType( |
| 2098 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2099 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2100 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2101 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2102 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2103 | SourceLocation(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2104 | getClassIdent, getClassType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2105 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2108 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2109 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2110 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2111 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2112 | ArgTys.push_back(Context->getPointerType( |
| 2113 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2114 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2115 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2116 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2117 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2118 | SourceLocation(), |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2119 | getClassIdent, getClassType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2120 | FunctionDecl::Extern, false); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2123 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2124 | QualType strType = getConstantStringStructType(); |
| 2125 | |
| 2126 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a1f0099 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2127 | |
| 2128 | std::string tmpName = InFileName; |
| 2129 | unsigned i; |
| 2130 | for (i=0; i < tmpName.length(); i++) { |
| 2131 | char c = tmpName.at(i); |
| 2132 | // replace any non alphanumeric characters with '_'. |
| 2133 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2134 | tmpName[i] = '_'; |
| 2135 | } |
| 2136 | S += tmpName; |
| 2137 | S += "_"; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2138 | S += utostr(NumObjCStringLiterals++); |
| 2139 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2140 | Preamble += "static __NSConstantStringImpl " + S; |
| 2141 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2142 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2143 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2144 | std::string prettyBufS; |
| 2145 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2146 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2147 | Preamble += prettyBuf.str(); |
| 2148 | Preamble += ","; |
Steve Naroff | 64bce35 | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2149 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2150 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2151 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2152 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2153 | &Context->Idents.get(S.c_str()), strType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2154 | VarDecl::Static); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2155 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2156 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2157 | Context->getPointerType(DRE->getType()), |
| 2158 | SourceLocation()); |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2159 | // cast to NSConstantString * |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2160 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), Unop, |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2161 | Exp->getType(), SourceLocation(), SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2162 | ReplaceStmt(Exp, cast); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2163 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2164 | return cast; |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2167 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2168 | // check if we are sending a message to 'super' |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2169 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | d860663 | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2171 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
| 2172 | const PointerType *PT = Super->getType()->getAsPointerType(); |
Chris Lattner | 4423d37 | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2173 | assert(PT); |
| 2174 | ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 2175 | return IT->getDecl(); |
| 2176 | } |
| 2177 | return 0; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2181 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2182 | if (!SuperStructDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2183 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2184 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2185 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2186 | QualType FieldTypes[2]; |
| 2187 | |
| 2188 | // struct objc_object *receiver; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2189 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2190 | // struct objc_class *super; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2191 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2192 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2193 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2194 | for (unsigned i = 0; i < 2; ++i) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2195 | SuperStructDecl->addDecl(*Context, |
| 2196 | FieldDecl::Create(*Context, SuperStructDecl, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2197 | SourceLocation(), 0, |
| 2198 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2199 | /*Mutable=*/false)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2200 | } |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2201 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2202 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2203 | } |
| 2204 | return Context->getTagDeclType(SuperStructDecl); |
| 2205 | } |
| 2206 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2207 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2208 | if (!ConstantStringDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2209 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2210 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2211 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2212 | QualType FieldTypes[4]; |
| 2213 | |
| 2214 | // struct objc_object *receiver; |
| 2215 | FieldTypes[0] = Context->getObjCIdType(); |
| 2216 | // int flags; |
| 2217 | FieldTypes[1] = Context->IntTy; |
| 2218 | // char *str; |
| 2219 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2220 | // long length; |
| 2221 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2222 | |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2223 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2224 | for (unsigned i = 0; i < 4; ++i) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2225 | ConstantStringDecl->addDecl(*Context, |
| 2226 | FieldDecl::Create(*Context, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2227 | ConstantStringDecl, |
| 2228 | SourceLocation(), 0, |
| 2229 | FieldTypes[i], |
| 2230 | /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2231 | /*Mutable=*/true)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2235 | } |
| 2236 | return Context->getTagDeclType(ConstantStringDecl); |
| 2237 | } |
| 2238 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2239 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2240 | if (!SelGetUidFunctionDecl) |
| 2241 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2242 | if (!MsgSendFunctionDecl) |
| 2243 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2244 | if (!MsgSendSuperFunctionDecl) |
| 2245 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2246 | if (!MsgSendStretFunctionDecl) |
| 2247 | SynthMsgSendStretFunctionDecl(); |
| 2248 | if (!MsgSendSuperStretFunctionDecl) |
| 2249 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2250 | if (!MsgSendFpretFunctionDecl) |
| 2251 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2252 | if (!GetClassFunctionDecl) |
| 2253 | SynthGetClassFunctionDecl(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2254 | if (!GetMetaClassFunctionDecl) |
| 2255 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2256 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2257 | // default to objc_msgSend(). |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2258 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2259 | // May need to use objc_msgSend_stret() as well. |
| 2260 | FunctionDecl *MsgSendStretFlavor = 0; |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2261 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 2262 | QualType resultType = OMD->getResultType(); |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2263 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2264 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2265 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2266 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2267 | } |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2268 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2269 | // Synthesize a call to objc_msgSend(). |
| 2270 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2271 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2272 | |
| 2273 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2274 | if (clsName) { // class message. |
Steve Naroff | 91a6920 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2275 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2276 | // the 'super' idiom within a class method. |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2277 | if (!strcmp(clsName->getName(), "super")) { |
| 2278 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2279 | if (MsgSendStretFlavor) |
| 2280 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2281 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2282 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2283 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2284 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2285 | |
| 2286 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2287 | |
| 2288 | // set the receiver to self, the first argument to all methods. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2289 | InitExprs.push_back(new (Context) DeclRefExpr( |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2290 | CurMethodDef->getSelfDecl(), |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2291 | Context->getObjCIdType(), |
| 2292 | SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2293 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2294 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2295 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2296 | SuperDecl->getIdentifier()->getName(), |
| 2297 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2298 | false, argType, SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2299 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2300 | &ClsExprs[0], |
| 2301 | ClsExprs.size()); |
| 2302 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2303 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2304 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2305 | Cls, Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2306 | SourceLocation(), SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2307 | // struct objc_super |
| 2308 | QualType superType = getSuperStructType(); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2309 | Expr *SuperRep; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2310 | |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2311 | if (LangOpts.Microsoft) { |
| 2312 | SynthSuperContructorFunctionDecl(); |
| 2313 | // Simulate a contructor call... |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2314 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2315 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2316 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2317 | InitExprs.size(), |
| 2318 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2319 | // The code for super is a little tricky to prevent collision with |
| 2320 | // the structure definition in the header. The rewriter has it's own |
| 2321 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2322 | // we need the cast below. For example: |
| 2323 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2324 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2325 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2326 | Context->getPointerType(SuperRep->getType()), |
| 2327 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2328 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2329 | SuperRep, Context->getPointerType(superType), |
| 2330 | SourceLocation(), SourceLocation()); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2331 | } else { |
| 2332 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2333 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2334 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2335 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2336 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2337 | false); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2338 | // struct objc_super * |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2339 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2340 | Context->getPointerType(SuperRep->getType()), |
| 2341 | SourceLocation()); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2342 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2343 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2344 | } else { |
| 2345 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2346 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2347 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2348 | clsName->getName(), |
| 2349 | clsName->getLength(), |
| 2350 | false, argType, |
| 2351 | SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2352 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2353 | &ClsExprs[0], |
| 2354 | ClsExprs.size()); |
| 2355 | MsgExprs.push_back(Cls); |
| 2356 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2357 | } else { // instance message. |
| 2358 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2359 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2360 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2361 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2362 | if (MsgSendStretFlavor) |
| 2363 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2364 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2365 | |
| 2366 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2367 | |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2368 | InitExprs.push_back( |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2369 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 2370 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 2352861 | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2371 | Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2372 | SourceLocation()), |
| 2373 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2374 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2375 | |
| 2376 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2377 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2378 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2379 | SuperDecl->getIdentifier()->getName(), |
| 2380 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2381 | false, argType, SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2382 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2383 | &ClsExprs[0], |
| 2384 | ClsExprs.size()); |
Fariborz Jahanian | fabf3bf | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2385 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2386 | InitExprs.push_back( |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2387 | // set 'super class', using objc_getClass(). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2388 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2389 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2390 | // struct objc_super |
| 2391 | QualType superType = getSuperStructType(); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2392 | Expr *SuperRep; |
| 2393 | |
| 2394 | if (LangOpts.Microsoft) { |
| 2395 | SynthSuperContructorFunctionDecl(); |
| 2396 | // Simulate a contructor call... |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2397 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2398 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2399 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2400 | InitExprs.size(), |
| 2401 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2402 | // The code for super is a little tricky to prevent collision with |
| 2403 | // the structure definition in the header. The rewriter has it's own |
| 2404 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2405 | // we need the cast below. For example: |
| 2406 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2407 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2408 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2409 | Context->getPointerType(SuperRep->getType()), |
| 2410 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2411 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2412 | SuperRep, Context->getPointerType(superType), |
| 2413 | SourceLocation(), SourceLocation()); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2414 | } else { |
| 2415 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2416 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2417 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2418 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2419 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2420 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2421 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2422 | } else { |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2423 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2424 | // Foo<Proto> *. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2425 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2426 | recExpr = CE->getSubExpr(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2427 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), recExpr, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2428 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2429 | SourceLocation(), SourceLocation()); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2430 | MsgExprs.push_back(recExpr); |
| 2431 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2432 | } |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2433 | // 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] | 2434 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2435 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2436 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2437 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2438 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2439 | false, argType, SourceLocation())); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2440 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2441 | &SelExprs[0], SelExprs.size()); |
| 2442 | MsgExprs.push_back(SelExp); |
| 2443 | |
| 2444 | // Now push any user supplied arguments. |
| 2445 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2446 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2447 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2448 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2449 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2450 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2451 | ? Context->getObjCIdType() |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2452 | : ICE->getType(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2453 | userExpr = new (Context) CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2454 | } |
| 2455 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2456 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2457 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2458 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2459 | userExpr = CE->getSubExpr(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2460 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2461 | userExpr, Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2462 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2463 | } |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2464 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2465 | MsgExprs.push_back(userExpr); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2466 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2467 | // the original expression, since we will delete it below. |
| 2468 | Exp->setArg(i, 0); |
| 2469 | } |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2470 | // Generate the funky cast. |
| 2471 | CastExpr *cast; |
| 2472 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2473 | QualType returnType; |
| 2474 | |
| 2475 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 0c04bb6 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2476 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2477 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2478 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2479 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2480 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2481 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2482 | // Push any user argument types. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2483 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2484 | E = OMD->param_end(); PI != E; ++PI) { |
| 2485 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2486 | ? Context->getObjCIdType() |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2487 | : (*PI)->getType(); |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2488 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2489 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2490 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 2491 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2492 | } |
Steve Naroff | 4242b97 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2493 | ArgTypes.push_back(t); |
| 2494 | } |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2495 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2496 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2497 | } else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2498 | returnType = Context->getObjCIdType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2499 | } |
| 2500 | // 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] | 2501 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2502 | |
| 2503 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2504 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2505 | SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2506 | |
| 2507 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2508 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2509 | // xx.m:13: warning: function called through a non-compatible type |
| 2510 | // 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] | 2511 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2512 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2513 | SourceLocation(), SourceLocation()); |
Steve Naroff | 29fe746 | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2514 | |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2515 | // Now do the "normal" pointer to function cast. |
| 2516 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2517 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 3b8b4e3 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2518 | // If we don't have a method decl, force a variadic cast. |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2519 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2520 | castType = Context->getPointerType(castType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2521 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2522 | |
| 2523 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2524 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2525 | |
| 2526 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2527 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2528 | MsgExprs.size(), |
| 2529 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2530 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2531 | if (MsgSendStretFlavor) { |
| 2532 | // We have the method which returns a struct/union. Must also generate |
| 2533 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2534 | // expression which dictate which one to envoke depending on size of |
| 2535 | // method's return type. |
| 2536 | |
| 2537 | // Create a reference to the objc_msgSend_stret() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2538 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2539 | SourceLocation()); |
| 2540 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2541 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2542 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2543 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2544 | // Now do the "normal" pointer to function cast. |
| 2545 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2546 | &ArgTypes[0], ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2547 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2548 | castType = Context->getPointerType(castType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2549 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2550 | |
| 2551 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2552 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2553 | |
| 2554 | FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2555 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2556 | MsgExprs.size(), |
| 2557 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2558 | |
| 2559 | // Build sizeof(returnType) |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2560 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
| 2561 | returnType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2562 | Context->getSizeType(), |
| 2563 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2564 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2565 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2566 | // For X86 it is more complicated and some kind of target specific routine |
| 2567 | // is needed to decide what to do. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2568 | unsigned IntSize = |
| 2569 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2570 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2571 | Context->IntTy, |
| 2572 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2573 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2574 | BinaryOperator::LE, |
| 2575 | Context->IntTy, |
| 2576 | SourceLocation()); |
| 2577 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2578 | ConditionalOperator *CondExpr = |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2579 | new (Context) ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
| 2580 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2581 | } |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2582 | return ReplacingStmt; |
| 2583 | } |
| 2584 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2585 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2586 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | e8efe89 | 2008-10-27 20:54:44 +0000 | [diff] [blame] | 2587 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 2588 | //ReplacingStmt->dump(); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2589 | // Now do the actual rewrite. |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2590 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2591 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2592 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2593 | return ReplacingStmt; |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2596 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2597 | /// call to objc_getProtocol("proto-name"). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2598 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2599 | if (!GetProtocolFunctionDecl) |
| 2600 | SynthGetProtocolFunctionDecl(); |
| 2601 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2602 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2603 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2604 | ProtoExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2605 | Exp->getProtocol()->getNameAsCString(), |
| 2606 | strlen(Exp->getProtocol()->getNameAsCString()), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2607 | false, argType, SourceLocation())); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2608 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2609 | &ProtoExprs[0], |
| 2610 | ProtoExprs.size()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2611 | ReplaceStmt(Exp, ProtoExp); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2612 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2613 | return ProtoExp; |
| 2614 | |
| 2615 | } |
| 2616 | |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2617 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2618 | const char *endBuf) { |
| 2619 | while (startBuf < endBuf) { |
| 2620 | if (*startBuf == '#') { |
| 2621 | // Skip whitespace. |
| 2622 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2623 | ; |
| 2624 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2625 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2626 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2627 | !strncmp(startBuf, "define", strlen("define")) || |
| 2628 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2629 | !strncmp(startBuf, "else", strlen("else")) || |
| 2630 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2631 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2632 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2633 | !strncmp(startBuf, "include", strlen("include")) || |
| 2634 | !strncmp(startBuf, "import", strlen("import")) || |
| 2635 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2636 | return true; |
| 2637 | } |
| 2638 | startBuf++; |
| 2639 | } |
| 2640 | return false; |
| 2641 | } |
| 2642 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2643 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2644 | /// an objective-c class with ivars. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2645 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2646 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2647 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2648 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2649 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2650 | // Do not synthesize more than once. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2651 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2652 | return; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2653 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2654 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2655 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2656 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2657 | |
| 2658 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2659 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2660 | |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2661 | // If no ivars and no root or if its root, directly or indirectly, |
| 2662 | // 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] | 2663 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2664 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2665 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2666 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2667 | return; |
| 2668 | } |
| 2669 | |
| 2670 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2671 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2672 | Result += "\nstruct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2673 | Result += CDecl->getNameAsString(); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2674 | if (LangOpts.Microsoft) |
| 2675 | Result += "_IMPL"; |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2676 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2677 | if (NumIvars > 0) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2678 | const char *cursor = strchr(startBuf, '{'); |
| 2679 | assert((cursor && endBuf) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2680 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2681 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2682 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2683 | // NSURL.h, for example): |
| 2684 | // |
| 2685 | // #ifdef XYZ |
| 2686 | // @interface Foo : NSObject |
| 2687 | // #else |
| 2688 | // @interface FooBar : NSObject |
| 2689 | // #endif |
| 2690 | // { |
| 2691 | // int i; |
| 2692 | // } |
| 2693 | // @end |
| 2694 | // |
| 2695 | // This clause is segregated to avoid breaking the common case. |
| 2696 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2697 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2698 | CDecl->getClassLoc(); |
| 2699 | const char *endHeader = SM->getCharacterData(L); |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2700 | endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2702 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2703 | // advance to the end of the referenced protocols. |
| 2704 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2705 | endHeader++; |
| 2706 | } |
| 2707 | // rewrite the original header |
| 2708 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2709 | } else { |
| 2710 | // rewrite the original header *without* disturbing the '{' |
| 2711 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2712 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2713 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2714 | Result = "\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2715 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2716 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2717 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2718 | Result += "_IVARS;\n"; |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2719 | |
| 2720 | // insert the super class structure definition. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2721 | SourceLocation OnePastCurly = |
| 2722 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2723 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2724 | } |
| 2725 | cursor++; // past '{' |
| 2726 | |
| 2727 | // Now comment out any visibility specifiers. |
| 2728 | while (cursor < endBuf) { |
| 2729 | if (*cursor == '@') { |
| 2730 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f04ead5 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2731 | // Skip whitespace. |
| 2732 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2733 | /*scan*/; |
| 2734 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2735 | // FIXME: presence of @public, etc. inside comment results in |
| 2736 | // this transformation as well, which is still correct c-code. |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2737 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2738 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | a93924a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2739 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2740 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2741 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2742 | } |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2743 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2744 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2745 | // for type checking. |
| 2746 | else if (*cursor == '<') { |
| 2747 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2748 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2749 | cursor = strchr(cursor, '>'); |
| 2750 | cursor++; |
| 2751 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2752 | InsertText(atLoc, " */", 3); |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2753 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2754 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2755 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2756 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2757 | cursor++; |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2758 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2759 | // Don't forget to add a ';'!! |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2760 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2761 | } else { // we don't have any instance variables - insert super struct. |
Chris Lattner | e1be602 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 2762 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2763 | Result += " {\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2764 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2765 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2766 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2767 | Result += "_IVARS;\n};\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2768 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2769 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2770 | // Mark this struct as having been generated. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2771 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2772 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2775 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2776 | /// class methods. |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2777 | template<typename MethodIterator> |
| 2778 | void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, |
| 2779 | MethodIterator MethodEnd, |
Fariborz Jahanian | a398637 | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2780 | bool IsInstanceMethod, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2781 | const char *prefix, |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2782 | const char *ClassName, |
| 2783 | std::string &Result) { |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2784 | if (MethodBegin == MethodEnd) return; |
| 2785 | |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2786 | static bool objc_impl_method = false; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2787 | if (!objc_impl_method) { |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2788 | /* struct _objc_method { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2789 | SEL _cmd; |
| 2790 | char *method_types; |
| 2791 | void *_imp; |
| 2792 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2793 | */ |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2794 | Result += "\nstruct _objc_method {\n"; |
| 2795 | Result += "\tSEL _cmd;\n"; |
| 2796 | Result += "\tchar *method_types;\n"; |
| 2797 | Result += "\tvoid *_imp;\n"; |
| 2798 | Result += "};\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2799 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2800 | objc_impl_method = true; |
Fariborz Jahanian | 96b55da | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2801 | } |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2802 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2803 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2804 | |
| 2805 | /* struct { |
| 2806 | struct _objc_method_list *next_method; |
| 2807 | int method_count; |
| 2808 | struct _objc_method method_list[]; |
| 2809 | } |
| 2810 | */ |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2811 | unsigned NumMethods = std::distance(MethodBegin, MethodEnd); |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2812 | Result += "\nstatic struct {\n"; |
| 2813 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2814 | Result += "\tint method_count;\n"; |
| 2815 | Result += "\tstruct _objc_method method_list["; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2816 | Result += utostr(NumMethods); |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2817 | Result += "];\n} _OBJC_"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2818 | Result += prefix; |
| 2819 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2820 | Result += "_METHODS_"; |
| 2821 | Result += ClassName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2822 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2823 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2824 | Result += "_meth\")))= "; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2825 | Result += "{\n\t0, " + utostr(NumMethods) + "\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2826 | |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2827 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2828 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2829 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2830 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2831 | Result += "\", \""; |
| 2832 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2833 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2834 | Result += MethodInternalNames[*MethodBegin]; |
| 2835 | Result += "}\n"; |
| 2836 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2837 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2838 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2839 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2840 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2841 | Result += "\", \""; |
| 2842 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2843 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2844 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2845 | Result += "}\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2846 | } |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2847 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2850 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2851 | void RewriteObjC:: |
| 2852 | RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 2853 | const char *prefix, |
| 2854 | const char *ClassName, |
| 2855 | std::string &Result) { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2856 | static bool objc_protocol_methods = false; |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2857 | if (Protocols.empty()) return; |
| 2858 | |
| 2859 | for (unsigned i = 0; i != Protocols.size(); i++) { |
| 2860 | ObjCProtocolDecl *PDecl = Protocols[i]; |
| 2861 | // Output struct protocol_methods holder of method selector and type. |
| 2862 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2863 | /* struct protocol_methods { |
| 2864 | SEL _cmd; |
| 2865 | char *method_types; |
| 2866 | } |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2867 | */ |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2868 | Result += "\nstruct protocol_methods {\n"; |
| 2869 | Result += "\tSEL _cmd;\n"; |
| 2870 | Result += "\tchar *method_types;\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2871 | Result += "};\n"; |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2872 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2873 | objc_protocol_methods = true; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2874 | } |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2875 | // Do not synthesize the protocol more than once. |
| 2876 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2877 | continue; |
| 2878 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2879 | if (PDecl->instmeth_begin(*Context) != PDecl->instmeth_end(*Context)) { |
| 2880 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(*Context), |
| 2881 | PDecl->instmeth_end(*Context)); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2882 | /* struct _objc_protocol_method_list { |
| 2883 | int protocol_method_count; |
| 2884 | struct protocol_methods protocols[]; |
| 2885 | } |
| 2886 | */ |
| 2887 | Result += "\nstatic struct {\n"; |
| 2888 | Result += "\tint protocol_method_count;\n"; |
| 2889 | Result += "\tstruct protocol_methods protocols["; |
| 2890 | Result += utostr(NumMethods); |
| 2891 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2892 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2893 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2894 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2895 | |
| 2896 | // Output instance methods declared in this protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2897 | for (ObjCProtocolDecl::instmeth_iterator |
| 2898 | I = PDecl->instmeth_begin(*Context), |
| 2899 | E = PDecl->instmeth_end(*Context); |
| 2900 | I != E; ++I) { |
| 2901 | if (I == PDecl->instmeth_begin(*Context)) |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2902 | Result += "\t ,{{(SEL)\""; |
| 2903 | else |
| 2904 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2905 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2906 | std::string MethodTypeString; |
| 2907 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2908 | Result += "\", \""; |
| 2909 | Result += MethodTypeString; |
| 2910 | Result += "\"}\n"; |
| 2911 | } |
| 2912 | Result += "\t }\n};\n"; |
| 2913 | } |
| 2914 | |
| 2915 | // Output class methods declared in this protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2916 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(*Context), |
| 2917 | PDecl->classmeth_end(*Context)); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2918 | if (NumMethods > 0) { |
| 2919 | /* struct _objc_protocol_method_list { |
| 2920 | int protocol_method_count; |
| 2921 | struct protocol_methods protocols[]; |
| 2922 | } |
| 2923 | */ |
| 2924 | Result += "\nstatic struct {\n"; |
| 2925 | Result += "\tint protocol_method_count;\n"; |
| 2926 | Result += "\tstruct protocol_methods protocols["; |
| 2927 | Result += utostr(NumMethods); |
| 2928 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2929 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2930 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2931 | "{\n\t"; |
| 2932 | Result += utostr(NumMethods); |
| 2933 | Result += "\n"; |
| 2934 | |
| 2935 | // Output instance methods declared in this protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2936 | for (ObjCProtocolDecl::classmeth_iterator |
| 2937 | I = PDecl->classmeth_begin(*Context), |
| 2938 | E = PDecl->classmeth_end(*Context); |
| 2939 | I != E; ++I) { |
| 2940 | if (I == PDecl->classmeth_begin(*Context)) |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2941 | Result += "\t ,{{(SEL)\""; |
| 2942 | else |
| 2943 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2944 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2945 | std::string MethodTypeString; |
| 2946 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2947 | Result += "\", \""; |
| 2948 | Result += MethodTypeString; |
| 2949 | Result += "\"}\n"; |
| 2950 | } |
| 2951 | Result += "\t }\n};\n"; |
| 2952 | } |
| 2953 | |
| 2954 | // Output: |
| 2955 | /* struct _objc_protocol { |
| 2956 | // Objective-C 1.0 extensions |
| 2957 | struct _objc_protocol_extension *isa; |
| 2958 | char *protocol_name; |
| 2959 | struct _objc_protocol **protocol_list; |
| 2960 | struct _objc_protocol_method_list *instance_methods; |
| 2961 | struct _objc_protocol_method_list *class_methods; |
| 2962 | }; |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2963 | */ |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2964 | static bool objc_protocol = false; |
| 2965 | if (!objc_protocol) { |
| 2966 | Result += "\nstruct _objc_protocol {\n"; |
| 2967 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2968 | Result += "\tchar *protocol_name;\n"; |
| 2969 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2970 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2971 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2972 | Result += "};\n"; |
| 2973 | |
| 2974 | objc_protocol = true; |
| 2975 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2976 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2977 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2978 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2979 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 2980 | "{\n\t0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2981 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2982 | Result += "\", 0, "; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2983 | if (PDecl->instmeth_begin(*Context) != PDecl->instmeth_end(*Context)) { |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2984 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2985 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2986 | Result += ", "; |
| 2987 | } |
| 2988 | else |
| 2989 | Result += "0, "; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2990 | if (PDecl->classmeth_begin(*Context) != PDecl->classmeth_end(*Context)) { |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2991 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2992 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2993 | Result += "\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2994 | } |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2995 | else |
| 2996 | Result += "0\n"; |
| 2997 | Result += "};\n"; |
| 2998 | |
| 2999 | // Mark this protocol as having been generated. |
| 3000 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 3001 | assert(false && "protocol already synthesized"); |
| 3002 | } |
| 3003 | // Output the top lovel protocol meta-data for the class. |
| 3004 | /* struct _objc_protocol_list { |
| 3005 | struct _objc_protocol_list *next; |
| 3006 | int protocol_count; |
| 3007 | struct _objc_protocol *class_protocols[]; |
| 3008 | } |
| 3009 | */ |
| 3010 | Result += "\nstatic struct {\n"; |
| 3011 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3012 | Result += "\tint protocol_count;\n"; |
| 3013 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3014 | Result += utostr(Protocols.size()); |
| 3015 | Result += "];\n} _OBJC_"; |
| 3016 | Result += prefix; |
| 3017 | Result += "_PROTOCOLS_"; |
| 3018 | Result += ClassName; |
| 3019 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3020 | "{\n\t0, "; |
| 3021 | Result += utostr(Protocols.size()); |
| 3022 | Result += "\n"; |
| 3023 | |
| 3024 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3025 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3026 | Result += " \n"; |
| 3027 | |
| 3028 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3029 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3030 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3031 | Result += "\n"; |
| 3032 | } |
| 3033 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3036 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3037 | /// implementation. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3038 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3039 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3040 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3041 | // Find category declaration for this implementation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3042 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3043 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 3044 | CDecl = CDecl->getNextClassCategory()) |
| 3045 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3046 | break; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3047 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3048 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3049 | FullCategoryName += '_'; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3050 | FullCategoryName += IDecl->getNameAsString(); |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3051 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3052 | // Build _objc_method_list for class's instance methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3053 | llvm::SmallVector<ObjCMethodDecl *, 32> |
| 3054 | InstanceMethods(IDecl->instmeth_begin(*Context), |
| 3055 | IDecl->instmeth_end(*Context)); |
| 3056 | |
| 3057 | // If any of our property implementations have associated getters or |
| 3058 | // setters, produce metadata for them as well. |
| 3059 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(*Context), |
| 3060 | PropEnd = IDecl->propimpl_end(*Context); |
| 3061 | Prop != PropEnd; ++Prop) { |
| 3062 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3063 | continue; |
| 3064 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3065 | continue; |
| 3066 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3067 | if (!PD) |
| 3068 | continue; |
| 3069 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3070 | InstanceMethods.push_back(Getter); |
| 3071 | if (PD->isReadOnly()) |
| 3072 | continue; |
| 3073 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3074 | InstanceMethods.push_back(Setter); |
| 3075 | } |
| 3076 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3077 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3078 | Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3079 | |
| 3080 | // Build _objc_method_list for class's class methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3081 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(*Context), |
| 3082 | IDecl->classmeth_end(*Context), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3083 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3084 | Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3085 | |
| 3086 | // Protocols referenced in class declaration? |
Fariborz Jahanian | d256e06 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3087 | // Null CDecl is case of a category implementation with no category interface |
| 3088 | if (CDecl) |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3089 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3090 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3091 | |
| 3092 | /* struct _objc_category { |
| 3093 | char *category_name; |
| 3094 | char *class_name; |
| 3095 | struct _objc_method_list *instance_methods; |
| 3096 | struct _objc_method_list *class_methods; |
| 3097 | struct _objc_protocol_list *protocols; |
| 3098 | // Objective-C 1.0 extensions |
| 3099 | uint32_t size; // sizeof (struct _objc_category) |
| 3100 | struct _objc_property_list *instance_properties; // category's own |
| 3101 | // @property decl. |
| 3102 | }; |
| 3103 | */ |
| 3104 | |
| 3105 | static bool objc_category = false; |
| 3106 | if (!objc_category) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3107 | Result += "\nstruct _objc_category {\n"; |
| 3108 | Result += "\tchar *category_name;\n"; |
| 3109 | Result += "\tchar *class_name;\n"; |
| 3110 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3111 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3112 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3113 | Result += "\tunsigned int size;\n"; |
| 3114 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3115 | Result += "};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3116 | objc_category = true; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3117 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3118 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3119 | Result += FullCategoryName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3120 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3121 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3122 | Result += "\"\n\t, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3123 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3124 | Result += "\"\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3125 | |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3126 | if (IDecl->instmeth_begin(*Context) != IDecl->instmeth_end(*Context)) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3127 | Result += "\t, (struct _objc_method_list *)" |
| 3128 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3129 | Result += FullCategoryName; |
| 3130 | Result += "\n"; |
| 3131 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3132 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3133 | Result += "\t, 0\n"; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3134 | if (IDecl->classmeth_begin(*Context) != IDecl->classmeth_end(*Context)) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3135 | Result += "\t, (struct _objc_method_list *)" |
| 3136 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3137 | Result += FullCategoryName; |
| 3138 | Result += "\n"; |
| 3139 | } |
| 3140 | else |
| 3141 | Result += "\t, 0\n"; |
| 3142 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3143 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3144 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 3145 | Result += FullCategoryName; |
| 3146 | Result += "\n"; |
| 3147 | } |
| 3148 | else |
| 3149 | Result += "\t, 0\n"; |
| 3150 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3153 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3154 | /// ivar offset. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3155 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3156 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3157 | std::string &Result) { |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3158 | if (ivar->isBitField()) { |
| 3159 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3160 | // place all bitfields at offset 0. |
| 3161 | Result += "0"; |
| 3162 | } else { |
| 3163 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3164 | Result += IDecl->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3165 | if (LangOpts.Microsoft) |
| 3166 | Result += "_IMPL"; |
| 3167 | Result += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3168 | Result += ivar->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3169 | Result += ")"; |
| 3170 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3173 | //===----------------------------------------------------------------------===// |
| 3174 | // Meta Data Emission |
| 3175 | //===----------------------------------------------------------------------===// |
| 3176 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3177 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3178 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3179 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3180 | |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3181 | // Explictly declared @interface's are already synthesized. |
Steve Naroff | 7333b49 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 3182 | if (CDecl->isImplicitInterfaceDecl()) { |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3183 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 3184 | // produce correct synthesis as yet. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3185 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3186 | } |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3187 | |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3188 | // Build _objc_ivar_list metadata for classes ivars if needed |
Douglas Gregor | 087dbf3 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3189 | unsigned NumIvars = !IDecl->ivar_empty(*Context) |
| 3190 | ? IDecl->ivar_size(*Context) |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3191 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3192 | if (NumIvars > 0) { |
| 3193 | static bool objc_ivar = false; |
| 3194 | if (!objc_ivar) { |
| 3195 | /* struct _objc_ivar { |
| 3196 | char *ivar_name; |
| 3197 | char *ivar_type; |
| 3198 | int ivar_offset; |
| 3199 | }; |
| 3200 | */ |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3201 | Result += "\nstruct _objc_ivar {\n"; |
| 3202 | Result += "\tchar *ivar_name;\n"; |
| 3203 | Result += "\tchar *ivar_type;\n"; |
| 3204 | Result += "\tint ivar_offset;\n"; |
| 3205 | Result += "};\n"; |
| 3206 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3207 | objc_ivar = true; |
| 3208 | } |
| 3209 | |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3210 | /* struct { |
| 3211 | int ivar_count; |
| 3212 | struct _objc_ivar ivar_list[nIvars]; |
| 3213 | }; |
| 3214 | */ |
| 3215 | Result += "\nstatic struct {\n"; |
| 3216 | Result += "\tint ivar_count;\n"; |
| 3217 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3218 | Result += utostr(NumIvars); |
| 3219 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3220 | Result += IDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3221 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3222 | "{\n\t"; |
| 3223 | Result += utostr(NumIvars); |
| 3224 | Result += "\n"; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3225 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3226 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Douglas Gregor | 087dbf3 | 2009-04-23 03:23:08 +0000 | [diff] [blame] | 3227 | llvm::SmallVector<ObjCIvarDecl *, 8> IVars; |
| 3228 | if (!IDecl->ivar_empty(*Context)) { |
| 3229 | for (ObjCImplementationDecl::ivar_iterator |
| 3230 | IV = IDecl->ivar_begin(*Context), |
| 3231 | IVEnd = IDecl->ivar_end(*Context); |
| 3232 | IV != IVEnd; ++IV) |
| 3233 | IVars.push_back(*IV); |
| 3234 | IVI = IVars.begin(); |
| 3235 | IVE = IVars.end(); |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3236 | } else { |
| 3237 | IVI = CDecl->ivar_begin(); |
| 3238 | IVE = CDecl->ivar_end(); |
| 3239 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3240 | Result += "\t,{{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3241 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3242 | Result += "\", \""; |
| 3243 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3244 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3245 | Result += StrEncoding; |
| 3246 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3247 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3248 | Result += "}\n"; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3249 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3250 | Result += "\t ,{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3251 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3252 | Result += "\", \""; |
| 3253 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3254 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3255 | Result += StrEncoding; |
| 3256 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3257 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3258 | Result += "}\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | // Build _objc_method_list for class's instance methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3265 | llvm::SmallVector<ObjCMethodDecl *, 32> |
| 3266 | InstanceMethods(IDecl->instmeth_begin(*Context), |
| 3267 | IDecl->instmeth_end(*Context)); |
| 3268 | |
| 3269 | // If any of our property implementations have associated getters or |
| 3270 | // setters, produce metadata for them as well. |
| 3271 | for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(*Context), |
| 3272 | PropEnd = IDecl->propimpl_end(*Context); |
| 3273 | Prop != PropEnd; ++Prop) { |
| 3274 | if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 3275 | continue; |
| 3276 | if (!(*Prop)->getPropertyIvarDecl()) |
| 3277 | continue; |
| 3278 | ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); |
| 3279 | if (!PD) |
| 3280 | continue; |
| 3281 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 3282 | InstanceMethods.push_back(Getter); |
| 3283 | if (PD->isReadOnly()) |
| 3284 | continue; |
| 3285 | if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) |
| 3286 | InstanceMethods.push_back(Setter); |
| 3287 | } |
| 3288 | RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3289 | true, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3290 | |
| 3291 | // Build _objc_method_list for class's class methods if needed |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3292 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(*Context), |
| 3293 | IDecl->classmeth_end(*Context), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3294 | false, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3295 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3296 | // Protocols referenced in class declaration? |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3297 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3298 | "CLASS", CDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3299 | |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3300 | |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3301 | // Declaration of class/meta-class metadata |
| 3302 | /* struct _objc_class { |
| 3303 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3304 | const char *super_class_name; |
| 3305 | char *name; |
| 3306 | long version; |
| 3307 | long info; |
| 3308 | long instance_size; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3309 | struct _objc_ivar_list *ivars; |
| 3310 | struct _objc_method_list *methods; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3311 | struct objc_cache *cache; |
| 3312 | struct objc_protocol_list *protocols; |
| 3313 | const char *ivar_layout; |
| 3314 | struct _objc_class_ext *ext; |
| 3315 | }; |
| 3316 | */ |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3317 | static bool objc_class = false; |
| 3318 | if (!objc_class) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3319 | Result += "\nstruct _objc_class {\n"; |
| 3320 | Result += "\tstruct _objc_class *isa;\n"; |
| 3321 | Result += "\tconst char *super_class_name;\n"; |
| 3322 | Result += "\tchar *name;\n"; |
| 3323 | Result += "\tlong version;\n"; |
| 3324 | Result += "\tlong info;\n"; |
| 3325 | Result += "\tlong instance_size;\n"; |
| 3326 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3327 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3328 | Result += "\tstruct objc_cache *cache;\n"; |
| 3329 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3330 | Result += "\tconst char *ivar_layout;\n"; |
| 3331 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3332 | Result += "};\n"; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3333 | objc_class = true; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | // Meta-class metadata generation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3337 | ObjCInterfaceDecl *RootClass = 0; |
| 3338 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3339 | while (SuperClass) { |
| 3340 | RootClass = SuperClass; |
| 3341 | SuperClass = SuperClass->getSuperClass(); |
| 3342 | } |
| 3343 | SuperClass = CDecl->getSuperClass(); |
| 3344 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3345 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3346 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3347 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3348 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3349 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3350 | Result += "\""; |
| 3351 | |
| 3352 | if (SuperClass) { |
| 3353 | Result += ", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3354 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3355 | Result += "\", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3356 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3357 | Result += "\""; |
| 3358 | } |
| 3359 | else { |
| 3360 | Result += ", 0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3361 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3362 | Result += "\""; |
| 3363 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3364 | // 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] | 3365 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3366 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3367 | if (IDecl->classmeth_begin(*Context) != IDecl->classmeth_end(*Context)) { |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3368 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3369 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3370 | Result += "\n"; |
| 3371 | } |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3372 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3373 | Result += ", 0\n"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3374 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3375 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3376 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3377 | Result += ",0,0\n"; |
| 3378 | } |
Fariborz Jahanian | 0cb4d92 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3379 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3380 | Result += "\t,0,0,0,0\n"; |
| 3381 | Result += "};\n"; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3382 | |
| 3383 | // class metadata generation. |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3384 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3385 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3386 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3387 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3388 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3389 | if (SuperClass) { |
| 3390 | Result += ", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3391 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3392 | Result += "\", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3393 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3394 | Result += "\""; |
| 3395 | } |
| 3396 | else { |
| 3397 | Result += ", 0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3398 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3399 | Result += "\""; |
| 3400 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3401 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3402 | Result += ", 0,1"; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3403 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3404 | Result += ",0"; |
| 3405 | else { |
| 3406 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3407 | Result += ",sizeof(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3408 | Result += CDecl->getNameAsString(); |
Steve Naroff | c302e5b | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3409 | if (LangOpts.Microsoft) |
| 3410 | Result += "_IMPL"; |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3411 | Result += ")"; |
| 3412 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3413 | if (NumIvars > 0) { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3414 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
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 += "\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"; |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3420 | if (IDecl->instmeth_begin(*Context) != IDecl->instmeth_end(*Context)) { |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3421 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
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\n\t"; |
| 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"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3427 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3428 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3429 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3430 | Result += ", 0,0\n"; |
| 3431 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3432 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3433 | Result += ",0,0,0\n"; |
| 3434 | Result += "};\n"; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3435 | } |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3436 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3437 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3438 | /// and emits meta-data. |
| 3439 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3440 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3441 | int ClsDefCount = ClassImplementation.size(); |
| 3442 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3443 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3444 | // Rewrite implemented methods |
| 3445 | for (int i = 0; i < ClsDefCount; i++) |
| 3446 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3447 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3448 | for (int i = 0; i < CatDefCount; i++) |
| 3449 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3450 | } |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3451 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3452 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3453 | int ClsDefCount = ClassImplementation.size(); |
| 3454 | int CatDefCount = CategoryImplementation.size(); |
| 3455 | |
Steve Naroff | 5bf1022 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3456 | // This is needed for determining instance variable offsets. |
Steve Naroff | 314c1a6 | 2008-08-05 18:47:23 +0000 | [diff] [blame] | 3457 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3458 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3459 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3460 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3461 | |
| 3462 | // For each implemented category, write out all its meta data. |
| 3463 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3464 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3465 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3466 | // Write objc_symtab metadata |
| 3467 | /* |
| 3468 | struct _objc_symtab |
| 3469 | { |
| 3470 | long sel_ref_cnt; |
| 3471 | SEL *refs; |
| 3472 | short cls_def_cnt; |
| 3473 | short cat_def_cnt; |
| 3474 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3475 | }; |
| 3476 | */ |
| 3477 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3478 | Result += "\nstruct _objc_symtab {\n"; |
| 3479 | Result += "\tlong sel_ref_cnt;\n"; |
| 3480 | Result += "\tSEL *refs;\n"; |
| 3481 | Result += "\tshort cls_def_cnt;\n"; |
| 3482 | Result += "\tshort cat_def_cnt;\n"; |
| 3483 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3484 | Result += "};\n\n"; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3485 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3486 | Result += "static struct _objc_symtab " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3487 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3488 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3489 | + ", " + utostr(CatDefCount) + "\n"; |
| 3490 | for (int i = 0; i < ClsDefCount; i++) { |
| 3491 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3492 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3493 | Result += "\n"; |
| 3494 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3495 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3496 | for (int i = 0; i < CatDefCount; i++) { |
| 3497 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3498 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3499 | Result += "_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3500 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3501 | Result += "\n"; |
| 3502 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3503 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3504 | Result += "};\n\n"; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3505 | |
| 3506 | // Write objc_module metadata |
| 3507 | |
| 3508 | /* |
| 3509 | struct _objc_module { |
| 3510 | long version; |
| 3511 | long size; |
| 3512 | const char *name; |
| 3513 | struct _objc_symtab *symtab; |
| 3514 | } |
| 3515 | */ |
| 3516 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3517 | Result += "\nstruct _objc_module {\n"; |
| 3518 | Result += "\tlong version;\n"; |
| 3519 | Result += "\tlong size;\n"; |
| 3520 | Result += "\tconst char *name;\n"; |
| 3521 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3522 | Result += "};\n\n"; |
| 3523 | Result += "static struct _objc_module " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3524 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3525 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3526 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3527 | Result += "};\n\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3528 | |
| 3529 | if (LangOpts.Microsoft) { |
| 3530 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | bdd2dba | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3531 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3532 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3533 | Result += "&_OBJC_MODULES;\n"; |
| 3534 | Result += "#pragma data_seg(pop)\n\n"; |
| 3535 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3536 | } |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3537 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3538 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3539 | const char *funcName, |
| 3540 | std::string Tag) { |
| 3541 | const FunctionType *AFT = CE->getFunctionType(); |
| 3542 | QualType RT = AFT->getResultType(); |
| 3543 | std::string StructRef = "struct " + Tag; |
| 3544 | std::string S = "static " + RT.getAsString() + " __" + |
| 3545 | funcName + "_" + "block_func_" + utostr(i); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3546 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3547 | BlockDecl *BD = CE->getBlockDecl(); |
| 3548 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3549 | if (isa<FunctionNoProtoType>(AFT)) { |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3550 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3551 | // block (to reference imported block decl refs). |
| 3552 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3553 | } else if (BD->param_empty()) { |
| 3554 | S += "(" + StructRef + " *__cself)"; |
| 3555 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3556 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3557 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3558 | S += '('; |
| 3559 | // first add the implicit argument. |
| 3560 | S += StructRef + " *__cself, "; |
| 3561 | std::string ParamStr; |
| 3562 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3563 | E = BD->param_end(); AI != E; ++AI) { |
| 3564 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3565 | ParamStr = (*AI)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3566 | (*AI)->getType().getAsStringInternal(ParamStr); |
| 3567 | S += ParamStr; |
| 3568 | } |
| 3569 | if (FT->isVariadic()) { |
| 3570 | if (!BD->param_empty()) S += ", "; |
| 3571 | S += "..."; |
| 3572 | } |
| 3573 | S += ')'; |
| 3574 | } |
| 3575 | S += " {\n"; |
| 3576 | |
| 3577 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3578 | // First, emit a declaration for all "by ref" decls. |
| 3579 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3580 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3581 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3582 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3583 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3584 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3585 | } |
| 3586 | // Next, emit a declaration for all "by copy" declarations. |
| 3587 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3588 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3589 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3590 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3591 | // Handle nested closure invocation. For example: |
| 3592 | // |
| 3593 | // void (^myImportedClosure)(void); |
| 3594 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3595 | // |
| 3596 | // void (^anotherClosure)(void); |
| 3597 | // anotherClosure = ^(void) { |
| 3598 | // myImportedClosure(); // import and invoke the closure |
| 3599 | // }; |
| 3600 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3601 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3602 | S += "struct __block_impl *"; |
| 3603 | else |
| 3604 | (*I)->getType().getAsStringInternal(Name); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3605 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3606 | } |
| 3607 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3608 | const char *cstr = RewrittenStr.c_str(); |
| 3609 | while (*cstr++ != '{') ; |
| 3610 | S += cstr; |
| 3611 | S += "\n"; |
| 3612 | return S; |
| 3613 | } |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3614 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3615 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3616 | const char *funcName, |
| 3617 | std::string Tag) { |
| 3618 | std::string StructRef = "struct " + Tag; |
| 3619 | std::string S = "static void __"; |
| 3620 | |
| 3621 | S += funcName; |
| 3622 | S += "_block_copy_" + utostr(i); |
| 3623 | S += "(" + StructRef; |
| 3624 | S += "*dst, " + StructRef; |
| 3625 | S += "*src) {"; |
| 3626 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3627 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3628 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3629 | S += (*I)->getNameAsString(); |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3630 | S += ", (void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3631 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3632 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3633 | } |
| 3634 | S += "\nstatic void __"; |
| 3635 | S += funcName; |
| 3636 | S += "_block_dispose_" + utostr(i); |
| 3637 | S += "(" + StructRef; |
| 3638 | S += "*src) {"; |
| 3639 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3640 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3641 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3642 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3643 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3644 | } |
| 3645 | S += "}\n"; |
| 3646 | return S; |
| 3647 | } |
| 3648 | |
| 3649 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3650 | bool hasCopyDisposeHelpers) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3651 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3652 | std::string Constructor = " " + Tag; |
| 3653 | |
| 3654 | S += " {\n struct __block_impl impl;\n"; |
| 3655 | |
| 3656 | if (hasCopyDisposeHelpers) |
| 3657 | S += " void *copy;\n void *dispose;\n"; |
| 3658 | |
| 3659 | Constructor += "(void *fp"; |
| 3660 | |
| 3661 | if (hasCopyDisposeHelpers) |
| 3662 | Constructor += ", void *copyHelp, void *disposeHelp"; |
| 3663 | |
| 3664 | if (BlockDeclRefs.size()) { |
| 3665 | // Output all "by copy" declarations. |
| 3666 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3667 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3668 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3669 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3670 | std::string ArgName = "_" + FieldName; |
| 3671 | // Handle nested closure invocation. For example: |
| 3672 | // |
| 3673 | // void (^myImportedBlock)(void); |
| 3674 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3675 | // |
| 3676 | // void (^anotherBlock)(void); |
| 3677 | // anotherBlock = ^(void) { |
| 3678 | // myImportedBlock(); // import and invoke the closure |
| 3679 | // }; |
| 3680 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3681 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3682 | S += "struct __block_impl *"; |
| 3683 | Constructor += ", void *" + ArgName; |
| 3684 | } else { |
| 3685 | (*I)->getType().getAsStringInternal(FieldName); |
| 3686 | (*I)->getType().getAsStringInternal(ArgName); |
| 3687 | Constructor += ", " + ArgName; |
| 3688 | } |
| 3689 | S += FieldName + ";\n"; |
| 3690 | } |
| 3691 | // Output all "by ref" declarations. |
| 3692 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3693 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3694 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3695 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3696 | std::string ArgName = "_" + FieldName; |
| 3697 | // Handle nested closure invocation. For example: |
| 3698 | // |
| 3699 | // void (^myImportedBlock)(void); |
| 3700 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3701 | // |
| 3702 | // void (^anotherBlock)(void); |
| 3703 | // anotherBlock = ^(void) { |
| 3704 | // myImportedBlock(); // import and invoke the closure |
| 3705 | // }; |
| 3706 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3707 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3708 | S += "struct __block_impl *"; |
| 3709 | Constructor += ", void *" + ArgName; |
| 3710 | } else { |
| 3711 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName); |
| 3712 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName); |
| 3713 | Constructor += ", " + ArgName; |
| 3714 | } |
| 3715 | S += FieldName + "; // by ref\n"; |
| 3716 | } |
| 3717 | // Finish writing the constructor. |
| 3718 | // FIXME: handle NSConcreteGlobalBlock. |
| 3719 | Constructor += ", int flags=0) {\n"; |
| 3720 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3721 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3722 | |
| 3723 | if (hasCopyDisposeHelpers) |
| 3724 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3725 | |
| 3726 | // Initialize all "by copy" arguments. |
| 3727 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3728 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3729 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3730 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3731 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3732 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3733 | else |
| 3734 | Constructor += Name + " = _"; |
| 3735 | Constructor += Name + ";\n"; |
| 3736 | } |
| 3737 | // Initialize all "by ref" arguments. |
| 3738 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3739 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3740 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3741 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3742 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3743 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3744 | else |
| 3745 | Constructor += Name + " = _"; |
| 3746 | Constructor += Name + ";\n"; |
| 3747 | } |
| 3748 | } else { |
| 3749 | // Finish writing the constructor. |
| 3750 | // FIXME: handle NSConcreteGlobalBlock. |
| 3751 | Constructor += ", int flags=0) {\n"; |
| 3752 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3753 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3754 | if (hasCopyDisposeHelpers) |
| 3755 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3756 | } |
| 3757 | Constructor += " "; |
| 3758 | Constructor += "}\n"; |
| 3759 | S += Constructor; |
| 3760 | S += "};\n"; |
| 3761 | return S; |
| 3762 | } |
| 3763 | |
| 3764 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3765 | const char *FunName) { |
| 3766 | // Insert closures that were part of the function. |
| 3767 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3768 | |
| 3769 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3770 | |
| 3771 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3772 | |
| 3773 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
| 3774 | ImportedBlockDecls.size() > 0); |
| 3775 | |
| 3776 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3777 | |
| 3778 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
| 3779 | |
| 3780 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3781 | |
| 3782 | if (ImportedBlockDecls.size()) { |
| 3783 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3784 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3785 | } |
| 3786 | |
| 3787 | BlockDeclRefs.clear(); |
| 3788 | BlockByRefDecls.clear(); |
| 3789 | BlockByCopyDecls.clear(); |
| 3790 | BlockCallExprs.clear(); |
| 3791 | ImportedBlockDecls.clear(); |
| 3792 | } |
| 3793 | Blocks.clear(); |
| 3794 | RewrittenBlockExprs.clear(); |
| 3795 | } |
| 3796 | |
| 3797 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3798 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3799 | const char *FuncName = FD->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3800 | |
| 3801 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3802 | } |
| 3803 | |
| 3804 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3805 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3806 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3807 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3808 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3809 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3810 | // Convert colons to underscores. |
| 3811 | std::string::size_type loc = 0; |
| 3812 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3813 | FuncName.replace(loc, 1, "_"); |
| 3814 | |
| 3815 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3816 | } |
| 3817 | |
| 3818 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3819 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3820 | CI != E; ++CI) |
| 3821 | if (*CI) { |
| 3822 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3823 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3824 | else |
| 3825 | GetBlockDeclRefExprs(*CI); |
| 3826 | } |
| 3827 | // Handle specific things. |
| 3828 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3829 | // FIXME: Handle enums. |
| 3830 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3831 | BlockDeclRefs.push_back(CDRE); |
| 3832 | return; |
| 3833 | } |
| 3834 | |
| 3835 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3836 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3837 | CI != E; ++CI) |
| 3838 | if (*CI) { |
| 3839 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3840 | GetBlockCallExprs(CBE->getBody()); |
| 3841 | else |
| 3842 | GetBlockCallExprs(*CI); |
| 3843 | } |
| 3844 | |
| 3845 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3846 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3847 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3848 | } |
| 3849 | } |
| 3850 | return; |
| 3851 | } |
| 3852 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3853 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3854 | // Navigate to relevant type information. |
| 3855 | const char *closureName = 0; |
| 3856 | const BlockPointerType *CPT = 0; |
| 3857 | |
| 3858 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3859 | closureName = DRE->getDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3860 | CPT = DRE->getType()->getAsBlockPointerType(); |
| 3861 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3862 | closureName = CDRE->getDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3863 | CPT = CDRE->getType()->getAsBlockPointerType(); |
| 3864 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3865 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3866 | CPT = MExpr->getType()->getAsBlockPointerType(); |
| 3867 | } else { |
| 3868 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3869 | } |
| 3870 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3871 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3872 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3873 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3874 | // FTP will be null for closures that don't take arguments. |
| 3875 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3876 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3877 | SourceLocation(), |
| 3878 | &Context->Idents.get("__block_impl")); |
| 3879 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3880 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3881 | // Generate a funky cast. |
| 3882 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3883 | |
| 3884 | // Push the block argument type. |
| 3885 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3886 | if (FTP) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3887 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3888 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3889 | QualType t = *I; |
| 3890 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3891 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3892 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 3893 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3894 | } |
| 3895 | ArgTypes.push_back(t); |
| 3896 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3897 | } |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3898 | // Now do the pointer to function cast. |
| 3899 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
| 3900 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
| 3901 | |
| 3902 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3903 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3904 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, Exp->getCallee(), |
| 3905 | PtrBlock, SourceLocation(), |
| 3906 | SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3907 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3908 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3909 | BlkCast); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3910 | //PE->dump(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3911 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3912 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3913 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3914 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3915 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 3916 | FD->getType()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3917 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3918 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, ME, |
| 3919 | PtrToFuncCastType, |
| 3920 | SourceLocation(), |
| 3921 | SourceLocation()); |
| 3922 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3923 | |
| 3924 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3925 | // Add the implicit argument. |
| 3926 | BlkExprs.push_back(BlkCast); |
| 3927 | // Add the user arguments. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3928 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3929 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3930 | BlkExprs.push_back(*I); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3931 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3932 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3933 | BlkExprs.size(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3934 | Exp->getType(), SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3935 | return CE; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3939 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3940 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | void RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
| 3944 | // FIXME: Add more elaborate code generation required by the ABI. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3945 | Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3946 | Context->getPointerType(BDRE->getType()), |
| 3947 | SourceLocation()); |
| 3948 | // Need parens to enforce precedence. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3949 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3950 | ReplaceStmt(BDRE, PE); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3953 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3954 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3955 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3956 | |
| 3957 | // Need to avoid trying to rewrite synthesized casts. |
| 3958 | if (LocStart.isInvalid()) |
| 3959 | return; |
Steve Naroff | 1c53c02 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3960 | // Need to avoid trying to rewrite casts contained in macros. |
| 3961 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3962 | return; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3963 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3964 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3965 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 3966 | |
| 3967 | // advance the location to startArgList. |
| 3968 | const char *argPtr = startBuf; |
| 3969 | |
| 3970 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3971 | switch (*argPtr) { |
| 3972 | case '^': |
| 3973 | // Replace the '^' with '*'. |
| 3974 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 3975 | ReplaceText(LocStart, 1, "*", 1); |
| 3976 | break; |
| 3977 | } |
| 3978 | } |
| 3979 | return; |
| 3980 | } |
| 3981 | |
| 3982 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3983 | SourceLocation DeclLoc = FD->getLocation(); |
| 3984 | unsigned parenCount = 0; |
| 3985 | |
| 3986 | // We have 1 or more arguments that have closure pointers. |
| 3987 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3988 | const char *startArgList = strchr(startBuf, '('); |
| 3989 | |
| 3990 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 3991 | |
| 3992 | parenCount++; |
| 3993 | // advance the location to startArgList. |
| 3994 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 3995 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 3996 | |
| 3997 | const char *argPtr = startArgList; |
| 3998 | |
| 3999 | while (*argPtr++ && parenCount) { |
| 4000 | switch (*argPtr) { |
| 4001 | case '^': |
| 4002 | // Replace the '^' with '*'. |
| 4003 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 4004 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4005 | break; |
| 4006 | case '(': |
| 4007 | parenCount++; |
| 4008 | break; |
| 4009 | case ')': |
| 4010 | parenCount--; |
| 4011 | break; |
| 4012 | } |
| 4013 | } |
| 4014 | return; |
| 4015 | } |
| 4016 | |
| 4017 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4018 | const FunctionProtoType *FTP; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4019 | const PointerType *PT = QT->getAsPointerType(); |
| 4020 | if (PT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4021 | FTP = PT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4022 | } else { |
| 4023 | const BlockPointerType *BPT = QT->getAsBlockPointerType(); |
| 4024 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4025 | FTP = BPT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4026 | } |
| 4027 | if (FTP) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4028 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4029 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4030 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4031 | return true; |
| 4032 | } |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4036 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 4037 | const char *&RParen) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4038 | const char *argPtr = strchr(Name, '('); |
| 4039 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 4040 | |
| 4041 | LParen = argPtr; // output the start. |
| 4042 | argPtr++; // skip past the left paren. |
| 4043 | unsigned parenCount = 1; |
| 4044 | |
| 4045 | while (*argPtr && parenCount) { |
| 4046 | switch (*argPtr) { |
| 4047 | case '(': parenCount++; break; |
| 4048 | case ')': parenCount--; break; |
| 4049 | default: break; |
| 4050 | } |
| 4051 | if (parenCount) argPtr++; |
| 4052 | } |
| 4053 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 4054 | RParen = argPtr; // output the end |
| 4055 | } |
| 4056 | |
| 4057 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 4058 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 4059 | RewriteBlockPointerFunctionArgs(FD); |
| 4060 | return; |
| 4061 | } |
| 4062 | // Handle Variables and Typedefs. |
| 4063 | SourceLocation DeclLoc = ND->getLocation(); |
| 4064 | QualType DeclT; |
| 4065 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4066 | DeclT = VD->getType(); |
| 4067 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4068 | DeclT = TDD->getUnderlyingType(); |
| 4069 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4070 | DeclT = FD->getType(); |
| 4071 | else |
| 4072 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4073 | |
| 4074 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4075 | const char *endBuf = startBuf; |
| 4076 | // scan backward (from the decl location) for the end of the previous decl. |
| 4077 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4078 | startBuf--; |
| 4079 | |
| 4080 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4081 | // may take block argument types (which will be handled below). |
| 4082 | if (*startBuf == '^') { |
| 4083 | // Replace the '^' with '*', computing a negative offset. |
| 4084 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4085 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4086 | } |
| 4087 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4088 | // Replace the '^' with '*' for arguments. |
| 4089 | DeclLoc = ND->getLocation(); |
| 4090 | startBuf = SM->getCharacterData(DeclLoc); |
| 4091 | const char *argListBegin, *argListEnd; |
| 4092 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4093 | while (argListBegin < argListEnd) { |
| 4094 | if (*argListBegin == '^') { |
| 4095 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4096 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4097 | } |
| 4098 | argListBegin++; |
| 4099 | } |
| 4100 | } |
| 4101 | return; |
| 4102 | } |
| 4103 | |
| 4104 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 4105 | // Add initializers for any closure decl refs. |
| 4106 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4107 | if (BlockDeclRefs.size()) { |
| 4108 | // Unique all "by copy" declarations. |
| 4109 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4110 | if (!BlockDeclRefs[i]->isByRef()) |
| 4111 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4112 | // Unique all "by ref" declarations. |
| 4113 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4114 | if (BlockDeclRefs[i]->isByRef()) { |
| 4115 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4116 | } |
| 4117 | // Find any imported blocks...they will need special attention. |
| 4118 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4119 | if (BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | e59c8b1 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4120 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4121 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4126 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4127 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4128 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4129 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4130 | ID, FType, FunctionDecl::Extern, false, |
| 4131 | false); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4134 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4135 | Blocks.push_back(Exp); |
| 4136 | |
| 4137 | CollectBlockDeclRefInfo(Exp); |
| 4138 | std::string FuncName; |
| 4139 | |
| 4140 | if (CurFunctionDef) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4141 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4142 | else if (CurMethodDef) { |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4143 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4144 | // Convert colons to underscores. |
| 4145 | std::string::size_type loc = 0; |
| 4146 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4147 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4148 | } else if (GlobalVarDecl) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4149 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4150 | |
| 4151 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4152 | |
| 4153 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4154 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4155 | |
| 4156 | // Get a pointer to the function type so we can cast appropriately. |
| 4157 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4158 | |
| 4159 | FunctionDecl *FD; |
| 4160 | Expr *NewRep; |
| 4161 | |
| 4162 | // Simulate a contructor call... |
| 4163 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4164 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4165 | |
| 4166 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 4167 | |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4168 | // Initialize the block function. |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4169 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4170 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4171 | SourceLocation()); |
| 4172 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4173 | Context->VoidPtrTy, SourceLocation(), |
| 4174 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4175 | InitExprs.push_back(castExpr); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4176 | |
| 4177 | if (ImportedBlockDecls.size()) { |
| 4178 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4179 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4180 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4181 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4182 | Context->VoidPtrTy, SourceLocation(), |
| 4183 | SourceLocation()); |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4184 | InitExprs.push_back(castExpr); |
| 4185 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4186 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4187 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4188 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4189 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4190 | Context->VoidPtrTy, SourceLocation(), |
| 4191 | SourceLocation()); |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4192 | InitExprs.push_back(castExpr); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4193 | } |
| 4194 | // Add initializers for any closure decl refs. |
| 4195 | if (BlockDeclRefs.size()) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4196 | Expr *Exp; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4197 | // Output all "by copy" declarations. |
| 4198 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4199 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4200 | if (isObjCType((*I)->getType())) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4201 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4202 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4203 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4204 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4205 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4206 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4207 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4208 | Context->VoidPtrTy, SourceLocation(), |
| 4209 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4210 | } else { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4211 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4212 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4213 | } |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4214 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4215 | } |
| 4216 | // Output all "by ref" declarations. |
| 4217 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4218 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4219 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4220 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4221 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4222 | Context->getPointerType(Exp->getType()), |
| 4223 | SourceLocation()); |
Steve Naroff | 362c756 | 2008-11-14 21:36:12 +0000 | [diff] [blame] | 4224 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4225 | } |
| 4226 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4227 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4228 | FType, SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4229 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4230 | Context->getPointerType(NewRep->getType()), |
| 4231 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4232 | NewRep = new (Context) CStyleCastExpr(FType, NewRep, FType, SourceLocation(), |
| 4233 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4234 | BlockDeclRefs.clear(); |
| 4235 | BlockByRefDecls.clear(); |
| 4236 | BlockByCopyDecls.clear(); |
| 4237 | ImportedBlockDecls.clear(); |
| 4238 | return NewRep; |
| 4239 | } |
| 4240 | |
| 4241 | //===----------------------------------------------------------------------===// |
| 4242 | // Function Body / Expression rewriting |
| 4243 | //===----------------------------------------------------------------------===// |
| 4244 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4245 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4246 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4247 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4248 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4249 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4250 | // we get this right. |
| 4251 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4252 | // Perform a bottom up traversal of all children. |
| 4253 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4254 | CI != E; ++CI) |
| 4255 | if (*CI) |
| 4256 | CollectPropertySetters(*CI); |
| 4257 | |
| 4258 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4259 | if (BinOp->isAssignmentOp()) { |
| 4260 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4261 | PropSetters[PRE] = BinOp; |
| 4262 | } |
| 4263 | } |
| 4264 | } |
| 4265 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4266 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4267 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4268 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4269 | Stmts.push_back(S); |
| 4270 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4271 | Stmts.push_back(S); |
| 4272 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4273 | } |
| 4274 | |
| 4275 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4276 | |
| 4277 | // Perform a bottom up rewrite of all children. |
| 4278 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4279 | CI != E; ++CI) |
| 4280 | if (*CI) { |
| 4281 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
| 4282 | if (newStmt) |
| 4283 | *CI = newStmt; |
| 4284 | } |
| 4285 | |
| 4286 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4287 | // Rewrite the block body in place. |
| 4288 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4289 | |
| 4290 | // 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] | 4291 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4292 | RewrittenBlockExprs[BE] = Str; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4293 | |
| 4294 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4295 | //blockTranscribed->dump(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4296 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4297 | return blockTranscribed; |
| 4298 | } |
| 4299 | // Handle specific things. |
| 4300 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4301 | return RewriteAtEncode(AtEncode); |
| 4302 | |
| 4303 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4304 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4305 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4306 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4307 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4308 | if (BinOp) { |
| 4309 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4310 | // 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] | 4311 | DisableReplaceStmt = true; |
| 4312 | // Save the source range. Even if we disable the replacement, the |
| 4313 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4314 | // node is at the 'end', the rewriter will fail. Consider this: |
| 4315 | // self.errorHandler = handler ? handler : |
| 4316 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4317 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4318 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4319 | DisableReplaceStmt = false; |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4320 | // |
| 4321 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4322 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4323 | // to see the original expression). Consider this example: |
| 4324 | // |
| 4325 | // Foo *obj1, *obj2; |
| 4326 | // |
| 4327 | // obj1.i = [obj2 rrrr]; |
| 4328 | // |
| 4329 | // 'BinOp' for the previous expression looks like: |
| 4330 | // |
| 4331 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4332 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4333 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4334 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4335 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4336 | // |
| 4337 | // 'newStmt' represents the rewritten message expression. For example: |
| 4338 | // |
| 4339 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4340 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4341 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4342 | // (CStyleCastExpr 0x231d220 'void *' |
| 4343 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4344 | // |
| 4345 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4346 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4347 | // the original RHS (since we haven't altered BinOp). |
| 4348 | // |
| 4349 | // This implies the Rewrite* routines can no longer delete the original |
| 4350 | // node. As a result, we now leak the original AST nodes. |
| 4351 | // |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4352 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4353 | } else { |
| 4354 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4355 | } |
| 4356 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4357 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4358 | return RewriteAtSelector(AtSelector); |
| 4359 | |
| 4360 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4361 | return RewriteObjCStringLiteral(AtString); |
| 4362 | |
| 4363 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4364 | #if 0 |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4365 | // Before we rewrite it, put the original message expression in a comment. |
| 4366 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4367 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4368 | |
| 4369 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4370 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4371 | |
| 4372 | std::string messString; |
| 4373 | messString += "// "; |
| 4374 | messString.append(startBuf, endBuf-startBuf+1); |
| 4375 | messString += "\n"; |
| 4376 | |
| 4377 | // FIXME: Missing definition of |
| 4378 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4379 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4380 | // Tried this, but it didn't work either... |
| 4381 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4382 | #endif |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4383 | return RewriteMessageExpr(MessExpr); |
| 4384 | } |
| 4385 | |
| 4386 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4387 | return RewriteObjCTryStmt(StmtTry); |
| 4388 | |
| 4389 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4390 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4391 | |
| 4392 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4393 | return RewriteObjCThrowStmt(StmtThrow); |
| 4394 | |
| 4395 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4396 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4397 | |
| 4398 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4399 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4400 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4401 | OrigStmtRange.getEnd()); |
| 4402 | if (BreakStmt *StmtBreakStmt = |
| 4403 | dyn_cast<BreakStmt>(S)) |
| 4404 | return RewriteBreakStmt(StmtBreakStmt); |
| 4405 | if (ContinueStmt *StmtContinueStmt = |
| 4406 | dyn_cast<ContinueStmt>(S)) |
| 4407 | return RewriteContinueStmt(StmtContinueStmt); |
| 4408 | |
| 4409 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4410 | // and cast exprs. |
| 4411 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4412 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4413 | // precedes the first Decl. In the future the DeclGroup should have |
| 4414 | // a separate type-specifier that we can rewrite. |
| 4415 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4416 | |
| 4417 | // Blocks rewrite rules. |
| 4418 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4419 | DI != DE; ++DI) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4420 | Decl *SD = *DI; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4421 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4422 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4423 | RewriteBlockPointerDecl(ND); |
| 4424 | else if (ND->getType()->isFunctionPointerType()) |
| 4425 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4426 | } |
| 4427 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4428 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4429 | RewriteBlockPointerDecl(TD); |
| 4430 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4431 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4432 | } |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4437 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4438 | |
| 4439 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4440 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4441 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4442 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4443 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4444 | && "Statement stack mismatch"); |
| 4445 | Stmts.pop_back(); |
| 4446 | } |
| 4447 | // Handle blocks rewriting. |
| 4448 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4449 | if (BDRE->isByRef()) |
| 4450 | RewriteBlockDeclRefExpr(BDRE); |
| 4451 | } |
| 4452 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4453 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4454 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4455 | ReplaceStmt(S, BlockCall); |
| 4456 | return BlockCall; |
| 4457 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4458 | } |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4459 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4460 | RewriteCastExpr(CE); |
| 4461 | } |
| 4462 | #if 0 |
| 4463 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4464 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4465 | // Get the new text. |
| 4466 | std::string SStr; |
| 4467 | llvm::raw_string_ostream Buf(SStr); |
| 4468 | Replacement->printPretty(Buf); |
| 4469 | const std::string &Str = Buf.str(); |
| 4470 | |
| 4471 | printf("CAST = %s\n", &Str[0]); |
| 4472 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4473 | delete S; |
| 4474 | return Replacement; |
| 4475 | } |
| 4476 | #endif |
| 4477 | // Return this stmt unmodified. |
| 4478 | return S; |
| 4479 | } |
| 4480 | |
| 4481 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4482 | /// main file of the input. |
| 4483 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4484 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | ee8d497 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 4485 | if (FD->isOverloadedOperator()) |
| 4486 | return; |
| 4487 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4488 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4489 | // prototype. This enables us to rewrite function declarations and |
| 4490 | // definitions using the same code. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4491 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4492 | |
Sebastian Redl | bc9ef25 | 2009-04-26 20:35:05 +0000 | [diff] [blame^] | 4493 | // FIXME: If this should support Obj-C++, support CXXTryStmt |
| 4494 | if (CompoundStmt *Body = FD->getCompoundBody(*Context)) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4495 | CurFunctionDef = FD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4496 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4497 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4498 | Body = |
| 4499 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4500 | FD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4501 | CurrentBody = 0; |
| 4502 | if (PropParentMap) { |
| 4503 | delete PropParentMap; |
| 4504 | PropParentMap = 0; |
| 4505 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4506 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4507 | // and any copy/dispose helper functions. |
| 4508 | InsertBlockLiteralsWithinFunction(FD); |
| 4509 | CurFunctionDef = 0; |
| 4510 | } |
| 4511 | return; |
| 4512 | } |
| 4513 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4514 | if (CompoundStmt *Body = MD->getBody()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4515 | CurMethodDef = MD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4516 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4517 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4518 | Body = |
| 4519 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4520 | MD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4521 | CurrentBody = 0; |
| 4522 | if (PropParentMap) { |
| 4523 | delete PropParentMap; |
| 4524 | PropParentMap = 0; |
| 4525 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4526 | InsertBlockLiteralsWithinMethod(MD); |
| 4527 | CurMethodDef = 0; |
| 4528 | } |
| 4529 | } |
| 4530 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4531 | ClassImplementation.push_back(CI); |
| 4532 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4533 | CategoryImplementation.push_back(CI); |
| 4534 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4535 | RewriteForwardClassDecl(CD); |
| 4536 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4537 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4538 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4539 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4540 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4541 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4542 | if (VD->getInit()) { |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4543 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4544 | RewriteCastExpr(CE); |
| 4545 | } |
| 4546 | } |
| 4547 | } |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4548 | if (VD->getInit()) { |
| 4549 | GlobalVarDecl = VD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4550 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4551 | CurrentBody = VD->getInit(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4552 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4553 | CurrentBody = 0; |
| 4554 | if (PropParentMap) { |
| 4555 | delete PropParentMap; |
| 4556 | PropParentMap = 0; |
| 4557 | } |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 4558 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4559 | VD->getNameAsCString()); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4560 | GlobalVarDecl = 0; |
| 4561 | |
| 4562 | // This is needed for blocks. |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4563 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4564 | RewriteCastExpr(CE); |
| 4565 | } |
| 4566 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4567 | return; |
| 4568 | } |
| 4569 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4570 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4571 | RewriteBlockPointerDecl(TD); |
| 4572 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4573 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4574 | return; |
| 4575 | } |
| 4576 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 4577 | if (RD->isDefinition()) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4578 | for (RecordDecl::field_iterator i = RD->field_begin(*Context), |
| 4579 | e = RD->field_end(*Context); i != e; ++i) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4580 | FieldDecl *FD = *i; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4581 | if (isTopLevelBlockPointerType(FD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4582 | RewriteBlockPointerDecl(FD); |
| 4583 | } |
| 4584 | } |
| 4585 | return; |
| 4586 | } |
| 4587 | // Nothing yet. |
| 4588 | } |
| 4589 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4590 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4591 | // Get the top-level buffer that this corresponds to. |
| 4592 | |
| 4593 | // Rewrite tabs if we care. |
| 4594 | //RewriteTabs(); |
| 4595 | |
| 4596 | if (Diags.hasErrorOccurred()) |
| 4597 | return; |
| 4598 | |
| 4599 | // Create the output file. |
| 4600 | |
| 4601 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 4602 | llvm::raw_ostream *OutFile; |
| 4603 | if (OutFileName == "-") { |
| 4604 | OutFile = &llvm::outs(); |
| 4605 | } else if (!OutFileName.empty()) { |
| 4606 | std::string Err; |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 4607 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), |
| 4608 | // set binary mode (critical for Windoze) |
| 4609 | true, |
| 4610 | Err); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4611 | OwnedStream.reset(OutFile); |
| 4612 | } else if (InFileName == "-") { |
| 4613 | OutFile = &llvm::outs(); |
| 4614 | } else { |
| 4615 | llvm::sys::Path Path(InFileName); |
| 4616 | Path.eraseSuffix(); |
| 4617 | Path.appendSuffix("cpp"); |
| 4618 | std::string Err; |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 4619 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), |
| 4620 | // set binary mode (critical for Windoze) |
| 4621 | true, |
| 4622 | Err); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4623 | OwnedStream.reset(OutFile); |
| 4624 | } |
| 4625 | |
| 4626 | RewriteInclude(); |
| 4627 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 4628 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4629 | Preamble.c_str(), Preamble.size(), false); |
| 4630 | |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4631 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4632 | RewriteImplementations(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4633 | |
| 4634 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4635 | // we are done. |
| 4636 | if (const RewriteBuffer *RewriteBuf = |
| 4637 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4638 | //printf("Changed:\n"); |
| 4639 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4640 | } else { |
| 4641 | fprintf(stderr, "No changes\n"); |
| 4642 | } |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4643 | |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4644 | if (ClassImplementation.size() || CategoryImplementation.size()) { |
| 4645 | // Rewrite Objective-c meta data* |
| 4646 | std::string ResultStr; |
| 4647 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4648 | // Emit metadata. |
| 4649 | *OutFile << ResultStr; |
| 4650 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4651 | OutFile->flush(); |
| 4652 | } |
| 4653 | |