blob: 7bbd7afdb522dc2ab3b43a236beb690b88deae03 [file] [log] [blame]
Chris Lattnerb429ae42007-10-11 00:43:27 +00001//===--- 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 Lattner569faa62007-10-11 18:38:32 +000015#include "clang/Rewrite/Rewriter.h"
Chris Lattnerb429ae42007-10-11 00:43:27 +000016#include "clang/AST/AST.h"
17#include "clang/AST/ASTConsumer.h"
Chris Lattner569faa62007-10-11 18:38:32 +000018#include "clang/Basic/SourceManager.h"
Steve Naroffe9780582007-10-23 23:50:29 +000019#include "clang/Basic/IdentifierTable.h"
Chris Lattnerc3aa5c42007-10-25 17:07:24 +000020#include "llvm/ADT/StringExtras.h"
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000021#include "llvm/ADT/SmallPtrSet.h"
Steve Naroff1ccf4632007-10-30 03:43:13 +000022#include "clang/Lex/Lexer.h"
Chris Lattnerb429ae42007-10-11 00:43:27 +000023using namespace clang;
Chris Lattnerc3aa5c42007-10-25 17:07:24 +000024using llvm::utostr;
Chris Lattnerb429ae42007-10-11 00:43:27 +000025
Chris Lattnerb429ae42007-10-11 00:43:27 +000026namespace {
Chris Lattner569faa62007-10-11 18:38:32 +000027 class RewriteTest : public ASTConsumer {
Chris Lattner74db1682007-10-16 21:07:07 +000028 Rewriter Rewrite;
Chris Lattnerbf0bfa62007-10-17 22:35:30 +000029 ASTContext *Context;
Chris Lattnerb429ae42007-10-11 00:43:27 +000030 SourceManager *SM;
Chris Lattner569faa62007-10-11 18:38:32 +000031 unsigned MainFileID;
Chris Lattner74db1682007-10-16 21:07:07 +000032 SourceLocation LastIncLoc;
Fariborz Jahanian640a01f2007-10-18 19:23:00 +000033 llvm::SmallVector<ObjcImplementationDecl *, 8> ClassImplementation;
34 llvm::SmallVector<ObjcCategoryImplDecl *, 8> CategoryImplementation;
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000035 llvm::SmallPtrSet<ObjcInterfaceDecl*, 8> ObjcSynthesizedStructs;
Steve Naroff809b4f02007-10-31 22:11:35 +000036 llvm::SmallPtrSet<ObjcInterfaceDecl*, 8> ObjcForwardDecls;
Steve Naroffe9780582007-10-23 23:50:29 +000037
38 FunctionDecl *MsgSendFunctionDecl;
39 FunctionDecl *GetClassFunctionDecl;
Steve Naroff71226032007-10-24 22:48:43 +000040 FunctionDecl *SelGetUidFunctionDecl;
Steve Naroffe9780582007-10-23 23:50:29 +000041
Fariborz Jahanian640a01f2007-10-18 19:23:00 +000042 static const int OBJC_ABI_VERSION =7 ;
Chris Lattnerb429ae42007-10-11 00:43:27 +000043 public:
Chris Lattnerbf0bfa62007-10-17 22:35:30 +000044 void Initialize(ASTContext &context, unsigned mainFileID) {
45 Context = &context;
46 SM = &Context->SourceMgr;
Chris Lattner569faa62007-10-11 18:38:32 +000047 MainFileID = mainFileID;
Steve Naroffe9780582007-10-23 23:50:29 +000048 MsgSendFunctionDecl = 0;
Steve Naroff95b28c12007-10-24 01:09:48 +000049 GetClassFunctionDecl = 0;
Steve Naroff71226032007-10-24 22:48:43 +000050 SelGetUidFunctionDecl = 0;
Chris Lattnerbf0bfa62007-10-17 22:35:30 +000051 Rewrite.setSourceMgr(Context->SourceMgr);
Chris Lattnerb429ae42007-10-11 00:43:27 +000052 }
Chris Lattner569faa62007-10-11 18:38:32 +000053
Chris Lattnerfce2c5a2007-10-24 17:06:59 +000054 // Top Level Driver code.
55 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner74db1682007-10-16 21:07:07 +000056 void HandleDeclInMainFile(Decl *D);
Chris Lattnerfce2c5a2007-10-24 17:06:59 +000057 ~RewriteTest();
58
59 // Syntactic Rewriting.
Chris Lattner74db1682007-10-16 21:07:07 +000060 void RewriteInclude(SourceLocation Loc);
Chris Lattnerfce2c5a2007-10-24 17:06:59 +000061 void RewriteTabs();
62 void RewriteForwardClassDecl(ObjcClassDecl *Dcl);
Steve Naroff3774dd92007-10-26 20:53:56 +000063 void RewriteInterfaceDecl(ObjcInterfaceDecl *Dcl);
Steve Naroff667f1682007-10-30 13:30:57 +000064 void RewriteCategoryDecl(ObjcCategoryDecl *Dcl);
Steve Narofff4b7d6a2007-10-30 16:42:30 +000065 void RewriteProtocolDecl(ObjcProtocolDecl *Dcl);
Steve Naroff667f1682007-10-30 13:30:57 +000066 void RewriteMethods(int nMethods, ObjcMethodDecl **Methods);
Steve Naroff02a82aa2007-10-30 23:14:51 +000067 void RewriteFunctionDecl(FunctionDecl *FD);
Steve Naroffc8a92d12007-11-01 13:24:47 +000068 void RewriteObjcQualifiedInterfaceTypes(
69 const FunctionTypeProto *proto, FunctionDecl *FD);
70 bool needToScanForQualifiers(QualType T);
Chris Lattner6fe8b272007-10-16 22:36:42 +000071
Chris Lattnerfce2c5a2007-10-24 17:06:59 +000072 // Expression Rewriting.
Chris Lattner0021f452007-10-24 16:57:36 +000073 Stmt *RewriteFunctionBody(Stmt *S);
74 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
75 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroff71226032007-10-24 22:48:43 +000076 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
77 Expr **args, unsigned nargs);
Steve Naroff02a82aa2007-10-30 23:14:51 +000078 void SynthMsgSendFunctionDecl();
79 void SynthGetClassFunctionDecl();
80
Chris Lattnerfce2c5a2007-10-24 17:06:59 +000081 // Metadata emission.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +000082 void RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
83 std::string &Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +000084
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +000085 void RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *CDecl,
86 std::string &Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +000087
88 void RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
89 int NumMethods,
Fariborz Jahaniana3986372007-10-25 00:14:44 +000090 bool IsInstanceMethod,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +000091 const char *prefix,
Chris Lattnerc3aa5c42007-10-25 17:07:24 +000092 const char *ClassName,
93 std::string &Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +000094
95 void RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
96 int NumProtocols,
97 const char *prefix,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +000098 const char *ClassName,
99 std::string &Result);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000100 void SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
101 std::string &Result);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000102 void SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
103 ObjcIvarDecl *ivar,
104 std::string &Result);
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000105 void WriteObjcMetaData(std::string &Result);
Chris Lattnerb429ae42007-10-11 00:43:27 +0000106 };
107}
108
Chris Lattner569faa62007-10-11 18:38:32 +0000109ASTConsumer *clang::CreateCodeRewriterTest() { return new RewriteTest(); }
Chris Lattnerb429ae42007-10-11 00:43:27 +0000110
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000111//===----------------------------------------------------------------------===//
112// Top Level Driver Code
113//===----------------------------------------------------------------------===//
114
Chris Lattner569faa62007-10-11 18:38:32 +0000115void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner74db1682007-10-16 21:07:07 +0000116 // 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 Naroffe9780582007-10-23 23:50:29 +0000125 // Look for built-in declarations that we need to refer during the rewrite.
126 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Steve Naroff02a82aa2007-10-30 23:14:51 +0000127 RewriteFunctionDecl(FD);
Steve Naroff3774dd92007-10-26 20:53:56 +0000128 } else if (ObjcInterfaceDecl *MD = dyn_cast<ObjcInterfaceDecl>(D)) {
129 RewriteInterfaceDecl(MD);
Steve Naroff667f1682007-10-30 13:30:57 +0000130 } else if (ObjcCategoryDecl *CD = dyn_cast<ObjcCategoryDecl>(D)) {
131 RewriteCategoryDecl(CD);
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000132 } else if (ObjcProtocolDecl *PD = dyn_cast<ObjcProtocolDecl>(D)) {
133 RewriteProtocolDecl(PD);
Steve Naroffe9780582007-10-23 23:50:29 +0000134 }
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000135 // If we have a decl in the main file, see if we should rewrite it.
Chris Lattner74db1682007-10-16 21:07:07 +0000136 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
137 return HandleDeclInMainFile(D);
138
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000139 // Otherwise, see if there is a #import in the main file that should be
140 // rewritten.
Chris Lattner74db1682007-10-16 21:07:07 +0000141 RewriteInclude(Loc);
142}
143
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000144/// HandleDeclInMainFile - This is called for each top-level decl defined in the
145/// main file of the input.
146void 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
160RewriteTest::~RewriteTest() {
161 // Get the top-level buffer that this corresponds to.
162 RewriteTabs();
163
Chris Lattner8593cbf2007-11-03 06:24:16 +0000164 // Rewrite Objective-c meta data*
165 std::string ResultStr;
166 WriteObjcMetaData(ResultStr);
167 // For now just print the string out.
168 printf("%s", ResultStr.c_str());
169
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000170 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
171 // we are done.
172 if (const RewriteBuffer *RewriteBuf =
173 Rewrite.getRewriteBufferFor(MainFileID)) {
174 printf("Changed:\n");
175 std::string S(RewriteBuf->begin(), RewriteBuf->end());
176 printf("%s\n", S.c_str());
177 } else {
178 printf("No changes\n");
179 }
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000180
181}
182
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000183//===----------------------------------------------------------------------===//
184// Syntactic (non-AST) Rewriting Code
185//===----------------------------------------------------------------------===//
186
Chris Lattner74db1682007-10-16 21:07:07 +0000187void RewriteTest::RewriteInclude(SourceLocation Loc) {
188 // Rip up the #include stack to the main file.
189 SourceLocation IncLoc = Loc, NextLoc = Loc;
190 do {
191 IncLoc = Loc;
192 Loc = SM->getLogicalLoc(NextLoc);
193 NextLoc = SM->getIncludeLoc(Loc);
194 } while (!NextLoc.isInvalid());
195
196 // Loc is now the location of the #include filename "foo" or <foo/bar.h>.
197 // IncLoc indicates the header that was included if it is useful.
198 IncLoc = SM->getLogicalLoc(IncLoc);
199 if (SM->getDecomposedFileLoc(Loc).first != MainFileID ||
200 Loc == LastIncLoc)
201 return;
202 LastIncLoc = Loc;
203
204 unsigned IncCol = SM->getColumnNumber(Loc);
205 SourceLocation LineStartLoc = Loc.getFileLocWithOffset(-IncCol+1);
206
207 // Replace the #import with #include.
208 Rewrite.ReplaceText(LineStartLoc, IncCol-1, "#include ", strlen("#include "));
209}
210
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000211void RewriteTest::RewriteTabs() {
212 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
213 const char *MainBufStart = MainBuf.first;
214 const char *MainBufEnd = MainBuf.second;
Fariborz Jahanian640a01f2007-10-18 19:23:00 +0000215
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000216 // Loop over the whole file, looking for tabs.
217 for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
218 if (*BufPtr != '\t')
219 continue;
220
221 // Okay, we found a tab. This tab will turn into at least one character,
222 // but it depends on which 'virtual column' it is in. Compute that now.
223 unsigned VCol = 0;
224 while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
225 BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
226 ++VCol;
227
228 // Okay, now that we know the virtual column, we know how many spaces to
229 // insert. We assume 8-character tab-stops.
230 unsigned Spaces = 8-(VCol & 7);
231
232 // Get the location of the tab.
233 SourceLocation TabLoc =
234 SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
235
236 // Rewrite the single tab character into a sequence of spaces.
237 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
238 }
Chris Lattner569faa62007-10-11 18:38:32 +0000239}
240
241
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000242void RewriteTest::RewriteForwardClassDecl(ObjcClassDecl *ClassDecl) {
243 int numDecls = ClassDecl->getNumForwardDecls();
244 ObjcInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
245
246 // Get the start location and compute the semi location.
247 SourceLocation startLoc = ClassDecl->getLocation();
248 const char *startBuf = SM->getCharacterData(startLoc);
249 const char *semiPtr = strchr(startBuf, ';');
250
251 // Translate to typedef's that forward reference structs with the same name
252 // as the class. As a convenience, we include the original declaration
253 // as a comment.
254 std::string typedefString;
255 typedefString += "// ";
Steve Naroff71226032007-10-24 22:48:43 +0000256 typedefString.append(startBuf, semiPtr-startBuf+1);
257 typedefString += "\n";
258 for (int i = 0; i < numDecls; i++) {
259 ObjcInterfaceDecl *ForwardDecl = ForwardDecls[i];
Steve Naroff809b4f02007-10-31 22:11:35 +0000260 if (ObjcForwardDecls.count(ForwardDecl))
261 continue;
Steve Naroff71226032007-10-24 22:48:43 +0000262 typedefString += "typedef struct ";
263 typedefString += ForwardDecl->getName();
264 typedefString += " ";
265 typedefString += ForwardDecl->getName();
266 typedefString += ";\n";
Steve Naroff809b4f02007-10-31 22:11:35 +0000267
268 // Mark this typedef as having been generated.
269 if (!ObjcForwardDecls.insert(ForwardDecl))
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000270 assert(false && "typedef already output");
Steve Naroff71226032007-10-24 22:48:43 +0000271 }
272
273 // Replace the @class with typedefs corresponding to the classes.
274 Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1,
275 typedefString.c_str(), typedefString.size());
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000276}
277
Steve Naroff667f1682007-10-30 13:30:57 +0000278void RewriteTest::RewriteMethods(int nMethods, ObjcMethodDecl **Methods) {
279 for (int i = 0; i < nMethods; i++) {
280 ObjcMethodDecl *Method = Methods[i];
281 SourceLocation Loc = Method->getLocStart();
282
283 Rewrite.ReplaceText(Loc, 0, "// ", 3);
284
285 // FIXME: handle methods that are declared across multiple lines.
286 }
287}
288
289void RewriteTest::RewriteCategoryDecl(ObjcCategoryDecl *CatDecl) {
290 SourceLocation LocStart = CatDecl->getLocStart();
291
292 // FIXME: handle category headers that are declared across multiple lines.
293 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
294
295 RewriteMethods(CatDecl->getNumInstanceMethods(),
296 CatDecl->getInstanceMethods());
297 RewriteMethods(CatDecl->getNumClassMethods(),
298 CatDecl->getClassMethods());
299 // Lastly, comment out the @end.
300 Rewrite.ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
301}
302
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000303void RewriteTest::RewriteProtocolDecl(ObjcProtocolDecl *PDecl) {
304 SourceLocation LocStart = PDecl->getLocStart();
305
306 // FIXME: handle protocol headers that are declared across multiple lines.
307 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
308
309 RewriteMethods(PDecl->getNumInstanceMethods(),
310 PDecl->getInstanceMethods());
311 RewriteMethods(PDecl->getNumClassMethods(),
312 PDecl->getClassMethods());
313 // Lastly, comment out the @end.
314 Rewrite.ReplaceText(PDecl->getAtEndLoc(), 0, "// ", 3);
315}
316
Steve Naroff3774dd92007-10-26 20:53:56 +0000317void RewriteTest::RewriteInterfaceDecl(ObjcInterfaceDecl *ClassDecl) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000318
319 SourceLocation LocStart = ClassDecl->getLocStart();
320 SourceLocation LocEnd = ClassDecl->getLocEnd();
321
322 const char *startBuf = SM->getCharacterData(LocStart);
323 const char *endBuf = SM->getCharacterData(LocEnd);
324
Steve Naroff1ccf4632007-10-30 03:43:13 +0000325 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
Steve Naroffef20ed32007-10-30 02:23:23 +0000326
327 std::string ResultStr;
Steve Naroff77d081b2007-11-01 03:35:41 +0000328 if (!ObjcForwardDecls.count(ClassDecl)) {
329 // we haven't seen a forward decl - generate a typedef.
330 ResultStr += "typedef struct ";
331 ResultStr += ClassDecl->getName();
332 ResultStr += " ";
333 ResultStr += ClassDecl->getName();
334 ResultStr += ";";
335
336 // Mark this typedef as having been generated.
337 ObjcForwardDecls.insert(ClassDecl);
338 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000339 SynthesizeObjcInternalStruct(ClassDecl, ResultStr);
340
Steve Naroff1ccf4632007-10-30 03:43:13 +0000341 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
Steve Naroffef20ed32007-10-30 02:23:23 +0000342 ResultStr.c_str(), ResultStr.size());
343
Steve Naroff667f1682007-10-30 13:30:57 +0000344 RewriteMethods(ClassDecl->getNumInstanceMethods(),
345 ClassDecl->getInstanceMethods());
346 RewriteMethods(ClassDecl->getNumClassMethods(),
347 ClassDecl->getClassMethods());
Steve Naroff3774dd92007-10-26 20:53:56 +0000348
Steve Naroff1ccf4632007-10-30 03:43:13 +0000349 // Lastly, comment out the @end.
350 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroff3774dd92007-10-26 20:53:56 +0000351}
352
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000353//===----------------------------------------------------------------------===//
354// Function Body / Expression rewriting
355//===----------------------------------------------------------------------===//
356
Chris Lattner0021f452007-10-24 16:57:36 +0000357Stmt *RewriteTest::RewriteFunctionBody(Stmt *S) {
Chris Lattner6fe8b272007-10-16 22:36:42 +0000358 // Otherwise, just rewrite all children.
359 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
360 CI != E; ++CI)
Chris Lattnere33506b2007-10-17 21:28:00 +0000361 if (*CI)
Chris Lattner0021f452007-10-24 16:57:36 +0000362 *CI = RewriteFunctionBody(*CI);
Steve Naroffe9780582007-10-23 23:50:29 +0000363
364 // Handle specific things.
365 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
366 return RewriteAtEncode(AtEncode);
367
Steve Naroff71226032007-10-24 22:48:43 +0000368 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
369 // Before we rewrite it, put the original message expression in a comment.
370 SourceLocation startLoc = MessExpr->getLocStart();
371 SourceLocation endLoc = MessExpr->getLocEnd();
372
373 const char *startBuf = SM->getCharacterData(startLoc);
374 const char *endBuf = SM->getCharacterData(endLoc);
375
376 std::string messString;
377 messString += "// ";
378 messString.append(startBuf, endBuf-startBuf+1);
379 messString += "\n";
Steve Naroff3774dd92007-10-26 20:53:56 +0000380
Steve Naroff71226032007-10-24 22:48:43 +0000381 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
382 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
383 // Tried this, but it didn't work either...
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000384 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffe9780582007-10-23 23:50:29 +0000385 return RewriteMessageExpr(MessExpr);
Steve Naroff71226032007-10-24 22:48:43 +0000386 }
Chris Lattner0021f452007-10-24 16:57:36 +0000387 // Return this stmt unmodified.
388 return S;
Chris Lattner6fe8b272007-10-16 22:36:42 +0000389}
Fariborz Jahanian45d52f72007-10-18 22:09:03 +0000390
Chris Lattner0021f452007-10-24 16:57:36 +0000391Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattnerbf0bfa62007-10-17 22:35:30 +0000392 // Create a new string expression.
393 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson36f07d82007-10-29 05:01:08 +0000394 std::string StrEncoding;
395 Context->getObjcEncodingForType(Exp->getEncodedType(), StrEncoding);
396 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
397 StrEncoding.length(), false, StrType,
Chris Lattnerbf0bfa62007-10-17 22:35:30 +0000398 SourceLocation(), SourceLocation());
399 Rewrite.ReplaceStmt(Exp, Replacement);
Chris Lattner0021f452007-10-24 16:57:36 +0000400 delete Exp;
401 return Replacement;
Chris Lattner6fe8b272007-10-16 22:36:42 +0000402}
403
Steve Naroff71226032007-10-24 22:48:43 +0000404CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
405 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffe9780582007-10-23 23:50:29 +0000406 // Get the type, we will need to reference it in a couple spots.
Steve Naroff71226032007-10-24 22:48:43 +0000407 QualType msgSendType = FD->getType();
Steve Naroffe9780582007-10-23 23:50:29 +0000408
409 // Create a reference to the objc_msgSend() declaration.
Steve Naroff71226032007-10-24 22:48:43 +0000410 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffe9780582007-10-23 23:50:29 +0000411
412 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000413 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffe9780582007-10-23 23:50:29 +0000414 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
415
416 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattner0021f452007-10-24 16:57:36 +0000417
Steve Naroff71226032007-10-24 22:48:43 +0000418 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
419}
420
Steve Naroffc8a92d12007-11-01 13:24:47 +0000421static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
422 const char *&startRef, const char *&endRef) {
423 while (startBuf < endBuf) {
424 if (*startBuf == '<')
425 startRef = startBuf; // mark the start.
426 if (*startBuf == '>') {
427 assert((startRef && *startRef == '<') && "rewrite scanning error");
428 endRef = startBuf; // mark the end.
429 return true;
430 }
431 startBuf++;
432 }
433 return false;
434}
435
436bool RewriteTest::needToScanForQualifiers(QualType T) {
437 // FIXME: we don't currently represent "id <Protocol>" in the type system.
438 if (T == Context->getObjcIdType())
439 return true;
440
441 if (const PointerType *pType = T->getAsPointerType()) {
Steve Naroff05d6ff52007-10-31 04:38:33 +0000442 Type *pointeeType = pType->getPointeeType().getTypePtr();
443 if (isa<ObjcQualifiedInterfaceType>(pointeeType))
444 return true; // we have "Class <Protocol> *".
445 }
Steve Naroffc8a92d12007-11-01 13:24:47 +0000446 return false;
447}
448
449void RewriteTest::RewriteObjcQualifiedInterfaceTypes(
450 const FunctionTypeProto *proto, FunctionDecl *FD) {
451
452 if (needToScanForQualifiers(proto->getResultType())) {
453 // Since types are unique, we need to scan the buffer.
454 SourceLocation Loc = FD->getLocation();
455
456 const char *endBuf = SM->getCharacterData(Loc);
457 const char *startBuf = endBuf;
458 while (*startBuf != ';')
459 startBuf--; // scan backward (from the decl location) for return type.
460 const char *startRef = 0, *endRef = 0;
461 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
462 // Get the locations of the startRef, endRef.
463 SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf);
464 SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1);
465 // Comment out the protocol references.
466 Rewrite.ReplaceText(LessLoc, 0, "/*", 2);
467 Rewrite.ReplaceText(GreaterLoc, 0, "*/", 2);
Steve Naroff05d6ff52007-10-31 04:38:33 +0000468 }
469 }
Steve Naroffc8a92d12007-11-01 13:24:47 +0000470 // Now check arguments.
471 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
472 if (needToScanForQualifiers(proto->getArgType(i))) {
473 // Since types are unique, we need to scan the buffer.
474 SourceLocation Loc = FD->getLocation();
475
476 const char *startBuf = SM->getCharacterData(Loc);
477 const char *endBuf = startBuf;
478 while (*endBuf != ';')
479 endBuf++; // scan forward (from the decl location) for argument types.
480 const char *startRef = 0, *endRef = 0;
481 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
482 // Get the locations of the startRef, endRef.
483 SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf);
484 SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1);
485 // Comment out the protocol references.
486 Rewrite.ReplaceText(LessLoc, 0, "/*", 2);
487 Rewrite.ReplaceText(GreaterLoc, 0, "*/", 2);
488 }
489 }
490 }
Steve Naroff05d6ff52007-10-31 04:38:33 +0000491}
492
Steve Naroff02a82aa2007-10-30 23:14:51 +0000493void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
494 // declared in <objc/objc.h>
Steve Naroff05d6ff52007-10-31 04:38:33 +0000495 if (strcmp(FD->getName(), "sel_getUid") == 0) {
Steve Naroff02a82aa2007-10-30 23:14:51 +0000496 SelGetUidFunctionDecl = FD;
Steve Naroff05d6ff52007-10-31 04:38:33 +0000497 return;
498 }
499 // Check for ObjC 'id' and class types that have been adorned with protocol
500 // information (id<p>, C<p>*). The protocol references need to be rewritten!
501 const FunctionType *funcType = FD->getType()->getAsFunctionType();
502 assert(funcType && "missing function type");
Steve Naroffc8a92d12007-11-01 13:24:47 +0000503 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcType))
504 RewriteObjcQualifiedInterfaceTypes(proto, FD);
Steve Naroff02a82aa2007-10-30 23:14:51 +0000505}
506
507// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
508void RewriteTest::SynthMsgSendFunctionDecl() {
509 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
510 llvm::SmallVector<QualType, 16> ArgTys;
511 QualType argT = Context->getObjcIdType();
512 assert(!argT.isNull() && "Can't find 'id' type");
513 ArgTys.push_back(argT);
514 argT = Context->getObjcSelType();
515 assert(!argT.isNull() && "Can't find 'SEL' type");
516 ArgTys.push_back(argT);
517 QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(),
518 &ArgTys[0], ArgTys.size(),
519 true /*isVariadic*/);
520 MsgSendFunctionDecl = new FunctionDecl(SourceLocation(),
521 msgSendIdent, msgSendType,
522 FunctionDecl::Extern, false, 0);
523}
524
525// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
526void RewriteTest::SynthGetClassFunctionDecl() {
527 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
528 llvm::SmallVector<QualType, 16> ArgTys;
529 ArgTys.push_back(Context->getPointerType(
530 Context->CharTy.getQualifiedType(QualType::Const)));
531 QualType getClassType = Context->getFunctionType(Context->getObjcIdType(),
532 &ArgTys[0], ArgTys.size(),
533 false /*isVariadic*/);
534 GetClassFunctionDecl = new FunctionDecl(SourceLocation(),
535 getClassIdent, getClassType,
536 FunctionDecl::Extern, false, 0);
537}
538
Steve Naroff71226032007-10-24 22:48:43 +0000539Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
Steve Naroff71226032007-10-24 22:48:43 +0000540 assert(SelGetUidFunctionDecl && "Can't find sel_getUid() decl");
Steve Naroff02a82aa2007-10-30 23:14:51 +0000541 if (!MsgSendFunctionDecl)
542 SynthMsgSendFunctionDecl();
543 if (!GetClassFunctionDecl)
544 SynthGetClassFunctionDecl();
Steve Naroff71226032007-10-24 22:48:43 +0000545
546 // Synthesize a call to objc_msgSend().
547 llvm::SmallVector<Expr*, 8> MsgExprs;
548 IdentifierInfo *clsName = Exp->getClassName();
549
550 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
551 if (clsName) { // class message.
552 llvm::SmallVector<Expr*, 8> ClsExprs;
553 QualType argType = Context->getPointerType(Context->CharTy);
554 ClsExprs.push_back(new StringLiteral(clsName->getName(),
555 clsName->getLength(),
556 false, argType, SourceLocation(),
557 SourceLocation()));
558 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
559 &ClsExprs[0], ClsExprs.size());
560 MsgExprs.push_back(Cls);
561 } else // instance message.
562 MsgExprs.push_back(Exp->getReceiver());
563
564 // Create a call to sel_getUid("selName"), it will be the 2nd argument.
565 llvm::SmallVector<Expr*, 8> SelExprs;
566 QualType argType = Context->getPointerType(Context->CharTy);
567 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
568 Exp->getSelector().getName().size(),
569 false, argType, SourceLocation(),
570 SourceLocation()));
571 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
572 &SelExprs[0], SelExprs.size());
573 MsgExprs.push_back(SelExp);
574
575 // Now push any user supplied arguments.
576 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
577 MsgExprs.push_back(Exp->getArg(i));
578 // We've transferred the ownership to MsgExprs. Null out the argument in
579 // the original expression, since we will delete it below.
580 Exp->setArg(i, 0);
581 }
582 CallExpr *MessExp = SynthesizeCallToFunctionDecl(MsgSendFunctionDecl,
583 &MsgExprs[0], MsgExprs.size());
584 // Now do the actual rewrite.
585 Rewrite.ReplaceStmt(Exp, MessExp);
586
Chris Lattner0021f452007-10-24 16:57:36 +0000587 delete Exp;
Steve Naroff71226032007-10-24 22:48:43 +0000588 return MessExp;
Steve Naroffe9780582007-10-23 23:50:29 +0000589}
590
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000591/// SynthesizeObjcInternalStruct - Rewrite one internal struct corresponding to
592/// an objective-c class with ivars.
593void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
594 std::string &Result) {
595 assert(CDecl && "Class missing in SynthesizeObjcInternalStruct");
596 assert(CDecl->getName() && "Name missing in SynthesizeObjcInternalStruct");
Fariborz Jahanianfb4f6a32007-10-31 23:08:24 +0000597 // Do not synthesize more than once.
598 if (ObjcSynthesizedStructs.count(CDecl))
599 return;
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000600 ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass();
601 if (RCDecl && !ObjcSynthesizedStructs.count(RCDecl)) {
602 // Do it for the root
603 SynthesizeObjcInternalStruct(RCDecl, Result);
604 }
605
606 int NumIvars = CDecl->getIntfDeclNumIvars();
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000607 // If no ivars and no root or if its root, directly or indirectly,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000608 // have no ivars (thus not synthesized) then no need to synthesize this class.
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000609 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
610 return;
611
Steve Naroffb1147382007-11-01 17:12:31 +0000612 Result += "\nstruct ";
613 Result += CDecl->getName();
614 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
615 Result += " {\n struct ";
616 Result += RCDecl->getName();
617 Result += " _";
618 Result += RCDecl->getName();
619 Result += ";\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000620 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000621 else
622 Result += " {";
Steve Naroff809b4f02007-10-31 22:11:35 +0000623
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000624 if (NumIvars > 0) {
625 SourceLocation LocStart = CDecl->getLocStart();
626 SourceLocation LocEnd = CDecl->getLocEnd();
627
628 const char *startBuf = SM->getCharacterData(LocStart);
629 const char *endBuf = SM->getCharacterData(LocEnd);
630 startBuf = strchr(startBuf, '{');
631 assert((startBuf && endBuf)
632 && "SynthesizeObjcInternalStruct - malformed @interface");
633 startBuf++; // past '{'
634 while (startBuf < endBuf) {
635 if (*startBuf == '@') {
636 startBuf = strchr(startBuf, 'p');
637 // FIXME: presence of @public, etc. inside comment results in
638 // this transformation as well, which is still correct c-code.
639 if (!strncmp(startBuf, "public", strlen("public"))) {
640 startBuf += strlen("public");
641 Result += "/* @public */";
642 }
643 else if (!strncmp(startBuf, "private", strlen("private"))) {
644 startBuf += strlen("private");
645 Result += "/* @private */";
646 }
647 else if (!strncmp(startBuf, "protected", strlen("protected"))) {
648 startBuf += strlen("protected");
649 Result += "/* @protected */";
650 }
651 }
652 Result += *startBuf++;
653 }
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000654 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000655
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000656 Result += "};\n";
657 // Mark this struct as having been generated.
658 if (!ObjcSynthesizedStructs.insert(CDecl))
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000659 assert(false && "struct already synthesize- SynthesizeObjcInternalStruct");
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000660}
661
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000662// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
663/// class methods.
664void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
665 int NumMethods,
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000666 bool IsInstanceMethod,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000667 const char *prefix,
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000668 const char *ClassName,
669 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000670 static bool objc_impl_method = false;
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000671 if (NumMethods > 0 && !objc_impl_method) {
672 /* struct _objc_method {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000673 SEL _cmd;
674 char *method_types;
675 void *_imp;
676 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000677 */
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000678 Result += "\nstruct _objc_method {\n";
679 Result += "\tSEL _cmd;\n";
680 Result += "\tchar *method_types;\n";
681 Result += "\tvoid *_imp;\n";
682 Result += "};\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000683
684 /* struct _objc_method_list {
685 struct _objc_method_list *next_method;
686 int method_count;
687 struct _objc_method method_list[];
688 }
689 */
690 Result += "\nstruct _objc_method_list {\n";
691 Result += "\tstruct _objc_method_list *next_method;\n";
692 Result += "\tint method_count;\n";
693 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000694 objc_impl_method = true;
Fariborz Jahanian96b55da2007-10-19 00:36:46 +0000695 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000696 // Build _objc_method_list for class's methods if needed
697 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000698 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000699 Result += prefix;
700 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
701 Result += "_METHODS_";
702 Result += ClassName;
703 Result += " __attribute__ ((section (\"__OBJC, __";
704 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000705 Result += "_meth\")))= ";
706 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
707
708 Result += "\t,{{(SEL)\"";
709 Result += Methods[0]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000710 std::string MethodTypeString;
711 Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
712 Result += "\", \"";
713 Result += MethodTypeString;
714 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000715 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000716 // TODO: Need method address as 3rd initializer.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000717 Result += "\t ,{(SEL)\"";
718 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000719 std::string MethodTypeString;
720 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
721 Result += "\", \"";
722 Result += MethodTypeString;
723 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000724 }
725 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000726 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000727}
728
729/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
730void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
731 int NumProtocols,
732 const char *prefix,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000733 const char *ClassName,
734 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000735 static bool objc_protocol_methods = false;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000736 if (NumProtocols > 0) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000737 for (int i = 0; i < NumProtocols; i++) {
738 ObjcProtocolDecl *PDecl = Protocols[i];
739 // Output struct protocol_methods holder of method selector and type.
740 if (!objc_protocol_methods &&
741 (PDecl->getNumInstanceMethods() > 0
742 || PDecl->getNumClassMethods() > 0)) {
743 /* struct protocol_methods {
744 SEL _cmd;
745 char *method_types;
746 }
747 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000748 Result += "\nstruct protocol_methods {\n";
749 Result += "\tSEL _cmd;\n";
750 Result += "\tchar *method_types;\n";
751 Result += "};\n";
752
753 /* struct _objc_protocol_method_list {
754 int protocol_method_count;
755 struct protocol_methods protocols[];
756 }
757 */
758 Result += "\nstruct _objc_protocol_method_list {\n";
759 Result += "\tint protocol_method_count;\n";
760 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000761 objc_protocol_methods = true;
762 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000763
Fariborz Jahanian04455192007-10-22 21:41:37 +0000764 // Output instance methods declared in this protocol.
Fariborz Jahanian04455192007-10-22 21:41:37 +0000765 int NumMethods = PDecl->getNumInstanceMethods();
766 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000767 Result += "\nstatic struct _objc_protocol_method_list "
768 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
769 Result += PDecl->getName();
770 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
771 "{\n\t" + utostr(NumMethods) + "\n";
772
Fariborz Jahanian04455192007-10-22 21:41:37 +0000773 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000774 Result += "\t,{{(SEL)\"";
775 Result += Methods[0]->getSelector().getName().c_str();
776 Result += "\", \"\"}\n";
777
778 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000779 Result += "\t ,{(SEL)\"";
780 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000781 std::string MethodTypeString;
782 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
783 Result += "\", \"";
784 Result += MethodTypeString;
785 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000786 }
787 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000788 }
789
790 // Output class methods declared in this protocol.
791 NumMethods = PDecl->getNumClassMethods();
792 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000793 Result += "\nstatic struct _objc_protocol_method_list "
794 "_OBJC_PROTOCOL_CLASS_METHODS_";
795 Result += PDecl->getName();
796 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
797 "{\n\t";
798 Result += utostr(NumMethods);
799 Result += "\n";
800
Fariborz Jahanian04455192007-10-22 21:41:37 +0000801 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000802 Result += "\t,{{(SEL)\"";
803 Result += Methods[0]->getSelector().getName().c_str();
804 Result += "\", \"\"}\n";
805
806 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000807 Result += "\t ,{(SEL)\"";
808 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000809 std::string MethodTypeString;
810 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
811 Result += "\", \"";
812 Result += MethodTypeString;
813 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000814 }
815 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000816 }
817 // Output:
818 /* struct _objc_protocol {
819 // Objective-C 1.0 extensions
820 struct _objc_protocol_extension *isa;
821 char *protocol_name;
822 struct _objc_protocol **protocol_list;
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000823 struct _objc_protocol_method_list *instance_methods;
824 struct _objc_protocol_method_list *class_methods;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000825 };
826 */
827 static bool objc_protocol = false;
828 if (!objc_protocol) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000829 Result += "\nstruct _objc_protocol {\n";
830 Result += "\tstruct _objc_protocol_extension *isa;\n";
831 Result += "\tchar *protocol_name;\n";
832 Result += "\tstruct _objc_protocol **protocol_list;\n";
833 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
834 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
835 Result += "};\n";
836
837 /* struct _objc_protocol_list {
838 struct _objc_protocol_list *next;
839 int protocol_count;
840 struct _objc_protocol *class_protocols[];
841 }
842 */
843 Result += "\nstruct _objc_protocol_list {\n";
844 Result += "\tstruct _objc_protocol_list *next;\n";
845 Result += "\tint protocol_count;\n";
846 Result += "\tstruct _objc_protocol *class_protocols[];\n";
847 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000848 objc_protocol = true;
849 }
850
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000851 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
852 Result += PDecl->getName();
853 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
854 "{\n\t0, \"";
855 Result += PDecl->getName();
856 Result += "\", 0, ";
857 if (PDecl->getInstanceMethods() > 0) {
858 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
859 Result += PDecl->getName();
860 Result += ", ";
861 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000862 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000863 Result += "0, ";
864 if (PDecl->getClassMethods() > 0) {
865 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
866 Result += PDecl->getName();
867 Result += "\n";
868 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000869 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000870 Result += "0\n";
871 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000872 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000873 // Output the top lovel protocol meta-data for the class.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000874 Result += "\nstatic struct _objc_protocol_list _OBJC_";
875 Result += prefix;
876 Result += "_PROTOCOLS_";
877 Result += ClassName;
878 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
879 "{\n\t0, ";
880 Result += utostr(NumProtocols);
881 Result += "\n";
882
883 Result += "\t,{&_OBJC_PROTOCOL_";
884 Result += Protocols[0]->getName();
885 Result += " \n";
886
887 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000888 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000889 Result += "\t ,&_OBJC_PROTOCOL_";
890 Result += PDecl->getName();
891 Result += "\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000892 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000893 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000894 }
895}
896
897/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
898/// implementation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000899void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
900 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000901 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
902 // Find category declaration for this implementation.
903 ObjcCategoryDecl *CDecl;
904 for (CDecl = ClassDecl->getCategoryList(); CDecl;
905 CDecl = CDecl->getNextClassCategory())
906 if (CDecl->getIdentifier() == IDecl->getIdentifier())
907 break;
908 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
909
910 char *FullCategoryName = (char*)alloca(
911 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
912 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
913
914 // Build _objc_method_list for class's instance methods if needed
915 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
916 IDecl->getNumInstanceMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000917 true,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000918 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000919
920 // Build _objc_method_list for class's class methods if needed
921 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
922 IDecl->getNumClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000923 false,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000924 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000925
926 // Protocols referenced in class declaration?
927 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
928 CDecl->getNumReferencedProtocols(),
929 "CATEGORY",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000930 FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000931
932 /* struct _objc_category {
933 char *category_name;
934 char *class_name;
935 struct _objc_method_list *instance_methods;
936 struct _objc_method_list *class_methods;
937 struct _objc_protocol_list *protocols;
938 // Objective-C 1.0 extensions
939 uint32_t size; // sizeof (struct _objc_category)
940 struct _objc_property_list *instance_properties; // category's own
941 // @property decl.
942 };
943 */
944
945 static bool objc_category = false;
946 if (!objc_category) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000947 Result += "\nstruct _objc_category {\n";
948 Result += "\tchar *category_name;\n";
949 Result += "\tchar *class_name;\n";
950 Result += "\tstruct _objc_method_list *instance_methods;\n";
951 Result += "\tstruct _objc_method_list *class_methods;\n";
952 Result += "\tstruct _objc_protocol_list *protocols;\n";
953 Result += "\tunsigned int size;\n";
954 Result += "\tstruct _objc_property_list *instance_properties;\n";
955 Result += "};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000956 objc_category = true;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000957 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000958 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
959 Result += FullCategoryName;
960 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
961 Result += IDecl->getName();
962 Result += "\"\n\t, \"";
963 Result += ClassDecl->getName();
964 Result += "\"\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000965
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000966 if (IDecl->getNumInstanceMethods() > 0) {
967 Result += "\t, (struct _objc_method_list *)"
968 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
969 Result += FullCategoryName;
970 Result += "\n";
971 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000972 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000973 Result += "\t, 0\n";
974 if (IDecl->getNumClassMethods() > 0) {
975 Result += "\t, (struct _objc_method_list *)"
976 "&_OBJC_CATEGORY_CLASS_METHODS_";
977 Result += FullCategoryName;
978 Result += "\n";
979 }
980 else
981 Result += "\t, 0\n";
982
983 if (CDecl->getNumReferencedProtocols() > 0) {
984 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
985 Result += FullCategoryName;
986 Result += "\n";
987 }
988 else
989 Result += "\t, 0\n";
990 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000991}
992
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000993/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
994/// ivar offset.
995void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
996 ObjcIvarDecl *ivar,
997 std::string &Result) {
998 Result += "offsetof(struct _interface_";
999 Result += IDecl->getName();
1000 Result += ", ";
1001 Result += ivar->getName();
1002 Result += ")";
1003}
1004
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001005//===----------------------------------------------------------------------===//
1006// Meta Data Emission
1007//===----------------------------------------------------------------------===//
1008
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001009void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
1010 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001011 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
1012
1013 // Build _objc_ivar_list metadata for classes ivars if needed
1014 int NumIvars = IDecl->getImplDeclNumIvars() > 0
1015 ? IDecl->getImplDeclNumIvars()
1016 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
1017
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00001018 SynthesizeObjcInternalStruct(CDecl, Result);
1019
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001020 if (NumIvars > 0) {
1021 static bool objc_ivar = false;
1022 if (!objc_ivar) {
1023 /* struct _objc_ivar {
1024 char *ivar_name;
1025 char *ivar_type;
1026 int ivar_offset;
1027 };
1028 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001029 Result += "\nstruct _objc_ivar {\n";
1030 Result += "\tchar *ivar_name;\n";
1031 Result += "\tchar *ivar_type;\n";
1032 Result += "\tint ivar_offset;\n";
1033 Result += "};\n";
1034
1035 /* struct _objc_ivar_list {
1036 int ivar_count;
1037 struct _objc_ivar ivar_list[];
1038 };
1039 */
1040 Result += "\nstruct _objc_ivar_list {\n";
1041 Result += "\tint ivar_count;\n";
1042 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001043 objc_ivar = true;
1044 }
1045
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001046 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
1047 Result += IDecl->getName();
1048 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
1049 "{\n\t";
1050 Result += utostr(NumIvars);
1051 Result += "\n";
1052
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001053 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
1054 ? IDecl->getImplDeclIVars()
1055 : CDecl->getIntfDeclIvars();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001056 Result += "\t,{{\"";
1057 Result += Ivars[0]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001058 Result += "\", \"";
1059 std::string StrEncoding;
1060 Context->getObjcEncodingForType(Ivars[0]->getType(), StrEncoding);
1061 Result += StrEncoding;
1062 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001063 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
1064 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001065 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001066 Result += "\t ,{\"";
1067 Result += Ivars[i]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001068 Result += "\", \"";
1069 std::string StrEncoding;
1070 Context->getObjcEncodingForType(Ivars[i]->getType(), StrEncoding);
1071 Result += StrEncoding;
1072 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001073 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
1074 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001075 }
1076
1077 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001078 }
1079
1080 // Build _objc_method_list for class's instance methods if needed
1081 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1082 IDecl->getNumInstanceMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001083 true,
1084 "", IDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001085
1086 // Build _objc_method_list for class's class methods if needed
1087 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +00001088 IDecl->getNumClassMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001089 false,
1090 "", IDecl->getName(), Result);
1091
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001092 // Protocols referenced in class declaration?
1093 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1094 CDecl->getNumIntfRefProtocols(),
1095 "CLASS",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001096 CDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001097
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001098
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001099 // Declaration of class/meta-class metadata
1100 /* struct _objc_class {
1101 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001102 const char *super_class_name;
1103 char *name;
1104 long version;
1105 long info;
1106 long instance_size;
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001107 struct _objc_ivar_list *ivars;
1108 struct _objc_method_list *methods;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001109 struct objc_cache *cache;
1110 struct objc_protocol_list *protocols;
1111 const char *ivar_layout;
1112 struct _objc_class_ext *ext;
1113 };
1114 */
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001115 static bool objc_class = false;
1116 if (!objc_class) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001117 Result += "\nstruct _objc_class {\n";
1118 Result += "\tstruct _objc_class *isa;\n";
1119 Result += "\tconst char *super_class_name;\n";
1120 Result += "\tchar *name;\n";
1121 Result += "\tlong version;\n";
1122 Result += "\tlong info;\n";
1123 Result += "\tlong instance_size;\n";
1124 Result += "\tstruct _objc_ivar_list *ivars;\n";
1125 Result += "\tstruct _objc_method_list *methods;\n";
1126 Result += "\tstruct objc_cache *cache;\n";
1127 Result += "\tstruct _objc_protocol_list *protocols;\n";
1128 Result += "\tconst char *ivar_layout;\n";
1129 Result += "\tstruct _objc_class_ext *ext;\n";
1130 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001131 objc_class = true;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001132 }
1133
1134 // Meta-class metadata generation.
1135 ObjcInterfaceDecl *RootClass = 0;
1136 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
1137 while (SuperClass) {
1138 RootClass = SuperClass;
1139 SuperClass = SuperClass->getSuperClass();
1140 }
1141 SuperClass = CDecl->getSuperClass();
1142
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001143 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
1144 Result += CDecl->getName();
1145 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
1146 "{\n\t(struct _objc_class *)\"";
1147 Result += (RootClass ? RootClass->getName() : CDecl->getName());
1148 Result += "\"";
1149
1150 if (SuperClass) {
1151 Result += ", \"";
1152 Result += SuperClass->getName();
1153 Result += "\", \"";
1154 Result += CDecl->getName();
1155 Result += "\"";
1156 }
1157 else {
1158 Result += ", 0, \"";
1159 Result += CDecl->getName();
1160 Result += "\"";
1161 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001162 // TODO: 'ivars' field for root class is currently set to 0.
1163 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001164 Result += ", 0,2, sizeof(struct _objc_class), 0";
1165 if (CDecl->getNumClassMethods() > 0) {
1166 Result += "\n\t, &_OBJC_CLASS_METHODS_";
1167 Result += CDecl->getName();
1168 Result += "\n";
1169 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001170 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001171 Result += ", 0\n";
1172 if (CDecl->getNumIntfRefProtocols() > 0) {
1173 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
1174 Result += CDecl->getName();
1175 Result += ",0,0\n";
1176 }
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001177 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001178 Result += "\t,0,0,0,0\n";
1179 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001180
1181 // class metadata generation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001182 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
1183 Result += CDecl->getName();
1184 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
1185 "{\n\t&_OBJC_METACLASS_";
1186 Result += CDecl->getName();
1187 if (SuperClass) {
1188 Result += ", \"";
1189 Result += SuperClass->getName();
1190 Result += "\", \"";
1191 Result += CDecl->getName();
1192 Result += "\"";
1193 }
1194 else {
1195 Result += ", 0, \"";
1196 Result += CDecl->getName();
1197 Result += "\"";
1198 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001199 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00001200 Result += ", 0,1";
1201 if (!ObjcSynthesizedStructs.count(CDecl))
1202 Result += ",0";
1203 else {
1204 // class has size. Must synthesize its size.
1205 Result += ",sizeof(struct _interface_";
1206 Result += CDecl->getName();
1207 Result += ")";
1208 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001209 if (NumIvars > 0) {
1210 Result += ", &_OBJC_INSTANCE_VARIABLES_";
1211 Result += CDecl->getName();
1212 Result += "\n\t";
1213 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001214 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001215 Result += ",0";
1216 if (IDecl->getNumInstanceMethods() > 0) {
1217 Result += ", &_OBJC_INSTANCE_METHODS_";
1218 Result += CDecl->getName();
1219 Result += ", 0\n\t";
1220 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001221 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001222 Result += ",0,0";
1223 if (CDecl->getNumIntfRefProtocols() > 0) {
1224 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1225 Result += CDecl->getName();
1226 Result += ", 0,0\n";
1227 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001228 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001229 Result += ",0,0,0\n";
1230 Result += "};\n";
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001231}
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001232
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001233void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001234 int ClsDefCount = ClassImplementation.size();
1235 int CatDefCount = CategoryImplementation.size();
1236 if (ClsDefCount == 0 && CatDefCount == 0)
1237 return;
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001238
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001239 // TODO: This is temporary until we decide how to access objc types in a
1240 // c program
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001241 Result += "#include <Objc/objc.h>\n";
1242 // This is needed for use of offsetof
1243 Result += "#include <stddef.h>\n";
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001244
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001245 // For each implemented class, write out all its meta data.
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001246 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001247 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001248
1249 // For each implemented category, write out all its meta data.
1250 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001251 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001252
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001253 // Write objc_symtab metadata
1254 /*
1255 struct _objc_symtab
1256 {
1257 long sel_ref_cnt;
1258 SEL *refs;
1259 short cls_def_cnt;
1260 short cat_def_cnt;
1261 void *defs[cls_def_cnt + cat_def_cnt];
1262 };
1263 */
1264
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001265 Result += "\nstruct _objc_symtab {\n";
1266 Result += "\tlong sel_ref_cnt;\n";
1267 Result += "\tSEL *refs;\n";
1268 Result += "\tshort cls_def_cnt;\n";
1269 Result += "\tshort cat_def_cnt;\n";
1270 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1271 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001272
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001273 Result += "static struct _objc_symtab "
1274 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1275 Result += "\t0, 0, " + utostr(ClsDefCount)
1276 + ", " + utostr(CatDefCount) + "\n";
1277 for (int i = 0; i < ClsDefCount; i++) {
1278 Result += "\t,&_OBJC_CLASS_";
1279 Result += ClassImplementation[i]->getName();
1280 Result += "\n";
1281 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001282
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001283 for (int i = 0; i < CatDefCount; i++) {
1284 Result += "\t,&_OBJC_CATEGORY_";
1285 Result += CategoryImplementation[i]->getClassInterface()->getName();
1286 Result += "_";
1287 Result += CategoryImplementation[i]->getName();
1288 Result += "\n";
1289 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001290
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001291 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001292
1293 // Write objc_module metadata
1294
1295 /*
1296 struct _objc_module {
1297 long version;
1298 long size;
1299 const char *name;
1300 struct _objc_symtab *symtab;
1301 }
1302 */
1303
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001304 Result += "\nstruct _objc_module {\n";
1305 Result += "\tlong version;\n";
1306 Result += "\tlong size;\n";
1307 Result += "\tconst char *name;\n";
1308 Result += "\tstruct _objc_symtab *symtab;\n";
1309 Result += "};\n\n";
1310 Result += "static struct _objc_module "
1311 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001312 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1313 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001314 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001315}
Chris Lattner6fe8b272007-10-16 22:36:42 +00001316