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