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