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