blob: 999b0796a97b713491043e24a9b2f9208d2424be [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");
Fariborz Jahanianfb4f6a32007-10-31 23:08:24 +0000547 // Do not synthesize more than once.
548 if (ObjcSynthesizedStructs.count(CDecl))
549 return;
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000550 ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass();
551 if (RCDecl && !ObjcSynthesizedStructs.count(RCDecl)) {
552 // Do it for the root
553 SynthesizeObjcInternalStruct(RCDecl, Result);
554 }
555
556 int NumIvars = CDecl->getIntfDeclNumIvars();
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000557 // If no ivars and no root or if its root, directly or indirectly,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000558 // have no ivars (thus not synthesized) then no need to synthesize this class.
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000559 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
560 return;
561
562 Result += "\nstruct _interface_";
563 Result += CDecl->getName();
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000564 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000565 Result += " {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000566 Result += "\tstruct _interface_";
567 Result += RCDecl->getName();
568 Result += " _";
569 Result += RCDecl->getName();
570 Result += ";\n";
571 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000572 else
573 Result += " {";
Steve Naroff809b4f02007-10-31 22:11:35 +0000574
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000575 if (NumIvars > 0) {
576 SourceLocation LocStart = CDecl->getLocStart();
577 SourceLocation LocEnd = CDecl->getLocEnd();
578
579 const char *startBuf = SM->getCharacterData(LocStart);
580 const char *endBuf = SM->getCharacterData(LocEnd);
581 startBuf = strchr(startBuf, '{');
582 assert((startBuf && endBuf)
583 && "SynthesizeObjcInternalStruct - malformed @interface");
584 startBuf++; // past '{'
585 while (startBuf < endBuf) {
586 if (*startBuf == '@') {
587 startBuf = strchr(startBuf, 'p');
588 // FIXME: presence of @public, etc. inside comment results in
589 // this transformation as well, which is still correct c-code.
590 if (!strncmp(startBuf, "public", strlen("public"))) {
591 startBuf += strlen("public");
592 Result += "/* @public */";
593 }
594 else if (!strncmp(startBuf, "private", strlen("private"))) {
595 startBuf += strlen("private");
596 Result += "/* @private */";
597 }
598 else if (!strncmp(startBuf, "protected", strlen("protected"))) {
599 startBuf += strlen("protected");
600 Result += "/* @protected */";
601 }
602 }
603 Result += *startBuf++;
604 }
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000605 }
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +0000606
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000607 Result += "};\n";
608 // Mark this struct as having been generated.
609 if (!ObjcSynthesizedStructs.insert(CDecl))
Fariborz Jahanian2a3c7762007-10-31 22:57:04 +0000610 assert(false && "struct already synthesize- SynthesizeObjcInternalStruct");
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000611}
612
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000613// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
614/// class methods.
615void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
616 int NumMethods,
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000617 bool IsInstanceMethod,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000618 const char *prefix,
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000619 const char *ClassName,
620 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000621 static bool objc_impl_method = false;
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000622 if (NumMethods > 0 && !objc_impl_method) {
623 /* struct _objc_method {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000624 SEL _cmd;
625 char *method_types;
626 void *_imp;
627 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000628 */
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000629 Result += "\nstruct _objc_method {\n";
630 Result += "\tSEL _cmd;\n";
631 Result += "\tchar *method_types;\n";
632 Result += "\tvoid *_imp;\n";
633 Result += "};\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000634
635 /* struct _objc_method_list {
636 struct _objc_method_list *next_method;
637 int method_count;
638 struct _objc_method method_list[];
639 }
640 */
641 Result += "\nstruct _objc_method_list {\n";
642 Result += "\tstruct _objc_method_list *next_method;\n";
643 Result += "\tint method_count;\n";
644 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000645 objc_impl_method = true;
Fariborz Jahanian96b55da2007-10-19 00:36:46 +0000646 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000647 // Build _objc_method_list for class's methods if needed
648 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000649 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000650 Result += prefix;
651 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
652 Result += "_METHODS_";
653 Result += ClassName;
654 Result += " __attribute__ ((section (\"__OBJC, __";
655 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000656 Result += "_meth\")))= ";
657 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
658
659 Result += "\t,{{(SEL)\"";
660 Result += Methods[0]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000661 std::string MethodTypeString;
662 Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
663 Result += "\", \"";
664 Result += MethodTypeString;
665 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000666 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000667 // TODO: Need method address as 3rd initializer.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000668 Result += "\t ,{(SEL)\"";
669 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000670 std::string MethodTypeString;
671 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
672 Result += "\", \"";
673 Result += MethodTypeString;
674 Result += "\", 0}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000675 }
676 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000677 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000678}
679
680/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
681void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
682 int NumProtocols,
683 const char *prefix,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000684 const char *ClassName,
685 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000686 static bool objc_protocol_methods = false;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000687 if (NumProtocols > 0) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000688 for (int i = 0; i < NumProtocols; i++) {
689 ObjcProtocolDecl *PDecl = Protocols[i];
690 // Output struct protocol_methods holder of method selector and type.
691 if (!objc_protocol_methods &&
692 (PDecl->getNumInstanceMethods() > 0
693 || PDecl->getNumClassMethods() > 0)) {
694 /* struct protocol_methods {
695 SEL _cmd;
696 char *method_types;
697 }
698 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000699 Result += "\nstruct protocol_methods {\n";
700 Result += "\tSEL _cmd;\n";
701 Result += "\tchar *method_types;\n";
702 Result += "};\n";
703
704 /* struct _objc_protocol_method_list {
705 int protocol_method_count;
706 struct protocol_methods protocols[];
707 }
708 */
709 Result += "\nstruct _objc_protocol_method_list {\n";
710 Result += "\tint protocol_method_count;\n";
711 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000712 objc_protocol_methods = true;
713 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000714
Fariborz Jahanian04455192007-10-22 21:41:37 +0000715 // Output instance methods declared in this protocol.
Fariborz Jahanian04455192007-10-22 21:41:37 +0000716 int NumMethods = PDecl->getNumInstanceMethods();
717 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000718 Result += "\nstatic struct _objc_protocol_method_list "
719 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
720 Result += PDecl->getName();
721 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
722 "{\n\t" + utostr(NumMethods) + "\n";
723
Fariborz Jahanian04455192007-10-22 21:41:37 +0000724 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000725 Result += "\t,{{(SEL)\"";
726 Result += Methods[0]->getSelector().getName().c_str();
727 Result += "\", \"\"}\n";
728
729 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000730 Result += "\t ,{(SEL)\"";
731 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000732 std::string MethodTypeString;
733 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
734 Result += "\", \"";
735 Result += MethodTypeString;
736 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000737 }
738 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000739 }
740
741 // Output class methods declared in this protocol.
742 NumMethods = PDecl->getNumClassMethods();
743 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000744 Result += "\nstatic struct _objc_protocol_method_list "
745 "_OBJC_PROTOCOL_CLASS_METHODS_";
746 Result += PDecl->getName();
747 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
748 "{\n\t";
749 Result += utostr(NumMethods);
750 Result += "\n";
751
Fariborz Jahanian04455192007-10-22 21:41:37 +0000752 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000753 Result += "\t,{{(SEL)\"";
754 Result += Methods[0]->getSelector().getName().c_str();
755 Result += "\", \"\"}\n";
756
757 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000758 Result += "\t ,{(SEL)\"";
759 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000760 std::string MethodTypeString;
761 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
762 Result += "\", \"";
763 Result += MethodTypeString;
764 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000765 }
766 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000767 }
768 // Output:
769 /* struct _objc_protocol {
770 // Objective-C 1.0 extensions
771 struct _objc_protocol_extension *isa;
772 char *protocol_name;
773 struct _objc_protocol **protocol_list;
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000774 struct _objc_protocol_method_list *instance_methods;
775 struct _objc_protocol_method_list *class_methods;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000776 };
777 */
778 static bool objc_protocol = false;
779 if (!objc_protocol) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000780 Result += "\nstruct _objc_protocol {\n";
781 Result += "\tstruct _objc_protocol_extension *isa;\n";
782 Result += "\tchar *protocol_name;\n";
783 Result += "\tstruct _objc_protocol **protocol_list;\n";
784 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
785 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
786 Result += "};\n";
787
788 /* struct _objc_protocol_list {
789 struct _objc_protocol_list *next;
790 int protocol_count;
791 struct _objc_protocol *class_protocols[];
792 }
793 */
794 Result += "\nstruct _objc_protocol_list {\n";
795 Result += "\tstruct _objc_protocol_list *next;\n";
796 Result += "\tint protocol_count;\n";
797 Result += "\tstruct _objc_protocol *class_protocols[];\n";
798 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000799 objc_protocol = true;
800 }
801
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000802 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
803 Result += PDecl->getName();
804 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
805 "{\n\t0, \"";
806 Result += PDecl->getName();
807 Result += "\", 0, ";
808 if (PDecl->getInstanceMethods() > 0) {
809 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
810 Result += PDecl->getName();
811 Result += ", ";
812 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000813 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000814 Result += "0, ";
815 if (PDecl->getClassMethods() > 0) {
816 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
817 Result += PDecl->getName();
818 Result += "\n";
819 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000820 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000821 Result += "0\n";
822 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000823 }
Fariborz Jahanian04455192007-10-22 21:41:37 +0000824 // Output the top lovel protocol meta-data for the class.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000825 Result += "\nstatic struct _objc_protocol_list _OBJC_";
826 Result += prefix;
827 Result += "_PROTOCOLS_";
828 Result += ClassName;
829 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
830 "{\n\t0, ";
831 Result += utostr(NumProtocols);
832 Result += "\n";
833
834 Result += "\t,{&_OBJC_PROTOCOL_";
835 Result += Protocols[0]->getName();
836 Result += " \n";
837
838 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahanian04455192007-10-22 21:41:37 +0000839 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000840 Result += "\t ,&_OBJC_PROTOCOL_";
841 Result += PDecl->getName();
842 Result += "\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +0000843 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000844 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000845 }
846}
847
848/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
849/// implementation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000850void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
851 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000852 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
853 // Find category declaration for this implementation.
854 ObjcCategoryDecl *CDecl;
855 for (CDecl = ClassDecl->getCategoryList(); CDecl;
856 CDecl = CDecl->getNextClassCategory())
857 if (CDecl->getIdentifier() == IDecl->getIdentifier())
858 break;
859 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
860
861 char *FullCategoryName = (char*)alloca(
862 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
863 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
864
865 // Build _objc_method_list for class's instance methods if needed
866 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
867 IDecl->getNumInstanceMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000868 true,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000869 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000870
871 // Build _objc_method_list for class's class methods if needed
872 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
873 IDecl->getNumClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000874 false,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000875 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000876
877 // Protocols referenced in class declaration?
878 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
879 CDecl->getNumReferencedProtocols(),
880 "CATEGORY",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000881 FullCategoryName, Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000882
883 /* struct _objc_category {
884 char *category_name;
885 char *class_name;
886 struct _objc_method_list *instance_methods;
887 struct _objc_method_list *class_methods;
888 struct _objc_protocol_list *protocols;
889 // Objective-C 1.0 extensions
890 uint32_t size; // sizeof (struct _objc_category)
891 struct _objc_property_list *instance_properties; // category's own
892 // @property decl.
893 };
894 */
895
896 static bool objc_category = false;
897 if (!objc_category) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000898 Result += "\nstruct _objc_category {\n";
899 Result += "\tchar *category_name;\n";
900 Result += "\tchar *class_name;\n";
901 Result += "\tstruct _objc_method_list *instance_methods;\n";
902 Result += "\tstruct _objc_method_list *class_methods;\n";
903 Result += "\tstruct _objc_protocol_list *protocols;\n";
904 Result += "\tunsigned int size;\n";
905 Result += "\tstruct _objc_property_list *instance_properties;\n";
906 Result += "};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000907 objc_category = true;
Fariborz Jahanian04455192007-10-22 21:41:37 +0000908 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000909 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
910 Result += FullCategoryName;
911 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
912 Result += IDecl->getName();
913 Result += "\"\n\t, \"";
914 Result += ClassDecl->getName();
915 Result += "\"\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000916
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000917 if (IDecl->getNumInstanceMethods() > 0) {
918 Result += "\t, (struct _objc_method_list *)"
919 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
920 Result += FullCategoryName;
921 Result += "\n";
922 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000923 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000924 Result += "\t, 0\n";
925 if (IDecl->getNumClassMethods() > 0) {
926 Result += "\t, (struct _objc_method_list *)"
927 "&_OBJC_CATEGORY_CLASS_METHODS_";
928 Result += FullCategoryName;
929 Result += "\n";
930 }
931 else
932 Result += "\t, 0\n";
933
934 if (CDecl->getNumReferencedProtocols() > 0) {
935 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
936 Result += FullCategoryName;
937 Result += "\n";
938 }
939 else
940 Result += "\t, 0\n";
941 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000942}
943
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000944/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
945/// ivar offset.
946void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
947 ObjcIvarDecl *ivar,
948 std::string &Result) {
949 Result += "offsetof(struct _interface_";
950 Result += IDecl->getName();
951 Result += ", ";
952 Result += ivar->getName();
953 Result += ")";
954}
955
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000956//===----------------------------------------------------------------------===//
957// Meta Data Emission
958//===----------------------------------------------------------------------===//
959
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000960void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
961 std::string &Result) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000962 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
963
964 // Build _objc_ivar_list metadata for classes ivars if needed
965 int NumIvars = IDecl->getImplDeclNumIvars() > 0
966 ? IDecl->getImplDeclNumIvars()
967 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
968
Fariborz Jahanianab3ec252007-10-26 23:09:28 +0000969 SynthesizeObjcInternalStruct(CDecl, Result);
970
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000971 if (NumIvars > 0) {
972 static bool objc_ivar = false;
973 if (!objc_ivar) {
974 /* struct _objc_ivar {
975 char *ivar_name;
976 char *ivar_type;
977 int ivar_offset;
978 };
979 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000980 Result += "\nstruct _objc_ivar {\n";
981 Result += "\tchar *ivar_name;\n";
982 Result += "\tchar *ivar_type;\n";
983 Result += "\tint ivar_offset;\n";
984 Result += "};\n";
985
986 /* struct _objc_ivar_list {
987 int ivar_count;
988 struct _objc_ivar ivar_list[];
989 };
990 */
991 Result += "\nstruct _objc_ivar_list {\n";
992 Result += "\tint ivar_count;\n";
993 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000994 objc_ivar = true;
995 }
996
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000997 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
998 Result += IDecl->getName();
999 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
1000 "{\n\t";
1001 Result += utostr(NumIvars);
1002 Result += "\n";
1003
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001004 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
1005 ? IDecl->getImplDeclIVars()
1006 : CDecl->getIntfDeclIvars();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001007 Result += "\t,{{\"";
1008 Result += Ivars[0]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001009 Result += "\", \"";
1010 std::string StrEncoding;
1011 Context->getObjcEncodingForType(Ivars[0]->getType(), StrEncoding);
1012 Result += StrEncoding;
1013 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001014 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
1015 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001016 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001017 Result += "\t ,{\"";
1018 Result += Ivars[i]->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00001019 Result += "\", \"";
1020 std::string StrEncoding;
1021 Context->getObjcEncodingForType(Ivars[i]->getType(), StrEncoding);
1022 Result += StrEncoding;
1023 Result += "\", ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001024 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
1025 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001026 }
1027
1028 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001029 }
1030
1031 // Build _objc_method_list for class's instance methods if needed
1032 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1033 IDecl->getNumInstanceMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001034 true,
1035 "", IDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001036
1037 // Build _objc_method_list for class's class methods if needed
1038 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahaniana3986372007-10-25 00:14:44 +00001039 IDecl->getNumClassMethods(),
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001040 false,
1041 "", IDecl->getName(), Result);
1042
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001043 // Protocols referenced in class declaration?
1044 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1045 CDecl->getNumIntfRefProtocols(),
1046 "CLASS",
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001047 CDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001048
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001049
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001050 // Declaration of class/meta-class metadata
1051 /* struct _objc_class {
1052 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001053 const char *super_class_name;
1054 char *name;
1055 long version;
1056 long info;
1057 long instance_size;
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001058 struct _objc_ivar_list *ivars;
1059 struct _objc_method_list *methods;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001060 struct objc_cache *cache;
1061 struct objc_protocol_list *protocols;
1062 const char *ivar_layout;
1063 struct _objc_class_ext *ext;
1064 };
1065 */
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001066 static bool objc_class = false;
1067 if (!objc_class) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001068 Result += "\nstruct _objc_class {\n";
1069 Result += "\tstruct _objc_class *isa;\n";
1070 Result += "\tconst char *super_class_name;\n";
1071 Result += "\tchar *name;\n";
1072 Result += "\tlong version;\n";
1073 Result += "\tlong info;\n";
1074 Result += "\tlong instance_size;\n";
1075 Result += "\tstruct _objc_ivar_list *ivars;\n";
1076 Result += "\tstruct _objc_method_list *methods;\n";
1077 Result += "\tstruct objc_cache *cache;\n";
1078 Result += "\tstruct _objc_protocol_list *protocols;\n";
1079 Result += "\tconst char *ivar_layout;\n";
1080 Result += "\tstruct _objc_class_ext *ext;\n";
1081 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001082 objc_class = true;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001083 }
1084
1085 // Meta-class metadata generation.
1086 ObjcInterfaceDecl *RootClass = 0;
1087 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
1088 while (SuperClass) {
1089 RootClass = SuperClass;
1090 SuperClass = SuperClass->getSuperClass();
1091 }
1092 SuperClass = CDecl->getSuperClass();
1093
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001094 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
1095 Result += CDecl->getName();
1096 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
1097 "{\n\t(struct _objc_class *)\"";
1098 Result += (RootClass ? RootClass->getName() : CDecl->getName());
1099 Result += "\"";
1100
1101 if (SuperClass) {
1102 Result += ", \"";
1103 Result += SuperClass->getName();
1104 Result += "\", \"";
1105 Result += CDecl->getName();
1106 Result += "\"";
1107 }
1108 else {
1109 Result += ", 0, \"";
1110 Result += CDecl->getName();
1111 Result += "\"";
1112 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001113 // TODO: 'ivars' field for root class is currently set to 0.
1114 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001115 Result += ", 0,2, sizeof(struct _objc_class), 0";
1116 if (CDecl->getNumClassMethods() > 0) {
1117 Result += "\n\t, &_OBJC_CLASS_METHODS_";
1118 Result += CDecl->getName();
1119 Result += "\n";
1120 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001121 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001122 Result += ", 0\n";
1123 if (CDecl->getNumIntfRefProtocols() > 0) {
1124 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
1125 Result += CDecl->getName();
1126 Result += ",0,0\n";
1127 }
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001128 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001129 Result += "\t,0,0,0,0\n";
1130 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001131
1132 // class metadata generation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001133 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
1134 Result += CDecl->getName();
1135 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
1136 "{\n\t&_OBJC_METACLASS_";
1137 Result += CDecl->getName();
1138 if (SuperClass) {
1139 Result += ", \"";
1140 Result += SuperClass->getName();
1141 Result += "\", \"";
1142 Result += CDecl->getName();
1143 Result += "\"";
1144 }
1145 else {
1146 Result += ", 0, \"";
1147 Result += CDecl->getName();
1148 Result += "\"";
1149 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001150 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00001151 Result += ", 0,1";
1152 if (!ObjcSynthesizedStructs.count(CDecl))
1153 Result += ",0";
1154 else {
1155 // class has size. Must synthesize its size.
1156 Result += ",sizeof(struct _interface_";
1157 Result += CDecl->getName();
1158 Result += ")";
1159 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001160 if (NumIvars > 0) {
1161 Result += ", &_OBJC_INSTANCE_VARIABLES_";
1162 Result += CDecl->getName();
1163 Result += "\n\t";
1164 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001165 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001166 Result += ",0";
1167 if (IDecl->getNumInstanceMethods() > 0) {
1168 Result += ", &_OBJC_INSTANCE_METHODS_";
1169 Result += CDecl->getName();
1170 Result += ", 0\n\t";
1171 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001172 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001173 Result += ",0,0";
1174 if (CDecl->getNumIntfRefProtocols() > 0) {
1175 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1176 Result += CDecl->getName();
1177 Result += ", 0,0\n";
1178 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00001179 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001180 Result += ",0,0,0\n";
1181 Result += "};\n";
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00001182}
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001183
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001184void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001185 int ClsDefCount = ClassImplementation.size();
1186 int CatDefCount = CategoryImplementation.size();
1187 if (ClsDefCount == 0 && CatDefCount == 0)
1188 return;
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001189
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001190 // TODO: This is temporary until we decide how to access objc types in a
1191 // c program
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001192 Result += "#include <Objc/objc.h>\n";
1193 // This is needed for use of offsetof
1194 Result += "#include <stddef.h>\n";
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00001195
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001196 // For each implemented class, write out all its meta data.
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001197 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001198 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00001199
1200 // For each implemented category, write out all its meta data.
1201 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001202 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00001203
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001204 // Write objc_symtab metadata
1205 /*
1206 struct _objc_symtab
1207 {
1208 long sel_ref_cnt;
1209 SEL *refs;
1210 short cls_def_cnt;
1211 short cat_def_cnt;
1212 void *defs[cls_def_cnt + cat_def_cnt];
1213 };
1214 */
1215
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001216 Result += "\nstruct _objc_symtab {\n";
1217 Result += "\tlong sel_ref_cnt;\n";
1218 Result += "\tSEL *refs;\n";
1219 Result += "\tshort cls_def_cnt;\n";
1220 Result += "\tshort cat_def_cnt;\n";
1221 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1222 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001223
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001224 Result += "static struct _objc_symtab "
1225 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1226 Result += "\t0, 0, " + utostr(ClsDefCount)
1227 + ", " + utostr(CatDefCount) + "\n";
1228 for (int i = 0; i < ClsDefCount; i++) {
1229 Result += "\t,&_OBJC_CLASS_";
1230 Result += ClassImplementation[i]->getName();
1231 Result += "\n";
1232 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001233
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001234 for (int i = 0; i < CatDefCount; i++) {
1235 Result += "\t,&_OBJC_CATEGORY_";
1236 Result += CategoryImplementation[i]->getClassInterface()->getName();
1237 Result += "_";
1238 Result += CategoryImplementation[i]->getName();
1239 Result += "\n";
1240 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001241
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001242 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001243
1244 // Write objc_module metadata
1245
1246 /*
1247 struct _objc_module {
1248 long version;
1249 long size;
1250 const char *name;
1251 struct _objc_symtab *symtab;
1252 }
1253 */
1254
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001255 Result += "\nstruct _objc_module {\n";
1256 Result += "\tlong version;\n";
1257 Result += "\tlong size;\n";
1258 Result += "\tconst char *name;\n";
1259 Result += "\tstruct _objc_symtab *symtab;\n";
1260 Result += "};\n\n";
1261 Result += "static struct _objc_module "
1262 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00001263 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1264 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00001265 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00001266}
Chris Lattner6fe8b272007-10-16 22:36:42 +00001267