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