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