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