Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 1 | //===--- RewriteTest.cpp - Playground for the code rewriter ---------------===// |
| 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" |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 19 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 26 | #include <sstream> |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 28 | using llvm::utostr; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 29 | |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 30 | static llvm::cl::opt<bool> |
| 31 | SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false), |
| 32 | llvm::cl::desc("Silence ObjC rewriting warnings")); |
| 33 | |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 34 | namespace { |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 35 | class RewriteTest : public ASTConsumer { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 36 | Rewriter Rewrite; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 37 | Diagnostic &Diags; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 38 | const LangOptions &LangOpts; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 39 | unsigned RewriteFailedDiag; |
| 40 | |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 41 | ASTContext *Context; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 42 | SourceManager *SM; |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 43 | unsigned MainFileID; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 44 | const char *MainFileStart, *MainFileEnd; |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 45 | SourceLocation LastIncLoc; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 46 | llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; |
| 47 | llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; |
| 48 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; |
| 49 | llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; |
| 50 | llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 51 | llvm::SmallVector<Stmt *, 32> Stmts; |
| 52 | llvm::SmallVector<int, 8> ObjCBcLabelNo; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 53 | llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 54 | |
| 55 | FunctionDecl *MsgSendFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 56 | FunctionDecl *MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 57 | FunctionDecl *MsgSendStretFunctionDecl; |
| 58 | FunctionDecl *MsgSendSuperStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 59 | FunctionDecl *MsgSendFpretFunctionDecl; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 60 | FunctionDecl *GetClassFunctionDecl; |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 61 | FunctionDecl *GetMetaClassFunctionDecl; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 62 | FunctionDecl *SelGetUidFunctionDecl; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 63 | FunctionDecl *CFStringFunctionDecl; |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 64 | FunctionDecl *GetProtocolFunctionDecl; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 65 | FunctionDecl *SuperContructorFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 66 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 67 | // ObjC string constant support. |
| 68 | FileVarDecl *ConstantStringClassReference; |
| 69 | RecordDecl *NSStringRecord; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 70 | |
Fariborz Jahanian | b586cce | 2008-01-16 00:09:11 +0000 | [diff] [blame] | 71 | // ObjC foreach break/continue generation support. |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 72 | int BcLabelCount; |
| 73 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 74 | // Needed for super. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 75 | ObjCMethodDecl *CurMethodDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 76 | RecordDecl *SuperStructDecl; |
| 77 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 78 | // Needed for header files being rewritten |
| 79 | bool IsHeader; |
| 80 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 81 | static const int OBJC_ABI_VERSION =7 ; |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 82 | public: |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 83 | void Initialize(ASTContext &context); |
| 84 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 85 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 86 | // Top Level Driver code. |
| 87 | virtual void HandleTopLevelDecl(Decl *D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 88 | void HandleDeclInMainFile(Decl *D); |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 89 | RewriteTest(bool isHeader, Diagnostic &D, const LangOptions &LOpts) : |
| 90 | Diags(D), LangOpts(LOpts) { |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 91 | IsHeader = isHeader; |
Steve Naroff | 0113c9d | 2008-01-28 21:34:52 +0000 | [diff] [blame] | 92 | RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, |
| 93 | "rewriting sub-expression within a macro (may not be correct)"); |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 95 | ~RewriteTest(); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 96 | |
| 97 | void ReplaceStmt(Stmt *Old, Stmt *New) { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 98 | // If replacement succeeded or warning disabled return with no warning. |
| 99 | if (!Rewrite.ReplaceStmt(Old, New) || SilenceRewriteMacroWarning) |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 100 | return; |
| 101 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 102 | SourceRange Range = Old->getSourceRange(); |
| 103 | Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag, |
| 104 | 0, 0, &Range, 1); |
| 105 | } |
| 106 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 107 | void InsertText(SourceLocation Loc, const char *StrData, unsigned StrLen) { |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 108 | // If insertion succeeded or warning disabled return with no warning. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 109 | if (!Rewrite.InsertText(Loc, StrData, StrLen) || |
| 110 | SilenceRewriteMacroWarning) |
| 111 | return; |
| 112 | |
| 113 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 114 | } |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 115 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 116 | void RemoveText(SourceLocation Loc, unsigned StrLen) { |
| 117 | // If removal succeeded or warning disabled return with no warning. |
| 118 | if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) |
| 119 | return; |
| 120 | |
| 121 | Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); |
| 122 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 123 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 124 | void ReplaceText(SourceLocation Start, unsigned OrigLength, |
| 125 | const char *NewStr, unsigned NewLength) { |
| 126 | // If removal succeeded or warning disabled return with no warning. |
| 127 | if (!Rewrite.ReplaceText(Start, OrigLength, NewStr, NewLength) || |
| 128 | SilenceRewriteMacroWarning) |
| 129 | return; |
| 130 | |
| 131 | Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); |
| 132 | } |
| 133 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 134 | // Syntactic Rewriting. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 135 | void RewritePrologue(SourceLocation Loc); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 136 | void RewriteInclude(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 137 | void RewriteTabs(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 138 | void RewriteForwardClassDecl(ObjCClassDecl *Dcl); |
| 139 | void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 140 | void RewriteImplementationDecl(NamedDecl *Dcl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 141 | void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); |
| 142 | void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); |
| 143 | void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); |
| 144 | void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); |
| 145 | void RewriteMethodDeclaration(ObjCMethodDecl *Method); |
| 146 | void RewriteProperties(int nProperties, ObjCPropertyDecl **Properties); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 147 | void RewriteFunctionDecl(FunctionDecl *FD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 148 | void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 149 | bool needToScanForQualifiers(QualType T); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 150 | ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 151 | QualType getSuperStructType(); |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 152 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 153 | // Expression Rewriting. |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 154 | Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 155 | Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 156 | Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 157 | Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 158 | Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 159 | Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 160 | Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 161 | Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 162 | Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 163 | Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); |
| 164 | Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); |
| 165 | Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 166 | Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 167 | SourceLocation OrigEnd); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 168 | CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, |
| 169 | Expr **args, unsigned nargs); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 170 | Stmt *SynthMessageExpr(ObjCMessageExpr *Exp); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 171 | Stmt *RewriteBreakStmt(BreakStmt *S); |
| 172 | Stmt *RewriteContinueStmt(ContinueStmt *S); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 173 | void SynthCountByEnumWithState(std::string &buf); |
| 174 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 175 | void SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 176 | void SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 177 | void SynthMsgSendStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 178 | void SynthMsgSendFpretFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 179 | void SynthMsgSendSuperStretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 180 | void SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 181 | void SynthGetMetaClassFunctionDecl(); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 182 | void SynthCFStringFunctionDecl(); |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 183 | void SynthSelGetUidFunctionDecl(); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 184 | void SynthGetProtocolFunctionDecl(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 185 | void SynthSuperContructorFunctionDecl(); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 186 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 187 | // Metadata emission. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 188 | void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 189 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 191 | void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 192 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 194 | typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator; |
| 195 | void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 196 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 197 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 198 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 199 | const char *ClassName, |
| 200 | std::string &Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 202 | void RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 203 | int NumProtocols, |
| 204 | const char *prefix, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 205 | const char *ClassName, |
| 206 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 207 | void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 208 | std::string &Result); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 209 | void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 210 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 211 | std::string &Result); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 212 | void RewriteImplementations(std::string &Result); |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 213 | }; |
| 214 | } |
| 215 | |
Fariborz Jahanian | b4b2f0c | 2008-01-18 01:15:54 +0000 | [diff] [blame] | 216 | static bool IsHeaderFile(const std::string &Filename) { |
| 217 | std::string::size_type DotPos = Filename.rfind('.'); |
| 218 | |
| 219 | if (DotPos == std::string::npos) { |
| 220 | // no file extension |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 225 | // C header: .h |
| 226 | // C++ header: .hh or .H; |
| 227 | return Ext == "h" || Ext == "hh" || Ext == "H"; |
| 228 | } |
| 229 | |
| 230 | ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile, |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 231 | Diagnostic &Diags, |
| 232 | const LangOptions &LOpts) { |
| 233 | return new RewriteTest(IsHeaderFile(InFile), Diags, LOpts); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | 77cd2a0 | 2007-10-11 00:43:27 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 236 | void RewriteTest::Initialize(ASTContext &context) { |
| 237 | Context = &context; |
| 238 | SM = &Context->getSourceManager(); |
| 239 | MsgSendFunctionDecl = 0; |
| 240 | MsgSendSuperFunctionDecl = 0; |
| 241 | MsgSendStretFunctionDecl = 0; |
| 242 | MsgSendSuperStretFunctionDecl = 0; |
| 243 | MsgSendFpretFunctionDecl = 0; |
| 244 | GetClassFunctionDecl = 0; |
| 245 | GetMetaClassFunctionDecl = 0; |
| 246 | SelGetUidFunctionDecl = 0; |
| 247 | CFStringFunctionDecl = 0; |
| 248 | GetProtocolFunctionDecl = 0; |
| 249 | ConstantStringClassReference = 0; |
| 250 | NSStringRecord = 0; |
| 251 | CurMethodDecl = 0; |
| 252 | SuperStructDecl = 0; |
| 253 | BcLabelCount = 0; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 254 | SuperContructorFunctionDecl = 0; |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 255 | |
| 256 | // Get the ID and start/end of the main file. |
| 257 | MainFileID = SM->getMainFileID(); |
| 258 | const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); |
| 259 | MainFileStart = MainBuf->getBufferStart(); |
| 260 | MainFileEnd = MainBuf->getBufferEnd(); |
| 261 | |
| 262 | |
| 263 | Rewrite.setSourceMgr(Context->getSourceManager()); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 265 | // declaring objc_selector outside the parameter list removes a silly |
| 266 | // scope related warning... |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 267 | std::string S = "#pragma once\n"; |
| 268 | S += "struct objc_selector; struct objc_class;\n"; |
| 269 | S += "#ifndef OBJC_SUPER\n"; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 270 | S += "struct objc_super { struct objc_object *object; "; |
| 271 | S += "struct objc_object *superClass; "; |
| 272 | if (LangOpts.Microsoft) { |
| 273 | // Add a constructor for creating temporary objects. |
| 274 | S += "objc_super(struct objc_object *o, struct objc_object *s) : "; |
| 275 | S += "object(o), superClass(s) {} "; |
| 276 | } |
| 277 | S += "};\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 278 | S += "#define OBJC_SUPER\n"; |
| 279 | S += "#endif\n"; |
| 280 | S += "#ifndef _REWRITER_typedef_Protocol\n"; |
| 281 | S += "typedef struct objc_object Protocol;\n"; |
| 282 | S += "#define _REWRITER_typedef_Protocol\n"; |
| 283 | S += "#endif\n"; |
| 284 | S += "extern struct objc_object *objc_msgSend"; |
| 285 | S += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 286 | S += "extern struct objc_object *objc_msgSendSuper"; |
| 287 | S += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 288 | S += "extern struct objc_object *objc_msgSend_stret"; |
| 289 | S += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 290 | S += "extern struct objc_object *objc_msgSendSuper_stret"; |
| 291 | S += "(struct objc_super *, struct objc_selector *, ...);\n"; |
| 292 | S += "extern struct objc_object *objc_msgSend_fpret"; |
| 293 | S += "(struct objc_object *, struct objc_selector *, ...);\n"; |
| 294 | S += "extern struct objc_object *objc_getClass"; |
| 295 | S += "(const char *);\n"; |
| 296 | S += "extern struct objc_object *objc_getMetaClass"; |
| 297 | S += "(const char *);\n"; |
| 298 | S += "extern void objc_exception_throw(struct objc_object *);\n"; |
| 299 | S += "extern void objc_exception_try_enter(void *);\n"; |
| 300 | S += "extern void objc_exception_try_exit(void *);\n"; |
| 301 | S += "extern struct objc_object *objc_exception_extract(void *);\n"; |
| 302 | S += "extern int objc_exception_match"; |
| 303 | S += "(struct objc_class *, struct objc_object *, ...);\n"; |
| 304 | S += "extern Protocol *objc_getProtocol(const char *);\n"; |
| 305 | S += "#include <objc/objc.h>\n"; |
| 306 | S += "#ifndef __FASTENUMERATIONSTATE\n"; |
| 307 | S += "struct __objcFastEnumerationState {\n\t"; |
| 308 | S += "unsigned long state;\n\t"; |
| 309 | S += "id *itemsPtr;\n\t"; |
| 310 | S += "unsigned long *mutationsPtr;\n\t"; |
| 311 | S += "unsigned long extra[5];\n};\n"; |
| 312 | S += "#define __FASTENUMERATIONSTATE\n"; |
| 313 | S += "#endif\n"; |
| 314 | #if 0 |
| 315 | if (LangOpts.Microsoft) |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 316 | S += "#define __attribute__(X)\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 317 | #endif |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 318 | if (IsHeader) { |
| 319 | // insert the whole string when rewriting a header file |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 320 | InsertText(SourceLocation::getFileLoc(MainFileID, 0), S.c_str(), S.size()); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 321 | } |
| 322 | else { |
| 323 | // Not rewriting header, exclude the #pragma once pragma |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 324 | const char *p = S.c_str() + strlen("#pragma once\n"); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 325 | InsertText(SourceLocation::getFileLoc(MainFileID, 0), p, strlen(p)); |
Chris Lattner | 9e13c2e | 2008-01-31 19:38:44 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 330 | //===----------------------------------------------------------------------===// |
| 331 | // Top Level Driver Code |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 334 | void RewriteTest::HandleTopLevelDecl(Decl *D) { |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 335 | // Two cases: either the decl could be in the main file, or it could be in a |
| 336 | // #included file. If the former, rewrite it now. If the later, check to see |
| 337 | // if we rewrote the #include/#import. |
| 338 | SourceLocation Loc = D->getLocation(); |
| 339 | Loc = SM->getLogicalLoc(Loc); |
| 340 | |
| 341 | // If this is for a builtin, ignore it. |
| 342 | if (Loc.isInvalid()) return; |
| 343 | |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 344 | // Look for built-in declarations that we need to refer during the rewrite. |
| 345 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 346 | RewriteFunctionDecl(FD); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 347 | } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) { |
| 348 | // declared in <Foundation/NSString.h> |
| 349 | if (strcmp(FVD->getName(), "_NSConstantStringClassReference") == 0) { |
| 350 | ConstantStringClassReference = FVD; |
| 351 | return; |
| 352 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 353 | } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 354 | RewriteInterfaceDecl(MD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 355 | } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 356 | RewriteCategoryDecl(CD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 357 | } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 358 | RewriteProtocolDecl(PD); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 359 | } else if (ObjCForwardProtocolDecl *FP = |
| 360 | dyn_cast<ObjCForwardProtocolDecl>(D)){ |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 361 | RewriteForwardProtocolDecl(FP); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 363 | // If we have a decl in the main file, see if we should rewrite it. |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 364 | if (SM->getDecomposedFileLoc(Loc).first == MainFileID) |
| 365 | return HandleDeclInMainFile(D); |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 368 | /// HandleDeclInMainFile - This is called for each top-level decl defined in the |
| 369 | /// main file of the input. |
| 370 | void RewriteTest::HandleDeclInMainFile(Decl *D) { |
| 371 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 372 | if (Stmt *Body = FD->getBody()) |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 373 | FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 375 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 376 | if (Stmt *Body = MD->getBody()) { |
| 377 | //Body->dump(); |
| 378 | CurMethodDecl = MD; |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 379 | MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body)); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 380 | CurMethodDecl = 0; |
| 381 | } |
Steve Naroff | 71c0a95 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 382 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 383 | if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 384 | ClassImplementation.push_back(CI); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 385 | else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 386 | CategoryImplementation.push_back(CI); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 387 | else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 388 | RewriteForwardClassDecl(CD); |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 389 | else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 390 | RewriteObjCQualifiedInterfaceTypes(VD); |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 391 | if (VD->getInit()) |
| 392 | RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); |
| 393 | } |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 394 | // Nothing yet. |
| 395 | } |
| 396 | |
| 397 | RewriteTest::~RewriteTest() { |
| 398 | // Get the top-level buffer that this corresponds to. |
Chris Lattner | 74a0c77 | 2007-11-08 04:27:23 +0000 | [diff] [blame] | 399 | |
| 400 | // Rewrite tabs if we care. |
| 401 | //RewriteTabs(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 402 | |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 403 | RewriteInclude(); |
| 404 | |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 405 | // Rewrite Objective-c meta data* |
| 406 | std::string ResultStr; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 407 | RewriteImplementations(ResultStr); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 408 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 409 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
| 410 | // we are done. |
| 411 | if (const RewriteBuffer *RewriteBuf = |
| 412 | Rewrite.getRewriteBufferFor(MainFileID)) { |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 413 | //printf("Changed:\n"); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 414 | std::string S(RewriteBuf->begin(), RewriteBuf->end()); |
| 415 | printf("%s\n", S.c_str()); |
| 416 | } else { |
| 417 | printf("No changes\n"); |
| 418 | } |
Fariborz Jahanian | 4402d81 | 2007-11-07 18:40:28 +0000 | [diff] [blame] | 419 | // Emit metadata. |
| 420 | printf("%s", ResultStr.c_str()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 423 | //===----------------------------------------------------------------------===// |
| 424 | // Syntactic (non-AST) Rewriting Code |
| 425 | //===----------------------------------------------------------------------===// |
| 426 | |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 427 | void RewriteTest::RewriteInclude() { |
| 428 | SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0); |
| 429 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 430 | const char *MainBufStart = MainBuf.first; |
| 431 | const char *MainBufEnd = MainBuf.second; |
| 432 | size_t ImportLen = strlen("import"); |
| 433 | size_t IncludeLen = strlen("include"); |
| 434 | |
Fariborz Jahanian | af57b46 | 2008-01-19 01:03:17 +0000 | [diff] [blame] | 435 | // Loop over the whole file, looking for includes. |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 436 | for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { |
| 437 | if (*BufPtr == '#') { |
| 438 | if (++BufPtr == MainBufEnd) |
| 439 | return; |
| 440 | while (*BufPtr == ' ' || *BufPtr == '\t') |
| 441 | if (++BufPtr == MainBufEnd) |
| 442 | return; |
| 443 | if (!strncmp(BufPtr, "import", ImportLen)) { |
| 444 | // replace import with include |
| 445 | SourceLocation ImportLoc = |
| 446 | LocStart.getFileLocWithOffset(BufPtr-MainBufStart); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 447 | ReplaceText(ImportLoc, ImportLen, "include", IncludeLen); |
Fariborz Jahanian | 452b899 | 2008-01-19 00:30:35 +0000 | [diff] [blame] | 448 | BufPtr += ImportLen; |
| 449 | } |
| 450 | } |
| 451 | } |
Chris Lattner | 2c64b7b | 2007-10-16 21:07:07 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 454 | void RewriteTest::RewriteTabs() { |
| 455 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
| 456 | const char *MainBufStart = MainBuf.first; |
| 457 | const char *MainBufEnd = MainBuf.second; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 458 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 459 | // Loop over the whole file, looking for tabs. |
| 460 | for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { |
| 461 | if (*BufPtr != '\t') |
| 462 | continue; |
| 463 | |
| 464 | // Okay, we found a tab. This tab will turn into at least one character, |
| 465 | // but it depends on which 'virtual column' it is in. Compute that now. |
| 466 | unsigned VCol = 0; |
| 467 | while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && |
| 468 | BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') |
| 469 | ++VCol; |
| 470 | |
| 471 | // Okay, now that we know the virtual column, we know how many spaces to |
| 472 | // insert. We assume 8-character tab-stops. |
| 473 | unsigned Spaces = 8-(VCol & 7); |
| 474 | |
| 475 | // Get the location of the tab. |
| 476 | SourceLocation TabLoc = |
| 477 | SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart); |
| 478 | |
| 479 | // Rewrite the single tab character into a sequence of spaces. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 480 | ReplaceText(TabLoc, 1, " ", Spaces); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 481 | } |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 485 | void RewriteTest::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 486 | int numDecls = ClassDecl->getNumForwardDecls(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 487 | ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls(); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 488 | |
| 489 | // Get the start location and compute the semi location. |
| 490 | SourceLocation startLoc = ClassDecl->getLocation(); |
| 491 | const char *startBuf = SM->getCharacterData(startLoc); |
| 492 | const char *semiPtr = strchr(startBuf, ';'); |
| 493 | |
| 494 | // Translate to typedef's that forward reference structs with the same name |
| 495 | // as the class. As a convenience, we include the original declaration |
| 496 | // as a comment. |
| 497 | std::string typedefString; |
| 498 | typedefString += "// "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 499 | typedefString.append(startBuf, semiPtr-startBuf+1); |
| 500 | typedefString += "\n"; |
| 501 | for (int i = 0; i < numDecls; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 502 | ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i]; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 503 | typedefString += "#ifndef _REWRITER_typedef_"; |
| 504 | typedefString += ForwardDecl->getName(); |
| 505 | typedefString += "\n"; |
| 506 | typedefString += "#define _REWRITER_typedef_"; |
| 507 | typedefString += ForwardDecl->getName(); |
| 508 | typedefString += "\n"; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 509 | typedefString += "typedef struct objc_object "; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 510 | typedefString += ForwardDecl->getName(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 511 | typedefString += ";\n#endif\n"; |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Replace the @class with typedefs corresponding to the classes. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 515 | ReplaceText(startLoc, semiPtr-startBuf+1, |
| 516 | typedefString.c_str(), typedefString.size()); |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 519 | void RewriteTest::RewriteMethodDeclaration(ObjCMethodDecl *Method) { |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 520 | SourceLocation LocStart = Method->getLocStart(); |
| 521 | SourceLocation LocEnd = Method->getLocEnd(); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 522 | |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 523 | if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 524 | InsertText(LocStart, "/* ", 3); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 525 | ReplaceText(LocEnd, 1, ";*/ ", 4); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 526 | } else { |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 527 | InsertText(LocStart, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 531 | void RewriteTest::RewriteProperties(int nProperties, ObjCPropertyDecl **Properties) |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 532 | { |
| 533 | for (int i = 0; i < nProperties; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 534 | ObjCPropertyDecl *Property = Properties[i]; |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 535 | SourceLocation Loc = Property->getLocation(); |
| 536 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 537 | ReplaceText(Loc, 0, "// ", 3); |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 538 | |
| 539 | // FIXME: handle properties that are declared across multiple lines. |
| 540 | } |
| 541 | } |
| 542 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 543 | void RewriteTest::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 544 | SourceLocation LocStart = CatDecl->getLocStart(); |
| 545 | |
| 546 | // FIXME: handle category headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 547 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 548 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 549 | for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 550 | E = CatDecl->instmeth_end(); I != E; ++I) |
| 551 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 552 | for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 553 | E = CatDecl->classmeth_end(); I != E; ++I) |
| 554 | RewriteMethodDeclaration(*I); |
| 555 | |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 556 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 557 | ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 560 | void RewriteTest::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 561 | std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 562 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 563 | SourceLocation LocStart = PDecl->getLocStart(); |
| 564 | |
| 565 | // FIXME: handle protocol headers that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 566 | ReplaceText(LocStart, 0, "// ", 3); |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 567 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 568 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 569 | E = PDecl->instmeth_end(); I != E; ++I) |
| 570 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 571 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 572 | E = PDecl->classmeth_end(); I != E; ++I) |
| 573 | RewriteMethodDeclaration(*I); |
| 574 | |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 575 | // Lastly, comment out the @end. |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 576 | SourceLocation LocEnd = PDecl->getAtEndLoc(); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 577 | ReplaceText(LocEnd, 0, "// ", 3); |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 578 | |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 579 | // Must comment out @optional/@required |
| 580 | const char *startBuf = SM->getCharacterData(LocStart); |
| 581 | const char *endBuf = SM->getCharacterData(LocEnd); |
| 582 | for (const char *p = startBuf; p < endBuf; p++) { |
| 583 | if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { |
| 584 | std::string CommentedOptional = "/* @optional */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 585 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 586 | ReplaceText(OptionalLoc, strlen("@optional"), |
| 587 | CommentedOptional.c_str(), CommentedOptional.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 588 | |
| 589 | } |
| 590 | else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { |
| 591 | std::string CommentedRequired = "/* @required */"; |
Steve Naroff | 8cc764c | 2007-11-14 15:03:57 +0000 | [diff] [blame] | 592 | SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 593 | ReplaceText(OptionalLoc, strlen("@required"), |
| 594 | CommentedRequired.c_str(), CommentedRequired.size()); |
Fariborz Jahanian | b82b3ea | 2007-11-14 01:37:46 +0000 | [diff] [blame] | 595 | |
| 596 | } |
| 597 | } |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 600 | void RewriteTest::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 601 | SourceLocation LocStart = PDecl->getLocation(); |
Steve Naroff | b7fa992 | 2007-11-14 03:37:28 +0000 | [diff] [blame] | 602 | if (LocStart.isInvalid()) |
| 603 | assert(false && "Invalid SourceLocation"); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 604 | // FIXME: handle forward protocol that are declared across multiple lines. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 605 | ReplaceText(LocStart, 0, "// ", 3); |
Fariborz Jahanian | d175ddf | 2007-11-14 00:42:16 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 608 | void RewriteTest::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 609 | std::string &ResultStr) { |
| 610 | ResultStr += "\nstatic "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 611 | if (OMD->getResultType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 612 | ResultStr += "id"; |
| 613 | else |
| 614 | ResultStr += OMD->getResultType().getAsString(); |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 615 | ResultStr += " "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 616 | |
| 617 | // Unique method name |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 618 | std::string NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 619 | |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 620 | if (OMD->isInstance()) |
| 621 | NameStr += "_I_"; |
| 622 | else |
| 623 | NameStr += "_C_"; |
| 624 | |
| 625 | NameStr += OMD->getClassInterface()->getName(); |
| 626 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 627 | |
| 628 | NamedDecl *MethodContext = OMD->getMethodContext(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 629 | if (ObjCCategoryImplDecl *CID = |
| 630 | dyn_cast<ObjCCategoryImplDecl>(MethodContext)) { |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 631 | NameStr += CID->getName(); |
| 632 | NameStr += "_"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 633 | } |
| 634 | // Append selector names, replacing ':' with '_' |
| 635 | const char *selName = OMD->getSelector().getName().c_str(); |
| 636 | if (!strchr(selName, ':')) |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 637 | NameStr += OMD->getSelector().getName(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 638 | else { |
| 639 | std::string selString = OMD->getSelector().getName(); |
| 640 | int len = selString.size(); |
| 641 | for (int i = 0; i < len; i++) |
| 642 | if (selString[i] == ':') |
| 643 | selString[i] = '_'; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 644 | NameStr += selString; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 645 | } |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 646 | // Remember this name for metadata emission |
| 647 | MethodInternalNames[OMD] = NameStr; |
| 648 | ResultStr += NameStr; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 649 | |
| 650 | // Rewrite arguments |
| 651 | ResultStr += "("; |
| 652 | |
| 653 | // invisible arguments |
| 654 | if (OMD->isInstance()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 655 | QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 656 | selfTy = Context->getPointerType(selfTy); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 657 | if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 658 | ResultStr += "struct "; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 659 | ResultStr += OMD->getClassInterface()->getName(); |
| 660 | if (LangOpts.Microsoft) |
| 661 | ResultStr += "_IMPL"; |
| 662 | ResultStr += " *"; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 663 | } |
| 664 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 665 | ResultStr += Context->getObjCIdType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 666 | |
| 667 | ResultStr += " self, "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 668 | ResultStr += Context->getObjCSelType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 669 | ResultStr += " _cmd"; |
| 670 | |
| 671 | // Method arguments. |
| 672 | for (int i = 0; i < OMD->getNumParams(); i++) { |
| 673 | ParmVarDecl *PDecl = OMD->getParamDecl(i); |
| 674 | ResultStr += ", "; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 675 | if (PDecl->getType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 676 | ResultStr += "id"; |
| 677 | else |
| 678 | ResultStr += PDecl->getType().getAsString(); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 679 | ResultStr += " "; |
| 680 | ResultStr += PDecl->getName(); |
| 681 | } |
Fariborz Jahanian | 7c39ff7 | 2008-01-21 20:14:23 +0000 | [diff] [blame] | 682 | if (OMD->isVariadic()) |
| 683 | ResultStr += ", ..."; |
Fariborz Jahanian | 531a1ea | 2008-01-10 01:39:52 +0000 | [diff] [blame] | 684 | ResultStr += ") "; |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 685 | |
| 686 | } |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 687 | void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 688 | ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); |
| 689 | ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 690 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 691 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 692 | InsertText(IMD->getLocStart(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 693 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 694 | InsertText(CID->getLocStart(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 695 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 696 | for (ObjCCategoryImplDecl::instmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 697 | I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), |
| 698 | E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 699 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 700 | ObjCMethodDecl *OMD = *I; |
| 701 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 702 | SourceLocation LocStart = OMD->getLocStart(); |
| 703 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 704 | |
| 705 | const char *startBuf = SM->getCharacterData(LocStart); |
| 706 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 707 | ReplaceText(LocStart, endBuf-startBuf, |
| 708 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 711 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 712 | I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), |
| 713 | E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 714 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 715 | ObjCMethodDecl *OMD = *I; |
| 716 | RewriteObjCMethodDecl(OMD, ResultStr); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 717 | SourceLocation LocStart = OMD->getLocStart(); |
| 718 | SourceLocation LocEnd = OMD->getBody()->getLocStart(); |
| 719 | |
| 720 | const char *startBuf = SM->getCharacterData(LocStart); |
| 721 | const char *endBuf = SM->getCharacterData(LocEnd); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 722 | ReplaceText(LocStart, endBuf-startBuf, |
| 723 | ResultStr.c_str(), ResultStr.size()); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 724 | } |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 725 | if (IMD) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 726 | InsertText(IMD->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 727 | else |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 728 | InsertText(CID->getLocEnd(), "// ", 3); |
Fariborz Jahanian | 48a0b6a | 2007-11-13 18:44:14 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 731 | void RewriteTest::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 732 | std::string ResultStr; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 733 | if (!ObjCForwardDecls.count(ClassDecl)) { |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 734 | // we haven't seen a forward decl - generate a typedef. |
Steve Naroff | 5086a8d | 2007-11-14 23:02:56 +0000 | [diff] [blame] | 735 | ResultStr = "#ifndef _REWRITER_typedef_"; |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 736 | ResultStr += ClassDecl->getName(); |
| 737 | ResultStr += "\n"; |
| 738 | ResultStr += "#define _REWRITER_typedef_"; |
| 739 | ResultStr += ClassDecl->getName(); |
| 740 | ResultStr += "\n"; |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 741 | ResultStr += "typedef struct objc_object "; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 742 | ResultStr += ClassDecl->getName(); |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 743 | ResultStr += ";\n#endif\n"; |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 744 | // Mark this typedef as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 745 | ObjCForwardDecls.insert(ClassDecl); |
Steve Naroff | 6c6a2db | 2007-11-01 03:35:41 +0000 | [diff] [blame] | 746 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 747 | SynthesizeObjCInternalStruct(ClassDecl, ResultStr); |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 748 | |
Fariborz Jahanian | 957cf65 | 2007-11-07 00:09:37 +0000 | [diff] [blame] | 749 | RewriteProperties(ClassDecl->getNumPropertyDecl(), |
| 750 | ClassDecl->getPropertyDecl()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 751 | for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 752 | E = ClassDecl->instmeth_end(); I != E; ++I) |
| 753 | RewriteMethodDeclaration(*I); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 754 | for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 755 | E = ClassDecl->classmeth_end(); I != E; ++I) |
| 756 | RewriteMethodDeclaration(*I); |
| 757 | |
Steve Naroff | 2feac5e | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 758 | // Lastly, comment out the @end. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 759 | ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3); |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 762 | Stmt *RewriteTest::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 763 | ObjCIvarDecl *D = IV->getDecl(); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 764 | if (IV->isFreeIvar()) { |
| 765 | Expr *Replacement = new MemberExpr(IV->getBase(), true, D, |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 766 | IV->getLocation(), D->getType()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 767 | ReplaceStmt(IV, Replacement); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 768 | delete IV; |
| 769 | return Replacement; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 770 | } else { |
Fariborz Jahanian | 7da8d94 | 2008-01-23 20:34:40 +0000 | [diff] [blame] | 771 | #if 0 |
| 772 | /// This code is not right. It seems unnecessary. It breaks use of |
| 773 | /// ivar reference used as 'receiver' of an expression; as in: |
| 774 | /// [newInv->_container addObject:0]; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 775 | if (CurMethodDecl) { |
| 776 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 777 | ObjCInterfaceType *intT = dyn_cast<ObjCInterfaceType>(pType->getPointeeType()); |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 778 | if (CurMethodDecl->getClassInterface() == intT->getDecl()) { |
| 779 | IdentifierInfo *II = intT->getDecl()->getIdentifier(); |
| 780 | RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), |
| 781 | II, 0); |
| 782 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 783 | |
| 784 | CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation()); |
| 785 | // Don't forget the parens to enforce the proper binding. |
| 786 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), castExpr); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 787 | ReplaceStmt(IV->getBase(), PE); |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 788 | delete IV->getBase(); |
| 789 | return PE; |
| 790 | } |
| 791 | } |
| 792 | } |
Fariborz Jahanian | 7da8d94 | 2008-01-23 20:34:40 +0000 | [diff] [blame] | 793 | #endif |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 794 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 795 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 798 | //===----------------------------------------------------------------------===// |
| 799 | // Function Body / Expression rewriting |
| 800 | //===----------------------------------------------------------------------===// |
| 801 | |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 802 | Stmt *RewriteTest::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 803 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 804 | isa<DoStmt>(S) || isa<ForStmt>(S)) |
| 805 | Stmts.push_back(S); |
| 806 | else if (isa<ObjCForCollectionStmt>(S)) { |
| 807 | Stmts.push_back(S); |
| 808 | ObjCBcLabelNo.push_back(++BcLabelCount); |
| 809 | } |
| 810 | |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 811 | SourceLocation OrigStmtEnd = S->getLocEnd(); |
| 812 | |
| 813 | // Start by rewriting all children. |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 814 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 815 | CI != E; ++CI) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 816 | if (*CI) { |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 817 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 818 | if (newStmt) |
| 819 | *CI = newStmt; |
| 820 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 821 | |
| 822 | // Handle specific things. |
| 823 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 824 | return RewriteAtEncode(AtEncode); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 825 | |
| 826 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 827 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 828 | |
| 829 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 830 | return RewriteAtSelector(AtSelector); |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 831 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 832 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 833 | return RewriteObjCStringLiteral(AtString); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 834 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 835 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 836 | // Before we rewrite it, put the original message expression in a comment. |
| 837 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 838 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 839 | |
| 840 | const char *startBuf = SM->getCharacterData(startLoc); |
| 841 | const char *endBuf = SM->getCharacterData(endLoc); |
| 842 | |
| 843 | std::string messString; |
| 844 | messString += "// "; |
| 845 | messString.append(startBuf, endBuf-startBuf+1); |
| 846 | messString += "\n"; |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 847 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 848 | // FIXME: Missing definition of |
| 849 | // InsertText(clang::SourceLocation, char const*, unsigned int). |
| 850 | // InsertText(startLoc, messString.c_str(), messString.size()); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 851 | // Tried this, but it didn't work either... |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 852 | // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 853 | return RewriteMessageExpr(MessExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 854 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 855 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 856 | if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) |
| 857 | return RewriteObjCTryStmt(StmtTry); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 858 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 859 | if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) |
| 860 | return RewriteObjCSynchronizedStmt(StmtTry); |
| 861 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 862 | if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) |
| 863 | return RewriteObjCThrowStmt(StmtThrow); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 864 | |
| 865 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 866 | return RewriteObjCProtocolExpr(ProtocolExp); |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 867 | |
| 868 | if (ObjCForCollectionStmt *StmtForCollection = |
| 869 | dyn_cast<ObjCForCollectionStmt>(S)) |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 870 | return RewriteObjCForCollectionStmt(StmtForCollection, OrigStmtEnd); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 871 | if (BreakStmt *StmtBreakStmt = |
| 872 | dyn_cast<BreakStmt>(S)) |
| 873 | return RewriteBreakStmt(StmtBreakStmt); |
| 874 | if (ContinueStmt *StmtContinueStmt = |
| 875 | dyn_cast<ContinueStmt>(S)) |
| 876 | return RewriteContinueStmt(StmtContinueStmt); |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 877 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 878 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 879 | isa<DoStmt>(S) || isa<ForStmt>(S)) { |
| 880 | assert(!Stmts.empty() && "Statement stack is empty"); |
| 881 | assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || |
| 882 | isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) |
| 883 | && "Statement stack mismatch"); |
| 884 | Stmts.pop_back(); |
| 885 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 886 | #if 0 |
| 887 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 888 | CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
| 889 | // Get the new text. |
| 890 | std::ostringstream Buf; |
| 891 | Replacement->printPretty(Buf); |
| 892 | const std::string &Str = Buf.str(); |
| 893 | |
| 894 | printf("CAST = %s\n", &Str[0]); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 895 | InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 896 | delete S; |
| 897 | return Replacement; |
| 898 | } |
| 899 | #endif |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 900 | // Return this stmt unmodified. |
| 901 | return S; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 902 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 903 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 904 | /// SynthCountByEnumWithState - To print: |
| 905 | /// ((unsigned int (*) |
| 906 | /// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 907 | /// (void *)objc_msgSend)((id)l_collection, |
| 908 | /// sel_registerName( |
| 909 | /// "countByEnumeratingWithState:objects:count:"), |
| 910 | /// &enumState, |
| 911 | /// (id *)items, (unsigned int)16) |
| 912 | /// |
| 913 | void RewriteTest::SynthCountByEnumWithState(std::string &buf) { |
| 914 | buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " |
| 915 | "id *, unsigned int))(void *)objc_msgSend)"; |
| 916 | buf += "\n\t\t"; |
| 917 | buf += "((id)l_collection,\n\t\t"; |
| 918 | buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; |
| 919 | buf += "\n\t\t"; |
| 920 | buf += "&enumState, " |
| 921 | "(id *)items, (unsigned int)16)"; |
| 922 | } |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 923 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 924 | /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach |
| 925 | /// statement to exit to its outer synthesized loop. |
| 926 | /// |
| 927 | Stmt *RewriteTest::RewriteBreakStmt(BreakStmt *S) { |
| 928 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 929 | return S; |
| 930 | // replace break with goto __break_label |
| 931 | std::string buf; |
| 932 | |
| 933 | SourceLocation startLoc = S->getLocStart(); |
| 934 | buf = "goto __break_label_"; |
| 935 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 936 | ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach |
| 942 | /// statement to continue with its inner synthesized loop. |
| 943 | /// |
| 944 | Stmt *RewriteTest::RewriteContinueStmt(ContinueStmt *S) { |
| 945 | if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) |
| 946 | return S; |
| 947 | // replace continue with goto __continue_label |
| 948 | std::string buf; |
| 949 | |
| 950 | SourceLocation startLoc = S->getLocStart(); |
| 951 | buf = "goto __continue_label_"; |
| 952 | buf += utostr(ObjCBcLabelNo.back()); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 953 | ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 958 | /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 959 | /// It rewrites: |
| 960 | /// for ( type elem in collection) { stmts; } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 961 | |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 962 | /// Into: |
| 963 | /// { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 964 | /// type elem; |
| 965 | /// struct __objcFastEnumerationState enumState = { 0 }; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 966 | /// id items[16]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 967 | /// id l_collection = (id)collection; |
| 968 | /// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 969 | /// objects:items count:16]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 970 | /// if (limit) { |
| 971 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 972 | /// do { |
| 973 | /// unsigned long counter = 0; |
| 974 | /// do { |
| 975 | /// if (startMutations != *enumState.mutationsPtr) |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 976 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 977 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 978 | /// stmts; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 979 | /// __continue_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 980 | /// } while (counter < limit); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 981 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 982 | /// objects:items count:16]); |
| 983 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 984 | /// __break_label: ; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 985 | /// } |
| 986 | /// else |
| 987 | /// elem = nil; |
| 988 | /// } |
| 989 | /// |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 990 | Stmt *RewriteTest::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, |
| 991 | SourceLocation OrigEnd) { |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 992 | assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); |
| 993 | assert(isa<ObjCForCollectionStmt>(Stmts.back()) && |
| 994 | "ObjCForCollectionStmt Statement stack mismatch"); |
| 995 | assert(!ObjCBcLabelNo.empty() && |
| 996 | "ObjCForCollectionStmt - Label No stack empty"); |
| 997 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 998 | SourceLocation startLoc = S->getLocStart(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 999 | const char *startBuf = SM->getCharacterData(startLoc); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1000 | const char *elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1001 | std::string elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1002 | std::string buf; |
| 1003 | buf = "\n{\n\t"; |
| 1004 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { |
| 1005 | // type elem; |
| 1006 | QualType ElementType = cast<ValueDecl>(DS->getDecl())->getType(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1007 | elementTypeAsString = ElementType.getAsString(); |
| 1008 | buf += elementTypeAsString; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1009 | buf += " "; |
| 1010 | elementName = DS->getDecl()->getName(); |
| 1011 | buf += elementName; |
| 1012 | buf += ";\n\t"; |
| 1013 | } |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1014 | else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S->getElement())) { |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1015 | elementName = DR->getDecl()->getName(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1016 | elementTypeAsString = DR->getDecl()->getType().getAsString(); |
| 1017 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1018 | else |
| 1019 | assert(false && "RewriteObjCForCollectionStmt - bad element kind"); |
| 1020 | |
| 1021 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1022 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1023 | // id items[16]; |
| 1024 | buf += "id items[16];\n\t"; |
| 1025 | // id l_collection = (id) |
| 1026 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1027 | // Find start location of 'collection' the hard way! |
| 1028 | const char *startCollectionBuf = startBuf; |
| 1029 | startCollectionBuf += 3; // skip 'for' |
| 1030 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1031 | startCollectionBuf++; // skip '(' |
| 1032 | // find 'in' and skip it. |
| 1033 | while (*startCollectionBuf != ' ' || |
| 1034 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1035 | (*(startCollectionBuf+3) != ' ' && |
| 1036 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1037 | startCollectionBuf++; |
| 1038 | startCollectionBuf += 3; |
| 1039 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1040 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1041 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1042 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1043 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1044 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1045 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1046 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1047 | buf = ";\n\t"; |
| 1048 | |
| 1049 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1050 | // objects:items count:16]; |
| 1051 | // which is synthesized into: |
| 1052 | // unsigned int limit = |
| 1053 | // ((unsigned int (*) |
| 1054 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1055 | // (void *)objc_msgSend)((id)l_collection, |
| 1056 | // sel_registerName( |
| 1057 | // "countByEnumeratingWithState:objects:count:"), |
| 1058 | // (struct __objcFastEnumerationState *)&state, |
| 1059 | // (id *)items, (unsigned int)16); |
| 1060 | buf += "unsigned long limit =\n\t\t"; |
| 1061 | SynthCountByEnumWithState(buf); |
| 1062 | buf += ";\n\t"; |
| 1063 | /// if (limit) { |
| 1064 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1065 | /// do { |
| 1066 | /// unsigned long counter = 0; |
| 1067 | /// do { |
| 1068 | /// if (startMutations != *enumState.mutationsPtr) |
| 1069 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1070 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1071 | buf += "if (limit) {\n\t"; |
| 1072 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1073 | buf += "do {\n\t\t"; |
| 1074 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1075 | buf += "do {\n\t\t\t"; |
| 1076 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1077 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1078 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1079 | buf += " = ("; |
| 1080 | buf += elementTypeAsString; |
| 1081 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1082 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1083 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1084 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1085 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1086 | /// } while (counter < limit); |
| 1087 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1088 | /// objects:items count:16]); |
| 1089 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1090 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1091 | /// } |
| 1092 | /// else |
| 1093 | /// elem = nil; |
| 1094 | /// } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1095 | /// |
| 1096 | buf = ";\n\t"; |
| 1097 | buf += "__continue_label_"; |
| 1098 | buf += utostr(ObjCBcLabelNo.back()); |
| 1099 | buf += ": ;"; |
| 1100 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1101 | buf += "} while (counter < limit);\n\t"; |
| 1102 | buf += "} while (limit = "; |
| 1103 | SynthCountByEnumWithState(buf); |
| 1104 | buf += ");\n\t"; |
| 1105 | buf += elementName; |
| 1106 | buf += " = nil;\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1107 | buf += "__break_label_"; |
| 1108 | buf += utostr(ObjCBcLabelNo.back()); |
| 1109 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1110 | buf += "}\n\t"; |
| 1111 | buf += "else\n\t\t"; |
| 1112 | buf += elementName; |
| 1113 | buf += " = nil;\n"; |
| 1114 | buf += "}\n"; |
| 1115 | // Insert all these *after* the statement body. |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1116 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1117 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1118 | Stmts.pop_back(); |
| 1119 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1120 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1123 | /// RewriteObjCSynchronizedStmt - |
| 1124 | /// This routine rewrites @synchronized(expr) stmt; |
| 1125 | /// into: |
| 1126 | /// objc_sync_enter(expr); |
| 1127 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1128 | /// |
| 1129 | Stmt *RewriteTest::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 1130 | // Get the start location and compute the semi location. |
| 1131 | SourceLocation startLoc = S->getLocStart(); |
| 1132 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1133 | |
| 1134 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1135 | |
| 1136 | std::string buf; |
| 1137 | buf = "objc_sync_enter"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1138 | ReplaceText(startLoc, 13, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1139 | SourceLocation endLoc = S->getSynchExpr()->getLocEnd(); |
| 1140 | const char *endBuf = SM->getCharacterData(endLoc); |
| 1141 | endBuf++; |
| 1142 | const char *rparenBuf = strchr(endBuf, ')'); |
| 1143 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
| 1144 | buf = ");\n"; |
| 1145 | // declare a new scope with two variables, _stack and _rethrow. |
| 1146 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1147 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1148 | buf += "char *pointers[4];} _stack;\n"; |
| 1149 | buf += "id volatile _rethrow = 0;\n"; |
| 1150 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1151 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1152 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1153 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1154 | startBuf = SM->getCharacterData(startLoc); |
| 1155 | |
| 1156 | assert((*startBuf == '}') && "bogus @try block"); |
| 1157 | SourceLocation lastCurlyLoc = startLoc; |
| 1158 | buf = "}\nelse {\n"; |
| 1159 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1160 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1161 | // FIXME: This must be objc_sync_exit(syncExpr); |
| 1162 | buf += " objc_sync_exit();\n"; |
| 1163 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1164 | buf += "}\n"; |
| 1165 | buf += "}"; |
| 1166 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1167 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
| 1170 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1171 | Stmt *RewriteTest::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1172 | // Get the start location and compute the semi location. |
| 1173 | SourceLocation startLoc = S->getLocStart(); |
| 1174 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1175 | |
| 1176 | assert((*startBuf == '@') && "bogus @try location"); |
| 1177 | |
| 1178 | std::string buf; |
| 1179 | // declare a new scope with two variables, _stack and _rethrow. |
| 1180 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1181 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1182 | buf += "char *pointers[4];} _stack;\n"; |
| 1183 | buf += "id volatile _rethrow = 0;\n"; |
| 1184 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1185 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1187 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1188 | |
| 1189 | startLoc = S->getTryBody()->getLocEnd(); |
| 1190 | startBuf = SM->getCharacterData(startLoc); |
| 1191 | |
| 1192 | assert((*startBuf == '}') && "bogus @try block"); |
| 1193 | |
| 1194 | SourceLocation lastCurlyLoc = startLoc; |
| 1195 | |
| 1196 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1197 | buf = " /* @catch begin */ else {\n"; |
| 1198 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1199 | buf += " objc_exception_try_enter (&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1200 | buf += " if (_setjmp(_stack.buf))\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1201 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1202 | buf += " else { /* @catch continue */"; |
| 1203 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1204 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1205 | |
| 1206 | bool sawIdTypedCatch = false; |
| 1207 | Stmt *lastCatchBody = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1208 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1209 | while (catchList) { |
| 1210 | Stmt *catchStmt = catchList->getCatchParamStmt(); |
| 1211 | |
| 1212 | if (catchList == S->getCatchStmts()) |
| 1213 | buf = "if ("; // we are generating code for the first catch clause |
| 1214 | else |
| 1215 | buf = "else if ("; |
| 1216 | startLoc = catchList->getLocStart(); |
| 1217 | startBuf = SM->getCharacterData(startLoc); |
| 1218 | |
| 1219 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1220 | |
| 1221 | const char *lParenLoc = strchr(startBuf, '('); |
| 1222 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1223 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1224 | // Now rewrite the body... |
| 1225 | lastCatchBody = catchList->getCatchBody(); |
| 1226 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1227 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1228 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1229 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1230 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1231 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1232 | |
| 1233 | buf += "1) { id _tmp = _caught;"; |
| 1234 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1235 | buf.c_str(), buf.size()); |
| 1236 | } else if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1237 | QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1238 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1239 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1240 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1241 | sawIdTypedCatch = true; |
| 1242 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1243 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1244 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1245 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1246 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1247 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1248 | buf += cls->getDecl()->getName(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1249 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1250 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | // Now rewrite the body... |
| 1254 | lastCatchBody = catchList->getCatchBody(); |
| 1255 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1256 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1257 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1258 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1259 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1260 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1261 | |
| 1262 | buf = " = _caught;"; |
| 1263 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1264 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1265 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1266 | } else if (!isa<NullStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1267 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1268 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1269 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1270 | catchList = catchList->getNextCatchStmt(); |
| 1271 | } |
| 1272 | // Complete the catch list... |
| 1273 | if (lastCatchBody) { |
| 1274 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
| 1275 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1276 | assert((*bodyBuf == '}') && "bogus @catch body location"); |
| 1277 | bodyLoc = bodyLoc.getFileLocWithOffset(1); |
| 1278 | buf = " } } /* @catch end */\n"; |
| 1279 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1280 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Set lastCurlyLoc |
| 1283 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1284 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1285 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1286 | startLoc = finalStmt->getLocStart(); |
| 1287 | startBuf = SM->getCharacterData(startLoc); |
| 1288 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1289 | |
| 1290 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1291 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1292 | |
| 1293 | Stmt *body = finalStmt->getFinallyBody(); |
| 1294 | SourceLocation startLoc = body->getLocStart(); |
| 1295 | SourceLocation endLoc = body->getLocEnd(); |
| 1296 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1297 | const char *endBuf = SM->getCharacterData(endLoc); |
| 1298 | assert((*startBuf == '{') && "bogus @finally body location"); |
| 1299 | assert((*endBuf == '}') && "bogus @finally body location"); |
| 1300 | |
| 1301 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1302 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1303 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1304 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1305 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1306 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1307 | |
| 1308 | // Set lastCurlyLoc |
| 1309 | lastCurlyLoc = body->getLocEnd(); |
| 1310 | } |
| 1311 | // Now emit the final closing curly brace... |
| 1312 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1313 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1314 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1315 | return 0; |
| 1316 | } |
| 1317 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1318 | Stmt *RewriteTest::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1319 | return 0; |
| 1320 | } |
| 1321 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1322 | Stmt *RewriteTest::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
| 1325 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1326 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1327 | // the throw expression is typically a message expression that's already |
| 1328 | // been rewritten! (which implies the SourceLocation's are invalid). |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1329 | Stmt *RewriteTest::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1330 | // Get the start location and compute the semi location. |
| 1331 | SourceLocation startLoc = S->getLocStart(); |
| 1332 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1333 | |
| 1334 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1335 | |
| 1336 | std::string buf; |
| 1337 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1338 | if (S->getThrowExpr()) |
| 1339 | buf = "objc_exception_throw("; |
| 1340 | else // add an implicit argument |
| 1341 | buf = "objc_exception_throw(_caught"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1342 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1343 | const char *semiBuf = strchr(startBuf, ';'); |
| 1344 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1345 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1346 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1347 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1348 | return 0; |
| 1349 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1351 | Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1352 | // Create a new string expression. |
| 1353 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1354 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1355 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding, |
| 1356 | EncodingRecordTypes); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1357 | Expr *Replacement = new StringLiteral(StrEncoding.c_str(), |
| 1358 | StrEncoding.length(), false, StrType, |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1359 | SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1360 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1361 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1362 | // Replace this subexpr in the parent. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1363 | delete Exp; |
| 1364 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1367 | Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 1368 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1369 | // Create a call to sel_registerName("selName"). |
| 1370 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1371 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1372 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 1373 | Exp->getSelector().getName().size(), |
| 1374 | false, argType, SourceLocation(), |
| 1375 | SourceLocation())); |
| 1376 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1377 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1378 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1379 | delete Exp; |
| 1380 | return SelExp; |
| 1381 | } |
| 1382 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1383 | CallExpr *RewriteTest::SynthesizeCallToFunctionDecl( |
| 1384 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1385 | // 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] | 1386 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Create a reference to the objc_msgSend() declaration. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1389 | DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1390 | |
| 1391 | // 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] | 1392 | QualType pToFunc = Context->getPointerType(msgSendType); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1393 | ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE); |
| 1394 | |
| 1395 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1396 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1397 | return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); |
| 1398 | } |
| 1399 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1400 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1401 | const char *&startRef, const char *&endRef) { |
| 1402 | while (startBuf < endBuf) { |
| 1403 | if (*startBuf == '<') |
| 1404 | startRef = startBuf; // mark the start. |
| 1405 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1406 | if (startRef && *startRef == '<') { |
| 1407 | endRef = startBuf; // mark the end. |
| 1408 | return true; |
| 1409 | } |
| 1410 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1411 | } |
| 1412 | startBuf++; |
| 1413 | } |
| 1414 | return false; |
| 1415 | } |
| 1416 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1417 | static void scanToNextArgument(const char *&argRef) { |
| 1418 | int angle = 0; |
| 1419 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1420 | if (*argRef == '<') |
| 1421 | angle++; |
| 1422 | else if (*argRef == '>') |
| 1423 | angle--; |
| 1424 | argRef++; |
| 1425 | } |
| 1426 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1427 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1428 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1429 | bool RewriteTest::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1430 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1431 | if (T == Context->getObjCIdType()) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1432 | return true; |
| 1433 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1434 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1435 | return true; |
| 1436 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1437 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1438 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1439 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1440 | return true; // we have "Class <Protocol> *". |
| 1441 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1442 | return false; |
| 1443 | } |
| 1444 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1445 | void RewriteTest::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1446 | SourceLocation Loc; |
| 1447 | QualType Type; |
| 1448 | const FunctionTypeProto *proto = 0; |
| 1449 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1450 | Loc = VD->getLocation(); |
| 1451 | Type = VD->getType(); |
| 1452 | } |
| 1453 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1454 | Loc = FD->getLocation(); |
| 1455 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1456 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1457 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1458 | assert(funcType && "missing function type"); |
| 1459 | proto = dyn_cast<FunctionTypeProto>(funcType); |
| 1460 | if (!proto) |
| 1461 | return; |
| 1462 | Type = proto->getResultType(); |
| 1463 | } |
| 1464 | else |
| 1465 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1466 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1467 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1468 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1469 | |
| 1470 | const char *endBuf = SM->getCharacterData(Loc); |
| 1471 | const char *startBuf = endBuf; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 1472 | while (*startBuf != ';' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1473 | startBuf--; // scan backward (from the decl location) for return type. |
| 1474 | const char *startRef = 0, *endRef = 0; |
| 1475 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1476 | // Get the locations of the startRef, endRef. |
| 1477 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1478 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1479 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1480 | InsertText(LessLoc, "/*", 2); |
| 1481 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1484 | if (!proto) |
| 1485 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1486 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1487 | const char *startBuf = SM->getCharacterData(Loc); |
| 1488 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1489 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1490 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1491 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1492 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1493 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1494 | // scan forward (from the decl location) for argument types. |
| 1495 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1496 | const char *startRef = 0, *endRef = 0; |
| 1497 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1498 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1499 | SourceLocation LessLoc = |
| 1500 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1501 | SourceLocation GreaterLoc = |
| 1502 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1503 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1504 | InsertText(LessLoc, "/*", 2); |
| 1505 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1506 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1507 | startBuf = ++endBuf; |
| 1508 | } |
| 1509 | else { |
| 1510 | while (*startBuf != ')' && *startBuf != ',') |
| 1511 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1512 | startBuf++; |
| 1513 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1514 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1517 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 1518 | void RewriteTest::SynthSelGetUidFunctionDecl() { |
| 1519 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1520 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1521 | ArgTys.push_back(Context->getPointerType( |
| 1522 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1523 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1524 | &ArgTys[0], ArgTys.size(), |
| 1525 | false /*isVariadic*/); |
| 1526 | SelGetUidFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1527 | SelGetUidIdent, getFuncType, |
| 1528 | FunctionDecl::Extern, false, 0); |
| 1529 | } |
| 1530 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1531 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
| 1532 | void RewriteTest::SynthGetProtocolFunctionDecl() { |
| 1533 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1534 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1535 | ArgTys.push_back(Context->getPointerType( |
| 1536 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1537 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1538 | &ArgTys[0], ArgTys.size(), |
| 1539 | false /*isVariadic*/); |
| 1540 | GetProtocolFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1541 | SelGetProtoIdent, getFuncType, |
| 1542 | FunctionDecl::Extern, false, 0); |
| 1543 | } |
| 1544 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1545 | void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) { |
| 1546 | // declared in <objc/objc.h> |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1547 | if (strcmp(FD->getName(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1548 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1549 | return; |
| 1550 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1551 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1554 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
| 1555 | void RewriteTest::SynthSuperContructorFunctionDecl() { |
| 1556 | if (SuperContructorFunctionDecl) |
| 1557 | return; |
| 1558 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_super"); |
| 1559 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1560 | QualType argT = Context->getObjCIdType(); |
| 1561 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1562 | ArgTys.push_back(argT); |
| 1563 | ArgTys.push_back(argT); |
| 1564 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1565 | &ArgTys[0], ArgTys.size(), |
| 1566 | false); |
| 1567 | SuperContructorFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1568 | msgSendIdent, msgSendType, |
| 1569 | FunctionDecl::Extern, false, 0); |
| 1570 | } |
| 1571 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1572 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 1573 | void RewriteTest::SynthMsgSendFunctionDecl() { |
| 1574 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1575 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1576 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1577 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1578 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1579 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1580 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1581 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1582 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1583 | &ArgTys[0], ArgTys.size(), |
| 1584 | true /*isVariadic*/); |
| 1585 | MsgSendFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1586 | msgSendIdent, msgSendType, |
| 1587 | FunctionDecl::Extern, false, 0); |
| 1588 | } |
| 1589 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1590 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
| 1591 | void RewriteTest::SynthMsgSendSuperFunctionDecl() { |
| 1592 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 1593 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1594 | RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), |
| 1595 | &Context->Idents.get("objc_super"), 0); |
| 1596 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1597 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1598 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1599 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1600 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1601 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1602 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1603 | &ArgTys[0], ArgTys.size(), |
| 1604 | true /*isVariadic*/); |
| 1605 | MsgSendSuperFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1606 | msgSendIdent, msgSendType, |
| 1607 | FunctionDecl::Extern, false, 0); |
| 1608 | } |
| 1609 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1610 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 1611 | void RewriteTest::SynthMsgSendStretFunctionDecl() { |
| 1612 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 1613 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1614 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1615 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1616 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1617 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1618 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1619 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1620 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1621 | &ArgTys[0], ArgTys.size(), |
| 1622 | true /*isVariadic*/); |
| 1623 | MsgSendStretFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1624 | msgSendIdent, msgSendType, |
| 1625 | FunctionDecl::Extern, false, 0); |
| 1626 | } |
| 1627 | |
| 1628 | // SynthMsgSendSuperStretFunctionDecl - |
| 1629 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
| 1630 | void RewriteTest::SynthMsgSendSuperStretFunctionDecl() { |
| 1631 | IdentifierInfo *msgSendIdent = |
| 1632 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 1633 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1634 | RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), |
| 1635 | &Context->Idents.get("objc_super"), 0); |
| 1636 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1637 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1638 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1639 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1640 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1641 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1642 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1643 | &ArgTys[0], ArgTys.size(), |
| 1644 | true /*isVariadic*/); |
| 1645 | MsgSendSuperStretFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1646 | msgSendIdent, msgSendType, |
| 1647 | FunctionDecl::Extern, false, 0); |
| 1648 | } |
| 1649 | |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1650 | // SynthMsgSendFpretFunctionDecl - id objc_msgSend_fpret(id self, SEL op, ...); |
| 1651 | void RewriteTest::SynthMsgSendFpretFunctionDecl() { |
| 1652 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 1653 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1654 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1655 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1656 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1657 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1658 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1659 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1660 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1661 | &ArgTys[0], ArgTys.size(), |
| 1662 | true /*isVariadic*/); |
| 1663 | MsgSendFpretFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1664 | msgSendIdent, msgSendType, |
| 1665 | FunctionDecl::Extern, false, 0); |
| 1666 | } |
| 1667 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1668 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
| 1669 | void RewriteTest::SynthGetClassFunctionDecl() { |
| 1670 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 1671 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1672 | ArgTys.push_back(Context->getPointerType( |
| 1673 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1674 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1675 | &ArgTys[0], ArgTys.size(), |
| 1676 | false /*isVariadic*/); |
| 1677 | GetClassFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1678 | getClassIdent, getClassType, |
| 1679 | FunctionDecl::Extern, false, 0); |
| 1680 | } |
| 1681 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1682 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
| 1683 | void RewriteTest::SynthGetMetaClassFunctionDecl() { |
| 1684 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 1685 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1686 | ArgTys.push_back(Context->getPointerType( |
| 1687 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1688 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1689 | &ArgTys[0], ArgTys.size(), |
| 1690 | false /*isVariadic*/); |
| 1691 | GetMetaClassFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1692 | getClassIdent, getClassType, |
| 1693 | FunctionDecl::Extern, false, 0); |
| 1694 | } |
| 1695 | |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1696 | // SynthCFStringFunctionDecl - id __builtin___CFStringMakeConstantString(const char *name); |
| 1697 | void RewriteTest::SynthCFStringFunctionDecl() { |
| 1698 | IdentifierInfo *getClassIdent = &Context->Idents.get("__builtin___CFStringMakeConstantString"); |
| 1699 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1700 | ArgTys.push_back(Context->getPointerType( |
| 1701 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1702 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1703 | &ArgTys[0], ArgTys.size(), |
| 1704 | false /*isVariadic*/); |
| 1705 | CFStringFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1706 | getClassIdent, getClassType, |
| 1707 | FunctionDecl::Extern, false, 0); |
| 1708 | } |
| 1709 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1710 | Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1711 | #if 1 |
| 1712 | // This rewrite is specific to GCC, which has builtin support for CFString. |
| 1713 | if (!CFStringFunctionDecl) |
| 1714 | SynthCFStringFunctionDecl(); |
| 1715 | // Create a call to __builtin___CFStringMakeConstantString("cstr"). |
| 1716 | llvm::SmallVector<Expr*, 8> StrExpr; |
| 1717 | StrExpr.push_back(Exp->getString()); |
| 1718 | CallExpr *call = SynthesizeCallToFunctionDecl(CFStringFunctionDecl, |
| 1719 | &StrExpr[0], StrExpr.size()); |
| 1720 | // cast to NSConstantString * |
| 1721 | CastExpr *cast = new CastExpr(Exp->getType(), call, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1722 | ReplaceStmt(Exp, cast); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1723 | delete Exp; |
| 1724 | return cast; |
| 1725 | #else |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1726 | assert(ConstantStringClassReference && "Can't find constant string reference"); |
| 1727 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1728 | |
| 1729 | // Synthesize "(Class)&_NSConstantStringClassReference" |
| 1730 | DeclRefExpr *ClsRef = new DeclRefExpr(ConstantStringClassReference, |
| 1731 | ConstantStringClassReference->getType(), |
| 1732 | SourceLocation()); |
| 1733 | QualType expType = Context->getPointerType(ClsRef->getType()); |
| 1734 | UnaryOperator *Unop = new UnaryOperator(ClsRef, UnaryOperator::AddrOf, |
| 1735 | expType, SourceLocation()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1736 | CastExpr *cast = new CastExpr(Context->getObjCClassType(), Unop, |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1737 | SourceLocation()); |
| 1738 | InitExprs.push_back(cast); // set the 'isa'. |
| 1739 | InitExprs.push_back(Exp->getString()); // set "char *bytes". |
| 1740 | unsigned IntSize = static_cast<unsigned>( |
| 1741 | Context->getTypeSize(Context->IntTy, Exp->getLocStart())); |
| 1742 | llvm::APInt IntVal(IntSize, Exp->getString()->getByteLength()); |
| 1743 | IntegerLiteral *len = new IntegerLiteral(IntVal, Context->IntTy, |
| 1744 | Exp->getLocStart()); |
| 1745 | InitExprs.push_back(len); // set "int numBytes". |
| 1746 | |
| 1747 | // struct NSConstantString |
| 1748 | QualType CFConstantStrType = Context->getCFConstantStringType(); |
| 1749 | // (struct NSConstantString) { <exprs from above> } |
| 1750 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 1751 | &InitExprs[0], InitExprs.size(), |
| 1752 | SourceLocation()); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1753 | CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE, false); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1754 | // struct NSConstantString * |
| 1755 | expType = Context->getPointerType(StrRep->getType()); |
| 1756 | Unop = new UnaryOperator(StrRep, UnaryOperator::AddrOf, expType, |
| 1757 | SourceLocation()); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 1758 | // cast to NSConstantString * |
| 1759 | cast = new CastExpr(Exp->getType(), Unop, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1760 | ReplaceStmt(Exp, cast); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1761 | delete Exp; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 1762 | return cast; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1763 | #endif |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1766 | ObjCInterfaceDecl *RewriteTest::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1767 | // check if we are sending a message to 'super' |
| 1768 | if (CurMethodDecl && CurMethodDecl->isInstance()) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1769 | if (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) { |
| 1770 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) { |
| 1771 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 1772 | if (!strcmp(PVD->getName(), "self")) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1773 | // is this id<P1..> type? |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1774 | if (CE->getType()->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1775 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1776 | if (const PointerType *PT = CE->getType()->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1777 | if (ObjCInterfaceType *IT = |
| 1778 | dyn_cast<ObjCInterfaceType>(PT->getPointeeType())) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1779 | if (IT->getDecl() == |
| 1780 | CurMethodDecl->getClassInterface()->getSuperClass()) |
| 1781 | return IT->getDecl(); |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1787 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1788 | } |
| 1789 | return 0; |
| 1790 | } |
| 1791 | |
| 1792 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
| 1793 | QualType RewriteTest::getSuperStructType() { |
| 1794 | if (!SuperStructDecl) { |
| 1795 | SuperStructDecl = new RecordDecl(Decl::Struct, SourceLocation(), |
| 1796 | &Context->Idents.get("objc_super"), 0); |
| 1797 | QualType FieldTypes[2]; |
| 1798 | |
| 1799 | // struct objc_object *receiver; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1800 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1801 | // struct objc_class *super; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1802 | FieldTypes[1] = Context->getObjCClassType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1803 | // Create fields |
| 1804 | FieldDecl *FieldDecls[2]; |
| 1805 | |
| 1806 | for (unsigned i = 0; i < 2; ++i) |
| 1807 | FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]); |
| 1808 | |
| 1809 | SuperStructDecl->defineBody(FieldDecls, 4); |
| 1810 | } |
| 1811 | return Context->getTagDeclType(SuperStructDecl); |
| 1812 | } |
| 1813 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1814 | Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1815 | if (!SelGetUidFunctionDecl) |
| 1816 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1817 | if (!MsgSendFunctionDecl) |
| 1818 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1819 | if (!MsgSendSuperFunctionDecl) |
| 1820 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1821 | if (!MsgSendStretFunctionDecl) |
| 1822 | SynthMsgSendStretFunctionDecl(); |
| 1823 | if (!MsgSendSuperStretFunctionDecl) |
| 1824 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1825 | if (!MsgSendFpretFunctionDecl) |
| 1826 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1827 | if (!GetClassFunctionDecl) |
| 1828 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1829 | if (!GetMetaClassFunctionDecl) |
| 1830 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1831 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1832 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1833 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 1834 | // May need to use objc_msgSend_stret() as well. |
| 1835 | FunctionDecl *MsgSendStretFlavor = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1836 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1837 | QualType resultType = mDecl->getResultType(); |
| 1838 | if (resultType.getCanonicalType()->isStructureType() |
| 1839 | || resultType.getCanonicalType()->isUnionType()) |
| 1840 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1841 | else if (resultType.getCanonicalType()->isRealFloatingType()) |
| 1842 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1843 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1844 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1845 | // Synthesize a call to objc_msgSend(). |
| 1846 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 1847 | IdentifierInfo *clsName = Exp->getClassName(); |
| 1848 | |
| 1849 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 1850 | if (clsName) { // class message. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1851 | if (!strcmp(clsName->getName(), "super")) { |
| 1852 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 1853 | if (MsgSendStretFlavor) |
| 1854 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 1855 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 1856 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1857 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1858 | CurMethodDecl->getClassInterface()->getSuperClass(); |
| 1859 | |
| 1860 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1861 | |
| 1862 | // set the receiver to self, the first argument to all methods. |
| 1863 | InitExprs.push_back(new DeclRefExpr(CurMethodDecl->getSelfDecl(), |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1864 | Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1865 | SourceLocation())); |
| 1866 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1867 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1868 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 1869 | SuperDecl->getIdentifier()->getLength(), |
| 1870 | false, argType, SourceLocation(), |
| 1871 | SourceLocation())); |
| 1872 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 1873 | &ClsExprs[0], |
| 1874 | ClsExprs.size()); |
| 1875 | // To turn off a warning, type-cast to 'id' |
| 1876 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1877 | new CastExpr(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1878 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
| 1879 | // struct objc_super |
| 1880 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame^] | 1881 | Expr *SuperRep; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1882 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame^] | 1883 | if (LangOpts.Microsoft) { |
| 1884 | SynthSuperContructorFunctionDecl(); |
| 1885 | // Simulate a contructor call... |
| 1886 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 1887 | superType, SourceLocation()); |
| 1888 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 1889 | superType, SourceLocation()); |
| 1890 | } else { |
| 1891 | // (struct objc_super) { <exprs from above> } |
| 1892 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 1893 | &InitExprs[0], InitExprs.size(), |
| 1894 | SourceLocation()); |
| 1895 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 1896 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1897 | // struct objc_super * |
| 1898 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 1899 | Context->getPointerType(SuperRep->getType()), |
| 1900 | SourceLocation()); |
| 1901 | MsgExprs.push_back(Unop); |
| 1902 | } else { |
| 1903 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1904 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1905 | ClsExprs.push_back(new StringLiteral(clsName->getName(), |
| 1906 | clsName->getLength(), |
| 1907 | false, argType, SourceLocation(), |
| 1908 | SourceLocation())); |
| 1909 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 1910 | &ClsExprs[0], |
| 1911 | ClsExprs.size()); |
| 1912 | MsgExprs.push_back(Cls); |
| 1913 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1914 | } else { // instance message. |
| 1915 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1916 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1917 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1918 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1919 | if (MsgSendStretFlavor) |
| 1920 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1921 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 1922 | |
| 1923 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1924 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1925 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1926 | new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1927 | recExpr, SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1928 | |
| 1929 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1930 | QualType argType = Context->getPointerType(Context->CharTy); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1931 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 1932 | SuperDecl->getIdentifier()->getLength(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1933 | false, argType, SourceLocation(), |
| 1934 | SourceLocation())); |
| 1935 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1936 | &ClsExprs[0], |
| 1937 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 1938 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1939 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1940 | new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1941 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1942 | // struct objc_super |
| 1943 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1944 | Expr *SuperRep; |
| 1945 | |
| 1946 | if (LangOpts.Microsoft) { |
| 1947 | SynthSuperContructorFunctionDecl(); |
| 1948 | // Simulate a contructor call... |
| 1949 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 1950 | superType, SourceLocation()); |
| 1951 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 1952 | superType, SourceLocation()); |
| 1953 | } else { |
| 1954 | // (struct objc_super) { <exprs from above> } |
| 1955 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 1956 | &InitExprs[0], InitExprs.size(), |
| 1957 | SourceLocation()); |
| 1958 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 1959 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1960 | // struct objc_super * |
| 1961 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 1962 | Context->getPointerType(SuperRep->getType()), |
| 1963 | SourceLocation()); |
| 1964 | MsgExprs.push_back(Unop); |
| 1965 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 1966 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 1967 | // Foo<Proto> *. |
| 1968 | while (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) |
| 1969 | recExpr = CE->getSubExpr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1970 | recExpr = new CastExpr(Context->getObjCIdType(), recExpr, SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1971 | MsgExprs.push_back(recExpr); |
| 1972 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1973 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1974 | // 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] | 1975 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1976 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1977 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 1978 | Exp->getSelector().getName().size(), |
| 1979 | false, argType, SourceLocation(), |
| 1980 | SourceLocation())); |
| 1981 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1982 | &SelExprs[0], SelExprs.size()); |
| 1983 | MsgExprs.push_back(SelExp); |
| 1984 | |
| 1985 | // Now push any user supplied arguments. |
| 1986 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1987 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1988 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 1989 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 1990 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1991 | userExpr = new CastExpr(ICE->getType()->isObjCQualifiedIdType() |
| 1992 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1993 | : ICE->getType(), userExpr, SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 1994 | } |
| 1995 | // Make id<P...> cast into an 'id' cast. |
| 1996 | else if (CastExpr *CE = dyn_cast<CastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1997 | if (CE->getType()->isObjCQualifiedIdType()) { |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 1998 | while ((CE = dyn_cast<CastExpr>(userExpr))) |
| 1999 | userExpr = CE->getSubExpr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2000 | userExpr = new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2001 | userExpr, SourceLocation()); |
| 2002 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2003 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2004 | MsgExprs.push_back(userExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2005 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2006 | // the original expression, since we will delete it below. |
| 2007 | Exp->setArg(i, 0); |
| 2008 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2009 | // Generate the funky cast. |
| 2010 | CastExpr *cast; |
| 2011 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2012 | QualType returnType; |
| 2013 | |
| 2014 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2015 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2016 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2017 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2018 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2019 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2020 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2021 | // Push any user argument types. |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2022 | for (int i = 0; i < mDecl->getNumParams(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2023 | QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType() |
| 2024 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2025 | : mDecl->getParamDecl(i)->getType(); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2026 | ArgTypes.push_back(t); |
| 2027 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2028 | returnType = mDecl->getResultType()->isObjCQualifiedIdType() |
| 2029 | ? Context->getObjCIdType() : mDecl->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2030 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2031 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2032 | } |
| 2033 | // 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] | 2034 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2035 | |
| 2036 | // Create a reference to the objc_msgSend() declaration. |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2037 | DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType, |
| 2038 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2041 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2042 | // xx.m:13: warning: function called through a non-compatible type |
| 2043 | // xx.m:13: note: if this code is reached, the program will abort |
| 2044 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE, |
| 2045 | SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2046 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2047 | // Now do the "normal" pointer to function cast. |
| 2048 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2049 | &ArgTypes[0], ArgTypes.size(), |
| 2050 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2051 | castType = Context->getPointerType(castType); |
| 2052 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 2053 | |
| 2054 | // Don't forget the parens to enforce the proper binding. |
| 2055 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2056 | |
| 2057 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
| 2058 | CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2059 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2060 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2061 | if (MsgSendStretFlavor) { |
| 2062 | // We have the method which returns a struct/union. Must also generate |
| 2063 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2064 | // expression which dictate which one to envoke depending on size of |
| 2065 | // method's return type. |
| 2066 | |
| 2067 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2068 | DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType, |
| 2069 | SourceLocation()); |
| 2070 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2071 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
| 2072 | SourceLocation()); |
| 2073 | // Now do the "normal" pointer to function cast. |
| 2074 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2075 | &ArgTypes[0], ArgTypes.size(), |
| 2076 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2077 | castType = Context->getPointerType(castType); |
| 2078 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 2079 | |
| 2080 | // Don't forget the parens to enforce the proper binding. |
| 2081 | PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2082 | |
| 2083 | FT = msgSendType->getAsFunctionType(); |
| 2084 | CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2085 | FT->getResultType(), SourceLocation()); |
| 2086 | |
| 2087 | // Build sizeof(returnType) |
| 2088 | SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true, |
| 2089 | returnType, Context->getSizeType(), |
| 2090 | SourceLocation(), SourceLocation()); |
| 2091 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2092 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2093 | // For X86 it is more complicated and some kind of target specific routine |
| 2094 | // is needed to decide what to do. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2095 | unsigned IntSize = |
| 2096 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2097 | IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8), |
| 2098 | Context->IntTy, |
| 2099 | SourceLocation()); |
| 2100 | BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit, |
| 2101 | BinaryOperator::LE, |
| 2102 | Context->IntTy, |
| 2103 | SourceLocation()); |
| 2104 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2105 | ConditionalOperator *CondExpr = |
| 2106 | new ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2107 | ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2108 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2109 | return ReplacingStmt; |
| 2110 | } |
| 2111 | |
| 2112 | Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
| 2113 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2114 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2115 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2116 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2117 | delete Exp; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2118 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2121 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2122 | /// call to objc_getProtocol("proto-name"). |
| 2123 | Stmt *RewriteTest::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
| 2124 | if (!GetProtocolFunctionDecl) |
| 2125 | SynthGetProtocolFunctionDecl(); |
| 2126 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2127 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2128 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2129 | ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(), |
| 2130 | strlen(Exp->getProtocol()->getName()), |
| 2131 | false, argType, SourceLocation(), |
| 2132 | SourceLocation())); |
| 2133 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2134 | &ProtoExprs[0], |
| 2135 | ProtoExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2136 | ReplaceStmt(Exp, ProtoExp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2137 | delete Exp; |
| 2138 | return ProtoExp; |
| 2139 | |
| 2140 | } |
| 2141 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2142 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2143 | /// an objective-c class with ivars. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2144 | void RewriteTest::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2145 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2146 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 2147 | assert(CDecl->getName() && "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2148 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2149 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2150 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2151 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 2152 | int NumIvars = CDecl->getNumInstanceVariables(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2153 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2154 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2155 | |
| 2156 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2157 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2158 | // If no ivars and no root or if its root, directly or indirectly, |
| 2159 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2160 | if (NumIvars <= 0 && (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2161 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2162 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2163 | return; |
| 2164 | } |
| 2165 | |
| 2166 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2167 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2168 | Result += "\nstruct "; |
| 2169 | Result += CDecl->getName(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2170 | if (LangOpts.Microsoft) |
| 2171 | Result += "_IMPL"; |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2172 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2173 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2174 | const char *cursor = strchr(startBuf, '{'); |
| 2175 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2176 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2177 | |
| 2178 | // rewrite the original header *without* disturbing the '{' |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2179 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2180 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2181 | Result = "\n struct "; |
| 2182 | Result += RCDecl->getName(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2183 | if (LangOpts.Microsoft) |
| 2184 | Result += "_IMPL"; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 2185 | // Note: We don't name the field decl. This simplifies the "codegen" for |
| 2186 | // accessing a superclasses instance variables (and is similar to what gcc |
| 2187 | // does internally). The unnamed struct field feature is enabled with |
| 2188 | // -fms-extensions. If the struct definition were "inlined", we wouldn't |
| 2189 | // need to use this switch. That said, I don't want to inline the def. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2190 | Result += ";\n"; |
| 2191 | |
| 2192 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2193 | SourceLocation OnePastCurly = |
| 2194 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2195 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2196 | } |
| 2197 | cursor++; // past '{' |
| 2198 | |
| 2199 | // Now comment out any visibility specifiers. |
| 2200 | while (cursor < endBuf) { |
| 2201 | if (*cursor == '@') { |
| 2202 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2203 | // Skip whitespace. |
| 2204 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2205 | /*scan*/; |
| 2206 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2207 | // FIXME: presence of @public, etc. inside comment results in |
| 2208 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2209 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2210 | !strncmp(cursor, "private", strlen("private")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2211 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2212 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2213 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2214 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2215 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2216 | // for type checking. |
| 2217 | else if (*cursor == '<') { |
| 2218 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2219 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2220 | cursor = strchr(cursor, '>'); |
| 2221 | cursor++; |
| 2222 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2223 | InsertText(atLoc, " */", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2224 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2225 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2226 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2227 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2228 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2229 | } else { // we don't have any instance variables - insert super struct. |
| 2230 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 2231 | Result += " {\n struct "; |
| 2232 | Result += RCDecl->getName(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 2233 | if (LangOpts.Microsoft) |
| 2234 | Result += "_IMPL"; |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 2235 | // Note: We don't name the field decl. This simplifies the "codegen" for |
| 2236 | // accessing a superclasses instance variables (and is similar to what gcc |
| 2237 | // does internally). The unnamed struct field feature is enabled with |
| 2238 | // -fms-extensions. If the struct definition were "inlined", we wouldn't |
| 2239 | // need to use this switch. That said, I don't want to inline the def. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2240 | Result += ";\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2241 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2242 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2243 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2244 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
| 2245 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2248 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2249 | /// class methods. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2250 | void RewriteTest::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2251 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2252 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2253 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2254 | const char *ClassName, |
| 2255 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2256 | if (MethodBegin == MethodEnd) return; |
| 2257 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2258 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2259 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2260 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2261 | SEL _cmd; |
| 2262 | char *method_types; |
| 2263 | void *_imp; |
| 2264 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2265 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2266 | Result += "\nstruct _objc_method {\n"; |
| 2267 | Result += "\tSEL _cmd;\n"; |
| 2268 | Result += "\tchar *method_types;\n"; |
| 2269 | Result += "\tvoid *_imp;\n"; |
| 2270 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2271 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2272 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2273 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2274 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2275 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2276 | |
| 2277 | /* struct { |
| 2278 | struct _objc_method_list *next_method; |
| 2279 | int method_count; |
| 2280 | struct _objc_method method_list[]; |
| 2281 | } |
| 2282 | */ |
| 2283 | Result += "\nstatic struct {\n"; |
| 2284 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2285 | Result += "\tint method_count;\n"; |
| 2286 | Result += "\tstruct _objc_method method_list["; |
| 2287 | Result += utostr(MethodEnd-MethodBegin); |
| 2288 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2289 | Result += prefix; |
| 2290 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2291 | Result += "_METHODS_"; |
| 2292 | Result += ClassName; |
| 2293 | Result += " __attribute__ ((section (\"__OBJC, __"; |
| 2294 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2295 | Result += "_meth\")))= "; |
| 2296 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2298 | Result += "\t,{{(SEL)\""; |
| 2299 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
| 2300 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2301 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2302 | Result += "\", \""; |
| 2303 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2304 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2305 | Result += MethodInternalNames[*MethodBegin]; |
| 2306 | Result += "}\n"; |
| 2307 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2308 | Result += "\t ,{(SEL)\""; |
| 2309 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2310 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2311 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2312 | Result += "\", \""; |
| 2313 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2314 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2315 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2316 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2317 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2318 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2321 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
| 2322 | void RewriteTest::RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2323 | int NumProtocols, |
| 2324 | const char *prefix, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2325 | const char *ClassName, |
| 2326 | std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2327 | static bool objc_protocol_methods = false; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2328 | if (NumProtocols > 0) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2329 | for (int i = 0; i < NumProtocols; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2330 | ObjCProtocolDecl *PDecl = Protocols[i]; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2331 | // Output struct protocol_methods holder of method selector and type. |
| 2332 | if (!objc_protocol_methods && |
| 2333 | (PDecl->getNumInstanceMethods() > 0 |
| 2334 | || PDecl->getNumClassMethods() > 0)) { |
| 2335 | /* struct protocol_methods { |
| 2336 | SEL _cmd; |
| 2337 | char *method_types; |
| 2338 | } |
| 2339 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2340 | Result += "\nstruct protocol_methods {\n"; |
| 2341 | Result += "\tSEL _cmd;\n"; |
| 2342 | Result += "\tchar *method_types;\n"; |
| 2343 | Result += "};\n"; |
| 2344 | |
| 2345 | /* struct _objc_protocol_method_list { |
| 2346 | int protocol_method_count; |
| 2347 | struct protocol_methods protocols[]; |
| 2348 | } |
| 2349 | */ |
| 2350 | Result += "\nstruct _objc_protocol_method_list {\n"; |
| 2351 | Result += "\tint protocol_method_count;\n"; |
| 2352 | Result += "\tstruct protocol_methods protocols[];\n};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2353 | objc_protocol_methods = true; |
| 2354 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2355 | |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2356 | int NumMethods = PDecl->getNumInstanceMethods(); |
| 2357 | if(NumMethods > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2358 | Result += "\nstatic struct _objc_protocol_method_list " |
| 2359 | "_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 2360 | Result += PDecl->getName(); |
| 2361 | Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= " |
| 2362 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2363 | |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2364 | // Output instance methods declared in this protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2365 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2366 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 2367 | if (I == PDecl->instmeth_begin()) |
| 2368 | Result += "\t ,{{(SEL)\""; |
| 2369 | else |
| 2370 | Result += "\t ,{(SEL)\""; |
| 2371 | Result += (*I)->getSelector().getName().c_str(); |
| 2372 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2373 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2374 | Result += "\", \""; |
| 2375 | Result += MethodTypeString; |
| 2376 | Result += "\"}\n"; |
| 2377 | } |
| 2378 | Result += "\t }\n};\n"; |
| 2379 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2380 | |
| 2381 | // Output class methods declared in this protocol. |
| 2382 | NumMethods = PDecl->getNumClassMethods(); |
| 2383 | if (NumMethods > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2384 | Result += "\nstatic struct _objc_protocol_method_list " |
| 2385 | "_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 2386 | Result += PDecl->getName(); |
| 2387 | Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= " |
| 2388 | "{\n\t"; |
| 2389 | Result += utostr(NumMethods); |
| 2390 | Result += "\n"; |
| 2391 | |
Fariborz Jahanian | c6e2c2a | 2007-12-17 18:07:01 +0000 | [diff] [blame] | 2392 | // Output instance methods declared in this protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2393 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Fariborz Jahanian | c6e2c2a | 2007-12-17 18:07:01 +0000 | [diff] [blame] | 2394 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 2395 | if (I == PDecl->classmeth_begin()) |
| 2396 | Result += "\t ,{{(SEL)\""; |
| 2397 | else |
| 2398 | Result += "\t ,{(SEL)\""; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2399 | Result += (*I)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2400 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2401 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2402 | Result += "\", \""; |
| 2403 | Result += MethodTypeString; |
| 2404 | Result += "\"}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2405 | } |
| 2406 | Result += "\t }\n};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2407 | } |
| 2408 | // Output: |
| 2409 | /* struct _objc_protocol { |
| 2410 | // Objective-C 1.0 extensions |
| 2411 | struct _objc_protocol_extension *isa; |
| 2412 | char *protocol_name; |
| 2413 | struct _objc_protocol **protocol_list; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2414 | struct _objc_protocol_method_list *instance_methods; |
| 2415 | struct _objc_protocol_method_list *class_methods; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2416 | }; |
| 2417 | */ |
| 2418 | static bool objc_protocol = false; |
| 2419 | if (!objc_protocol) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2420 | Result += "\nstruct _objc_protocol {\n"; |
| 2421 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2422 | Result += "\tchar *protocol_name;\n"; |
| 2423 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2424 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2425 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2426 | Result += "};\n"; |
| 2427 | |
| 2428 | /* struct _objc_protocol_list { |
| 2429 | struct _objc_protocol_list *next; |
| 2430 | int protocol_count; |
| 2431 | struct _objc_protocol *class_protocols[]; |
| 2432 | } |
| 2433 | */ |
| 2434 | Result += "\nstruct _objc_protocol_list {\n"; |
| 2435 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 2436 | Result += "\tint protocol_count;\n"; |
| 2437 | Result += "\tstruct _objc_protocol *class_protocols[];\n"; |
| 2438 | Result += "};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2439 | objc_protocol = true; |
| 2440 | } |
| 2441 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2442 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 2443 | Result += PDecl->getName(); |
| 2444 | Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= " |
| 2445 | "{\n\t0, \""; |
| 2446 | Result += PDecl->getName(); |
| 2447 | Result += "\", 0, "; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2448 | if (PDecl->getNumInstanceMethods() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2449 | Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 2450 | Result += PDecl->getName(); |
| 2451 | Result += ", "; |
| 2452 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2453 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2454 | Result += "0, "; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2455 | if (PDecl->getNumClassMethods() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2456 | Result += "&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 2457 | Result += PDecl->getName(); |
| 2458 | Result += "\n"; |
| 2459 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2460 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2461 | Result += "0\n"; |
| 2462 | Result += "};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2463 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2464 | // Output the top lovel protocol meta-data for the class. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2465 | Result += "\nstatic struct _objc_protocol_list _OBJC_"; |
| 2466 | Result += prefix; |
| 2467 | Result += "_PROTOCOLS_"; |
| 2468 | Result += ClassName; |
| 2469 | Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= " |
| 2470 | "{\n\t0, "; |
| 2471 | Result += utostr(NumProtocols); |
| 2472 | Result += "\n"; |
| 2473 | |
| 2474 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 2475 | Result += Protocols[0]->getName(); |
| 2476 | Result += " \n"; |
| 2477 | |
| 2478 | for (int i = 1; i < NumProtocols; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2479 | ObjCProtocolDecl *PDecl = Protocols[i]; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2480 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 2481 | Result += PDecl->getName(); |
| 2482 | Result += "\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2483 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2484 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2485 | } |
| 2486 | } |
| 2487 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2488 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2489 | /// implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2490 | void RewriteTest::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2491 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2492 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2493 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2494 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2495 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 2496 | CDecl = CDecl->getNextClassCategory()) |
| 2497 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 2498 | break; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2499 | |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2500 | std::string FullCategoryName = ClassDecl->getName(); |
| 2501 | FullCategoryName += '_'; |
| 2502 | FullCategoryName += IDecl->getName(); |
| 2503 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2504 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2505 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2506 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 2507 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2508 | |
| 2509 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2510 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2511 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 2512 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2515 | // Null CDecl is case of a category implementation with no category interface |
| 2516 | if (CDecl) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2517 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2518 | CDecl->getNumReferencedProtocols(), |
| 2519 | "CATEGORY", |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2520 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2521 | |
| 2522 | /* struct _objc_category { |
| 2523 | char *category_name; |
| 2524 | char *class_name; |
| 2525 | struct _objc_method_list *instance_methods; |
| 2526 | struct _objc_method_list *class_methods; |
| 2527 | struct _objc_protocol_list *protocols; |
| 2528 | // Objective-C 1.0 extensions |
| 2529 | uint32_t size; // sizeof (struct _objc_category) |
| 2530 | struct _objc_property_list *instance_properties; // category's own |
| 2531 | // @property decl. |
| 2532 | }; |
| 2533 | */ |
| 2534 | |
| 2535 | static bool objc_category = false; |
| 2536 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2537 | Result += "\nstruct _objc_category {\n"; |
| 2538 | Result += "\tchar *category_name;\n"; |
| 2539 | Result += "\tchar *class_name;\n"; |
| 2540 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 2541 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 2542 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2543 | Result += "\tunsigned int size;\n"; |
| 2544 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 2545 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2546 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2547 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2548 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 2549 | Result += FullCategoryName; |
| 2550 | Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\""; |
| 2551 | Result += IDecl->getName(); |
| 2552 | Result += "\"\n\t, \""; |
| 2553 | Result += ClassDecl->getName(); |
| 2554 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2555 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2556 | if (IDecl->getNumInstanceMethods() > 0) { |
| 2557 | Result += "\t, (struct _objc_method_list *)" |
| 2558 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 2559 | Result += FullCategoryName; |
| 2560 | Result += "\n"; |
| 2561 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2562 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2563 | Result += "\t, 0\n"; |
| 2564 | if (IDecl->getNumClassMethods() > 0) { |
| 2565 | Result += "\t, (struct _objc_method_list *)" |
| 2566 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 2567 | Result += FullCategoryName; |
| 2568 | Result += "\n"; |
| 2569 | } |
| 2570 | else |
| 2571 | Result += "\t, 0\n"; |
| 2572 | |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2573 | if (CDecl && CDecl->getNumReferencedProtocols() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2574 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 2575 | Result += FullCategoryName; |
| 2576 | Result += "\n"; |
| 2577 | } |
| 2578 | else |
| 2579 | Result += "\t, 0\n"; |
| 2580 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2583 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 2584 | /// ivar offset. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2585 | void RewriteTest::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 2586 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2587 | std::string &Result) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2588 | Result += "offsetof(struct "; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2589 | Result += IDecl->getName(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2590 | if (LangOpts.Microsoft) |
| 2591 | Result += "_IMPL"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2592 | Result += ", "; |
| 2593 | Result += ivar->getName(); |
| 2594 | Result += ")"; |
| 2595 | } |
| 2596 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2597 | //===----------------------------------------------------------------------===// |
| 2598 | // Meta Data Emission |
| 2599 | //===----------------------------------------------------------------------===// |
| 2600 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2601 | void RewriteTest::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2602 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2603 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2604 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 2605 | // Explictly declared @interface's are already synthesized. |
| 2606 | if (CDecl->ImplicitInterfaceDecl()) { |
| 2607 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 2608 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2609 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 2610 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2611 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2612 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 2613 | int NumIvars = IDecl->getImplDeclNumIvars() > 0 |
| 2614 | ? IDecl->getImplDeclNumIvars() |
| 2615 | : (CDecl ? CDecl->getNumInstanceVariables() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2616 | if (NumIvars > 0) { |
| 2617 | static bool objc_ivar = false; |
| 2618 | if (!objc_ivar) { |
| 2619 | /* struct _objc_ivar { |
| 2620 | char *ivar_name; |
| 2621 | char *ivar_type; |
| 2622 | int ivar_offset; |
| 2623 | }; |
| 2624 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2625 | Result += "\nstruct _objc_ivar {\n"; |
| 2626 | Result += "\tchar *ivar_name;\n"; |
| 2627 | Result += "\tchar *ivar_type;\n"; |
| 2628 | Result += "\tint ivar_offset;\n"; |
| 2629 | Result += "};\n"; |
| 2630 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2631 | objc_ivar = true; |
| 2632 | } |
| 2633 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2634 | /* struct { |
| 2635 | int ivar_count; |
| 2636 | struct _objc_ivar ivar_list[nIvars]; |
| 2637 | }; |
| 2638 | */ |
| 2639 | Result += "\nstatic struct {\n"; |
| 2640 | Result += "\tint ivar_count;\n"; |
| 2641 | Result += "\tstruct _objc_ivar ivar_list["; |
| 2642 | Result += utostr(NumIvars); |
| 2643 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2644 | Result += IDecl->getName(); |
| 2645 | Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= " |
| 2646 | "{\n\t"; |
| 2647 | Result += utostr(NumIvars); |
| 2648 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2649 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2650 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2651 | if (IDecl->getImplDeclNumIvars() > 0) { |
| 2652 | IVI = IDecl->ivar_begin(); |
| 2653 | IVE = IDecl->ivar_end(); |
| 2654 | } else { |
| 2655 | IVI = CDecl->ivar_begin(); |
| 2656 | IVE = CDecl->ivar_end(); |
| 2657 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2658 | Result += "\t,{{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2659 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2660 | Result += "\", \""; |
| 2661 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2662 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding, |
| 2663 | EncodingRecordTypes); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2664 | Result += StrEncoding; |
| 2665 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2666 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2667 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2668 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2669 | Result += "\t ,{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2670 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2671 | Result += "\", \""; |
| 2672 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2673 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding, |
| 2674 | EncodingRecordTypes); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2675 | Result += StrEncoding; |
| 2676 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2677 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2678 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2685 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2686 | true, "", IDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2687 | |
| 2688 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2689 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2690 | false, "", IDecl->getName(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2691 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2692 | // Protocols referenced in class declaration? |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2693 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2694 | CDecl->getNumIntfRefProtocols(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2695 | "CLASS", CDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2696 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2697 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2698 | // Declaration of class/meta-class metadata |
| 2699 | /* struct _objc_class { |
| 2700 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2701 | const char *super_class_name; |
| 2702 | char *name; |
| 2703 | long version; |
| 2704 | long info; |
| 2705 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2706 | struct _objc_ivar_list *ivars; |
| 2707 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2708 | struct objc_cache *cache; |
| 2709 | struct objc_protocol_list *protocols; |
| 2710 | const char *ivar_layout; |
| 2711 | struct _objc_class_ext *ext; |
| 2712 | }; |
| 2713 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2714 | static bool objc_class = false; |
| 2715 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2716 | Result += "\nstruct _objc_class {\n"; |
| 2717 | Result += "\tstruct _objc_class *isa;\n"; |
| 2718 | Result += "\tconst char *super_class_name;\n"; |
| 2719 | Result += "\tchar *name;\n"; |
| 2720 | Result += "\tlong version;\n"; |
| 2721 | Result += "\tlong info;\n"; |
| 2722 | Result += "\tlong instance_size;\n"; |
| 2723 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 2724 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 2725 | Result += "\tstruct objc_cache *cache;\n"; |
| 2726 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2727 | Result += "\tconst char *ivar_layout;\n"; |
| 2728 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 2729 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2730 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2734 | ObjCInterfaceDecl *RootClass = 0; |
| 2735 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2736 | while (SuperClass) { |
| 2737 | RootClass = SuperClass; |
| 2738 | SuperClass = SuperClass->getSuperClass(); |
| 2739 | } |
| 2740 | SuperClass = CDecl->getSuperClass(); |
| 2741 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2742 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 2743 | Result += CDecl->getName(); |
| 2744 | Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= " |
| 2745 | "{\n\t(struct _objc_class *)\""; |
| 2746 | Result += (RootClass ? RootClass->getName() : CDecl->getName()); |
| 2747 | Result += "\""; |
| 2748 | |
| 2749 | if (SuperClass) { |
| 2750 | Result += ", \""; |
| 2751 | Result += SuperClass->getName(); |
| 2752 | Result += "\", \""; |
| 2753 | Result += CDecl->getName(); |
| 2754 | Result += "\""; |
| 2755 | } |
| 2756 | else { |
| 2757 | Result += ", 0, \""; |
| 2758 | Result += CDecl->getName(); |
| 2759 | Result += "\""; |
| 2760 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2761 | // 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] | 2762 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2763 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2764 | if (IDecl->getNumClassMethods() > 0) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame^] | 2765 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2766 | Result += IDecl->getName(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2767 | Result += "\n"; |
| 2768 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2769 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2770 | Result += ", 0\n"; |
| 2771 | if (CDecl->getNumIntfRefProtocols() > 0) { |
| 2772 | Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_"; |
| 2773 | Result += CDecl->getName(); |
| 2774 | Result += ",0,0\n"; |
| 2775 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 2776 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2777 | Result += "\t,0,0,0,0\n"; |
| 2778 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2779 | |
| 2780 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2781 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 2782 | Result += CDecl->getName(); |
| 2783 | Result += " __attribute__ ((section (\"__OBJC, __class\")))= " |
| 2784 | "{\n\t&_OBJC_METACLASS_"; |
| 2785 | Result += CDecl->getName(); |
| 2786 | if (SuperClass) { |
| 2787 | Result += ", \""; |
| 2788 | Result += SuperClass->getName(); |
| 2789 | Result += "\", \""; |
| 2790 | Result += CDecl->getName(); |
| 2791 | Result += "\""; |
| 2792 | } |
| 2793 | else { |
| 2794 | Result += ", 0, \""; |
| 2795 | Result += CDecl->getName(); |
| 2796 | Result += "\""; |
| 2797 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2798 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2799 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2800 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2801 | Result += ",0"; |
| 2802 | else { |
| 2803 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2804 | Result += ",sizeof(struct "; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2805 | Result += CDecl->getName(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 2806 | if (LangOpts.Microsoft) |
| 2807 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2808 | Result += ")"; |
| 2809 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2810 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2811 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2812 | Result += CDecl->getName(); |
| 2813 | Result += "\n\t"; |
| 2814 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2815 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2816 | Result += ",0"; |
| 2817 | if (IDecl->getNumInstanceMethods() > 0) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2818 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2819 | Result += CDecl->getName(); |
| 2820 | Result += ", 0\n\t"; |
| 2821 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2822 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2823 | Result += ",0,0"; |
| 2824 | if (CDecl->getNumIntfRefProtocols() > 0) { |
| 2825 | Result += ", &_OBJC_CLASS_PROTOCOLS_"; |
| 2826 | Result += CDecl->getName(); |
| 2827 | Result += ", 0,0\n"; |
| 2828 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2829 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2830 | Result += ",0,0,0\n"; |
| 2831 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2832 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2833 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2834 | /// RewriteImplementations - This routine rewrites all method implementations |
| 2835 | /// and emits meta-data. |
| 2836 | |
| 2837 | void RewriteTest::RewriteImplementations(std::string &Result) { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2838 | int ClsDefCount = ClassImplementation.size(); |
| 2839 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2840 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2841 | if (ClsDefCount == 0 && CatDefCount == 0) |
| 2842 | return; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2843 | // Rewrite implemented methods |
| 2844 | for (int i = 0; i < ClsDefCount; i++) |
| 2845 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2846 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 2847 | for (int i = 0; i < CatDefCount; i++) |
| 2848 | RewriteImplementationDecl(CategoryImplementation[i]); |
| 2849 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2850 | // This is needed for use of offsetof |
| 2851 | Result += "#include <stddef.h>\n"; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2852 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2853 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2854 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2855 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2856 | |
| 2857 | // For each implemented category, write out all its meta data. |
| 2858 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2859 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2860 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2861 | // Write objc_symtab metadata |
| 2862 | /* |
| 2863 | struct _objc_symtab |
| 2864 | { |
| 2865 | long sel_ref_cnt; |
| 2866 | SEL *refs; |
| 2867 | short cls_def_cnt; |
| 2868 | short cat_def_cnt; |
| 2869 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 2870 | }; |
| 2871 | */ |
| 2872 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2873 | Result += "\nstruct _objc_symtab {\n"; |
| 2874 | Result += "\tlong sel_ref_cnt;\n"; |
| 2875 | Result += "\tSEL *refs;\n"; |
| 2876 | Result += "\tshort cls_def_cnt;\n"; |
| 2877 | Result += "\tshort cat_def_cnt;\n"; |
| 2878 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 2879 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2880 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2881 | Result += "static struct _objc_symtab " |
| 2882 | "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n"; |
| 2883 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 2884 | + ", " + utostr(CatDefCount) + "\n"; |
| 2885 | for (int i = 0; i < ClsDefCount; i++) { |
| 2886 | Result += "\t,&_OBJC_CLASS_"; |
| 2887 | Result += ClassImplementation[i]->getName(); |
| 2888 | Result += "\n"; |
| 2889 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2890 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2891 | for (int i = 0; i < CatDefCount; i++) { |
| 2892 | Result += "\t,&_OBJC_CATEGORY_"; |
| 2893 | Result += CategoryImplementation[i]->getClassInterface()->getName(); |
| 2894 | Result += "_"; |
| 2895 | Result += CategoryImplementation[i]->getName(); |
| 2896 | Result += "\n"; |
| 2897 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2898 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2899 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2900 | |
| 2901 | // Write objc_module metadata |
| 2902 | |
| 2903 | /* |
| 2904 | struct _objc_module { |
| 2905 | long version; |
| 2906 | long size; |
| 2907 | const char *name; |
| 2908 | struct _objc_symtab *symtab; |
| 2909 | } |
| 2910 | */ |
| 2911 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2912 | Result += "\nstruct _objc_module {\n"; |
| 2913 | Result += "\tlong version;\n"; |
| 2914 | Result += "\tlong size;\n"; |
| 2915 | Result += "\tconst char *name;\n"; |
| 2916 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 2917 | Result += "};\n\n"; |
| 2918 | Result += "static struct _objc_module " |
| 2919 | "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2920 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 2921 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2922 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 2923 | |
| 2924 | if (LangOpts.Microsoft) { |
| 2925 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 2926 | Result += "#pragma data_seq(push, \".objc_module_info$B\")\n"; |
| 2927 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 2928 | Result += "&_OBJC_MODULES;\n"; |
| 2929 | Result += "#pragma data_seg(pop)\n\n"; |
| 2930 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2931 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2932 | |