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