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