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