blob: 55fb4d7565dfc14ae7b505a55e12e2b70b64f097 [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 Naroff05d6ff52007-10-31 04:38:33 +000068 bool functionReferencesAnyObjcQualifiedInterfaceTypes(
69 const FunctionTypeProto *proto);
Chris Lattner6fe8b272007-10-16 22:36:42 +000070
Chris Lattnerfce2c5a2007-10-24 17:06:59 +000071 // Expression Rewriting.
Chris Lattner0021f452007-10-24 16:57:36 +000072 Stmt *RewriteFunctionBody(Stmt *S);
73 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
74 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroff71226032007-10-24 22:48:43 +000075 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
76 Expr **args, unsigned nargs);
Steve Naroff02a82aa2007-10-30 23:14:51 +000077 void SynthMsgSendFunctionDecl();
78 void SynthGetClassFunctionDecl();
79
Chris Lattnerfce2c5a2007-10-24 17:06:59 +000080 // Metadata emission.
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000081 void HandleObjcMetaDataEmission();
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
164 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
165 // we are done.
166 if (const RewriteBuffer *RewriteBuf =
167 Rewrite.getRewriteBufferFor(MainFileID)) {
168 printf("Changed:\n");
169 std::string S(RewriteBuf->begin(), RewriteBuf->end());
170 printf("%s\n", S.c_str());
171 } else {
172 printf("No changes\n");
173 }
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000174
175}
176
177/// HandleObjcMetaDataEmission - main routine to generate objective-c's
178/// metadata.
179void RewriteTest::HandleObjcMetaDataEmission() {
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000180 // Rewrite Objective-c meta data*
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000181 std::string ResultStr;
182 WriteObjcMetaData(ResultStr);
183 // For now just print the string out.
184 printf("%s", ResultStr.c_str());
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000185}
186
187//===----------------------------------------------------------------------===//
188// Syntactic (non-AST) Rewriting Code
189//===----------------------------------------------------------------------===//
190
Chris Lattner74db1682007-10-16 21:07:07 +0000191void RewriteTest::RewriteInclude(SourceLocation Loc) {
192 // Rip up the #include stack to the main file.
193 SourceLocation IncLoc = Loc, NextLoc = Loc;
194 do {
195 IncLoc = Loc;
196 Loc = SM->getLogicalLoc(NextLoc);
197 NextLoc = SM->getIncludeLoc(Loc);
198 } while (!NextLoc.isInvalid());
199
200 // Loc is now the location of the #include filename "foo" or <foo/bar.h>.
201 // IncLoc indicates the header that was included if it is useful.
202 IncLoc = SM->getLogicalLoc(IncLoc);
203 if (SM->getDecomposedFileLoc(Loc).first != MainFileID ||
204 Loc == LastIncLoc)
205 return;
206 LastIncLoc = Loc;
207
208 unsigned IncCol = SM->getColumnNumber(Loc);
209 SourceLocation LineStartLoc = Loc.getFileLocWithOffset(-IncCol+1);
210
211 // Replace the #import with #include.
212 Rewrite.ReplaceText(LineStartLoc, IncCol-1, "#include ", strlen("#include "));
213}
214
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000215void RewriteTest::RewriteTabs() {
216 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
217 const char *MainBufStart = MainBuf.first;
218 const char *MainBufEnd = MainBuf.second;
Fariborz Jahanian640a01f2007-10-18 19:23:00 +0000219
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000220 // Loop over the whole file, looking for tabs.
221 for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
222 if (*BufPtr != '\t')
223 continue;
224
225 // Okay, we found a tab. This tab will turn into at least one character,
226 // but it depends on which 'virtual column' it is in. Compute that now.
227 unsigned VCol = 0;
228 while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
229 BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
230 ++VCol;
231
232 // Okay, now that we know the virtual column, we know how many spaces to
233 // insert. We assume 8-character tab-stops.
234 unsigned Spaces = 8-(VCol & 7);
235
236 // Get the location of the tab.
237 SourceLocation TabLoc =
238 SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
239
240 // Rewrite the single tab character into a sequence of spaces.
241 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
242 }
Chris Lattner569faa62007-10-11 18:38:32 +0000243}
244
245
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000246void RewriteTest::RewriteForwardClassDecl(ObjcClassDecl *ClassDecl) {
247 int numDecls = ClassDecl->getNumForwardDecls();
248 ObjcInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
249
250 // Get the start location and compute the semi location.
251 SourceLocation startLoc = ClassDecl->getLocation();
252 const char *startBuf = SM->getCharacterData(startLoc);
253 const char *semiPtr = strchr(startBuf, ';');
254
255 // Translate to typedef's that forward reference structs with the same name
256 // as the class. As a convenience, we include the original declaration
257 // as a comment.
258 std::string typedefString;
259 typedefString += "// ";
Steve Naroff71226032007-10-24 22:48:43 +0000260 typedefString.append(startBuf, semiPtr-startBuf+1);
261 typedefString += "\n";
262 for (int i = 0; i < numDecls; i++) {
263 ObjcInterfaceDecl *ForwardDecl = ForwardDecls[i];
Steve Naroff809b4f02007-10-31 22:11:35 +0000264 if (ObjcForwardDecls.count(ForwardDecl))
265 continue;
Steve Naroff71226032007-10-24 22:48:43 +0000266 typedefString += "typedef struct ";
267 typedefString += ForwardDecl->getName();
268 typedefString += " ";
269 typedefString += ForwardDecl->getName();
270 typedefString += ";\n";
Steve Naroff809b4f02007-10-31 22:11:35 +0000271
272 // Mark this typedef as having been generated.
273 if (!ObjcForwardDecls.insert(ForwardDecl))
274 assert(true && "typedef already output");
Steve Naroff71226032007-10-24 22:48:43 +0000275 }
276
277 // Replace the @class with typedefs corresponding to the classes.
278 Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1,
279 typedefString.c_str(), typedefString.size());
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000280}
281
Steve Naroff667f1682007-10-30 13:30:57 +0000282void RewriteTest::RewriteMethods(int nMethods, ObjcMethodDecl **Methods) {
283 for (int i = 0; i < nMethods; i++) {
284 ObjcMethodDecl *Method = Methods[i];
285 SourceLocation Loc = Method->getLocStart();
286
287 Rewrite.ReplaceText(Loc, 0, "// ", 3);
288
289 // FIXME: handle methods that are declared across multiple lines.
290 }
291}
292
293void RewriteTest::RewriteCategoryDecl(ObjcCategoryDecl *CatDecl) {
294 SourceLocation LocStart = CatDecl->getLocStart();
295
296 // FIXME: handle category headers that are declared across multiple lines.
297 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
298
299 RewriteMethods(CatDecl->getNumInstanceMethods(),
300 CatDecl->getInstanceMethods());
301 RewriteMethods(CatDecl->getNumClassMethods(),
302 CatDecl->getClassMethods());
303 // Lastly, comment out the @end.
304 Rewrite.ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
305}
306
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000307void RewriteTest::RewriteProtocolDecl(ObjcProtocolDecl *PDecl) {
308 SourceLocation LocStart = PDecl->getLocStart();
309
310 // FIXME: handle protocol headers that are declared across multiple lines.
311 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
312
313 RewriteMethods(PDecl->getNumInstanceMethods(),
314 PDecl->getInstanceMethods());
315 RewriteMethods(PDecl->getNumClassMethods(),
316 PDecl->getClassMethods());
317 // Lastly, comment out the @end.
318 Rewrite.ReplaceText(PDecl->getAtEndLoc(), 0, "// ", 3);
319}
320
Steve Naroff3774dd92007-10-26 20:53:56 +0000321void RewriteTest::RewriteInterfaceDecl(ObjcInterfaceDecl *ClassDecl) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000322
323 SourceLocation LocStart = ClassDecl->getLocStart();
324 SourceLocation LocEnd = ClassDecl->getLocEnd();
325
326 const char *startBuf = SM->getCharacterData(LocStart);
327 const char *endBuf = SM->getCharacterData(LocEnd);
328
Steve Naroff1ccf4632007-10-30 03:43:13 +0000329 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
Steve Naroffef20ed32007-10-30 02:23:23 +0000330
331 std::string ResultStr;
332 SynthesizeObjcInternalStruct(ClassDecl, ResultStr);
333
Steve Naroff1ccf4632007-10-30 03:43:13 +0000334 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
Steve Naroffef20ed32007-10-30 02:23:23 +0000335 ResultStr.c_str(), ResultStr.size());
336
Steve Naroff667f1682007-10-30 13:30:57 +0000337 RewriteMethods(ClassDecl->getNumInstanceMethods(),
338 ClassDecl->getInstanceMethods());
339 RewriteMethods(ClassDecl->getNumClassMethods(),
340 ClassDecl->getClassMethods());
Steve Naroff3774dd92007-10-26 20:53:56 +0000341
Steve Naroff1ccf4632007-10-30 03:43:13 +0000342 // Lastly, comment out the @end.
343 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroff3774dd92007-10-26 20:53:56 +0000344}
345
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000346//===----------------------------------------------------------------------===//
347// Function Body / Expression rewriting
348//===----------------------------------------------------------------------===//
349
Chris Lattner0021f452007-10-24 16:57:36 +0000350Stmt *RewriteTest::RewriteFunctionBody(Stmt *S) {
Chris Lattner6fe8b272007-10-16 22:36:42 +0000351 // Otherwise, just rewrite all children.
352 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
353 CI != E; ++CI)
Chris Lattnere33506b2007-10-17 21:28:00 +0000354 if (*CI)
Chris Lattner0021f452007-10-24 16:57:36 +0000355 *CI = RewriteFunctionBody(*CI);
Steve Naroffe9780582007-10-23 23:50:29 +0000356
357 // Handle specific things.
358 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
359 return RewriteAtEncode(AtEncode);
360
Steve Naroff71226032007-10-24 22:48:43 +0000361 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
362 // Before we rewrite it, put the original message expression in a comment.
363 SourceLocation startLoc = MessExpr->getLocStart();
364 SourceLocation endLoc = MessExpr->getLocEnd();
365
366 const char *startBuf = SM->getCharacterData(startLoc);
367 const char *endBuf = SM->getCharacterData(endLoc);
368
369 std::string messString;
370 messString += "// ";
371 messString.append(startBuf, endBuf-startBuf+1);
372 messString += "\n";
Steve Naroff3774dd92007-10-26 20:53:56 +0000373
Steve Naroff71226032007-10-24 22:48:43 +0000374 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
375 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
376 // Tried this, but it didn't work either...
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000377 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffe9780582007-10-23 23:50:29 +0000378 return RewriteMessageExpr(MessExpr);
Steve Naroff71226032007-10-24 22:48:43 +0000379 }
Chris Lattner0021f452007-10-24 16:57:36 +0000380 // Return this stmt unmodified.
381 return S;
Chris Lattner6fe8b272007-10-16 22:36:42 +0000382}
Fariborz Jahanian45d52f72007-10-18 22:09:03 +0000383
Chris Lattner0021f452007-10-24 16:57:36 +0000384Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattnerbf0bfa62007-10-17 22:35:30 +0000385 // Create a new string expression.
386 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson36f07d82007-10-29 05:01:08 +0000387 std::string StrEncoding;
388 Context->getObjcEncodingForType(Exp->getEncodedType(), StrEncoding);
389 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
390 StrEncoding.length(), false, StrType,
Chris Lattnerbf0bfa62007-10-17 22:35:30 +0000391 SourceLocation(), SourceLocation());
392 Rewrite.ReplaceStmt(Exp, Replacement);
Chris Lattner0021f452007-10-24 16:57:36 +0000393 delete Exp;
394 return Replacement;
Chris Lattner6fe8b272007-10-16 22:36:42 +0000395}
396
Steve Naroff71226032007-10-24 22:48:43 +0000397CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
398 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffe9780582007-10-23 23:50:29 +0000399 // Get the type, we will need to reference it in a couple spots.
Steve Naroff71226032007-10-24 22:48:43 +0000400 QualType msgSendType = FD->getType();
Steve Naroffe9780582007-10-23 23:50:29 +0000401
402 // Create a reference to the objc_msgSend() declaration.
Steve Naroff71226032007-10-24 22:48:43 +0000403 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffe9780582007-10-23 23:50:29 +0000404
405 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000406 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffe9780582007-10-23 23:50:29 +0000407 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
408
409 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattner0021f452007-10-24 16:57:36 +0000410
Steve Naroff71226032007-10-24 22:48:43 +0000411 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
412}
413
Steve Naroff05d6ff52007-10-31 04:38:33 +0000414bool RewriteTest::functionReferencesAnyObjcQualifiedInterfaceTypes(
415 const FunctionTypeProto *proto) {
Steve Naroff5b77c212007-10-31 16:03:04 +0000416 QualType resultType = proto->getResultType();
417
418 if (resultType == Context->getObjcIdType()) {
419 // FIXME: we don't currently represent "id <Protocol>" in the type system.
420 // Implement a heuristic here (until we do).
421 } else if (const PointerType *pType = resultType->getAsPointerType()) {
Steve Naroff05d6ff52007-10-31 04:38:33 +0000422 Type *pointeeType = pType->getPointeeType().getTypePtr();
423 if (isa<ObjcQualifiedInterfaceType>(pointeeType))
424 return true; // we have "Class <Protocol> *".
425 }
426 // Now check arguments.
427 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
Steve Naroff5b77c212007-10-31 16:03:04 +0000428 QualType argType = proto->getArgType(i);
429 if (argType == Context->getObjcIdType()) {
430 // FIXME: we don't currently represent "id <Protocol>" in the type system.
431 // Implement a heuristic here (until we do).
432 } else if (const PointerType *pType = argType->getAsPointerType()) {
Steve Naroff05d6ff52007-10-31 04:38:33 +0000433 Type *pointeeType = pType->getPointeeType().getTypePtr();
434 if (isa<ObjcQualifiedInterfaceType>(pointeeType))
435 return true;
436 }
437 }
Steve Naroff05d6ff52007-10-31 04:38:33 +0000438 return false;
439}
440
Steve Naroff02a82aa2007-10-30 23:14:51 +0000441void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
442 // declared in <objc/objc.h>
Steve Naroff05d6ff52007-10-31 04:38:33 +0000443 if (strcmp(FD->getName(), "sel_getUid") == 0) {
Steve Naroff02a82aa2007-10-30 23:14:51 +0000444 SelGetUidFunctionDecl = FD;
Steve Naroff05d6ff52007-10-31 04:38:33 +0000445 return;
446 }
447 // Check for ObjC 'id' and class types that have been adorned with protocol
448 // information (id<p>, C<p>*). The protocol references need to be rewritten!
449 const FunctionType *funcType = FD->getType()->getAsFunctionType();
450 assert(funcType && "missing function type");
451 const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcType);
452 if (proto && functionReferencesAnyObjcQualifiedInterfaceTypes(proto)) {
453 // FIXME: Rewrite function decl...
454 }
Steve Naroff02a82aa2007-10-30 23:14:51 +0000455}
456
457// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
458void RewriteTest::SynthMsgSendFunctionDecl() {
459 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
460 llvm::SmallVector<QualType, 16> ArgTys;
461 QualType argT = Context->getObjcIdType();
462 assert(!argT.isNull() && "Can't find 'id' type");
463 ArgTys.push_back(argT);
464 argT = Context->getObjcSelType();
465 assert(!argT.isNull() && "Can't find 'SEL' type");
466 ArgTys.push_back(argT);
467 QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(),
468 &ArgTys[0], ArgTys.size(),
469 true /*isVariadic*/);
470 MsgSendFunctionDecl = new FunctionDecl(SourceLocation(),
471 msgSendIdent, msgSendType,
472 FunctionDecl::Extern, false, 0);
473}
474
475// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
476void RewriteTest::SynthGetClassFunctionDecl() {
477 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
478 llvm::SmallVector<QualType, 16> ArgTys;
479 ArgTys.push_back(Context->getPointerType(
480 Context->CharTy.getQualifiedType(QualType::Const)));
481 QualType getClassType = Context->getFunctionType(Context->getObjcIdType(),
482 &ArgTys[0], ArgTys.size(),
483 false /*isVariadic*/);
484 GetClassFunctionDecl = new FunctionDecl(SourceLocation(),
485 getClassIdent, getClassType,
486 FunctionDecl::Extern, false, 0);
487}
488
Steve Naroff71226032007-10-24 22:48:43 +0000489Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
Steve Naroff71226032007-10-24 22:48:43 +0000490 assert(SelGetUidFunctionDecl && "Can't find sel_getUid() decl");
Steve Naroff02a82aa2007-10-30 23:14:51 +0000491 if (!MsgSendFunctionDecl)
492 SynthMsgSendFunctionDecl();
493 if (!GetClassFunctionDecl)
494 SynthGetClassFunctionDecl();
Steve Naroff71226032007-10-24 22:48:43 +0000495
496 // Synthesize a call to objc_msgSend().
497 llvm::SmallVector<Expr*, 8> MsgExprs;
498 IdentifierInfo *clsName = Exp->getClassName();
499
500 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
501 if (clsName) { // class message.
502 llvm::SmallVector<Expr*, 8> ClsExprs;
503 QualType argType = Context->getPointerType(Context->CharTy);
504 ClsExprs.push_back(new StringLiteral(clsName->getName(),
505 clsName->getLength(),
506 false, argType, SourceLocation(),
507 SourceLocation()));
508 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
509 &ClsExprs[0], ClsExprs.size());
510 MsgExprs.push_back(Cls);
511 } else // instance message.
512 MsgExprs.push_back(Exp->getReceiver());
513
514 // Create a call to sel_getUid("selName"), it will be the 2nd argument.
515 llvm::SmallVector<Expr*, 8> SelExprs;
516 QualType argType = Context->getPointerType(Context->CharTy);
517 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
518 Exp->getSelector().getName().size(),
519 false, argType, SourceLocation(),
520 SourceLocation()));
521 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
522 &SelExprs[0], SelExprs.size());
523 MsgExprs.push_back(SelExp);
524
525 // Now push any user supplied arguments.
526 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
527 MsgExprs.push_back(Exp->getArg(i));
528 // We've transferred the ownership to MsgExprs. Null out the argument in
529 // the original expression, since we will delete it below.
530 Exp->setArg(i, 0);
531 }
532 CallExpr *MessExp = SynthesizeCallToFunctionDecl(MsgSendFunctionDecl,
533 &MsgExprs[0], MsgExprs.size());
534 // Now do the actual rewrite.
535 Rewrite.ReplaceStmt(Exp, MessExp);
536
Chris Lattner0021f452007-10-24 16:57:36 +0000537 delete Exp;
Steve Naroff71226032007-10-24 22:48:43 +0000538 return MessExp;
Steve Naroffe9780582007-10-23 23:50:29 +0000539}
540
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000541/// SynthesizeObjcInternalStruct - Rewrite one internal struct corresponding to
542/// an objective-c class with ivars.
543void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
544 std::string &Result) {
545 assert(CDecl && "Class missing in SynthesizeObjcInternalStruct");
546 assert(CDecl->getName() && "Name missing in SynthesizeObjcInternalStruct");
547 ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass();
548 if (RCDecl && !ObjcSynthesizedStructs.count(RCDecl)) {
549 // Do it for the root
550 SynthesizeObjcInternalStruct(RCDecl, Result);
551 }
552
553 int NumIvars = CDecl->getIntfDeclNumIvars();
554 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
555 return;
556
557 Result += "\nstruct _interface_";
558 Result += CDecl->getName();
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000559 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000560 Result += " {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000561 Result += "\tstruct _interface_";
562 Result += RCDecl->getName();
563 Result += " _";
564 Result += RCDecl->getName();
565 Result += ";\n";
566 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000567 else
568 Result += " {";
Steve Naroff809b4f02007-10-31 22:11:35 +0000569
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000570 if (NumIvars > 0) {
571 SourceLocation LocStart = CDecl->getLocStart();
572 SourceLocation LocEnd = CDecl->getLocEnd();
573
574 const char *startBuf = SM->getCharacterData(LocStart);
575 const char *endBuf = SM->getCharacterData(LocEnd);
576 startBuf = strchr(startBuf, '{');
577 assert((startBuf && endBuf)
578 && "SynthesizeObjcInternalStruct - malformed @interface");
579 startBuf++; // past '{'
580 while (startBuf < endBuf) {
581 if (*startBuf == '@') {
582 startBuf = strchr(startBuf, 'p');
583 // FIXME: presence of @public, etc. inside comment results in
584 // this transformation as well, which is still correct c-code.
585 if (!strncmp(startBuf, "public", strlen("public"))) {
586 startBuf += strlen("public");
587 Result += "/* @public */";
588 }
589 else if (!strncmp(startBuf, "private", strlen("private"))) {
590 startBuf += strlen("private");
591 Result += "/* @private */";
592 }
593 else if (!strncmp(startBuf, "protected", strlen("protected"))) {
594 startBuf += strlen("protected");
595 Result += "/* @protected */";
596 }
597 }
598 Result += *startBuf++;
599 }
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000600 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000601
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000602 Result += "};\n";
603 // Mark this struct as having been generated.
604 if (!ObjcSynthesizedStructs.insert(CDecl))
Steve Naroff809b4f02007-10-31 22:11:35 +0000605 assert(true && "struct already synthesize- SynthesizeObjcInternalStruct");
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000606}
607
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000608// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
609/// class methods.
610void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
611 int NumMethods,
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000612 bool IsInstanceMethod,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000613 const char *prefix,
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000614 const char *ClassName,
615 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000616 static bool objc_impl_method = false;
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000617 if (NumMethods > 0 && !objc_impl_method) {
618 /* struct _objc_method {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000619 SEL _cmd;
620 char *method_types;
621 void *_imp;
622 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000623 */
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000624 Result += "\nstruct _objc_method {\n";
625 Result += "\tSEL _cmd;\n";
626 Result += "\tchar *method_types;\n";
627 Result += "\tvoid *_imp;\n";
628 Result += "};\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000629
630 /* struct _objc_method_list {
631 struct _objc_method_list *next_method;
632 int method_count;
633 struct _objc_method method_list[];
634 }
635 */
636 Result += "\nstruct _objc_method_list {\n";
637 Result += "\tstruct _objc_method_list *next_method;\n";
638 Result += "\tint method_count;\n";
639 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000640 objc_impl_method = true;
Fariborz Jahanian96b55da2007-10-19 00:36:46 +0000641 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000642 // Build _objc_method_list for class's methods if needed
643 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000644 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000645 Result += prefix;
646 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
647 Result += "_METHODS_";
648 Result += ClassName;
649 Result += " __attribute__ ((section (\"__OBJC, __";
650 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000651 Result += "_meth\")))= ";
652 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
653
654 Result += "\t,{{(SEL)\"";
655 Result += Methods[0]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000656 std::string MethodTypeString;
657 Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
658 Result += "\", \"";
659 Result += MethodTypeString;
660 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000661 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000662 // TODO: Need method address as 3rd initializer.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000663 Result += "\t ,{(SEL)\"";
664 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000665 std::string MethodTypeString;
666 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
667 Result += "\", \"";
668 Result += MethodTypeString;
669 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000670 }
671 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000672 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000673}
674
675/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
676void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
677 int NumProtocols,
678 const char *prefix,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000679 const char *ClassName,
680 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000681 static bool objc_protocol_methods = false;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000682 if (NumProtocols > 0) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000683 for (int i = 0; i < NumProtocols; i++) {
684 ObjcProtocolDecl *PDecl = Protocols[i];
685 // Output struct protocol_methods holder of method selector and type.
686 if (!objc_protocol_methods &&
687 (PDecl->getNumInstanceMethods() > 0
688 || PDecl->getNumClassMethods() > 0)) {
689 /* struct protocol_methods {
690 SEL _cmd;
691 char *method_types;
692 }
693 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000694 Result += "\nstruct protocol_methods {\n";
695 Result += "\tSEL _cmd;\n";
696 Result += "\tchar *method_types;\n";
697 Result += "};\n";
698
699 /* struct _objc_protocol_method_list {
700 int protocol_method_count;
701 struct protocol_methods protocols[];
702 }
703 */
704 Result += "\nstruct _objc_protocol_method_list {\n";
705 Result += "\tint protocol_method_count;\n";
706 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000707 objc_protocol_methods = true;
708 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000709
Fariborz Jahanian04455192007-10-22 21:41:37 +0000710 // Output instance methods declared in this protocol.
Fariborz Jahanian04455192007-10-22 21:41:37 +0000711 int NumMethods = PDecl->getNumInstanceMethods();
712 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000713 Result += "\nstatic struct _objc_protocol_method_list "
714 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
715 Result += PDecl->getName();
716 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
717 "{\n\t" + utostr(NumMethods) + "\n";
718
Fariborz Jahanian04455192007-10-22 21:41:37 +0000719 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000720 Result += "\t,{{(SEL)\"";
721 Result += Methods[0]->getSelector().getName().c_str();
722 Result += "\", \"\"}\n";
723
724 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000725 Result += "\t ,{(SEL)\"";
726 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000727 std::string MethodTypeString;
728 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
729 Result += "\", \"";
730 Result += MethodTypeString;
731 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000732 }
733 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000734 }
735
736 // Output class methods declared in this protocol.
737 NumMethods = PDecl->getNumClassMethods();
738 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000739 Result += "\nstatic struct _objc_protocol_method_list "
740 "_OBJC_PROTOCOL_CLASS_METHODS_";
741 Result += PDecl->getName();
742 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
743 "{\n\t";
744 Result += utostr(NumMethods);
745 Result += "\n";
746
Fariborz Jahanian04455192007-10-22 21:41:37 +0000747 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000748 Result += "\t,{{(SEL)\"";
749 Result += Methods[0]->getSelector().getName().c_str();
750 Result += "\", \"\"}\n";
751
752 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000753 Result += "\t ,{(SEL)\"";
754 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000755 std::string MethodTypeString;
756 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
757 Result += "\", \"";
758 Result += MethodTypeString;
759 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000760 }
761 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000762 }
763 // Output:
764 /* struct _objc_protocol {
765 // Objective-C 1.0 extensions
766 struct _objc_protocol_extension *isa;
767 char *protocol_name;
768 struct _objc_protocol **protocol_list;
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000769 struct _objc_protocol_method_list *instance_methods;
770 struct _objc_protocol_method_list *class_methods;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000771 };
772 */
773 static bool objc_protocol = false;
774 if (!objc_protocol) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000775 Result += "\nstruct _objc_protocol {\n";
776 Result += "\tstruct _objc_protocol_extension *isa;\n";
777 Result += "\tchar *protocol_name;\n";
778 Result += "\tstruct _objc_protocol **protocol_list;\n";
779 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
780 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
781 Result += "};\n";
782
783 /* struct _objc_protocol_list {
784 struct _objc_protocol_list *next;
785 int protocol_count;
786 struct _objc_protocol *class_protocols[];
787 }
788 */
789 Result += "\nstruct _objc_protocol_list {\n";
790 Result += "\tstruct _objc_protocol_list *next;\n";
791 Result += "\tint protocol_count;\n";
792 Result += "\tstruct _objc_protocol *class_protocols[];\n";
793 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000794 objc_protocol = true;
795 }
796
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000797 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
798 Result += PDecl->getName();
799 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
800 "{\n\t0, \"";
801 Result += PDecl->getName();
802 Result += "\", 0, ";
803 if (PDecl->getInstanceMethods() > 0) {
804 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
805 Result += PDecl->getName();
806 Result += ", ";
807 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000808 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000809 Result += "0, ";
810 if (PDecl->getClassMethods() > 0) {
811 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
812 Result += PDecl->getName();
813 Result += "\n";
814 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000815 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000816 Result += "0\n";
817 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000818 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000819 // Output the top lovel protocol meta-data for the class.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000820 Result += "\nstatic struct _objc_protocol_list _OBJC_";
821 Result += prefix;
822 Result += "_PROTOCOLS_";
823 Result += ClassName;
824 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
825 "{\n\t0, ";
826 Result += utostr(NumProtocols);
827 Result += "\n";
828
829 Result += "\t,{&_OBJC_PROTOCOL_";
830 Result += Protocols[0]->getName();
831 Result += " \n";
832
833 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000834 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000835 Result += "\t ,&_OBJC_PROTOCOL_";
836 Result += PDecl->getName();
837 Result += "\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000838 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000839 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000840 }
841}
842
843/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
844/// implementation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000845void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
846 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000847 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
848 // Find category declaration for this implementation.
849 ObjcCategoryDecl *CDecl;
850 for (CDecl = ClassDecl->getCategoryList(); CDecl;
851 CDecl = CDecl->getNextClassCategory())
852 if (CDecl->getIdentifier() == IDecl->getIdentifier())
853 break;
854 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
855
856 char *FullCategoryName = (char*)alloca(
857 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
858 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
859
860 // Build _objc_method_list for class's instance methods if needed
861 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
862 IDecl->getNumInstanceMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000863 true,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000864 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000865
866 // Build _objc_method_list for class's class methods if needed
867 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
868 IDecl->getNumClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000869 false,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000870 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000871
872 // Protocols referenced in class declaration?
873 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
874 CDecl->getNumReferencedProtocols(),
875 "CATEGORY",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000876 FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000877
878 /* struct _objc_category {
879 char *category_name;
880 char *class_name;
881 struct _objc_method_list *instance_methods;
882 struct _objc_method_list *class_methods;
883 struct _objc_protocol_list *protocols;
884 // Objective-C 1.0 extensions
885 uint32_t size; // sizeof (struct _objc_category)
886 struct _objc_property_list *instance_properties; // category's own
887 // @property decl.
888 };
889 */
890
891 static bool objc_category = false;
892 if (!objc_category) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000893 Result += "\nstruct _objc_category {\n";
894 Result += "\tchar *category_name;\n";
895 Result += "\tchar *class_name;\n";
896 Result += "\tstruct _objc_method_list *instance_methods;\n";
897 Result += "\tstruct _objc_method_list *class_methods;\n";
898 Result += "\tstruct _objc_protocol_list *protocols;\n";
899 Result += "\tunsigned int size;\n";
900 Result += "\tstruct _objc_property_list *instance_properties;\n";
901 Result += "};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000902 objc_category = true;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000903 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000904 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
905 Result += FullCategoryName;
906 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
907 Result += IDecl->getName();
908 Result += "\"\n\t, \"";
909 Result += ClassDecl->getName();
910 Result += "\"\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000911
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000912 if (IDecl->getNumInstanceMethods() > 0) {
913 Result += "\t, (struct _objc_method_list *)"
914 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
915 Result += FullCategoryName;
916 Result += "\n";
917 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000918 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000919 Result += "\t, 0\n";
920 if (IDecl->getNumClassMethods() > 0) {
921 Result += "\t, (struct _objc_method_list *)"
922 "&_OBJC_CATEGORY_CLASS_METHODS_";
923 Result += FullCategoryName;
924 Result += "\n";
925 }
926 else
927 Result += "\t, 0\n";
928
929 if (CDecl->getNumReferencedProtocols() > 0) {
930 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
931 Result += FullCategoryName;
932 Result += "\n";
933 }
934 else
935 Result += "\t, 0\n";
936 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000937}
938
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000939/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
940/// ivar offset.
941void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
942 ObjcIvarDecl *ivar,
943 std::string &Result) {
944 Result += "offsetof(struct _interface_";
945 Result += IDecl->getName();
946 Result += ", ";
947 Result += ivar->getName();
948 Result += ")";
949}
950
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000951//===----------------------------------------------------------------------===//
952// Meta Data Emission
953//===----------------------------------------------------------------------===//
954
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000955void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
956 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000957 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
958
959 // Build _objc_ivar_list metadata for classes ivars if needed
960 int NumIvars = IDecl->getImplDeclNumIvars() > 0
961 ? IDecl->getImplDeclNumIvars()
962 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
963
Fariborz Jahanianab3ec252007-10-26 23:09:28 +0000964 SynthesizeObjcInternalStruct(CDecl, Result);
965
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000966 if (NumIvars > 0) {
967 static bool objc_ivar = false;
968 if (!objc_ivar) {
969 /* struct _objc_ivar {
970 char *ivar_name;
971 char *ivar_type;
972 int ivar_offset;
973 };
974 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000975 Result += "\nstruct _objc_ivar {\n";
976 Result += "\tchar *ivar_name;\n";
977 Result += "\tchar *ivar_type;\n";
978 Result += "\tint ivar_offset;\n";
979 Result += "};\n";
980
981 /* struct _objc_ivar_list {
982 int ivar_count;
983 struct _objc_ivar ivar_list[];
984 };
985 */
986 Result += "\nstruct _objc_ivar_list {\n";
987 Result += "\tint ivar_count;\n";
988 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000989 objc_ivar = true;
990 }
991
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000992 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
993 Result += IDecl->getName();
994 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
995 "{\n\t";
996 Result += utostr(NumIvars);
997 Result += "\n";
998
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000999 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
1000 ? IDecl->getImplDeclIVars()
1001 : CDecl->getIntfDeclIvars();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001002 Result += "\t,{{\"";
1003 Result += Ivars[0]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001004 Result += "\", \"";
1005 std::string StrEncoding;
1006 Context->getObjcEncodingForType(Ivars[0]->getType(), StrEncoding);
1007 Result += StrEncoding;
1008 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001009 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
1010 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001011 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001012 Result += "\t ,{\"";
1013 Result += Ivars[i]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001014 Result += "\", \"";
1015 std::string StrEncoding;
1016 Context->getObjcEncodingForType(Ivars[i]->getType(), StrEncoding);
1017 Result += StrEncoding;
1018 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001019 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
1020 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001021 }
1022
1023 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001024 }
1025
1026 // Build _objc_method_list for class's instance methods if needed
1027 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1028 IDecl->getNumInstanceMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001029 true,
1030 "", IDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001031
1032 // Build _objc_method_list for class's class methods if needed
1033 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +00001034 IDecl->getNumClassMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001035 false,
1036 "", IDecl->getName(), Result);
1037
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001038 // Protocols referenced in class declaration?
1039 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1040 CDecl->getNumIntfRefProtocols(),
1041 "CLASS",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001042 CDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001043
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001044
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001045 // Declaration of class/meta-class metadata
1046 /* struct _objc_class {
1047 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001048 const char *super_class_name;
1049 char *name;
1050 long version;
1051 long info;
1052 long instance_size;
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001053 struct _objc_ivar_list *ivars;
1054 struct _objc_method_list *methods;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001055 struct objc_cache *cache;
1056 struct objc_protocol_list *protocols;
1057 const char *ivar_layout;
1058 struct _objc_class_ext *ext;
1059 };
1060 */
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001061 static bool objc_class = false;
1062 if (!objc_class) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001063 Result += "\nstruct _objc_class {\n";
1064 Result += "\tstruct _objc_class *isa;\n";
1065 Result += "\tconst char *super_class_name;\n";
1066 Result += "\tchar *name;\n";
1067 Result += "\tlong version;\n";
1068 Result += "\tlong info;\n";
1069 Result += "\tlong instance_size;\n";
1070 Result += "\tstruct _objc_ivar_list *ivars;\n";
1071 Result += "\tstruct _objc_method_list *methods;\n";
1072 Result += "\tstruct objc_cache *cache;\n";
1073 Result += "\tstruct _objc_protocol_list *protocols;\n";
1074 Result += "\tconst char *ivar_layout;\n";
1075 Result += "\tstruct _objc_class_ext *ext;\n";
1076 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001077 objc_class = true;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001078 }
1079
1080 // Meta-class metadata generation.
1081 ObjcInterfaceDecl *RootClass = 0;
1082 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
1083 while (SuperClass) {
1084 RootClass = SuperClass;
1085 SuperClass = SuperClass->getSuperClass();
1086 }
1087 SuperClass = CDecl->getSuperClass();
1088
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001089 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
1090 Result += CDecl->getName();
1091 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
1092 "{\n\t(struct _objc_class *)\"";
1093 Result += (RootClass ? RootClass->getName() : CDecl->getName());
1094 Result += "\"";
1095
1096 if (SuperClass) {
1097 Result += ", \"";
1098 Result += SuperClass->getName();
1099 Result += "\", \"";
1100 Result += CDecl->getName();
1101 Result += "\"";
1102 }
1103 else {
1104 Result += ", 0, \"";
1105 Result += CDecl->getName();
1106 Result += "\"";
1107 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001108 // TODO: 'ivars' field for root class is currently set to 0.
1109 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001110 Result += ", 0,2, sizeof(struct _objc_class), 0";
1111 if (CDecl->getNumClassMethods() > 0) {
1112 Result += "\n\t, &_OBJC_CLASS_METHODS_";
1113 Result += CDecl->getName();
1114 Result += "\n";
1115 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001116 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001117 Result += ", 0\n";
1118 if (CDecl->getNumIntfRefProtocols() > 0) {
1119 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
1120 Result += CDecl->getName();
1121 Result += ",0,0\n";
1122 }
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001123 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001124 Result += "\t,0,0,0,0\n";
1125 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001126
1127 // class metadata generation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001128 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
1129 Result += CDecl->getName();
1130 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
1131 "{\n\t&_OBJC_METACLASS_";
1132 Result += CDecl->getName();
1133 if (SuperClass) {
1134 Result += ", \"";
1135 Result += SuperClass->getName();
1136 Result += "\", \"";
1137 Result += CDecl->getName();
1138 Result += "\"";
1139 }
1140 else {
1141 Result += ", 0, \"";
1142 Result += CDecl->getName();
1143 Result += "\"";
1144 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001145 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00001146 Result += ", 0,1";
1147 if (!ObjcSynthesizedStructs.count(CDecl))
1148 Result += ",0";
1149 else {
1150 // class has size. Must synthesize its size.
1151 Result += ",sizeof(struct _interface_";
1152 Result += CDecl->getName();
1153 Result += ")";
1154 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001155 if (NumIvars > 0) {
1156 Result += ", &_OBJC_INSTANCE_VARIABLES_";
1157 Result += CDecl->getName();
1158 Result += "\n\t";
1159 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001160 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001161 Result += ",0";
1162 if (IDecl->getNumInstanceMethods() > 0) {
1163 Result += ", &_OBJC_INSTANCE_METHODS_";
1164 Result += CDecl->getName();
1165 Result += ", 0\n\t";
1166 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001167 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001168 Result += ",0,0";
1169 if (CDecl->getNumIntfRefProtocols() > 0) {
1170 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1171 Result += CDecl->getName();
1172 Result += ", 0,0\n";
1173 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001174 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001175 Result += ",0,0,0\n";
1176 Result += "};\n";
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001177}
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001178
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001179void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001180 int ClsDefCount = ClassImplementation.size();
1181 int CatDefCount = CategoryImplementation.size();
1182 if (ClsDefCount == 0 && CatDefCount == 0)
1183 return;
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001184
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001185 // TODO: This is temporary until we decide how to access objc types in a
1186 // c program
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001187 Result += "#include <Objc/objc.h>\n";
1188 // This is needed for use of offsetof
1189 Result += "#include <stddef.h>\n";
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001190
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001191 // For each implemented class, write out all its meta data.
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001192 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001193 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001194
1195 // For each implemented category, write out all its meta data.
1196 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001197 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001198
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001199 // Write objc_symtab metadata
1200 /*
1201 struct _objc_symtab
1202 {
1203 long sel_ref_cnt;
1204 SEL *refs;
1205 short cls_def_cnt;
1206 short cat_def_cnt;
1207 void *defs[cls_def_cnt + cat_def_cnt];
1208 };
1209 */
1210
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001211 Result += "\nstruct _objc_symtab {\n";
1212 Result += "\tlong sel_ref_cnt;\n";
1213 Result += "\tSEL *refs;\n";
1214 Result += "\tshort cls_def_cnt;\n";
1215 Result += "\tshort cat_def_cnt;\n";
1216 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1217 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001218
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001219 Result += "static struct _objc_symtab "
1220 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1221 Result += "\t0, 0, " + utostr(ClsDefCount)
1222 + ", " + utostr(CatDefCount) + "\n";
1223 for (int i = 0; i < ClsDefCount; i++) {
1224 Result += "\t,&_OBJC_CLASS_";
1225 Result += ClassImplementation[i]->getName();
1226 Result += "\n";
1227 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001228
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001229 for (int i = 0; i < CatDefCount; i++) {
1230 Result += "\t,&_OBJC_CATEGORY_";
1231 Result += CategoryImplementation[i]->getClassInterface()->getName();
1232 Result += "_";
1233 Result += CategoryImplementation[i]->getName();
1234 Result += "\n";
1235 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001236
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001237 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001238
1239 // Write objc_module metadata
1240
1241 /*
1242 struct _objc_module {
1243 long version;
1244 long size;
1245 const char *name;
1246 struct _objc_symtab *symtab;
1247 }
1248 */
1249
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001250 Result += "\nstruct _objc_module {\n";
1251 Result += "\tlong version;\n";
1252 Result += "\tlong size;\n";
1253 Result += "\tconst char *name;\n";
1254 Result += "\tstruct _objc_symtab *symtab;\n";
1255 Result += "};\n\n";
1256 Result += "static struct _objc_module "
1257 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001258 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1259 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001260 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001261}
Chris Lattner6fe8b272007-10-16 22:36:42 +00001262