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