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