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