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