blob: db2c1ee70b86724b88df8d84a17073d24925a9a6 [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"
Chris Lattner77cd2a02007-10-11 00:43:27 +000022using namespace clang;
Chris Lattner158ecb92007-10-25 17:07:24 +000023using llvm::utostr;
Chris Lattner77cd2a02007-10-11 00:43:27 +000024
Chris Lattner77cd2a02007-10-11 00:43:27 +000025namespace {
Chris Lattner8a12c272007-10-11 18:38:32 +000026 class RewriteTest : public ASTConsumer {
Chris Lattner2c64b7b2007-10-16 21:07:07 +000027 Rewriter Rewrite;
Chris Lattner01c57482007-10-17 22:35:30 +000028 ASTContext *Context;
Chris Lattner77cd2a02007-10-11 00:43:27 +000029 SourceManager *SM;
Chris Lattner8a12c272007-10-11 18:38:32 +000030 unsigned MainFileID;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000031 SourceLocation LastIncLoc;
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000032 llvm::SmallVector<ObjcImplementationDecl *, 8> ClassImplementation;
33 llvm::SmallVector<ObjcCategoryImplDecl *, 8> CategoryImplementation;
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000034 llvm::SmallPtrSet<ObjcInterfaceDecl*, 8> ObjcSynthesizedStructs;
Steve Naroffebf2b562007-10-23 23:50:29 +000035
36 FunctionDecl *MsgSendFunctionDecl;
37 FunctionDecl *GetClassFunctionDecl;
Steve Naroff934f2762007-10-24 22:48:43 +000038 FunctionDecl *SelGetUidFunctionDecl;
Steve Naroffebf2b562007-10-23 23:50:29 +000039
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000040 static const int OBJC_ABI_VERSION =7 ;
Chris Lattner77cd2a02007-10-11 00:43:27 +000041 public:
Chris Lattner01c57482007-10-17 22:35:30 +000042 void Initialize(ASTContext &context, unsigned mainFileID) {
43 Context = &context;
44 SM = &Context->SourceMgr;
Chris Lattner8a12c272007-10-11 18:38:32 +000045 MainFileID = mainFileID;
Steve Naroffebf2b562007-10-23 23:50:29 +000046 MsgSendFunctionDecl = 0;
Steve Naroffc0006092007-10-24 01:09:48 +000047 GetClassFunctionDecl = 0;
Steve Naroff934f2762007-10-24 22:48:43 +000048 SelGetUidFunctionDecl = 0;
Chris Lattner01c57482007-10-17 22:35:30 +000049 Rewrite.setSourceMgr(Context->SourceMgr);
Chris Lattner77cd2a02007-10-11 00:43:27 +000050 }
Chris Lattner8a12c272007-10-11 18:38:32 +000051
Chris Lattnerf04da132007-10-24 17:06:59 +000052 // Top Level Driver code.
53 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner2c64b7b2007-10-16 21:07:07 +000054 void HandleDeclInMainFile(Decl *D);
Chris Lattnerf04da132007-10-24 17:06:59 +000055 ~RewriteTest();
56
57 // Syntactic Rewriting.
Chris Lattner2c64b7b2007-10-16 21:07:07 +000058 void RewriteInclude(SourceLocation Loc);
Chris Lattnerf04da132007-10-24 17:06:59 +000059 void RewriteTabs();
60 void RewriteForwardClassDecl(ObjcClassDecl *Dcl);
Steve Naroffbef11852007-10-26 20:53:56 +000061 void RewriteInterfaceDecl(ObjcInterfaceDecl *Dcl);
Chris Lattner311ff022007-10-16 22:36:42 +000062
Chris Lattnerf04da132007-10-24 17:06:59 +000063 // Expression Rewriting.
Chris Lattnere64b7772007-10-24 16:57:36 +000064 Stmt *RewriteFunctionBody(Stmt *S);
65 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
66 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroff934f2762007-10-24 22:48:43 +000067 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
68 Expr **args, unsigned nargs);
Chris Lattnerf04da132007-10-24 17:06:59 +000069 // Metadata emission.
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000070 void HandleObjcMetaDataEmission();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +000071 void RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
72 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000073
Fariborz Jahanianccd87b02007-10-25 20:55:25 +000074 void RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *CDecl,
75 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000076
77 void RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
78 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +000079 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000080 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +000081 const char *ClassName,
82 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +000083
84 void RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
85 int NumProtocols,
86 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +000087 const char *ClassName,
88 std::string &Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000089 void SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
90 std::string &Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000091 void SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
92 ObjcIvarDecl *ivar,
93 std::string &Result);
Fariborz Jahanianccd87b02007-10-25 20:55:25 +000094 void WriteObjcMetaData(std::string &Result);
Chris Lattner77cd2a02007-10-11 00:43:27 +000095 };
96}
97
Chris Lattner8a12c272007-10-11 18:38:32 +000098ASTConsumer *clang::CreateCodeRewriterTest() { return new RewriteTest(); }
Chris Lattner77cd2a02007-10-11 00:43:27 +000099
Chris Lattnerf04da132007-10-24 17:06:59 +0000100//===----------------------------------------------------------------------===//
101// Top Level Driver Code
102//===----------------------------------------------------------------------===//
103
Chris Lattner8a12c272007-10-11 18:38:32 +0000104void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000105 // Two cases: either the decl could be in the main file, or it could be in a
106 // #included file. If the former, rewrite it now. If the later, check to see
107 // if we rewrote the #include/#import.
108 SourceLocation Loc = D->getLocation();
109 Loc = SM->getLogicalLoc(Loc);
110
111 // If this is for a builtin, ignore it.
112 if (Loc.isInvalid()) return;
113
Steve Naroffebf2b562007-10-23 23:50:29 +0000114 // Look for built-in declarations that we need to refer during the rewrite.
115 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Steve Naroffc0006092007-10-24 01:09:48 +0000116 if (strcmp(FD->getName(), "objc_msgSend") == 0)
Steve Naroffebf2b562007-10-23 23:50:29 +0000117 MsgSendFunctionDecl = FD;
Steve Naroffc0006092007-10-24 01:09:48 +0000118 else if (strcmp(FD->getName(), "objc_getClass") == 0)
Steve Naroffebf2b562007-10-23 23:50:29 +0000119 GetClassFunctionDecl = FD;
Steve Naroff934f2762007-10-24 22:48:43 +0000120 else if (strcmp(FD->getName(), "sel_getUid") == 0)
121 SelGetUidFunctionDecl = FD;
Steve Naroffbef11852007-10-26 20:53:56 +0000122 } else if (ObjcInterfaceDecl *MD = dyn_cast<ObjcInterfaceDecl>(D)) {
123 RewriteInterfaceDecl(MD);
Steve Naroffebf2b562007-10-23 23:50:29 +0000124 }
Chris Lattnerf04da132007-10-24 17:06:59 +0000125 // If we have a decl in the main file, see if we should rewrite it.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000126 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
127 return HandleDeclInMainFile(D);
128
Chris Lattnerf04da132007-10-24 17:06:59 +0000129 // Otherwise, see if there is a #import in the main file that should be
130 // rewritten.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000131 RewriteInclude(Loc);
132}
133
Chris Lattnerf04da132007-10-24 17:06:59 +0000134/// HandleDeclInMainFile - This is called for each top-level decl defined in the
135/// main file of the input.
136void RewriteTest::HandleDeclInMainFile(Decl *D) {
137 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
138 if (Stmt *Body = FD->getBody())
139 FD->setBody(RewriteFunctionBody(Body));
140
141 if (ObjcImplementationDecl *CI = dyn_cast<ObjcImplementationDecl>(D))
142 ClassImplementation.push_back(CI);
143 else if (ObjcCategoryImplDecl *CI = dyn_cast<ObjcCategoryImplDecl>(D))
144 CategoryImplementation.push_back(CI);
145 else if (ObjcClassDecl *CD = dyn_cast<ObjcClassDecl>(D))
146 RewriteForwardClassDecl(CD);
147 // Nothing yet.
148}
149
150RewriteTest::~RewriteTest() {
151 // Get the top-level buffer that this corresponds to.
152 RewriteTabs();
153
154 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
155 // we are done.
156 if (const RewriteBuffer *RewriteBuf =
157 Rewrite.getRewriteBufferFor(MainFileID)) {
158 printf("Changed:\n");
159 std::string S(RewriteBuf->begin(), RewriteBuf->end());
160 printf("%s\n", S.c_str());
161 } else {
162 printf("No changes\n");
163 }
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000164
165}
166
167/// HandleObjcMetaDataEmission - main routine to generate objective-c's
168/// metadata.
169void RewriteTest::HandleObjcMetaDataEmission() {
Chris Lattnerf04da132007-10-24 17:06:59 +0000170 // Rewrite Objective-c meta data*
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000171 std::string ResultStr;
172 WriteObjcMetaData(ResultStr);
173 // For now just print the string out.
174 printf("%s", ResultStr.c_str());
Chris Lattnerf04da132007-10-24 17:06:59 +0000175}
176
177//===----------------------------------------------------------------------===//
178// Syntactic (non-AST) Rewriting Code
179//===----------------------------------------------------------------------===//
180
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000181void RewriteTest::RewriteInclude(SourceLocation Loc) {
182 // Rip up the #include stack to the main file.
183 SourceLocation IncLoc = Loc, NextLoc = Loc;
184 do {
185 IncLoc = Loc;
186 Loc = SM->getLogicalLoc(NextLoc);
187 NextLoc = SM->getIncludeLoc(Loc);
188 } while (!NextLoc.isInvalid());
189
190 // Loc is now the location of the #include filename "foo" or <foo/bar.h>.
191 // IncLoc indicates the header that was included if it is useful.
192 IncLoc = SM->getLogicalLoc(IncLoc);
193 if (SM->getDecomposedFileLoc(Loc).first != MainFileID ||
194 Loc == LastIncLoc)
195 return;
196 LastIncLoc = Loc;
197
198 unsigned IncCol = SM->getColumnNumber(Loc);
199 SourceLocation LineStartLoc = Loc.getFileLocWithOffset(-IncCol+1);
200
201 // Replace the #import with #include.
202 Rewrite.ReplaceText(LineStartLoc, IncCol-1, "#include ", strlen("#include "));
203}
204
Chris Lattnerf04da132007-10-24 17:06:59 +0000205void RewriteTest::RewriteTabs() {
206 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
207 const char *MainBufStart = MainBuf.first;
208 const char *MainBufEnd = MainBuf.second;
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +0000209
Chris Lattnerf04da132007-10-24 17:06:59 +0000210 // Loop over the whole file, looking for tabs.
211 for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
212 if (*BufPtr != '\t')
213 continue;
214
215 // Okay, we found a tab. This tab will turn into at least one character,
216 // but it depends on which 'virtual column' it is in. Compute that now.
217 unsigned VCol = 0;
218 while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
219 BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
220 ++VCol;
221
222 // Okay, now that we know the virtual column, we know how many spaces to
223 // insert. We assume 8-character tab-stops.
224 unsigned Spaces = 8-(VCol & 7);
225
226 // Get the location of the tab.
227 SourceLocation TabLoc =
228 SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
229
230 // Rewrite the single tab character into a sequence of spaces.
231 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
232 }
Chris Lattner8a12c272007-10-11 18:38:32 +0000233}
234
235
Chris Lattnerf04da132007-10-24 17:06:59 +0000236void RewriteTest::RewriteForwardClassDecl(ObjcClassDecl *ClassDecl) {
237 int numDecls = ClassDecl->getNumForwardDecls();
238 ObjcInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
239
240 // Get the start location and compute the semi location.
241 SourceLocation startLoc = ClassDecl->getLocation();
242 const char *startBuf = SM->getCharacterData(startLoc);
243 const char *semiPtr = strchr(startBuf, ';');
244
245 // Translate to typedef's that forward reference structs with the same name
246 // as the class. As a convenience, we include the original declaration
247 // as a comment.
248 std::string typedefString;
249 typedefString += "// ";
Steve Naroff934f2762007-10-24 22:48:43 +0000250 typedefString.append(startBuf, semiPtr-startBuf+1);
251 typedefString += "\n";
252 for (int i = 0; i < numDecls; i++) {
253 ObjcInterfaceDecl *ForwardDecl = ForwardDecls[i];
254 typedefString += "typedef struct ";
255 typedefString += ForwardDecl->getName();
256 typedefString += " ";
257 typedefString += ForwardDecl->getName();
258 typedefString += ";\n";
259 }
260
261 // Replace the @class with typedefs corresponding to the classes.
262 Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1,
263 typedefString.c_str(), typedefString.size());
Chris Lattnerf04da132007-10-24 17:06:59 +0000264}
265
Steve Naroffbef11852007-10-26 20:53:56 +0000266void RewriteTest::RewriteInterfaceDecl(ObjcInterfaceDecl *ClassDecl) {
267 int nInstanceMethods = ClassDecl->getNumInstanceMethods();
268 ObjcMethodDecl **instanceMethods = ClassDecl->getInstanceMethods();
269
270 for (int i = 0; i < nInstanceMethods; i++) {
271 ObjcMethodDecl *instanceMethod = instanceMethods[i];
272 SourceLocation Loc = instanceMethod->getLocStart();
273
274 Rewrite.ReplaceText(Loc, 0, "// ", 3);
275
276 // FIXME: handle methods that are declared across multiple lines.
277 }
278 int nClassMethods = ClassDecl->getNumClassMethods();
279 ObjcMethodDecl **classMethods = ClassDecl->getClassMethods();
280
281 for (int i = 0; i < nClassMethods; i++) {
282 ObjcMethodDecl *classMethod = classMethods[i];
283 SourceLocation Loc = classMethod->getLocStart();
284
285 Rewrite.ReplaceText(Loc, 0, "// ", 3);
286
287 // FIXME: handle methods that are declared across multiple lines.
288 }
289}
290
Chris Lattnerf04da132007-10-24 17:06:59 +0000291//===----------------------------------------------------------------------===//
292// Function Body / Expression rewriting
293//===----------------------------------------------------------------------===//
294
Chris Lattnere64b7772007-10-24 16:57:36 +0000295Stmt *RewriteTest::RewriteFunctionBody(Stmt *S) {
Chris Lattner311ff022007-10-16 22:36:42 +0000296 // Otherwise, just rewrite all children.
297 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
298 CI != E; ++CI)
Chris Lattner50754772007-10-17 21:28:00 +0000299 if (*CI)
Chris Lattnere64b7772007-10-24 16:57:36 +0000300 *CI = RewriteFunctionBody(*CI);
Steve Naroffebf2b562007-10-23 23:50:29 +0000301
302 // Handle specific things.
303 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
304 return RewriteAtEncode(AtEncode);
305
Steve Naroff934f2762007-10-24 22:48:43 +0000306 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
307 // Before we rewrite it, put the original message expression in a comment.
308 SourceLocation startLoc = MessExpr->getLocStart();
309 SourceLocation endLoc = MessExpr->getLocEnd();
310
311 const char *startBuf = SM->getCharacterData(startLoc);
312 const char *endBuf = SM->getCharacterData(endLoc);
313
314 std::string messString;
315 messString += "// ";
316 messString.append(startBuf, endBuf-startBuf+1);
317 messString += "\n";
Steve Naroffbef11852007-10-26 20:53:56 +0000318
Steve Naroff934f2762007-10-24 22:48:43 +0000319 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
320 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
321 // Tried this, but it didn't work either...
Steve Naroffbef11852007-10-26 20:53:56 +0000322 Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffebf2b562007-10-23 23:50:29 +0000323 return RewriteMessageExpr(MessExpr);
Steve Naroff934f2762007-10-24 22:48:43 +0000324 }
Chris Lattnere64b7772007-10-24 16:57:36 +0000325 // Return this stmt unmodified.
326 return S;
Chris Lattner311ff022007-10-16 22:36:42 +0000327}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +0000328
Chris Lattnere64b7772007-10-24 16:57:36 +0000329Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattner01c57482007-10-17 22:35:30 +0000330 // Create a new string expression.
331 QualType StrType = Context->getPointerType(Context->CharTy);
332 Expr *Replacement = new StringLiteral("foo", 3, false, StrType,
333 SourceLocation(), SourceLocation());
334 Rewrite.ReplaceStmt(Exp, Replacement);
Chris Lattnere64b7772007-10-24 16:57:36 +0000335 delete Exp;
336 return Replacement;
Chris Lattner311ff022007-10-16 22:36:42 +0000337}
338
Steve Naroff934f2762007-10-24 22:48:43 +0000339CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
340 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffebf2b562007-10-23 23:50:29 +0000341 // Get the type, we will need to reference it in a couple spots.
Steve Naroff934f2762007-10-24 22:48:43 +0000342 QualType msgSendType = FD->getType();
Steve Naroffebf2b562007-10-23 23:50:29 +0000343
344 // Create a reference to the objc_msgSend() declaration.
Steve Naroff934f2762007-10-24 22:48:43 +0000345 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffebf2b562007-10-23 23:50:29 +0000346
347 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerf04da132007-10-24 17:06:59 +0000348 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffebf2b562007-10-23 23:50:29 +0000349 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
350
351 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattnere64b7772007-10-24 16:57:36 +0000352
Steve Naroff934f2762007-10-24 22:48:43 +0000353 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
354}
355
356Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
357 assert(MsgSendFunctionDecl && "Can't find objc_msgSend() decl");
358 assert(SelGetUidFunctionDecl && "Can't find sel_getUid() decl");
359 assert(GetClassFunctionDecl && "Can't find objc_getClass() decl");
360
361 // Synthesize a call to objc_msgSend().
362 llvm::SmallVector<Expr*, 8> MsgExprs;
363 IdentifierInfo *clsName = Exp->getClassName();
364
365 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
366 if (clsName) { // class message.
367 llvm::SmallVector<Expr*, 8> ClsExprs;
368 QualType argType = Context->getPointerType(Context->CharTy);
369 ClsExprs.push_back(new StringLiteral(clsName->getName(),
370 clsName->getLength(),
371 false, argType, SourceLocation(),
372 SourceLocation()));
373 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
374 &ClsExprs[0], ClsExprs.size());
375 MsgExprs.push_back(Cls);
376 } else // instance message.
377 MsgExprs.push_back(Exp->getReceiver());
378
379 // Create a call to sel_getUid("selName"), it will be the 2nd argument.
380 llvm::SmallVector<Expr*, 8> SelExprs;
381 QualType argType = Context->getPointerType(Context->CharTy);
382 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
383 Exp->getSelector().getName().size(),
384 false, argType, SourceLocation(),
385 SourceLocation()));
386 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
387 &SelExprs[0], SelExprs.size());
388 MsgExprs.push_back(SelExp);
389
390 // Now push any user supplied arguments.
391 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
392 MsgExprs.push_back(Exp->getArg(i));
393 // We've transferred the ownership to MsgExprs. Null out the argument in
394 // the original expression, since we will delete it below.
395 Exp->setArg(i, 0);
396 }
397 CallExpr *MessExp = SynthesizeCallToFunctionDecl(MsgSendFunctionDecl,
398 &MsgExprs[0], MsgExprs.size());
399 // Now do the actual rewrite.
400 Rewrite.ReplaceStmt(Exp, MessExp);
401
Chris Lattnere64b7772007-10-24 16:57:36 +0000402 delete Exp;
Steve Naroff934f2762007-10-24 22:48:43 +0000403 return MessExp;
Steve Naroffebf2b562007-10-23 23:50:29 +0000404}
405
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000406/// SynthesizeObjcInternalStruct - Rewrite one internal struct corresponding to
407/// an objective-c class with ivars.
408void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
409 std::string &Result) {
410 assert(CDecl && "Class missing in SynthesizeObjcInternalStruct");
411 assert(CDecl->getName() && "Name missing in SynthesizeObjcInternalStruct");
412 ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass();
413 if (RCDecl && !ObjcSynthesizedStructs.count(RCDecl)) {
414 // Do it for the root
415 SynthesizeObjcInternalStruct(RCDecl, Result);
416 }
417
418 int NumIvars = CDecl->getIntfDeclNumIvars();
419 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
420 return;
421
422 Result += "\nstruct _interface_";
423 Result += CDecl->getName();
424 Result += " {\n";
425 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
426 Result += "\tstruct _interface_";
427 Result += RCDecl->getName();
428 Result += " _";
429 Result += RCDecl->getName();
430 Result += ";\n";
431 }
432
433 ObjcIvarDecl **Ivars = CDecl->getIntfDeclIvars();
434 for (int i = 0; i < NumIvars; i++) {
435 Result += "\t";
436 std::string Name = Ivars[i]->getName();
437 Ivars[i]->getType().getAsStringInternal(Name);
438 Result += Name;
439 Result += ";\n";
440 }
441 Result += "};\n";
442 // Mark this struct as having been generated.
443 if (!ObjcSynthesizedStructs.insert(CDecl))
444 assert(true && "struct already synthesize- SynthesizeObjcInternalStruct");
445}
446
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000447// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
448/// class methods.
449void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
450 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000451 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000452 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000453 const char *ClassName,
454 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000455 static bool objc_impl_method = false;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000456 if (NumMethods > 0 && !objc_impl_method) {
457 /* struct _objc_method {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000458 SEL _cmd;
459 char *method_types;
460 void *_imp;
461 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000462 */
Chris Lattner158ecb92007-10-25 17:07:24 +0000463 Result += "\nstruct _objc_method {\n";
464 Result += "\tSEL _cmd;\n";
465 Result += "\tchar *method_types;\n";
466 Result += "\tvoid *_imp;\n";
467 Result += "};\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000468
469 /* struct _objc_method_list {
470 struct _objc_method_list *next_method;
471 int method_count;
472 struct _objc_method method_list[];
473 }
474 */
475 Result += "\nstruct _objc_method_list {\n";
476 Result += "\tstruct _objc_method_list *next_method;\n";
477 Result += "\tint method_count;\n";
478 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000479 objc_impl_method = true;
Fariborz Jahanian776d6ff2007-10-19 00:36:46 +0000480 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000481 // Build _objc_method_list for class's methods if needed
482 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000483 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattner158ecb92007-10-25 17:07:24 +0000484 Result += prefix;
485 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
486 Result += "_METHODS_";
487 Result += ClassName;
488 Result += " __attribute__ ((section (\"__OBJC, __";
489 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000490 Result += "_meth\")))= ";
491 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
492
493 Result += "\t,{{(SEL)\"";
494 Result += Methods[0]->getSelector().getName().c_str();
495 Result += "\", \"\", 0}\n";
496
497 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000498 // TODO: 1) method selector name may hav to go into their own section
499 // 2) encode method types for use here (which may have to go into
500 // __meth_var_types section, 3) Need method address as 3rd initializer.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000501 Result += "\t ,{(SEL)\"";
502 Result += Methods[i]->getSelector().getName().c_str();
503 Result += "\", \"\", 0}\n";
504 }
505 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000506 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000507}
508
509/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
510void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
511 int NumProtocols,
512 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000513 const char *ClassName,
514 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000515 static bool objc_protocol_methods = false;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000516 if (NumProtocols > 0) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000517 for (int i = 0; i < NumProtocols; i++) {
518 ObjcProtocolDecl *PDecl = Protocols[i];
519 // Output struct protocol_methods holder of method selector and type.
520 if (!objc_protocol_methods &&
521 (PDecl->getNumInstanceMethods() > 0
522 || PDecl->getNumClassMethods() > 0)) {
523 /* struct protocol_methods {
524 SEL _cmd;
525 char *method_types;
526 }
527 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000528 Result += "\nstruct protocol_methods {\n";
529 Result += "\tSEL _cmd;\n";
530 Result += "\tchar *method_types;\n";
531 Result += "};\n";
532
533 /* struct _objc_protocol_method_list {
534 int protocol_method_count;
535 struct protocol_methods protocols[];
536 }
537 */
538 Result += "\nstruct _objc_protocol_method_list {\n";
539 Result += "\tint protocol_method_count;\n";
540 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000541 objc_protocol_methods = true;
542 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000543
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000544 // Output instance methods declared in this protocol.
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000545 int NumMethods = PDecl->getNumInstanceMethods();
546 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000547 Result += "\nstatic struct _objc_protocol_method_list "
548 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
549 Result += PDecl->getName();
550 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
551 "{\n\t" + utostr(NumMethods) + "\n";
552
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000553 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000554 Result += "\t,{{(SEL)\"";
555 Result += Methods[0]->getSelector().getName().c_str();
556 Result += "\", \"\"}\n";
557
558 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000559 // TODO: 1) method selector name may hav to go into their own section
560 // 2) encode method types for use here (which may have to go into
561 // __meth_var_types section.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000562 Result += "\t ,{(SEL)\"";
563 Result += Methods[i]->getSelector().getName().c_str();
564 Result += "\", \"\"}\n";
565 }
566 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000567 }
568
569 // Output class methods declared in this protocol.
570 NumMethods = PDecl->getNumClassMethods();
571 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000572 Result += "\nstatic struct _objc_protocol_method_list "
573 "_OBJC_PROTOCOL_CLASS_METHODS_";
574 Result += PDecl->getName();
575 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
576 "{\n\t";
577 Result += utostr(NumMethods);
578 Result += "\n";
579
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000580 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000581 Result += "\t,{{(SEL)\"";
582 Result += Methods[0]->getSelector().getName().c_str();
583 Result += "\", \"\"}\n";
584
585 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000586 // TODO: 1) method selector name may hav to go into their own section
587 // 2) encode method types for use here (which may have to go into
588 // __meth_var_types section.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000589 Result += "\t ,{(SEL)\"";
590 Result += Methods[i]->getSelector().getName().c_str();
591 Result += "\", \"\"}\n";
592 }
593 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000594 }
595 // Output:
596 /* struct _objc_protocol {
597 // Objective-C 1.0 extensions
598 struct _objc_protocol_extension *isa;
599 char *protocol_name;
600 struct _objc_protocol **protocol_list;
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000601 struct _objc_protocol_method_list *instance_methods;
602 struct _objc_protocol_method_list *class_methods;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000603 };
604 */
605 static bool objc_protocol = false;
606 if (!objc_protocol) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000607 Result += "\nstruct _objc_protocol {\n";
608 Result += "\tstruct _objc_protocol_extension *isa;\n";
609 Result += "\tchar *protocol_name;\n";
610 Result += "\tstruct _objc_protocol **protocol_list;\n";
611 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
612 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
613 Result += "};\n";
614
615 /* struct _objc_protocol_list {
616 struct _objc_protocol_list *next;
617 int protocol_count;
618 struct _objc_protocol *class_protocols[];
619 }
620 */
621 Result += "\nstruct _objc_protocol_list {\n";
622 Result += "\tstruct _objc_protocol_list *next;\n";
623 Result += "\tint protocol_count;\n";
624 Result += "\tstruct _objc_protocol *class_protocols[];\n";
625 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000626 objc_protocol = true;
627 }
628
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000629 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
630 Result += PDecl->getName();
631 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
632 "{\n\t0, \"";
633 Result += PDecl->getName();
634 Result += "\", 0, ";
635 if (PDecl->getInstanceMethods() > 0) {
636 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
637 Result += PDecl->getName();
638 Result += ", ";
639 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000640 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000641 Result += "0, ";
642 if (PDecl->getClassMethods() > 0) {
643 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
644 Result += PDecl->getName();
645 Result += "\n";
646 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000647 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000648 Result += "0\n";
649 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000650 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000651 // Output the top lovel protocol meta-data for the class.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000652 Result += "\nstatic struct _objc_protocol_list _OBJC_";
653 Result += prefix;
654 Result += "_PROTOCOLS_";
655 Result += ClassName;
656 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
657 "{\n\t0, ";
658 Result += utostr(NumProtocols);
659 Result += "\n";
660
661 Result += "\t,{&_OBJC_PROTOCOL_";
662 Result += Protocols[0]->getName();
663 Result += " \n";
664
665 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000666 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000667 Result += "\t ,&_OBJC_PROTOCOL_";
668 Result += PDecl->getName();
669 Result += "\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000670 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000671 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000672 }
673}
674
675/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
676/// implementation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000677void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
678 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000679 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
680 // Find category declaration for this implementation.
681 ObjcCategoryDecl *CDecl;
682 for (CDecl = ClassDecl->getCategoryList(); CDecl;
683 CDecl = CDecl->getNextClassCategory())
684 if (CDecl->getIdentifier() == IDecl->getIdentifier())
685 break;
686 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
687
688 char *FullCategoryName = (char*)alloca(
689 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
690 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
691
692 // Build _objc_method_list for class's instance methods if needed
693 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
694 IDecl->getNumInstanceMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000695 true,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000696 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000697
698 // Build _objc_method_list for class's class methods if needed
699 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
700 IDecl->getNumClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000701 false,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000702 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000703
704 // Protocols referenced in class declaration?
705 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
706 CDecl->getNumReferencedProtocols(),
707 "CATEGORY",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000708 FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000709
710 /* struct _objc_category {
711 char *category_name;
712 char *class_name;
713 struct _objc_method_list *instance_methods;
714 struct _objc_method_list *class_methods;
715 struct _objc_protocol_list *protocols;
716 // Objective-C 1.0 extensions
717 uint32_t size; // sizeof (struct _objc_category)
718 struct _objc_property_list *instance_properties; // category's own
719 // @property decl.
720 };
721 */
722
723 static bool objc_category = false;
724 if (!objc_category) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000725 Result += "\nstruct _objc_category {\n";
726 Result += "\tchar *category_name;\n";
727 Result += "\tchar *class_name;\n";
728 Result += "\tstruct _objc_method_list *instance_methods;\n";
729 Result += "\tstruct _objc_method_list *class_methods;\n";
730 Result += "\tstruct _objc_protocol_list *protocols;\n";
731 Result += "\tunsigned int size;\n";
732 Result += "\tstruct _objc_property_list *instance_properties;\n";
733 Result += "};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000734 objc_category = true;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000735 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000736 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
737 Result += FullCategoryName;
738 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
739 Result += IDecl->getName();
740 Result += "\"\n\t, \"";
741 Result += ClassDecl->getName();
742 Result += "\"\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000743
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000744 if (IDecl->getNumInstanceMethods() > 0) {
745 Result += "\t, (struct _objc_method_list *)"
746 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
747 Result += FullCategoryName;
748 Result += "\n";
749 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000750 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000751 Result += "\t, 0\n";
752 if (IDecl->getNumClassMethods() > 0) {
753 Result += "\t, (struct _objc_method_list *)"
754 "&_OBJC_CATEGORY_CLASS_METHODS_";
755 Result += FullCategoryName;
756 Result += "\n";
757 }
758 else
759 Result += "\t, 0\n";
760
761 if (CDecl->getNumReferencedProtocols() > 0) {
762 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
763 Result += FullCategoryName;
764 Result += "\n";
765 }
766 else
767 Result += "\t, 0\n";
768 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000769}
770
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000771/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
772/// ivar offset.
773void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
774 ObjcIvarDecl *ivar,
775 std::string &Result) {
776 Result += "offsetof(struct _interface_";
777 Result += IDecl->getName();
778 Result += ", ";
779 Result += ivar->getName();
780 Result += ")";
781}
782
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000783//===----------------------------------------------------------------------===//
784// Meta Data Emission
785//===----------------------------------------------------------------------===//
786
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000787void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
788 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000789 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
790
791 // Build _objc_ivar_list metadata for classes ivars if needed
792 int NumIvars = IDecl->getImplDeclNumIvars() > 0
793 ? IDecl->getImplDeclNumIvars()
794 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
795
Fariborz Jahanian4d733d32007-10-26 23:09:28 +0000796 SynthesizeObjcInternalStruct(CDecl, Result);
797
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000798 if (NumIvars > 0) {
799 static bool objc_ivar = false;
800 if (!objc_ivar) {
801 /* struct _objc_ivar {
802 char *ivar_name;
803 char *ivar_type;
804 int ivar_offset;
805 };
806 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000807 Result += "\nstruct _objc_ivar {\n";
808 Result += "\tchar *ivar_name;\n";
809 Result += "\tchar *ivar_type;\n";
810 Result += "\tint ivar_offset;\n";
811 Result += "};\n";
812
813 /* struct _objc_ivar_list {
814 int ivar_count;
815 struct _objc_ivar ivar_list[];
816 };
817 */
818 Result += "\nstruct _objc_ivar_list {\n";
819 Result += "\tint ivar_count;\n";
820 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000821 objc_ivar = true;
822 }
823
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000824 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
825 Result += IDecl->getName();
826 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
827 "{\n\t";
828 Result += utostr(NumIvars);
829 Result += "\n";
830
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000831 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
832 ? IDecl->getImplDeclIVars()
833 : CDecl->getIntfDeclIvars();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000834 Result += "\t,{{\"";
835 Result += Ivars[0]->getName();
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000836 Result += "\", \"\", ";
837 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
838 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000839 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000840 // TODO: 1) ivar names may have to go to another section. 2) encode
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000841 // ivar_type type of each ivar .
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000842 Result += "\t ,{\"";
843 Result += Ivars[i]->getName();
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000844 Result += "\", \"\", ";
845 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
846 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000847 }
848
849 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000850 }
851
852 // Build _objc_method_list for class's instance methods if needed
853 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
854 IDecl->getNumInstanceMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000855 true,
856 "", IDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000857
858 // Build _objc_method_list for class's class methods if needed
859 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000860 IDecl->getNumClassMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000861 false,
862 "", IDecl->getName(), Result);
863
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000864 // Protocols referenced in class declaration?
865 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
866 CDecl->getNumIntfRefProtocols(),
867 "CLASS",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000868 CDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000869
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000870
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000871 // Declaration of class/meta-class metadata
872 /* struct _objc_class {
873 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000874 const char *super_class_name;
875 char *name;
876 long version;
877 long info;
878 long instance_size;
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000879 struct _objc_ivar_list *ivars;
880 struct _objc_method_list *methods;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000881 struct objc_cache *cache;
882 struct objc_protocol_list *protocols;
883 const char *ivar_layout;
884 struct _objc_class_ext *ext;
885 };
886 */
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000887 static bool objc_class = false;
888 if (!objc_class) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000889 Result += "\nstruct _objc_class {\n";
890 Result += "\tstruct _objc_class *isa;\n";
891 Result += "\tconst char *super_class_name;\n";
892 Result += "\tchar *name;\n";
893 Result += "\tlong version;\n";
894 Result += "\tlong info;\n";
895 Result += "\tlong instance_size;\n";
896 Result += "\tstruct _objc_ivar_list *ivars;\n";
897 Result += "\tstruct _objc_method_list *methods;\n";
898 Result += "\tstruct objc_cache *cache;\n";
899 Result += "\tstruct _objc_protocol_list *protocols;\n";
900 Result += "\tconst char *ivar_layout;\n";
901 Result += "\tstruct _objc_class_ext *ext;\n";
902 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000903 objc_class = true;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000904 }
905
906 // Meta-class metadata generation.
907 ObjcInterfaceDecl *RootClass = 0;
908 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
909 while (SuperClass) {
910 RootClass = SuperClass;
911 SuperClass = SuperClass->getSuperClass();
912 }
913 SuperClass = CDecl->getSuperClass();
914
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000915 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
916 Result += CDecl->getName();
917 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
918 "{\n\t(struct _objc_class *)\"";
919 Result += (RootClass ? RootClass->getName() : CDecl->getName());
920 Result += "\"";
921
922 if (SuperClass) {
923 Result += ", \"";
924 Result += SuperClass->getName();
925 Result += "\", \"";
926 Result += CDecl->getName();
927 Result += "\"";
928 }
929 else {
930 Result += ", 0, \"";
931 Result += CDecl->getName();
932 Result += "\"";
933 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000934 // TODO: 'ivars' field for root class is currently set to 0.
935 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000936 Result += ", 0,2, sizeof(struct _objc_class), 0";
937 if (CDecl->getNumClassMethods() > 0) {
938 Result += "\n\t, &_OBJC_CLASS_METHODS_";
939 Result += CDecl->getName();
940 Result += "\n";
941 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000942 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000943 Result += ", 0\n";
944 if (CDecl->getNumIntfRefProtocols() > 0) {
945 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
946 Result += CDecl->getName();
947 Result += ",0,0\n";
948 }
Fariborz Jahanian454cb012007-10-24 20:54:23 +0000949 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000950 Result += "\t,0,0,0,0\n";
951 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000952
953 // class metadata generation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000954 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
955 Result += CDecl->getName();
956 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
957 "{\n\t&_OBJC_METACLASS_";
958 Result += CDecl->getName();
959 if (SuperClass) {
960 Result += ", \"";
961 Result += SuperClass->getName();
962 Result += "\", \"";
963 Result += CDecl->getName();
964 Result += "\"";
965 }
966 else {
967 Result += ", 0, \"";
968 Result += CDecl->getName();
969 Result += "\"";
970 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000971 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanian4d733d32007-10-26 23:09:28 +0000972 Result += ", 0,1";
973 if (!ObjcSynthesizedStructs.count(CDecl))
974 Result += ",0";
975 else {
976 // class has size. Must synthesize its size.
977 Result += ",sizeof(struct _interface_";
978 Result += CDecl->getName();
979 Result += ")";
980 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000981 if (NumIvars > 0) {
982 Result += ", &_OBJC_INSTANCE_VARIABLES_";
983 Result += CDecl->getName();
984 Result += "\n\t";
985 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000986 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000987 Result += ",0";
988 if (IDecl->getNumInstanceMethods() > 0) {
989 Result += ", &_OBJC_INSTANCE_METHODS_";
990 Result += CDecl->getName();
991 Result += ", 0\n\t";
992 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000993 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000994 Result += ",0,0";
995 if (CDecl->getNumIntfRefProtocols() > 0) {
996 Result += ", &_OBJC_CLASS_PROTOCOLS_";
997 Result += CDecl->getName();
998 Result += ", 0,0\n";
999 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001000 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001001 Result += ",0,0,0\n";
1002 Result += "};\n";
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001003}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001004
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001005void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001006 int ClsDefCount = ClassImplementation.size();
1007 int CatDefCount = CategoryImplementation.size();
1008 if (ClsDefCount == 0 && CatDefCount == 0)
1009 return;
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001010
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001011 // TODO: This is temporary until we decide how to access objc types in a
1012 // c program
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001013 Result += "#include <Objc/objc.h>\n";
1014 // This is needed for use of offsetof
1015 Result += "#include <stddef.h>\n";
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001016
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001017 // For each implemented class, write out all its meta data.
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001018 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001019 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001020
1021 // For each implemented category, write out all its meta data.
1022 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001023 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001024
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001025 // Write objc_symtab metadata
1026 /*
1027 struct _objc_symtab
1028 {
1029 long sel_ref_cnt;
1030 SEL *refs;
1031 short cls_def_cnt;
1032 short cat_def_cnt;
1033 void *defs[cls_def_cnt + cat_def_cnt];
1034 };
1035 */
1036
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001037 Result += "\nstruct _objc_symtab {\n";
1038 Result += "\tlong sel_ref_cnt;\n";
1039 Result += "\tSEL *refs;\n";
1040 Result += "\tshort cls_def_cnt;\n";
1041 Result += "\tshort cat_def_cnt;\n";
1042 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1043 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001044
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001045 Result += "static struct _objc_symtab "
1046 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1047 Result += "\t0, 0, " + utostr(ClsDefCount)
1048 + ", " + utostr(CatDefCount) + "\n";
1049 for (int i = 0; i < ClsDefCount; i++) {
1050 Result += "\t,&_OBJC_CLASS_";
1051 Result += ClassImplementation[i]->getName();
1052 Result += "\n";
1053 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001054
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001055 for (int i = 0; i < CatDefCount; i++) {
1056 Result += "\t,&_OBJC_CATEGORY_";
1057 Result += CategoryImplementation[i]->getClassInterface()->getName();
1058 Result += "_";
1059 Result += CategoryImplementation[i]->getName();
1060 Result += "\n";
1061 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001062
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001063 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001064
1065 // Write objc_module metadata
1066
1067 /*
1068 struct _objc_module {
1069 long version;
1070 long size;
1071 const char *name;
1072 struct _objc_symtab *symtab;
1073 }
1074 */
1075
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001076 Result += "\nstruct _objc_module {\n";
1077 Result += "\tlong version;\n";
1078 Result += "\tlong size;\n";
1079 Result += "\tconst char *name;\n";
1080 Result += "\tstruct _objc_symtab *symtab;\n";
1081 Result += "};\n\n";
1082 Result += "static struct _objc_module "
1083 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001084 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1085 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001086 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001087}
Chris Lattner311ff022007-10-16 22:36:42 +00001088