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