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