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