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