blob: d9af1f97438ae36ab11ce2b3cdb650594db188bf [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 Naroff874e2322007-11-15 10:28:18 +000025#include <sstream>
Chris Lattner77cd2a02007-10-11 00:43:27 +000026using namespace clang;
Chris Lattner158ecb92007-10-25 17:07:24 +000027using llvm::utostr;
Chris Lattner77cd2a02007-10-11 00:43:27 +000028
Chris Lattner77cd2a02007-10-11 00:43:27 +000029namespace {
Chris Lattner8a12c272007-10-11 18:38:32 +000030 class RewriteTest : public ASTConsumer {
Chris Lattner2c64b7b2007-10-16 21:07:07 +000031 Rewriter Rewrite;
Chris Lattnere365c502007-11-30 22:25:36 +000032 Diagnostic &Diags;
Chris Lattner01c57482007-10-17 22:35:30 +000033 ASTContext *Context;
Chris Lattner77cd2a02007-10-11 00:43:27 +000034 SourceManager *SM;
Chris Lattner8a12c272007-10-11 18:38:32 +000035 unsigned MainFileID;
Chris Lattner26de4652007-12-02 01:13:47 +000036 const char *MainFileStart, *MainFileEnd;
Chris Lattner2c64b7b2007-10-16 21:07:07 +000037 SourceLocation LastIncLoc;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000038 llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation;
39 llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation;
40 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs;
41 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls;
42 llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +000043 llvm::SmallVector<Stmt *, 32> Stmts;
44 llvm::SmallVector<int, 8> ObjCBcLabelNo;
Steve Naroffebf2b562007-10-23 23:50:29 +000045
46 FunctionDecl *MsgSendFunctionDecl;
Steve Naroff874e2322007-11-15 10:28:18 +000047 FunctionDecl *MsgSendSuperFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +000048 FunctionDecl *MsgSendStretFunctionDecl;
49 FunctionDecl *MsgSendSuperStretFunctionDecl;
Fariborz Jahanianacb49772007-12-03 21:26:48 +000050 FunctionDecl *MsgSendFpretFunctionDecl;
Steve Naroffebf2b562007-10-23 23:50:29 +000051 FunctionDecl *GetClassFunctionDecl;
Steve Naroff9bcb5fc2007-12-07 03:50:46 +000052 FunctionDecl *GetMetaClassFunctionDecl;
Steve Naroff934f2762007-10-24 22:48:43 +000053 FunctionDecl *SelGetUidFunctionDecl;
Steve Naroff96984642007-11-08 14:30:50 +000054 FunctionDecl *CFStringFunctionDecl;
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +000055 FunctionDecl *GetProtocolFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +000056
Steve Naroffbeaf2992007-11-03 11:27:19 +000057 // ObjC string constant support.
58 FileVarDecl *ConstantStringClassReference;
59 RecordDecl *NSStringRecord;
Steve Naroffab972d32007-11-04 22:37:50 +000060
Fariborz Jahanianb586cce2008-01-16 00:09:11 +000061 // ObjC foreach break/continue generation support.
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +000062 int BcLabelCount;
63
Steve Naroff874e2322007-11-15 10:28:18 +000064 // Needed for super.
Ted Kremeneka526c5c2008-01-07 19:49:32 +000065 ObjCMethodDecl *CurMethodDecl;
Steve Naroff874e2322007-11-15 10:28:18 +000066 RecordDecl *SuperStructDecl;
67
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +000068 // Needed for header files being rewritten
69 bool IsHeader;
70
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +000071 static const int OBJC_ABI_VERSION =7 ;
Chris Lattner77cd2a02007-10-11 00:43:27 +000072 public:
Ted Kremenek95041a22007-12-19 22:51:13 +000073 void Initialize(ASTContext &context) {
Chris Lattner01c57482007-10-17 22:35:30 +000074 Context = &context;
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000075 SM = &Context->getSourceManager();
Steve Naroffebf2b562007-10-23 23:50:29 +000076 MsgSendFunctionDecl = 0;
Steve Naroff874e2322007-11-15 10:28:18 +000077 MsgSendSuperFunctionDecl = 0;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +000078 MsgSendStretFunctionDecl = 0;
79 MsgSendSuperStretFunctionDecl = 0;
Fariborz Jahanianacb49772007-12-03 21:26:48 +000080 MsgSendFpretFunctionDecl = 0;
Steve Naroffc0006092007-10-24 01:09:48 +000081 GetClassFunctionDecl = 0;
Steve Naroff9bcb5fc2007-12-07 03:50:46 +000082 GetMetaClassFunctionDecl = 0;
Steve Naroff934f2762007-10-24 22:48:43 +000083 SelGetUidFunctionDecl = 0;
Steve Naroff96984642007-11-08 14:30:50 +000084 CFStringFunctionDecl = 0;
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +000085 GetProtocolFunctionDecl = 0;
Steve Naroffbeaf2992007-11-03 11:27:19 +000086 ConstantStringClassReference = 0;
87 NSStringRecord = 0;
Steve Naroff874e2322007-11-15 10:28:18 +000088 CurMethodDecl = 0;
89 SuperStructDecl = 0;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +000090 BcLabelCount = 0;
Steve Naroff874e2322007-11-15 10:28:18 +000091
Chris Lattner26de4652007-12-02 01:13:47 +000092 // Get the ID and start/end of the main file.
Ted Kremenek95041a22007-12-19 22:51:13 +000093 MainFileID = SM->getMainFileID();
Chris Lattner26de4652007-12-02 01:13:47 +000094 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
95 MainFileStart = MainBuf->getBufferStart();
96 MainFileEnd = MainBuf->getBufferEnd();
97
98
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000099 Rewrite.setSourceMgr(Context->getSourceManager());
Steve Naroffe3abbf52007-11-05 14:55:35 +0000100 // declaring objc_selector outside the parameter list removes a silly
101 // scope related warning...
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +0000102 const char *s = "#pragma once\n"
103 "struct objc_selector; struct objc_class;\n"
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000104 "#ifndef OBJC_SUPER\n"
105 "struct objc_super { struct objc_object *o; "
106 "struct objc_object *superClass; };\n"
107 "#define OBJC_SUPER\n"
108 "#endif\n"
109 "#ifndef _REWRITER_typedef_Protocol\n"
110 "typedef struct objc_object Protocol;\n"
111 "#define _REWRITER_typedef_Protocol\n"
112 "#endif\n"
Steve Naroffe3abbf52007-11-05 14:55:35 +0000113 "extern struct objc_object *objc_msgSend"
Steve Naroffab972d32007-11-04 22:37:50 +0000114 "(struct objc_object *, struct objc_selector *, ...);\n"
Steve Naroff874e2322007-11-15 10:28:18 +0000115 "extern struct objc_object *objc_msgSendSuper"
116 "(struct objc_super *, struct objc_selector *, ...);\n"
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +0000117 "extern struct objc_object *objc_msgSend_stret"
118 "(struct objc_object *, struct objc_selector *, ...);\n"
119 "extern struct objc_object *objc_msgSendSuper_stret"
120 "(struct objc_super *, struct objc_selector *, ...);\n"
Fariborz Jahanianacb49772007-12-03 21:26:48 +0000121 "extern struct objc_object *objc_msgSend_fpret"
122 "(struct objc_object *, struct objc_selector *, ...);\n"
Steve Naroffab972d32007-11-04 22:37:50 +0000123 "extern struct objc_object *objc_getClass"
Steve Naroff21867b12007-11-07 18:43:40 +0000124 "(const char *);\n"
Steve Naroff9bcb5fc2007-12-07 03:50:46 +0000125 "extern struct objc_object *objc_getMetaClass"
126 "(const char *);\n"
Steve Naroff21867b12007-11-07 18:43:40 +0000127 "extern void objc_exception_throw(struct objc_object *);\n"
128 "extern void objc_exception_try_enter(void *);\n"
129 "extern void objc_exception_try_exit(void *);\n"
130 "extern struct objc_object *objc_exception_extract(void *);\n"
131 "extern int objc_exception_match"
Fariborz Jahanian95673922007-11-14 22:26:25 +0000132 "(struct objc_class *, struct objc_object *, ...);\n"
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000133 "extern Protocol *objc_getProtocol(const char *);\n"
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000134 "#include <objc/objc.h>\n"
Fariborz Jahanian88007422008-01-10 23:04:06 +0000135 "#ifndef __FASTENUMERATIONSTATE\n"
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000136 "struct __objcFastEnumerationState {\n\t"
137 "unsigned long state;\n\t"
138 "id *itemsPtr;\n\t"
139 "unsigned long *mutationsPtr;\n\t"
Fariborz Jahanian88007422008-01-10 23:04:06 +0000140 "unsigned long extra[5];\n};\n"
141 "#define __FASTENUMERATIONSTATE\n"
142 "#endif\n";
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +0000143 if (IsHeader) {
144 // insert the whole string when rewriting a header file
145 Rewrite.InsertText(SourceLocation::getFileLoc(MainFileID, 0),
146 s, strlen(s));
147 }
148 else {
149 // Not rewriting header, exclude the #pragma once pragma
150 const char *p = s + strlen("#pragma once\n");
151 Rewrite.InsertText(SourceLocation::getFileLoc(MainFileID, 0),
152 p, strlen(p));
153 }
Chris Lattner77cd2a02007-10-11 00:43:27 +0000154 }
Chris Lattner8a12c272007-10-11 18:38:32 +0000155
Chris Lattnerf04da132007-10-24 17:06:59 +0000156 // Top Level Driver code.
157 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000158 void HandleDeclInMainFile(Decl *D);
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +0000159 RewriteTest(bool isHeader, Diagnostic &D) : Diags(D) {IsHeader = isHeader;}
Chris Lattnerf04da132007-10-24 17:06:59 +0000160 ~RewriteTest();
161
162 // Syntactic Rewriting.
Steve Naroffab972d32007-11-04 22:37:50 +0000163 void RewritePrologue(SourceLocation Loc);
Fariborz Jahanian452b8992008-01-19 00:30:35 +0000164 void RewriteInclude();
Chris Lattnerf04da132007-10-24 17:06:59 +0000165 void RewriteTabs();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000166 void RewriteForwardClassDecl(ObjCClassDecl *Dcl);
167 void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000168 void RewriteImplementationDecl(NamedDecl *Dcl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000169 void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr);
170 void RewriteCategoryDecl(ObjCCategoryDecl *Dcl);
171 void RewriteProtocolDecl(ObjCProtocolDecl *Dcl);
172 void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl);
173 void RewriteMethodDeclaration(ObjCMethodDecl *Method);
174 void RewriteProperties(int nProperties, ObjCPropertyDecl **Properties);
Steve Naroff09b266e2007-10-30 23:14:51 +0000175 void RewriteFunctionDecl(FunctionDecl *FD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000176 void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
Steve Naroffd5255f52007-11-01 13:24:47 +0000177 bool needToScanForQualifiers(QualType T);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000178 ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr);
Steve Naroff874e2322007-11-15 10:28:18 +0000179 QualType getSuperStructType();
Chris Lattner311ff022007-10-16 22:36:42 +0000180
Chris Lattnerf04da132007-10-24 17:06:59 +0000181 // Expression Rewriting.
Steve Narofff3473a72007-11-09 15:20:18 +0000182 Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
Chris Lattnere64b7772007-10-24 16:57:36 +0000183 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
Steve Naroff7e3411b2007-11-15 02:58:25 +0000184 Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV);
Steve Naroffb42f8412007-11-05 14:50:49 +0000185 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
Chris Lattnere64b7772007-10-24 16:57:36 +0000186 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000187 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000188 Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000189 Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
190 Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S);
191 Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S);
192 Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S);
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000193 Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S);
Steve Naroff934f2762007-10-24 22:48:43 +0000194 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
195 Expr **args, unsigned nargs);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000196 Stmt *SynthMessageExpr(ObjCMessageExpr *Exp);
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000197 Stmt *RewriteBreakStmt(BreakStmt *S);
198 Stmt *RewriteContinueStmt(ContinueStmt *S);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000199 void SynthCountByEnumWithState(std::string &buf);
200
Steve Naroff09b266e2007-10-30 23:14:51 +0000201 void SynthMsgSendFunctionDecl();
Steve Naroff874e2322007-11-15 10:28:18 +0000202 void SynthMsgSendSuperFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +0000203 void SynthMsgSendStretFunctionDecl();
Fariborz Jahanianacb49772007-12-03 21:26:48 +0000204 void SynthMsgSendFpretFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +0000205 void SynthMsgSendSuperStretFunctionDecl();
Steve Naroff09b266e2007-10-30 23:14:51 +0000206 void SynthGetClassFunctionDecl();
Steve Naroff9bcb5fc2007-12-07 03:50:46 +0000207 void SynthGetMetaClassFunctionDecl();
Steve Naroff96984642007-11-08 14:30:50 +0000208 void SynthCFStringFunctionDecl();
Fariborz Jahaniana70711b2007-12-04 21:47:40 +0000209 void SynthSelGetUidFunctionDecl();
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000210 void SynthGetProtocolFunctionDecl();
Steve Naroff96984642007-11-08 14:30:50 +0000211
Chris Lattnerf04da132007-10-24 17:06:59 +0000212 // Metadata emission.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000213 void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000214 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000215
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000216 void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000217 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000218
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000219 typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator;
220 void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000221 instmeth_iterator MethodEnd,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +0000222 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000223 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +0000224 const char *ClassName,
225 std::string &Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000226
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227 void RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +0000228 int NumProtocols,
229 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +0000230 const char *ClassName,
231 std::string &Result);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000232 void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000233 std::string &Result);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000234 void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
235 ObjCIvarDecl *ivar,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000236 std::string &Result);
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +0000237 void RewriteImplementations(std::string &Result);
Chris Lattner77cd2a02007-10-11 00:43:27 +0000238 };
239}
240
Fariborz Jahanianb4b2f0c2008-01-18 01:15:54 +0000241static bool IsHeaderFile(const std::string &Filename) {
242 std::string::size_type DotPos = Filename.rfind('.');
243
244 if (DotPos == std::string::npos) {
245 // no file extension
246 return false;
247 }
248
249 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
250 // C header: .h
251 // C++ header: .hh or .H;
252 return Ext == "h" || Ext == "hh" || Ext == "H";
253}
254
255ASTConsumer *clang::CreateCodeRewriterTest(const std::string& InFile,
256 Diagnostic &Diags) {
257 return new RewriteTest(IsHeaderFile(InFile), Diags);
Chris Lattnere365c502007-11-30 22:25:36 +0000258}
Chris Lattner77cd2a02007-10-11 00:43:27 +0000259
Chris Lattnerf04da132007-10-24 17:06:59 +0000260//===----------------------------------------------------------------------===//
261// Top Level Driver Code
262//===----------------------------------------------------------------------===//
263
Chris Lattner8a12c272007-10-11 18:38:32 +0000264void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000265 // Two cases: either the decl could be in the main file, or it could be in a
266 // #included file. If the former, rewrite it now. If the later, check to see
267 // if we rewrote the #include/#import.
268 SourceLocation Loc = D->getLocation();
269 Loc = SM->getLogicalLoc(Loc);
270
271 // If this is for a builtin, ignore it.
272 if (Loc.isInvalid()) return;
273
Steve Naroffebf2b562007-10-23 23:50:29 +0000274 // Look for built-in declarations that we need to refer during the rewrite.
275 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Steve Naroff09b266e2007-10-30 23:14:51 +0000276 RewriteFunctionDecl(FD);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000277 } else if (FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) {
278 // declared in <Foundation/NSString.h>
279 if (strcmp(FVD->getName(), "_NSConstantStringClassReference") == 0) {
280 ConstantStringClassReference = FVD;
281 return;
282 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000283 } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) {
Steve Naroffbef11852007-10-26 20:53:56 +0000284 RewriteInterfaceDecl(MD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000285 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
Steve Naroff423cb562007-10-30 13:30:57 +0000286 RewriteCategoryDecl(CD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000287 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Steve Naroff752d6ef2007-10-30 16:42:30 +0000288 RewriteProtocolDecl(PD);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000289 } else if (ObjCForwardProtocolDecl *FP =
290 dyn_cast<ObjCForwardProtocolDecl>(D)){
Fariborz Jahaniand175ddf2007-11-14 00:42:16 +0000291 RewriteForwardProtocolDecl(FP);
Steve Naroffebf2b562007-10-23 23:50:29 +0000292 }
Chris Lattnerf04da132007-10-24 17:06:59 +0000293 // If we have a decl in the main file, see if we should rewrite it.
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000294 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
295 return HandleDeclInMainFile(D);
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000296}
297
Chris Lattnerf04da132007-10-24 17:06:59 +0000298/// HandleDeclInMainFile - This is called for each top-level decl defined in the
299/// main file of the input.
300void RewriteTest::HandleDeclInMainFile(Decl *D) {
301 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
302 if (Stmt *Body = FD->getBody())
Steve Narofff3473a72007-11-09 15:20:18 +0000303 FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
Steve Naroff71c0a952007-11-13 23:01:27 +0000304
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000305 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Steve Naroff874e2322007-11-15 10:28:18 +0000306 if (Stmt *Body = MD->getBody()) {
307 //Body->dump();
308 CurMethodDecl = MD;
Steve Naroff71c0a952007-11-13 23:01:27 +0000309 MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
Steve Naroff874e2322007-11-15 10:28:18 +0000310 CurMethodDecl = 0;
311 }
Steve Naroff71c0a952007-11-13 23:01:27 +0000312 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000313 if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D))
Chris Lattnerf04da132007-10-24 17:06:59 +0000314 ClassImplementation.push_back(CI);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000315 else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D))
Chris Lattnerf04da132007-10-24 17:06:59 +0000316 CategoryImplementation.push_back(CI);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000317 else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D))
Chris Lattnerf04da132007-10-24 17:06:59 +0000318 RewriteForwardClassDecl(CD);
Steve Narofff3473a72007-11-09 15:20:18 +0000319 else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000320 RewriteObjCQualifiedInterfaceTypes(VD);
Steve Narofff3473a72007-11-09 15:20:18 +0000321 if (VD->getInit())
322 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
323 }
Chris Lattnerf04da132007-10-24 17:06:59 +0000324 // Nothing yet.
325}
326
327RewriteTest::~RewriteTest() {
328 // Get the top-level buffer that this corresponds to.
Chris Lattner74a0c772007-11-08 04:27:23 +0000329
330 // Rewrite tabs if we care.
331 //RewriteTabs();
Chris Lattnerf04da132007-10-24 17:06:59 +0000332
Fariborz Jahanian452b8992008-01-19 00:30:35 +0000333 RewriteInclude();
334
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000335 // Rewrite Objective-c meta data*
336 std::string ResultStr;
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +0000337 RewriteImplementations(ResultStr);
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000338
Chris Lattnerf04da132007-10-24 17:06:59 +0000339 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
340 // we are done.
341 if (const RewriteBuffer *RewriteBuf =
342 Rewrite.getRewriteBufferFor(MainFileID)) {
Steve Naroffbeaf2992007-11-03 11:27:19 +0000343 //printf("Changed:\n");
Chris Lattnerf04da132007-10-24 17:06:59 +0000344 std::string S(RewriteBuf->begin(), RewriteBuf->end());
345 printf("%s\n", S.c_str());
346 } else {
347 printf("No changes\n");
348 }
Fariborz Jahanian4402d812007-11-07 18:40:28 +0000349 // Emit metadata.
350 printf("%s", ResultStr.c_str());
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000351}
352
Chris Lattnerf04da132007-10-24 17:06:59 +0000353//===----------------------------------------------------------------------===//
354// Syntactic (non-AST) Rewriting Code
355//===----------------------------------------------------------------------===//
356
Fariborz Jahanian452b8992008-01-19 00:30:35 +0000357void RewriteTest::RewriteInclude() {
358 SourceLocation LocStart = SourceLocation::getFileLoc(MainFileID, 0);
359 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
360 const char *MainBufStart = MainBuf.first;
361 const char *MainBufEnd = MainBuf.second;
362 size_t ImportLen = strlen("import");
363 size_t IncludeLen = strlen("include");
364
365 // Loop over the whole file, looking for tabs.
366 for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) {
367 if (*BufPtr == '#') {
368 if (++BufPtr == MainBufEnd)
369 return;
370 while (*BufPtr == ' ' || *BufPtr == '\t')
371 if (++BufPtr == MainBufEnd)
372 return;
373 if (!strncmp(BufPtr, "import", ImportLen)) {
374 // replace import with include
375 SourceLocation ImportLoc =
376 LocStart.getFileLocWithOffset(BufPtr-MainBufStart);
377 Rewrite.ReplaceText(ImportLoc, ImportLen, "include", IncludeLen);
378 BufPtr += ImportLen;
379 }
380 }
381 }
Chris Lattner2c64b7b2007-10-16 21:07:07 +0000382}
383
Chris Lattnerf04da132007-10-24 17:06:59 +0000384void RewriteTest::RewriteTabs() {
385 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
386 const char *MainBufStart = MainBuf.first;
387 const char *MainBufEnd = MainBuf.second;
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +0000388
Chris Lattnerf04da132007-10-24 17:06:59 +0000389 // Loop over the whole file, looking for tabs.
390 for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
391 if (*BufPtr != '\t')
392 continue;
393
394 // Okay, we found a tab. This tab will turn into at least one character,
395 // but it depends on which 'virtual column' it is in. Compute that now.
396 unsigned VCol = 0;
397 while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
398 BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
399 ++VCol;
400
401 // Okay, now that we know the virtual column, we know how many spaces to
402 // insert. We assume 8-character tab-stops.
403 unsigned Spaces = 8-(VCol & 7);
404
405 // Get the location of the tab.
406 SourceLocation TabLoc =
407 SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
408
409 // Rewrite the single tab character into a sequence of spaces.
410 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
411 }
Chris Lattner8a12c272007-10-11 18:38:32 +0000412}
413
414
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000415void RewriteTest::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) {
Chris Lattnerf04da132007-10-24 17:06:59 +0000416 int numDecls = ClassDecl->getNumForwardDecls();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000417 ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
Chris Lattnerf04da132007-10-24 17:06:59 +0000418
419 // Get the start location and compute the semi location.
420 SourceLocation startLoc = ClassDecl->getLocation();
421 const char *startBuf = SM->getCharacterData(startLoc);
422 const char *semiPtr = strchr(startBuf, ';');
423
424 // Translate to typedef's that forward reference structs with the same name
425 // as the class. As a convenience, we include the original declaration
426 // as a comment.
427 std::string typedefString;
428 typedefString += "// ";
Steve Naroff934f2762007-10-24 22:48:43 +0000429 typedefString.append(startBuf, semiPtr-startBuf+1);
430 typedefString += "\n";
431 for (int i = 0; i < numDecls; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000432 ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i];
Steve Naroff32174822007-11-09 12:50:28 +0000433 typedefString += "#ifndef _REWRITER_typedef_";
434 typedefString += ForwardDecl->getName();
435 typedefString += "\n";
436 typedefString += "#define _REWRITER_typedef_";
437 typedefString += ForwardDecl->getName();
438 typedefString += "\n";
Steve Naroff352336b2007-11-05 14:36:37 +0000439 typedefString += "typedef struct objc_object ";
Steve Naroff934f2762007-10-24 22:48:43 +0000440 typedefString += ForwardDecl->getName();
Steve Naroff32174822007-11-09 12:50:28 +0000441 typedefString += ";\n#endif\n";
Steve Naroff934f2762007-10-24 22:48:43 +0000442 }
443
444 // Replace the @class with typedefs corresponding to the classes.
445 Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1,
446 typedefString.c_str(), typedefString.size());
Chris Lattnerf04da132007-10-24 17:06:59 +0000447}
448
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449void RewriteTest::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000450 SourceLocation LocStart = Method->getLocStart();
451 SourceLocation LocEnd = Method->getLocEnd();
Steve Naroff423cb562007-10-30 13:30:57 +0000452
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000453 if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) {
454 Rewrite.InsertText(LocStart, "/* ", 3);
455 Rewrite.ReplaceText(LocEnd, 1, ";*/ ", 4);
456 } else {
457 Rewrite.InsertText(LocStart, "// ", 3);
Steve Naroff423cb562007-10-30 13:30:57 +0000458 }
459}
460
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000461void RewriteTest::RewriteProperties(int nProperties, ObjCPropertyDecl **Properties)
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000462{
463 for (int i = 0; i < nProperties; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000464 ObjCPropertyDecl *Property = Properties[i];
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000465 SourceLocation Loc = Property->getLocation();
466
467 Rewrite.ReplaceText(Loc, 0, "// ", 3);
468
469 // FIXME: handle properties that are declared across multiple lines.
470 }
471}
472
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000473void RewriteTest::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
Steve Naroff423cb562007-10-30 13:30:57 +0000474 SourceLocation LocStart = CatDecl->getLocStart();
475
476 // FIXME: handle category headers that are declared across multiple lines.
477 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
478
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000479 for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000480 E = CatDecl->instmeth_end(); I != E; ++I)
481 RewriteMethodDeclaration(*I);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000482 for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000483 E = CatDecl->classmeth_end(); I != E; ++I)
484 RewriteMethodDeclaration(*I);
485
Steve Naroff423cb562007-10-30 13:30:57 +0000486 // Lastly, comment out the @end.
487 Rewrite.ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
488}
489
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000490void RewriteTest::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000491 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000492
Steve Naroff752d6ef2007-10-30 16:42:30 +0000493 SourceLocation LocStart = PDecl->getLocStart();
494
495 // FIXME: handle protocol headers that are declared across multiple lines.
496 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
497
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000498 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000499 E = PDecl->instmeth_end(); I != E; ++I)
500 RewriteMethodDeclaration(*I);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000501 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000502 E = PDecl->classmeth_end(); I != E; ++I)
503 RewriteMethodDeclaration(*I);
504
Steve Naroff752d6ef2007-10-30 16:42:30 +0000505 // Lastly, comment out the @end.
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000506 SourceLocation LocEnd = PDecl->getAtEndLoc();
507 Rewrite.ReplaceText(LocEnd, 0, "// ", 3);
Steve Naroff8cc764c2007-11-14 15:03:57 +0000508
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000509 // Must comment out @optional/@required
510 const char *startBuf = SM->getCharacterData(LocStart);
511 const char *endBuf = SM->getCharacterData(LocEnd);
512 for (const char *p = startBuf; p < endBuf; p++) {
513 if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
514 std::string CommentedOptional = "/* @optional */";
Steve Naroff8cc764c2007-11-14 15:03:57 +0000515 SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000516 Rewrite.ReplaceText(OptionalLoc, strlen("@optional"),
517 CommentedOptional.c_str(), CommentedOptional.size());
518
519 }
520 else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
521 std::string CommentedRequired = "/* @required */";
Steve Naroff8cc764c2007-11-14 15:03:57 +0000522 SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
Fariborz Jahanianb82b3ea2007-11-14 01:37:46 +0000523 Rewrite.ReplaceText(OptionalLoc, strlen("@required"),
524 CommentedRequired.c_str(), CommentedRequired.size());
525
526 }
527 }
Steve Naroff752d6ef2007-10-30 16:42:30 +0000528}
529
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000530void RewriteTest::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) {
Fariborz Jahaniand175ddf2007-11-14 00:42:16 +0000531 SourceLocation LocStart = PDecl->getLocation();
Steve Naroffb7fa9922007-11-14 03:37:28 +0000532 if (LocStart.isInvalid())
533 assert(false && "Invalid SourceLocation");
Fariborz Jahaniand175ddf2007-11-14 00:42:16 +0000534 // FIXME: handle forward protocol that are declared across multiple lines.
535 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
536}
537
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000538void RewriteTest::RewriteObjCMethodDecl(ObjCMethodDecl *OMD,
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000539 std::string &ResultStr) {
540 ResultStr += "\nstatic ";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000541 if (OMD->getResultType()->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000542 ResultStr += "id";
543 else
544 ResultStr += OMD->getResultType().getAsString();
Fariborz Jahanian531a1ea2008-01-10 01:39:52 +0000545 ResultStr += " ";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000546
547 // Unique method name
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000548 std::string NameStr;
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000549
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000550 if (OMD->isInstance())
551 NameStr += "_I_";
552 else
553 NameStr += "_C_";
554
555 NameStr += OMD->getClassInterface()->getName();
556 NameStr += "_";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000557
558 NamedDecl *MethodContext = OMD->getMethodContext();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000559 if (ObjCCategoryImplDecl *CID =
560 dyn_cast<ObjCCategoryImplDecl>(MethodContext)) {
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000561 NameStr += CID->getName();
562 NameStr += "_";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000563 }
564 // Append selector names, replacing ':' with '_'
565 const char *selName = OMD->getSelector().getName().c_str();
566 if (!strchr(selName, ':'))
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000567 NameStr += OMD->getSelector().getName();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000568 else {
569 std::string selString = OMD->getSelector().getName();
570 int len = selString.size();
571 for (int i = 0; i < len; i++)
572 if (selString[i] == ':')
573 selString[i] = '_';
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000574 NameStr += selString;
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000575 }
Fariborz Jahanianb7908b52007-11-13 21:02:00 +0000576 // Remember this name for metadata emission
577 MethodInternalNames[OMD] = NameStr;
578 ResultStr += NameStr;
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000579
580 // Rewrite arguments
581 ResultStr += "(";
582
583 // invisible arguments
584 if (OMD->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585 QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface());
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000586 selfTy = Context->getPointerType(selfTy);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000587 if (ObjCSynthesizedStructs.count(OMD->getClassInterface()))
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000588 ResultStr += "struct ";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000589 ResultStr += selfTy.getAsString();
590 }
591 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 ResultStr += Context->getObjCIdType().getAsString();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000593
594 ResultStr += " self, ";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 ResultStr += Context->getObjCSelType().getAsString();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000596 ResultStr += " _cmd";
597
598 // Method arguments.
599 for (int i = 0; i < OMD->getNumParams(); i++) {
600 ParmVarDecl *PDecl = OMD->getParamDecl(i);
601 ResultStr += ", ";
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000602 if (PDecl->getType()->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000603 ResultStr += "id";
604 else
605 ResultStr += PDecl->getType().getAsString();
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000606 ResultStr += " ";
607 ResultStr += PDecl->getName();
608 }
Fariborz Jahanian531a1ea2008-01-10 01:39:52 +0000609 ResultStr += ") ";
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000610
611}
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000612void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000613 ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
614 ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000615
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000616 if (IMD)
617 Rewrite.InsertText(IMD->getLocStart(), "// ", 3);
618 else
619 Rewrite.InsertText(CID->getLocStart(), "// ", 3);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000620
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621 for (ObjCCategoryImplDecl::instmeth_iterator
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000622 I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
623 E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) {
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000624 std::string ResultStr;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 ObjCMethodDecl *OMD = *I;
626 RewriteObjCMethodDecl(OMD, ResultStr);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000627 SourceLocation LocStart = OMD->getLocStart();
628 SourceLocation LocEnd = OMD->getBody()->getLocStart();
629
630 const char *startBuf = SM->getCharacterData(LocStart);
631 const char *endBuf = SM->getCharacterData(LocEnd);
632 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
633 ResultStr.c_str(), ResultStr.size());
634 }
635
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000636 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000637 I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
638 E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) {
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000639 std::string ResultStr;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640 ObjCMethodDecl *OMD = *I;
641 RewriteObjCMethodDecl(OMD, ResultStr);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000642 SourceLocation LocStart = OMD->getLocStart();
643 SourceLocation LocEnd = OMD->getBody()->getLocStart();
644
645 const char *startBuf = SM->getCharacterData(LocStart);
646 const char *endBuf = SM->getCharacterData(LocEnd);
647 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
648 ResultStr.c_str(), ResultStr.size());
649 }
Fariborz Jahanian66d6b292007-11-13 20:04:28 +0000650 if (IMD)
651 Rewrite.InsertText(IMD->getLocEnd(), "// ", 3);
652 else
653 Rewrite.InsertText(CID->getLocEnd(), "// ", 3);
Fariborz Jahanian48a0b6a2007-11-13 18:44:14 +0000654}
655
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000656void RewriteTest::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
Steve Narofff908a872007-10-30 02:23:23 +0000657 std::string ResultStr;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000658 if (!ObjCForwardDecls.count(ClassDecl)) {
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000659 // we haven't seen a forward decl - generate a typedef.
Steve Naroff5086a8d2007-11-14 23:02:56 +0000660 ResultStr = "#ifndef _REWRITER_typedef_";
Steve Naroff32174822007-11-09 12:50:28 +0000661 ResultStr += ClassDecl->getName();
662 ResultStr += "\n";
663 ResultStr += "#define _REWRITER_typedef_";
664 ResultStr += ClassDecl->getName();
665 ResultStr += "\n";
Fariborz Jahanian87ce5d12007-12-03 22:25:42 +0000666 ResultStr += "typedef struct ";
667 ResultStr += ClassDecl->getName();
668 ResultStr += " ";
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000669 ResultStr += ClassDecl->getName();
Steve Naroff32174822007-11-09 12:50:28 +0000670 ResultStr += ";\n#endif\n";
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000671
672 // Mark this typedef as having been generated.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000673 ObjCForwardDecls.insert(ClassDecl);
Steve Naroff6c6a2db2007-11-01 03:35:41 +0000674 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000675 SynthesizeObjCInternalStruct(ClassDecl, ResultStr);
Steve Narofff908a872007-10-30 02:23:23 +0000676
Fariborz Jahanian957cf652007-11-07 00:09:37 +0000677 RewriteProperties(ClassDecl->getNumPropertyDecl(),
678 ClassDecl->getPropertyDecl());
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000679 for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000680 E = ClassDecl->instmeth_end(); I != E; ++I)
681 RewriteMethodDeclaration(*I);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682 for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(),
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000683 E = ClassDecl->classmeth_end(); I != E; ++I)
684 RewriteMethodDeclaration(*I);
685
Steve Naroff2feac5e2007-10-30 03:43:13 +0000686 // Lastly, comment out the @end.
687 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroffbef11852007-10-26 20:53:56 +0000688}
689
Steve Naroff7e3411b2007-11-15 02:58:25 +0000690Stmt *RewriteTest::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000691 ObjCIvarDecl *D = IV->getDecl();
Steve Naroff7e3411b2007-11-15 02:58:25 +0000692 if (IV->isFreeIvar()) {
693 Expr *Replacement = new MemberExpr(IV->getBase(), true, D,
694 IV->getLocation());
Steve Naroff5ca40202007-12-19 14:32:56 +0000695 if (Rewrite.ReplaceStmt(IV, Replacement)) {
696 // replacement failed.
Steve Naroff23ae0912007-12-19 19:16:49 +0000697 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
698 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroff5ca40202007-12-19 14:32:56 +0000699 SourceRange Range = IV->getSourceRange();
700 Diags.Report(Context->getFullLoc(IV->getLocation()), DiagID, 0, 0, &Range, 1);
Steve Naroff5ca40202007-12-19 14:32:56 +0000701 }
Steve Naroff7e3411b2007-11-15 02:58:25 +0000702 delete IV;
703 return Replacement;
Steve Naroffc2a689b2007-11-15 11:33:00 +0000704 } else {
705 if (CurMethodDecl) {
706 if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000707 ObjCInterfaceType *intT = dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
Steve Naroffc2a689b2007-11-15 11:33:00 +0000708 if (CurMethodDecl->getClassInterface() == intT->getDecl()) {
709 IdentifierInfo *II = intT->getDecl()->getIdentifier();
710 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
711 II, 0);
712 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
713
714 CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation());
715 // Don't forget the parens to enforce the proper binding.
716 ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), castExpr);
Steve Naroff5ca40202007-12-19 14:32:56 +0000717 if (Rewrite.ReplaceStmt(IV->getBase(), PE)) {
718 // replacement failed.
Steve Naroff23ae0912007-12-19 19:16:49 +0000719 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
720 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroff5ca40202007-12-19 14:32:56 +0000721 SourceRange Range = IV->getBase()->getSourceRange();
722 Diags.Report(Context->getFullLoc(IV->getBase()->getLocStart()), DiagID, 0, 0, &Range, 1);
Steve Naroff5ca40202007-12-19 14:32:56 +0000723 }
Steve Naroffc2a689b2007-11-15 11:33:00 +0000724 delete IV->getBase();
725 return PE;
726 }
727 }
728 }
Steve Naroff7e3411b2007-11-15 02:58:25 +0000729 return IV;
Steve Naroffc2a689b2007-11-15 11:33:00 +0000730 }
Steve Naroff7e3411b2007-11-15 02:58:25 +0000731}
732
Chris Lattnerf04da132007-10-24 17:06:59 +0000733//===----------------------------------------------------------------------===//
734// Function Body / Expression rewriting
735//===----------------------------------------------------------------------===//
736
Steve Narofff3473a72007-11-09 15:20:18 +0000737Stmt *RewriteTest::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000738 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
739 isa<DoStmt>(S) || isa<ForStmt>(S))
740 Stmts.push_back(S);
741 else if (isa<ObjCForCollectionStmt>(S)) {
742 Stmts.push_back(S);
743 ObjCBcLabelNo.push_back(++BcLabelCount);
744 }
745
Chris Lattner311ff022007-10-16 22:36:42 +0000746 // Otherwise, just rewrite all children.
747 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
748 CI != E; ++CI)
Steve Naroff75730982007-11-07 04:08:17 +0000749 if (*CI) {
Steve Narofff3473a72007-11-09 15:20:18 +0000750 Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI);
Steve Naroff75730982007-11-07 04:08:17 +0000751 if (newStmt)
752 *CI = newStmt;
753 }
Steve Naroffebf2b562007-10-23 23:50:29 +0000754
755 // Handle specific things.
756 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
757 return RewriteAtEncode(AtEncode);
Steve Naroff7e3411b2007-11-15 02:58:25 +0000758
759 if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S))
760 return RewriteObjCIvarRefExpr(IvarRefExpr);
Steve Naroffb42f8412007-11-05 14:50:49 +0000761
762 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
763 return RewriteAtSelector(AtSelector);
Steve Naroffbeaf2992007-11-03 11:27:19 +0000764
765 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
766 return RewriteObjCStringLiteral(AtString);
Steve Naroffebf2b562007-10-23 23:50:29 +0000767
Steve Naroff934f2762007-10-24 22:48:43 +0000768 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
769 // Before we rewrite it, put the original message expression in a comment.
770 SourceLocation startLoc = MessExpr->getLocStart();
771 SourceLocation endLoc = MessExpr->getLocEnd();
772
773 const char *startBuf = SM->getCharacterData(startLoc);
774 const char *endBuf = SM->getCharacterData(endLoc);
775
776 std::string messString;
777 messString += "// ";
778 messString.append(startBuf, endBuf-startBuf+1);
779 messString += "\n";
Steve Naroffbef11852007-10-26 20:53:56 +0000780
Steve Naroff934f2762007-10-24 22:48:43 +0000781 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
782 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
783 // Tried this, but it didn't work either...
Steve Naroff752d6ef2007-10-30 16:42:30 +0000784 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffebf2b562007-10-23 23:50:29 +0000785 return RewriteMessageExpr(MessExpr);
Steve Naroff934f2762007-10-24 22:48:43 +0000786 }
Fariborz Jahanian909f02a2007-11-05 17:47:33 +0000787
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000788 if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
789 return RewriteObjCTryStmt(StmtTry);
Steve Naroff2bd03922007-11-07 15:32:26 +0000790
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000791 if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
792 return RewriteObjCThrowStmt(StmtThrow);
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +0000793
794 if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
795 return RewriteObjCProtocolExpr(ProtocolExp);
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000796
797 if (ObjCForCollectionStmt *StmtForCollection =
798 dyn_cast<ObjCForCollectionStmt>(S))
799 return RewriteObjCForCollectionStmt(StmtForCollection);
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000800 if (BreakStmt *StmtBreakStmt =
801 dyn_cast<BreakStmt>(S))
802 return RewriteBreakStmt(StmtBreakStmt);
803 if (ContinueStmt *StmtContinueStmt =
804 dyn_cast<ContinueStmt>(S))
805 return RewriteContinueStmt(StmtContinueStmt);
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000806
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000807 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
808 isa<DoStmt>(S) || isa<ForStmt>(S)) {
809 assert(!Stmts.empty() && "Statement stack is empty");
810 assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
811 isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
812 && "Statement stack mismatch");
813 Stmts.pop_back();
814 }
Steve Naroff874e2322007-11-15 10:28:18 +0000815#if 0
816 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
817 CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation());
818 // Get the new text.
819 std::ostringstream Buf;
820 Replacement->printPretty(Buf);
821 const std::string &Str = Buf.str();
822
823 printf("CAST = %s\n", &Str[0]);
824 Rewrite.InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
825 delete S;
826 return Replacement;
827 }
828#endif
Chris Lattnere64b7772007-10-24 16:57:36 +0000829 // Return this stmt unmodified.
830 return S;
Chris Lattner311ff022007-10-16 22:36:42 +0000831}
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000832
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000833/// SynthCountByEnumWithState - To print:
834/// ((unsigned int (*)
835/// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
836/// (void *)objc_msgSend)((id)l_collection,
837/// sel_registerName(
838/// "countByEnumeratingWithState:objects:count:"),
839/// &enumState,
840/// (id *)items, (unsigned int)16)
841///
842void RewriteTest::SynthCountByEnumWithState(std::string &buf) {
843 buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, "
844 "id *, unsigned int))(void *)objc_msgSend)";
845 buf += "\n\t\t";
846 buf += "((id)l_collection,\n\t\t";
847 buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
848 buf += "\n\t\t";
849 buf += "&enumState, "
850 "(id *)items, (unsigned int)16)";
851}
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000852
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000853/// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
854/// statement to exit to its outer synthesized loop.
855///
856Stmt *RewriteTest::RewriteBreakStmt(BreakStmt *S) {
857 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
858 return S;
859 // replace break with goto __break_label
860 std::string buf;
861
862 SourceLocation startLoc = S->getLocStart();
863 buf = "goto __break_label_";
864 buf += utostr(ObjCBcLabelNo.back());
865 Rewrite.ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size());
866
867 return 0;
868}
869
870/// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
871/// statement to continue with its inner synthesized loop.
872///
873Stmt *RewriteTest::RewriteContinueStmt(ContinueStmt *S) {
874 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
875 return S;
876 // replace continue with goto __continue_label
877 std::string buf;
878
879 SourceLocation startLoc = S->getLocStart();
880 buf = "goto __continue_label_";
881 buf += utostr(ObjCBcLabelNo.back());
882 Rewrite.ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size());
883
884 return 0;
885}
886
Fariborz Jahaniane84b0402008-01-09 01:25:54 +0000887/// RewriteObjCTryStmt - Rewriter for ObjC2's foreach statement.
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000888/// It rewrites:
889/// for ( type elem in collection) { stmts; }
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +0000890
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000891/// Into:
892/// {
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000893/// type elem;
894/// struct __objcFastEnumerationState enumState = { 0 };
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000895/// id items[16];
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000896/// id l_collection = (id)collection;
897/// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
898/// objects:items count:16];
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000899/// if (limit) {
900/// unsigned long startMutations = *enumState.mutationsPtr;
901/// do {
902/// unsigned long counter = 0;
903/// do {
904/// if (startMutations != *enumState.mutationsPtr)
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000905/// objc_enumerationMutation(l_collection);
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000906/// elem = (type)enumState.itemsPtr[counter++];
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000907/// stmts;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000908/// __continue_label: ;
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000909/// } while (counter < limit);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000910/// } while (limit = [l_collection countByEnumeratingWithState:&enumState
911/// objects:items count:16]);
912/// elem = nil;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000913/// __break_label: ;
Fariborz Jahanian10d24f02008-01-07 21:40:22 +0000914/// }
915/// else
916/// elem = nil;
917/// }
918///
919Stmt *RewriteTest::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S) {
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +0000920 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
921 assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
922 "ObjCForCollectionStmt Statement stack mismatch");
923 assert(!ObjCBcLabelNo.empty() &&
924 "ObjCForCollectionStmt - Label No stack empty");
925
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000926 SourceLocation startLoc = S->getLocStart();
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000927 const char *startBuf = SM->getCharacterData(startLoc);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000928 const char *elementName;
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000929 std::string elementTypeAsString;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000930 std::string buf;
931 buf = "\n{\n\t";
932 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
933 // type elem;
934 QualType ElementType = cast<ValueDecl>(DS->getDecl())->getType();
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000935 elementTypeAsString = ElementType.getAsString();
936 buf += elementTypeAsString;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000937 buf += " ";
938 elementName = DS->getDecl()->getName();
939 buf += elementName;
940 buf += ";\n\t";
941 }
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000942 else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S->getElement())) {
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000943 elementName = DR->getDecl()->getName();
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000944 elementTypeAsString = DR->getDecl()->getType().getAsString();
945 }
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000946 else
947 assert(false && "RewriteObjCForCollectionStmt - bad element kind");
948
949 // struct __objcFastEnumerationState enumState = { 0 };
950 buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
951 // id items[16];
952 buf += "id items[16];\n\t";
953 // id l_collection = (id)
954 buf += "id l_collection = (id)";
Fariborz Jahanian75712282008-01-10 00:24:29 +0000955 // Find start location of 'collection' the hard way!
956 const char *startCollectionBuf = startBuf;
957 startCollectionBuf += 3; // skip 'for'
958 startCollectionBuf = strchr(startCollectionBuf, '(');
959 startCollectionBuf++; // skip '('
960 // find 'in' and skip it.
961 while (*startCollectionBuf != ' ' ||
962 *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
963 (*(startCollectionBuf+3) != ' ' &&
964 *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
965 startCollectionBuf++;
966 startCollectionBuf += 3;
967
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000968 // Replace: "for (type element in" with string constructed thus far.
969 Rewrite.ReplaceText(startLoc, startCollectionBuf - startBuf,
970 buf.c_str(), buf.size());
971 // Replace ')' in for '(' type elem in collection ')' with ';'
Fariborz Jahanian75712282008-01-10 00:24:29 +0000972 SourceLocation rightParenLoc = S->getRParenLoc();
973 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
974 SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000975 buf = ";\n\t";
976
977 // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
978 // objects:items count:16];
979 // which is synthesized into:
980 // unsigned int limit =
981 // ((unsigned int (*)
982 // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
983 // (void *)objc_msgSend)((id)l_collection,
984 // sel_registerName(
985 // "countByEnumeratingWithState:objects:count:"),
986 // (struct __objcFastEnumerationState *)&state,
987 // (id *)items, (unsigned int)16);
988 buf += "unsigned long limit =\n\t\t";
989 SynthCountByEnumWithState(buf);
990 buf += ";\n\t";
991 /// if (limit) {
992 /// unsigned long startMutations = *enumState.mutationsPtr;
993 /// do {
994 /// unsigned long counter = 0;
995 /// do {
996 /// if (startMutations != *enumState.mutationsPtr)
997 /// objc_enumerationMutation(l_collection);
Fariborz Jahanian88f50f32008-01-09 18:15:42 +0000998 /// elem = (type)enumState.itemsPtr[counter++];
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +0000999 buf += "if (limit) {\n\t";
1000 buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
1001 buf += "do {\n\t\t";
1002 buf += "unsigned long counter = 0;\n\t\t";
1003 buf += "do {\n\t\t\t";
1004 buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
1005 buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
1006 buf += elementName;
Fariborz Jahanian88f50f32008-01-09 18:15:42 +00001007 buf += " = (";
1008 buf += elementTypeAsString;
1009 buf += ")enumState.itemsPtr[counter++];";
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001010 // Replace ')' in for '(' type elem in collection ')' with all of these.
1011 Rewrite.ReplaceText(lparenLoc, 1, buf.c_str(), buf.size());
1012
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001013 /// __continue_label: ;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001014 /// } while (counter < limit);
1015 /// } while (limit = [l_collection countByEnumeratingWithState:&enumState
1016 /// objects:items count:16]);
1017 /// elem = nil;
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001018 /// __break_label: ;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001019 /// }
1020 /// else
1021 /// elem = nil;
1022 /// }
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001023 ///
1024 buf = ";\n\t";
1025 buf += "__continue_label_";
1026 buf += utostr(ObjCBcLabelNo.back());
1027 buf += ": ;";
1028 buf += "\n\t\t";
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001029 buf += "} while (counter < limit);\n\t";
1030 buf += "} while (limit = ";
1031 SynthCountByEnumWithState(buf);
1032 buf += ");\n\t";
1033 buf += elementName;
1034 buf += " = nil;\n\t";
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001035 buf += "__break_label_";
1036 buf += utostr(ObjCBcLabelNo.back());
1037 buf += ": ;\n\t";
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001038 buf += "}\n\t";
1039 buf += "else\n\t\t";
1040 buf += elementName;
1041 buf += " = nil;\n";
1042 buf += "}\n";
1043 // Insert all these *after* the statement body.
1044 SourceLocation endBodyLoc = S->getBody()->getLocEnd();
1045 const char *endBodyBuf = SM->getCharacterData(endBodyLoc)+1;
1046 endBodyLoc = startLoc.getFileLocWithOffset(endBodyBuf-startBuf);
1047 Rewrite.InsertText(endBodyLoc, buf.c_str(), buf.size());
Fariborz Jahaniane8d1c052008-01-15 23:58:23 +00001048 Stmts.pop_back();
1049 ObjCBcLabelNo.pop_back();
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001050
1051 return 0;
Fariborz Jahanian10d24f02008-01-07 21:40:22 +00001052}
1053
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001054Stmt *RewriteTest::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
Steve Naroff75730982007-11-07 04:08:17 +00001055 // Get the start location and compute the semi location.
1056 SourceLocation startLoc = S->getLocStart();
1057 const char *startBuf = SM->getCharacterData(startLoc);
1058
1059 assert((*startBuf == '@') && "bogus @try location");
1060
1061 std::string buf;
1062 // declare a new scope with two variables, _stack and _rethrow.
1063 buf = "/* @try scope begin */ { struct _objc_exception_data {\n";
1064 buf += "int buf[18/*32-bit i386*/];\n";
1065 buf += "char *pointers[4];} _stack;\n";
1066 buf += "id volatile _rethrow = 0;\n";
1067 buf += "objc_exception_try_enter(&_stack);\n";
Steve Naroff21867b12007-11-07 18:43:40 +00001068 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
Steve Naroff75730982007-11-07 04:08:17 +00001069
1070 Rewrite.ReplaceText(startLoc, 4, buf.c_str(), buf.size());
1071
1072 startLoc = S->getTryBody()->getLocEnd();
1073 startBuf = SM->getCharacterData(startLoc);
1074
1075 assert((*startBuf == '}') && "bogus @try block");
1076
1077 SourceLocation lastCurlyLoc = startLoc;
1078
1079 startLoc = startLoc.getFileLocWithOffset(1);
1080 buf = " /* @catch begin */ else {\n";
1081 buf += " id _caught = objc_exception_extract(&_stack);\n";
1082 buf += " objc_exception_try_enter (&_stack);\n";
Steve Naroff21867b12007-11-07 18:43:40 +00001083 buf += " if (_setjmp(_stack.buf))\n";
Steve Naroff75730982007-11-07 04:08:17 +00001084 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1085 buf += " else { /* @catch continue */";
1086
Chris Lattner28d1fe82007-11-08 04:41:51 +00001087 Rewrite.InsertText(startLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001088
1089 bool sawIdTypedCatch = false;
1090 Stmt *lastCatchBody = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001091 ObjCAtCatchStmt *catchList = S->getCatchStmts();
Steve Naroff75730982007-11-07 04:08:17 +00001092 while (catchList) {
1093 Stmt *catchStmt = catchList->getCatchParamStmt();
1094
1095 if (catchList == S->getCatchStmts())
1096 buf = "if ("; // we are generating code for the first catch clause
1097 else
1098 buf = "else if (";
1099 startLoc = catchList->getLocStart();
1100 startBuf = SM->getCharacterData(startLoc);
1101
1102 assert((*startBuf == '@') && "bogus @catch location");
1103
1104 const char *lParenLoc = strchr(startBuf, '(');
1105
1106 if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) {
1107 QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108 if (t == Context->getObjCIdType()) {
Steve Naroff75730982007-11-07 04:08:17 +00001109 buf += "1) { ";
1110 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
1111 buf.c_str(), buf.size());
1112 sawIdTypedCatch = true;
1113 } else if (const PointerType *pType = t->getAsPointerType()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001114 ObjCInterfaceType *cls; // Should be a pointer to a class.
Steve Naroff75730982007-11-07 04:08:17 +00001115
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001116 cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr());
Steve Naroff75730982007-11-07 04:08:17 +00001117 if (cls) {
Steve Naroff21867b12007-11-07 18:43:40 +00001118 buf += "objc_exception_match((struct objc_class *)objc_getClass(\"";
Steve Naroff75730982007-11-07 04:08:17 +00001119 buf += cls->getDecl()->getName();
Steve Naroff21867b12007-11-07 18:43:40 +00001120 buf += "\"), (struct objc_object *)_caught)) { ";
Steve Naroff75730982007-11-07 04:08:17 +00001121 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
1122 buf.c_str(), buf.size());
1123 }
1124 }
1125 // Now rewrite the body...
1126 lastCatchBody = catchList->getCatchBody();
1127 SourceLocation rParenLoc = catchList->getRParenLoc();
1128 SourceLocation bodyLoc = lastCatchBody->getLocStart();
1129 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1130 const char *rParenBuf = SM->getCharacterData(rParenLoc);
1131 assert((*rParenBuf == ')') && "bogus @catch paren location");
1132 assert((*bodyBuf == '{') && "bogus @catch body location");
1133
1134 buf = " = _caught;";
1135 // Here we replace ") {" with "= _caught;" (which initializes and
1136 // declares the @catch parameter).
1137 Rewrite.ReplaceText(rParenLoc, bodyBuf-rParenBuf+1,
1138 buf.c_str(), buf.size());
Steve Naroff2bd03922007-11-07 15:32:26 +00001139 } else if (!isa<NullStmt>(catchStmt)) {
Steve Naroff75730982007-11-07 04:08:17 +00001140 assert(false && "@catch rewrite bug");
Steve Naroff2bd03922007-11-07 15:32:26 +00001141 }
Steve Naroff75730982007-11-07 04:08:17 +00001142 catchList = catchList->getNextCatchStmt();
1143 }
1144 // Complete the catch list...
1145 if (lastCatchBody) {
1146 SourceLocation bodyLoc = lastCatchBody->getLocEnd();
1147 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1148 assert((*bodyBuf == '}') && "bogus @catch body location");
1149 bodyLoc = bodyLoc.getFileLocWithOffset(1);
1150 buf = " } } /* @catch end */\n";
1151
Chris Lattner28d1fe82007-11-08 04:41:51 +00001152 Rewrite.InsertText(bodyLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001153
1154 // Set lastCurlyLoc
1155 lastCurlyLoc = lastCatchBody->getLocEnd();
1156 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001157 if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) {
Steve Naroff75730982007-11-07 04:08:17 +00001158 startLoc = finalStmt->getLocStart();
1159 startBuf = SM->getCharacterData(startLoc);
1160 assert((*startBuf == '@') && "bogus @finally start");
1161
1162 buf = "/* @finally */";
1163 Rewrite.ReplaceText(startLoc, 8, buf.c_str(), buf.size());
1164
1165 Stmt *body = finalStmt->getFinallyBody();
1166 SourceLocation startLoc = body->getLocStart();
1167 SourceLocation endLoc = body->getLocEnd();
1168 const char *startBuf = SM->getCharacterData(startLoc);
1169 const char *endBuf = SM->getCharacterData(endLoc);
1170 assert((*startBuf == '{') && "bogus @finally body location");
1171 assert((*endBuf == '}') && "bogus @finally body location");
1172
1173 startLoc = startLoc.getFileLocWithOffset(1);
1174 buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
Chris Lattner28d1fe82007-11-08 04:41:51 +00001175 Rewrite.InsertText(startLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001176 endLoc = endLoc.getFileLocWithOffset(-1);
1177 buf = " if (_rethrow) objc_exception_throw(_rethrow);\n";
Chris Lattner28d1fe82007-11-08 04:41:51 +00001178 Rewrite.InsertText(endLoc, buf.c_str(), buf.size());
Steve Naroff75730982007-11-07 04:08:17 +00001179
1180 // Set lastCurlyLoc
1181 lastCurlyLoc = body->getLocEnd();
1182 }
1183 // Now emit the final closing curly brace...
1184 lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1);
1185 buf = " } /* @try scope end */\n";
Chris Lattner28d1fe82007-11-08 04:41:51 +00001186 Rewrite.InsertText(lastCurlyLoc, buf.c_str(), buf.size());
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001187 return 0;
1188}
1189
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001190Stmt *RewriteTest::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) {
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001191 return 0;
1192}
1193
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001194Stmt *RewriteTest::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) {
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001195 return 0;
1196}
1197
Steve Naroff2bd03922007-11-07 15:32:26 +00001198// This can't be done with Rewrite.ReplaceStmt(S, ThrowExpr), since
1199// the throw expression is typically a message expression that's already
1200// been rewritten! (which implies the SourceLocation's are invalid).
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001201Stmt *RewriteTest::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
Steve Naroff2bd03922007-11-07 15:32:26 +00001202 // Get the start location and compute the semi location.
1203 SourceLocation startLoc = S->getLocStart();
1204 const char *startBuf = SM->getCharacterData(startLoc);
1205
1206 assert((*startBuf == '@') && "bogus @throw location");
1207
1208 std::string buf;
1209 /* void objc_exception_throw(id) __attribute__((noreturn)); */
1210 buf = "objc_exception_throw(";
1211 Rewrite.ReplaceText(startLoc, 6, buf.c_str(), buf.size());
1212 const char *semiBuf = strchr(startBuf, ';');
1213 assert((*semiBuf == ';') && "@throw: can't find ';'");
1214 SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf);
1215 buf = ");";
1216 Rewrite.ReplaceText(semiLoc, 1, buf.c_str(), buf.size());
1217 return 0;
1218}
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00001219
Chris Lattnere64b7772007-10-24 16:57:36 +00001220Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattner01c57482007-10-17 22:35:30 +00001221 // Create a new string expression.
1222 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001223 std::string StrEncoding;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001224 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001225 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
1226 StrEncoding.length(), false, StrType,
Chris Lattner01c57482007-10-17 22:35:30 +00001227 SourceLocation(), SourceLocation());
Chris Lattnere365c502007-11-30 22:25:36 +00001228 if (Rewrite.ReplaceStmt(Exp, Replacement)) {
1229 // replacement failed.
Steve Naroff23ae0912007-12-19 19:16:49 +00001230 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1231 "rewriting sub-expression within a macro (may not be correct)");
Chris Lattner182745a2007-12-02 01:09:57 +00001232 SourceRange Range = Exp->getSourceRange();
Ted Kremenek9c728dc2007-12-12 22:39:36 +00001233 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Chris Lattnere365c502007-11-30 22:25:36 +00001234 }
1235
Chris Lattner07506182007-11-30 22:53:43 +00001236 // Replace this subexpr in the parent.
Chris Lattnere64b7772007-10-24 16:57:36 +00001237 delete Exp;
1238 return Replacement;
Chris Lattner311ff022007-10-16 22:36:42 +00001239}
1240
Steve Naroffb42f8412007-11-05 14:50:49 +00001241Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) {
1242 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
1243 // Create a call to sel_registerName("selName").
1244 llvm::SmallVector<Expr*, 8> SelExprs;
1245 QualType argType = Context->getPointerType(Context->CharTy);
1246 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
1247 Exp->getSelector().getName().size(),
1248 false, argType, SourceLocation(),
1249 SourceLocation()));
1250 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
1251 &SelExprs[0], SelExprs.size());
Steve Naroff5ca40202007-12-19 14:32:56 +00001252 if (Rewrite.ReplaceStmt(Exp, SelExp)) {
1253 // replacement failed.
Steve Naroff23ae0912007-12-19 19:16:49 +00001254 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1255 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroff5ca40202007-12-19 14:32:56 +00001256 SourceRange Range = Exp->getSourceRange();
1257 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Steve Naroff5ca40202007-12-19 14:32:56 +00001258 }
Steve Naroffb42f8412007-11-05 14:50:49 +00001259 delete Exp;
1260 return SelExp;
1261}
1262
Steve Naroff934f2762007-10-24 22:48:43 +00001263CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
1264 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffebf2b562007-10-23 23:50:29 +00001265 // Get the type, we will need to reference it in a couple spots.
Steve Naroff934f2762007-10-24 22:48:43 +00001266 QualType msgSendType = FD->getType();
Steve Naroffebf2b562007-10-23 23:50:29 +00001267
1268 // Create a reference to the objc_msgSend() declaration.
Steve Naroff934f2762007-10-24 22:48:43 +00001269 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffebf2b562007-10-23 23:50:29 +00001270
1271 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerf04da132007-10-24 17:06:59 +00001272 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffebf2b562007-10-23 23:50:29 +00001273 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
1274
1275 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattnere64b7772007-10-24 16:57:36 +00001276
Steve Naroff934f2762007-10-24 22:48:43 +00001277 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
1278}
1279
Steve Naroffd5255f52007-11-01 13:24:47 +00001280static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
1281 const char *&startRef, const char *&endRef) {
1282 while (startBuf < endBuf) {
1283 if (*startBuf == '<')
1284 startRef = startBuf; // mark the start.
1285 if (*startBuf == '>') {
Steve Naroff32174822007-11-09 12:50:28 +00001286 if (startRef && *startRef == '<') {
1287 endRef = startBuf; // mark the end.
1288 return true;
1289 }
1290 return false;
Steve Naroffd5255f52007-11-01 13:24:47 +00001291 }
1292 startBuf++;
1293 }
1294 return false;
1295}
1296
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001297static void scanToNextArgument(const char *&argRef) {
1298 int angle = 0;
1299 while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
1300 if (*argRef == '<')
1301 angle++;
1302 else if (*argRef == '>')
1303 angle--;
1304 argRef++;
1305 }
1306 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
1307}
Fariborz Jahanian291e04b2007-12-11 23:04:08 +00001308
Steve Naroffd5255f52007-11-01 13:24:47 +00001309bool RewriteTest::needToScanForQualifiers(QualType T) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001310
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001311 if (T == Context->getObjCIdType())
Steve Naroffd5255f52007-11-01 13:24:47 +00001312 return true;
1313
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001314 if (T->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001315 return true;
1316
Steve Naroffd5255f52007-11-01 13:24:47 +00001317 if (const PointerType *pType = T->getAsPointerType()) {
Steve Naroff9165ad32007-10-31 04:38:33 +00001318 Type *pointeeType = pType->getPointeeType().getTypePtr();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001319 if (isa<ObjCQualifiedInterfaceType>(pointeeType))
Steve Naroff9165ad32007-10-31 04:38:33 +00001320 return true; // we have "Class <Protocol> *".
1321 }
Steve Naroffd5255f52007-11-01 13:24:47 +00001322 return false;
1323}
1324
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001325void RewriteTest::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001326 SourceLocation Loc;
1327 QualType Type;
1328 const FunctionTypeProto *proto = 0;
1329 if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
1330 Loc = VD->getLocation();
1331 Type = VD->getType();
1332 }
1333 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
1334 Loc = FD->getLocation();
1335 // Check for ObjC 'id' and class types that have been adorned with protocol
1336 // information (id<p>, C<p>*). The protocol references need to be rewritten!
1337 const FunctionType *funcType = FD->getType()->getAsFunctionType();
1338 assert(funcType && "missing function type");
1339 proto = dyn_cast<FunctionTypeProto>(funcType);
1340 if (!proto)
1341 return;
1342 Type = proto->getResultType();
1343 }
1344 else
1345 return;
Steve Naroffd5255f52007-11-01 13:24:47 +00001346
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001347 if (needToScanForQualifiers(Type)) {
Steve Naroffd5255f52007-11-01 13:24:47 +00001348 // Since types are unique, we need to scan the buffer.
Steve Naroffd5255f52007-11-01 13:24:47 +00001349
1350 const char *endBuf = SM->getCharacterData(Loc);
1351 const char *startBuf = endBuf;
Chris Lattner26de4652007-12-02 01:13:47 +00001352 while (*startBuf != ';' && startBuf != MainFileStart)
Steve Naroffd5255f52007-11-01 13:24:47 +00001353 startBuf--; // scan backward (from the decl location) for return type.
1354 const char *startRef = 0, *endRef = 0;
1355 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
1356 // Get the locations of the startRef, endRef.
1357 SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf);
1358 SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1);
1359 // Comment out the protocol references.
Chris Lattner28d1fe82007-11-08 04:41:51 +00001360 Rewrite.InsertText(LessLoc, "/*", 2);
1361 Rewrite.InsertText(GreaterLoc, "*/", 2);
Steve Naroff9165ad32007-10-31 04:38:33 +00001362 }
1363 }
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001364 if (!proto)
1365 return; // most likely, was a variable
Steve Naroffd5255f52007-11-01 13:24:47 +00001366 // Now check arguments.
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001367 const char *startBuf = SM->getCharacterData(Loc);
1368 const char *startFuncBuf = startBuf;
Steve Naroffd5255f52007-11-01 13:24:47 +00001369 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
1370 if (needToScanForQualifiers(proto->getArgType(i))) {
1371 // Since types are unique, we need to scan the buffer.
Steve Naroffd5255f52007-11-01 13:24:47 +00001372
Steve Naroffd5255f52007-11-01 13:24:47 +00001373 const char *endBuf = startBuf;
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001374 // scan forward (from the decl location) for argument types.
1375 scanToNextArgument(endBuf);
Steve Naroffd5255f52007-11-01 13:24:47 +00001376 const char *startRef = 0, *endRef = 0;
1377 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
1378 // Get the locations of the startRef, endRef.
Fariborz Jahanian291e04b2007-12-11 23:04:08 +00001379 SourceLocation LessLoc =
1380 Loc.getFileLocWithOffset(startRef-startFuncBuf);
1381 SourceLocation GreaterLoc =
1382 Loc.getFileLocWithOffset(endRef-startFuncBuf+1);
Steve Naroffd5255f52007-11-01 13:24:47 +00001383 // Comment out the protocol references.
Chris Lattner28d1fe82007-11-08 04:41:51 +00001384 Rewrite.InsertText(LessLoc, "/*", 2);
1385 Rewrite.InsertText(GreaterLoc, "*/", 2);
Steve Naroffd5255f52007-11-01 13:24:47 +00001386 }
Fariborz Jahanian61477f72007-12-11 22:50:14 +00001387 startBuf = ++endBuf;
1388 }
1389 else {
1390 while (*startBuf != ')' && *startBuf != ',')
1391 startBuf++; // scan forward (from the decl location) for argument types.
1392 startBuf++;
1393 }
Steve Naroffd5255f52007-11-01 13:24:47 +00001394 }
Steve Naroff9165ad32007-10-31 04:38:33 +00001395}
1396
Fariborz Jahaniana70711b2007-12-04 21:47:40 +00001397// SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
1398void RewriteTest::SynthSelGetUidFunctionDecl() {
1399 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
1400 llvm::SmallVector<QualType, 16> ArgTys;
1401 ArgTys.push_back(Context->getPointerType(
1402 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001403 QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(),
Fariborz Jahaniana70711b2007-12-04 21:47:40 +00001404 &ArgTys[0], ArgTys.size(),
1405 false /*isVariadic*/);
1406 SelGetUidFunctionDecl = new FunctionDecl(SourceLocation(),
1407 SelGetUidIdent, getFuncType,
1408 FunctionDecl::Extern, false, 0);
1409}
1410
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00001411// SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto);
1412void RewriteTest::SynthGetProtocolFunctionDecl() {
1413 IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol");
1414 llvm::SmallVector<QualType, 16> ArgTys;
1415 ArgTys.push_back(Context->getPointerType(
1416 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001417 QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(),
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00001418 &ArgTys[0], ArgTys.size(),
1419 false /*isVariadic*/);
1420 GetProtocolFunctionDecl = new FunctionDecl(SourceLocation(),
1421 SelGetProtoIdent, getFuncType,
1422 FunctionDecl::Extern, false, 0);
1423}
1424
Steve Naroff09b266e2007-10-30 23:14:51 +00001425void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
1426 // declared in <objc/objc.h>
Steve Naroffbeaf2992007-11-03 11:27:19 +00001427 if (strcmp(FD->getName(), "sel_registerName") == 0) {
Steve Naroff09b266e2007-10-30 23:14:51 +00001428 SelGetUidFunctionDecl = FD;
Steve Naroff9165ad32007-10-31 04:38:33 +00001429 return;
1430 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431 RewriteObjCQualifiedInterfaceTypes(FD);
Steve Naroff09b266e2007-10-30 23:14:51 +00001432}
1433
1434// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
1435void RewriteTest::SynthMsgSendFunctionDecl() {
1436 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
1437 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001438 QualType argT = Context->getObjCIdType();
Steve Naroff09b266e2007-10-30 23:14:51 +00001439 assert(!argT.isNull() && "Can't find 'id' type");
1440 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 argT = Context->getObjCSelType();
Steve Naroff09b266e2007-10-30 23:14:51 +00001442 assert(!argT.isNull() && "Can't find 'SEL' type");
1443 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001444 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff09b266e2007-10-30 23:14:51 +00001445 &ArgTys[0], ArgTys.size(),
1446 true /*isVariadic*/);
1447 MsgSendFunctionDecl = new FunctionDecl(SourceLocation(),
1448 msgSendIdent, msgSendType,
1449 FunctionDecl::Extern, false, 0);
1450}
1451
Steve Naroff874e2322007-11-15 10:28:18 +00001452// SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...);
1453void RewriteTest::SynthMsgSendSuperFunctionDecl() {
1454 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
1455 llvm::SmallVector<QualType, 16> ArgTys;
1456 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
1457 &Context->Idents.get("objc_super"), 0);
1458 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
1459 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
1460 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001461 argT = Context->getObjCSelType();
Steve Naroff874e2322007-11-15 10:28:18 +00001462 assert(!argT.isNull() && "Can't find 'SEL' type");
1463 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001464 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff874e2322007-11-15 10:28:18 +00001465 &ArgTys[0], ArgTys.size(),
1466 true /*isVariadic*/);
1467 MsgSendSuperFunctionDecl = new FunctionDecl(SourceLocation(),
1468 msgSendIdent, msgSendType,
1469 FunctionDecl::Extern, false, 0);
1470}
1471
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001472// SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
1473void RewriteTest::SynthMsgSendStretFunctionDecl() {
1474 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
1475 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 QualType argT = Context->getObjCIdType();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001477 assert(!argT.isNull() && "Can't find 'id' type");
1478 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001479 argT = Context->getObjCSelType();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001480 assert(!argT.isNull() && "Can't find 'SEL' type");
1481 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001482 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001483 &ArgTys[0], ArgTys.size(),
1484 true /*isVariadic*/);
1485 MsgSendStretFunctionDecl = new FunctionDecl(SourceLocation(),
1486 msgSendIdent, msgSendType,
1487 FunctionDecl::Extern, false, 0);
1488}
1489
1490// SynthMsgSendSuperStretFunctionDecl -
1491// id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...);
1492void RewriteTest::SynthMsgSendSuperStretFunctionDecl() {
1493 IdentifierInfo *msgSendIdent =
1494 &Context->Idents.get("objc_msgSendSuper_stret");
1495 llvm::SmallVector<QualType, 16> ArgTys;
1496 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
1497 &Context->Idents.get("objc_super"), 0);
1498 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
1499 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
1500 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001501 argT = Context->getObjCSelType();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001502 assert(!argT.isNull() && "Can't find 'SEL' type");
1503 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001504 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001505 &ArgTys[0], ArgTys.size(),
1506 true /*isVariadic*/);
1507 MsgSendSuperStretFunctionDecl = new FunctionDecl(SourceLocation(),
1508 msgSendIdent, msgSendType,
1509 FunctionDecl::Extern, false, 0);
1510}
1511
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001512// SynthMsgSendFpretFunctionDecl - id objc_msgSend_fpret(id self, SEL op, ...);
1513void RewriteTest::SynthMsgSendFpretFunctionDecl() {
1514 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
1515 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001516 QualType argT = Context->getObjCIdType();
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001517 assert(!argT.isNull() && "Can't find 'id' type");
1518 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001519 argT = Context->getObjCSelType();
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001520 assert(!argT.isNull() && "Can't find 'SEL' type");
1521 ArgTys.push_back(argT);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001522 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001523 &ArgTys[0], ArgTys.size(),
1524 true /*isVariadic*/);
1525 MsgSendFpretFunctionDecl = new FunctionDecl(SourceLocation(),
1526 msgSendIdent, msgSendType,
1527 FunctionDecl::Extern, false, 0);
1528}
1529
Steve Naroff09b266e2007-10-30 23:14:51 +00001530// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
1531void RewriteTest::SynthGetClassFunctionDecl() {
1532 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
1533 llvm::SmallVector<QualType, 16> ArgTys;
1534 ArgTys.push_back(Context->getPointerType(
1535 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001536 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff09b266e2007-10-30 23:14:51 +00001537 &ArgTys[0], ArgTys.size(),
1538 false /*isVariadic*/);
1539 GetClassFunctionDecl = new FunctionDecl(SourceLocation(),
1540 getClassIdent, getClassType,
1541 FunctionDecl::Extern, false, 0);
1542}
1543
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001544// SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name);
1545void RewriteTest::SynthGetMetaClassFunctionDecl() {
1546 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
1547 llvm::SmallVector<QualType, 16> ArgTys;
1548 ArgTys.push_back(Context->getPointerType(
1549 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001550 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001551 &ArgTys[0], ArgTys.size(),
1552 false /*isVariadic*/);
1553 GetMetaClassFunctionDecl = new FunctionDecl(SourceLocation(),
1554 getClassIdent, getClassType,
1555 FunctionDecl::Extern, false, 0);
1556}
1557
Steve Naroff96984642007-11-08 14:30:50 +00001558// SynthCFStringFunctionDecl - id __builtin___CFStringMakeConstantString(const char *name);
1559void RewriteTest::SynthCFStringFunctionDecl() {
1560 IdentifierInfo *getClassIdent = &Context->Idents.get("__builtin___CFStringMakeConstantString");
1561 llvm::SmallVector<QualType, 16> ArgTys;
1562 ArgTys.push_back(Context->getPointerType(
1563 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001564 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff96984642007-11-08 14:30:50 +00001565 &ArgTys[0], ArgTys.size(),
1566 false /*isVariadic*/);
1567 CFStringFunctionDecl = new FunctionDecl(SourceLocation(),
1568 getClassIdent, getClassType,
1569 FunctionDecl::Extern, false, 0);
1570}
1571
Steve Naroffbeaf2992007-11-03 11:27:19 +00001572Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
Steve Naroff96984642007-11-08 14:30:50 +00001573#if 1
1574 // This rewrite is specific to GCC, which has builtin support for CFString.
1575 if (!CFStringFunctionDecl)
1576 SynthCFStringFunctionDecl();
1577 // Create a call to __builtin___CFStringMakeConstantString("cstr").
1578 llvm::SmallVector<Expr*, 8> StrExpr;
1579 StrExpr.push_back(Exp->getString());
1580 CallExpr *call = SynthesizeCallToFunctionDecl(CFStringFunctionDecl,
1581 &StrExpr[0], StrExpr.size());
1582 // cast to NSConstantString *
1583 CastExpr *cast = new CastExpr(Exp->getType(), call, SourceLocation());
Steve Naroff5ca40202007-12-19 14:32:56 +00001584 if (Rewrite.ReplaceStmt(Exp, cast)) {
1585 // replacement failed.
Steve Naroff23ae0912007-12-19 19:16:49 +00001586 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1587 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroff5ca40202007-12-19 14:32:56 +00001588 SourceRange Range = Exp->getSourceRange();
1589 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Steve Naroff5ca40202007-12-19 14:32:56 +00001590 }
Steve Naroff96984642007-11-08 14:30:50 +00001591 delete Exp;
1592 return cast;
1593#else
Steve Naroffbeaf2992007-11-03 11:27:19 +00001594 assert(ConstantStringClassReference && "Can't find constant string reference");
1595 llvm::SmallVector<Expr*, 4> InitExprs;
1596
1597 // Synthesize "(Class)&_NSConstantStringClassReference"
1598 DeclRefExpr *ClsRef = new DeclRefExpr(ConstantStringClassReference,
1599 ConstantStringClassReference->getType(),
1600 SourceLocation());
1601 QualType expType = Context->getPointerType(ClsRef->getType());
1602 UnaryOperator *Unop = new UnaryOperator(ClsRef, UnaryOperator::AddrOf,
1603 expType, SourceLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001604 CastExpr *cast = new CastExpr(Context->getObjCClassType(), Unop,
Steve Naroffbeaf2992007-11-03 11:27:19 +00001605 SourceLocation());
1606 InitExprs.push_back(cast); // set the 'isa'.
1607 InitExprs.push_back(Exp->getString()); // set "char *bytes".
1608 unsigned IntSize = static_cast<unsigned>(
1609 Context->getTypeSize(Context->IntTy, Exp->getLocStart()));
1610 llvm::APInt IntVal(IntSize, Exp->getString()->getByteLength());
1611 IntegerLiteral *len = new IntegerLiteral(IntVal, Context->IntTy,
1612 Exp->getLocStart());
1613 InitExprs.push_back(len); // set "int numBytes".
1614
1615 // struct NSConstantString
1616 QualType CFConstantStrType = Context->getCFConstantStringType();
1617 // (struct NSConstantString) { <exprs from above> }
1618 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1619 &InitExprs[0], InitExprs.size(),
1620 SourceLocation());
Steve Naroffe9b12192008-01-14 18:19:28 +00001621 CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE, false);
Steve Naroffbeaf2992007-11-03 11:27:19 +00001622 // struct NSConstantString *
1623 expType = Context->getPointerType(StrRep->getType());
1624 Unop = new UnaryOperator(StrRep, UnaryOperator::AddrOf, expType,
1625 SourceLocation());
Steve Naroff352336b2007-11-05 14:36:37 +00001626 // cast to NSConstantString *
1627 cast = new CastExpr(Exp->getType(), Unop, SourceLocation());
Steve Naroffbeaf2992007-11-03 11:27:19 +00001628 Rewrite.ReplaceStmt(Exp, cast);
1629 delete Exp;
Steve Naroff352336b2007-11-05 14:36:37 +00001630 return cast;
Steve Naroff96984642007-11-08 14:30:50 +00001631#endif
Steve Naroffbeaf2992007-11-03 11:27:19 +00001632}
1633
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001634ObjCInterfaceDecl *RewriteTest::isSuperReceiver(Expr *recExpr) {
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001635 // check if we are sending a message to 'super'
1636 if (CurMethodDecl && CurMethodDecl->isInstance()) {
Steve Naroff874e2322007-11-15 10:28:18 +00001637 if (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) {
1638 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1639 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
1640 if (!strcmp(PVD->getName(), "self")) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001641 // is this id<P1..> type?
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001642 if (CE->getType()->isObjCQualifiedIdType())
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001643 return 0;
Steve Naroff874e2322007-11-15 10:28:18 +00001644 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001645 if (ObjCInterfaceType *IT =
1646 dyn_cast<ObjCInterfaceType>(PT->getPointeeType())) {
Steve Naroff874e2322007-11-15 10:28:18 +00001647 if (IT->getDecl() ==
1648 CurMethodDecl->getClassInterface()->getSuperClass())
1649 return IT->getDecl();
1650 }
1651 }
1652 }
1653 }
1654 }
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001655 }
Steve Naroff874e2322007-11-15 10:28:18 +00001656 }
1657 return 0;
1658}
1659
1660// struct objc_super { struct objc_object *receiver; struct objc_class *super; };
1661QualType RewriteTest::getSuperStructType() {
1662 if (!SuperStructDecl) {
1663 SuperStructDecl = new RecordDecl(Decl::Struct, SourceLocation(),
1664 &Context->Idents.get("objc_super"), 0);
1665 QualType FieldTypes[2];
1666
1667 // struct objc_object *receiver;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001668 FieldTypes[0] = Context->getObjCIdType();
Steve Naroff874e2322007-11-15 10:28:18 +00001669 // struct objc_class *super;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001670 FieldTypes[1] = Context->getObjCClassType();
Steve Naroff874e2322007-11-15 10:28:18 +00001671 // Create fields
1672 FieldDecl *FieldDecls[2];
1673
1674 for (unsigned i = 0; i < 2; ++i)
1675 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
1676
1677 SuperStructDecl->defineBody(FieldDecls, 4);
1678 }
1679 return Context->getTagDeclType(SuperStructDecl);
1680}
1681
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001682Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) {
Fariborz Jahaniana70711b2007-12-04 21:47:40 +00001683 if (!SelGetUidFunctionDecl)
1684 SynthSelGetUidFunctionDecl();
Steve Naroff09b266e2007-10-30 23:14:51 +00001685 if (!MsgSendFunctionDecl)
1686 SynthMsgSendFunctionDecl();
Steve Naroff874e2322007-11-15 10:28:18 +00001687 if (!MsgSendSuperFunctionDecl)
1688 SynthMsgSendSuperFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001689 if (!MsgSendStretFunctionDecl)
1690 SynthMsgSendStretFunctionDecl();
1691 if (!MsgSendSuperStretFunctionDecl)
1692 SynthMsgSendSuperStretFunctionDecl();
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001693 if (!MsgSendFpretFunctionDecl)
1694 SynthMsgSendFpretFunctionDecl();
Steve Naroff09b266e2007-10-30 23:14:51 +00001695 if (!GetClassFunctionDecl)
1696 SynthGetClassFunctionDecl();
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001697 if (!GetMetaClassFunctionDecl)
1698 SynthGetMetaClassFunctionDecl();
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001699
Steve Naroff874e2322007-11-15 10:28:18 +00001700 // default to objc_msgSend().
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001701 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
1702 // May need to use objc_msgSend_stret() as well.
1703 FunctionDecl *MsgSendStretFlavor = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001704 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001705 QualType resultType = mDecl->getResultType();
1706 if (resultType.getCanonicalType()->isStructureType()
1707 || resultType.getCanonicalType()->isUnionType())
1708 MsgSendStretFlavor = MsgSendStretFunctionDecl;
Fariborz Jahanianacb49772007-12-03 21:26:48 +00001709 else if (resultType.getCanonicalType()->isRealFloatingType())
1710 MsgSendFlavor = MsgSendFpretFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001711 }
Steve Naroff874e2322007-11-15 10:28:18 +00001712
Steve Naroff934f2762007-10-24 22:48:43 +00001713 // Synthesize a call to objc_msgSend().
1714 llvm::SmallVector<Expr*, 8> MsgExprs;
1715 IdentifierInfo *clsName = Exp->getClassName();
1716
1717 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
1718 if (clsName) { // class message.
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001719 if (!strcmp(clsName->getName(), "super")) {
1720 MsgSendFlavor = MsgSendSuperFunctionDecl;
1721 if (MsgSendStretFlavor)
1722 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
1723 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
1724
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001725 ObjCInterfaceDecl *SuperDecl =
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001726 CurMethodDecl->getClassInterface()->getSuperClass();
1727
1728 llvm::SmallVector<Expr*, 4> InitExprs;
1729
1730 // set the receiver to self, the first argument to all methods.
1731 InitExprs.push_back(new DeclRefExpr(CurMethodDecl->getSelfDecl(),
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001732 Context->getObjCIdType(),
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001733 SourceLocation()));
1734 llvm::SmallVector<Expr*, 8> ClsExprs;
1735 QualType argType = Context->getPointerType(Context->CharTy);
1736 ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
1737 SuperDecl->getIdentifier()->getLength(),
1738 false, argType, SourceLocation(),
1739 SourceLocation()));
1740 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
1741 &ClsExprs[0],
1742 ClsExprs.size());
1743 // To turn off a warning, type-cast to 'id'
1744 InitExprs.push_back(
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001745 new CastExpr(Context->getObjCIdType(),
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001746 Cls, SourceLocation())); // set 'super class', using objc_getClass().
1747 // struct objc_super
1748 QualType superType = getSuperStructType();
1749 // (struct objc_super) { <exprs from above> }
1750 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1751 &InitExprs[0], InitExprs.size(),
1752 SourceLocation());
Chris Lattner0fc53df2008-01-02 21:46:24 +00001753 CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
Steve Naroffe9b12192008-01-14 18:19:28 +00001754 superType, ILE, false);
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001755 // struct objc_super *
1756 Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
1757 Context->getPointerType(SuperRep->getType()),
1758 SourceLocation());
1759 MsgExprs.push_back(Unop);
1760 } else {
1761 llvm::SmallVector<Expr*, 8> ClsExprs;
1762 QualType argType = Context->getPointerType(Context->CharTy);
1763 ClsExprs.push_back(new StringLiteral(clsName->getName(),
1764 clsName->getLength(),
1765 false, argType, SourceLocation(),
1766 SourceLocation()));
1767 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
1768 &ClsExprs[0],
1769 ClsExprs.size());
1770 MsgExprs.push_back(Cls);
1771 }
Steve Naroff6568d4d2007-11-14 23:54:14 +00001772 } else { // instance message.
1773 Expr *recExpr = Exp->getReceiver();
Steve Naroff874e2322007-11-15 10:28:18 +00001774
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001775 if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) {
Steve Naroff874e2322007-11-15 10:28:18 +00001776 MsgSendFlavor = MsgSendSuperFunctionDecl;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001777 if (MsgSendStretFlavor)
1778 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
Steve Naroff874e2322007-11-15 10:28:18 +00001779 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
1780
1781 llvm::SmallVector<Expr*, 4> InitExprs;
1782
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001783 InitExprs.push_back(
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001784 new CastExpr(Context->getObjCIdType(),
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001785 recExpr, SourceLocation())); // set the 'receiver'.
Steve Naroff874e2322007-11-15 10:28:18 +00001786
1787 llvm::SmallVector<Expr*, 8> ClsExprs;
1788 QualType argType = Context->getPointerType(Context->CharTy);
Steve Naroff9bcb5fc2007-12-07 03:50:46 +00001789 ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
1790 SuperDecl->getIdentifier()->getLength(),
Steve Naroff874e2322007-11-15 10:28:18 +00001791 false, argType, SourceLocation(),
1792 SourceLocation()));
1793 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001794 &ClsExprs[0],
1795 ClsExprs.size());
Fariborz Jahanian71274312007-12-05 17:29:46 +00001796 // To turn off a warning, type-cast to 'id'
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001797 InitExprs.push_back(
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001798 new CastExpr(Context->getObjCIdType(),
Fariborz Jahanianceee3e82007-12-04 22:32:58 +00001799 Cls, SourceLocation())); // set 'super class', using objc_getClass().
Steve Naroff874e2322007-11-15 10:28:18 +00001800 // struct objc_super
1801 QualType superType = getSuperStructType();
1802 // (struct objc_super) { <exprs from above> }
1803 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1804 &InitExprs[0], InitExprs.size(),
1805 SourceLocation());
Chris Lattner0fc53df2008-01-02 21:46:24 +00001806 CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
Steve Naroffe9b12192008-01-14 18:19:28 +00001807 superType, ILE, false);
Steve Naroff874e2322007-11-15 10:28:18 +00001808 // struct objc_super *
1809 Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
1810 Context->getPointerType(SuperRep->getType()),
1811 SourceLocation());
1812 MsgExprs.push_back(Unop);
1813 } else {
Fariborz Jahanian7dd82832007-12-07 21:21:21 +00001814 // Remove all type-casts because it may contain objc-style types; e.g.
1815 // Foo<Proto> *.
1816 while (CastExpr *CE = dyn_cast<CastExpr>(recExpr))
1817 recExpr = CE->getSubExpr();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001818 recExpr = new CastExpr(Context->getObjCIdType(), recExpr, SourceLocation());
Steve Naroff874e2322007-11-15 10:28:18 +00001819 MsgExprs.push_back(recExpr);
1820 }
Steve Naroff6568d4d2007-11-14 23:54:14 +00001821 }
Steve Naroffbeaf2992007-11-03 11:27:19 +00001822 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
Steve Naroff934f2762007-10-24 22:48:43 +00001823 llvm::SmallVector<Expr*, 8> SelExprs;
1824 QualType argType = Context->getPointerType(Context->CharTy);
1825 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
1826 Exp->getSelector().getName().size(),
1827 false, argType, SourceLocation(),
1828 SourceLocation()));
1829 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
1830 &SelExprs[0], SelExprs.size());
1831 MsgExprs.push_back(SelExp);
1832
1833 // Now push any user supplied arguments.
1834 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
Steve Naroff6568d4d2007-11-14 23:54:14 +00001835 Expr *userExpr = Exp->getArg(i);
Steve Naroff7e3411b2007-11-15 02:58:25 +00001836 // Make all implicit casts explicit...ICE comes in handy:-)
1837 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
1838 // Reuse the ICE type, it is exactly what the doctor ordered.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001839 userExpr = new CastExpr(ICE->getType()->isObjCQualifiedIdType()
1840 ? Context->getObjCIdType()
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001841 : ICE->getType(), userExpr, SourceLocation());
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001842 }
1843 // Make id<P...> cast into an 'id' cast.
1844 else if (CastExpr *CE = dyn_cast<CastExpr>(userExpr)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001845 if (CE->getType()->isObjCQualifiedIdType()) {
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001846 while ((CE = dyn_cast<CastExpr>(userExpr)))
1847 userExpr = CE->getSubExpr();
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001848 userExpr = new CastExpr(Context->getObjCIdType(),
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +00001849 userExpr, SourceLocation());
1850 }
Steve Naroff7e3411b2007-11-15 02:58:25 +00001851 }
Steve Naroff6568d4d2007-11-14 23:54:14 +00001852 MsgExprs.push_back(userExpr);
Steve Naroff934f2762007-10-24 22:48:43 +00001853 // We've transferred the ownership to MsgExprs. Null out the argument in
1854 // the original expression, since we will delete it below.
1855 Exp->setArg(i, 0);
1856 }
Steve Naroffab972d32007-11-04 22:37:50 +00001857 // Generate the funky cast.
1858 CastExpr *cast;
1859 llvm::SmallVector<QualType, 8> ArgTypes;
1860 QualType returnType;
1861
1862 // Push 'id' and 'SEL', the 2 implicit arguments.
Steve Naroffc3a438c2007-11-15 10:43:57 +00001863 if (MsgSendFlavor == MsgSendSuperFunctionDecl)
1864 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
1865 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001866 ArgTypes.push_back(Context->getObjCIdType());
1867 ArgTypes.push_back(Context->getObjCSelType());
1868 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
Steve Naroffab972d32007-11-04 22:37:50 +00001869 // Push any user argument types.
Steve Naroff352336b2007-11-05 14:36:37 +00001870 for (int i = 0; i < mDecl->getNumParams(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001871 QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType()
1872 ? Context->getObjCIdType()
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001873 : mDecl->getParamDecl(i)->getType();
Steve Naroff352336b2007-11-05 14:36:37 +00001874 ArgTypes.push_back(t);
1875 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001876 returnType = mDecl->getResultType()->isObjCQualifiedIdType()
1877 ? Context->getObjCIdType() : mDecl->getResultType();
Steve Naroffab972d32007-11-04 22:37:50 +00001878 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001879 returnType = Context->getObjCIdType();
Steve Naroffab972d32007-11-04 22:37:50 +00001880 }
1881 // Get the type, we will need to reference it in a couple spots.
Steve Naroff874e2322007-11-15 10:28:18 +00001882 QualType msgSendType = MsgSendFlavor->getType();
Steve Naroffab972d32007-11-04 22:37:50 +00001883
1884 // Create a reference to the objc_msgSend() declaration.
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001885 DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType,
1886 SourceLocation());
Steve Naroffab972d32007-11-04 22:37:50 +00001887
1888 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
1889 // If we don't do this cast, we get the following bizarre warning/note:
1890 // xx.m:13: warning: function called through a non-compatible type
1891 // xx.m:13: note: if this code is reached, the program will abort
1892 cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE,
1893 SourceLocation());
Steve Naroff335eafa2007-11-15 12:35:21 +00001894
Steve Naroffab972d32007-11-04 22:37:50 +00001895 // Now do the "normal" pointer to function cast.
1896 QualType castType = Context->getFunctionType(returnType,
Fariborz Jahaniand0ee6f92007-12-06 19:49:56 +00001897 &ArgTypes[0], ArgTypes.size(),
1898 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
Steve Naroffab972d32007-11-04 22:37:50 +00001899 castType = Context->getPointerType(castType);
1900 cast = new CastExpr(castType, cast, SourceLocation());
1901
1902 // Don't forget the parens to enforce the proper binding.
1903 ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
1904
1905 const FunctionType *FT = msgSendType->getAsFunctionType();
1906 CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
1907 FT->getResultType(), SourceLocation());
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001908 Stmt *ReplacingStmt = CE;
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001909 if (MsgSendStretFlavor) {
1910 // We have the method which returns a struct/union. Must also generate
1911 // call to objc_msgSend_stret and hang both varieties on a conditional
1912 // expression which dictate which one to envoke depending on size of
1913 // method's return type.
1914
1915 // Create a reference to the objc_msgSend_stret() declaration.
1916 DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType,
1917 SourceLocation());
1918 // Need to cast objc_msgSend_stret to "void *" (see above comment).
1919 cast = new CastExpr(Context->getPointerType(Context->VoidTy), STDRE,
1920 SourceLocation());
1921 // Now do the "normal" pointer to function cast.
1922 castType = Context->getFunctionType(returnType,
Fariborz Jahaniand0ee6f92007-12-06 19:49:56 +00001923 &ArgTypes[0], ArgTypes.size(),
1924 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001925 castType = Context->getPointerType(castType);
1926 cast = new CastExpr(castType, cast, SourceLocation());
1927
1928 // Don't forget the parens to enforce the proper binding.
1929 PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
1930
1931 FT = msgSendType->getAsFunctionType();
1932 CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
1933 FT->getResultType(), SourceLocation());
1934
1935 // Build sizeof(returnType)
1936 SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true,
1937 returnType, Context->getSizeType(),
1938 SourceLocation(), SourceLocation());
1939 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
1940 // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases.
1941 // For X86 it is more complicated and some kind of target specific routine
1942 // is needed to decide what to do.
1943 unsigned IntSize = static_cast<unsigned>(
1944 Context->getTypeSize(Context->IntTy, SourceLocation()));
1945
1946 IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8),
1947 Context->IntTy,
1948 SourceLocation());
1949 BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit,
1950 BinaryOperator::LE,
1951 Context->IntTy,
1952 SourceLocation());
1953 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
1954 ConditionalOperator *CondExpr =
1955 new ConditionalOperator(lessThanExpr, CE, STCE, returnType);
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001956 ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr);
Fariborz Jahanian80a6a5a2007-12-03 19:17:29 +00001957 }
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001958 return ReplacingStmt;
1959}
1960
1961Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
1962 Stmt *ReplacingStmt = SynthMessageExpr(Exp);
Steve Naroff934f2762007-10-24 22:48:43 +00001963 // Now do the actual rewrite.
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001964 if (Rewrite.ReplaceStmt(Exp, ReplacingStmt)) {
Steve Naroff5ca40202007-12-19 14:32:56 +00001965 // replacement failed.
Steve Naroff23ae0912007-12-19 19:16:49 +00001966 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1967 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroff5ca40202007-12-19 14:32:56 +00001968 SourceRange Range = Exp->getSourceRange();
1969 Diags.Report(Context->getFullLoc(Exp->getLocStart()), DiagID, 0, 0, &Range, 1);
Steve Naroff5ca40202007-12-19 14:32:56 +00001970 }
Steve Naroff934f2762007-10-24 22:48:43 +00001971
Chris Lattnere64b7772007-10-24 16:57:36 +00001972 delete Exp;
Fariborz Jahanian33b9c4e2008-01-08 22:06:28 +00001973 return ReplacingStmt;
Steve Naroffebf2b562007-10-23 23:50:29 +00001974}
1975
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00001976/// RewriteObjCProtocolExpr - Rewrite a protocol expression into
1977/// call to objc_getProtocol("proto-name").
1978Stmt *RewriteTest::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
1979 if (!GetProtocolFunctionDecl)
1980 SynthGetProtocolFunctionDecl();
1981 // Create a call to objc_getProtocol("ProtocolName").
1982 llvm::SmallVector<Expr*, 8> ProtoExprs;
1983 QualType argType = Context->getPointerType(Context->CharTy);
1984 ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(),
1985 strlen(Exp->getProtocol()->getName()),
1986 false, argType, SourceLocation(),
1987 SourceLocation()));
1988 CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl,
1989 &ProtoExprs[0],
1990 ProtoExprs.size());
Steve Naroff5ca40202007-12-19 14:32:56 +00001991 if (Rewrite.ReplaceStmt(Exp, ProtoExp)) {
1992 // replacement failed.
Steve Naroff23ae0912007-12-19 19:16:49 +00001993 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1994 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroff5ca40202007-12-19 14:32:56 +00001995 SourceRange Range = Exp->getSourceRange();
1996 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Steve Naroff5ca40202007-12-19 14:32:56 +00001997 }
Fariborz Jahanian36ee2cb2007-12-07 18:47:10 +00001998 delete Exp;
1999 return ProtoExp;
2000
2001}
2002
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002003/// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002004/// an objective-c class with ivars.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002005void RewriteTest::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002006 std::string &Result) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002007 assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
2008 assert(CDecl->getName() && "Name missing in SynthesizeObjCInternalStruct");
Fariborz Jahanian212b7682007-10-31 23:08:24 +00002009 // Do not synthesize more than once.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002010 if (ObjCSynthesizedStructs.count(CDecl))
Fariborz Jahanian212b7682007-10-31 23:08:24 +00002011 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002012 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
Steve Naroff03300712007-11-12 13:56:41 +00002013 int NumIvars = CDecl->getNumInstanceVariables();
Steve Narofffea763e82007-11-14 19:25:57 +00002014 SourceLocation LocStart = CDecl->getLocStart();
2015 SourceLocation LocEnd = CDecl->getLocEnd();
2016
2017 const char *startBuf = SM->getCharacterData(LocStart);
2018 const char *endBuf = SM->getCharacterData(LocEnd);
Fariborz Jahanian2c7038b2007-11-26 19:52:57 +00002019 // If no ivars and no root or if its root, directly or indirectly,
2020 // have no ivars (thus not synthesized) then no need to synthesize this class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002021 if (NumIvars <= 0 && (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
Fariborz Jahanian2c7038b2007-11-26 19:52:57 +00002022 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
2023 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
2024 Result.c_str(), Result.size());
2025 return;
2026 }
2027
2028 // FIXME: This has potential of causing problem. If
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002029 // SynthesizeObjCInternalStruct is ever called recursively.
Fariborz Jahanian2c7038b2007-11-26 19:52:57 +00002030 Result += "\nstruct ";
2031 Result += CDecl->getName();
Steve Narofffea763e82007-11-14 19:25:57 +00002032
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002033 if (NumIvars > 0) {
Steve Narofffea763e82007-11-14 19:25:57 +00002034 const char *cursor = strchr(startBuf, '{');
2035 assert((cursor && endBuf)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002036 && "SynthesizeObjCInternalStruct - malformed @interface");
Steve Narofffea763e82007-11-14 19:25:57 +00002037
2038 // rewrite the original header *without* disturbing the '{'
2039 Rewrite.ReplaceText(LocStart, cursor-startBuf-1,
2040 Result.c_str(), Result.size());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002041 if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
Steve Narofffea763e82007-11-14 19:25:57 +00002042 Result = "\n struct ";
2043 Result += RCDecl->getName();
Steve Naroff75ae7762007-12-07 22:15:58 +00002044 // Note: We don't name the field decl. This simplifies the "codegen" for
2045 // accessing a superclasses instance variables (and is similar to what gcc
2046 // does internally). The unnamed struct field feature is enabled with
2047 // -fms-extensions. If the struct definition were "inlined", we wouldn't
2048 // need to use this switch. That said, I don't want to inline the def.
Steve Narofffea763e82007-11-14 19:25:57 +00002049 Result += ";\n";
2050
2051 // insert the super class structure definition.
2052 SourceLocation OnePastCurly = LocStart.getFileLocWithOffset(cursor-startBuf+1);
2053 Rewrite.InsertText(OnePastCurly, Result.c_str(), Result.size());
2054 }
2055 cursor++; // past '{'
2056
2057 // Now comment out any visibility specifiers.
2058 while (cursor < endBuf) {
2059 if (*cursor == '@') {
2060 SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
Chris Lattnerdf6a51b2007-11-14 22:57:51 +00002061 // Skip whitespace.
2062 for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor)
2063 /*scan*/;
2064
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002065 // FIXME: presence of @public, etc. inside comment results in
2066 // this transformation as well, which is still correct c-code.
Steve Narofffea763e82007-11-14 19:25:57 +00002067 if (!strncmp(cursor, "public", strlen("public")) ||
2068 !strncmp(cursor, "private", strlen("private")) ||
Fariborz Jahanian95673922007-11-14 22:26:25 +00002069 !strncmp(cursor, "protected", strlen("protected")))
Steve Narofffea763e82007-11-14 19:25:57 +00002070 Rewrite.InsertText(atLoc, "// ", 3);
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002071 }
Fariborz Jahanian95673922007-11-14 22:26:25 +00002072 // FIXME: If there are cases where '<' is used in ivar declaration part
2073 // of user code, then scan the ivar list and use needToScanForQualifiers
2074 // for type checking.
2075 else if (*cursor == '<') {
2076 SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
2077 Rewrite.InsertText(atLoc, "/* ", 3);
2078 cursor = strchr(cursor, '>');
2079 cursor++;
2080 atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
2081 Rewrite.InsertText(atLoc, " */", 3);
2082 }
Steve Narofffea763e82007-11-14 19:25:57 +00002083 cursor++;
Fariborz Jahanianfdc08a02007-10-31 17:29:28 +00002084 }
Steve Narofffea763e82007-11-14 19:25:57 +00002085 // Don't forget to add a ';'!!
2086 Rewrite.InsertText(LocEnd.getFileLocWithOffset(1), ";", 1);
2087 } else { // we don't have any instance variables - insert super struct.
2088 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
2089 Result += " {\n struct ";
2090 Result += RCDecl->getName();
Steve Naroff75ae7762007-12-07 22:15:58 +00002091 // Note: We don't name the field decl. This simplifies the "codegen" for
2092 // accessing a superclasses instance variables (and is similar to what gcc
2093 // does internally). The unnamed struct field feature is enabled with
2094 // -fms-extensions. If the struct definition were "inlined", we wouldn't
2095 // need to use this switch. That said, I don't want to inline the def.
Steve Narofffea763e82007-11-14 19:25:57 +00002096 Result += ";\n};\n";
2097 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
2098 Result.c_str(), Result.size());
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002099 }
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002100 // Mark this struct as having been generated.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002101 if (!ObjCSynthesizedStructs.insert(CDecl))
2102 assert(false && "struct already synthesize- SynthesizeObjCInternalStruct");
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002103}
2104
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002105// RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002106/// class methods.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002107void RewriteTest::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002108 instmeth_iterator MethodEnd,
Fariborz Jahanian8e991ba2007-10-25 00:14:44 +00002109 bool IsInstanceMethod,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002110 const char *prefix,
Chris Lattner158ecb92007-10-25 17:07:24 +00002111 const char *ClassName,
2112 std::string &Result) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002113 if (MethodBegin == MethodEnd) return;
2114
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002115 static bool objc_impl_method = false;
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002116 if (!objc_impl_method) {
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002117 /* struct _objc_method {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002118 SEL _cmd;
2119 char *method_types;
2120 void *_imp;
2121 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002122 */
Chris Lattner158ecb92007-10-25 17:07:24 +00002123 Result += "\nstruct _objc_method {\n";
2124 Result += "\tSEL _cmd;\n";
2125 Result += "\tchar *method_types;\n";
2126 Result += "\tvoid *_imp;\n";
2127 Result += "};\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002128
2129 /* struct _objc_method_list {
2130 struct _objc_method_list *next_method;
2131 int method_count;
2132 struct _objc_method method_list[];
2133 }
2134 */
2135 Result += "\nstruct _objc_method_list {\n";
2136 Result += "\tstruct _objc_method_list *next_method;\n";
2137 Result += "\tint method_count;\n";
2138 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002139 objc_impl_method = true;
Fariborz Jahanian776d6ff2007-10-19 00:36:46 +00002140 }
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002141
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002142 // Build _objc_method_list for class's methods if needed
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002143 Result += "\nstatic struct _objc_method_list _OBJC_";
2144 Result += prefix;
2145 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
2146 Result += "_METHODS_";
2147 Result += ClassName;
2148 Result += " __attribute__ ((section (\"__OBJC, __";
2149 Result += IsInstanceMethod ? "inst" : "cls";
2150 Result += "_meth\")))= ";
2151 Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002152
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002153 Result += "\t,{{(SEL)\"";
2154 Result += (*MethodBegin)->getSelector().getName().c_str();
2155 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002156 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002157 Result += "\", \"";
2158 Result += MethodTypeString;
2159 Result += "\", ";
2160 Result += MethodInternalNames[*MethodBegin];
2161 Result += "}\n";
2162 for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
2163 Result += "\t ,{(SEL)\"";
2164 Result += (*MethodBegin)->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002165 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002166 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002167 Result += "\", \"";
2168 Result += MethodTypeString;
Fariborz Jahanianb7908b52007-11-13 21:02:00 +00002169 Result += "\", ";
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002170 Result += MethodInternalNames[*MethodBegin];
Fariborz Jahanianb7908b52007-11-13 21:02:00 +00002171 Result += "}\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002172 }
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002173 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002174}
2175
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002176/// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data.
2177void RewriteTest::RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols,
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002178 int NumProtocols,
2179 const char *prefix,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002180 const char *ClassName,
2181 std::string &Result) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002182 static bool objc_protocol_methods = false;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002183 if (NumProtocols > 0) {
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002184 for (int i = 0; i < NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002185 ObjCProtocolDecl *PDecl = Protocols[i];
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002186 // Output struct protocol_methods holder of method selector and type.
2187 if (!objc_protocol_methods &&
2188 (PDecl->getNumInstanceMethods() > 0
2189 || PDecl->getNumClassMethods() > 0)) {
2190 /* struct protocol_methods {
2191 SEL _cmd;
2192 char *method_types;
2193 }
2194 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002195 Result += "\nstruct protocol_methods {\n";
2196 Result += "\tSEL _cmd;\n";
2197 Result += "\tchar *method_types;\n";
2198 Result += "};\n";
2199
2200 /* struct _objc_protocol_method_list {
2201 int protocol_method_count;
2202 struct protocol_methods protocols[];
2203 }
2204 */
2205 Result += "\nstruct _objc_protocol_method_list {\n";
2206 Result += "\tint protocol_method_count;\n";
2207 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002208 objc_protocol_methods = true;
2209 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002210
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002211 int NumMethods = PDecl->getNumInstanceMethods();
2212 if(NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002213 Result += "\nstatic struct _objc_protocol_method_list "
2214 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
2215 Result += PDecl->getName();
2216 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
2217 "{\n\t" + utostr(NumMethods) + "\n";
2218
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002219 // Output instance methods declared in this protocol.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002220 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002221 E = PDecl->instmeth_end(); I != E; ++I) {
2222 if (I == PDecl->instmeth_begin())
2223 Result += "\t ,{{(SEL)\"";
2224 else
2225 Result += "\t ,{(SEL)\"";
2226 Result += (*I)->getSelector().getName().c_str();
2227 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002228 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
Fariborz Jahanianfc591ac2007-12-17 17:56:10 +00002229 Result += "\", \"";
2230 Result += MethodTypeString;
2231 Result += "\"}\n";
2232 }
2233 Result += "\t }\n};\n";
2234 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002235
2236 // Output class methods declared in this protocol.
2237 NumMethods = PDecl->getNumClassMethods();
2238 if (NumMethods > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002239 Result += "\nstatic struct _objc_protocol_method_list "
2240 "_OBJC_PROTOCOL_CLASS_METHODS_";
2241 Result += PDecl->getName();
2242 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
2243 "{\n\t";
2244 Result += utostr(NumMethods);
2245 Result += "\n";
2246
Fariborz Jahanianc6e2c2a2007-12-17 18:07:01 +00002247 // Output instance methods declared in this protocol.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002248 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Fariborz Jahanianc6e2c2a2007-12-17 18:07:01 +00002249 E = PDecl->classmeth_end(); I != E; ++I) {
2250 if (I == PDecl->classmeth_begin())
2251 Result += "\t ,{{(SEL)\"";
2252 else
2253 Result += "\t ,{(SEL)\"";
Steve Naroff58dbdeb2007-12-14 23:37:57 +00002254 Result += (*I)->getSelector().getName().c_str();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002255 std::string MethodTypeString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002256 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002257 Result += "\", \"";
2258 Result += MethodTypeString;
2259 Result += "\"}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002260 }
2261 Result += "\t }\n};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002262 }
2263 // Output:
2264 /* struct _objc_protocol {
2265 // Objective-C 1.0 extensions
2266 struct _objc_protocol_extension *isa;
2267 char *protocol_name;
2268 struct _objc_protocol **protocol_list;
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002269 struct _objc_protocol_method_list *instance_methods;
2270 struct _objc_protocol_method_list *class_methods;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002271 };
2272 */
2273 static bool objc_protocol = false;
2274 if (!objc_protocol) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002275 Result += "\nstruct _objc_protocol {\n";
2276 Result += "\tstruct _objc_protocol_extension *isa;\n";
2277 Result += "\tchar *protocol_name;\n";
2278 Result += "\tstruct _objc_protocol **protocol_list;\n";
2279 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
2280 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
2281 Result += "};\n";
2282
2283 /* struct _objc_protocol_list {
2284 struct _objc_protocol_list *next;
2285 int protocol_count;
2286 struct _objc_protocol *class_protocols[];
2287 }
2288 */
2289 Result += "\nstruct _objc_protocol_list {\n";
2290 Result += "\tstruct _objc_protocol_list *next;\n";
2291 Result += "\tint protocol_count;\n";
2292 Result += "\tstruct _objc_protocol *class_protocols[];\n";
2293 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002294 objc_protocol = true;
2295 }
2296
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002297 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
2298 Result += PDecl->getName();
2299 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
2300 "{\n\t0, \"";
2301 Result += PDecl->getName();
2302 Result += "\", 0, ";
Steve Naroff58dbdeb2007-12-14 23:37:57 +00002303 if (PDecl->getNumInstanceMethods() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002304 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
2305 Result += PDecl->getName();
2306 Result += ", ";
2307 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002308 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002309 Result += "0, ";
Steve Naroff58dbdeb2007-12-14 23:37:57 +00002310 if (PDecl->getNumClassMethods() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002311 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
2312 Result += PDecl->getName();
2313 Result += "\n";
2314 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002315 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002316 Result += "0\n";
2317 Result += "};\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002318 }
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002319 // Output the top lovel protocol meta-data for the class.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002320 Result += "\nstatic struct _objc_protocol_list _OBJC_";
2321 Result += prefix;
2322 Result += "_PROTOCOLS_";
2323 Result += ClassName;
2324 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
2325 "{\n\t0, ";
2326 Result += utostr(NumProtocols);
2327 Result += "\n";
2328
2329 Result += "\t,{&_OBJC_PROTOCOL_";
2330 Result += Protocols[0]->getName();
2331 Result += " \n";
2332
2333 for (int i = 1; i < NumProtocols; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002334 ObjCProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002335 Result += "\t ,&_OBJC_PROTOCOL_";
2336 Result += PDecl->getName();
2337 Result += "\n";
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002338 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002339 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002340 }
2341}
2342
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002343/// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002344/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002345void RewriteTest::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002346 std::string &Result) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002347 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002348 // Find category declaration for this implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002349 ObjCCategoryDecl *CDecl;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002350 for (CDecl = ClassDecl->getCategoryList(); CDecl;
2351 CDecl = CDecl->getNextClassCategory())
2352 if (CDecl->getIdentifier() == IDecl->getIdentifier())
2353 break;
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002354
Chris Lattnereb44eee2007-12-23 01:40:15 +00002355 std::string FullCategoryName = ClassDecl->getName();
2356 FullCategoryName += '_';
2357 FullCategoryName += IDecl->getName();
2358
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002359 // Build _objc_method_list for class's instance methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002360 RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
Chris Lattnereb44eee2007-12-23 01:40:15 +00002361 true, "CATEGORY_", FullCategoryName.c_str(),
2362 Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002363
2364 // Build _objc_method_list for class's class methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002365 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
Chris Lattnereb44eee2007-12-23 01:40:15 +00002366 false, "CATEGORY_", FullCategoryName.c_str(),
2367 Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002368
2369 // Protocols referenced in class declaration?
Fariborz Jahanianbac97d42007-11-13 22:09:49 +00002370 // Null CDecl is case of a category implementation with no category interface
2371 if (CDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002372 RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
Fariborz Jahanianbac97d42007-11-13 22:09:49 +00002373 CDecl->getNumReferencedProtocols(),
2374 "CATEGORY",
Chris Lattnereb44eee2007-12-23 01:40:15 +00002375 FullCategoryName.c_str(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002376
2377 /* struct _objc_category {
2378 char *category_name;
2379 char *class_name;
2380 struct _objc_method_list *instance_methods;
2381 struct _objc_method_list *class_methods;
2382 struct _objc_protocol_list *protocols;
2383 // Objective-C 1.0 extensions
2384 uint32_t size; // sizeof (struct _objc_category)
2385 struct _objc_property_list *instance_properties; // category's own
2386 // @property decl.
2387 };
2388 */
2389
2390 static bool objc_category = false;
2391 if (!objc_category) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002392 Result += "\nstruct _objc_category {\n";
2393 Result += "\tchar *category_name;\n";
2394 Result += "\tchar *class_name;\n";
2395 Result += "\tstruct _objc_method_list *instance_methods;\n";
2396 Result += "\tstruct _objc_method_list *class_methods;\n";
2397 Result += "\tstruct _objc_protocol_list *protocols;\n";
2398 Result += "\tunsigned int size;\n";
2399 Result += "\tstruct _objc_property_list *instance_properties;\n";
2400 Result += "};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002401 objc_category = true;
Fariborz Jahaniane887c092007-10-22 21:41:37 +00002402 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002403 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
2404 Result += FullCategoryName;
2405 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
2406 Result += IDecl->getName();
2407 Result += "\"\n\t, \"";
2408 Result += ClassDecl->getName();
2409 Result += "\"\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002410
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002411 if (IDecl->getNumInstanceMethods() > 0) {
2412 Result += "\t, (struct _objc_method_list *)"
2413 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
2414 Result += FullCategoryName;
2415 Result += "\n";
2416 }
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002417 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002418 Result += "\t, 0\n";
2419 if (IDecl->getNumClassMethods() > 0) {
2420 Result += "\t, (struct _objc_method_list *)"
2421 "&_OBJC_CATEGORY_CLASS_METHODS_";
2422 Result += FullCategoryName;
2423 Result += "\n";
2424 }
2425 else
2426 Result += "\t, 0\n";
2427
Fariborz Jahanianbac97d42007-11-13 22:09:49 +00002428 if (CDecl && CDecl->getNumReferencedProtocols() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002429 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
2430 Result += FullCategoryName;
2431 Result += "\n";
2432 }
2433 else
2434 Result += "\t, 0\n";
2435 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002436}
2437
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002438/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
2439/// ivar offset.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002440void RewriteTest::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
2441 ObjCIvarDecl *ivar,
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002442 std::string &Result) {
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00002443 Result += "offsetof(struct ";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002444 Result += IDecl->getName();
2445 Result += ", ";
2446 Result += ivar->getName();
2447 Result += ")";
2448}
2449
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002450//===----------------------------------------------------------------------===//
2451// Meta Data Emission
2452//===----------------------------------------------------------------------===//
2453
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002454void RewriteTest::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002455 std::string &Result) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002456 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002457
Fariborz Jahanianebe668f2007-11-26 20:59:57 +00002458 // Explictly declared @interface's are already synthesized.
2459 if (CDecl->ImplicitInterfaceDecl()) {
2460 // FIXME: Implementation of a class with no @interface (legacy) doese not
2461 // produce correct synthesis as yet.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002462 SynthesizeObjCInternalStruct(CDecl, Result);
Fariborz Jahanianebe668f2007-11-26 20:59:57 +00002463 }
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002464
Chris Lattnerbe6df082007-12-12 07:56:42 +00002465 // Build _objc_ivar_list metadata for classes ivars if needed
2466 int NumIvars = IDecl->getImplDeclNumIvars() > 0
2467 ? IDecl->getImplDeclNumIvars()
2468 : (CDecl ? CDecl->getNumInstanceVariables() : 0);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002469 if (NumIvars > 0) {
2470 static bool objc_ivar = false;
2471 if (!objc_ivar) {
2472 /* struct _objc_ivar {
2473 char *ivar_name;
2474 char *ivar_type;
2475 int ivar_offset;
2476 };
2477 */
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002478 Result += "\nstruct _objc_ivar {\n";
2479 Result += "\tchar *ivar_name;\n";
2480 Result += "\tchar *ivar_type;\n";
2481 Result += "\tint ivar_offset;\n";
2482 Result += "};\n";
2483
2484 /* struct _objc_ivar_list {
2485 int ivar_count;
2486 struct _objc_ivar ivar_list[];
2487 };
2488 */
2489 Result += "\nstruct _objc_ivar_list {\n";
2490 Result += "\tint ivar_count;\n";
2491 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002492 objc_ivar = true;
2493 }
2494
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002495 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
2496 Result += IDecl->getName();
2497 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
2498 "{\n\t";
2499 Result += utostr(NumIvars);
2500 Result += "\n";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002501
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002502 ObjCInterfaceDecl::ivar_iterator IVI, IVE;
Chris Lattnerbe6df082007-12-12 07:56:42 +00002503 if (IDecl->getImplDeclNumIvars() > 0) {
2504 IVI = IDecl->ivar_begin();
2505 IVE = IDecl->ivar_end();
2506 } else {
2507 IVI = CDecl->ivar_begin();
2508 IVE = CDecl->ivar_end();
2509 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002510 Result += "\t,{{\"";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002511 Result += (*IVI)->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002512 Result += "\", \"";
2513 std::string StrEncoding;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002514 Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding);
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002515 Result += StrEncoding;
2516 Result += "\", ";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002517 SynthesizeIvarOffsetComputation(IDecl, *IVI, Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002518 Result += "}\n";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002519 for (++IVI; IVI != IVE; ++IVI) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002520 Result += "\t ,{\"";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002521 Result += (*IVI)->getName();
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002522 Result += "\", \"";
2523 std::string StrEncoding;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002524 Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding);
Fariborz Jahanian160eb652007-10-29 17:16:25 +00002525 Result += StrEncoding;
2526 Result += "\", ";
Chris Lattnerbe6df082007-12-12 07:56:42 +00002527 SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002528 Result += "}\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002529 }
2530
2531 Result += "\t }\n};\n";
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002532 }
2533
2534 // Build _objc_method_list for class's instance methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002535 RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002536 true, "", IDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002537
2538 // Build _objc_method_list for class's class methods if needed
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002539 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002540 false, "", IDecl->getName(), Result);
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002541
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002542 // Protocols referenced in class declaration?
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002543 RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002544 CDecl->getNumIntfRefProtocols(),
Chris Lattnerab4c4d52007-12-12 07:46:12 +00002545 "CLASS", CDecl->getName(), Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002546
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002547
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002548 // Declaration of class/meta-class metadata
2549 /* struct _objc_class {
2550 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002551 const char *super_class_name;
2552 char *name;
2553 long version;
2554 long info;
2555 long instance_size;
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002556 struct _objc_ivar_list *ivars;
2557 struct _objc_method_list *methods;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002558 struct objc_cache *cache;
2559 struct objc_protocol_list *protocols;
2560 const char *ivar_layout;
2561 struct _objc_class_ext *ext;
2562 };
2563 */
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002564 static bool objc_class = false;
2565 if (!objc_class) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002566 Result += "\nstruct _objc_class {\n";
2567 Result += "\tstruct _objc_class *isa;\n";
2568 Result += "\tconst char *super_class_name;\n";
2569 Result += "\tchar *name;\n";
2570 Result += "\tlong version;\n";
2571 Result += "\tlong info;\n";
2572 Result += "\tlong instance_size;\n";
2573 Result += "\tstruct _objc_ivar_list *ivars;\n";
2574 Result += "\tstruct _objc_method_list *methods;\n";
2575 Result += "\tstruct objc_cache *cache;\n";
2576 Result += "\tstruct _objc_protocol_list *protocols;\n";
2577 Result += "\tconst char *ivar_layout;\n";
2578 Result += "\tstruct _objc_class_ext *ext;\n";
2579 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002580 objc_class = true;
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002581 }
2582
2583 // Meta-class metadata generation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002584 ObjCInterfaceDecl *RootClass = 0;
2585 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002586 while (SuperClass) {
2587 RootClass = SuperClass;
2588 SuperClass = SuperClass->getSuperClass();
2589 }
2590 SuperClass = CDecl->getSuperClass();
2591
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002592 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
2593 Result += CDecl->getName();
2594 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
2595 "{\n\t(struct _objc_class *)\"";
2596 Result += (RootClass ? RootClass->getName() : CDecl->getName());
2597 Result += "\"";
2598
2599 if (SuperClass) {
2600 Result += ", \"";
2601 Result += SuperClass->getName();
2602 Result += "\", \"";
2603 Result += CDecl->getName();
2604 Result += "\"";
2605 }
2606 else {
2607 Result += ", 0, \"";
2608 Result += CDecl->getName();
2609 Result += "\"";
2610 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002611 // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it.
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002612 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002613 Result += ", 0,2, sizeof(struct _objc_class), 0";
Steve Naroffb26d7132007-12-05 21:49:40 +00002614 if (IDecl->getNumClassMethods() > 0) {
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002615 Result += "\n\t, &_OBJC_CLASS_METHODS_";
Steve Naroffb26d7132007-12-05 21:49:40 +00002616 Result += IDecl->getName();
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002617 Result += "\n";
2618 }
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002619 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002620 Result += ", 0\n";
2621 if (CDecl->getNumIntfRefProtocols() > 0) {
2622 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
2623 Result += CDecl->getName();
2624 Result += ",0,0\n";
2625 }
Fariborz Jahanian454cb012007-10-24 20:54:23 +00002626 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002627 Result += "\t,0,0,0,0\n";
2628 Result += "};\n";
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002629
2630 // class metadata generation.
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002631 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
2632 Result += CDecl->getName();
2633 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
2634 "{\n\t&_OBJC_METACLASS_";
2635 Result += CDecl->getName();
2636 if (SuperClass) {
2637 Result += ", \"";
2638 Result += SuperClass->getName();
2639 Result += "\", \"";
2640 Result += CDecl->getName();
2641 Result += "\"";
2642 }
2643 else {
2644 Result += ", 0, \"";
2645 Result += CDecl->getName();
2646 Result += "\"";
2647 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002648 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002649 Result += ", 0,1";
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002650 if (!ObjCSynthesizedStructs.count(CDecl))
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002651 Result += ",0";
2652 else {
2653 // class has size. Must synthesize its size.
Fariborz Jahanian909f02a2007-11-05 17:47:33 +00002654 Result += ",sizeof(struct ";
Fariborz Jahanian4d733d32007-10-26 23:09:28 +00002655 Result += CDecl->getName();
2656 Result += ")";
2657 }
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002658 if (NumIvars > 0) {
2659 Result += ", &_OBJC_INSTANCE_VARIABLES_";
2660 Result += CDecl->getName();
2661 Result += "\n\t";
2662 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002663 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002664 Result += ",0";
2665 if (IDecl->getNumInstanceMethods() > 0) {
2666 Result += ", &_OBJC_INSTANCE_METHODS_";
2667 Result += CDecl->getName();
2668 Result += ", 0\n\t";
2669 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002670 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002671 Result += ",0,0";
2672 if (CDecl->getNumIntfRefProtocols() > 0) {
2673 Result += ", &_OBJC_CLASS_PROTOCOLS_";
2674 Result += CDecl->getName();
2675 Result += ", 0,0\n";
2676 }
Fariborz Jahaniandeef5182007-10-23 18:53:48 +00002677 else
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002678 Result += ",0,0,0\n";
2679 Result += "};\n";
Fariborz Jahanian9f0a1cb2007-10-23 00:02:02 +00002680}
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002681
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002682/// RewriteImplementations - This routine rewrites all method implementations
2683/// and emits meta-data.
2684
2685void RewriteTest::RewriteImplementations(std::string &Result) {
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002686 int ClsDefCount = ClassImplementation.size();
2687 int CatDefCount = CategoryImplementation.size();
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002688
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002689 if (ClsDefCount == 0 && CatDefCount == 0)
2690 return;
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002691 // Rewrite implemented methods
2692 for (int i = 0; i < ClsDefCount; i++)
2693 RewriteImplementationDecl(ClassImplementation[i]);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002694
Fariborz Jahanian66d6b292007-11-13 20:04:28 +00002695 for (int i = 0; i < CatDefCount; i++)
2696 RewriteImplementationDecl(CategoryImplementation[i]);
2697
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002698 // This is needed for use of offsetof
2699 Result += "#include <stddef.h>\n";
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002700
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002701 // For each implemented class, write out all its meta data.
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002702 for (int i = 0; i < ClsDefCount; i++)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002703 RewriteObjCClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian2e6d9352007-10-24 19:23:36 +00002704
2705 // For each implemented category, write out all its meta data.
2706 for (int i = 0; i < CatDefCount; i++)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002707 RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanianf4d331d2007-10-18 22:09:03 +00002708
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002709 // Write objc_symtab metadata
2710 /*
2711 struct _objc_symtab
2712 {
2713 long sel_ref_cnt;
2714 SEL *refs;
2715 short cls_def_cnt;
2716 short cat_def_cnt;
2717 void *defs[cls_def_cnt + cat_def_cnt];
2718 };
2719 */
2720
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002721 Result += "\nstruct _objc_symtab {\n";
2722 Result += "\tlong sel_ref_cnt;\n";
2723 Result += "\tSEL *refs;\n";
2724 Result += "\tshort cls_def_cnt;\n";
2725 Result += "\tshort cat_def_cnt;\n";
2726 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
2727 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002728
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002729 Result += "static struct _objc_symtab "
2730 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
2731 Result += "\t0, 0, " + utostr(ClsDefCount)
2732 + ", " + utostr(CatDefCount) + "\n";
2733 for (int i = 0; i < ClsDefCount; i++) {
2734 Result += "\t,&_OBJC_CLASS_";
2735 Result += ClassImplementation[i]->getName();
2736 Result += "\n";
2737 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002738
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002739 for (int i = 0; i < CatDefCount; i++) {
2740 Result += "\t,&_OBJC_CATEGORY_";
2741 Result += CategoryImplementation[i]->getClassInterface()->getName();
2742 Result += "_";
2743 Result += CategoryImplementation[i]->getName();
2744 Result += "\n";
2745 }
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002746
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002747 Result += "};\n\n";
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002748
2749 // Write objc_module metadata
2750
2751 /*
2752 struct _objc_module {
2753 long version;
2754 long size;
2755 const char *name;
2756 struct _objc_symtab *symtab;
2757 }
2758 */
2759
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002760 Result += "\nstruct _objc_module {\n";
2761 Result += "\tlong version;\n";
2762 Result += "\tlong size;\n";
2763 Result += "\tconst char *name;\n";
2764 Result += "\tstruct _objc_symtab *symtab;\n";
2765 Result += "};\n\n";
2766 Result += "static struct _objc_module "
2767 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +00002768 Result += "\t" + utostr(OBJC_ABI_VERSION) +
2769 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahanianccd87b02007-10-25 20:55:25 +00002770 Result += "};\n\n";
Fariborz Jahanian7a3279d2007-11-13 19:21:13 +00002771
Fariborz Jahanian545b9ae2007-10-18 19:23:00 +00002772}
Chris Lattner311ff022007-10-16 22:36:42 +00002773