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