Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1 | //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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 | 77cd2a0 | 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 | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 15 | #include "clang/Rewrite/Rewriter.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 18 | #include "clang/AST/TranslationUnit.h" |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 19 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 63e2dcc | 2008-05-21 20:19:16 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 31 | #include "llvm/System/Path.h" |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 32 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 33 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 34 | |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 35 | static llvm::cl::opt<bool> |
| 36 | SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false), |
| 37 | llvm::cl::desc("Silence ObjC rewriting warnings")); |
| 38 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 39 | namespace { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 40 | class RewriteObjC : public ASTConsumer { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 41 | Rewriter Rewrite; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 42 | Diagnostic &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 43 | const LangOptions &LangOpts; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 44 | unsigned RewriteFailedDiag; |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 45 | unsigned TryFinallyContainsReturnDiag; |
| 46 | |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 47 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 48 | SourceManager *SM; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 49 | TranslationUnitDecl *TUDecl; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 50 | FileID MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 51 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 52 | SourceLocation LastIncLoc; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 54 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 55 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 56 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 57 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 58 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 59 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 60 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 61 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 62 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 63 | unsigned NumObjCStringLiterals; |
| 64 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 65 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 66 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 67 | FunctionDecl *MsgSendStretFunctionDecl; |
| 68 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 69 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 70 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 71 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 72 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 73 | FunctionDecl *CFStringFunctionDecl; |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 74 | FunctionDecl *GetProtocolFunctionDecl; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 75 | FunctionDecl *SuperContructorFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 76 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 77 | // ObjC string constant support. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 78 | VarDecl *ConstantStringClassReference; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 79 | RecordDecl *NSStringRecord; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 80 | |
Fariborz Jahanian | b586cce | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 81 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 82 | int BcLabelCount; |
| 83 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 84 | // Needed for super. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 85 | ObjCMethodDecl *CurMethodDef; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 86 | RecordDecl *SuperStructDecl; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 87 | RecordDecl *ConstantStringDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 88 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 89 | // Needed for header files being rewritten |
| 90 | bool IsHeader; |
| 91 | |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 92 | std::string InFileName; |
| 93 | std::string OutFileName; |
| 94 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 95 | std::string Preamble; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 96 | |
| 97 | // Block expressions. |
| 98 | llvm::SmallVector<BlockExpr *, 32> Blocks; |
| 99 | llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; |
| 100 | llvm::DenseMap<BlockDeclRefExpr *, CallExpr *> BlockCallExprs; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 101 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 102 | // Block related declarations. |
| 103 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDecls; |
| 104 | llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDecls; |
| 105 | llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; |
| 106 | |
| 107 | llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; |
| 108 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 109 | // This maps a property to it's assignment statement. |
| 110 | llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 111 | // This maps a property to it's synthesied message expression. |
| 112 | // This allows us to rewrite chained getters (e.g. o.a.b.c). |
| 113 | llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; |
| 114 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 115 | // This maps an original source AST to it's rewritten form. This allows |
| 116 | // us to avoid rewriting the same node twice (which is very uncommon). |
| 117 | // This is needed to support some of the exotic property rewriting. |
| 118 | llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 119 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 120 | FunctionDecl *CurFunctionDef; |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 121 | VarDecl *GlobalVarDecl; |
| 122 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 123 | bool DisableReplaceStmt; |
| 124 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 125 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 126 | public: |
Ted Kremenek | e3a6198 | 2008-05-31 20:11:04 +0000 | [diff] [blame] | 127 | virtual void Initialize(ASTContext &context); |
| 128 | |
| 129 | virtual void InitializeTU(TranslationUnit &TU) { |
| 130 | TU.SetOwnsDecls(false); |
| 131 | Initialize(TU.getContext()); |
| 132 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 134 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 135 | // Top Level Driver code. |
| 136 | virtual void HandleTopLevelDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 137 | void HandleDeclInMainFile(Decl *D); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 138 | RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 139 | Diagnostic &D, const LangOptions &LOpts); |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 140 | |
| 141 | ~RewriteObjC() {} |
| 142 | |
| 143 | virtual void HandleTranslationUnit(TranslationUnit& TU); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 144 | |
| 145 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 146 | Stmt *ReplacingStmt = ReplacedNodes[Old]; |
| 147 | |
| 148 | if (ReplacingStmt) |
| 149 | return; // We can't rewrite the same node twice. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 150 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 151 | if (DisableReplaceStmt) |
| 152 | return; // Used when rewriting the assignment of a property setter. |
| 153 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 154 | // If replacement succeeded or warning disabled return with no warning. |
| 155 | if (!Rewrite.ReplaceStmt(Old, New)) { |
| 156 | ReplacedNodes[Old] = New; |
| 157 | return; |
| 158 | } |
| 159 | if (SilenceRewriteMacroWarning) |
| 160 | return; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 161 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 162 | << Old->getSourceRange(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 163 | } |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 164 | |
| 165 | void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { |
| 166 | // Measaure the old text. |
| 167 | int Size = Rewrite.getRangeSize(SrcRange); |
| 168 | if (Size == -1) { |
| 169 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 170 | << Old->getSourceRange(); |
| 171 | return; |
| 172 | } |
| 173 | // Get the new text. |
| 174 | std::string SStr; |
| 175 | llvm::raw_string_ostream S(SStr); |
| 176 | New->printPretty(S); |
| 177 | const std::string &Str = S.str(); |
| 178 | |
| 179 | // If replacement succeeded or warning disabled return with no warning. |
| 180 | if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, &Str[0], Str.size())) { |
| 181 | ReplacedNodes[Old] = New; |
| 182 | return; |
| 183 | } |
| 184 | if (SilenceRewriteMacroWarning) |
| 185 | return; |
| 186 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) |
| 187 | << Old->getSourceRange(); |
| 188 | } |
| 189 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 190 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen, |
| 191 | bool InsertAfter = true) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 192 | // If insertion succeeded or warning disabled return with no warning. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 193 | if (!Rewrite.InsertText(Loc, StrData, StrLen, InsertAfter) || |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 194 | SilenceRewriteMacroWarning) |
| 195 | return; |
| 196 | |
| 197 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 198 | } |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 199 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 200 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 201 | // If removal succeeded or warning disabled return with no warning. |
| 202 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 203 | return; |
| 204 | |
| 205 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 206 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 207 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 208 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 209 | const char *NewStr, unsigned NewLength) { |
| 210 | // If removal succeeded or warning disabled return with no warning. |
| 211 | if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) || |
| 212 | SilenceRewriteMacroWarning) |
| 213 | return; |
| 214 | |
| 215 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 216 | } |
| 217 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 218 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 219 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 220 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 221 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 222 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 223 | void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 224 | ObjCImplementationDecl *IMD, |
| 225 | ObjCCategoryImplDecl *CID); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 226 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 227 | void RewriteImplementationDecl(Decl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 228 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 229 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 230 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 231 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 232 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 233 | void RewriteProperty(ObjCPropertyDecl *prop); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 234 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 235 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 236 | void RewriteObjCQualifiedInterfaceTypes(Expr *E); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 237 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 238 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 239 | QualType getSuperStructType(); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 240 | QualType getConstantStringStructType(); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 241 | bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 242 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 243 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 244 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 245 | void CollectPropertySetters(Stmt *S); |
| 246 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 247 | Stmt *CurrentBody; |
| 248 | ParentMap *PropParentMap; // created lazily. |
| 249 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 250 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 251 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 252 | Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 253 | Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 254 | SourceRange SrcRange); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 255 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 256 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 257 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 258 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 259 | void WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 260 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 261 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 262 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 263 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 264 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 265 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 266 | SourceLocation OrigEnd); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 267 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 268 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 269 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 270 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 271 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 272 | void SynthCountByEnumWithState(std::string &buf); |
| 273 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 274 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 275 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 276 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 277 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 278 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 279 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 280 | void SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 281 | void SynthSelGetUidFunctionDecl(); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 282 | void SynthGetProtocolFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 283 | void SynthSuperContructorFunctionDecl(); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 284 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 285 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 286 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 287 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 289 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 290 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 292 | typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator; |
| 293 | void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 294 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 295 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 296 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 297 | const char *ClassName, |
| 298 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 300 | void RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> |
| 301 | &Protocols, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 302 | const char *prefix, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 303 | const char *ClassName, |
| 304 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 305 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 306 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 307 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 308 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 309 | std::string &Result); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 310 | void RewriteImplementations(); |
| 311 | void SynthesizeMetaDataIntoBuffer(std::string &Result); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 312 | |
| 313 | // Block rewriting. |
| 314 | void RewriteBlocksInFunctionTypeProto(QualType funcType, NamedDecl *D); |
| 315 | void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); |
| 316 | |
| 317 | void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); |
| 318 | void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); |
| 319 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 320 | // Block specific rewrite rules. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 321 | void RewriteBlockCall(CallExpr *Exp); |
| 322 | void RewriteBlockPointerDecl(NamedDecl *VD); |
| 323 | void RewriteBlockDeclRefExpr(BlockDeclRefExpr *VD); |
| 324 | void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); |
| 325 | |
| 326 | std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 327 | const char *funcName, std::string Tag); |
| 328 | std::string SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 329 | const char *funcName, std::string Tag); |
| 330 | std::string SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 331 | bool hasCopyDisposeHelpers); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 332 | Stmt *SynthesizeBlockCall(CallExpr *Exp); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 333 | void SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 334 | const char *FunName); |
| 335 | |
| 336 | void CollectBlockDeclRefInfo(BlockExpr *Exp); |
| 337 | void GetBlockCallExprs(Stmt *S); |
| 338 | void GetBlockDeclRefExprs(Stmt *S); |
| 339 | |
| 340 | // We avoid calling Type::isBlockPointerType(), since it operates on the |
| 341 | // canonical type. We only care if the top-level type is a closure pointer. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 342 | bool isTopLevelBlockPointerType(QualType T) { |
| 343 | return isa<BlockPointerType>(T); |
| 344 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 345 | |
| 346 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 347 | bool isObjCType(QualType T) { |
| 348 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 349 | return false; |
| 350 | |
| 351 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 352 | |
| 353 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 354 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 355 | return true; |
| 356 | |
| 357 | if (const PointerType *PT = OCT->getAsPointerType()) { |
| 358 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 359 | isa<ObjCQualifiedIdType>(PT->getPointeeType())) |
| 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 365 | void GetExtentOfArgList(const char *Name, const char *&LParen, |
| 366 | const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 367 | void RewriteCastExpr(CStyleCastExpr *CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 368 | |
| 369 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 370 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 371 | }; |
| 372 | } |
| 373 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 374 | void RewriteObjC::RewriteBlocksInFunctionTypeProto(QualType funcType, |
| 375 | NamedDecl *D) { |
| 376 | if (FunctionTypeProto *fproto = dyn_cast<FunctionTypeProto>(funcType)) { |
| 377 | for (FunctionTypeProto::arg_type_iterator I = fproto->arg_type_begin(), |
| 378 | E = fproto->arg_type_end(); I && (I != E); ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 379 | if (isTopLevelBlockPointerType(*I)) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 380 | // All the args are checked/rewritten. Don't call twice! |
| 381 | RewriteBlockPointerDecl(D); |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 388 | const PointerType *PT = funcType->getAsPointerType(); |
| 389 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 390 | RewriteBlocksInFunctionTypeProto(PT->getPointeeType(), ND); |
| 391 | } |
| 392 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 393 | static bool IsHeaderFile(const std::string &Filename) { |
| 394 | std::string::size_type DotPos = Filename.rfind('.'); |
| 395 | |
| 396 | if (DotPos == std::string::npos) { |
| 397 | // no file extension |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 402 | // C header: .h |
| 403 | // C++ header: .hh or .H; |
| 404 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 405 | } |
| 406 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 407 | RewriteObjC::RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 408 | Diagnostic &D, const LangOptions &LOpts) |
| 409 | : Diags(D), LangOpts(LOpts) { |
| 410 | IsHeader = IsHeaderFile(inFile); |
| 411 | InFileName = inFile; |
| 412 | OutFileName = outFile; |
| 413 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 414 | "rewriting sub-expression within a macro (may not be correct)"); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 415 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 416 | "rewriter doesn't support user-specified control flow semantics " |
| 417 | "for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 420 | ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 421 | const std::string& OutFile, |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 422 | Diagnostic &Diags, |
| 423 | const LangOptions &LOpts) { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 424 | return new RewriteObjC(InFile, OutFile, Diags, LOpts); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 425 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 426 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 427 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 428 | Context = &context; |
| 429 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 430 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 431 | MsgSendFunctionDecl = 0; |
| 432 | MsgSendSuperFunctionDecl = 0; |
| 433 | MsgSendStretFunctionDecl = 0; |
| 434 | MsgSendSuperStretFunctionDecl = 0; |
| 435 | MsgSendFpretFunctionDecl = 0; |
| 436 | GetClassFunctionDecl = 0; |
| 437 | GetMetaClassFunctionDecl = 0; |
| 438 | SelGetUidFunctionDecl = 0; |
| 439 | CFStringFunctionDecl = 0; |
| 440 | GetProtocolFunctionDecl = 0; |
| 441 | ConstantStringClassReference = 0; |
| 442 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 443 | CurMethodDef = 0; |
| 444 | CurFunctionDef = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 445 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 446 | SuperStructDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 447 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 448 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 449 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 450 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 451 | PropParentMap = 0; |
| 452 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 453 | DisableReplaceStmt = false; |
| 454 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 455 | // Get the ID and start/end of the main file. |
| 456 | MainFileID = SM->getMainFileID(); |
| 457 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 458 | MainFileStart = MainBuf->getBufferStart(); |
| 459 | MainFileEnd = MainBuf->getBufferEnd(); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 461 | Rewrite.setSourceMgr(Context->getSourceManager()); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 463 | // declaring objc_selector outside the parameter list removes a silly |
| 464 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 465 | if (IsHeader) |
Steve Naroff | 62c2632 | 2009-02-03 20:39:18 +0000 | [diff] [blame] | 466 | Preamble = "#pragma once\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 467 | Preamble += "struct objc_selector; struct objc_class;\n"; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 468 | Preamble += "struct __rw_objc_super { struct objc_object *object; "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 469 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 470 | if (LangOpts.Microsoft) { |
| 471 | // Add a constructor for creating temporary objects. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 472 | Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " |
| 473 | ": "; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 474 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 475 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 476 | Preamble += "};\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 477 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 478 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 479 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 480 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 481 | if (LangOpts.Microsoft) { |
| 482 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 483 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 484 | } else |
| 485 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 486 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 487 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 488 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 489 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 490 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 491 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 492 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 493 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 494 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 495 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 496 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 497 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 498 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 499 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 500 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; |
| 501 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 502 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 503 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 504 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 505 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 506 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 507 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 508 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 509 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 510 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 511 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 512 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 513 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 514 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 515 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 516 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 517 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 518 | Preamble += "#endif\n"; |
| 519 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 520 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 521 | Preamble += " int *isa;\n"; |
| 522 | Preamble += " int flags;\n"; |
| 523 | Preamble += " char *str;\n"; |
| 524 | Preamble += " long length;\n"; |
| 525 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 526 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 527 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 528 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 529 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 530 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 531 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 532 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 533 | // Blocks preamble. |
| 534 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 535 | Preamble += "#define BLOCK_IMPL\n"; |
| 536 | Preamble += "struct __block_impl {\n"; |
| 537 | Preamble += " void *isa;\n"; |
| 538 | Preamble += " int Flags;\n"; |
| 539 | Preamble += " int Size;\n"; |
| 540 | Preamble += " void *FuncPtr;\n"; |
| 541 | Preamble += "};\n"; |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 542 | Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; |
| 543 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_assign(void *, const void *, const int);\n"; |
| 544 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_object_dispose(const void *, const int);\n"; |
| 545 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteGlobalBlock[32];\n"; |
| 546 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteStackBlock[32];\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 547 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 548 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 549 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 550 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 551 | Preamble += "#define __attribute__(X)\n"; |
| 552 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 556 | //===----------------------------------------------------------------------===// |
| 557 | // Top Level Driver Code |
| 558 | //===----------------------------------------------------------------------===// |
| 559 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 560 | void RewriteObjC::HandleTopLevelDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 561 | // Two cases: either the decl could be in the main file, or it could be in a |
| 562 | // #included file. If the former, rewrite it now. If the later, check to see |
| 563 | // if we rewrote the #include/#import. |
| 564 | SourceLocation Loc = D->getLocation(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 565 | Loc = SM->getInstantiationLoc(Loc); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 566 | |
| 567 | // If this is for a builtin, ignore it. |
| 568 | if (Loc.isInvalid()) return; |
| 569 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 570 | // Look for built-in declarations that we need to refer during the rewrite. |
| 571 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 572 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 573 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 574 | // declared in <Foundation/NSString.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 575 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 576 | ConstantStringClassReference = FVD; |
| 577 | return; |
| 578 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 579 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 580 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 581 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 582 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 583 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 584 | RewriteProtocolDecl(PD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 585 | } else if (ObjCForwardProtocolDecl *FP = |
| 586 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 587 | RewriteForwardProtocolDecl(FP); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 588 | } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { |
| 589 | // Recurse into linkage specifications |
| 590 | for (DeclContext::decl_iterator DI = LSD->decls_begin(), |
| 591 | DIEnd = LSD->decls_end(); |
| 592 | DI != DIEnd; ++DI) |
| 593 | HandleTopLevelDecl(*DI); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 594 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 595 | // If we have a decl in the main file, see if we should rewrite it. |
Ted Kremenek | cf7e958 | 2008-04-14 21:24:13 +0000 | [diff] [blame] | 596 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 597 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 600 | //===----------------------------------------------------------------------===// |
| 601 | // Syntactic (non-AST) Rewriting Code |
| 602 | //===----------------------------------------------------------------------===// |
| 603 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 604 | void RewriteObjC::RewriteInclude() { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 605 | SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 606 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 607 | const char *MainBufStart = MainBuf.first; |
| 608 | const char *MainBufEnd = MainBuf.second; |
| 609 | size_t ImportLen = strlen("import"); |
| 610 | size_t IncludeLen = strlen("include"); |
| 611 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 612 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 613 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 614 | if (*BufPtr == '#') { |
| 615 | if (++BufPtr == MainBufEnd) |
| 616 | return; |
| 617 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 618 | if (++BufPtr == MainBufEnd) |
| 619 | return; |
| 620 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 621 | // replace import with include |
| 622 | SourceLocation ImportLoc = |
| 623 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 624 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 625 | BufPtr += ImportLen; |
| 626 | } |
| 627 | } |
| 628 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 631 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 632 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 633 | const char *MainBufStart = MainBuf.first; |
| 634 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 635 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 636 | // Loop over the whole file, looking for tabs. |
| 637 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 638 | if (*BufPtr != '\t') |
| 639 | continue; |
| 640 | |
| 641 | // Okay, we found a tab. This tab will turn into at least one character, |
| 642 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 643 | unsigned VCol = 0; |
| 644 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 645 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 646 | ++VCol; |
| 647 | |
| 648 | // Okay, now that we know the virtual column, we know how many spaces to |
| 649 | // insert. We assume 8-character tab-stops. |
| 650 | unsigned Spaces = 8-(VCol & 7); |
| 651 | |
| 652 | // Get the location of the tab. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 653 | SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); |
| 654 | TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 655 | |
| 656 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 657 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 658 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 661 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 662 | ObjCIvarDecl *OID) { |
| 663 | std::string S; |
| 664 | S = "((struct "; |
| 665 | S += ClassDecl->getIdentifier()->getName(); |
| 666 | S += "_IMPL *)self)->"; |
| 667 | S += OID->getNameAsCString(); |
| 668 | return S; |
| 669 | } |
| 670 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 671 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 672 | ObjCImplementationDecl *IMD, |
| 673 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 674 | SourceLocation startLoc = PID->getLocStart(); |
| 675 | InsertText(startLoc, "// ", 3); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 676 | const char *startBuf = SM->getCharacterData(startLoc); |
| 677 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 678 | const char *semiBuf = strchr(startBuf, ';'); |
| 679 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 680 | SourceLocation onePastSemiLoc = |
| 681 | startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 682 | |
| 683 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 684 | return; // FIXME: is this correct? |
| 685 | |
| 686 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 687 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 688 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 689 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 690 | |
| 691 | if (!OID) |
| 692 | return; |
| 693 | |
| 694 | std::string Getr; |
| 695 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 696 | Getr += "{ "; |
| 697 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 698 | // FIXME: deal with code generation implications for various property |
| 699 | // attributes (copy, retain, nonatomic). |
| 700 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 701 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 702 | Getr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 703 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 704 | |
| 705 | // Add the rewritten getter to trigger meta data generation. An alternate, and |
| 706 | // possibly cleaner approach is to hack RewriteObjCMethodsMetaData() to deal |
| 707 | // with properties explicitly. The following addInstanceMethod() required far |
| 708 | // less code change (and actually models what the rewriter is doing). |
| 709 | if (IMD) |
| 710 | IMD->addInstanceMethod(PD->getGetterMethodDecl()); |
| 711 | else |
| 712 | CID->addInstanceMethod(PD->getGetterMethodDecl()); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 713 | |
| 714 | if (PD->isReadOnly()) |
| 715 | return; |
| 716 | |
| 717 | // Generate the 'setter' function. |
| 718 | std::string Setr; |
| 719 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 720 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 721 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 722 | // FIXME: deal with code generation implications for various property |
| 723 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 724 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 725 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 726 | Setr += PD->getNameAsCString(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 727 | Setr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 728 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 729 | |
| 730 | // Add the rewritten setter to trigger meta data generation. |
| 731 | if (IMD) |
| 732 | IMD->addInstanceMethod(PD->getSetterMethodDecl()); |
| 733 | else |
| 734 | CID->addInstanceMethod(PD->getSetterMethodDecl()); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 735 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 736 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 737 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 738 | int numDecls = ClassDecl->getNumForwardDecls(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 739 | ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 740 | |
| 741 | // Get the start location and compute the semi location. |
| 742 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 743 | const char *startBuf = SM->getCharacterData(startLoc); |
| 744 | const char *semiPtr = strchr(startBuf, ';'); |
| 745 | |
| 746 | // Translate to typedef's that forward reference structs with the same name |
| 747 | // as the class. As a convenience, we include the original declaration |
| 748 | // as a comment. |
| 749 | std::string typedefString; |
| 750 | typedefString += "// "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 751 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 752 | typedefString += "\n"; |
| 753 | for (int i = 0; i < numDecls; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 754 | ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i]; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 755 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 756 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 757 | typedefString += "\n"; |
| 758 | typedefString += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 759 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 760 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 761 | typedefString += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 762 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 763 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 767 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 768 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 771 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 772 | SourceLocation LocStart = Method->getLocStart(); |
| 773 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 775 | if (SM->getInstantiationLineNumber(LocEnd) > |
| 776 | SM->getInstantiationLineNumber(LocStart)) { |
Steve Naroff | 94ac21e | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 777 | InsertText(LocStart, "#if 0\n", 6); |
| 778 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 779 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 780 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 784 | void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 785 | { |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 786 | SourceLocation Loc = prop->getLocation(); |
| 787 | |
| 788 | ReplaceText(Loc, 0, "// ", 3); |
| 789 | |
| 790 | // FIXME: handle properties that are declared across multiple lines. |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 793 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 794 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 795 | |
| 796 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 797 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 798 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 799 | for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 800 | E = CatDecl->instmeth_end(); I != E; ++I) |
| 801 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 802 | for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 803 | E = CatDecl->classmeth_end(); I != E; ++I) |
| 804 | RewriteMethodDeclaration(*I); |
| 805 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 806 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 807 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 810 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 811 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 812 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 813 | SourceLocation LocStart = PDecl->getLocStart(); |
| 814 | |
| 815 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 816 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 817 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 818 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 819 | E = PDecl->instmeth_end(); I != E; ++I) |
| 820 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 821 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 822 | E = PDecl->classmeth_end(); I != E; ++I) |
| 823 | RewriteMethodDeclaration(*I); |
| 824 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 825 | // Lastly, comment out the @end. |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 826 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 827 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 828 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 829 | // Must comment out @optional/@required |
| 830 | const char *startBuf = SM->getCharacterData(LocStart); |
| 831 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 832 | for (const char *p = startBuf; p < endBuf; p++) { |
| 833 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 834 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 835 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 836 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 837 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 838 | |
| 839 | } |
| 840 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 841 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 842 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 843 | ReplaceText(OptionalLoc, strlen("@required"), |
| 844 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 845 | |
| 846 | } |
| 847 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 850 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 851 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 852 | if (LocStart.isInvalid()) |
| 853 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 854 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 855 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 858 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 859 | std::string &ResultStr) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 860 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 861 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 862 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 863 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 864 | ResultStr += "id"; |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 865 | else if (OMD->getResultType()->isFunctionPointerType() || |
| 866 | OMD->getResultType()->isBlockPointerType()) { |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 867 | // needs special handling, since pointer-to-functions have special |
| 868 | // syntax (where a decaration models use). |
| 869 | QualType retType = OMD->getResultType(); |
Steve Naroff | f4312dc | 2008-12-11 19:29:16 +0000 | [diff] [blame] | 870 | QualType PointeeTy; |
| 871 | if (const PointerType* PT = retType->getAsPointerType()) |
| 872 | PointeeTy = PT->getPointeeType(); |
| 873 | else if (const BlockPointerType *BPT = retType->getAsBlockPointerType()) |
| 874 | PointeeTy = BPT->getPointeeType(); |
| 875 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 876 | ResultStr += FPRetType->getResultType().getAsString(); |
| 877 | ResultStr += "(*"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 878 | } |
| 879 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 880 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 881 | ResultStr += " "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 882 | |
| 883 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 884 | std::string NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 886 | if (OMD->isInstanceMethod()) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 887 | NameStr += "_I_"; |
| 888 | else |
| 889 | NameStr += "_C_"; |
| 890 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 891 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 892 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 893 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 894 | if (ObjCCategoryImplDecl *CID = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 895 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 896 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 897 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 898 | } |
Steve Naroff | 563b828 | 2008-04-04 22:23:44 +0000 | [diff] [blame] | 899 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 900 | { |
| 901 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 902 | int len = selString.size(); |
| 903 | for (int i = 0; i < len; i++) |
| 904 | if (selString[i] == ':') |
| 905 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 906 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 907 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 908 | // Remember this name for metadata emission |
| 909 | MethodInternalNames[OMD] = NameStr; |
| 910 | ResultStr += NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 911 | |
| 912 | // Rewrite arguments |
| 913 | ResultStr += "("; |
| 914 | |
| 915 | // invisible arguments |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 916 | if (OMD->isInstanceMethod()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 917 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 918 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 919 | if (!LangOpts.Microsoft) { |
| 920 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 921 | ResultStr += "struct "; |
| 922 | } |
| 923 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 924 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 925 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 926 | } |
| 927 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 928 | ResultStr += Context->getObjCIdType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 929 | |
| 930 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 931 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 932 | ResultStr += " _cmd"; |
| 933 | |
| 934 | // Method arguments. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 935 | for (unsigned i = 0; i < OMD->getNumParams(); i++) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 936 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
| 937 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 938 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 939 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 940 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 941 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 942 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 943 | if (isTopLevelBlockPointerType(PDecl->getType())) { |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 944 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 945 | const BlockPointerType *BPT = PDecl->getType()->getAsBlockPointerType(); |
| 946 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name); |
| 947 | } else |
| 948 | PDecl->getType().getAsStringInternal(Name); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 949 | ResultStr += Name; |
| 950 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 951 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 952 | if (OMD->isVariadic()) |
| 953 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 954 | ResultStr += ") "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 955 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 956 | if (FPRetType) { |
| 957 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 958 | |
| 959 | // Now, emit the argument types (if any). |
| 960 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(FPRetType)) { |
| 961 | ResultStr += "("; |
| 962 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 963 | if (i) ResultStr += ", "; |
| 964 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 965 | ResultStr += ParamStr; |
| 966 | } |
| 967 | if (FT->isVariadic()) { |
| 968 | if (FT->getNumArgs()) ResultStr += ", "; |
| 969 | ResultStr += "..."; |
| 970 | } |
| 971 | ResultStr += ")"; |
| 972 | } else { |
| 973 | ResultStr += "()"; |
| 974 | } |
| 975 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 976 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 977 | void RewriteObjC::RewriteImplementationDecl(Decl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 978 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 979 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 980 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 981 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 982 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 983 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 984 | InsertText(CID->getLocStart(), "// ", 3); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 985 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 986 | for (ObjCCategoryImplDecl::instmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 987 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 988 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 989 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 990 | ObjCMethodDecl *OMD = *I; |
| 991 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 992 | SourceLocation LocStart = OMD->getLocStart(); |
| 993 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 994 | |
| 995 | const char *startBuf = SM->getCharacterData(LocStart); |
| 996 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 997 | ReplaceText(LocStart, endBuf-startBuf, |
| 998 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1001 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1002 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1003 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1004 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1005 | ObjCMethodDecl *OMD = *I; |
| 1006 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1007 | SourceLocation LocStart = OMD->getLocStart(); |
| 1008 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 1009 | |
| 1010 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1011 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1012 | ReplaceText(LocStart, endBuf-startBuf, |
| 1013 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1014 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1015 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1016 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1017 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1018 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1021 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1022 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1023 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1024 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1027 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1028 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1029 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1030 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1031 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1032 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1033 | ResultStr += "\n"; |
| 1034 | ResultStr += "#define _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1035 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1036 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1037 | ResultStr += "typedef struct objc_object "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1038 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1039 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1040 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1041 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1042 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1043 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1044 | |
Steve Naroff | 6327e0d | 2009-01-11 01:06:09 +0000 | [diff] [blame] | 1045 | for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), |
| 1046 | E = ClassDecl->prop_end(); I != E; ++I) |
| 1047 | RewriteProperty(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1048 | for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1049 | E = ClassDecl->instmeth_end(); I != E; ++I) |
| 1050 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1051 | for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1052 | E = ClassDecl->classmeth_end(); I != E; ++I) |
| 1053 | RewriteMethodDeclaration(*I); |
| 1054 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1055 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1056 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1059 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1060 | SourceRange SrcRange) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1061 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1062 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1063 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1064 | ObjCMessageExpr *MsgExpr; |
| 1065 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1066 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1067 | ExprVec.push_back(newStmt); |
| 1068 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1069 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1070 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1071 | if (PRE && PropGetters[PRE]) { |
| 1072 | // This allows us to handle chain/nested property getters. |
| 1073 | Receiver = PropGetters[PRE]; |
| 1074 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1075 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1076 | PDecl->getSetterName(), PDecl->getType(), |
| 1077 | PDecl->getSetterMethodDecl(), |
| 1078 | SourceLocation(), SourceLocation(), |
| 1079 | &ExprVec[0], 1); |
| 1080 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
| 1081 | |
| 1082 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1083 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1084 | //delete BinOp; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1085 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1086 | // to things that stay around. |
| 1087 | Context->Deallocate(MsgExpr); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1088 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1091 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1092 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1093 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1094 | ObjCMessageExpr *MsgExpr; |
| 1095 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1096 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1097 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1098 | |
| 1099 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1100 | if (PRE && PropGetters[PRE]) { |
| 1101 | // This allows us to handle chain/nested property getters. |
| 1102 | Receiver = PropGetters[PRE]; |
| 1103 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1104 | MsgExpr = new (Context) ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1105 | PDecl->getGetterName(), PDecl->getType(), |
| 1106 | PDecl->getGetterMethodDecl(), |
| 1107 | SourceLocation(), SourceLocation(), |
| 1108 | 0, 0); |
| 1109 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1110 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (!PropParentMap) |
| 1113 | PropParentMap = new ParentMap(CurrentBody); |
| 1114 | |
| 1115 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1116 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1117 | // We stash away the ReplacingStmt since actually doing the |
| 1118 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1119 | PropGetters[PropRefExpr] = ReplacingStmt; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1120 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1121 | // to things that stay around. |
| 1122 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1123 | return PropRefExpr; // return the original... |
| 1124 | } else { |
| 1125 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1126 | // delete PropRefExpr; elsewhere... |
| 1127 | // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references |
| 1128 | // to things that stay around. |
| 1129 | Context->Deallocate(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1130 | return ReplacingStmt; |
| 1131 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1134 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
| 1135 | SourceLocation OrigStart) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1136 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1137 | if (CurMethodDef) { |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1138 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1139 | ObjCInterfaceType *iFaceDecl = |
| 1140 | dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1141 | // lookup which class implements the instance variable. |
| 1142 | ObjCInterfaceDecl *clsDeclared = 0; |
| 1143 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
| 1144 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1145 | |
| 1146 | // Synthesize an explicit cast to gain access to the ivar. |
| 1147 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1148 | RecName += "_IMPL"; |
| 1149 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1150 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1151 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1152 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1153 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1154 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), |
| 1155 | castT,SourceLocation(), |
| 1156 | SourceLocation()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1157 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1158 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
| 1159 | IV->getBase()->getLocEnd(), |
| 1160 | castExpr); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1161 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1162 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1163 | MemberExpr *ME = new (Context) MemberExpr(PE, true, D, |
| 1164 | IV->getLocation(), |
| 1165 | D->getType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1166 | ReplaceStmt(IV, ME); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1167 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1168 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1169 | } |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1170 | |
| 1171 | ReplaceStmt(IV->getBase(), PE); |
| 1172 | // Cannot delete IV->getBase(), since PE points to it. |
| 1173 | // Replace the old base with the cast. This is important when doing |
| 1174 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1175 | IV->setBase(PE); |
| 1176 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1177 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1178 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1179 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 1180 | |
| 1181 | // Explicit ivar refs need to have a cast inserted. |
| 1182 | // FIXME: consider sharing some of this code with the code above. |
| 1183 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1184 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1185 | // lookup which class implements the instance variable. |
| 1186 | ObjCInterfaceDecl *clsDeclared = 0; |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1187 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1188 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1189 | |
| 1190 | // Synthesize an explicit cast to gain access to the ivar. |
| 1191 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1192 | RecName += "_IMPL"; |
| 1193 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1194 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1195 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1196 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1197 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1198 | CastExpr *castExpr = new (Context) CStyleCastExpr(castT, IV->getBase(), |
| 1199 | castT, SourceLocation(), |
| 1200 | SourceLocation()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1201 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1202 | ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1203 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1204 | ReplaceStmt(IV->getBase(), PE); |
| 1205 | // Cannot delete IV->getBase(), since PE points to it. |
| 1206 | // Replace the old base with the cast. This is important when doing |
| 1207 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1208 | IV->setBase(PE); |
| 1209 | return IV; |
| 1210 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1211 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1212 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1215 | /// SynthCountByEnumWithState - To print: |
| 1216 | /// ((unsigned int (*) |
| 1217 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1218 | /// (void *)objc_msgSend)((id)l_collection, |
| 1219 | /// sel_registerName( |
| 1220 | /// "countByEnumeratingWithState:objects:count:"), |
| 1221 | /// &enumState, |
| 1222 | /// (id *)items, (unsigned int)16) |
| 1223 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1224 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1225 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1226 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1227 | buf += "\n\t\t"; |
| 1228 | buf += "((id)l_collection,\n\t\t"; |
| 1229 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1230 | buf += "\n\t\t"; |
| 1231 | buf += "&enumState, " |
| 1232 | "(id *)items, (unsigned int)16)"; |
| 1233 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1234 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1235 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1236 | /// statement to exit to its outer synthesized loop. |
| 1237 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1238 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1239 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1240 | return S; |
| 1241 | // replace break with goto __break_label |
| 1242 | std::string buf; |
| 1243 | |
| 1244 | SourceLocation startLoc = S->getLocStart(); |
| 1245 | buf = "goto __break_label_"; |
| 1246 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1247 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1253 | /// statement to continue with its inner synthesized loop. |
| 1254 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1255 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1256 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1257 | return S; |
| 1258 | // replace continue with goto __continue_label |
| 1259 | std::string buf; |
| 1260 | |
| 1261 | SourceLocation startLoc = S->getLocStart(); |
| 1262 | buf = "goto __continue_label_"; |
| 1263 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1264 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1269 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1270 | /// It rewrites: |
| 1271 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 1272 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1273 | /// Into: |
| 1274 | /// { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1275 | /// type elem; |
| 1276 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1277 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1278 | /// id l_collection = (id)collection; |
| 1279 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1280 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1281 | /// if (limit) { |
| 1282 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1283 | /// do { |
| 1284 | /// unsigned long counter = 0; |
| 1285 | /// do { |
| 1286 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1287 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1288 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1289 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1290 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1291 | /// } while (counter < limit); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1292 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1293 | /// objects:items count:16]); |
| 1294 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1295 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1296 | /// } |
| 1297 | /// else |
| 1298 | /// elem = nil; |
| 1299 | /// } |
| 1300 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1301 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1302 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1303 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1304 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1305 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1306 | assert(!ObjCBcLabelNo.empty() && |
| 1307 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1308 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1309 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1310 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1311 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1312 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1313 | std::string buf; |
| 1314 | buf = "\n{\n\t"; |
| 1315 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1316 | // type elem; |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1317 | NamedDecl* D = cast<NamedDecl>(DS->getSolitaryDecl()); |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1318 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1319 | elementTypeAsString = ElementType.getAsString(); |
| 1320 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1321 | buf += " "; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1322 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1323 | buf += elementName; |
| 1324 | buf += ";\n\t"; |
| 1325 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1326 | else { |
| 1327 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1328 | elementName = DR->getDecl()->getNameAsCString(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1329 | elementTypeAsString |
| 1330 | = cast<ValueDecl>(DR->getDecl())->getType().getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1331 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1332 | |
| 1333 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1334 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1335 | // id items[16]; |
| 1336 | buf += "id items[16];\n\t"; |
| 1337 | // id l_collection = (id) |
| 1338 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1339 | // Find start location of 'collection' the hard way! |
| 1340 | const char *startCollectionBuf = startBuf; |
| 1341 | startCollectionBuf += 3; // skip 'for' |
| 1342 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1343 | startCollectionBuf++; // skip '(' |
| 1344 | // find 'in' and skip it. |
| 1345 | while (*startCollectionBuf != ' ' || |
| 1346 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1347 | (*(startCollectionBuf+3) != ' ' && |
| 1348 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1349 | startCollectionBuf++; |
| 1350 | startCollectionBuf += 3; |
| 1351 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1352 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1353 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1354 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1355 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1356 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1357 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1358 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1359 | buf = ";\n\t"; |
| 1360 | |
| 1361 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1362 | // objects:items count:16]; |
| 1363 | // which is synthesized into: |
| 1364 | // unsigned int limit = |
| 1365 | // ((unsigned int (*) |
| 1366 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1367 | // (void *)objc_msgSend)((id)l_collection, |
| 1368 | // sel_registerName( |
| 1369 | // "countByEnumeratingWithState:objects:count:"), |
| 1370 | // (struct __objcFastEnumerationState *)&state, |
| 1371 | // (id *)items, (unsigned int)16); |
| 1372 | buf += "unsigned long limit =\n\t\t"; |
| 1373 | SynthCountByEnumWithState(buf); |
| 1374 | buf += ";\n\t"; |
| 1375 | /// if (limit) { |
| 1376 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1377 | /// do { |
| 1378 | /// unsigned long counter = 0; |
| 1379 | /// do { |
| 1380 | /// if (startMutations != *enumState.mutationsPtr) |
| 1381 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1382 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1383 | buf += "if (limit) {\n\t"; |
| 1384 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1385 | buf += "do {\n\t\t"; |
| 1386 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1387 | buf += "do {\n\t\t\t"; |
| 1388 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1389 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1390 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1391 | buf += " = ("; |
| 1392 | buf += elementTypeAsString; |
| 1393 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1394 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1395 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1396 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1397 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1398 | /// } while (counter < limit); |
| 1399 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1400 | /// objects:items count:16]); |
| 1401 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1402 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1403 | /// } |
| 1404 | /// else |
| 1405 | /// elem = nil; |
| 1406 | /// } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1407 | /// |
| 1408 | buf = ";\n\t"; |
| 1409 | buf += "__continue_label_"; |
| 1410 | buf += utostr(ObjCBcLabelNo.back()); |
| 1411 | buf += ": ;"; |
| 1412 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1413 | buf += "} while (counter < limit);\n\t"; |
| 1414 | buf += "} while (limit = "; |
| 1415 | SynthCountByEnumWithState(buf); |
| 1416 | buf += ");\n\t"; |
| 1417 | buf += elementName; |
Steve Naroff | 5605fdf | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1418 | buf += " = ((id)0);\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1419 | buf += "__break_label_"; |
| 1420 | buf += utostr(ObjCBcLabelNo.back()); |
| 1421 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1422 | buf += "}\n\t"; |
| 1423 | buf += "else\n\t\t"; |
| 1424 | buf += elementName; |
Steve Naroff | 5605fdf | 2008-12-17 14:24:39 +0000 | [diff] [blame] | 1425 | buf += " = ((id)0);\n"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1426 | buf += "}\n"; |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1427 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1428 | // Insert all these *after* the statement body. |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1429 | if (isa<CompoundStmt>(S->getBody())) { |
| 1430 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1431 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1432 | } else { |
| 1433 | /* Need to treat single statements specially. For example: |
| 1434 | * |
| 1435 | * for (A *a in b) if (stuff()) break; |
| 1436 | * for (A *a in b) xxxyy; |
| 1437 | * |
| 1438 | * The following code simply scans ahead to the semi to find the actual end. |
| 1439 | */ |
| 1440 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1441 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1442 | assert(semiBuf && "Can't find ';'"); |
| 1443 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1444 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1445 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1446 | Stmts.pop_back(); |
| 1447 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1448 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1451 | /// RewriteObjCSynchronizedStmt - |
| 1452 | /// This routine rewrites @synchronized(expr) stmt; |
| 1453 | /// into: |
| 1454 | /// objc_sync_enter(expr); |
| 1455 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1456 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1457 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1458 | // Get the start location and compute the semi location. |
| 1459 | SourceLocation startLoc = S->getLocStart(); |
| 1460 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1461 | |
| 1462 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1463 | |
| 1464 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1465 | buf = "objc_sync_enter((id)"; |
| 1466 | const char *lparenBuf = startBuf; |
| 1467 | while (*lparenBuf != '(') lparenBuf++; |
| 1468 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1469 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1470 | // the sync expression is typically a message expression that's already |
| 1471 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1472 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1473 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1474 | while (*endBuf != ')') endBuf--; |
| 1475 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1476 | buf = ");\n"; |
| 1477 | // declare a new scope with two variables, _stack and _rethrow. |
| 1478 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1479 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1480 | buf += "char *pointers[4];} _stack;\n"; |
| 1481 | buf += "id volatile _rethrow = 0;\n"; |
| 1482 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1483 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1484 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1485 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1486 | startBuf = SM->getCharacterData(startLoc); |
| 1487 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1488 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1489 | SourceLocation lastCurlyLoc = startLoc; |
| 1490 | buf = "}\nelse {\n"; |
| 1491 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1492 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1493 | buf += " objc_sync_exit("; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1494 | Expr *syncExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 1495 | S->getSynchExpr(), |
| 1496 | Context->getObjCIdType(), |
| 1497 | SourceLocation(), |
| 1498 | SourceLocation()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1499 | std::string syncExprBufS; |
| 1500 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1501 | syncExpr->printPretty(syncExprBuf); |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1502 | buf += syncExprBuf.str(); |
| 1503 | buf += ");\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1504 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1505 | buf += "}\n"; |
| 1506 | buf += "}"; |
| 1507 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1508 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1509 | return 0; |
| 1510 | } |
| 1511 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1512 | void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) { |
| 1513 | // Perform a bottom up traversal of all children. |
| 1514 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1515 | CI != E; ++CI) |
| 1516 | if (*CI) |
| 1517 | WarnAboutReturnGotoContinueOrBreakStmts(*CI); |
| 1518 | |
| 1519 | if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || |
| 1520 | isa<BreakStmt>(S) || isa<GotoStmt>(S)) { |
| 1521 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1522 | TryFinallyContainsReturnDiag); |
| 1523 | } |
| 1524 | return; |
| 1525 | } |
| 1526 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1527 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1528 | // Get the start location and compute the semi location. |
| 1529 | SourceLocation startLoc = S->getLocStart(); |
| 1530 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1531 | |
| 1532 | assert((*startBuf == '@') && "bogus @try location"); |
| 1533 | |
| 1534 | std::string buf; |
| 1535 | // declare a new scope with two variables, _stack and _rethrow. |
| 1536 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1537 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1538 | buf += "char *pointers[4];} _stack;\n"; |
| 1539 | buf += "id volatile _rethrow = 0;\n"; |
| 1540 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1541 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1543 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1544 | |
| 1545 | startLoc = S->getTryBody()->getLocEnd(); |
| 1546 | startBuf = SM->getCharacterData(startLoc); |
| 1547 | |
| 1548 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1549 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1550 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1551 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1552 | if (catchList) { |
| 1553 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1554 | buf = " /* @catch begin */ else {\n"; |
| 1555 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1556 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1557 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1558 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1559 | buf += " else { /* @catch continue */"; |
| 1560 | |
| 1561 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1562 | } else { /* no catch list */ |
| 1563 | buf = "}\nelse {\n"; |
| 1564 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1565 | buf += "}"; |
| 1566 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1567 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1568 | bool sawIdTypedCatch = false; |
| 1569 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1570 | while (catchList) { |
| 1571 | Stmt *catchStmt = catchList->getCatchParamStmt(); |
| 1572 | |
| 1573 | if (catchList == S->getCatchStmts()) |
| 1574 | buf = "if ("; // we are generating code for the first catch clause |
| 1575 | else |
| 1576 | buf = "else if ("; |
| 1577 | startLoc = catchList->getLocStart(); |
| 1578 | startBuf = SM->getCharacterData(startLoc); |
| 1579 | |
| 1580 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1581 | |
| 1582 | const char *lParenLoc = strchr(startBuf, '('); |
| 1583 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1584 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1585 | // Now rewrite the body... |
| 1586 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1587 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1588 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1589 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1590 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1591 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1592 | |
| 1593 | buf += "1) { id _tmp = _caught;"; |
| 1594 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1595 | buf.c_str(), buf.size()); |
| 1596 | } else if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) { |
Ted Kremenek | 50a25e2 | 2008-10-06 22:39:38 +0000 | [diff] [blame] | 1597 | QualType t = dyn_cast<ValueDecl>(declStmt->getSolitaryDecl())->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1598 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1599 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1600 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1601 | sawIdTypedCatch = true; |
| 1602 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1603 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1604 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1605 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1606 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1607 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1608 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1609 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1610 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | // Now rewrite the body... |
| 1614 | lastCatchBody = catchList->getCatchBody(); |
| 1615 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1616 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1617 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1618 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1619 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1620 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1621 | |
| 1622 | buf = " = _caught;"; |
| 1623 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1624 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1625 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1626 | } else if (!isa<NullStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1627 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1628 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1629 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1630 | catchList = catchList->getNextCatchStmt(); |
| 1631 | } |
| 1632 | // Complete the catch list... |
| 1633 | if (lastCatchBody) { |
| 1634 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1635 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1636 | "bogus @catch body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1637 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1638 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1639 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1640 | buf = "} /* last catch end */\n"; |
| 1641 | buf += "else {\n"; |
| 1642 | buf += " _rethrow = _caught;\n"; |
| 1643 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1644 | buf += "} } /* @catch end */\n"; |
| 1645 | if (!S->getFinallyStmt()) |
| 1646 | buf += "}\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1647 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1648 | |
| 1649 | // Set lastCurlyLoc |
| 1650 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1651 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1652 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1653 | startLoc = finalStmt->getLocStart(); |
| 1654 | startBuf = SM->getCharacterData(startLoc); |
| 1655 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1656 | |
| 1657 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1658 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1659 | |
| 1660 | Stmt *body = finalStmt->getFinallyBody(); |
| 1661 | SourceLocation startLoc = body->getLocStart(); |
| 1662 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1663 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1664 | "bogus @finally body location"); |
| 1665 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1666 | "bogus @finally body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1667 | |
| 1668 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1669 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1670 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1671 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1672 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1673 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1674 | |
| 1675 | // Set lastCurlyLoc |
| 1676 | lastCurlyLoc = body->getLocEnd(); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Now check for any return/continue/go statements within the @try. |
| 1679 | WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1680 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1681 | buf = "{ /* implicit finally clause */\n"; |
| 1682 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1683 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1684 | buf += "}"; |
| 1685 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1686 | } |
| 1687 | // Now emit the final closing curly brace... |
| 1688 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1689 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1690 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1694 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1695 | return 0; |
| 1696 | } |
| 1697 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1698 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1699 | return 0; |
| 1700 | } |
| 1701 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1702 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1703 | // the throw expression is typically a message expression that's already |
| 1704 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1705 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1706 | // Get the start location and compute the semi location. |
| 1707 | SourceLocation startLoc = S->getLocStart(); |
| 1708 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1709 | |
| 1710 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1711 | |
| 1712 | std::string buf; |
| 1713 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1714 | if (S->getThrowExpr()) |
| 1715 | buf = "objc_exception_throw("; |
| 1716 | else // add an implicit argument |
| 1717 | buf = "objc_exception_throw(_caught"; |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1718 | |
| 1719 | // handle "@ throw" correctly. |
| 1720 | const char *wBuf = strchr(startBuf, 'w'); |
| 1721 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1722 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
| 1723 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1724 | const char *semiBuf = strchr(startBuf, ';'); |
| 1725 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1726 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1727 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1728 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1729 | return 0; |
| 1730 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1731 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1732 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1733 | // Create a new string expression. |
| 1734 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1735 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1736 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1737 | Expr *Replacement = new (Context) StringLiteral(*Context,StrEncoding.c_str(), |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1738 | StrEncoding.length(), false, StrType, |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame^] | 1739 | SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1740 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1742 | // Replace this subexpr in the parent. |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1743 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1744 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1747 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | 1a93764 | 2008-12-22 22:16:07 +0000 | [diff] [blame] | 1748 | if (!SelGetUidFunctionDecl) |
| 1749 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1750 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1751 | // Create a call to sel_registerName("selName"). |
| 1752 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1753 | QualType argType = Context->getPointerType(Context->CharTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1754 | SelExprs.push_back(new (Context) StringLiteral((*Context), |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 1755 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1756 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame^] | 1757 | false, argType, SourceLocation())); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1758 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1759 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1760 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1761 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1762 | return SelExp; |
| 1763 | } |
| 1764 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1765 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1766 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1767 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1768 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1769 | |
| 1770 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1771 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Now, we cast the reference to a pointer to the objc_msgSend type. |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 1774 | QualType pToFunc = Context->getPointerType(msgSendType); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1775 | ImplicitCastExpr *ICE = new (Context) ImplicitCastExpr(pToFunc, DRE, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1776 | /*isLvalue=*/false); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1777 | |
| 1778 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1779 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1780 | return new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), |
| 1781 | SourceLocation()); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1784 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1785 | const char *&startRef, const char *&endRef) { |
| 1786 | while (startBuf < endBuf) { |
| 1787 | if (*startBuf == '<') |
| 1788 | startRef = startBuf; // mark the start. |
| 1789 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1790 | if (startRef && *startRef == '<') { |
| 1791 | endRef = startBuf; // mark the end. |
| 1792 | return true; |
| 1793 | } |
| 1794 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1795 | } |
| 1796 | startBuf++; |
| 1797 | } |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1801 | static void scanToNextArgument(const char *&argRef) { |
| 1802 | int angle = 0; |
| 1803 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1804 | if (*argRef == '<') |
| 1805 | angle++; |
| 1806 | else if (*argRef == '>') |
| 1807 | angle--; |
| 1808 | argRef++; |
| 1809 | } |
| 1810 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1811 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1812 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1813 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1814 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1815 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1816 | return true; |
| 1817 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1818 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1819 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1820 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1821 | return true; // we have "Class <Protocol> *". |
| 1822 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1823 | return false; |
| 1824 | } |
| 1825 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1826 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1827 | QualType Type = E->getType(); |
| 1828 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1829 | SourceLocation Loc, EndLoc; |
| 1830 | |
| 1831 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1832 | Loc = ECE->getLParenLoc(); |
| 1833 | EndLoc = ECE->getRParenLoc(); |
| 1834 | } else { |
| 1835 | Loc = E->getLocStart(); |
| 1836 | EndLoc = E->getLocEnd(); |
| 1837 | } |
| 1838 | // This will defend against trying to rewrite synthesized expressions. |
| 1839 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1840 | return; |
| 1841 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1842 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1843 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1844 | const char *startRef = 0, *endRef = 0; |
| 1845 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1846 | // Get the locations of the startRef, endRef. |
| 1847 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1848 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1849 | // Comment out the protocol references. |
| 1850 | InsertText(LessLoc, "/*", 2); |
| 1851 | InsertText(GreaterLoc, "*/", 2); |
| 1852 | } |
| 1853 | } |
| 1854 | } |
| 1855 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1856 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1857 | SourceLocation Loc; |
| 1858 | QualType Type; |
| 1859 | const FunctionTypeProto *proto = 0; |
| 1860 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1861 | Loc = VD->getLocation(); |
| 1862 | Type = VD->getType(); |
| 1863 | } |
| 1864 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1865 | Loc = FD->getLocation(); |
| 1866 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1867 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1868 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1869 | assert(funcType && "missing function type"); |
| 1870 | proto = dyn_cast<FunctionTypeProto>(funcType); |
| 1871 | if (!proto) |
| 1872 | return; |
| 1873 | Type = proto->getResultType(); |
| 1874 | } |
| 1875 | else |
| 1876 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1877 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1878 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1879 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1880 | |
| 1881 | const char *endBuf = SM->getCharacterData(Loc); |
| 1882 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1883 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1884 | startBuf--; // scan backward (from the decl location) for return type. |
| 1885 | const char *startRef = 0, *endRef = 0; |
| 1886 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1887 | // Get the locations of the startRef, endRef. |
| 1888 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1889 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1890 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1891 | InsertText(LessLoc, "/*", 2); |
| 1892 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1895 | if (!proto) |
| 1896 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1897 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1898 | const char *startBuf = SM->getCharacterData(Loc); |
| 1899 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1900 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1901 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1902 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1903 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1904 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1905 | // scan forward (from the decl location) for argument types. |
| 1906 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1907 | const char *startRef = 0, *endRef = 0; |
| 1908 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1909 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1910 | SourceLocation LessLoc = |
| 1911 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1912 | SourceLocation GreaterLoc = |
| 1913 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1914 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1915 | InsertText(LessLoc, "/*", 2); |
| 1916 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1917 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1918 | startBuf = ++endBuf; |
| 1919 | } |
| 1920 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1921 | // If the function name is derived from a macro expansion, then the |
| 1922 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1923 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1924 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1925 | startBuf++; |
| 1926 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1927 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1930 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1931 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1932 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1933 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1934 | ArgTys.push_back(Context->getPointerType( |
| 1935 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1936 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1937 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1938 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1939 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1940 | SourceLocation(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1941 | SelGetUidIdent, getFuncType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1942 | FunctionDecl::Extern, false); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1945 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1946 | void RewriteObjC::SynthGetProtocolFunctionDecl() { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1947 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1948 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1949 | ArgTys.push_back(Context->getPointerType( |
| 1950 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1951 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1952 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1953 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1954 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1955 | SourceLocation(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1956 | SelGetProtoIdent, getFuncType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1957 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1960 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1961 | // declared in <objc/objc.h> |
Douglas Gregor | 51efe56 | 2009-01-09 01:47:02 +0000 | [diff] [blame] | 1962 | if (FD->getIdentifier() && |
| 1963 | strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1964 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1965 | return; |
| 1966 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1967 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1970 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1971 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1972 | if (SuperContructorFunctionDecl) |
| 1973 | return; |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 1974 | IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1975 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1976 | QualType argT = Context->getObjCIdType(); |
| 1977 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1978 | ArgTys.push_back(argT); |
| 1979 | ArgTys.push_back(argT); |
| 1980 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1981 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1982 | false, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1983 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1984 | SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1985 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1986 | FunctionDecl::Extern, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1989 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1990 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1991 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1992 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1993 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1994 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1995 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1996 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1997 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1998 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1999 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2000 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2001 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2002 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2003 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2004 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2005 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2008 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2009 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2010 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 2011 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2012 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2013 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2014 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2015 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2016 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2017 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2018 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2019 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2020 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2021 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2022 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2023 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2024 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2025 | SourceLocation(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2026 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2027 | FunctionDecl::Extern, false); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2030 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2031 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2032 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2033 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2034 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2035 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2036 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2037 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2038 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2039 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2040 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2041 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2042 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2043 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2044 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2045 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2046 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | // SynthMsgSendSuperStretFunctionDecl - |
| 2050 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2051 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2052 | IdentifierInfo *msgSendIdent = |
| 2053 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2054 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2055 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2056 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2057 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2058 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2059 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2060 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2061 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2062 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2063 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2064 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2065 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2066 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2067 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 2068 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2069 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2070 | FunctionDecl::Extern, false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2073 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2074 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2075 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2076 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2077 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2078 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2079 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2080 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2081 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2082 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2083 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2084 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2085 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2086 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2087 | SourceLocation(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2088 | msgSendIdent, msgSendType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2089 | FunctionDecl::Extern, false); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2092 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2093 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2094 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2095 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2096 | ArgTys.push_back(Context->getPointerType( |
| 2097 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2098 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2099 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2100 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2101 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2102 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2103 | getClassIdent, getClassType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2104 | FunctionDecl::Extern, false); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2107 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2108 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2109 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2110 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2111 | ArgTys.push_back(Context->getPointerType( |
| 2112 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2113 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2114 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2115 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2116 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2117 | SourceLocation(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2118 | getClassIdent, getClassType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2119 | FunctionDecl::Extern, false); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2122 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2123 | QualType strType = getConstantStringStructType(); |
| 2124 | |
| 2125 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2126 | |
| 2127 | std::string tmpName = InFileName; |
| 2128 | unsigned i; |
| 2129 | for (i=0; i < tmpName.length(); i++) { |
| 2130 | char c = tmpName.at(i); |
| 2131 | // replace any non alphanumeric characters with '_'. |
| 2132 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2133 | tmpName[i] = '_'; |
| 2134 | } |
| 2135 | S += tmpName; |
| 2136 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2137 | S += utostr(NumObjCStringLiterals++); |
| 2138 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2139 | Preamble += "static __NSConstantStringImpl " + S; |
| 2140 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2141 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2142 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2143 | std::string prettyBufS; |
| 2144 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2145 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2146 | Preamble += prettyBuf.str(); |
| 2147 | Preamble += ","; |
Steve Naroff | 3652c2d | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2148 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2149 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2150 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2151 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2152 | &Context->Idents.get(S.c_str()), strType, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2153 | VarDecl::Static); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2154 | DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2155 | Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2156 | Context->getPointerType(DRE->getType()), |
| 2157 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2158 | // cast to NSConstantString * |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2159 | CastExpr *cast = new (Context) CStyleCastExpr(Exp->getType(), Unop, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2160 | Exp->getType(), SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2161 | ReplaceStmt(Exp, cast); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2162 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2163 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2166 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2167 | // check if we are sending a message to 'super' |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2168 | if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return 0; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2170 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
| 2171 | const PointerType *PT = Super->getType()->getAsPointerType(); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2172 | assert(PT); |
| 2173 | ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 2174 | return IT->getDecl(); |
| 2175 | } |
| 2176 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2180 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2181 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2182 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2183 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2184 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2185 | QualType FieldTypes[2]; |
| 2186 | |
| 2187 | // struct objc_object *receiver; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2188 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2189 | // struct objc_class *super; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2190 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2191 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2192 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2193 | for (unsigned i = 0; i < 2; ++i) { |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 2194 | SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2195 | SourceLocation(), 0, |
| 2196 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2197 | /*Mutable=*/false)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2198 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2200 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2201 | } |
| 2202 | return Context->getTagDeclType(SuperStructDecl); |
| 2203 | } |
| 2204 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2205 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2206 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2207 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2208 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2209 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2210 | QualType FieldTypes[4]; |
| 2211 | |
| 2212 | // struct objc_object *receiver; |
| 2213 | FieldTypes[0] = Context->getObjCIdType(); |
| 2214 | // int flags; |
| 2215 | FieldTypes[1] = Context->IntTy; |
| 2216 | // char *str; |
| 2217 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2218 | // long length; |
| 2219 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2220 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2221 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2222 | for (unsigned i = 0; i < 4; ++i) { |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 2223 | ConstantStringDecl->addDecl(FieldDecl::Create(*Context, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2224 | ConstantStringDecl, |
| 2225 | SourceLocation(), 0, |
| 2226 | FieldTypes[i], |
| 2227 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2228 | /*Mutable=*/true)); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2232 | } |
| 2233 | return Context->getTagDeclType(ConstantStringDecl); |
| 2234 | } |
| 2235 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2236 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2237 | if (!SelGetUidFunctionDecl) |
| 2238 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2239 | if (!MsgSendFunctionDecl) |
| 2240 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2241 | if (!MsgSendSuperFunctionDecl) |
| 2242 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2243 | if (!MsgSendStretFunctionDecl) |
| 2244 | SynthMsgSendStretFunctionDecl(); |
| 2245 | if (!MsgSendSuperStretFunctionDecl) |
| 2246 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2247 | if (!MsgSendFpretFunctionDecl) |
| 2248 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2249 | if (!GetClassFunctionDecl) |
| 2250 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2251 | if (!GetMetaClassFunctionDecl) |
| 2252 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2253 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2254 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2255 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2256 | // May need to use objc_msgSend_stret() as well. |
| 2257 | FunctionDecl *MsgSendStretFlavor = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2258 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2259 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2260 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2261 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2262 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2263 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2264 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2265 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2266 | // Synthesize a call to objc_msgSend(). |
| 2267 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2268 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2269 | |
| 2270 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2271 | if (clsName) { // class message. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2272 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2273 | // the 'super' idiom within a class method. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2274 | if (!strcmp(clsName->getName(), "super")) { |
| 2275 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2276 | if (MsgSendStretFlavor) |
| 2277 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2278 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2279 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2280 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2281 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2282 | |
| 2283 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2284 | |
| 2285 | // set the receiver to self, the first argument to all methods. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2286 | InitExprs.push_back(new (Context) DeclRefExpr( |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2287 | CurMethodDef->getSelfDecl(), |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2288 | Context->getObjCIdType(), |
| 2289 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2290 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2291 | QualType argType = Context->getPointerType(Context->CharTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2292 | ClsExprs.push_back(new (Context) StringLiteral(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2293 | SuperDecl->getIdentifier()->getName(), |
| 2294 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame^] | 2295 | false, argType, SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2296 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2297 | &ClsExprs[0], |
| 2298 | ClsExprs.size()); |
| 2299 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2300 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2301 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2302 | Cls, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2303 | SourceLocation(), SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2304 | // struct objc_super |
| 2305 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2306 | Expr *SuperRep; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2307 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2308 | if (LangOpts.Microsoft) { |
| 2309 | SynthSuperContructorFunctionDecl(); |
| 2310 | // Simulate a contructor call... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2311 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2312 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2313 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2314 | InitExprs.size(), |
| 2315 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2316 | // The code for super is a little tricky to prevent collision with |
| 2317 | // the structure definition in the header. The rewriter has it's own |
| 2318 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2319 | // we need the cast below. For example: |
| 2320 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2321 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2322 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2323 | Context->getPointerType(SuperRep->getType()), |
| 2324 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2325 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2326 | SuperRep, Context->getPointerType(superType), |
| 2327 | SourceLocation(), SourceLocation()); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2328 | } else { |
| 2329 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2330 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2331 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2332 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2333 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2334 | false); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2335 | // struct objc_super * |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2336 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2337 | Context->getPointerType(SuperRep->getType()), |
| 2338 | SourceLocation()); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2339 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2340 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2341 | } else { |
| 2342 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2343 | QualType argType = Context->getPointerType(Context->CharTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2344 | ClsExprs.push_back(new (Context) StringLiteral(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2345 | clsName->getName(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2346 | clsName->getLength(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame^] | 2347 | false, argType, SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2348 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2349 | &ClsExprs[0], |
| 2350 | ClsExprs.size()); |
| 2351 | MsgExprs.push_back(Cls); |
| 2352 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2353 | } else { // instance message. |
| 2354 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2355 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2356 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2357 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2358 | if (MsgSendStretFlavor) |
| 2359 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2360 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2361 | |
| 2362 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2363 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2364 | InitExprs.push_back( |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2365 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
| 2366 | new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2367 | Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2368 | SourceLocation()), |
| 2369 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2370 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2371 | |
| 2372 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2373 | QualType argType = Context->getPointerType(Context->CharTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2374 | ClsExprs.push_back(new (Context) StringLiteral(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2375 | SuperDecl->getIdentifier()->getName(), |
| 2376 | SuperDecl->getIdentifier()->getLength(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame^] | 2377 | false, argType, SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2378 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2379 | &ClsExprs[0], |
| 2380 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2381 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2382 | InitExprs.push_back( |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2383 | // set 'super class', using objc_getClass(). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2384 | new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2385 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2386 | // struct objc_super |
| 2387 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2388 | Expr *SuperRep; |
| 2389 | |
| 2390 | if (LangOpts.Microsoft) { |
| 2391 | SynthSuperContructorFunctionDecl(); |
| 2392 | // Simulate a contructor call... |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2393 | DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2394 | superType, SourceLocation()); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2395 | SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], |
| 2396 | InitExprs.size(), |
| 2397 | superType, SourceLocation()); |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2398 | // The code for super is a little tricky to prevent collision with |
| 2399 | // the structure definition in the header. The rewriter has it's own |
| 2400 | // internal definition (__rw_objc_super) that is uses. This is why |
| 2401 | // we need the cast below. For example: |
| 2402 | // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) |
| 2403 | // |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2404 | SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2405 | Context->getPointerType(SuperRep->getType()), |
| 2406 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2407 | SuperRep = new (Context) CStyleCastExpr(Context->getPointerType(superType), |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2408 | SuperRep, Context->getPointerType(superType), |
| 2409 | SourceLocation(), SourceLocation()); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2410 | } else { |
| 2411 | // (struct objc_super) { <exprs from above> } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2412 | InitListExpr *ILE = new (Context) InitListExpr(SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2413 | &InitExprs[0], InitExprs.size(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2414 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2415 | SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2416 | } |
Steve Naroff | 46a98a7 | 2008-12-23 20:11:22 +0000 | [diff] [blame] | 2417 | MsgExprs.push_back(SuperRep); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2418 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2419 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2420 | // Foo<Proto> *. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2421 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2422 | recExpr = CE->getSubExpr(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2423 | recExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), recExpr, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2424 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2425 | SourceLocation(), SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2426 | MsgExprs.push_back(recExpr); |
| 2427 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2428 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2429 | // Create a call to sel_registerName("selName"), it will be the 2nd argument. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2430 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2431 | QualType argType = Context->getPointerType(Context->CharTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2432 | SelExprs.push_back(new (Context) StringLiteral(*Context, |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2433 | Exp->getSelector().getAsString().c_str(), |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2434 | Exp->getSelector().getAsString().size(), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame^] | 2435 | false, argType, SourceLocation())); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2436 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2437 | &SelExprs[0], SelExprs.size()); |
| 2438 | MsgExprs.push_back(SelExp); |
| 2439 | |
| 2440 | // Now push any user supplied arguments. |
| 2441 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2442 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2443 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2444 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2445 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2446 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2447 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2448 | : ICE->getType(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2449 | userExpr = new (Context) CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2450 | } |
| 2451 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2452 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2453 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2454 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2455 | userExpr = CE->getSubExpr(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2456 | userExpr = new (Context) CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2457 | userExpr, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2458 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2459 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2460 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2461 | MsgExprs.push_back(userExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2462 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2463 | // the original expression, since we will delete it below. |
| 2464 | Exp->setArg(i, 0); |
| 2465 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2466 | // Generate the funky cast. |
| 2467 | CastExpr *cast; |
| 2468 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2469 | QualType returnType; |
| 2470 | |
| 2471 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2472 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2473 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2474 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2475 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2476 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2477 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2478 | // Push any user argument types. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 2479 | for (unsigned i = 0; i < mDecl->getNumParams(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2480 | QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType() |
| 2481 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2482 | : mDecl->getParamDecl(i)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2483 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 2484 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2485 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 2486 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2487 | } |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2488 | ArgTypes.push_back(t); |
| 2489 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2490 | returnType = mDecl->getResultType()->isObjCQualifiedIdType() |
| 2491 | ? Context->getObjCIdType() : mDecl->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2492 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2493 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2494 | } |
| 2495 | // Get the type, we will need to reference it in a couple spots. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2496 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2497 | |
| 2498 | // Create a reference to the objc_msgSend() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2499 | DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2500 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2501 | |
| 2502 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2503 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2504 | // xx.m:13: warning: function called through a non-compatible type |
| 2505 | // xx.m:13: note: if this code is reached, the program will abort |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2506 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2507 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2508 | SourceLocation(), SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2509 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2510 | // Now do the "normal" pointer to function cast. |
| 2511 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2512 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2513 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2514 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2515 | castType = Context->getPointerType(castType); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2516 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2517 | |
| 2518 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2519 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2520 | |
| 2521 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2522 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2523 | MsgExprs.size(), |
| 2524 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2525 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2526 | if (MsgSendStretFlavor) { |
| 2527 | // We have the method which returns a struct/union. Must also generate |
| 2528 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2529 | // expression which dictate which one to envoke depending on size of |
| 2530 | // method's return type. |
| 2531 | |
| 2532 | // Create a reference to the objc_msgSend_stret() declaration. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2533 | DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2534 | SourceLocation()); |
| 2535 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2536 | cast = new (Context) CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2537 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2538 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2539 | // Now do the "normal" pointer to function cast. |
| 2540 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2541 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2542 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2543 | castType = Context->getPointerType(castType); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2544 | cast = new (Context) CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2545 | |
| 2546 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2547 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2548 | |
| 2549 | FT = msgSendType->getAsFunctionType(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2550 | CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], |
| 2551 | MsgExprs.size(), |
| 2552 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2553 | |
| 2554 | // Build sizeof(returnType) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2555 | SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, true, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2556 | returnType.getAsOpaquePtr(), |
| 2557 | Context->getSizeType(), |
| 2558 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2559 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2560 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2561 | // For X86 it is more complicated and some kind of target specific routine |
| 2562 | // is needed to decide what to do. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2563 | unsigned IntSize = |
| 2564 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2565 | IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2566 | Context->IntTy, |
| 2567 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2568 | BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2569 | BinaryOperator::LE, |
| 2570 | Context->IntTy, |
| 2571 | SourceLocation()); |
| 2572 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2573 | ConditionalOperator *CondExpr = |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2574 | new (Context) ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
| 2575 | ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2576 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2577 | return ReplacingStmt; |
| 2578 | } |
| 2579 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2580 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2581 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | 80c2855 | 2008-10-27 20:54:44 +0000 | [diff] [blame] | 2582 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 2583 | //ReplacingStmt->dump(); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2584 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2585 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2586 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2587 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2588 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2591 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2592 | /// call to objc_getProtocol("proto-name"). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2593 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2594 | if (!GetProtocolFunctionDecl) |
| 2595 | SynthGetProtocolFunctionDecl(); |
| 2596 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2597 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2598 | QualType argType = Context->getPointerType(Context->CharTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2599 | ProtoExprs.push_back(new (Context) |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 2600 | StringLiteral(*Context, |
| 2601 | Exp->getProtocol()->getNameAsCString(), |
| 2602 | strlen(Exp->getProtocol()->getNameAsCString()), |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame^] | 2603 | false, argType, SourceLocation())); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2604 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2605 | &ProtoExprs[0], |
| 2606 | ProtoExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2607 | ReplaceStmt(Exp, ProtoExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2608 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2609 | return ProtoExp; |
| 2610 | |
| 2611 | } |
| 2612 | |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2613 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2614 | const char *endBuf) { |
| 2615 | while (startBuf < endBuf) { |
| 2616 | if (*startBuf == '#') { |
| 2617 | // Skip whitespace. |
| 2618 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2619 | ; |
| 2620 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2621 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2622 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2623 | !strncmp(startBuf, "define", strlen("define")) || |
| 2624 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2625 | !strncmp(startBuf, "else", strlen("else")) || |
| 2626 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2627 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2628 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2629 | !strncmp(startBuf, "include", strlen("include")) || |
| 2630 | !strncmp(startBuf, "import", strlen("import")) || |
| 2631 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2632 | return true; |
| 2633 | } |
| 2634 | startBuf++; |
| 2635 | } |
| 2636 | return false; |
| 2637 | } |
| 2638 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2639 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2640 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2641 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2642 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2643 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2644 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2645 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2646 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2647 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2648 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2649 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2650 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2651 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2652 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2653 | |
| 2654 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2655 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2656 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2657 | // If no ivars and no root or if its root, directly or indirectly, |
| 2658 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2659 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2660 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2661 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2662 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2663 | return; |
| 2664 | } |
| 2665 | |
| 2666 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2667 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2668 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2669 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2670 | if (LangOpts.Microsoft) |
| 2671 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2672 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2673 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2674 | const char *cursor = strchr(startBuf, '{'); |
| 2675 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2676 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2677 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2678 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2679 | // NSURL.h, for example): |
| 2680 | // |
| 2681 | // #ifdef XYZ |
| 2682 | // @interface Foo : NSObject |
| 2683 | // #else |
| 2684 | // @interface FooBar : NSObject |
| 2685 | // #endif |
| 2686 | // { |
| 2687 | // int i; |
| 2688 | // } |
| 2689 | // @end |
| 2690 | // |
| 2691 | // This clause is segregated to avoid breaking the common case. |
| 2692 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2693 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2694 | CDecl->getClassLoc(); |
| 2695 | const char *endHeader = SM->getCharacterData(L); |
| 2696 | endHeader += Lexer::MeasureTokenLength(L, *SM); |
| 2697 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 2698 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2699 | // advance to the end of the referenced protocols. |
| 2700 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2701 | endHeader++; |
| 2702 | } |
| 2703 | // rewrite the original header |
| 2704 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2705 | } else { |
| 2706 | // rewrite the original header *without* disturbing the '{' |
| 2707 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2708 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2709 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2710 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2711 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2712 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2713 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2714 | Result += "_IVARS;\n"; |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2715 | |
| 2716 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2717 | SourceLocation OnePastCurly = |
| 2718 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2719 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2720 | } |
| 2721 | cursor++; // past '{' |
| 2722 | |
| 2723 | // Now comment out any visibility specifiers. |
| 2724 | while (cursor < endBuf) { |
| 2725 | if (*cursor == '@') { |
| 2726 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2727 | // Skip whitespace. |
| 2728 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2729 | /*scan*/; |
| 2730 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2731 | // FIXME: presence of @public, etc. inside comment results in |
| 2732 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2733 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2734 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2735 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2736 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2737 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2738 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2739 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2740 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2741 | // for type checking. |
| 2742 | else if (*cursor == '<') { |
| 2743 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2744 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2745 | cursor = strchr(cursor, '>'); |
| 2746 | cursor++; |
| 2747 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2748 | InsertText(atLoc, " */", 3); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2749 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2750 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2751 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2752 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2753 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2754 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2755 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2756 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2757 | } else { // we don't have any instance variables - insert super struct. |
| 2758 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 2759 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2760 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2761 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2762 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2763 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2764 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2765 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2766 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2767 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2768 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2771 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2772 | /// class methods. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2773 | void RewriteObjC::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2774 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2775 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2776 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2777 | const char *ClassName, |
| 2778 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2779 | if (MethodBegin == MethodEnd) return; |
| 2780 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2781 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2782 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2783 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2784 | SEL _cmd; |
| 2785 | char *method_types; |
| 2786 | void *_imp; |
| 2787 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2788 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2789 | Result += "\nstruct _objc_method {\n"; |
| 2790 | Result += "\tSEL _cmd;\n"; |
| 2791 | Result += "\tchar *method_types;\n"; |
| 2792 | Result += "\tvoid *_imp;\n"; |
| 2793 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2794 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2795 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2796 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2797 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2798 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2799 | |
| 2800 | /* struct { |
| 2801 | struct _objc_method_list *next_method; |
| 2802 | int method_count; |
| 2803 | struct _objc_method method_list[]; |
| 2804 | } |
| 2805 | */ |
| 2806 | Result += "\nstatic struct {\n"; |
| 2807 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2808 | Result += "\tint method_count;\n"; |
| 2809 | Result += "\tstruct _objc_method method_list["; |
| 2810 | Result += utostr(MethodEnd-MethodBegin); |
| 2811 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2812 | Result += prefix; |
| 2813 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2814 | Result += "_METHODS_"; |
| 2815 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2816 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2817 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2818 | Result += "_meth\")))= "; |
| 2819 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2820 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2821 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2822 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2823 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2824 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2825 | Result += "\", \""; |
| 2826 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2827 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2828 | Result += MethodInternalNames[*MethodBegin]; |
| 2829 | Result += "}\n"; |
| 2830 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2831 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2832 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2833 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2834 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2835 | Result += "\", \""; |
| 2836 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2837 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2838 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2839 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2840 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2841 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2844 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2845 | void RewriteObjC:: |
| 2846 | RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 2847 | const char *prefix, |
| 2848 | const char *ClassName, |
| 2849 | std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2850 | static bool objc_protocol_methods = false; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2851 | if (Protocols.empty()) return; |
| 2852 | |
| 2853 | for (unsigned i = 0; i != Protocols.size(); i++) { |
| 2854 | ObjCProtocolDecl *PDecl = Protocols[i]; |
| 2855 | // Output struct protocol_methods holder of method selector and type. |
| 2856 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2857 | /* struct protocol_methods { |
| 2858 | SEL _cmd; |
| 2859 | char *method_types; |
| 2860 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2861 | */ |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2862 | Result += "\nstruct protocol_methods {\n"; |
| 2863 | Result += "\tSEL _cmd;\n"; |
| 2864 | Result += "\tchar *method_types;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2865 | Result += "};\n"; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2866 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2867 | objc_protocol_methods = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2868 | } |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2869 | // Do not synthesize the protocol more than once. |
| 2870 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2871 | continue; |
| 2872 | |
| 2873 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2874 | unsigned NumMethods = PDecl->getNumInstanceMethods(); |
| 2875 | /* struct _objc_protocol_method_list { |
| 2876 | int protocol_method_count; |
| 2877 | struct protocol_methods protocols[]; |
| 2878 | } |
| 2879 | */ |
| 2880 | Result += "\nstatic struct {\n"; |
| 2881 | Result += "\tint protocol_method_count;\n"; |
| 2882 | Result += "\tstruct protocol_methods protocols["; |
| 2883 | Result += utostr(NumMethods); |
| 2884 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2885 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2886 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2887 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2888 | |
| 2889 | // Output instance methods declared in this protocol. |
| 2890 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
| 2891 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 2892 | if (I == PDecl->instmeth_begin()) |
| 2893 | Result += "\t ,{{(SEL)\""; |
| 2894 | else |
| 2895 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2896 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2897 | std::string MethodTypeString; |
| 2898 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2899 | Result += "\", \""; |
| 2900 | Result += MethodTypeString; |
| 2901 | Result += "\"}\n"; |
| 2902 | } |
| 2903 | Result += "\t }\n};\n"; |
| 2904 | } |
| 2905 | |
| 2906 | // Output class methods declared in this protocol. |
| 2907 | int NumMethods = PDecl->getNumClassMethods(); |
| 2908 | if (NumMethods > 0) { |
| 2909 | /* struct _objc_protocol_method_list { |
| 2910 | int protocol_method_count; |
| 2911 | struct protocol_methods protocols[]; |
| 2912 | } |
| 2913 | */ |
| 2914 | Result += "\nstatic struct {\n"; |
| 2915 | Result += "\tint protocol_method_count;\n"; |
| 2916 | Result += "\tstruct protocol_methods protocols["; |
| 2917 | Result += utostr(NumMethods); |
| 2918 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2919 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2920 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2921 | "{\n\t"; |
| 2922 | Result += utostr(NumMethods); |
| 2923 | Result += "\n"; |
| 2924 | |
| 2925 | // Output instance methods declared in this protocol. |
| 2926 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
| 2927 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 2928 | if (I == PDecl->classmeth_begin()) |
| 2929 | Result += "\t ,{{(SEL)\""; |
| 2930 | else |
| 2931 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2932 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2933 | std::string MethodTypeString; |
| 2934 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2935 | Result += "\", \""; |
| 2936 | Result += MethodTypeString; |
| 2937 | Result += "\"}\n"; |
| 2938 | } |
| 2939 | Result += "\t }\n};\n"; |
| 2940 | } |
| 2941 | |
| 2942 | // Output: |
| 2943 | /* struct _objc_protocol { |
| 2944 | // Objective-C 1.0 extensions |
| 2945 | struct _objc_protocol_extension *isa; |
| 2946 | char *protocol_name; |
| 2947 | struct _objc_protocol **protocol_list; |
| 2948 | struct _objc_protocol_method_list *instance_methods; |
| 2949 | struct _objc_protocol_method_list *class_methods; |
| 2950 | }; |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2951 | */ |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2952 | static bool objc_protocol = false; |
| 2953 | if (!objc_protocol) { |
| 2954 | Result += "\nstruct _objc_protocol {\n"; |
| 2955 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2956 | Result += "\tchar *protocol_name;\n"; |
| 2957 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2958 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2959 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2960 | Result += "};\n"; |
| 2961 | |
| 2962 | objc_protocol = true; |
| 2963 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2964 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2965 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2966 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2967 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 2968 | "{\n\t0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2969 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2970 | Result += "\", 0, "; |
| 2971 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2972 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2973 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2974 | Result += ", "; |
| 2975 | } |
| 2976 | else |
| 2977 | Result += "0, "; |
| 2978 | if (PDecl->getNumClassMethods() > 0) { |
| 2979 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2980 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2981 | Result += "\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2982 | } |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2983 | else |
| 2984 | Result += "0\n"; |
| 2985 | Result += "};\n"; |
| 2986 | |
| 2987 | // Mark this protocol as having been generated. |
| 2988 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 2989 | assert(false && "protocol already synthesized"); |
| 2990 | } |
| 2991 | // Output the top lovel protocol meta-data for the class. |
| 2992 | /* struct _objc_protocol_list { |
| 2993 | struct _objc_protocol_list *next; |
| 2994 | int protocol_count; |
| 2995 | struct _objc_protocol *class_protocols[]; |
| 2996 | } |
| 2997 | */ |
| 2998 | Result += "\nstatic struct {\n"; |
| 2999 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 3000 | Result += "\tint protocol_count;\n"; |
| 3001 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 3002 | Result += utostr(Protocols.size()); |
| 3003 | Result += "];\n} _OBJC_"; |
| 3004 | Result += prefix; |
| 3005 | Result += "_PROTOCOLS_"; |
| 3006 | Result += ClassName; |
| 3007 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 3008 | "{\n\t0, "; |
| 3009 | Result += utostr(Protocols.size()); |
| 3010 | Result += "\n"; |
| 3011 | |
| 3012 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3013 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3014 | Result += " \n"; |
| 3015 | |
| 3016 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 3017 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3018 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 3019 | Result += "\n"; |
| 3020 | } |
| 3021 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3024 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3025 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3026 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3027 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3028 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3029 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3030 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3031 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 3032 | CDecl = CDecl->getNextClassCategory()) |
| 3033 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 3034 | break; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3035 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3036 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3037 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3038 | FullCategoryName += IDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3039 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3040 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3041 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3042 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3043 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3044 | |
| 3045 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3046 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3047 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3048 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3049 | |
| 3050 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3051 | // Null CDecl is case of a category implementation with no category interface |
| 3052 | if (CDecl) |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3053 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3054 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3055 | |
| 3056 | /* struct _objc_category { |
| 3057 | char *category_name; |
| 3058 | char *class_name; |
| 3059 | struct _objc_method_list *instance_methods; |
| 3060 | struct _objc_method_list *class_methods; |
| 3061 | struct _objc_protocol_list *protocols; |
| 3062 | // Objective-C 1.0 extensions |
| 3063 | uint32_t size; // sizeof (struct _objc_category) |
| 3064 | struct _objc_property_list *instance_properties; // category's own |
| 3065 | // @property decl. |
| 3066 | }; |
| 3067 | */ |
| 3068 | |
| 3069 | static bool objc_category = false; |
| 3070 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3071 | Result += "\nstruct _objc_category {\n"; |
| 3072 | Result += "\tchar *category_name;\n"; |
| 3073 | Result += "\tchar *class_name;\n"; |
| 3074 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3075 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3076 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3077 | Result += "\tunsigned int size;\n"; |
| 3078 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3079 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3080 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3081 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3082 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3083 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3084 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3085 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3086 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3087 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3088 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3089 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3090 | if (IDecl->getNumInstanceMethods() > 0) { |
| 3091 | Result += "\t, (struct _objc_method_list *)" |
| 3092 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3093 | Result += FullCategoryName; |
| 3094 | Result += "\n"; |
| 3095 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3096 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3097 | Result += "\t, 0\n"; |
| 3098 | if (IDecl->getNumClassMethods() > 0) { |
| 3099 | Result += "\t, (struct _objc_method_list *)" |
| 3100 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3101 | Result += FullCategoryName; |
| 3102 | Result += "\n"; |
| 3103 | } |
| 3104 | else |
| 3105 | Result += "\t, 0\n"; |
| 3106 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3107 | if (CDecl && !CDecl->getReferencedProtocols().empty()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3108 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 3109 | Result += FullCategoryName; |
| 3110 | Result += "\n"; |
| 3111 | } |
| 3112 | else |
| 3113 | Result += "\t, 0\n"; |
| 3114 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3117 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3118 | /// ivar offset. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3119 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3120 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3121 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3122 | if (ivar->isBitField()) { |
| 3123 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3124 | // place all bitfields at offset 0. |
| 3125 | Result += "0"; |
| 3126 | } else { |
| 3127 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3128 | Result += IDecl->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3129 | if (LangOpts.Microsoft) |
| 3130 | Result += "_IMPL"; |
| 3131 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3132 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3133 | Result += ")"; |
| 3134 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3137 | //===----------------------------------------------------------------------===// |
| 3138 | // Meta Data Emission |
| 3139 | //===----------------------------------------------------------------------===// |
| 3140 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3141 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3142 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3143 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3144 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3145 | // Explictly declared @interface's are already synthesized. |
| 3146 | if (CDecl->ImplicitInterfaceDecl()) { |
| 3147 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 3148 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3149 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3150 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3151 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3152 | // Build _objc_ivar_list metadata for classes ivars if needed |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3153 | unsigned NumIvars = !IDecl->ivar_empty() |
| 3154 | ? IDecl->ivar_size() |
| 3155 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3156 | if (NumIvars > 0) { |
| 3157 | static bool objc_ivar = false; |
| 3158 | if (!objc_ivar) { |
| 3159 | /* struct _objc_ivar { |
| 3160 | char *ivar_name; |
| 3161 | char *ivar_type; |
| 3162 | int ivar_offset; |
| 3163 | }; |
| 3164 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3165 | Result += "\nstruct _objc_ivar {\n"; |
| 3166 | Result += "\tchar *ivar_name;\n"; |
| 3167 | Result += "\tchar *ivar_type;\n"; |
| 3168 | Result += "\tint ivar_offset;\n"; |
| 3169 | Result += "};\n"; |
| 3170 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3171 | objc_ivar = true; |
| 3172 | } |
| 3173 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3174 | /* struct { |
| 3175 | int ivar_count; |
| 3176 | struct _objc_ivar ivar_list[nIvars]; |
| 3177 | }; |
| 3178 | */ |
| 3179 | Result += "\nstatic struct {\n"; |
| 3180 | Result += "\tint ivar_count;\n"; |
| 3181 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3182 | Result += utostr(NumIvars); |
| 3183 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3184 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3185 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3186 | "{\n\t"; |
| 3187 | Result += utostr(NumIvars); |
| 3188 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3189 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3190 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3191 | if (!IDecl->ivar_empty()) { |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3192 | IVI = IDecl->ivar_begin(); |
| 3193 | IVE = IDecl->ivar_end(); |
| 3194 | } else { |
| 3195 | IVI = CDecl->ivar_begin(); |
| 3196 | IVE = CDecl->ivar_end(); |
| 3197 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3198 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3199 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3200 | Result += "\", \""; |
| 3201 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3202 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3203 | Result += StrEncoding; |
| 3204 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3205 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3206 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3207 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3208 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3209 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3210 | Result += "\", \""; |
| 3211 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3212 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3213 | Result += StrEncoding; |
| 3214 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3215 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3216 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3223 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3224 | true, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3225 | |
| 3226 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3227 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3228 | false, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3229 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3230 | // Protocols referenced in class declaration? |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3231 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3232 | "CLASS", CDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3233 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3234 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3235 | // Declaration of class/meta-class metadata |
| 3236 | /* struct _objc_class { |
| 3237 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3238 | const char *super_class_name; |
| 3239 | char *name; |
| 3240 | long version; |
| 3241 | long info; |
| 3242 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3243 | struct _objc_ivar_list *ivars; |
| 3244 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3245 | struct objc_cache *cache; |
| 3246 | struct objc_protocol_list *protocols; |
| 3247 | const char *ivar_layout; |
| 3248 | struct _objc_class_ext *ext; |
| 3249 | }; |
| 3250 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3251 | static bool objc_class = false; |
| 3252 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3253 | Result += "\nstruct _objc_class {\n"; |
| 3254 | Result += "\tstruct _objc_class *isa;\n"; |
| 3255 | Result += "\tconst char *super_class_name;\n"; |
| 3256 | Result += "\tchar *name;\n"; |
| 3257 | Result += "\tlong version;\n"; |
| 3258 | Result += "\tlong info;\n"; |
| 3259 | Result += "\tlong instance_size;\n"; |
| 3260 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3261 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3262 | Result += "\tstruct objc_cache *cache;\n"; |
| 3263 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3264 | Result += "\tconst char *ivar_layout;\n"; |
| 3265 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3266 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3267 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3271 | ObjCInterfaceDecl *RootClass = 0; |
| 3272 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3273 | while (SuperClass) { |
| 3274 | RootClass = SuperClass; |
| 3275 | SuperClass = SuperClass->getSuperClass(); |
| 3276 | } |
| 3277 | SuperClass = CDecl->getSuperClass(); |
| 3278 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3279 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3280 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3281 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3282 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3283 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3284 | Result += "\""; |
| 3285 | |
| 3286 | if (SuperClass) { |
| 3287 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3288 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3289 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3290 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3291 | Result += "\""; |
| 3292 | } |
| 3293 | else { |
| 3294 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3295 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3296 | Result += "\""; |
| 3297 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3298 | // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3299 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3300 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 3301 | if (IDecl->getNumClassMethods() > 0) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3302 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3303 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3304 | Result += "\n"; |
| 3305 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3306 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3307 | Result += ", 0\n"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 3308 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3309 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3310 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3311 | Result += ",0,0\n"; |
| 3312 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3313 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3314 | Result += "\t,0,0,0,0\n"; |
| 3315 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3316 | |
| 3317 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3318 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3319 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3320 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3321 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3322 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3323 | if (SuperClass) { |
| 3324 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3325 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3326 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3327 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3328 | Result += "\""; |
| 3329 | } |
| 3330 | else { |
| 3331 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3332 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3333 | Result += "\""; |
| 3334 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3335 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3336 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3337 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3338 | Result += ",0"; |
| 3339 | else { |
| 3340 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3341 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3342 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3343 | if (LangOpts.Microsoft) |
| 3344 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3345 | Result += ")"; |
| 3346 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3347 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3348 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3349 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3350 | Result += "\n\t"; |
| 3351 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3352 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3353 | Result += ",0"; |
| 3354 | if (IDecl->getNumInstanceMethods() > 0) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3355 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3356 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3357 | Result += ", 0\n\t"; |
| 3358 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3359 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3360 | Result += ",0,0"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 3361 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3362 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3363 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3364 | Result += ", 0,0\n"; |
| 3365 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3366 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3367 | Result += ",0,0,0\n"; |
| 3368 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3369 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3370 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3371 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3372 | /// and emits meta-data. |
| 3373 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3374 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3375 | int ClsDefCount = ClassImplementation.size(); |
| 3376 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3377 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3378 | // Rewrite implemented methods |
| 3379 | for (int i = 0; i < ClsDefCount; i++) |
| 3380 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3381 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3382 | for (int i = 0; i < CatDefCount; i++) |
| 3383 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3384 | } |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3385 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3386 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3387 | int ClsDefCount = ClassImplementation.size(); |
| 3388 | int CatDefCount = CategoryImplementation.size(); |
| 3389 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3390 | // This is needed for determining instance variable offsets. |
Steve Naroff | a11440b | 2008-08-05 18:47:23 +0000 | [diff] [blame] | 3391 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3392 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3393 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3394 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3395 | |
| 3396 | // For each implemented category, write out all its meta data. |
| 3397 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3398 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3399 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3400 | // Write objc_symtab metadata |
| 3401 | /* |
| 3402 | struct _objc_symtab |
| 3403 | { |
| 3404 | long sel_ref_cnt; |
| 3405 | SEL *refs; |
| 3406 | short cls_def_cnt; |
| 3407 | short cat_def_cnt; |
| 3408 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3409 | }; |
| 3410 | */ |
| 3411 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3412 | Result += "\nstruct _objc_symtab {\n"; |
| 3413 | Result += "\tlong sel_ref_cnt;\n"; |
| 3414 | Result += "\tSEL *refs;\n"; |
| 3415 | Result += "\tshort cls_def_cnt;\n"; |
| 3416 | Result += "\tshort cat_def_cnt;\n"; |
| 3417 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3418 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3419 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3420 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3421 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3422 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3423 | + ", " + utostr(CatDefCount) + "\n"; |
| 3424 | for (int i = 0; i < ClsDefCount; i++) { |
| 3425 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3426 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3427 | Result += "\n"; |
| 3428 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3429 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3430 | for (int i = 0; i < CatDefCount; i++) { |
| 3431 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3432 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3433 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3434 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3435 | Result += "\n"; |
| 3436 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3437 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3438 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3439 | |
| 3440 | // Write objc_module metadata |
| 3441 | |
| 3442 | /* |
| 3443 | struct _objc_module { |
| 3444 | long version; |
| 3445 | long size; |
| 3446 | const char *name; |
| 3447 | struct _objc_symtab *symtab; |
| 3448 | } |
| 3449 | */ |
| 3450 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3451 | Result += "\nstruct _objc_module {\n"; |
| 3452 | Result += "\tlong version;\n"; |
| 3453 | Result += "\tlong size;\n"; |
| 3454 | Result += "\tconst char *name;\n"; |
| 3455 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3456 | Result += "};\n\n"; |
| 3457 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3458 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3459 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3460 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3461 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3462 | |
| 3463 | if (LangOpts.Microsoft) { |
| 3464 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3465 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3466 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3467 | Result += "&_OBJC_MODULES;\n"; |
| 3468 | Result += "#pragma data_seg(pop)\n\n"; |
| 3469 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3470 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3471 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3472 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3473 | const char *funcName, |
| 3474 | std::string Tag) { |
| 3475 | const FunctionType *AFT = CE->getFunctionType(); |
| 3476 | QualType RT = AFT->getResultType(); |
| 3477 | std::string StructRef = "struct " + Tag; |
| 3478 | std::string S = "static " + RT.getAsString() + " __" + |
| 3479 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3480 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3481 | BlockDecl *BD = CE->getBlockDecl(); |
| 3482 | |
| 3483 | if (isa<FunctionTypeNoProto>(AFT)) { |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3484 | // No user-supplied arguments. Still need to pass in a pointer to the |
| 3485 | // block (to reference imported block decl refs). |
| 3486 | S += "(" + StructRef + " *__cself)"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3487 | } else if (BD->param_empty()) { |
| 3488 | S += "(" + StructRef + " *__cself)"; |
| 3489 | } else { |
| 3490 | const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT); |
| 3491 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3492 | S += '('; |
| 3493 | // first add the implicit argument. |
| 3494 | S += StructRef + " *__cself, "; |
| 3495 | std::string ParamStr; |
| 3496 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3497 | E = BD->param_end(); AI != E; ++AI) { |
| 3498 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3499 | ParamStr = (*AI)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3500 | (*AI)->getType().getAsStringInternal(ParamStr); |
| 3501 | S += ParamStr; |
| 3502 | } |
| 3503 | if (FT->isVariadic()) { |
| 3504 | if (!BD->param_empty()) S += ", "; |
| 3505 | S += "..."; |
| 3506 | } |
| 3507 | S += ')'; |
| 3508 | } |
| 3509 | S += " {\n"; |
| 3510 | |
| 3511 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3512 | // First, emit a declaration for all "by ref" decls. |
| 3513 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3514 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3515 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3516 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3517 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3518 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3519 | } |
| 3520 | // Next, emit a declaration for all "by copy" declarations. |
| 3521 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3522 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3523 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3524 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3525 | // Handle nested closure invocation. For example: |
| 3526 | // |
| 3527 | // void (^myImportedClosure)(void); |
| 3528 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3529 | // |
| 3530 | // void (^anotherClosure)(void); |
| 3531 | // anotherClosure = ^(void) { |
| 3532 | // myImportedClosure(); // import and invoke the closure |
| 3533 | // }; |
| 3534 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3535 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3536 | S += "struct __block_impl *"; |
| 3537 | else |
| 3538 | (*I)->getType().getAsStringInternal(Name); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3539 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3540 | } |
| 3541 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3542 | const char *cstr = RewrittenStr.c_str(); |
| 3543 | while (*cstr++ != '{') ; |
| 3544 | S += cstr; |
| 3545 | S += "\n"; |
| 3546 | return S; |
| 3547 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3548 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3549 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3550 | const char *funcName, |
| 3551 | std::string Tag) { |
| 3552 | std::string StructRef = "struct " + Tag; |
| 3553 | std::string S = "static void __"; |
| 3554 | |
| 3555 | S += funcName; |
| 3556 | S += "_block_copy_" + utostr(i); |
| 3557 | S += "(" + StructRef; |
| 3558 | S += "*dst, " + StructRef; |
| 3559 | S += "*src) {"; |
| 3560 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3561 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3562 | S += "_Block_object_assign((void*)&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3563 | S += (*I)->getNameAsString(); |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 3564 | S += ", (void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3565 | S += (*I)->getNameAsString(); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3566 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);}"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3567 | } |
| 3568 | S += "\nstatic void __"; |
| 3569 | S += funcName; |
| 3570 | S += "_block_dispose_" + utostr(i); |
| 3571 | S += "(" + StructRef; |
| 3572 | S += "*src) {"; |
| 3573 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3574 | E = ImportedBlockDecls.end(); I != E; ++I) { |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3575 | S += "_Block_object_dispose((void*)src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3576 | S += (*I)->getNameAsString(); |
Steve Naroff | 5bc60d0 | 2008-12-16 15:50:30 +0000 | [diff] [blame] | 3577 | S += ", 3/*BLOCK_FIELD_IS_OBJECT*/);"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3578 | } |
| 3579 | S += "}\n"; |
| 3580 | return S; |
| 3581 | } |
| 3582 | |
| 3583 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3584 | bool hasCopyDisposeHelpers) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3585 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3586 | std::string Constructor = " " + Tag; |
| 3587 | |
| 3588 | S += " {\n struct __block_impl impl;\n"; |
| 3589 | |
| 3590 | if (hasCopyDisposeHelpers) |
| 3591 | S += " void *copy;\n void *dispose;\n"; |
| 3592 | |
| 3593 | Constructor += "(void *fp"; |
| 3594 | |
| 3595 | if (hasCopyDisposeHelpers) |
| 3596 | Constructor += ", void *copyHelp, void *disposeHelp"; |
| 3597 | |
| 3598 | if (BlockDeclRefs.size()) { |
| 3599 | // Output all "by copy" declarations. |
| 3600 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3601 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3602 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3603 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3604 | std::string ArgName = "_" + FieldName; |
| 3605 | // Handle nested closure invocation. For example: |
| 3606 | // |
| 3607 | // void (^myImportedBlock)(void); |
| 3608 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3609 | // |
| 3610 | // void (^anotherBlock)(void); |
| 3611 | // anotherBlock = ^(void) { |
| 3612 | // myImportedBlock(); // import and invoke the closure |
| 3613 | // }; |
| 3614 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3615 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3616 | S += "struct __block_impl *"; |
| 3617 | Constructor += ", void *" + ArgName; |
| 3618 | } else { |
| 3619 | (*I)->getType().getAsStringInternal(FieldName); |
| 3620 | (*I)->getType().getAsStringInternal(ArgName); |
| 3621 | Constructor += ", " + ArgName; |
| 3622 | } |
| 3623 | S += FieldName + ";\n"; |
| 3624 | } |
| 3625 | // Output all "by ref" declarations. |
| 3626 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3627 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3628 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3629 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3630 | std::string ArgName = "_" + FieldName; |
| 3631 | // Handle nested closure invocation. For example: |
| 3632 | // |
| 3633 | // void (^myImportedBlock)(void); |
| 3634 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3635 | // |
| 3636 | // void (^anotherBlock)(void); |
| 3637 | // anotherBlock = ^(void) { |
| 3638 | // myImportedBlock(); // import and invoke the closure |
| 3639 | // }; |
| 3640 | // |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3641 | if (isTopLevelBlockPointerType((*I)->getType())) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3642 | S += "struct __block_impl *"; |
| 3643 | Constructor += ", void *" + ArgName; |
| 3644 | } else { |
| 3645 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName); |
| 3646 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName); |
| 3647 | Constructor += ", " + ArgName; |
| 3648 | } |
| 3649 | S += FieldName + "; // by ref\n"; |
| 3650 | } |
| 3651 | // Finish writing the constructor. |
| 3652 | // FIXME: handle NSConcreteGlobalBlock. |
| 3653 | Constructor += ", int flags=0) {\n"; |
| 3654 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3655 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3656 | |
| 3657 | if (hasCopyDisposeHelpers) |
| 3658 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3659 | |
| 3660 | // Initialize all "by copy" arguments. |
| 3661 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3662 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3663 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3664 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3665 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3666 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3667 | else |
| 3668 | Constructor += Name + " = _"; |
| 3669 | Constructor += Name + ";\n"; |
| 3670 | } |
| 3671 | // Initialize all "by ref" arguments. |
| 3672 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3673 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3674 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3675 | Constructor += " "; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3676 | if (isTopLevelBlockPointerType((*I)->getType())) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3677 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3678 | else |
| 3679 | Constructor += Name + " = _"; |
| 3680 | Constructor += Name + ";\n"; |
| 3681 | } |
| 3682 | } else { |
| 3683 | // Finish writing the constructor. |
| 3684 | // FIXME: handle NSConcreteGlobalBlock. |
| 3685 | Constructor += ", int flags=0) {\n"; |
| 3686 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3687 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3688 | if (hasCopyDisposeHelpers) |
| 3689 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3690 | } |
| 3691 | Constructor += " "; |
| 3692 | Constructor += "}\n"; |
| 3693 | S += Constructor; |
| 3694 | S += "};\n"; |
| 3695 | return S; |
| 3696 | } |
| 3697 | |
| 3698 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3699 | const char *FunName) { |
| 3700 | // Insert closures that were part of the function. |
| 3701 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3702 | |
| 3703 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3704 | |
| 3705 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3706 | |
| 3707 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
| 3708 | ImportedBlockDecls.size() > 0); |
| 3709 | |
| 3710 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3711 | |
| 3712 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
| 3713 | |
| 3714 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3715 | |
| 3716 | if (ImportedBlockDecls.size()) { |
| 3717 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3718 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3719 | } |
| 3720 | |
| 3721 | BlockDeclRefs.clear(); |
| 3722 | BlockByRefDecls.clear(); |
| 3723 | BlockByCopyDecls.clear(); |
| 3724 | BlockCallExprs.clear(); |
| 3725 | ImportedBlockDecls.clear(); |
| 3726 | } |
| 3727 | Blocks.clear(); |
| 3728 | RewrittenBlockExprs.clear(); |
| 3729 | } |
| 3730 | |
| 3731 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3732 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3733 | const char *FuncName = FD->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3734 | |
| 3735 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3736 | } |
| 3737 | |
| 3738 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3739 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3740 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3741 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3742 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3743 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3744 | // Convert colons to underscores. |
| 3745 | std::string::size_type loc = 0; |
| 3746 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3747 | FuncName.replace(loc, 1, "_"); |
| 3748 | |
| 3749 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3750 | } |
| 3751 | |
| 3752 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3753 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3754 | CI != E; ++CI) |
| 3755 | if (*CI) { |
| 3756 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3757 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3758 | else |
| 3759 | GetBlockDeclRefExprs(*CI); |
| 3760 | } |
| 3761 | // Handle specific things. |
| 3762 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3763 | // FIXME: Handle enums. |
| 3764 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3765 | BlockDeclRefs.push_back(CDRE); |
| 3766 | return; |
| 3767 | } |
| 3768 | |
| 3769 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3770 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3771 | CI != E; ++CI) |
| 3772 | if (*CI) { |
| 3773 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3774 | GetBlockCallExprs(CBE->getBody()); |
| 3775 | else |
| 3776 | GetBlockCallExprs(*CI); |
| 3777 | } |
| 3778 | |
| 3779 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3780 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3781 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3782 | } |
| 3783 | } |
| 3784 | return; |
| 3785 | } |
| 3786 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3787 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3788 | // Navigate to relevant type information. |
| 3789 | const char *closureName = 0; |
| 3790 | const BlockPointerType *CPT = 0; |
| 3791 | |
| 3792 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3793 | closureName = DRE->getDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3794 | CPT = DRE->getType()->getAsBlockPointerType(); |
| 3795 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3796 | closureName = CDRE->getDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3797 | CPT = CDRE->getType()->getAsBlockPointerType(); |
| 3798 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3799 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3800 | CPT = MExpr->getType()->getAsBlockPointerType(); |
| 3801 | } else { |
| 3802 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3803 | } |
| 3804 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3805 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3806 | assert(FT && "RewriteBlockClass: Bad type"); |
| 3807 | const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FT); |
| 3808 | // FTP will be null for closures that don't take arguments. |
| 3809 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3810 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3811 | SourceLocation(), |
| 3812 | &Context->Idents.get("__block_impl")); |
| 3813 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3814 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3815 | // Generate a funky cast. |
| 3816 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3817 | |
| 3818 | // Push the block argument type. |
| 3819 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3820 | if (FTP) { |
| 3821 | for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3822 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3823 | QualType t = *I; |
| 3824 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3825 | if (isTopLevelBlockPointerType(t)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3826 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 3827 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3828 | } |
| 3829 | ArgTypes.push_back(t); |
| 3830 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3831 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3832 | // Now do the pointer to function cast. |
| 3833 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
| 3834 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
| 3835 | |
| 3836 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3837 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3838 | CastExpr *BlkCast = new (Context) CStyleCastExpr(PtrBlock, Exp->getCallee(), |
| 3839 | PtrBlock, SourceLocation(), |
| 3840 | SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3841 | // Don't forget the parens to enforce the proper binding. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3842 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), |
| 3843 | BlkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3844 | //PE->dump(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3845 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3846 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3847 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3848 | /*BitWidth=*/0, /*Mutable=*/true); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3849 | MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), |
| 3850 | FD->getType()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3851 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3852 | CastExpr *FunkCast = new (Context) CStyleCastExpr(PtrToFuncCastType, ME, |
| 3853 | PtrToFuncCastType, |
| 3854 | SourceLocation(), |
| 3855 | SourceLocation()); |
| 3856 | PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3857 | |
| 3858 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3859 | // Add the implicit argument. |
| 3860 | BlkExprs.push_back(BlkCast); |
| 3861 | // Add the user arguments. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3862 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3863 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3864 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3865 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3866 | CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], |
| 3867 | BlkExprs.size(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3868 | Exp->getType(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3869 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3873 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3874 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | void RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
| 3878 | // FIXME: Add more elaborate code generation required by the ABI. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3879 | Expr *DerefExpr = new (Context) UnaryOperator(BDRE, UnaryOperator::Deref, |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3880 | Context->getPointerType(BDRE->getType()), |
| 3881 | SourceLocation()); |
| 3882 | // Need parens to enforce precedence. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3883 | ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), DerefExpr); |
Steve Naroff | df8570d | 2009-02-02 17:19:26 +0000 | [diff] [blame] | 3884 | ReplaceStmt(BDRE, PE); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3887 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3888 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3889 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3890 | |
| 3891 | // Need to avoid trying to rewrite synthesized casts. |
| 3892 | if (LocStart.isInvalid()) |
| 3893 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3894 | // Need to avoid trying to rewrite casts contained in macros. |
| 3895 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3896 | return; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3897 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3898 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3899 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 3900 | |
| 3901 | // advance the location to startArgList. |
| 3902 | const char *argPtr = startBuf; |
| 3903 | |
| 3904 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3905 | switch (*argPtr) { |
| 3906 | case '^': |
| 3907 | // Replace the '^' with '*'. |
| 3908 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 3909 | ReplaceText(LocStart, 1, "*", 1); |
| 3910 | break; |
| 3911 | } |
| 3912 | } |
| 3913 | return; |
| 3914 | } |
| 3915 | |
| 3916 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3917 | SourceLocation DeclLoc = FD->getLocation(); |
| 3918 | unsigned parenCount = 0; |
| 3919 | |
| 3920 | // We have 1 or more arguments that have closure pointers. |
| 3921 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3922 | const char *startArgList = strchr(startBuf, '('); |
| 3923 | |
| 3924 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 3925 | |
| 3926 | parenCount++; |
| 3927 | // advance the location to startArgList. |
| 3928 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 3929 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 3930 | |
| 3931 | const char *argPtr = startArgList; |
| 3932 | |
| 3933 | while (*argPtr++ && parenCount) { |
| 3934 | switch (*argPtr) { |
| 3935 | case '^': |
| 3936 | // Replace the '^' with '*'. |
| 3937 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 3938 | ReplaceText(DeclLoc, 1, "*", 1); |
| 3939 | break; |
| 3940 | case '(': |
| 3941 | parenCount++; |
| 3942 | break; |
| 3943 | case ')': |
| 3944 | parenCount--; |
| 3945 | break; |
| 3946 | } |
| 3947 | } |
| 3948 | return; |
| 3949 | } |
| 3950 | |
| 3951 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 3952 | const FunctionTypeProto *FTP; |
| 3953 | const PointerType *PT = QT->getAsPointerType(); |
| 3954 | if (PT) { |
| 3955 | FTP = PT->getPointeeType()->getAsFunctionTypeProto(); |
| 3956 | } else { |
| 3957 | const BlockPointerType *BPT = QT->getAsBlockPointerType(); |
| 3958 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 3959 | FTP = BPT->getPointeeType()->getAsFunctionTypeProto(); |
| 3960 | } |
| 3961 | if (FTP) { |
| 3962 | for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(), |
| 3963 | E = FTP->arg_type_end(); I != E; ++I) |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 3964 | if (isTopLevelBlockPointerType(*I)) |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3965 | return true; |
| 3966 | } |
| 3967 | return false; |
| 3968 | } |
| 3969 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3970 | void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, |
| 3971 | const char *&RParen) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3972 | const char *argPtr = strchr(Name, '('); |
| 3973 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 3974 | |
| 3975 | LParen = argPtr; // output the start. |
| 3976 | argPtr++; // skip past the left paren. |
| 3977 | unsigned parenCount = 1; |
| 3978 | |
| 3979 | while (*argPtr && parenCount) { |
| 3980 | switch (*argPtr) { |
| 3981 | case '(': parenCount++; break; |
| 3982 | case ')': parenCount--; break; |
| 3983 | default: break; |
| 3984 | } |
| 3985 | if (parenCount) argPtr++; |
| 3986 | } |
| 3987 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 3988 | RParen = argPtr; // output the end |
| 3989 | } |
| 3990 | |
| 3991 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 3992 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 3993 | RewriteBlockPointerFunctionArgs(FD); |
| 3994 | return; |
| 3995 | } |
| 3996 | // Handle Variables and Typedefs. |
| 3997 | SourceLocation DeclLoc = ND->getLocation(); |
| 3998 | QualType DeclT; |
| 3999 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4000 | DeclT = VD->getType(); |
| 4001 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 4002 | DeclT = TDD->getUnderlyingType(); |
| 4003 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 4004 | DeclT = FD->getType(); |
| 4005 | else |
| 4006 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 4007 | |
| 4008 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 4009 | const char *endBuf = startBuf; |
| 4010 | // scan backward (from the decl location) for the end of the previous decl. |
| 4011 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 4012 | startBuf--; |
| 4013 | |
| 4014 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 4015 | // may take block argument types (which will be handled below). |
| 4016 | if (*startBuf == '^') { |
| 4017 | // Replace the '^' with '*', computing a negative offset. |
| 4018 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 4019 | ReplaceText(DeclLoc, 1, "*", 1); |
| 4020 | } |
| 4021 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 4022 | // Replace the '^' with '*' for arguments. |
| 4023 | DeclLoc = ND->getLocation(); |
| 4024 | startBuf = SM->getCharacterData(DeclLoc); |
| 4025 | const char *argListBegin, *argListEnd; |
| 4026 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 4027 | while (argListBegin < argListEnd) { |
| 4028 | if (*argListBegin == '^') { |
| 4029 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 4030 | ReplaceText(CaretLoc, 1, "*", 1); |
| 4031 | } |
| 4032 | argListBegin++; |
| 4033 | } |
| 4034 | } |
| 4035 | return; |
| 4036 | } |
| 4037 | |
| 4038 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 4039 | // Add initializers for any closure decl refs. |
| 4040 | GetBlockDeclRefExprs(Exp->getBody()); |
| 4041 | if (BlockDeclRefs.size()) { |
| 4042 | // Unique all "by copy" declarations. |
| 4043 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4044 | if (!BlockDeclRefs[i]->isByRef()) |
| 4045 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4046 | // Unique all "by ref" declarations. |
| 4047 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 4048 | if (BlockDeclRefs[i]->isByRef()) { |
| 4049 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4050 | } |
| 4051 | // Find any imported blocks...they will need special attention. |
| 4052 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
Steve Naroff | 47a2422 | 2008-12-11 20:51:38 +0000 | [diff] [blame] | 4053 | if (BlockDeclRefs[i]->getType()->isBlockPointerType()) { |
Steve Naroff | 0007268 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 4054 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 4055 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4056 | } |
| 4057 | } |
| 4058 | } |
| 4059 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4060 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4061 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 4062 | QualType FType = Context->getFunctionTypeNoProto(Context->VoidPtrTy); |
| 4063 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4064 | ID, FType, FunctionDecl::Extern, false); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4067 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4068 | Blocks.push_back(Exp); |
| 4069 | |
| 4070 | CollectBlockDeclRefInfo(Exp); |
| 4071 | std::string FuncName; |
| 4072 | |
| 4073 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4074 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4075 | else if (CurMethodDef) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4076 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4077 | // Convert colons to underscores. |
| 4078 | std::string::size_type loc = 0; |
| 4079 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4080 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4081 | } else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4082 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4083 | |
| 4084 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4085 | |
| 4086 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4087 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4088 | |
| 4089 | // Get a pointer to the function type so we can cast appropriately. |
| 4090 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4091 | |
| 4092 | FunctionDecl *FD; |
| 4093 | Expr *NewRep; |
| 4094 | |
| 4095 | // Simulate a contructor call... |
| 4096 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4097 | DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4098 | |
| 4099 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 4100 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4101 | // Initialize the block function. |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4102 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4103 | DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), |
| 4104 | SourceLocation()); |
| 4105 | CastExpr *castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4106 | Context->VoidPtrTy, SourceLocation(), |
| 4107 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4108 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4109 | |
| 4110 | if (ImportedBlockDecls.size()) { |
| 4111 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4112 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4113 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4114 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4115 | Context->VoidPtrTy, SourceLocation(), |
| 4116 | SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4117 | InitExprs.push_back(castExpr); |
| 4118 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4119 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4120 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4121 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4122 | castExpr = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4123 | Context->VoidPtrTy, SourceLocation(), |
| 4124 | SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4125 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4126 | } |
| 4127 | // Add initializers for any closure decl refs. |
| 4128 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4129 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4130 | // Output all "by copy" declarations. |
| 4131 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4132 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4133 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4134 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4135 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4136 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4137 | } else if (isTopLevelBlockPointerType((*I)->getType())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4138 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4139 | Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4140 | Exp = new (Context) CStyleCastExpr(Context->VoidPtrTy, Arg, |
| 4141 | Context->VoidPtrTy, SourceLocation(), |
| 4142 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4143 | } else { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4144 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4145 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4146 | } |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4147 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4148 | } |
| 4149 | // Output all "by ref" declarations. |
| 4150 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4151 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4152 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4153 | Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4154 | Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4155 | Context->getPointerType(Exp->getType()), |
| 4156 | SourceLocation()); |
Steve Naroff | 707b0fe | 2008-11-14 21:36:12 +0000 | [diff] [blame] | 4157 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4158 | } |
| 4159 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4160 | NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), |
| 4161 | FType, SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4162 | NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4163 | Context->getPointerType(NewRep->getType()), |
| 4164 | SourceLocation()); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4165 | NewRep = new (Context) CStyleCastExpr(FType, NewRep, FType, SourceLocation(), |
| 4166 | SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4167 | BlockDeclRefs.clear(); |
| 4168 | BlockByRefDecls.clear(); |
| 4169 | BlockByCopyDecls.clear(); |
| 4170 | ImportedBlockDecls.clear(); |
| 4171 | return NewRep; |
| 4172 | } |
| 4173 | |
| 4174 | //===----------------------------------------------------------------------===// |
| 4175 | // Function Body / Expression rewriting |
| 4176 | //===----------------------------------------------------------------------===// |
| 4177 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4178 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4179 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4180 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4181 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4182 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4183 | // we get this right. |
| 4184 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4185 | // Perform a bottom up traversal of all children. |
| 4186 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4187 | CI != E; ++CI) |
| 4188 | if (*CI) |
| 4189 | CollectPropertySetters(*CI); |
| 4190 | |
| 4191 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4192 | if (BinOp->isAssignmentOp()) { |
| 4193 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4194 | PropSetters[PRE] = BinOp; |
| 4195 | } |
| 4196 | } |
| 4197 | } |
| 4198 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4199 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4200 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4201 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4202 | Stmts.push_back(S); |
| 4203 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4204 | Stmts.push_back(S); |
| 4205 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4206 | } |
| 4207 | |
| 4208 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4209 | |
| 4210 | // Perform a bottom up rewrite of all children. |
| 4211 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4212 | CI != E; ++CI) |
| 4213 | if (*CI) { |
| 4214 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
| 4215 | if (newStmt) |
| 4216 | *CI = newStmt; |
| 4217 | } |
| 4218 | |
| 4219 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4220 | // Rewrite the block body in place. |
| 4221 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4222 | |
| 4223 | // Now we snarf the rewritten text and stash it away for later use. |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4224 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4225 | RewrittenBlockExprs[BE] = Str; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4226 | |
| 4227 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4228 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4229 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4230 | return blockTranscribed; |
| 4231 | } |
| 4232 | // Handle specific things. |
| 4233 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4234 | return RewriteAtEncode(AtEncode); |
| 4235 | |
| 4236 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4237 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4238 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4239 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4240 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4241 | if (BinOp) { |
| 4242 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4243 | // we need to rewrite the right hand side prior to rewriting the setter. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4244 | DisableReplaceStmt = true; |
| 4245 | // Save the source range. Even if we disable the replacement, the |
| 4246 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4247 | // node is at the 'end', the rewriter will fail. Consider this: |
| 4248 | // self.errorHandler = handler ? handler : |
| 4249 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4250 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4251 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4252 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4253 | // |
| 4254 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4255 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4256 | // to see the original expression). Consider this example: |
| 4257 | // |
| 4258 | // Foo *obj1, *obj2; |
| 4259 | // |
| 4260 | // obj1.i = [obj2 rrrr]; |
| 4261 | // |
| 4262 | // 'BinOp' for the previous expression looks like: |
| 4263 | // |
| 4264 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4265 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4266 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4267 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4268 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4269 | // |
| 4270 | // 'newStmt' represents the rewritten message expression. For example: |
| 4271 | // |
| 4272 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4273 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4274 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4275 | // (CStyleCastExpr 0x231d220 'void *' |
| 4276 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4277 | // |
| 4278 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4279 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4280 | // the original RHS (since we haven't altered BinOp). |
| 4281 | // |
| 4282 | // This implies the Rewrite* routines can no longer delete the original |
| 4283 | // node. As a result, we now leak the original AST nodes. |
| 4284 | // |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4285 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4286 | } else { |
| 4287 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4288 | } |
| 4289 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4290 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4291 | return RewriteAtSelector(AtSelector); |
| 4292 | |
| 4293 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4294 | return RewriteObjCStringLiteral(AtString); |
| 4295 | |
| 4296 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4297 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4298 | // Before we rewrite it, put the original message expression in a comment. |
| 4299 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4300 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4301 | |
| 4302 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4303 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4304 | |
| 4305 | std::string messString; |
| 4306 | messString += "// "; |
| 4307 | messString.append(startBuf, endBuf-startBuf+1); |
| 4308 | messString += "\n"; |
| 4309 | |
| 4310 | // FIXME: Missing definition of |
| 4311 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4312 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4313 | // Tried this, but it didn't work either... |
| 4314 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4315 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4316 | return RewriteMessageExpr(MessExpr); |
| 4317 | } |
| 4318 | |
| 4319 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4320 | return RewriteObjCTryStmt(StmtTry); |
| 4321 | |
| 4322 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4323 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4324 | |
| 4325 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4326 | return RewriteObjCThrowStmt(StmtThrow); |
| 4327 | |
| 4328 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4329 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4330 | |
| 4331 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4332 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4333 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4334 | OrigStmtRange.getEnd()); |
| 4335 | if (BreakStmt *StmtBreakStmt = |
| 4336 | dyn_cast<BreakStmt>(S)) |
| 4337 | return RewriteBreakStmt(StmtBreakStmt); |
| 4338 | if (ContinueStmt *StmtContinueStmt = |
| 4339 | dyn_cast<ContinueStmt>(S)) |
| 4340 | return RewriteContinueStmt(StmtContinueStmt); |
| 4341 | |
| 4342 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4343 | // and cast exprs. |
| 4344 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4345 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4346 | // precedes the first Decl. In the future the DeclGroup should have |
| 4347 | // a separate type-specifier that we can rewrite. |
| 4348 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4349 | |
| 4350 | // Blocks rewrite rules. |
| 4351 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4352 | DI != DE; ++DI) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4353 | Decl *SD = *DI; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4354 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4355 | if (isTopLevelBlockPointerType(ND->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4356 | RewriteBlockPointerDecl(ND); |
| 4357 | else if (ND->getType()->isFunctionPointerType()) |
| 4358 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4359 | } |
| 4360 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4361 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4362 | RewriteBlockPointerDecl(TD); |
| 4363 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4364 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4365 | } |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4370 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4371 | |
| 4372 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4373 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4374 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4375 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4376 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4377 | && "Statement stack mismatch"); |
| 4378 | Stmts.pop_back(); |
| 4379 | } |
| 4380 | // Handle blocks rewriting. |
| 4381 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4382 | if (BDRE->isByRef()) |
| 4383 | RewriteBlockDeclRefExpr(BDRE); |
| 4384 | } |
| 4385 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4386 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4387 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4388 | ReplaceStmt(S, BlockCall); |
| 4389 | return BlockCall; |
| 4390 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4391 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4392 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4393 | RewriteCastExpr(CE); |
| 4394 | } |
| 4395 | #if 0 |
| 4396 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4397 | CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4398 | // Get the new text. |
| 4399 | std::string SStr; |
| 4400 | llvm::raw_string_ostream Buf(SStr); |
| 4401 | Replacement->printPretty(Buf); |
| 4402 | const std::string &Str = Buf.str(); |
| 4403 | |
| 4404 | printf("CAST = %s\n", &Str[0]); |
| 4405 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4406 | delete S; |
| 4407 | return Replacement; |
| 4408 | } |
| 4409 | #endif |
| 4410 | // Return this stmt unmodified. |
| 4411 | return S; |
| 4412 | } |
| 4413 | |
| 4414 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4415 | /// main file of the input. |
| 4416 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4417 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | cb73530 | 2008-12-17 00:20:22 +0000 | [diff] [blame] | 4418 | if (FD->isOverloadedOperator()) |
| 4419 | return; |
| 4420 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4421 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4422 | // prototype. This enables us to rewrite function declarations and |
| 4423 | // definitions using the same code. |
| 4424 | RewriteBlocksInFunctionTypeProto(FD->getType(), FD); |
| 4425 | |
| 4426 | if (Stmt *Body = FD->getBody()) { |
| 4427 | CurFunctionDef = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4428 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4429 | CurrentBody = Body; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4430 | FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4431 | CurrentBody = 0; |
| 4432 | if (PropParentMap) { |
| 4433 | delete PropParentMap; |
| 4434 | PropParentMap = 0; |
| 4435 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4436 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4437 | // and any copy/dispose helper functions. |
| 4438 | InsertBlockLiteralsWithinFunction(FD); |
| 4439 | CurFunctionDef = 0; |
| 4440 | } |
| 4441 | return; |
| 4442 | } |
| 4443 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 4444 | if (Stmt *Body = MD->getBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4445 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4446 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4447 | CurrentBody = Body; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4448 | MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4449 | CurrentBody = 0; |
| 4450 | if (PropParentMap) { |
| 4451 | delete PropParentMap; |
| 4452 | PropParentMap = 0; |
| 4453 | } |
Steve Naroff | fa15fd9 | 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 | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4466 | if (isTopLevelBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4467 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4468 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4469 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4470 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4471 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4472 | RewriteCastExpr(CE); |
| 4473 | } |
| 4474 | } |
| 4475 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4476 | if (VD->getInit()) { |
| 4477 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4478 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4479 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4480 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4481 | CurrentBody = 0; |
| 4482 | if (PropParentMap) { |
| 4483 | delete PropParentMap; |
| 4484 | PropParentMap = 0; |
| 4485 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 4486 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4487 | VD->getNameAsCString()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4488 | GlobalVarDecl = 0; |
| 4489 | |
| 4490 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4491 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4492 | RewriteCastExpr(CE); |
| 4493 | } |
| 4494 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4495 | return; |
| 4496 | } |
| 4497 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4498 | if (isTopLevelBlockPointerType(TD->getUnderlyingType())) |
Steve Naroff | fa15fd9 | 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 | a4c46df | 2008-12-11 17:59:21 +0000 | [diff] [blame] | 4506 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4507 | e = RD->field_end(); i != e; ++i) { |
| 4508 | FieldDecl *FD = *i; |
Steve Naroff | 01f2ffa | 2008-12-11 21:05:33 +0000 | [diff] [blame] | 4509 | if (isTopLevelBlockPointerType(FD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4510 | RewriteBlockPointerDecl(FD); |
| 4511 | } |
| 4512 | } |
| 4513 | return; |
| 4514 | } |
| 4515 | // Nothing yet. |
| 4516 | } |
| 4517 | |
| 4518 | void RewriteObjC::HandleTranslationUnit(TranslationUnit& TU) { |
| 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 | 62c2632 | 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 | fa15fd9 | 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 | 62c2632 | 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 | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4551 | OwnedStream.reset(OutFile); |
| 4552 | } |
| 4553 | |
| 4554 | RewriteInclude(); |
| 4555 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 4556 | InsertText(SM->getLocForStartOfFile(MainFileID), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4557 | Preamble.c_str(), Preamble.size(), false); |
| 4558 | |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4559 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4560 | RewriteImplementations(); |
Steve Naroff | fa15fd9 | 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 | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4571 | |
Steve Naroff | 0aab796 | 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 | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4579 | OutFile->flush(); |
| 4580 | } |
| 4581 | |