blob: 8ff6bc6438816012954a4164b8be4cff3674a2ea [file] [log] [blame]
Chris Lattner77cd2a02007-10-11 00:43:27 +00001//===--- RewriteTest.cpp - Playground for the code rewriter ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Hacks and fun related to the code rewriter.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ASTConsumers.h"
Chris Lattner8a12c272007-10-11 18:38:32 +000015#include "clang/Rewrite/Rewriter.h"
Chris Lattner77cd2a02007-10-11 00:43:27 +000016#include "clang/AST/AST.h"
17#include "clang/AST/ASTConsumer.h"
Chris Lattner8a12c272007-10-11 18:38:32 +000018#include "clang/Basic/SourceManager.h"
Steve Naroffebf2b562007-10-23 23:50:29 +000019#include "clang/Basic/IdentifierTable.h"
Chris Lattner158ecb92007-10-25 17:07:24 +000020#include "llvm/ADT/StringExtras.h"
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000021#include "llvm/ADT/SmallPtrSet.h"
Steve Naroff2feac5e2007-10-30 03:43:13 +000022#include "clang/Lex/Lexer.h"
Chris Lattner77cd2a02007-10-11 00:43:27 +000023using namespace clang;
Chris Lattner158ecb92007-10-25 17:07:24 +000024using llvm::utostr;
Chris Lattner77cd2a02007-10-11 00:43:27 +000025
Chris Lattner77cd2a02007-10-11 00:43:27 +000026namespace {
Chris Lattner8a12c272007-10-11 18:38:32 +000027 class RewriteTest : public ASTConsumer {
Chris Lattner2c64b7b2007-10-16 21:07:07 +000028 Rewriter Rewrite;
Chris Lattner01c57482007-10-17 22:35:30 +000029 ASTContext *Context;
Chris Lattner77cd2a02007-10-11 00:43:27 +000030 SourceManager *SM;
Chris Lattner8a12c272007-10-11 18:38:32 +000031 unsigned MainFileID;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000032 SourceLocation LastIncLoc;
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000033 llvm::SmallVector<ObjcImplementationDecl *, 8> ClassImplementation;
34 llvm::SmallVector<ObjcCategoryImplDecl *, 8> CategoryImplementation;
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000035 llvm::SmallPtrSet<ObjcInterfaceDecl*, 8> ObjcSynthesizedStructs;
Steve Naroff8749be52007-10-31 22:11:35 +000036 llvm::SmallPtrSet<ObjcInterfaceDecl*, 8> ObjcForwardDecls;
Steve Naroffebf2b562007-10-23 23:50:29 +000037
38 FunctionDecl *MsgSendFunctionDecl;
39 FunctionDecl *GetClassFunctionDecl;
Steve Naroff934f2762007-10-24 22:48:43 +000040 FunctionDecl *SelGetUidFunctionDecl;
Steve Naroffebf2b562007-10-23 23:50:29 +000041
Steve Naroffbeaf2992007-11-03 11:27:19 +000042 // ObjC string constant support.
43 FileVarDecl *ConstantStringClassReference;
44 RecordDecl *NSStringRecord;
Steve Naroffab972d32007-11-04 22:37:50 +000045
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000046 static const int OBJC_ABI_VERSION =7 ;
Chris Lattner77cd2a02007-10-11 00:43:27 +000047 public:
Chris Lattner01c57482007-10-17 22:35:30 +000048 void Initialize(ASTContext &context, unsigned mainFileID) {
49 Context = &context;
50 SM = &Context->SourceMgr;
Chris Lattner8a12c272007-10-11 18:38:32 +000051 MainFileID = mainFileID;
Steve Naroffebf2b562007-10-23 23:50:29 +000052 MsgSendFunctionDecl = 0;
Steve Naroffc0006092007-10-24 01:09:48 +000053 GetClassFunctionDecl = 0;
Steve Naroff934f2762007-10-24 22:48:43 +000054 SelGetUidFunctionDecl = 0;
Steve Naroffbeaf2992007-11-03 11:27:19 +000055 ConstantStringClassReference = 0;
56 NSStringRecord = 0;
Chris Lattner01c57482007-10-17 22:35:30 +000057 Rewrite.setSourceMgr(Context->SourceMgr);
Steve Naroffe3abbf52007-11-05 14:55:35 +000058 // declaring objc_selector outside the parameter list removes a silly
59 // scope related warning...
Steve Naroff21867b12007-11-07 18:43:40 +000060 const char *s = "struct objc_selector; struct objc_class;\n"
Steve Naroffe3abbf52007-11-05 14:55:35 +000061 "extern struct objc_object *objc_msgSend"
Steve Naroffab972d32007-11-04 22:37:50 +000062 "(struct objc_object *, struct objc_selector *, ...);\n"
63 "extern struct objc_object *objc_getClass"
Steve Naroff21867b12007-11-07 18:43:40 +000064 "(const char *);\n"
65 "extern void objc_exception_throw(struct objc_object *);\n"
66 "extern void objc_exception_try_enter(void *);\n"
67 "extern void objc_exception_try_exit(void *);\n"
68 "extern struct objc_object *objc_exception_extract(void *);\n"
69 "extern int objc_exception_match"
70 "(struct objc_class *, struct objc_object *, ...);\n";
71
Steve Naroffab972d32007-11-04 22:37:50 +000072 Rewrite.InsertText(SourceLocation::getFileLoc(mainFileID, 0),
73 s, strlen(s));
Chris Lattner77cd2a02007-10-11 00:43:27 +000074 }
Chris Lattner8a12c272007-10-11 18:38:32 +000075
Chris Lattnerf04da132007-10-24 17:06:59 +000076 // Top Level Driver code.
77 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner2c64b7b2007-10-16 21:07:07 +000078 void HandleDeclInMainFile(Decl *D);
Chris Lattnerf04da132007-10-24 17:06:59 +000079 ~RewriteTest();
80
81 // Syntactic Rewriting.
Steve Naroffab972d32007-11-04 22:37:50 +000082 void RewritePrologue(SourceLocation Loc);
Chris Lattner2c64b7b2007-10-16 21:07:07 +000083 void RewriteInclude(SourceLocation Loc);
Chris Lattnerf04da132007-10-24 17:06:59 +000084 void RewriteTabs();
85 void RewriteForwardClassDecl(ObjcClassDecl *Dcl);
Steve Naroffbef11852007-10-26 20:53:56 +000086 void RewriteInterfaceDecl(ObjcInterfaceDecl *Dcl);
Steve Naroff423cb562007-10-30 13:30:57 +000087 void RewriteCategoryDecl(ObjcCategoryDecl *Dcl);
Steve Naroff752d6ef2007-10-30 16:42:30 +000088 void RewriteProtocolDecl(ObjcProtocolDecl *Dcl);
Steve Naroff423cb562007-10-30 13:30:57 +000089 void RewriteMethods(int nMethods, ObjcMethodDecl **Methods);
Fariborz Jahanian957cf652007-11-07 00:09:37 +000090 void RewriteProperties(int nProperties, ObjcPropertyDecl **Properties);
Steve Naroff09b266e2007-10-30 23:14:51 +000091 void RewriteFunctionDecl(FunctionDecl *FD);
Steve Naroffd5255f52007-11-01 13:24:47 +000092 void RewriteObjcQualifiedInterfaceTypes(
93 const FunctionTypeProto *proto, FunctionDecl *FD);
94 bool needToScanForQualifiers(QualType T);
Chris Lattner311ff022007-10-16 22:36:42 +000095
Chris Lattnerf04da132007-10-24 17:06:59 +000096 // Expression Rewriting.
Chris Lattnere64b7772007-10-24 16:57:36 +000097 Stmt *RewriteFunctionBody(Stmt *S);
98 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
Steve Naroffb42f8412007-11-05 14:50:49 +000099 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
Chris Lattnere64b7772007-10-24 16:57:36 +0000100 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000101 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000102 Stmt *RewriteObjcTryStmt(ObjcAtTryStmt *S);
103 Stmt *RewriteObjcCatchStmt(ObjcAtCatchStmt *S);
104 Stmt *RewriteObjcFinallyStmt(ObjcAtFinallyStmt *S);
Steve Naroff2bd03922007-11-07 15:32:26 +0000105 Stmt *RewriteObjcThrowStmt(ObjcAtThrowStmt *S);
Steve Naroff934f2762007-10-24 22:48:43 +0000106 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
107 Expr **args, unsigned nargs);
Steve Naroff09b266e2007-10-30 23:14:51 +0000108 void SynthMsgSendFunctionDecl();
109 void SynthGetClassFunctionDecl();
110
Chris Lattnerf04da132007-10-24 17:06:59 +0000111 // Metadata emission.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000112 void RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
113 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000114
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000115 void RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *CDecl,
116 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000117
118 void RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
119 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000120 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000121 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000122 const char *ClassName,
123 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000124
125 void RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
126 int NumProtocols,
127 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000128 const char *ClassName,
129 std::string &Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000130 void SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
131 std::string &Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000132 void SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
133 ObjcIvarDecl *ivar,
134 std::string &Result);
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000135 void WriteObjcMetaData(std::string &Result);
Chris Lattner77cd2a02007-10-11 00:43:27 +0000136 };
137}
138
Chris Lattner8a12c272007-10-11 18:38:32 +0000139ASTConsumer *clang::CreateCodeRewriterTest() { return new RewriteTest(); }
Chris Lattner77cd2a02007-10-11 00:43:27 +0000140
Chris Lattnerf04da132007-10-24 17:06:59 +0000141//===----------------------------------------------------------------------===//
142// Top Level Driver Code
143//===----------------------------------------------------------------------===//
144
Chris Lattner8a12c272007-10-11 18:38:32 +0000145void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000146 // Two cases: either the decl could be in the main file, or it could be in a
147 // #included file. If the former, rewrite it now. If the later, check to see
148 // if we rewrote the #include/#import.
149 SourceLocation Loc = D->getLocation();
150 Loc = SM->getLogicalLoc(Loc);
151
152 // If this is for a builtin, ignore it.
153 if (Loc.isInvalid()) return;
154
Steve Naroffebf2b562007-10-23 23:50:29 +0000155 // Look for built-in declarations that we need to refer during the rewrite.
156 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Steve Naroff09b266e2007-10-30 23:14:51 +0000157 RewriteFunctionDecl(FD);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000158 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
159 // declared in <Foundation/NSString.h>
160 if (strcmp(FVD->getName(), "_NSConstantStringClassReference") == 0) {
161 ConstantStringClassReference = FVD;
162 return;
163 }
Steve Naroffbef11852007-10-26 20:53:56 +0000164 } else if (ObjcInterfaceDecl *MD = dyn_cast<ObjcInterfaceDecl>(D)) {
165 RewriteInterfaceDecl(MD);
Steve Naroff423cb562007-10-30 13:30:57 +0000166 } else if (ObjcCategoryDecl *CD = dyn_cast<ObjcCategoryDecl>(D)) {
167 RewriteCategoryDecl(CD);
Steve Naroff752d6ef2007-10-30 16:42:30 +0000168 } else if (ObjcProtocolDecl *PD = dyn_cast<ObjcProtocolDecl>(D)) {
169 RewriteProtocolDecl(PD);
Steve Naroffebf2b562007-10-23 23:50:29 +0000170 }
Chris Lattnerf04da132007-10-24 17:06:59 +0000171 // If we have a decl in the main file, see if we should rewrite it.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000172 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
173 return HandleDeclInMainFile(D);
174
Chris Lattnerf04da132007-10-24 17:06:59 +0000175 // Otherwise, see if there is a #import in the main file that should be
176 // rewritten.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000177 RewriteInclude(Loc);
178}
179
Chris Lattnerf04da132007-10-24 17:06:59 +0000180/// HandleDeclInMainFile - This is called for each top-level decl defined in the
181/// main file of the input.
182void RewriteTest::HandleDeclInMainFile(Decl *D) {
183 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
184 if (Stmt *Body = FD->getBody())
185 FD->setBody(RewriteFunctionBody(Body));
186
187 if (ObjcImplementationDecl *CI = dyn_cast<ObjcImplementationDecl>(D))
188 ClassImplementation.push_back(CI);
189 else if (ObjcCategoryImplDecl *CI = dyn_cast<ObjcCategoryImplDecl>(D))
190 CategoryImplementation.push_back(CI);
191 else if (ObjcClassDecl *CD = dyn_cast<ObjcClassDecl>(D))
192 RewriteForwardClassDecl(CD);
193 // Nothing yet.
194}
195
196RewriteTest::~RewriteTest() {
197 // Get the top-level buffer that this corresponds to.
198 RewriteTabs();
199
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000200 // Rewrite Objective-c meta data*
201 std::string ResultStr;
202 WriteObjcMetaData(ResultStr);
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000203
Chris Lattnerf04da132007-10-24 17:06:59 +0000204 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
205 // we are done.
206 if (const RewriteBuffer *RewriteBuf =
207 Rewrite.getRewriteBufferFor(MainFileID)) {
Steve Naroffbeaf2992007-11-03 11:27:19 +0000208 //printf("Changed:\n");
Chris Lattnerf04da132007-10-24 17:06:59 +0000209 std::string S(RewriteBuf->begin(), RewriteBuf->end());
210 printf("%s\n", S.c_str());
211 } else {
212 printf("No changes\n");
213 }
Fariborz Jahanian4402d812007-11-07 18:40:28 +0000214 // Emit metadata.
215 printf("%s", ResultStr.c_str());
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000216}
217
Chris Lattnerf04da132007-10-24 17:06:59 +0000218//===----------------------------------------------------------------------===//
219// Syntactic (non-AST) Rewriting Code
220//===----------------------------------------------------------------------===//
221
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000222void RewriteTest::RewriteInclude(SourceLocation Loc) {
223 // Rip up the #include stack to the main file.
224 SourceLocation IncLoc = Loc, NextLoc = Loc;
225 do {
226 IncLoc = Loc;
227 Loc = SM->getLogicalLoc(NextLoc);
228 NextLoc = SM->getIncludeLoc(Loc);
229 } while (!NextLoc.isInvalid());
230
231 // Loc is now the location of the #include filename "foo" or <foo/bar.h>.
232 // IncLoc indicates the header that was included if it is useful.
233 IncLoc = SM->getLogicalLoc(IncLoc);
234 if (SM->getDecomposedFileLoc(Loc).first != MainFileID ||
235 Loc == LastIncLoc)
236 return;
237 LastIncLoc = Loc;
238
239 unsigned IncCol = SM->getColumnNumber(Loc);
240 SourceLocation LineStartLoc = Loc.getFileLocWithOffset(-IncCol+1);
241
242 // Replace the #import with #include.
243 Rewrite.ReplaceText(LineStartLoc, IncCol-1, "#include ", strlen("#include "));
244}
245
Chris Lattnerf04da132007-10-24 17:06:59 +0000246void RewriteTest::RewriteTabs() {
247 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
248 const char *MainBufStart = MainBuf.first;
249 const char *MainBufEnd = MainBuf.second;
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +0000250
Chris Lattnerf04da132007-10-24 17:06:59 +0000251 // Loop over the whole file, looking for tabs.
252 for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
253 if (*BufPtr != '\t')
254 continue;
255
256 // Okay, we found a tab. This tab will turn into at least one character,
257 // but it depends on which 'virtual column' it is in. Compute that now.
258 unsigned VCol = 0;
259 while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
260 BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
261 ++VCol;
262
263 // Okay, now that we know the virtual column, we know how many spaces to
264 // insert. We assume 8-character tab-stops.
265 unsigned Spaces = 8-(VCol & 7);
266
267 // Get the location of the tab.
268 SourceLocation TabLoc =
269 SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
270
271 // Rewrite the single tab character into a sequence of spaces.
272 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
273 }
Chris Lattner8a12c272007-10-11 18:38:32 +0000274}
275
276
Chris Lattnerf04da132007-10-24 17:06:59 +0000277void RewriteTest::RewriteForwardClassDecl(ObjcClassDecl *ClassDecl) {
278 int numDecls = ClassDecl->getNumForwardDecls();
279 ObjcInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
280
281 // Get the start location and compute the semi location.
282 SourceLocation startLoc = ClassDecl->getLocation();
283 const char *startBuf = SM->getCharacterData(startLoc);
284 const char *semiPtr = strchr(startBuf, ';');
285
286 // Translate to typedef's that forward reference structs with the same name
287 // as the class. As a convenience, we include the original declaration
288 // as a comment.
289 std::string typedefString;
290 typedefString += "// ";
Steve Naroff934f2762007-10-24 22:48:43 +0000291 typedefString.append(startBuf, semiPtr-startBuf+1);
292 typedefString += "\n";
293 for (int i = 0; i < numDecls; i++) {
294 ObjcInterfaceDecl *ForwardDecl = ForwardDecls[i];
Steve Naroff8749be52007-10-31 22:11:35 +0000295 if (ObjcForwardDecls.count(ForwardDecl))
296 continue;
Steve Naroff352336b2007-11-05 14:36:37 +0000297 typedefString += "typedef struct objc_object ";
Steve Naroff934f2762007-10-24 22:48:43 +0000298 typedefString += ForwardDecl->getName();
299 typedefString += ";\n";
Steve Naroff8749be52007-10-31 22:11:35 +0000300
301 // Mark this typedef as having been generated.
302 if (!ObjcForwardDecls.insert(ForwardDecl))
Fariborz Jahanianaff56d02007-10-31 22:57:04 +0000303 assert(false && "typedef already output");
Steve Naroff934f2762007-10-24 22:48:43 +0000304 }
305
306 // Replace the @class with typedefs corresponding to the classes.
307 Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1,
308 typedefString.c_str(), typedefString.size());
Chris Lattnerf04da132007-10-24 17:06:59 +0000309}
310
Steve Naroff423cb562007-10-30 13:30:57 +0000311void RewriteTest::RewriteMethods(int nMethods, ObjcMethodDecl **Methods) {
312 for (int i = 0; i < nMethods; i++) {
313 ObjcMethodDecl *Method = Methods[i];
314 SourceLocation Loc = Method->getLocStart();
315
316 Rewrite.ReplaceText(Loc, 0, "// ", 3);
317
318 // FIXME: handle methods that are declared across multiple lines.
319 }
320}
321
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000322void RewriteTest::RewriteProperties(int nProperties, ObjcPropertyDecl **Properties)
323{
324 for (int i = 0; i < nProperties; i++) {
325 ObjcPropertyDecl *Property = Properties[i];
326 SourceLocation Loc = Property->getLocation();
327
328 Rewrite.ReplaceText(Loc, 0, "// ", 3);
329
330 // FIXME: handle properties that are declared across multiple lines.
331 }
332}
333
Steve Naroff423cb562007-10-30 13:30:57 +0000334void RewriteTest::RewriteCategoryDecl(ObjcCategoryDecl *CatDecl) {
335 SourceLocation LocStart = CatDecl->getLocStart();
336
337 // FIXME: handle category headers that are declared across multiple lines.
338 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
339
340 RewriteMethods(CatDecl->getNumInstanceMethods(),
341 CatDecl->getInstanceMethods());
342 RewriteMethods(CatDecl->getNumClassMethods(),
343 CatDecl->getClassMethods());
344 // Lastly, comment out the @end.
345 Rewrite.ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
346}
347
Steve Naroff752d6ef2007-10-30 16:42:30 +0000348void RewriteTest::RewriteProtocolDecl(ObjcProtocolDecl *PDecl) {
349 SourceLocation LocStart = PDecl->getLocStart();
350
351 // FIXME: handle protocol headers that are declared across multiple lines.
352 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
353
354 RewriteMethods(PDecl->getNumInstanceMethods(),
355 PDecl->getInstanceMethods());
356 RewriteMethods(PDecl->getNumClassMethods(),
357 PDecl->getClassMethods());
358 // Lastly, comment out the @end.
359 Rewrite.ReplaceText(PDecl->getAtEndLoc(), 0, "// ", 3);
360}
361
Steve Naroffbef11852007-10-26 20:53:56 +0000362void RewriteTest::RewriteInterfaceDecl(ObjcInterfaceDecl *ClassDecl) {
Steve Narofff908a872007-10-30 02:23:23 +0000363
364 SourceLocation LocStart = ClassDecl->getLocStart();
365 SourceLocation LocEnd = ClassDecl->getLocEnd();
366
367 const char *startBuf = SM->getCharacterData(LocStart);
368 const char *endBuf = SM->getCharacterData(LocEnd);
369
Steve Naroff2feac5e2007-10-30 03:43:13 +0000370 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
Steve Narofff908a872007-10-30 02:23:23 +0000371
372 std::string ResultStr;
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000373 if (!ObjcForwardDecls.count(ClassDecl)) {
374 // we haven't seen a forward decl - generate a typedef.
Steve Naroff352336b2007-11-05 14:36:37 +0000375 ResultStr += "typedef struct objc_object ";
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000376 ResultStr += ClassDecl->getName();
377 ResultStr += ";";
378
379 // Mark this typedef as having been generated.
380 ObjcForwardDecls.insert(ClassDecl);
381 }
Steve Narofff908a872007-10-30 02:23:23 +0000382 SynthesizeObjcInternalStruct(ClassDecl, ResultStr);
383
Steve Naroff2feac5e2007-10-30 03:43:13 +0000384 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
Steve Narofff908a872007-10-30 02:23:23 +0000385 ResultStr.c_str(), ResultStr.size());
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000386 RewriteProperties(ClassDecl->getNumPropertyDecl(),
387 ClassDecl->getPropertyDecl());
Steve Naroff423cb562007-10-30 13:30:57 +0000388 RewriteMethods(ClassDecl->getNumInstanceMethods(),
389 ClassDecl->getInstanceMethods());
390 RewriteMethods(ClassDecl->getNumClassMethods(),
391 ClassDecl->getClassMethods());
Steve Naroffbef11852007-10-26 20:53:56 +0000392
Steve Naroff2feac5e2007-10-30 03:43:13 +0000393 // Lastly, comment out the @end.
394 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroffbef11852007-10-26 20:53:56 +0000395}
396
Chris Lattnerf04da132007-10-24 17:06:59 +0000397//===----------------------------------------------------------------------===//
398// Function Body / Expression rewriting
399//===----------------------------------------------------------------------===//
400
Chris Lattnere64b7772007-10-24 16:57:36 +0000401Stmt *RewriteTest::RewriteFunctionBody(Stmt *S) {
Chris Lattner311ff022007-10-16 22:36:42 +0000402 // Otherwise, just rewrite all children.
403 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
404 CI != E; ++CI)
Steve Naroff75730982007-11-07 04:08:17 +0000405 if (*CI) {
406 Stmt *newStmt = RewriteFunctionBody(*CI);
407 if (newStmt)
408 *CI = newStmt;
409 }
Steve Naroffebf2b562007-10-23 23:50:29 +0000410
411 // Handle specific things.
412 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
413 return RewriteAtEncode(AtEncode);
Steve Naroffb42f8412007-11-05 14:50:49 +0000414
415 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
416 return RewriteAtSelector(AtSelector);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000417
418 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
419 return RewriteObjCStringLiteral(AtString);
Steve Naroffebf2b562007-10-23 23:50:29 +0000420
Steve Naroff934f2762007-10-24 22:48:43 +0000421 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
422 // Before we rewrite it, put the original message expression in a comment.
423 SourceLocation startLoc = MessExpr->getLocStart();
424 SourceLocation endLoc = MessExpr->getLocEnd();
425
426 const char *startBuf = SM->getCharacterData(startLoc);
427 const char *endBuf = SM->getCharacterData(endLoc);
428
429 std::string messString;
430 messString += "// ";
431 messString.append(startBuf, endBuf-startBuf+1);
432 messString += "\n";
Steve Naroffbef11852007-10-26 20:53:56 +0000433
Steve Naroff934f2762007-10-24 22:48:43 +0000434 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
435 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
436 // Tried this, but it didn't work either...
Steve Naroff752d6ef2007-10-30 16:42:30 +0000437 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffebf2b562007-10-23 23:50:29 +0000438 return RewriteMessageExpr(MessExpr);
Steve Naroff934f2762007-10-24 22:48:43 +0000439 }
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000440
441 if (ObjcAtTryStmt *StmtTry = dyn_cast<ObjcAtTryStmt>(S))
442 return RewriteObjcTryStmt(StmtTry);
Steve Naroff2bd03922007-11-07 15:32:26 +0000443
444 if (ObjcAtThrowStmt *StmtThrow = dyn_cast<ObjcAtThrowStmt>(S))
445 return RewriteObjcThrowStmt(StmtThrow);
446
Chris Lattnere64b7772007-10-24 16:57:36 +0000447 // Return this stmt unmodified.
448 return S;
Chris Lattner311ff022007-10-16 22:36:42 +0000449}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +0000450
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000451Stmt *RewriteTest::RewriteObjcTryStmt(ObjcAtTryStmt *S) {
Steve Naroff75730982007-11-07 04:08:17 +0000452 // Get the start location and compute the semi location.
453 SourceLocation startLoc = S->getLocStart();
454 const char *startBuf = SM->getCharacterData(startLoc);
455
456 assert((*startBuf == '@') && "bogus @try location");
457
458 std::string buf;
459 // declare a new scope with two variables, _stack and _rethrow.
460 buf = "/* @try scope begin */ { struct _objc_exception_data {\n";
461 buf += "int buf[18/*32-bit i386*/];\n";
462 buf += "char *pointers[4];} _stack;\n";
463 buf += "id volatile _rethrow = 0;\n";
464 buf += "objc_exception_try_enter(&_stack);\n";
Steve Naroff21867b12007-11-07 18:43:40 +0000465 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
Steve Naroff75730982007-11-07 04:08:17 +0000466
467 Rewrite.ReplaceText(startLoc, 4, buf.c_str(), buf.size());
468
469 startLoc = S->getTryBody()->getLocEnd();
470 startBuf = SM->getCharacterData(startLoc);
471
472 assert((*startBuf == '}') && "bogus @try block");
473
474 SourceLocation lastCurlyLoc = startLoc;
475
476 startLoc = startLoc.getFileLocWithOffset(1);
477 buf = " /* @catch begin */ else {\n";
478 buf += " id _caught = objc_exception_extract(&_stack);\n";
479 buf += " objc_exception_try_enter (&_stack);\n";
Steve Naroff21867b12007-11-07 18:43:40 +0000480 buf += " if (_setjmp(_stack.buf))\n";
Steve Naroff75730982007-11-07 04:08:17 +0000481 buf += " _rethrow = objc_exception_extract(&_stack);\n";
482 buf += " else { /* @catch continue */";
483
484 Rewrite.ReplaceText(startLoc, 0, buf.c_str(), buf.size());
485
486 bool sawIdTypedCatch = false;
487 Stmt *lastCatchBody = 0;
488 ObjcAtCatchStmt *catchList = S->getCatchStmts();
489 while (catchList) {
490 Stmt *catchStmt = catchList->getCatchParamStmt();
491
492 if (catchList == S->getCatchStmts())
493 buf = "if ("; // we are generating code for the first catch clause
494 else
495 buf = "else if (";
496 startLoc = catchList->getLocStart();
497 startBuf = SM->getCharacterData(startLoc);
498
499 assert((*startBuf == '@') && "bogus @catch location");
500
501 const char *lParenLoc = strchr(startBuf, '(');
502
503 if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) {
504 QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType();
505 if (t == Context->getObjcIdType()) {
506 buf += "1) { ";
507 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
508 buf.c_str(), buf.size());
509 sawIdTypedCatch = true;
510 } else if (const PointerType *pType = t->getAsPointerType()) {
511 ObjcInterfaceType *cls; // Should be a pointer to a class.
512
513 cls = dyn_cast<ObjcInterfaceType>(pType->getPointeeType().getTypePtr());
514 if (cls) {
Steve Naroff21867b12007-11-07 18:43:40 +0000515 buf += "objc_exception_match((struct objc_class *)objc_getClass(\"";
Steve Naroff75730982007-11-07 04:08:17 +0000516 buf += cls->getDecl()->getName();
Steve Naroff21867b12007-11-07 18:43:40 +0000517 buf += "\"), (struct objc_object *)_caught)) { ";
Steve Naroff75730982007-11-07 04:08:17 +0000518 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
519 buf.c_str(), buf.size());
520 }
521 }
522 // Now rewrite the body...
523 lastCatchBody = catchList->getCatchBody();
524 SourceLocation rParenLoc = catchList->getRParenLoc();
525 SourceLocation bodyLoc = lastCatchBody->getLocStart();
526 const char *bodyBuf = SM->getCharacterData(bodyLoc);
527 const char *rParenBuf = SM->getCharacterData(rParenLoc);
528 assert((*rParenBuf == ')') && "bogus @catch paren location");
529 assert((*bodyBuf == '{') && "bogus @catch body location");
530
531 buf = " = _caught;";
532 // Here we replace ") {" with "= _caught;" (which initializes and
533 // declares the @catch parameter).
534 Rewrite.ReplaceText(rParenLoc, bodyBuf-rParenBuf+1,
535 buf.c_str(), buf.size());
Steve Naroff2bd03922007-11-07 15:32:26 +0000536 } else if (!isa<NullStmt>(catchStmt)) {
Steve Naroff75730982007-11-07 04:08:17 +0000537 assert(false && "@catch rewrite bug");
Steve Naroff2bd03922007-11-07 15:32:26 +0000538 }
Steve Naroff75730982007-11-07 04:08:17 +0000539 catchList = catchList->getNextCatchStmt();
540 }
541 // Complete the catch list...
542 if (lastCatchBody) {
543 SourceLocation bodyLoc = lastCatchBody->getLocEnd();
544 const char *bodyBuf = SM->getCharacterData(bodyLoc);
545 assert((*bodyBuf == '}') && "bogus @catch body location");
546 bodyLoc = bodyLoc.getFileLocWithOffset(1);
547 buf = " } } /* @catch end */\n";
548
549 Rewrite.ReplaceText(bodyLoc, 0, buf.c_str(), buf.size());
550
551 // Set lastCurlyLoc
552 lastCurlyLoc = lastCatchBody->getLocEnd();
553 }
554 if (ObjcAtFinallyStmt *finalStmt = S->getFinallyStmt()) {
555 startLoc = finalStmt->getLocStart();
556 startBuf = SM->getCharacterData(startLoc);
557 assert((*startBuf == '@') && "bogus @finally start");
558
559 buf = "/* @finally */";
560 Rewrite.ReplaceText(startLoc, 8, buf.c_str(), buf.size());
561
562 Stmt *body = finalStmt->getFinallyBody();
563 SourceLocation startLoc = body->getLocStart();
564 SourceLocation endLoc = body->getLocEnd();
565 const char *startBuf = SM->getCharacterData(startLoc);
566 const char *endBuf = SM->getCharacterData(endLoc);
567 assert((*startBuf == '{') && "bogus @finally body location");
568 assert((*endBuf == '}') && "bogus @finally body location");
569
570 startLoc = startLoc.getFileLocWithOffset(1);
571 buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
572 Rewrite.ReplaceText(startLoc, 0, buf.c_str(), buf.size());
573 endLoc = endLoc.getFileLocWithOffset(-1);
574 buf = " if (_rethrow) objc_exception_throw(_rethrow);\n";
575 Rewrite.ReplaceText(endLoc, 0, buf.c_str(), buf.size());
576
577 // Set lastCurlyLoc
578 lastCurlyLoc = body->getLocEnd();
579 }
580 // Now emit the final closing curly brace...
581 lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1);
582 buf = " } /* @try scope end */\n";
583 Rewrite.ReplaceText(lastCurlyLoc, 0, buf.c_str(), buf.size());
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000584 return 0;
585}
586
587Stmt *RewriteTest::RewriteObjcCatchStmt(ObjcAtCatchStmt *S) {
588 return 0;
589}
590
591Stmt *RewriteTest::RewriteObjcFinallyStmt(ObjcAtFinallyStmt *S) {
592 return 0;
593}
594
Steve Naroff2bd03922007-11-07 15:32:26 +0000595// This can't be done with Rewrite.ReplaceStmt(S, ThrowExpr), since
596// the throw expression is typically a message expression that's already
597// been rewritten! (which implies the SourceLocation's are invalid).
598Stmt *RewriteTest::RewriteObjcThrowStmt(ObjcAtThrowStmt *S) {
599 // Get the start location and compute the semi location.
600 SourceLocation startLoc = S->getLocStart();
601 const char *startBuf = SM->getCharacterData(startLoc);
602
603 assert((*startBuf == '@') && "bogus @throw location");
604
605 std::string buf;
606 /* void objc_exception_throw(id) __attribute__((noreturn)); */
607 buf = "objc_exception_throw(";
608 Rewrite.ReplaceText(startLoc, 6, buf.c_str(), buf.size());
609 const char *semiBuf = strchr(startBuf, ';');
610 assert((*semiBuf == ';') && "@throw: can't find ';'");
611 SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf);
612 buf = ");";
613 Rewrite.ReplaceText(semiLoc, 1, buf.c_str(), buf.size());
614 return 0;
615}
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000616
Chris Lattnere64b7772007-10-24 16:57:36 +0000617Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattner01c57482007-10-17 22:35:30 +0000618 // Create a new string expression.
619 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000620 std::string StrEncoding;
621 Context->getObjcEncodingForType(Exp->getEncodedType(), StrEncoding);
622 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
623 StrEncoding.length(), false, StrType,
Chris Lattner01c57482007-10-17 22:35:30 +0000624 SourceLocation(), SourceLocation());
625 Rewrite.ReplaceStmt(Exp, Replacement);
Chris Lattnere64b7772007-10-24 16:57:36 +0000626 delete Exp;
627 return Replacement;
Chris Lattner311ff022007-10-16 22:36:42 +0000628}
629
Steve Naroffb42f8412007-11-05 14:50:49 +0000630Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) {
631 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
632 // Create a call to sel_registerName("selName").
633 llvm::SmallVector<Expr*, 8> SelExprs;
634 QualType argType = Context->getPointerType(Context->CharTy);
635 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
636 Exp->getSelector().getName().size(),
637 false, argType, SourceLocation(),
638 SourceLocation()));
639 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
640 &SelExprs[0], SelExprs.size());
641 Rewrite.ReplaceStmt(Exp, SelExp);
642 delete Exp;
643 return SelExp;
644}
645
Steve Naroff934f2762007-10-24 22:48:43 +0000646CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
647 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffebf2b562007-10-23 23:50:29 +0000648 // Get the type, we will need to reference it in a couple spots.
Steve Naroff934f2762007-10-24 22:48:43 +0000649 QualType msgSendType = FD->getType();
Steve Naroffebf2b562007-10-23 23:50:29 +0000650
651 // Create a reference to the objc_msgSend() declaration.
Steve Naroff934f2762007-10-24 22:48:43 +0000652 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffebf2b562007-10-23 23:50:29 +0000653
654 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerf04da132007-10-24 17:06:59 +0000655 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffebf2b562007-10-23 23:50:29 +0000656 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
657
658 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattnere64b7772007-10-24 16:57:36 +0000659
Steve Naroff934f2762007-10-24 22:48:43 +0000660 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
661}
662
Steve Naroffd5255f52007-11-01 13:24:47 +0000663static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
664 const char *&startRef, const char *&endRef) {
665 while (startBuf < endBuf) {
666 if (*startBuf == '<')
667 startRef = startBuf; // mark the start.
668 if (*startBuf == '>') {
669 assert((startRef && *startRef == '<') && "rewrite scanning error");
670 endRef = startBuf; // mark the end.
671 return true;
672 }
673 startBuf++;
674 }
675 return false;
676}
677
678bool RewriteTest::needToScanForQualifiers(QualType T) {
679 // FIXME: we don't currently represent "id <Protocol>" in the type system.
680 if (T == Context->getObjcIdType())
681 return true;
682
683 if (const PointerType *pType = T->getAsPointerType()) {
Steve Naroff9165ad32007-10-31 04:38:33 +0000684 Type *pointeeType = pType->getPointeeType().getTypePtr();
685 if (isa<ObjcQualifiedInterfaceType>(pointeeType))
686 return true; // we have "Class <Protocol> *".
687 }
Steve Naroffd5255f52007-11-01 13:24:47 +0000688 return false;
689}
690
691void RewriteTest::RewriteObjcQualifiedInterfaceTypes(
692 const FunctionTypeProto *proto, FunctionDecl *FD) {
693
694 if (needToScanForQualifiers(proto->getResultType())) {
695 // Since types are unique, we need to scan the buffer.
696 SourceLocation Loc = FD->getLocation();
697
698 const char *endBuf = SM->getCharacterData(Loc);
699 const char *startBuf = endBuf;
700 while (*startBuf != ';')
701 startBuf--; // scan backward (from the decl location) for return type.
702 const char *startRef = 0, *endRef = 0;
703 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
704 // Get the locations of the startRef, endRef.
705 SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf);
706 SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1);
707 // Comment out the protocol references.
708 Rewrite.ReplaceText(LessLoc, 0, "/*", 2);
709 Rewrite.ReplaceText(GreaterLoc, 0, "*/", 2);
Steve Naroff9165ad32007-10-31 04:38:33 +0000710 }
711 }
Steve Naroffd5255f52007-11-01 13:24:47 +0000712 // Now check arguments.
713 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
714 if (needToScanForQualifiers(proto->getArgType(i))) {
715 // Since types are unique, we need to scan the buffer.
716 SourceLocation Loc = FD->getLocation();
717
718 const char *startBuf = SM->getCharacterData(Loc);
719 const char *endBuf = startBuf;
720 while (*endBuf != ';')
721 endBuf++; // scan forward (from the decl location) for argument types.
722 const char *startRef = 0, *endRef = 0;
723 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
724 // Get the locations of the startRef, endRef.
725 SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf);
726 SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1);
727 // Comment out the protocol references.
728 Rewrite.ReplaceText(LessLoc, 0, "/*", 2);
729 Rewrite.ReplaceText(GreaterLoc, 0, "*/", 2);
730 }
731 }
732 }
Steve Naroff9165ad32007-10-31 04:38:33 +0000733}
734
Steve Naroff09b266e2007-10-30 23:14:51 +0000735void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
736 // declared in <objc/objc.h>
Steve Naroffbeaf2992007-11-03 11:27:19 +0000737 if (strcmp(FD->getName(), "sel_registerName") == 0) {
Steve Naroff09b266e2007-10-30 23:14:51 +0000738 SelGetUidFunctionDecl = FD;
Steve Naroff9165ad32007-10-31 04:38:33 +0000739 return;
740 }
741 // Check for ObjC 'id' and class types that have been adorned with protocol
742 // information (id<p>, C<p>*). The protocol references need to be rewritten!
743 const FunctionType *funcType = FD->getType()->getAsFunctionType();
744 assert(funcType && "missing function type");
Steve Naroffd5255f52007-11-01 13:24:47 +0000745 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcType))
746 RewriteObjcQualifiedInterfaceTypes(proto, FD);
Steve Naroff09b266e2007-10-30 23:14:51 +0000747}
748
749// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
750void RewriteTest::SynthMsgSendFunctionDecl() {
751 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
752 llvm::SmallVector<QualType, 16> ArgTys;
753 QualType argT = Context->getObjcIdType();
754 assert(!argT.isNull() && "Can't find 'id' type");
755 ArgTys.push_back(argT);
756 argT = Context->getObjcSelType();
757 assert(!argT.isNull() && "Can't find 'SEL' type");
758 ArgTys.push_back(argT);
759 QualType msgSendType = Context->getFunctionType(Context->getObjcIdType(),
760 &ArgTys[0], ArgTys.size(),
761 true /*isVariadic*/);
762 MsgSendFunctionDecl = new FunctionDecl(SourceLocation(),
763 msgSendIdent, msgSendType,
764 FunctionDecl::Extern, false, 0);
765}
766
767// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
768void RewriteTest::SynthGetClassFunctionDecl() {
769 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
770 llvm::SmallVector<QualType, 16> ArgTys;
771 ArgTys.push_back(Context->getPointerType(
772 Context->CharTy.getQualifiedType(QualType::Const)));
773 QualType getClassType = Context->getFunctionType(Context->getObjcIdType(),
774 &ArgTys[0], ArgTys.size(),
775 false /*isVariadic*/);
776 GetClassFunctionDecl = new FunctionDecl(SourceLocation(),
777 getClassIdent, getClassType,
778 FunctionDecl::Extern, false, 0);
779}
780
Steve Naroffbeaf2992007-11-03 11:27:19 +0000781Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
782 assert(ConstantStringClassReference && "Can't find constant string reference");
783 llvm::SmallVector<Expr*, 4> InitExprs;
784
785 // Synthesize "(Class)&_NSConstantStringClassReference"
786 DeclRefExpr *ClsRef = new DeclRefExpr(ConstantStringClassReference,
787 ConstantStringClassReference->getType(),
788 SourceLocation());
789 QualType expType = Context->getPointerType(ClsRef->getType());
790 UnaryOperator *Unop = new UnaryOperator(ClsRef, UnaryOperator::AddrOf,
791 expType, SourceLocation());
792 CastExpr *cast = new CastExpr(Context->getObjcClassType(), Unop,
793 SourceLocation());
794 InitExprs.push_back(cast); // set the 'isa'.
795 InitExprs.push_back(Exp->getString()); // set "char *bytes".
796 unsigned IntSize = static_cast<unsigned>(
797 Context->getTypeSize(Context->IntTy, Exp->getLocStart()));
798 llvm::APInt IntVal(IntSize, Exp->getString()->getByteLength());
799 IntegerLiteral *len = new IntegerLiteral(IntVal, Context->IntTy,
800 Exp->getLocStart());
801 InitExprs.push_back(len); // set "int numBytes".
802
803 // struct NSConstantString
804 QualType CFConstantStrType = Context->getCFConstantStringType();
805 // (struct NSConstantString) { <exprs from above> }
806 InitListExpr *ILE = new InitListExpr(SourceLocation(),
807 &InitExprs[0], InitExprs.size(),
808 SourceLocation());
809 CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE);
810 // struct NSConstantString *
811 expType = Context->getPointerType(StrRep->getType());
812 Unop = new UnaryOperator(StrRep, UnaryOperator::AddrOf, expType,
813 SourceLocation());
Steve Naroff352336b2007-11-05 14:36:37 +0000814 // cast to NSConstantString *
815 cast = new CastExpr(Exp->getType(), Unop, SourceLocation());
Steve Naroffbeaf2992007-11-03 11:27:19 +0000816 Rewrite.ReplaceStmt(Exp, cast);
817 delete Exp;
Steve Naroff352336b2007-11-05 14:36:37 +0000818 return cast;
Steve Naroffbeaf2992007-11-03 11:27:19 +0000819}
820
Steve Naroff934f2762007-10-24 22:48:43 +0000821Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
Steve Naroffbeaf2992007-11-03 11:27:19 +0000822 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
Steve Naroff09b266e2007-10-30 23:14:51 +0000823 if (!MsgSendFunctionDecl)
824 SynthMsgSendFunctionDecl();
825 if (!GetClassFunctionDecl)
826 SynthGetClassFunctionDecl();
Steve Naroff934f2762007-10-24 22:48:43 +0000827
828 // Synthesize a call to objc_msgSend().
829 llvm::SmallVector<Expr*, 8> MsgExprs;
830 IdentifierInfo *clsName = Exp->getClassName();
831
832 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
833 if (clsName) { // class message.
834 llvm::SmallVector<Expr*, 8> ClsExprs;
835 QualType argType = Context->getPointerType(Context->CharTy);
836 ClsExprs.push_back(new StringLiteral(clsName->getName(),
837 clsName->getLength(),
838 false, argType, SourceLocation(),
839 SourceLocation()));
840 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
841 &ClsExprs[0], ClsExprs.size());
842 MsgExprs.push_back(Cls);
843 } else // instance message.
844 MsgExprs.push_back(Exp->getReceiver());
845
Steve Naroffbeaf2992007-11-03 11:27:19 +0000846 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
Steve Naroff934f2762007-10-24 22:48:43 +0000847 llvm::SmallVector<Expr*, 8> SelExprs;
848 QualType argType = Context->getPointerType(Context->CharTy);
849 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
850 Exp->getSelector().getName().size(),
851 false, argType, SourceLocation(),
852 SourceLocation()));
853 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
854 &SelExprs[0], SelExprs.size());
855 MsgExprs.push_back(SelExp);
856
857 // Now push any user supplied arguments.
858 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
859 MsgExprs.push_back(Exp->getArg(i));
860 // We've transferred the ownership to MsgExprs. Null out the argument in
861 // the original expression, since we will delete it below.
862 Exp->setArg(i, 0);
863 }
Steve Naroffab972d32007-11-04 22:37:50 +0000864 // Generate the funky cast.
865 CastExpr *cast;
866 llvm::SmallVector<QualType, 8> ArgTypes;
867 QualType returnType;
868
869 // Push 'id' and 'SEL', the 2 implicit arguments.
870 ArgTypes.push_back(Context->getObjcIdType());
871 ArgTypes.push_back(Context->getObjcSelType());
872 if (ObjcMethodDecl *mDecl = Exp->getMethodDecl()) {
873 // Push any user argument types.
Steve Naroff352336b2007-11-05 14:36:37 +0000874 for (int i = 0; i < mDecl->getNumParams(); i++) {
875 QualType t = mDecl->getParamDecl(i)->getType();
876 if (t == Context->getObjcClassType())
877 t = Context->getObjcIdType(); // Convert "Class"->"id"
878 ArgTypes.push_back(t);
879 }
Steve Naroffab972d32007-11-04 22:37:50 +0000880 returnType = mDecl->getResultType();
881 } else {
882 returnType = Context->getObjcIdType();
883 }
884 // Get the type, we will need to reference it in a couple spots.
885 QualType msgSendType = MsgSendFunctionDecl->getType();
886
887 // Create a reference to the objc_msgSend() declaration.
888 DeclRefExpr *DRE = new DeclRefExpr(MsgSendFunctionDecl, msgSendType, SourceLocation());
889
890 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
891 // If we don't do this cast, we get the following bizarre warning/note:
892 // xx.m:13: warning: function called through a non-compatible type
893 // xx.m:13: note: if this code is reached, the program will abort
894 cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE,
895 SourceLocation());
896
897 // Now do the "normal" pointer to function cast.
898 QualType castType = Context->getFunctionType(returnType,
899 &ArgTypes[0], ArgTypes.size(),
900 false/*FIXME:variadic*/);
901 castType = Context->getPointerType(castType);
902 cast = new CastExpr(castType, cast, SourceLocation());
903
904 // Don't forget the parens to enforce the proper binding.
905 ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
906
907 const FunctionType *FT = msgSendType->getAsFunctionType();
908 CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
909 FT->getResultType(), SourceLocation());
Steve Naroff934f2762007-10-24 22:48:43 +0000910 // Now do the actual rewrite.
Steve Naroffab972d32007-11-04 22:37:50 +0000911 Rewrite.ReplaceStmt(Exp, CE);
Steve Naroff934f2762007-10-24 22:48:43 +0000912
Chris Lattnere64b7772007-10-24 16:57:36 +0000913 delete Exp;
Steve Naroffab972d32007-11-04 22:37:50 +0000914 return CE;
Steve Naroffebf2b562007-10-23 23:50:29 +0000915}
916
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000917/// SynthesizeObjcInternalStruct - Rewrite one internal struct corresponding to
918/// an objective-c class with ivars.
919void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl,
920 std::string &Result) {
921 assert(CDecl && "Class missing in SynthesizeObjcInternalStruct");
922 assert(CDecl->getName() && "Name missing in SynthesizeObjcInternalStruct");
Fariborz Jahanian212b7682007-10-31 23:08:24 +0000923 // Do not synthesize more than once.
924 if (ObjcSynthesizedStructs.count(CDecl))
925 return;
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000926 ObjcInterfaceDecl *RCDecl = CDecl->getSuperClass();
927 if (RCDecl && !ObjcSynthesizedStructs.count(RCDecl)) {
928 // Do it for the root
929 SynthesizeObjcInternalStruct(RCDecl, Result);
930 }
931
932 int NumIvars = CDecl->getIntfDeclNumIvars();
Fariborz Jahanianaff56d02007-10-31 22:57:04 +0000933 // If no ivars and no root or if its root, directly or indirectly,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000934 // have no ivars (thus not synthesized) then no need to synthesize this class.
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000935 if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl)))
936 return;
937
Steve Naroff04960052007-11-01 17:12:31 +0000938 Result += "\nstruct ";
939 Result += CDecl->getName();
940 if (RCDecl && ObjcSynthesizedStructs.count(RCDecl)) {
941 Result += " {\n struct ";
942 Result += RCDecl->getName();
943 Result += " _";
944 Result += RCDecl->getName();
945 Result += ";\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000946 }
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000947 else
948 Result += " {";
Steve Naroff8749be52007-10-31 22:11:35 +0000949
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000950 if (NumIvars > 0) {
951 SourceLocation LocStart = CDecl->getLocStart();
952 SourceLocation LocEnd = CDecl->getLocEnd();
953
954 const char *startBuf = SM->getCharacterData(LocStart);
955 const char *endBuf = SM->getCharacterData(LocEnd);
956 startBuf = strchr(startBuf, '{');
957 assert((startBuf && endBuf)
958 && "SynthesizeObjcInternalStruct - malformed @interface");
959 startBuf++; // past '{'
960 while (startBuf < endBuf) {
961 if (*startBuf == '@') {
962 startBuf = strchr(startBuf, 'p');
963 // FIXME: presence of @public, etc. inside comment results in
964 // this transformation as well, which is still correct c-code.
965 if (!strncmp(startBuf, "public", strlen("public"))) {
966 startBuf += strlen("public");
967 Result += "/* @public */";
968 }
969 else if (!strncmp(startBuf, "private", strlen("private"))) {
970 startBuf += strlen("private");
971 Result += "/* @private */";
972 }
973 else if (!strncmp(startBuf, "protected", strlen("protected"))) {
974 startBuf += strlen("protected");
975 Result += "/* @protected */";
976 }
977 }
978 Result += *startBuf++;
979 }
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000980 }
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +0000981
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000982 Result += "};\n";
983 // Mark this struct as having been generated.
984 if (!ObjcSynthesizedStructs.insert(CDecl))
Fariborz Jahanianaff56d02007-10-31 22:57:04 +0000985 assert(false && "struct already synthesize- SynthesizeObjcInternalStruct");
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000986}
987
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000988// RewriteObjcMethodsMetaData - Rewrite methods metadata for instance or
989/// class methods.
990void RewriteTest::RewriteObjcMethodsMetaData(ObjcMethodDecl **Methods,
991 int NumMethods,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000992 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000993 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000994 const char *ClassName,
995 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000996 static bool objc_impl_method = false;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000997 if (NumMethods > 0 && !objc_impl_method) {
998 /* struct _objc_method {
Fariborz Jahaniane887c092007-10-22 21:41:37 +0000999 SEL _cmd;
1000 char *method_types;
1001 void *_imp;
1002 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001003 */
Chris Lattner158ecb92007-10-25 17:07:24 +00001004 Result += "\nstruct _objc_method {\n";
1005 Result += "\tSEL _cmd;\n";
1006 Result += "\tchar *method_types;\n";
1007 Result += "\tvoid *_imp;\n";
1008 Result += "};\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001009
1010 /* struct _objc_method_list {
1011 struct _objc_method_list *next_method;
1012 int method_count;
1013 struct _objc_method method_list[];
1014 }
1015 */
1016 Result += "\nstruct _objc_method_list {\n";
1017 Result += "\tstruct _objc_method_list *next_method;\n";
1018 Result += "\tint method_count;\n";
1019 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001020 objc_impl_method = true;
Fariborz Jahanian776d6ff2007-10-19 00:36:46 +00001021 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001022 // Build _objc_method_list for class's methods if needed
1023 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001024 Result += "\nstatic struct _objc_method_list _OBJC_";
Chris Lattner158ecb92007-10-25 17:07:24 +00001025 Result += prefix;
1026 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
1027 Result += "_METHODS_";
1028 Result += ClassName;
1029 Result += " __attribute__ ((section (\"__OBJC, __";
1030 Result += IsInstanceMethod ? "inst" : "cls";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001031 Result += "_meth\")))= ";
1032 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
1033
1034 Result += "\t,{{(SEL)\"";
1035 Result += Methods[0]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001036 std::string MethodTypeString;
1037 Context->getObjcEncodingForMethodDecl(Methods[0], MethodTypeString);
1038 Result += "\", \"";
1039 Result += MethodTypeString;
1040 Result += "\", 0}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001041 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001042 // TODO: Need method address as 3rd initializer.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001043 Result += "\t ,{(SEL)\"";
1044 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001045 std::string MethodTypeString;
1046 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
1047 Result += "\", \"";
1048 Result += MethodTypeString;
1049 Result += "\", 0}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001050 }
1051 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001052 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001053}
1054
1055/// RewriteObjcProtocolsMetaData - Rewrite protocols meta-data.
1056void RewriteTest::RewriteObjcProtocolsMetaData(ObjcProtocolDecl **Protocols,
1057 int NumProtocols,
1058 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001059 const char *ClassName,
1060 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001061 static bool objc_protocol_methods = false;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001062 if (NumProtocols > 0) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001063 for (int i = 0; i < NumProtocols; i++) {
1064 ObjcProtocolDecl *PDecl = Protocols[i];
1065 // Output struct protocol_methods holder of method selector and type.
1066 if (!objc_protocol_methods &&
1067 (PDecl->getNumInstanceMethods() > 0
1068 || PDecl->getNumClassMethods() > 0)) {
1069 /* struct protocol_methods {
1070 SEL _cmd;
1071 char *method_types;
1072 }
1073 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001074 Result += "\nstruct protocol_methods {\n";
1075 Result += "\tSEL _cmd;\n";
1076 Result += "\tchar *method_types;\n";
1077 Result += "};\n";
1078
1079 /* struct _objc_protocol_method_list {
1080 int protocol_method_count;
1081 struct protocol_methods protocols[];
1082 }
1083 */
1084 Result += "\nstruct _objc_protocol_method_list {\n";
1085 Result += "\tint protocol_method_count;\n";
1086 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001087 objc_protocol_methods = true;
1088 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001089
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001090 // Output instance methods declared in this protocol.
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001091 int NumMethods = PDecl->getNumInstanceMethods();
1092 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001093 Result += "\nstatic struct _objc_protocol_method_list "
1094 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
1095 Result += PDecl->getName();
1096 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
1097 "{\n\t" + utostr(NumMethods) + "\n";
1098
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001099 ObjcMethodDecl **Methods = PDecl->getInstanceMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001100 Result += "\t,{{(SEL)\"";
1101 Result += Methods[0]->getSelector().getName().c_str();
1102 Result += "\", \"\"}\n";
1103
1104 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001105 Result += "\t ,{(SEL)\"";
1106 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001107 std::string MethodTypeString;
1108 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
1109 Result += "\", \"";
1110 Result += MethodTypeString;
1111 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001112 }
1113 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001114 }
1115
1116 // Output class methods declared in this protocol.
1117 NumMethods = PDecl->getNumClassMethods();
1118 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001119 Result += "\nstatic struct _objc_protocol_method_list "
1120 "_OBJC_PROTOCOL_CLASS_METHODS_";
1121 Result += PDecl->getName();
1122 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
1123 "{\n\t";
1124 Result += utostr(NumMethods);
1125 Result += "\n";
1126
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001127 ObjcMethodDecl **Methods = PDecl->getClassMethods();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001128 Result += "\t,{{(SEL)\"";
1129 Result += Methods[0]->getSelector().getName().c_str();
1130 Result += "\", \"\"}\n";
1131
1132 for (int i = 1; i < NumMethods; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001133 Result += "\t ,{(SEL)\"";
1134 Result += Methods[i]->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001135 std::string MethodTypeString;
1136 Context->getObjcEncodingForMethodDecl(Methods[i], MethodTypeString);
1137 Result += "\", \"";
1138 Result += MethodTypeString;
1139 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001140 }
1141 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001142 }
1143 // Output:
1144 /* struct _objc_protocol {
1145 // Objective-C 1.0 extensions
1146 struct _objc_protocol_extension *isa;
1147 char *protocol_name;
1148 struct _objc_protocol **protocol_list;
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001149 struct _objc_protocol_method_list *instance_methods;
1150 struct _objc_protocol_method_list *class_methods;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001151 };
1152 */
1153 static bool objc_protocol = false;
1154 if (!objc_protocol) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001155 Result += "\nstruct _objc_protocol {\n";
1156 Result += "\tstruct _objc_protocol_extension *isa;\n";
1157 Result += "\tchar *protocol_name;\n";
1158 Result += "\tstruct _objc_protocol **protocol_list;\n";
1159 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
1160 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
1161 Result += "};\n";
1162
1163 /* struct _objc_protocol_list {
1164 struct _objc_protocol_list *next;
1165 int protocol_count;
1166 struct _objc_protocol *class_protocols[];
1167 }
1168 */
1169 Result += "\nstruct _objc_protocol_list {\n";
1170 Result += "\tstruct _objc_protocol_list *next;\n";
1171 Result += "\tint protocol_count;\n";
1172 Result += "\tstruct _objc_protocol *class_protocols[];\n";
1173 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001174 objc_protocol = true;
1175 }
1176
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001177 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
1178 Result += PDecl->getName();
1179 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
1180 "{\n\t0, \"";
1181 Result += PDecl->getName();
1182 Result += "\", 0, ";
1183 if (PDecl->getInstanceMethods() > 0) {
1184 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
1185 Result += PDecl->getName();
1186 Result += ", ";
1187 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001188 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001189 Result += "0, ";
1190 if (PDecl->getClassMethods() > 0) {
1191 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
1192 Result += PDecl->getName();
1193 Result += "\n";
1194 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001195 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001196 Result += "0\n";
1197 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001198 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001199 // Output the top lovel protocol meta-data for the class.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001200 Result += "\nstatic struct _objc_protocol_list _OBJC_";
1201 Result += prefix;
1202 Result += "_PROTOCOLS_";
1203 Result += ClassName;
1204 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
1205 "{\n\t0, ";
1206 Result += utostr(NumProtocols);
1207 Result += "\n";
1208
1209 Result += "\t,{&_OBJC_PROTOCOL_";
1210 Result += Protocols[0]->getName();
1211 Result += " \n";
1212
1213 for (int i = 1; i < NumProtocols; i++) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001214 ObjcProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001215 Result += "\t ,&_OBJC_PROTOCOL_";
1216 Result += PDecl->getName();
1217 Result += "\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001218 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001219 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001220 }
1221}
1222
1223/// RewriteObjcCategoryImplDecl - Rewrite metadata for each category
1224/// implementation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001225void RewriteTest::RewriteObjcCategoryImplDecl(ObjcCategoryImplDecl *IDecl,
1226 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001227 ObjcInterfaceDecl *ClassDecl = IDecl->getClassInterface();
1228 // Find category declaration for this implementation.
1229 ObjcCategoryDecl *CDecl;
1230 for (CDecl = ClassDecl->getCategoryList(); CDecl;
1231 CDecl = CDecl->getNextClassCategory())
1232 if (CDecl->getIdentifier() == IDecl->getIdentifier())
1233 break;
1234 assert(CDecl && "RewriteObjcCategoryImplDecl - bad category");
1235
1236 char *FullCategoryName = (char*)alloca(
1237 strlen(ClassDecl->getName()) + strlen(IDecl->getName()) + 2);
1238 sprintf(FullCategoryName, "%s_%s", ClassDecl->getName(), IDecl->getName());
1239
1240 // Build _objc_method_list for class's instance methods if needed
1241 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1242 IDecl->getNumInstanceMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +00001243 true,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001244 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001245
1246 // Build _objc_method_list for class's class methods if needed
1247 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
1248 IDecl->getNumClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +00001249 false,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001250 "CATEGORY_", FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001251
1252 // Protocols referenced in class declaration?
1253 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1254 CDecl->getNumReferencedProtocols(),
1255 "CATEGORY",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001256 FullCategoryName, Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001257
1258 /* struct _objc_category {
1259 char *category_name;
1260 char *class_name;
1261 struct _objc_method_list *instance_methods;
1262 struct _objc_method_list *class_methods;
1263 struct _objc_protocol_list *protocols;
1264 // Objective-C 1.0 extensions
1265 uint32_t size; // sizeof (struct _objc_category)
1266 struct _objc_property_list *instance_properties; // category's own
1267 // @property decl.
1268 };
1269 */
1270
1271 static bool objc_category = false;
1272 if (!objc_category) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001273 Result += "\nstruct _objc_category {\n";
1274 Result += "\tchar *category_name;\n";
1275 Result += "\tchar *class_name;\n";
1276 Result += "\tstruct _objc_method_list *instance_methods;\n";
1277 Result += "\tstruct _objc_method_list *class_methods;\n";
1278 Result += "\tstruct _objc_protocol_list *protocols;\n";
1279 Result += "\tunsigned int size;\n";
1280 Result += "\tstruct _objc_property_list *instance_properties;\n";
1281 Result += "};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001282 objc_category = true;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00001283 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001284 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
1285 Result += FullCategoryName;
1286 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
1287 Result += IDecl->getName();
1288 Result += "\"\n\t, \"";
1289 Result += ClassDecl->getName();
1290 Result += "\"\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001291
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001292 if (IDecl->getNumInstanceMethods() > 0) {
1293 Result += "\t, (struct _objc_method_list *)"
1294 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
1295 Result += FullCategoryName;
1296 Result += "\n";
1297 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001298 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001299 Result += "\t, 0\n";
1300 if (IDecl->getNumClassMethods() > 0) {
1301 Result += "\t, (struct _objc_method_list *)"
1302 "&_OBJC_CATEGORY_CLASS_METHODS_";
1303 Result += FullCategoryName;
1304 Result += "\n";
1305 }
1306 else
1307 Result += "\t, 0\n";
1308
1309 if (CDecl->getNumReferencedProtocols() > 0) {
1310 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
1311 Result += FullCategoryName;
1312 Result += "\n";
1313 }
1314 else
1315 Result += "\t, 0\n";
1316 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001317}
1318
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001319/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
1320/// ivar offset.
1321void RewriteTest::SynthesizeIvarOffsetComputation(ObjcImplementationDecl *IDecl,
1322 ObjcIvarDecl *ivar,
1323 std::string &Result) {
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001324 Result += "offsetof(struct ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001325 Result += IDecl->getName();
1326 Result += ", ";
1327 Result += ivar->getName();
1328 Result += ")";
1329}
1330
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001331//===----------------------------------------------------------------------===//
1332// Meta Data Emission
1333//===----------------------------------------------------------------------===//
1334
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001335void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl,
1336 std::string &Result) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001337 ObjcInterfaceDecl *CDecl = IDecl->getClassInterface();
1338
1339 // Build _objc_ivar_list metadata for classes ivars if needed
1340 int NumIvars = IDecl->getImplDeclNumIvars() > 0
1341 ? IDecl->getImplDeclNumIvars()
1342 : (CDecl ? CDecl->getIntfDeclNumIvars() : 0);
1343
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00001344 SynthesizeObjcInternalStruct(CDecl, Result);
1345
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001346 if (NumIvars > 0) {
1347 static bool objc_ivar = false;
1348 if (!objc_ivar) {
1349 /* struct _objc_ivar {
1350 char *ivar_name;
1351 char *ivar_type;
1352 int ivar_offset;
1353 };
1354 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001355 Result += "\nstruct _objc_ivar {\n";
1356 Result += "\tchar *ivar_name;\n";
1357 Result += "\tchar *ivar_type;\n";
1358 Result += "\tint ivar_offset;\n";
1359 Result += "};\n";
1360
1361 /* struct _objc_ivar_list {
1362 int ivar_count;
1363 struct _objc_ivar ivar_list[];
1364 };
1365 */
1366 Result += "\nstruct _objc_ivar_list {\n";
1367 Result += "\tint ivar_count;\n";
1368 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001369 objc_ivar = true;
1370 }
1371
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001372 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
1373 Result += IDecl->getName();
1374 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
1375 "{\n\t";
1376 Result += utostr(NumIvars);
1377 Result += "\n";
1378
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001379 ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars()
1380 ? IDecl->getImplDeclIVars()
1381 : CDecl->getIntfDeclIvars();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001382 Result += "\t,{{\"";
1383 Result += Ivars[0]->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00001384 Result += "\", \"";
1385 std::string StrEncoding;
1386 Context->getObjcEncodingForType(Ivars[0]->getType(), StrEncoding);
1387 Result += StrEncoding;
1388 Result += "\", ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001389 SynthesizeIvarOffsetComputation(IDecl, Ivars[0], Result);
1390 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001391 for (int i = 1; i < NumIvars; i++) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001392 Result += "\t ,{\"";
1393 Result += Ivars[i]->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00001394 Result += "\", \"";
1395 std::string StrEncoding;
1396 Context->getObjcEncodingForType(Ivars[i]->getType(), StrEncoding);
1397 Result += StrEncoding;
1398 Result += "\", ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001399 SynthesizeIvarOffsetComputation(IDecl, Ivars[i], Result);
1400 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001401 }
1402
1403 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001404 }
1405
1406 // Build _objc_method_list for class's instance methods if needed
1407 RewriteObjcMethodsMetaData(IDecl->getInstanceMethods(),
1408 IDecl->getNumInstanceMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001409 true,
1410 "", IDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001411
1412 // Build _objc_method_list for class's class methods if needed
1413 RewriteObjcMethodsMetaData(IDecl->getClassMethods(),
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +00001414 IDecl->getNumClassMethods(),
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001415 false,
1416 "", IDecl->getName(), Result);
1417
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001418 // Protocols referenced in class declaration?
1419 RewriteObjcProtocolsMetaData(CDecl->getReferencedProtocols(),
1420 CDecl->getNumIntfRefProtocols(),
1421 "CLASS",
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001422 CDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001423
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001424
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001425 // Declaration of class/meta-class metadata
1426 /* struct _objc_class {
1427 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001428 const char *super_class_name;
1429 char *name;
1430 long version;
1431 long info;
1432 long instance_size;
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001433 struct _objc_ivar_list *ivars;
1434 struct _objc_method_list *methods;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001435 struct objc_cache *cache;
1436 struct objc_protocol_list *protocols;
1437 const char *ivar_layout;
1438 struct _objc_class_ext *ext;
1439 };
1440 */
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001441 static bool objc_class = false;
1442 if (!objc_class) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001443 Result += "\nstruct _objc_class {\n";
1444 Result += "\tstruct _objc_class *isa;\n";
1445 Result += "\tconst char *super_class_name;\n";
1446 Result += "\tchar *name;\n";
1447 Result += "\tlong version;\n";
1448 Result += "\tlong info;\n";
1449 Result += "\tlong instance_size;\n";
1450 Result += "\tstruct _objc_ivar_list *ivars;\n";
1451 Result += "\tstruct _objc_method_list *methods;\n";
1452 Result += "\tstruct objc_cache *cache;\n";
1453 Result += "\tstruct _objc_protocol_list *protocols;\n";
1454 Result += "\tconst char *ivar_layout;\n";
1455 Result += "\tstruct _objc_class_ext *ext;\n";
1456 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001457 objc_class = true;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001458 }
1459
1460 // Meta-class metadata generation.
1461 ObjcInterfaceDecl *RootClass = 0;
1462 ObjcInterfaceDecl *SuperClass = CDecl->getSuperClass();
1463 while (SuperClass) {
1464 RootClass = SuperClass;
1465 SuperClass = SuperClass->getSuperClass();
1466 }
1467 SuperClass = CDecl->getSuperClass();
1468
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001469 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
1470 Result += CDecl->getName();
1471 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
1472 "{\n\t(struct _objc_class *)\"";
1473 Result += (RootClass ? RootClass->getName() : CDecl->getName());
1474 Result += "\"";
1475
1476 if (SuperClass) {
1477 Result += ", \"";
1478 Result += SuperClass->getName();
1479 Result += "\", \"";
1480 Result += CDecl->getName();
1481 Result += "\"";
1482 }
1483 else {
1484 Result += ", 0, \"";
1485 Result += CDecl->getName();
1486 Result += "\"";
1487 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001488 // TODO: 'ivars' field for root class is currently set to 0.
1489 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001490 Result += ", 0,2, sizeof(struct _objc_class), 0";
1491 if (CDecl->getNumClassMethods() > 0) {
1492 Result += "\n\t, &_OBJC_CLASS_METHODS_";
1493 Result += CDecl->getName();
1494 Result += "\n";
1495 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001496 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001497 Result += ", 0\n";
1498 if (CDecl->getNumIntfRefProtocols() > 0) {
1499 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
1500 Result += CDecl->getName();
1501 Result += ",0,0\n";
1502 }
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001503 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001504 Result += "\t,0,0,0,0\n";
1505 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001506
1507 // class metadata generation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001508 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
1509 Result += CDecl->getName();
1510 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
1511 "{\n\t&_OBJC_METACLASS_";
1512 Result += CDecl->getName();
1513 if (SuperClass) {
1514 Result += ", \"";
1515 Result += SuperClass->getName();
1516 Result += "\", \"";
1517 Result += CDecl->getName();
1518 Result += "\"";
1519 }
1520 else {
1521 Result += ", 0, \"";
1522 Result += CDecl->getName();
1523 Result += "\"";
1524 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001525 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00001526 Result += ", 0,1";
1527 if (!ObjcSynthesizedStructs.count(CDecl))
1528 Result += ",0";
1529 else {
1530 // class has size. Must synthesize its size.
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001531 Result += ",sizeof(struct ";
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00001532 Result += CDecl->getName();
1533 Result += ")";
1534 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001535 if (NumIvars > 0) {
1536 Result += ", &_OBJC_INSTANCE_VARIABLES_";
1537 Result += CDecl->getName();
1538 Result += "\n\t";
1539 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001540 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001541 Result += ",0";
1542 if (IDecl->getNumInstanceMethods() > 0) {
1543 Result += ", &_OBJC_INSTANCE_METHODS_";
1544 Result += CDecl->getName();
1545 Result += ", 0\n\t";
1546 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001547 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001548 Result += ",0,0";
1549 if (CDecl->getNumIntfRefProtocols() > 0) {
1550 Result += ", &_OBJC_CLASS_PROTOCOLS_";
1551 Result += CDecl->getName();
1552 Result += ", 0,0\n";
1553 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00001554 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001555 Result += ",0,0,0\n";
1556 Result += "};\n";
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00001557}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001558
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001559void RewriteTest::WriteObjcMetaData(std::string &Result) {
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001560 int ClsDefCount = ClassImplementation.size();
1561 int CatDefCount = CategoryImplementation.size();
1562 if (ClsDefCount == 0 && CatDefCount == 0)
1563 return;
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001564
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001565 // TODO: This is temporary until we decide how to access objc types in a
1566 // c program
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001567 Result += "#include <Objc/objc.h>\n";
1568 // This is needed for use of offsetof
1569 Result += "#include <stddef.h>\n";
Fariborz Jahanian454cb012007-10-24 20:54:23 +00001570
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001571 // For each implemented class, write out all its meta data.
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001572 for (int i = 0; i < ClsDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001573 RewriteObjcClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00001574
1575 // For each implemented category, write out all its meta data.
1576 for (int i = 0; i < CatDefCount; i++)
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001577 RewriteObjcCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00001578
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001579 // Write objc_symtab metadata
1580 /*
1581 struct _objc_symtab
1582 {
1583 long sel_ref_cnt;
1584 SEL *refs;
1585 short cls_def_cnt;
1586 short cat_def_cnt;
1587 void *defs[cls_def_cnt + cat_def_cnt];
1588 };
1589 */
1590
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001591 Result += "\nstruct _objc_symtab {\n";
1592 Result += "\tlong sel_ref_cnt;\n";
1593 Result += "\tSEL *refs;\n";
1594 Result += "\tshort cls_def_cnt;\n";
1595 Result += "\tshort cat_def_cnt;\n";
1596 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
1597 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001598
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001599 Result += "static struct _objc_symtab "
1600 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
1601 Result += "\t0, 0, " + utostr(ClsDefCount)
1602 + ", " + utostr(CatDefCount) + "\n";
1603 for (int i = 0; i < ClsDefCount; i++) {
1604 Result += "\t,&_OBJC_CLASS_";
1605 Result += ClassImplementation[i]->getName();
1606 Result += "\n";
1607 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001608
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001609 for (int i = 0; i < CatDefCount; i++) {
1610 Result += "\t,&_OBJC_CATEGORY_";
1611 Result += CategoryImplementation[i]->getClassInterface()->getName();
1612 Result += "_";
1613 Result += CategoryImplementation[i]->getName();
1614 Result += "\n";
1615 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001616
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001617 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001618
1619 // Write objc_module metadata
1620
1621 /*
1622 struct _objc_module {
1623 long version;
1624 long size;
1625 const char *name;
1626 struct _objc_symtab *symtab;
1627 }
1628 */
1629
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001630 Result += "\nstruct _objc_module {\n";
1631 Result += "\tlong version;\n";
1632 Result += "\tlong size;\n";
1633 Result += "\tconst char *name;\n";
1634 Result += "\tstruct _objc_symtab *symtab;\n";
1635 Result += "};\n\n";
1636 Result += "static struct _objc_module "
1637 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00001638 Result += "\t" + utostr(OBJC_ABI_VERSION) +
1639 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00001640 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00001641}
Chris Lattner311ff022007-10-16 22:36:42 +00001642