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