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