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