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