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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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: |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 65 | void Initialize(ASTContext &context, unsigned mainFileID) { |
| 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. |
| 84 | MainFileID = mainFileID; |
| 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 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 126 | Rewrite.InsertText(SourceLocation::getFileLoc(mainFileID, 0), |
| 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 | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 493 | if (isa<ObjcQualifiedIdType>(OMD->getResultType())) |
| 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 | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 554 | if (isa<ObjcQualifiedIdType>(PDecl->getType())) |
| 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()); |
| 647 | Rewrite.ReplaceStmt(IV, Replacement); |
| 648 | delete IV; |
| 649 | return Replacement; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 650 | } else { |
| 651 | if (CurMethodDecl) { |
| 652 | if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) { |
| 653 | ObjcInterfaceType *intT = dyn_cast<ObjcInterfaceType>(pType->getPointeeType()); |
| 654 | if (CurMethodDecl->getClassInterface() == intT->getDecl()) { |
| 655 | IdentifierInfo *II = intT->getDecl()->getIdentifier(); |
| 656 | RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), |
| 657 | II, 0); |
| 658 | QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 659 | |
| 660 | CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation()); |
| 661 | // Don't forget the parens to enforce the proper binding. |
| 662 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), castExpr); |
| 663 | Rewrite.ReplaceStmt(IV->getBase(), PE); |
| 664 | delete IV->getBase(); |
| 665 | return PE; |
| 666 | } |
| 667 | } |
| 668 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 669 | return IV; |
Steve Naroff | c2a689b | 2007-11-15 11:33:00 +0000 | [diff] [blame] | 670 | } |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Chris Lattner | f04da13 | 2007-10-24 17:06:59 +0000 | [diff] [blame] | 673 | //===----------------------------------------------------------------------===// |
| 674 | // Function Body / Expression rewriting |
| 675 | //===----------------------------------------------------------------------===// |
| 676 | |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 677 | Stmt *RewriteTest::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 678 | // Otherwise, just rewrite all children. |
| 679 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); |
| 680 | CI != E; ++CI) |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 681 | if (*CI) { |
Steve Naroff | f3473a7 | 2007-11-09 15:20:18 +0000 | [diff] [blame] | 682 | Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 683 | if (newStmt) |
| 684 | *CI = newStmt; |
| 685 | } |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 686 | |
| 687 | // Handle specific things. |
| 688 | if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) |
| 689 | return RewriteAtEncode(AtEncode); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 690 | |
| 691 | if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) |
| 692 | return RewriteObjCIvarRefExpr(IvarRefExpr); |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 693 | |
| 694 | if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) |
| 695 | return RewriteAtSelector(AtSelector); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 696 | |
| 697 | if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) |
| 698 | return RewriteObjCStringLiteral(AtString); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 699 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 700 | if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { |
| 701 | // Before we rewrite it, put the original message expression in a comment. |
| 702 | SourceLocation startLoc = MessExpr->getLocStart(); |
| 703 | SourceLocation endLoc = MessExpr->getLocEnd(); |
| 704 | |
| 705 | const char *startBuf = SM->getCharacterData(startLoc); |
| 706 | const char *endBuf = SM->getCharacterData(endLoc); |
| 707 | |
| 708 | std::string messString; |
| 709 | messString += "// "; |
| 710 | messString.append(startBuf, endBuf-startBuf+1); |
| 711 | messString += "\n"; |
Steve Naroff | bef1185 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 712 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 713 | // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int). |
| 714 | // Rewrite.InsertText(startLoc, messString.c_str(), messString.size()); |
| 715 | // Tried this, but it didn't work either... |
Steve Naroff | 752d6ef | 2007-10-30 16:42:30 +0000 | [diff] [blame] | 716 | // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 717 | return RewriteMessageExpr(MessExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 718 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 719 | |
| 720 | if (ObjcAtTryStmt *StmtTry = dyn_cast<ObjcAtTryStmt>(S)) |
| 721 | return RewriteObjcTryStmt(StmtTry); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 722 | |
| 723 | if (ObjcAtThrowStmt *StmtThrow = dyn_cast<ObjcAtThrowStmt>(S)) |
| 724 | return RewriteObjcThrowStmt(StmtThrow); |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 725 | |
| 726 | if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) |
| 727 | return RewriteObjCProtocolExpr(ProtocolExp); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 728 | #if 0 |
| 729 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { |
| 730 | CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); |
| 731 | // Get the new text. |
| 732 | std::ostringstream Buf; |
| 733 | Replacement->printPretty(Buf); |
| 734 | const std::string &Str = Buf.str(); |
| 735 | |
| 736 | printf("CAST = %s\n", &Str[0]); |
| 737 | Rewrite.InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); |
| 738 | delete S; |
| 739 | return Replacement; |
| 740 | } |
| 741 | #endif |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 742 | // Return this stmt unmodified. |
| 743 | return S; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 744 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 745 | |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 746 | Stmt *RewriteTest::RewriteObjcTryStmt(ObjcAtTryStmt *S) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 747 | // Get the start location and compute the semi location. |
| 748 | SourceLocation startLoc = S->getLocStart(); |
| 749 | const char *startBuf = SM->getCharacterData(startLoc); |
| 750 | |
| 751 | assert((*startBuf == '@') && "bogus @try location"); |
| 752 | |
| 753 | std::string buf; |
| 754 | // declare a new scope with two variables, _stack and _rethrow. |
| 755 | buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; |
| 756 | buf += "int buf[18/*32-bit i386*/];\n"; |
| 757 | buf += "char *pointers[4];} _stack;\n"; |
| 758 | buf += "id volatile _rethrow = 0;\n"; |
| 759 | buf += "objc_exception_try_enter(&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 760 | buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 761 | |
| 762 | Rewrite.ReplaceText(startLoc, 4, buf.c_str(), buf.size()); |
| 763 | |
| 764 | startLoc = S->getTryBody()->getLocEnd(); |
| 765 | startBuf = SM->getCharacterData(startLoc); |
| 766 | |
| 767 | assert((*startBuf == '}') && "bogus @try block"); |
| 768 | |
| 769 | SourceLocation lastCurlyLoc = startLoc; |
| 770 | |
| 771 | startLoc = startLoc.getFileLocWithOffset(1); |
| 772 | buf = " /* @catch begin */ else {\n"; |
| 773 | buf += " id _caught = objc_exception_extract(&_stack);\n"; |
| 774 | buf += " objc_exception_try_enter (&_stack);\n"; |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 775 | buf += " if (_setjmp(_stack.buf))\n"; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 776 | buf += " _rethrow = objc_exception_extract(&_stack);\n"; |
| 777 | buf += " else { /* @catch continue */"; |
| 778 | |
Chris Lattner | 28d1fe8 | 2007-11-08 04:41:51 +0000 | [diff] [blame] | 779 | Rewrite.InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 780 | |
| 781 | bool sawIdTypedCatch = false; |
| 782 | Stmt *lastCatchBody = 0; |
| 783 | ObjcAtCatchStmt *catchList = S->getCatchStmts(); |
| 784 | while (catchList) { |
| 785 | Stmt *catchStmt = catchList->getCatchParamStmt(); |
| 786 | |
| 787 | if (catchList == S->getCatchStmts()) |
| 788 | buf = "if ("; // we are generating code for the first catch clause |
| 789 | else |
| 790 | buf = "else if ("; |
| 791 | startLoc = catchList->getLocStart(); |
| 792 | startBuf = SM->getCharacterData(startLoc); |
| 793 | |
| 794 | assert((*startBuf == '@') && "bogus @catch location"); |
| 795 | |
| 796 | const char *lParenLoc = strchr(startBuf, '('); |
| 797 | |
| 798 | if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) { |
| 799 | QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType(); |
| 800 | if (t == Context->getObjcIdType()) { |
| 801 | buf += "1) { "; |
| 802 | Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1, |
| 803 | buf.c_str(), buf.size()); |
| 804 | sawIdTypedCatch = true; |
| 805 | } else if (const PointerType *pType = t->getAsPointerType()) { |
| 806 | ObjcInterfaceType *cls; // Should be a pointer to a class. |
| 807 | |
| 808 | cls = dyn_cast<ObjcInterfaceType>(pType->getPointeeType().getTypePtr()); |
| 809 | if (cls) { |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 810 | buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 811 | buf += cls->getDecl()->getName(); |
Steve Naroff | 21867b1 | 2007-11-07 18:43:40 +0000 | [diff] [blame] | 812 | buf += "\"), (struct objc_object *)_caught)) { "; |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 813 | Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1, |
| 814 | buf.c_str(), buf.size()); |
| 815 | } |
| 816 | } |
| 817 | // Now rewrite the body... |
| 818 | lastCatchBody = catchList->getCatchBody(); |
| 819 | SourceLocation rParenLoc = catchList->getRParenLoc(); |
| 820 | SourceLocation bodyLoc = lastCatchBody->getLocStart(); |
| 821 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 822 | const char *rParenBuf = SM->getCharacterData(rParenLoc); |
| 823 | assert((*rParenBuf == ')') && "bogus @catch paren location"); |
| 824 | assert((*bodyBuf == '{') && "bogus @catch body location"); |
| 825 | |
| 826 | buf = " = _caught;"; |
| 827 | // Here we replace ") {" with "= _caught;" (which initializes and |
| 828 | // declares the @catch parameter). |
| 829 | Rewrite.ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, |
| 830 | buf.c_str(), buf.size()); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 831 | } else if (!isa<NullStmt>(catchStmt)) { |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 832 | assert(false && "@catch rewrite bug"); |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 833 | } |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 834 | catchList = catchList->getNextCatchStmt(); |
| 835 | } |
| 836 | // Complete the catch list... |
| 837 | if (lastCatchBody) { |
| 838 | SourceLocation bodyLoc = lastCatchBody->getLocEnd(); |
| 839 | const char *bodyBuf = SM->getCharacterData(bodyLoc); |
| 840 | assert((*bodyBuf == '}') && "bogus @catch body location"); |
| 841 | bodyLoc = bodyLoc.getFileLocWithOffset(1); |
| 842 | buf = " } } /* @catch end */\n"; |
| 843 | |
Chris Lattner | 28d1fe8 | 2007-11-08 04:41:51 +0000 | [diff] [blame] | 844 | Rewrite.InsertText(bodyLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 845 | |
| 846 | // Set lastCurlyLoc |
| 847 | lastCurlyLoc = lastCatchBody->getLocEnd(); |
| 848 | } |
| 849 | if (ObjcAtFinallyStmt *finalStmt = S->getFinallyStmt()) { |
| 850 | startLoc = finalStmt->getLocStart(); |
| 851 | startBuf = SM->getCharacterData(startLoc); |
| 852 | assert((*startBuf == '@') && "bogus @finally start"); |
| 853 | |
| 854 | buf = "/* @finally */"; |
| 855 | Rewrite.ReplaceText(startLoc, 8, buf.c_str(), buf.size()); |
| 856 | |
| 857 | Stmt *body = finalStmt->getFinallyBody(); |
| 858 | SourceLocation startLoc = body->getLocStart(); |
| 859 | SourceLocation endLoc = body->getLocEnd(); |
| 860 | const char *startBuf = SM->getCharacterData(startLoc); |
| 861 | const char *endBuf = SM->getCharacterData(endLoc); |
| 862 | assert((*startBuf == '{') && "bogus @finally body location"); |
| 863 | assert((*endBuf == '}') && "bogus @finally body location"); |
| 864 | |
| 865 | startLoc = startLoc.getFileLocWithOffset(1); |
| 866 | buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; |
Chris Lattner | 28d1fe8 | 2007-11-08 04:41:51 +0000 | [diff] [blame] | 867 | Rewrite.InsertText(startLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 868 | endLoc = endLoc.getFileLocWithOffset(-1); |
| 869 | buf = " if (_rethrow) objc_exception_throw(_rethrow);\n"; |
Chris Lattner | 28d1fe8 | 2007-11-08 04:41:51 +0000 | [diff] [blame] | 870 | Rewrite.InsertText(endLoc, buf.c_str(), buf.size()); |
Steve Naroff | 7573098 | 2007-11-07 04:08:17 +0000 | [diff] [blame] | 871 | |
| 872 | // Set lastCurlyLoc |
| 873 | lastCurlyLoc = body->getLocEnd(); |
| 874 | } |
| 875 | // Now emit the final closing curly brace... |
| 876 | lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); |
| 877 | buf = " } /* @try scope end */\n"; |
Chris Lattner | 28d1fe8 | 2007-11-08 04:41:51 +0000 | [diff] [blame] | 878 | Rewrite.InsertText(lastCurlyLoc, buf.c_str(), buf.size()); |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | Stmt *RewriteTest::RewriteObjcCatchStmt(ObjcAtCatchStmt *S) { |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | Stmt *RewriteTest::RewriteObjcFinallyStmt(ObjcAtFinallyStmt *S) { |
| 887 | return 0; |
| 888 | } |
| 889 | |
Steve Naroff | 2bd0392 | 2007-11-07 15:32:26 +0000 | [diff] [blame] | 890 | // This can't be done with Rewrite.ReplaceStmt(S, ThrowExpr), since |
| 891 | // the throw expression is typically a message expression that's already |
| 892 | // been rewritten! (which implies the SourceLocation's are invalid). |
| 893 | Stmt *RewriteTest::RewriteObjcThrowStmt(ObjcAtThrowStmt *S) { |
| 894 | // Get the start location and compute the semi location. |
| 895 | SourceLocation startLoc = S->getLocStart(); |
| 896 | const char *startBuf = SM->getCharacterData(startLoc); |
| 897 | |
| 898 | assert((*startBuf == '@') && "bogus @throw location"); |
| 899 | |
| 900 | std::string buf; |
| 901 | /* void objc_exception_throw(id) __attribute__((noreturn)); */ |
| 902 | buf = "objc_exception_throw("; |
| 903 | Rewrite.ReplaceText(startLoc, 6, buf.c_str(), buf.size()); |
| 904 | const char *semiBuf = strchr(startBuf, ';'); |
| 905 | assert((*semiBuf == ';') && "@throw: can't find ';'"); |
| 906 | SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); |
| 907 | buf = ");"; |
| 908 | Rewrite.ReplaceText(semiLoc, 1, buf.c_str(), buf.size()); |
| 909 | return 0; |
| 910 | } |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 911 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 912 | Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) { |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 913 | // Create a new string expression. |
| 914 | QualType StrType = Context->getPointerType(Context->CharTy); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 915 | std::string StrEncoding; |
| 916 | Context->getObjcEncodingForType(Exp->getEncodedType(), StrEncoding); |
| 917 | Expr *Replacement = new StringLiteral(StrEncoding.c_str(), |
| 918 | StrEncoding.length(), false, StrType, |
Chris Lattner | 01c5748 | 2007-10-17 22:35:30 +0000 | [diff] [blame] | 919 | SourceLocation(), SourceLocation()); |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 920 | if (Rewrite.ReplaceStmt(Exp, Replacement)) { |
| 921 | // replacement failed. |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 922 | unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, |
| 923 | "rewriter could not replace sub-expression due to macros"); |
| 924 | SourceRange Range = Exp->getSourceRange(); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 925 | Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 926 | delete Replacement; |
Chris Lattner | e365c50 | 2007-11-30 22:25:36 +0000 | [diff] [blame] | 927 | return Exp; |
| 928 | } |
| 929 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 930 | // Replace this subexpr in the parent. |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 931 | delete Exp; |
| 932 | return Replacement; |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Steve Naroff | b42f841 | 2007-11-05 14:50:49 +0000 | [diff] [blame] | 935 | Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) { |
| 936 | assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); |
| 937 | // Create a call to sel_registerName("selName"). |
| 938 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 939 | QualType argType = Context->getPointerType(Context->CharTy); |
| 940 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 941 | Exp->getSelector().getName().size(), |
| 942 | false, argType, SourceLocation(), |
| 943 | SourceLocation())); |
| 944 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 945 | &SelExprs[0], SelExprs.size()); |
| 946 | Rewrite.ReplaceStmt(Exp, SelExp); |
| 947 | delete Exp; |
| 948 | return SelExp; |
| 949 | } |
| 950 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 951 | CallExpr *RewriteTest::SynthesizeCallToFunctionDecl( |
| 952 | FunctionDecl *FD, Expr **args, unsigned nargs) { |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 953 | // 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] | 954 | QualType msgSendType = FD->getType(); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 955 | |
| 956 | // Create a reference to the objc_msgSend() declaration. |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 957 | DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation()); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 958 | |
| 959 | // 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] | 960 | QualType pToFunc = Context->getPointerType(msgSendType); |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 961 | ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE); |
| 962 | |
| 963 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 964 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 965 | return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation()); |
| 966 | } |
| 967 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 968 | static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, |
| 969 | const char *&startRef, const char *&endRef) { |
| 970 | while (startBuf < endBuf) { |
| 971 | if (*startBuf == '<') |
| 972 | startRef = startBuf; // mark the start. |
| 973 | if (*startBuf == '>') { |
Steve Naroff | 3217482 | 2007-11-09 12:50:28 +0000 | [diff] [blame] | 974 | if (startRef && *startRef == '<') { |
| 975 | endRef = startBuf; // mark the end. |
| 976 | return true; |
| 977 | } |
| 978 | return false; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 979 | } |
| 980 | startBuf++; |
| 981 | } |
| 982 | return false; |
| 983 | } |
| 984 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 985 | static void scanToNextArgument(const char *&argRef) { |
| 986 | int angle = 0; |
| 987 | while (*argRef != ')' && (*argRef != ',' || angle > 0)) { |
| 988 | if (*argRef == '<') |
| 989 | angle++; |
| 990 | else if (*argRef == '>') |
| 991 | angle--; |
| 992 | argRef++; |
| 993 | } |
| 994 | assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); |
| 995 | } |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 996 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 997 | bool RewriteTest::needToScanForQualifiers(QualType T) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 998 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 999 | if (T == Context->getObjcIdType()) |
| 1000 | return true; |
| 1001 | |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 1002 | if (isa<ObjcQualifiedIdType>(T)) |
| 1003 | return true; |
| 1004 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1005 | if (const PointerType *pType = T->getAsPointerType()) { |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1006 | Type *pointeeType = pType->getPointeeType().getTypePtr(); |
| 1007 | if (isa<ObjcQualifiedInterfaceType>(pointeeType)) |
| 1008 | return true; // we have "Class <Protocol> *". |
| 1009 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1010 | return false; |
| 1011 | } |
| 1012 | |
Fariborz Jahanian | e66894c | 2007-12-11 19:56:36 +0000 | [diff] [blame] | 1013 | void RewriteTest::RewriteObjcQualifiedInterfaceTypes(Decl *Dcl) { |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1014 | SourceLocation Loc; |
| 1015 | QualType Type; |
| 1016 | const FunctionTypeProto *proto = 0; |
| 1017 | if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { |
| 1018 | Loc = VD->getLocation(); |
| 1019 | Type = VD->getType(); |
| 1020 | } |
| 1021 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { |
| 1022 | Loc = FD->getLocation(); |
| 1023 | // Check for ObjC 'id' and class types that have been adorned with protocol |
| 1024 | // information (id<p>, C<p>*). The protocol references need to be rewritten! |
| 1025 | const FunctionType *funcType = FD->getType()->getAsFunctionType(); |
| 1026 | assert(funcType && "missing function type"); |
| 1027 | proto = dyn_cast<FunctionTypeProto>(funcType); |
| 1028 | if (!proto) |
| 1029 | return; |
| 1030 | Type = proto->getResultType(); |
| 1031 | } |
| 1032 | else |
| 1033 | return; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1034 | |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1035 | if (needToScanForQualifiers(Type)) { |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1036 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1037 | |
| 1038 | const char *endBuf = SM->getCharacterData(Loc); |
| 1039 | const char *startBuf = endBuf; |
Chris Lattner | 26de465 | 2007-12-02 01:13:47 +0000 | [diff] [blame] | 1040 | while (*startBuf != ';' && startBuf != MainFileStart) |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1041 | startBuf--; // scan backward (from the decl location) for return type. |
| 1042 | const char *startRef = 0, *endRef = 0; |
| 1043 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1044 | // Get the locations of the startRef, endRef. |
| 1045 | SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); |
| 1046 | SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); |
| 1047 | // Comment out the protocol references. |
Chris Lattner | 28d1fe8 | 2007-11-08 04:41:51 +0000 | [diff] [blame] | 1048 | Rewrite.InsertText(LessLoc, "/*", 2); |
| 1049 | Rewrite.InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1052 | if (!proto) |
| 1053 | return; // most likely, was a variable |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1054 | // Now check arguments. |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1055 | const char *startBuf = SM->getCharacterData(Loc); |
| 1056 | const char *startFuncBuf = startBuf; |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1057 | for (unsigned i = 0; i < proto->getNumArgs(); i++) { |
| 1058 | if (needToScanForQualifiers(proto->getArgType(i))) { |
| 1059 | // Since types are unique, we need to scan the buffer. |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1060 | |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1061 | const char *endBuf = startBuf; |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1062 | // scan forward (from the decl location) for argument types. |
| 1063 | scanToNextArgument(endBuf); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1064 | const char *startRef = 0, *endRef = 0; |
| 1065 | if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { |
| 1066 | // Get the locations of the startRef, endRef. |
Fariborz Jahanian | 291e04b | 2007-12-11 23:04:08 +0000 | [diff] [blame] | 1067 | SourceLocation LessLoc = |
| 1068 | Loc.getFileLocWithOffset(startRef-startFuncBuf); |
| 1069 | SourceLocation GreaterLoc = |
| 1070 | Loc.getFileLocWithOffset(endRef-startFuncBuf+1); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1071 | // Comment out the protocol references. |
Chris Lattner | 28d1fe8 | 2007-11-08 04:41:51 +0000 | [diff] [blame] | 1072 | Rewrite.InsertText(LessLoc, "/*", 2); |
| 1073 | Rewrite.InsertText(GreaterLoc, "*/", 2); |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1074 | } |
Fariborz Jahanian | 61477f7 | 2007-12-11 22:50:14 +0000 | [diff] [blame] | 1075 | startBuf = ++endBuf; |
| 1076 | } |
| 1077 | else { |
| 1078 | while (*startBuf != ')' && *startBuf != ',') |
| 1079 | startBuf++; // scan forward (from the decl location) for argument types. |
| 1080 | startBuf++; |
| 1081 | } |
Steve Naroff | d5255f5 | 2007-11-01 13:24:47 +0000 | [diff] [blame] | 1082 | } |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1085 | // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); |
| 1086 | void RewriteTest::SynthSelGetUidFunctionDecl() { |
| 1087 | IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); |
| 1088 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1089 | ArgTys.push_back(Context->getPointerType( |
| 1090 | Context->CharTy.getQualifiedType(QualType::Const))); |
| 1091 | QualType getFuncType = Context->getFunctionType(Context->getObjcSelType(), |
| 1092 | &ArgTys[0], ArgTys.size(), |
| 1093 | false /*isVariadic*/); |
| 1094 | SelGetUidFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1095 | SelGetUidIdent, getFuncType, |
| 1096 | FunctionDecl::Extern, false, 0); |
| 1097 | } |
| 1098 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1099 | // SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto); |
| 1100 | void RewriteTest::SynthGetProtocolFunctionDecl() { |
| 1101 | IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol"); |
| 1102 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1103 | ArgTys.push_back(Context->getPointerType( |
| 1104 | Context->CharTy.getQualifiedType(QualType::Const))); |
| 1105 | QualType getFuncType = Context->getFunctionType(Context->getObjcProtoType(), |
| 1106 | &ArgTys[0], ArgTys.size(), |
| 1107 | false /*isVariadic*/); |
| 1108 | GetProtocolFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1109 | SelGetProtoIdent, getFuncType, |
| 1110 | FunctionDecl::Extern, false, 0); |
| 1111 | } |
| 1112 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1113 | void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) { |
| 1114 | // declared in <objc/objc.h> |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1115 | if (strcmp(FD->getName(), "sel_registerName") == 0) { |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1116 | SelGetUidFunctionDecl = FD; |
Steve Naroff | 9165ad3 | 2007-10-31 04:38:33 +0000 | [diff] [blame] | 1117 | return; |
| 1118 | } |
Fariborz Jahanian | e66894c | 2007-12-11 19:56:36 +0000 | [diff] [blame] | 1119 | RewriteObjcQualifiedInterfaceTypes(FD); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); |
| 1123 | void RewriteTest::SynthMsgSendFunctionDecl() { |
| 1124 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); |
| 1125 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1126 | QualType argT = Context->getObjcIdType(); |
| 1127 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1128 | ArgTys.push_back(argT); |
| 1129 | argT = Context->getObjcSelType(); |
| 1130 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1131 | ArgTys.push_back(argT); |
| 1132 | QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(), |
| 1133 | &ArgTys[0], ArgTys.size(), |
| 1134 | true /*isVariadic*/); |
| 1135 | MsgSendFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1136 | msgSendIdent, msgSendType, |
| 1137 | FunctionDecl::Extern, false, 0); |
| 1138 | } |
| 1139 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1140 | // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); |
| 1141 | void RewriteTest::SynthMsgSendSuperFunctionDecl() { |
| 1142 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); |
| 1143 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1144 | RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), |
| 1145 | &Context->Idents.get("objc_super"), 0); |
| 1146 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1147 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1148 | ArgTys.push_back(argT); |
| 1149 | argT = Context->getObjcSelType(); |
| 1150 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1151 | ArgTys.push_back(argT); |
| 1152 | QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(), |
| 1153 | &ArgTys[0], ArgTys.size(), |
| 1154 | true /*isVariadic*/); |
| 1155 | MsgSendSuperFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1156 | msgSendIdent, msgSendType, |
| 1157 | FunctionDecl::Extern, false, 0); |
| 1158 | } |
| 1159 | |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1160 | // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); |
| 1161 | void RewriteTest::SynthMsgSendStretFunctionDecl() { |
| 1162 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); |
| 1163 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1164 | QualType argT = Context->getObjcIdType(); |
| 1165 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1166 | ArgTys.push_back(argT); |
| 1167 | argT = Context->getObjcSelType(); |
| 1168 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1169 | ArgTys.push_back(argT); |
| 1170 | QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(), |
| 1171 | &ArgTys[0], ArgTys.size(), |
| 1172 | true /*isVariadic*/); |
| 1173 | MsgSendStretFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1174 | msgSendIdent, msgSendType, |
| 1175 | FunctionDecl::Extern, false, 0); |
| 1176 | } |
| 1177 | |
| 1178 | // SynthMsgSendSuperStretFunctionDecl - |
| 1179 | // id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); |
| 1180 | void RewriteTest::SynthMsgSendSuperStretFunctionDecl() { |
| 1181 | IdentifierInfo *msgSendIdent = |
| 1182 | &Context->Idents.get("objc_msgSendSuper_stret"); |
| 1183 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1184 | RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), |
| 1185 | &Context->Idents.get("objc_super"), 0); |
| 1186 | QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); |
| 1187 | assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); |
| 1188 | ArgTys.push_back(argT); |
| 1189 | argT = Context->getObjcSelType(); |
| 1190 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1191 | ArgTys.push_back(argT); |
| 1192 | QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(), |
| 1193 | &ArgTys[0], ArgTys.size(), |
| 1194 | true /*isVariadic*/); |
| 1195 | MsgSendSuperStretFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1196 | msgSendIdent, msgSendType, |
| 1197 | FunctionDecl::Extern, false, 0); |
| 1198 | } |
| 1199 | |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1200 | // SynthMsgSendFpretFunctionDecl - id objc_msgSend_fpret(id self, SEL op, ...); |
| 1201 | void RewriteTest::SynthMsgSendFpretFunctionDecl() { |
| 1202 | IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); |
| 1203 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1204 | QualType argT = Context->getObjcIdType(); |
| 1205 | assert(!argT.isNull() && "Can't find 'id' type"); |
| 1206 | ArgTys.push_back(argT); |
| 1207 | argT = Context->getObjcSelType(); |
| 1208 | assert(!argT.isNull() && "Can't find 'SEL' type"); |
| 1209 | ArgTys.push_back(argT); |
| 1210 | QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(), |
| 1211 | &ArgTys[0], ArgTys.size(), |
| 1212 | true /*isVariadic*/); |
| 1213 | MsgSendFpretFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1214 | msgSendIdent, msgSendType, |
| 1215 | FunctionDecl::Extern, false, 0); |
| 1216 | } |
| 1217 | |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1218 | // SynthGetClassFunctionDecl - id objc_getClass(const char *name); |
| 1219 | void RewriteTest::SynthGetClassFunctionDecl() { |
| 1220 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); |
| 1221 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1222 | ArgTys.push_back(Context->getPointerType( |
| 1223 | Context->CharTy.getQualifiedType(QualType::Const))); |
| 1224 | QualType getClassType = Context->getFunctionType(Context->getObjcIdType(), |
| 1225 | &ArgTys[0], ArgTys.size(), |
| 1226 | false /*isVariadic*/); |
| 1227 | GetClassFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1228 | getClassIdent, getClassType, |
| 1229 | FunctionDecl::Extern, false, 0); |
| 1230 | } |
| 1231 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1232 | // SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); |
| 1233 | void RewriteTest::SynthGetMetaClassFunctionDecl() { |
| 1234 | IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); |
| 1235 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1236 | ArgTys.push_back(Context->getPointerType( |
| 1237 | Context->CharTy.getQualifiedType(QualType::Const))); |
| 1238 | QualType getClassType = Context->getFunctionType(Context->getObjcIdType(), |
| 1239 | &ArgTys[0], ArgTys.size(), |
| 1240 | false /*isVariadic*/); |
| 1241 | GetMetaClassFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1242 | getClassIdent, getClassType, |
| 1243 | FunctionDecl::Extern, false, 0); |
| 1244 | } |
| 1245 | |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1246 | // SynthCFStringFunctionDecl - id __builtin___CFStringMakeConstantString(const char *name); |
| 1247 | void RewriteTest::SynthCFStringFunctionDecl() { |
| 1248 | IdentifierInfo *getClassIdent = &Context->Idents.get("__builtin___CFStringMakeConstantString"); |
| 1249 | llvm::SmallVector<QualType, 16> ArgTys; |
| 1250 | ArgTys.push_back(Context->getPointerType( |
| 1251 | Context->CharTy.getQualifiedType(QualType::Const))); |
| 1252 | QualType getClassType = Context->getFunctionType(Context->getObjcIdType(), |
| 1253 | &ArgTys[0], ArgTys.size(), |
| 1254 | false /*isVariadic*/); |
| 1255 | CFStringFunctionDecl = new FunctionDecl(SourceLocation(), |
| 1256 | getClassIdent, getClassType, |
| 1257 | FunctionDecl::Extern, false, 0); |
| 1258 | } |
| 1259 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1260 | Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1261 | #if 1 |
| 1262 | // This rewrite is specific to GCC, which has builtin support for CFString. |
| 1263 | if (!CFStringFunctionDecl) |
| 1264 | SynthCFStringFunctionDecl(); |
| 1265 | // Create a call to __builtin___CFStringMakeConstantString("cstr"). |
| 1266 | llvm::SmallVector<Expr*, 8> StrExpr; |
| 1267 | StrExpr.push_back(Exp->getString()); |
| 1268 | CallExpr *call = SynthesizeCallToFunctionDecl(CFStringFunctionDecl, |
| 1269 | &StrExpr[0], StrExpr.size()); |
| 1270 | // cast to NSConstantString * |
| 1271 | CastExpr *cast = new CastExpr(Exp->getType(), call, SourceLocation()); |
| 1272 | Rewrite.ReplaceStmt(Exp, cast); |
| 1273 | delete Exp; |
| 1274 | return cast; |
| 1275 | #else |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1276 | assert(ConstantStringClassReference && "Can't find constant string reference"); |
| 1277 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1278 | |
| 1279 | // Synthesize "(Class)&_NSConstantStringClassReference" |
| 1280 | DeclRefExpr *ClsRef = new DeclRefExpr(ConstantStringClassReference, |
| 1281 | ConstantStringClassReference->getType(), |
| 1282 | SourceLocation()); |
| 1283 | QualType expType = Context->getPointerType(ClsRef->getType()); |
| 1284 | UnaryOperator *Unop = new UnaryOperator(ClsRef, UnaryOperator::AddrOf, |
| 1285 | expType, SourceLocation()); |
| 1286 | CastExpr *cast = new CastExpr(Context->getObjcClassType(), Unop, |
| 1287 | SourceLocation()); |
| 1288 | InitExprs.push_back(cast); // set the 'isa'. |
| 1289 | InitExprs.push_back(Exp->getString()); // set "char *bytes". |
| 1290 | unsigned IntSize = static_cast<unsigned>( |
| 1291 | Context->getTypeSize(Context->IntTy, Exp->getLocStart())); |
| 1292 | llvm::APInt IntVal(IntSize, Exp->getString()->getByteLength()); |
| 1293 | IntegerLiteral *len = new IntegerLiteral(IntVal, Context->IntTy, |
| 1294 | Exp->getLocStart()); |
| 1295 | InitExprs.push_back(len); // set "int numBytes". |
| 1296 | |
| 1297 | // struct NSConstantString |
| 1298 | QualType CFConstantStrType = Context->getCFConstantStringType(); |
| 1299 | // (struct NSConstantString) { <exprs from above> } |
| 1300 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 1301 | &InitExprs[0], InitExprs.size(), |
| 1302 | SourceLocation()); |
| 1303 | CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE); |
| 1304 | // struct NSConstantString * |
| 1305 | expType = Context->getPointerType(StrRep->getType()); |
| 1306 | Unop = new UnaryOperator(StrRep, UnaryOperator::AddrOf, expType, |
| 1307 | SourceLocation()); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 1308 | // cast to NSConstantString * |
| 1309 | cast = new CastExpr(Exp->getType(), Unop, SourceLocation()); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1310 | Rewrite.ReplaceStmt(Exp, cast); |
| 1311 | delete Exp; |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 1312 | return cast; |
Steve Naroff | 9698464 | 2007-11-08 14:30:50 +0000 | [diff] [blame] | 1313 | #endif |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1316 | ObjcInterfaceDecl *RewriteTest::isSuperReceiver(Expr *recExpr) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1317 | // check if we are sending a message to 'super' |
| 1318 | if (CurMethodDecl && CurMethodDecl->isInstance()) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1319 | if (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) { |
| 1320 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) { |
| 1321 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 1322 | if (!strcmp(PVD->getName(), "self")) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 1323 | // is this id<P1..> type? |
| 1324 | if (isa<ObjcQualifiedIdType>(CE->getType())) |
| 1325 | return 0; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1326 | if (const PointerType *PT = CE->getType()->getAsPointerType()) { |
| 1327 | if (ObjcInterfaceType *IT = |
| 1328 | dyn_cast<ObjcInterfaceType>(PT->getPointeeType())) { |
| 1329 | if (IT->getDecl() == |
| 1330 | CurMethodDecl->getClassInterface()->getSuperClass()) |
| 1331 | return IT->getDecl(); |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1337 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1338 | } |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; |
| 1343 | QualType RewriteTest::getSuperStructType() { |
| 1344 | if (!SuperStructDecl) { |
| 1345 | SuperStructDecl = new RecordDecl(Decl::Struct, SourceLocation(), |
| 1346 | &Context->Idents.get("objc_super"), 0); |
| 1347 | QualType FieldTypes[2]; |
| 1348 | |
| 1349 | // struct objc_object *receiver; |
| 1350 | FieldTypes[0] = Context->getObjcIdType(); |
| 1351 | // struct objc_class *super; |
| 1352 | FieldTypes[1] = Context->getObjcClassType(); |
| 1353 | // Create fields |
| 1354 | FieldDecl *FieldDecls[2]; |
| 1355 | |
| 1356 | for (unsigned i = 0; i < 2; ++i) |
| 1357 | FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]); |
| 1358 | |
| 1359 | SuperStructDecl->defineBody(FieldDecls, 4); |
| 1360 | } |
| 1361 | return Context->getTagDeclType(SuperStructDecl); |
| 1362 | } |
| 1363 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1364 | Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) { |
Fariborz Jahanian | a70711b | 2007-12-04 21:47:40 +0000 | [diff] [blame] | 1365 | if (!SelGetUidFunctionDecl) |
| 1366 | SynthSelGetUidFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1367 | if (!MsgSendFunctionDecl) |
| 1368 | SynthMsgSendFunctionDecl(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1369 | if (!MsgSendSuperFunctionDecl) |
| 1370 | SynthMsgSendSuperFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1371 | if (!MsgSendStretFunctionDecl) |
| 1372 | SynthMsgSendStretFunctionDecl(); |
| 1373 | if (!MsgSendSuperStretFunctionDecl) |
| 1374 | SynthMsgSendSuperStretFunctionDecl(); |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1375 | if (!MsgSendFpretFunctionDecl) |
| 1376 | SynthMsgSendFpretFunctionDecl(); |
Steve Naroff | 09b266e | 2007-10-30 23:14:51 +0000 | [diff] [blame] | 1377 | if (!GetClassFunctionDecl) |
| 1378 | SynthGetClassFunctionDecl(); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1379 | if (!GetMetaClassFunctionDecl) |
| 1380 | SynthGetMetaClassFunctionDecl(); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1381 | |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1382 | // default to objc_msgSend(). |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1383 | FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; |
| 1384 | // May need to use objc_msgSend_stret() as well. |
| 1385 | FunctionDecl *MsgSendStretFlavor = 0; |
| 1386 | if (ObjcMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 1387 | QualType resultType = mDecl->getResultType(); |
| 1388 | if (resultType.getCanonicalType()->isStructureType() |
| 1389 | || resultType.getCanonicalType()->isUnionType()) |
| 1390 | MsgSendStretFlavor = MsgSendStretFunctionDecl; |
Fariborz Jahanian | acb4977 | 2007-12-03 21:26:48 +0000 | [diff] [blame] | 1391 | else if (resultType.getCanonicalType()->isRealFloatingType()) |
| 1392 | MsgSendFlavor = MsgSendFpretFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1393 | } |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1394 | |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1395 | // Synthesize a call to objc_msgSend(). |
| 1396 | llvm::SmallVector<Expr*, 8> MsgExprs; |
| 1397 | IdentifierInfo *clsName = Exp->getClassName(); |
| 1398 | |
| 1399 | // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend(). |
| 1400 | if (clsName) { // class message. |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1401 | if (!strcmp(clsName->getName(), "super")) { |
| 1402 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
| 1403 | if (MsgSendStretFlavor) |
| 1404 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
| 1405 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 1406 | |
| 1407 | ObjcInterfaceDecl *SuperDecl = |
| 1408 | CurMethodDecl->getClassInterface()->getSuperClass(); |
| 1409 | |
| 1410 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1411 | |
| 1412 | // set the receiver to self, the first argument to all methods. |
| 1413 | InitExprs.push_back(new DeclRefExpr(CurMethodDecl->getSelfDecl(), |
| 1414 | Context->getObjcIdType(), |
| 1415 | SourceLocation())); |
| 1416 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1417 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1418 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 1419 | SuperDecl->getIdentifier()->getLength(), |
| 1420 | false, argType, SourceLocation(), |
| 1421 | SourceLocation())); |
| 1422 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, |
| 1423 | &ClsExprs[0], |
| 1424 | ClsExprs.size()); |
| 1425 | // To turn off a warning, type-cast to 'id' |
| 1426 | InitExprs.push_back( |
| 1427 | new CastExpr(Context->getObjcIdType(), |
| 1428 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
| 1429 | // struct objc_super |
| 1430 | QualType superType = getSuperStructType(); |
| 1431 | // (struct objc_super) { <exprs from above> } |
| 1432 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 1433 | &InitExprs[0], InitExprs.size(), |
| 1434 | SourceLocation()); |
| 1435 | CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(superType, ILE); |
| 1436 | // struct objc_super * |
| 1437 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 1438 | Context->getPointerType(SuperRep->getType()), |
| 1439 | SourceLocation()); |
| 1440 | MsgExprs.push_back(Unop); |
| 1441 | } else { |
| 1442 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1443 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1444 | ClsExprs.push_back(new StringLiteral(clsName->getName(), |
| 1445 | clsName->getLength(), |
| 1446 | false, argType, SourceLocation(), |
| 1447 | SourceLocation())); |
| 1448 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
| 1449 | &ClsExprs[0], |
| 1450 | ClsExprs.size()); |
| 1451 | MsgExprs.push_back(Cls); |
| 1452 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1453 | } else { // instance message. |
| 1454 | Expr *recExpr = Exp->getReceiver(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1455 | |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1456 | if (ObjcInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) { |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1457 | MsgSendFlavor = MsgSendSuperFunctionDecl; |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1458 | if (MsgSendStretFlavor) |
| 1459 | MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1460 | assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); |
| 1461 | |
| 1462 | llvm::SmallVector<Expr*, 4> InitExprs; |
| 1463 | |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1464 | InitExprs.push_back( |
| 1465 | new CastExpr(Context->getObjcIdType(), |
| 1466 | recExpr, SourceLocation())); // set the 'receiver'. |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1467 | |
| 1468 | llvm::SmallVector<Expr*, 8> ClsExprs; |
| 1469 | QualType argType = Context->getPointerType(Context->CharTy); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame] | 1470 | ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(), |
| 1471 | SuperDecl->getIdentifier()->getLength(), |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1472 | false, argType, SourceLocation(), |
| 1473 | SourceLocation())); |
| 1474 | CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1475 | &ClsExprs[0], |
| 1476 | ClsExprs.size()); |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 1477 | // To turn off a warning, type-cast to 'id' |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1478 | InitExprs.push_back( |
Fariborz Jahanian | 7127431 | 2007-12-05 17:29:46 +0000 | [diff] [blame] | 1479 | new CastExpr(Context->getObjcIdType(), |
Fariborz Jahanian | ceee3e8 | 2007-12-04 22:32:58 +0000 | [diff] [blame] | 1480 | Cls, SourceLocation())); // set 'super class', using objc_getClass(). |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1481 | // struct objc_super |
| 1482 | QualType superType = getSuperStructType(); |
| 1483 | // (struct objc_super) { <exprs from above> } |
| 1484 | InitListExpr *ILE = new InitListExpr(SourceLocation(), |
| 1485 | &InitExprs[0], InitExprs.size(), |
| 1486 | SourceLocation()); |
| 1487 | CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(superType, ILE); |
| 1488 | // struct objc_super * |
| 1489 | Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf, |
| 1490 | Context->getPointerType(SuperRep->getType()), |
| 1491 | SourceLocation()); |
| 1492 | MsgExprs.push_back(Unop); |
| 1493 | } else { |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 1494 | // Remove all type-casts because it may contain objc-style types; e.g. |
| 1495 | // Foo<Proto> *. |
| 1496 | while (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) |
| 1497 | recExpr = CE->getSubExpr(); |
Steve Naroff | 874e232 | 2007-11-15 10:28:18 +0000 | [diff] [blame] | 1498 | recExpr = new CastExpr(Context->getObjcIdType(), recExpr, SourceLocation()); |
| 1499 | MsgExprs.push_back(recExpr); |
| 1500 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1501 | } |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 1502 | // 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] | 1503 | llvm::SmallVector<Expr*, 8> SelExprs; |
| 1504 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1505 | SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(), |
| 1506 | Exp->getSelector().getName().size(), |
| 1507 | false, argType, SourceLocation(), |
| 1508 | SourceLocation())); |
| 1509 | CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, |
| 1510 | &SelExprs[0], SelExprs.size()); |
| 1511 | MsgExprs.push_back(SelExp); |
| 1512 | |
| 1513 | // Now push any user supplied arguments. |
| 1514 | for (unsigned i = 0; i < Exp->getNumArgs(); i++) { |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1515 | Expr *userExpr = Exp->getArg(i); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1516 | // Make all implicit casts explicit...ICE comes in handy:-) |
| 1517 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { |
| 1518 | // Reuse the ICE type, it is exactly what the doctor ordered. |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 1519 | userExpr = new CastExpr(isa<ObjcQualifiedIdType>(ICE->getType()) |
| 1520 | ? Context->getObjcIdType() |
| 1521 | : ICE->getType(), userExpr, SourceLocation()); |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 1522 | } |
Steve Naroff | 6568d4d | 2007-11-14 23:54:14 +0000 | [diff] [blame] | 1523 | MsgExprs.push_back(userExpr); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1524 | // We've transferred the ownership to MsgExprs. Null out the argument in |
| 1525 | // the original expression, since we will delete it below. |
| 1526 | Exp->setArg(i, 0); |
| 1527 | } |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1528 | // Generate the funky cast. |
| 1529 | CastExpr *cast; |
| 1530 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 1531 | QualType returnType; |
| 1532 | |
| 1533 | // Push 'id' and 'SEL', the 2 implicit arguments. |
Steve Naroff | c3a438c | 2007-11-15 10:43:57 +0000 | [diff] [blame] | 1534 | if (MsgSendFlavor == MsgSendSuperFunctionDecl) |
| 1535 | ArgTypes.push_back(Context->getPointerType(getSuperStructType())); |
| 1536 | else |
| 1537 | ArgTypes.push_back(Context->getObjcIdType()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1538 | ArgTypes.push_back(Context->getObjcSelType()); |
| 1539 | if (ObjcMethodDecl *mDecl = Exp->getMethodDecl()) { |
| 1540 | // Push any user argument types. |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 1541 | for (int i = 0; i < mDecl->getNumParams(); i++) { |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 1542 | QualType t = isa<ObjcQualifiedIdType>(mDecl->getParamDecl(i)->getType()) |
| 1543 | ? Context->getObjcIdType() |
| 1544 | : mDecl->getParamDecl(i)->getType(); |
Steve Naroff | 352336b | 2007-11-05 14:36:37 +0000 | [diff] [blame] | 1545 | ArgTypes.push_back(t); |
| 1546 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame^] | 1547 | returnType = isa<ObjcQualifiedIdType>(mDecl->getResultType()) |
| 1548 | ? Context->getObjcIdType() : mDecl->getResultType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1549 | } else { |
| 1550 | returnType = Context->getObjcIdType(); |
| 1551 | } |
| 1552 | // 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] | 1553 | QualType msgSendType = MsgSendFlavor->getType(); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1554 | |
| 1555 | // Create a reference to the objc_msgSend() declaration. |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1556 | DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType, |
| 1557 | SourceLocation()); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1558 | |
| 1559 | // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). |
| 1560 | // If we don't do this cast, we get the following bizarre warning/note: |
| 1561 | // xx.m:13: warning: function called through a non-compatible type |
| 1562 | // xx.m:13: note: if this code is reached, the program will abort |
| 1563 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE, |
| 1564 | SourceLocation()); |
Steve Naroff | 335eafa | 2007-11-15 12:35:21 +0000 | [diff] [blame] | 1565 | |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1566 | // Now do the "normal" pointer to function cast. |
| 1567 | QualType castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 1568 | &ArgTypes[0], ArgTypes.size(), |
| 1569 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1570 | castType = Context->getPointerType(castType); |
| 1571 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 1572 | |
| 1573 | // Don't forget the parens to enforce the proper binding. |
| 1574 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 1575 | |
| 1576 | const FunctionType *FT = msgSendType->getAsFunctionType(); |
| 1577 | CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 1578 | FT->getResultType(), SourceLocation()); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1579 | if (MsgSendStretFlavor) { |
| 1580 | // We have the method which returns a struct/union. Must also generate |
| 1581 | // call to objc_msgSend_stret and hang both varieties on a conditional |
| 1582 | // expression which dictate which one to envoke depending on size of |
| 1583 | // method's return type. |
| 1584 | |
| 1585 | // Create a reference to the objc_msgSend_stret() declaration. |
| 1586 | DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType, |
| 1587 | SourceLocation()); |
| 1588 | // Need to cast objc_msgSend_stret to "void *" (see above comment). |
| 1589 | cast = new CastExpr(Context->getPointerType(Context->VoidTy), STDRE, |
| 1590 | SourceLocation()); |
| 1591 | // Now do the "normal" pointer to function cast. |
| 1592 | castType = Context->getFunctionType(returnType, |
Fariborz Jahanian | d0ee6f9 | 2007-12-06 19:49:56 +0000 | [diff] [blame] | 1593 | &ArgTypes[0], ArgTypes.size(), |
| 1594 | Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false); |
Fariborz Jahanian | 80a6a5a | 2007-12-03 19:17:29 +0000 | [diff] [blame] | 1595 | castType = Context->getPointerType(castType); |
| 1596 | cast = new CastExpr(castType, cast, SourceLocation()); |
| 1597 | |
| 1598 | // Don't forget the parens to enforce the proper binding. |
| 1599 | PE = new ParenExpr(SourceLocation(), SourceLocation(), cast); |
| 1600 | |
| 1601 | FT = msgSendType->getAsFunctionType(); |
| 1602 | CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(), |
| 1603 | FT->getResultType(), SourceLocation()); |
| 1604 | |
| 1605 | // Build sizeof(returnType) |
| 1606 | SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true, |
| 1607 | returnType, Context->getSizeType(), |
| 1608 | SourceLocation(), SourceLocation()); |
| 1609 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 1610 | // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. |
| 1611 | // For X86 it is more complicated and some kind of target specific routine |
| 1612 | // is needed to decide what to do. |
| 1613 | unsigned IntSize = static_cast<unsigned>( |
| 1614 | Context->getTypeSize(Context->IntTy, SourceLocation())); |
| 1615 | |
| 1616 | IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8), |
| 1617 | Context->IntTy, |
| 1618 | SourceLocation()); |
| 1619 | BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit, |
| 1620 | BinaryOperator::LE, |
| 1621 | Context->IntTy, |
| 1622 | SourceLocation()); |
| 1623 | // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) |
| 1624 | ConditionalOperator *CondExpr = |
| 1625 | new ConditionalOperator(lessThanExpr, CE, STCE, returnType); |
| 1626 | ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr); |
| 1627 | // Now do the actual rewrite. |
| 1628 | Rewrite.ReplaceStmt(Exp, PE); |
| 1629 | delete Exp; |
| 1630 | return PE; |
| 1631 | } |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1632 | // Now do the actual rewrite. |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1633 | Rewrite.ReplaceStmt(Exp, CE); |
Steve Naroff | 934f276 | 2007-10-24 22:48:43 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | e64b777 | 2007-10-24 16:57:36 +0000 | [diff] [blame] | 1635 | delete Exp; |
Steve Naroff | ab972d3 | 2007-11-04 22:37:50 +0000 | [diff] [blame] | 1636 | return CE; |
Steve Naroff | ebf2b56 | 2007-10-23 23:50:29 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Fariborz Jahanian | 36ee2cb | 2007-12-07 18:47:10 +0000 | [diff] [blame] | 1639 | /// RewriteObjCProtocolExpr - Rewrite a protocol expression into |
| 1640 | /// call to objc_getProtocol("proto-name"). |
| 1641 | Stmt *RewriteTest::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { |
| 1642 | if (!GetProtocolFunctionDecl) |
| 1643 | SynthGetProtocolFunctionDecl(); |
| 1644 | // Create a call to objc_getProtocol("ProtocolName"). |
| 1645 | llvm::SmallVector<Expr*, 8> ProtoExprs; |
| 1646 | QualType argType = Context->getPointerType(Context->CharTy); |
| 1647 | ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(), |
| 1648 | strlen(Exp->getProtocol()->getName()), |
| 1649 | false, argType, SourceLocation(), |
| 1650 | SourceLocation())); |
| 1651 | CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl, |
| 1652 | &ProtoExprs[0], |
| 1653 | ProtoExprs.size()); |
| 1654 | Rewrite.ReplaceStmt(Exp, ProtoExp); |
| 1655 | delete Exp; |
| 1656 | return ProtoExp; |
| 1657 | |
| 1658 | } |
| 1659 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 1660 | /// SynthesizeObjcInternalStruct - Rewrite one internal struct corresponding to |
| 1661 | /// an objective-c class with ivars. |
| 1662 | void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl, |
| 1663 | std::string &Result) { |
| 1664 | assert(CDecl && "Class missing in SynthesizeObjcInternalStruct"); |
| 1665 | assert(CDecl->getName() && "Name missing in SynthesizeObjcInternalStruct"); |
Fariborz Jahanian | 212b768 | 2007-10-31 23:08:24 +0000 | [diff] [blame] | 1666 | // Do not synthesize more than once. |
| 1667 | if (ObjcSynthesizedStructs.count(CDecl)) |
| 1668 | return; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 1669 | ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass(); |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 1670 | int NumIvars = CDecl->getNumInstanceVariables(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1671 | SourceLocation LocStart = CDecl->getLocStart(); |
| 1672 | SourceLocation LocEnd = CDecl->getLocEnd(); |
| 1673 | |
| 1674 | const char *startBuf = SM->getCharacterData(LocStart); |
| 1675 | const char *endBuf = SM->getCharacterData(LocEnd); |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 1676 | // If no ivars and no root or if its root, directly or indirectly, |
| 1677 | // have no ivars (thus not synthesized) then no need to synthesize this class. |
| 1678 | if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl))) { |
Fariborz Jahanian | 2c7038b | 2007-11-26 19:52:57 +0000 | [diff] [blame] | 1679 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 1680 | Rewrite.ReplaceText(LocStart, endBuf-startBuf, |
| 1681 | Result.c_str(), Result.size()); |
| 1682 | return; |
| 1683 | } |
| 1684 | |
| 1685 | // FIXME: This has potential of causing problem. If |
| 1686 | // SynthesizeObjcInternalStruct is ever called recursively. |
| 1687 | Result += "\nstruct "; |
| 1688 | Result += CDecl->getName(); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1689 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 1690 | if (NumIvars > 0) { |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1691 | const char *cursor = strchr(startBuf, '{'); |
| 1692 | assert((cursor && endBuf) |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 1693 | && "SynthesizeObjcInternalStruct - malformed @interface"); |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1694 | |
| 1695 | // rewrite the original header *without* disturbing the '{' |
| 1696 | Rewrite.ReplaceText(LocStart, cursor-startBuf-1, |
| 1697 | Result.c_str(), Result.size()); |
| 1698 | if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) { |
| 1699 | Result = "\n struct "; |
| 1700 | Result += RCDecl->getName(); |
Steve Naroff | 75ae776 | 2007-12-07 22:15:58 +0000 | [diff] [blame] | 1701 | // Note: We don't name the field decl. This simplifies the "codegen" for |
| 1702 | // accessing a superclasses instance variables (and is similar to what gcc |
| 1703 | // does internally). The unnamed struct field feature is enabled with |
| 1704 | // -fms-extensions. If the struct definition were "inlined", we wouldn't |
| 1705 | // 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] | 1706 | Result += ";\n"; |
| 1707 | |
| 1708 | // insert the super class structure definition. |
| 1709 | SourceLocation OnePastCurly = LocStart.getFileLocWithOffset(cursor-startBuf+1); |
| 1710 | Rewrite.InsertText(OnePastCurly, Result.c_str(), Result.size()); |
| 1711 | } |
| 1712 | cursor++; // past '{' |
| 1713 | |
| 1714 | // Now comment out any visibility specifiers. |
| 1715 | while (cursor < endBuf) { |
| 1716 | if (*cursor == '@') { |
| 1717 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
Chris Lattner | df6a51b | 2007-11-14 22:57:51 +0000 | [diff] [blame] | 1718 | // Skip whitespace. |
| 1719 | for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) |
| 1720 | /*scan*/; |
| 1721 | |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 1722 | // FIXME: presence of @public, etc. inside comment results in |
| 1723 | // this transformation as well, which is still correct c-code. |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1724 | if (!strncmp(cursor, "public", strlen("public")) || |
| 1725 | !strncmp(cursor, "private", strlen("private")) || |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 1726 | !strncmp(cursor, "protected", strlen("protected"))) |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1727 | Rewrite.InsertText(atLoc, "// ", 3); |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 1728 | } |
Fariborz Jahanian | 9567392 | 2007-11-14 22:26:25 +0000 | [diff] [blame] | 1729 | // FIXME: If there are cases where '<' is used in ivar declaration part |
| 1730 | // of user code, then scan the ivar list and use needToScanForQualifiers |
| 1731 | // for type checking. |
| 1732 | else if (*cursor == '<') { |
| 1733 | SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 1734 | Rewrite.InsertText(atLoc, "/* ", 3); |
| 1735 | cursor = strchr(cursor, '>'); |
| 1736 | cursor++; |
| 1737 | atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); |
| 1738 | Rewrite.InsertText(atLoc, " */", 3); |
| 1739 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1740 | cursor++; |
Fariborz Jahanian | fdc08a0 | 2007-10-31 17:29:28 +0000 | [diff] [blame] | 1741 | } |
Steve Naroff | fea763e8 | 2007-11-14 19:25:57 +0000 | [diff] [blame] | 1742 | // Don't forget to add a ';'!! |
| 1743 | Rewrite.InsertText(LocEnd.getFileLocWithOffset(1), ";", 1); |
| 1744 | } else { // we don't have any instance variables - insert super struct. |
| 1745 | endBuf += Lexer::MeasureTokenLength(LocEnd, *SM); |
| 1746 | Result += " {\n struct "; |
| 1747 | Result += RCDecl->getName(); |
Steve Naroff | 75ae776 | 2007-12-07 22:15:58 +0000 | [diff] [blame] | 1748 | // Note: We don't name the field decl. This simplifies the "codegen" for |
| 1749 | // accessing a superclasses instance variables (and is similar to what gcc |
| 1750 | // does internally). The unnamed struct field feature is enabled with |
| 1751 | // -fms-extensions. If the struct definition were "inlined", we wouldn't |
| 1752 | // 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] | 1753 | Result += ";\n};\n"; |
| 1754 | Rewrite.ReplaceText(LocStart, endBuf-startBuf, |
| 1755 | Result.c_str(), Result.size()); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 1756 | } |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 1757 | // Mark this struct as having been generated. |
| 1758 | if (!ObjcSynthesizedStructs.insert(CDecl)) |
Fariborz Jahanian | aff56d0 | 2007-10-31 22:57:04 +0000 | [diff] [blame] | 1759 | assert(false && "struct already synthesize- SynthesizeObjcInternalStruct"); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1762 | // RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or |
| 1763 | /// class methods. |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1764 | void RewriteTest::RewriteObjcMethodsMetaData(instmeth_iterator MethodBegin, |
| 1765 | instmeth_iterator MethodEnd, |
Fariborz Jahanian | 8e991ba | 2007-10-25 00:14:44 +0000 | [diff] [blame] | 1766 | bool IsInstanceMethod, |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1767 | const char *prefix, |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 1768 | const char *ClassName, |
| 1769 | std::string &Result) { |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1770 | if (MethodBegin == MethodEnd) return; |
| 1771 | |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1772 | static bool objc_impl_method = false; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1773 | if (!objc_impl_method) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1774 | /* struct _objc_method { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1775 | SEL _cmd; |
| 1776 | char *method_types; |
| 1777 | void *_imp; |
| 1778 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1779 | */ |
Chris Lattner | 158ecb9 | 2007-10-25 17:07:24 +0000 | [diff] [blame] | 1780 | Result += "\nstruct _objc_method {\n"; |
| 1781 | Result += "\tSEL _cmd;\n"; |
| 1782 | Result += "\tchar *method_types;\n"; |
| 1783 | Result += "\tvoid *_imp;\n"; |
| 1784 | Result += "};\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* struct _objc_method_list { |
| 1787 | struct _objc_method_list *next_method; |
| 1788 | int method_count; |
| 1789 | struct _objc_method method_list[]; |
| 1790 | } |
| 1791 | */ |
| 1792 | Result += "\nstruct _objc_method_list {\n"; |
| 1793 | Result += "\tstruct _objc_method_list *next_method;\n"; |
| 1794 | Result += "\tint method_count;\n"; |
| 1795 | Result += "\tstruct _objc_method method_list[];\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1796 | objc_impl_method = true; |
Fariborz Jahanian | 776d6ff | 2007-10-19 00:36:46 +0000 | [diff] [blame] | 1797 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1798 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1799 | // Build _objc_method_list for class's methods if needed |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1800 | Result += "\nstatic struct _objc_method_list _OBJC_"; |
| 1801 | Result += prefix; |
| 1802 | Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; |
| 1803 | Result += "_METHODS_"; |
| 1804 | Result += ClassName; |
| 1805 | Result += " __attribute__ ((section (\"__OBJC, __"; |
| 1806 | Result += IsInstanceMethod ? "inst" : "cls"; |
| 1807 | Result += "_meth\")))= "; |
| 1808 | Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1810 | Result += "\t,{{(SEL)\""; |
| 1811 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
| 1812 | std::string MethodTypeString; |
| 1813 | Context->getObjcEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
| 1814 | Result += "\", \""; |
| 1815 | Result += MethodTypeString; |
| 1816 | Result += "\", "; |
| 1817 | Result += MethodInternalNames[*MethodBegin]; |
| 1818 | Result += "}\n"; |
| 1819 | for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { |
| 1820 | Result += "\t ,{(SEL)\""; |
| 1821 | Result += (*MethodBegin)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1822 | std::string MethodTypeString; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1823 | Context->getObjcEncodingForMethodDecl(*MethodBegin, MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1824 | Result += "\", \""; |
| 1825 | Result += MethodTypeString; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1826 | Result += "\", "; |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1827 | Result += MethodInternalNames[*MethodBegin]; |
Fariborz Jahanian | b7908b5 | 2007-11-13 21:02:00 +0000 | [diff] [blame] | 1828 | Result += "}\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1829 | } |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 1830 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | /// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data. |
| 1834 | void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols, |
| 1835 | int NumProtocols, |
| 1836 | const char *prefix, |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1837 | const char *ClassName, |
| 1838 | std::string &Result) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1839 | static bool objc_protocol_methods = false; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1840 | if (NumProtocols > 0) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1841 | for (int i = 0; i < NumProtocols; i++) { |
| 1842 | ObjcProtocolDecl *PDecl = Protocols[i]; |
| 1843 | // Output struct protocol_methods holder of method selector and type. |
| 1844 | if (!objc_protocol_methods && |
| 1845 | (PDecl->getNumInstanceMethods() > 0 |
| 1846 | || PDecl->getNumClassMethods() > 0)) { |
| 1847 | /* struct protocol_methods { |
| 1848 | SEL _cmd; |
| 1849 | char *method_types; |
| 1850 | } |
| 1851 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1852 | Result += "\nstruct protocol_methods {\n"; |
| 1853 | Result += "\tSEL _cmd;\n"; |
| 1854 | Result += "\tchar *method_types;\n"; |
| 1855 | Result += "};\n"; |
| 1856 | |
| 1857 | /* struct _objc_protocol_method_list { |
| 1858 | int protocol_method_count; |
| 1859 | struct protocol_methods protocols[]; |
| 1860 | } |
| 1861 | */ |
| 1862 | Result += "\nstruct _objc_protocol_method_list {\n"; |
| 1863 | Result += "\tint protocol_method_count;\n"; |
| 1864 | Result += "\tstruct protocol_methods protocols[];\n};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1865 | objc_protocol_methods = true; |
| 1866 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1867 | |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 1868 | int NumMethods = PDecl->getNumInstanceMethods(); |
| 1869 | if(NumMethods > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1870 | Result += "\nstatic struct _objc_protocol_method_list " |
| 1871 | "_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 1872 | Result += PDecl->getName(); |
| 1873 | Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= " |
| 1874 | "{\n\t" + utostr(NumMethods) + "\n"; |
| 1875 | |
Fariborz Jahanian | fc591ac | 2007-12-17 17:56:10 +0000 | [diff] [blame] | 1876 | // Output instance methods declared in this protocol. |
| 1877 | for (ObjcProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
| 1878 | E = PDecl->instmeth_end(); I != E; ++I) { |
| 1879 | if (I == PDecl->instmeth_begin()) |
| 1880 | Result += "\t ,{{(SEL)\""; |
| 1881 | else |
| 1882 | Result += "\t ,{(SEL)\""; |
| 1883 | Result += (*I)->getSelector().getName().c_str(); |
| 1884 | std::string MethodTypeString; |
| 1885 | Context->getObjcEncodingForMethodDecl((*I), MethodTypeString); |
| 1886 | Result += "\", \""; |
| 1887 | Result += MethodTypeString; |
| 1888 | Result += "\"}\n"; |
| 1889 | } |
| 1890 | Result += "\t }\n};\n"; |
| 1891 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Output class methods declared in this protocol. |
| 1894 | NumMethods = PDecl->getNumClassMethods(); |
| 1895 | if (NumMethods > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1896 | Result += "\nstatic struct _objc_protocol_method_list " |
| 1897 | "_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 1898 | Result += PDecl->getName(); |
| 1899 | Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= " |
| 1900 | "{\n\t"; |
| 1901 | Result += utostr(NumMethods); |
| 1902 | Result += "\n"; |
| 1903 | |
Fariborz Jahanian | c6e2c2a | 2007-12-17 18:07:01 +0000 | [diff] [blame] | 1904 | // Output instance methods declared in this protocol. |
| 1905 | for (ObjcProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
| 1906 | E = PDecl->classmeth_end(); I != E; ++I) { |
| 1907 | if (I == PDecl->classmeth_begin()) |
| 1908 | Result += "\t ,{{(SEL)\""; |
| 1909 | else |
| 1910 | Result += "\t ,{(SEL)\""; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1911 | Result += (*I)->getSelector().getName().c_str(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1912 | std::string MethodTypeString; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1913 | Context->getObjcEncodingForMethodDecl((*I), MethodTypeString); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1914 | Result += "\", \""; |
| 1915 | Result += MethodTypeString; |
| 1916 | Result += "\"}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1917 | } |
| 1918 | Result += "\t }\n};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1919 | } |
| 1920 | // Output: |
| 1921 | /* struct _objc_protocol { |
| 1922 | // Objective-C 1.0 extensions |
| 1923 | struct _objc_protocol_extension *isa; |
| 1924 | char *protocol_name; |
| 1925 | struct _objc_protocol **protocol_list; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1926 | struct _objc_protocol_method_list *instance_methods; |
| 1927 | struct _objc_protocol_method_list *class_methods; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1928 | }; |
| 1929 | */ |
| 1930 | static bool objc_protocol = false; |
| 1931 | if (!objc_protocol) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1932 | Result += "\nstruct _objc_protocol {\n"; |
| 1933 | Result += "\tstruct _objc_protocol_extension *isa;\n"; |
| 1934 | Result += "\tchar *protocol_name;\n"; |
| 1935 | Result += "\tstruct _objc_protocol **protocol_list;\n"; |
| 1936 | Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; |
| 1937 | Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; |
| 1938 | Result += "};\n"; |
| 1939 | |
| 1940 | /* struct _objc_protocol_list { |
| 1941 | struct _objc_protocol_list *next; |
| 1942 | int protocol_count; |
| 1943 | struct _objc_protocol *class_protocols[]; |
| 1944 | } |
| 1945 | */ |
| 1946 | Result += "\nstruct _objc_protocol_list {\n"; |
| 1947 | Result += "\tstruct _objc_protocol_list *next;\n"; |
| 1948 | Result += "\tint protocol_count;\n"; |
| 1949 | Result += "\tstruct _objc_protocol *class_protocols[];\n"; |
| 1950 | Result += "};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1951 | objc_protocol = true; |
| 1952 | } |
| 1953 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1954 | Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; |
| 1955 | Result += PDecl->getName(); |
| 1956 | Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= " |
| 1957 | "{\n\t0, \""; |
| 1958 | Result += PDecl->getName(); |
| 1959 | Result += "\", 0, "; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1960 | if (PDecl->getNumInstanceMethods() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1961 | Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_"; |
| 1962 | Result += PDecl->getName(); |
| 1963 | Result += ", "; |
| 1964 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1965 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1966 | Result += "0, "; |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1967 | if (PDecl->getNumClassMethods() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1968 | Result += "&_OBJC_PROTOCOL_CLASS_METHODS_"; |
| 1969 | Result += PDecl->getName(); |
| 1970 | Result += "\n"; |
| 1971 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1972 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1973 | Result += "0\n"; |
| 1974 | Result += "};\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1975 | } |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1976 | // Output the top lovel protocol meta-data for the class. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1977 | Result += "\nstatic struct _objc_protocol_list _OBJC_"; |
| 1978 | Result += prefix; |
| 1979 | Result += "_PROTOCOLS_"; |
| 1980 | Result += ClassName; |
| 1981 | Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= " |
| 1982 | "{\n\t0, "; |
| 1983 | Result += utostr(NumProtocols); |
| 1984 | Result += "\n"; |
| 1985 | |
| 1986 | Result += "\t,{&_OBJC_PROTOCOL_"; |
| 1987 | Result += Protocols[0]->getName(); |
| 1988 | Result += " \n"; |
| 1989 | |
| 1990 | for (int i = 1; i < NumProtocols; i++) { |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1991 | ObjcProtocolDecl *PDecl = Protocols[i]; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1992 | Result += "\t ,&_OBJC_PROTOCOL_"; |
| 1993 | Result += PDecl->getName(); |
| 1994 | Result += "\n"; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 1995 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 1996 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | /// RewriteObjcCategoryImplDecl - Rewrite metadata for each category |
| 2001 | /// implementation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2002 | void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl, |
| 2003 | std::string &Result) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2004 | ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface(); |
| 2005 | // Find category declaration for this implementation. |
| 2006 | ObjcCategoryDecl *CDecl; |
| 2007 | for (CDecl = ClassDecl->getCategoryList(); CDecl; |
| 2008 | CDecl = CDecl->getNextClassCategory()) |
| 2009 | if (CDecl->getIdentifier() == IDecl->getIdentifier()) |
| 2010 | break; |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2011 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2012 | char *FullCategoryName = (char*)alloca( |
| 2013 | strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2); |
| 2014 | sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName()); |
| 2015 | |
| 2016 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2017 | RewriteObjcMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
| 2018 | true, "CATEGORY_", FullCategoryName, Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2019 | |
| 2020 | // Build _objc_method_list for class's class methods if needed |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2021 | RewriteObjcMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 2022 | false, "CATEGORY_", FullCategoryName, Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2023 | |
| 2024 | // Protocols referenced in class declaration? |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2025 | // Null CDecl is case of a category implementation with no category interface |
| 2026 | if (CDecl) |
| 2027 | RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(), |
| 2028 | CDecl->getNumReferencedProtocols(), |
| 2029 | "CATEGORY", |
| 2030 | FullCategoryName, Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2031 | |
| 2032 | /* struct _objc_category { |
| 2033 | char *category_name; |
| 2034 | char *class_name; |
| 2035 | struct _objc_method_list *instance_methods; |
| 2036 | struct _objc_method_list *class_methods; |
| 2037 | struct _objc_protocol_list *protocols; |
| 2038 | // Objective-C 1.0 extensions |
| 2039 | uint32_t size; // sizeof (struct _objc_category) |
| 2040 | struct _objc_property_list *instance_properties; // category's own |
| 2041 | // @property decl. |
| 2042 | }; |
| 2043 | */ |
| 2044 | |
| 2045 | static bool objc_category = false; |
| 2046 | if (!objc_category) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2047 | Result += "\nstruct _objc_category {\n"; |
| 2048 | Result += "\tchar *category_name;\n"; |
| 2049 | Result += "\tchar *class_name;\n"; |
| 2050 | Result += "\tstruct _objc_method_list *instance_methods;\n"; |
| 2051 | Result += "\tstruct _objc_method_list *class_methods;\n"; |
| 2052 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2053 | Result += "\tunsigned int size;\n"; |
| 2054 | Result += "\tstruct _objc_property_list *instance_properties;\n"; |
| 2055 | Result += "};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2056 | objc_category = true; |
Fariborz Jahanian | e887c09 | 2007-10-22 21:41:37 +0000 | [diff] [blame] | 2057 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2058 | Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; |
| 2059 | Result += FullCategoryName; |
| 2060 | Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\""; |
| 2061 | Result += IDecl->getName(); |
| 2062 | Result += "\"\n\t, \""; |
| 2063 | Result += ClassDecl->getName(); |
| 2064 | Result += "\"\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2065 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2066 | if (IDecl->getNumInstanceMethods() > 0) { |
| 2067 | Result += "\t, (struct _objc_method_list *)" |
| 2068 | "&_OBJC_CATEGORY_INSTANCE_METHODS_"; |
| 2069 | Result += FullCategoryName; |
| 2070 | Result += "\n"; |
| 2071 | } |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2072 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2073 | Result += "\t, 0\n"; |
| 2074 | if (IDecl->getNumClassMethods() > 0) { |
| 2075 | Result += "\t, (struct _objc_method_list *)" |
| 2076 | "&_OBJC_CATEGORY_CLASS_METHODS_"; |
| 2077 | Result += FullCategoryName; |
| 2078 | Result += "\n"; |
| 2079 | } |
| 2080 | else |
| 2081 | Result += "\t, 0\n"; |
| 2082 | |
Fariborz Jahanian | bac97d4 | 2007-11-13 22:09:49 +0000 | [diff] [blame] | 2083 | if (CDecl && CDecl->getNumReferencedProtocols() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2084 | Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; |
| 2085 | Result += FullCategoryName; |
| 2086 | Result += "\n"; |
| 2087 | } |
| 2088 | else |
| 2089 | Result += "\t, 0\n"; |
| 2090 | Result += "\t, sizeof(struct _objc_category), 0\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2093 | /// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of |
| 2094 | /// ivar offset. |
| 2095 | void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl, |
| 2096 | ObjcIvarDecl *ivar, |
| 2097 | std::string &Result) { |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2098 | Result += "offsetof(struct "; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2099 | Result += IDecl->getName(); |
| 2100 | Result += ", "; |
| 2101 | Result += ivar->getName(); |
| 2102 | Result += ")"; |
| 2103 | } |
| 2104 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2105 | //===----------------------------------------------------------------------===// |
| 2106 | // Meta Data Emission |
| 2107 | //===----------------------------------------------------------------------===// |
| 2108 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2109 | void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl, |
| 2110 | std::string &Result) { |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2111 | ObjcInterfaceDecl *CDecl = IDecl->getClassInterface(); |
| 2112 | |
Fariborz Jahanian | ebe668f | 2007-11-26 20:59:57 +0000 | [diff] [blame] | 2113 | // Explictly declared @interface's are already synthesized. |
| 2114 | if (CDecl->ImplicitInterfaceDecl()) { |
| 2115 | // FIXME: Implementation of a class with no @interface (legacy) doese not |
| 2116 | // produce correct synthesis as yet. |
| 2117 | SynthesizeObjcInternalStruct(CDecl, Result); |
| 2118 | } |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2119 | |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2120 | // Build _objc_ivar_list metadata for classes ivars if needed |
| 2121 | int NumIvars = IDecl->getImplDeclNumIvars() > 0 |
| 2122 | ? IDecl->getImplDeclNumIvars() |
| 2123 | : (CDecl ? CDecl->getNumInstanceVariables() : 0); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2124 | if (NumIvars > 0) { |
| 2125 | static bool objc_ivar = false; |
| 2126 | if (!objc_ivar) { |
| 2127 | /* struct _objc_ivar { |
| 2128 | char *ivar_name; |
| 2129 | char *ivar_type; |
| 2130 | int ivar_offset; |
| 2131 | }; |
| 2132 | */ |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2133 | Result += "\nstruct _objc_ivar {\n"; |
| 2134 | Result += "\tchar *ivar_name;\n"; |
| 2135 | Result += "\tchar *ivar_type;\n"; |
| 2136 | Result += "\tint ivar_offset;\n"; |
| 2137 | Result += "};\n"; |
| 2138 | |
| 2139 | /* struct _objc_ivar_list { |
| 2140 | int ivar_count; |
| 2141 | struct _objc_ivar ivar_list[]; |
| 2142 | }; |
| 2143 | */ |
| 2144 | Result += "\nstruct _objc_ivar_list {\n"; |
| 2145 | Result += "\tint ivar_count;\n"; |
| 2146 | Result += "\tstruct _objc_ivar ivar_list[];\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2147 | objc_ivar = true; |
| 2148 | } |
| 2149 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2150 | Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_"; |
| 2151 | Result += IDecl->getName(); |
| 2152 | Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= " |
| 2153 | "{\n\t"; |
| 2154 | Result += utostr(NumIvars); |
| 2155 | Result += "\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2156 | |
| 2157 | ObjcInterfaceDecl::ivar_iterator IVI, IVE; |
| 2158 | if (IDecl->getImplDeclNumIvars() > 0) { |
| 2159 | IVI = IDecl->ivar_begin(); |
| 2160 | IVE = IDecl->ivar_end(); |
| 2161 | } else { |
| 2162 | IVI = CDecl->ivar_begin(); |
| 2163 | IVE = CDecl->ivar_end(); |
| 2164 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2165 | Result += "\t,{{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2166 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2167 | Result += "\", \""; |
| 2168 | std::string StrEncoding; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2169 | Context->getObjcEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2170 | Result += StrEncoding; |
| 2171 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2172 | SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2173 | Result += "}\n"; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2174 | for (++IVI; IVI != IVE; ++IVI) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2175 | Result += "\t ,{\""; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2176 | Result += (*IVI)->getName(); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2177 | Result += "\", \""; |
| 2178 | std::string StrEncoding; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2179 | Context->getObjcEncodingForType((*IVI)->getType(), StrEncoding); |
Fariborz Jahanian | 160eb65 | 2007-10-29 17:16:25 +0000 | [diff] [blame] | 2180 | Result += StrEncoding; |
| 2181 | Result += "\", "; |
Chris Lattner | be6df08 | 2007-12-12 07:56:42 +0000 | [diff] [blame] | 2182 | SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2183 | Result += "}\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | Result += "\t }\n};\n"; |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | // Build _objc_method_list for class's instance methods if needed |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2190 | RewriteObjcMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(), |
| 2191 | true, "", IDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2192 | |
| 2193 | // Build _objc_method_list for class's class methods if needed |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2194 | RewriteObjcMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), |
| 2195 | false, "", IDecl->getName(), Result); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2196 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2197 | // Protocols referenced in class declaration? |
| 2198 | RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(), |
| 2199 | CDecl->getNumIntfRefProtocols(), |
Chris Lattner | ab4c4d5 | 2007-12-12 07:46:12 +0000 | [diff] [blame] | 2200 | "CLASS", CDecl->getName(), Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2201 | |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2202 | |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2203 | // Declaration of class/meta-class metadata |
| 2204 | /* struct _objc_class { |
| 2205 | struct _objc_class *isa; // or const char *root_class_name when metadata |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2206 | const char *super_class_name; |
| 2207 | char *name; |
| 2208 | long version; |
| 2209 | long info; |
| 2210 | long instance_size; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2211 | struct _objc_ivar_list *ivars; |
| 2212 | struct _objc_method_list *methods; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2213 | struct objc_cache *cache; |
| 2214 | struct objc_protocol_list *protocols; |
| 2215 | const char *ivar_layout; |
| 2216 | struct _objc_class_ext *ext; |
| 2217 | }; |
| 2218 | */ |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2219 | static bool objc_class = false; |
| 2220 | if (!objc_class) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2221 | Result += "\nstruct _objc_class {\n"; |
| 2222 | Result += "\tstruct _objc_class *isa;\n"; |
| 2223 | Result += "\tconst char *super_class_name;\n"; |
| 2224 | Result += "\tchar *name;\n"; |
| 2225 | Result += "\tlong version;\n"; |
| 2226 | Result += "\tlong info;\n"; |
| 2227 | Result += "\tlong instance_size;\n"; |
| 2228 | Result += "\tstruct _objc_ivar_list *ivars;\n"; |
| 2229 | Result += "\tstruct _objc_method_list *methods;\n"; |
| 2230 | Result += "\tstruct objc_cache *cache;\n"; |
| 2231 | Result += "\tstruct _objc_protocol_list *protocols;\n"; |
| 2232 | Result += "\tconst char *ivar_layout;\n"; |
| 2233 | Result += "\tstruct _objc_class_ext *ext;\n"; |
| 2234 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2235 | objc_class = true; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | // Meta-class metadata generation. |
| 2239 | ObjcInterfaceDecl *RootClass = 0; |
| 2240 | ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass(); |
| 2241 | while (SuperClass) { |
| 2242 | RootClass = SuperClass; |
| 2243 | SuperClass = SuperClass->getSuperClass(); |
| 2244 | } |
| 2245 | SuperClass = CDecl->getSuperClass(); |
| 2246 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2247 | Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; |
| 2248 | Result += CDecl->getName(); |
| 2249 | Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= " |
| 2250 | "{\n\t(struct _objc_class *)\""; |
| 2251 | Result += (RootClass ? RootClass->getName() : CDecl->getName()); |
| 2252 | Result += "\""; |
| 2253 | |
| 2254 | if (SuperClass) { |
| 2255 | Result += ", \""; |
| 2256 | Result += SuperClass->getName(); |
| 2257 | Result += "\", \""; |
| 2258 | Result += CDecl->getName(); |
| 2259 | Result += "\""; |
| 2260 | } |
| 2261 | else { |
| 2262 | Result += ", 0, \""; |
| 2263 | Result += CDecl->getName(); |
| 2264 | Result += "\""; |
| 2265 | } |
Fariborz Jahanian | 04b3824 | 2007-12-04 19:31:56 +0000 | [diff] [blame] | 2266 | // 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] | 2267 | // 'info' field is initialized to CLS_META(2) for metaclass |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2268 | Result += ", 0,2, sizeof(struct _objc_class), 0"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2269 | if (IDecl->getNumClassMethods() > 0) { |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2270 | Result += "\n\t, &_OBJC_CLASS_METHODS_"; |
Steve Naroff | b26d713 | 2007-12-05 21:49:40 +0000 | [diff] [blame] | 2271 | Result += IDecl->getName(); |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2272 | Result += "\n"; |
| 2273 | } |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2274 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2275 | Result += ", 0\n"; |
| 2276 | if (CDecl->getNumIntfRefProtocols() > 0) { |
| 2277 | Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_"; |
| 2278 | Result += CDecl->getName(); |
| 2279 | Result += ",0,0\n"; |
| 2280 | } |
Fariborz Jahanian | 454cb01 | 2007-10-24 20:54:23 +0000 | [diff] [blame] | 2281 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2282 | Result += "\t,0,0,0,0\n"; |
| 2283 | Result += "};\n"; |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2284 | |
| 2285 | // class metadata generation. |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2286 | Result += "\nstatic struct _objc_class _OBJC_CLASS_"; |
| 2287 | Result += CDecl->getName(); |
| 2288 | Result += " __attribute__ ((section (\"__OBJC, __class\")))= " |
| 2289 | "{\n\t&_OBJC_METACLASS_"; |
| 2290 | Result += CDecl->getName(); |
| 2291 | if (SuperClass) { |
| 2292 | Result += ", \""; |
| 2293 | Result += SuperClass->getName(); |
| 2294 | Result += "\", \""; |
| 2295 | Result += CDecl->getName(); |
| 2296 | Result += "\""; |
| 2297 | } |
| 2298 | else { |
| 2299 | Result += ", 0, \""; |
| 2300 | Result += CDecl->getName(); |
| 2301 | Result += "\""; |
| 2302 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2303 | // 'info' field is initialized to CLS_CLASS(1) for class |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2304 | Result += ", 0,1"; |
| 2305 | if (!ObjcSynthesizedStructs.count(CDecl)) |
| 2306 | Result += ",0"; |
| 2307 | else { |
| 2308 | // class has size. Must synthesize its size. |
Fariborz Jahanian | 909f02a | 2007-11-05 17:47:33 +0000 | [diff] [blame] | 2309 | Result += ",sizeof(struct "; |
Fariborz Jahanian | 4d733d3 | 2007-10-26 23:09:28 +0000 | [diff] [blame] | 2310 | Result += CDecl->getName(); |
| 2311 | Result += ")"; |
| 2312 | } |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2313 | if (NumIvars > 0) { |
| 2314 | Result += ", &_OBJC_INSTANCE_VARIABLES_"; |
| 2315 | Result += CDecl->getName(); |
| 2316 | Result += "\n\t"; |
| 2317 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2318 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2319 | Result += ",0"; |
| 2320 | if (IDecl->getNumInstanceMethods() > 0) { |
| 2321 | Result += ", &_OBJC_INSTANCE_METHODS_"; |
| 2322 | Result += CDecl->getName(); |
| 2323 | Result += ", 0\n\t"; |
| 2324 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2325 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2326 | Result += ",0,0"; |
| 2327 | if (CDecl->getNumIntfRefProtocols() > 0) { |
| 2328 | Result += ", &_OBJC_CLASS_PROTOCOLS_"; |
| 2329 | Result += CDecl->getName(); |
| 2330 | Result += ", 0,0\n"; |
| 2331 | } |
Fariborz Jahanian | deef518 | 2007-10-23 18:53:48 +0000 | [diff] [blame] | 2332 | else |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2333 | Result += ",0,0,0\n"; |
| 2334 | Result += "};\n"; |
Fariborz Jahanian | 9f0a1cb | 2007-10-23 00:02:02 +0000 | [diff] [blame] | 2335 | } |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2336 | |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2337 | /// RewriteImplementations - This routine rewrites all method implementations |
| 2338 | /// and emits meta-data. |
| 2339 | |
| 2340 | void RewriteTest::RewriteImplementations(std::string &Result) { |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2341 | int ClsDefCount = ClassImplementation.size(); |
| 2342 | int CatDefCount = CategoryImplementation.size(); |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2343 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2344 | if (ClsDefCount == 0 && CatDefCount == 0) |
| 2345 | return; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2346 | // Rewrite implemented methods |
| 2347 | for (int i = 0; i < ClsDefCount; i++) |
| 2348 | RewriteImplementationDecl(ClassImplementation[i]); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2349 | |
Fariborz Jahanian | 66d6b29 | 2007-11-13 20:04:28 +0000 | [diff] [blame] | 2350 | for (int i = 0; i < CatDefCount; i++) |
| 2351 | RewriteImplementationDecl(CategoryImplementation[i]); |
| 2352 | |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2353 | // This is needed for use of offsetof |
| 2354 | Result += "#include <stddef.h>\n"; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2355 | |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2356 | // For each implemented class, write out all its meta data. |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2357 | for (int i = 0; i < ClsDefCount; i++) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2358 | RewriteObjcClassMetaData(ClassImplementation[i], Result); |
Fariborz Jahanian | 2e6d935 | 2007-10-24 19:23:36 +0000 | [diff] [blame] | 2359 | |
| 2360 | // For each implemented category, write out all its meta data. |
| 2361 | for (int i = 0; i < CatDefCount; i++) |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2362 | RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result); |
Fariborz Jahanian | f4d331d | 2007-10-18 22:09:03 +0000 | [diff] [blame] | 2363 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2364 | // Write objc_symtab metadata |
| 2365 | /* |
| 2366 | struct _objc_symtab |
| 2367 | { |
| 2368 | long sel_ref_cnt; |
| 2369 | SEL *refs; |
| 2370 | short cls_def_cnt; |
| 2371 | short cat_def_cnt; |
| 2372 | void *defs[cls_def_cnt + cat_def_cnt]; |
| 2373 | }; |
| 2374 | */ |
| 2375 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2376 | Result += "\nstruct _objc_symtab {\n"; |
| 2377 | Result += "\tlong sel_ref_cnt;\n"; |
| 2378 | Result += "\tSEL *refs;\n"; |
| 2379 | Result += "\tshort cls_def_cnt;\n"; |
| 2380 | Result += "\tshort cat_def_cnt;\n"; |
| 2381 | Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; |
| 2382 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2383 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2384 | Result += "static struct _objc_symtab " |
| 2385 | "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n"; |
| 2386 | Result += "\t0, 0, " + utostr(ClsDefCount) |
| 2387 | + ", " + utostr(CatDefCount) + "\n"; |
| 2388 | for (int i = 0; i < ClsDefCount; i++) { |
| 2389 | Result += "\t,&_OBJC_CLASS_"; |
| 2390 | Result += ClassImplementation[i]->getName(); |
| 2391 | Result += "\n"; |
| 2392 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2393 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2394 | for (int i = 0; i < CatDefCount; i++) { |
| 2395 | Result += "\t,&_OBJC_CATEGORY_"; |
| 2396 | Result += CategoryImplementation[i]->getClassInterface()->getName(); |
| 2397 | Result += "_"; |
| 2398 | Result += CategoryImplementation[i]->getName(); |
| 2399 | Result += "\n"; |
| 2400 | } |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2401 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2402 | Result += "};\n\n"; |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2403 | |
| 2404 | // Write objc_module metadata |
| 2405 | |
| 2406 | /* |
| 2407 | struct _objc_module { |
| 2408 | long version; |
| 2409 | long size; |
| 2410 | const char *name; |
| 2411 | struct _objc_symtab *symtab; |
| 2412 | } |
| 2413 | */ |
| 2414 | |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2415 | Result += "\nstruct _objc_module {\n"; |
| 2416 | Result += "\tlong version;\n"; |
| 2417 | Result += "\tlong size;\n"; |
| 2418 | Result += "\tconst char *name;\n"; |
| 2419 | Result += "\tstruct _objc_symtab *symtab;\n"; |
| 2420 | Result += "};\n\n"; |
| 2421 | Result += "static struct _objc_module " |
| 2422 | "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n"; |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 2423 | Result += "\t" + utostr(OBJC_ABI_VERSION) + |
| 2424 | ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; |
Fariborz Jahanian | ccd87b0 | 2007-10-25 20:55:25 +0000 | [diff] [blame] | 2425 | Result += "};\n\n"; |
Fariborz Jahanian | 7a3279d | 2007-11-13 19:21:13 +0000 | [diff] [blame] | 2426 | |
Fariborz Jahanian | 545b9ae | 2007-10-18 19:23:00 +0000 | [diff] [blame] | 2427 | } |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 2428 | |