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