blob: d5df9393e98f66772b9949386ba7813ef5caab29 [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//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner77cd2a02007-10-11 00:43:27 +00007//
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 Lattner07506182007-11-30 22:53:43 +000020#include "clang/Basic/Diagnostic.h"
Chris Lattner26de4652007-12-02 01:13:47 +000021#include "clang/Lex/Lexer.h"
Chris Lattner158ecb92007-10-25 17:07:24 +000022#include "llvm/ADT/StringExtras.h"
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000023#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner26de4652007-12-02 01:13:47 +000024#include "llvm/Support/MemoryBuffer.h"
Steve Naroff0113c9d2008-01-28 21:34:52 +000025#include "llvm/Support/CommandLine.h"
Steve Naroff874e2322007-11-15 10:28:18 +000026#include <sstream>
Chris Lattner77cd2a02007-10-11 00:43:27 +000027using namespace clang;
Chris Lattner158ecb92007-10-25 17:07:24 +000028using llvm::utostr;
Chris Lattner77cd2a02007-10-11 00:43:27 +000029
Steve Naroff0113c9d2008-01-28 21:34:52 +000030static llvm::cl::opt<bool>
31SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false),
32 llvm::cl::desc("Silence ObjC rewriting warnings"));
33
Chris Lattner77cd2a02007-10-11 00:43:27 +000034namespace {
Chris Lattner8a12c272007-10-11 18:38:32 +000035 class RewriteTest : public ASTConsumer {
Chris Lattner2c64b7b2007-10-16 21:07:07 +000036 Rewriter Rewrite;
Chris Lattnere365c502007-11-30 22:25:36 +000037 Diagnostic &Diags;
Steve Narofff69cc5d2008-01-30 19:17:43 +000038 unsigned RewriteFailedDiag;
39
Chris Lattner01c57482007-10-17 22:35:30 +000040 ASTContext *Context;
Chris Lattner77cd2a02007-10-11 00:43:27 +000041 SourceManager *SM;
Chris Lattner8a12c272007-10-11 18:38:32 +000042 unsigned MainFileID;
Chris Lattner26de4652007-12-02 01:13:47 +000043 const char *MainFileStart, *MainFileEnd;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000044 SourceLocation LastIncLoc;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000045 llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation;
46 llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation;
47 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs;
48 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls;
49 llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +000050 llvm::SmallVector<Stmt *, 32> Stmts;
51 llvm::SmallVector<int, 8> ObjCBcLabelNo;
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +000052 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
Steve Naroffebf2b562007-10-23 23:50:29 +000053
54 FunctionDecl *MsgSendFunctionDecl;
Steve Naroff874e2322007-11-15 10:28:18 +000055 FunctionDecl *MsgSendSuperFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +000056 FunctionDecl *MsgSendStretFunctionDecl;
57 FunctionDecl *MsgSendSuperStretFunctionDecl;
Fariborz Jahanianacb49772007-12-03 21:26:48 +000058 FunctionDecl *MsgSendFpretFunctionDecl;
Steve Naroffebf2b562007-10-23 23:50:29 +000059 FunctionDecl *GetClassFunctionDecl;
Steve Naroff9bcb5fc2007-12-07 03:50:46 +000060 FunctionDecl *GetMetaClassFunctionDecl;
Steve Naroff934f2762007-10-24 22:48:43 +000061 FunctionDecl *SelGetUidFunctionDecl;
Steve Naroff96984642007-11-08 14:30:50 +000062 FunctionDecl *CFStringFunctionDecl;
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +000063 FunctionDecl *GetProtocolFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +000064
Steve Naroffbeaf2992007-11-03 11:27:19 +000065 // ObjC string constant support.
66 FileVarDecl *ConstantStringClassReference;
67 RecordDecl *NSStringRecord;
Steve Naroffab972d32007-11-04 22:37:50 +000068
Fariborz Jahanianb586cce2008-01-16 00:09:11 +000069 // ObjC foreach break/continue generation support.
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +000070 int BcLabelCount;
71
Steve Naroff874e2322007-11-15 10:28:18 +000072 // Needed for super.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000073 ObjCMethodDecl *CurMethodDecl;
Steve Naroff874e2322007-11-15 10:28:18 +000074 RecordDecl *SuperStructDecl;
75
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +000076 // Needed for header files being rewritten
77 bool IsHeader;
78
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000079 static const int OBJC_ABI_VERSION =7 ;
Chris Lattner77cd2a02007-10-11 00:43:27 +000080 public:
Chris Lattner9e13c2e2008-01-31 19:38:44 +000081 void Initialize(ASTContext &context);
82
Chris Lattner8a12c272007-10-11 18:38:32 +000083
Chris Lattnerf04da132007-10-24 17:06:59 +000084 // Top Level Driver code.
85 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner2c64b7b2007-10-16 21:07:07 +000086 void HandleDeclInMainFile(Decl *D);
Steve Naroff0113c9d2008-01-28 21:34:52 +000087 RewriteTest(bool isHeader, Diagnostic &D) : Diags(D) {
Steve Narofff69cc5d2008-01-30 19:17:43 +000088 IsHeader = isHeader;
Steve Naroff0113c9d2008-01-28 21:34:52 +000089 RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning,
90 "rewriting sub-expression within a macro (may not be correct)");
Steve Narofff69cc5d2008-01-30 19:17:43 +000091 }
Chris Lattnerf04da132007-10-24 17:06:59 +000092 ~RewriteTest();
Chris Lattnerdcbc5b02008-01-31 19:37:57 +000093
94 void ReplaceStmt(Stmt *Old, Stmt *New) {
95 if (!Rewrite.ReplaceStmt(Old, New))
96 return;
97
98 // Replacement failed, report a warning unless disabled.
99 if (SilenceRewriteMacroWarning) return;
100
101 SourceRange Range = Old->getSourceRange();
102 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag,
103 0, 0, &Range, 1);
104 }
105
106
Chris Lattnerf04da132007-10-24 17:06:59 +0000107
108 // Syntactic Rewriting.
Steve Naroffab972d32007-11-04 22:37:50 +0000109 void RewritePrologue(SourceLocation Loc);
Fariborz Jahanian452b8992008-01-19 00:30:35 +0000110 void RewriteInclude();
Chris Lattnerf04da132007-10-24 17:06:59 +0000111 void RewriteTabs();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000112 void RewriteForwardClassDecl(ObjCClassDecl *Dcl);
113 void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000114 void RewriteImplementationDecl(NamedDecl *Dcl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000115 void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr);
116 void RewriteCategoryDecl(ObjCCategoryDecl *Dcl);
117 void RewriteProtocolDecl(ObjCProtocolDecl *Dcl);
118 void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl);
119 void RewriteMethodDeclaration(ObjCMethodDecl *Method);
120 void RewriteProperties(int nProperties, ObjCPropertyDecl **Properties);
Steve Naroff09b266e2007-10-30 23:14:51 +0000121 void RewriteFunctionDecl(FunctionDecl *FD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000122 void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
Steve Naroffd5255f52007-11-01 13:24:47 +0000123 bool needToScanForQualifiers(QualType T);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000124 ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr);
Steve Naroff874e2322007-11-15 10:28:18 +0000125 QualType getSuperStructType();
Chris Lattner311ff022007-10-16 22:36:42 +0000126
Chris Lattnerf04da132007-10-24 17:06:59 +0000127 // Expression Rewriting.
Steve Narofff3473a72007-11-09 15:20:18 +0000128 Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
Chris Lattnere64b7772007-10-24 16:57:36 +0000129 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
Steve Naroff7e3411b2007-11-15 02:58:25 +0000130 Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV);
Steve Naroffb42f8412007-11-05 14:50:49 +0000131 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
Chris Lattnere64b7772007-10-24 16:57:36 +0000132 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000133 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000134 Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000135 Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000136 Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000137 Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S);
138 Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S);
139 Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S);
Chris Lattner338d1e22008-01-31 05:10:40 +0000140 Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
141 SourceLocation OrigEnd);
Steve Naroff934f2762007-10-24 22:48:43 +0000142 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
143 Expr **args, unsigned nargs);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000144 Stmt *SynthMessageExpr(ObjCMessageExpr *Exp);
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000145 Stmt *RewriteBreakStmt(BreakStmt *S);
146 Stmt *RewriteContinueStmt(ContinueStmt *S);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000147 void SynthCountByEnumWithState(std::string &buf);
148
Steve Naroff09b266e2007-10-30 23:14:51 +0000149 void SynthMsgSendFunctionDecl();
Steve Naroff874e2322007-11-15 10:28:18 +0000150 void SynthMsgSendSuperFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +0000151 void SynthMsgSendStretFunctionDecl();
Fariborz Jahanianacb49772007-12-03 21:26:48 +0000152 void SynthMsgSendFpretFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +0000153 void SynthMsgSendSuperStretFunctionDecl();
Steve Naroff09b266e2007-10-30 23:14:51 +0000154 void SynthGetClassFunctionDecl();
Steve Naroff9bcb5fc2007-12-07 03:50:46 +0000155 void SynthGetMetaClassFunctionDecl();
Steve Naroff96984642007-11-08 14:30:50 +0000156 void SynthCFStringFunctionDecl();
Fariborz Jahaniana70711b2007-12-04 21:47:40 +0000157 void SynthSelGetUidFunctionDecl();
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000158 void SynthGetProtocolFunctionDecl();
Steve Naroff96984642007-11-08 14:30:50 +0000159
Chris Lattnerf04da132007-10-24 17:06:59 +0000160 // Metadata emission.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000161 void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000162 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000163
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000164 void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000165 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000166
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000167 typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator;
168 void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000169 instmeth_iterator MethodEnd,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000170 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000171 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000172 const char *ClassName,
173 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000174
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 void RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000176 int NumProtocols,
177 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000178 const char *ClassName,
179 std::string &Result);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000180 void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000181 std::string &Result);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000182 void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
183 ObjCIvarDecl *ivar,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000184 std::string &Result);
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +0000185 void RewriteImplementations(std::string &Result);
Chris Lattner77cd2a02007-10-11 00:43:27 +0000186 };
187}
188
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +0000189static bool IsHeaderFile(const std::string &Filename) {
190 std::string::size_type DotPos = Filename.rfind('.');
191
192 if (DotPos == std::string::npos) {
193 // no file extension
194 return false;
195 }
196
197 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
198 // C header: .h
199 // C++ header: .hh or .H;
200 return Ext == "h" || Ext == "hh" || Ext == "H";
201}
202
203ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile,
204 Diagnostic &Diags) {
205 return new RewriteTest(IsHeaderFile(InFile), Diags);
Chris Lattnere365c502007-11-30 22:25:36 +0000206}
Chris Lattner77cd2a02007-10-11 00:43:27 +0000207
Chris Lattner9e13c2e2008-01-31 19:38:44 +0000208void RewriteTest::Initialize(ASTContext &context) {
209 Context = &context;
210 SM = &Context->getSourceManager();
211 MsgSendFunctionDecl = 0;
212 MsgSendSuperFunctionDecl = 0;
213 MsgSendStretFunctionDecl = 0;
214 MsgSendSuperStretFunctionDecl = 0;
215 MsgSendFpretFunctionDecl = 0;
216 GetClassFunctionDecl = 0;
217 GetMetaClassFunctionDecl = 0;
218 SelGetUidFunctionDecl = 0;
219 CFStringFunctionDecl = 0;
220 GetProtocolFunctionDecl = 0;
221 ConstantStringClassReference = 0;
222 NSStringRecord = 0;
223 CurMethodDecl = 0;
224 SuperStructDecl = 0;
225 BcLabelCount = 0;
226
227 // Get the ID and start/end of the main file.
228 MainFileID = SM->getMainFileID();
229 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
230 MainFileStart = MainBuf->getBufferStart();
231 MainFileEnd = MainBuf->getBufferEnd();
232
233
234 Rewrite.setSourceMgr(Context->getSourceManager());
235 // declaring objc_selector outside the parameter list removes a silly
236 // scope related warning...
237 const char *s = "#pragma once\n"
238 "struct objc_selector; struct objc_class;\n"
239 "#ifndef OBJC_SUPER\n"
240 "struct objc_super { struct objc_object *o; "
241 "struct objc_object *superClass; };\n"
242 "#define OBJC_SUPER\n"
243 "#endif\n"
244 "#ifndef _REWRITER_typedef_Protocol\n"
245 "typedef struct objc_object Protocol;\n"
246 "#define _REWRITER_typedef_Protocol\n"
247 "#endif\n"
248 "extern struct objc_object *objc_msgSend"
249 "(struct objc_object *, struct objc_selector *, ...);\n"
250 "extern struct objc_object *objc_msgSendSuper"
251 "(struct objc_super *, struct objc_selector *, ...);\n"
252 "extern struct objc_object *objc_msgSend_stret"
253 "(struct objc_object *, struct objc_selector *, ...);\n"
254 "extern struct objc_object *objc_msgSendSuper_stret"
255 "(struct objc_super *, struct objc_selector *, ...);\n"
256 "extern struct objc_object *objc_msgSend_fpret"
257 "(struct objc_object *, struct objc_selector *, ...);\n"
258 "extern struct objc_object *objc_getClass"
259 "(const char *);\n"
260 "extern struct objc_object *objc_getMetaClass"
261 "(const char *);\n"
262 "extern void objc_exception_throw(struct objc_object *);\n"
263 "extern void objc_exception_try_enter(void *);\n"
264 "extern void objc_exception_try_exit(void *);\n"
265 "extern struct objc_object *objc_exception_extract(void *);\n"
266 "extern int objc_exception_match"
267 "(struct objc_class *, struct objc_object *, ...);\n"
268 "extern Protocol *objc_getProtocol(const char *);\n"
269 "#include <objc/objc.h>\n"
270 "#ifndef __FASTENUMERATIONSTATE\n"
271 "struct __objcFastEnumerationState {\n\t"
272 "unsigned long state;\n\t"
273 "id *itemsPtr;\n\t"
274 "unsigned long *mutationsPtr;\n\t"
275 "unsigned long extra[5];\n};\n"
276 "#define __FASTENUMERATIONSTATE\n"
277 "#endif\n";
278 if (IsHeader) {
279 // insert the whole string when rewriting a header file
280 Rewrite.InsertText(SourceLocation::getFileLoc(MainFileID, 0),
281 s, strlen(s));
282 }
283 else {
284 // Not rewriting header, exclude the #pragma once pragma
285 const char *p = s + strlen("#pragma once\n");
286 Rewrite.InsertText(SourceLocation::getFileLoc(MainFileID, 0),
287 p, strlen(p));
288 }
289}
290
291
Chris Lattnerf04da132007-10-24 17:06:59 +0000292//===----------------------------------------------------------------------===//
293// Top Level Driver Code
294//===----------------------------------------------------------------------===//
295
Chris Lattner8a12c272007-10-11 18:38:32 +0000296void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000297 // Two cases: either the decl could be in the main file, or it could be in a
298 // #included file. If the former, rewrite it now. If the later, check to see
299 // if we rewrote the #include/#import.
300 SourceLocation Loc = D->getLocation();
301 Loc = SM->getLogicalLoc(Loc);
302
303 // If this is for a builtin, ignore it.
304 if (Loc.isInvalid()) return;
305
Steve Naroffebf2b562007-10-23 23:50:29 +0000306 // Look for built-in declarations that we need to refer during the rewrite.
307 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Steve Naroff09b266e2007-10-30 23:14:51 +0000308 RewriteFunctionDecl(FD);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000309 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
310 // declared in <Foundation/NSString.h>
311 if (strcmp(FVD->getName(), "_NSConstantStringClassReference") == 0) {
312 ConstantStringClassReference = FVD;
313 return;
314 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000315 } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) {
Steve Naroffbef11852007-10-26 20:53:56 +0000316 RewriteInterfaceDecl(MD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000317 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
Steve Naroff423cb562007-10-30 13:30:57 +0000318 RewriteCategoryDecl(CD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000319 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Steve Naroff752d6ef2007-10-30 16:42:30 +0000320 RewriteProtocolDecl(PD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000321 } else if (ObjCForwardProtocolDecl *FP =
322 dyn_cast<ObjCForwardProtocolDecl>(D)){
Fariborz Jahaniand175ddf2007-11-14 00:42:16 +0000323 RewriteForwardProtocolDecl(FP);
Steve Naroffebf2b562007-10-23 23:50:29 +0000324 }
Chris Lattnerf04da132007-10-24 17:06:59 +0000325 // If we have a decl in the main file, see if we should rewrite it.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000326 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
327 return HandleDeclInMainFile(D);
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000328}
329
Chris Lattnerf04da132007-10-24 17:06:59 +0000330/// HandleDeclInMainFile - This is called for each top-level decl defined in the
331/// main file of the input.
332void RewriteTest::HandleDeclInMainFile(Decl *D) {
333 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
334 if (Stmt *Body = FD->getBody())
Steve Narofff3473a72007-11-09 15:20:18 +0000335 FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
Steve Narofff69cc5d2008-01-30 19:17:43 +0000336
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000337 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Steve Naroff874e2322007-11-15 10:28:18 +0000338 if (Stmt *Body = MD->getBody()) {
339 //Body->dump();
340 CurMethodDecl = MD;
Steve Naroff71c0a952007-11-13 23:01:27 +0000341 MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
Steve Naroff874e2322007-11-15 10:28:18 +0000342 CurMethodDecl = 0;
343 }
Steve Naroff71c0a952007-11-13 23:01:27 +0000344 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000345 if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D))
Chris Lattnerf04da132007-10-24 17:06:59 +0000346 ClassImplementation.push_back(CI);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000347 else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D))
Chris Lattnerf04da132007-10-24 17:06:59 +0000348 CategoryImplementation.push_back(CI);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000349 else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D))
Chris Lattnerf04da132007-10-24 17:06:59 +0000350 RewriteForwardClassDecl(CD);
Steve Narofff3473a72007-11-09 15:20:18 +0000351 else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000352 RewriteObjCQualifiedInterfaceTypes(VD);
Steve Narofff3473a72007-11-09 15:20:18 +0000353 if (VD->getInit())
354 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
355 }
Chris Lattnerf04da132007-10-24 17:06:59 +0000356 // Nothing yet.
357}
358
359RewriteTest::~RewriteTest() {
360 // Get the top-level buffer that this corresponds to.
Chris Lattner74a0c772007-11-08 04:27:23 +0000361
362 // Rewrite tabs if we care.
363 //RewriteTabs();
Chris Lattnerf04da132007-10-24 17:06:59 +0000364
Fariborz Jahanian452b8992008-01-19 00:30:35 +0000365 RewriteInclude();
366
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000367 // Rewrite Objective-c meta data*
368 std::string ResultStr;
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +0000369 RewriteImplementations(ResultStr);
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000370
Chris Lattnerf04da132007-10-24 17:06:59 +0000371 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
372 // we are done.
373 if (const RewriteBuffer *RewriteBuf =
374 Rewrite.getRewriteBufferFor(MainFileID)) {
Steve Naroffbeaf2992007-11-03 11:27:19 +0000375 //printf("Changed:\n");
Chris Lattnerf04da132007-10-24 17:06:59 +0000376 std::string S(RewriteBuf->begin(), RewriteBuf->end());
377 printf("%s\n", S.c_str());
378 } else {
379 printf("No changes\n");
380 }
Fariborz Jahanian4402d812007-11-07 18:40:28 +0000381 // Emit metadata.
382 printf("%s", ResultStr.c_str());
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000383}
384
Chris Lattnerf04da132007-10-24 17:06:59 +0000385//===----------------------------------------------------------------------===//
386// Syntactic (non-AST) Rewriting Code
387//===----------------------------------------------------------------------===//
388
Fariborz Jahanian452b8992008-01-19 00:30:35 +0000389void RewriteTest::RewriteInclude() {
390 SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0);
391 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
392 const char *MainBufStart = MainBuf.first;
393 const char *MainBufEnd = MainBuf.second;
394 size_t ImportLen = strlen("import");
395 size_t IncludeLen = strlen("include");
396
Fariborz Jahanianaf57b462008-01-19 01:03:17 +0000397 // Loop over the whole file, looking for includes.
Fariborz Jahanian452b8992008-01-19 00:30:35 +0000398 for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) {
399 if (*BufPtr == '#') {
400 if (++BufPtr == MainBufEnd)
401 return;
402 while (*BufPtr == ' ' || *BufPtr == '\t')
403 if (++BufPtr == MainBufEnd)
404 return;
405 if (!strncmp(BufPtr, "import", ImportLen)) {
406 // replace import with include
407 SourceLocation ImportLoc =
408 LocStart.getFileLocWithOffset(BufPtr-MainBufStart);
409 Rewrite.ReplaceText(ImportLoc, ImportLen, "include", IncludeLen);
410 BufPtr += ImportLen;
411 }
412 }
413 }
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000414}
415
Chris Lattnerf04da132007-10-24 17:06:59 +0000416void RewriteTest::RewriteTabs() {
417 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
418 const char *MainBufStart = MainBuf.first;
419 const char *MainBufEnd = MainBuf.second;
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +0000420
Chris Lattnerf04da132007-10-24 17:06:59 +0000421 // Loop over the whole file, looking for tabs.
422 for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
423 if (*BufPtr != '\t')
424 continue;
425
426 // Okay, we found a tab. This tab will turn into at least one character,
427 // but it depends on which 'virtual column' it is in. Compute that now.
428 unsigned VCol = 0;
429 while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
430 BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
431 ++VCol;
432
433 // Okay, now that we know the virtual column, we know how many spaces to
434 // insert. We assume 8-character tab-stops.
435 unsigned Spaces = 8-(VCol & 7);
436
437 // Get the location of the tab.
438 SourceLocation TabLoc =
439 SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
440
441 // Rewrite the single tab character into a sequence of spaces.
442 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
443 }
Chris Lattner8a12c272007-10-11 18:38:32 +0000444}
445
446
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447void RewriteTest::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) {
Chris Lattnerf04da132007-10-24 17:06:59 +0000448 int numDecls = ClassDecl->getNumForwardDecls();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449 ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
Chris Lattnerf04da132007-10-24 17:06:59 +0000450
451 // Get the start location and compute the semi location.
452 SourceLocation startLoc = ClassDecl->getLocation();
453 const char *startBuf = SM->getCharacterData(startLoc);
454 const char *semiPtr = strchr(startBuf, ';');
455
456 // Translate to typedef's that forward reference structs with the same name
457 // as the class. As a convenience, we include the original declaration
458 // as a comment.
459 std::string typedefString;
460 typedefString += "// ";
Steve Naroff934f2762007-10-24 22:48:43 +0000461 typedefString.append(startBuf, semiPtr-startBuf+1);
462 typedefString += "\n";
463 for (int i = 0; i < numDecls; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000464 ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i];
Steve Naroff32174822007-11-09 12:50:28 +0000465 typedefString += "#ifndef _REWRITER_typedef_";
466 typedefString += ForwardDecl->getName();
467 typedefString += "\n";
468 typedefString += "#define _REWRITER_typedef_";
469 typedefString += ForwardDecl->getName();
470 typedefString += "\n";
Steve Naroff352336b2007-11-05 14:36:37 +0000471 typedefString += "typedef struct objc_object ";
Steve Naroff934f2762007-10-24 22:48:43 +0000472 typedefString += ForwardDecl->getName();
Steve Naroff32174822007-11-09 12:50:28 +0000473 typedefString += ";\n#endif\n";
Steve Naroff934f2762007-10-24 22:48:43 +0000474 }
475
476 // Replace the @class with typedefs corresponding to the classes.
477 Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1,
478 typedefString.c_str(), typedefString.size());
Chris Lattnerf04da132007-10-24 17:06:59 +0000479}
480
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000481void RewriteTest::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000482 SourceLocation LocStart = Method->getLocStart();
483 SourceLocation LocEnd = Method->getLocEnd();
Steve Naroff423cb562007-10-30 13:30:57 +0000484
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000485 if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) {
Steve Narofff69cc5d2008-01-30 19:17:43 +0000486 Rewrite.InsertText(LocStart, "/* ", 3);
487 Rewrite.ReplaceText(LocEnd, 1, ";*/ ", 4);
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000488 } else {
Steve Narofff69cc5d2008-01-30 19:17:43 +0000489 Rewrite.InsertText(LocStart, "// ", 3);
Steve Naroff423cb562007-10-30 13:30:57 +0000490 }
491}
492
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000493void RewriteTest::RewriteProperties(int nProperties, ObjCPropertyDecl **Properties)
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000494{
495 for (int i = 0; i < nProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000496 ObjCPropertyDecl *Property = Properties[i];
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000497 SourceLocation Loc = Property->getLocation();
498
499 Rewrite.ReplaceText(Loc, 0, "// ", 3);
500
501 // FIXME: handle properties that are declared across multiple lines.
502 }
503}
504
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000505void RewriteTest::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
Steve Naroff423cb562007-10-30 13:30:57 +0000506 SourceLocation LocStart = CatDecl->getLocStart();
507
508 // FIXME: handle category headers that are declared across multiple lines.
509 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
510
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000511 for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000512 E = CatDecl->instmeth_end(); I != E; ++I)
513 RewriteMethodDeclaration(*I);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000514 for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000515 E = CatDecl->classmeth_end(); I != E; ++I)
516 RewriteMethodDeclaration(*I);
517
Steve Naroff423cb562007-10-30 13:30:57 +0000518 // Lastly, comment out the @end.
519 Rewrite.ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
520}
521
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522void RewriteTest::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000523 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000524
Steve Naroff752d6ef2007-10-30 16:42:30 +0000525 SourceLocation LocStart = PDecl->getLocStart();
526
527 // FIXME: handle protocol headers that are declared across multiple lines.
528 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
529
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000530 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000531 E = PDecl->instmeth_end(); I != E; ++I)
532 RewriteMethodDeclaration(*I);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000533 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000534 E = PDecl->classmeth_end(); I != E; ++I)
535 RewriteMethodDeclaration(*I);
536
Steve Naroff752d6ef2007-10-30 16:42:30 +0000537 // Lastly, comment out the @end.
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000538 SourceLocation LocEnd = PDecl->getAtEndLoc();
539 Rewrite.ReplaceText(LocEnd, 0, "// ", 3);
Steve Naroff8cc764c2007-11-14 15:03:57 +0000540
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000541 // Must comment out @optional/@required
542 const char *startBuf = SM->getCharacterData(LocStart);
543 const char *endBuf = SM->getCharacterData(LocEnd);
544 for (const char *p = startBuf; p < endBuf; p++) {
545 if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
546 std::string CommentedOptional = "/* @optional */";
Steve Naroff8cc764c2007-11-14 15:03:57 +0000547 SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000548 Rewrite.ReplaceText(OptionalLoc, strlen("@optional"),
549 CommentedOptional.c_str(), CommentedOptional.size());
550
551 }
552 else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
553 std::string CommentedRequired = "/* @required */";
Steve Naroff8cc764c2007-11-14 15:03:57 +0000554 SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000555 Rewrite.ReplaceText(OptionalLoc, strlen("@required"),
556 CommentedRequired.c_str(), CommentedRequired.size());
557
558 }
559 }
Steve Naroff752d6ef2007-10-30 16:42:30 +0000560}
561
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000562void RewriteTest::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) {
Fariborz Jahaniand175ddf2007-11-14 00:42:16 +0000563 SourceLocation LocStart = PDecl->getLocation();
Steve Naroffb7fa9922007-11-14 03:37:28 +0000564 if (LocStart.isInvalid())
565 assert(false && "Invalid SourceLocation");
Fariborz Jahaniand175ddf2007-11-14 00:42:16 +0000566 // FIXME: handle forward protocol that are declared across multiple lines.
567 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
568}
569
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570void RewriteTest::RewriteObjCMethodDecl(ObjCMethodDecl *OMD,
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000571 std::string &ResultStr) {
572 ResultStr += "\nstatic ";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573 if (OMD->getResultType()->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000574 ResultStr += "id";
575 else
576 ResultStr += OMD->getResultType().getAsString();
Fariborz Jahanian531a1ea2008-01-10 01:39:52 +0000577 ResultStr += " ";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000578
579 // Unique method name
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000580 std::string NameStr;
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000581
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000582 if (OMD->isInstance())
583 NameStr += "_I_";
584 else
585 NameStr += "_C_";
586
587 NameStr += OMD->getClassInterface()->getName();
588 NameStr += "_";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000589
590 NamedDecl *MethodContext = OMD->getMethodContext();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591 if (ObjCCategoryImplDecl *CID =
592 dyn_cast<ObjCCategoryImplDecl>(MethodContext)) {
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000593 NameStr += CID->getName();
594 NameStr += "_";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000595 }
596 // Append selector names, replacing ':' with '_'
597 const char *selName = OMD->getSelector().getName().c_str();
598 if (!strchr(selName, ':'))
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000599 NameStr += OMD->getSelector().getName();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000600 else {
601 std::string selString = OMD->getSelector().getName();
602 int len = selString.size();
603 for (int i = 0; i < len; i++)
604 if (selString[i] == ':')
605 selString[i] = '_';
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000606 NameStr += selString;
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000607 }
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000608 // Remember this name for metadata emission
609 MethodInternalNames[OMD] = NameStr;
610 ResultStr += NameStr;
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000611
612 // Rewrite arguments
613 ResultStr += "(";
614
615 // invisible arguments
616 if (OMD->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface());
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000618 selfTy = Context->getPointerType(selfTy);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619 if (ObjCSynthesizedStructs.count(OMD->getClassInterface()))
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000620 ResultStr += "struct ";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000621 ResultStr += selfTy.getAsString();
622 }
623 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 ResultStr += Context->getObjCIdType().getAsString();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000625
626 ResultStr += " self, ";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 ResultStr += Context->getObjCSelType().getAsString();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000628 ResultStr += " _cmd";
629
630 // Method arguments.
631 for (int i = 0; i < OMD->getNumParams(); i++) {
632 ParmVarDecl *PDecl = OMD->getParamDecl(i);
633 ResultStr += ", ";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000634 if (PDecl->getType()->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000635 ResultStr += "id";
636 else
637 ResultStr += PDecl->getType().getAsString();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000638 ResultStr += " ";
639 ResultStr += PDecl->getName();
640 }
Fariborz Jahanian7c39ff72008-01-21 20:14:23 +0000641 if (OMD->isVariadic())
642 ResultStr += ", ...";
Fariborz Jahanian531a1ea2008-01-10 01:39:52 +0000643 ResultStr += ") ";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000644
645}
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000646void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647 ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
648 ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000649
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000650 if (IMD)
651 Rewrite.InsertText(IMD->getLocStart(), "// ", 3);
652 else
653 Rewrite.InsertText(CID->getLocStart(), "// ", 3);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000654
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000655 for (ObjCCategoryImplDecl::instmeth_iterator
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000656 I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
657 E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) {
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000658 std::string ResultStr;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000659 ObjCMethodDecl *OMD = *I;
660 RewriteObjCMethodDecl(OMD, ResultStr);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000661 SourceLocation LocStart = OMD->getLocStart();
662 SourceLocation LocEnd = OMD->getBody()->getLocStart();
663
664 const char *startBuf = SM->getCharacterData(LocStart);
665 const char *endBuf = SM->getCharacterData(LocEnd);
666 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
667 ResultStr.c_str(), ResultStr.size());
668 }
669
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000670 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000671 I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
672 E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) {
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000673 std::string ResultStr;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000674 ObjCMethodDecl *OMD = *I;
675 RewriteObjCMethodDecl(OMD, ResultStr);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000676 SourceLocation LocStart = OMD->getLocStart();
677 SourceLocation LocEnd = OMD->getBody()->getLocStart();
678
679 const char *startBuf = SM->getCharacterData(LocStart);
680 const char *endBuf = SM->getCharacterData(LocEnd);
681 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
682 ResultStr.c_str(), ResultStr.size());
683 }
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000684 if (IMD)
685 Rewrite.InsertText(IMD->getLocEnd(), "// ", 3);
686 else
687 Rewrite.InsertText(CID->getLocEnd(), "// ", 3);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000688}
689
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690void RewriteTest::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
Steve Narofff908a872007-10-30 02:23:23 +0000691 std::string ResultStr;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000692 if (!ObjCForwardDecls.count(ClassDecl)) {
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000693 // we haven't seen a forward decl - generate a typedef.
Steve Naroff5086a8d2007-11-14 23:02:56 +0000694 ResultStr = "#ifndef _REWRITER_typedef_";
Steve Naroff32174822007-11-09 12:50:28 +0000695 ResultStr += ClassDecl->getName();
696 ResultStr += "\n";
697 ResultStr += "#define _REWRITER_typedef_";
698 ResultStr += ClassDecl->getName();
699 ResultStr += "\n";
Fariborz Jahanian87ce5d12007-12-03 22:25:42 +0000700 ResultStr += "typedef struct ";
701 ResultStr += ClassDecl->getName();
702 ResultStr += " ";
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000703 ResultStr += ClassDecl->getName();
Steve Naroff32174822007-11-09 12:50:28 +0000704 ResultStr += ";\n#endif\n";
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000705
706 // Mark this typedef as having been generated.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000707 ObjCForwardDecls.insert(ClassDecl);
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000708 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000709 SynthesizeObjCInternalStruct(ClassDecl, ResultStr);
Steve Narofff908a872007-10-30 02:23:23 +0000710
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000711 RewriteProperties(ClassDecl->getNumPropertyDecl(),
712 ClassDecl->getPropertyDecl());
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000713 for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000714 E = ClassDecl->instmeth_end(); I != E; ++I)
715 RewriteMethodDeclaration(*I);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000716 for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000717 E = ClassDecl->classmeth_end(); I != E; ++I)
718 RewriteMethodDeclaration(*I);
719
Steve Naroff2feac5e2007-10-30 03:43:13 +0000720 // Lastly, comment out the @end.
721 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroffbef11852007-10-26 20:53:56 +0000722}
723
Steve Naroff7e3411b2007-11-15 02:58:25 +0000724Stmt *RewriteTest::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725 ObjCIvarDecl *D = IV->getDecl();
Steve Naroff7e3411b2007-11-15 02:58:25 +0000726 if (IV->isFreeIvar()) {
727 Expr *Replacement = new MemberExpr(IV->getBase(), true, D,
728 IV->getLocation());
Chris Lattnerdcbc5b02008-01-31 19:37:57 +0000729 ReplaceStmt(IV, Replacement);
Steve Naroff7e3411b2007-11-15 02:58:25 +0000730 delete IV;
731 return Replacement;
Steve Naroffc2a689b2007-11-15 11:33:00 +0000732 } else {
Fariborz Jahanian7da8d942008-01-23 20:34:40 +0000733#if 0
734 /// This code is not right. It seems unnecessary. It breaks use of
735 /// ivar reference used as 'receiver' of an expression; as in:
736 /// [newInv->_container addObject:0];
Steve Naroffc2a689b2007-11-15 11:33:00 +0000737 if (CurMethodDecl) {
738 if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000739 ObjCInterfaceType *intT = dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
Steve Naroffc2a689b2007-11-15 11:33:00 +0000740 if (CurMethodDecl->getClassInterface() == intT->getDecl()) {
741 IdentifierInfo *II = intT->getDecl()->getIdentifier();
742 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
743 II, 0);
744 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
745
746 CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation());
747 // Don't forget the parens to enforce the proper binding.
748 ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), castExpr);
Chris Lattnerdcbc5b02008-01-31 19:37:57 +0000749 ReplaceStmt(IV->getBase(), PE);
Steve Naroffc2a689b2007-11-15 11:33:00 +0000750 delete IV->getBase();
751 return PE;
752 }
753 }
754 }
Fariborz Jahanian7da8d942008-01-23 20:34:40 +0000755#endif
Steve Naroff7e3411b2007-11-15 02:58:25 +0000756 return IV;
Steve Naroffc2a689b2007-11-15 11:33:00 +0000757 }
Steve Naroff7e3411b2007-11-15 02:58:25 +0000758}
759
Chris Lattnerf04da132007-10-24 17:06:59 +0000760//===----------------------------------------------------------------------===//
761// Function Body / Expression rewriting
762//===----------------------------------------------------------------------===//
763
Steve Narofff3473a72007-11-09 15:20:18 +0000764Stmt *RewriteTest::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000765 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
766 isa<DoStmt>(S) || isa<ForStmt>(S))
767 Stmts.push_back(S);
768 else if (isa<ObjCForCollectionStmt>(S)) {
769 Stmts.push_back(S);
770 ObjCBcLabelNo.push_back(++BcLabelCount);
771 }
772
Chris Lattner338d1e22008-01-31 05:10:40 +0000773 SourceLocation OrigStmtEnd = S->getLocEnd();
774
775 // Start by rewriting all children.
Chris Lattner311ff022007-10-16 22:36:42 +0000776 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
777 CI != E; ++CI)
Steve Naroff75730982007-11-07 04:08:17 +0000778 if (*CI) {
Steve Narofff3473a72007-11-09 15:20:18 +0000779 Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI);
Steve Naroff75730982007-11-07 04:08:17 +0000780 if (newStmt)
781 *CI = newStmt;
782 }
Steve Naroffebf2b562007-10-23 23:50:29 +0000783
784 // Handle specific things.
785 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
786 return RewriteAtEncode(AtEncode);
Steve Naroff7e3411b2007-11-15 02:58:25 +0000787
788 if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S))
789 return RewriteObjCIvarRefExpr(IvarRefExpr);
Steve Naroffb42f8412007-11-05 14:50:49 +0000790
791 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
792 return RewriteAtSelector(AtSelector);
Steve Narofff69cc5d2008-01-30 19:17:43 +0000793
Steve Naroffbeaf2992007-11-03 11:27:19 +0000794 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
795 return RewriteObjCStringLiteral(AtString);
Steve Naroffebf2b562007-10-23 23:50:29 +0000796
Steve Naroff934f2762007-10-24 22:48:43 +0000797 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
798 // Before we rewrite it, put the original message expression in a comment.
799 SourceLocation startLoc = MessExpr->getLocStart();
800 SourceLocation endLoc = MessExpr->getLocEnd();
801
802 const char *startBuf = SM->getCharacterData(startLoc);
803 const char *endBuf = SM->getCharacterData(endLoc);
804
805 std::string messString;
806 messString += "// ";
807 messString.append(startBuf, endBuf-startBuf+1);
808 messString += "\n";
Steve Naroffbef11852007-10-26 20:53:56 +0000809
Steve Naroff934f2762007-10-24 22:48:43 +0000810 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
811 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
812 // Tried this, but it didn't work either...
Steve Naroff752d6ef2007-10-30 16:42:30 +0000813 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffebf2b562007-10-23 23:50:29 +0000814 return RewriteMessageExpr(MessExpr);
Steve Naroff934f2762007-10-24 22:48:43 +0000815 }
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000816
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817 if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
818 return RewriteObjCTryStmt(StmtTry);
Steve Naroff2bd03922007-11-07 15:32:26 +0000819
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000820 if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S))
821 return RewriteObjCSynchronizedStmt(StmtTry);
822
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000823 if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
824 return RewriteObjCThrowStmt(StmtThrow);
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000825
826 if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
827 return RewriteObjCProtocolExpr(ProtocolExp);
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000828
829 if (ObjCForCollectionStmt *StmtForCollection =
830 dyn_cast<ObjCForCollectionStmt>(S))
Chris Lattner338d1e22008-01-31 05:10:40 +0000831 return RewriteObjCForCollectionStmt(StmtForCollection, OrigStmtEnd);
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000832 if (BreakStmt *StmtBreakStmt =
833 dyn_cast<BreakStmt>(S))
834 return RewriteBreakStmt(StmtBreakStmt);
835 if (ContinueStmt *StmtContinueStmt =
836 dyn_cast<ContinueStmt>(S))
837 return RewriteContinueStmt(StmtContinueStmt);
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000838
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000839 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
840 isa<DoStmt>(S) || isa<ForStmt>(S)) {
841 assert(!Stmts.empty() && "Statement stack is empty");
842 assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
843 isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
844 && "Statement stack mismatch");
845 Stmts.pop_back();
846 }
Steve Naroff874e2322007-11-15 10:28:18 +0000847#if 0
848 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
849 CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation());
850 // Get the new text.
851 std::ostringstream Buf;
852 Replacement->printPretty(Buf);
853 const std::string &Str = Buf.str();
854
855 printf("CAST = %s\n", &Str[0]);
856 Rewrite.InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
857 delete S;
858 return Replacement;
859 }
860#endif
Chris Lattnere64b7772007-10-24 16:57:36 +0000861 // Return this stmt unmodified.
862 return S;
Chris Lattner311ff022007-10-16 22:36:42 +0000863}
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000864
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000865/// SynthCountByEnumWithState - To print:
866/// ((unsigned int (*)
867/// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
868/// (void *)objc_msgSend)((id)l_collection,
869/// sel_registerName(
870/// "countByEnumeratingWithState:objects:count:"),
871/// &enumState,
872/// (id *)items, (unsigned int)16)
873///
874void RewriteTest::SynthCountByEnumWithState(std::string &buf) {
875 buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, "
876 "id *, unsigned int))(void *)objc_msgSend)";
877 buf += "\n\t\t";
878 buf += "((id)l_collection,\n\t\t";
879 buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
880 buf += "\n\t\t";
881 buf += "&enumState, "
882 "(id *)items, (unsigned int)16)";
883}
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000884
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000885/// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
886/// statement to exit to its outer synthesized loop.
887///
888Stmt *RewriteTest::RewriteBreakStmt(BreakStmt *S) {
889 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
890 return S;
891 // replace break with goto __break_label
892 std::string buf;
893
894 SourceLocation startLoc = S->getLocStart();
895 buf = "goto __break_label_";
896 buf += utostr(ObjCBcLabelNo.back());
897 Rewrite.ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size());
898
899 return 0;
900}
901
902/// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
903/// statement to continue with its inner synthesized loop.
904///
905Stmt *RewriteTest::RewriteContinueStmt(ContinueStmt *S) {
906 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
907 return S;
908 // replace continue with goto __continue_label
909 std::string buf;
910
911 SourceLocation startLoc = S->getLocStart();
912 buf = "goto __continue_label_";
913 buf += utostr(ObjCBcLabelNo.back());
914 Rewrite.ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size());
915
916 return 0;
917}
918
Fariborz Jahaniana0f55792008-01-29 22:59:37 +0000919/// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement.
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000920/// It rewrites:
921/// for ( type elem in collection) { stmts; }
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +0000922
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000923/// Into:
924/// {
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000925/// type elem;
926/// struct __objcFastEnumerationState enumState = { 0 };
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000927/// id items[16];
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000928/// id l_collection = (id)collection;
929/// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
930/// objects:items count:16];
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000931/// if (limit) {
932/// unsigned long startMutations = *enumState.mutationsPtr;
933/// do {
934/// unsigned long counter = 0;
935/// do {
936/// if (startMutations != *enumState.mutationsPtr)
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000937/// objc_enumerationMutation(l_collection);
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000938/// elem = (type)enumState.itemsPtr[counter++];
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000939/// stmts;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000940/// __continue_label: ;
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000941/// } while (counter < limit);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000942/// } while (limit = [l_collection countByEnumeratingWithState:&enumState
943/// objects:items count:16]);
944/// elem = nil;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000945/// __break_label: ;
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000946/// }
947/// else
948/// elem = nil;
949/// }
950///
Chris Lattner338d1e22008-01-31 05:10:40 +0000951Stmt *RewriteTest::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
952 SourceLocation OrigEnd) {
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000953 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
954 assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
955 "ObjCForCollectionStmt Statement stack mismatch");
956 assert(!ObjCBcLabelNo.empty() &&
957 "ObjCForCollectionStmt - Label No stack empty");
958
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000959 SourceLocation startLoc = S->getLocStart();
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000960 const char *startBuf = SM->getCharacterData(startLoc);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000961 const char *elementName;
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000962 std::string elementTypeAsString;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000963 std::string buf;
964 buf = "\n{\n\t";
965 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
966 // type elem;
967 QualType ElementType = cast<ValueDecl>(DS->getDecl())->getType();
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000968 elementTypeAsString = ElementType.getAsString();
969 buf += elementTypeAsString;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000970 buf += " ";
971 elementName = DS->getDecl()->getName();
972 buf += elementName;
973 buf += ";\n\t";
974 }
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000975 else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S->getElement())) {
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000976 elementName = DR->getDecl()->getName();
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000977 elementTypeAsString = DR->getDecl()->getType().getAsString();
978 }
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000979 else
980 assert(false && "RewriteObjCForCollectionStmt - bad element kind");
981
982 // struct __objcFastEnumerationState enumState = { 0 };
983 buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
984 // id items[16];
985 buf += "id items[16];\n\t";
986 // id l_collection = (id)
987 buf += "id l_collection = (id)";
Fariborz Jahanian75712282008-01-10 00:24:29 +0000988 // Find start location of 'collection' the hard way!
989 const char *startCollectionBuf = startBuf;
990 startCollectionBuf += 3; // skip 'for'
991 startCollectionBuf = strchr(startCollectionBuf, '(');
992 startCollectionBuf++; // skip '('
993 // find 'in' and skip it.
994 while (*startCollectionBuf != ' ' ||
995 *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
996 (*(startCollectionBuf+3) != ' ' &&
997 *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
998 startCollectionBuf++;
999 startCollectionBuf += 3;
1000
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001001 // Replace: "for (type element in" with string constructed thus far.
1002 Rewrite.ReplaceText(startLoc, startCollectionBuf - startBuf,
1003 buf.c_str(), buf.size());
1004 // Replace ')' in for '(' type elem in collection ')' with ';'
Fariborz Jahanian75712282008-01-10 00:24:29 +00001005 SourceLocation rightParenLoc = S->getRParenLoc();
1006 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
1007 SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001008 buf = ";\n\t";
1009
1010 // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
1011 // objects:items count:16];
1012 // which is synthesized into:
1013 // unsigned int limit =
1014 // ((unsigned int (*)
1015 // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
1016 // (void *)objc_msgSend)((id)l_collection,
1017 // sel_registerName(
1018 // "countByEnumeratingWithState:objects:count:"),
1019 // (struct __objcFastEnumerationState *)&state,
1020 // (id *)items, (unsigned int)16);
1021 buf += "unsigned long limit =\n\t\t";
1022 SynthCountByEnumWithState(buf);
1023 buf += ";\n\t";
1024 /// if (limit) {
1025 /// unsigned long startMutations = *enumState.mutationsPtr;
1026 /// do {
1027 /// unsigned long counter = 0;
1028 /// do {
1029 /// if (startMutations != *enumState.mutationsPtr)
1030 /// objc_enumerationMutation(l_collection);
Fariborz Jahanian88f50f32008-01-09 18:15:42 +00001031 /// elem = (type)enumState.itemsPtr[counter++];
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001032 buf += "if (limit) {\n\t";
1033 buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
1034 buf += "do {\n\t\t";
1035 buf += "unsigned long counter = 0;\n\t\t";
1036 buf += "do {\n\t\t\t";
1037 buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
1038 buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
1039 buf += elementName;
Fariborz Jahanian88f50f32008-01-09 18:15:42 +00001040 buf += " = (";
1041 buf += elementTypeAsString;
1042 buf += ")enumState.itemsPtr[counter++];";
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001043 // Replace ')' in for '(' type elem in collection ')' with all of these.
1044 Rewrite.ReplaceText(lparenLoc, 1, buf.c_str(), buf.size());
1045
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001046 /// __continue_label: ;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001047 /// } while (counter < limit);
1048 /// } while (limit = [l_collection countByEnumeratingWithState:&enumState
1049 /// objects:items count:16]);
1050 /// elem = nil;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001051 /// __break_label: ;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001052 /// }
1053 /// else
1054 /// elem = nil;
1055 /// }
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001056 ///
1057 buf = ";\n\t";
1058 buf += "__continue_label_";
1059 buf += utostr(ObjCBcLabelNo.back());
1060 buf += ": ;";
1061 buf += "\n\t\t";
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001062 buf += "} while (counter < limit);\n\t";
1063 buf += "} while (limit = ";
1064 SynthCountByEnumWithState(buf);
1065 buf += ");\n\t";
1066 buf += elementName;
1067 buf += " = nil;\n\t";
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001068 buf += "__break_label_";
1069 buf += utostr(ObjCBcLabelNo.back());
1070 buf += ": ;\n\t";
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001071 buf += "}\n\t";
1072 buf += "else\n\t\t";
1073 buf += elementName;
1074 buf += " = nil;\n";
1075 buf += "}\n";
1076 // Insert all these *after* the statement body.
Chris Lattner338d1e22008-01-31 05:10:40 +00001077 SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001078 Rewrite.InsertText(endBodyLoc, buf.c_str(), buf.size());
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001079 Stmts.pop_back();
1080 ObjCBcLabelNo.pop_back();
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001081 return 0;
Fariborz Jahanian10d24f02008-01-07 21:40:22 +00001082}
1083
Fariborz Jahaniana0f55792008-01-29 22:59:37 +00001084/// RewriteObjCSynchronizedStmt -
1085/// This routine rewrites @synchronized(expr) stmt;
1086/// into:
1087/// objc_sync_enter(expr);
1088/// @try stmt @finally { objc_sync_exit(expr); }
1089///
1090Stmt *RewriteTest::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1091 // Get the start location and compute the semi location.
1092 SourceLocation startLoc = S->getLocStart();
1093 const char *startBuf = SM->getCharacterData(startLoc);
1094
1095 assert((*startBuf == '@') && "bogus @synchronized location");
1096
1097 std::string buf;
1098 buf = "objc_sync_enter";
1099 Rewrite.ReplaceText(startLoc, 13, buf.c_str(), buf.size());
1100 SourceLocation endLoc = S->getSynchExpr()->getLocEnd();
1101 const char *endBuf = SM->getCharacterData(endLoc);
1102 endBuf++;
1103 const char *rparenBuf = strchr(endBuf, ')');
1104 SourceLocation rparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf);
1105 buf = ");\n";
1106 // declare a new scope with two variables, _stack and _rethrow.
1107 buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n";
1108 buf += "int buf[18/*32-bit i386*/];\n";
1109 buf += "char *pointers[4];} _stack;\n";
1110 buf += "id volatile _rethrow = 0;\n";
1111 buf += "objc_exception_try_enter(&_stack);\n";
1112 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
1113 Rewrite.ReplaceText(rparenLoc, 1, buf.c_str(), buf.size());
1114 startLoc = S->getSynchBody()->getLocEnd();
1115 startBuf = SM->getCharacterData(startLoc);
1116
1117 assert((*startBuf == '}') && "bogus @try block");
1118 SourceLocation lastCurlyLoc = startLoc;
1119 buf = "}\nelse {\n";
1120 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1121 buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
1122 // FIXME: This must be objc_sync_exit(syncExpr);
1123 buf += " objc_sync_exit();\n";
1124 buf += " if (_rethrow) objc_exception_throw(_rethrow);\n";
1125 buf += "}\n";
1126 buf += "}";
1127
1128 Rewrite.ReplaceText(lastCurlyLoc, 1, buf.c_str(), buf.size());
1129 return 0;
1130}
1131
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001132Stmt *RewriteTest::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
Steve Naroff75730982007-11-07 04:08:17 +00001133 // Get the start location and compute the semi location.
1134 SourceLocation startLoc = S->getLocStart();
1135 const char *startBuf = SM->getCharacterData(startLoc);
1136
1137 assert((*startBuf == '@') && "bogus @try location");
1138
1139 std::string buf;
1140 // declare a new scope with two variables, _stack and _rethrow.
1141 buf = "/* @try scope begin */ { struct _objc_exception_data {\n";
1142 buf += "int buf[18/*32-bit i386*/];\n";
1143 buf += "char *pointers[4];} _stack;\n";
1144 buf += "id volatile _rethrow = 0;\n";
1145 buf += "objc_exception_try_enter(&_stack);\n";
Steve Naroff21867b12007-11-07 18:43:40 +00001146 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
Steve Naroff75730982007-11-07 04:08:17 +00001147
1148 Rewrite.ReplaceText(startLoc, 4, buf.c_str(), buf.size());
1149
1150 startLoc = S->getTryBody()->getLocEnd();
1151 startBuf = SM->getCharacterData(startLoc);
1152
1153 assert((*startBuf == '}') && "bogus @try block");
1154
1155 SourceLocation lastCurlyLoc = startLoc;
1156
1157 startLoc = startLoc.getFileLocWithOffset(1);
1158 buf = " /* @catch begin */ else {\n";
1159 buf += " id _caught = objc_exception_extract(&_stack);\n";
1160 buf += " objc_exception_try_enter (&_stack);\n";
Steve Naroff21867b12007-11-07 18:43:40 +00001161 buf += " if (_setjmp(_stack.buf))\n";
Steve Naroff75730982007-11-07 04:08:17 +00001162 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1163 buf += " else { /* @catch continue */";
1164
Chris Lattner28d1fe82007-11-08 04:41:51 +00001165 Rewrite.InsertText(startLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001166
1167 bool sawIdTypedCatch = false;
1168 Stmt *lastCatchBody = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001169 ObjCAtCatchStmt *catchList = S->getCatchStmts();
Steve Naroff75730982007-11-07 04:08:17 +00001170 while (catchList) {
1171 Stmt *catchStmt = catchList->getCatchParamStmt();
1172
1173 if (catchList == S->getCatchStmts())
1174 buf = "if ("; // we are generating code for the first catch clause
1175 else
1176 buf = "else if (";
1177 startLoc = catchList->getLocStart();
1178 startBuf = SM->getCharacterData(startLoc);
1179
1180 assert((*startBuf == '@') && "bogus @catch location");
1181
1182 const char *lParenLoc = strchr(startBuf, '(');
1183
1184 if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) {
1185 QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001186 if (t == Context->getObjCIdType()) {
Steve Naroff75730982007-11-07 04:08:17 +00001187 buf += "1) { ";
1188 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
1189 buf.c_str(), buf.size());
1190 sawIdTypedCatch = true;
1191 } else if (const PointerType *pType = t->getAsPointerType()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001192 ObjCInterfaceType *cls; // Should be a pointer to a class.
Steve Naroff75730982007-11-07 04:08:17 +00001193
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001194 cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr());
Steve Naroff75730982007-11-07 04:08:17 +00001195 if (cls) {
Steve Naroff21867b12007-11-07 18:43:40 +00001196 buf += "objc_exception_match((struct objc_class *)objc_getClass(\"";
Steve Naroff75730982007-11-07 04:08:17 +00001197 buf += cls->getDecl()->getName();
Steve Naroff21867b12007-11-07 18:43:40 +00001198 buf += "\"), (struct objc_object *)_caught)) { ";
Steve Naroff75730982007-11-07 04:08:17 +00001199 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
1200 buf.c_str(), buf.size());
1201 }
1202 }
1203 // Now rewrite the body...
1204 lastCatchBody = catchList->getCatchBody();
1205 SourceLocation rParenLoc = catchList->getRParenLoc();
1206 SourceLocation bodyLoc = lastCatchBody->getLocStart();
1207 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1208 const char *rParenBuf = SM->getCharacterData(rParenLoc);
1209 assert((*rParenBuf == ')') && "bogus @catch paren location");
1210 assert((*bodyBuf == '{') && "bogus @catch body location");
1211
1212 buf = " = _caught;";
1213 // Here we replace ") {" with "= _caught;" (which initializes and
1214 // declares the @catch parameter).
1215 Rewrite.ReplaceText(rParenLoc, bodyBuf-rParenBuf+1,
1216 buf.c_str(), buf.size());
Steve Naroff2bd03922007-11-07 15:32:26 +00001217 } else if (!isa<NullStmt>(catchStmt)) {
Steve Naroff75730982007-11-07 04:08:17 +00001218 assert(false && "@catch rewrite bug");
Steve Naroff2bd03922007-11-07 15:32:26 +00001219 }
Steve Naroff75730982007-11-07 04:08:17 +00001220 catchList = catchList->getNextCatchStmt();
1221 }
1222 // Complete the catch list...
1223 if (lastCatchBody) {
1224 SourceLocation bodyLoc = lastCatchBody->getLocEnd();
1225 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1226 assert((*bodyBuf == '}') && "bogus @catch body location");
1227 bodyLoc = bodyLoc.getFileLocWithOffset(1);
1228 buf = " } } /* @catch end */\n";
1229
Chris Lattner28d1fe82007-11-08 04:41:51 +00001230 Rewrite.InsertText(bodyLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001231
1232 // Set lastCurlyLoc
1233 lastCurlyLoc = lastCatchBody->getLocEnd();
1234 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001235 if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) {
Steve Naroff75730982007-11-07 04:08:17 +00001236 startLoc = finalStmt->getLocStart();
1237 startBuf = SM->getCharacterData(startLoc);
1238 assert((*startBuf == '@') && "bogus @finally start");
1239
1240 buf = "/* @finally */";
1241 Rewrite.ReplaceText(startLoc, 8, buf.c_str(), buf.size());
1242
1243 Stmt *body = finalStmt->getFinallyBody();
1244 SourceLocation startLoc = body->getLocStart();
1245 SourceLocation endLoc = body->getLocEnd();
1246 const char *startBuf = SM->getCharacterData(startLoc);
1247 const char *endBuf = SM->getCharacterData(endLoc);
1248 assert((*startBuf == '{') && "bogus @finally body location");
1249 assert((*endBuf == '}') && "bogus @finally body location");
1250
1251 startLoc = startLoc.getFileLocWithOffset(1);
1252 buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
Chris Lattner28d1fe82007-11-08 04:41:51 +00001253 Rewrite.InsertText(startLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001254 endLoc = endLoc.getFileLocWithOffset(-1);
1255 buf = " if (_rethrow) objc_exception_throw(_rethrow);\n";
Chris Lattner28d1fe82007-11-08 04:41:51 +00001256 Rewrite.InsertText(endLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001257
1258 // Set lastCurlyLoc
1259 lastCurlyLoc = body->getLocEnd();
1260 }
1261 // Now emit the final closing curly brace...
1262 lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1);
1263 buf = " } /* @try scope end */\n";
Chris Lattner28d1fe82007-11-08 04:41:51 +00001264 Rewrite.InsertText(lastCurlyLoc, buf.c_str(), buf.size());
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001265 return 0;
1266}
1267
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001268Stmt *RewriteTest::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) {
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001269 return 0;
1270}
1271
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001272Stmt *RewriteTest::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) {
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001273 return 0;
1274}
1275
Chris Lattnerdcbc5b02008-01-31 19:37:57 +00001276// This can't be done with ReplaceStmt(S, ThrowExpr), since
Steve Naroff2bd03922007-11-07 15:32:26 +00001277// the throw expression is typically a message expression that's already
1278// been rewritten! (which implies the SourceLocation's are invalid).
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001279Stmt *RewriteTest::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
Steve Naroff2bd03922007-11-07 15:32:26 +00001280 // Get the start location and compute the semi location.
1281 SourceLocation startLoc = S->getLocStart();
1282 const char *startBuf = SM->getCharacterData(startLoc);
1283
1284 assert((*startBuf == '@') && "bogus @throw location");
1285
1286 std::string buf;
1287 /* void objc_exception_throw(id) __attribute__((noreturn)); */
Steve Naroff20ebf8f2008-01-19 00:42:38 +00001288 if (S->getThrowExpr())
1289 buf = "objc_exception_throw(";
1290 else // add an implicit argument
1291 buf = "objc_exception_throw(_caught";
Steve Naroff2bd03922007-11-07 15:32:26 +00001292 Rewrite.ReplaceText(startLoc, 6, buf.c_str(), buf.size());
1293 const char *semiBuf = strchr(startBuf, ';');
1294 assert((*semiBuf == ';') && "@throw: can't find ';'");
1295 SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf);
1296 buf = ");";
1297 Rewrite.ReplaceText(semiLoc, 1, buf.c_str(), buf.size());
1298 return 0;
1299}
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001300
Chris Lattnere64b7772007-10-24 16:57:36 +00001301Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattner01c57482007-10-17 22:35:30 +00001302 // Create a new string expression.
1303 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001304 std::string StrEncoding;
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00001305 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding,
1306 EncodingRecordTypes);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001307 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
1308 StrEncoding.length(), false, StrType,
Chris Lattner01c57482007-10-17 22:35:30 +00001309 SourceLocation(), SourceLocation());
Chris Lattnerdcbc5b02008-01-31 19:37:57 +00001310 ReplaceStmt(Exp, Replacement);
Chris Lattnere365c502007-11-30 22:25:36 +00001311
Chris Lattner07506182007-11-30 22:53:43 +00001312 // Replace this subexpr in the parent.
Chris Lattnere64b7772007-10-24 16:57:36 +00001313 delete Exp;
1314 return Replacement;
Chris Lattner311ff022007-10-16 22:36:42 +00001315}
1316
Steve Naroffb42f8412007-11-05 14:50:49 +00001317Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) {
1318 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
1319 // Create a call to sel_registerName("selName").
1320 llvm::SmallVector<Expr*, 8> SelExprs;
1321 QualType argType = Context->getPointerType(Context->CharTy);
1322 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
1323 Exp->getSelector().getName().size(),
1324 false, argType, SourceLocation(),
1325 SourceLocation()));
1326 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
1327 &SelExprs[0], SelExprs.size());
Chris Lattnerdcbc5b02008-01-31 19:37:57 +00001328 ReplaceStmt(Exp, SelExp);
Steve Naroffb42f8412007-11-05 14:50:49 +00001329 delete Exp;
1330 return SelExp;
1331}
1332
Steve Naroff934f2762007-10-24 22:48:43 +00001333CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
1334 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffebf2b562007-10-23 23:50:29 +00001335 // Get the type, we will need to reference it in a couple spots.
Steve Naroff934f2762007-10-24 22:48:43 +00001336 QualType msgSendType = FD->getType();
Steve Naroffebf2b562007-10-23 23:50:29 +00001337
1338 // Create a reference to the objc_msgSend() declaration.
Steve Naroff934f2762007-10-24 22:48:43 +00001339 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffebf2b562007-10-23 23:50:29 +00001340
1341 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerf04da132007-10-24 17:06:59 +00001342 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffebf2b562007-10-23 23:50:29 +00001343 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
1344
1345 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattnere64b7772007-10-24 16:57:36 +00001346
Steve Naroff934f2762007-10-24 22:48:43 +00001347 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
1348}
1349
Steve Naroffd5255f52007-11-01 13:24:47 +00001350static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
1351 const char *&startRef, const char *&endRef) {
1352 while (startBuf < endBuf) {
1353 if (*startBuf == '<')
1354 startRef = startBuf; // mark the start.
1355 if (*startBuf == '>') {
Steve Naroff32174822007-11-09 12:50:28 +00001356 if (startRef && *startRef == '<') {
1357 endRef = startBuf; // mark the end.
1358 return true;
1359 }
1360 return false;
Steve Naroffd5255f52007-11-01 13:24:47 +00001361 }
1362 startBuf++;
1363 }
1364 return false;
1365}
1366
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001367static void scanToNextArgument(const char *&argRef) {
1368 int angle = 0;
1369 while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
1370 if (*argRef == '<')
1371 angle++;
1372 else if (*argRef == '>')
1373 angle--;
1374 argRef++;
1375 }
1376 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
1377}
Fariborz Jahanian291e04b2007-12-11 23:04:08 +00001378
Steve Naroffd5255f52007-11-01 13:24:47 +00001379bool RewriteTest::needToScanForQualifiers(QualType T) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001380
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001381 if (T == Context->getObjCIdType())
Steve Naroffd5255f52007-11-01 13:24:47 +00001382 return true;
1383
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384 if (T->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001385 return true;
1386
Steve Naroffd5255f52007-11-01 13:24:47 +00001387 if (const PointerType *pType = T->getAsPointerType()) {
Steve Naroff9165ad32007-10-31 04:38:33 +00001388 Type *pointeeType = pType->getPointeeType().getTypePtr();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001389 if (isa<ObjCQualifiedInterfaceType>(pointeeType))
Steve Naroff9165ad32007-10-31 04:38:33 +00001390 return true; // we have "Class <Protocol> *".
1391 }
Steve Naroffd5255f52007-11-01 13:24:47 +00001392 return false;
1393}
1394
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001395void RewriteTest::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001396 SourceLocation Loc;
1397 QualType Type;
1398 const FunctionTypeProto *proto = 0;
1399 if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
1400 Loc = VD->getLocation();
1401 Type = VD->getType();
1402 }
1403 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
1404 Loc = FD->getLocation();
1405 // Check for ObjC 'id' and class types that have been adorned with protocol
1406 // information (id<p>, C<p>*). The protocol references need to be rewritten!
1407 const FunctionType *funcType = FD->getType()->getAsFunctionType();
1408 assert(funcType && "missing function type");
1409 proto = dyn_cast<FunctionTypeProto>(funcType);
1410 if (!proto)
1411 return;
1412 Type = proto->getResultType();
1413 }
1414 else
1415 return;
Steve Naroffd5255f52007-11-01 13:24:47 +00001416
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001417 if (needToScanForQualifiers(Type)) {
Steve Naroffd5255f52007-11-01 13:24:47 +00001418 // Since types are unique, we need to scan the buffer.
Steve Naroffd5255f52007-11-01 13:24:47 +00001419
1420 const char *endBuf = SM->getCharacterData(Loc);
1421 const char *startBuf = endBuf;
Chris Lattner26de4652007-12-02 01:13:47 +00001422 while (*startBuf != ';' && startBuf != MainFileStart)
Steve Naroffd5255f52007-11-01 13:24:47 +00001423 startBuf--; // scan backward (from the decl location) for return type.
1424 const char *startRef = 0, *endRef = 0;
1425 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
1426 // Get the locations of the startRef, endRef.
1427 SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf);
1428 SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1);
1429 // Comment out the protocol references.
Chris Lattner28d1fe82007-11-08 04:41:51 +00001430 Rewrite.InsertText(LessLoc, "/*", 2);
1431 Rewrite.InsertText(GreaterLoc, "*/", 2);
Steve Naroff9165ad32007-10-31 04:38:33 +00001432 }
1433 }
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001434 if (!proto)
1435 return; // most likely, was a variable
Steve Naroffd5255f52007-11-01 13:24:47 +00001436 // Now check arguments.
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001437 const char *startBuf = SM->getCharacterData(Loc);
1438 const char *startFuncBuf = startBuf;
Steve Naroffd5255f52007-11-01 13:24:47 +00001439 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
1440 if (needToScanForQualifiers(proto->getArgType(i))) {
1441 // Since types are unique, we need to scan the buffer.
Steve Naroffd5255f52007-11-01 13:24:47 +00001442
Steve Naroffd5255f52007-11-01 13:24:47 +00001443 const char *endBuf = startBuf;
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001444 // scan forward (from the decl location) for argument types.
1445 scanToNextArgument(endBuf);
Steve Naroffd5255f52007-11-01 13:24:47 +00001446 const char *startRef = 0, *endRef = 0;
1447 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
1448 // Get the locations of the startRef, endRef.
Fariborz Jahanian291e04b2007-12-11 23:04:08 +00001449 SourceLocation LessLoc =
1450 Loc.getFileLocWithOffset(startRef-startFuncBuf);
1451 SourceLocation GreaterLoc =
1452 Loc.getFileLocWithOffset(endRef-startFuncBuf+1);
Steve Naroffd5255f52007-11-01 13:24:47 +00001453 // Comment out the protocol references.
Chris Lattner28d1fe82007-11-08 04:41:51 +00001454 Rewrite.InsertText(LessLoc, "/*", 2);
1455 Rewrite.InsertText(GreaterLoc, "*/", 2);
Steve Naroffd5255f52007-11-01 13:24:47 +00001456 }
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001457 startBuf = ++endBuf;
1458 }
1459 else {
1460 while (*startBuf != ')' && *startBuf != ',')
1461 startBuf++; // scan forward (from the decl location) for argument types.
1462 startBuf++;
1463 }
Steve Naroffd5255f52007-11-01 13:24:47 +00001464 }
Steve Naroff9165ad32007-10-31 04:38:33 +00001465}
1466
Fariborz Jahaniana70711b2007-12-04 21:47:40 +00001467// SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
1468void RewriteTest::SynthSelGetUidFunctionDecl() {
1469 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
1470 llvm::SmallVector<QualType, 16> ArgTys;
1471 ArgTys.push_back(Context->getPointerType(
1472 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001473 QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(),
Fariborz Jahaniana70711b2007-12-04 21:47:40 +00001474 &ArgTys[0], ArgTys.size(),
1475 false /*isVariadic*/);
1476 SelGetUidFunctionDecl = new FunctionDecl(SourceLocation(),
1477 SelGetUidIdent, getFuncType,
1478 FunctionDecl::Extern, false, 0);
1479}
1480
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00001481// SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto);
1482void RewriteTest::SynthGetProtocolFunctionDecl() {
1483 IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol");
1484 llvm::SmallVector<QualType, 16> ArgTys;
1485 ArgTys.push_back(Context->getPointerType(
1486 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001487 QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(),
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00001488 &ArgTys[0], ArgTys.size(),
1489 false /*isVariadic*/);
1490 GetProtocolFunctionDecl = new FunctionDecl(SourceLocation(),
1491 SelGetProtoIdent, getFuncType,
1492 FunctionDecl::Extern, false, 0);
1493}
1494
Steve Naroff09b266e2007-10-30 23:14:51 +00001495void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
1496 // declared in <objc/objc.h>
Steve Naroffbeaf2992007-11-03 11:27:19 +00001497 if (strcmp(FD->getName(), "sel_registerName") == 0) {
Steve Naroff09b266e2007-10-30 23:14:51 +00001498 SelGetUidFunctionDecl = FD;
Steve Naroff9165ad32007-10-31 04:38:33 +00001499 return;
1500 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001501 RewriteObjCQualifiedInterfaceTypes(FD);
Steve Naroff09b266e2007-10-30 23:14:51 +00001502}
1503
1504// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
1505void RewriteTest::SynthMsgSendFunctionDecl() {
1506 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
1507 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001508 QualType argT = Context->getObjCIdType();
Steve Naroff09b266e2007-10-30 23:14:51 +00001509 assert(!argT.isNull() && "Can't find 'id' type");
1510 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001511 argT = Context->getObjCSelType();
Steve Naroff09b266e2007-10-30 23:14:51 +00001512 assert(!argT.isNull() && "Can't find 'SEL' type");
1513 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff09b266e2007-10-30 23:14:51 +00001515 &ArgTys[0], ArgTys.size(),
1516 true /*isVariadic*/);
1517 MsgSendFunctionDecl = new FunctionDecl(SourceLocation(),
1518 msgSendIdent, msgSendType,
1519 FunctionDecl::Extern, false, 0);
1520}
1521
Steve Naroff874e2322007-11-15 10:28:18 +00001522// SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...);
1523void RewriteTest::SynthMsgSendSuperFunctionDecl() {
1524 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
1525 llvm::SmallVector<QualType, 16> ArgTys;
1526 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
1527 &Context->Idents.get("objc_super"), 0);
1528 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
1529 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
1530 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001531 argT = Context->getObjCSelType();
Steve Naroff874e2322007-11-15 10:28:18 +00001532 assert(!argT.isNull() && "Can't find 'SEL' type");
1533 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001534 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff874e2322007-11-15 10:28:18 +00001535 &ArgTys[0], ArgTys.size(),
1536 true /*isVariadic*/);
1537 MsgSendSuperFunctionDecl = new FunctionDecl(SourceLocation(),
1538 msgSendIdent, msgSendType,
1539 FunctionDecl::Extern, false, 0);
1540}
1541
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001542// SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
1543void RewriteTest::SynthMsgSendStretFunctionDecl() {
1544 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
1545 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 QualType argT = Context->getObjCIdType();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001547 assert(!argT.isNull() && "Can't find 'id' type");
1548 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001549 argT = Context->getObjCSelType();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001550 assert(!argT.isNull() && "Can't find 'SEL' type");
1551 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001552 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001553 &ArgTys[0], ArgTys.size(),
1554 true /*isVariadic*/);
1555 MsgSendStretFunctionDecl = new FunctionDecl(SourceLocation(),
1556 msgSendIdent, msgSendType,
1557 FunctionDecl::Extern, false, 0);
1558}
1559
1560// SynthMsgSendSuperStretFunctionDecl -
1561// id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...);
1562void RewriteTest::SynthMsgSendSuperStretFunctionDecl() {
1563 IdentifierInfo *msgSendIdent =
1564 &Context->Idents.get("objc_msgSendSuper_stret");
1565 llvm::SmallVector<QualType, 16> ArgTys;
1566 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
1567 &Context->Idents.get("objc_super"), 0);
1568 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
1569 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
1570 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001571 argT = Context->getObjCSelType();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001572 assert(!argT.isNull() && "Can't find 'SEL' type");
1573 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001574 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001575 &ArgTys[0], ArgTys.size(),
1576 true /*isVariadic*/);
1577 MsgSendSuperStretFunctionDecl = new FunctionDecl(SourceLocation(),
1578 msgSendIdent, msgSendType,
1579 FunctionDecl::Extern, false, 0);
1580}
1581
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001582// SynthMsgSendFpretFunctionDecl - id objc_msgSend_fpret(id self, SEL op, ...);
1583void RewriteTest::SynthMsgSendFpretFunctionDecl() {
1584 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
1585 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001586 QualType argT = Context->getObjCIdType();
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001587 assert(!argT.isNull() && "Can't find 'id' type");
1588 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001589 argT = Context->getObjCSelType();
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001590 assert(!argT.isNull() && "Can't find 'SEL' type");
1591 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001592 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001593 &ArgTys[0], ArgTys.size(),
1594 true /*isVariadic*/);
1595 MsgSendFpretFunctionDecl = new FunctionDecl(SourceLocation(),
1596 msgSendIdent, msgSendType,
1597 FunctionDecl::Extern, false, 0);
1598}
1599
Steve Naroff09b266e2007-10-30 23:14:51 +00001600// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
1601void RewriteTest::SynthGetClassFunctionDecl() {
1602 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
1603 llvm::SmallVector<QualType, 16> ArgTys;
1604 ArgTys.push_back(Context->getPointerType(
1605 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001606 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff09b266e2007-10-30 23:14:51 +00001607 &ArgTys[0], ArgTys.size(),
1608 false /*isVariadic*/);
1609 GetClassFunctionDecl = new FunctionDecl(SourceLocation(),
1610 getClassIdent, getClassType,
1611 FunctionDecl::Extern, false, 0);
1612}
1613
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001614// SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name);
1615void RewriteTest::SynthGetMetaClassFunctionDecl() {
1616 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
1617 llvm::SmallVector<QualType, 16> ArgTys;
1618 ArgTys.push_back(Context->getPointerType(
1619 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001620 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001621 &ArgTys[0], ArgTys.size(),
1622 false /*isVariadic*/);
1623 GetMetaClassFunctionDecl = new FunctionDecl(SourceLocation(),
1624 getClassIdent, getClassType,
1625 FunctionDecl::Extern, false, 0);
1626}
1627
Steve Naroff96984642007-11-08 14:30:50 +00001628// SynthCFStringFunctionDecl - id __builtin___CFStringMakeConstantString(const char *name);
1629void RewriteTest::SynthCFStringFunctionDecl() {
1630 IdentifierInfo *getClassIdent = &Context->Idents.get("__builtin___CFStringMakeConstantString");
1631 llvm::SmallVector<QualType, 16> ArgTys;
1632 ArgTys.push_back(Context->getPointerType(
1633 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001634 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff96984642007-11-08 14:30:50 +00001635 &ArgTys[0], ArgTys.size(),
1636 false /*isVariadic*/);
1637 CFStringFunctionDecl = new FunctionDecl(SourceLocation(),
1638 getClassIdent, getClassType,
1639 FunctionDecl::Extern, false, 0);
1640}
1641
Steve Naroffbeaf2992007-11-03 11:27:19 +00001642Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
Steve Naroff96984642007-11-08 14:30:50 +00001643#if 1
1644 // This rewrite is specific to GCC, which has builtin support for CFString.
1645 if (!CFStringFunctionDecl)
1646 SynthCFStringFunctionDecl();
1647 // Create a call to __builtin___CFStringMakeConstantString("cstr").
1648 llvm::SmallVector<Expr*, 8> StrExpr;
1649 StrExpr.push_back(Exp->getString());
1650 CallExpr *call = SynthesizeCallToFunctionDecl(CFStringFunctionDecl,
1651 &StrExpr[0], StrExpr.size());
1652 // cast to NSConstantString *
1653 CastExpr *cast = new CastExpr(Exp->getType(), call, SourceLocation());
Chris Lattnerdcbc5b02008-01-31 19:37:57 +00001654 ReplaceStmt(Exp, cast);
Steve Naroff96984642007-11-08 14:30:50 +00001655 delete Exp;
1656 return cast;
1657#else
Steve Naroffbeaf2992007-11-03 11:27:19 +00001658 assert(ConstantStringClassReference && "Can't find constant string reference");
1659 llvm::SmallVector<Expr*, 4> InitExprs;
1660
1661 // Synthesize "(Class)&_NSConstantStringClassReference"
1662 DeclRefExpr *ClsRef = new DeclRefExpr(ConstantStringClassReference,
1663 ConstantStringClassReference->getType(),
1664 SourceLocation());
1665 QualType expType = Context->getPointerType(ClsRef->getType());
1666 UnaryOperator *Unop = new UnaryOperator(ClsRef, UnaryOperator::AddrOf,
1667 expType, SourceLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001668 CastExpr *cast = new CastExpr(Context->getObjCClassType(), Unop,
Steve Naroffbeaf2992007-11-03 11:27:19 +00001669 SourceLocation());
1670 InitExprs.push_back(cast); // set the 'isa'.
1671 InitExprs.push_back(Exp->getString()); // set "char *bytes".
1672 unsigned IntSize = static_cast<unsigned>(
1673 Context->getTypeSize(Context->IntTy, Exp->getLocStart()));
1674 llvm::APInt IntVal(IntSize, Exp->getString()->getByteLength());
1675 IntegerLiteral *len = new IntegerLiteral(IntVal, Context->IntTy,
1676 Exp->getLocStart());
1677 InitExprs.push_back(len); // set "int numBytes".
1678
1679 // struct NSConstantString
1680 QualType CFConstantStrType = Context->getCFConstantStringType();
1681 // (struct NSConstantString) { <exprs from above> }
1682 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1683 &InitExprs[0], InitExprs.size(),
1684 SourceLocation());
Steve Naroffe9b12192008-01-14 18:19:28 +00001685 CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE, false);
Steve Naroffbeaf2992007-11-03 11:27:19 +00001686 // struct NSConstantString *
1687 expType = Context->getPointerType(StrRep->getType());
1688 Unop = new UnaryOperator(StrRep, UnaryOperator::AddrOf, expType,
1689 SourceLocation());
Steve Naroff352336b2007-11-05 14:36:37 +00001690 // cast to NSConstantString *
1691 cast = new CastExpr(Exp->getType(), Unop, SourceLocation());
Chris Lattnerdcbc5b02008-01-31 19:37:57 +00001692 ReplaceStmt(Exp, cast);
Steve Naroffbeaf2992007-11-03 11:27:19 +00001693 delete Exp;
Steve Naroff352336b2007-11-05 14:36:37 +00001694 return cast;
Steve Naroff96984642007-11-08 14:30:50 +00001695#endif
Steve Naroffbeaf2992007-11-03 11:27:19 +00001696}
1697
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001698ObjCInterfaceDecl *RewriteTest::isSuperReceiver(Expr *recExpr) {
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001699 // check if we are sending a message to 'super'
1700 if (CurMethodDecl && CurMethodDecl->isInstance()) {
Steve Naroff874e2322007-11-15 10:28:18 +00001701 if (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) {
1702 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1703 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
1704 if (!strcmp(PVD->getName(), "self")) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001705 // is this id<P1..> type?
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001706 if (CE->getType()->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001707 return 0;
Steve Naroff874e2322007-11-15 10:28:18 +00001708 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001709 if (ObjCInterfaceType *IT =
1710 dyn_cast<ObjCInterfaceType>(PT->getPointeeType())) {
Steve Naroff874e2322007-11-15 10:28:18 +00001711 if (IT->getDecl() ==
1712 CurMethodDecl->getClassInterface()->getSuperClass())
1713 return IT->getDecl();
1714 }
1715 }
1716 }
1717 }
1718 }
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001719 }
Steve Naroff874e2322007-11-15 10:28:18 +00001720 }
1721 return 0;
1722}
1723
1724// struct objc_super { struct objc_object *receiver; struct objc_class *super; };
1725QualType RewriteTest::getSuperStructType() {
1726 if (!SuperStructDecl) {
1727 SuperStructDecl = new RecordDecl(Decl::Struct, SourceLocation(),
1728 &Context->Idents.get("objc_super"), 0);
1729 QualType FieldTypes[2];
1730
1731 // struct objc_object *receiver;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001732 FieldTypes[0] = Context->getObjCIdType();
Steve Naroff874e2322007-11-15 10:28:18 +00001733 // struct objc_class *super;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001734 FieldTypes[1] = Context->getObjCClassType();
Steve Naroff874e2322007-11-15 10:28:18 +00001735 // Create fields
1736 FieldDecl *FieldDecls[2];
1737
1738 for (unsigned i = 0; i < 2; ++i)
1739 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
1740
1741 SuperStructDecl->defineBody(FieldDecls, 4);
1742 }
1743 return Context->getTagDeclType(SuperStructDecl);
1744}
1745
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001746Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) {
Fariborz Jahaniana70711b2007-12-04 21:47:40 +00001747 if (!SelGetUidFunctionDecl)
1748 SynthSelGetUidFunctionDecl();
Steve Naroff09b266e2007-10-30 23:14:51 +00001749 if (!MsgSendFunctionDecl)
1750 SynthMsgSendFunctionDecl();
Steve Naroff874e2322007-11-15 10:28:18 +00001751 if (!MsgSendSuperFunctionDecl)
1752 SynthMsgSendSuperFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001753 if (!MsgSendStretFunctionDecl)
1754 SynthMsgSendStretFunctionDecl();
1755 if (!MsgSendSuperStretFunctionDecl)
1756 SynthMsgSendSuperStretFunctionDecl();
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001757 if (!MsgSendFpretFunctionDecl)
1758 SynthMsgSendFpretFunctionDecl();
Steve Naroff09b266e2007-10-30 23:14:51 +00001759 if (!GetClassFunctionDecl)
1760 SynthGetClassFunctionDecl();
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001761 if (!GetMetaClassFunctionDecl)
1762 SynthGetMetaClassFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001763
Steve Naroff874e2322007-11-15 10:28:18 +00001764 // default to objc_msgSend().
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001765 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
1766 // May need to use objc_msgSend_stret() as well.
1767 FunctionDecl *MsgSendStretFlavor = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001768 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001769 QualType resultType = mDecl->getResultType();
1770 if (resultType.getCanonicalType()->isStructureType()
1771 || resultType.getCanonicalType()->isUnionType())
1772 MsgSendStretFlavor = MsgSendStretFunctionDecl;
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001773 else if (resultType.getCanonicalType()->isRealFloatingType())
1774 MsgSendFlavor = MsgSendFpretFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001775 }
Steve Naroff874e2322007-11-15 10:28:18 +00001776
Steve Naroff934f2762007-10-24 22:48:43 +00001777 // Synthesize a call to objc_msgSend().
1778 llvm::SmallVector<Expr*, 8> MsgExprs;
1779 IdentifierInfo *clsName = Exp->getClassName();
1780
1781 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
1782 if (clsName) { // class message.
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001783 if (!strcmp(clsName->getName(), "super")) {
1784 MsgSendFlavor = MsgSendSuperFunctionDecl;
1785 if (MsgSendStretFlavor)
1786 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
1787 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
1788
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001789 ObjCInterfaceDecl *SuperDecl =
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001790 CurMethodDecl->getClassInterface()->getSuperClass();
1791
1792 llvm::SmallVector<Expr*, 4> InitExprs;
1793
1794 // set the receiver to self, the first argument to all methods.
1795 InitExprs.push_back(new DeclRefExpr(CurMethodDecl->getSelfDecl(),
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001796 Context->getObjCIdType(),
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001797 SourceLocation()));
1798 llvm::SmallVector<Expr*, 8> ClsExprs;
1799 QualType argType = Context->getPointerType(Context->CharTy);
1800 ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
1801 SuperDecl->getIdentifier()->getLength(),
1802 false, argType, SourceLocation(),
1803 SourceLocation()));
1804 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
1805 &ClsExprs[0],
1806 ClsExprs.size());
1807 // To turn off a warning, type-cast to 'id'
1808 InitExprs.push_back(
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001809 new CastExpr(Context->getObjCIdType(),
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001810 Cls, SourceLocation())); // set 'super class', using objc_getClass().
1811 // struct objc_super
1812 QualType superType = getSuperStructType();
1813 // (struct objc_super) { <exprs from above> }
1814 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1815 &InitExprs[0], InitExprs.size(),
1816 SourceLocation());
Chris Lattner0fc53df2008-01-02 21:46:24 +00001817 CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
Steve Naroffe9b12192008-01-14 18:19:28 +00001818 superType, ILE, false);
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001819 // struct objc_super *
1820 Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
1821 Context->getPointerType(SuperRep->getType()),
1822 SourceLocation());
1823 MsgExprs.push_back(Unop);
1824 } else {
1825 llvm::SmallVector<Expr*, 8> ClsExprs;
1826 QualType argType = Context->getPointerType(Context->CharTy);
1827 ClsExprs.push_back(new StringLiteral(clsName->getName(),
1828 clsName->getLength(),
1829 false, argType, SourceLocation(),
1830 SourceLocation()));
1831 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
1832 &ClsExprs[0],
1833 ClsExprs.size());
1834 MsgExprs.push_back(Cls);
1835 }
Steve Naroff6568d4d2007-11-14 23:54:14 +00001836 } else { // instance message.
1837 Expr *recExpr = Exp->getReceiver();
Steve Naroff874e2322007-11-15 10:28:18 +00001838
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001839 if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) {
Steve Naroff874e2322007-11-15 10:28:18 +00001840 MsgSendFlavor = MsgSendSuperFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001841 if (MsgSendStretFlavor)
1842 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
Steve Naroff874e2322007-11-15 10:28:18 +00001843 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
1844
1845 llvm::SmallVector<Expr*, 4> InitExprs;
1846
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001847 InitExprs.push_back(
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001848 new CastExpr(Context->getObjCIdType(),
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001849 recExpr, SourceLocation())); // set the 'receiver'.
Steve Naroff874e2322007-11-15 10:28:18 +00001850
1851 llvm::SmallVector<Expr*, 8> ClsExprs;
1852 QualType argType = Context->getPointerType(Context->CharTy);
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001853 ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
1854 SuperDecl->getIdentifier()->getLength(),
Steve Naroff874e2322007-11-15 10:28:18 +00001855 false, argType, SourceLocation(),
1856 SourceLocation()));
1857 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001858 &ClsExprs[0],
1859 ClsExprs.size());
Fariborz Jahanian71274312007-12-05 17:29:46 +00001860 // To turn off a warning, type-cast to 'id'
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001861 InitExprs.push_back(
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001862 new CastExpr(Context->getObjCIdType(),
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001863 Cls, SourceLocation())); // set 'super class', using objc_getClass().
Steve Naroff874e2322007-11-15 10:28:18 +00001864 // struct objc_super
1865 QualType superType = getSuperStructType();
1866 // (struct objc_super) { <exprs from above> }
1867 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1868 &InitExprs[0], InitExprs.size(),
1869 SourceLocation());
Chris Lattner0fc53df2008-01-02 21:46:24 +00001870 CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
Steve Naroffe9b12192008-01-14 18:19:28 +00001871 superType, ILE, false);
Steve Naroff874e2322007-11-15 10:28:18 +00001872 // struct objc_super *
1873 Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
1874 Context->getPointerType(SuperRep->getType()),
1875 SourceLocation());
1876 MsgExprs.push_back(Unop);
1877 } else {
Fariborz Jahanian7dd82832007-12-07 21:21:21 +00001878 // Remove all type-casts because it may contain objc-style types; e.g.
1879 // Foo<Proto> *.
1880 while (CastExpr *CE = dyn_cast<CastExpr>(recExpr))
1881 recExpr = CE->getSubExpr();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001882 recExpr = new CastExpr(Context->getObjCIdType(), recExpr, SourceLocation());
Steve Naroff874e2322007-11-15 10:28:18 +00001883 MsgExprs.push_back(recExpr);
1884 }
Steve Naroff6568d4d2007-11-14 23:54:14 +00001885 }
Steve Naroffbeaf2992007-11-03 11:27:19 +00001886 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
Steve Naroff934f2762007-10-24 22:48:43 +00001887 llvm::SmallVector<Expr*, 8> SelExprs;
1888 QualType argType = Context->getPointerType(Context->CharTy);
1889 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
1890 Exp->getSelector().getName().size(),
1891 false, argType, SourceLocation(),
1892 SourceLocation()));
1893 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
1894 &SelExprs[0], SelExprs.size());
1895 MsgExprs.push_back(SelExp);
1896
1897 // Now push any user supplied arguments.
1898 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
Steve Naroff6568d4d2007-11-14 23:54:14 +00001899 Expr *userExpr = Exp->getArg(i);
Steve Naroff7e3411b2007-11-15 02:58:25 +00001900 // Make all implicit casts explicit...ICE comes in handy:-)
1901 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
1902 // Reuse the ICE type, it is exactly what the doctor ordered.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001903 userExpr = new CastExpr(ICE->getType()->isObjCQualifiedIdType()
1904 ? Context->getObjCIdType()
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001905 : ICE->getType(), userExpr, SourceLocation());
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001906 }
1907 // Make id<P...> cast into an 'id' cast.
1908 else if (CastExpr *CE = dyn_cast<CastExpr>(userExpr)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001909 if (CE->getType()->isObjCQualifiedIdType()) {
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001910 while ((CE = dyn_cast<CastExpr>(userExpr)))
1911 userExpr = CE->getSubExpr();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001912 userExpr = new CastExpr(Context->getObjCIdType(),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001913 userExpr, SourceLocation());
1914 }
Steve Naroff7e3411b2007-11-15 02:58:25 +00001915 }
Steve Naroff6568d4d2007-11-14 23:54:14 +00001916 MsgExprs.push_back(userExpr);
Steve Naroff934f2762007-10-24 22:48:43 +00001917 // We've transferred the ownership to MsgExprs. Null out the argument in
1918 // the original expression, since we will delete it below.
1919 Exp->setArg(i, 0);
1920 }
Steve Naroffab972d32007-11-04 22:37:50 +00001921 // Generate the funky cast.
1922 CastExpr *cast;
1923 llvm::SmallVector<QualType, 8> ArgTypes;
1924 QualType returnType;
1925
1926 // Push 'id' and 'SEL', the 2 implicit arguments.
Steve Naroffc3a438c2007-11-15 10:43:57 +00001927 if (MsgSendFlavor == MsgSendSuperFunctionDecl)
1928 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
1929 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001930 ArgTypes.push_back(Context->getObjCIdType());
1931 ArgTypes.push_back(Context->getObjCSelType());
1932 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
Steve Naroffab972d32007-11-04 22:37:50 +00001933 // Push any user argument types.
Steve Naroff352336b2007-11-05 14:36:37 +00001934 for (int i = 0; i < mDecl->getNumParams(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001935 QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType()
1936 ? Context->getObjCIdType()
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001937 : mDecl->getParamDecl(i)->getType();
Steve Naroff352336b2007-11-05 14:36:37 +00001938 ArgTypes.push_back(t);
1939 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001940 returnType = mDecl->getResultType()->isObjCQualifiedIdType()
1941 ? Context->getObjCIdType() : mDecl->getResultType();
Steve Naroffab972d32007-11-04 22:37:50 +00001942 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001943 returnType = Context->getObjCIdType();
Steve Naroffab972d32007-11-04 22:37:50 +00001944 }
1945 // Get the type, we will need to reference it in a couple spots.
Steve Naroff874e2322007-11-15 10:28:18 +00001946 QualType msgSendType = MsgSendFlavor->getType();
Steve Naroffab972d32007-11-04 22:37:50 +00001947
1948 // Create a reference to the objc_msgSend() declaration.
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001949 DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType,
1950 SourceLocation());
Steve Naroffab972d32007-11-04 22:37:50 +00001951
1952 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
1953 // If we don't do this cast, we get the following bizarre warning/note:
1954 // xx.m:13: warning: function called through a non-compatible type
1955 // xx.m:13: note: if this code is reached, the program will abort
1956 cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE,
1957 SourceLocation());
Steve Naroff335eafa2007-11-15 12:35:21 +00001958
Steve Naroffab972d32007-11-04 22:37:50 +00001959 // Now do the "normal" pointer to function cast.
1960 QualType castType = Context->getFunctionType(returnType,
Fariborz Jahaniand0ee6f92007-12-06 19:49:56 +00001961 &ArgTypes[0], ArgTypes.size(),
1962 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
Steve Naroffab972d32007-11-04 22:37:50 +00001963 castType = Context->getPointerType(castType);
1964 cast = new CastExpr(castType, cast, SourceLocation());
1965
1966 // Don't forget the parens to enforce the proper binding.
1967 ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
1968
1969 const FunctionType *FT = msgSendType->getAsFunctionType();
1970 CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
1971 FT->getResultType(), SourceLocation());
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001972 Stmt *ReplacingStmt = CE;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001973 if (MsgSendStretFlavor) {
1974 // We have the method which returns a struct/union. Must also generate
1975 // call to objc_msgSend_stret and hang both varieties on a conditional
1976 // expression which dictate which one to envoke depending on size of
1977 // method's return type.
1978
1979 // Create a reference to the objc_msgSend_stret() declaration.
1980 DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType,
1981 SourceLocation());
1982 // Need to cast objc_msgSend_stret to "void *" (see above comment).
1983 cast = new CastExpr(Context->getPointerType(Context->VoidTy), STDRE,
1984 SourceLocation());
1985 // Now do the "normal" pointer to function cast.
1986 castType = Context->getFunctionType(returnType,
Fariborz Jahaniand0ee6f92007-12-06 19:49:56 +00001987 &ArgTypes[0], ArgTypes.size(),
1988 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001989 castType = Context->getPointerType(castType);
1990 cast = new CastExpr(castType, cast, SourceLocation());
1991
1992 // Don't forget the parens to enforce the proper binding.
1993 PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
1994
1995 FT = msgSendType->getAsFunctionType();
1996 CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
1997 FT->getResultType(), SourceLocation());
1998
1999 // Build sizeof(returnType)
2000 SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true,
2001 returnType, Context->getSizeType(),
2002 SourceLocation(), SourceLocation());
2003 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
2004 // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases.
2005 // For X86 it is more complicated and some kind of target specific routine
2006 // is needed to decide what to do.
2007 unsigned IntSize = static_cast<unsigned>(
2008 Context->getTypeSize(Context->IntTy, SourceLocation()));
2009
2010 IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8),
2011 Context->IntTy,
2012 SourceLocation());
2013 BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit,
2014 BinaryOperator::LE,
2015 Context->IntTy,
2016 SourceLocation());
2017 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
2018 ConditionalOperator *CondExpr =
2019 new ConditionalOperator(lessThanExpr, CE, STCE, returnType);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00002020 ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr);
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00002021 }
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00002022 return ReplacingStmt;
2023}
2024
2025Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
2026 Stmt *ReplacingStmt = SynthMessageExpr(Exp);
Steve Naroff934f2762007-10-24 22:48:43 +00002027 // Now do the actual rewrite.
Chris Lattnerdcbc5b02008-01-31 19:37:57 +00002028 ReplaceStmt(Exp, ReplacingStmt);
Steve Naroff934f2762007-10-24 22:48:43 +00002029
Chris Lattnere64b7772007-10-24 16:57:36 +00002030 delete Exp;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00002031 return ReplacingStmt;
Steve Naroffebf2b562007-10-23 23:50:29 +00002032}
2033
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00002034/// RewriteObjCProtocolExpr - Rewrite a protocol expression into
2035/// call to objc_getProtocol("proto-name").
2036Stmt *RewriteTest::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
2037 if (!GetProtocolFunctionDecl)
2038 SynthGetProtocolFunctionDecl();
2039 // Create a call to objc_getProtocol("ProtocolName").
2040 llvm::SmallVector<Expr*, 8> ProtoExprs;
2041 QualType argType = Context->getPointerType(Context->CharTy);
2042 ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(),
2043 strlen(Exp->getProtocol()->getName()),
2044 false, argType, SourceLocation(),
2045 SourceLocation()));
2046 CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl,
2047 &ProtoExprs[0],
2048 ProtoExprs.size());
Chris Lattnerdcbc5b02008-01-31 19:37:57 +00002049 ReplaceStmt(Exp, ProtoExp);
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00002050 delete Exp;
2051 return ProtoExp;
2052
2053}
2054
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002055/// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002056/// an objective-c class with ivars.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002057void RewriteTest::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002058 std::string &Result) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002059 assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
2060 assert(CDecl->getName() && "Name missing in SynthesizeObjCInternalStruct");
Fariborz Jahanian212b7682007-10-31 23:08:24 +00002061 // Do not synthesize more than once.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002062 if (ObjCSynthesizedStructs.count(CDecl))
Fariborz Jahanian212b7682007-10-31 23:08:24 +00002063 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002064 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
Steve Naroff03300712007-11-12 13:56:41 +00002065 int NumIvars = CDecl->getNumInstanceVariables();
Steve Narofffea763e82007-11-14 19:25:57 +00002066 SourceLocation LocStart = CDecl->getLocStart();
2067 SourceLocation LocEnd = CDecl->getLocEnd();
2068
2069 const char *startBuf = SM->getCharacterData(LocStart);
2070 const char *endBuf = SM->getCharacterData(LocEnd);
Fariborz Jahanian2c7038b2007-11-26 19:52:57 +00002071 // If no ivars and no root or if its root, directly or indirectly,
2072 // have no ivars (thus not synthesized) then no need to synthesize this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002073 if (NumIvars <= 0 && (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
Fariborz Jahanian2c7038b2007-11-26 19:52:57 +00002074 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
2075 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
2076 Result.c_str(), Result.size());
2077 return;
2078 }
2079
2080 // FIXME: This has potential of causing problem. If
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002081 // SynthesizeObjCInternalStruct is ever called recursively.
Fariborz Jahanian2c7038b2007-11-26 19:52:57 +00002082 Result += "\nstruct ";
2083 Result += CDecl->getName();
Steve Narofffea763e82007-11-14 19:25:57 +00002084
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002085 if (NumIvars > 0) {
Steve Narofffea763e82007-11-14 19:25:57 +00002086 const char *cursor = strchr(startBuf, '{');
2087 assert((cursor && endBuf)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002088 && "SynthesizeObjCInternalStruct - malformed @interface");
Steve Narofffea763e82007-11-14 19:25:57 +00002089
2090 // rewrite the original header *without* disturbing the '{'
2091 Rewrite.ReplaceText(LocStart, cursor-startBuf-1,
2092 Result.c_str(), Result.size());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002093 if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
Steve Narofffea763e82007-11-14 19:25:57 +00002094 Result = "\n struct ";
2095 Result += RCDecl->getName();
Steve Narofff69cc5d2008-01-30 19:17:43 +00002096 // Note: We don't name the field decl. This simplifies the "codegen" for
2097 // accessing a superclasses instance variables (and is similar to what gcc
2098 // does internally). The unnamed struct field feature is enabled with
2099 // -fms-extensions. If the struct definition were "inlined", we wouldn't
2100 // need to use this switch. That said, I don't want to inline the def.
Steve Narofffea763e82007-11-14 19:25:57 +00002101 Result += ";\n";
2102
2103 // insert the super class structure definition.
2104 SourceLocation OnePastCurly = LocStart.getFileLocWithOffset(cursor-startBuf+1);
2105 Rewrite.InsertText(OnePastCurly, Result.c_str(), Result.size());
2106 }
2107 cursor++; // past '{'
2108
2109 // Now comment out any visibility specifiers.
2110 while (cursor < endBuf) {
2111 if (*cursor == '@') {
2112 SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
Chris Lattnerdf6a51b2007-11-14 22:57:51 +00002113 // Skip whitespace.
2114 for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor)
2115 /*scan*/;
2116
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002117 // FIXME: presence of @public, etc. inside comment results in
2118 // this transformation as well, which is still correct c-code.
Steve Narofffea763e82007-11-14 19:25:57 +00002119 if (!strncmp(cursor, "public", strlen("public")) ||
2120 !strncmp(cursor, "private", strlen("private")) ||
Fariborz Jahanian95673922007-11-14 22:26:25 +00002121 !strncmp(cursor, "protected", strlen("protected")))
Steve Narofffea763e82007-11-14 19:25:57 +00002122 Rewrite.InsertText(atLoc, "// ", 3);
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002123 }
Fariborz Jahanian95673922007-11-14 22:26:25 +00002124 // FIXME: If there are cases where '<' is used in ivar declaration part
2125 // of user code, then scan the ivar list and use needToScanForQualifiers
2126 // for type checking.
2127 else if (*cursor == '<') {
2128 SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
2129 Rewrite.InsertText(atLoc, "/* ", 3);
2130 cursor = strchr(cursor, '>');
2131 cursor++;
2132 atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
2133 Rewrite.InsertText(atLoc, " */", 3);
2134 }
Steve Narofffea763e82007-11-14 19:25:57 +00002135 cursor++;
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002136 }
Steve Narofffea763e82007-11-14 19:25:57 +00002137 // Don't forget to add a ';'!!
2138 Rewrite.InsertText(LocEnd.getFileLocWithOffset(1), ";", 1);
2139 } else { // we don't have any instance variables - insert super struct.
2140 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
2141 Result += " {\n struct ";
2142 Result += RCDecl->getName();
Steve Narofff69cc5d2008-01-30 19:17:43 +00002143 // Note: We don't name the field decl. This simplifies the "codegen" for
2144 // accessing a superclasses instance variables (and is similar to what gcc
2145 // does internally). The unnamed struct field feature is enabled with
2146 // -fms-extensions. If the struct definition were "inlined", we wouldn't
2147 // need to use this switch. That said, I don't want to inline the def.
Steve Narofffea763e82007-11-14 19:25:57 +00002148 Result += ";\n};\n";
2149 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
2150 Result.c_str(), Result.size());
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002151 }
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002152 // Mark this struct as having been generated.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002153 if (!ObjCSynthesizedStructs.insert(CDecl))
2154 assert(false && "struct already synthesize- SynthesizeObjCInternalStruct");
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002155}
2156
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002157// RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002158/// class methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002159void RewriteTest::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002160 instmeth_iterator MethodEnd,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +00002161 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002162 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +00002163 const char *ClassName,
2164 std::string &Result) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002165 if (MethodBegin == MethodEnd) return;
2166
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002167 static bool objc_impl_method = false;
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002168 if (!objc_impl_method) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002169 /* struct _objc_method {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002170 SEL _cmd;
2171 char *method_types;
2172 void *_imp;
2173 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002174 */
Chris Lattner158ecb92007-10-25 17:07:24 +00002175 Result += "\nstruct _objc_method {\n";
2176 Result += "\tSEL _cmd;\n";
2177 Result += "\tchar *method_types;\n";
2178 Result += "\tvoid *_imp;\n";
2179 Result += "};\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002180
2181 /* struct _objc_method_list {
2182 struct _objc_method_list *next_method;
2183 int method_count;
2184 struct _objc_method method_list[];
2185 }
2186 */
2187 Result += "\nstruct _objc_method_list {\n";
2188 Result += "\tstruct _objc_method_list *next_method;\n";
2189 Result += "\tint method_count;\n";
2190 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002191 objc_impl_method = true;
Fariborz Jahanian776d6ff2007-10-19 00:36:46 +00002192 }
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002193
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002194 // Build _objc_method_list for class's methods if needed
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002195 Result += "\nstatic struct _objc_method_list _OBJC_";
2196 Result += prefix;
2197 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
2198 Result += "_METHODS_";
2199 Result += ClassName;
2200 Result += " __attribute__ ((section (\"__OBJC, __";
2201 Result += IsInstanceMethod ? "inst" : "cls";
2202 Result += "_meth\")))= ";
2203 Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002204
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002205 Result += "\t,{{(SEL)\"";
2206 Result += (*MethodBegin)->getSelector().getName().c_str();
2207 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002208 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002209 Result += "\", \"";
2210 Result += MethodTypeString;
2211 Result += "\", ";
2212 Result += MethodInternalNames[*MethodBegin];
2213 Result += "}\n";
2214 for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
2215 Result += "\t ,{(SEL)\"";
2216 Result += (*MethodBegin)->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002217 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002218 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002219 Result += "\", \"";
2220 Result += MethodTypeString;
Fariborz Jahanianb7908b52007-11-13 21:02:00 +00002221 Result += "\", ";
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002222 Result += MethodInternalNames[*MethodBegin];
Fariborz Jahanianb7908b52007-11-13 21:02:00 +00002223 Result += "}\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002224 }
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002225 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002226}
2227
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002228/// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data.
2229void RewriteTest::RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002230 int NumProtocols,
2231 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002232 const char *ClassName,
2233 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002234 static bool objc_protocol_methods = false;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002235 if (NumProtocols > 0) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002236 for (int i = 0; i < NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002237 ObjCProtocolDecl *PDecl = Protocols[i];
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002238 // Output struct protocol_methods holder of method selector and type.
2239 if (!objc_protocol_methods &&
2240 (PDecl->getNumInstanceMethods() > 0
2241 || PDecl->getNumClassMethods() > 0)) {
2242 /* struct protocol_methods {
2243 SEL _cmd;
2244 char *method_types;
2245 }
2246 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002247 Result += "\nstruct protocol_methods {\n";
2248 Result += "\tSEL _cmd;\n";
2249 Result += "\tchar *method_types;\n";
2250 Result += "};\n";
2251
2252 /* struct _objc_protocol_method_list {
2253 int protocol_method_count;
2254 struct protocol_methods protocols[];
2255 }
2256 */
2257 Result += "\nstruct _objc_protocol_method_list {\n";
2258 Result += "\tint protocol_method_count;\n";
2259 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002260 objc_protocol_methods = true;
2261 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002262
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002263 int NumMethods = PDecl->getNumInstanceMethods();
2264 if(NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002265 Result += "\nstatic struct _objc_protocol_method_list "
2266 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
2267 Result += PDecl->getName();
2268 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
2269 "{\n\t" + utostr(NumMethods) + "\n";
2270
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002271 // Output instance methods declared in this protocol.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002272 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002273 E = PDecl->instmeth_end(); I != E; ++I) {
2274 if (I == PDecl->instmeth_begin())
2275 Result += "\t ,{{(SEL)\"";
2276 else
2277 Result += "\t ,{(SEL)\"";
2278 Result += (*I)->getSelector().getName().c_str();
2279 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002280 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002281 Result += "\", \"";
2282 Result += MethodTypeString;
2283 Result += "\"}\n";
2284 }
2285 Result += "\t }\n};\n";
2286 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002287
2288 // Output class methods declared in this protocol.
2289 NumMethods = PDecl->getNumClassMethods();
2290 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002291 Result += "\nstatic struct _objc_protocol_method_list "
2292 "_OBJC_PROTOCOL_CLASS_METHODS_";
2293 Result += PDecl->getName();
2294 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
2295 "{\n\t";
2296 Result += utostr(NumMethods);
2297 Result += "\n";
2298
Fariborz Jahanianc6e2c2a2007-12-17 18:07:01 +00002299 // Output instance methods declared in this protocol.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002300 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Fariborz Jahanianc6e2c2a2007-12-17 18:07:01 +00002301 E = PDecl->classmeth_end(); I != E; ++I) {
2302 if (I == PDecl->classmeth_begin())
2303 Result += "\t ,{{(SEL)\"";
2304 else
2305 Result += "\t ,{(SEL)\"";
Steve Naroff58dbdeb2007-12-14 23:37:57 +00002306 Result += (*I)->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002307 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002308 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002309 Result += "\", \"";
2310 Result += MethodTypeString;
2311 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002312 }
2313 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002314 }
2315 // Output:
2316 /* struct _objc_protocol {
2317 // Objective-C 1.0 extensions
2318 struct _objc_protocol_extension *isa;
2319 char *protocol_name;
2320 struct _objc_protocol **protocol_list;
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002321 struct _objc_protocol_method_list *instance_methods;
2322 struct _objc_protocol_method_list *class_methods;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002323 };
2324 */
2325 static bool objc_protocol = false;
2326 if (!objc_protocol) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002327 Result += "\nstruct _objc_protocol {\n";
2328 Result += "\tstruct _objc_protocol_extension *isa;\n";
2329 Result += "\tchar *protocol_name;\n";
2330 Result += "\tstruct _objc_protocol **protocol_list;\n";
2331 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
2332 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
2333 Result += "};\n";
2334
2335 /* struct _objc_protocol_list {
2336 struct _objc_protocol_list *next;
2337 int protocol_count;
2338 struct _objc_protocol *class_protocols[];
2339 }
2340 */
2341 Result += "\nstruct _objc_protocol_list {\n";
2342 Result += "\tstruct _objc_protocol_list *next;\n";
2343 Result += "\tint protocol_count;\n";
2344 Result += "\tstruct _objc_protocol *class_protocols[];\n";
2345 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002346 objc_protocol = true;
2347 }
2348
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002349 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
2350 Result += PDecl->getName();
2351 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
2352 "{\n\t0, \"";
2353 Result += PDecl->getName();
2354 Result += "\", 0, ";
Steve Naroff58dbdeb2007-12-14 23:37:57 +00002355 if (PDecl->getNumInstanceMethods() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002356 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
2357 Result += PDecl->getName();
2358 Result += ", ";
2359 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002360 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002361 Result += "0, ";
Steve Naroff58dbdeb2007-12-14 23:37:57 +00002362 if (PDecl->getNumClassMethods() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002363 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
2364 Result += PDecl->getName();
2365 Result += "\n";
2366 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002367 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002368 Result += "0\n";
2369 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002370 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002371 // Output the top lovel protocol meta-data for the class.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002372 Result += "\nstatic struct _objc_protocol_list _OBJC_";
2373 Result += prefix;
2374 Result += "_PROTOCOLS_";
2375 Result += ClassName;
2376 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
2377 "{\n\t0, ";
2378 Result += utostr(NumProtocols);
2379 Result += "\n";
2380
2381 Result += "\t,{&_OBJC_PROTOCOL_";
2382 Result += Protocols[0]->getName();
2383 Result += " \n";
2384
2385 for (int i = 1; i < NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002386 ObjCProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002387 Result += "\t ,&_OBJC_PROTOCOL_";
2388 Result += PDecl->getName();
2389 Result += "\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002390 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002391 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002392 }
2393}
2394
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002395/// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002396/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002397void RewriteTest::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002398 std::string &Result) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002399 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002400 // Find category declaration for this implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002401 ObjCCategoryDecl *CDecl;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002402 for (CDecl = ClassDecl->getCategoryList(); CDecl;
2403 CDecl = CDecl->getNextClassCategory())
2404 if (CDecl->getIdentifier() == IDecl->getIdentifier())
2405 break;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002406
Chris Lattnereb44eee2007-12-23 01:40:15 +00002407 std::string FullCategoryName = ClassDecl->getName();
2408 FullCategoryName += '_';
2409 FullCategoryName += IDecl->getName();
2410
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002411 // Build _objc_method_list for class's instance methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002412 RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
Chris Lattnereb44eee2007-12-23 01:40:15 +00002413 true, "CATEGORY_", FullCategoryName.c_str(),
2414 Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002415
2416 // Build _objc_method_list for class's class methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002417 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
Chris Lattnereb44eee2007-12-23 01:40:15 +00002418 false, "CATEGORY_", FullCategoryName.c_str(),
2419 Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002420
2421 // Protocols referenced in class declaration?
Fariborz Jahanianbac97d42007-11-13 22:09:49 +00002422 // Null CDecl is case of a category implementation with no category interface
2423 if (CDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002424 RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
Fariborz Jahanianbac97d42007-11-13 22:09:49 +00002425 CDecl->getNumReferencedProtocols(),
2426 "CATEGORY",
Chris Lattnereb44eee2007-12-23 01:40:15 +00002427 FullCategoryName.c_str(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002428
2429 /* struct _objc_category {
2430 char *category_name;
2431 char *class_name;
2432 struct _objc_method_list *instance_methods;
2433 struct _objc_method_list *class_methods;
2434 struct _objc_protocol_list *protocols;
2435 // Objective-C 1.0 extensions
2436 uint32_t size; // sizeof (struct _objc_category)
2437 struct _objc_property_list *instance_properties; // category's own
2438 // @property decl.
2439 };
2440 */
2441
2442 static bool objc_category = false;
2443 if (!objc_category) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002444 Result += "\nstruct _objc_category {\n";
2445 Result += "\tchar *category_name;\n";
2446 Result += "\tchar *class_name;\n";
2447 Result += "\tstruct _objc_method_list *instance_methods;\n";
2448 Result += "\tstruct _objc_method_list *class_methods;\n";
2449 Result += "\tstruct _objc_protocol_list *protocols;\n";
2450 Result += "\tunsigned int size;\n";
2451 Result += "\tstruct _objc_property_list *instance_properties;\n";
2452 Result += "};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002453 objc_category = true;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002454 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002455 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
2456 Result += FullCategoryName;
2457 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
2458 Result += IDecl->getName();
2459 Result += "\"\n\t, \"";
2460 Result += ClassDecl->getName();
2461 Result += "\"\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002462
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002463 if (IDecl->getNumInstanceMethods() > 0) {
2464 Result += "\t, (struct _objc_method_list *)"
2465 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
2466 Result += FullCategoryName;
2467 Result += "\n";
2468 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002469 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002470 Result += "\t, 0\n";
2471 if (IDecl->getNumClassMethods() > 0) {
2472 Result += "\t, (struct _objc_method_list *)"
2473 "&_OBJC_CATEGORY_CLASS_METHODS_";
2474 Result += FullCategoryName;
2475 Result += "\n";
2476 }
2477 else
2478 Result += "\t, 0\n";
2479
Fariborz Jahanianbac97d42007-11-13 22:09:49 +00002480 if (CDecl && CDecl->getNumReferencedProtocols() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002481 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
2482 Result += FullCategoryName;
2483 Result += "\n";
2484 }
2485 else
2486 Result += "\t, 0\n";
2487 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002488}
2489
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002490/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
2491/// ivar offset.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002492void RewriteTest::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
2493 ObjCIvarDecl *ivar,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002494 std::string &Result) {
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00002495 Result += "offsetof(struct ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002496 Result += IDecl->getName();
2497 Result += ", ";
2498 Result += ivar->getName();
2499 Result += ")";
2500}
2501
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002502//===----------------------------------------------------------------------===//
2503// Meta Data Emission
2504//===----------------------------------------------------------------------===//
2505
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002506void RewriteTest::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002507 std::string &Result) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002508 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002509
Fariborz Jahanianebe668f2007-11-26 20:59:57 +00002510 // Explictly declared @interface's are already synthesized.
2511 if (CDecl->ImplicitInterfaceDecl()) {
2512 // FIXME: Implementation of a class with no @interface (legacy) doese not
2513 // produce correct synthesis as yet.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002514 SynthesizeObjCInternalStruct(CDecl, Result);
Fariborz Jahanianebe668f2007-11-26 20:59:57 +00002515 }
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002516
Chris Lattnerbe6df082007-12-12 07:56:42 +00002517 // Build _objc_ivar_list metadata for classes ivars if needed
2518 int NumIvars = IDecl->getImplDeclNumIvars() > 0
2519 ? IDecl->getImplDeclNumIvars()
2520 : (CDecl ? CDecl->getNumInstanceVariables() : 0);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002521 if (NumIvars > 0) {
2522 static bool objc_ivar = false;
2523 if (!objc_ivar) {
2524 /* struct _objc_ivar {
2525 char *ivar_name;
2526 char *ivar_type;
2527 int ivar_offset;
2528 };
2529 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002530 Result += "\nstruct _objc_ivar {\n";
2531 Result += "\tchar *ivar_name;\n";
2532 Result += "\tchar *ivar_type;\n";
2533 Result += "\tint ivar_offset;\n";
2534 Result += "};\n";
2535
2536 /* struct _objc_ivar_list {
2537 int ivar_count;
2538 struct _objc_ivar ivar_list[];
2539 };
2540 */
2541 Result += "\nstruct _objc_ivar_list {\n";
2542 Result += "\tint ivar_count;\n";
2543 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002544 objc_ivar = true;
2545 }
2546
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002547 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
2548 Result += IDecl->getName();
2549 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
2550 "{\n\t";
2551 Result += utostr(NumIvars);
2552 Result += "\n";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002553
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002554 ObjCInterfaceDecl::ivar_iterator IVI, IVE;
Chris Lattnerbe6df082007-12-12 07:56:42 +00002555 if (IDecl->getImplDeclNumIvars() > 0) {
2556 IVI = IDecl->ivar_begin();
2557 IVE = IDecl->ivar_end();
2558 } else {
2559 IVI = CDecl->ivar_begin();
2560 IVE = CDecl->ivar_end();
2561 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002562 Result += "\t,{{\"";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002563 Result += (*IVI)->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002564 Result += "\", \"";
2565 std::string StrEncoding;
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002566 Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding,
2567 EncodingRecordTypes);
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002568 Result += StrEncoding;
2569 Result += "\", ";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002570 SynthesizeIvarOffsetComputation(IDecl, *IVI, Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002571 Result += "}\n";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002572 for (++IVI; IVI != IVE; ++IVI) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002573 Result += "\t ,{\"";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002574 Result += (*IVI)->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002575 Result += "\", \"";
2576 std::string StrEncoding;
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002577 Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding,
2578 EncodingRecordTypes);
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002579 Result += StrEncoding;
2580 Result += "\", ";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002581 SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002582 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002583 }
2584
2585 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002586 }
2587
2588 // Build _objc_method_list for class's instance methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002589 RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002590 true, "", IDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002591
2592 // Build _objc_method_list for class's class methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002593 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002594 false, "", IDecl->getName(), Result);
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002595
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002596 // Protocols referenced in class declaration?
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002597 RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002598 CDecl->getNumIntfRefProtocols(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002599 "CLASS", CDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002600
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002601
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002602 // Declaration of class/meta-class metadata
2603 /* struct _objc_class {
2604 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002605 const char *super_class_name;
2606 char *name;
2607 long version;
2608 long info;
2609 long instance_size;
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002610 struct _objc_ivar_list *ivars;
2611 struct _objc_method_list *methods;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002612 struct objc_cache *cache;
2613 struct objc_protocol_list *protocols;
2614 const char *ivar_layout;
2615 struct _objc_class_ext *ext;
2616 };
2617 */
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002618 static bool objc_class = false;
2619 if (!objc_class) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002620 Result += "\nstruct _objc_class {\n";
2621 Result += "\tstruct _objc_class *isa;\n";
2622 Result += "\tconst char *super_class_name;\n";
2623 Result += "\tchar *name;\n";
2624 Result += "\tlong version;\n";
2625 Result += "\tlong info;\n";
2626 Result += "\tlong instance_size;\n";
2627 Result += "\tstruct _objc_ivar_list *ivars;\n";
2628 Result += "\tstruct _objc_method_list *methods;\n";
2629 Result += "\tstruct objc_cache *cache;\n";
2630 Result += "\tstruct _objc_protocol_list *protocols;\n";
2631 Result += "\tconst char *ivar_layout;\n";
2632 Result += "\tstruct _objc_class_ext *ext;\n";
2633 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002634 objc_class = true;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002635 }
2636
2637 // Meta-class metadata generation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002638 ObjCInterfaceDecl *RootClass = 0;
2639 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002640 while (SuperClass) {
2641 RootClass = SuperClass;
2642 SuperClass = SuperClass->getSuperClass();
2643 }
2644 SuperClass = CDecl->getSuperClass();
2645
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002646 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
2647 Result += CDecl->getName();
2648 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
2649 "{\n\t(struct _objc_class *)\"";
2650 Result += (RootClass ? RootClass->getName() : CDecl->getName());
2651 Result += "\"";
2652
2653 if (SuperClass) {
2654 Result += ", \"";
2655 Result += SuperClass->getName();
2656 Result += "\", \"";
2657 Result += CDecl->getName();
2658 Result += "\"";
2659 }
2660 else {
2661 Result += ", 0, \"";
2662 Result += CDecl->getName();
2663 Result += "\"";
2664 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002665 // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it.
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002666 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002667 Result += ", 0,2, sizeof(struct _objc_class), 0";
Steve Naroffb26d7132007-12-05 21:49:40 +00002668 if (IDecl->getNumClassMethods() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002669 Result += "\n\t, &_OBJC_CLASS_METHODS_";
Steve Naroffb26d7132007-12-05 21:49:40 +00002670 Result += IDecl->getName();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002671 Result += "\n";
2672 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002673 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002674 Result += ", 0\n";
2675 if (CDecl->getNumIntfRefProtocols() > 0) {
2676 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
2677 Result += CDecl->getName();
2678 Result += ",0,0\n";
2679 }
Fariborz Jahanian454cb012007-10-24 20:54:23 +00002680 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002681 Result += "\t,0,0,0,0\n";
2682 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002683
2684 // class metadata generation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002685 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
2686 Result += CDecl->getName();
2687 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
2688 "{\n\t&_OBJC_METACLASS_";
2689 Result += CDecl->getName();
2690 if (SuperClass) {
2691 Result += ", \"";
2692 Result += SuperClass->getName();
2693 Result += "\", \"";
2694 Result += CDecl->getName();
2695 Result += "\"";
2696 }
2697 else {
2698 Result += ", 0, \"";
2699 Result += CDecl->getName();
2700 Result += "\"";
2701 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002702 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002703 Result += ", 0,1";
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002704 if (!ObjCSynthesizedStructs.count(CDecl))
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002705 Result += ",0";
2706 else {
2707 // class has size. Must synthesize its size.
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00002708 Result += ",sizeof(struct ";
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002709 Result += CDecl->getName();
2710 Result += ")";
2711 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002712 if (NumIvars > 0) {
2713 Result += ", &_OBJC_INSTANCE_VARIABLES_";
2714 Result += CDecl->getName();
2715 Result += "\n\t";
2716 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002717 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002718 Result += ",0";
2719 if (IDecl->getNumInstanceMethods() > 0) {
2720 Result += ", &_OBJC_INSTANCE_METHODS_";
2721 Result += CDecl->getName();
2722 Result += ", 0\n\t";
2723 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002724 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002725 Result += ",0,0";
2726 if (CDecl->getNumIntfRefProtocols() > 0) {
2727 Result += ", &_OBJC_CLASS_PROTOCOLS_";
2728 Result += CDecl->getName();
2729 Result += ", 0,0\n";
2730 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002731 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002732 Result += ",0,0,0\n";
2733 Result += "};\n";
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002734}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002735
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002736/// RewriteImplementations - This routine rewrites all method implementations
2737/// and emits meta-data.
2738
2739void RewriteTest::RewriteImplementations(std::string &Result) {
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002740 int ClsDefCount = ClassImplementation.size();
2741 int CatDefCount = CategoryImplementation.size();
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002742
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002743 if (ClsDefCount == 0 && CatDefCount == 0)
2744 return;
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002745 // Rewrite implemented methods
2746 for (int i = 0; i < ClsDefCount; i++)
2747 RewriteImplementationDecl(ClassImplementation[i]);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002748
Fariborz Jahanian66d6b292007-11-13 20:04:28 +00002749 for (int i = 0; i < CatDefCount; i++)
2750 RewriteImplementationDecl(CategoryImplementation[i]);
2751
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002752 // This is needed for use of offsetof
2753 Result += "#include <stddef.h>\n";
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002754
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002755 // For each implemented class, write out all its meta data.
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002756 for (int i = 0; i < ClsDefCount; i++)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002757 RewriteObjCClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002758
2759 // For each implemented category, write out all its meta data.
2760 for (int i = 0; i < CatDefCount; i++)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002761 RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002762
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002763 // Write objc_symtab metadata
2764 /*
2765 struct _objc_symtab
2766 {
2767 long sel_ref_cnt;
2768 SEL *refs;
2769 short cls_def_cnt;
2770 short cat_def_cnt;
2771 void *defs[cls_def_cnt + cat_def_cnt];
2772 };
2773 */
2774
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002775 Result += "\nstruct _objc_symtab {\n";
2776 Result += "\tlong sel_ref_cnt;\n";
2777 Result += "\tSEL *refs;\n";
2778 Result += "\tshort cls_def_cnt;\n";
2779 Result += "\tshort cat_def_cnt;\n";
2780 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
2781 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002782
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002783 Result += "static struct _objc_symtab "
2784 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
2785 Result += "\t0, 0, " + utostr(ClsDefCount)
2786 + ", " + utostr(CatDefCount) + "\n";
2787 for (int i = 0; i < ClsDefCount; i++) {
2788 Result += "\t,&_OBJC_CLASS_";
2789 Result += ClassImplementation[i]->getName();
2790 Result += "\n";
2791 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002792
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002793 for (int i = 0; i < CatDefCount; i++) {
2794 Result += "\t,&_OBJC_CATEGORY_";
2795 Result += CategoryImplementation[i]->getClassInterface()->getName();
2796 Result += "_";
2797 Result += CategoryImplementation[i]->getName();
2798 Result += "\n";
2799 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002800
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002801 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002802
2803 // Write objc_module metadata
2804
2805 /*
2806 struct _objc_module {
2807 long version;
2808 long size;
2809 const char *name;
2810 struct _objc_symtab *symtab;
2811 }
2812 */
2813
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002814 Result += "\nstruct _objc_module {\n";
2815 Result += "\tlong version;\n";
2816 Result += "\tlong size;\n";
2817 Result += "\tconst char *name;\n";
2818 Result += "\tstruct _objc_symtab *symtab;\n";
2819 Result += "};\n\n";
2820 Result += "static struct _objc_module "
2821 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002822 Result += "\t" + utostr(OBJC_ABI_VERSION) +
2823 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002824 Result += "};\n\n";
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002825
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002826}
Chris Lattner311ff022007-10-16 22:36:42 +00002827