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