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