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