blob: 026c5d62c4e748a6075f451af761efa74fb24b57 [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;
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000332 if (!ObjcForwardDecls.count(ClassDecl)) {
333 // we haven't seen a forward decl - generate a typedef.
334 ResultStr += "typedef struct ";
335 ResultStr += ClassDecl->getName();
336 ResultStr += " ";
337 ResultStr += ClassDecl->getName();
338 ResultStr += ";";
339
340 // Mark this typedef as having been generated.
341 ObjcForwardDecls.insert(ClassDecl);
342 }
Steve Narofff908a872007-10-30 02:23:23 +0000343 SynthesizeObjcInternalStruct(ClassDecl, ResultStr);
344
Steve Naroff2feac5e2007-10-30 03:43:13 +0000345 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
Steve Narofff908a872007-10-30 02:23:23 +0000346 ResultStr.c_str(), ResultStr.size());
347
Steve Naroff423cb562007-10-30 13:30:57 +0000348 RewriteMethods(ClassDecl->getNumInstanceMethods(),
349 ClassDecl->getInstanceMethods());
350 RewriteMethods(ClassDecl->getNumClassMethods(),
351 ClassDecl->getClassMethods());
Steve Naroffbef11852007-10-26 20:53:56 +0000352
Steve Naroff2feac5e2007-10-30 03:43:13 +0000353 // Lastly, comment out the @end.
354 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroffbef11852007-10-26 20:53:56 +0000355}
356
Chris Lattnerf04da132007-10-24 17:06:59 +0000357//===----------------------------------------------------------------------===//
358// Function Body / Expression rewriting
359//===----------------------------------------------------------------------===//
360
Chris Lattnere64b7772007-10-24 16:57:36 +0000361Stmt *RewriteTest::RewriteFunctionBody(Stmt *S) {
Chris Lattner311ff022007-10-16 22:36:42 +0000362 // Otherwise, just rewrite all children.
363 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
364 CI != E; ++CI)
Chris Lattner50754772007-10-17 21:28:00 +0000365 if (*CI)
Chris Lattnere64b7772007-10-24 16:57:36 +0000366 *CI = RewriteFunctionBody(*CI);
Steve Naroffebf2b562007-10-23 23:50:29 +0000367
368 // Handle specific things.
369 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
370 return RewriteAtEncode(AtEncode);
371
Steve Naroff934f2762007-10-24 22:48:43 +0000372 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
373 // Before we rewrite it, put the original message expression in a comment.
374 SourceLocation startLoc = MessExpr->getLocStart();
375 SourceLocation endLoc = MessExpr->getLocEnd();
376
377 const char *startBuf = SM->getCharacterData(startLoc);
378 const char *endBuf = SM->getCharacterData(endLoc);
379
380 std::string messString;
381 messString += "// ";
382 messString.append(startBuf, endBuf-startBuf+1);
383 messString += "\n";
Steve Naroffbef11852007-10-26 20:53:56 +0000384
Steve Naroff934f2762007-10-24 22:48:43 +0000385 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
386 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
387 // Tried this, but it didn't work either...
Steve Naroff752d6ef2007-10-30 16:42:30 +0000388 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffebf2b562007-10-23 23:50:29 +0000389 return RewriteMessageExpr(MessExpr);
Steve Naroff934f2762007-10-24 22:48:43 +0000390 }
Chris Lattnere64b7772007-10-24 16:57:36 +0000391 // Return this stmt unmodified.
392 return S;
Chris Lattner311ff022007-10-16 22:36:42 +0000393}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +0000394
Chris Lattnere64b7772007-10-24 16:57:36 +0000395Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattner01c57482007-10-17 22:35:30 +0000396 // Create a new string expression.
397 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000398 std::string StrEncoding;
399 Context->getObjcEncodingForType(Exp->getEncodedType(), StrEncoding);
400 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
401 StrEncoding.length(), false, StrType,
Chris Lattner01c57482007-10-17 22:35:30 +0000402 SourceLocation(), SourceLocation());
403 Rewrite.ReplaceStmt(Exp, Replacement);
Chris Lattnere64b7772007-10-24 16:57:36 +0000404 delete Exp;
405 return Replacement;
Chris Lattner311ff022007-10-16 22:36:42 +0000406}
407
Steve Naroff934f2762007-10-24 22:48:43 +0000408CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
409 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffebf2b562007-10-23 23:50:29 +0000410 // Get the type, we will need to reference it in a couple spots.
Steve Naroff934f2762007-10-24 22:48:43 +0000411 QualType msgSendType = FD->getType();
Steve Naroffebf2b562007-10-23 23:50:29 +0000412
413 // Create a reference to the objc_msgSend() declaration.
Steve Naroff934f2762007-10-24 22:48:43 +0000414 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffebf2b562007-10-23 23:50:29 +0000415
416 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerf04da132007-10-24 17:06:59 +0000417 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffebf2b562007-10-23 23:50:29 +0000418 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
419
420 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattnere64b7772007-10-24 16:57:36 +0000421
Steve Naroff934f2762007-10-24 22:48:43 +0000422 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
423}
424
Steve Naroff9165ad32007-10-31 04:38:33 +0000425bool RewriteTest::functionReferencesAnyObjcQualifiedInterfaceTypes(
426 const FunctionTypeProto *proto) {
Steve Naroff498856c2007-10-31 16:03:04 +0000427 QualType resultType = proto->getResultType();
428
429 if (resultType == 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 = resultType->getAsPointerType()) {
Steve Naroff9165ad32007-10-31 04:38:33 +0000433 Type *pointeeType = pType->getPointeeType().getTypePtr();
434 if (isa<ObjcQualifiedInterfaceType>(pointeeType))
435 return true; // we have "Class <Protocol> *".
436 }
437 // Now check arguments.
438 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
Steve Naroff498856c2007-10-31 16:03:04 +0000439 QualType argType = proto->getArgType(i);
440 if (argType == Context->getObjcIdType()) {
441 // FIXME: we don't currently represent "id <Protocol>" in the type system.
442 // Implement a heuristic here (until we do).
443 } else if (const PointerType *pType = argType->getAsPointerType()) {
Steve Naroff9165ad32007-10-31 04:38:33 +0000444 Type *pointeeType = pType->getPointeeType().getTypePtr();
445 if (isa<ObjcQualifiedInterfaceType>(pointeeType))
446 return true;
447 }
448 }
Steve Naroff9165ad32007-10-31 04:38:33 +0000449 return false;
450}
451
Steve Naroff09b266e2007-10-30 23:14:51 +0000452void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
453 // declared in <objc/objc.h>
Steve Naroff9165ad32007-10-31 04:38:33 +0000454 if (strcmp(FD->getName(), "sel_getUid") == 0) {
Steve Naroff09b266e2007-10-30 23:14:51 +0000455 SelGetUidFunctionDecl = FD;
Steve Naroff9165ad32007-10-31 04:38:33 +0000456 return;
457 }
458 // Check for ObjC 'id' and class types that have been adorned with protocol
459 // information (id<p>, C<p>*). The protocol references need to be rewritten!
460 const FunctionType *funcType = FD->getType()->getAsFunctionType();
461 assert(funcType && "missing function type");
462 const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcType);
463 if (proto && functionReferencesAnyObjcQualifiedInterfaceTypes(proto)) {
464 // FIXME: Rewrite function decl...
465 }
Steve Naroff09b266e2007-10-30 23:14:51 +0000466}
467
468// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
469void RewriteTest::SynthMsgSendFunctionDecl() {
470 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
471 llvm::SmallVector<QualType, 16> ArgTys;
472 QualType argT = Context->getObjcIdType();
473 assert(!argT.isNull() && "Can't find 'id' type");
474 ArgTys.push_back(argT);
475 argT = Context->getObjcSelType();
476 assert(!argT.isNull() && "Can't find 'SEL' type");
477 ArgTys.push_back(argT);
478 QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(),
479 &ArgTys[0], ArgTys.size(),
480 true /*isVariadic*/);
481 MsgSendFunctionDecl = new FunctionDecl(SourceLocation(),
482 msgSendIdent, msgSendType,
483 FunctionDecl::Extern, false, 0);
484}
485
486// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
487void RewriteTest::SynthGetClassFunctionDecl() {
488 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
489 llvm::SmallVector<QualType, 16> ArgTys;
490 ArgTys.push_back(Context->getPointerType(
491 Context->CharTy.getQualifiedType(QualType::Const)));
492 QualType getClassType = Context->getFunctionType(Context->getObjcIdType(),
493 &ArgTys[0], ArgTys.size(),
494 false /*isVariadic*/);
495 GetClassFunctionDecl = new FunctionDecl(SourceLocation(),
496 getClassIdent, getClassType,
497 FunctionDecl::Extern, false, 0);
498}
499
Steve Naroff934f2762007-10-24 22:48:43 +0000500Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
Steve Naroff934f2762007-10-24 22:48:43 +0000501 assert(SelGetUidFunctionDecl && "Can't find sel_getUid() decl");
Steve Naroff09b266e2007-10-30 23:14:51 +0000502 if (!MsgSendFunctionDecl)
503 SynthMsgSendFunctionDecl();
504 if (!GetClassFunctionDecl)
505 SynthGetClassFunctionDecl();
Steve Naroff934f2762007-10-24 22:48:43 +0000506
507 // Synthesize a call to objc_msgSend().
508 llvm::SmallVector<Expr*, 8> MsgExprs;
509 IdentifierInfo *clsName = Exp->getClassName();
510
511 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
512 if (clsName) { // class message.
513 llvm::SmallVector<Expr*, 8> ClsExprs;
514 QualType argType = Context->getPointerType(Context->CharTy);
515 ClsExprs.push_back(new StringLiteral(clsName->getName(),
516 clsName->getLength(),
517 false, argType, SourceLocation(),
518 SourceLocation()));
519 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
520 &ClsExprs[0], ClsExprs.size());
521 MsgExprs.push_back(Cls);
522 } else // instance message.
523 MsgExprs.push_back(Exp->getReceiver());
524
525 // Create a call to sel_getUid("selName"), it will be the 2nd argument.
526 llvm::SmallVector<Expr*, 8> SelExprs;
527 QualType argType = Context->getPointerType(Context->CharTy);
528 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
529 Exp->getSelector().getName().size(),
530 false, argType, SourceLocation(),
531 SourceLocation()));
532 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
533 &SelExprs[0], SelExprs.size());
534 MsgExprs.push_back(SelExp);
535
536 // Now push any user supplied arguments.
537 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
538 MsgExprs.push_back(Exp->getArg(i));
539 // We've transferred the ownership to MsgExprs. Null out the argument in
540 // the original expression, since we will delete it below.
541 Exp->setArg(i, 0);
542 }
543 CallExpr *MessExp = SynthesizeCallToFunctionDecl(MsgSendFunctionDecl,
544 &MsgExprs[0], MsgExprs.size());
545 // Now do the actual rewrite.
546 Rewrite.ReplaceStmt(Exp, MessExp);
547
Chris Lattnere64b7772007-10-24 16:57:36 +0000548 delete Exp;
Steve Naroff934f2762007-10-24 22:48:43 +0000549 return MessExp;
Steve Naroffebf2b562007-10-23 23:50:29 +0000550}
551
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000552/// SynthesizeObjcInternalStruct - Rewrite one internal struct corresponding to
553/// an objective-c class with ivars.
554void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
555 std::string &Result) {
556 assert(CDecl && "Class missing in SynthesizeObjcInternalStruct");
557 assert(CDecl->getName() && "Name missing in SynthesizeObjcInternalStruct");
Fariborz Jahanian212b7682007-10-31 23:08:24 +0000558 // Do not synthesize more than once.
559 if (ObjcSynthesizedStructs.count(CDecl))
560 return;
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000561 ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass();
562 if (RCDecl && !ObjcSynthesizedStructs.count(RCDecl)) {
563 // Do it for the root
564 SynthesizeObjcInternalStruct(RCDecl, Result);
565 }
566
567 int NumIvars = CDecl->getIntfDeclNumIvars();
Fariborz Jahanianaff56d02007-10-31 22:57:04 +0000568 // If no ivars and no root or if its root, directly or indirectly,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000569 // have no ivars (thus not synthesized) then no need to synthesize this class.
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000570 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
571 return;
572
573 Result += "\nstruct _interface_";
574 Result += CDecl->getName();
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000575 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000576 Result += " {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000577 Result += "\tstruct _interface_";
578 Result += RCDecl->getName();
579 Result += " _";
580 Result += RCDecl->getName();
581 Result += ";\n";
582 }
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000583 else
584 Result += " {";
Steve Naroff8749be52007-10-31 22:11:35 +0000585
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000586 if (NumIvars > 0) {
587 SourceLocation LocStart = CDecl->getLocStart();
588 SourceLocation LocEnd = CDecl->getLocEnd();
589
590 const char *startBuf = SM->getCharacterData(LocStart);
591 const char *endBuf = SM->getCharacterData(LocEnd);
592 startBuf = strchr(startBuf, '{');
593 assert((startBuf && endBuf)
594 && "SynthesizeObjcInternalStruct - malformed @interface");
595 startBuf++; // past '{'
596 while (startBuf < endBuf) {
597 if (*startBuf == '@') {
598 startBuf = strchr(startBuf, 'p');
599 // FIXME: presence of @public, etc. inside comment results in
600 // this transformation as well, which is still correct c-code.
601 if (!strncmp(startBuf, "public", strlen("public"))) {
602 startBuf += strlen("public");
603 Result += "/* @public */";
604 }
605 else if (!strncmp(startBuf, "private", strlen("private"))) {
606 startBuf += strlen("private");
607 Result += "/* @private */";
608 }
609 else if (!strncmp(startBuf, "protected", strlen("protected"))) {
610 startBuf += strlen("protected");
611 Result += "/* @protected */";
612 }
613 }
614 Result += *startBuf++;
615 }
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000616 }
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000617
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000618 Result += "};\n";
619 // Mark this struct as having been generated.
620 if (!ObjcSynthesizedStructs.insert(CDecl))
Fariborz Jahanianaff56d02007-10-31 22:57:04 +0000621 assert(false && "struct already synthesize- SynthesizeObjcInternalStruct");
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000622}
623
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000624// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
625/// class methods.
626void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
627 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000628 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000629 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000630 const char *ClassName,
631 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000632 static bool objc_impl_method = false;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000633 if (NumMethods > 0 && !objc_impl_method) {
634 /* struct _objc_method {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000635 SEL _cmd;
636 char *method_types;
637 void *_imp;
638 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000639 */
Chris Lattner158ecb92007-10-25 17:07:24 +0000640 Result += "\nstruct _objc_method {\n";
641 Result += "\tSEL _cmd;\n";
642 Result += "\tchar *method_types;\n";
643 Result += "\tvoid *_imp;\n";
644 Result += "};\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000645
646 /* struct _objc_method_list {
647 struct _objc_method_list *next_method;
648 int method_count;
649 struct _objc_method method_list[];
650 }
651 */
652 Result += "\nstruct _objc_method_list {\n";
653 Result += "\tstruct _objc_method_list *next_method;\n";
654 Result += "\tint method_count;\n";
655 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000656 objc_impl_method = true;
Fariborz Jahanian776d6ff2007-10-19 00:36:46 +0000657 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000658 // Build _objc_method_list for class's methods if needed
659 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000660 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattner158ecb92007-10-25 17:07:24 +0000661 Result += prefix;
662 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
663 Result += "_METHODS_";
664 Result += ClassName;
665 Result += " __attribute__ ((section (\"__OBJC, __";
666 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000667 Result += "_meth\")))= ";
668 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
669
670 Result += "\t,{{(SEL)\"";
671 Result += Methods[0]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000672 std::string MethodTypeString;
673 Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
674 Result += "\", \"";
675 Result += MethodTypeString;
676 Result += "\", 0}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000677 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000678 // TODO: Need method address as 3rd initializer.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000679 Result += "\t ,{(SEL)\"";
680 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000681 std::string MethodTypeString;
682 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
683 Result += "\", \"";
684 Result += MethodTypeString;
685 Result += "\", 0}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000686 }
687 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000688 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000689}
690
691/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
692void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
693 int NumProtocols,
694 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000695 const char *ClassName,
696 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000697 static bool objc_protocol_methods = false;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000698 if (NumProtocols > 0) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000699 for (int i = 0; i < NumProtocols; i++) {
700 ObjcProtocolDecl *PDecl = Protocols[i];
701 // Output struct protocol_methods holder of method selector and type.
702 if (!objc_protocol_methods &&
703 (PDecl->getNumInstanceMethods() > 0
704 || PDecl->getNumClassMethods() > 0)) {
705 /* struct protocol_methods {
706 SEL _cmd;
707 char *method_types;
708 }
709 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000710 Result += "\nstruct protocol_methods {\n";
711 Result += "\tSEL _cmd;\n";
712 Result += "\tchar *method_types;\n";
713 Result += "};\n";
714
715 /* struct _objc_protocol_method_list {
716 int protocol_method_count;
717 struct protocol_methods protocols[];
718 }
719 */
720 Result += "\nstruct _objc_protocol_method_list {\n";
721 Result += "\tint protocol_method_count;\n";
722 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000723 objc_protocol_methods = true;
724 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000725
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000726 // Output instance methods declared in this protocol.
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000727 int NumMethods = PDecl->getNumInstanceMethods();
728 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000729 Result += "\nstatic struct _objc_protocol_method_list "
730 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
731 Result += PDecl->getName();
732 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
733 "{\n\t" + utostr(NumMethods) + "\n";
734
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000735 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000736 Result += "\t,{{(SEL)\"";
737 Result += Methods[0]->getSelector().getName().c_str();
738 Result += "\", \"\"}\n";
739
740 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000741 Result += "\t ,{(SEL)\"";
742 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000743 std::string MethodTypeString;
744 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
745 Result += "\", \"";
746 Result += MethodTypeString;
747 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000748 }
749 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000750 }
751
752 // Output class methods declared in this protocol.
753 NumMethods = PDecl->getNumClassMethods();
754 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000755 Result += "\nstatic struct _objc_protocol_method_list "
756 "_OBJC_PROTOCOL_CLASS_METHODS_";
757 Result += PDecl->getName();
758 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
759 "{\n\t";
760 Result += utostr(NumMethods);
761 Result += "\n";
762
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000763 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000764 Result += "\t,{{(SEL)\"";
765 Result += Methods[0]->getSelector().getName().c_str();
766 Result += "\", \"\"}\n";
767
768 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000769 Result += "\t ,{(SEL)\"";
770 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000771 std::string MethodTypeString;
772 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
773 Result += "\", \"";
774 Result += MethodTypeString;
775 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000776 }
777 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000778 }
779 // Output:
780 /* struct _objc_protocol {
781 // Objective-C 1.0 extensions
782 struct _objc_protocol_extension *isa;
783 char *protocol_name;
784 struct _objc_protocol **protocol_list;
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000785 struct _objc_protocol_method_list *instance_methods;
786 struct _objc_protocol_method_list *class_methods;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000787 };
788 */
789 static bool objc_protocol = false;
790 if (!objc_protocol) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000791 Result += "\nstruct _objc_protocol {\n";
792 Result += "\tstruct _objc_protocol_extension *isa;\n";
793 Result += "\tchar *protocol_name;\n";
794 Result += "\tstruct _objc_protocol **protocol_list;\n";
795 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
796 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
797 Result += "};\n";
798
799 /* struct _objc_protocol_list {
800 struct _objc_protocol_list *next;
801 int protocol_count;
802 struct _objc_protocol *class_protocols[];
803 }
804 */
805 Result += "\nstruct _objc_protocol_list {\n";
806 Result += "\tstruct _objc_protocol_list *next;\n";
807 Result += "\tint protocol_count;\n";
808 Result += "\tstruct _objc_protocol *class_protocols[];\n";
809 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000810 objc_protocol = true;
811 }
812
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000813 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
814 Result += PDecl->getName();
815 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
816 "{\n\t0, \"";
817 Result += PDecl->getName();
818 Result += "\", 0, ";
819 if (PDecl->getInstanceMethods() > 0) {
820 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
821 Result += PDecl->getName();
822 Result += ", ";
823 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000824 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000825 Result += "0, ";
826 if (PDecl->getClassMethods() > 0) {
827 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
828 Result += PDecl->getName();
829 Result += "\n";
830 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000831 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000832 Result += "0\n";
833 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000834 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000835 // Output the top lovel protocol meta-data for the class.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000836 Result += "\nstatic struct _objc_protocol_list _OBJC_";
837 Result += prefix;
838 Result += "_PROTOCOLS_";
839 Result += ClassName;
840 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
841 "{\n\t0, ";
842 Result += utostr(NumProtocols);
843 Result += "\n";
844
845 Result += "\t,{&_OBJC_PROTOCOL_";
846 Result += Protocols[0]->getName();
847 Result += " \n";
848
849 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000850 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000851 Result += "\t ,&_OBJC_PROTOCOL_";
852 Result += PDecl->getName();
853 Result += "\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000854 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000855 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000856 }
857}
858
859/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
860/// implementation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000861void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
862 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000863 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
864 // Find category declaration for this implementation.
865 ObjcCategoryDecl *CDecl;
866 for (CDecl = ClassDecl->getCategoryList(); CDecl;
867 CDecl = CDecl->getNextClassCategory())
868 if (CDecl->getIdentifier() == IDecl->getIdentifier())
869 break;
870 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
871
872 char *FullCategoryName = (char*)alloca(
873 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
874 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
875
876 // Build _objc_method_list for class's instance methods if needed
877 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
878 IDecl->getNumInstanceMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000879 true,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000880 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000881
882 // Build _objc_method_list for class's class methods if needed
883 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
884 IDecl->getNumClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000885 false,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000886 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000887
888 // Protocols referenced in class declaration?
889 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
890 CDecl->getNumReferencedProtocols(),
891 "CATEGORY",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000892 FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000893
894 /* struct _objc_category {
895 char *category_name;
896 char *class_name;
897 struct _objc_method_list *instance_methods;
898 struct _objc_method_list *class_methods;
899 struct _objc_protocol_list *protocols;
900 // Objective-C 1.0 extensions
901 uint32_t size; // sizeof (struct _objc_category)
902 struct _objc_property_list *instance_properties; // category's own
903 // @property decl.
904 };
905 */
906
907 static bool objc_category = false;
908 if (!objc_category) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000909 Result += "\nstruct _objc_category {\n";
910 Result += "\tchar *category_name;\n";
911 Result += "\tchar *class_name;\n";
912 Result += "\tstruct _objc_method_list *instance_methods;\n";
913 Result += "\tstruct _objc_method_list *class_methods;\n";
914 Result += "\tstruct _objc_protocol_list *protocols;\n";
915 Result += "\tunsigned int size;\n";
916 Result += "\tstruct _objc_property_list *instance_properties;\n";
917 Result += "};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000918 objc_category = true;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000919 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000920 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
921 Result += FullCategoryName;
922 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
923 Result += IDecl->getName();
924 Result += "\"\n\t, \"";
925 Result += ClassDecl->getName();
926 Result += "\"\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000927
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000928 if (IDecl->getNumInstanceMethods() > 0) {
929 Result += "\t, (struct _objc_method_list *)"
930 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
931 Result += FullCategoryName;
932 Result += "\n";
933 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000934 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000935 Result += "\t, 0\n";
936 if (IDecl->getNumClassMethods() > 0) {
937 Result += "\t, (struct _objc_method_list *)"
938 "&_OBJC_CATEGORY_CLASS_METHODS_";
939 Result += FullCategoryName;
940 Result += "\n";
941 }
942 else
943 Result += "\t, 0\n";
944
945 if (CDecl->getNumReferencedProtocols() > 0) {
946 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
947 Result += FullCategoryName;
948 Result += "\n";
949 }
950 else
951 Result += "\t, 0\n";
952 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000953}
954
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000955/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
956/// ivar offset.
957void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
958 ObjcIvarDecl *ivar,
959 std::string &Result) {
960 Result += "offsetof(struct _interface_";
961 Result += IDecl->getName();
962 Result += ", ";
963 Result += ivar->getName();
964 Result += ")";
965}
966
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000967//===----------------------------------------------------------------------===//
968// Meta Data Emission
969//===----------------------------------------------------------------------===//
970
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000971void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
972 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000973 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
974
975 // Build _objc_ivar_list metadata for classes ivars if needed
976 int NumIvars = IDecl->getImplDeclNumIvars() > 0
977 ? IDecl->getImplDeclNumIvars()
978 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
979
Fariborz Jahanian4d733d32007-10-26 23:09:28 +0000980 SynthesizeObjcInternalStruct(CDecl, Result);
981
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000982 if (NumIvars > 0) {
983 static bool objc_ivar = false;
984 if (!objc_ivar) {
985 /* struct _objc_ivar {
986 char *ivar_name;
987 char *ivar_type;
988 int ivar_offset;
989 };
990 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000991 Result += "\nstruct _objc_ivar {\n";
992 Result += "\tchar *ivar_name;\n";
993 Result += "\tchar *ivar_type;\n";
994 Result += "\tint ivar_offset;\n";
995 Result += "};\n";
996
997 /* struct _objc_ivar_list {
998 int ivar_count;
999 struct _objc_ivar ivar_list[];
1000 };
1001 */
1002 Result += "\nstruct _objc_ivar_list {\n";
1003 Result += "\tint ivar_count;\n";
1004 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001005 objc_ivar = true;
1006 }
1007
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001008 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
1009 Result += IDecl->getName();
1010 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
1011 "{\n\t";
1012 Result += utostr(NumIvars);
1013 Result += "\n";
1014
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001015 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
1016 ? IDecl->getImplDeclIVars()
1017 : CDecl->getIntfDeclIvars();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001018 Result += "\t,{{\"";
1019 Result += Ivars[0]->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00001020 Result += "\", \"";
1021 std::string StrEncoding;
1022 Context->getObjcEncodingForType(Ivars[0]->getType(), StrEncoding);
1023 Result += StrEncoding;
1024 Result += "\", ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001025 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
1026 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001027 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001028 Result += "\t ,{\"";
1029 Result += Ivars[i]->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00001030 Result += "\", \"";
1031 std::string StrEncoding;
1032 Context->getObjcEncodingForType(Ivars[i]->getType(), StrEncoding);
1033 Result += StrEncoding;
1034 Result += "\", ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001035 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
1036 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001037 }
1038
1039 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001040 }
1041
1042 // Build _objc_method_list for class's instance methods if needed
1043 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1044 IDecl->getNumInstanceMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001045 true,
1046 "", IDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001047
1048 // Build _objc_method_list for class's class methods if needed
1049 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +00001050 IDecl->getNumClassMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001051 false,
1052 "", IDecl->getName(), Result);
1053
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001054 // Protocols referenced in class declaration?
1055 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1056 CDecl->getNumIntfRefProtocols(),
1057 "CLASS",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001058 CDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001059
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001060
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001061 // Declaration of class/meta-class metadata
1062 /* struct _objc_class {
1063 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001064 const char *super_class_name;
1065 char *name;
1066 long version;
1067 long info;
1068 long instance_size;
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001069 struct _objc_ivar_list *ivars;
1070 struct _objc_method_list *methods;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001071 struct objc_cache *cache;
1072 struct objc_protocol_list *protocols;
1073 const char *ivar_layout;
1074 struct _objc_class_ext *ext;
1075 };
1076 */
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001077 static bool objc_class = false;
1078 if (!objc_class) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001079 Result += "\nstruct _objc_class {\n";
1080 Result += "\tstruct _objc_class *isa;\n";
1081 Result += "\tconst char *super_class_name;\n";
1082 Result += "\tchar *name;\n";
1083 Result += "\tlong version;\n";
1084 Result += "\tlong info;\n";
1085 Result += "\tlong instance_size;\n";
1086 Result += "\tstruct _objc_ivar_list *ivars;\n";
1087 Result += "\tstruct _objc_method_list *methods;\n";
1088 Result += "\tstruct objc_cache *cache;\n";
1089 Result += "\tstruct _objc_protocol_list *protocols;\n";
1090 Result += "\tconst char *ivar_layout;\n";
1091 Result += "\tstruct _objc_class_ext *ext;\n";
1092 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001093 objc_class = true;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001094 }
1095
1096 // Meta-class metadata generation.
1097 ObjcInterfaceDecl *RootClass = 0;
1098 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
1099 while (SuperClass) {
1100 RootClass = SuperClass;
1101 SuperClass = SuperClass->getSuperClass();
1102 }
1103 SuperClass = CDecl->getSuperClass();
1104
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001105 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
1106 Result += CDecl->getName();
1107 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
1108 "{\n\t(struct _objc_class *)\"";
1109 Result += (RootClass ? RootClass->getName() : CDecl->getName());
1110 Result += "\"";
1111
1112 if (SuperClass) {
1113 Result += ", \"";
1114 Result += SuperClass->getName();
1115 Result += "\", \"";
1116 Result += CDecl->getName();
1117 Result += "\"";
1118 }
1119 else {
1120 Result += ", 0, \"";
1121 Result += CDecl->getName();
1122 Result += "\"";
1123 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001124 // TODO: 'ivars' field for root class is currently set to 0.
1125 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001126 Result += ", 0,2, sizeof(struct _objc_class), 0";
1127 if (CDecl->getNumClassMethods() > 0) {
1128 Result += "\n\t, &_OBJC_CLASS_METHODS_";
1129 Result += CDecl->getName();
1130 Result += "\n";
1131 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001132 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001133 Result += ", 0\n";
1134 if (CDecl->getNumIntfRefProtocols() > 0) {
1135 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
1136 Result += CDecl->getName();
1137 Result += ",0,0\n";
1138 }
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001139 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001140 Result += "\t,0,0,0,0\n";
1141 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001142
1143 // class metadata generation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001144 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
1145 Result += CDecl->getName();
1146 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
1147 "{\n\t&_OBJC_METACLASS_";
1148 Result += CDecl->getName();
1149 if (SuperClass) {
1150 Result += ", \"";
1151 Result += SuperClass->getName();
1152 Result += "\", \"";
1153 Result += CDecl->getName();
1154 Result += "\"";
1155 }
1156 else {
1157 Result += ", 0, \"";
1158 Result += CDecl->getName();
1159 Result += "\"";
1160 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001161 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00001162 Result += ", 0,1";
1163 if (!ObjcSynthesizedStructs.count(CDecl))
1164 Result += ",0";
1165 else {
1166 // class has size. Must synthesize its size.
1167 Result += ",sizeof(struct _interface_";
1168 Result += CDecl->getName();
1169 Result += ")";
1170 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001171 if (NumIvars > 0) {
1172 Result += ", &_OBJC_INSTANCE_VARIABLES_";
1173 Result += CDecl->getName();
1174 Result += "\n\t";
1175 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001176 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001177 Result += ",0";
1178 if (IDecl->getNumInstanceMethods() > 0) {
1179 Result += ", &_OBJC_INSTANCE_METHODS_";
1180 Result += CDecl->getName();
1181 Result += ", 0\n\t";
1182 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001183 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001184 Result += ",0,0";
1185 if (CDecl->getNumIntfRefProtocols() > 0) {
1186 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1187 Result += CDecl->getName();
1188 Result += ", 0,0\n";
1189 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001190 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001191 Result += ",0,0,0\n";
1192 Result += "};\n";
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001193}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001194
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001195void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001196 int ClsDefCount = ClassImplementation.size();
1197 int CatDefCount = CategoryImplementation.size();
1198 if (ClsDefCount == 0 && CatDefCount == 0)
1199 return;
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001200
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001201 // TODO: This is temporary until we decide how to access objc types in a
1202 // c program
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001203 Result += "#include <Objc/objc.h>\n";
1204 // This is needed for use of offsetof
1205 Result += "#include <stddef.h>\n";
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001206
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001207 // For each implemented class, write out all its meta data.
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001208 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001209 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001210
1211 // For each implemented category, write out all its meta data.
1212 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001213 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001214
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001215 // Write objc_symtab metadata
1216 /*
1217 struct _objc_symtab
1218 {
1219 long sel_ref_cnt;
1220 SEL *refs;
1221 short cls_def_cnt;
1222 short cat_def_cnt;
1223 void *defs[cls_def_cnt + cat_def_cnt];
1224 };
1225 */
1226
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001227 Result += "\nstruct _objc_symtab {\n";
1228 Result += "\tlong sel_ref_cnt;\n";
1229 Result += "\tSEL *refs;\n";
1230 Result += "\tshort cls_def_cnt;\n";
1231 Result += "\tshort cat_def_cnt;\n";
1232 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1233 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001234
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001235 Result += "static struct _objc_symtab "
1236 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1237 Result += "\t0, 0, " + utostr(ClsDefCount)
1238 + ", " + utostr(CatDefCount) + "\n";
1239 for (int i = 0; i < ClsDefCount; i++) {
1240 Result += "\t,&_OBJC_CLASS_";
1241 Result += ClassImplementation[i]->getName();
1242 Result += "\n";
1243 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001244
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001245 for (int i = 0; i < CatDefCount; i++) {
1246 Result += "\t,&_OBJC_CATEGORY_";
1247 Result += CategoryImplementation[i]->getClassInterface()->getName();
1248 Result += "_";
1249 Result += CategoryImplementation[i]->getName();
1250 Result += "\n";
1251 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001252
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001253 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001254
1255 // Write objc_module metadata
1256
1257 /*
1258 struct _objc_module {
1259 long version;
1260 long size;
1261 const char *name;
1262 struct _objc_symtab *symtab;
1263 }
1264 */
1265
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001266 Result += "\nstruct _objc_module {\n";
1267 Result += "\tlong version;\n";
1268 Result += "\tlong size;\n";
1269 Result += "\tconst char *name;\n";
1270 Result += "\tstruct _objc_symtab *symtab;\n";
1271 Result += "};\n\n";
1272 Result += "static struct _objc_module "
1273 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001274 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1275 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001276 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001277}
Chris Lattner311ff022007-10-16 22:36:42 +00001278