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 | } |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame^] | 1070 | else { |
| 1071 | DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1072 | elementName = DR->getDecl()->getName(); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1073 | elementTypeAsString = DR->getDecl()->getType().getAsString(); |
| 1074 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1075 | |
| 1076 | // struct __objcFastEnumerationState enumState = { 0 }; |
| 1077 | buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; |
| 1078 | // id items[16]; |
| 1079 | buf += "id items[16];\n\t"; |
| 1080 | // id l_collection = (id) |
| 1081 | buf += "id l_collection = (id)"; |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1082 | // Find start location of 'collection' the hard way! |
| 1083 | const char *startCollectionBuf = startBuf; |
| 1084 | startCollectionBuf += 3; // skip 'for' |
| 1085 | startCollectionBuf = strchr(startCollectionBuf, '('); |
| 1086 | startCollectionBuf++; // skip '(' |
| 1087 | // find 'in' and skip it. |
| 1088 | while (*startCollectionBuf != ' ' || |
| 1089 | *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || |
| 1090 | (*(startCollectionBuf+3) != ' ' && |
| 1091 | *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) |
| 1092 | startCollectionBuf++; |
| 1093 | startCollectionBuf += 3; |
| 1094 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1095 | // Replace: "for (type element in" with string constructed thus far. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1096 | ReplaceText(startLoc, startCollectionBuf - startBuf, |
| 1097 | buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1098 | // Replace ')' in for '(' type elem in collection ')' with ';' |
Fariborz Jahanian | 7571228 | 2008-01-10 00:24:29 +0000 | [diff] [blame] | 1099 | SourceLocation rightParenLoc = S->getRParenLoc(); |
| 1100 | const char *rparenBuf = SM->getCharacterData(rightParenLoc); |
| 1101 | SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1102 | buf = ";\n\t"; |
| 1103 | |
| 1104 | // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState |
| 1105 | // objects:items count:16]; |
| 1106 | // which is synthesized into: |
| 1107 | // unsigned int limit = |
| 1108 | // ((unsigned int (*) |
| 1109 | // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) |
| 1110 | // (void *)objc_msgSend)((id)l_collection, |
| 1111 | // sel_registerName( |
| 1112 | // "countByEnumeratingWithState:objects:count:"), |
| 1113 | // (struct __objcFastEnumerationState *)&state, |
| 1114 | // (id *)items, (unsigned int)16); |
| 1115 | buf += "unsigned long limit =\n\t\t"; |
| 1116 | SynthCountByEnumWithState(buf); |
| 1117 | buf += ";\n\t"; |
| 1118 | /// if (limit) { |
| 1119 | /// unsigned long startMutations = *enumState.mutationsPtr; |
| 1120 | /// do { |
| 1121 | /// unsigned long counter = 0; |
| 1122 | /// do { |
| 1123 | /// if (startMutations != *enumState.mutationsPtr) |
| 1124 | /// objc_enumerationMutation(l_collection); |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1125 | /// elem = (type)enumState.itemsPtr[counter++]; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1126 | buf += "if (limit) {\n\t"; |
| 1127 | buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; |
| 1128 | buf += "do {\n\t\t"; |
| 1129 | buf += "unsigned long counter = 0;\n\t\t"; |
| 1130 | buf += "do {\n\t\t\t"; |
| 1131 | buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; |
| 1132 | buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; |
| 1133 | buf += elementName; |
Fariborz Jahanian | 88f50f3 | 2008-01-09 18:15:42 +0000 | [diff] [blame] | 1134 | buf += " = ("; |
| 1135 | buf += elementTypeAsString; |
| 1136 | buf += ")enumState.itemsPtr[counter++];"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1137 | // Replace ')' in for '(' type elem in collection ')' with all of these. |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1138 | ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1139 | |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1140 | /// __continue_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1141 | /// } while (counter < limit); |
| 1142 | /// } while (limit = [l_collection countByEnumeratingWithState:&enumState |
| 1143 | /// objects:items count:16]); |
| 1144 | /// elem = nil; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1145 | /// __break_label: ; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1146 | /// } |
| 1147 | /// else |
| 1148 | /// elem = nil; |
| 1149 | /// } |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1150 | /// |
| 1151 | buf = ";\n\t"; |
| 1152 | buf += "__continue_label_"; |
| 1153 | buf += utostr(ObjCBcLabelNo.back()); |
| 1154 | buf += ": ;"; |
| 1155 | buf += "\n\t\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1156 | buf += "} while (counter < limit);\n\t"; |
| 1157 | buf += "} while (limit = "; |
| 1158 | SynthCountByEnumWithState(buf); |
| 1159 | buf += ");\n\t"; |
| 1160 | buf += elementName; |
| 1161 | buf += " = nil;\n\t"; |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1162 | buf += "__break_label_"; |
| 1163 | buf += utostr(ObjCBcLabelNo.back()); |
| 1164 | buf += ": ;\n\t"; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1165 | buf += "}\n\t"; |
| 1166 | buf += "else\n\t\t"; |
| 1167 | buf += elementName; |
| 1168 | buf += " = nil;\n"; |
| 1169 | buf += "}\n"; |
| 1170 | // Insert all these *after* the statement body. |
Chris Lattner | 338d1e2 | 2008-01-31 05:10:40 +0000 | [diff] [blame] | 1171 | SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1172 | InsertText(endBodyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | e8d1c05 | 2008-01-15 23:58:23 +0000 | [diff] [blame] | 1173 | Stmts.pop_back(); |
| 1174 | ObjCBcLabelNo.pop_back(); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1175 | return 0; |
Fariborz Jahanian | 10d24f0 | 2008-01-07 21:40:22 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1178 | /// RewriteObjCSynchronizedStmt - |
| 1179 | /// This routine rewrites @synchronized(expr) stmt; |
| 1180 | /// into: |
| 1181 | /// objc_sync_enter(expr); |
| 1182 | /// @try stmt @finally { objc_sync_exit(expr); } |
| 1183 | /// |
| 1184 | Stmt *RewriteTest::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
| 1185 | // Get the start location and compute the semi location. |
| 1186 | SourceLocation startLoc = S->getLocStart(); |
| 1187 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1188 | |
| 1189 | assert((*startBuf == '@') && "bogus @synchronized location"); |
| 1190 | |
| 1191 | std::string buf; |
| 1192 | buf = "objc_sync_enter"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1193 | ReplaceText(startLoc, 13, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1194 | SourceLocation endLoc = S->getSynchExpr()->getLocEnd(); |
| 1195 | const char *endBuf = SM->getCharacterData(endLoc); |
| 1196 | endBuf++; |
| 1197 | const char *rparenBuf = strchr(endBuf, ')'); |
| 1198 | SourceLocation rparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); |
| 1199 | buf = ");\n"; |
| 1200 | // declare a new scope with two variables, _stack and _rethrow. |
| 1201 | buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; |
| 1202 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1203 | buf += "char *pointers[4];} _stack;\n"; |
| 1204 | buf += "id volatile _rethrow = 0;\n"; |
| 1205 | buf += "objc_exception_try_enter(&_stack);\n"; |
| 1206 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1207 | ReplaceText(rparenLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1208 | startLoc = S->getSynchBody()->getLocEnd(); |
| 1209 | startBuf = SM->getCharacterData(startLoc); |
| 1210 | |
| 1211 | assert((*startBuf == '}') && "bogus @try block"); |
| 1212 | SourceLocation lastCurlyLoc = startLoc; |
| 1213 | buf = "}\nelse {\n"; |
| 1214 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1215 | buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
| 1216 | // FIXME: This must be objc_sync_exit(syncExpr); |
| 1217 | buf += " objc_sync_exit();\n"; |
| 1218 | buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
| 1219 | buf += "}\n"; |
| 1220 | buf += "}"; |
| 1221 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1222 | ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size()); |
Fariborz Jahanian | a0f5579 | 2008-01-29 22:59:37 +0000 | [diff] [blame] | 1223 | return 0; |
| 1224 | } |
| 1225 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1226 | Stmt *RewriteTest::RewriteObjCTryStmt(ObjCAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1227 | // Get the start location and compute the semi location. |
| 1228 | SourceLocation startLoc = S->getLocStart(); |
| 1229 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1230 | |
| 1231 | assert((*startBuf == '@') && "bogus @try location"); |
| 1232 | |
| 1233 | std::string buf; |
| 1234 | // declare a new scope with two variables, _stack and _rethrow. |
| 1235 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 1236 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 1237 | buf += "char *pointers[4];} _stack;\n"; |
| 1238 | buf += "id volatile _rethrow = 0;\n"; |
| 1239 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1240 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1242 | ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1243 | |
| 1244 | startLoc = S->getTryBody()->getLocEnd(); |
| 1245 | startBuf = SM->getCharacterData(startLoc); |
| 1246 | |
| 1247 | assert((*startBuf == '}') && "bogus @try block"); |
| 1248 | |
| 1249 | SourceLocation lastCurlyLoc = startLoc; |
| 1250 | |
| 1251 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1252 | buf = " /* @catch begin */ else {\n"; |
| 1253 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 1254 | buf += " objc_exception_try_enter (&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1255 | buf += " if (_setjmp(_stack.buf))\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1256 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 1257 | buf += " else { /* @catch continue */"; |
| 1258 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1259 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1260 | |
| 1261 | bool sawIdTypedCatch = false; |
| 1262 | Stmt *lastCatchBody = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1263 | ObjCAtCatchStmt *catchList = S->getCatchStmts(); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1264 | while (catchList) { |
| 1265 | Stmt *catchStmt = catchList->getCatchParamStmt(); |
| 1266 | |
| 1267 | if (catchList == S->getCatchStmts()) |
| 1268 | buf = "if ("; // we are generating code for the first catch clause |
| 1269 | else |
| 1270 | buf = "else if ("; |
| 1271 | startLoc = catchList->getLocStart(); |
| 1272 | startBuf = SM->getCharacterData(startLoc); |
| 1273 | |
| 1274 | assert((*startBuf == '@') && "bogus @catch location"); |
| 1275 | |
| 1276 | const char *lParenLoc = strchr(startBuf, '('); |
| 1277 | |
Steve Naroff | be4b333 | 2008-02-01 22:08:12 +0000 | [diff] [blame] | 1278 | if (catchList->hasEllipsis()) { |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1279 | // Now rewrite the body... |
| 1280 | lastCatchBody = catchList->getCatchBody(); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1281 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1282 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame^] | 1283 | assert(*SM->getCharacterData(catchList->getRParenLoc()) == ')' && |
| 1284 | "bogus @catch paren location"); |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1285 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1286 | |
| 1287 | buf += "1) { id _tmp = _caught;"; |
| 1288 | Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, |
| 1289 | buf.c_str(), buf.size()); |
| 1290 | } else if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1291 | QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1292 | if (t == Context->getObjCIdType()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1293 | buf += "1) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1294 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1295 | sawIdTypedCatch = true; |
| 1296 | } else if (const PointerType *pType = t->getAsPointerType()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1297 | ObjCInterfaceType *cls; // Should be a pointer to a class. |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1298 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1299 | cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1300 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1301 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1302 | buf += cls->getDecl()->getName(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 1303 | buf += "\"), (struct objc_object *)_caught)) { "; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1304 | ReplaceText(startLoc, lParenLoc-startBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | // Now rewrite the body... |
| 1308 | lastCatchBody = catchList->getCatchBody(); |
| 1309 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 1310 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 1311 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 1312 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 1313 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 1314 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 1315 | |
| 1316 | buf = " = _caught;"; |
| 1317 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 1318 | // declares the @catch parameter). |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1319 | ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1320 | } else if (!isa<NullStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1321 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1322 | } |
Steve Naroff | e12e692 | 2008-02-01 20:02:07 +0000 | [diff] [blame] | 1323 | // make sure all the catch bodies get rewritten! |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1324 | catchList = catchList->getNextCatchStmt(); |
| 1325 | } |
| 1326 | // Complete the catch list... |
| 1327 | if (lastCatchBody) { |
| 1328 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame^] | 1329 | assert(*SM->getCharacterData(bodyLoc) == '}' && |
| 1330 | "bogus @catch body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1331 | bodyLoc = bodyLoc.getFileLocWithOffset(1); |
| 1332 | buf = " } } /* @catch end */\n"; |
| 1333 | |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1334 | InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1335 | |
| 1336 | // Set lastCurlyLoc |
| 1337 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 1338 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1339 | if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1340 | startLoc = finalStmt->getLocStart(); |
| 1341 | startBuf = SM->getCharacterData(startLoc); |
| 1342 | assert((*startBuf == '@') && "bogus @finally start"); |
| 1343 | |
| 1344 | buf = "/* @finally */"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1345 | ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1346 | |
| 1347 | Stmt *body = finalStmt->getFinallyBody(); |
| 1348 | SourceLocation startLoc = body->getLocStart(); |
| 1349 | SourceLocation endLoc = body->getLocEnd(); |
Chris Lattner | 0676751 | 2008-04-08 05:52:18 +0000 | [diff] [blame^] | 1350 | assert(*SM->getCharacterData(startLoc) == '{' && |
| 1351 | "bogus @finally body location"); |
| 1352 | assert(*SM->getCharacterData(endLoc) == '}' && |
| 1353 | "bogus @finally body location"); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1354 | |
| 1355 | startLoc = startLoc.getFileLocWithOffset(1); |
| 1356 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1357 | InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1358 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 1359 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1360 | InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 1361 | |
| 1362 | // Set lastCurlyLoc |
| 1363 | lastCurlyLoc = body->getLocEnd(); |
| 1364 | } |
| 1365 | // Now emit the final closing curly brace... |
| 1366 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 1367 | buf = " } /* @try scope end */\n"; |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1368 | InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1369 | return 0; |
| 1370 | } |
| 1371 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1372 | Stmt *RewriteTest::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1373 | return 0; |
| 1374 | } |
| 1375 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1376 | Stmt *RewriteTest::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1380 | // This can't be done with ReplaceStmt(S, ThrowExpr), since |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1381 | // the throw expression is typically a message expression that's already |
| 1382 | // been rewritten! (which implies the SourceLocation's are invalid). |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1383 | Stmt *RewriteTest::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1384 | // Get the start location and compute the semi location. |
| 1385 | SourceLocation startLoc = S->getLocStart(); |
| 1386 | const char *startBuf = SM->getCharacterData(startLoc); |
| 1387 | |
| 1388 | assert((*startBuf == '@') && "bogus @throw location"); |
| 1389 | |
| 1390 | std::string buf; |
| 1391 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
Steve Naroff | 20ebf8f | 2008-01-19 00:42:38 +0000 | [diff] [blame] | 1392 | if (S->getThrowExpr()) |
| 1393 | buf = "objc_exception_throw("; |
| 1394 | else // add an implicit argument |
| 1395 | buf = "objc_exception_throw(_caught"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1396 | ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1397 | const char *semiBuf = strchr(startBuf, ';'); |
| 1398 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 1399 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 1400 | buf = ");"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 1401 | ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 1402 | return 0; |
| 1403 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1405 | Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1406 | // Create a new string expression. |
| 1407 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1408 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1409 | Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding, |
| 1410 | EncodingRecordTypes); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1411 | Expr *Replacement = new StringLiteral(StrEncoding.c_str(), |
| 1412 | StrEncoding.length(), false, StrType, |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 1413 | SourceLocation(), SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1414 | ReplaceStmt(Exp, Replacement); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 1416 | // Replace this subexpr in the parent. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1417 | delete Exp; |
| 1418 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1421 | Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 1422 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 1423 | // Create a call to sel_registerName("selName"). |
| 1424 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1425 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1426 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 1427 | Exp->getSelector().getName().size(), |
| 1428 | false, argType, SourceLocation(), |
| 1429 | SourceLocation())); |
| 1430 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1431 | &SelExprs[0], SelExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1432 | ReplaceStmt(Exp, SelExp); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 1433 | delete Exp; |
| 1434 | return SelExp; |
| 1435 | } |
| 1436 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1437 | CallExpr *RewriteTest::SynthesizeCallToFunctionDecl( |
| 1438 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1439 | // 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] | 1440 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1441 | |
| 1442 | // Create a reference to the objc_msgSend() declaration. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1443 | DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1444 | |
| 1445 | // 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] | 1446 | QualType pToFunc = Context->getPointerType(msgSendType); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1447 | ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE); |
| 1448 | |
| 1449 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1450 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1451 | return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); |
| 1452 | } |
| 1453 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1454 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 1455 | const char *&startRef, const char *&endRef) { |
| 1456 | while (startBuf < endBuf) { |
| 1457 | if (*startBuf == '<') |
| 1458 | startRef = startBuf; // mark the start. |
| 1459 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 1460 | if (startRef && *startRef == '<') { |
| 1461 | endRef = startBuf; // mark the end. |
| 1462 | return true; |
| 1463 | } |
| 1464 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1465 | } |
| 1466 | startBuf++; |
| 1467 | } |
| 1468 | return false; |
| 1469 | } |
| 1470 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1471 | static void scanToNextArgument(const char *&argRef) { |
| 1472 | int angle = 0; |
| 1473 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 1474 | if (*argRef == '<') |
| 1475 | angle++; |
| 1476 | else if (*argRef == '>') |
| 1477 | angle--; |
| 1478 | argRef++; |
| 1479 | } |
| 1480 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 1481 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1482 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1483 | bool RewriteTest::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1484 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1485 | if (T == Context->getObjCIdType()) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1486 | return true; |
| 1487 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1488 | if (T->isObjCQualifiedIdType()) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1489 | return true; |
| 1490 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1491 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1492 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1493 | if (isa<ObjCQualifiedInterfaceType>(pointeeType)) |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1494 | return true; // we have "Class <Protocol> *". |
| 1495 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
| 1498 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1499 | void RewriteTest::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1500 | SourceLocation Loc; |
| 1501 | QualType Type; |
| 1502 | const FunctionTypeProto *proto = 0; |
| 1503 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1504 | Loc = VD->getLocation(); |
| 1505 | Type = VD->getType(); |
| 1506 | } |
| 1507 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1508 | Loc = FD->getLocation(); |
| 1509 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1510 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1511 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1512 | assert(funcType && "missing function type"); |
| 1513 | proto = dyn_cast<FunctionTypeProto>(funcType); |
| 1514 | if (!proto) |
| 1515 | return; |
| 1516 | Type = proto->getResultType(); |
| 1517 | } |
| 1518 | else |
| 1519 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1520 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1521 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1522 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1523 | |
| 1524 | const char *endBuf = SM->getCharacterData(Loc); |
| 1525 | const char *startBuf = endBuf; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 1526 | while (*startBuf != ';' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1527 | startBuf--; // scan backward (from the decl location) for return type. |
| 1528 | const char *startRef = 0, *endRef = 0; |
| 1529 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1530 | // Get the locations of the startRef, endRef. |
| 1531 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1532 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1533 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1534 | InsertText(LessLoc, "/*", 2); |
| 1535 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1538 | if (!proto) |
| 1539 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1540 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1541 | const char *startBuf = SM->getCharacterData(Loc); |
| 1542 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1543 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1544 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1545 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1546 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1547 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1548 | // scan forward (from the decl location) for argument types. |
| 1549 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1550 | const char *startRef = 0, *endRef = 0; |
| 1551 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1552 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1553 | SourceLocation LessLoc = |
| 1554 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1555 | SourceLocation GreaterLoc = |
| 1556 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1557 | // Comment out the protocol references. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 1558 | InsertText(LessLoc, "/*", 2); |
| 1559 | InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1560 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1561 | startBuf = ++endBuf; |
| 1562 | } |
| 1563 | else { |
| 1564 | while (*startBuf != ')' && *startBuf != ',') |
| 1565 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1566 | startBuf++; |
| 1567 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1568 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1571 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 1572 | void RewriteTest::SynthSelGetUidFunctionDecl() { |
| 1573 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1574 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1575 | ArgTys.push_back(Context->getPointerType( |
| 1576 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1577 | QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1578 | &ArgTys[0], ArgTys.size(), |
| 1579 | false /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1580 | SelGetUidFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1581 | SourceLocation(), |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1582 | SelGetUidIdent, getFuncType, |
| 1583 | FunctionDecl::Extern, false, 0); |
| 1584 | } |
| 1585 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1586 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
| 1587 | void RewriteTest::SynthGetProtocolFunctionDecl() { |
| 1588 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1589 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1590 | ArgTys.push_back(Context->getPointerType( |
| 1591 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1592 | QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1593 | &ArgTys[0], ArgTys.size(), |
| 1594 | false /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1595 | GetProtocolFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1596 | SourceLocation(), |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1597 | SelGetProtoIdent, getFuncType, |
| 1598 | FunctionDecl::Extern, false, 0); |
| 1599 | } |
| 1600 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1601 | void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) { |
| 1602 | // declared in <objc/objc.h> |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1603 | if (strcmp(FD->getName(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1604 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1605 | return; |
| 1606 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1607 | RewriteObjCQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1610 | // SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); |
| 1611 | void RewriteTest::SynthSuperContructorFunctionDecl() { |
| 1612 | if (SuperContructorFunctionDecl) |
| 1613 | return; |
| 1614 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_super"); |
| 1615 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1616 | QualType argT = Context->getObjCIdType(); |
| 1617 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1618 | ArgTys.push_back(argT); |
| 1619 | ArgTys.push_back(argT); |
| 1620 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
| 1621 | &ArgTys[0], ArgTys.size(), |
| 1622 | false); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1623 | SuperContructorFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1624 | SourceLocation(), |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1625 | msgSendIdent, msgSendType, |
| 1626 | FunctionDecl::Extern, false, 0); |
| 1627 | } |
| 1628 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1629 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 1630 | void RewriteTest::SynthMsgSendFunctionDecl() { |
| 1631 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1632 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1633 | QualType argT = Context->getObjCIdType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1634 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1635 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1636 | argT = Context->getObjCSelType(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1637 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1638 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1639 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1640 | &ArgTys[0], ArgTys.size(), |
| 1641 | true /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1642 | MsgSendFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1643 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1644 | msgSendIdent, msgSendType, |
| 1645 | FunctionDecl::Extern, false, 0); |
| 1646 | } |
| 1647 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1648 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
| 1649 | void RewriteTest::SynthMsgSendSuperFunctionDecl() { |
| 1650 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 1651 | llvm::SmallVector<QualType, 16> ArgTys; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1652 | RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, NULL, |
| 1653 | SourceLocation(), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1654 | &Context->Idents.get("objc_super"), 0); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1655 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1656 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1657 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1658 | argT = Context->getObjCSelType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1659 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1660 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1661 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1662 | &ArgTys[0], ArgTys.size(), |
| 1663 | true /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1664 | MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1665 | SourceLocation(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1666 | msgSendIdent, msgSendType, |
| 1667 | FunctionDecl::Extern, false, 0); |
| 1668 | } |
| 1669 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1670 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 1671 | void RewriteTest::SynthMsgSendStretFunctionDecl() { |
| 1672 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 1673 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1674 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1675 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1676 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1677 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1678 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1679 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1680 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1681 | &ArgTys[0], ArgTys.size(), |
| 1682 | true /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1683 | MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1684 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1685 | msgSendIdent, msgSendType, |
| 1686 | FunctionDecl::Extern, false, 0); |
| 1687 | } |
| 1688 | |
| 1689 | // SynthMsgSendSuperStretFunctionDecl - |
| 1690 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
| 1691 | void RewriteTest::SynthMsgSendSuperStretFunctionDecl() { |
| 1692 | IdentifierInfo *msgSendIdent = |
| 1693 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 1694 | llvm::SmallVector<QualType, 16> ArgTys; |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1695 | RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, NULL, |
| 1696 | SourceLocation(), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1697 | &Context->Idents.get("objc_super"), 0); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1698 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1699 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1700 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1701 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1702 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1703 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1704 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1705 | &ArgTys[0], ArgTys.size(), |
| 1706 | true /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1707 | MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, NULL, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 1708 | SourceLocation(), |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1709 | msgSendIdent, msgSendType, |
| 1710 | FunctionDecl::Extern, false, 0); |
| 1711 | } |
| 1712 | |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1713 | // SynthMsgSendFpretFunctionDecl - id objc_msgSend_fpret(id self, SEL op, ...); |
| 1714 | void RewriteTest::SynthMsgSendFpretFunctionDecl() { |
| 1715 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 1716 | llvm::SmallVector<QualType, 16> ArgTys; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1717 | QualType argT = Context->getObjCIdType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1718 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1719 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1720 | argT = Context->getObjCSelType(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1721 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1722 | ArgTys.push_back(argT); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1723 | QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1724 | &ArgTys[0], ArgTys.size(), |
| 1725 | true /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1726 | MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1727 | SourceLocation(), |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1728 | msgSendIdent, msgSendType, |
| 1729 | FunctionDecl::Extern, false, 0); |
| 1730 | } |
| 1731 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1732 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
| 1733 | void RewriteTest::SynthGetClassFunctionDecl() { |
| 1734 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 1735 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1736 | ArgTys.push_back(Context->getPointerType( |
| 1737 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1738 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1739 | &ArgTys[0], ArgTys.size(), |
| 1740 | false /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1741 | GetClassFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1742 | SourceLocation(), |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1743 | getClassIdent, getClassType, |
| 1744 | FunctionDecl::Extern, false, 0); |
| 1745 | } |
| 1746 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1747 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
| 1748 | void RewriteTest::SynthGetMetaClassFunctionDecl() { |
| 1749 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 1750 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1751 | ArgTys.push_back(Context->getPointerType( |
| 1752 | Context->CharTy.getQualifiedType(QualType::Const))); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1753 | QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1754 | &ArgTys[0], ArgTys.size(), |
| 1755 | false /*isVariadic*/); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1756 | GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, NULL, |
| 1757 | SourceLocation(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1758 | getClassIdent, getClassType, |
| 1759 | FunctionDecl::Extern, false, 0); |
| 1760 | } |
| 1761 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1762 | Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1763 | QualType strType = getConstantStringStructType(); |
| 1764 | |
| 1765 | std::string S = "__NSConstantStringImpl_"; |
| 1766 | S += utostr(NumObjCStringLiterals++); |
| 1767 | |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 1768 | Preamble += "static __NSConstantStringImpl " + S; |
| 1769 | Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; |
| 1770 | Preamble += "0x000007c8,"; // utf8_str |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1771 | // The pretty printer for StringLiteral handles escape characters properly. |
| 1772 | std::ostringstream prettyBuf; |
| 1773 | Exp->getString()->printPretty(prettyBuf); |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 1774 | Preamble += prettyBuf.str(); |
| 1775 | Preamble += ","; |
Steve Naroff | 3652c2d | 2008-03-15 01:36:04 +0000 | [diff] [blame] | 1776 | // The minus 2 removes the begin/end double quotes. |
Steve Naroff | ba92b2e | 2008-03-27 22:29:16 +0000 | [diff] [blame] | 1777 | Preamble += utostr(prettyBuf.str().size()-2) + "};\n"; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1778 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1779 | FileVarDecl *NewVD = FileVarDecl::Create(*Context, NULL, SourceLocation(), |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1780 | &Context->Idents.get(S.c_str()), strType, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1781 | VarDecl::Static, NULL); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1782 | DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation()); |
| 1783 | Expr *Unop = new UnaryOperator(DRE, UnaryOperator::AddrOf, |
| 1784 | Context->getPointerType(DRE->getType()), |
| 1785 | SourceLocation()); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1786 | // cast to NSConstantString * |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1787 | CastExpr *cast = new CastExpr(Exp->getType(), Unop, SourceLocation()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 1788 | ReplaceStmt(Exp, cast); |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1789 | delete Exp; |
| 1790 | return cast; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1793 | ObjCInterfaceDecl *RewriteTest::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1794 | // check if we are sending a message to 'super' |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 1795 | if (!CurMethodDecl || !CurMethodDecl->isInstance()) return 0; |
| 1796 | |
| 1797 | CastExpr *CE = dyn_cast<CastExpr>(recExpr); |
| 1798 | if (!CE) return 0; |
| 1799 | |
| 1800 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr()); |
| 1801 | if (!DRE) return 0; |
| 1802 | |
| 1803 | ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 1804 | if (!PVD) return 0; |
| 1805 | |
| 1806 | if (strcmp(PVD->getName(), "self") != 0) |
| 1807 | return 0; |
| 1808 | |
| 1809 | // is this id<P1..> type? |
| 1810 | if (CE->getType()->isObjCQualifiedIdType()) |
| 1811 | return 0; |
| 1812 | const PointerType *PT = CE->getType()->getAsPointerType(); |
| 1813 | if (!PT) return 0; |
| 1814 | |
| 1815 | ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 1816 | if (!IT) return 0; |
| 1817 | |
| 1818 | if (IT->getDecl() != CurMethodDecl->getClassInterface()->getSuperClass()) |
| 1819 | return 0; |
| 1820 | |
| 1821 | return IT->getDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
| 1825 | QualType RewriteTest::getSuperStructType() { |
| 1826 | if (!SuperStructDecl) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1827 | SuperStructDecl = RecordDecl::Create(*Context, Decl::Struct, NULL, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1828 | SourceLocation(), |
| 1829 | &Context->Idents.get("objc_super"), 0); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1830 | QualType FieldTypes[2]; |
| 1831 | |
| 1832 | // struct objc_object *receiver; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1833 | FieldTypes[0] = Context->getObjCIdType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1834 | // struct objc_class *super; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1835 | FieldTypes[1] = Context->getObjCClassType(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1836 | // Create fields |
| 1837 | FieldDecl *FieldDecls[2]; |
| 1838 | |
| 1839 | for (unsigned i = 0; i < 2; ++i) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1840 | FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 1841 | FieldTypes[i]); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1842 | |
| 1843 | SuperStructDecl->defineBody(FieldDecls, 4); |
| 1844 | } |
| 1845 | return Context->getTagDeclType(SuperStructDecl); |
| 1846 | } |
| 1847 | |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1848 | QualType RewriteTest::getConstantStringStructType() { |
| 1849 | if (!ConstantStringDecl) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1850 | ConstantStringDecl = RecordDecl::Create(*Context, Decl::Struct, NULL, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 1851 | SourceLocation(), |
| 1852 | &Context->Idents.get("__NSConstantStringImpl"), 0); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1853 | QualType FieldTypes[4]; |
| 1854 | |
| 1855 | // struct objc_object *receiver; |
| 1856 | FieldTypes[0] = Context->getObjCIdType(); |
| 1857 | // int flags; |
| 1858 | FieldTypes[1] = Context->IntTy; |
| 1859 | // char *str; |
| 1860 | FieldTypes[2] = Context->getPointerType(Context->CharTy); |
| 1861 | // long length; |
| 1862 | FieldTypes[3] = Context->LongTy; |
| 1863 | // Create fields |
Steve Naroff | 9630ec5 | 2008-03-27 22:59:54 +0000 | [diff] [blame] | 1864 | FieldDecl *FieldDecls[4]; |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1865 | |
| 1866 | for (unsigned i = 0; i < 4; ++i) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 1867 | FieldDecls[i] = FieldDecl::Create(*Context, SourceLocation(), 0, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 1868 | FieldTypes[i]); |
Steve Naroff | d82a9ab | 2008-03-15 00:55:56 +0000 | [diff] [blame] | 1869 | |
| 1870 | ConstantStringDecl->defineBody(FieldDecls, 4); |
| 1871 | } |
| 1872 | return Context->getTagDeclType(ConstantStringDecl); |
| 1873 | } |
| 1874 | |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 1875 | Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1876 | if (!SelGetUidFunctionDecl) |
| 1877 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1878 | if (!MsgSendFunctionDecl) |
| 1879 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1880 | if (!MsgSendSuperFunctionDecl) |
| 1881 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1882 | if (!MsgSendStretFunctionDecl) |
| 1883 | SynthMsgSendStretFunctionDecl(); |
| 1884 | if (!MsgSendSuperStretFunctionDecl) |
| 1885 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1886 | if (!MsgSendFpretFunctionDecl) |
| 1887 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1888 | if (!GetClassFunctionDecl) |
| 1889 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1890 | if (!GetMetaClassFunctionDecl) |
| 1891 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1892 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1893 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1894 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 1895 | // May need to use objc_msgSend_stret() as well. |
| 1896 | FunctionDecl *MsgSendStretFlavor = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1897 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1898 | QualType resultType = mDecl->getResultType(); |
| 1899 | if (resultType.getCanonicalType()->isStructureType() |
| 1900 | || resultType.getCanonicalType()->isUnionType()) |
| 1901 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1902 | else if (resultType.getCanonicalType()->isRealFloatingType()) |
| 1903 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1904 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1905 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1906 | // Synthesize a call to objc_msgSend(). |
| 1907 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 1908 | IdentifierInfo *clsName = Exp->getClassName(); |
| 1909 | |
| 1910 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 1911 | if (clsName) { // class message. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1912 | if (!strcmp(clsName->getName(), "super")) { |
| 1913 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 1914 | if (MsgSendStretFlavor) |
| 1915 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 1916 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 1917 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1918 | ObjCInterfaceDecl *SuperDecl = |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1919 | CurMethodDecl->getClassInterface()->getSuperClass(); |
| 1920 | |
| 1921 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1922 | |
| 1923 | // set the receiver to self, the first argument to all methods. |
| 1924 | InitExprs.push_back(new DeclRefExpr(CurMethodDecl->getSelfDecl(), |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1925 | Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1926 | SourceLocation())); |
| 1927 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1928 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1929 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 1930 | SuperDecl->getIdentifier()->getLength(), |
| 1931 | false, argType, SourceLocation(), |
| 1932 | SourceLocation())); |
| 1933 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 1934 | &ClsExprs[0], |
| 1935 | ClsExprs.size()); |
| 1936 | // To turn off a warning, type-cast to 'id' |
| 1937 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1938 | new CastExpr(Context->getObjCIdType(), |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1939 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
| 1940 | // struct objc_super |
| 1941 | QualType superType = getSuperStructType(); |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 1942 | Expr *SuperRep; |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 1943 | |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 1944 | if (LangOpts.Microsoft) { |
| 1945 | SynthSuperContructorFunctionDecl(); |
| 1946 | // Simulate a contructor call... |
| 1947 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 1948 | superType, SourceLocation()); |
| 1949 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 1950 | superType, SourceLocation()); |
| 1951 | } else { |
| 1952 | // (struct objc_super) { <exprs from above> } |
| 1953 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 1954 | &InitExprs[0], InitExprs.size(), |
| 1955 | SourceLocation()); |
| 1956 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 1957 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1958 | // struct objc_super * |
| 1959 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 1960 | Context->getPointerType(SuperRep->getType()), |
| 1961 | SourceLocation()); |
| 1962 | MsgExprs.push_back(Unop); |
| 1963 | } else { |
| 1964 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1965 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1966 | ClsExprs.push_back(new StringLiteral(clsName->getName(), |
| 1967 | clsName->getLength(), |
| 1968 | false, argType, SourceLocation(), |
| 1969 | SourceLocation())); |
| 1970 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 1971 | &ClsExprs[0], |
| 1972 | ClsExprs.size()); |
| 1973 | MsgExprs.push_back(Cls); |
| 1974 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1975 | } else { // instance message. |
| 1976 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1977 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1978 | if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1979 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1980 | if (MsgSendStretFlavor) |
| 1981 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1982 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 1983 | |
| 1984 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1985 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1986 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1987 | new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1988 | recExpr, SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1989 | |
| 1990 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1991 | QualType argType = Context->getPointerType(Context->CharTy); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1992 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 1993 | SuperDecl->getIdentifier()->getLength(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1994 | false, argType, SourceLocation(), |
| 1995 | SourceLocation())); |
| 1996 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1997 | &ClsExprs[0], |
| 1998 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 1999 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2000 | InitExprs.push_back( |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2001 | new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 2002 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2003 | // struct objc_super |
| 2004 | QualType superType = getSuperStructType(); |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2005 | Expr *SuperRep; |
| 2006 | |
| 2007 | if (LangOpts.Microsoft) { |
| 2008 | SynthSuperContructorFunctionDecl(); |
| 2009 | // Simulate a contructor call... |
| 2010 | DeclRefExpr *DRE = new DeclRefExpr(SuperContructorFunctionDecl, |
| 2011 | superType, SourceLocation()); |
| 2012 | SuperRep = new CallExpr(DRE, &InitExprs[0], InitExprs.size(), |
| 2013 | superType, SourceLocation()); |
| 2014 | } else { |
| 2015 | // (struct objc_super) { <exprs from above> } |
| 2016 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 2017 | &InitExprs[0], InitExprs.size(), |
| 2018 | SourceLocation()); |
| 2019 | SuperRep = new CompoundLiteralExpr(SourceLocation(), superType, ILE, false); |
| 2020 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2021 | // struct objc_super * |
| 2022 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 2023 | Context->getPointerType(SuperRep->getType()), |
| 2024 | SourceLocation()); |
| 2025 | MsgExprs.push_back(Unop); |
| 2026 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 2027 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 2028 | // Foo<Proto> *. |
| 2029 | while (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) |
| 2030 | recExpr = CE->getSubExpr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2031 | recExpr = new CastExpr(Context->getObjCIdType(), recExpr, SourceLocation()); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 2032 | MsgExprs.push_back(recExpr); |
| 2033 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2034 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2035 | // 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] | 2036 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 2037 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2038 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 2039 | Exp->getSelector().getName().size(), |
| 2040 | false, argType, SourceLocation(), |
| 2041 | SourceLocation())); |
| 2042 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 2043 | &SelExprs[0], SelExprs.size()); |
| 2044 | MsgExprs.push_back(SelExp); |
| 2045 | |
| 2046 | // Now push any user supplied arguments. |
| 2047 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2048 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2049 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 2050 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 2051 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2052 | userExpr = new CastExpr(ICE->getType()->isObjCQualifiedIdType() |
| 2053 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2054 | : ICE->getType(), userExpr, SourceLocation()); |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2055 | } |
| 2056 | // Make id<P...> cast into an 'id' cast. |
| 2057 | else if (CastExpr *CE = dyn_cast<CastExpr>(userExpr)) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2058 | if (CE->getType()->isObjCQualifiedIdType()) { |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2059 | while ((CE = dyn_cast<CastExpr>(userExpr))) |
| 2060 | userExpr = CE->getSubExpr(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2061 | userExpr = new CastExpr(Context->getObjCIdType(), |
Fariborz Jahanian | d58fabf | 2007-12-18 21:33:44 +0000 | [diff] [blame] | 2062 | userExpr, SourceLocation()); |
| 2063 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 2064 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 2065 | MsgExprs.push_back(userExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2066 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 2067 | // the original expression, since we will delete it below. |
| 2068 | Exp->setArg(i, 0); |
| 2069 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2070 | // Generate the funky cast. |
| 2071 | CastExpr *cast; |
| 2072 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2073 | QualType returnType; |
| 2074 | |
| 2075 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 2076 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 2077 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 2078 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2079 | ArgTypes.push_back(Context->getObjCIdType()); |
| 2080 | ArgTypes.push_back(Context->getObjCSelType()); |
| 2081 | if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2082 | // Push any user argument types. |
Chris Lattner | 58cce3b | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 2083 | for (unsigned i = 0; i < mDecl->getNumParams(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2084 | QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType() |
| 2085 | ? Context->getObjCIdType() |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2086 | : mDecl->getParamDecl(i)->getType(); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 2087 | ArgTypes.push_back(t); |
| 2088 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2089 | returnType = mDecl->getResultType()->isObjCQualifiedIdType() |
| 2090 | ? Context->getObjCIdType() : mDecl->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2091 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2092 | returnType = Context->getObjCIdType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2093 | } |
| 2094 | // 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] | 2095 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2096 | |
| 2097 | // Create a reference to the objc_msgSend() declaration. |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2098 | DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType, |
| 2099 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2100 | |
| 2101 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 2102 | // If we don't do this cast, we get the following bizarre warning/note: |
| 2103 | // xx.m:13: warning: function called through a non-compatible type |
| 2104 | // xx.m:13: note: if this code is reached, the program will abort |
| 2105 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE, |
| 2106 | SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 2107 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2108 | // Now do the "normal" pointer to function cast. |
| 2109 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2110 | &ArgTypes[0], ArgTypes.size(), |
Steve Naroff | 2679e48 | 2008-03-18 02:02:04 +0000 | [diff] [blame] | 2111 | // If we don't have a method decl, force a variadic cast. |
| 2112 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 2113 | castType = Context->getPointerType(castType); |
| 2114 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 2115 | |
| 2116 | // Don't forget the parens to enforce the proper binding. |
| 2117 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2118 | |
| 2119 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
| 2120 | CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2121 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2122 | Stmt *ReplacingStmt = CE; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2123 | if (MsgSendStretFlavor) { |
| 2124 | // We have the method which returns a struct/union. Must also generate |
| 2125 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 2126 | // expression which dictate which one to envoke depending on size of |
| 2127 | // method's return type. |
| 2128 | |
| 2129 | // Create a reference to the objc_msgSend_stret() declaration. |
| 2130 | DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType, |
| 2131 | SourceLocation()); |
| 2132 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 2133 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
| 2134 | SourceLocation()); |
| 2135 | // Now do the "normal" pointer to function cast. |
| 2136 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 2137 | &ArgTypes[0], ArgTypes.size(), |
| 2138 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2139 | castType = Context->getPointerType(castType); |
| 2140 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 2141 | |
| 2142 | // Don't forget the parens to enforce the proper binding. |
| 2143 | PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 2144 | |
| 2145 | FT = msgSendType->getAsFunctionType(); |
| 2146 | CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 2147 | FT->getResultType(), SourceLocation()); |
| 2148 | |
| 2149 | // Build sizeof(returnType) |
| 2150 | SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true, |
| 2151 | returnType, Context->getSizeType(), |
| 2152 | SourceLocation(), SourceLocation()); |
| 2153 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2154 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 2155 | // For X86 it is more complicated and some kind of target specific routine |
| 2156 | // is needed to decide what to do. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2157 | unsigned IntSize = |
| 2158 | static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2159 | IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8), |
| 2160 | Context->IntTy, |
| 2161 | SourceLocation()); |
| 2162 | BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit, |
| 2163 | BinaryOperator::LE, |
| 2164 | Context->IntTy, |
| 2165 | SourceLocation()); |
| 2166 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 2167 | ConditionalOperator *CondExpr = |
| 2168 | new ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2169 | ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 2170 | } |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2171 | return ReplacingStmt; |
| 2172 | } |
| 2173 | |
| 2174 | Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
| 2175 | Stmt *ReplacingStmt = SynthMessageExpr(Exp); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2176 | // Now do the actual rewrite. |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2177 | ReplaceStmt(Exp, ReplacingStmt); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 2178 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 2179 | delete Exp; |
Fariborz Jahanian | 33b9c4e | 2008-01-08 22:06:28 +0000 | [diff] [blame] | 2180 | return ReplacingStmt; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2183 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 2184 | /// call to objc_getProtocol("proto-name"). |
| 2185 | Stmt *RewriteTest::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
| 2186 | if (!GetProtocolFunctionDecl) |
| 2187 | SynthGetProtocolFunctionDecl(); |
| 2188 | // Create a call to objc_getProtocol("ProtocolName"). |
| 2189 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 2190 | QualType argType = Context->getPointerType(Context->CharTy); |
| 2191 | ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(), |
| 2192 | strlen(Exp->getProtocol()->getName()), |
| 2193 | false, argType, SourceLocation(), |
| 2194 | SourceLocation())); |
| 2195 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 2196 | &ProtoExprs[0], |
| 2197 | ProtoExprs.size()); |
Chris Lattner | dcbc5b0 | 2008-01-31 19:37:57 +0000 | [diff] [blame] | 2198 | ReplaceStmt(Exp, ProtoExp); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 2199 | delete Exp; |
| 2200 | return ProtoExp; |
| 2201 | |
| 2202 | } |
| 2203 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2204 | /// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2205 | /// an objective-c class with ivars. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2206 | void RewriteTest::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2207 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2208 | assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); |
| 2209 | assert(CDecl->getName() && "Name missing in SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2210 | // Do not synthesize more than once. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2211 | if (ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 2212 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2213 | ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2214 | int NumIvars = CDecl->ivar_size(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2215 | SourceLocation LocStart = CDecl->getLocStart(); |
| 2216 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 2217 | |
| 2218 | const char *startBuf = SM->getCharacterData(LocStart); |
| 2219 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2220 | // If no ivars and no root or if its root, directly or indirectly, |
| 2221 | // 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] | 2222 | if ((CDecl->isForwardDecl() || NumIvars == 0) && |
| 2223 | (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2224 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2225 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2226 | return; |
| 2227 | } |
| 2228 | |
| 2229 | // FIXME: This has potential of causing problem. If |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2230 | // SynthesizeObjCInternalStruct is ever called recursively. |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 2231 | Result += "\nstruct "; |
| 2232 | Result += CDecl->getName(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2233 | if (LangOpts.Microsoft) |
| 2234 | Result += "_IMPL"; |
Steve Naroff | 05b8c78 | 2008-03-12 00:25:36 +0000 | [diff] [blame] | 2235 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2236 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2237 | const char *cursor = strchr(startBuf, '{'); |
| 2238 | assert((cursor && endBuf) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2239 | && "SynthesizeObjCInternalStruct - malformed @interface"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2240 | |
| 2241 | // rewrite the original header *without* disturbing the '{' |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2242 | ReplaceText(LocStart, cursor-startBuf-1, Result.c_str(), Result.size()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2243 | if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2244 | Result = "\n struct "; |
| 2245 | Result += RCDecl->getName(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2246 | Result += "_IMPL "; |
| 2247 | Result += RCDecl->getName(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2248 | Result += "_IVARS;\n"; |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2249 | |
| 2250 | // insert the super class structure definition. |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2251 | SourceLocation OnePastCurly = |
| 2252 | LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 2253 | InsertText(OnePastCurly, Result.c_str(), Result.size()); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2254 | } |
| 2255 | cursor++; // past '{' |
| 2256 | |
| 2257 | // Now comment out any visibility specifiers. |
| 2258 | while (cursor < endBuf) { |
| 2259 | if (*cursor == '@') { |
| 2260 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 2261 | // Skip whitespace. |
| 2262 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 2263 | /*scan*/; |
| 2264 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2265 | // FIXME: presence of @public, etc. inside comment results in |
| 2266 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2267 | if (!strncmp(cursor, "public", strlen("public")) || |
| 2268 | !strncmp(cursor, "private", strlen("private")) || |
Steve Naroff | c5e3277 | 2008-04-04 22:34:24 +0000 | [diff] [blame] | 2269 | !strncmp(cursor, "package", strlen("package")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2270 | !strncmp(cursor, "protected", strlen("protected"))) |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2271 | InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2272 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2273 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 2274 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 2275 | // for type checking. |
| 2276 | else if (*cursor == '<') { |
| 2277 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2278 | InsertText(atLoc, "/* ", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2279 | cursor = strchr(cursor, '>'); |
| 2280 | cursor++; |
| 2281 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2282 | InsertText(atLoc, " */", 3); |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 2283 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2284 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 2285 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2286 | // Don't forget to add a ';'!! |
Chris Lattner | f3dd57e | 2008-01-31 19:42:41 +0000 | [diff] [blame] | 2287 | InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 2288 | } else { // we don't have any instance variables - insert super struct. |
| 2289 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 2290 | Result += " {\n struct "; |
| 2291 | Result += RCDecl->getName(); |
Steve Naroff | 39bbd9f | 2008-03-12 21:09:20 +0000 | [diff] [blame] | 2292 | Result += "_IMPL "; |
| 2293 | Result += RCDecl->getName(); |
Steve Naroff | 819173c | 2008-03-12 21:22:52 +0000 | [diff] [blame] | 2294 | Result += "_IVARS;\n};\n"; |
Chris Lattner | aadaf78 | 2008-01-31 19:51:04 +0000 | [diff] [blame] | 2295 | ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2296 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2297 | // Mark this struct as having been generated. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2298 | if (!ObjCSynthesizedStructs.insert(CDecl)) |
| 2299 | assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2302 | // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2303 | /// class methods. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2304 | void RewriteTest::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin, |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2305 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 2306 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2307 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2308 | const char *ClassName, |
| 2309 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2310 | if (MethodBegin == MethodEnd) return; |
| 2311 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2312 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2313 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2314 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2315 | SEL _cmd; |
| 2316 | char *method_types; |
| 2317 | void *_imp; |
| 2318 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2319 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 2320 | Result += "\nstruct _objc_method {\n"; |
| 2321 | Result += "\tSEL _cmd;\n"; |
| 2322 | Result += "\tchar *method_types;\n"; |
| 2323 | Result += "\tvoid *_imp;\n"; |
| 2324 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2325 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2326 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 2327 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2328 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2329 | // Build _objc_method_list for class's methods if needed |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2330 | |
| 2331 | /* struct { |
| 2332 | struct _objc_method_list *next_method; |
| 2333 | int method_count; |
| 2334 | struct _objc_method method_list[]; |
| 2335 | } |
| 2336 | */ |
| 2337 | Result += "\nstatic struct {\n"; |
| 2338 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 2339 | Result += "\tint method_count;\n"; |
| 2340 | Result += "\tstruct _objc_method method_list["; |
| 2341 | Result += utostr(MethodEnd-MethodBegin); |
| 2342 | Result += "];\n} _OBJC_"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2343 | Result += prefix; |
| 2344 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 2345 | Result += "_METHODS_"; |
| 2346 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2347 | Result += " __attribute__ ((used, section (\"__OBJC, __"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2348 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 2349 | Result += "_meth\")))= "; |
| 2350 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2351 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2352 | Result += "\t,{{(SEL)\""; |
| 2353 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
| 2354 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2355 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2356 | Result += "\", \""; |
| 2357 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2358 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2359 | Result += MethodInternalNames[*MethodBegin]; |
| 2360 | Result += "}\n"; |
| 2361 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 2362 | Result += "\t ,{(SEL)\""; |
| 2363 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2364 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2365 | Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2366 | Result += "\", \""; |
| 2367 | Result += MethodTypeString; |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2368 | Result += "\", (void *)"; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2369 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 2370 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2371 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2372 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2375 | /// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data. |
| 2376 | void RewriteTest::RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2377 | int NumProtocols, |
| 2378 | const char *prefix, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2379 | const char *ClassName, |
| 2380 | std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2381 | static bool objc_protocol_methods = false; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2382 | if (NumProtocols > 0) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2383 | for (int i = 0; i < NumProtocols; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2384 | ObjCProtocolDecl *PDecl = Protocols[i]; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2385 | // Output struct protocol_methods holder of method selector and type. |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2386 | if (!objc_protocol_methods && !PDecl->isForwardDecl()) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2387 | /* struct protocol_methods { |
| 2388 | SEL _cmd; |
| 2389 | char *method_types; |
| 2390 | } |
| 2391 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2392 | Result += "\nstruct protocol_methods {\n"; |
| 2393 | Result += "\tSEL _cmd;\n"; |
| 2394 | Result += "\tchar *method_types;\n"; |
| 2395 | Result += "};\n"; |
| 2396 | |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2397 | objc_protocol_methods = true; |
| 2398 | } |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2399 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
| 2400 | unsigned NumMethods = PDecl->getNumInstanceMethods(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2401 | /* struct _objc_protocol_method_list { |
| 2402 | int protocol_method_count; |
| 2403 | struct protocol_methods protocols[]; |
| 2404 | } |
| 2405 | */ |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2406 | Result += "\nstatic struct {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2407 | Result += "\tint protocol_method_count;\n"; |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2408 | Result += "\tstruct protocol_methods protocols["; |
| 2409 | Result += utostr(NumMethods); |
| 2410 | Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2411 | Result += PDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2412 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2413 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 2414 | |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2415 | // Output instance methods declared in this protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2416 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2417 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 2418 | if (I == PDecl->instmeth_begin()) |
| 2419 | Result += "\t ,{{(SEL)\""; |
| 2420 | else |
| 2421 | Result += "\t ,{(SEL)\""; |
| 2422 | Result += (*I)->getSelector().getName().c_str(); |
| 2423 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2424 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 2425 | Result += "\", \""; |
| 2426 | Result += MethodTypeString; |
| 2427 | Result += "\"}\n"; |
| 2428 | } |
| 2429 | Result += "\t }\n};\n"; |
| 2430 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2431 | |
| 2432 | // Output class methods declared in this protocol. |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2433 | int NumMethods = PDecl->getNumClassMethods(); |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2434 | if (NumMethods > 0) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2435 | /* struct _objc_protocol_method_list { |
| 2436 | int protocol_method_count; |
| 2437 | struct protocol_methods protocols[]; |
| 2438 | } |
| 2439 | */ |
| 2440 | Result += "\nstatic struct {\n"; |
| 2441 | Result += "\tint protocol_method_count;\n"; |
| 2442 | Result += "\tstruct protocol_methods protocols["; |
| 2443 | Result += utostr(NumMethods); |
| 2444 | Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2445 | Result += PDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2446 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2447 | "{\n\t"; |
| 2448 | Result += utostr(NumMethods); |
| 2449 | Result += "\n"; |
| 2450 | |
Fariborz Jahanian | c6e2c2a | 2007-12-17 18:07:01 +0000 | [diff] [blame] | 2451 | // Output instance methods declared in this protocol. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2452 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Fariborz Jahanian | c6e2c2a | 2007-12-17 18:07:01 +0000 | [diff] [blame] | 2453 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 2454 | if (I == PDecl->classmeth_begin()) |
| 2455 | Result += "\t ,{{(SEL)\""; |
| 2456 | else |
| 2457 | Result += "\t ,{(SEL)\""; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2458 | Result += (*I)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2459 | std::string MethodTypeString; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2460 | Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2461 | Result += "\", \""; |
| 2462 | Result += MethodTypeString; |
| 2463 | Result += "\"}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2464 | } |
| 2465 | Result += "\t }\n};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2466 | } |
| 2467 | // Output: |
| 2468 | /* struct _objc_protocol { |
| 2469 | // Objective-C 1.0 extensions |
| 2470 | struct _objc_protocol_extension *isa; |
| 2471 | char *protocol_name; |
| 2472 | struct _objc_protocol **protocol_list; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2473 | struct _objc_protocol_method_list *instance_methods; |
| 2474 | struct _objc_protocol_method_list *class_methods; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2475 | }; |
| 2476 | */ |
| 2477 | static bool objc_protocol = false; |
| 2478 | if (!objc_protocol) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2479 | Result += "\nstruct _objc_protocol {\n"; |
| 2480 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 2481 | Result += "\tchar *protocol_name;\n"; |
| 2482 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 2483 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 2484 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 2485 | Result += "};\n"; |
| 2486 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2487 | objc_protocol = true; |
| 2488 | } |
| 2489 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2490 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 2491 | Result += PDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2492 | Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2493 | "{\n\t0, \""; |
| 2494 | Result += PDecl->getName(); |
| 2495 | Result += "\", 0, "; |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 2496 | if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2497 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2498 | Result += PDecl->getName(); |
| 2499 | Result += ", "; |
| 2500 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2501 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2502 | Result += "0, "; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 2503 | if (PDecl->getNumClassMethods() > 0) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2504 | Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2505 | Result += PDecl->getName(); |
| 2506 | Result += "\n"; |
| 2507 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2508 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2509 | Result += "0\n"; |
| 2510 | Result += "};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2511 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2512 | // Output the top lovel protocol meta-data for the class. |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2513 | /* struct _objc_protocol_list { |
| 2514 | struct _objc_protocol_list *next; |
| 2515 | int protocol_count; |
| 2516 | struct _objc_protocol *class_protocols[]; |
| 2517 | } |
| 2518 | */ |
| 2519 | Result += "\nstatic struct {\n"; |
| 2520 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 2521 | Result += "\tint protocol_count;\n"; |
| 2522 | Result += "\tstruct _objc_protocol *class_protocols["; |
| 2523 | Result += utostr(NumProtocols); |
| 2524 | Result += "];\n} _OBJC_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2525 | Result += prefix; |
| 2526 | Result += "_PROTOCOLS_"; |
| 2527 | Result += ClassName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2528 | Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2529 | "{\n\t0, "; |
| 2530 | Result += utostr(NumProtocols); |
| 2531 | Result += "\n"; |
| 2532 | |
| 2533 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 2534 | Result += Protocols[0]->getName(); |
| 2535 | Result += " \n"; |
| 2536 | |
| 2537 | for (int i = 1; i < NumProtocols; i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2538 | ObjCProtocolDecl *PDecl = Protocols[i]; |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2539 | Result += "\t ,(struct _objc_protocol_list*)&_OBJC_PROTOCOL_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2540 | Result += PDecl->getName(); |
| 2541 | Result += "\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2542 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2543 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2547 | /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2548 | /// implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2549 | void RewriteTest::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2550 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2551 | ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2552 | // Find category declaration for this implementation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2553 | ObjCCategoryDecl *CDecl; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2554 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 2555 | CDecl = CDecl->getNextClassCategory()) |
| 2556 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 2557 | break; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2558 | |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2559 | std::string FullCategoryName = ClassDecl->getName(); |
| 2560 | FullCategoryName += '_'; |
| 2561 | FullCategoryName += IDecl->getName(); |
| 2562 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2563 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2564 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2565 | true, "CATEGORY_", FullCategoryName.c_str(), |
| 2566 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2567 | |
| 2568 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2569 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2570 | false, "CATEGORY_", FullCategoryName.c_str(), |
| 2571 | Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2572 | |
| 2573 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2574 | // Null CDecl is case of a category implementation with no category interface |
| 2575 | if (CDecl) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2576 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2577 | CDecl->getNumReferencedProtocols(), |
| 2578 | "CATEGORY", |
Chris Lattner | eb44eee | 2007-12-23 01:40:15 +0000 | [diff] [blame] | 2579 | FullCategoryName.c_str(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2580 | |
| 2581 | /* struct _objc_category { |
| 2582 | char *category_name; |
| 2583 | char *class_name; |
| 2584 | struct _objc_method_list *instance_methods; |
| 2585 | struct _objc_method_list *class_methods; |
| 2586 | struct _objc_protocol_list *protocols; |
| 2587 | // Objective-C 1.0 extensions |
| 2588 | uint32_t size; // sizeof (struct _objc_category) |
| 2589 | struct _objc_property_list *instance_properties; // category's own |
| 2590 | // @property decl. |
| 2591 | }; |
| 2592 | */ |
| 2593 | |
| 2594 | static bool objc_category = false; |
| 2595 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2596 | Result += "\nstruct _objc_category {\n"; |
| 2597 | Result += "\tchar *category_name;\n"; |
| 2598 | Result += "\tchar *class_name;\n"; |
| 2599 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 2600 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 2601 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2602 | Result += "\tunsigned int size;\n"; |
| 2603 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 2604 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2605 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2606 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2607 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 2608 | Result += FullCategoryName; |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2609 | Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2610 | Result += IDecl->getName(); |
| 2611 | Result += "\"\n\t, \""; |
| 2612 | Result += ClassDecl->getName(); |
| 2613 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2614 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2615 | if (IDecl->getNumInstanceMethods() > 0) { |
| 2616 | Result += "\t, (struct _objc_method_list *)" |
| 2617 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 2618 | Result += FullCategoryName; |
| 2619 | Result += "\n"; |
| 2620 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2621 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2622 | Result += "\t, 0\n"; |
| 2623 | if (IDecl->getNumClassMethods() > 0) { |
| 2624 | Result += "\t, (struct _objc_method_list *)" |
| 2625 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 2626 | Result += FullCategoryName; |
| 2627 | Result += "\n"; |
| 2628 | } |
| 2629 | else |
| 2630 | Result += "\t, 0\n"; |
| 2631 | |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2632 | if (CDecl && CDecl->getNumReferencedProtocols() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2633 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 2634 | Result += FullCategoryName; |
| 2635 | Result += "\n"; |
| 2636 | } |
| 2637 | else |
| 2638 | Result += "\t, 0\n"; |
| 2639 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2642 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 2643 | /// ivar offset. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2644 | void RewriteTest::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl, |
| 2645 | ObjCIvarDecl *ivar, |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2646 | std::string &Result) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2647 | Result += "offsetof(struct "; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2648 | Result += IDecl->getName(); |
Steve Naroff | 61ed9ca | 2008-03-10 23:16:54 +0000 | [diff] [blame] | 2649 | if (LangOpts.Microsoft) |
| 2650 | Result += "_IMPL"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2651 | Result += ", "; |
| 2652 | Result += ivar->getName(); |
| 2653 | Result += ")"; |
| 2654 | } |
| 2655 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2656 | //===----------------------------------------------------------------------===// |
| 2657 | // Meta Data Emission |
| 2658 | //===----------------------------------------------------------------------===// |
| 2659 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2660 | void RewriteTest::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2661 | std::string &Result) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2662 | ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2663 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 2664 | // Explictly declared @interface's are already synthesized. |
| 2665 | if (CDecl->ImplicitInterfaceDecl()) { |
| 2666 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 2667 | // produce correct synthesis as yet. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2668 | SynthesizeObjCInternalStruct(CDecl, Result); |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 2669 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2671 | // Build _objc_ivar_list metadata for classes ivars if needed |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2672 | unsigned NumIvars = !IDecl->ivar_empty() |
| 2673 | ? IDecl->ivar_size() |
| 2674 | : (CDecl ? CDecl->ivar_size() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2675 | if (NumIvars > 0) { |
| 2676 | static bool objc_ivar = false; |
| 2677 | if (!objc_ivar) { |
| 2678 | /* struct _objc_ivar { |
| 2679 | char *ivar_name; |
| 2680 | char *ivar_type; |
| 2681 | int ivar_offset; |
| 2682 | }; |
| 2683 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2684 | Result += "\nstruct _objc_ivar {\n"; |
| 2685 | Result += "\tchar *ivar_name;\n"; |
| 2686 | Result += "\tchar *ivar_type;\n"; |
| 2687 | Result += "\tint ivar_offset;\n"; |
| 2688 | Result += "};\n"; |
| 2689 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2690 | objc_ivar = true; |
| 2691 | } |
| 2692 | |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2693 | /* struct { |
| 2694 | int ivar_count; |
| 2695 | struct _objc_ivar ivar_list[nIvars]; |
| 2696 | }; |
| 2697 | */ |
| 2698 | Result += "\nstatic struct {\n"; |
| 2699 | Result += "\tint ivar_count;\n"; |
| 2700 | Result += "\tstruct _objc_ivar ivar_list["; |
| 2701 | Result += utostr(NumIvars); |
| 2702 | Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2703 | Result += IDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2704 | Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2705 | "{\n\t"; |
| 2706 | Result += utostr(NumIvars); |
| 2707 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2708 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2709 | ObjCInterfaceDecl::ivar_iterator IVI, IVE; |
Chris Lattner | f3a7af9 | 2008-03-16 21:08:55 +0000 | [diff] [blame] | 2710 | if (!IDecl->ivar_empty()) { |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2711 | IVI = IDecl->ivar_begin(); |
| 2712 | IVE = IDecl->ivar_end(); |
| 2713 | } else { |
| 2714 | IVI = CDecl->ivar_begin(); |
| 2715 | IVE = CDecl->ivar_end(); |
| 2716 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2717 | Result += "\t,{{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2718 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2719 | Result += "\", \""; |
| 2720 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2721 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding, |
| 2722 | EncodingRecordTypes); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2723 | Result += StrEncoding; |
| 2724 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2725 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2726 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2727 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2728 | Result += "\t ,{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2729 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2730 | Result += "\", \""; |
| 2731 | std::string StrEncoding; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2732 | Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding, |
| 2733 | EncodingRecordTypes); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2734 | Result += StrEncoding; |
| 2735 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2736 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2737 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | // Build _objc_method_list for class's instance methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2744 | RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2745 | true, "", IDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2746 | |
| 2747 | // Build _objc_method_list for class's class methods if needed |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2748 | RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2749 | false, "", IDecl->getName(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2750 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2751 | // Protocols referenced in class declaration? |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2752 | RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(), |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2753 | CDecl->getNumIntfRefProtocols(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2754 | "CLASS", CDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2755 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2756 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2757 | // Declaration of class/meta-class metadata |
| 2758 | /* struct _objc_class { |
| 2759 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2760 | const char *super_class_name; |
| 2761 | char *name; |
| 2762 | long version; |
| 2763 | long info; |
| 2764 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2765 | struct _objc_ivar_list *ivars; |
| 2766 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2767 | struct objc_cache *cache; |
| 2768 | struct objc_protocol_list *protocols; |
| 2769 | const char *ivar_layout; |
| 2770 | struct _objc_class_ext *ext; |
| 2771 | }; |
| 2772 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2773 | static bool objc_class = false; |
| 2774 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2775 | Result += "\nstruct _objc_class {\n"; |
| 2776 | Result += "\tstruct _objc_class *isa;\n"; |
| 2777 | Result += "\tconst char *super_class_name;\n"; |
| 2778 | Result += "\tchar *name;\n"; |
| 2779 | Result += "\tlong version;\n"; |
| 2780 | Result += "\tlong info;\n"; |
| 2781 | Result += "\tlong instance_size;\n"; |
| 2782 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 2783 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 2784 | Result += "\tstruct objc_cache *cache;\n"; |
| 2785 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2786 | Result += "\tconst char *ivar_layout;\n"; |
| 2787 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 2788 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2789 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | // Meta-class metadata generation. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2793 | ObjCInterfaceDecl *RootClass = 0; |
| 2794 | ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2795 | while (SuperClass) { |
| 2796 | RootClass = SuperClass; |
| 2797 | SuperClass = SuperClass->getSuperClass(); |
| 2798 | } |
| 2799 | SuperClass = CDecl->getSuperClass(); |
| 2800 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2801 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 2802 | Result += CDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2803 | Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2804 | "{\n\t(struct _objc_class *)\""; |
| 2805 | Result += (RootClass ? RootClass->getName() : CDecl->getName()); |
| 2806 | Result += "\""; |
| 2807 | |
| 2808 | if (SuperClass) { |
| 2809 | Result += ", \""; |
| 2810 | Result += SuperClass->getName(); |
| 2811 | Result += "\", \""; |
| 2812 | Result += CDecl->getName(); |
| 2813 | Result += "\""; |
| 2814 | } |
| 2815 | else { |
| 2816 | Result += ", 0, \""; |
| 2817 | Result += CDecl->getName(); |
| 2818 | Result += "\""; |
| 2819 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2820 | // 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] | 2821 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2822 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2823 | if (IDecl->getNumClassMethods() > 0) { |
Steve Naroff | 23f4127 | 2008-03-11 18:14:26 +0000 | [diff] [blame] | 2824 | Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2825 | Result += IDecl->getName(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2826 | Result += "\n"; |
| 2827 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2828 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2829 | Result += ", 0\n"; |
| 2830 | if (CDecl->getNumIntfRefProtocols() > 0) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2831 | Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2832 | Result += CDecl->getName(); |
| 2833 | Result += ",0,0\n"; |
| 2834 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 2835 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2836 | Result += "\t,0,0,0,0\n"; |
| 2837 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2838 | |
| 2839 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2840 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 2841 | Result += CDecl->getName(); |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2842 | Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2843 | "{\n\t&_OBJC_METACLASS_"; |
| 2844 | Result += CDecl->getName(); |
| 2845 | if (SuperClass) { |
| 2846 | Result += ", \""; |
| 2847 | Result += SuperClass->getName(); |
| 2848 | Result += "\", \""; |
| 2849 | Result += CDecl->getName(); |
| 2850 | Result += "\""; |
| 2851 | } |
| 2852 | else { |
| 2853 | Result += ", 0, \""; |
| 2854 | Result += CDecl->getName(); |
| 2855 | Result += "\""; |
| 2856 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2857 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2858 | Result += ", 0,1"; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2859 | if (!ObjCSynthesizedStructs.count(CDecl)) |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2860 | Result += ",0"; |
| 2861 | else { |
| 2862 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2863 | Result += ",sizeof(struct "; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2864 | Result += CDecl->getName(); |
Steve Naroff | ba9ac4e | 2008-03-10 23:33:22 +0000 | [diff] [blame] | 2865 | if (LangOpts.Microsoft) |
| 2866 | Result += "_IMPL"; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2867 | Result += ")"; |
| 2868 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2869 | if (NumIvars > 0) { |
Steve Naroff | c0a123c | 2008-03-11 17:37:02 +0000 | [diff] [blame] | 2870 | Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2871 | Result += CDecl->getName(); |
| 2872 | Result += "\n\t"; |
| 2873 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2874 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2875 | Result += ",0"; |
| 2876 | if (IDecl->getNumInstanceMethods() > 0) { |
Steve Naroff | 946a693 | 2008-03-11 00:12:29 +0000 | [diff] [blame] | 2877 | Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2878 | Result += CDecl->getName(); |
| 2879 | Result += ", 0\n\t"; |
| 2880 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2881 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2882 | Result += ",0,0"; |
| 2883 | if (CDecl->getNumIntfRefProtocols() > 0) { |
Steve Naroff | 8eb4a5e | 2008-03-12 01:06:30 +0000 | [diff] [blame] | 2884 | Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2885 | Result += CDecl->getName(); |
| 2886 | Result += ", 0,0\n"; |
| 2887 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2888 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2889 | Result += ",0,0,0\n"; |
| 2890 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2891 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2892 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2893 | /// RewriteImplementations - This routine rewrites all method implementations |
| 2894 | /// and emits meta-data. |
| 2895 | |
| 2896 | void RewriteTest::RewriteImplementations(std::string &Result) { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2897 | int ClsDefCount = ClassImplementation.size(); |
| 2898 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2899 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2900 | if (ClsDefCount == 0 && CatDefCount == 0) |
| 2901 | return; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2902 | // Rewrite implemented methods |
| 2903 | for (int i = 0; i < ClsDefCount; i++) |
| 2904 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2905 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 2906 | for (int i = 0; i < CatDefCount; i++) |
| 2907 | RewriteImplementationDecl(CategoryImplementation[i]); |
| 2908 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2909 | // This is needed for use of offsetof |
Steve Naroff | b10f273 | 2008-04-04 22:58:22 +0000 | [diff] [blame] | 2910 | Result += "#define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2911 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2912 | for (int i = 0; i < ClsDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2913 | RewriteObjCClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2914 | |
| 2915 | // For each implemented category, write out all its meta data. |
| 2916 | for (int i = 0; i < CatDefCount; i++) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2917 | RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2918 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2919 | // Write objc_symtab metadata |
| 2920 | /* |
| 2921 | struct _objc_symtab |
| 2922 | { |
| 2923 | long sel_ref_cnt; |
| 2924 | SEL *refs; |
| 2925 | short cls_def_cnt; |
| 2926 | short cat_def_cnt; |
| 2927 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 2928 | }; |
| 2929 | */ |
| 2930 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2931 | Result += "\nstruct _objc_symtab {\n"; |
| 2932 | Result += "\tlong sel_ref_cnt;\n"; |
| 2933 | Result += "\tSEL *refs;\n"; |
| 2934 | Result += "\tshort cls_def_cnt;\n"; |
| 2935 | Result += "\tshort cat_def_cnt;\n"; |
| 2936 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 2937 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2938 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2939 | Result += "static struct _objc_symtab " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2940 | "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2941 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 2942 | + ", " + utostr(CatDefCount) + "\n"; |
| 2943 | for (int i = 0; i < ClsDefCount; i++) { |
| 2944 | Result += "\t,&_OBJC_CLASS_"; |
| 2945 | Result += ClassImplementation[i]->getName(); |
| 2946 | Result += "\n"; |
| 2947 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2948 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2949 | for (int i = 0; i < CatDefCount; i++) { |
| 2950 | Result += "\t,&_OBJC_CATEGORY_"; |
| 2951 | Result += CategoryImplementation[i]->getClassInterface()->getName(); |
| 2952 | Result += "_"; |
| 2953 | Result += CategoryImplementation[i]->getName(); |
| 2954 | Result += "\n"; |
| 2955 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2956 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2957 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2958 | |
| 2959 | // Write objc_module metadata |
| 2960 | |
| 2961 | /* |
| 2962 | struct _objc_module { |
| 2963 | long version; |
| 2964 | long size; |
| 2965 | const char *name; |
| 2966 | struct _objc_symtab *symtab; |
| 2967 | } |
| 2968 | */ |
| 2969 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2970 | Result += "\nstruct _objc_module {\n"; |
| 2971 | Result += "\tlong version;\n"; |
| 2972 | Result += "\tlong size;\n"; |
| 2973 | Result += "\tconst char *name;\n"; |
| 2974 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 2975 | Result += "};\n\n"; |
| 2976 | Result += "static struct _objc_module " |
Steve Naroff | dbb6543 | 2008-03-12 17:18:30 +0000 | [diff] [blame] | 2977 | "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2978 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 2979 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2980 | Result += "};\n\n"; |
Steve Naroff | 4f943c2 | 2008-03-10 20:43:59 +0000 | [diff] [blame] | 2981 | |
| 2982 | if (LangOpts.Microsoft) { |
| 2983 | Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; |
| 2984 | Result += "#pragma data_seq(push, \".objc_module_info$B\")\n"; |
| 2985 | Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; |
| 2986 | Result += "&_OBJC_MODULES;\n"; |
| 2987 | Result += "#pragma data_seg(pop)\n\n"; |
| 2988 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2989 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2990 | |