blob: 4e01dedcb55a68946a876c4232341ad91e029c21 [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))
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000274 assert(false && "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();
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000554 // If no ivars and no root or if its root, directly or indirectly,
555 // have no ivars (thus not synthesize)
556 // then no need to synthesize this class either.
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000557 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
558 return;
559
560 Result += "\nstruct _interface_";
561 Result += CDecl->getName();
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000562 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000563 Result += " {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000564 Result += "\tstruct _interface_";
565 Result += RCDecl->getName();
566 Result += " _";
567 Result += RCDecl->getName();
568 Result += ";\n";
569 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000570 else
571 Result += " {";
Steve Naroff809b4f02007-10-31 22:11:35 +0000572
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000573 if (NumIvars > 0) {
574 SourceLocation LocStart = CDecl->getLocStart();
575 SourceLocation LocEnd = CDecl->getLocEnd();
576
577 const char *startBuf = SM->getCharacterData(LocStart);
578 const char *endBuf = SM->getCharacterData(LocEnd);
579 startBuf = strchr(startBuf, '{');
580 assert((startBuf && endBuf)
581 && "SynthesizeObjcInternalStruct - malformed @interface");
582 startBuf++; // past '{'
583 while (startBuf < endBuf) {
584 if (*startBuf == '@') {
585 startBuf = strchr(startBuf, 'p');
586 // FIXME: presence of @public, etc. inside comment results in
587 // this transformation as well, which is still correct c-code.
588 if (!strncmp(startBuf, "public", strlen("public"))) {
589 startBuf += strlen("public");
590 Result += "/* @public */";
591 }
592 else if (!strncmp(startBuf, "private", strlen("private"))) {
593 startBuf += strlen("private");
594 Result += "/* @private */";
595 }
596 else if (!strncmp(startBuf, "protected", strlen("protected"))) {
597 startBuf += strlen("protected");
598 Result += "/* @protected */";
599 }
600 }
601 Result += *startBuf++;
602 }
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000603 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000604
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000605 Result += "};\n";
606 // Mark this struct as having been generated.
607 if (!ObjcSynthesizedStructs.insert(CDecl))
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000608 assert(false && "struct already synthesize- SynthesizeObjcInternalStruct");
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000609}
610
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000611// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
612/// class methods.
613void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
614 int NumMethods,
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000615 bool IsInstanceMethod,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000616 const char *prefix,
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000617 const char *ClassName,
618 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000619 static bool objc_impl_method = false;
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000620 if (NumMethods > 0 && !objc_impl_method) {
621 /* struct _objc_method {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000622 SEL _cmd;
623 char *method_types;
624 void *_imp;
625 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000626 */
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000627 Result += "\nstruct _objc_method {\n";
628 Result += "\tSEL _cmd;\n";
629 Result += "\tchar *method_types;\n";
630 Result += "\tvoid *_imp;\n";
631 Result += "};\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000632
633 /* struct _objc_method_list {
634 struct _objc_method_list *next_method;
635 int method_count;
636 struct _objc_method method_list[];
637 }
638 */
639 Result += "\nstruct _objc_method_list {\n";
640 Result += "\tstruct _objc_method_list *next_method;\n";
641 Result += "\tint method_count;\n";
642 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000643 objc_impl_method = true;
Fariborz Jahanian96b55da2007-10-19 00:36:46 +0000644 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000645 // Build _objc_method_list for class's methods if needed
646 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000647 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000648 Result += prefix;
649 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
650 Result += "_METHODS_";
651 Result += ClassName;
652 Result += " __attribute__ ((section (\"__OBJC, __";
653 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000654 Result += "_meth\")))= ";
655 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
656
657 Result += "\t,{{(SEL)\"";
658 Result += Methods[0]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000659 std::string MethodTypeString;
660 Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
661 Result += "\", \"";
662 Result += MethodTypeString;
663 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000664 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000665 // TODO: Need method address as 3rd initializer.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000666 Result += "\t ,{(SEL)\"";
667 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000668 std::string MethodTypeString;
669 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
670 Result += "\", \"";
671 Result += MethodTypeString;
672 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000673 }
674 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000675 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000676}
677
678/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
679void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
680 int NumProtocols,
681 const char *prefix,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000682 const char *ClassName,
683 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000684 static bool objc_protocol_methods = false;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000685 if (NumProtocols > 0) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000686 for (int i = 0; i < NumProtocols; i++) {
687 ObjcProtocolDecl *PDecl = Protocols[i];
688 // Output struct protocol_methods holder of method selector and type.
689 if (!objc_protocol_methods &&
690 (PDecl->getNumInstanceMethods() > 0
691 || PDecl->getNumClassMethods() > 0)) {
692 /* struct protocol_methods {
693 SEL _cmd;
694 char *method_types;
695 }
696 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000697 Result += "\nstruct protocol_methods {\n";
698 Result += "\tSEL _cmd;\n";
699 Result += "\tchar *method_types;\n";
700 Result += "};\n";
701
702 /* struct _objc_protocol_method_list {
703 int protocol_method_count;
704 struct protocol_methods protocols[];
705 }
706 */
707 Result += "\nstruct _objc_protocol_method_list {\n";
708 Result += "\tint protocol_method_count;\n";
709 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000710 objc_protocol_methods = true;
711 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000712
Fariborz Jahanian04455192007-10-22 21:41:37 +0000713 // Output instance methods declared in this protocol.
Fariborz Jahanian04455192007-10-22 21:41:37 +0000714 int NumMethods = PDecl->getNumInstanceMethods();
715 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000716 Result += "\nstatic struct _objc_protocol_method_list "
717 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
718 Result += PDecl->getName();
719 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
720 "{\n\t" + utostr(NumMethods) + "\n";
721
Fariborz Jahanian04455192007-10-22 21:41:37 +0000722 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000723 Result += "\t,{{(SEL)\"";
724 Result += Methods[0]->getSelector().getName().c_str();
725 Result += "\", \"\"}\n";
726
727 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000728 Result += "\t ,{(SEL)\"";
729 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000730 std::string MethodTypeString;
731 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
732 Result += "\", \"";
733 Result += MethodTypeString;
734 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000735 }
736 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000737 }
738
739 // Output class methods declared in this protocol.
740 NumMethods = PDecl->getNumClassMethods();
741 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000742 Result += "\nstatic struct _objc_protocol_method_list "
743 "_OBJC_PROTOCOL_CLASS_METHODS_";
744 Result += PDecl->getName();
745 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
746 "{\n\t";
747 Result += utostr(NumMethods);
748 Result += "\n";
749
Fariborz Jahanian04455192007-10-22 21:41:37 +0000750 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000751 Result += "\t,{{(SEL)\"";
752 Result += Methods[0]->getSelector().getName().c_str();
753 Result += "\", \"\"}\n";
754
755 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000756 Result += "\t ,{(SEL)\"";
757 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000758 std::string MethodTypeString;
759 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
760 Result += "\", \"";
761 Result += MethodTypeString;
762 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000763 }
764 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000765 }
766 // Output:
767 /* struct _objc_protocol {
768 // Objective-C 1.0 extensions
769 struct _objc_protocol_extension *isa;
770 char *protocol_name;
771 struct _objc_protocol **protocol_list;
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000772 struct _objc_protocol_method_list *instance_methods;
773 struct _objc_protocol_method_list *class_methods;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000774 };
775 */
776 static bool objc_protocol = false;
777 if (!objc_protocol) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000778 Result += "\nstruct _objc_protocol {\n";
779 Result += "\tstruct _objc_protocol_extension *isa;\n";
780 Result += "\tchar *protocol_name;\n";
781 Result += "\tstruct _objc_protocol **protocol_list;\n";
782 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
783 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
784 Result += "};\n";
785
786 /* struct _objc_protocol_list {
787 struct _objc_protocol_list *next;
788 int protocol_count;
789 struct _objc_protocol *class_protocols[];
790 }
791 */
792 Result += "\nstruct _objc_protocol_list {\n";
793 Result += "\tstruct _objc_protocol_list *next;\n";
794 Result += "\tint protocol_count;\n";
795 Result += "\tstruct _objc_protocol *class_protocols[];\n";
796 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000797 objc_protocol = true;
798 }
799
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000800 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
801 Result += PDecl->getName();
802 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
803 "{\n\t0, \"";
804 Result += PDecl->getName();
805 Result += "\", 0, ";
806 if (PDecl->getInstanceMethods() > 0) {
807 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
808 Result += PDecl->getName();
809 Result += ", ";
810 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000811 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000812 Result += "0, ";
813 if (PDecl->getClassMethods() > 0) {
814 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
815 Result += PDecl->getName();
816 Result += "\n";
817 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000818 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000819 Result += "0\n";
820 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000821 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000822 // Output the top lovel protocol meta-data for the class.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000823 Result += "\nstatic struct _objc_protocol_list _OBJC_";
824 Result += prefix;
825 Result += "_PROTOCOLS_";
826 Result += ClassName;
827 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
828 "{\n\t0, ";
829 Result += utostr(NumProtocols);
830 Result += "\n";
831
832 Result += "\t,{&_OBJC_PROTOCOL_";
833 Result += Protocols[0]->getName();
834 Result += " \n";
835
836 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000837 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000838 Result += "\t ,&_OBJC_PROTOCOL_";
839 Result += PDecl->getName();
840 Result += "\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000841 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000842 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000843 }
844}
845
846/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
847/// implementation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000848void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
849 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000850 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
851 // Find category declaration for this implementation.
852 ObjcCategoryDecl *CDecl;
853 for (CDecl = ClassDecl->getCategoryList(); CDecl;
854 CDecl = CDecl->getNextClassCategory())
855 if (CDecl->getIdentifier() == IDecl->getIdentifier())
856 break;
857 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
858
859 char *FullCategoryName = (char*)alloca(
860 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
861 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
862
863 // Build _objc_method_list for class's instance methods if needed
864 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
865 IDecl->getNumInstanceMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000866 true,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000867 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000868
869 // Build _objc_method_list for class's class methods if needed
870 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
871 IDecl->getNumClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000872 false,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000873 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000874
875 // Protocols referenced in class declaration?
876 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
877 CDecl->getNumReferencedProtocols(),
878 "CATEGORY",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000879 FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000880
881 /* struct _objc_category {
882 char *category_name;
883 char *class_name;
884 struct _objc_method_list *instance_methods;
885 struct _objc_method_list *class_methods;
886 struct _objc_protocol_list *protocols;
887 // Objective-C 1.0 extensions
888 uint32_t size; // sizeof (struct _objc_category)
889 struct _objc_property_list *instance_properties; // category's own
890 // @property decl.
891 };
892 */
893
894 static bool objc_category = false;
895 if (!objc_category) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000896 Result += "\nstruct _objc_category {\n";
897 Result += "\tchar *category_name;\n";
898 Result += "\tchar *class_name;\n";
899 Result += "\tstruct _objc_method_list *instance_methods;\n";
900 Result += "\tstruct _objc_method_list *class_methods;\n";
901 Result += "\tstruct _objc_protocol_list *protocols;\n";
902 Result += "\tunsigned int size;\n";
903 Result += "\tstruct _objc_property_list *instance_properties;\n";
904 Result += "};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000905 objc_category = true;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000906 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000907 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
908 Result += FullCategoryName;
909 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
910 Result += IDecl->getName();
911 Result += "\"\n\t, \"";
912 Result += ClassDecl->getName();
913 Result += "\"\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000914
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000915 if (IDecl->getNumInstanceMethods() > 0) {
916 Result += "\t, (struct _objc_method_list *)"
917 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
918 Result += FullCategoryName;
919 Result += "\n";
920 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000921 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000922 Result += "\t, 0\n";
923 if (IDecl->getNumClassMethods() > 0) {
924 Result += "\t, (struct _objc_method_list *)"
925 "&_OBJC_CATEGORY_CLASS_METHODS_";
926 Result += FullCategoryName;
927 Result += "\n";
928 }
929 else
930 Result += "\t, 0\n";
931
932 if (CDecl->getNumReferencedProtocols() > 0) {
933 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
934 Result += FullCategoryName;
935 Result += "\n";
936 }
937 else
938 Result += "\t, 0\n";
939 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000940}
941
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000942/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
943/// ivar offset.
944void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
945 ObjcIvarDecl *ivar,
946 std::string &Result) {
947 Result += "offsetof(struct _interface_";
948 Result += IDecl->getName();
949 Result += ", ";
950 Result += ivar->getName();
951 Result += ")";
952}
953
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000954//===----------------------------------------------------------------------===//
955// Meta Data Emission
956//===----------------------------------------------------------------------===//
957
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000958void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
959 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000960 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
961
962 // Build _objc_ivar_list metadata for classes ivars if needed
963 int NumIvars = IDecl->getImplDeclNumIvars() > 0
964 ? IDecl->getImplDeclNumIvars()
965 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
966
Fariborz Jahanianab3ec252007-10-26 23:09:28 +0000967 SynthesizeObjcInternalStruct(CDecl, Result);
968
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000969 if (NumIvars > 0) {
970 static bool objc_ivar = false;
971 if (!objc_ivar) {
972 /* struct _objc_ivar {
973 char *ivar_name;
974 char *ivar_type;
975 int ivar_offset;
976 };
977 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000978 Result += "\nstruct _objc_ivar {\n";
979 Result += "\tchar *ivar_name;\n";
980 Result += "\tchar *ivar_type;\n";
981 Result += "\tint ivar_offset;\n";
982 Result += "};\n";
983
984 /* struct _objc_ivar_list {
985 int ivar_count;
986 struct _objc_ivar ivar_list[];
987 };
988 */
989 Result += "\nstruct _objc_ivar_list {\n";
990 Result += "\tint ivar_count;\n";
991 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000992 objc_ivar = true;
993 }
994
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000995 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
996 Result += IDecl->getName();
997 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
998 "{\n\t";
999 Result += utostr(NumIvars);
1000 Result += "\n";
1001
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001002 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
1003 ? IDecl->getImplDeclIVars()
1004 : CDecl->getIntfDeclIvars();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001005 Result += "\t,{{\"";
1006 Result += Ivars[0]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001007 Result += "\", \"";
1008 std::string StrEncoding;
1009 Context->getObjcEncodingForType(Ivars[0]->getType(), StrEncoding);
1010 Result += StrEncoding;
1011 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001012 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
1013 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001014 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001015 Result += "\t ,{\"";
1016 Result += Ivars[i]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001017 Result += "\", \"";
1018 std::string StrEncoding;
1019 Context->getObjcEncodingForType(Ivars[i]->getType(), StrEncoding);
1020 Result += StrEncoding;
1021 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001022 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
1023 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001024 }
1025
1026 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001027 }
1028
1029 // Build _objc_method_list for class's instance methods if needed
1030 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1031 IDecl->getNumInstanceMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001032 true,
1033 "", IDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001034
1035 // Build _objc_method_list for class's class methods if needed
1036 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +00001037 IDecl->getNumClassMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001038 false,
1039 "", IDecl->getName(), Result);
1040
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001041 // Protocols referenced in class declaration?
1042 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1043 CDecl->getNumIntfRefProtocols(),
1044 "CLASS",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001045 CDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001046
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001047
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001048 // Declaration of class/meta-class metadata
1049 /* struct _objc_class {
1050 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001051 const char *super_class_name;
1052 char *name;
1053 long version;
1054 long info;
1055 long instance_size;
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001056 struct _objc_ivar_list *ivars;
1057 struct _objc_method_list *methods;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001058 struct objc_cache *cache;
1059 struct objc_protocol_list *protocols;
1060 const char *ivar_layout;
1061 struct _objc_class_ext *ext;
1062 };
1063 */
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001064 static bool objc_class = false;
1065 if (!objc_class) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001066 Result += "\nstruct _objc_class {\n";
1067 Result += "\tstruct _objc_class *isa;\n";
1068 Result += "\tconst char *super_class_name;\n";
1069 Result += "\tchar *name;\n";
1070 Result += "\tlong version;\n";
1071 Result += "\tlong info;\n";
1072 Result += "\tlong instance_size;\n";
1073 Result += "\tstruct _objc_ivar_list *ivars;\n";
1074 Result += "\tstruct _objc_method_list *methods;\n";
1075 Result += "\tstruct objc_cache *cache;\n";
1076 Result += "\tstruct _objc_protocol_list *protocols;\n";
1077 Result += "\tconst char *ivar_layout;\n";
1078 Result += "\tstruct _objc_class_ext *ext;\n";
1079 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001080 objc_class = true;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001081 }
1082
1083 // Meta-class metadata generation.
1084 ObjcInterfaceDecl *RootClass = 0;
1085 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
1086 while (SuperClass) {
1087 RootClass = SuperClass;
1088 SuperClass = SuperClass->getSuperClass();
1089 }
1090 SuperClass = CDecl->getSuperClass();
1091
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001092 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
1093 Result += CDecl->getName();
1094 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
1095 "{\n\t(struct _objc_class *)\"";
1096 Result += (RootClass ? RootClass->getName() : CDecl->getName());
1097 Result += "\"";
1098
1099 if (SuperClass) {
1100 Result += ", \"";
1101 Result += SuperClass->getName();
1102 Result += "\", \"";
1103 Result += CDecl->getName();
1104 Result += "\"";
1105 }
1106 else {
1107 Result += ", 0, \"";
1108 Result += CDecl->getName();
1109 Result += "\"";
1110 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001111 // TODO: 'ivars' field for root class is currently set to 0.
1112 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001113 Result += ", 0,2, sizeof(struct _objc_class), 0";
1114 if (CDecl->getNumClassMethods() > 0) {
1115 Result += "\n\t, &_OBJC_CLASS_METHODS_";
1116 Result += CDecl->getName();
1117 Result += "\n";
1118 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001119 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001120 Result += ", 0\n";
1121 if (CDecl->getNumIntfRefProtocols() > 0) {
1122 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
1123 Result += CDecl->getName();
1124 Result += ",0,0\n";
1125 }
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001126 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001127 Result += "\t,0,0,0,0\n";
1128 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001129
1130 // class metadata generation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001131 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
1132 Result += CDecl->getName();
1133 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
1134 "{\n\t&_OBJC_METACLASS_";
1135 Result += CDecl->getName();
1136 if (SuperClass) {
1137 Result += ", \"";
1138 Result += SuperClass->getName();
1139 Result += "\", \"";
1140 Result += CDecl->getName();
1141 Result += "\"";
1142 }
1143 else {
1144 Result += ", 0, \"";
1145 Result += CDecl->getName();
1146 Result += "\"";
1147 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001148 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00001149 Result += ", 0,1";
1150 if (!ObjcSynthesizedStructs.count(CDecl))
1151 Result += ",0";
1152 else {
1153 // class has size. Must synthesize its size.
1154 Result += ",sizeof(struct _interface_";
1155 Result += CDecl->getName();
1156 Result += ")";
1157 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001158 if (NumIvars > 0) {
1159 Result += ", &_OBJC_INSTANCE_VARIABLES_";
1160 Result += CDecl->getName();
1161 Result += "\n\t";
1162 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001163 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001164 Result += ",0";
1165 if (IDecl->getNumInstanceMethods() > 0) {
1166 Result += ", &_OBJC_INSTANCE_METHODS_";
1167 Result += CDecl->getName();
1168 Result += ", 0\n\t";
1169 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001170 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001171 Result += ",0,0";
1172 if (CDecl->getNumIntfRefProtocols() > 0) {
1173 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1174 Result += CDecl->getName();
1175 Result += ", 0,0\n";
1176 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001177 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001178 Result += ",0,0,0\n";
1179 Result += "};\n";
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001180}
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001181
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001182void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001183 int ClsDefCount = ClassImplementation.size();
1184 int CatDefCount = CategoryImplementation.size();
1185 if (ClsDefCount == 0 && CatDefCount == 0)
1186 return;
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001187
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001188 // TODO: This is temporary until we decide how to access objc types in a
1189 // c program
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001190 Result += "#include <Objc/objc.h>\n";
1191 // This is needed for use of offsetof
1192 Result += "#include <stddef.h>\n";
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001193
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001194 // For each implemented class, write out all its meta data.
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001195 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001196 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001197
1198 // For each implemented category, write out all its meta data.
1199 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001200 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001201
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001202 // Write objc_symtab metadata
1203 /*
1204 struct _objc_symtab
1205 {
1206 long sel_ref_cnt;
1207 SEL *refs;
1208 short cls_def_cnt;
1209 short cat_def_cnt;
1210 void *defs[cls_def_cnt + cat_def_cnt];
1211 };
1212 */
1213
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001214 Result += "\nstruct _objc_symtab {\n";
1215 Result += "\tlong sel_ref_cnt;\n";
1216 Result += "\tSEL *refs;\n";
1217 Result += "\tshort cls_def_cnt;\n";
1218 Result += "\tshort cat_def_cnt;\n";
1219 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1220 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001221
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001222 Result += "static struct _objc_symtab "
1223 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1224 Result += "\t0, 0, " + utostr(ClsDefCount)
1225 + ", " + utostr(CatDefCount) + "\n";
1226 for (int i = 0; i < ClsDefCount; i++) {
1227 Result += "\t,&_OBJC_CLASS_";
1228 Result += ClassImplementation[i]->getName();
1229 Result += "\n";
1230 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001231
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001232 for (int i = 0; i < CatDefCount; i++) {
1233 Result += "\t,&_OBJC_CATEGORY_";
1234 Result += CategoryImplementation[i]->getClassInterface()->getName();
1235 Result += "_";
1236 Result += CategoryImplementation[i]->getName();
1237 Result += "\n";
1238 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001239
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001240 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001241
1242 // Write objc_module metadata
1243
1244 /*
1245 struct _objc_module {
1246 long version;
1247 long size;
1248 const char *name;
1249 struct _objc_symtab *symtab;
1250 }
1251 */
1252
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001253 Result += "\nstruct _objc_module {\n";
1254 Result += "\tlong version;\n";
1255 Result += "\tlong size;\n";
1256 Result += "\tconst char *name;\n";
1257 Result += "\tstruct _objc_symtab *symtab;\n";
1258 Result += "};\n\n";
1259 Result += "static struct _objc_module "
1260 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001261 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1262 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001263 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001264}
Chris Lattner6fe8b272007-10-16 22:36:42 +00001265