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 | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 50 | unsigned 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); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 227 | void RewriteImplementationDecl(NamedDecl *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); |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 233 | void RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties); |
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. |
| 342 | bool isBlockPointerType(QualType T) { return isa<BlockPointerType>(T); } |
| 343 | |
| 344 | // FIXME: This predicate seems like it would be useful to add to ASTContext. |
| 345 | bool isObjCType(QualType T) { |
| 346 | if (!LangOpts.ObjC1 && !LangOpts.ObjC2) |
| 347 | return false; |
| 348 | |
| 349 | QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); |
| 350 | |
| 351 | if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || |
| 352 | OCT == Context->getCanonicalType(Context->getObjCClassType())) |
| 353 | return true; |
| 354 | |
| 355 | if (const PointerType *PT = OCT->getAsPointerType()) { |
| 356 | if (isa<ObjCInterfaceType>(PT->getPointeeType()) || |
| 357 | isa<ObjCQualifiedIdType>(PT->getPointeeType())) |
| 358 | return true; |
| 359 | } |
| 360 | return false; |
| 361 | } |
| 362 | bool PointerTypeTakesAnyBlockArguments(QualType QT); |
| 363 | void GetExtentOfArgList(const char *Name, const char *&LParen, const char *&RParen); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 364 | void RewriteCastExpr(CStyleCastExpr *CE); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 365 | |
| 366 | FunctionDecl *SynthBlockInitFunctionDecl(const char *name); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 367 | Stmt *SynthBlockInitExpr(BlockExpr *Exp); |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 368 | }; |
| 369 | } |
| 370 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 371 | void RewriteObjC::RewriteBlocksInFunctionTypeProto(QualType funcType, |
| 372 | NamedDecl *D) { |
| 373 | if (FunctionTypeProto *fproto = dyn_cast<FunctionTypeProto>(funcType)) { |
| 374 | for (FunctionTypeProto::arg_type_iterator I = fproto->arg_type_begin(), |
| 375 | E = fproto->arg_type_end(); I && (I != E); ++I) |
| 376 | if (isBlockPointerType(*I)) { |
| 377 | // All the args are checked/rewritten. Don't call twice! |
| 378 | RewriteBlockPointerDecl(D); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { |
| 385 | const PointerType *PT = funcType->getAsPointerType(); |
| 386 | if (PT && PointerTypeTakesAnyBlockArguments(funcType)) |
| 387 | RewriteBlocksInFunctionTypeProto(PT->getPointeeType(), ND); |
| 388 | } |
| 389 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 390 | static bool IsHeaderFile(const std::string &Filename) { |
| 391 | std::string::size_type DotPos = Filename.rfind('.'); |
| 392 | |
| 393 | if (DotPos == std::string::npos) { |
| 394 | // no file extension |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 399 | // C header: .h |
| 400 | // C++ header: .hh or .H; |
| 401 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 402 | } |
| 403 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 404 | RewriteObjC::RewriteObjC(std::string inFile, std::string outFile, |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 405 | Diagnostic &D, const LangOptions &LOpts) |
| 406 | : Diags(D), LangOpts(LOpts) { |
| 407 | IsHeader = IsHeaderFile(inFile); |
| 408 | InFileName = inFile; |
| 409 | OutFileName = outFile; |
| 410 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 411 | "rewriting sub-expression within a macro (may not be correct)"); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 412 | TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 413 | "rewriter doesn't support user-specified control flow semantics for @try/@finally (code may not execute properly)"); |
Steve Naroff | a7b402d | 2008-03-28 22:26:09 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 416 | ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 417 | const std::string& OutFile, |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 418 | Diagnostic &Diags, |
| 419 | const LangOptions &LOpts) { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 420 | return new RewriteObjC(InFile, OutFile, Diags, LOpts); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 421 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 422 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 423 | void RewriteObjC::Initialize(ASTContext &context) { |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 424 | Context = &context; |
| 425 | SM = &Context->getSourceManager(); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 426 | TUDecl = Context->getTranslationUnitDecl(); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 427 | MsgSendFunctionDecl = 0; |
| 428 | MsgSendSuperFunctionDecl = 0; |
| 429 | MsgSendStretFunctionDecl = 0; |
| 430 | MsgSendSuperStretFunctionDecl = 0; |
| 431 | MsgSendFpretFunctionDecl = 0; |
| 432 | GetClassFunctionDecl = 0; |
| 433 | GetMetaClassFunctionDecl = 0; |
| 434 | SelGetUidFunctionDecl = 0; |
| 435 | CFStringFunctionDecl = 0; |
| 436 | GetProtocolFunctionDecl = 0; |
| 437 | ConstantStringClassReference = 0; |
| 438 | NSStringRecord = 0; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 439 | CurMethodDef = 0; |
| 440 | CurFunctionDef = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 441 | GlobalVarDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 442 | SuperStructDecl = 0; |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 443 | ConstantStringDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 444 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 445 | SuperContructorFunctionDecl = 0; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 446 | NumObjCStringLiterals = 0; |
Steve Naroff | 68272b8 | 2008-12-08 20:01:41 +0000 | [diff] [blame] | 447 | PropParentMap = 0; |
| 448 | CurrentBody = 0; |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 449 | DisableReplaceStmt = false; |
| 450 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 451 | // Get the ID and start/end of the main file. |
| 452 | MainFileID = SM->getMainFileID(); |
| 453 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 454 | MainFileStart = MainBuf->getBufferStart(); |
| 455 | MainFileEnd = MainBuf->getBufferEnd(); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 457 | Rewrite.setSourceMgr(Context->getSourceManager()); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 459 | // declaring objc_selector outside the parameter list removes a silly |
| 460 | // scope related warning... |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 461 | if (IsHeader) |
| 462 | Preamble = "#pragma once\n"; |
| 463 | Preamble += "struct objc_selector; struct objc_class;\n"; |
| 464 | Preamble += "#ifndef OBJC_SUPER\n"; |
| 465 | Preamble += "struct objc_super { struct objc_object *object; "; |
| 466 | Preamble += "struct objc_object *superClass; "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 467 | if (LangOpts.Microsoft) { |
| 468 | // Add a constructor for creating temporary objects. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 469 | Preamble += "objc_super(struct objc_object *o, struct objc_object *s) : "; |
| 470 | Preamble += "object(o), superClass(s) {} "; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 471 | } |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 472 | Preamble += "};\n"; |
| 473 | Preamble += "#define OBJC_SUPER\n"; |
| 474 | Preamble += "#endif\n"; |
| 475 | Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 476 | Preamble += "typedef struct objc_object Protocol;\n"; |
| 477 | Preamble += "#define _REWRITER_typedef_Protocol\n"; |
| 478 | Preamble += "#endif\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 479 | if (LangOpts.Microsoft) { |
| 480 | Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; |
| 481 | Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; |
| 482 | } else |
| 483 | Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; |
| 484 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 485 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 486 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 487 | Preamble += "(struct objc_super *, 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_msgSend_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 489 | Preamble += "(struct objc_object *, 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_msgSendSuper_stret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 491 | Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 492 | Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 493 | Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 494 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 495 | Preamble += "(const char *);\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 496 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; |
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 void objc_exception_throw(struct objc_object *);\n"; |
| 499 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; |
| 500 | Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; |
| 501 | Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; |
| 502 | Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; |
Steve Naroff | 580ca78 | 2008-05-09 21:17:56 +0000 | [diff] [blame] | 503 | Preamble += "(struct objc_class *, struct objc_object *);\n"; |
Steve Naroff | 59f05a4 | 2008-07-16 18:58:11 +0000 | [diff] [blame] | 504 | // @synchronized hooks. |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 505 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; |
| 506 | Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; |
| 507 | Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 508 | Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 509 | Preamble += "struct __objcFastEnumerationState {\n\t"; |
| 510 | Preamble += "unsigned long state;\n\t"; |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 511 | Preamble += "void **itemsPtr;\n\t"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 512 | Preamble += "unsigned long *mutationsPtr;\n\t"; |
| 513 | Preamble += "unsigned long extra[5];\n};\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 514 | Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 515 | Preamble += "#define __FASTENUMERATIONSTATE\n"; |
| 516 | Preamble += "#endif\n"; |
| 517 | Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; |
| 518 | Preamble += "struct __NSConstantStringImpl {\n"; |
| 519 | Preamble += " int *isa;\n"; |
| 520 | Preamble += " int flags;\n"; |
| 521 | Preamble += " char *str;\n"; |
| 522 | Preamble += " long length;\n"; |
| 523 | Preamble += "};\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 524 | Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; |
| 525 | Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; |
| 526 | Preamble += "#else\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 527 | Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; |
Steve Naroff | 88bee74 | 2008-08-05 20:04:48 +0000 | [diff] [blame] | 528 | Preamble += "#endif\n"; |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 529 | Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; |
| 530 | Preamble += "#endif\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 531 | // Blocks preamble. |
| 532 | Preamble += "#ifndef BLOCK_IMPL\n"; |
| 533 | Preamble += "#define BLOCK_IMPL\n"; |
| 534 | Preamble += "struct __block_impl {\n"; |
| 535 | Preamble += " void *isa;\n"; |
| 536 | Preamble += " int Flags;\n"; |
| 537 | Preamble += " int Size;\n"; |
| 538 | Preamble += " void *FuncPtr;\n"; |
| 539 | Preamble += "};\n"; |
| 540 | Preamble += "enum {\n"; |
| 541 | Preamble += " BLOCK_HAS_COPY_DISPOSE = (1<<25),\n"; |
| 542 | Preamble += " BLOCK_IS_GLOBAL = (1<<28)\n"; |
| 543 | Preamble += "};\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 544 | Preamble += "// Runtime copy/destroy helper functions\n"; |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 545 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_copy_assign(void *, void *);\n"; |
| 546 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_byref_assign_copy(void *, void *);\n"; |
| 547 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_destroy(void *);\n"; |
| 548 | Preamble += "__OBJC_RW_STATICIMPORT void _Block_byref_release(void *);\n"; |
| 549 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteGlobalBlock;\n"; |
| 550 | Preamble += "__OBJC_RW_STATICIMPORT void *_NSConcreteStackBlock;\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 551 | Preamble += "#endif\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 552 | if (LangOpts.Microsoft) { |
Steve Naroff | 4ebd716 | 2008-12-08 17:30:33 +0000 | [diff] [blame] | 553 | Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; |
| 554 | Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; |
Steve Naroff | a48396e | 2008-10-27 18:50:14 +0000 | [diff] [blame] | 555 | Preamble += "#define __attribute__(X)\n"; |
| 556 | } |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 560 | //===----------------------------------------------------------------------===// |
| 561 | // Top Level Driver Code |
| 562 | //===----------------------------------------------------------------------===// |
| 563 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 564 | void RewriteObjC::HandleTopLevelDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 565 | // Two cases: either the decl could be in the main file, or it could be in a |
| 566 | // #included file. If the former, rewrite it now. If the later, check to see |
| 567 | // if we rewrote the #include/#import. |
| 568 | SourceLocation Loc = D->getLocation(); |
| 569 | Loc = SM->getLogicalLoc(Loc); |
| 570 | |
| 571 | // If this is for a builtin, ignore it. |
| 572 | if (Loc.isInvalid()) return; |
| 573 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 574 | // Look for built-in declarations that we need to refer during the rewrite. |
| 575 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 576 | RewriteFunctionDecl(FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 577 | } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 578 | // declared in <Foundation/NSString.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 579 | if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 580 | ConstantStringClassReference = FVD; |
| 581 | return; |
| 582 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 583 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 584 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 585 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 586 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 587 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 588 | RewriteProtocolDecl(PD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 589 | } else if (ObjCForwardProtocolDecl *FP = |
| 590 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 591 | RewriteForwardProtocolDecl(FP); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 593 | // 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] | 594 | if (SM->isFromMainFile(Loc)) |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 595 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 598 | //===----------------------------------------------------------------------===// |
| 599 | // Syntactic (non-AST) Rewriting Code |
| 600 | //===----------------------------------------------------------------------===// |
| 601 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 602 | void RewriteObjC::RewriteInclude() { |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 603 | SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0); |
| 604 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 605 | const char *MainBufStart = MainBuf.first; |
| 606 | const char *MainBufEnd = MainBuf.second; |
| 607 | size_t ImportLen = strlen("import"); |
| 608 | size_t IncludeLen = strlen("include"); |
| 609 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 610 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 611 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 612 | if (*BufPtr == '#') { |
| 613 | if (++BufPtr == MainBufEnd) |
| 614 | return; |
| 615 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 616 | if (++BufPtr == MainBufEnd) |
| 617 | return; |
| 618 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 619 | // replace import with include |
| 620 | SourceLocation ImportLoc = |
| 621 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 622 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 623 | BufPtr += ImportLen; |
| 624 | } |
| 625 | } |
| 626 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 629 | void RewriteObjC::RewriteTabs() { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 630 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 631 | const char *MainBufStart = MainBuf.first; |
| 632 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 633 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 634 | // Loop over the whole file, looking for tabs. |
| 635 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 636 | if (*BufPtr != '\t') |
| 637 | continue; |
| 638 | |
| 639 | // Okay, we found a tab. This tab will turn into at least one character, |
| 640 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 641 | unsigned VCol = 0; |
| 642 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 643 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 644 | ++VCol; |
| 645 | |
| 646 | // Okay, now that we know the virtual column, we know how many spaces to |
| 647 | // insert. We assume 8-character tab-stops. |
| 648 | unsigned Spaces = 8-(VCol & 7); |
| 649 | |
| 650 | // Get the location of the tab. |
| 651 | SourceLocation TabLoc = |
| 652 | SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart); |
| 653 | |
| 654 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 655 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 659 | static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, |
| 660 | ObjCIvarDecl *OID) { |
| 661 | std::string S; |
| 662 | S = "((struct "; |
| 663 | S += ClassDecl->getIdentifier()->getName(); |
| 664 | S += "_IMPL *)self)->"; |
| 665 | S += OID->getNameAsCString(); |
| 666 | return S; |
| 667 | } |
| 668 | |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 669 | void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, |
| 670 | ObjCImplementationDecl *IMD, |
| 671 | ObjCCategoryImplDecl *CID) { |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 672 | SourceLocation startLoc = PID->getLocStart(); |
| 673 | InsertText(startLoc, "// ", 3); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 674 | const char *startBuf = SM->getCharacterData(startLoc); |
| 675 | assert((*startBuf == '@') && "bogus @synthesize location"); |
| 676 | const char *semiBuf = strchr(startBuf, ';'); |
| 677 | assert((*semiBuf == ';') && "@synthesize: can't find ';'"); |
| 678 | SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); |
| 679 | |
| 680 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 681 | return; // FIXME: is this correct? |
| 682 | |
| 683 | // Generate the 'getter' function. |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 684 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 685 | ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 686 | ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 687 | |
| 688 | if (!OID) |
| 689 | return; |
| 690 | |
| 691 | std::string Getr; |
| 692 | RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); |
| 693 | Getr += "{ "; |
| 694 | // Synthesize an explicit cast to gain access to the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 695 | // FIXME: deal with code generation implications for various property |
| 696 | // attributes (copy, retain, nonatomic). |
| 697 | // See objc-act.c:objc_synthesize_new_getter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 698 | Getr += "return " + getIvarAccessString(ClassDecl, OID); |
| 699 | Getr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 700 | InsertText(onePastSemiLoc, Getr.c_str(), Getr.size()); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 701 | |
| 702 | // Add the rewritten getter to trigger meta data generation. An alternate, and |
| 703 | // possibly cleaner approach is to hack RewriteObjCMethodsMetaData() to deal |
| 704 | // with properties explicitly. The following addInstanceMethod() required far |
| 705 | // less code change (and actually models what the rewriter is doing). |
| 706 | if (IMD) |
| 707 | IMD->addInstanceMethod(PD->getGetterMethodDecl()); |
| 708 | else |
| 709 | CID->addInstanceMethod(PD->getGetterMethodDecl()); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 710 | |
| 711 | if (PD->isReadOnly()) |
| 712 | return; |
| 713 | |
| 714 | // Generate the 'setter' function. |
| 715 | std::string Setr; |
| 716 | RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 717 | Setr += "{ "; |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 718 | // Synthesize an explicit cast to initialize the ivar. |
Steve Naroff | 3539cdb | 2008-12-02 17:54:50 +0000 | [diff] [blame] | 719 | // FIXME: deal with code generation implications for various property |
| 720 | // attributes (copy, retain, nonatomic). |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 721 | // See objc-act.c:objc_synthesize_new_setter() for details. |
Steve Naroff | dd2fdf1 | 2008-12-02 16:05:55 +0000 | [diff] [blame] | 722 | Setr += getIvarAccessString(ClassDecl, OID) + " = "; |
| 723 | Setr += OID->getNameAsCString(); |
| 724 | Setr += "; }"; |
Steve Naroff | eb0646c | 2008-12-02 15:48:25 +0000 | [diff] [blame] | 725 | InsertText(onePastSemiLoc, Setr.c_str(), Setr.size()); |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 726 | |
| 727 | // Add the rewritten setter to trigger meta data generation. |
| 728 | if (IMD) |
| 729 | IMD->addInstanceMethod(PD->getSetterMethodDecl()); |
| 730 | else |
| 731 | CID->addInstanceMethod(PD->getSetterMethodDecl()); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 732 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 733 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 734 | void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 735 | int numDecls = ClassDecl->getNumForwardDecls(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 736 | ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 737 | |
| 738 | // Get the start location and compute the semi location. |
| 739 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 740 | const char *startBuf = SM->getCharacterData(startLoc); |
| 741 | const char *semiPtr = strchr(startBuf, ';'); |
| 742 | |
| 743 | // Translate to typedef's that forward reference structs with the same name |
| 744 | // as the class. As a convenience, we include the original declaration |
| 745 | // as a comment. |
| 746 | std::string typedefString; |
| 747 | typedefString += "// "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 748 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 749 | typedefString += "\n"; |
| 750 | for (int i = 0; i < numDecls; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 751 | ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i]; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 752 | typedefString += "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 753 | typedefString += ForwardDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 754 | typedefString += "\n"; |
| 755 | typedefString += "#define _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"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 758 | typedefString += "typedef struct objc_object "; |
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#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 764 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 765 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 768 | void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 769 | SourceLocation LocStart = Method->getLocStart(); |
| 770 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 771 | |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 772 | if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) { |
Steve Naroff | 94ac21e | 2008-10-21 13:37:27 +0000 | [diff] [blame] | 773 | InsertText(LocStart, "#if 0\n", 6); |
| 774 | ReplaceText(LocEnd, 1, ";\n#endif\n", 9); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 775 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 776 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 780 | void RewriteObjC::RewriteProperties(unsigned nProperties, ObjCPropertyDecl **Properties) |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 781 | { |
Chris Lattner | 55d13b4 | 2008-03-16 21:23:50 +0000 | [diff] [blame] | 782 | for (unsigned i = 0; i < nProperties; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 783 | ObjCPropertyDecl *Property = Properties[i]; |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 784 | SourceLocation Loc = Property->getLocation(); |
| 785 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 786 | ReplaceText(Loc, 0, "// ", 3); |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 787 | |
| 788 | // FIXME: handle properties that are declared across multiple lines. |
| 789 | } |
| 790 | } |
| 791 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 792 | void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 793 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 794 | |
| 795 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 796 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 797 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 798 | for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 799 | E = CatDecl->instmeth_end(); I != E; ++I) |
| 800 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 801 | for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 802 | E = CatDecl->classmeth_end(); I != E; ++I) |
| 803 | RewriteMethodDeclaration(*I); |
| 804 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 805 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 806 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 809 | void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 810 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 811 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 812 | SourceLocation LocStart = PDecl->getLocStart(); |
| 813 | |
| 814 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 815 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 816 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 817 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 818 | E = PDecl->instmeth_end(); I != E; ++I) |
| 819 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 820 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 821 | E = PDecl->classmeth_end(); I != E; ++I) |
| 822 | RewriteMethodDeclaration(*I); |
| 823 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 824 | // Lastly, comment out the @end. |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 825 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 826 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 827 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 828 | // Must comment out @optional/@required |
| 829 | const char *startBuf = SM->getCharacterData(LocStart); |
| 830 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 831 | for (const char *p = startBuf; p < endBuf; p++) { |
| 832 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 833 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 834 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 835 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 836 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 837 | |
| 838 | } |
| 839 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 840 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 841 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 842 | ReplaceText(OptionalLoc, strlen("@required"), |
| 843 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 844 | |
| 845 | } |
| 846 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 849 | void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 850 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 851 | if (LocStart.isInvalid()) |
| 852 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 853 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 854 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 857 | void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 858 | std::string &ResultStr) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 859 | //fprintf(stderr,"In RewriteObjCMethodDecl\n"); |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 860 | const FunctionType *FPRetType = 0; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 861 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 862 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 863 | ResultStr += "id"; |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 864 | else if (OMD->getResultType()->isFunctionPointerType()) { |
| 865 | // needs special handling, since pointer-to-functions have special |
| 866 | // syntax (where a decaration models use). |
| 867 | QualType retType = OMD->getResultType(); |
| 868 | if (const PointerType* PT = retType->getAsPointerType()) { |
| 869 | QualType PointeeTy = PT->getPointeeType(); |
| 870 | if ((FPRetType = PointeeTy->getAsFunctionType())) { |
| 871 | ResultStr += FPRetType->getResultType().getAsString(); |
| 872 | ResultStr += "(*"; |
| 873 | } |
| 874 | } |
| 875 | } else |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 876 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 877 | ResultStr += " "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 878 | |
| 879 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 880 | std::string NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 881 | |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 882 | if (OMD->isInstance()) |
| 883 | NameStr += "_I_"; |
| 884 | else |
| 885 | NameStr += "_C_"; |
| 886 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 887 | NameStr += OMD->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 888 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 889 | |
| 890 | NamedDecl *MethodContext = OMD->getMethodContext(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 891 | if (ObjCCategoryImplDecl *CID = |
| 892 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 893 | NameStr += CID->getNameAsString(); |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 894 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 895 | } |
Steve Naroff | 563b828 | 2008-04-04 22:23:44 +0000 | [diff] [blame] | 896 | // Append selector names, replacing ':' with '_' |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 897 | { |
| 898 | std::string selString = OMD->getSelector().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 899 | int len = selString.size(); |
| 900 | for (int i = 0; i < len; i++) |
| 901 | if (selString[i] == ':') |
| 902 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 903 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 904 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 905 | // Remember this name for metadata emission |
| 906 | MethodInternalNames[OMD] = NameStr; |
| 907 | ResultStr += NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 908 | |
| 909 | // Rewrite arguments |
| 910 | ResultStr += "("; |
| 911 | |
| 912 | // invisible arguments |
| 913 | if (OMD->isInstance()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 914 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 915 | selfTy = Context->getPointerType(selfTy); |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 916 | if (!LangOpts.Microsoft) { |
| 917 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
| 918 | ResultStr += "struct "; |
| 919 | } |
| 920 | // When rewriting for Microsoft, explicitly omit the structure name. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 921 | ResultStr += OMD->getClassInterface()->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 922 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 923 | } |
| 924 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 925 | ResultStr += Context->getObjCIdType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 926 | |
| 927 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 928 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 929 | ResultStr += " _cmd"; |
| 930 | |
| 931 | // Method arguments. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 932 | for (unsigned i = 0; i < OMD->getNumParams(); i++) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 933 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
| 934 | ResultStr += ", "; |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 935 | if (PDecl->getType()->isObjCQualifiedIdType()) { |
| 936 | ResultStr += "id "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 937 | ResultStr += PDecl->getNameAsString(); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 938 | } else { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 939 | std::string Name = PDecl->getNameAsString(); |
Steve Naroff | c8ad87b | 2008-10-30 14:45:29 +0000 | [diff] [blame] | 940 | if (isBlockPointerType(PDecl->getType())) { |
| 941 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 942 | const BlockPointerType *BPT = PDecl->getType()->getAsBlockPointerType(); |
| 943 | Context->getPointerType(BPT->getPointeeType()).getAsStringInternal(Name); |
| 944 | } else |
| 945 | PDecl->getType().getAsStringInternal(Name); |
Steve Naroff | 543409e | 2008-04-18 21:13:19 +0000 | [diff] [blame] | 946 | ResultStr += Name; |
| 947 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 948 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 949 | if (OMD->isVariadic()) |
| 950 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 951 | ResultStr += ") "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 952 | |
Steve Naroff | 76e429d | 2008-07-16 14:40:40 +0000 | [diff] [blame] | 953 | if (FPRetType) { |
| 954 | ResultStr += ")"; // close the precedence "scope" for "*". |
| 955 | |
| 956 | // Now, emit the argument types (if any). |
| 957 | if (const FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(FPRetType)) { |
| 958 | ResultStr += "("; |
| 959 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { |
| 960 | if (i) ResultStr += ", "; |
| 961 | std::string ParamStr = FT->getArgType(i).getAsString(); |
| 962 | ResultStr += ParamStr; |
| 963 | } |
| 964 | if (FT->isVariadic()) { |
| 965 | if (FT->getNumArgs()) ResultStr += ", "; |
| 966 | ResultStr += "..."; |
| 967 | } |
| 968 | ResultStr += ")"; |
| 969 | } else { |
| 970 | ResultStr += "()"; |
| 971 | } |
| 972 | } |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 973 | } |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 974 | void RewriteObjC::RewriteImplementationDecl(NamedDecl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 975 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 976 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 977 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 978 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 979 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 980 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 981 | InsertText(CID->getLocStart(), "// ", 3); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 982 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 983 | for (ObjCCategoryImplDecl::instmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 984 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 985 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 986 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 987 | ObjCMethodDecl *OMD = *I; |
| 988 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 989 | SourceLocation LocStart = OMD->getLocStart(); |
| 990 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 991 | |
| 992 | const char *startBuf = SM->getCharacterData(LocStart); |
| 993 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 994 | ReplaceText(LocStart, endBuf-startBuf, |
| 995 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 998 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 999 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 1000 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1001 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1002 | ObjCMethodDecl *OMD = *I; |
| 1003 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1004 | SourceLocation LocStart = OMD->getLocStart(); |
| 1005 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 1006 | |
| 1007 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1008 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1009 | ReplaceText(LocStart, endBuf-startBuf, |
| 1010 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1011 | } |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1012 | for (ObjCCategoryImplDecl::propimpl_iterator |
| 1013 | I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), |
| 1014 | E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); I != E; ++I) { |
Steve Naroff | a0876e8 | 2008-12-02 17:36:43 +0000 | [diff] [blame] | 1015 | RewritePropertyImplDecl(*I, IMD, CID); |
Steve Naroff | d40910b | 2008-12-01 20:33:01 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1018 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1019 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 1020 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1021 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1024 | void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1025 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1026 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1027 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 1028 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1029 | ResultStr += ClassDecl->getNameAsString(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1030 | ResultStr += "\n"; |
| 1031 | ResultStr += "#define _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"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 1034 | ResultStr += "typedef struct objc_object "; |
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#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1037 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1038 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 1039 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1040 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 1041 | |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 1042 | RewriteProperties(ClassDecl->getNumPropertyDecl(), |
| 1043 | ClassDecl->getPropertyDecl()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1044 | for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1045 | E = ClassDecl->instmeth_end(); I != E; ++I) |
| 1046 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1047 | for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1048 | E = ClassDecl->classmeth_end(); I != E; ++I) |
| 1049 | RewriteMethodDeclaration(*I); |
| 1050 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 1051 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1052 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1055 | Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, |
| 1056 | SourceRange SrcRange) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1057 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1058 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1059 | ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); |
| 1060 | ObjCMessageExpr *MsgExpr; |
| 1061 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1062 | llvm::SmallVector<Expr *, 1> ExprVec; |
| 1063 | ExprVec.push_back(newStmt); |
| 1064 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1065 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1066 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1067 | if (PRE && PropGetters[PRE]) { |
| 1068 | // This allows us to handle chain/nested property getters. |
| 1069 | Receiver = PropGetters[PRE]; |
| 1070 | } |
| 1071 | MsgExpr = new ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1072 | PDecl->getSetterName(), PDecl->getType(), |
| 1073 | PDecl->getSetterMethodDecl(), |
| 1074 | SourceLocation(), SourceLocation(), |
| 1075 | &ExprVec[0], 1); |
| 1076 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
| 1077 | |
| 1078 | // Now do the actual rewrite. |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 1079 | ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); |
Steve Naroff | e58ee0c | 2008-12-10 14:53:27 +0000 | [diff] [blame] | 1080 | //delete BinOp; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1081 | delete MsgExpr; |
| 1082 | return ReplacingStmt; |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 1085 | Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1086 | // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. |
| 1087 | // This allows us to reuse all the fun and games in SynthMessageExpr(). |
| 1088 | ObjCMessageExpr *MsgExpr; |
| 1089 | ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); |
| 1090 | |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1091 | Stmt *Receiver = PropRefExpr->getBase(); |
| 1092 | |
| 1093 | ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); |
| 1094 | if (PRE && PropGetters[PRE]) { |
| 1095 | // This allows us to handle chain/nested property getters. |
| 1096 | Receiver = PropGetters[PRE]; |
| 1097 | } |
| 1098 | MsgExpr = new ObjCMessageExpr(dyn_cast<Expr>(Receiver), |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1099 | PDecl->getGetterName(), PDecl->getType(), |
| 1100 | PDecl->getGetterMethodDecl(), |
| 1101 | SourceLocation(), SourceLocation(), |
| 1102 | 0, 0); |
| 1103 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1104 | Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 1105 | |
| 1106 | if (!PropParentMap) |
| 1107 | PropParentMap = new ParentMap(CurrentBody); |
| 1108 | |
| 1109 | Stmt *Parent = PropParentMap->getParent(PropRefExpr); |
| 1110 | if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { |
| 1111 | // We stash away the ReplacingStmt since actually doing the |
| 1112 | // replacement/rewrite won't work for nested getters (e.g. obj.p.i) |
| 1113 | PropGetters[PropRefExpr] = ReplacingStmt; |
| 1114 | delete MsgExpr; |
| 1115 | return PropRefExpr; // return the original... |
| 1116 | } else { |
| 1117 | ReplaceStmt(PropRefExpr, ReplacingStmt); |
| 1118 | // delete PropRefExpr; elsewhere... |
| 1119 | delete MsgExpr; |
| 1120 | return ReplacingStmt; |
| 1121 | } |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1124 | Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, |
| 1125 | SourceLocation OrigStart) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1126 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1127 | if (CurMethodDef) { |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 1128 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1129 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
| 1130 | // lookup which class implements the instance variable. |
| 1131 | ObjCInterfaceDecl *clsDeclared = 0; |
| 1132 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
| 1133 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1134 | |
| 1135 | // Synthesize an explicit cast to gain access to the ivar. |
| 1136 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1137 | RecName += "_IMPL"; |
| 1138 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1139 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1140 | SourceLocation(), II); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1141 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1142 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1143 | CastExpr *castExpr = new CStyleCastExpr(castT, IV->getBase(), castT, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 1144 | SourceLocation(), SourceLocation()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1145 | // Don't forget the parens to enforce the proper binding. |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1146 | ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), |
| 1147 | IV->getBase()->getLocEnd(), |
| 1148 | castExpr); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1149 | if (IV->isFreeIvar() && |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 1150 | CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1151 | MemberExpr *ME = new MemberExpr(PE, true, D, IV->getLocation(), |
| 1152 | D->getType()); |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1153 | ReplaceStmt(IV, ME); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1154 | // delete IV; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1155 | return ME; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1156 | } |
Chris Lattner | 3b2c58c | 2008-05-23 20:40:52 +0000 | [diff] [blame] | 1157 | |
| 1158 | ReplaceStmt(IV->getBase(), PE); |
| 1159 | // Cannot delete IV->getBase(), since PE points to it. |
| 1160 | // Replace the old base with the cast. This is important when doing |
| 1161 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1162 | IV->setBase(PE); |
| 1163 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1164 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1165 | } else { // we are outside a method. |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1166 | assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); |
| 1167 | |
| 1168 | // Explicit ivar refs need to have a cast inserted. |
| 1169 | // FIXME: consider sharing some of this code with the code above. |
| 1170 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1171 | ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1172 | // lookup which class implements the instance variable. |
| 1173 | ObjCInterfaceDecl *clsDeclared = 0; |
Steve Naroff | f075761 | 2008-05-08 17:52:16 +0000 | [diff] [blame] | 1174 | iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1175 | assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); |
| 1176 | |
| 1177 | // Synthesize an explicit cast to gain access to the ivar. |
| 1178 | std::string RecName = clsDeclared->getIdentifier()->getName(); |
| 1179 | RecName += "_IMPL"; |
| 1180 | IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1181 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1182 | SourceLocation(), II); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1183 | assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); |
| 1184 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1185 | CastExpr *castExpr = new CStyleCastExpr(castT, IV->getBase(), castT, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 1186 | SourceLocation(), SourceLocation()); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1187 | // Don't forget the parens to enforce the proper binding. |
Chris Lattner | 8d36616 | 2008-05-28 16:38:23 +0000 | [diff] [blame] | 1188 | ParenExpr *PE = new ParenExpr(IV->getBase()->getLocStart(), |
| 1189 | IV->getBase()->getLocEnd(), castExpr); |
Steve Naroff | 9f52597 | 2008-05-06 23:20:07 +0000 | [diff] [blame] | 1190 | ReplaceStmt(IV->getBase(), PE); |
| 1191 | // Cannot delete IV->getBase(), since PE points to it. |
| 1192 | // Replace the old base with the cast. This is important when doing |
| 1193 | // embedded rewrites. For example, [newInv->_container addObject:0]. |
| 1194 | IV->setBase(PE); |
| 1195 | return IV; |
| 1196 | } |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 1197 | } |
Steve Naroff | 84472a8 | 2008-04-18 21:55:08 +0000 | [diff] [blame] | 1198 | return IV; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1201 | /// SynthCountByEnumWithState - To print: |
| 1202 | /// ((unsigned int (*) |
| 1203 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1204 | /// (void *)objc_msgSend)((id)l_collection, |
| 1205 | /// sel_registerName( |
| 1206 | /// "countByEnumeratingWithState:objects:count:"), |
| 1207 | /// &enumState, |
| 1208 | /// (id *)items, (unsigned int)16) |
| 1209 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1210 | void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1211 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 1212 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 1213 | buf += "\n\t\t"; |
| 1214 | buf += "((id)l_collection,\n\t\t"; |
| 1215 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 1216 | buf += "\n\t\t"; |
| 1217 | buf += "&enumState, " |
| 1218 | "(id *)items, (unsigned int)16)"; |
| 1219 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1220 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1221 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 1222 | /// statement to exit to its outer synthesized loop. |
| 1223 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1224 | Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1225 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1226 | return S; |
| 1227 | // replace break with goto __break_label |
| 1228 | std::string buf; |
| 1229 | |
| 1230 | SourceLocation startLoc = S->getLocStart(); |
| 1231 | buf = "goto __break_label_"; |
| 1232 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1233 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 1239 | /// statement to continue with its inner synthesized loop. |
| 1240 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1241 | Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1242 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 1243 | return S; |
| 1244 | // replace continue with goto __continue_label |
| 1245 | std::string buf; |
| 1246 | |
| 1247 | SourceLocation startLoc = S->getLocStart(); |
| 1248 | buf = "goto __continue_label_"; |
| 1249 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1250 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1255 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1256 | /// It rewrites: |
| 1257 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 1258 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1259 | /// Into: |
| 1260 | /// { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1261 | /// type elem; |
| 1262 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1263 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1264 | /// id l_collection = (id)collection; |
| 1265 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1266 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1267 | /// if (limit) { |
| 1268 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1269 | /// do { |
| 1270 | /// unsigned long counter = 0; |
| 1271 | /// do { |
| 1272 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1273 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1274 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1275 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1276 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1277 | /// } while (counter < limit); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1278 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1279 | /// objects:items count:16]); |
| 1280 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1281 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1282 | /// } |
| 1283 | /// else |
| 1284 | /// elem = nil; |
| 1285 | /// } |
| 1286 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1287 | Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1288 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1289 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 1290 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 1291 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 1292 | assert(!ObjCBcLabelNo.empty() && |
| 1293 | "ObjCForCollectionStmt - Label No stack empty"); |
| 1294 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1295 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1296 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1297 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1298 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1299 | std::string buf; |
| 1300 | buf = "\n{\n\t"; |
| 1301 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1302 | // type elem; |
Ted Kremenek | 1ed8e2a | 2008-10-06 22:16:13 +0000 | [diff] [blame] | 1303 | ScopedDecl* D = DS->getSolitaryDecl(); |
| 1304 | QualType ElementType = cast<ValueDecl>(D)->getType(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1305 | elementTypeAsString = ElementType.getAsString(); |
| 1306 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1307 | buf += " "; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1308 | elementName = D->getNameAsCString(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1309 | buf += elementName; |
| 1310 | buf += ";\n\t"; |
| 1311 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1312 | else { |
| 1313 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1314 | elementName = DR->getDecl()->getNameAsCString(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1315 | elementTypeAsString |
| 1316 | = cast<ValueDecl>(DR->getDecl())->getType().getAsString(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1317 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1318 | |
| 1319 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1320 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1321 | // id items[16]; |
| 1322 | buf += "id items[16];\n\t"; |
| 1323 | // id l_collection = (id) |
| 1324 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1325 | // Find start location of 'collection' the hard way! |
| 1326 | const char *startCollectionBuf = startBuf; |
| 1327 | startCollectionBuf += 3; // skip 'for' |
| 1328 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1329 | startCollectionBuf++; // skip '(' |
| 1330 | // find 'in' and skip it. |
| 1331 | while (*startCollectionBuf != ' ' || |
| 1332 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1333 | (*(startCollectionBuf+3) != ' ' && |
| 1334 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1335 | startCollectionBuf++; |
| 1336 | startCollectionBuf += 3; |
| 1337 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1338 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1339 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1340 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1341 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1342 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1343 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1344 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1345 | buf = ";\n\t"; |
| 1346 | |
| 1347 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1348 | // objects:items count:16]; |
| 1349 | // which is synthesized into: |
| 1350 | // unsigned int limit = |
| 1351 | // ((unsigned int (*) |
| 1352 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1353 | // (void *)objc_msgSend)((id)l_collection, |
| 1354 | // sel_registerName( |
| 1355 | // "countByEnumeratingWithState:objects:count:"), |
| 1356 | // (struct __objcFastEnumerationState *)&state, |
| 1357 | // (id *)items, (unsigned int)16); |
| 1358 | buf += "unsigned long limit =\n\t\t"; |
| 1359 | SynthCountByEnumWithState(buf); |
| 1360 | buf += ";\n\t"; |
| 1361 | /// if (limit) { |
| 1362 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1363 | /// do { |
| 1364 | /// unsigned long counter = 0; |
| 1365 | /// do { |
| 1366 | /// if (startMutations != *enumState.mutationsPtr) |
| 1367 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1368 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1369 | buf += "if (limit) {\n\t"; |
| 1370 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1371 | buf += "do {\n\t\t"; |
| 1372 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1373 | buf += "do {\n\t\t\t"; |
| 1374 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1375 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1376 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1377 | buf += " = ("; |
| 1378 | buf += elementTypeAsString; |
| 1379 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1380 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1381 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1382 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1383 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1384 | /// } while (counter < limit); |
| 1385 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1386 | /// objects:items count:16]); |
| 1387 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1388 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1389 | /// } |
| 1390 | /// else |
| 1391 | /// elem = nil; |
| 1392 | /// } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1393 | /// |
| 1394 | buf = ";\n\t"; |
| 1395 | buf += "__continue_label_"; |
| 1396 | buf += utostr(ObjCBcLabelNo.back()); |
| 1397 | buf += ": ;"; |
| 1398 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1399 | buf += "} while (counter < limit);\n\t"; |
| 1400 | buf += "} while (limit = "; |
| 1401 | SynthCountByEnumWithState(buf); |
| 1402 | buf += ");\n\t"; |
| 1403 | buf += elementName; |
| 1404 | buf += " = nil;\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1405 | buf += "__break_label_"; |
| 1406 | buf += utostr(ObjCBcLabelNo.back()); |
| 1407 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1408 | buf += "}\n\t"; |
| 1409 | buf += "else\n\t\t"; |
| 1410 | buf += elementName; |
| 1411 | buf += " = nil;\n"; |
| 1412 | buf += "}\n"; |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1413 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1414 | // Insert all these *after* the statement body. |
Steve Naroff | 600e4e8 | 2008-07-21 18:26:02 +0000 | [diff] [blame] | 1415 | if (isa<CompoundStmt>(S->getBody())) { |
| 1416 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
| 1417 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1418 | } else { |
| 1419 | /* Need to treat single statements specially. For example: |
| 1420 | * |
| 1421 | * for (A *a in b) if (stuff()) break; |
| 1422 | * for (A *a in b) xxxyy; |
| 1423 | * |
| 1424 | * The following code simply scans ahead to the semi to find the actual end. |
| 1425 | */ |
| 1426 | const char *stmtBuf = SM->getCharacterData(OrigEnd); |
| 1427 | const char *semiBuf = strchr(stmtBuf, ';'); |
| 1428 | assert(semiBuf && "Can't find ';'"); |
| 1429 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); |
| 1430 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
| 1431 | } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1432 | Stmts.pop_back(); |
| 1433 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1434 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1437 | /// RewriteObjCSynchronizedStmt - |
| 1438 | /// This routine rewrites @synchronized(expr) stmt; |
| 1439 | /// into: |
| 1440 | /// objc_sync_enter(expr); |
| 1441 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1442 | /// |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1443 | Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1444 | // Get the start location and compute the semi location. |
| 1445 | SourceLocation startLoc = S->getLocStart(); |
| 1446 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1447 | |
| 1448 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1449 | |
| 1450 | std::string buf; |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1451 | buf = "objc_sync_enter((id)"; |
| 1452 | const char *lparenBuf = startBuf; |
| 1453 | while (*lparenBuf != '(') lparenBuf++; |
| 1454 | ReplaceText(startLoc, lparenBuf-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1455 | // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since |
| 1456 | // the sync expression is typically a message expression that's already |
| 1457 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 1458 | SourceLocation endLoc = S->getSynchBody()->getLocStart(); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1459 | const char *endBuf = SM->getCharacterData(endLoc); |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1460 | while (*endBuf != ')') endBuf--; |
| 1461 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1462 | buf = ");\n"; |
| 1463 | // declare a new scope with two variables, _stack and _rethrow. |
| 1464 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1465 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1466 | buf += "char *pointers[4];} _stack;\n"; |
| 1467 | buf += "id volatile _rethrow = 0;\n"; |
| 1468 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1469 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1470 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1471 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1472 | startBuf = SM->getCharacterData(startLoc); |
| 1473 | |
Steve Naroff | c7089f1 | 2008-08-19 13:04:19 +0000 | [diff] [blame] | 1474 | assert((*startBuf == '}') && "bogus @synchronized block"); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1475 | SourceLocation lastCurlyLoc = startLoc; |
| 1476 | buf = "}\nelse {\n"; |
| 1477 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1478 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1479 | buf += " objc_sync_exit("; |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1480 | Expr *syncExpr = new CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1481 | S->getSynchExpr(), |
| 1482 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 1483 | SourceLocation(), SourceLocation()); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1484 | std::string syncExprBufS; |
| 1485 | llvm::raw_string_ostream syncExprBuf(syncExprBufS); |
Steve Naroff | 3498cc9 | 2008-08-21 13:03:03 +0000 | [diff] [blame] | 1486 | syncExpr->printPretty(syncExprBuf); |
Steve Naroff | b2a3945 | 2008-07-16 19:47:39 +0000 | [diff] [blame] | 1487 | buf += syncExprBuf.str(); |
| 1488 | buf += ");\n"; |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1489 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1490 | buf += "}\n"; |
| 1491 | buf += "}"; |
| 1492 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1493 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1494 | return 0; |
| 1495 | } |
| 1496 | |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1497 | void RewriteObjC::WarnAboutReturnGotoContinueOrBreakStmts(Stmt *S) { |
| 1498 | // Perform a bottom up traversal of all children. |
| 1499 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 1500 | CI != E; ++CI) |
| 1501 | if (*CI) |
| 1502 | WarnAboutReturnGotoContinueOrBreakStmts(*CI); |
| 1503 | |
| 1504 | if (isa<ReturnStmt>(S) || isa<ContinueStmt>(S) || |
| 1505 | isa<BreakStmt>(S) || isa<GotoStmt>(S)) { |
| 1506 | Diags.Report(Context->getFullLoc(S->getLocStart()), |
| 1507 | TryFinallyContainsReturnDiag); |
| 1508 | } |
| 1509 | return; |
| 1510 | } |
| 1511 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1512 | Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1513 | // Get the start location and compute the semi location. |
| 1514 | SourceLocation startLoc = S->getLocStart(); |
| 1515 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1516 | |
| 1517 | assert((*startBuf == '@') && "bogus @try location"); |
| 1518 | |
| 1519 | std::string buf; |
| 1520 | // declare a new scope with two variables, _stack and _rethrow. |
| 1521 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1522 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1523 | buf += "char *pointers[4];} _stack;\n"; |
| 1524 | buf += "id volatile _rethrow = 0;\n"; |
| 1525 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1526 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1528 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1529 | |
| 1530 | startLoc = S->getTryBody()->getLocEnd(); |
| 1531 | startBuf = SM->getCharacterData(startLoc); |
| 1532 | |
| 1533 | assert((*startBuf == '}') && "bogus @try block"); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1534 | |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1535 | SourceLocation lastCurlyLoc = startLoc; |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1536 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
| 1537 | if (catchList) { |
| 1538 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1539 | buf = " /* @catch begin */ else {\n"; |
| 1540 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1541 | buf += " objc_exception_try_enter (&_stack);\n"; |
| 1542 | buf += " if (_setjmp(_stack.buf))\n"; |
| 1543 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1544 | buf += " else { /* @catch continue */"; |
| 1545 | |
| 1546 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 8bd3dc6 | 2008-09-09 19:59:12 +0000 | [diff] [blame] | 1547 | } else { /* no catch list */ |
| 1548 | buf = "}\nelse {\n"; |
| 1549 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1550 | buf += "}"; |
| 1551 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | c9ba172 | 2008-07-16 15:31:30 +0000 | [diff] [blame] | 1552 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1553 | bool sawIdTypedCatch = false; |
| 1554 | Stmt *lastCatchBody = 0; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1555 | while (catchList) { |
| 1556 | Stmt *catchStmt = catchList->getCatchParamStmt(); |
| 1557 | |
| 1558 | if (catchList == S->getCatchStmts()) |
| 1559 | buf = "if ("; // we are generating code for the first catch clause |
| 1560 | else |
| 1561 | buf = "else if ("; |
| 1562 | startLoc = catchList->getLocStart(); |
| 1563 | startBuf = SM->getCharacterData(startLoc); |
| 1564 | |
| 1565 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1566 | |
| 1567 | const char *lParenLoc = strchr(startBuf, '('); |
| 1568 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1569 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1570 | // Now rewrite the body... |
| 1571 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1572 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1573 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1574 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1575 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1576 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1577 | |
| 1578 | buf += "1) { id _tmp = _caught;"; |
| 1579 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1580 | buf.c_str(), buf.size()); |
| 1581 | } else if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) { |
Ted Kremenek | 50a25e2 | 2008-10-06 22:39:38 +0000 | [diff] [blame] | 1582 | QualType t = dyn_cast<ValueDecl>(declStmt->getSolitaryDecl())->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1583 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1584 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1585 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1586 | sawIdTypedCatch = true; |
| 1587 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1588 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1589 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1590 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1591 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1592 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1593 | buf += cls->getDecl()->getNameAsString(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1594 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1595 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | // Now rewrite the body... |
| 1599 | lastCatchBody = catchList->getCatchBody(); |
| 1600 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1601 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1602 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1603 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1604 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1605 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1606 | |
| 1607 | buf = " = _caught;"; |
| 1608 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1609 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1610 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1611 | } else if (!isa<NullStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1612 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1613 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1614 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1615 | catchList = catchList->getNextCatchStmt(); |
| 1616 | } |
| 1617 | // Complete the catch list... |
| 1618 | if (lastCatchBody) { |
| 1619 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1620 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1621 | "bogus @catch body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1622 | |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1623 | // Insert the last (implicit) else clause *before* the right curly brace. |
| 1624 | bodyLoc = bodyLoc.getFileLocWithOffset(-1); |
| 1625 | buf = "} /* last catch end */\n"; |
| 1626 | buf += "else {\n"; |
| 1627 | buf += " _rethrow = _caught;\n"; |
| 1628 | buf += " objc_exception_try_exit(&_stack);\n"; |
| 1629 | buf += "} } /* @catch end */\n"; |
| 1630 | if (!S->getFinallyStmt()) |
| 1631 | buf += "}\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1632 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Set lastCurlyLoc |
| 1635 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1636 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1637 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1638 | startLoc = finalStmt->getLocStart(); |
| 1639 | startBuf = SM->getCharacterData(startLoc); |
| 1640 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1641 | |
| 1642 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1643 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1644 | |
| 1645 | Stmt *body = finalStmt->getFinallyBody(); |
| 1646 | SourceLocation startLoc = body->getLocStart(); |
| 1647 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame] | 1648 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1649 | "bogus @finally body location"); |
| 1650 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1651 | "bogus @finally body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1652 | |
| 1653 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1654 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1655 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1656 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1657 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1658 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1659 | |
| 1660 | // Set lastCurlyLoc |
| 1661 | lastCurlyLoc = body->getLocEnd(); |
Steve Naroff | 8c56515 | 2008-12-05 17:03:39 +0000 | [diff] [blame] | 1662 | |
| 1663 | // Now check for any return/continue/go statements within the @try. |
| 1664 | WarnAboutReturnGotoContinueOrBreakStmts(S->getTryBody()); |
Steve Naroff | 378f47a | 2008-09-11 15:29:03 +0000 | [diff] [blame] | 1665 | } else { /* no finally clause - make sure we synthesize an implicit one */ |
| 1666 | buf = "{ /* implicit finally clause */\n"; |
| 1667 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1668 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1669 | buf += "}"; |
| 1670 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1671 | } |
| 1672 | // Now emit the final closing curly brace... |
| 1673 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1674 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1675 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1676 | return 0; |
| 1677 | } |
| 1678 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1679 | Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1680 | return 0; |
| 1681 | } |
| 1682 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1683 | Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1684 | return 0; |
| 1685 | } |
| 1686 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1687 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1688 | // the throw expression is typically a message expression that's already |
| 1689 | // been rewritten! (which implies the SourceLocation's are invalid). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1690 | Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1691 | // Get the start location and compute the semi location. |
| 1692 | SourceLocation startLoc = S->getLocStart(); |
| 1693 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1694 | |
| 1695 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1696 | |
| 1697 | std::string buf; |
| 1698 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1699 | if (S->getThrowExpr()) |
| 1700 | buf = "objc_exception_throw("; |
| 1701 | else // add an implicit argument |
| 1702 | buf = "objc_exception_throw(_caught"; |
Steve Naroff | 4ba0acb | 2008-07-25 15:41:30 +0000 | [diff] [blame] | 1703 | |
| 1704 | // handle "@ throw" correctly. |
| 1705 | const char *wBuf = strchr(startBuf, 'w'); |
| 1706 | assert((*wBuf == 'w') && "@throw: can't find 'w'"); |
| 1707 | ReplaceText(startLoc, wBuf-startBuf+1, buf.c_str(), buf.size()); |
| 1708 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1709 | const char *semiBuf = strchr(startBuf, ';'); |
| 1710 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1711 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1712 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1713 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1716 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1717 | Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1718 | // Create a new string expression. |
| 1719 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1720 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1721 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1722 | Expr *Replacement = new StringLiteral(StrEncoding.c_str(), |
| 1723 | StrEncoding.length(), false, StrType, |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1724 | SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1725 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1727 | // Replace this subexpr in the parent. |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1728 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1729 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1732 | Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1733 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1734 | // Create a call to sel_registerName("selName"). |
| 1735 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1736 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1737 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getAsString().c_str(), |
| 1738 | Exp->getSelector().getAsString().size(), |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1739 | false, argType, SourceLocation(), |
| 1740 | SourceLocation())); |
| 1741 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1742 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1743 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 1744 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1745 | return SelExp; |
| 1746 | } |
| 1747 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1748 | CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1749 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1750 | // 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] | 1751 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1752 | |
| 1753 | // Create a reference to the objc_msgSend() declaration. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1754 | DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1755 | |
| 1756 | // 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] | 1757 | QualType pToFunc = Context->getPointerType(msgSendType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1758 | ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE, |
| 1759 | /*isLvalue=*/false); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1760 | |
| 1761 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1762 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1763 | return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); |
| 1764 | } |
| 1765 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1766 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1767 | const char *&startRef, const char *&endRef) { |
| 1768 | while (startBuf < endBuf) { |
| 1769 | if (*startBuf == '<') |
| 1770 | startRef = startBuf; // mark the start. |
| 1771 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1772 | if (startRef && *startRef == '<') { |
| 1773 | endRef = startBuf; // mark the end. |
| 1774 | return true; |
| 1775 | } |
| 1776 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1777 | } |
| 1778 | startBuf++; |
| 1779 | } |
| 1780 | return false; |
| 1781 | } |
| 1782 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1783 | static void scanToNextArgument(const char *&argRef) { |
| 1784 | int angle = 0; |
| 1785 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1786 | if (*argRef == '<') |
| 1787 | angle++; |
| 1788 | else if (*argRef == '>') |
| 1789 | angle--; |
| 1790 | argRef++; |
| 1791 | } |
| 1792 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1793 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1794 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1795 | bool RewriteObjC::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1796 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1797 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1798 | return true; |
| 1799 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1800 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1801 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1802 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1803 | return true; // we have "Class <Protocol> *". |
| 1804 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1805 | return false; |
| 1806 | } |
| 1807 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1808 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { |
| 1809 | QualType Type = E->getType(); |
| 1810 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1811 | SourceLocation Loc, EndLoc; |
| 1812 | |
| 1813 | if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { |
| 1814 | Loc = ECE->getLParenLoc(); |
| 1815 | EndLoc = ECE->getRParenLoc(); |
| 1816 | } else { |
| 1817 | Loc = E->getLocStart(); |
| 1818 | EndLoc = E->getLocEnd(); |
| 1819 | } |
| 1820 | // This will defend against trying to rewrite synthesized expressions. |
| 1821 | if (Loc.isInvalid() || EndLoc.isInvalid()) |
| 1822 | return; |
| 1823 | |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1824 | const char *startBuf = SM->getCharacterData(Loc); |
Steve Naroff | cda658e | 2008-11-19 21:15:47 +0000 | [diff] [blame] | 1825 | const char *endBuf = SM->getCharacterData(EndLoc); |
Steve Naroff | 4f95b75 | 2008-07-29 18:15:38 +0000 | [diff] [blame] | 1826 | const char *startRef = 0, *endRef = 0; |
| 1827 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1828 | // Get the locations of the startRef, endRef. |
| 1829 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); |
| 1830 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); |
| 1831 | // Comment out the protocol references. |
| 1832 | InsertText(LessLoc, "/*", 2); |
| 1833 | InsertText(GreaterLoc, "*/", 2); |
| 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1838 | void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1839 | SourceLocation Loc; |
| 1840 | QualType Type; |
| 1841 | const FunctionTypeProto *proto = 0; |
| 1842 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1843 | Loc = VD->getLocation(); |
| 1844 | Type = VD->getType(); |
| 1845 | } |
| 1846 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1847 | Loc = FD->getLocation(); |
| 1848 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1849 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1850 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1851 | assert(funcType && "missing function type"); |
| 1852 | proto = dyn_cast<FunctionTypeProto>(funcType); |
| 1853 | if (!proto) |
| 1854 | return; |
| 1855 | Type = proto->getResultType(); |
| 1856 | } |
| 1857 | else |
| 1858 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1859 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1860 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1861 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1862 | |
| 1863 | const char *endBuf = SM->getCharacterData(Loc); |
| 1864 | const char *startBuf = endBuf; |
Steve Naroff | 6cafbf2 | 2008-05-31 05:02:17 +0000 | [diff] [blame] | 1865 | while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1866 | startBuf--; // scan backward (from the decl location) for return type. |
| 1867 | const char *startRef = 0, *endRef = 0; |
| 1868 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1869 | // Get the locations of the startRef, endRef. |
| 1870 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1871 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1872 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1873 | InsertText(LessLoc, "/*", 2); |
| 1874 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1875 | } |
| 1876 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1877 | if (!proto) |
| 1878 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1879 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1880 | const char *startBuf = SM->getCharacterData(Loc); |
| 1881 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1882 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1883 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1884 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1885 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1886 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1887 | // scan forward (from the decl location) for argument types. |
| 1888 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1889 | const char *startRef = 0, *endRef = 0; |
| 1890 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1891 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1892 | SourceLocation LessLoc = |
| 1893 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1894 | SourceLocation GreaterLoc = |
| 1895 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1896 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1897 | InsertText(LessLoc, "/*", 2); |
| 1898 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1899 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1900 | startBuf = ++endBuf; |
| 1901 | } |
| 1902 | else { |
Steve Naroff | aba49d1 | 2008-08-06 15:58:23 +0000 | [diff] [blame] | 1903 | // If the function name is derived from a macro expansion, then the |
| 1904 | // argument buffer will not follow the name. Need to speak with Chris. |
| 1905 | while (*startBuf && *startBuf != ')' && *startBuf != ',') |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1906 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1907 | startBuf++; |
| 1908 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1909 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1912 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1913 | void RewriteObjC::SynthSelGetUidFunctionDecl() { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1914 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1915 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1916 | ArgTys.push_back(Context->getPointerType( |
| 1917 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1918 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1919 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1920 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1921 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1922 | SourceLocation(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1923 | SelGetUidIdent, getFuncType, |
| 1924 | FunctionDecl::Extern, false, 0); |
| 1925 | } |
| 1926 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1927 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1928 | void RewriteObjC::SynthGetProtocolFunctionDecl() { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1929 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1930 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1931 | ArgTys.push_back(Context->getPointerType( |
| 1932 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1933 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1934 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1935 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1936 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1937 | SourceLocation(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1938 | SelGetProtoIdent, getFuncType, |
| 1939 | FunctionDecl::Extern, false, 0); |
| 1940 | } |
| 1941 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1942 | void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1943 | // declared in <objc/objc.h> |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1944 | if (strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1945 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1948 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1951 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1952 | void RewriteObjC::SynthSuperContructorFunctionDecl() { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1953 | if (SuperContructorFunctionDecl) |
| 1954 | return; |
| 1955 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_super"); |
| 1956 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1957 | QualType argT = Context->getObjCIdType(); |
| 1958 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1959 | ArgTys.push_back(argT); |
| 1960 | ArgTys.push_back(argT); |
| 1961 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1962 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1963 | false, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1964 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1965 | SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1966 | msgSendIdent, msgSendType, |
| 1967 | FunctionDecl::Extern, false, 0); |
| 1968 | } |
| 1969 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1970 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1971 | void RewriteObjC::SynthMsgSendFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1972 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1973 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1974 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1975 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1976 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1977 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1978 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1979 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1980 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1981 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 1982 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 1983 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1984 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1985 | msgSendIdent, msgSendType, |
| 1986 | FunctionDecl::Extern, false, 0); |
| 1987 | } |
| 1988 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1989 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1990 | void RewriteObjC::SynthMsgSendSuperFunctionDecl() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1991 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 1992 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 1993 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1994 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 1995 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1996 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1997 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1998 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1999 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2000 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2001 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2002 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2003 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2004 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2005 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2006 | SourceLocation(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2007 | msgSendIdent, msgSendType, |
| 2008 | FunctionDecl::Extern, false, 0); |
| 2009 | } |
| 2010 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2011 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2012 | void RewriteObjC::SynthMsgSendStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2013 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 2014 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2015 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2016 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2017 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2018 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +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(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +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 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2025 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2026 | msgSendIdent, msgSendType, |
| 2027 | FunctionDecl::Extern, false, 0); |
| 2028 | } |
| 2029 | |
| 2030 | // SynthMsgSendSuperStretFunctionDecl - |
| 2031 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2032 | void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2033 | IdentifierInfo *msgSendIdent = |
| 2034 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 2035 | llvm::SmallVector<QualType, 16> ArgTys; |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2036 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2037 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2038 | &Context->Idents.get("objc_super")); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2039 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 2040 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 2041 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2042 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2043 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2044 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2045 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2046 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2047 | true /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2048 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 2049 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2050 | msgSendIdent, msgSendType, |
| 2051 | FunctionDecl::Extern, false, 0); |
| 2052 | } |
| 2053 | |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2054 | // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2055 | void RewriteObjC::SynthMsgSendFpretFunctionDecl() { |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2056 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 2057 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2058 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2059 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 2060 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2061 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2062 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 2063 | ArgTys.push_back(argT); |
Steve Naroff | 1284db8 | 2008-05-08 22:02:18 +0000 | [diff] [blame] | 2064 | QualType msgSendType = Context->getFunctionType(Context->DoubleTy, |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +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 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2068 | SourceLocation(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2069 | msgSendIdent, msgSendType, |
| 2070 | FunctionDecl::Extern, false, 0); |
| 2071 | } |
| 2072 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2073 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2074 | void RewriteObjC::SynthGetClassFunctionDecl() { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2075 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 2076 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2077 | ArgTys.push_back(Context->getPointerType( |
| 2078 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2079 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2080 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2081 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2082 | GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2083 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2084 | getClassIdent, getClassType, |
| 2085 | FunctionDecl::Extern, false, 0); |
| 2086 | } |
| 2087 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2088 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2089 | void RewriteObjC::SynthGetMetaClassFunctionDecl() { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2090 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 2091 | llvm::SmallVector<QualType, 16> ArgTys; |
| 2092 | ArgTys.push_back(Context->getPointerType( |
| 2093 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2094 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2095 | &ArgTys[0], ArgTys.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2096 | false /*isVariadic*/, 0); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2097 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2098 | SourceLocation(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2099 | getClassIdent, getClassType, |
| 2100 | FunctionDecl::Extern, false, 0); |
| 2101 | } |
| 2102 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2103 | Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2104 | QualType strType = getConstantStringStructType(); |
| 2105 | |
| 2106 | std::string S = "__NSConstantStringImpl_"; |
Steve Naroff | 7691d9b | 2008-05-31 03:35:42 +0000 | [diff] [blame] | 2107 | |
| 2108 | std::string tmpName = InFileName; |
| 2109 | unsigned i; |
| 2110 | for (i=0; i < tmpName.length(); i++) { |
| 2111 | char c = tmpName.at(i); |
| 2112 | // replace any non alphanumeric characters with '_'. |
| 2113 | if (!isalpha(c) && (c < '0' || c > '9')) |
| 2114 | tmpName[i] = '_'; |
| 2115 | } |
| 2116 | S += tmpName; |
| 2117 | S += "_"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2118 | S += utostr(NumObjCStringLiterals++); |
| 2119 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2120 | Preamble += "static __NSConstantStringImpl " + S; |
| 2121 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 2122 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2123 | // The pretty printer for StringLiteral handles escape characters properly. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2124 | std::string prettyBufS; |
| 2125 | llvm::raw_string_ostream prettyBuf(prettyBufS); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2126 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2127 | Preamble += prettyBuf.str(); |
| 2128 | Preamble += ","; |
Steve Naroff | 3652c2d | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 2129 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 2130 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2131 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 2132 | VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 2133 | &Context->Idents.get(S.c_str()), strType, |
| 2134 | VarDecl::Static, NULL); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2135 | DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation()); |
| 2136 | Expr *Unop = new UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 2137 | Context->getPointerType(DRE->getType()), |
| 2138 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2139 | // cast to NSConstantString * |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2140 | CastExpr *cast = new CStyleCastExpr(Exp->getType(), Unop, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2141 | Exp->getType(), SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2142 | ReplaceStmt(Exp, cast); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2143 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 2144 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2147 | ObjCInterfaceDecl *RewriteObjC::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2148 | // check if we are sending a message to 'super' |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2149 | if (!CurMethodDef || !CurMethodDef->isInstance()) return 0; |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2150 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2151 | if (ObjCSuperExpr *Super = dyn_cast<ObjCSuperExpr>(recExpr)) { |
| 2152 | const PointerType *PT = Super->getType()->getAsPointerType(); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 2153 | assert(PT); |
| 2154 | ObjCInterfaceType *IT = cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 2155 | return IT->getDecl(); |
| 2156 | } |
| 2157 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2161 | QualType RewriteObjC::getSuperStructType() { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2162 | if (!SuperStructDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2163 | SuperStructDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2164 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2165 | &Context->Idents.get("objc_super")); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2166 | QualType FieldTypes[2]; |
| 2167 | |
| 2168 | // struct objc_object *receiver; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2169 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2170 | // struct objc_class *super; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2171 | FieldTypes[1] = Context->getObjCClassType(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2172 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2173 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2174 | for (unsigned i = 0; i < 2; ++i) { |
| 2175 | SuperStructDecl->addDecl(*Context, |
| 2176 | FieldDecl::Create(*Context, SuperStructDecl, |
| 2177 | SourceLocation(), 0, |
| 2178 | FieldTypes[i], /*BitWidth=*/0, |
| 2179 | /*Mutable=*/false, 0), |
| 2180 | true); |
| 2181 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2183 | SuperStructDecl->completeDefinition(*Context); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2184 | } |
| 2185 | return Context->getTagDeclType(SuperStructDecl); |
| 2186 | } |
| 2187 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2188 | QualType RewriteObjC::getConstantStringStructType() { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2189 | if (!ConstantStringDecl) { |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2190 | ConstantStringDecl = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 2191 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2192 | &Context->Idents.get("__NSConstantStringImpl")); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2193 | QualType FieldTypes[4]; |
| 2194 | |
| 2195 | // struct objc_object *receiver; |
| 2196 | FieldTypes[0] = Context->getObjCIdType(); |
| 2197 | // int flags; |
| 2198 | FieldTypes[1] = Context->IntTy; |
| 2199 | // char *str; |
| 2200 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 2201 | // long length; |
| 2202 | FieldTypes[3] = Context->LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2203 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2204 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2205 | for (unsigned i = 0; i < 4; ++i) { |
| 2206 | ConstantStringDecl->addDecl(*Context, |
| 2207 | FieldDecl::Create(*Context, |
| 2208 | ConstantStringDecl, |
| 2209 | SourceLocation(), 0, |
| 2210 | FieldTypes[i], |
| 2211 | /*BitWidth=*/0, |
| 2212 | /*Mutable=*/true, 0), |
| 2213 | true); |
| 2214 | } |
| 2215 | |
| 2216 | ConstantStringDecl->completeDefinition(*Context); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 2217 | } |
| 2218 | return Context->getTagDeclType(ConstantStringDecl); |
| 2219 | } |
| 2220 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2221 | Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 2222 | if (!SelGetUidFunctionDecl) |
| 2223 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2224 | if (!MsgSendFunctionDecl) |
| 2225 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2226 | if (!MsgSendSuperFunctionDecl) |
| 2227 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2228 | if (!MsgSendStretFunctionDecl) |
| 2229 | SynthMsgSendStretFunctionDecl(); |
| 2230 | if (!MsgSendSuperStretFunctionDecl) |
| 2231 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2232 | if (!MsgSendFpretFunctionDecl) |
| 2233 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 2234 | if (!GetClassFunctionDecl) |
| 2235 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2236 | if (!GetMetaClassFunctionDecl) |
| 2237 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2238 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2239 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2240 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 2241 | // May need to use objc_msgSend_stret() as well. |
| 2242 | FunctionDecl *MsgSendStretFlavor = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2243 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2244 | QualType resultType = mDecl->getResultType(); |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2245 | if (resultType->isStructureType() || resultType->isUnionType()) |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2246 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2247 | else if (resultType->isRealFloatingType()) |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 2248 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2249 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2250 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2251 | // Synthesize a call to objc_msgSend(). |
| 2252 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 2253 | IdentifierInfo *clsName = Exp->getClassName(); |
| 2254 | |
| 2255 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 2256 | if (clsName) { // class message. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 2257 | // FIXME: We need to fix Sema (and the AST for ObjCMessageExpr) to handle |
| 2258 | // the 'super' idiom within a class method. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2259 | if (!strcmp(clsName->getName(), "super")) { |
| 2260 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 2261 | if (MsgSendStretFlavor) |
| 2262 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 2263 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2264 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2265 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2266 | CurMethodDef->getClassInterface()->getSuperClass(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2267 | |
| 2268 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2269 | |
| 2270 | // set the receiver to self, the first argument to all methods. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2271 | InitExprs.push_back(new DeclRefExpr( |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2272 | CurMethodDef->getSelfDecl(), |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 2273 | Context->getObjCIdType(), |
| 2274 | SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2275 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2276 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2277 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 2278 | SuperDecl->getIdentifier()->getLength(), |
| 2279 | false, argType, SourceLocation(), |
| 2280 | SourceLocation())); |
| 2281 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 2282 | &ClsExprs[0], |
| 2283 | ClsExprs.size()); |
| 2284 | // To turn off a warning, type-cast to 'id' |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2285 | InitExprs.push_back( // set 'super class', using objc_getClass(). |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2286 | new CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2287 | Cls, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2288 | SourceLocation(), SourceLocation())); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2289 | // struct objc_super |
| 2290 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2291 | Expr *SuperRep; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2292 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2293 | if (LangOpts.Microsoft) { |
| 2294 | SynthSuperContructorFunctionDecl(); |
| 2295 | // Simulate a contructor call... |
| 2296 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 2297 | superType, SourceLocation()); |
| 2298 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 2299 | superType, SourceLocation()); |
| 2300 | } else { |
| 2301 | // (struct objc_super) { <exprs from above> } |
| 2302 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 2303 | &InitExprs[0], InitExprs.size(), |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2304 | SourceLocation(), false); |
| 2305 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, |
| 2306 | false); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2307 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2308 | // struct objc_super * |
| 2309 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2310 | Context->getPointerType(SuperRep->getType()), |
| 2311 | SourceLocation()); |
| 2312 | MsgExprs.push_back(Unop); |
| 2313 | } else { |
| 2314 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2315 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2316 | ClsExprs.push_back(new StringLiteral(clsName->getName(), |
| 2317 | clsName->getLength(), |
| 2318 | false, argType, SourceLocation(), |
| 2319 | SourceLocation())); |
| 2320 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 2321 | &ClsExprs[0], |
| 2322 | ClsExprs.size()); |
| 2323 | MsgExprs.push_back(Cls); |
| 2324 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2325 | } else { // instance message. |
| 2326 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2327 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2328 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2329 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2330 | if (MsgSendStretFlavor) |
| 2331 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2332 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 2333 | |
| 2334 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 2335 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2336 | InitExprs.push_back( |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2337 | new CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 2338 | new DeclRefExpr(CurMethodDef->getSelfDecl(), |
Steve Naroff | f616ebb | 2008-07-16 22:35:27 +0000 | [diff] [blame] | 2339 | Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2340 | SourceLocation()), |
| 2341 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2342 | SourceLocation(), SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2343 | |
| 2344 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 2345 | QualType argType = Context->getPointerType(Context->CharTy); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 2346 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 2347 | SuperDecl->getIdentifier()->getLength(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2348 | false, argType, SourceLocation(), |
| 2349 | SourceLocation())); |
| 2350 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2351 | &ClsExprs[0], |
| 2352 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 2353 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2354 | InitExprs.push_back( |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2355 | // set 'super class', using objc_getClass(). |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2356 | new CStyleCastExpr(Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2357 | Cls, Context->getObjCIdType(), SourceLocation(), SourceLocation())); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2358 | // struct objc_super |
| 2359 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2360 | Expr *SuperRep; |
| 2361 | |
| 2362 | if (LangOpts.Microsoft) { |
| 2363 | SynthSuperContructorFunctionDecl(); |
| 2364 | // Simulate a contructor call... |
| 2365 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 2366 | superType, SourceLocation()); |
| 2367 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 2368 | superType, SourceLocation()); |
| 2369 | } else { |
| 2370 | // (struct objc_super) { <exprs from above> } |
| 2371 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 2372 | &InitExprs[0], InitExprs.size(), |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 2373 | SourceLocation(), false); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2374 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 2375 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2376 | // struct objc_super * |
| 2377 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2378 | Context->getPointerType(SuperRep->getType()), |
| 2379 | SourceLocation()); |
| 2380 | MsgExprs.push_back(Unop); |
| 2381 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2382 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2383 | // Foo<Proto> *. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2384 | while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2385 | recExpr = CE->getSubExpr(); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2386 | recExpr = new CStyleCastExpr(Context->getObjCIdType(), recExpr, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2387 | Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2388 | SourceLocation(), SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2389 | MsgExprs.push_back(recExpr); |
| 2390 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2391 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2392 | // 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] | 2393 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2394 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2395 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getAsString().c_str(), |
| 2396 | Exp->getSelector().getAsString().size(), |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2397 | false, argType, SourceLocation(), |
| 2398 | SourceLocation())); |
| 2399 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2400 | &SelExprs[0], SelExprs.size()); |
| 2401 | MsgExprs.push_back(SelExp); |
| 2402 | |
| 2403 | // Now push any user supplied arguments. |
| 2404 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2405 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2406 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2407 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2408 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2409 | QualType type = ICE->getType()->isObjCQualifiedIdType() |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2410 | ? Context->getObjCIdType() |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2411 | : ICE->getType(); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2412 | userExpr = new CStyleCastExpr(type, userExpr, type, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2413 | } |
| 2414 | // Make id<P...> cast into an 'id' cast. |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2415 | else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2416 | if (CE->getType()->isObjCQualifiedIdType()) { |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2417 | while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2418 | userExpr = CE->getSubExpr(); |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2419 | userExpr = new CStyleCastExpr(Context->getObjCIdType(), |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2420 | userExpr, Context->getObjCIdType(), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2421 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2422 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2423 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2424 | MsgExprs.push_back(userExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2425 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2426 | // the original expression, since we will delete it below. |
| 2427 | Exp->setArg(i, 0); |
| 2428 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2429 | // Generate the funky cast. |
| 2430 | CastExpr *cast; |
| 2431 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2432 | QualType returnType; |
| 2433 | |
| 2434 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2435 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2436 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2437 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2438 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2439 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2440 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2441 | // Push any user argument types. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 2442 | for (unsigned i = 0; i < mDecl->getNumParams(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2443 | QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType() |
| 2444 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2445 | : mDecl->getParamDecl(i)->getType(); |
Steve Naroff | a206b06 | 2008-10-29 14:49:46 +0000 | [diff] [blame] | 2446 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 2447 | if (isBlockPointerType(t)) { |
| 2448 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 2449 | t = Context->getPointerType(BPT->getPointeeType()); |
| 2450 | } |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2451 | ArgTypes.push_back(t); |
| 2452 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2453 | returnType = mDecl->getResultType()->isObjCQualifiedIdType() |
| 2454 | ? Context->getObjCIdType() : mDecl->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2455 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2456 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2457 | } |
| 2458 | // 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] | 2459 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2460 | |
| 2461 | // Create a reference to the objc_msgSend() declaration. |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2462 | DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType, |
| 2463 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2466 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2467 | // xx.m:13: warning: function called through a non-compatible type |
| 2468 | // xx.m:13: note: if this code is reached, the program will abort |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2469 | cast = new CStyleCastExpr(Context->getPointerType(Context->VoidTy), DRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2470 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2471 | SourceLocation(), SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2472 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2473 | // Now do the "normal" pointer to function cast. |
| 2474 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2475 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2476 | // If we don't have a method decl, force a variadic cast. |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2477 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2478 | castType = Context->getPointerType(castType); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2479 | cast = new CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2480 | |
| 2481 | // Don't forget the parens to enforce the proper binding. |
| 2482 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2483 | |
| 2484 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
| 2485 | CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2486 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2487 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2488 | if (MsgSendStretFlavor) { |
| 2489 | // We have the method which returns a struct/union. Must also generate |
| 2490 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2491 | // expression which dictate which one to envoke depending on size of |
| 2492 | // method's return type. |
| 2493 | |
| 2494 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2495 | DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType, |
| 2496 | SourceLocation()); |
| 2497 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2498 | cast = new CStyleCastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2499 | Context->getPointerType(Context->VoidTy), |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2500 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2501 | // Now do the "normal" pointer to function cast. |
| 2502 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2503 | &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2504 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2505 | castType = Context->getPointerType(castType); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 2506 | cast = new CStyleCastExpr(castType, cast, castType, SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2507 | |
| 2508 | // Don't forget the parens to enforce the proper binding. |
| 2509 | PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2510 | |
| 2511 | FT = msgSendType->getAsFunctionType(); |
| 2512 | CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2513 | FT->getResultType(), SourceLocation()); |
| 2514 | |
| 2515 | // Build sizeof(returnType) |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2516 | SizeOfAlignOfExpr *sizeofExpr = new SizeOfAlignOfExpr(true, true, |
| 2517 | returnType.getAsOpaquePtr(), |
| 2518 | Context->getSizeType(), |
| 2519 | SourceLocation(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2520 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2521 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2522 | // For X86 it is more complicated and some kind of target specific routine |
| 2523 | // is needed to decide what to do. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2524 | unsigned IntSize = |
| 2525 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2526 | IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8), |
| 2527 | Context->IntTy, |
| 2528 | SourceLocation()); |
| 2529 | BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit, |
| 2530 | BinaryOperator::LE, |
| 2531 | Context->IntTy, |
| 2532 | SourceLocation()); |
| 2533 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2534 | ConditionalOperator *CondExpr = |
| 2535 | new ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2536 | ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2537 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2538 | return ReplacingStmt; |
| 2539 | } |
| 2540 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2541 | Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2542 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | 80c2855 | 2008-10-27 20:54:44 +0000 | [diff] [blame] | 2543 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 2544 | //ReplacingStmt->dump(); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2545 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2546 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2547 | |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2548 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2549 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2552 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2553 | /// call to objc_getProtocol("proto-name"). |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2554 | Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2555 | if (!GetProtocolFunctionDecl) |
| 2556 | SynthGetProtocolFunctionDecl(); |
| 2557 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2558 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2559 | QualType argType = Context->getPointerType(Context->CharTy); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2560 | ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getNameAsCString(), |
| 2561 | strlen(Exp->getProtocol()->getNameAsCString()), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2562 | false, argType, SourceLocation(), |
| 2563 | SourceLocation())); |
| 2564 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2565 | &ProtoExprs[0], |
| 2566 | ProtoExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2567 | ReplaceStmt(Exp, ProtoExp); |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 2568 | // delete Exp; leak for now, see RewritePropertySetter() usage for more info. |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2569 | return ProtoExp; |
| 2570 | |
| 2571 | } |
| 2572 | |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2573 | bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, |
| 2574 | const char *endBuf) { |
| 2575 | while (startBuf < endBuf) { |
| 2576 | if (*startBuf == '#') { |
| 2577 | // Skip whitespace. |
| 2578 | for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) |
| 2579 | ; |
| 2580 | if (!strncmp(startBuf, "if", strlen("if")) || |
| 2581 | !strncmp(startBuf, "ifdef", strlen("ifdef")) || |
| 2582 | !strncmp(startBuf, "ifndef", strlen("ifndef")) || |
| 2583 | !strncmp(startBuf, "define", strlen("define")) || |
| 2584 | !strncmp(startBuf, "undef", strlen("undef")) || |
| 2585 | !strncmp(startBuf, "else", strlen("else")) || |
| 2586 | !strncmp(startBuf, "elif", strlen("elif")) || |
| 2587 | !strncmp(startBuf, "endif", strlen("endif")) || |
| 2588 | !strncmp(startBuf, "pragma", strlen("pragma")) || |
| 2589 | !strncmp(startBuf, "include", strlen("include")) || |
| 2590 | !strncmp(startBuf, "import", strlen("import")) || |
| 2591 | !strncmp(startBuf, "include_next", strlen("include_next"))) |
| 2592 | return true; |
| 2593 | } |
| 2594 | startBuf++; |
| 2595 | } |
| 2596 | return false; |
| 2597 | } |
| 2598 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2599 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2600 | /// an objective-c class with ivars. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2601 | void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2602 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2603 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2604 | assert(CDecl->getNameAsCString() && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2605 | "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2606 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2607 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2608 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2609 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2610 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2611 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2612 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2613 | |
| 2614 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2615 | const char *endBuf = SM->getCharacterData(LocEnd); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2616 | |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2617 | // If no ivars and no root or if its root, directly or indirectly, |
| 2618 | // 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] | 2619 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2620 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2621 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2622 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2623 | return; |
| 2624 | } |
| 2625 | |
| 2626 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2627 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2628 | Result += "\nstruct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2629 | Result += CDecl->getNameAsString(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2630 | if (LangOpts.Microsoft) |
| 2631 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2632 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2633 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2634 | const char *cursor = strchr(startBuf, '{'); |
| 2635 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2636 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2637 | // If the buffer contains preprocessor directives, we do more fine-grained |
| 2638 | // rewrites. This is intended to fix code that looks like (which occurs in |
| 2639 | // NSURL.h, for example): |
| 2640 | // |
| 2641 | // #ifdef XYZ |
| 2642 | // @interface Foo : NSObject |
| 2643 | // #else |
| 2644 | // @interface FooBar : NSObject |
| 2645 | // #endif |
| 2646 | // { |
| 2647 | // int i; |
| 2648 | // } |
| 2649 | // @end |
| 2650 | // |
| 2651 | // This clause is segregated to avoid breaking the common case. |
| 2652 | if (BufferContainsPPDirectives(startBuf, cursor)) { |
| 2653 | SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : |
| 2654 | CDecl->getClassLoc(); |
| 2655 | const char *endHeader = SM->getCharacterData(L); |
| 2656 | endHeader += Lexer::MeasureTokenLength(L, *SM); |
| 2657 | |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 2658 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | baf58c3 | 2008-05-31 14:15:04 +0000 | [diff] [blame] | 2659 | // advance to the end of the referenced protocols. |
| 2660 | while (endHeader < cursor && *endHeader != '>') endHeader++; |
| 2661 | endHeader++; |
| 2662 | } |
| 2663 | // rewrite the original header |
| 2664 | ReplaceText(LocStart, endHeader-startBuf, Result.c_str(), Result.size()); |
| 2665 | } else { |
| 2666 | // rewrite the original header *without* disturbing the '{' |
| 2667 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
| 2668 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2669 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2670 | Result = "\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2671 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2672 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2673 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2674 | Result += "_IVARS;\n"; |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2675 | |
| 2676 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2677 | SourceLocation OnePastCurly = |
| 2678 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2679 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2680 | } |
| 2681 | cursor++; // past '{' |
| 2682 | |
| 2683 | // Now comment out any visibility specifiers. |
| 2684 | while (cursor < endBuf) { |
| 2685 | if (*cursor == '@') { |
| 2686 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2687 | // Skip whitespace. |
| 2688 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2689 | /*scan*/; |
| 2690 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2691 | // FIXME: presence of @public, etc. inside comment results in |
| 2692 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2693 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2694 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2695 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2696 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2697 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2698 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2699 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2700 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2701 | // for type checking. |
| 2702 | else if (*cursor == '<') { |
| 2703 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2704 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2705 | cursor = strchr(cursor, '>'); |
| 2706 | cursor++; |
| 2707 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2708 | InsertText(atLoc, " */", 3); |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 2709 | } else if (*cursor == '^') { // rewrite block specifier. |
| 2710 | SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 2711 | ReplaceText(caretLoc, 1, "*", 1); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2712 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2713 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2714 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2715 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2716 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2717 | } else { // we don't have any instance variables - insert super struct. |
| 2718 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 2719 | Result += " {\n struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2720 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2721 | Result += "_IMPL "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2722 | Result += RCDecl->getNameAsString(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2723 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2724 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2725 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2726 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2727 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2728 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2731 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2732 | /// class methods. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2733 | void RewriteObjC::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2734 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2735 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2736 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2737 | const char *ClassName, |
| 2738 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2739 | if (MethodBegin == MethodEnd) return; |
| 2740 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2741 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2742 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2743 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2744 | SEL _cmd; |
| 2745 | char *method_types; |
| 2746 | void *_imp; |
| 2747 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2748 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2749 | Result += "\nstruct _objc_method {\n"; |
| 2750 | Result += "\tSEL _cmd;\n"; |
| 2751 | Result += "\tchar *method_types;\n"; |
| 2752 | Result += "\tvoid *_imp;\n"; |
| 2753 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2754 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2755 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2756 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2757 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2758 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2759 | |
| 2760 | /* struct { |
| 2761 | struct _objc_method_list *next_method; |
| 2762 | int method_count; |
| 2763 | struct _objc_method method_list[]; |
| 2764 | } |
| 2765 | */ |
| 2766 | Result += "\nstatic struct {\n"; |
| 2767 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2768 | Result += "\tint method_count;\n"; |
| 2769 | Result += "\tstruct _objc_method method_list["; |
| 2770 | Result += utostr(MethodEnd-MethodBegin); |
| 2771 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2772 | Result += prefix; |
| 2773 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2774 | Result += "_METHODS_"; |
| 2775 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2776 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2777 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2778 | Result += "_meth\")))= "; |
| 2779 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2780 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2781 | Result += "\t,{{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2782 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2783 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2784 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2785 | Result += "\", \""; |
| 2786 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2787 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2788 | Result += MethodInternalNames[*MethodBegin]; |
| 2789 | Result += "}\n"; |
| 2790 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2791 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2792 | Result += (*MethodBegin)->getSelector().getAsString().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2793 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2794 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2795 | Result += "\", \""; |
| 2796 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2797 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2798 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2799 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2800 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2801 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2804 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 2805 | void RewriteObjC:: |
| 2806 | RewriteObjCProtocolsMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, |
| 2807 | const char *prefix, |
| 2808 | const char *ClassName, |
| 2809 | std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2810 | static bool objc_protocol_methods = false; |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2811 | if (Protocols.empty()) return; |
| 2812 | |
| 2813 | for (unsigned i = 0; i != Protocols.size(); i++) { |
| 2814 | ObjCProtocolDecl *PDecl = Protocols[i]; |
| 2815 | // Output struct protocol_methods holder of method selector and type. |
| 2816 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
| 2817 | /* struct protocol_methods { |
| 2818 | SEL _cmd; |
| 2819 | char *method_types; |
| 2820 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2821 | */ |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2822 | Result += "\nstruct protocol_methods {\n"; |
| 2823 | Result += "\tSEL _cmd;\n"; |
| 2824 | Result += "\tchar *method_types;\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2825 | Result += "};\n"; |
Steve Naroff | fbfe825 | 2008-05-06 18:26:51 +0000 | [diff] [blame] | 2826 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2827 | objc_protocol_methods = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2828 | } |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2829 | // Do not synthesize the protocol more than once. |
| 2830 | if (ObjCSynthesizedProtocols.count(PDecl)) |
| 2831 | continue; |
| 2832 | |
| 2833 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2834 | unsigned NumMethods = PDecl->getNumInstanceMethods(); |
| 2835 | /* struct _objc_protocol_method_list { |
| 2836 | int protocol_method_count; |
| 2837 | struct protocol_methods protocols[]; |
| 2838 | } |
| 2839 | */ |
| 2840 | Result += "\nstatic struct {\n"; |
| 2841 | Result += "\tint protocol_method_count;\n"; |
| 2842 | Result += "\tstruct protocol_methods protocols["; |
| 2843 | Result += utostr(NumMethods); |
| 2844 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2845 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2846 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
| 2847 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2848 | |
| 2849 | // Output instance methods declared in this protocol. |
| 2850 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
| 2851 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 2852 | if (I == PDecl->instmeth_begin()) |
| 2853 | Result += "\t ,{{(SEL)\""; |
| 2854 | else |
| 2855 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2856 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2857 | std::string MethodTypeString; |
| 2858 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2859 | Result += "\", \""; |
| 2860 | Result += MethodTypeString; |
| 2861 | Result += "\"}\n"; |
| 2862 | } |
| 2863 | Result += "\t }\n};\n"; |
| 2864 | } |
| 2865 | |
| 2866 | // Output class methods declared in this protocol. |
| 2867 | int NumMethods = PDecl->getNumClassMethods(); |
| 2868 | if (NumMethods > 0) { |
| 2869 | /* struct _objc_protocol_method_list { |
| 2870 | int protocol_method_count; |
| 2871 | struct protocol_methods protocols[]; |
| 2872 | } |
| 2873 | */ |
| 2874 | Result += "\nstatic struct {\n"; |
| 2875 | Result += "\tint protocol_method_count;\n"; |
| 2876 | Result += "\tstruct protocol_methods protocols["; |
| 2877 | Result += utostr(NumMethods); |
| 2878 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2879 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2880 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2881 | "{\n\t"; |
| 2882 | Result += utostr(NumMethods); |
| 2883 | Result += "\n"; |
| 2884 | |
| 2885 | // Output instance methods declared in this protocol. |
| 2886 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
| 2887 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 2888 | if (I == PDecl->classmeth_begin()) |
| 2889 | Result += "\t ,{{(SEL)\""; |
| 2890 | else |
| 2891 | Result += "\t ,{(SEL)\""; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2892 | Result += (*I)->getSelector().getAsString().c_str(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2893 | std::string MethodTypeString; |
| 2894 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
| 2895 | Result += "\", \""; |
| 2896 | Result += MethodTypeString; |
| 2897 | Result += "\"}\n"; |
| 2898 | } |
| 2899 | Result += "\t }\n};\n"; |
| 2900 | } |
| 2901 | |
| 2902 | // Output: |
| 2903 | /* struct _objc_protocol { |
| 2904 | // Objective-C 1.0 extensions |
| 2905 | struct _objc_protocol_extension *isa; |
| 2906 | char *protocol_name; |
| 2907 | struct _objc_protocol **protocol_list; |
| 2908 | struct _objc_protocol_method_list *instance_methods; |
| 2909 | struct _objc_protocol_method_list *class_methods; |
| 2910 | }; |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2911 | */ |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2912 | static bool objc_protocol = false; |
| 2913 | if (!objc_protocol) { |
| 2914 | Result += "\nstruct _objc_protocol {\n"; |
| 2915 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2916 | Result += "\tchar *protocol_name;\n"; |
| 2917 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2918 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2919 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2920 | Result += "};\n"; |
| 2921 | |
| 2922 | objc_protocol = true; |
| 2923 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2924 | |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2925 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2926 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2927 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
| 2928 | "{\n\t0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2929 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2930 | Result += "\", 0, "; |
| 2931 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2932 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2933 | Result += PDecl->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2934 | Result += ", "; |
| 2935 | } |
| 2936 | else |
| 2937 | Result += "0, "; |
| 2938 | if (PDecl->getNumClassMethods() > 0) { |
| 2939 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2940 | Result += PDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2941 | Result += "\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2942 | } |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2943 | else |
| 2944 | Result += "0\n"; |
| 2945 | Result += "};\n"; |
| 2946 | |
| 2947 | // Mark this protocol as having been generated. |
| 2948 | if (!ObjCSynthesizedProtocols.insert(PDecl)) |
| 2949 | assert(false && "protocol already synthesized"); |
| 2950 | } |
| 2951 | // Output the top lovel protocol meta-data for the class. |
| 2952 | /* struct _objc_protocol_list { |
| 2953 | struct _objc_protocol_list *next; |
| 2954 | int protocol_count; |
| 2955 | struct _objc_protocol *class_protocols[]; |
| 2956 | } |
| 2957 | */ |
| 2958 | Result += "\nstatic struct {\n"; |
| 2959 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 2960 | Result += "\tint protocol_count;\n"; |
| 2961 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 2962 | Result += utostr(Protocols.size()); |
| 2963 | Result += "];\n} _OBJC_"; |
| 2964 | Result += prefix; |
| 2965 | Result += "_PROTOCOLS_"; |
| 2966 | Result += ClassName; |
| 2967 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
| 2968 | "{\n\t0, "; |
| 2969 | Result += utostr(Protocols.size()); |
| 2970 | Result += "\n"; |
| 2971 | |
| 2972 | Result += "\t,{&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2973 | Result += Protocols[0]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2974 | Result += " \n"; |
| 2975 | |
| 2976 | for (unsigned i = 1; i != Protocols.size(); i++) { |
| 2977 | Result += "\t ,&_OBJC_PROTOCOL_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2978 | Result += Protocols[i]->getNameAsString(); |
Chris Lattner | 9d0aaa1 | 2008-07-21 21:33:21 +0000 | [diff] [blame] | 2979 | Result += "\n"; |
| 2980 | } |
| 2981 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2984 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2985 | /// implementation. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 2986 | void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2987 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2988 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2989 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2990 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2991 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 2992 | CDecl = CDecl->getNextClassCategory()) |
| 2993 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 2994 | break; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2995 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2996 | std::string FullCategoryName = ClassDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2997 | FullCategoryName += '_'; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2998 | FullCategoryName += IDecl->getNameAsString(); |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2999 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3000 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3001 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3002 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 3003 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3004 | |
| 3005 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3006 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3007 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 3008 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3009 | |
| 3010 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 3011 | // Null CDecl is case of a category implementation with no category interface |
| 3012 | if (CDecl) |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3013 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), "CATEGORY", |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 3014 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3015 | |
| 3016 | /* struct _objc_category { |
| 3017 | char *category_name; |
| 3018 | char *class_name; |
| 3019 | struct _objc_method_list *instance_methods; |
| 3020 | struct _objc_method_list *class_methods; |
| 3021 | struct _objc_protocol_list *protocols; |
| 3022 | // Objective-C 1.0 extensions |
| 3023 | uint32_t size; // sizeof (struct _objc_category) |
| 3024 | struct _objc_property_list *instance_properties; // category's own |
| 3025 | // @property decl. |
| 3026 | }; |
| 3027 | */ |
| 3028 | |
| 3029 | static bool objc_category = false; |
| 3030 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3031 | Result += "\nstruct _objc_category {\n"; |
| 3032 | Result += "\tchar *category_name;\n"; |
| 3033 | Result += "\tchar *class_name;\n"; |
| 3034 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 3035 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 3036 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3037 | Result += "\tunsigned int size;\n"; |
| 3038 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 3039 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3040 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 3041 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3042 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 3043 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3044 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3045 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3046 | Result += "\"\n\t, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3047 | Result += ClassDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3048 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3049 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3050 | if (IDecl->getNumInstanceMethods() > 0) { |
| 3051 | Result += "\t, (struct _objc_method_list *)" |
| 3052 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 3053 | Result += FullCategoryName; |
| 3054 | Result += "\n"; |
| 3055 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3056 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3057 | Result += "\t, 0\n"; |
| 3058 | if (IDecl->getNumClassMethods() > 0) { |
| 3059 | Result += "\t, (struct _objc_method_list *)" |
| 3060 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 3061 | Result += FullCategoryName; |
| 3062 | Result += "\n"; |
| 3063 | } |
| 3064 | else |
| 3065 | Result += "\t, 0\n"; |
| 3066 | |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3067 | if (CDecl && !CDecl->getReferencedProtocols().empty()) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3068 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 3069 | Result += FullCategoryName; |
| 3070 | Result += "\n"; |
| 3071 | } |
| 3072 | else |
| 3073 | Result += "\t, 0\n"; |
| 3074 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3077 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 3078 | /// ivar offset. |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3079 | void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3080 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3081 | std::string &Result) { |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3082 | if (ivar->isBitField()) { |
| 3083 | // FIXME: The hack below doesn't work for bitfields. For now, we simply |
| 3084 | // place all bitfields at offset 0. |
| 3085 | Result += "0"; |
| 3086 | } else { |
| 3087 | Result += "__OFFSETOFIVAR__(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3088 | Result += IDecl->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3089 | if (LangOpts.Microsoft) |
| 3090 | Result += "_IMPL"; |
| 3091 | Result += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3092 | Result += ivar->getNameAsString(); |
Steve Naroff | 8f3b265 | 2008-07-16 18:22:22 +0000 | [diff] [blame] | 3093 | Result += ")"; |
| 3094 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3097 | //===----------------------------------------------------------------------===// |
| 3098 | // Meta Data Emission |
| 3099 | //===----------------------------------------------------------------------===// |
| 3100 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 3101 | void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3102 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3103 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3104 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3105 | // Explictly declared @interface's are already synthesized. |
| 3106 | if (CDecl->ImplicitInterfaceDecl()) { |
| 3107 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 3108 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3109 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 3110 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3112 | // Build _objc_ivar_list metadata for classes ivars if needed |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3113 | unsigned NumIvars = !IDecl->ivar_empty() |
| 3114 | ? IDecl->ivar_size() |
| 3115 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3116 | if (NumIvars > 0) { |
| 3117 | static bool objc_ivar = false; |
| 3118 | if (!objc_ivar) { |
| 3119 | /* struct _objc_ivar { |
| 3120 | char *ivar_name; |
| 3121 | char *ivar_type; |
| 3122 | int ivar_offset; |
| 3123 | }; |
| 3124 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3125 | Result += "\nstruct _objc_ivar {\n"; |
| 3126 | Result += "\tchar *ivar_name;\n"; |
| 3127 | Result += "\tchar *ivar_type;\n"; |
| 3128 | Result += "\tint ivar_offset;\n"; |
| 3129 | Result += "};\n"; |
| 3130 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3131 | objc_ivar = true; |
| 3132 | } |
| 3133 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3134 | /* struct { |
| 3135 | int ivar_count; |
| 3136 | struct _objc_ivar ivar_list[nIvars]; |
| 3137 | }; |
| 3138 | */ |
| 3139 | Result += "\nstatic struct {\n"; |
| 3140 | Result += "\tint ivar_count;\n"; |
| 3141 | Result += "\tstruct _objc_ivar ivar_list["; |
| 3142 | Result += utostr(NumIvars); |
| 3143 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3144 | Result += IDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3145 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3146 | "{\n\t"; |
| 3147 | Result += utostr(NumIvars); |
| 3148 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3149 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3150 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 3151 | if (!IDecl->ivar_empty()) { |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3152 | IVI = IDecl->ivar_begin(); |
| 3153 | IVE = IDecl->ivar_end(); |
| 3154 | } else { |
| 3155 | IVI = CDecl->ivar_begin(); |
| 3156 | IVE = CDecl->ivar_end(); |
| 3157 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3158 | Result += "\t,{{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3159 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3160 | Result += "\", \""; |
| 3161 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3162 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3163 | Result += StrEncoding; |
| 3164 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3165 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3166 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3167 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3168 | Result += "\t ,{\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3169 | Result += (*IVI)->getNameAsString(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3170 | Result += "\", \""; |
| 3171 | std::string StrEncoding; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3172 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 3173 | Result += StrEncoding; |
| 3174 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 3175 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3176 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3183 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3184 | true, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3185 | |
| 3186 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3187 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3188 | false, "", IDecl->getNameAsCString(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3189 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3190 | // Protocols referenced in class declaration? |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 3191 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3192 | "CLASS", CDecl->getNameAsCString(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3193 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3194 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3195 | // Declaration of class/meta-class metadata |
| 3196 | /* struct _objc_class { |
| 3197 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3198 | const char *super_class_name; |
| 3199 | char *name; |
| 3200 | long version; |
| 3201 | long info; |
| 3202 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3203 | struct _objc_ivar_list *ivars; |
| 3204 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3205 | struct objc_cache *cache; |
| 3206 | struct objc_protocol_list *protocols; |
| 3207 | const char *ivar_layout; |
| 3208 | struct _objc_class_ext *ext; |
| 3209 | }; |
| 3210 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3211 | static bool objc_class = false; |
| 3212 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3213 | Result += "\nstruct _objc_class {\n"; |
| 3214 | Result += "\tstruct _objc_class *isa;\n"; |
| 3215 | Result += "\tconst char *super_class_name;\n"; |
| 3216 | Result += "\tchar *name;\n"; |
| 3217 | Result += "\tlong version;\n"; |
| 3218 | Result += "\tlong info;\n"; |
| 3219 | Result += "\tlong instance_size;\n"; |
| 3220 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 3221 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 3222 | Result += "\tstruct objc_cache *cache;\n"; |
| 3223 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 3224 | Result += "\tconst char *ivar_layout;\n"; |
| 3225 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 3226 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3227 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3231 | ObjCInterfaceDecl *RootClass = 0; |
| 3232 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3233 | while (SuperClass) { |
| 3234 | RootClass = SuperClass; |
| 3235 | SuperClass = SuperClass->getSuperClass(); |
| 3236 | } |
| 3237 | SuperClass = CDecl->getSuperClass(); |
| 3238 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3239 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3240 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3241 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3242 | "{\n\t(struct _objc_class *)\""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3243 | Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3244 | Result += "\""; |
| 3245 | |
| 3246 | if (SuperClass) { |
| 3247 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3248 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3249 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3250 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3251 | Result += "\""; |
| 3252 | } |
| 3253 | else { |
| 3254 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3255 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3256 | Result += "\""; |
| 3257 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3258 | // 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] | 3259 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3260 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 3261 | if (IDecl->getNumClassMethods() > 0) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 3262 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3263 | Result += IDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3264 | Result += "\n"; |
| 3265 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3266 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3267 | Result += ", 0\n"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 3268 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3269 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3270 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3271 | Result += ",0,0\n"; |
| 3272 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 3273 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3274 | Result += "\t,0,0,0,0\n"; |
| 3275 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3276 | |
| 3277 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3278 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3279 | Result += CDecl->getNameAsString(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3280 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3281 | "{\n\t&_OBJC_METACLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3282 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3283 | if (SuperClass) { |
| 3284 | Result += ", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3285 | Result += SuperClass->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3286 | Result += "\", \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3287 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3288 | Result += "\""; |
| 3289 | } |
| 3290 | else { |
| 3291 | Result += ", 0, \""; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3292 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3293 | Result += "\""; |
| 3294 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3295 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3296 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3297 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3298 | Result += ",0"; |
| 3299 | else { |
| 3300 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 3301 | Result += ",sizeof(struct "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3302 | Result += CDecl->getNameAsString(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 3303 | if (LangOpts.Microsoft) |
| 3304 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 3305 | Result += ")"; |
| 3306 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3307 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 3308 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3309 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3310 | Result += "\n\t"; |
| 3311 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3312 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3313 | Result += ",0"; |
| 3314 | if (IDecl->getNumInstanceMethods() > 0) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 3315 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3316 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3317 | Result += ", 0\n\t"; |
| 3318 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3319 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3320 | Result += ",0,0"; |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 3321 | if (!CDecl->getReferencedProtocols().empty()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 3322 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3323 | Result += CDecl->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3324 | Result += ", 0,0\n"; |
| 3325 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 3326 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3327 | Result += ",0,0,0\n"; |
| 3328 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 3329 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3330 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3331 | /// RewriteImplementations - This routine rewrites all method implementations |
| 3332 | /// and emits meta-data. |
| 3333 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3334 | void RewriteObjC::RewriteImplementations() { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3335 | int ClsDefCount = ClassImplementation.size(); |
| 3336 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3337 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 3338 | // Rewrite implemented methods |
| 3339 | for (int i = 0; i < ClsDefCount; i++) |
| 3340 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3341 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3342 | for (int i = 0; i < CatDefCount; i++) |
| 3343 | RewriteImplementationDecl(CategoryImplementation[i]); |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3344 | } |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 3345 | |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 3346 | void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { |
| 3347 | int ClsDefCount = ClassImplementation.size(); |
| 3348 | int CatDefCount = CategoryImplementation.size(); |
| 3349 | |
Steve Naroff | 5df5b76 | 2008-05-07 21:23:49 +0000 | [diff] [blame] | 3350 | // This is needed for determining instance variable offsets. |
Steve Naroff | a11440b | 2008-08-05 18:47:23 +0000 | [diff] [blame] | 3351 | Result += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3352 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3353 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3354 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 3355 | |
| 3356 | // For each implemented category, write out all its meta data. |
| 3357 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3358 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 3359 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3360 | // Write objc_symtab metadata |
| 3361 | /* |
| 3362 | struct _objc_symtab |
| 3363 | { |
| 3364 | long sel_ref_cnt; |
| 3365 | SEL *refs; |
| 3366 | short cls_def_cnt; |
| 3367 | short cat_def_cnt; |
| 3368 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 3369 | }; |
| 3370 | */ |
| 3371 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3372 | Result += "\nstruct _objc_symtab {\n"; |
| 3373 | Result += "\tlong sel_ref_cnt;\n"; |
| 3374 | Result += "\tSEL *refs;\n"; |
| 3375 | Result += "\tshort cls_def_cnt;\n"; |
| 3376 | Result += "\tshort cat_def_cnt;\n"; |
| 3377 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 3378 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3379 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3380 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3381 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3382 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 3383 | + ", " + utostr(CatDefCount) + "\n"; |
| 3384 | for (int i = 0; i < ClsDefCount; i++) { |
| 3385 | Result += "\t,&_OBJC_CLASS_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3386 | Result += ClassImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3387 | Result += "\n"; |
| 3388 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3389 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3390 | for (int i = 0; i < CatDefCount; i++) { |
| 3391 | Result += "\t,&_OBJC_CATEGORY_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3392 | Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3393 | Result += "_"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3394 | Result += CategoryImplementation[i]->getNameAsString(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3395 | Result += "\n"; |
| 3396 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3397 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3398 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3399 | |
| 3400 | // Write objc_module metadata |
| 3401 | |
| 3402 | /* |
| 3403 | struct _objc_module { |
| 3404 | long version; |
| 3405 | long size; |
| 3406 | const char *name; |
| 3407 | struct _objc_symtab *symtab; |
| 3408 | } |
| 3409 | */ |
| 3410 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3411 | Result += "\nstruct _objc_module {\n"; |
| 3412 | Result += "\tlong version;\n"; |
| 3413 | Result += "\tlong size;\n"; |
| 3414 | Result += "\tconst char *name;\n"; |
| 3415 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 3416 | Result += "};\n\n"; |
| 3417 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 3418 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 3419 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 3420 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 3421 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3422 | |
| 3423 | if (LangOpts.Microsoft) { |
| 3424 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
Steve Naroff | 1919032 | 2008-05-07 00:06:16 +0000 | [diff] [blame] | 3425 | Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 3426 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 3427 | Result += "&_OBJC_MODULES;\n"; |
| 3428 | Result += "#pragma data_seg(pop)\n\n"; |
| 3429 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 3430 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 3431 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3432 | std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, |
| 3433 | const char *funcName, |
| 3434 | std::string Tag) { |
| 3435 | const FunctionType *AFT = CE->getFunctionType(); |
| 3436 | QualType RT = AFT->getResultType(); |
| 3437 | std::string StructRef = "struct " + Tag; |
| 3438 | std::string S = "static " + RT.getAsString() + " __" + |
| 3439 | funcName + "_" + "block_func_" + utostr(i); |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 3440 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3441 | BlockDecl *BD = CE->getBlockDecl(); |
| 3442 | |
| 3443 | if (isa<FunctionTypeNoProto>(AFT)) { |
| 3444 | S += "()"; |
| 3445 | } else if (BD->param_empty()) { |
| 3446 | S += "(" + StructRef + " *__cself)"; |
| 3447 | } else { |
| 3448 | const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT); |
| 3449 | assert(FT && "SynthesizeBlockFunc: No function proto"); |
| 3450 | S += '('; |
| 3451 | // first add the implicit argument. |
| 3452 | S += StructRef + " *__cself, "; |
| 3453 | std::string ParamStr; |
| 3454 | for (BlockDecl::param_iterator AI = BD->param_begin(), |
| 3455 | E = BD->param_end(); AI != E; ++AI) { |
| 3456 | if (AI != BD->param_begin()) S += ", "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3457 | ParamStr = (*AI)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3458 | (*AI)->getType().getAsStringInternal(ParamStr); |
| 3459 | S += ParamStr; |
| 3460 | } |
| 3461 | if (FT->isVariadic()) { |
| 3462 | if (!BD->param_empty()) S += ", "; |
| 3463 | S += "..."; |
| 3464 | } |
| 3465 | S += ')'; |
| 3466 | } |
| 3467 | S += " {\n"; |
| 3468 | |
| 3469 | // Create local declarations to avoid rewriting all closure decl ref exprs. |
| 3470 | // First, emit a declaration for all "by ref" decls. |
| 3471 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3472 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3473 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3474 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3475 | Context->getPointerType((*I)->getType()).getAsStringInternal(Name); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3476 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3477 | } |
| 3478 | // Next, emit a declaration for all "by copy" declarations. |
| 3479 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3480 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3481 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3482 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3483 | // Handle nested closure invocation. For example: |
| 3484 | // |
| 3485 | // void (^myImportedClosure)(void); |
| 3486 | // myImportedClosure = ^(void) { setGlobalInt(x + y); }; |
| 3487 | // |
| 3488 | // void (^anotherClosure)(void); |
| 3489 | // anotherClosure = ^(void) { |
| 3490 | // myImportedClosure(); // import and invoke the closure |
| 3491 | // }; |
| 3492 | // |
| 3493 | if (isBlockPointerType((*I)->getType())) |
| 3494 | S += "struct __block_impl *"; |
| 3495 | else |
| 3496 | (*I)->getType().getAsStringInternal(Name); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3497 | S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3498 | } |
| 3499 | std::string RewrittenStr = RewrittenBlockExprs[CE]; |
| 3500 | const char *cstr = RewrittenStr.c_str(); |
| 3501 | while (*cstr++ != '{') ; |
| 3502 | S += cstr; |
| 3503 | S += "\n"; |
| 3504 | return S; |
| 3505 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3506 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3507 | std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, |
| 3508 | const char *funcName, |
| 3509 | std::string Tag) { |
| 3510 | std::string StructRef = "struct " + Tag; |
| 3511 | std::string S = "static void __"; |
| 3512 | |
| 3513 | S += funcName; |
| 3514 | S += "_block_copy_" + utostr(i); |
| 3515 | S += "(" + StructRef; |
| 3516 | S += "*dst, " + StructRef; |
| 3517 | S += "*src) {"; |
| 3518 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3519 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3520 | S += "_Block_copy_assign(&dst->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3521 | S += (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3522 | S += ", src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3523 | S += (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3524 | S += ");}"; |
| 3525 | } |
| 3526 | S += "\nstatic void __"; |
| 3527 | S += funcName; |
| 3528 | S += "_block_dispose_" + utostr(i); |
| 3529 | S += "(" + StructRef; |
| 3530 | S += "*src) {"; |
| 3531 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), |
| 3532 | E = ImportedBlockDecls.end(); I != E; ++I) { |
| 3533 | S += "_Block_destroy(src->"; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3534 | S += (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3535 | S += ");"; |
| 3536 | } |
| 3537 | S += "}\n"; |
| 3538 | return S; |
| 3539 | } |
| 3540 | |
| 3541 | std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, |
| 3542 | bool hasCopyDisposeHelpers) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3543 | std::string S = "\nstruct " + Tag; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3544 | std::string Constructor = " " + Tag; |
| 3545 | |
| 3546 | S += " {\n struct __block_impl impl;\n"; |
| 3547 | |
| 3548 | if (hasCopyDisposeHelpers) |
| 3549 | S += " void *copy;\n void *dispose;\n"; |
| 3550 | |
| 3551 | Constructor += "(void *fp"; |
| 3552 | |
| 3553 | if (hasCopyDisposeHelpers) |
| 3554 | Constructor += ", void *copyHelp, void *disposeHelp"; |
| 3555 | |
| 3556 | if (BlockDeclRefs.size()) { |
| 3557 | // Output all "by copy" declarations. |
| 3558 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3559 | E = BlockByCopyDecls.end(); I != E; ++I) { |
| 3560 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3561 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3562 | std::string ArgName = "_" + FieldName; |
| 3563 | // Handle nested closure invocation. For example: |
| 3564 | // |
| 3565 | // void (^myImportedBlock)(void); |
| 3566 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3567 | // |
| 3568 | // void (^anotherBlock)(void); |
| 3569 | // anotherBlock = ^(void) { |
| 3570 | // myImportedBlock(); // import and invoke the closure |
| 3571 | // }; |
| 3572 | // |
| 3573 | if (isBlockPointerType((*I)->getType())) { |
| 3574 | S += "struct __block_impl *"; |
| 3575 | Constructor += ", void *" + ArgName; |
| 3576 | } else { |
| 3577 | (*I)->getType().getAsStringInternal(FieldName); |
| 3578 | (*I)->getType().getAsStringInternal(ArgName); |
| 3579 | Constructor += ", " + ArgName; |
| 3580 | } |
| 3581 | S += FieldName + ";\n"; |
| 3582 | } |
| 3583 | // Output all "by ref" declarations. |
| 3584 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3585 | E = BlockByRefDecls.end(); I != E; ++I) { |
| 3586 | S += " "; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3587 | std::string FieldName = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3588 | std::string ArgName = "_" + FieldName; |
| 3589 | // Handle nested closure invocation. For example: |
| 3590 | // |
| 3591 | // void (^myImportedBlock)(void); |
| 3592 | // myImportedBlock = ^(void) { setGlobalInt(x + y); }; |
| 3593 | // |
| 3594 | // void (^anotherBlock)(void); |
| 3595 | // anotherBlock = ^(void) { |
| 3596 | // myImportedBlock(); // import and invoke the closure |
| 3597 | // }; |
| 3598 | // |
| 3599 | if (isBlockPointerType((*I)->getType())) { |
| 3600 | S += "struct __block_impl *"; |
| 3601 | Constructor += ", void *" + ArgName; |
| 3602 | } else { |
| 3603 | Context->getPointerType((*I)->getType()).getAsStringInternal(FieldName); |
| 3604 | Context->getPointerType((*I)->getType()).getAsStringInternal(ArgName); |
| 3605 | Constructor += ", " + ArgName; |
| 3606 | } |
| 3607 | S += FieldName + "; // by ref\n"; |
| 3608 | } |
| 3609 | // Finish writing the constructor. |
| 3610 | // FIXME: handle NSConcreteGlobalBlock. |
| 3611 | Constructor += ", int flags=0) {\n"; |
| 3612 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3613 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3614 | |
| 3615 | if (hasCopyDisposeHelpers) |
| 3616 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3617 | |
| 3618 | // Initialize all "by copy" arguments. |
| 3619 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 3620 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3621 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3622 | Constructor += " "; |
| 3623 | if (isBlockPointerType((*I)->getType())) |
| 3624 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3625 | else |
| 3626 | Constructor += Name + " = _"; |
| 3627 | Constructor += Name + ";\n"; |
| 3628 | } |
| 3629 | // Initialize all "by ref" arguments. |
| 3630 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 3631 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3632 | std::string Name = (*I)->getNameAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3633 | Constructor += " "; |
| 3634 | if (isBlockPointerType((*I)->getType())) |
| 3635 | Constructor += Name + " = (struct __block_impl *)_"; |
| 3636 | else |
| 3637 | Constructor += Name + " = _"; |
| 3638 | Constructor += Name + ";\n"; |
| 3639 | } |
| 3640 | } else { |
| 3641 | // Finish writing the constructor. |
| 3642 | // FIXME: handle NSConcreteGlobalBlock. |
| 3643 | Constructor += ", int flags=0) {\n"; |
| 3644 | Constructor += " impl.isa = 0/*&_NSConcreteStackBlock*/;\n impl.Size = sizeof("; |
| 3645 | Constructor += Tag + ");\n impl.Flags = flags;\n impl.FuncPtr = fp;\n"; |
| 3646 | if (hasCopyDisposeHelpers) |
| 3647 | Constructor += " copy = copyHelp;\n dispose = disposeHelp;\n"; |
| 3648 | } |
| 3649 | Constructor += " "; |
| 3650 | Constructor += "}\n"; |
| 3651 | S += Constructor; |
| 3652 | S += "};\n"; |
| 3653 | return S; |
| 3654 | } |
| 3655 | |
| 3656 | void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, |
| 3657 | const char *FunName) { |
| 3658 | // Insert closures that were part of the function. |
| 3659 | for (unsigned i = 0; i < Blocks.size(); i++) { |
| 3660 | |
| 3661 | CollectBlockDeclRefInfo(Blocks[i]); |
| 3662 | |
| 3663 | std::string Tag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); |
| 3664 | |
| 3665 | std::string CI = SynthesizeBlockImpl(Blocks[i], Tag, |
| 3666 | ImportedBlockDecls.size() > 0); |
| 3667 | |
| 3668 | InsertText(FunLocStart, CI.c_str(), CI.size()); |
| 3669 | |
| 3670 | std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, Tag); |
| 3671 | |
| 3672 | InsertText(FunLocStart, CF.c_str(), CF.size()); |
| 3673 | |
| 3674 | if (ImportedBlockDecls.size()) { |
| 3675 | std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, Tag); |
| 3676 | InsertText(FunLocStart, HF.c_str(), HF.size()); |
| 3677 | } |
| 3678 | |
| 3679 | BlockDeclRefs.clear(); |
| 3680 | BlockByRefDecls.clear(); |
| 3681 | BlockByCopyDecls.clear(); |
| 3682 | BlockCallExprs.clear(); |
| 3683 | ImportedBlockDecls.clear(); |
| 3684 | } |
| 3685 | Blocks.clear(); |
| 3686 | RewrittenBlockExprs.clear(); |
| 3687 | } |
| 3688 | |
| 3689 | void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { |
| 3690 | SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3691 | const char *FuncName = FD->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3692 | |
| 3693 | SynthesizeBlockLiterals(FunLocStart, FuncName); |
| 3694 | } |
| 3695 | |
| 3696 | void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { |
Steve Naroff | ced80a8 | 2008-10-30 12:09:33 +0000 | [diff] [blame] | 3697 | //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); |
| 3698 | //SourceLocation FunLocStart = MD->getLocStart(); |
| 3699 | // FIXME: This hack works around a bug in Rewrite.InsertText(). |
| 3700 | SourceLocation FunLocStart = MD->getLocStart().getFileLocWithOffset(-1); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3701 | std::string FuncName = MD->getSelector().getAsString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3702 | // Convert colons to underscores. |
| 3703 | std::string::size_type loc = 0; |
| 3704 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 3705 | FuncName.replace(loc, 1, "_"); |
| 3706 | |
| 3707 | SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); |
| 3708 | } |
| 3709 | |
| 3710 | void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { |
| 3711 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3712 | CI != E; ++CI) |
| 3713 | if (*CI) { |
| 3714 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3715 | GetBlockDeclRefExprs(CBE->getBody()); |
| 3716 | else |
| 3717 | GetBlockDeclRefExprs(*CI); |
| 3718 | } |
| 3719 | // Handle specific things. |
| 3720 | if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) |
| 3721 | // FIXME: Handle enums. |
| 3722 | if (!isa<FunctionDecl>(CDRE->getDecl())) |
| 3723 | BlockDeclRefs.push_back(CDRE); |
| 3724 | return; |
| 3725 | } |
| 3726 | |
| 3727 | void RewriteObjC::GetBlockCallExprs(Stmt *S) { |
| 3728 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 3729 | CI != E; ++CI) |
| 3730 | if (*CI) { |
| 3731 | if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) |
| 3732 | GetBlockCallExprs(CBE->getBody()); |
| 3733 | else |
| 3734 | GetBlockCallExprs(*CI); |
| 3735 | } |
| 3736 | |
| 3737 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 3738 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 3739 | BlockCallExprs[dyn_cast<BlockDeclRefExpr>(CE->getCallee())] = CE; |
| 3740 | } |
| 3741 | } |
| 3742 | return; |
| 3743 | } |
| 3744 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3745 | Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp) { |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3746 | // Navigate to relevant type information. |
| 3747 | const char *closureName = 0; |
| 3748 | const BlockPointerType *CPT = 0; |
| 3749 | |
| 3750 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3751 | closureName = DRE->getDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3752 | CPT = DRE->getType()->getAsBlockPointerType(); |
| 3753 | } else if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3754 | closureName = CDRE->getDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3755 | CPT = CDRE->getType()->getAsBlockPointerType(); |
| 3756 | } else if (MemberExpr *MExpr = dyn_cast<MemberExpr>(Exp->getCallee())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3757 | closureName = MExpr->getMemberDecl()->getNameAsCString(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3758 | CPT = MExpr->getType()->getAsBlockPointerType(); |
| 3759 | } else { |
| 3760 | assert(1 && "RewriteBlockClass: Bad type"); |
| 3761 | } |
| 3762 | assert(CPT && "RewriteBlockClass: Bad type"); |
| 3763 | const FunctionType *FT = CPT->getPointeeType()->getAsFunctionType(); |
| 3764 | assert(FT && "RewriteBlockClass: Bad type"); |
| 3765 | const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FT); |
| 3766 | // FTP will be null for closures that don't take arguments. |
| 3767 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3768 | RecordDecl *RD = RecordDecl::Create(*Context, TagDecl::TK_struct, TUDecl, |
| 3769 | SourceLocation(), |
| 3770 | &Context->Idents.get("__block_impl")); |
| 3771 | QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3772 | |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3773 | // Generate a funky cast. |
| 3774 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3775 | |
| 3776 | // Push the block argument type. |
| 3777 | ArgTypes.push_back(PtrBlock); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3778 | if (FTP) { |
| 3779 | for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(), |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3780 | E = FTP->arg_type_end(); I && (I != E); ++I) { |
| 3781 | QualType t = *I; |
| 3782 | // Make sure we convert "t (^)(...)" to "t (*)(...)". |
| 3783 | if (isBlockPointerType(t)) { |
| 3784 | const BlockPointerType *BPT = t->getAsBlockPointerType(); |
| 3785 | t = Context->getPointerType(BPT->getPointeeType()); |
| 3786 | } |
| 3787 | ArgTypes.push_back(t); |
| 3788 | } |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3789 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3790 | // Now do the pointer to function cast. |
| 3791 | QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), |
| 3792 | &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0); |
| 3793 | |
| 3794 | PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3795 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3796 | CastExpr *BlkCast = new CStyleCastExpr(PtrBlock, Exp->getCallee(), PtrBlock, SourceLocation(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3797 | // Don't forget the parens to enforce the proper binding. |
| 3798 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), BlkCast); |
| 3799 | //PE->dump(); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3801 | FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), |
| 3802 | &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, |
| 3803 | /*BitWidth=*/0, /*Mutable=*/true, 0); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3804 | MemberExpr *ME = new MemberExpr(PE, true, FD, SourceLocation(), FD->getType()); |
| 3805 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3806 | CastExpr *FunkCast = new CStyleCastExpr(PtrToFuncCastType, ME, PtrToFuncCastType, SourceLocation(), SourceLocation()); |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3807 | PE = new ParenExpr(SourceLocation(), SourceLocation(), FunkCast); |
| 3808 | |
| 3809 | llvm::SmallVector<Expr*, 8> BlkExprs; |
| 3810 | // Add the implicit argument. |
| 3811 | BlkExprs.push_back(BlkCast); |
| 3812 | // Add the user arguments. |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3813 | for (CallExpr::arg_iterator I = Exp->arg_begin(), |
| 3814 | E = Exp->arg_end(); I != E; ++I) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3815 | BlkExprs.push_back(*I); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3816 | } |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3817 | CallExpr *CE = new CallExpr(PE, &BlkExprs[0], BlkExprs.size(), Exp->getType(), SourceLocation()); |
| 3818 | return CE; |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 3822 | Stmt *BlockCall = SynthesizeBlockCall(Exp); |
| 3823 | ReplaceStmt(Exp, BlockCall); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | void RewriteObjC::RewriteBlockDeclRefExpr(BlockDeclRefExpr *BDRE) { |
| 3827 | // FIXME: Add more elaborate code generation required by the ABI. |
| 3828 | InsertText(BDRE->getLocStart(), "*", 1); |
| 3829 | } |
| 3830 | |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 3831 | void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { |
| 3832 | SourceLocation LocStart = CE->getLParenLoc(); |
| 3833 | SourceLocation LocEnd = CE->getRParenLoc(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3834 | |
| 3835 | // Need to avoid trying to rewrite synthesized casts. |
| 3836 | if (LocStart.isInvalid()) |
| 3837 | return; |
Steve Naroff | 8f6ce57 | 2008-11-03 11:20:24 +0000 | [diff] [blame] | 3838 | // Need to avoid trying to rewrite casts contained in macros. |
| 3839 | if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) |
| 3840 | return; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 3841 | |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3842 | const char *startBuf = SM->getCharacterData(LocStart); |
| 3843 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 3844 | |
| 3845 | // advance the location to startArgList. |
| 3846 | const char *argPtr = startBuf; |
| 3847 | |
| 3848 | while (*argPtr++ && (argPtr < endBuf)) { |
| 3849 | switch (*argPtr) { |
| 3850 | case '^': |
| 3851 | // Replace the '^' with '*'. |
| 3852 | LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); |
| 3853 | ReplaceText(LocStart, 1, "*", 1); |
| 3854 | break; |
| 3855 | } |
| 3856 | } |
| 3857 | return; |
| 3858 | } |
| 3859 | |
| 3860 | void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { |
| 3861 | SourceLocation DeclLoc = FD->getLocation(); |
| 3862 | unsigned parenCount = 0; |
| 3863 | |
| 3864 | // We have 1 or more arguments that have closure pointers. |
| 3865 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3866 | const char *startArgList = strchr(startBuf, '('); |
| 3867 | |
| 3868 | assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); |
| 3869 | |
| 3870 | parenCount++; |
| 3871 | // advance the location to startArgList. |
| 3872 | DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); |
| 3873 | assert((DeclLoc.isValid()) && "Invalid DeclLoc"); |
| 3874 | |
| 3875 | const char *argPtr = startArgList; |
| 3876 | |
| 3877 | while (*argPtr++ && parenCount) { |
| 3878 | switch (*argPtr) { |
| 3879 | case '^': |
| 3880 | // Replace the '^' with '*'. |
| 3881 | DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); |
| 3882 | ReplaceText(DeclLoc, 1, "*", 1); |
| 3883 | break; |
| 3884 | case '(': |
| 3885 | parenCount++; |
| 3886 | break; |
| 3887 | case ')': |
| 3888 | parenCount--; |
| 3889 | break; |
| 3890 | } |
| 3891 | } |
| 3892 | return; |
| 3893 | } |
| 3894 | |
| 3895 | bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { |
| 3896 | const FunctionTypeProto *FTP; |
| 3897 | const PointerType *PT = QT->getAsPointerType(); |
| 3898 | if (PT) { |
| 3899 | FTP = PT->getPointeeType()->getAsFunctionTypeProto(); |
| 3900 | } else { |
| 3901 | const BlockPointerType *BPT = QT->getAsBlockPointerType(); |
| 3902 | assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); |
| 3903 | FTP = BPT->getPointeeType()->getAsFunctionTypeProto(); |
| 3904 | } |
| 3905 | if (FTP) { |
| 3906 | for (FunctionTypeProto::arg_type_iterator I = FTP->arg_type_begin(), |
| 3907 | E = FTP->arg_type_end(); I != E; ++I) |
| 3908 | if (isBlockPointerType(*I)) |
| 3909 | return true; |
| 3910 | } |
| 3911 | return false; |
| 3912 | } |
| 3913 | |
| 3914 | void RewriteObjC::GetExtentOfArgList(const char *Name, |
| 3915 | const char *&LParen, const char *&RParen) { |
| 3916 | const char *argPtr = strchr(Name, '('); |
| 3917 | assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); |
| 3918 | |
| 3919 | LParen = argPtr; // output the start. |
| 3920 | argPtr++; // skip past the left paren. |
| 3921 | unsigned parenCount = 1; |
| 3922 | |
| 3923 | while (*argPtr && parenCount) { |
| 3924 | switch (*argPtr) { |
| 3925 | case '(': parenCount++; break; |
| 3926 | case ')': parenCount--; break; |
| 3927 | default: break; |
| 3928 | } |
| 3929 | if (parenCount) argPtr++; |
| 3930 | } |
| 3931 | assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); |
| 3932 | RParen = argPtr; // output the end |
| 3933 | } |
| 3934 | |
| 3935 | void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { |
| 3936 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 3937 | RewriteBlockPointerFunctionArgs(FD); |
| 3938 | return; |
| 3939 | } |
| 3940 | // Handle Variables and Typedefs. |
| 3941 | SourceLocation DeclLoc = ND->getLocation(); |
| 3942 | QualType DeclT; |
| 3943 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3944 | DeclT = VD->getType(); |
| 3945 | else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) |
| 3946 | DeclT = TDD->getUnderlyingType(); |
| 3947 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) |
| 3948 | DeclT = FD->getType(); |
| 3949 | else |
| 3950 | assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); |
| 3951 | |
| 3952 | const char *startBuf = SM->getCharacterData(DeclLoc); |
| 3953 | const char *endBuf = startBuf; |
| 3954 | // scan backward (from the decl location) for the end of the previous decl. |
| 3955 | while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) |
| 3956 | startBuf--; |
| 3957 | |
| 3958 | // *startBuf != '^' if we are dealing with a pointer to function that |
| 3959 | // may take block argument types (which will be handled below). |
| 3960 | if (*startBuf == '^') { |
| 3961 | // Replace the '^' with '*', computing a negative offset. |
| 3962 | DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); |
| 3963 | ReplaceText(DeclLoc, 1, "*", 1); |
| 3964 | } |
| 3965 | if (PointerTypeTakesAnyBlockArguments(DeclT)) { |
| 3966 | // Replace the '^' with '*' for arguments. |
| 3967 | DeclLoc = ND->getLocation(); |
| 3968 | startBuf = SM->getCharacterData(DeclLoc); |
| 3969 | const char *argListBegin, *argListEnd; |
| 3970 | GetExtentOfArgList(startBuf, argListBegin, argListEnd); |
| 3971 | while (argListBegin < argListEnd) { |
| 3972 | if (*argListBegin == '^') { |
| 3973 | SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); |
| 3974 | ReplaceText(CaretLoc, 1, "*", 1); |
| 3975 | } |
| 3976 | argListBegin++; |
| 3977 | } |
| 3978 | } |
| 3979 | return; |
| 3980 | } |
| 3981 | |
| 3982 | void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { |
| 3983 | // Add initializers for any closure decl refs. |
| 3984 | GetBlockDeclRefExprs(Exp->getBody()); |
| 3985 | if (BlockDeclRefs.size()) { |
| 3986 | // Unique all "by copy" declarations. |
| 3987 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 3988 | if (!BlockDeclRefs[i]->isByRef()) |
| 3989 | BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 3990 | // Unique all "by ref" declarations. |
| 3991 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 3992 | if (BlockDeclRefs[i]->isByRef()) { |
| 3993 | BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 3994 | } |
| 3995 | // Find any imported blocks...they will need special attention. |
| 3996 | for (unsigned i = 0; i < BlockDeclRefs.size(); i++) |
| 3997 | if (isBlockPointerType(BlockDeclRefs[i]->getType())) { |
Steve Naroff | 0007268 | 2008-11-13 17:40:07 +0000 | [diff] [blame] | 3998 | GetBlockCallExprs(BlockDeclRefs[i]); |
Steve Naroff | 5405523 | 2008-10-27 17:20:55 +0000 | [diff] [blame] | 3999 | ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); |
| 4000 | } |
| 4001 | } |
| 4002 | } |
| 4003 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4004 | FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { |
| 4005 | IdentifierInfo *ID = &Context->Idents.get(name); |
| 4006 | QualType FType = Context->getFunctionTypeNoProto(Context->VoidPtrTy); |
| 4007 | return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), |
| 4008 | ID, FType, FunctionDecl::Extern, false, 0); |
| 4009 | } |
| 4010 | |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4011 | Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4012 | Blocks.push_back(Exp); |
| 4013 | |
| 4014 | CollectBlockDeclRefInfo(Exp); |
| 4015 | std::string FuncName; |
| 4016 | |
| 4017 | if (CurFunctionDef) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4018 | FuncName = CurFunctionDef->getNameAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4019 | else if (CurMethodDef) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4020 | FuncName = CurMethodDef->getSelector().getAsString(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4021 | // Convert colons to underscores. |
| 4022 | std::string::size_type loc = 0; |
| 4023 | while ((loc = FuncName.find(":", loc)) != std::string::npos) |
| 4024 | FuncName.replace(loc, 1, "_"); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4025 | } else if (GlobalVarDecl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4026 | FuncName = std::string(GlobalVarDecl->getNameAsString()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4027 | |
| 4028 | std::string BlockNumber = utostr(Blocks.size()-1); |
| 4029 | |
| 4030 | std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; |
| 4031 | std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; |
| 4032 | |
| 4033 | // Get a pointer to the function type so we can cast appropriately. |
| 4034 | QualType FType = Context->getPointerType(QualType(Exp->getFunctionType(),0)); |
| 4035 | |
| 4036 | FunctionDecl *FD; |
| 4037 | Expr *NewRep; |
| 4038 | |
| 4039 | // Simulate a contructor call... |
| 4040 | FD = SynthBlockInitFunctionDecl(Tag.c_str()); |
| 4041 | DeclRefExpr *DRE = new DeclRefExpr(FD, FType, SourceLocation()); |
| 4042 | |
| 4043 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 4044 | |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4045 | // Initialize the block function. |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4046 | FD = SynthBlockInitFunctionDecl(Func.c_str()); |
| 4047 | DeclRefExpr *Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4048 | CastExpr *castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4049 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4050 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4051 | |
| 4052 | if (ImportedBlockDecls.size()) { |
| 4053 | std::string Buf = "__" + FuncName + "_block_copy_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4054 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
| 4055 | Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4056 | castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4057 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4058 | InitExprs.push_back(castExpr); |
| 4059 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4060 | Buf = "__" + FuncName + "_block_dispose_" + BlockNumber; |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4061 | FD = SynthBlockInitFunctionDecl(Buf.c_str()); |
| 4062 | Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4063 | castExpr = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4064 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4065 | InitExprs.push_back(castExpr); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4066 | } |
| 4067 | // Add initializers for any closure decl refs. |
| 4068 | if (BlockDeclRefs.size()) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4069 | Expr *Exp; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4070 | // Output all "by copy" declarations. |
| 4071 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), |
| 4072 | E = BlockByCopyDecls.end(); I != E; ++I) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4073 | if (isObjCType((*I)->getType())) { |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4074 | // FIXME: Conform to ABI ([[obj retain] autorelease]). |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4075 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4076 | Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4077 | } else if (isBlockPointerType((*I)->getType())) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4078 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4079 | Arg = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4080 | Exp = new CStyleCastExpr(Context->VoidPtrTy, Arg, |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4081 | Context->VoidPtrTy, SourceLocation(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4082 | } else { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4083 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4084 | Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4085 | } |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4086 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4087 | } |
| 4088 | // Output all "by ref" declarations. |
| 4089 | for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), |
| 4090 | E = BlockByRefDecls.end(); I != E; ++I) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4091 | FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); |
Steve Naroff | fdc0372 | 2008-10-29 21:23:59 +0000 | [diff] [blame] | 4092 | Exp = new DeclRefExpr(FD, FD->getType(), SourceLocation()); |
| 4093 | Exp = new UnaryOperator(Exp, UnaryOperator::AddrOf, |
| 4094 | Context->getPointerType(Exp->getType()), |
| 4095 | SourceLocation()); |
Steve Naroff | 707b0fe | 2008-11-14 21:36:12 +0000 | [diff] [blame] | 4096 | InitExprs.push_back(Exp); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4097 | } |
| 4098 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4099 | NewRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 4100 | FType, SourceLocation()); |
| 4101 | NewRep = new UnaryOperator(NewRep, UnaryOperator::AddrOf, |
| 4102 | Context->getPointerType(NewRep->getType()), |
| 4103 | SourceLocation()); |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4104 | NewRep = new CStyleCastExpr(FType, NewRep, FType, SourceLocation(), SourceLocation()); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4105 | BlockDeclRefs.clear(); |
| 4106 | BlockByRefDecls.clear(); |
| 4107 | BlockByCopyDecls.clear(); |
| 4108 | ImportedBlockDecls.clear(); |
| 4109 | return NewRep; |
| 4110 | } |
| 4111 | |
| 4112 | //===----------------------------------------------------------------------===// |
| 4113 | // Function Body / Expression rewriting |
| 4114 | //===----------------------------------------------------------------------===// |
| 4115 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4116 | // This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). |
| 4117 | // The allows the main rewrite loop to associate all ObjCPropertyRefExprs with |
| 4118 | // their respective BinaryOperator. Without this knowledge, we'd need to rewrite |
| 4119 | // the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). |
| 4120 | // Since the rewriter isn't capable of rewriting rewritten code, it's important |
| 4121 | // we get this right. |
| 4122 | void RewriteObjC::CollectPropertySetters(Stmt *S) { |
| 4123 | // Perform a bottom up traversal of all children. |
| 4124 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4125 | CI != E; ++CI) |
| 4126 | if (*CI) |
| 4127 | CollectPropertySetters(*CI); |
| 4128 | |
| 4129 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { |
| 4130 | if (BinOp->isAssignmentOp()) { |
| 4131 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) |
| 4132 | PropSetters[PRE] = BinOp; |
| 4133 | } |
| 4134 | } |
| 4135 | } |
| 4136 | |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4137 | Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
| 4138 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4139 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 4140 | Stmts.push_back(S); |
| 4141 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 4142 | Stmts.push_back(S); |
| 4143 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 4144 | } |
| 4145 | |
| 4146 | SourceRange OrigStmtRange = S->getSourceRange(); |
| 4147 | |
| 4148 | // Perform a bottom up rewrite of all children. |
| 4149 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 4150 | CI != E; ++CI) |
| 4151 | if (*CI) { |
| 4152 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
| 4153 | if (newStmt) |
| 4154 | *CI = newStmt; |
| 4155 | } |
| 4156 | |
| 4157 | if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 4158 | // Rewrite the block body in place. |
| 4159 | RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); |
| 4160 | |
| 4161 | // 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] | 4162 | std::string Str = Rewrite.getRewritenText(BE->getSourceRange()); |
| 4163 | RewrittenBlockExprs[BE] = Str; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4164 | |
| 4165 | Stmt *blockTranscribed = SynthBlockInitExpr(BE); |
| 4166 | //blockTranscribed->dump(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4167 | ReplaceStmt(S, blockTranscribed); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4168 | return blockTranscribed; |
| 4169 | } |
| 4170 | // Handle specific things. |
| 4171 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 4172 | return RewriteAtEncode(AtEncode); |
| 4173 | |
| 4174 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 4175 | return RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin()); |
| 4176 | |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4177 | if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 4178 | BinaryOperator *BinOp = PropSetters[PropRefExpr]; |
| 4179 | if (BinOp) { |
| 4180 | // Because the rewriter doesn't allow us to rewrite rewritten code, |
| 4181 | // 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] | 4182 | DisableReplaceStmt = true; |
| 4183 | // Save the source range. Even if we disable the replacement, the |
| 4184 | // rewritten node will have been inserted into the tree. If the synthesized |
| 4185 | // node is at the 'end', the rewriter will fail. Consider this: |
| 4186 | // self.errorHandler = handler ? handler : |
| 4187 | // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; |
| 4188 | SourceRange SrcRange = BinOp->getSourceRange(); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4189 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4190 | DisableReplaceStmt = false; |
Steve Naroff | 4c3580e | 2008-12-04 23:50:32 +0000 | [diff] [blame] | 4191 | // |
| 4192 | // Unlike the main iterator, we explicily avoid changing 'BinOp'. If |
| 4193 | // we changed the RHS of BinOp, the rewriter would fail (since it needs |
| 4194 | // to see the original expression). Consider this example: |
| 4195 | // |
| 4196 | // Foo *obj1, *obj2; |
| 4197 | // |
| 4198 | // obj1.i = [obj2 rrrr]; |
| 4199 | // |
| 4200 | // 'BinOp' for the previous expression looks like: |
| 4201 | // |
| 4202 | // (BinaryOperator 0x231ccf0 'int' '=' |
| 4203 | // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" |
| 4204 | // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) |
| 4205 | // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr |
| 4206 | // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) |
| 4207 | // |
| 4208 | // 'newStmt' represents the rewritten message expression. For example: |
| 4209 | // |
| 4210 | // (CallExpr 0x231d300 'id':'struct objc_object *' |
| 4211 | // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' |
| 4212 | // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' |
| 4213 | // (CStyleCastExpr 0x231d220 'void *' |
| 4214 | // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) |
| 4215 | // |
| 4216 | // Note that 'newStmt' is passed to RewritePropertySetter so that it |
| 4217 | // can be used as the setter argument. ReplaceStmt() will still 'see' |
| 4218 | // the original RHS (since we haven't altered BinOp). |
| 4219 | // |
| 4220 | // This implies the Rewrite* routines can no longer delete the original |
| 4221 | // node. As a result, we now leak the original AST nodes. |
| 4222 | // |
Steve Naroff | b619d95 | 2008-12-09 12:56:34 +0000 | [diff] [blame] | 4223 | return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4224 | } else { |
| 4225 | return RewritePropertyGetter(PropRefExpr); |
Steve Naroff | 15f081d | 2008-12-03 00:56:33 +0000 | [diff] [blame] | 4226 | } |
| 4227 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4228 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 4229 | return RewriteAtSelector(AtSelector); |
| 4230 | |
| 4231 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 4232 | return RewriteObjCStringLiteral(AtString); |
| 4233 | |
| 4234 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4235 | #if 0 |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4236 | // Before we rewrite it, put the original message expression in a comment. |
| 4237 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 4238 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 4239 | |
| 4240 | const char *startBuf = SM->getCharacterData(startLoc); |
| 4241 | const char *endBuf = SM->getCharacterData(endLoc); |
| 4242 | |
| 4243 | std::string messString; |
| 4244 | messString += "// "; |
| 4245 | messString.append(startBuf, endBuf-startBuf+1); |
| 4246 | messString += "\n"; |
| 4247 | |
| 4248 | // FIXME: Missing definition of |
| 4249 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 4250 | // InsertText(startLoc, messString.c_str(), messString.size()); |
| 4251 | // Tried this, but it didn't work either... |
| 4252 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4253 | #endif |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4254 | return RewriteMessageExpr(MessExpr); |
| 4255 | } |
| 4256 | |
| 4257 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 4258 | return RewriteObjCTryStmt(StmtTry); |
| 4259 | |
| 4260 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 4261 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 4262 | |
| 4263 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 4264 | return RewriteObjCThrowStmt(StmtThrow); |
| 4265 | |
| 4266 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 4267 | return RewriteObjCProtocolExpr(ProtocolExp); |
| 4268 | |
| 4269 | if (ObjCForCollectionStmt *StmtForCollection = |
| 4270 | dyn_cast<ObjCForCollectionStmt>(S)) |
| 4271 | return RewriteObjCForCollectionStmt(StmtForCollection, |
| 4272 | OrigStmtRange.getEnd()); |
| 4273 | if (BreakStmt *StmtBreakStmt = |
| 4274 | dyn_cast<BreakStmt>(S)) |
| 4275 | return RewriteBreakStmt(StmtBreakStmt); |
| 4276 | if (ContinueStmt *StmtContinueStmt = |
| 4277 | dyn_cast<ContinueStmt>(S)) |
| 4278 | return RewriteContinueStmt(StmtContinueStmt); |
| 4279 | |
| 4280 | // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls |
| 4281 | // and cast exprs. |
| 4282 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { |
| 4283 | // FIXME: What we're doing here is modifying the type-specifier that |
| 4284 | // precedes the first Decl. In the future the DeclGroup should have |
| 4285 | // a separate type-specifier that we can rewrite. |
| 4286 | RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); |
| 4287 | |
| 4288 | // Blocks rewrite rules. |
| 4289 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); |
| 4290 | DI != DE; ++DI) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4291 | ScopedDecl *SD = *DI; |
| 4292 | if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { |
| 4293 | if (isBlockPointerType(ND->getType())) |
| 4294 | RewriteBlockPointerDecl(ND); |
| 4295 | else if (ND->getType()->isFunctionPointerType()) |
| 4296 | CheckFunctionPointerDecl(ND->getType(), ND); |
| 4297 | } |
| 4298 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { |
| 4299 | if (isBlockPointerType(TD->getUnderlyingType())) |
| 4300 | RewriteBlockPointerDecl(TD); |
| 4301 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4302 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4303 | } |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) |
| 4308 | RewriteObjCQualifiedInterfaceTypes(CE); |
| 4309 | |
| 4310 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 4311 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 4312 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 4313 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 4314 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 4315 | && "Statement stack mismatch"); |
| 4316 | Stmts.pop_back(); |
| 4317 | } |
| 4318 | // Handle blocks rewriting. |
| 4319 | if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 4320 | if (BDRE->isByRef()) |
| 4321 | RewriteBlockDeclRefExpr(BDRE); |
| 4322 | } |
| 4323 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
Steve Naroff | aa4d5ae | 2008-10-30 10:07:53 +0000 | [diff] [blame] | 4324 | if (CE->getCallee()->getType()->isBlockPointerType()) { |
| 4325 | Stmt *BlockCall = SynthesizeBlockCall(CE); |
| 4326 | ReplaceStmt(S, BlockCall); |
| 4327 | return BlockCall; |
| 4328 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4329 | } |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4330 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4331 | RewriteCastExpr(CE); |
| 4332 | } |
| 4333 | #if 0 |
| 4334 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 4335 | CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
| 4336 | // Get the new text. |
| 4337 | std::string SStr; |
| 4338 | llvm::raw_string_ostream Buf(SStr); |
| 4339 | Replacement->printPretty(Buf); |
| 4340 | const std::string &Str = Buf.str(); |
| 4341 | |
| 4342 | printf("CAST = %s\n", &Str[0]); |
| 4343 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 4344 | delete S; |
| 4345 | return Replacement; |
| 4346 | } |
| 4347 | #endif |
| 4348 | // Return this stmt unmodified. |
| 4349 | return S; |
| 4350 | } |
| 4351 | |
| 4352 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 4353 | /// main file of the input. |
| 4354 | void RewriteObjC::HandleDeclInMainFile(Decl *D) { |
| 4355 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4356 | // Since function prototypes don't have ParmDecl's, we check the function |
| 4357 | // prototype. This enables us to rewrite function declarations and |
| 4358 | // definitions using the same code. |
| 4359 | RewriteBlocksInFunctionTypeProto(FD->getType(), FD); |
| 4360 | |
| 4361 | if (Stmt *Body = FD->getBody()) { |
| 4362 | CurFunctionDef = FD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4363 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4364 | CurrentBody = Body; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4365 | FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4366 | CurrentBody = 0; |
| 4367 | if (PropParentMap) { |
| 4368 | delete PropParentMap; |
| 4369 | PropParentMap = 0; |
| 4370 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4371 | // This synthesizes and inserts the block "impl" struct, invoke function, |
| 4372 | // and any copy/dispose helper functions. |
| 4373 | InsertBlockLiteralsWithinFunction(FD); |
| 4374 | CurFunctionDef = 0; |
| 4375 | } |
| 4376 | return; |
| 4377 | } |
| 4378 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 4379 | if (Stmt *Body = MD->getBody()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4380 | CurMethodDef = MD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4381 | CollectPropertySetters(Body); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4382 | CurrentBody = Body; |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4383 | MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4384 | CurrentBody = 0; |
| 4385 | if (PropParentMap) { |
| 4386 | delete PropParentMap; |
| 4387 | PropParentMap = 0; |
| 4388 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4389 | InsertBlockLiteralsWithinMethod(MD); |
| 4390 | CurMethodDef = 0; |
| 4391 | } |
| 4392 | } |
| 4393 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
| 4394 | ClassImplementation.push_back(CI); |
| 4395 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4396 | CategoryImplementation.push_back(CI); |
| 4397 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
| 4398 | RewriteForwardClassDecl(CD); |
| 4399 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4400 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4401 | if (isBlockPointerType(VD->getType())) |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4402 | RewriteBlockPointerDecl(VD); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4403 | else if (VD->getType()->isFunctionPointerType()) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4404 | CheckFunctionPointerDecl(VD->getType(), VD); |
| 4405 | if (VD->getInit()) { |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4406 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4407 | RewriteCastExpr(CE); |
| 4408 | } |
| 4409 | } |
| 4410 | } |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4411 | if (VD->getInit()) { |
| 4412 | GlobalVarDecl = VD; |
Steve Naroff | c77a636 | 2008-12-04 16:24:46 +0000 | [diff] [blame] | 4413 | CollectPropertySetters(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4414 | CurrentBody = VD->getInit(); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4415 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
Steve Naroff | 8599e7a | 2008-12-08 16:43:47 +0000 | [diff] [blame] | 4416 | CurrentBody = 0; |
| 4417 | if (PropParentMap) { |
| 4418 | delete PropParentMap; |
| 4419 | PropParentMap = 0; |
| 4420 | } |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 4421 | SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 4422 | VD->getNameAsCString()); |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4423 | GlobalVarDecl = 0; |
| 4424 | |
| 4425 | // This is needed for blocks. |
Steve Naroff | b2f9e51 | 2008-11-03 23:29:32 +0000 | [diff] [blame] | 4426 | if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { |
Steve Naroff | 8e2f57a | 2008-10-29 18:15:37 +0000 | [diff] [blame] | 4427 | RewriteCastExpr(CE); |
| 4428 | } |
| 4429 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4430 | return; |
| 4431 | } |
| 4432 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 4433 | if (isBlockPointerType(TD->getUnderlyingType())) |
| 4434 | RewriteBlockPointerDecl(TD); |
| 4435 | else if (TD->getUnderlyingType()->isFunctionPointerType()) |
| 4436 | CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); |
| 4437 | return; |
| 4438 | } |
| 4439 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { |
| 4440 | if (RD->isDefinition()) { |
Douglas Gregor | a4c46df | 2008-12-11 17:59:21 +0000 | [diff] [blame^] | 4441 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4442 | e = RD->field_end(); i != e; ++i) { |
| 4443 | FieldDecl *FD = *i; |
| 4444 | if (isBlockPointerType(FD->getType())) |
| 4445 | RewriteBlockPointerDecl(FD); |
| 4446 | } |
| 4447 | } |
| 4448 | return; |
| 4449 | } |
| 4450 | // Nothing yet. |
| 4451 | } |
| 4452 | |
| 4453 | void RewriteObjC::HandleTranslationUnit(TranslationUnit& TU) { |
| 4454 | // Get the top-level buffer that this corresponds to. |
| 4455 | |
| 4456 | // Rewrite tabs if we care. |
| 4457 | //RewriteTabs(); |
| 4458 | |
| 4459 | if (Diags.hasErrorOccurred()) |
| 4460 | return; |
| 4461 | |
| 4462 | // Create the output file. |
| 4463 | |
| 4464 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 4465 | llvm::raw_ostream *OutFile; |
| 4466 | if (OutFileName == "-") { |
| 4467 | OutFile = &llvm::outs(); |
| 4468 | } else if (!OutFileName.empty()) { |
| 4469 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 4470 | OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), false, Err); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4471 | OwnedStream.reset(OutFile); |
| 4472 | } else if (InFileName == "-") { |
| 4473 | OutFile = &llvm::outs(); |
| 4474 | } else { |
| 4475 | llvm::sys::Path Path(InFileName); |
| 4476 | Path.eraseSuffix(); |
| 4477 | Path.appendSuffix("cpp"); |
| 4478 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 4479 | OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), false, Err); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4480 | OwnedStream.reset(OutFile); |
| 4481 | } |
| 4482 | |
| 4483 | RewriteInclude(); |
| 4484 | |
| 4485 | InsertText(SourceLocation::getFileLoc(MainFileID, 0), |
| 4486 | Preamble.c_str(), Preamble.size(), false); |
| 4487 | |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4488 | if (ClassImplementation.size() || CategoryImplementation.size()) |
| 4489 | RewriteImplementations(); |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4490 | |
| 4491 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 4492 | // we are done. |
| 4493 | if (const RewriteBuffer *RewriteBuf = |
| 4494 | Rewrite.getRewriteBufferFor(MainFileID)) { |
| 4495 | //printf("Changed:\n"); |
| 4496 | *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
| 4497 | } else { |
| 4498 | fprintf(stderr, "No changes\n"); |
| 4499 | } |
Steve Naroff | ace6625 | 2008-11-13 20:07:04 +0000 | [diff] [blame] | 4500 | |
Steve Naroff | 0aab796 | 2008-11-14 14:10:01 +0000 | [diff] [blame] | 4501 | if (ClassImplementation.size() || CategoryImplementation.size()) { |
| 4502 | // Rewrite Objective-c meta data* |
| 4503 | std::string ResultStr; |
| 4504 | SynthesizeMetaDataIntoBuffer(ResultStr); |
| 4505 | // Emit metadata. |
| 4506 | *OutFile << ResultStr; |
| 4507 | } |
Steve Naroff | fa15fd9 | 2008-10-28 20:29:00 +0000 | [diff] [blame] | 4508 | OutFile->flush(); |
| 4509 | } |
| 4510 | |