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