blob: 257a060ba981d1b50ee6eecd96b3f9d388bb1c7f [file] [log] [blame]
Chris Lattner77cd2a02007-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 Lattner8a12c272007-10-11 18:38:32 +000015#include "clang/Rewrite/Rewriter.h"
Chris Lattner77cd2a02007-10-11 00:43:27 +000016#include "clang/AST/AST.h"
17#include "clang/AST/ASTConsumer.h"
Chris Lattner8a12c272007-10-11 18:38:32 +000018#include "clang/Basic/SourceManager.h"
Steve Naroffebf2b562007-10-23 23:50:29 +000019#include "clang/Basic/IdentifierTable.h"
Chris Lattner158ecb92007-10-25 17:07:24 +000020#include "llvm/ADT/StringExtras.h"
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000021#include "llvm/ADT/SmallPtrSet.h"
Steve Naroff2feac5e2007-10-30 03:43:13 +000022#include "clang/Lex/Lexer.h"
Chris Lattner77cd2a02007-10-11 00:43:27 +000023using namespace clang;
Chris Lattner158ecb92007-10-25 17:07:24 +000024using llvm::utostr;
Chris Lattner77cd2a02007-10-11 00:43:27 +000025
Chris Lattner77cd2a02007-10-11 00:43:27 +000026namespace {
Chris Lattner8a12c272007-10-11 18:38:32 +000027 class RewriteTest : public ASTConsumer {
Chris Lattner2c64b7b2007-10-16 21:07:07 +000028 Rewriter Rewrite;
Chris Lattner01c57482007-10-17 22:35:30 +000029 ASTContext *Context;
Chris Lattner77cd2a02007-10-11 00:43:27 +000030 SourceManager *SM;
Chris Lattner8a12c272007-10-11 18:38:32 +000031 unsigned MainFileID;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000032 SourceLocation LastIncLoc;
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000033 llvm::SmallVector<ObjcImplementationDecl *, 8> ClassImplementation;
34 llvm::SmallVector<ObjcCategoryImplDecl *, 8> CategoryImplementation;
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000035 llvm::SmallPtrSet<ObjcInterfaceDecl*, 8> ObjcSynthesizedStructs;
Steve Naroff8749be52007-10-31 22:11:35 +000036 llvm::SmallPtrSet<ObjcInterfaceDecl*, 8> ObjcForwardDecls;
Steve Naroffebf2b562007-10-23 23:50:29 +000037
38 FunctionDecl *MsgSendFunctionDecl;
39 FunctionDecl *GetClassFunctionDecl;
Steve Naroff934f2762007-10-24 22:48:43 +000040 FunctionDecl *SelGetUidFunctionDecl;
Steve Naroffebf2b562007-10-23 23:50:29 +000041
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000042 static const int OBJC_ABI_VERSION =7 ;
Chris Lattner77cd2a02007-10-11 00:43:27 +000043 public:
Chris Lattner01c57482007-10-17 22:35:30 +000044 void Initialize(ASTContext &context, unsigned mainFileID) {
45 Context = &context;
46 SM = &Context->SourceMgr;
Chris Lattner8a12c272007-10-11 18:38:32 +000047 MainFileID = mainFileID;
Steve Naroffebf2b562007-10-23 23:50:29 +000048 MsgSendFunctionDecl = 0;
Steve Naroffc0006092007-10-24 01:09:48 +000049 GetClassFunctionDecl = 0;
Steve Naroff934f2762007-10-24 22:48:43 +000050 SelGetUidFunctionDecl = 0;
Chris Lattner01c57482007-10-17 22:35:30 +000051 Rewrite.setSourceMgr(Context->SourceMgr);
Chris Lattner77cd2a02007-10-11 00:43:27 +000052 }
Chris Lattner8a12c272007-10-11 18:38:32 +000053
Chris Lattnerf04da132007-10-24 17:06:59 +000054 // Top Level Driver code.
55 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner2c64b7b2007-10-16 21:07:07 +000056 void HandleDeclInMainFile(Decl *D);
Chris Lattnerf04da132007-10-24 17:06:59 +000057 ~RewriteTest();
58
59 // Syntactic Rewriting.
Chris Lattner2c64b7b2007-10-16 21:07:07 +000060 void RewriteInclude(SourceLocation Loc);
Chris Lattnerf04da132007-10-24 17:06:59 +000061 void RewriteTabs();
62 void RewriteForwardClassDecl(ObjcClassDecl *Dcl);
Steve Naroffbef11852007-10-26 20:53:56 +000063 void RewriteInterfaceDecl(ObjcInterfaceDecl *Dcl);
Steve Naroff423cb562007-10-30 13:30:57 +000064 void RewriteCategoryDecl(ObjcCategoryDecl *Dcl);
Steve Naroff752d6ef2007-10-30 16:42:30 +000065 void RewriteProtocolDecl(ObjcProtocolDecl *Dcl);
Steve Naroff423cb562007-10-30 13:30:57 +000066 void RewriteMethods(int nMethods, ObjcMethodDecl **Methods);
Steve Naroff09b266e2007-10-30 23:14:51 +000067 void RewriteFunctionDecl(FunctionDecl *FD);
Steve Naroff9165ad32007-10-31 04:38:33 +000068 bool functionReferencesAnyObjcQualifiedInterfaceTypes(
69 const FunctionTypeProto *proto);
Chris Lattner311ff022007-10-16 22:36:42 +000070
Chris Lattnerf04da132007-10-24 17:06:59 +000071 // Expression Rewriting.
Chris Lattnere64b7772007-10-24 16:57:36 +000072 Stmt *RewriteFunctionBody(Stmt *S);
73 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
74 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroff934f2762007-10-24 22:48:43 +000075 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
76 Expr **args, unsigned nargs);
Steve Naroff09b266e2007-10-30 23:14:51 +000077 void SynthMsgSendFunctionDecl();
78 void SynthGetClassFunctionDecl();
79
Chris Lattnerf04da132007-10-24 17:06:59 +000080 // Metadata emission.
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000081 void HandleObjcMetaDataEmission();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +000082 void RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
83 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000084
Fariborz Jahanianccd87b02007-10-25 20:55:25 +000085 void RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *CDecl,
86 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000087
88 void RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
89 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +000090 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000091 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +000092 const char *ClassName,
93 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000094
95 void RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
96 int NumProtocols,
97 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +000098 const char *ClassName,
99 std::string &Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000100 void SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
101 std::string &Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000102 void SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
103 ObjcIvarDecl *ivar,
104 std::string &Result);
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000105 void WriteObjcMetaData(std::string &Result);
Chris Lattner77cd2a02007-10-11 00:43:27 +0000106 };
107}
108
Chris Lattner8a12c272007-10-11 18:38:32 +0000109ASTConsumer *clang::CreateCodeRewriterTest() { return new RewriteTest(); }
Chris Lattner77cd2a02007-10-11 00:43:27 +0000110
Chris Lattnerf04da132007-10-24 17:06:59 +0000111//===----------------------------------------------------------------------===//
112// Top Level Driver Code
113//===----------------------------------------------------------------------===//
114
Chris Lattner8a12c272007-10-11 18:38:32 +0000115void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner2c64b7b2007-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 Naroffebf2b562007-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 Naroff09b266e2007-10-30 23:14:51 +0000127 RewriteFunctionDecl(FD);
Steve Naroffbef11852007-10-26 20:53:56 +0000128 } else if (ObjcInterfaceDecl *MD = dyn_cast<ObjcInterfaceDecl>(D)) {
129 RewriteInterfaceDecl(MD);
Steve Naroff423cb562007-10-30 13:30:57 +0000130 } else if (ObjcCategoryDecl *CD = dyn_cast<ObjcCategoryDecl>(D)) {
131 RewriteCategoryDecl(CD);
Steve Naroff752d6ef2007-10-30 16:42:30 +0000132 } else if (ObjcProtocolDecl *PD = dyn_cast<ObjcProtocolDecl>(D)) {
133 RewriteProtocolDecl(PD);
Steve Naroffebf2b562007-10-23 23:50:29 +0000134 }
Chris Lattnerf04da132007-10-24 17:06:59 +0000135 // If we have a decl in the main file, see if we should rewrite it.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000136 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
137 return HandleDeclInMainFile(D);
138
Chris Lattnerf04da132007-10-24 17:06:59 +0000139 // Otherwise, see if there is a #import in the main file that should be
140 // rewritten.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000141 RewriteInclude(Loc);
142}
143
Chris Lattnerf04da132007-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 Jahanian26e4cd32007-10-26 19:46:17 +0000174
175}
176
177/// HandleObjcMetaDataEmission - main routine to generate objective-c's
178/// metadata.
179void RewriteTest::HandleObjcMetaDataEmission() {
Chris Lattnerf04da132007-10-24 17:06:59 +0000180 // Rewrite Objective-c meta data*
Fariborz Jahanianccd87b02007-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 Lattnerf04da132007-10-24 17:06:59 +0000185}
186
187//===----------------------------------------------------------------------===//
188// Syntactic (non-AST) Rewriting Code
189//===----------------------------------------------------------------------===//
190
Chris Lattner2c64b7b2007-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 Lattnerf04da132007-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 Jahanian545b9ae2007-10-18 19:23:00 +0000219
Chris Lattnerf04da132007-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 Lattner8a12c272007-10-11 18:38:32 +0000243}
244
245
Chris Lattnerf04da132007-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 Naroff934f2762007-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 Naroff8749be52007-10-31 22:11:35 +0000264 if (ObjcForwardDecls.count(ForwardDecl))
265 continue;
Steve Naroff934f2762007-10-24 22:48:43 +0000266 typedefString += "typedef struct ";
267 typedefString += ForwardDecl->getName();
268 typedefString += " ";
269 typedefString += ForwardDecl->getName();
270 typedefString += ";\n";
Steve Naroff8749be52007-10-31 22:11:35 +0000271
272 // Mark this typedef as having been generated.
273 if (!ObjcForwardDecls.insert(ForwardDecl))
Fariborz Jahanianaff56d02007-10-31 22:57:04 +0000274 assert(false && "typedef already output");
Steve Naroff934f2762007-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 Lattnerf04da132007-10-24 17:06:59 +0000280}
281
Steve Naroff423cb562007-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 Naroff752d6ef2007-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 Naroffbef11852007-10-26 20:53:56 +0000321void RewriteTest::RewriteInterfaceDecl(ObjcInterfaceDecl *ClassDecl) {
Steve Narofff908a872007-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 Naroff2feac5e2007-10-30 03:43:13 +0000329 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
Steve Narofff908a872007-10-30 02:23:23 +0000330
331 std::string ResultStr;
332 SynthesizeObjcInternalStruct(ClassDecl, ResultStr);
333
Steve Naroff2feac5e2007-10-30 03:43:13 +0000334 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
Steve Narofff908a872007-10-30 02:23:23 +0000335 ResultStr.c_str(), ResultStr.size());
336
Steve Naroff423cb562007-10-30 13:30:57 +0000337 RewriteMethods(ClassDecl->getNumInstanceMethods(),
338 ClassDecl->getInstanceMethods());
339 RewriteMethods(ClassDecl->getNumClassMethods(),
340 ClassDecl->getClassMethods());
Steve Naroffbef11852007-10-26 20:53:56 +0000341
Steve Naroff2feac5e2007-10-30 03:43:13 +0000342 // Lastly, comment out the @end.
343 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroffbef11852007-10-26 20:53:56 +0000344}
345
Chris Lattnerf04da132007-10-24 17:06:59 +0000346//===----------------------------------------------------------------------===//
347// Function Body / Expression rewriting
348//===----------------------------------------------------------------------===//
349
Chris Lattnere64b7772007-10-24 16:57:36 +0000350Stmt *RewriteTest::RewriteFunctionBody(Stmt *S) {
Chris Lattner311ff022007-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 Lattner50754772007-10-17 21:28:00 +0000354 if (*CI)
Chris Lattnere64b7772007-10-24 16:57:36 +0000355 *CI = RewriteFunctionBody(*CI);
Steve Naroffebf2b562007-10-23 23:50:29 +0000356
357 // Handle specific things.
358 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
359 return RewriteAtEncode(AtEncode);
360
Steve Naroff934f2762007-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 Naroffbef11852007-10-26 20:53:56 +0000373
Steve Naroff934f2762007-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 Naroff752d6ef2007-10-30 16:42:30 +0000377 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffebf2b562007-10-23 23:50:29 +0000378 return RewriteMessageExpr(MessExpr);
Steve Naroff934f2762007-10-24 22:48:43 +0000379 }
Chris Lattnere64b7772007-10-24 16:57:36 +0000380 // Return this stmt unmodified.
381 return S;
Chris Lattner311ff022007-10-16 22:36:42 +0000382}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +0000383
Chris Lattnere64b7772007-10-24 16:57:36 +0000384Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattner01c57482007-10-17 22:35:30 +0000385 // Create a new string expression.
386 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson85f9bce2007-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 Lattner01c57482007-10-17 22:35:30 +0000391 SourceLocation(), SourceLocation());
392 Rewrite.ReplaceStmt(Exp, Replacement);
Chris Lattnere64b7772007-10-24 16:57:36 +0000393 delete Exp;
394 return Replacement;
Chris Lattner311ff022007-10-16 22:36:42 +0000395}
396
Steve Naroff934f2762007-10-24 22:48:43 +0000397CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
398 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffebf2b562007-10-23 23:50:29 +0000399 // Get the type, we will need to reference it in a couple spots.
Steve Naroff934f2762007-10-24 22:48:43 +0000400 QualType msgSendType = FD->getType();
Steve Naroffebf2b562007-10-23 23:50:29 +0000401
402 // Create a reference to the objc_msgSend() declaration.
Steve Naroff934f2762007-10-24 22:48:43 +0000403 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffebf2b562007-10-23 23:50:29 +0000404
405 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerf04da132007-10-24 17:06:59 +0000406 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffebf2b562007-10-23 23:50:29 +0000407 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
408
409 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattnere64b7772007-10-24 16:57:36 +0000410
Steve Naroff934f2762007-10-24 22:48:43 +0000411 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
412}
413
Steve Naroff9165ad32007-10-31 04:38:33 +0000414bool RewriteTest::functionReferencesAnyObjcQualifiedInterfaceTypes(
415 const FunctionTypeProto *proto) {
Steve Naroff498856c2007-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 Naroff9165ad32007-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 Naroff498856c2007-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 Naroff9165ad32007-10-31 04:38:33 +0000433 Type *pointeeType = pType->getPointeeType().getTypePtr();
434 if (isa<ObjcQualifiedInterfaceType>(pointeeType))
435 return true;
436 }
437 }
Steve Naroff9165ad32007-10-31 04:38:33 +0000438 return false;
439}
440
Steve Naroff09b266e2007-10-30 23:14:51 +0000441void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
442 // declared in <objc/objc.h>
Steve Naroff9165ad32007-10-31 04:38:33 +0000443 if (strcmp(FD->getName(), "sel_getUid") == 0) {
Steve Naroff09b266e2007-10-30 23:14:51 +0000444 SelGetUidFunctionDecl = FD;
Steve Naroff9165ad32007-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 Naroff09b266e2007-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 Naroff934f2762007-10-24 22:48:43 +0000489Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
Steve Naroff934f2762007-10-24 22:48:43 +0000490 assert(SelGetUidFunctionDecl && "Can't find sel_getUid() decl");
Steve Naroff09b266e2007-10-30 23:14:51 +0000491 if (!MsgSendFunctionDecl)
492 SynthMsgSendFunctionDecl();
493 if (!GetClassFunctionDecl)
494 SynthGetClassFunctionDecl();
Steve Naroff934f2762007-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 Lattnere64b7772007-10-24 16:57:36 +0000537 delete Exp;
Steve Naroff934f2762007-10-24 22:48:43 +0000538 return MessExp;
Steve Naroffebf2b562007-10-23 23:50:29 +0000539}
540
Fariborz Jahanian26e4cd32007-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 Jahanian212b7682007-10-31 23:08:24 +0000547 // Do not synthesize more than once.
548 if (ObjcSynthesizedStructs.count(CDecl))
549 return;
Fariborz Jahanian26e4cd32007-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 Jahanianaff56d02007-10-31 22:57:04 +0000557 // If no ivars and no root or if its root, directly or indirectly,
558 // have no ivars (thus not synthesize)
559 // then no need to synthesize this class either.
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000560 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
561 return;
562
563 Result += "\nstruct _interface_";
564 Result += CDecl->getName();
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000565 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000566 Result += " {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000567 Result += "\tstruct _interface_";
568 Result += RCDecl->getName();
569 Result += " _";
570 Result += RCDecl->getName();
571 Result += ";\n";
572 }
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000573 else
574 Result += " {";
Steve Naroff8749be52007-10-31 22:11:35 +0000575
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000576 if (NumIvars > 0) {
577 SourceLocation LocStart = CDecl->getLocStart();
578 SourceLocation LocEnd = CDecl->getLocEnd();
579
580 const char *startBuf = SM->getCharacterData(LocStart);
581 const char *endBuf = SM->getCharacterData(LocEnd);
582 startBuf = strchr(startBuf, '{');
583 assert((startBuf && endBuf)
584 && "SynthesizeObjcInternalStruct - malformed @interface");
585 startBuf++; // past '{'
586 while (startBuf < endBuf) {
587 if (*startBuf == '@') {
588 startBuf = strchr(startBuf, 'p');
589 // FIXME: presence of @public, etc. inside comment results in
590 // this transformation as well, which is still correct c-code.
591 if (!strncmp(startBuf, "public", strlen("public"))) {
592 startBuf += strlen("public");
593 Result += "/* @public */";
594 }
595 else if (!strncmp(startBuf, "private", strlen("private"))) {
596 startBuf += strlen("private");
597 Result += "/* @private */";
598 }
599 else if (!strncmp(startBuf, "protected", strlen("protected"))) {
600 startBuf += strlen("protected");
601 Result += "/* @protected */";
602 }
603 }
604 Result += *startBuf++;
605 }
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000606 }
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000607
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000608 Result += "};\n";
609 // Mark this struct as having been generated.
610 if (!ObjcSynthesizedStructs.insert(CDecl))
Fariborz Jahanianaff56d02007-10-31 22:57:04 +0000611 assert(false && "struct already synthesize- SynthesizeObjcInternalStruct");
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000612}
613
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000614// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
615/// class methods.
616void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
617 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000618 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000619 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000620 const char *ClassName,
621 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000622 static bool objc_impl_method = false;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000623 if (NumMethods > 0 && !objc_impl_method) {
624 /* struct _objc_method {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000625 SEL _cmd;
626 char *method_types;
627 void *_imp;
628 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000629 */
Chris Lattner158ecb92007-10-25 17:07:24 +0000630 Result += "\nstruct _objc_method {\n";
631 Result += "\tSEL _cmd;\n";
632 Result += "\tchar *method_types;\n";
633 Result += "\tvoid *_imp;\n";
634 Result += "};\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000635
636 /* struct _objc_method_list {
637 struct _objc_method_list *next_method;
638 int method_count;
639 struct _objc_method method_list[];
640 }
641 */
642 Result += "\nstruct _objc_method_list {\n";
643 Result += "\tstruct _objc_method_list *next_method;\n";
644 Result += "\tint method_count;\n";
645 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000646 objc_impl_method = true;
Fariborz Jahanian776d6ff2007-10-19 00:36:46 +0000647 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000648 // Build _objc_method_list for class's methods if needed
649 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000650 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattner158ecb92007-10-25 17:07:24 +0000651 Result += prefix;
652 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
653 Result += "_METHODS_";
654 Result += ClassName;
655 Result += " __attribute__ ((section (\"__OBJC, __";
656 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000657 Result += "_meth\")))= ";
658 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
659
660 Result += "\t,{{(SEL)\"";
661 Result += Methods[0]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000662 std::string MethodTypeString;
663 Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
664 Result += "\", \"";
665 Result += MethodTypeString;
666 Result += "\", 0}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000667 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000668 // TODO: Need method address as 3rd initializer.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000669 Result += "\t ,{(SEL)\"";
670 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000671 std::string MethodTypeString;
672 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
673 Result += "\", \"";
674 Result += MethodTypeString;
675 Result += "\", 0}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000676 }
677 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000678 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000679}
680
681/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
682void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
683 int NumProtocols,
684 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000685 const char *ClassName,
686 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000687 static bool objc_protocol_methods = false;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000688 if (NumProtocols > 0) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000689 for (int i = 0; i < NumProtocols; i++) {
690 ObjcProtocolDecl *PDecl = Protocols[i];
691 // Output struct protocol_methods holder of method selector and type.
692 if (!objc_protocol_methods &&
693 (PDecl->getNumInstanceMethods() > 0
694 || PDecl->getNumClassMethods() > 0)) {
695 /* struct protocol_methods {
696 SEL _cmd;
697 char *method_types;
698 }
699 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000700 Result += "\nstruct protocol_methods {\n";
701 Result += "\tSEL _cmd;\n";
702 Result += "\tchar *method_types;\n";
703 Result += "};\n";
704
705 /* struct _objc_protocol_method_list {
706 int protocol_method_count;
707 struct protocol_methods protocols[];
708 }
709 */
710 Result += "\nstruct _objc_protocol_method_list {\n";
711 Result += "\tint protocol_method_count;\n";
712 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000713 objc_protocol_methods = true;
714 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000715
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000716 // Output instance methods declared in this protocol.
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000717 int NumMethods = PDecl->getNumInstanceMethods();
718 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000719 Result += "\nstatic struct _objc_protocol_method_list "
720 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
721 Result += PDecl->getName();
722 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
723 "{\n\t" + utostr(NumMethods) + "\n";
724
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000725 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000726 Result += "\t,{{(SEL)\"";
727 Result += Methods[0]->getSelector().getName().c_str();
728 Result += "\", \"\"}\n";
729
730 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000731 Result += "\t ,{(SEL)\"";
732 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000733 std::string MethodTypeString;
734 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
735 Result += "\", \"";
736 Result += MethodTypeString;
737 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000738 }
739 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000740 }
741
742 // Output class methods declared in this protocol.
743 NumMethods = PDecl->getNumClassMethods();
744 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000745 Result += "\nstatic struct _objc_protocol_method_list "
746 "_OBJC_PROTOCOL_CLASS_METHODS_";
747 Result += PDecl->getName();
748 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
749 "{\n\t";
750 Result += utostr(NumMethods);
751 Result += "\n";
752
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000753 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000754 Result += "\t,{{(SEL)\"";
755 Result += Methods[0]->getSelector().getName().c_str();
756 Result += "\", \"\"}\n";
757
758 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000759 Result += "\t ,{(SEL)\"";
760 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000761 std::string MethodTypeString;
762 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
763 Result += "\", \"";
764 Result += MethodTypeString;
765 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000766 }
767 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000768 }
769 // Output:
770 /* struct _objc_protocol {
771 // Objective-C 1.0 extensions
772 struct _objc_protocol_extension *isa;
773 char *protocol_name;
774 struct _objc_protocol **protocol_list;
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000775 struct _objc_protocol_method_list *instance_methods;
776 struct _objc_protocol_method_list *class_methods;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000777 };
778 */
779 static bool objc_protocol = false;
780 if (!objc_protocol) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000781 Result += "\nstruct _objc_protocol {\n";
782 Result += "\tstruct _objc_protocol_extension *isa;\n";
783 Result += "\tchar *protocol_name;\n";
784 Result += "\tstruct _objc_protocol **protocol_list;\n";
785 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
786 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
787 Result += "};\n";
788
789 /* struct _objc_protocol_list {
790 struct _objc_protocol_list *next;
791 int protocol_count;
792 struct _objc_protocol *class_protocols[];
793 }
794 */
795 Result += "\nstruct _objc_protocol_list {\n";
796 Result += "\tstruct _objc_protocol_list *next;\n";
797 Result += "\tint protocol_count;\n";
798 Result += "\tstruct _objc_protocol *class_protocols[];\n";
799 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000800 objc_protocol = true;
801 }
802
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000803 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
804 Result += PDecl->getName();
805 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
806 "{\n\t0, \"";
807 Result += PDecl->getName();
808 Result += "\", 0, ";
809 if (PDecl->getInstanceMethods() > 0) {
810 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
811 Result += PDecl->getName();
812 Result += ", ";
813 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000814 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000815 Result += "0, ";
816 if (PDecl->getClassMethods() > 0) {
817 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
818 Result += PDecl->getName();
819 Result += "\n";
820 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000821 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000822 Result += "0\n";
823 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000824 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000825 // Output the top lovel protocol meta-data for the class.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000826 Result += "\nstatic struct _objc_protocol_list _OBJC_";
827 Result += prefix;
828 Result += "_PROTOCOLS_";
829 Result += ClassName;
830 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
831 "{\n\t0, ";
832 Result += utostr(NumProtocols);
833 Result += "\n";
834
835 Result += "\t,{&_OBJC_PROTOCOL_";
836 Result += Protocols[0]->getName();
837 Result += " \n";
838
839 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000840 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000841 Result += "\t ,&_OBJC_PROTOCOL_";
842 Result += PDecl->getName();
843 Result += "\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000844 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000845 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000846 }
847}
848
849/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
850/// implementation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000851void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
852 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000853 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
854 // Find category declaration for this implementation.
855 ObjcCategoryDecl *CDecl;
856 for (CDecl = ClassDecl->getCategoryList(); CDecl;
857 CDecl = CDecl->getNextClassCategory())
858 if (CDecl->getIdentifier() == IDecl->getIdentifier())
859 break;
860 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
861
862 char *FullCategoryName = (char*)alloca(
863 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
864 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
865
866 // Build _objc_method_list for class's instance methods if needed
867 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
868 IDecl->getNumInstanceMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000869 true,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000870 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000871
872 // Build _objc_method_list for class's class methods if needed
873 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
874 IDecl->getNumClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000875 false,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000876 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000877
878 // Protocols referenced in class declaration?
879 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
880 CDecl->getNumReferencedProtocols(),
881 "CATEGORY",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000882 FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000883
884 /* struct _objc_category {
885 char *category_name;
886 char *class_name;
887 struct _objc_method_list *instance_methods;
888 struct _objc_method_list *class_methods;
889 struct _objc_protocol_list *protocols;
890 // Objective-C 1.0 extensions
891 uint32_t size; // sizeof (struct _objc_category)
892 struct _objc_property_list *instance_properties; // category's own
893 // @property decl.
894 };
895 */
896
897 static bool objc_category = false;
898 if (!objc_category) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000899 Result += "\nstruct _objc_category {\n";
900 Result += "\tchar *category_name;\n";
901 Result += "\tchar *class_name;\n";
902 Result += "\tstruct _objc_method_list *instance_methods;\n";
903 Result += "\tstruct _objc_method_list *class_methods;\n";
904 Result += "\tstruct _objc_protocol_list *protocols;\n";
905 Result += "\tunsigned int size;\n";
906 Result += "\tstruct _objc_property_list *instance_properties;\n";
907 Result += "};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000908 objc_category = true;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000909 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000910 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
911 Result += FullCategoryName;
912 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
913 Result += IDecl->getName();
914 Result += "\"\n\t, \"";
915 Result += ClassDecl->getName();
916 Result += "\"\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000917
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000918 if (IDecl->getNumInstanceMethods() > 0) {
919 Result += "\t, (struct _objc_method_list *)"
920 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
921 Result += FullCategoryName;
922 Result += "\n";
923 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000924 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000925 Result += "\t, 0\n";
926 if (IDecl->getNumClassMethods() > 0) {
927 Result += "\t, (struct _objc_method_list *)"
928 "&_OBJC_CATEGORY_CLASS_METHODS_";
929 Result += FullCategoryName;
930 Result += "\n";
931 }
932 else
933 Result += "\t, 0\n";
934
935 if (CDecl->getNumReferencedProtocols() > 0) {
936 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
937 Result += FullCategoryName;
938 Result += "\n";
939 }
940 else
941 Result += "\t, 0\n";
942 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000943}
944
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000945/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
946/// ivar offset.
947void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
948 ObjcIvarDecl *ivar,
949 std::string &Result) {
950 Result += "offsetof(struct _interface_";
951 Result += IDecl->getName();
952 Result += ", ";
953 Result += ivar->getName();
954 Result += ")";
955}
956
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000957//===----------------------------------------------------------------------===//
958// Meta Data Emission
959//===----------------------------------------------------------------------===//
960
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000961void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
962 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000963 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
964
965 // Build _objc_ivar_list metadata for classes ivars if needed
966 int NumIvars = IDecl->getImplDeclNumIvars() > 0
967 ? IDecl->getImplDeclNumIvars()
968 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
969
Fariborz Jahanian4d733d32007-10-26 23:09:28 +0000970 SynthesizeObjcInternalStruct(CDecl, Result);
971
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000972 if (NumIvars > 0) {
973 static bool objc_ivar = false;
974 if (!objc_ivar) {
975 /* struct _objc_ivar {
976 char *ivar_name;
977 char *ivar_type;
978 int ivar_offset;
979 };
980 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000981 Result += "\nstruct _objc_ivar {\n";
982 Result += "\tchar *ivar_name;\n";
983 Result += "\tchar *ivar_type;\n";
984 Result += "\tint ivar_offset;\n";
985 Result += "};\n";
986
987 /* struct _objc_ivar_list {
988 int ivar_count;
989 struct _objc_ivar ivar_list[];
990 };
991 */
992 Result += "\nstruct _objc_ivar_list {\n";
993 Result += "\tint ivar_count;\n";
994 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000995 objc_ivar = true;
996 }
997
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000998 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
999 Result += IDecl->getName();
1000 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
1001 "{\n\t";
1002 Result += utostr(NumIvars);
1003 Result += "\n";
1004
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001005 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
1006 ? IDecl->getImplDeclIVars()
1007 : CDecl->getIntfDeclIvars();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001008 Result += "\t,{{\"";
1009 Result += Ivars[0]->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00001010 Result += "\", \"";
1011 std::string StrEncoding;
1012 Context->getObjcEncodingForType(Ivars[0]->getType(), StrEncoding);
1013 Result += StrEncoding;
1014 Result += "\", ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001015 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
1016 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001017 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001018 Result += "\t ,{\"";
1019 Result += Ivars[i]->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00001020 Result += "\", \"";
1021 std::string StrEncoding;
1022 Context->getObjcEncodingForType(Ivars[i]->getType(), StrEncoding);
1023 Result += StrEncoding;
1024 Result += "\", ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001025 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
1026 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001027 }
1028
1029 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001030 }
1031
1032 // Build _objc_method_list for class's instance methods if needed
1033 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1034 IDecl->getNumInstanceMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001035 true,
1036 "", IDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001037
1038 // Build _objc_method_list for class's class methods if needed
1039 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +00001040 IDecl->getNumClassMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001041 false,
1042 "", IDecl->getName(), Result);
1043
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001044 // Protocols referenced in class declaration?
1045 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1046 CDecl->getNumIntfRefProtocols(),
1047 "CLASS",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001048 CDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001049
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001050
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001051 // Declaration of class/meta-class metadata
1052 /* struct _objc_class {
1053 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001054 const char *super_class_name;
1055 char *name;
1056 long version;
1057 long info;
1058 long instance_size;
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001059 struct _objc_ivar_list *ivars;
1060 struct _objc_method_list *methods;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001061 struct objc_cache *cache;
1062 struct objc_protocol_list *protocols;
1063 const char *ivar_layout;
1064 struct _objc_class_ext *ext;
1065 };
1066 */
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001067 static bool objc_class = false;
1068 if (!objc_class) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001069 Result += "\nstruct _objc_class {\n";
1070 Result += "\tstruct _objc_class *isa;\n";
1071 Result += "\tconst char *super_class_name;\n";
1072 Result += "\tchar *name;\n";
1073 Result += "\tlong version;\n";
1074 Result += "\tlong info;\n";
1075 Result += "\tlong instance_size;\n";
1076 Result += "\tstruct _objc_ivar_list *ivars;\n";
1077 Result += "\tstruct _objc_method_list *methods;\n";
1078 Result += "\tstruct objc_cache *cache;\n";
1079 Result += "\tstruct _objc_protocol_list *protocols;\n";
1080 Result += "\tconst char *ivar_layout;\n";
1081 Result += "\tstruct _objc_class_ext *ext;\n";
1082 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001083 objc_class = true;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001084 }
1085
1086 // Meta-class metadata generation.
1087 ObjcInterfaceDecl *RootClass = 0;
1088 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
1089 while (SuperClass) {
1090 RootClass = SuperClass;
1091 SuperClass = SuperClass->getSuperClass();
1092 }
1093 SuperClass = CDecl->getSuperClass();
1094
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001095 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
1096 Result += CDecl->getName();
1097 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
1098 "{\n\t(struct _objc_class *)\"";
1099 Result += (RootClass ? RootClass->getName() : CDecl->getName());
1100 Result += "\"";
1101
1102 if (SuperClass) {
1103 Result += ", \"";
1104 Result += SuperClass->getName();
1105 Result += "\", \"";
1106 Result += CDecl->getName();
1107 Result += "\"";
1108 }
1109 else {
1110 Result += ", 0, \"";
1111 Result += CDecl->getName();
1112 Result += "\"";
1113 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001114 // TODO: 'ivars' field for root class is currently set to 0.
1115 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001116 Result += ", 0,2, sizeof(struct _objc_class), 0";
1117 if (CDecl->getNumClassMethods() > 0) {
1118 Result += "\n\t, &_OBJC_CLASS_METHODS_";
1119 Result += CDecl->getName();
1120 Result += "\n";
1121 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001122 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001123 Result += ", 0\n";
1124 if (CDecl->getNumIntfRefProtocols() > 0) {
1125 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
1126 Result += CDecl->getName();
1127 Result += ",0,0\n";
1128 }
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001129 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001130 Result += "\t,0,0,0,0\n";
1131 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001132
1133 // class metadata generation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001134 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
1135 Result += CDecl->getName();
1136 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
1137 "{\n\t&_OBJC_METACLASS_";
1138 Result += CDecl->getName();
1139 if (SuperClass) {
1140 Result += ", \"";
1141 Result += SuperClass->getName();
1142 Result += "\", \"";
1143 Result += CDecl->getName();
1144 Result += "\"";
1145 }
1146 else {
1147 Result += ", 0, \"";
1148 Result += CDecl->getName();
1149 Result += "\"";
1150 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001151 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00001152 Result += ", 0,1";
1153 if (!ObjcSynthesizedStructs.count(CDecl))
1154 Result += ",0";
1155 else {
1156 // class has size. Must synthesize its size.
1157 Result += ",sizeof(struct _interface_";
1158 Result += CDecl->getName();
1159 Result += ")";
1160 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001161 if (NumIvars > 0) {
1162 Result += ", &_OBJC_INSTANCE_VARIABLES_";
1163 Result += CDecl->getName();
1164 Result += "\n\t";
1165 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001166 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001167 Result += ",0";
1168 if (IDecl->getNumInstanceMethods() > 0) {
1169 Result += ", &_OBJC_INSTANCE_METHODS_";
1170 Result += CDecl->getName();
1171 Result += ", 0\n\t";
1172 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001173 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001174 Result += ",0,0";
1175 if (CDecl->getNumIntfRefProtocols() > 0) {
1176 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1177 Result += CDecl->getName();
1178 Result += ", 0,0\n";
1179 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001180 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001181 Result += ",0,0,0\n";
1182 Result += "};\n";
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001183}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001184
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001185void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001186 int ClsDefCount = ClassImplementation.size();
1187 int CatDefCount = CategoryImplementation.size();
1188 if (ClsDefCount == 0 && CatDefCount == 0)
1189 return;
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001190
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001191 // TODO: This is temporary until we decide how to access objc types in a
1192 // c program
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001193 Result += "#include <Objc/objc.h>\n";
1194 // This is needed for use of offsetof
1195 Result += "#include <stddef.h>\n";
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001196
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001197 // For each implemented class, write out all its meta data.
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001198 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001199 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001200
1201 // For each implemented category, write out all its meta data.
1202 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001203 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001204
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001205 // Write objc_symtab metadata
1206 /*
1207 struct _objc_symtab
1208 {
1209 long sel_ref_cnt;
1210 SEL *refs;
1211 short cls_def_cnt;
1212 short cat_def_cnt;
1213 void *defs[cls_def_cnt + cat_def_cnt];
1214 };
1215 */
1216
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001217 Result += "\nstruct _objc_symtab {\n";
1218 Result += "\tlong sel_ref_cnt;\n";
1219 Result += "\tSEL *refs;\n";
1220 Result += "\tshort cls_def_cnt;\n";
1221 Result += "\tshort cat_def_cnt;\n";
1222 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1223 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001224
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001225 Result += "static struct _objc_symtab "
1226 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1227 Result += "\t0, 0, " + utostr(ClsDefCount)
1228 + ", " + utostr(CatDefCount) + "\n";
1229 for (int i = 0; i < ClsDefCount; i++) {
1230 Result += "\t,&_OBJC_CLASS_";
1231 Result += ClassImplementation[i]->getName();
1232 Result += "\n";
1233 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001234
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001235 for (int i = 0; i < CatDefCount; i++) {
1236 Result += "\t,&_OBJC_CATEGORY_";
1237 Result += CategoryImplementation[i]->getClassInterface()->getName();
1238 Result += "_";
1239 Result += CategoryImplementation[i]->getName();
1240 Result += "\n";
1241 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001242
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001243 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001244
1245 // Write objc_module metadata
1246
1247 /*
1248 struct _objc_module {
1249 long version;
1250 long size;
1251 const char *name;
1252 struct _objc_symtab *symtab;
1253 }
1254 */
1255
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001256 Result += "\nstruct _objc_module {\n";
1257 Result += "\tlong version;\n";
1258 Result += "\tlong size;\n";
1259 Result += "\tconst char *name;\n";
1260 Result += "\tstruct _objc_symtab *symtab;\n";
1261 Result += "};\n\n";
1262 Result += "static struct _objc_module "
1263 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001264 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1265 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001266 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001267}
Chris Lattner311ff022007-10-16 22:36:42 +00001268