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