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