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