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. |
| 129 | virtual void HandleTopLevelDecl(Decl *D); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 130 | void HandleDeclInMainFile(Decl *D); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 131 | RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 132 | Diagnostic &D, const LangOptions &LOpts); |
Ted Kremenek | cab0b0c | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 133 | |
| 134 | ~RewriteObjC() {} |
| 135 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 136 | virtual void HandleTranslationUnit(ASTContext &C); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 137 | |
| 138 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 139 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 140 | |
| 141 | if (ReplacingStmt) |
| 142 | return; // We can't rewrite the same node twice. |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 143 | |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 144 | if (DisableReplaceStmt) |
| 145 | return; // Used when rewriting the assignment of a property setter. |
| 146 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 147 | // If replacement succeeded or warning disabled return with no warning. |
| 148 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 149 | ReplacedNodes[Old] = New; |
| 150 | return; |
| 151 | } |
| 152 | if (SilenceRewriteMacroWarning) |
| 153 | return; |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 154 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 155 | << Old->getSourceRange(); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 156 | } |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 157 | |
| 158 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 159 | // Measaure the old text. |
| 160 | int Size = Rewrite.getRangeSize(SrcRange); |
| 161 | if (Size == -1) { |
| 162 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 163 | << Old->getSourceRange(); |
| 164 | return; |
| 165 | } |
| 166 | // Get the new text. |
| 167 | std::string SStr; |
| 168 | llvm::raw_string_ostream S(SStr); |
| 169 | New->printPretty(S); |
| 170 | const std::string &Str = S.str(); |
| 171 | |
| 172 | // If replacement succeeded or warning disabled return with no warning. |
| 173 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, &Str[0], Str.size())) { |
| 174 | ReplacedNodes[Old] = New; |
| 175 | return; |
| 176 | } |
| 177 | if (SilenceRewriteMacroWarning) |
| 178 | return; |
| 179 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 180 | << Old->getSourceRange(); |
| 181 | } |
| 182 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 183 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 184 | bool InsertAfter = true) { |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 185 | // If insertion succeeded or warning disabled return with no warning. |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 186 | if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) || |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 187 | SilenceRewriteMacroWarning) |
| 188 | return; |
| 189 | |
| 190 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 191 | } |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 192 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 193 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 194 | // If removal succeeded or warning disabled return with no warning. |
| 195 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 196 | return; |
| 197 | |
| 198 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 199 | } |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 200 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 201 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 202 | const char *NewStr, unsigned NewLength) { |
| 203 | // If removal succeeded or warning disabled return with no warning. |
| 204 | if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) || |
| 205 | SilenceRewriteMacroWarning) |
| 206 | return; |
| 207 | |
| 208 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 209 | } |
| 210 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 211 | // Syntactic Rewriting. |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 212 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 213 | void RewriteInclude(); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 214 | void RewriteTabs(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 215 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 216 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 217 | ObjCImplementationDecl *IMD, |
| 218 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 219 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 220 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 221 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 222 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 223 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 224 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 225 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 226 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 227 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 228 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 229 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 230 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 231 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 232 | QualType getSuperStructType(); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 233 | QualType getConstantStringStructType(); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 234 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 235 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 236 | // Expression Rewriting. |
Steve Naroff | 334fbc2 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 237 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 238 | void CollectPropertySetters(Stmt *S); |
| 239 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 240 | Stmt *CurrentBody; |
| 241 | ParentMap *PropParentMap; // created lazily. |
| 242 | |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 243 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 244 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 245 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 246 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 247 | SourceRange SrcRange); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 248 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 249 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 250 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 251 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 252 | void WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 253 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 254 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 255 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 256 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 257 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 2c02216 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 258 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 259 | SourceLocation OrigEnd); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 260 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 261 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 262 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 263 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 264 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 265 | void SynthCountByEnumWithState(std::string &buf); |
| 266 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 267 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 268 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 269 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 270 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 271 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 272 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 273 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 274 | void SynthSelGetUidFunctionDecl(); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 275 | void SynthGetProtocolFunctionDecl(); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 276 | void SynthSuperContructorFunctionDecl(); |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 277 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 278 | // Metadata emission. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 279 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 280 | std::string &Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 281 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 282 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 283 | std::string &Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 285 | typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator; |
| 286 | void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 287 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | a398637 | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 288 | bool IsInstanceMethod, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 289 | const char *prefix, |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 290 | const char *ClassName, |
| 291 | std::string &Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 293 | void RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> |
| 294 | &Protocols, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 295 | const char *prefix, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 296 | const char *ClassName, |
| 297 | std::string &Result); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 298 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 299 | std::string &Result); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 300 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 301 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 302 | std::string &Result); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 303 | void RewriteImplementations(); |
| 304 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 305 | |
| 306 | // Block rewriting. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 307 | void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 308 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 309 | |
| 310 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 311 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 312 | |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 313 | // Block specific rewrite rules. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 314 | void RewriteBlockCall(CallExpr *Exp); |
| 315 | void RewriteBlockPointerDecl(NamedDecl *VD); |
| 316 | void RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
| 317 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 318 | |
| 319 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 320 | const char *funcName, std::string Tag); |
| 321 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 322 | const char *funcName, std::string Tag); |
| 323 | std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 324 | bool hasCopyDisposeHelpers); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 325 | Stmt *SynthesizeBlockCall(CallExpr *Exp); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 326 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 327 | const char *FunName); |
| 328 | |
| 329 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 330 | void GetBlockCallExprs(Stmt *S); |
| 331 | void GetBlockDeclRefExprs(Stmt *S); |
| 332 | |
| 333 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 334 | // 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] | 335 | bool isTopLevelBlockPointerType(QualType T) { |
| 336 | return isa<BlockPointerType>(T); |
| 337 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 338 | |
| 339 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 340 | bool isObjCType(QualType T) { |
| 341 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 342 | return false; |
| 343 | |
| 344 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 345 | |
| 346 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 347 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 348 | return true; |
| 349 | |
| 350 | if (const PointerType *PT = OCT->getAsPointerType()) { |
| 351 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 352 | isa<ObjCQualifiedIdType>(PT->getPointeeType())) |
| 353 | return true; |
| 354 | } |
| 355 | return false; |
| 356 | } |
| 357 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 358 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 359 | const char *&RParen); |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 360 | void RewriteCastExpr(CStyleCastExpr *CE); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 361 | |
| 362 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 363 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 364 | }; |
| 365 | } |
| 366 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 367 | void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 368 | NamedDecl *D) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 369 | if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { |
| 370 | for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 371 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 372 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 373 | // All the args are checked/rewritten. Don't call twice! |
| 374 | RewriteBlockPointerDecl(D); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 381 | const PointerType *PT = funcType->getAsPointerType(); |
| 382 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 383 | RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 386 | static bool IsHeaderFile(const std::string &Filename) { |
| 387 | std::string::size_type DotPos = Filename.rfind('.'); |
| 388 | |
| 389 | if (DotPos == std::string::npos) { |
| 390 | // no file extension |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 395 | // C header: .h |
| 396 | // C++ header: .hh or .H; |
| 397 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 398 | } |
| 399 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 400 | RewriteObjC::RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 401 | Diagnostic &D, const LangOptions &LOpts) |
| 402 | : Diags(D), LangOpts(LOpts) { |
| 403 | IsHeader = IsHeaderFile(inFile); |
| 404 | InFileName = inFile; |
| 405 | OutFileName = outFile; |
| 406 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 407 | "rewriting sub-expression within a macro (may not be correct)"); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 408 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 409 | "rewriter doesn't support user-specified control flow semantics " |
| 410 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | cfd9f4c | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Fariborz Jahanian | 8d2080c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 413 | ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, |
Chris Lattner | 673f2bd | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 414 | const std::string& OutFile, |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 415 | Diagnostic &Diags, |
| 416 | const LangOptions &LOpts) { |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 417 | return new RewriteObjC(InFile, OutFile, Diags, LOpts); |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 418 | } |
Chris Lattner | b429ae4 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 419 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 420 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 421 | Context = &context; |
| 422 | SM = &Context->getSourceManager(); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 423 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 424 | MsgSendFunctionDecl = 0; |
| 425 | MsgSendSuperFunctionDecl = 0; |
| 426 | MsgSendStretFunctionDecl = 0; |
| 427 | MsgSendSuperStretFunctionDecl = 0; |
| 428 | MsgSendFpretFunctionDecl = 0; |
| 429 | GetClassFunctionDecl = 0; |
| 430 | GetMetaClassFunctionDecl = 0; |
| 431 | SelGetUidFunctionDecl = 0; |
| 432 | CFStringFunctionDecl = 0; |
| 433 | GetProtocolFunctionDecl = 0; |
| 434 | ConstantStringClassReference = 0; |
| 435 | NSStringRecord = 0; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 436 | CurMethodDef = 0; |
| 437 | CurFunctionDef = 0; |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 438 | GlobalVarDecl = 0; |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 439 | SuperStructDecl = 0; |
Steve Naroff | 053ae3f | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 440 | ConstantStringDecl = 0; |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 441 | BcLabelCount = 0; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 442 | SuperContructorFunctionDecl = 0; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 443 | NumObjCStringLiterals = 0; |
Steve Naroff | bad6f3b | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 444 | PropParentMap = 0; |
| 445 | CurrentBody = 0; |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 446 | DisableReplaceStmt = false; |
| 447 | |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 448 | // Get the ID and start/end of the main file. |
| 449 | MainFileID = SM->getMainFileID(); |
| 450 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 451 | MainFileStart = MainBuf->getBufferStart(); |
| 452 | MainFileEnd = MainBuf->getBufferEnd(); |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 454 | Rewrite.setSourceMgr(Context->getSourceManager()); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 456 | // declaring objc_selector outside the parameter list removes a silly |
| 457 | // scope related warning... |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 458 | if (IsHeader) |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 459 | Preamble = "#pragma once\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 460 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 461 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 462 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 463 | if (LangOpts.Microsoft) { |
| 464 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 465 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 466 | ": "; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 467 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 468 | } |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 469 | Preamble += "};\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 470 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 471 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 472 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 473 | Preamble += "#endif\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 474 | if (LangOpts.Microsoft) { |
| 475 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 476 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 477 | } else |
| 478 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 479 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 480 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 481 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 482 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 483 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
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_stret"; |
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 double objc_msgSend_fpret"; |
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_getClass"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 490 | Preamble += "(const char *);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 491 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 492 | Preamble += "(const char *);\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 493 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 494 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 495 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 496 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 497 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 3e2c98c | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 498 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | cd25d78 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 499 | // @synchronized hooks. |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 500 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 501 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 502 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 503 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 504 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 505 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | c960e9d | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 506 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 507 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 508 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 509 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 510 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 511 | Preamble += "#endif\n"; |
| 512 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 513 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 514 | Preamble += " int *isa;\n"; |
| 515 | Preamble += " int flags;\n"; |
| 516 | Preamble += " char *str;\n"; |
| 517 | Preamble += " long length;\n"; |
| 518 | Preamble += "};\n"; |
Steve Naroff | c6e571e | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 519 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 520 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 521 | Preamble += "#else\n"; |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 522 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | c6e571e | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 523 | Preamble += "#endif\n"; |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 524 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 525 | Preamble += "#endif\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 526 | // Blocks preamble. |
| 527 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 528 | Preamble += "#define BLOCK_IMPL\n"; |
| 529 | Preamble += "struct __block_impl {\n"; |
| 530 | Preamble += " void *isa;\n"; |
| 531 | Preamble += " int Flags;\n"; |
| 532 | Preamble += " int Size;\n"; |
| 533 | Preamble += " void *FuncPtr;\n"; |
| 534 | Preamble += "};\n"; |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 535 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 536 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 537 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 538 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 539 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteStackBlock[32];\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 540 | Preamble += "#endif\n"; |
Steve Naroff | 39ba90e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 541 | if (LangOpts.Microsoft) { |
Steve Naroff | 1b208d7 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 542 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 543 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | 39ba90e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 544 | Preamble += "#define __attribute__(X)\n"; |
| 545 | } |
Chris Lattner | 1249968 | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 549 | //===----------------------------------------------------------------------===// |
| 550 | // Top Level Driver Code |
| 551 | //===----------------------------------------------------------------------===// |
| 552 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 553 | void RewriteObjC::HandleTopLevelDecl(Decl *D) { |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 554 | // Two cases: either the decl could be in the main file, or it could be in a |
| 555 | // #included file. If the former, rewrite it now. If the later, check to see |
| 556 | // if we rewrote the #include/#import. |
| 557 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 558 | Loc = SM->getInstantiationLoc(Loc); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 559 | |
| 560 | // If this is for a builtin, ignore it. |
| 561 | if (Loc.isInvalid()) return; |
| 562 | |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 563 | // Look for built-in declarations that we need to refer during the rewrite. |
| 564 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 565 | RewriteFunctionDecl(FD); |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 566 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 567 | // declared in <Foundation/NSString.h> |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 568 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 569 | ConstantStringClassReference = FVD; |
| 570 | return; |
| 571 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 572 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 573 | RewriteInterfaceDecl(MD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 574 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 575 | RewriteCategoryDecl(CD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 576 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 577 | RewriteProtocolDecl(PD); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 578 | } else if (ObjCForwardProtocolDecl *FP = |
| 579 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 580 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 581 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 582 | // Recurse into linkage specifications |
| 583 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 584 | DIEnd = LSD->decls_end(); |
| 585 | DI != DIEnd; ++DI) |
| 586 | HandleTopLevelDecl(*DI); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 587 | } |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 588 | // 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] | 589 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 590 | return HandleDeclInMainFile(D); |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 593 | //===----------------------------------------------------------------------===// |
| 594 | // Syntactic (non-AST) Rewriting Code |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 597 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 598 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 599 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 600 | const char *MainBufStart = MainBuf.first; |
| 601 | const char *MainBufEnd = MainBuf.second; |
| 602 | size_t ImportLen = strlen("import"); |
| 603 | size_t IncludeLen = strlen("include"); |
| 604 | |
Fariborz Jahanian | c81ed74 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 605 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 606 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 607 | if (*BufPtr == '#') { |
| 608 | if (++BufPtr == MainBufEnd) |
| 609 | return; |
| 610 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 611 | if (++BufPtr == MainBufEnd) |
| 612 | return; |
| 613 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 614 | // replace import with include |
| 615 | SourceLocation ImportLoc = |
| 616 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 617 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | a42227a | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 618 | BufPtr += ImportLen; |
| 619 | } |
| 620 | } |
| 621 | } |
Chris Lattner | 74db168 | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 624 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 625 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 626 | const char *MainBufStart = MainBuf.first; |
| 627 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 628 | |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 629 | // Loop over the whole file, looking for tabs. |
| 630 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 631 | if (*BufPtr != '\t') |
| 632 | continue; |
| 633 | |
| 634 | // Okay, we found a tab. This tab will turn into at least one character, |
| 635 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 636 | unsigned VCol = 0; |
| 637 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 638 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 639 | ++VCol; |
| 640 | |
| 641 | // Okay, now that we know the virtual column, we know how many spaces to |
| 642 | // insert. We assume 8-character tab-stops. |
| 643 | unsigned Spaces = 8-(VCol & 7); |
| 644 | |
| 645 | // Get the location of the tab. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 646 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 647 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 648 | |
| 649 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 650 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 651 | } |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 654 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 655 | ObjCIvarDecl *OID) { |
| 656 | std::string S; |
| 657 | S = "((struct "; |
| 658 | S += ClassDecl->getIdentifier()->getName(); |
| 659 | S += "_IMPL *)self)->"; |
| 660 | S += OID->getNameAsCString(); |
| 661 | return S; |
| 662 | } |
| 663 | |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 664 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 665 | ObjCImplementationDecl *IMD, |
| 666 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 667 | SourceLocation startLoc = PID->getLocStart(); |
| 668 | InsertText(startLoc, "// ", 3); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 669 | const char *startBuf = SM->getCharacterData(startLoc); |
| 670 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 671 | const char *semiBuf = strchr(startBuf, ';'); |
| 672 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 673 | SourceLocation onePastSemiLoc = |
| 674 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 675 | |
| 676 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 677 | return; // FIXME: is this correct? |
| 678 | |
| 679 | // Generate the 'getter' function. |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 680 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 681 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 682 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 683 | |
| 684 | if (!OID) |
| 685 | return; |
| 686 | |
| 687 | std::string Getr; |
| 688 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 689 | Getr += "{ "; |
| 690 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 11671e7 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 691 | // FIXME: deal with code generation implications for various property |
| 692 | // attributes (copy, retain, nonatomic). |
| 693 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 694 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 695 | Getr += "; }"; |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 696 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 697 | |
| 698 | // Add the rewritten getter to trigger meta data generation. An alternate, and |
| 699 | // possibly cleaner approach is to hack RewriteObjCMethodsMetaData() to deal |
| 700 | // with properties explicitly. The following addInstanceMethod() required far |
| 701 | // less code change (and actually models what the rewriter is doing). |
| 702 | if (IMD) |
| 703 | IMD->addInstanceMethod(PD->getGetterMethodDecl()); |
| 704 | else |
| 705 | CID->addInstanceMethod(PD->getGetterMethodDecl()); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 706 | |
| 707 | if (PD->isReadOnly()) |
| 708 | return; |
| 709 | |
| 710 | // Generate the 'setter' function. |
| 711 | std::string Setr; |
| 712 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 713 | Setr += "{ "; |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 714 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 11671e7 | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 715 | // FIXME: deal with code generation implications for various property |
| 716 | // attributes (copy, retain, nonatomic). |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 717 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 718 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 719 | Setr += PD->getNameAsCString(); |
Steve Naroff | 1e7b796 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 720 | Setr += "; }"; |
Steve Naroff | 121ed22 | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 721 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | ea6610f | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 722 | |
| 723 | // Add the rewritten setter to trigger meta data generation. |
| 724 | if (IMD) |
| 725 | IMD->addInstanceMethod(PD->getSetterMethodDecl()); |
| 726 | else |
| 727 | CID->addInstanceMethod(PD->getSetterMethodDecl()); |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 728 | } |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 729 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 730 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 731 | // Get the start location and compute the semi location. |
| 732 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 733 | const char *startBuf = SM->getCharacterData(startLoc); |
| 734 | const char *semiPtr = strchr(startBuf, ';'); |
| 735 | |
| 736 | // Translate to typedef's that forward reference structs with the same name |
| 737 | // as the class. As a convenience, we include the original declaration |
| 738 | // as a comment. |
| 739 | std::string typedefString; |
| 740 | typedefString += "// "; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 741 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 742 | typedefString += "\n"; |
Chris Lattner | 6a02874 | 2009-02-20 18:04:31 +0000 | [diff] [blame] | 743 | for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); |
| 744 | I != E; ++I) { |
| 745 | ObjCInterfaceDecl *ForwardDecl = *I; |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 746 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 747 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 748 | typedefString += "\n"; |
| 749 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 750 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 751 | typedefString += "\n"; |
Steve Naroff | 4242b97 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 752 | typedefString += "typedef struct objc_object "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 753 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 754 | typedefString += ";\n#endif\n"; |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 758 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 759 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | fce2c5a | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 762 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 763 | SourceLocation LocStart = Method->getLocStart(); |
| 764 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 2d89c56 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 766 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 767 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | 99f50fe | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 768 | InsertText(LocStart, "#if 0\n", 6); |
| 769 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 770 | } else { |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 771 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 775 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) |
Fariborz Jahanian | eca7fad | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 776 | { |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 777 | SourceLocation Loc = prop->getLocation(); |
| 778 | |
| 779 | ReplaceText(Loc, 0, "// ", 3); |
| 780 | |
| 781 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | eca7fad | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 784 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 785 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 786 | |
| 787 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 788 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 789 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 790 | for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 791 | E = CatDecl->instmeth_end(); I != E; ++I) |
| 792 | RewriteMethodDeclaration(*I); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 793 | for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 794 | E = CatDecl->classmeth_end(); I != E; ++I) |
| 795 | RewriteMethodDeclaration(*I); |
| 796 | |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 797 | // Lastly, comment out the @end. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 798 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 801 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 802 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 803 | |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 804 | SourceLocation LocStart = PDecl->getLocStart(); |
| 805 | |
| 806 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 807 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 808 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 809 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 810 | E = PDecl->instmeth_end(); I != E; ++I) |
| 811 | RewriteMethodDeclaration(*I); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 812 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 813 | E = PDecl->classmeth_end(); I != E; ++I) |
| 814 | RewriteMethodDeclaration(*I); |
| 815 | |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 816 | // Lastly, comment out the @end. |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 817 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 818 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 819 | |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 820 | // Must comment out @optional/@required |
| 821 | const char *startBuf = SM->getCharacterData(LocStart); |
| 822 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 823 | for (const char *p = startBuf; p < endBuf; p++) { |
| 824 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 825 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 826 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 827 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 828 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 829 | |
| 830 | } |
| 831 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 832 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 268fa59 | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 833 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 834 | ReplaceText(OptionalLoc, strlen("@required"), |
| 835 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | 3960e9c | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 836 | |
| 837 | } |
| 838 | } |
Steve Naroff | f4b7d6a | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 841 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 842 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | 0540f3f | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 843 | if (LocStart.isInvalid()) |
| 844 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 845 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 846 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | 016a188 | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 849 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 850 | std::string &ResultStr) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 851 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 852 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 853 | ResultStr += "\nstatic "; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 854 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 855 | ResultStr += "id"; |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 856 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 857 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 858 | // needs special handling, since pointer-to-functions have special |
| 859 | // syntax (where a decaration models use). |
| 860 | QualType retType = OMD->getResultType(); |
Steve Naroff | 20f6739 | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 861 | QualType PointeeTy; |
| 862 | if (const PointerType* PT = retType->getAsPointerType()) |
| 863 | PointeeTy = PT->getPointeeType(); |
| 864 | else if (const BlockPointerType *BPT = retType->getAsBlockPointerType()) |
| 865 | PointeeTy = BPT->getPointeeType(); |
| 866 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 867 | ResultStr += FPRetType->getResultType().getAsString(); |
| 868 | ResultStr += "(*"; |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 869 | } |
| 870 | } else |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 871 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 7202d98 | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 872 | ResultStr += " "; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 873 | |
| 874 | // Unique method name |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 875 | std::string NameStr; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 877 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 878 | NameStr += "_I_"; |
| 879 | else |
| 880 | NameStr += "_C_"; |
| 881 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 882 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 883 | NameStr += "_"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 884 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 885 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 438be77 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 886 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 887 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 888 | NameStr += "_"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 889 | } |
Steve Naroff | 5cb1e6e | 2008-04-04 22:23:44 +0000 | [diff] [blame] | 890 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 891 | { |
| 892 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 893 | int len = selString.size(); |
| 894 | for (int i = 0; i < len; i++) |
| 895 | if (selString[i] == ':') |
| 896 | selString[i] = '_'; |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 897 | NameStr += selString; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 898 | } |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 899 | // Remember this name for metadata emission |
| 900 | MethodInternalNames[OMD] = NameStr; |
| 901 | ResultStr += NameStr; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 902 | |
| 903 | // Rewrite arguments |
| 904 | ResultStr += "("; |
| 905 | |
| 906 | // invisible arguments |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 907 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 908 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 909 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 910 | if (!LangOpts.Microsoft) { |
| 911 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 912 | ResultStr += "struct "; |
| 913 | } |
| 914 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 915 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 916 | ResultStr += " *"; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 917 | } |
| 918 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 919 | ResultStr += Context->getObjCIdType().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 920 | |
| 921 | ResultStr += " self, "; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 922 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 923 | ResultStr += " _cmd"; |
| 924 | |
| 925 | // Method arguments. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 926 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 927 | E = OMD->param_end(); PI != E; ++PI) { |
| 928 | ParmVarDecl *PDecl = *PI; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 929 | ResultStr += ", "; |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 930 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 931 | ResultStr += "id "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 932 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 933 | } else { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 934 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 935 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | 4e2ae51 | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 936 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 937 | const BlockPointerType *BPT = PDecl->getType()->getAsBlockPointerType(); |
| 938 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name); |
| 939 | } else |
| 940 | PDecl->getType().getAsStringInternal(Name); |
Steve Naroff | 133dfda | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 941 | ResultStr += Name; |
| 942 | } |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 943 | } |
Fariborz Jahanian | 2fab94e | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 944 | if (OMD->isVariadic()) |
| 945 | ResultStr += ", ..."; |
Fariborz Jahanian | 7202d98 | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 946 | ResultStr += ") "; |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 947 | |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 948 | if (FPRetType) { |
| 949 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 950 | |
| 951 | // Now, emit the argument types (if any). |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 952 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { |
Steve Naroff | 91044cf | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 953 | ResultStr += "("; |
| 954 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 955 | if (i) ResultStr += ", "; |
| 956 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 957 | ResultStr += ParamStr; |
| 958 | } |
| 959 | if (FT->isVariadic()) { |
| 960 | if (FT->getNumArgs()) ResultStr += ", "; |
| 961 | ResultStr += "..."; |
| 962 | } |
| 963 | ResultStr += ")"; |
| 964 | } else { |
| 965 | ResultStr += "()"; |
| 966 | } |
| 967 | } |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 968 | } |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 969 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 970 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 971 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 972 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 973 | if (IMD) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 974 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 975 | else |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 976 | InsertText(CID->getLocStart(), "// ", 3); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 977 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 978 | for (ObjCCategoryImplDecl::instmeth_iterator |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 979 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 980 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 981 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 982 | ObjCMethodDecl *OMD = *I; |
| 983 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 984 | SourceLocation LocStart = OMD->getLocStart(); |
| 985 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 986 | |
| 987 | const char *startBuf = SM->getCharacterData(LocStart); |
| 988 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 989 | ReplaceText(LocStart, endBuf-startBuf, |
| 990 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 993 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 994 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 995 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 996 | std::string ResultStr; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 997 | ObjCMethodDecl *OMD = *I; |
| 998 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 999 | SourceLocation LocStart = OMD->getLocStart(); |
| 1000 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 1001 | |
| 1002 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1003 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1004 | ReplaceText(LocStart, endBuf-startBuf, |
| 1005 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 5ea3a76 | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1006 | } |
Steve Naroff | 110be84 | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1007 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1008 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1009 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); 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 | |
Steve Naroff | 56af200 | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1037 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
| 1038 | E = ClassDecl->prop_end(); I != E; ++I) |
| 1039 | RewriteProperty(*I); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1040 | for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1041 | E = ClassDecl->instmeth_end(); I != E; ++I) |
| 1042 | RewriteMethodDeclaration(*I); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1043 | for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1044 | E = ClassDecl->classmeth_end(); I != E; ++I) |
| 1045 | RewriteMethodDeclaration(*I); |
| 1046 | |
Steve Naroff | 1ccf463 | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1047 | // Lastly, comment out the @end. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1048 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1051 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1052 | SourceRange SrcRange) { |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1053 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1054 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1055 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1056 | ObjCMessageExpr *MsgExpr; |
| 1057 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1058 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1059 | ExprVec.push_back(newStmt); |
| 1060 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1061 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1062 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1063 | if (PRE && PropGetters[PRE]) { |
| 1064 | // This allows us to handle chain/nested property getters. |
| 1065 | Receiver = PropGetters[PRE]; |
| 1066 | } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1067 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1068 | PDecl->getSetterName(), PDecl->getType(), |
| 1069 | PDecl->getSetterMethodDecl(), |
| 1070 | SourceLocation(), SourceLocation(), |
| 1071 | &ExprVec[0], 1); |
| 1072 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
| 1073 | |
| 1074 | // Now do the actual rewrite. |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1075 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | 478f738 | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1076 | //delete BinOp; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1077 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1078 | // to things that stay around. |
| 1079 | Context->Deallocate(MsgExpr); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1080 | return ReplacingStmt; |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1083 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1084 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1085 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1086 | ObjCMessageExpr *MsgExpr; |
| 1087 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1088 | |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1089 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1090 | |
| 1091 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1092 | if (PRE && PropGetters[PRE]) { |
| 1093 | // This allows us to handle chain/nested property getters. |
| 1094 | Receiver = PropGetters[PRE]; |
| 1095 | } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1096 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1097 | PDecl->getGetterName(), PDecl->getType(), |
| 1098 | PDecl->getGetterMethodDecl(), |
| 1099 | SourceLocation(), SourceLocation(), |
| 1100 | 0, 0); |
| 1101 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1102 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (!PropParentMap) |
| 1105 | PropParentMap = new ParentMap(CurrentBody); |
| 1106 | |
| 1107 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1108 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1109 | // We stash away the ReplacingStmt since actually doing the |
| 1110 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1111 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1112 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1113 | // to things that stay around. |
| 1114 | Context->Deallocate(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1115 | return PropRefExpr; // return the original... |
| 1116 | } else { |
| 1117 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1118 | // delete PropRefExpr; elsewhere... |
| 1119 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1120 | // to things that stay around. |
| 1121 | Context->Deallocate(MsgExpr); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1122 | return ReplacingStmt; |
| 1123 | } |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1126 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
| 1127 | SourceLocation OrigStart) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1128 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1129 | if (CurMethodDef) { |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1130 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1131 | ObjCInterfaceType *iFaceDecl = |
| 1132 | dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1133 | // lookup which class implements the instance variable. |
| 1134 | ObjCInterfaceDecl *clsDeclared = 0; |
| 1135 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
| 1136 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1137 | |
| 1138 | // Synthesize an explicit cast to gain access to the ivar. |
| 1139 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1140 | RecName += "_IMPL"; |
| 1141 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1142 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1143 | SourceLocation(), II); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1144 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1145 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1146 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), |
| 1147 | castT,SourceLocation(), |
| 1148 | SourceLocation()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1149 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1150 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1151 | IV->getBase()->getLocEnd(), |
| 1152 | castExpr); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1153 | if (IV->isFreeIvar() && |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1154 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1155 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1156 | IV->getLocation(), |
| 1157 | D->getType()); |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1158 | ReplaceStmt(IV, ME); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1159 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1160 | return ME; |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1161 | } |
Chris Lattner | 343c82b | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1162 | |
| 1163 | ReplaceStmt(IV->getBase(), PE); |
| 1164 | // Cannot delete IV->getBase(), since PE points to it. |
| 1165 | // Replace the old base with the cast. This is important when doing |
| 1166 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1167 | IV->setBase(PE); |
| 1168 | return IV; |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1169 | } |
Steve Naroff | e615536 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1170 | } else { // we are outside a method. |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1171 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 1172 | |
| 1173 | // Explicit ivar refs need to have a cast inserted. |
| 1174 | // FIXME: consider sharing some of this code with the code above. |
| 1175 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1176 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1177 | // lookup which class implements the instance variable. |
| 1178 | ObjCInterfaceDecl *clsDeclared = 0; |
Steve Naroff | a963622 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1179 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1180 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1181 | |
| 1182 | // Synthesize an explicit cast to gain access to the ivar. |
| 1183 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1184 | RecName += "_IMPL"; |
| 1185 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1186 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1187 | SourceLocation(), II); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1188 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1189 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1190 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), |
| 1191 | castT, SourceLocation(), |
| 1192 | SourceLocation()); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1193 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1194 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 3cca661 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1195 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | d8d30b9 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1196 | ReplaceStmt(IV->getBase(), PE); |
| 1197 | // Cannot delete IV->getBase(), since PE points to it. |
| 1198 | // Replace the old base with the cast. This is important when doing |
| 1199 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1200 | IV->setBase(PE); |
| 1201 | return IV; |
| 1202 | } |
Steve Naroff | 292b7b9 | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1203 | } |
Steve Naroff | e615536 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1204 | return IV; |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1207 | /// SynthCountByEnumWithState - To print: |
| 1208 | /// ((unsigned int (*) |
| 1209 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1210 | /// (void *)objc_msgSend)((id)l_collection, |
| 1211 | /// sel_registerName( |
| 1212 | /// "countByEnumeratingWithState:objects:count:"), |
| 1213 | /// &enumState, |
| 1214 | /// (id *)items, (unsigned int)16) |
| 1215 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1216 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1217 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1218 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1219 | buf += "\n\t\t"; |
| 1220 | buf += "((id)l_collection,\n\t\t"; |
| 1221 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1222 | buf += "\n\t\t"; |
| 1223 | buf += "&enumState, " |
| 1224 | "(id *)items, (unsigned int)16)"; |
| 1225 | } |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1226 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1227 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1228 | /// statement to exit to its outer synthesized loop. |
| 1229 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1230 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1231 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1232 | return S; |
| 1233 | // replace break with goto __break_label |
| 1234 | std::string buf; |
| 1235 | |
| 1236 | SourceLocation startLoc = S->getLocStart(); |
| 1237 | buf = "goto __break_label_"; |
| 1238 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1239 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1240 | |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1245 | /// statement to continue with its inner synthesized loop. |
| 1246 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1247 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1248 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1249 | return S; |
| 1250 | // replace continue with goto __continue_label |
| 1251 | std::string buf; |
| 1252 | |
| 1253 | SourceLocation startLoc = S->getLocStart(); |
| 1254 | buf = "goto __continue_label_"; |
| 1255 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1256 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1261 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1262 | /// It rewrites: |
| 1263 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 1264 | |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1265 | /// Into: |
| 1266 | /// { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1267 | /// type elem; |
| 1268 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1269 | /// id items[16]; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1270 | /// id l_collection = (id)collection; |
| 1271 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1272 | /// objects:items count:16]; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1273 | /// if (limit) { |
| 1274 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1275 | /// do { |
| 1276 | /// unsigned long counter = 0; |
| 1277 | /// do { |
| 1278 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1279 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1280 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1281 | /// stmts; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1282 | /// __continue_label: ; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1283 | /// } while (counter < limit); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1284 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1285 | /// objects:items count:16]); |
| 1286 | /// elem = nil; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1287 | /// __break_label: ; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1288 | /// } |
| 1289 | /// else |
| 1290 | /// elem = nil; |
| 1291 | /// } |
| 1292 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1293 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 2c02216 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1294 | SourceLocation OrigEnd) { |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1295 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1296 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1297 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1298 | assert(!ObjCBcLabelNo.empty() && |
| 1299 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1300 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1301 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1302 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1303 | const char *elementName; |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1304 | std::string elementTypeAsString; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1305 | std::string buf; |
| 1306 | buf = "\n{\n\t"; |
| 1307 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1308 | // type elem; |
Chris Lattner | 4a9a85e | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1309 | NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); |
Ted Kremenek | 91a2bd3 | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1310 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1311 | elementTypeAsString = ElementType.getAsString(); |
| 1312 | buf += elementTypeAsString; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1313 | buf += " "; |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1314 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1315 | buf += elementName; |
| 1316 | buf += ";\n\t"; |
| 1317 | } |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1318 | else { |
| 1319 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1320 | elementName = DR->getDecl()->getNameAsCString(); |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1321 | elementTypeAsString |
| 1322 | = cast<ValueDecl>(DR->getDecl())->getType().getAsString(); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1323 | } |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1324 | |
| 1325 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1326 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1327 | // id items[16]; |
| 1328 | buf += "id items[16];\n\t"; |
| 1329 | // id l_collection = (id) |
| 1330 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1331 | // Find start location of 'collection' the hard way! |
| 1332 | const char *startCollectionBuf = startBuf; |
| 1333 | startCollectionBuf += 3; // skip 'for' |
| 1334 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1335 | startCollectionBuf++; // skip '(' |
| 1336 | // find 'in' and skip it. |
| 1337 | while (*startCollectionBuf != ' ' || |
| 1338 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1339 | (*(startCollectionBuf+3) != ' ' && |
| 1340 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1341 | startCollectionBuf++; |
| 1342 | startCollectionBuf += 3; |
| 1343 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1344 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1345 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1346 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1347 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | df2b095 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1348 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1349 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1350 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1351 | buf = ";\n\t"; |
| 1352 | |
| 1353 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1354 | // objects:items count:16]; |
| 1355 | // which is synthesized into: |
| 1356 | // unsigned int limit = |
| 1357 | // ((unsigned int (*) |
| 1358 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1359 | // (void *)objc_msgSend)((id)l_collection, |
| 1360 | // sel_registerName( |
| 1361 | // "countByEnumeratingWithState:objects:count:"), |
| 1362 | // (struct __objcFastEnumerationState *)&state, |
| 1363 | // (id *)items, (unsigned int)16); |
| 1364 | buf += "unsigned long limit =\n\t\t"; |
| 1365 | SynthCountByEnumWithState(buf); |
| 1366 | buf += ";\n\t"; |
| 1367 | /// if (limit) { |
| 1368 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1369 | /// do { |
| 1370 | /// unsigned long counter = 0; |
| 1371 | /// do { |
| 1372 | /// if (startMutations != *enumState.mutationsPtr) |
| 1373 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1374 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1375 | buf += "if (limit) {\n\t"; |
| 1376 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1377 | buf += "do {\n\t\t"; |
| 1378 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1379 | buf += "do {\n\t\t\t"; |
| 1380 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1381 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1382 | buf += elementName; |
Fariborz Jahanian | fb09aa0 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1383 | buf += " = ("; |
| 1384 | buf += elementTypeAsString; |
| 1385 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1386 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1387 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1388 | |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1389 | /// __continue_label: ; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1390 | /// } while (counter < limit); |
| 1391 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1392 | /// objects:items count:16]); |
| 1393 | /// elem = nil; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1394 | /// __break_label: ; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1395 | /// } |
| 1396 | /// else |
| 1397 | /// elem = nil; |
| 1398 | /// } |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1399 | /// |
| 1400 | buf = ";\n\t"; |
| 1401 | buf += "__continue_label_"; |
| 1402 | buf += utostr(ObjCBcLabelNo.back()); |
| 1403 | buf += ": ;"; |
| 1404 | buf += "\n\t\t"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1405 | buf += "} while (counter < limit);\n\t"; |
| 1406 | buf += "} while (limit = "; |
| 1407 | SynthCountByEnumWithState(buf); |
| 1408 | buf += ");\n\t"; |
| 1409 | buf += elementName; |
Steve Naroff | a9dc026 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1410 | buf += " = ((id)0);\n\t"; |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1411 | buf += "__break_label_"; |
| 1412 | buf += utostr(ObjCBcLabelNo.back()); |
| 1413 | buf += ": ;\n\t"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1414 | buf += "}\n\t"; |
| 1415 | buf += "else\n\t\t"; |
| 1416 | buf += elementName; |
Steve Naroff | a9dc026 | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1417 | buf += " = ((id)0);\n"; |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1418 | buf += "}\n"; |
Steve Naroff | 5f238fe | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1419 | |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1420 | // Insert all these *after* the statement body. |
Steve Naroff | 5f238fe | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1421 | if (isa<CompoundStmt>(S->getBody())) { |
| 1422 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1423 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1424 | } else { |
| 1425 | /* Need to treat single statements specially. For example: |
| 1426 | * |
| 1427 | * for (A *a in b) if (stuff()) break; |
| 1428 | * for (A *a in b) xxxyy; |
| 1429 | * |
| 1430 | * The following code simply scans ahead to the semi to find the actual end. |
| 1431 | */ |
| 1432 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1433 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1434 | assert(semiBuf && "Can't find ';'"); |
| 1435 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1436 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1437 | } |
Fariborz Jahanian | 13dad33 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1438 | Stmts.pop_back(); |
| 1439 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1440 | return 0; |
Fariborz Jahanian | 955fcb8 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1443 | /// RewriteObjCSynchronizedStmt - |
| 1444 | /// This routine rewrites @synchronized(expr) stmt; |
| 1445 | /// into: |
| 1446 | /// objc_sync_enter(expr); |
| 1447 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1448 | /// |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1449 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1450 | // Get the start location and compute the semi location. |
| 1451 | SourceLocation startLoc = S->getLocStart(); |
| 1452 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1453 | |
| 1454 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1455 | |
| 1456 | std::string buf; |
Steve Naroff | c1656a6 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1457 | buf = "objc_sync_enter((id)"; |
| 1458 | const char *lparenBuf = startBuf; |
| 1459 | while (*lparenBuf != '(') lparenBuf++; |
| 1460 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1461 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1462 | // the sync expression is typically a message expression that's already |
| 1463 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1464 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1465 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1466 | while (*endBuf != ')') endBuf--; |
| 1467 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1468 | buf = ");\n"; |
| 1469 | // declare a new scope with two variables, _stack and _rethrow. |
| 1470 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1471 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1472 | buf += "char *pointers[4];} _stack;\n"; |
| 1473 | buf += "id volatile _rethrow = 0;\n"; |
| 1474 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1475 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1476 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1477 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1478 | startBuf = SM->getCharacterData(startLoc); |
| 1479 | |
Steve Naroff | 027cd0b | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1480 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1481 | SourceLocation lastCurlyLoc = startLoc; |
| 1482 | buf = "}\nelse {\n"; |
| 1483 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1484 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1485 | buf += " objc_sync_exit("; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1486 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 1487 | S->getSynchExpr(), |
| 1488 | Context->getObjCIdType(), |
| 1489 | SourceLocation(), |
| 1490 | SourceLocation()); |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1491 | std::string syncExprBufS; |
| 1492 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Steve Naroff | c1656a6 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1493 | syncExpr->printPretty(syncExprBuf); |
Steve Naroff | 17919b5 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1494 | buf += syncExprBuf.str(); |
| 1495 | buf += ");\n"; |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1496 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1497 | buf += "}\n"; |
| 1498 | buf += "}"; |
| 1499 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1500 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 499bf41 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1501 | return 0; |
| 1502 | } |
| 1503 | |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1504 | void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) { |
| 1505 | // Perform a bottom up traversal of all children. |
| 1506 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1507 | CI != E; ++CI) |
| 1508 | if (*CI) |
| 1509 | WarnAboutReturnGotoContinueOrBreakStmts(*CI); |
| 1510 | |
| 1511 | if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || |
| 1512 | isa<BreakStmt>(S) || isa<GotoStmt>(S)) { |
| 1513 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1514 | TryFinallyContainsReturnDiag); |
| 1515 | } |
| 1516 | return; |
| 1517 | } |
| 1518 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1519 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1520 | // Get the start location and compute the semi location. |
| 1521 | SourceLocation startLoc = S->getLocStart(); |
| 1522 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1523 | |
| 1524 | assert((*startBuf == '@') && "bogus @try location"); |
| 1525 | |
| 1526 | std::string buf; |
| 1527 | // declare a new scope with two variables, _stack and _rethrow. |
| 1528 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1529 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1530 | buf += "char *pointers[4];} _stack;\n"; |
| 1531 | buf += "id volatile _rethrow = 0;\n"; |
| 1532 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1533 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1535 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1536 | |
| 1537 | startLoc = S->getTryBody()->getLocEnd(); |
| 1538 | startBuf = SM->getCharacterData(startLoc); |
| 1539 | |
| 1540 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1541 | |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1542 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1543 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1544 | if (catchList) { |
| 1545 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1546 | buf = " /* @catch begin */ else {\n"; |
| 1547 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1548 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1549 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1550 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1551 | buf += " else { /* @catch continue */"; |
| 1552 | |
| 1553 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 36269d2 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1554 | } else { /* no catch list */ |
| 1555 | buf = "}\nelse {\n"; |
| 1556 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1557 | buf += "}"; |
| 1558 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 9d0ff35 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1559 | } |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1560 | bool sawIdTypedCatch = false; |
| 1561 | Stmt *lastCatchBody = 0; |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1562 | while (catchList) { |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1563 | ParmVarDecl *catchDecl = catchList->getCatchParamDecl(); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1564 | |
| 1565 | if (catchList == S->getCatchStmts()) |
| 1566 | buf = "if ("; // we are generating code for the first catch clause |
| 1567 | else |
| 1568 | buf = "else if ("; |
| 1569 | startLoc = catchList->getLocStart(); |
| 1570 | startBuf = SM->getCharacterData(startLoc); |
| 1571 | |
| 1572 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1573 | |
| 1574 | const char *lParenLoc = strchr(startBuf, '('); |
| 1575 | |
Steve Naroff | 397c4ce | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1576 | if (catchList->hasEllipsis()) { |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1577 | // Now rewrite the body... |
| 1578 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1579 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1580 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1581 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1582 | "bogus @catch paren location"); |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1583 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1584 | |
| 1585 | buf += "1) { id _tmp = _caught;"; |
| 1586 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1587 | buf.c_str(), buf.size()); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1588 | } else if (catchDecl) { |
| 1589 | QualType t = catchDecl->getType(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1590 | if (t == Context->getObjCIdType()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1591 | buf += "1) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1592 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1593 | sawIdTypedCatch = true; |
| 1594 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1595 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1596 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1597 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1598 | if (cls) { |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1599 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1600 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | d3287d8 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1601 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1602 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | // Now rewrite the body... |
| 1606 | lastCatchBody = catchList->getCatchBody(); |
| 1607 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1608 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1609 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1610 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1611 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1612 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1613 | |
| 1614 | buf = " = _caught;"; |
| 1615 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1616 | // declares the @catch parameter). |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1617 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 0e8b96a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 1618 | } else { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1619 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1620 | } |
Steve Naroff | 929c77e | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1621 | // make sure all the catch bodies get rewritten! |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1622 | catchList = catchList->getNextCatchStmt(); |
| 1623 | } |
| 1624 | // Complete the catch list... |
| 1625 | if (lastCatchBody) { |
| 1626 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1627 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1628 | "bogus @catch body location"); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1629 | |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1630 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1631 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1632 | buf = "} /* last catch end */\n"; |
| 1633 | buf += "else {\n"; |
| 1634 | buf += " _rethrow = _caught;\n"; |
| 1635 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1636 | buf += "} } /* @catch end */\n"; |
| 1637 | if (!S->getFinallyStmt()) |
| 1638 | buf += "}\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1639 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1640 | |
| 1641 | // Set lastCurlyLoc |
| 1642 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1643 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1644 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1645 | startLoc = finalStmt->getLocStart(); |
| 1646 | startBuf = SM->getCharacterData(startLoc); |
| 1647 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1648 | |
| 1649 | buf = "/* @finally */"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1650 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1651 | |
| 1652 | Stmt *body = finalStmt->getFinallyBody(); |
| 1653 | SourceLocation startLoc = body->getLocStart(); |
| 1654 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 690c287 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1655 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1656 | "bogus @finally body location"); |
| 1657 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1658 | "bogus @finally body location"); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1659 | |
| 1660 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1661 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1662 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1663 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1664 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1665 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1666 | |
| 1667 | // Set lastCurlyLoc |
| 1668 | lastCurlyLoc = body->getLocEnd(); |
Steve Naroff | 43964fb | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1669 | |
| 1670 | // Now check for any return/continue/go statements within the @try. |
| 1671 | WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody()); |
Steve Naroff | 31325c1 | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1672 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1673 | buf = "{ /* implicit finally clause */\n"; |
| 1674 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1675 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1676 | buf += "}"; |
| 1677 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | e9f6984 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1678 | } |
| 1679 | // Now emit the final closing curly brace... |
| 1680 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1681 | buf = " } /* @try scope end */\n"; |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1682 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1683 | return 0; |
| 1684 | } |
| 1685 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1686 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
| 1689 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1690 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1694 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1695 | // the throw expression is typically a message expression that's already |
| 1696 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1697 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1698 | // Get the start location and compute the semi location. |
| 1699 | SourceLocation startLoc = S->getLocStart(); |
| 1700 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1701 | |
| 1702 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1703 | |
| 1704 | std::string buf; |
| 1705 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | be72efa | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1706 | if (S->getThrowExpr()) |
| 1707 | buf = "objc_exception_throw("; |
| 1708 | else // add an implicit argument |
| 1709 | buf = "objc_exception_throw(_caught"; |
Steve Naroff | 3de6eaa | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1710 | |
| 1711 | // handle "@ throw" correctly. |
| 1712 | const char *wBuf = strchr(startBuf, 'w'); |
| 1713 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1714 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
| 1715 | |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1716 | const char *semiBuf = strchr(startBuf, ';'); |
| 1717 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1718 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1719 | buf = ");"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1720 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 8b1fb8c | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1721 | return 0; |
| 1722 | } |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1723 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1724 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | bf0bfa6 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1725 | // Create a new string expression. |
| 1726 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1727 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1728 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1729 | Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), |
| 1730 | StrEncoding.length(), false,StrType, |
| 1731 | SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1732 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | 258f26c | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | 4478db9 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1734 | // Replace this subexpr in the parent. |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1735 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1736 | return Replacement; |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1739 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 95f6bce | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1740 | if (!SelGetUidFunctionDecl) |
| 1741 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1742 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1743 | // Create a call to sel_registerName("selName"). |
| 1744 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1745 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 1746 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1747 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1748 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 1749 | false, argType, SourceLocation())); |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1750 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1751 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1752 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1753 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 296b74f | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1754 | return SelExp; |
| 1755 | } |
| 1756 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1757 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1758 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1759 | // 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] | 1760 | QualType msgSendType = FD->getType(); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1761 | |
| 1762 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1763 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1764 | |
| 1765 | // 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] | 1766 | QualType pToFunc = Context->getPointerType(msgSendType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1767 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, DRE, |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1768 | /*isLvalue=*/false); |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1769 | |
| 1770 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | 0021f45 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1771 | |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1772 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1773 | SourceLocation()); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1776 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1777 | const char *&startRef, const char *&endRef) { |
| 1778 | while (startBuf < endBuf) { |
| 1779 | if (*startBuf == '<') |
| 1780 | startRef = startBuf; // mark the start. |
| 1781 | if (*startBuf == '>') { |
Steve Naroff | 2aeae31 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1782 | if (startRef && *startRef == '<') { |
| 1783 | endRef = startBuf; // mark the end. |
| 1784 | return true; |
| 1785 | } |
| 1786 | return false; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1787 | } |
| 1788 | startBuf++; |
| 1789 | } |
| 1790 | return false; |
| 1791 | } |
| 1792 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1793 | static void scanToNextArgument(const char *&argRef) { |
| 1794 | int angle = 0; |
| 1795 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1796 | if (*argRef == '<') |
| 1797 | angle++; |
| 1798 | else if (*argRef == '>') |
| 1799 | angle--; |
| 1800 | argRef++; |
| 1801 | } |
| 1802 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1803 | } |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1804 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1805 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1806 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1807 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1808 | return true; |
| 1809 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1810 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1811 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1812 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1813 | return true; // we have "Class <Protocol> *". |
| 1814 | } |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1815 | return false; |
| 1816 | } |
| 1817 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1818 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1819 | QualType Type = E->getType(); |
| 1820 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1821 | SourceLocation Loc, EndLoc; |
| 1822 | |
| 1823 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1824 | Loc = ECE->getLParenLoc(); |
| 1825 | EndLoc = ECE->getRParenLoc(); |
| 1826 | } else { |
| 1827 | Loc = E->getLocStart(); |
| 1828 | EndLoc = E->getLocEnd(); |
| 1829 | } |
| 1830 | // This will defend against trying to rewrite synthesized expressions. |
| 1831 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1832 | return; |
| 1833 | |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1834 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | f31ece9 | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1835 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | d06c88d | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1836 | const char *startRef = 0, *endRef = 0; |
| 1837 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1838 | // Get the locations of the startRef, endRef. |
| 1839 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1840 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1841 | // Comment out the protocol references. |
| 1842 | InsertText(LessLoc, "/*", 2); |
| 1843 | InsertText(GreaterLoc, "*/", 2); |
| 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1848 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1849 | SourceLocation Loc; |
| 1850 | QualType Type; |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1851 | const FunctionProtoType *proto = 0; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1852 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1853 | Loc = VD->getLocation(); |
| 1854 | Type = VD->getType(); |
| 1855 | } |
| 1856 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1857 | Loc = FD->getLocation(); |
| 1858 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1859 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1860 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1861 | assert(funcType && "missing function type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1862 | proto = dyn_cast<FunctionProtoType>(funcType); |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1863 | if (!proto) |
| 1864 | return; |
| 1865 | Type = proto->getResultType(); |
| 1866 | } |
| 1867 | else |
| 1868 | return; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1869 | |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1870 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1871 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1872 | |
| 1873 | const char *endBuf = SM->getCharacterData(Loc); |
| 1874 | const char *startBuf = endBuf; |
Steve Naroff | ff2fa19 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1875 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1876 | startBuf--; // scan backward (from the decl location) for return type. |
| 1877 | const char *startRef = 0, *endRef = 0; |
| 1878 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1879 | // Get the locations of the startRef, endRef. |
| 1880 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1881 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1882 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1883 | InsertText(LessLoc, "/*", 2); |
| 1884 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1887 | if (!proto) |
| 1888 | return; // most likely, was a variable |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1889 | // Now check arguments. |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1890 | const char *startBuf = SM->getCharacterData(Loc); |
| 1891 | const char *startFuncBuf = startBuf; |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1892 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1893 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1894 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1895 | |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1896 | const char *endBuf = startBuf; |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1897 | // scan forward (from the decl location) for argument types. |
| 1898 | scanToNextArgument(endBuf); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1899 | const char *startRef = 0, *endRef = 0; |
| 1900 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1901 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | db2904f | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1902 | SourceLocation LessLoc = |
| 1903 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1904 | SourceLocation GreaterLoc = |
| 1905 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1906 | // Comment out the protocol references. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1907 | InsertText(LessLoc, "/*", 2); |
| 1908 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1909 | } |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1910 | startBuf = ++endBuf; |
| 1911 | } |
| 1912 | else { |
Steve Naroff | 6a1d88b | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1913 | // If the function name is derived from a macro expansion, then the |
| 1914 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1915 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 150c6ea | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1916 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1917 | startBuf++; |
| 1918 | } |
Steve Naroff | c8a92d1 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1919 | } |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1922 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1923 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1924 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1925 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1926 | ArgTys.push_back(Context->getPointerType( |
| 1927 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1928 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1929 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1930 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1931 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1932 | SourceLocation(), |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1933 | SelGetUidIdent, getFuncType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1934 | FunctionDecl::Extern, false); |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1937 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1938 | void RewriteObjC::SynthGetProtocolFunctionDecl() { |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1939 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1940 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1941 | ArgTys.push_back(Context->getPointerType( |
| 1942 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1943 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1944 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1945 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1946 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1947 | SourceLocation(), |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1948 | SelGetProtoIdent, getFuncType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1949 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1952 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1953 | // declared in <objc/objc.h> |
Douglas Gregor | 7339c9e | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 1954 | if (FD->getIdentifier() && |
| 1955 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1956 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 05d6ff5 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1957 | return; |
| 1958 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1959 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1962 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1963 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1964 | if (SuperContructorFunctionDecl) |
| 1965 | return; |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 1966 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1967 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1968 | QualType argT = Context->getObjCIdType(); |
| 1969 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1970 | ArgTys.push_back(argT); |
| 1971 | ArgTys.push_back(argT); |
| 1972 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1973 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1974 | false, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1975 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1976 | SourceLocation(), |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1977 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1978 | FunctionDecl::Extern, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1981 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1982 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1983 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1984 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1985 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1986 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1987 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1988 | argT = Context->getObjCSelType(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1989 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1990 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1991 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1992 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1993 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1994 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1995 | SourceLocation(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1996 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1997 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2000 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2001 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2002 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2003 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2004 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2005 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2006 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2007 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2008 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2009 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2010 | argT = Context->getObjCSelType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2011 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2012 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2013 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2014 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2015 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2016 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2017 | SourceLocation(), |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2018 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2019 | FunctionDecl::Extern, false); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2022 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2023 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2024 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2025 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2026 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2027 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2028 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2029 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2030 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2031 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2032 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2033 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2034 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2035 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2036 | SourceLocation(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2037 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2038 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | // SynthMsgSendSuperStretFunctionDecl - |
| 2042 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2043 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2044 | IdentifierInfo *msgSendIdent = |
| 2045 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2046 | llvm::SmallVector<QualType, 16> ArgTys; |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2047 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2048 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2049 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2050 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2051 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2052 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2053 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2054 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2055 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2056 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2057 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2058 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2059 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 2060 | SourceLocation(), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2061 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2062 | FunctionDecl::Extern, false); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2065 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2066 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2067 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2068 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2069 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2070 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2071 | ArgTys.push_back(argT); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2072 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2073 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2074 | ArgTys.push_back(argT); |
Steve Naroff | 31316a1 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2075 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2076 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2077 | true /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2078 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2079 | SourceLocation(), |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2080 | msgSendIdent, msgSendType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2081 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2084 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2085 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2086 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2087 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2088 | ArgTys.push_back(Context->getPointerType( |
| 2089 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2090 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2091 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2092 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2093 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2094 | SourceLocation(), |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2095 | getClassIdent, getClassType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2096 | FunctionDecl::Extern, false); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2099 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2100 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2101 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2102 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2103 | ArgTys.push_back(Context->getPointerType( |
| 2104 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2105 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2106 | &ArgTys[0], ArgTys.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2107 | false /*isVariadic*/, 0); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2108 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2109 | SourceLocation(), |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2110 | getClassIdent, getClassType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2111 | FunctionDecl::Extern, false); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2114 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2115 | QualType strType = getConstantStringStructType(); |
| 2116 | |
| 2117 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | a1f0099 | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2118 | |
| 2119 | std::string tmpName = InFileName; |
| 2120 | unsigned i; |
| 2121 | for (i=0; i < tmpName.length(); i++) { |
| 2122 | char c = tmpName.at(i); |
| 2123 | // replace any non alphanumeric characters with '_'. |
| 2124 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2125 | tmpName[i] = '_'; |
| 2126 | } |
| 2127 | S += tmpName; |
| 2128 | S += "_"; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2129 | S += utostr(NumObjCStringLiterals++); |
| 2130 | |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2131 | Preamble += "static __NSConstantStringImpl " + S; |
| 2132 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2133 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2134 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | 7b6f67b | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2135 | std::string prettyBufS; |
| 2136 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2137 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2138 | Preamble += prettyBuf.str(); |
| 2139 | Preamble += ","; |
Steve Naroff | 64bce35 | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2140 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | fef037c | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2141 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2142 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2143 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2144 | &Context->Idents.get(S.c_str()), strType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2145 | VarDecl::Static); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2146 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2147 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2148 | Context->getPointerType(DRE->getType()), |
| 2149 | SourceLocation()); |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2150 | // cast to NSConstantString * |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2151 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), Unop, |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2152 | Exp->getType(), SourceLocation(), SourceLocation()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2153 | ReplaceStmt(Exp, cast); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2154 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | abb9636 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2155 | return cast; |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2158 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2159 | // check if we are sending a message to 'super' |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2160 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2161 | |
Douglas Gregor | d860663 | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2162 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
| 2163 | const PointerType *PT = Super->getType()->getAsPointerType(); |
Chris Lattner | 4423d37 | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2164 | assert(PT); |
| 2165 | ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 2166 | return IT->getDecl(); |
| 2167 | } |
| 2168 | return 0; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2172 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2173 | if (!SuperStructDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2174 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2175 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2176 | &Context->Idents.get("objc_super")); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2177 | QualType FieldTypes[2]; |
| 2178 | |
| 2179 | // struct objc_object *receiver; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2180 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2181 | // struct objc_class *super; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2182 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2183 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2184 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2185 | for (unsigned i = 0; i < 2; ++i) { |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 2186 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2187 | SourceLocation(), 0, |
| 2188 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2189 | /*Mutable=*/false)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2190 | } |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2192 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2193 | } |
| 2194 | return Context->getTagDeclType(SuperStructDecl); |
| 2195 | } |
| 2196 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2197 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2198 | if (!ConstantStringDecl) { |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2199 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2200 | SourceLocation(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2201 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2202 | QualType FieldTypes[4]; |
| 2203 | |
| 2204 | // struct objc_object *receiver; |
| 2205 | FieldTypes[0] = Context->getObjCIdType(); |
| 2206 | // int flags; |
| 2207 | FieldTypes[1] = Context->IntTy; |
| 2208 | // char *str; |
| 2209 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2210 | // long length; |
| 2211 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2212 | |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2213 | // Create fields |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2214 | for (unsigned i = 0; i < 4; ++i) { |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 2215 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2216 | ConstantStringDecl, |
| 2217 | SourceLocation(), 0, |
| 2218 | FieldTypes[i], |
| 2219 | /*BitWidth=*/0, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2220 | /*Mutable=*/true)); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | 47e7fa2 | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2224 | } |
| 2225 | return Context->getTagDeclType(ConstantStringDecl); |
| 2226 | } |
| 2227 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2228 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | fb8e991 | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2229 | if (!SelGetUidFunctionDecl) |
| 2230 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2231 | if (!MsgSendFunctionDecl) |
| 2232 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2233 | if (!MsgSendSuperFunctionDecl) |
| 2234 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2235 | if (!MsgSendStretFunctionDecl) |
| 2236 | SynthMsgSendStretFunctionDecl(); |
| 2237 | if (!MsgSendSuperStretFunctionDecl) |
| 2238 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2239 | if (!MsgSendFpretFunctionDecl) |
| 2240 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 02a82aa | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2241 | if (!GetClassFunctionDecl) |
| 2242 | SynthGetClassFunctionDecl(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2243 | if (!GetMetaClassFunctionDecl) |
| 2244 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2245 | |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2246 | // default to objc_msgSend(). |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2247 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2248 | // May need to use objc_msgSend_stret() as well. |
| 2249 | FunctionDecl *MsgSendStretFlavor = 0; |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2250 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
| 2251 | QualType resultType = OMD->getResultType(); |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2252 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2253 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | b724ab2 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2254 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | 1d29b5d | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2255 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
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 | |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2258 | // Synthesize a call to objc_msgSend(). |
| 2259 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2260 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2261 | |
| 2262 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2263 | if (clsName) { // class message. |
Steve Naroff | 91a6920 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2264 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2265 | // the 'super' idiom within a class method. |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2266 | if (!strcmp(clsName->getName(), "super")) { |
| 2267 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2268 | if (MsgSendStretFlavor) |
| 2269 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2270 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2271 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2272 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2273 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2274 | |
| 2275 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2276 | |
| 2277 | // set the receiver to self, the first argument to all methods. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2278 | InitExprs.push_back(new (Context) DeclRefExpr( |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2279 | CurMethodDef->getSelfDecl(), |
Chris Lattner | 8c7c6a1 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2280 | Context->getObjCIdType(), |
| 2281 | SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2282 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2283 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2284 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2285 | SuperDecl->getIdentifier()->getName(), |
| 2286 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2287 | false, argType, SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2288 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2289 | &ClsExprs[0], |
| 2290 | ClsExprs.size()); |
| 2291 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2292 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2293 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2294 | Cls, Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2295 | SourceLocation(), SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2296 | // struct objc_super |
| 2297 | QualType superType = getSuperStructType(); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2298 | Expr *SuperRep; |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2299 | |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2300 | if (LangOpts.Microsoft) { |
| 2301 | SynthSuperContructorFunctionDecl(); |
| 2302 | // Simulate a contructor call... |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2303 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2304 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2305 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2306 | InitExprs.size(), |
| 2307 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2308 | // The code for super is a little tricky to prevent collision with |
| 2309 | // the structure definition in the header. The rewriter has it's own |
| 2310 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2311 | // we need the cast below. For example: |
| 2312 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2313 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2314 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2315 | Context->getPointerType(SuperRep->getType()), |
| 2316 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2317 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2318 | SuperRep, Context->getPointerType(superType), |
| 2319 | SourceLocation(), SourceLocation()); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2320 | } else { |
| 2321 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2322 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2323 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2324 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2325 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2326 | false); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2327 | // struct objc_super * |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2328 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2329 | Context->getPointerType(SuperRep->getType()), |
| 2330 | SourceLocation()); |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2331 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2332 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2333 | } else { |
| 2334 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2335 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2336 | ClsExprs.push_back(StringLiteral::Create(*Context, |
| 2337 | clsName->getName(), |
| 2338 | clsName->getLength(), |
| 2339 | false, argType, |
| 2340 | SourceLocation())); |
Steve Naroff | 3b1caac | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2341 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2342 | &ClsExprs[0], |
| 2343 | ClsExprs.size()); |
| 2344 | MsgExprs.push_back(Cls); |
| 2345 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2346 | } else { // instance message. |
| 2347 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2348 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2349 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2350 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2351 | if (MsgSendStretFlavor) |
| 2352 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2353 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2354 | |
| 2355 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2356 | |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2357 | InitExprs.push_back( |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2358 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 2359 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | 2352861 | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2360 | Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2361 | SourceLocation()), |
| 2362 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2363 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2364 | |
| 2365 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2366 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2367 | ClsExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2368 | SuperDecl->getIdentifier()->getName(), |
| 2369 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2370 | false, argType, SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2371 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2372 | &ClsExprs[0], |
| 2373 | ClsExprs.size()); |
Fariborz Jahanian | fabf3bf | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2374 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | dc25ba7 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2375 | InitExprs.push_back( |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2376 | // set 'super class', using objc_getClass(). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2377 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2378 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2379 | // struct objc_super |
| 2380 | QualType superType = getSuperStructType(); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2381 | Expr *SuperRep; |
| 2382 | |
| 2383 | if (LangOpts.Microsoft) { |
| 2384 | SynthSuperContructorFunctionDecl(); |
| 2385 | // Simulate a contructor call... |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2386 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2387 | superType, SourceLocation()); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2388 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2389 | InitExprs.size(), |
| 2390 | superType, SourceLocation()); |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2391 | // The code for super is a little tricky to prevent collision with |
| 2392 | // the structure definition in the header. The rewriter has it's own |
| 2393 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2394 | // we need the cast below. For example: |
| 2395 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2396 | // |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2397 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2398 | Context->getPointerType(SuperRep->getType()), |
| 2399 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2400 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2401 | SuperRep, Context->getPointerType(superType), |
| 2402 | SourceLocation(), SourceLocation()); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2403 | } else { |
| 2404 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2405 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2406 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2407 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2408 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2409 | } |
Steve Naroff | 1267362 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2410 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2411 | } else { |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2412 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2413 | // Foo<Proto> *. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2414 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | be4283c | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2415 | recExpr = CE->getSubExpr(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2416 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), recExpr, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2417 | Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2418 | SourceLocation(), SourceLocation()); |
Steve Naroff | 764c1ae | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2419 | MsgExprs.push_back(recExpr); |
| 2420 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2421 | } |
Steve Naroff | 0add5d2 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2422 | // 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] | 2423 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2424 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2425 | SelExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2426 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2427 | Exp->getSelector().getAsString().size(), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2428 | false, argType, SourceLocation())); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2429 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2430 | &SelExprs[0], SelExprs.size()); |
| 2431 | MsgExprs.push_back(SelExp); |
| 2432 | |
| 2433 | // Now push any user supplied arguments. |
| 2434 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2435 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2436 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2437 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2438 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2439 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2440 | ? Context->getObjCIdType() |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2441 | : ICE->getType(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2442 | userExpr = new (Context) CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2443 | } |
| 2444 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2445 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2446 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 035d088 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2447 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2448 | userExpr = CE->getSubExpr(); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2449 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2450 | userExpr, Context->getObjCIdType(), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2451 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | dcb2b1e | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2452 | } |
Steve Naroff | 6b759ce | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2453 | } |
Steve Naroff | 885e212 | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2454 | MsgExprs.push_back(userExpr); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2455 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2456 | // the original expression, since we will delete it below. |
| 2457 | Exp->setArg(i, 0); |
| 2458 | } |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2459 | // Generate the funky cast. |
| 2460 | CastExpr *cast; |
| 2461 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2462 | QualType returnType; |
| 2463 | |
| 2464 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | 0c04bb6 | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2465 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2466 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2467 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2468 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2469 | ArgTypes.push_back(Context->getObjCSelType()); |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2470 | if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2471 | // Push any user argument types. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2472 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 2473 | E = OMD->param_end(); PI != E; ++PI) { |
| 2474 | QualType t = (*PI)->getType()->isObjCQualifiedIdType() |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2475 | ? Context->getObjCIdType() |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2476 | : (*PI)->getType(); |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2477 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2478 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | 66c842f | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2479 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 2480 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2481 | } |
Steve Naroff | 4242b97 | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2482 | ArgTypes.push_back(t); |
| 2483 | } |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2484 | returnType = OMD->getResultType()->isObjCQualifiedIdType() |
| 2485 | ? Context->getObjCIdType() : OMD->getResultType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2486 | } else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2487 | returnType = Context->getObjCIdType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2488 | } |
| 2489 | // 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] | 2490 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2491 | |
| 2492 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2493 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2494 | SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2495 | |
| 2496 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2497 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2498 | // xx.m:13: warning: function called through a non-compatible type |
| 2499 | // 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] | 2500 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2501 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2502 | SourceLocation(), SourceLocation()); |
Steve Naroff | 29fe746 | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2503 | |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2504 | // Now do the "normal" pointer to function cast. |
| 2505 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2506 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 3b8b4e3 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2507 | // If we don't have a method decl, force a variadic cast. |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2508 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2509 | castType = Context->getPointerType(castType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2510 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2511 | |
| 2512 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2513 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Steve Naroff | 0744c47 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2514 | |
| 2515 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2516 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2517 | MsgExprs.size(), |
| 2518 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2519 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2520 | if (MsgSendStretFlavor) { |
| 2521 | // We have the method which returns a struct/union. Must also generate |
| 2522 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2523 | // expression which dictate which one to envoke depending on size of |
| 2524 | // method's return type. |
| 2525 | |
| 2526 | // Create a reference to the objc_msgSend_stret() declaration. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2527 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2528 | SourceLocation()); |
| 2529 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2530 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
Douglas Gregor | 21a04f3 | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2531 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2532 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2533 | // Now do the "normal" pointer to function cast. |
| 2534 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | efba8c8 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2535 | &ArgTypes[0], ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2536 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2537 | castType = Context->getPointerType(castType); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2538 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2539 | |
| 2540 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2541 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2542 | |
| 2543 | FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2544 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2545 | MsgExprs.size(), |
| 2546 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2547 | |
| 2548 | // Build sizeof(returnType) |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2549 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, |
| 2550 | returnType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2551 | Context->getSizeType(), |
| 2552 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2553 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2554 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2555 | // For X86 it is more complicated and some kind of target specific routine |
| 2556 | // is needed to decide what to do. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2557 | unsigned IntSize = |
| 2558 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2559 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2560 | Context->IntTy, |
| 2561 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2562 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2563 | BinaryOperator::LE, |
| 2564 | Context->IntTy, |
| 2565 | SourceLocation()); |
| 2566 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2567 | ConditionalOperator *CondExpr = |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2568 | new (Context) ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
| 2569 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | c26b250 | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2570 | } |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2571 | return ReplacingStmt; |
| 2572 | } |
| 2573 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2574 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2575 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | e8efe89 | 2008-10-27 20:54:44 +0000 | [diff] [blame] | 2576 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 2577 | //ReplacingStmt->dump(); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2578 | // Now do the actual rewrite. |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2579 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 7122603 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2580 | |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2581 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 9ea6a2d | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2582 | return ReplacingStmt; |
Steve Naroff | e978058 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2585 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2586 | /// call to objc_getProtocol("proto-name"). |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2587 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2588 | if (!GetProtocolFunctionDecl) |
| 2589 | SynthGetProtocolFunctionDecl(); |
| 2590 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2591 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2592 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 2593 | ProtoExprs.push_back(StringLiteral::Create(*Context, |
Ted Kremenek | 4f530a9 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2594 | Exp->getProtocol()->getNameAsCString(), |
| 2595 | strlen(Exp->getProtocol()->getNameAsCString()), |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 2596 | false, argType, SourceLocation())); |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2597 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2598 | &ProtoExprs[0], |
| 2599 | ProtoExprs.size()); |
Chris Lattner | b154837 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2600 | ReplaceStmt(Exp, ProtoExp); |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2601 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 6ff57c6 | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2602 | return ProtoExp; |
| 2603 | |
| 2604 | } |
| 2605 | |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2606 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2607 | const char *endBuf) { |
| 2608 | while (startBuf < endBuf) { |
| 2609 | if (*startBuf == '#') { |
| 2610 | // Skip whitespace. |
| 2611 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2612 | ; |
| 2613 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2614 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2615 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2616 | !strncmp(startBuf, "define", strlen("define")) || |
| 2617 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2618 | !strncmp(startBuf, "else", strlen("else")) || |
| 2619 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2620 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2621 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2622 | !strncmp(startBuf, "include", strlen("include")) || |
| 2623 | !strncmp(startBuf, "import", strlen("import")) || |
| 2624 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2625 | return true; |
| 2626 | } |
| 2627 | startBuf++; |
| 2628 | } |
| 2629 | return false; |
| 2630 | } |
| 2631 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2632 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2633 | /// an objective-c class with ivars. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2634 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2635 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2636 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2637 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2638 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2639 | // Do not synthesize more than once. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2640 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | fb4f6a3 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2641 | return; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2642 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2643 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2644 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2645 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2646 | |
| 2647 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2648 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2649 | |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2650 | // If no ivars and no root or if its root, directly or indirectly, |
| 2651 | // 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] | 2652 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2653 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2654 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2655 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2656 | return; |
| 2657 | } |
| 2658 | |
| 2659 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2660 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 920cde3 | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2661 | Result += "\nstruct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2662 | Result += CDecl->getNameAsString(); |
Steve Naroff | de0da10 | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2663 | if (LangOpts.Microsoft) |
| 2664 | Result += "_IMPL"; |
Steve Naroff | 60dfb6b | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2665 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2666 | if (NumIvars > 0) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2667 | const char *cursor = strchr(startBuf, '{'); |
| 2668 | assert((cursor && endBuf) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2669 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2670 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2671 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2672 | // NSURL.h, for example): |
| 2673 | // |
| 2674 | // #ifdef XYZ |
| 2675 | // @interface Foo : NSObject |
| 2676 | // #else |
| 2677 | // @interface FooBar : NSObject |
| 2678 | // #endif |
| 2679 | // { |
| 2680 | // int i; |
| 2681 | // } |
| 2682 | // @end |
| 2683 | // |
| 2684 | // This clause is segregated to avoid breaking the common case. |
| 2685 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2686 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2687 | CDecl->getClassLoc(); |
| 2688 | const char *endHeader = SM->getCharacterData(L); |
| 2689 | endHeader += Lexer::MeasureTokenLength(L, *SM); |
| 2690 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2691 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | ef82b74 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2692 | // advance to the end of the referenced protocols. |
| 2693 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2694 | endHeader++; |
| 2695 | } |
| 2696 | // rewrite the original header |
| 2697 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2698 | } else { |
| 2699 | // rewrite the original header *without* disturbing the '{' |
| 2700 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2701 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2702 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2703 | Result = "\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2704 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2705 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2706 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2707 | Result += "_IVARS;\n"; |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2708 | |
| 2709 | // insert the super class structure definition. |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2710 | SourceLocation OnePastCurly = |
| 2711 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2712 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2713 | } |
| 2714 | cursor++; // past '{' |
| 2715 | |
| 2716 | // Now comment out any visibility specifiers. |
| 2717 | while (cursor < endBuf) { |
| 2718 | if (*cursor == '@') { |
| 2719 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f04ead5 | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2720 | // Skip whitespace. |
| 2721 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2722 | /*scan*/; |
| 2723 | |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2724 | // FIXME: presence of @public, etc. inside comment results in |
| 2725 | // this transformation as well, which is still correct c-code. |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2726 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2727 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | a93924a | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2728 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2729 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2730 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2731 | } |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2732 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2733 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2734 | // for type checking. |
| 2735 | else if (*cursor == '<') { |
| 2736 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2737 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2738 | cursor = strchr(cursor, '>'); |
| 2739 | cursor++; |
| 2740 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2741 | InsertText(atLoc, " */", 3); |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2742 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2743 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2744 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 8d9c735 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2745 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2746 | cursor++; |
Fariborz Jahanian | 6acfa2b | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2747 | } |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2748 | // Don't forget to add a ';'!! |
Chris Lattner | 6216f29 | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2749 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | 2c7afc9 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2750 | } else { // we don't have any instance variables - insert super struct. |
| 2751 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 2752 | Result += " {\n struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2753 | Result += RCDecl->getNameAsString(); |
Steve Naroff | cbf88fe | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2754 | Result += "_IMPL "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2755 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 4850dfe | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2756 | Result += "_IVARS;\n};\n"; |
Chris Lattner | b8a1b04 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2757 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2758 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2759 | // Mark this struct as having been generated. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2760 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2761 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2764 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2765 | /// class methods. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2766 | void RewriteObjC::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2767 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | a398637 | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2768 | bool IsInstanceMethod, |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2769 | const char *prefix, |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2770 | const char *ClassName, |
| 2771 | std::string &Result) { |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2772 | if (MethodBegin == MethodEnd) return; |
| 2773 | |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2774 | static bool objc_impl_method = false; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2775 | if (!objc_impl_method) { |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2776 | /* struct _objc_method { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2777 | SEL _cmd; |
| 2778 | char *method_types; |
| 2779 | void *_imp; |
| 2780 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2781 | */ |
Chris Lattner | c3aa5c4 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2782 | Result += "\nstruct _objc_method {\n"; |
| 2783 | Result += "\tSEL _cmd;\n"; |
| 2784 | Result += "\tchar *method_types;\n"; |
| 2785 | Result += "\tvoid *_imp;\n"; |
| 2786 | Result += "};\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2787 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2788 | objc_impl_method = true; |
Fariborz Jahanian | 96b55da | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2789 | } |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2790 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2791 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2792 | |
| 2793 | /* struct { |
| 2794 | struct _objc_method_list *next_method; |
| 2795 | int method_count; |
| 2796 | struct _objc_method method_list[]; |
| 2797 | } |
| 2798 | */ |
| 2799 | Result += "\nstatic struct {\n"; |
| 2800 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2801 | Result += "\tint method_count;\n"; |
| 2802 | Result += "\tstruct _objc_method method_list["; |
| 2803 | Result += utostr(MethodEnd-MethodBegin); |
| 2804 | Result += "];\n} _OBJC_"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2805 | Result += prefix; |
| 2806 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2807 | Result += "_METHODS_"; |
| 2808 | Result += ClassName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2809 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2810 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2811 | Result += "_meth\")))= "; |
| 2812 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2813 | |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2814 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2815 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2816 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2817 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2818 | Result += "\", \""; |
| 2819 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2820 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2821 | Result += MethodInternalNames[*MethodBegin]; |
| 2822 | Result += "}\n"; |
| 2823 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2824 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2825 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2826 | std::string MethodTypeString; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2827 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | c81f316 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2828 | Result += "\", \""; |
| 2829 | Result += MethodTypeString; |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2830 | Result += "\", (void *)"; |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2831 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | bd2fd92 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2832 | Result += "}\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2833 | } |
Chris Lattner | dea5bec | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2834 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2837 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2838 | void RewriteObjC:: |
| 2839 | RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 2840 | const char *prefix, |
| 2841 | const char *ClassName, |
| 2842 | std::string &Result) { |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2843 | static bool objc_protocol_methods = false; |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2844 | if (Protocols.empty()) return; |
| 2845 | |
| 2846 | for (unsigned i = 0; i != Protocols.size(); i++) { |
| 2847 | ObjCProtocolDecl *PDecl = Protocols[i]; |
| 2848 | // Output struct protocol_methods holder of method selector and type. |
| 2849 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2850 | /* struct protocol_methods { |
| 2851 | SEL _cmd; |
| 2852 | char *method_types; |
| 2853 | } |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2854 | */ |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2855 | Result += "\nstruct protocol_methods {\n"; |
| 2856 | Result += "\tSEL _cmd;\n"; |
| 2857 | Result += "\tchar *method_types;\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2858 | Result += "};\n"; |
Steve Naroff | 04eaa19 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2860 | objc_protocol_methods = true; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2861 | } |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2862 | // Do not synthesize the protocol more than once. |
| 2863 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2864 | continue; |
| 2865 | |
| 2866 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2867 | unsigned NumMethods = std::distance(PDecl->instmeth_begin(), |
| 2868 | PDecl->instmeth_end()); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2869 | /* struct _objc_protocol_method_list { |
| 2870 | int protocol_method_count; |
| 2871 | struct protocol_methods protocols[]; |
| 2872 | } |
| 2873 | */ |
| 2874 | Result += "\nstatic struct {\n"; |
| 2875 | Result += "\tint protocol_method_count;\n"; |
| 2876 | Result += "\tstruct protocol_methods protocols["; |
| 2877 | Result += utostr(NumMethods); |
| 2878 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2879 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2880 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2881 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2882 | |
| 2883 | // Output instance methods declared in this protocol. |
| 2884 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
| 2885 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 2886 | if (I == PDecl->instmeth_begin()) |
| 2887 | Result += "\t ,{{(SEL)\""; |
| 2888 | else |
| 2889 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2890 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2891 | std::string MethodTypeString; |
| 2892 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2893 | Result += "\", \""; |
| 2894 | Result += MethodTypeString; |
| 2895 | Result += "\"}\n"; |
| 2896 | } |
| 2897 | Result += "\t }\n};\n"; |
| 2898 | } |
| 2899 | |
| 2900 | // Output class methods declared in this protocol. |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2901 | unsigned NumMethods = std::distance(PDecl->classmeth_begin(), |
| 2902 | PDecl->classmeth_end()); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2903 | if (NumMethods > 0) { |
| 2904 | /* struct _objc_protocol_method_list { |
| 2905 | int protocol_method_count; |
| 2906 | struct protocol_methods protocols[]; |
| 2907 | } |
| 2908 | */ |
| 2909 | Result += "\nstatic struct {\n"; |
| 2910 | Result += "\tint protocol_method_count;\n"; |
| 2911 | Result += "\tstruct protocol_methods protocols["; |
| 2912 | Result += utostr(NumMethods); |
| 2913 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2914 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2915 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2916 | "{\n\t"; |
| 2917 | Result += utostr(NumMethods); |
| 2918 | Result += "\n"; |
| 2919 | |
| 2920 | // Output instance methods declared in this protocol. |
| 2921 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
| 2922 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 2923 | if (I == PDecl->classmeth_begin()) |
| 2924 | Result += "\t ,{{(SEL)\""; |
| 2925 | else |
| 2926 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2927 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2928 | std::string MethodTypeString; |
| 2929 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2930 | Result += "\", \""; |
| 2931 | Result += MethodTypeString; |
| 2932 | Result += "\"}\n"; |
| 2933 | } |
| 2934 | Result += "\t }\n};\n"; |
| 2935 | } |
| 2936 | |
| 2937 | // Output: |
| 2938 | /* struct _objc_protocol { |
| 2939 | // Objective-C 1.0 extensions |
| 2940 | struct _objc_protocol_extension *isa; |
| 2941 | char *protocol_name; |
| 2942 | struct _objc_protocol **protocol_list; |
| 2943 | struct _objc_protocol_method_list *instance_methods; |
| 2944 | struct _objc_protocol_method_list *class_methods; |
| 2945 | }; |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2946 | */ |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2947 | static bool objc_protocol = false; |
| 2948 | if (!objc_protocol) { |
| 2949 | Result += "\nstruct _objc_protocol {\n"; |
| 2950 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2951 | Result += "\tchar *protocol_name;\n"; |
| 2952 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2953 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2954 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2955 | Result += "};\n"; |
| 2956 | |
| 2957 | objc_protocol = true; |
| 2958 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2959 | |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2960 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2961 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2962 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 2963 | "{\n\t0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2964 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2965 | Result += "\", 0, "; |
| 2966 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2967 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2968 | Result += PDecl->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2969 | Result += ", "; |
| 2970 | } |
| 2971 | else |
| 2972 | Result += "0, "; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 2973 | if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2974 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2975 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2976 | Result += "\n"; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2977 | } |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2978 | else |
| 2979 | Result += "0\n"; |
| 2980 | Result += "};\n"; |
| 2981 | |
| 2982 | // Mark this protocol as having been generated. |
| 2983 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 2984 | assert(false && "protocol already synthesized"); |
| 2985 | } |
| 2986 | // Output the top lovel protocol meta-data for the class. |
| 2987 | /* struct _objc_protocol_list { |
| 2988 | struct _objc_protocol_list *next; |
| 2989 | int protocol_count; |
| 2990 | struct _objc_protocol *class_protocols[]; |
| 2991 | } |
| 2992 | */ |
| 2993 | Result += "\nstatic struct {\n"; |
| 2994 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 2995 | Result += "\tint protocol_count;\n"; |
| 2996 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 2997 | Result += utostr(Protocols.size()); |
| 2998 | Result += "];\n} _OBJC_"; |
| 2999 | Result += prefix; |
| 3000 | Result += "_PROTOCOLS_"; |
| 3001 | Result += ClassName; |
| 3002 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3003 | "{\n\t0, "; |
| 3004 | Result += utostr(Protocols.size()); |
| 3005 | Result += "\n"; |
| 3006 | |
| 3007 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3008 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3009 | Result += " \n"; |
| 3010 | |
| 3011 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3012 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3013 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | a19b5ec | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3014 | Result += "\n"; |
| 3015 | } |
| 3016 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3019 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3020 | /// implementation. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3021 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3022 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3023 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3024 | // Find category declaration for this implementation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3025 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3026 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 3027 | CDecl = CDecl->getNextClassCategory()) |
| 3028 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3029 | break; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3030 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3031 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3032 | FullCategoryName += '_'; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3033 | FullCategoryName += IDecl->getNameAsString(); |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3034 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3035 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3036 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3037 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3038 | Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3039 | |
| 3040 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3041 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3042 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3043 | Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3044 | |
| 3045 | // Protocols referenced in class declaration? |
Fariborz Jahanian | d256e06 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3046 | // Null CDecl is case of a category implementation with no category interface |
| 3047 | if (CDecl) |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3048 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Chris Lattner | a661a4d | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3049 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3050 | |
| 3051 | /* struct _objc_category { |
| 3052 | char *category_name; |
| 3053 | char *class_name; |
| 3054 | struct _objc_method_list *instance_methods; |
| 3055 | struct _objc_method_list *class_methods; |
| 3056 | struct _objc_protocol_list *protocols; |
| 3057 | // Objective-C 1.0 extensions |
| 3058 | uint32_t size; // sizeof (struct _objc_category) |
| 3059 | struct _objc_property_list *instance_properties; // category's own |
| 3060 | // @property decl. |
| 3061 | }; |
| 3062 | */ |
| 3063 | |
| 3064 | static bool objc_category = false; |
| 3065 | if (!objc_category) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3066 | Result += "\nstruct _objc_category {\n"; |
| 3067 | Result += "\tchar *category_name;\n"; |
| 3068 | Result += "\tchar *class_name;\n"; |
| 3069 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3070 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3071 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3072 | Result += "\tunsigned int size;\n"; |
| 3073 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3074 | Result += "};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3075 | objc_category = true; |
Fariborz Jahanian | 0445519 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3076 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3077 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3078 | Result += FullCategoryName; |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3079 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3080 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3081 | Result += "\"\n\t, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3082 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3083 | Result += "\"\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3084 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3085 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3086 | Result += "\t, (struct _objc_method_list *)" |
| 3087 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3088 | Result += FullCategoryName; |
| 3089 | Result += "\n"; |
| 3090 | } |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3091 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3092 | Result += "\t, 0\n"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3093 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3094 | Result += "\t, (struct _objc_method_list *)" |
| 3095 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3096 | Result += FullCategoryName; |
| 3097 | Result += "\n"; |
| 3098 | } |
| 3099 | else |
| 3100 | Result += "\t, 0\n"; |
| 3101 | |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3102 | if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3103 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 3104 | Result += FullCategoryName; |
| 3105 | Result += "\n"; |
| 3106 | } |
| 3107 | else |
| 3108 | Result += "\t, 0\n"; |
| 3109 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3112 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3113 | /// ivar offset. |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3114 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3115 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3116 | std::string &Result) { |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3117 | if (ivar->isBitField()) { |
| 3118 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3119 | // place all bitfields at offset 0. |
| 3120 | Result += "0"; |
| 3121 | } else { |
| 3122 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3123 | Result += IDecl->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3124 | if (LangOpts.Microsoft) |
| 3125 | Result += "_IMPL"; |
| 3126 | Result += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3127 | Result += ivar->getNameAsString(); |
Steve Naroff | d335422 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3128 | Result += ")"; |
| 3129 | } |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3132 | //===----------------------------------------------------------------------===// |
| 3133 | // Meta Data Emission |
| 3134 | //===----------------------------------------------------------------------===// |
| 3135 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3136 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3137 | std::string &Result) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3138 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3139 | |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3140 | // Explictly declared @interface's are already synthesized. |
| 3141 | if (CDecl->ImplicitInterfaceDecl()) { |
| 3142 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 3143 | // produce correct synthesis as yet. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3144 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | 7e21652 | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3145 | } |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3146 | |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3147 | // Build _objc_ivar_list metadata for classes ivars if needed |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3148 | unsigned NumIvars = !IDecl->ivar_empty() |
| 3149 | ? IDecl->ivar_size() |
| 3150 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3151 | if (NumIvars > 0) { |
| 3152 | static bool objc_ivar = false; |
| 3153 | if (!objc_ivar) { |
| 3154 | /* struct _objc_ivar { |
| 3155 | char *ivar_name; |
| 3156 | char *ivar_type; |
| 3157 | int ivar_offset; |
| 3158 | }; |
| 3159 | */ |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3160 | Result += "\nstruct _objc_ivar {\n"; |
| 3161 | Result += "\tchar *ivar_name;\n"; |
| 3162 | Result += "\tchar *ivar_type;\n"; |
| 3163 | Result += "\tint ivar_offset;\n"; |
| 3164 | Result += "};\n"; |
| 3165 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3166 | objc_ivar = true; |
| 3167 | } |
| 3168 | |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3169 | /* struct { |
| 3170 | int ivar_count; |
| 3171 | struct _objc_ivar ivar_list[nIvars]; |
| 3172 | }; |
| 3173 | */ |
| 3174 | Result += "\nstatic struct {\n"; |
| 3175 | Result += "\tint ivar_count;\n"; |
| 3176 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3177 | Result += utostr(NumIvars); |
| 3178 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3179 | Result += IDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3180 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3181 | "{\n\t"; |
| 3182 | Result += utostr(NumIvars); |
| 3183 | Result += "\n"; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3184 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3185 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | ec4979b | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3186 | if (!IDecl->ivar_empty()) { |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3187 | IVI = IDecl->ivar_begin(); |
| 3188 | IVE = IDecl->ivar_end(); |
| 3189 | } else { |
| 3190 | IVI = CDecl->ivar_begin(); |
| 3191 | IVE = CDecl->ivar_end(); |
| 3192 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3193 | Result += "\t,{{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3194 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3195 | Result += "\", \""; |
| 3196 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3197 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3198 | Result += StrEncoding; |
| 3199 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3200 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3201 | Result += "}\n"; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3202 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3203 | Result += "\t ,{\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3204 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3205 | Result += "\", \""; |
| 3206 | std::string StrEncoding; |
Daniel Dunbar | c9197cd | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3207 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | d5ea461 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3208 | Result += StrEncoding; |
| 3209 | Result += "\", "; |
Chris Lattner | c7b0675 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3210 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3211 | Result += "}\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3218 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3219 | true, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3220 | |
| 3221 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3222 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3223 | false, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3224 | |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3225 | // Protocols referenced in class declaration? |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3226 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3227 | "CLASS", CDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3228 | |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3229 | |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3230 | // Declaration of class/meta-class metadata |
| 3231 | /* struct _objc_class { |
| 3232 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3233 | const char *super_class_name; |
| 3234 | char *name; |
| 3235 | long version; |
| 3236 | long info; |
| 3237 | long instance_size; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3238 | struct _objc_ivar_list *ivars; |
| 3239 | struct _objc_method_list *methods; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3240 | struct objc_cache *cache; |
| 3241 | struct objc_protocol_list *protocols; |
| 3242 | const char *ivar_layout; |
| 3243 | struct _objc_class_ext *ext; |
| 3244 | }; |
| 3245 | */ |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3246 | static bool objc_class = false; |
| 3247 | if (!objc_class) { |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3248 | Result += "\nstruct _objc_class {\n"; |
| 3249 | Result += "\tstruct _objc_class *isa;\n"; |
| 3250 | Result += "\tconst char *super_class_name;\n"; |
| 3251 | Result += "\tchar *name;\n"; |
| 3252 | Result += "\tlong version;\n"; |
| 3253 | Result += "\tlong info;\n"; |
| 3254 | Result += "\tlong instance_size;\n"; |
| 3255 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3256 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3257 | Result += "\tstruct objc_cache *cache;\n"; |
| 3258 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3259 | Result += "\tconst char *ivar_layout;\n"; |
| 3260 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3261 | Result += "};\n"; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3262 | objc_class = true; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | // Meta-class metadata generation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3266 | ObjCInterfaceDecl *RootClass = 0; |
| 3267 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3268 | while (SuperClass) { |
| 3269 | RootClass = SuperClass; |
| 3270 | SuperClass = SuperClass->getSuperClass(); |
| 3271 | } |
| 3272 | SuperClass = CDecl->getSuperClass(); |
| 3273 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3274 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3275 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3276 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3277 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3278 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3279 | Result += "\""; |
| 3280 | |
| 3281 | if (SuperClass) { |
| 3282 | Result += ", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3283 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3284 | Result += "\", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3285 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3286 | Result += "\""; |
| 3287 | } |
| 3288 | else { |
| 3289 | Result += ", 0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3290 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3291 | Result += "\""; |
| 3292 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3293 | // 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] | 3294 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3295 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3296 | if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { |
Steve Naroff | dee066b | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3297 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3298 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3299 | Result += "\n"; |
| 3300 | } |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3301 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3302 | Result += ", 0\n"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3303 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3304 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3305 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3306 | Result += ",0,0\n"; |
| 3307 | } |
Fariborz Jahanian | 0cb4d92 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3308 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3309 | Result += "\t,0,0,0,0\n"; |
| 3310 | Result += "};\n"; |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3311 | |
| 3312 | // class metadata generation. |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3313 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3314 | Result += CDecl->getNameAsString(); |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3315 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3316 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3317 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3318 | if (SuperClass) { |
| 3319 | Result += ", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3320 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3321 | Result += "\", \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3322 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3323 | Result += "\""; |
| 3324 | } |
| 3325 | else { |
| 3326 | Result += ", 0, \""; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3327 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3328 | Result += "\""; |
| 3329 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3330 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3331 | Result += ", 0,1"; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3332 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3333 | Result += ",0"; |
| 3334 | else { |
| 3335 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 9447e46 | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3336 | Result += ",sizeof(struct "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3337 | Result += CDecl->getNameAsString(); |
Steve Naroff | c302e5b | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3338 | if (LangOpts.Microsoft) |
| 3339 | Result += "_IMPL"; |
Fariborz Jahanian | ab3ec25 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3340 | Result += ")"; |
| 3341 | } |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3342 | if (NumIvars > 0) { |
Steve Naroff | bec4bf5 | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3343 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3344 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3345 | Result += "\n\t"; |
| 3346 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3347 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3348 | Result += ",0"; |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3349 | if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { |
Steve Naroff | c723eec | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3350 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3351 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3352 | Result += ", 0\n\t"; |
| 3353 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3354 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3355 | Result += ",0,0"; |
Chris Lattner | 179fd52 | 2009-02-20 18:18:36 +0000 | [diff] [blame] | 3356 | if (CDecl->protocol_begin() != CDecl->protocol_end()) { |
Steve Naroff | 2742943 | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3357 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3358 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3359 | Result += ", 0,0\n"; |
| 3360 | } |
Fariborz Jahanian | f8fa1e7 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3361 | else |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3362 | Result += ",0,0,0\n"; |
| 3363 | Result += "};\n"; |
Fariborz Jahanian | 0f013d1 | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3364 | } |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3365 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3366 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3367 | /// and emits meta-data. |
| 3368 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3369 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3370 | int ClsDefCount = ClassImplementation.size(); |
| 3371 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3372 | |
Fariborz Jahanian | 8c66491 | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3373 | // Rewrite implemented methods |
| 3374 | for (int i = 0; i < ClsDefCount; i++) |
| 3375 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3376 | |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3377 | for (int i = 0; i < CatDefCount; i++) |
| 3378 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3379 | } |
Fariborz Jahanian | 0136e37 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3380 | |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3381 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3382 | int ClsDefCount = ClassImplementation.size(); |
| 3383 | int CatDefCount = CategoryImplementation.size(); |
| 3384 | |
Steve Naroff | 5bf1022 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3385 | // This is needed for determining instance variable offsets. |
Steve Naroff | 314c1a6 | 2008-08-05 18:47:23 +0000 | [diff] [blame] | 3386 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3387 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3388 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3389 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 9b4e4ff | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3390 | |
| 3391 | // For each implemented category, write out all its meta data. |
| 3392 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3393 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | 45d52f7 | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3394 | |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3395 | // Write objc_symtab metadata |
| 3396 | /* |
| 3397 | struct _objc_symtab |
| 3398 | { |
| 3399 | long sel_ref_cnt; |
| 3400 | SEL *refs; |
| 3401 | short cls_def_cnt; |
| 3402 | short cat_def_cnt; |
| 3403 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3404 | }; |
| 3405 | */ |
| 3406 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3407 | Result += "\nstruct _objc_symtab {\n"; |
| 3408 | Result += "\tlong sel_ref_cnt;\n"; |
| 3409 | Result += "\tSEL *refs;\n"; |
| 3410 | Result += "\tshort cls_def_cnt;\n"; |
| 3411 | Result += "\tshort cat_def_cnt;\n"; |
| 3412 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3413 | Result += "};\n\n"; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3414 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3415 | Result += "static struct _objc_symtab " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3416 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3417 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3418 | + ", " + utostr(CatDefCount) + "\n"; |
| 3419 | for (int i = 0; i < ClsDefCount; i++) { |
| 3420 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3421 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3422 | Result += "\n"; |
| 3423 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3424 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3425 | for (int i = 0; i < CatDefCount; i++) { |
| 3426 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3427 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3428 | Result += "_"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3429 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3430 | Result += "\n"; |
| 3431 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3432 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3433 | Result += "};\n\n"; |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3434 | |
| 3435 | // Write objc_module metadata |
| 3436 | |
| 3437 | /* |
| 3438 | struct _objc_module { |
| 3439 | long version; |
| 3440 | long size; |
| 3441 | const char *name; |
| 3442 | struct _objc_symtab *symtab; |
| 3443 | } |
| 3444 | */ |
| 3445 | |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3446 | Result += "\nstruct _objc_module {\n"; |
| 3447 | Result += "\tlong version;\n"; |
| 3448 | Result += "\tlong size;\n"; |
| 3449 | Result += "\tconst char *name;\n"; |
| 3450 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3451 | Result += "};\n\n"; |
| 3452 | Result += "static struct _objc_module " |
Steve Naroff | 5ce4a24 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3453 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | f185aef | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3454 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3455 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | cf89c7e | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3456 | Result += "};\n\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3457 | |
| 3458 | if (LangOpts.Microsoft) { |
| 3459 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | bdd2dba | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3460 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 7fd0aff | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3461 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3462 | Result += "&_OBJC_MODULES;\n"; |
| 3463 | Result += "#pragma data_seg(pop)\n\n"; |
| 3464 | } |
Fariborz Jahanian | 640a01f | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3465 | } |
Chris Lattner | 6fe8b27 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3466 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3467 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3468 | const char *funcName, |
| 3469 | std::string Tag) { |
| 3470 | const FunctionType *AFT = CE->getFunctionType(); |
| 3471 | QualType RT = AFT->getResultType(); |
| 3472 | std::string StructRef = "struct " + Tag; |
| 3473 | std::string S = "static " + RT.getAsString() + " __" + |
| 3474 | funcName + "_" + "block_func_" + utostr(i); |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3475 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3476 | BlockDecl *BD = CE->getBlockDecl(); |
| 3477 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3478 | if (isa<FunctionNoProtoType>(AFT)) { |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3479 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3480 | // block (to reference imported block decl refs). |
| 3481 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3482 | } else if (BD->param_empty()) { |
| 3483 | S += "(" + StructRef + " *__cself)"; |
| 3484 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3485 | const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3486 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3487 | S += '('; |
| 3488 | // first add the implicit argument. |
| 3489 | S += StructRef + " *__cself, "; |
| 3490 | std::string ParamStr; |
| 3491 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3492 | E = BD->param_end(); AI != E; ++AI) { |
| 3493 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3494 | ParamStr = (*AI)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3495 | (*AI)->getType().getAsStringInternal(ParamStr); |
| 3496 | S += ParamStr; |
| 3497 | } |
| 3498 | if (FT->isVariadic()) { |
| 3499 | if (!BD->param_empty()) S += ", "; |
| 3500 | S += "..."; |
| 3501 | } |
| 3502 | S += ')'; |
| 3503 | } |
| 3504 | S += " {\n"; |
| 3505 | |
| 3506 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3507 | // First, emit a declaration for all "by ref" decls. |
| 3508 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3509 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3510 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3511 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3512 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3513 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3514 | } |
| 3515 | // Next, emit a declaration for all "by copy" declarations. |
| 3516 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3517 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3518 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3519 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3520 | // Handle nested closure invocation. For example: |
| 3521 | // |
| 3522 | // void (^myImportedClosure)(void); |
| 3523 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3524 | // |
| 3525 | // void (^anotherClosure)(void); |
| 3526 | // anotherClosure = ^(void) { |
| 3527 | // myImportedClosure(); // import and invoke the closure |
| 3528 | // }; |
| 3529 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3530 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3531 | S += "struct __block_impl *"; |
| 3532 | else |
| 3533 | (*I)->getType().getAsStringInternal(Name); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3534 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3535 | } |
| 3536 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3537 | const char *cstr = RewrittenStr.c_str(); |
| 3538 | while (*cstr++ != '{') ; |
| 3539 | S += cstr; |
| 3540 | S += "\n"; |
| 3541 | return S; |
| 3542 | } |
Argiris Kirtzidis | c6cc7d5 | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3543 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3544 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3545 | const char *funcName, |
| 3546 | std::string Tag) { |
| 3547 | std::string StructRef = "struct " + Tag; |
| 3548 | std::string S = "static void __"; |
| 3549 | |
| 3550 | S += funcName; |
| 3551 | S += "_block_copy_" + utostr(i); |
| 3552 | S += "(" + StructRef; |
| 3553 | S += "*dst, " + StructRef; |
| 3554 | S += "*src) {"; |
| 3555 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3556 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3557 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3558 | S += (*I)->getNameAsString(); |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3559 | S += ", (void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3560 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3561 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3562 | } |
| 3563 | S += "\nstatic void __"; |
| 3564 | S += funcName; |
| 3565 | S += "_block_dispose_" + utostr(i); |
| 3566 | S += "(" + StructRef; |
| 3567 | S += "*src) {"; |
| 3568 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3569 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3570 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3571 | S += (*I)->getNameAsString(); |
Steve Naroff | 44fe1e3 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3572 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3573 | } |
| 3574 | S += "}\n"; |
| 3575 | return S; |
| 3576 | } |
| 3577 | |
| 3578 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3579 | bool hasCopyDisposeHelpers) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3580 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3581 | std::string Constructor = " " + Tag; |
| 3582 | |
| 3583 | S += " {\n struct __block_impl impl;\n"; |
| 3584 | |
| 3585 | if (hasCopyDisposeHelpers) |
| 3586 | S += " void *copy;\n void *dispose;\n"; |
| 3587 | |
| 3588 | Constructor += "(void *fp"; |
| 3589 | |
| 3590 | if (hasCopyDisposeHelpers) |
| 3591 | Constructor += ", void *copyHelp, void *disposeHelp"; |
| 3592 | |
| 3593 | if (BlockDeclRefs.size()) { |
| 3594 | // Output all "by copy" declarations. |
| 3595 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3596 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3597 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3598 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3599 | std::string ArgName = "_" + FieldName; |
| 3600 | // Handle nested closure invocation. For example: |
| 3601 | // |
| 3602 | // void (^myImportedBlock)(void); |
| 3603 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3604 | // |
| 3605 | // void (^anotherBlock)(void); |
| 3606 | // anotherBlock = ^(void) { |
| 3607 | // myImportedBlock(); // import and invoke the closure |
| 3608 | // }; |
| 3609 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3610 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3611 | S += "struct __block_impl *"; |
| 3612 | Constructor += ", void *" + ArgName; |
| 3613 | } else { |
| 3614 | (*I)->getType().getAsStringInternal(FieldName); |
| 3615 | (*I)->getType().getAsStringInternal(ArgName); |
| 3616 | Constructor += ", " + ArgName; |
| 3617 | } |
| 3618 | S += FieldName + ";\n"; |
| 3619 | } |
| 3620 | // Output all "by ref" declarations. |
| 3621 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3622 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3623 | S += " "; |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3624 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3625 | std::string ArgName = "_" + FieldName; |
| 3626 | // Handle nested closure invocation. For example: |
| 3627 | // |
| 3628 | // void (^myImportedBlock)(void); |
| 3629 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3630 | // |
| 3631 | // void (^anotherBlock)(void); |
| 3632 | // anotherBlock = ^(void) { |
| 3633 | // myImportedBlock(); // import and invoke the closure |
| 3634 | // }; |
| 3635 | // |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3636 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3637 | S += "struct __block_impl *"; |
| 3638 | Constructor += ", void *" + ArgName; |
| 3639 | } else { |
| 3640 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName); |
| 3641 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName); |
| 3642 | Constructor += ", " + ArgName; |
| 3643 | } |
| 3644 | S += FieldName + "; // by ref\n"; |
| 3645 | } |
| 3646 | // Finish writing the constructor. |
| 3647 | // FIXME: handle NSConcreteGlobalBlock. |
| 3648 | Constructor += ", int flags=0) {\n"; |
| 3649 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3650 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3651 | |
| 3652 | if (hasCopyDisposeHelpers) |
| 3653 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3654 | |
| 3655 | // Initialize all "by copy" arguments. |
| 3656 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3657 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3658 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3659 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3660 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3661 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3662 | else |
| 3663 | Constructor += Name + " = _"; |
| 3664 | Constructor += Name + ";\n"; |
| 3665 | } |
| 3666 | // Initialize all "by ref" arguments. |
| 3667 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3668 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3669 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3670 | Constructor += " "; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3671 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3672 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3673 | else |
| 3674 | Constructor += Name + " = _"; |
| 3675 | Constructor += Name + ";\n"; |
| 3676 | } |
| 3677 | } else { |
| 3678 | // Finish writing the constructor. |
| 3679 | // FIXME: handle NSConcreteGlobalBlock. |
| 3680 | Constructor += ", int flags=0) {\n"; |
| 3681 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3682 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3683 | if (hasCopyDisposeHelpers) |
| 3684 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3685 | } |
| 3686 | Constructor += " "; |
| 3687 | Constructor += "}\n"; |
| 3688 | S += Constructor; |
| 3689 | S += "};\n"; |
| 3690 | return S; |
| 3691 | } |
| 3692 | |
| 3693 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3694 | const char *FunName) { |
| 3695 | // Insert closures that were part of the function. |
| 3696 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3697 | |
| 3698 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3699 | |
| 3700 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3701 | |
| 3702 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
| 3703 | ImportedBlockDecls.size() > 0); |
| 3704 | |
| 3705 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3706 | |
| 3707 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
| 3708 | |
| 3709 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3710 | |
| 3711 | if (ImportedBlockDecls.size()) { |
| 3712 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3713 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3714 | } |
| 3715 | |
| 3716 | BlockDeclRefs.clear(); |
| 3717 | BlockByRefDecls.clear(); |
| 3718 | BlockByCopyDecls.clear(); |
| 3719 | BlockCallExprs.clear(); |
| 3720 | ImportedBlockDecls.clear(); |
| 3721 | } |
| 3722 | Blocks.clear(); |
| 3723 | RewrittenBlockExprs.clear(); |
| 3724 | } |
| 3725 | |
| 3726 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3727 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3728 | const char *FuncName = FD->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3729 | |
| 3730 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3731 | } |
| 3732 | |
| 3733 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | 6449c6c | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3734 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3735 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3736 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3737 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3738 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3739 | // Convert colons to underscores. |
| 3740 | std::string::size_type loc = 0; |
| 3741 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3742 | FuncName.replace(loc, 1, "_"); |
| 3743 | |
| 3744 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3745 | } |
| 3746 | |
| 3747 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3748 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3749 | CI != E; ++CI) |
| 3750 | if (*CI) { |
| 3751 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3752 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3753 | else |
| 3754 | GetBlockDeclRefExprs(*CI); |
| 3755 | } |
| 3756 | // Handle specific things. |
| 3757 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3758 | // FIXME: Handle enums. |
| 3759 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3760 | BlockDeclRefs.push_back(CDRE); |
| 3761 | return; |
| 3762 | } |
| 3763 | |
| 3764 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3765 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3766 | CI != E; ++CI) |
| 3767 | if (*CI) { |
| 3768 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3769 | GetBlockCallExprs(CBE->getBody()); |
| 3770 | else |
| 3771 | GetBlockCallExprs(*CI); |
| 3772 | } |
| 3773 | |
| 3774 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3775 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3776 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3777 | } |
| 3778 | } |
| 3779 | return; |
| 3780 | } |
| 3781 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3782 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3783 | // Navigate to relevant type information. |
| 3784 | const char *closureName = 0; |
| 3785 | const BlockPointerType *CPT = 0; |
| 3786 | |
| 3787 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3788 | closureName = DRE->getDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3789 | CPT = DRE->getType()->getAsBlockPointerType(); |
| 3790 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3791 | closureName = CDRE->getDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3792 | CPT = CDRE->getType()->getAsBlockPointerType(); |
| 3793 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3794 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3795 | CPT = MExpr->getType()->getAsBlockPointerType(); |
| 3796 | } else { |
| 3797 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3798 | } |
| 3799 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3800 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3801 | assert(FT && "RewriteBlockClass: Bad type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3802 | const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3803 | // FTP will be null for closures that don't take arguments. |
| 3804 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3805 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3806 | SourceLocation(), |
| 3807 | &Context->Idents.get("__block_impl")); |
| 3808 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3809 | |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3810 | // Generate a funky cast. |
| 3811 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3812 | |
| 3813 | // Push the block argument type. |
| 3814 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3815 | if (FTP) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3816 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3817 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3818 | QualType t = *I; |
| 3819 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3820 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3821 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 3822 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3823 | } |
| 3824 | ArgTypes.push_back(t); |
| 3825 | } |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3826 | } |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3827 | // Now do the pointer to function cast. |
| 3828 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
| 3829 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
| 3830 | |
| 3831 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3832 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3833 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, Exp->getCallee(), |
| 3834 | PtrBlock, SourceLocation(), |
| 3835 | SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3836 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3837 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3838 | BlkCast); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3839 | //PE->dump(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3840 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3841 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3842 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3843 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3844 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 3845 | FD->getType()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3846 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3847 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, ME, |
| 3848 | PtrToFuncCastType, |
| 3849 | SourceLocation(), |
| 3850 | SourceLocation()); |
| 3851 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3852 | |
| 3853 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3854 | // Add the implicit argument. |
| 3855 | BlkExprs.push_back(BlkCast); |
| 3856 | // Add the user arguments. |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3857 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3858 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3859 | BlkExprs.push_back(*I); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3860 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3861 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3862 | BlkExprs.size(), |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3863 | Exp->getType(), SourceLocation()); |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3864 | return CE; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3868 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3869 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | void RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
| 3873 | // FIXME: Add more elaborate code generation required by the ABI. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3874 | Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3875 | Context->getPointerType(BDRE->getType()), |
| 3876 | SourceLocation()); |
| 3877 | // Need parens to enforce precedence. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3878 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); |
Steve Naroff | 16478cf | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3879 | ReplaceStmt(BDRE, PE); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3882 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3883 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3884 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3885 | |
| 3886 | // Need to avoid trying to rewrite synthesized casts. |
| 3887 | if (LocStart.isInvalid()) |
| 3888 | return; |
Steve Naroff | 1c53c02 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3889 | // Need to avoid trying to rewrite casts contained in macros. |
| 3890 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3891 | return; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3892 | |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3893 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3894 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 3895 | |
| 3896 | // advance the location to startArgList. |
| 3897 | const char *argPtr = startBuf; |
| 3898 | |
| 3899 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3900 | switch (*argPtr) { |
| 3901 | case '^': |
| 3902 | // Replace the '^' with '*'. |
| 3903 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 3904 | ReplaceText(LocStart, 1, "*", 1); |
| 3905 | break; |
| 3906 | } |
| 3907 | } |
| 3908 | return; |
| 3909 | } |
| 3910 | |
| 3911 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3912 | SourceLocation DeclLoc = FD->getLocation(); |
| 3913 | unsigned parenCount = 0; |
| 3914 | |
| 3915 | // We have 1 or more arguments that have closure pointers. |
| 3916 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3917 | const char *startArgList = strchr(startBuf, '('); |
| 3918 | |
| 3919 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 3920 | |
| 3921 | parenCount++; |
| 3922 | // advance the location to startArgList. |
| 3923 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 3924 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 3925 | |
| 3926 | const char *argPtr = startArgList; |
| 3927 | |
| 3928 | while (*argPtr++ && parenCount) { |
| 3929 | switch (*argPtr) { |
| 3930 | case '^': |
| 3931 | // Replace the '^' with '*'. |
| 3932 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 3933 | ReplaceText(DeclLoc, 1, "*", 1); |
| 3934 | break; |
| 3935 | case '(': |
| 3936 | parenCount++; |
| 3937 | break; |
| 3938 | case ')': |
| 3939 | parenCount--; |
| 3940 | break; |
| 3941 | } |
| 3942 | } |
| 3943 | return; |
| 3944 | } |
| 3945 | |
| 3946 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3947 | const FunctionProtoType *FTP; |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3948 | const PointerType *PT = QT->getAsPointerType(); |
| 3949 | if (PT) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3950 | FTP = PT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3951 | } else { |
| 3952 | const BlockPointerType *BPT = QT->getAsBlockPointerType(); |
| 3953 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3954 | FTP = BPT->getPointeeType()->getAsFunctionProtoType(); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3955 | } |
| 3956 | if (FTP) { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3957 | for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3958 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3959 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3960 | return true; |
| 3961 | } |
| 3962 | return false; |
| 3963 | } |
| 3964 | |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3965 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 3966 | const char *&RParen) { |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3967 | const char *argPtr = strchr(Name, '('); |
| 3968 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 3969 | |
| 3970 | LParen = argPtr; // output the start. |
| 3971 | argPtr++; // skip past the left paren. |
| 3972 | unsigned parenCount = 1; |
| 3973 | |
| 3974 | while (*argPtr && parenCount) { |
| 3975 | switch (*argPtr) { |
| 3976 | case '(': parenCount++; break; |
| 3977 | case ')': parenCount--; break; |
| 3978 | default: break; |
| 3979 | } |
| 3980 | if (parenCount) argPtr++; |
| 3981 | } |
| 3982 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 3983 | RParen = argPtr; // output the end |
| 3984 | } |
| 3985 | |
| 3986 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 3987 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 3988 | RewriteBlockPointerFunctionArgs(FD); |
| 3989 | return; |
| 3990 | } |
| 3991 | // Handle Variables and Typedefs. |
| 3992 | SourceLocation DeclLoc = ND->getLocation(); |
| 3993 | QualType DeclT; |
| 3994 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3995 | DeclT = VD->getType(); |
| 3996 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 3997 | DeclT = TDD->getUnderlyingType(); |
| 3998 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 3999 | DeclT = FD->getType(); |
| 4000 | else |
| 4001 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4002 | |
| 4003 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4004 | const char *endBuf = startBuf; |
| 4005 | // scan backward (from the decl location) for the end of the previous decl. |
| 4006 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4007 | startBuf--; |
| 4008 | |
| 4009 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4010 | // may take block argument types (which will be handled below). |
| 4011 | if (*startBuf == '^') { |
| 4012 | // Replace the '^' with '*', computing a negative offset. |
| 4013 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4014 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4015 | } |
| 4016 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4017 | // Replace the '^' with '*' for arguments. |
| 4018 | DeclLoc = ND->getLocation(); |
| 4019 | startBuf = SM->getCharacterData(DeclLoc); |
| 4020 | const char *argListBegin, *argListEnd; |
| 4021 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4022 | while (argListBegin < argListEnd) { |
| 4023 | if (*argListBegin == '^') { |
| 4024 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4025 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4026 | } |
| 4027 | argListBegin++; |
| 4028 | } |
| 4029 | } |
| 4030 | return; |
| 4031 | } |
| 4032 | |
| 4033 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 4034 | // Add initializers for any closure decl refs. |
| 4035 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4036 | if (BlockDeclRefs.size()) { |
| 4037 | // Unique all "by copy" declarations. |
| 4038 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4039 | if (!BlockDeclRefs[i]->isByRef()) |
| 4040 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4041 | // Unique all "by ref" declarations. |
| 4042 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4043 | if (BlockDeclRefs[i]->isByRef()) { |
| 4044 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4045 | } |
| 4046 | // Find any imported blocks...they will need special attention. |
| 4047 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Steve Naroff | 1d56d9e | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4048 | if (BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | e59c8b1 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4049 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 38a9e3f | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4050 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4051 | } |
| 4052 | } |
| 4053 | } |
| 4054 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4055 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4056 | IdentifierInfo *ID = &Context->Idents.get(name); |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4057 | QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4058 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 4059 | ID, FType, FunctionDecl::Extern, false, |
| 4060 | false); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4063 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4064 | Blocks.push_back(Exp); |
| 4065 | |
| 4066 | CollectBlockDeclRefInfo(Exp); |
| 4067 | std::string FuncName; |
| 4068 | |
| 4069 | if (CurFunctionDef) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4070 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4071 | else if (CurMethodDef) { |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4072 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4073 | // Convert colons to underscores. |
| 4074 | std::string::size_type loc = 0; |
| 4075 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4076 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4077 | } else if (GlobalVarDecl) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4078 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4079 | |
| 4080 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4081 | |
| 4082 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4083 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4084 | |
| 4085 | // Get a pointer to the function type so we can cast appropriately. |
| 4086 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4087 | |
| 4088 | FunctionDecl *FD; |
| 4089 | Expr *NewRep; |
| 4090 | |
| 4091 | // Simulate a contructor call... |
| 4092 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4093 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4094 | |
| 4095 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 4096 | |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4097 | // Initialize the block function. |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4098 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4099 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4100 | SourceLocation()); |
| 4101 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4102 | Context->VoidPtrTy, SourceLocation(), |
| 4103 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4104 | InitExprs.push_back(castExpr); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4105 | |
| 4106 | if (ImportedBlockDecls.size()) { |
| 4107 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4108 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4109 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4110 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4111 | Context->VoidPtrTy, SourceLocation(), |
| 4112 | SourceLocation()); |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4113 | InitExprs.push_back(castExpr); |
| 4114 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4115 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4116 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4117 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4118 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4119 | Context->VoidPtrTy, SourceLocation(), |
| 4120 | SourceLocation()); |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4121 | InitExprs.push_back(castExpr); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4122 | } |
| 4123 | // Add initializers for any closure decl refs. |
| 4124 | if (BlockDeclRefs.size()) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4125 | Expr *Exp; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4126 | // Output all "by copy" declarations. |
| 4127 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4128 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4129 | if (isObjCType((*I)->getType())) { |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4130 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4131 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4132 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4133 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4134 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4135 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4136 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4137 | Context->VoidPtrTy, SourceLocation(), |
| 4138 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4139 | } else { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4140 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4141 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4142 | } |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4143 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4144 | } |
| 4145 | // Output all "by ref" declarations. |
| 4146 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4147 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4148 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4149 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4150 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Steve Naroff | d0419d6 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4151 | Context->getPointerType(Exp->getType()), |
| 4152 | SourceLocation()); |
Steve Naroff | 362c756 | 2008-11-14 21:36:12 +0000 | [diff] [blame] | 4153 | InitExprs.push_back(Exp); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4154 | } |
| 4155 | } |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4156 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4157 | FType, SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4158 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4159 | Context->getPointerType(NewRep->getType()), |
| 4160 | SourceLocation()); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4161 | NewRep = new (Context) CStyleCastExpr(FType, NewRep, FType, SourceLocation(), |
| 4162 | SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4163 | BlockDeclRefs.clear(); |
| 4164 | BlockByRefDecls.clear(); |
| 4165 | BlockByCopyDecls.clear(); |
| 4166 | ImportedBlockDecls.clear(); |
| 4167 | return NewRep; |
| 4168 | } |
| 4169 | |
| 4170 | //===----------------------------------------------------------------------===// |
| 4171 | // Function Body / Expression rewriting |
| 4172 | //===----------------------------------------------------------------------===// |
| 4173 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4174 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4175 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4176 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4177 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4178 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4179 | // we get this right. |
| 4180 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4181 | // Perform a bottom up traversal of all children. |
| 4182 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4183 | CI != E; ++CI) |
| 4184 | if (*CI) |
| 4185 | CollectPropertySetters(*CI); |
| 4186 | |
| 4187 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4188 | if (BinOp->isAssignmentOp()) { |
| 4189 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4190 | PropSetters[PRE] = BinOp; |
| 4191 | } |
| 4192 | } |
| 4193 | } |
| 4194 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4195 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4196 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4197 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4198 | Stmts.push_back(S); |
| 4199 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4200 | Stmts.push_back(S); |
| 4201 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4202 | } |
| 4203 | |
| 4204 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4205 | |
| 4206 | // Perform a bottom up rewrite of all children. |
| 4207 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4208 | CI != E; ++CI) |
| 4209 | if (*CI) { |
| 4210 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
| 4211 | if (newStmt) |
| 4212 | *CI = newStmt; |
| 4213 | } |
| 4214 | |
| 4215 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4216 | // Rewrite the block body in place. |
| 4217 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4218 | |
| 4219 | // 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] | 4220 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4221 | RewrittenBlockExprs[BE] = Str; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4222 | |
| 4223 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4224 | //blockTranscribed->dump(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4225 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4226 | return blockTranscribed; |
| 4227 | } |
| 4228 | // Handle specific things. |
| 4229 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4230 | return RewriteAtEncode(AtEncode); |
| 4231 | |
| 4232 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4233 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4234 | |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4235 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4236 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4237 | if (BinOp) { |
| 4238 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4239 | // 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] | 4240 | DisableReplaceStmt = true; |
| 4241 | // Save the source range. Even if we disable the replacement, the |
| 4242 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4243 | // node is at the 'end', the rewriter will fail. Consider this: |
| 4244 | // self.errorHandler = handler ? handler : |
| 4245 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4246 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4247 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4248 | DisableReplaceStmt = false; |
Steve Naroff | 102c3f2 | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4249 | // |
| 4250 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4251 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4252 | // to see the original expression). Consider this example: |
| 4253 | // |
| 4254 | // Foo *obj1, *obj2; |
| 4255 | // |
| 4256 | // obj1.i = [obj2 rrrr]; |
| 4257 | // |
| 4258 | // 'BinOp' for the previous expression looks like: |
| 4259 | // |
| 4260 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4261 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4262 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4263 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4264 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4265 | // |
| 4266 | // 'newStmt' represents the rewritten message expression. For example: |
| 4267 | // |
| 4268 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4269 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4270 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4271 | // (CStyleCastExpr 0x231d220 'void *' |
| 4272 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4273 | // |
| 4274 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4275 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4276 | // the original RHS (since we haven't altered BinOp). |
| 4277 | // |
| 4278 | // This implies the Rewrite* routines can no longer delete the original |
| 4279 | // node. As a result, we now leak the original AST nodes. |
| 4280 | // |
Steve Naroff | edb4bc9 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4281 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4282 | } else { |
| 4283 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | f6ce8a1 | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4284 | } |
| 4285 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4286 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4287 | return RewriteAtSelector(AtSelector); |
| 4288 | |
| 4289 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4290 | return RewriteObjCStringLiteral(AtString); |
| 4291 | |
| 4292 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4293 | #if 0 |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4294 | // Before we rewrite it, put the original message expression in a comment. |
| 4295 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4296 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4297 | |
| 4298 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4299 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4300 | |
| 4301 | std::string messString; |
| 4302 | messString += "// "; |
| 4303 | messString.append(startBuf, endBuf-startBuf+1); |
| 4304 | messString += "\n"; |
| 4305 | |
| 4306 | // FIXME: Missing definition of |
| 4307 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4308 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4309 | // Tried this, but it didn't work either... |
| 4310 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4311 | #endif |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4312 | return RewriteMessageExpr(MessExpr); |
| 4313 | } |
| 4314 | |
| 4315 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4316 | return RewriteObjCTryStmt(StmtTry); |
| 4317 | |
| 4318 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4319 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4320 | |
| 4321 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4322 | return RewriteObjCThrowStmt(StmtThrow); |
| 4323 | |
| 4324 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4325 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4326 | |
| 4327 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4328 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4329 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4330 | OrigStmtRange.getEnd()); |
| 4331 | if (BreakStmt *StmtBreakStmt = |
| 4332 | dyn_cast<BreakStmt>(S)) |
| 4333 | return RewriteBreakStmt(StmtBreakStmt); |
| 4334 | if (ContinueStmt *StmtContinueStmt = |
| 4335 | dyn_cast<ContinueStmt>(S)) |
| 4336 | return RewriteContinueStmt(StmtContinueStmt); |
| 4337 | |
| 4338 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4339 | // and cast exprs. |
| 4340 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4341 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4342 | // precedes the first Decl. In the future the DeclGroup should have |
| 4343 | // a separate type-specifier that we can rewrite. |
| 4344 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4345 | |
| 4346 | // Blocks rewrite rules. |
| 4347 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4348 | DI != DE; ++DI) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4349 | Decl *SD = *DI; |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4350 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4351 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4352 | RewriteBlockPointerDecl(ND); |
| 4353 | else if (ND->getType()->isFunctionPointerType()) |
| 4354 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4355 | } |
| 4356 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4357 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4358 | RewriteBlockPointerDecl(TD); |
| 4359 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4360 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4361 | } |
| 4362 | } |
| 4363 | } |
| 4364 | |
| 4365 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4366 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4367 | |
| 4368 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4369 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4370 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4371 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4372 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4373 | && "Statement stack mismatch"); |
| 4374 | Stmts.pop_back(); |
| 4375 | } |
| 4376 | // Handle blocks rewriting. |
| 4377 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4378 | if (BDRE->isByRef()) |
| 4379 | RewriteBlockDeclRefExpr(BDRE); |
| 4380 | } |
| 4381 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | 85eb17b | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4382 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4383 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4384 | ReplaceStmt(S, BlockCall); |
| 4385 | return BlockCall; |
| 4386 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4387 | } |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4388 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4389 | RewriteCastExpr(CE); |
| 4390 | } |
| 4391 | #if 0 |
| 4392 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4393 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4394 | // Get the new text. |
| 4395 | std::string SStr; |
| 4396 | llvm::raw_string_ostream Buf(SStr); |
| 4397 | Replacement->printPretty(Buf); |
| 4398 | const std::string &Str = Buf.str(); |
| 4399 | |
| 4400 | printf("CAST = %s\n", &Str[0]); |
| 4401 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4402 | delete S; |
| 4403 | return Replacement; |
| 4404 | } |
| 4405 | #endif |
| 4406 | // Return this stmt unmodified. |
| 4407 | return S; |
| 4408 | } |
| 4409 | |
| 4410 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4411 | /// main file of the input. |
| 4412 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4413 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | ee8d497 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 4414 | if (FD->isOverloadedOperator()) |
| 4415 | return; |
| 4416 | |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4417 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4418 | // prototype. This enables us to rewrite function declarations and |
| 4419 | // definitions using the same code. |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4420 | RewriteBlocksInFunctionProtoType(FD->getType(), FD); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4421 | |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4422 | if (CompoundStmt *Body = FD->getBody()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4423 | CurFunctionDef = FD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4424 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4425 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4426 | Body = |
| 4427 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4428 | FD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4429 | CurrentBody = 0; |
| 4430 | if (PropParentMap) { |
| 4431 | delete PropParentMap; |
| 4432 | PropParentMap = 0; |
| 4433 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4434 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4435 | // and any copy/dispose helper functions. |
| 4436 | InsertBlockLiteralsWithinFunction(FD); |
| 4437 | CurFunctionDef = 0; |
| 4438 | } |
| 4439 | return; |
| 4440 | } |
| 4441 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4442 | if (CompoundStmt *Body = MD->getBody()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4443 | CurMethodDef = MD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4444 | CollectPropertySetters(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4445 | CurrentBody = Body; |
Ted Kremenek | 3091607 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4446 | Body = |
| 4447 | cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); |
| 4448 | MD->setBody(Body); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4449 | CurrentBody = 0; |
| 4450 | if (PropParentMap) { |
| 4451 | delete PropParentMap; |
| 4452 | PropParentMap = 0; |
| 4453 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4454 | InsertBlockLiteralsWithinMethod(MD); |
| 4455 | CurMethodDef = 0; |
| 4456 | } |
| 4457 | } |
| 4458 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4459 | ClassImplementation.push_back(CI); |
| 4460 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4461 | CategoryImplementation.push_back(CI); |
| 4462 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4463 | RewriteForwardClassDecl(CD); |
| 4464 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4465 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4466 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4467 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4468 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4469 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4470 | if (VD->getInit()) { |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4471 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4472 | RewriteCastExpr(CE); |
| 4473 | } |
| 4474 | } |
| 4475 | } |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4476 | if (VD->getInit()) { |
| 4477 | GlobalVarDecl = VD; |
Steve Naroff | 0e94841 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4478 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4479 | CurrentBody = VD->getInit(); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4480 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | fbed680 | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4481 | CurrentBody = 0; |
| 4482 | if (PropParentMap) { |
| 4483 | delete PropParentMap; |
| 4484 | PropParentMap = 0; |
| 4485 | } |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 4486 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4487 | VD->getNameAsCString()); |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4488 | GlobalVarDecl = 0; |
| 4489 | |
| 4490 | // This is needed for blocks. |
Steve Naroff | 7f1412d | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4491 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 80c5475 | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4492 | RewriteCastExpr(CE); |
| 4493 | } |
| 4494 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4495 | return; |
| 4496 | } |
| 4497 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4498 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4499 | RewriteBlockPointerDecl(TD); |
| 4500 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4501 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4502 | return; |
| 4503 | } |
| 4504 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 4505 | if (RD->isDefinition()) { |
Douglas Gregor | 640a04b | 2008-12-11 17:59:21 +0000 | [diff] [blame] | 4506 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4507 | e = RD->field_end(); i != e; ++i) { |
| 4508 | FieldDecl *FD = *i; |
Steve Naroff | d896f4b | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4509 | if (isTopLevelBlockPointerType(FD->getType())) |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4510 | RewriteBlockPointerDecl(FD); |
| 4511 | } |
| 4512 | } |
| 4513 | return; |
| 4514 | } |
| 4515 | // Nothing yet. |
| 4516 | } |
| 4517 | |
Chris Lattner | 2a594d0 | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 4518 | void RewriteObjC::HandleTranslationUnit(ASTContext &C) { |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4519 | // Get the top-level buffer that this corresponds to. |
| 4520 | |
| 4521 | // Rewrite tabs if we care. |
| 4522 | //RewriteTabs(); |
| 4523 | |
| 4524 | if (Diags.hasErrorOccurred()) |
| 4525 | return; |
| 4526 | |
| 4527 | // Create the output file. |
| 4528 | |
| 4529 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 4530 | llvm::raw_ostream *OutFile; |
| 4531 | if (OutFileName == "-") { |
| 4532 | OutFile = &llvm::outs(); |
| 4533 | } else if (!OutFileName.empty()) { |
| 4534 | std::string Err; |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 4535 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), |
| 4536 | // set binary mode (critical for Windoze) |
| 4537 | true, |
| 4538 | Err); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4539 | OwnedStream.reset(OutFile); |
| 4540 | } else if (InFileName == "-") { |
| 4541 | OutFile = &llvm::outs(); |
| 4542 | } else { |
| 4543 | llvm::sys::Path Path(InFileName); |
| 4544 | Path.eraseSuffix(); |
| 4545 | Path.appendSuffix("cpp"); |
| 4546 | std::string Err; |
Steve Naroff | 08299f8 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 4547 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), |
| 4548 | // set binary mode (critical for Windoze) |
| 4549 | true, |
| 4550 | Err); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4551 | OwnedStream.reset(OutFile); |
| 4552 | } |
| 4553 | |
| 4554 | RewriteInclude(); |
| 4555 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 4556 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4557 | Preamble.c_str(), Preamble.size(), false); |
| 4558 | |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4559 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4560 | RewriteImplementations(); |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4561 | |
| 4562 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4563 | // we are done. |
| 4564 | if (const RewriteBuffer *RewriteBuf = |
| 4565 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4566 | //printf("Changed:\n"); |
| 4567 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4568 | } else { |
| 4569 | fprintf(stderr, "No changes\n"); |
| 4570 | } |
Steve Naroff | 21658f6 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4571 | |
Steve Naroff | 901d8fd | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4572 | if (ClassImplementation.size() || CategoryImplementation.size()) { |
| 4573 | // Rewrite Objective-c meta data* |
| 4574 | std::string ResultStr; |
| 4575 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4576 | // Emit metadata. |
| 4577 | *OutFile << ResultStr; |
| 4578 | } |
Steve Naroff | 0f3b9eb | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4579 | OutFile->flush(); |
| 4580 | } |
| 4581 | |