blob: 0c303daddf2f96b94d4722d8b08ba88f6d23369f [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);
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000332 std::string StrEncoding;
333 Context->getObjcEncodingForType(Exp->getEncodedType(), StrEncoding);
334 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
335 StrEncoding.length(), false, StrType,
Chris Lattner01c57482007-10-17 22:35:30 +0000336 SourceLocation(), SourceLocation());
337 Rewrite.ReplaceStmt(Exp, Replacement);
Chris Lattnere64b7772007-10-24 16:57:36 +0000338 delete Exp;
339 return Replacement;
Chris Lattner311ff022007-10-16 22:36:42 +0000340}
341
Steve Naroff934f2762007-10-24 22:48:43 +0000342CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
343 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffebf2b562007-10-23 23:50:29 +0000344 // Get the type, we will need to reference it in a couple spots.
Steve Naroff934f2762007-10-24 22:48:43 +0000345 QualType msgSendType = FD->getType();
Steve Naroffebf2b562007-10-23 23:50:29 +0000346
347 // Create a reference to the objc_msgSend() declaration.
Steve Naroff934f2762007-10-24 22:48:43 +0000348 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffebf2b562007-10-23 23:50:29 +0000349
350 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerf04da132007-10-24 17:06:59 +0000351 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffebf2b562007-10-23 23:50:29 +0000352 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
353
354 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattnere64b7772007-10-24 16:57:36 +0000355
Steve Naroff934f2762007-10-24 22:48:43 +0000356 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
357}
358
359Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
360 assert(MsgSendFunctionDecl && "Can't find objc_msgSend() decl");
361 assert(SelGetUidFunctionDecl && "Can't find sel_getUid() decl");
362 assert(GetClassFunctionDecl && "Can't find objc_getClass() decl");
363
364 // Synthesize a call to objc_msgSend().
365 llvm::SmallVector<Expr*, 8> MsgExprs;
366 IdentifierInfo *clsName = Exp->getClassName();
367
368 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
369 if (clsName) { // class message.
370 llvm::SmallVector<Expr*, 8> ClsExprs;
371 QualType argType = Context->getPointerType(Context->CharTy);
372 ClsExprs.push_back(new StringLiteral(clsName->getName(),
373 clsName->getLength(),
374 false, argType, SourceLocation(),
375 SourceLocation()));
376 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
377 &ClsExprs[0], ClsExprs.size());
378 MsgExprs.push_back(Cls);
379 } else // instance message.
380 MsgExprs.push_back(Exp->getReceiver());
381
382 // Create a call to sel_getUid("selName"), it will be the 2nd argument.
383 llvm::SmallVector<Expr*, 8> SelExprs;
384 QualType argType = Context->getPointerType(Context->CharTy);
385 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
386 Exp->getSelector().getName().size(),
387 false, argType, SourceLocation(),
388 SourceLocation()));
389 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
390 &SelExprs[0], SelExprs.size());
391 MsgExprs.push_back(SelExp);
392
393 // Now push any user supplied arguments.
394 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
395 MsgExprs.push_back(Exp->getArg(i));
396 // We've transferred the ownership to MsgExprs. Null out the argument in
397 // the original expression, since we will delete it below.
398 Exp->setArg(i, 0);
399 }
400 CallExpr *MessExp = SynthesizeCallToFunctionDecl(MsgSendFunctionDecl,
401 &MsgExprs[0], MsgExprs.size());
402 // Now do the actual rewrite.
403 Rewrite.ReplaceStmt(Exp, MessExp);
404
Chris Lattnere64b7772007-10-24 16:57:36 +0000405 delete Exp;
Steve Naroff934f2762007-10-24 22:48:43 +0000406 return MessExp;
Steve Naroffebf2b562007-10-23 23:50:29 +0000407}
408
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000409/// SynthesizeObjcInternalStruct - Rewrite one internal struct corresponding to
410/// an objective-c class with ivars.
411void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
412 std::string &Result) {
413 assert(CDecl && "Class missing in SynthesizeObjcInternalStruct");
414 assert(CDecl->getName() && "Name missing in SynthesizeObjcInternalStruct");
415 ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass();
416 if (RCDecl && !ObjcSynthesizedStructs.count(RCDecl)) {
417 // Do it for the root
418 SynthesizeObjcInternalStruct(RCDecl, Result);
419 }
420
421 int NumIvars = CDecl->getIntfDeclNumIvars();
422 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
423 return;
424
425 Result += "\nstruct _interface_";
426 Result += CDecl->getName();
427 Result += " {\n";
428 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
429 Result += "\tstruct _interface_";
430 Result += RCDecl->getName();
431 Result += " _";
432 Result += RCDecl->getName();
433 Result += ";\n";
434 }
435
436 ObjcIvarDecl **Ivars = CDecl->getIntfDeclIvars();
437 for (int i = 0; i < NumIvars; i++) {
438 Result += "\t";
439 std::string Name = Ivars[i]->getName();
440 Ivars[i]->getType().getAsStringInternal(Name);
441 Result += Name;
442 Result += ";\n";
443 }
444 Result += "};\n";
445 // Mark this struct as having been generated.
446 if (!ObjcSynthesizedStructs.insert(CDecl))
447 assert(true && "struct already synthesize- SynthesizeObjcInternalStruct");
448}
449
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000450// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
451/// class methods.
452void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
453 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000454 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000455 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000456 const char *ClassName,
457 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000458 static bool objc_impl_method = false;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000459 if (NumMethods > 0 && !objc_impl_method) {
460 /* struct _objc_method {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000461 SEL _cmd;
462 char *method_types;
463 void *_imp;
464 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000465 */
Chris Lattner158ecb92007-10-25 17:07:24 +0000466 Result += "\nstruct _objc_method {\n";
467 Result += "\tSEL _cmd;\n";
468 Result += "\tchar *method_types;\n";
469 Result += "\tvoid *_imp;\n";
470 Result += "};\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000471
472 /* struct _objc_method_list {
473 struct _objc_method_list *next_method;
474 int method_count;
475 struct _objc_method method_list[];
476 }
477 */
478 Result += "\nstruct _objc_method_list {\n";
479 Result += "\tstruct _objc_method_list *next_method;\n";
480 Result += "\tint method_count;\n";
481 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000482 objc_impl_method = true;
Fariborz Jahanian776d6ff2007-10-19 00:36:46 +0000483 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000484 // Build _objc_method_list for class's methods if needed
485 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000486 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattner158ecb92007-10-25 17:07:24 +0000487 Result += prefix;
488 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
489 Result += "_METHODS_";
490 Result += ClassName;
491 Result += " __attribute__ ((section (\"__OBJC, __";
492 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000493 Result += "_meth\")))= ";
494 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
495
496 Result += "\t,{{(SEL)\"";
497 Result += Methods[0]->getSelector().getName().c_str();
498 Result += "\", \"\", 0}\n";
499
500 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000501 // TODO: 1) method selector name may hav to go into their own section
502 // 2) encode method types for use here (which may have to go into
503 // __meth_var_types section, 3) Need method address as 3rd initializer.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000504 Result += "\t ,{(SEL)\"";
505 Result += Methods[i]->getSelector().getName().c_str();
506 Result += "\", \"\", 0}\n";
507 }
508 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000509 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000510}
511
512/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
513void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
514 int NumProtocols,
515 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000516 const char *ClassName,
517 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000518 static bool objc_protocol_methods = false;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000519 if (NumProtocols > 0) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000520 for (int i = 0; i < NumProtocols; i++) {
521 ObjcProtocolDecl *PDecl = Protocols[i];
522 // Output struct protocol_methods holder of method selector and type.
523 if (!objc_protocol_methods &&
524 (PDecl->getNumInstanceMethods() > 0
525 || PDecl->getNumClassMethods() > 0)) {
526 /* struct protocol_methods {
527 SEL _cmd;
528 char *method_types;
529 }
530 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000531 Result += "\nstruct protocol_methods {\n";
532 Result += "\tSEL _cmd;\n";
533 Result += "\tchar *method_types;\n";
534 Result += "};\n";
535
536 /* struct _objc_protocol_method_list {
537 int protocol_method_count;
538 struct protocol_methods protocols[];
539 }
540 */
541 Result += "\nstruct _objc_protocol_method_list {\n";
542 Result += "\tint protocol_method_count;\n";
543 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000544 objc_protocol_methods = true;
545 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000546
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000547 // Output instance methods declared in this protocol.
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000548 int NumMethods = PDecl->getNumInstanceMethods();
549 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000550 Result += "\nstatic struct _objc_protocol_method_list "
551 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
552 Result += PDecl->getName();
553 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
554 "{\n\t" + utostr(NumMethods) + "\n";
555
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000556 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000557 Result += "\t,{{(SEL)\"";
558 Result += Methods[0]->getSelector().getName().c_str();
559 Result += "\", \"\"}\n";
560
561 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000562 // TODO: 1) method selector name may hav to go into their own section
563 // 2) encode method types for use here (which may have to go into
564 // __meth_var_types section.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000565 Result += "\t ,{(SEL)\"";
566 Result += Methods[i]->getSelector().getName().c_str();
567 Result += "\", \"\"}\n";
568 }
569 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000570 }
571
572 // Output class methods declared in this protocol.
573 NumMethods = PDecl->getNumClassMethods();
574 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000575 Result += "\nstatic struct _objc_protocol_method_list "
576 "_OBJC_PROTOCOL_CLASS_METHODS_";
577 Result += PDecl->getName();
578 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
579 "{\n\t";
580 Result += utostr(NumMethods);
581 Result += "\n";
582
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000583 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000584 Result += "\t,{{(SEL)\"";
585 Result += Methods[0]->getSelector().getName().c_str();
586 Result += "\", \"\"}\n";
587
588 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000589 // TODO: 1) method selector name may hav to go into their own section
590 // 2) encode method types for use here (which may have to go into
591 // __meth_var_types section.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000592 Result += "\t ,{(SEL)\"";
593 Result += Methods[i]->getSelector().getName().c_str();
594 Result += "\", \"\"}\n";
595 }
596 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000597 }
598 // Output:
599 /* struct _objc_protocol {
600 // Objective-C 1.0 extensions
601 struct _objc_protocol_extension *isa;
602 char *protocol_name;
603 struct _objc_protocol **protocol_list;
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000604 struct _objc_protocol_method_list *instance_methods;
605 struct _objc_protocol_method_list *class_methods;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000606 };
607 */
608 static bool objc_protocol = false;
609 if (!objc_protocol) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000610 Result += "\nstruct _objc_protocol {\n";
611 Result += "\tstruct _objc_protocol_extension *isa;\n";
612 Result += "\tchar *protocol_name;\n";
613 Result += "\tstruct _objc_protocol **protocol_list;\n";
614 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
615 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
616 Result += "};\n";
617
618 /* struct _objc_protocol_list {
619 struct _objc_protocol_list *next;
620 int protocol_count;
621 struct _objc_protocol *class_protocols[];
622 }
623 */
624 Result += "\nstruct _objc_protocol_list {\n";
625 Result += "\tstruct _objc_protocol_list *next;\n";
626 Result += "\tint protocol_count;\n";
627 Result += "\tstruct _objc_protocol *class_protocols[];\n";
628 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000629 objc_protocol = true;
630 }
631
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000632 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
633 Result += PDecl->getName();
634 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
635 "{\n\t0, \"";
636 Result += PDecl->getName();
637 Result += "\", 0, ";
638 if (PDecl->getInstanceMethods() > 0) {
639 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
640 Result += PDecl->getName();
641 Result += ", ";
642 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000643 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000644 Result += "0, ";
645 if (PDecl->getClassMethods() > 0) {
646 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
647 Result += PDecl->getName();
648 Result += "\n";
649 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000650 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000651 Result += "0\n";
652 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000653 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000654 // Output the top lovel protocol meta-data for the class.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000655 Result += "\nstatic struct _objc_protocol_list _OBJC_";
656 Result += prefix;
657 Result += "_PROTOCOLS_";
658 Result += ClassName;
659 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
660 "{\n\t0, ";
661 Result += utostr(NumProtocols);
662 Result += "\n";
663
664 Result += "\t,{&_OBJC_PROTOCOL_";
665 Result += Protocols[0]->getName();
666 Result += " \n";
667
668 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000669 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000670 Result += "\t ,&_OBJC_PROTOCOL_";
671 Result += PDecl->getName();
672 Result += "\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000673 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000674 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000675 }
676}
677
678/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
679/// implementation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000680void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
681 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000682 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
683 // Find category declaration for this implementation.
684 ObjcCategoryDecl *CDecl;
685 for (CDecl = ClassDecl->getCategoryList(); CDecl;
686 CDecl = CDecl->getNextClassCategory())
687 if (CDecl->getIdentifier() == IDecl->getIdentifier())
688 break;
689 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
690
691 char *FullCategoryName = (char*)alloca(
692 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
693 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
694
695 // Build _objc_method_list for class's instance methods if needed
696 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
697 IDecl->getNumInstanceMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000698 true,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000699 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000700
701 // Build _objc_method_list for class's class methods if needed
702 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
703 IDecl->getNumClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000704 false,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000705 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000706
707 // Protocols referenced in class declaration?
708 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
709 CDecl->getNumReferencedProtocols(),
710 "CATEGORY",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000711 FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000712
713 /* struct _objc_category {
714 char *category_name;
715 char *class_name;
716 struct _objc_method_list *instance_methods;
717 struct _objc_method_list *class_methods;
718 struct _objc_protocol_list *protocols;
719 // Objective-C 1.0 extensions
720 uint32_t size; // sizeof (struct _objc_category)
721 struct _objc_property_list *instance_properties; // category's own
722 // @property decl.
723 };
724 */
725
726 static bool objc_category = false;
727 if (!objc_category) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000728 Result += "\nstruct _objc_category {\n";
729 Result += "\tchar *category_name;\n";
730 Result += "\tchar *class_name;\n";
731 Result += "\tstruct _objc_method_list *instance_methods;\n";
732 Result += "\tstruct _objc_method_list *class_methods;\n";
733 Result += "\tstruct _objc_protocol_list *protocols;\n";
734 Result += "\tunsigned int size;\n";
735 Result += "\tstruct _objc_property_list *instance_properties;\n";
736 Result += "};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000737 objc_category = true;
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000738 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000739 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
740 Result += FullCategoryName;
741 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
742 Result += IDecl->getName();
743 Result += "\"\n\t, \"";
744 Result += ClassDecl->getName();
745 Result += "\"\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000746
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000747 if (IDecl->getNumInstanceMethods() > 0) {
748 Result += "\t, (struct _objc_method_list *)"
749 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
750 Result += FullCategoryName;
751 Result += "\n";
752 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000753 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000754 Result += "\t, 0\n";
755 if (IDecl->getNumClassMethods() > 0) {
756 Result += "\t, (struct _objc_method_list *)"
757 "&_OBJC_CATEGORY_CLASS_METHODS_";
758 Result += FullCategoryName;
759 Result += "\n";
760 }
761 else
762 Result += "\t, 0\n";
763
764 if (CDecl->getNumReferencedProtocols() > 0) {
765 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
766 Result += FullCategoryName;
767 Result += "\n";
768 }
769 else
770 Result += "\t, 0\n";
771 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000772}
773
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000774/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
775/// ivar offset.
776void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
777 ObjcIvarDecl *ivar,
778 std::string &Result) {
779 Result += "offsetof(struct _interface_";
780 Result += IDecl->getName();
781 Result += ", ";
782 Result += ivar->getName();
783 Result += ")";
784}
785
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000786//===----------------------------------------------------------------------===//
787// Meta Data Emission
788//===----------------------------------------------------------------------===//
789
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000790void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
791 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000792 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
793
794 // Build _objc_ivar_list metadata for classes ivars if needed
795 int NumIvars = IDecl->getImplDeclNumIvars() > 0
796 ? IDecl->getImplDeclNumIvars()
797 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
798
Fariborz Jahanian4d733d32007-10-26 23:09:28 +0000799 SynthesizeObjcInternalStruct(CDecl, Result);
800
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000801 if (NumIvars > 0) {
802 static bool objc_ivar = false;
803 if (!objc_ivar) {
804 /* struct _objc_ivar {
805 char *ivar_name;
806 char *ivar_type;
807 int ivar_offset;
808 };
809 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000810 Result += "\nstruct _objc_ivar {\n";
811 Result += "\tchar *ivar_name;\n";
812 Result += "\tchar *ivar_type;\n";
813 Result += "\tint ivar_offset;\n";
814 Result += "};\n";
815
816 /* struct _objc_ivar_list {
817 int ivar_count;
818 struct _objc_ivar ivar_list[];
819 };
820 */
821 Result += "\nstruct _objc_ivar_list {\n";
822 Result += "\tint ivar_count;\n";
823 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000824 objc_ivar = true;
825 }
826
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000827 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
828 Result += IDecl->getName();
829 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
830 "{\n\t";
831 Result += utostr(NumIvars);
832 Result += "\n";
833
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000834 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
835 ? IDecl->getImplDeclIVars()
836 : CDecl->getIntfDeclIvars();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000837 Result += "\t,{{\"";
838 Result += Ivars[0]->getName();
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000839 Result += "\", \"\", ";
840 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
841 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000842 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000843 // TODO: 1) ivar names may have to go to another section. 2) encode
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000844 // ivar_type type of each ivar .
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000845 Result += "\t ,{\"";
846 Result += Ivars[i]->getName();
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000847 Result += "\", \"\", ";
848 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
849 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000850 }
851
852 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000853 }
854
855 // Build _objc_method_list for class's instance methods if needed
856 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
857 IDecl->getNumInstanceMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000858 true,
859 "", IDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000860
861 // Build _objc_method_list for class's class methods if needed
862 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000863 IDecl->getNumClassMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000864 false,
865 "", IDecl->getName(), Result);
866
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000867 // Protocols referenced in class declaration?
868 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
869 CDecl->getNumIntfRefProtocols(),
870 "CLASS",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000871 CDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000872
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000873
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000874 // Declaration of class/meta-class metadata
875 /* struct _objc_class {
876 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000877 const char *super_class_name;
878 char *name;
879 long version;
880 long info;
881 long instance_size;
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000882 struct _objc_ivar_list *ivars;
883 struct _objc_method_list *methods;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000884 struct objc_cache *cache;
885 struct objc_protocol_list *protocols;
886 const char *ivar_layout;
887 struct _objc_class_ext *ext;
888 };
889 */
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000890 static bool objc_class = false;
891 if (!objc_class) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000892 Result += "\nstruct _objc_class {\n";
893 Result += "\tstruct _objc_class *isa;\n";
894 Result += "\tconst char *super_class_name;\n";
895 Result += "\tchar *name;\n";
896 Result += "\tlong version;\n";
897 Result += "\tlong info;\n";
898 Result += "\tlong instance_size;\n";
899 Result += "\tstruct _objc_ivar_list *ivars;\n";
900 Result += "\tstruct _objc_method_list *methods;\n";
901 Result += "\tstruct objc_cache *cache;\n";
902 Result += "\tstruct _objc_protocol_list *protocols;\n";
903 Result += "\tconst char *ivar_layout;\n";
904 Result += "\tstruct _objc_class_ext *ext;\n";
905 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000906 objc_class = true;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000907 }
908
909 // Meta-class metadata generation.
910 ObjcInterfaceDecl *RootClass = 0;
911 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
912 while (SuperClass) {
913 RootClass = SuperClass;
914 SuperClass = SuperClass->getSuperClass();
915 }
916 SuperClass = CDecl->getSuperClass();
917
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000918 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
919 Result += CDecl->getName();
920 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
921 "{\n\t(struct _objc_class *)\"";
922 Result += (RootClass ? RootClass->getName() : CDecl->getName());
923 Result += "\"";
924
925 if (SuperClass) {
926 Result += ", \"";
927 Result += SuperClass->getName();
928 Result += "\", \"";
929 Result += CDecl->getName();
930 Result += "\"";
931 }
932 else {
933 Result += ", 0, \"";
934 Result += CDecl->getName();
935 Result += "\"";
936 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000937 // TODO: 'ivars' field for root class is currently set to 0.
938 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000939 Result += ", 0,2, sizeof(struct _objc_class), 0";
940 if (CDecl->getNumClassMethods() > 0) {
941 Result += "\n\t, &_OBJC_CLASS_METHODS_";
942 Result += CDecl->getName();
943 Result += "\n";
944 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +0000945 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000946 Result += ", 0\n";
947 if (CDecl->getNumIntfRefProtocols() > 0) {
948 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
949 Result += CDecl->getName();
950 Result += ",0,0\n";
951 }
Fariborz Jahanian454cb012007-10-24 20:54:23 +0000952 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000953 Result += "\t,0,0,0,0\n";
954 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000955
956 // class metadata generation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000957 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
958 Result += CDecl->getName();
959 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
960 "{\n\t&_OBJC_METACLASS_";
961 Result += CDecl->getName();
962 if (SuperClass) {
963 Result += ", \"";
964 Result += SuperClass->getName();
965 Result += "\", \"";
966 Result += CDecl->getName();
967 Result += "\"";
968 }
969 else {
970 Result += ", 0, \"";
971 Result += CDecl->getName();
972 Result += "\"";
973 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000974 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanian4d733d32007-10-26 23:09:28 +0000975 Result += ", 0,1";
976 if (!ObjcSynthesizedStructs.count(CDecl))
977 Result += ",0";
978 else {
979 // class has size. Must synthesize its size.
980 Result += ",sizeof(struct _interface_";
981 Result += CDecl->getName();
982 Result += ")";
983 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000984 if (NumIvars > 0) {
985 Result += ", &_OBJC_INSTANCE_VARIABLES_";
986 Result += CDecl->getName();
987 Result += "\n\t";
988 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000989 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000990 Result += ",0";
991 if (IDecl->getNumInstanceMethods() > 0) {
992 Result += ", &_OBJC_INSTANCE_METHODS_";
993 Result += CDecl->getName();
994 Result += ", 0\n\t";
995 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +0000996 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000997 Result += ",0,0";
998 if (CDecl->getNumIntfRefProtocols() > 0) {
999 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1000 Result += CDecl->getName();
1001 Result += ", 0,0\n";
1002 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001003 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001004 Result += ",0,0,0\n";
1005 Result += "};\n";
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001006}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001007
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001008void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001009 int ClsDefCount = ClassImplementation.size();
1010 int CatDefCount = CategoryImplementation.size();
1011 if (ClsDefCount == 0 && CatDefCount == 0)
1012 return;
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001013
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001014 // TODO: This is temporary until we decide how to access objc types in a
1015 // c program
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001016 Result += "#include <Objc/objc.h>\n";
1017 // This is needed for use of offsetof
1018 Result += "#include <stddef.h>\n";
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001019
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001020 // For each implemented class, write out all its meta data.
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001021 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001022 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001023
1024 // For each implemented category, write out all its meta data.
1025 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001026 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001027
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001028 // Write objc_symtab metadata
1029 /*
1030 struct _objc_symtab
1031 {
1032 long sel_ref_cnt;
1033 SEL *refs;
1034 short cls_def_cnt;
1035 short cat_def_cnt;
1036 void *defs[cls_def_cnt + cat_def_cnt];
1037 };
1038 */
1039
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001040 Result += "\nstruct _objc_symtab {\n";
1041 Result += "\tlong sel_ref_cnt;\n";
1042 Result += "\tSEL *refs;\n";
1043 Result += "\tshort cls_def_cnt;\n";
1044 Result += "\tshort cat_def_cnt;\n";
1045 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1046 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001047
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001048 Result += "static struct _objc_symtab "
1049 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1050 Result += "\t0, 0, " + utostr(ClsDefCount)
1051 + ", " + utostr(CatDefCount) + "\n";
1052 for (int i = 0; i < ClsDefCount; i++) {
1053 Result += "\t,&_OBJC_CLASS_";
1054 Result += ClassImplementation[i]->getName();
1055 Result += "\n";
1056 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001057
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001058 for (int i = 0; i < CatDefCount; i++) {
1059 Result += "\t,&_OBJC_CATEGORY_";
1060 Result += CategoryImplementation[i]->getClassInterface()->getName();
1061 Result += "_";
1062 Result += CategoryImplementation[i]->getName();
1063 Result += "\n";
1064 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001065
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001066 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001067
1068 // Write objc_module metadata
1069
1070 /*
1071 struct _objc_module {
1072 long version;
1073 long size;
1074 const char *name;
1075 struct _objc_symtab *symtab;
1076 }
1077 */
1078
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001079 Result += "\nstruct _objc_module {\n";
1080 Result += "\tlong version;\n";
1081 Result += "\tlong size;\n";
1082 Result += "\tconst char *name;\n";
1083 Result += "\tstruct _objc_symtab *symtab;\n";
1084 Result += "};\n\n";
1085 Result += "static struct _objc_module "
1086 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001087 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1088 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001089 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001090}
Chris Lattner311ff022007-10-16 22:36:42 +00001091