blob: fb52dfe29f75bab40a6f7466549b34e49cd65412 [file] [log] [blame]
Chris Lattnerb429ae42007-10-11 00:43:27 +00001//===--- RewriteTest.cpp - Playground for the code rewriter ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattnerb429ae42007-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 Lattner569faa62007-10-11 18:38:32 +000015#include "clang/Rewrite/Rewriter.h"
Chris Lattnerb429ae42007-10-11 00:43:27 +000016#include "clang/AST/AST.h"
17#include "clang/AST/ASTConsumer.h"
Chris Lattner569faa62007-10-11 18:38:32 +000018#include "clang/Basic/SourceManager.h"
Steve Naroffe9780582007-10-23 23:50:29 +000019#include "clang/Basic/IdentifierTable.h"
Chris Lattner4478db92007-11-30 22:53:43 +000020#include "clang/Basic/Diagnostic.h"
Chris Lattnerae43eb72007-12-02 01:13:47 +000021#include "clang/Lex/Lexer.h"
Chris Lattnerc3aa5c42007-10-25 17:07:24 +000022#include "llvm/ADT/StringExtras.h"
Fariborz Jahanianf185aef2007-10-26 19:46:17 +000023#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerae43eb72007-12-02 01:13:47 +000024#include "llvm/Support/MemoryBuffer.h"
Steve Naroff764c1ae2007-11-15 10:28:18 +000025#include <sstream>
Chris Lattnerb429ae42007-10-11 00:43:27 +000026using namespace clang;
Chris Lattnerc3aa5c42007-10-25 17:07:24 +000027using llvm::utostr;
Chris Lattnerb429ae42007-10-11 00:43:27 +000028
Chris Lattnerb429ae42007-10-11 00:43:27 +000029namespace {
Chris Lattner569faa62007-10-11 18:38:32 +000030 class RewriteTest : public ASTConsumer {
Chris Lattner74db1682007-10-16 21:07:07 +000031 Rewriter Rewrite;
Chris Lattner258f26c2007-11-30 22:25:36 +000032 Diagnostic &Diags;
Chris Lattnerbf0bfa62007-10-17 22:35:30 +000033 ASTContext *Context;
Chris Lattnerb429ae42007-10-11 00:43:27 +000034 SourceManager *SM;
Chris Lattner569faa62007-10-11 18:38:32 +000035 unsigned MainFileID;
Chris Lattnerae43eb72007-12-02 01:13:47 +000036 const char *MainFileStart, *MainFileEnd;
Chris Lattner74db1682007-10-16 21:07:07 +000037 SourceLocation LastIncLoc;
Ted Kremenek42730c52008-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 Jahanian13dad332008-01-15 23:58:23 +000043 llvm::SmallVector<Stmt *, 32> Stmts;
44 llvm::SmallVector<int, 8> ObjCBcLabelNo;
Steve Naroffe9780582007-10-23 23:50:29 +000045
46 FunctionDecl *MsgSendFunctionDecl;
Steve Naroff764c1ae2007-11-15 10:28:18 +000047 FunctionDecl *MsgSendSuperFunctionDecl;
Fariborz Jahanianc26b2502007-12-03 19:17:29 +000048 FunctionDecl *MsgSendStretFunctionDecl;
49 FunctionDecl *MsgSendSuperStretFunctionDecl;
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +000050 FunctionDecl *MsgSendFpretFunctionDecl;
Steve Naroffe9780582007-10-23 23:50:29 +000051 FunctionDecl *GetClassFunctionDecl;
Steve Naroff3b1caac2007-12-07 03:50:46 +000052 FunctionDecl *GetMetaClassFunctionDecl;
Steve Naroff71226032007-10-24 22:48:43 +000053 FunctionDecl *SelGetUidFunctionDecl;
Steve Naroffabb96362007-11-08 14:30:50 +000054 FunctionDecl *CFStringFunctionDecl;
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +000055 FunctionDecl *GetProtocolFunctionDecl;
Fariborz Jahanianc26b2502007-12-03 19:17:29 +000056
Steve Naroff0add5d22007-11-03 11:27:19 +000057 // ObjC string constant support.
58 FileVarDecl *ConstantStringClassReference;
59 RecordDecl *NSStringRecord;
Steve Naroff0744c472007-11-04 22:37:50 +000060
Fariborz Jahanian22277422008-01-16 00:09:11 +000061 // ObjC foreach break/continue generation support.
Fariborz Jahanian13dad332008-01-15 23:58:23 +000062 int BcLabelCount;
63
Steve Naroff764c1ae2007-11-15 10:28:18 +000064 // Needed for super.
Ted Kremenek42730c52008-01-07 19:49:32 +000065 ObjCMethodDecl *CurMethodDecl;
Steve Naroff764c1ae2007-11-15 10:28:18 +000066 RecordDecl *SuperStructDecl;
67
Fariborz Jahanian8d2080c2008-01-18 01:15:54 +000068 // Needed for header files being rewritten
69 bool IsHeader;
70
Fariborz Jahanian640a01f2007-10-18 19:23:00 +000071 static const int OBJC_ABI_VERSION =7 ;
Chris Lattnerb429ae42007-10-11 00:43:27 +000072 public:
Ted Kremenek17861c52007-12-19 22:51:13 +000073 void Initialize(ASTContext &context) {
Chris Lattnerbf0bfa62007-10-17 22:35:30 +000074 Context = &context;
Ted Kremenekb3ee1932007-12-11 21:27:55 +000075 SM = &Context->getSourceManager();
Steve Naroffe9780582007-10-23 23:50:29 +000076 MsgSendFunctionDecl = 0;
Steve Naroff764c1ae2007-11-15 10:28:18 +000077 MsgSendSuperFunctionDecl = 0;
Fariborz Jahanianc26b2502007-12-03 19:17:29 +000078 MsgSendStretFunctionDecl = 0;
79 MsgSendSuperStretFunctionDecl = 0;
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +000080 MsgSendFpretFunctionDecl = 0;
Steve Naroff95b28c12007-10-24 01:09:48 +000081 GetClassFunctionDecl = 0;
Steve Naroff3b1caac2007-12-07 03:50:46 +000082 GetMetaClassFunctionDecl = 0;
Steve Naroff71226032007-10-24 22:48:43 +000083 SelGetUidFunctionDecl = 0;
Steve Naroffabb96362007-11-08 14:30:50 +000084 CFStringFunctionDecl = 0;
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +000085 GetProtocolFunctionDecl = 0;
Steve Naroff0add5d22007-11-03 11:27:19 +000086 ConstantStringClassReference = 0;
87 NSStringRecord = 0;
Steve Naroff764c1ae2007-11-15 10:28:18 +000088 CurMethodDecl = 0;
89 SuperStructDecl = 0;
Fariborz Jahanian13dad332008-01-15 23:58:23 +000090 BcLabelCount = 0;
Steve Naroff764c1ae2007-11-15 10:28:18 +000091
Chris Lattnerae43eb72007-12-02 01:13:47 +000092 // Get the ID and start/end of the main file.
Ted Kremenek17861c52007-12-19 22:51:13 +000093 MainFileID = SM->getMainFileID();
Chris Lattnerae43eb72007-12-02 01:13:47 +000094 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
95 MainFileStart = MainBuf->getBufferStart();
96 MainFileEnd = MainBuf->getBufferEnd();
97
98
Ted Kremenekb3ee1932007-12-11 21:27:55 +000099 Rewrite.setSourceMgr(Context->getSourceManager());
Steve Narofffcab7932007-11-05 14:55:35 +0000100 // declaring objc_selector outside the parameter list removes a silly
101 // scope related warning...
Fariborz Jahanian8d2080c2008-01-18 01:15:54 +0000102 const char *s = "#pragma once\n"
103 "struct objc_selector; struct objc_class;\n"
Fariborz Jahanian6ff57c62007-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 Narofffcab7932007-11-05 14:55:35 +0000113 "extern struct objc_object *objc_msgSend"
Steve Naroff0744c472007-11-04 22:37:50 +0000114 "(struct objc_object *, struct objc_selector *, ...);\n"
Steve Naroff764c1ae2007-11-15 10:28:18 +0000115 "extern struct objc_object *objc_msgSendSuper"
116 "(struct objc_super *, struct objc_selector *, ...);\n"
Fariborz Jahanianc26b2502007-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 Jahanian1d29b5d2007-12-03 21:26:48 +0000121 "extern struct objc_object *objc_msgSend_fpret"
122 "(struct objc_object *, struct objc_selector *, ...);\n"
Steve Naroff0744c472007-11-04 22:37:50 +0000123 "extern struct objc_object *objc_getClass"
Steve Naroffd3287d82007-11-07 18:43:40 +0000124 "(const char *);\n"
Steve Naroff3b1caac2007-12-07 03:50:46 +0000125 "extern struct objc_object *objc_getMetaClass"
126 "(const char *);\n"
Steve Naroffd3287d82007-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 Jahanian8d9c7352007-11-14 22:26:25 +0000132 "(struct objc_class *, struct objc_object *, ...);\n"
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +0000133 "extern Protocol *objc_getProtocol(const char *);\n"
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000134 "#include <objc/objc.h>\n"
Fariborz Jahaniand8d36532008-01-10 23:04:06 +0000135 "#ifndef __FASTENUMERATIONSTATE\n"
Fariborz Jahanian9ea6a2d2008-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 Jahaniand8d36532008-01-10 23:04:06 +0000140 "unsigned long extra[5];\n};\n"
141 "#define __FASTENUMERATIONSTATE\n"
142 "#endif\n";
Fariborz Jahanian8d2080c2008-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 Lattnerb429ae42007-10-11 00:43:27 +0000154 }
Chris Lattner569faa62007-10-11 18:38:32 +0000155
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000156 // Top Level Driver code.
157 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattner74db1682007-10-16 21:07:07 +0000158 void HandleDeclInMainFile(Decl *D);
Fariborz Jahanian8d2080c2008-01-18 01:15:54 +0000159 RewriteTest(bool isHeader, Diagnostic &D) : Diags(D) {IsHeader = isHeader;}
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000160 ~RewriteTest();
161
162 // Syntactic Rewriting.
Steve Naroff0744c472007-11-04 22:37:50 +0000163 void RewritePrologue(SourceLocation Loc);
Chris Lattner74db1682007-10-16 21:07:07 +0000164 void RewriteInclude(SourceLocation Loc);
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000165 void RewriteTabs();
Ted Kremenek42730c52008-01-07 19:49:32 +0000166 void RewriteForwardClassDecl(ObjCClassDecl *Dcl);
167 void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
Fariborz Jahanian0136e372007-11-13 20:04:28 +0000168 void RewriteImplementationDecl(NamedDecl *Dcl);
Ted Kremenek42730c52008-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 Naroff02a82aa2007-10-30 23:14:51 +0000175 void RewriteFunctionDecl(FunctionDecl *FD);
Ted Kremenek42730c52008-01-07 19:49:32 +0000176 void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
Steve Naroffc8a92d12007-11-01 13:24:47 +0000177 bool needToScanForQualifiers(QualType T);
Ted Kremenek42730c52008-01-07 19:49:32 +0000178 ObjCInterfaceDecl *isSuperReceiver(Expr *recExpr);
Steve Naroff764c1ae2007-11-15 10:28:18 +0000179 QualType getSuperStructType();
Chris Lattner6fe8b272007-10-16 22:36:42 +0000180
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000181 // Expression Rewriting.
Steve Naroff334fbc22007-11-09 15:20:18 +0000182 Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
Chris Lattner0021f452007-10-24 16:57:36 +0000183 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
Steve Naroff6b759ce2007-11-15 02:58:25 +0000184 Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV);
Steve Naroff296b74f2007-11-05 14:50:49 +0000185 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
Chris Lattner0021f452007-10-24 16:57:36 +0000186 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
Steve Naroff0add5d22007-11-03 11:27:19 +0000187 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +0000188 Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
Ted Kremenek42730c52008-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 Jahanian955fcb82008-01-07 21:40:22 +0000193 Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S);
Steve Naroff71226032007-10-24 22:48:43 +0000194 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
195 Expr **args, unsigned nargs);
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000196 Stmt *SynthMessageExpr(ObjCMessageExpr *Exp);
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000197 Stmt *RewriteBreakStmt(BreakStmt *S);
198 Stmt *RewriteContinueStmt(ContinueStmt *S);
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000199 void SynthCountByEnumWithState(std::string &buf);
200
Steve Naroff02a82aa2007-10-30 23:14:51 +0000201 void SynthMsgSendFunctionDecl();
Steve Naroff764c1ae2007-11-15 10:28:18 +0000202 void SynthMsgSendSuperFunctionDecl();
Fariborz Jahanianc26b2502007-12-03 19:17:29 +0000203 void SynthMsgSendStretFunctionDecl();
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +0000204 void SynthMsgSendFpretFunctionDecl();
Fariborz Jahanianc26b2502007-12-03 19:17:29 +0000205 void SynthMsgSendSuperStretFunctionDecl();
Steve Naroff02a82aa2007-10-30 23:14:51 +0000206 void SynthGetClassFunctionDecl();
Steve Naroff3b1caac2007-12-07 03:50:46 +0000207 void SynthGetMetaClassFunctionDecl();
Steve Naroffabb96362007-11-08 14:30:50 +0000208 void SynthCFStringFunctionDecl();
Fariborz Jahanianfb8e9912007-12-04 21:47:40 +0000209 void SynthSelGetUidFunctionDecl();
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +0000210 void SynthGetProtocolFunctionDecl();
Steve Naroffabb96362007-11-08 14:30:50 +0000211
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000212 // Metadata emission.
Ted Kremenek42730c52008-01-07 19:49:32 +0000213 void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000214 std::string &Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000215
Ted Kremenek42730c52008-01-07 19:49:32 +0000216 void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000217 std::string &Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000218
Ted Kremenek42730c52008-01-07 19:49:32 +0000219 typedef ObjCCategoryImplDecl::instmeth_iterator instmeth_iterator;
220 void RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000221 instmeth_iterator MethodEnd,
Fariborz Jahaniana3986372007-10-25 00:14:44 +0000222 bool IsInstanceMethod,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000223 const char *prefix,
Chris Lattnerc3aa5c42007-10-25 17:07:24 +0000224 const char *ClassName,
225 std::string &Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000226
Ted Kremenek42730c52008-01-07 19:49:32 +0000227 void RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +0000228 int NumProtocols,
229 const char *prefix,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +0000230 const char *ClassName,
231 std::string &Result);
Ted Kremenek42730c52008-01-07 19:49:32 +0000232 void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000233 std::string &Result);
Ted Kremenek42730c52008-01-07 19:49:32 +0000234 void SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
235 ObjCIvarDecl *ivar,
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000236 std::string &Result);
Fariborz Jahanian8c664912007-11-13 19:21:13 +0000237 void RewriteImplementations(std::string &Result);
Chris Lattnerb429ae42007-10-11 00:43:27 +0000238 };
239}
240
Fariborz Jahanian8d2080c2008-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 Lattner258f26c2007-11-30 22:25:36 +0000258}
Chris Lattnerb429ae42007-10-11 00:43:27 +0000259
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000260//===----------------------------------------------------------------------===//
261// Top Level Driver Code
262//===----------------------------------------------------------------------===//
263
Chris Lattner569faa62007-10-11 18:38:32 +0000264void RewriteTest::HandleTopLevelDecl(Decl *D) {
Chris Lattner74db1682007-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 Naroffe9780582007-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 Naroff02a82aa2007-10-30 23:14:51 +0000276 RewriteFunctionDecl(FD);
Steve Naroff0add5d22007-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 Kremenek42730c52008-01-07 19:49:32 +0000283 } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) {
Steve Naroff3774dd92007-10-26 20:53:56 +0000284 RewriteInterfaceDecl(MD);
Ted Kremenek42730c52008-01-07 19:49:32 +0000285 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
Steve Naroff667f1682007-10-30 13:30:57 +0000286 RewriteCategoryDecl(CD);
Ted Kremenek42730c52008-01-07 19:49:32 +0000287 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000288 RewriteProtocolDecl(PD);
Ted Kremenek42730c52008-01-07 19:49:32 +0000289 } else if (ObjCForwardProtocolDecl *FP =
290 dyn_cast<ObjCForwardProtocolDecl>(D)){
Fariborz Jahanian016a1882007-11-14 00:42:16 +0000291 RewriteForwardProtocolDecl(FP);
Steve Naroffe9780582007-10-23 23:50:29 +0000292 }
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000293 // If we have a decl in the main file, see if we should rewrite it.
Chris Lattner74db1682007-10-16 21:07:07 +0000294 if (SM->getDecomposedFileLoc(Loc).first == MainFileID)
295 return HandleDeclInMainFile(D);
296
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000297 // Otherwise, see if there is a #import in the main file that should be
298 // rewritten.
Steve Naroff2aeae312007-11-09 12:50:28 +0000299 //RewriteInclude(Loc);
Chris Lattner74db1682007-10-16 21:07:07 +0000300}
301
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000302/// HandleDeclInMainFile - This is called for each top-level decl defined in the
303/// main file of the input.
304void RewriteTest::HandleDeclInMainFile(Decl *D) {
305 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
306 if (Stmt *Body = FD->getBody())
Steve Naroff334fbc22007-11-09 15:20:18 +0000307 FD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
Steve Naroff18c83382007-11-13 23:01:27 +0000308
Ted Kremenek42730c52008-01-07 19:49:32 +0000309 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Steve Naroff764c1ae2007-11-15 10:28:18 +0000310 if (Stmt *Body = MD->getBody()) {
311 //Body->dump();
312 CurMethodDecl = MD;
Steve Naroff18c83382007-11-13 23:01:27 +0000313 MD->setBody(RewriteFunctionBodyOrGlobalInitializer(Body));
Steve Naroff764c1ae2007-11-15 10:28:18 +0000314 CurMethodDecl = 0;
315 }
Steve Naroff18c83382007-11-13 23:01:27 +0000316 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000317 if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D))
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000318 ClassImplementation.push_back(CI);
Ted Kremenek42730c52008-01-07 19:49:32 +0000319 else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D))
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000320 CategoryImplementation.push_back(CI);
Ted Kremenek42730c52008-01-07 19:49:32 +0000321 else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D))
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000322 RewriteForwardClassDecl(CD);
Steve Naroff334fbc22007-11-09 15:20:18 +0000323 else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000324 RewriteObjCQualifiedInterfaceTypes(VD);
Steve Naroff334fbc22007-11-09 15:20:18 +0000325 if (VD->getInit())
326 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
327 }
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000328 // Nothing yet.
329}
330
331RewriteTest::~RewriteTest() {
332 // Get the top-level buffer that this corresponds to.
Chris Lattner257236c2007-11-08 04:27:23 +0000333
334 // Rewrite tabs if we care.
335 //RewriteTabs();
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000336
Fariborz Jahanian9447e462007-11-05 17:47:33 +0000337 // Rewrite Objective-c meta data*
338 std::string ResultStr;
Fariborz Jahanian8c664912007-11-13 19:21:13 +0000339 RewriteImplementations(ResultStr);
Fariborz Jahanian9447e462007-11-05 17:47:33 +0000340
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000341 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
342 // we are done.
343 if (const RewriteBuffer *RewriteBuf =
344 Rewrite.getRewriteBufferFor(MainFileID)) {
Steve Naroff0add5d22007-11-03 11:27:19 +0000345 //printf("Changed:\n");
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000346 std::string S(RewriteBuf->begin(), RewriteBuf->end());
347 printf("%s\n", S.c_str());
348 } else {
349 printf("No changes\n");
350 }
Fariborz Jahanian70ef5462007-11-07 18:40:28 +0000351 // Emit metadata.
352 printf("%s", ResultStr.c_str());
Fariborz Jahanianf185aef2007-10-26 19:46:17 +0000353}
354
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000355//===----------------------------------------------------------------------===//
356// Syntactic (non-AST) Rewriting Code
357//===----------------------------------------------------------------------===//
358
Chris Lattner74db1682007-10-16 21:07:07 +0000359void RewriteTest::RewriteInclude(SourceLocation Loc) {
360 // Rip up the #include stack to the main file.
361 SourceLocation IncLoc = Loc, NextLoc = Loc;
362 do {
363 IncLoc = Loc;
364 Loc = SM->getLogicalLoc(NextLoc);
365 NextLoc = SM->getIncludeLoc(Loc);
366 } while (!NextLoc.isInvalid());
367
368 // Loc is now the location of the #include filename "foo" or <foo/bar.h>.
369 // IncLoc indicates the header that was included if it is useful.
370 IncLoc = SM->getLogicalLoc(IncLoc);
371 if (SM->getDecomposedFileLoc(Loc).first != MainFileID ||
372 Loc == LastIncLoc)
373 return;
374 LastIncLoc = Loc;
375
376 unsigned IncCol = SM->getColumnNumber(Loc);
377 SourceLocation LineStartLoc = Loc.getFileLocWithOffset(-IncCol+1);
378
379 // Replace the #import with #include.
380 Rewrite.ReplaceText(LineStartLoc, IncCol-1, "#include ", strlen("#include "));
381}
382
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000383void RewriteTest::RewriteTabs() {
384 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
385 const char *MainBufStart = MainBuf.first;
386 const char *MainBufEnd = MainBuf.second;
Fariborz Jahanian640a01f2007-10-18 19:23:00 +0000387
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000388 // Loop over the whole file, looking for tabs.
389 for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) {
390 if (*BufPtr != '\t')
391 continue;
392
393 // Okay, we found a tab. This tab will turn into at least one character,
394 // but it depends on which 'virtual column' it is in. Compute that now.
395 unsigned VCol = 0;
396 while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' &&
397 BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r')
398 ++VCol;
399
400 // Okay, now that we know the virtual column, we know how many spaces to
401 // insert. We assume 8-character tab-stops.
402 unsigned Spaces = 8-(VCol & 7);
403
404 // Get the location of the tab.
405 SourceLocation TabLoc =
406 SourceLocation::getFileLoc(MainFileID, BufPtr-MainBufStart);
407
408 // Rewrite the single tab character into a sequence of spaces.
409 Rewrite.ReplaceText(TabLoc, 1, " ", Spaces);
410 }
Chris Lattner569faa62007-10-11 18:38:32 +0000411}
412
413
Ted Kremenek42730c52008-01-07 19:49:32 +0000414void RewriteTest::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) {
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000415 int numDecls = ClassDecl->getNumForwardDecls();
Ted Kremenek42730c52008-01-07 19:49:32 +0000416 ObjCInterfaceDecl **ForwardDecls = ClassDecl->getForwardDecls();
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000417
418 // Get the start location and compute the semi location.
419 SourceLocation startLoc = ClassDecl->getLocation();
420 const char *startBuf = SM->getCharacterData(startLoc);
421 const char *semiPtr = strchr(startBuf, ';');
422
423 // Translate to typedef's that forward reference structs with the same name
424 // as the class. As a convenience, we include the original declaration
425 // as a comment.
426 std::string typedefString;
427 typedefString += "// ";
Steve Naroff71226032007-10-24 22:48:43 +0000428 typedefString.append(startBuf, semiPtr-startBuf+1);
429 typedefString += "\n";
430 for (int i = 0; i < numDecls; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000431 ObjCInterfaceDecl *ForwardDecl = ForwardDecls[i];
Steve Naroff2aeae312007-11-09 12:50:28 +0000432 typedefString += "#ifndef _REWRITER_typedef_";
433 typedefString += ForwardDecl->getName();
434 typedefString += "\n";
435 typedefString += "#define _REWRITER_typedef_";
436 typedefString += ForwardDecl->getName();
437 typedefString += "\n";
Steve Naroff4242b972007-11-05 14:36:37 +0000438 typedefString += "typedef struct objc_object ";
Steve Naroff71226032007-10-24 22:48:43 +0000439 typedefString += ForwardDecl->getName();
Steve Naroff2aeae312007-11-09 12:50:28 +0000440 typedefString += ";\n#endif\n";
Steve Naroff71226032007-10-24 22:48:43 +0000441 }
442
443 // Replace the @class with typedefs corresponding to the classes.
444 Rewrite.ReplaceText(startLoc, semiPtr-startBuf+1,
445 typedefString.c_str(), typedefString.size());
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000446}
447
Ted Kremenek42730c52008-01-07 19:49:32 +0000448void RewriteTest::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
Steve Naroff2ce399a2007-12-14 23:37:57 +0000449 SourceLocation LocStart = Method->getLocStart();
450 SourceLocation LocEnd = Method->getLocEnd();
Steve Naroff667f1682007-10-30 13:30:57 +0000451
Steve Naroff2ce399a2007-12-14 23:37:57 +0000452 if (SM->getLineNumber(LocEnd) > SM->getLineNumber(LocStart)) {
453 Rewrite.InsertText(LocStart, "/* ", 3);
454 Rewrite.ReplaceText(LocEnd, 1, ";*/ ", 4);
455 } else {
456 Rewrite.InsertText(LocStart, "// ", 3);
Steve Naroff667f1682007-10-30 13:30:57 +0000457 }
458}
459
Ted Kremenek42730c52008-01-07 19:49:32 +0000460void RewriteTest::RewriteProperties(int nProperties, ObjCPropertyDecl **Properties)
Fariborz Jahanianeca7fad2007-11-07 00:09:37 +0000461{
462 for (int i = 0; i < nProperties; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000463 ObjCPropertyDecl *Property = Properties[i];
Fariborz Jahanianeca7fad2007-11-07 00:09:37 +0000464 SourceLocation Loc = Property->getLocation();
465
466 Rewrite.ReplaceText(Loc, 0, "// ", 3);
467
468 // FIXME: handle properties that are declared across multiple lines.
469 }
470}
471
Ted Kremenek42730c52008-01-07 19:49:32 +0000472void RewriteTest::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
Steve Naroff667f1682007-10-30 13:30:57 +0000473 SourceLocation LocStart = CatDecl->getLocStart();
474
475 // FIXME: handle category headers that are declared across multiple lines.
476 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
477
Ted Kremenek42730c52008-01-07 19:49:32 +0000478 for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000479 E = CatDecl->instmeth_end(); I != E; ++I)
480 RewriteMethodDeclaration(*I);
Ted Kremenek42730c52008-01-07 19:49:32 +0000481 for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000482 E = CatDecl->classmeth_end(); I != E; ++I)
483 RewriteMethodDeclaration(*I);
484
Steve Naroff667f1682007-10-30 13:30:57 +0000485 // Lastly, comment out the @end.
486 Rewrite.ReplaceText(CatDecl->getAtEndLoc(), 0, "// ", 3);
487}
488
Ted Kremenek42730c52008-01-07 19:49:32 +0000489void RewriteTest::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
Fariborz Jahanian3960e9c2007-11-14 01:37:46 +0000490 std::pair<const char*, const char*> MainBuf = SM->getBufferData(MainFileID);
Fariborz Jahanian3960e9c2007-11-14 01:37:46 +0000491
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000492 SourceLocation LocStart = PDecl->getLocStart();
493
494 // FIXME: handle protocol headers that are declared across multiple lines.
495 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
496
Ted Kremenek42730c52008-01-07 19:49:32 +0000497 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000498 E = PDecl->instmeth_end(); I != E; ++I)
499 RewriteMethodDeclaration(*I);
Ted Kremenek42730c52008-01-07 19:49:32 +0000500 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000501 E = PDecl->classmeth_end(); I != E; ++I)
502 RewriteMethodDeclaration(*I);
503
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000504 // Lastly, comment out the @end.
Fariborz Jahanian3960e9c2007-11-14 01:37:46 +0000505 SourceLocation LocEnd = PDecl->getAtEndLoc();
506 Rewrite.ReplaceText(LocEnd, 0, "// ", 3);
Steve Naroff268fa592007-11-14 15:03:57 +0000507
Fariborz Jahanian3960e9c2007-11-14 01:37:46 +0000508 // Must comment out @optional/@required
509 const char *startBuf = SM->getCharacterData(LocStart);
510 const char *endBuf = SM->getCharacterData(LocEnd);
511 for (const char *p = startBuf; p < endBuf; p++) {
512 if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
513 std::string CommentedOptional = "/* @optional */";
Steve Naroff268fa592007-11-14 15:03:57 +0000514 SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
Fariborz Jahanian3960e9c2007-11-14 01:37:46 +0000515 Rewrite.ReplaceText(OptionalLoc, strlen("@optional"),
516 CommentedOptional.c_str(), CommentedOptional.size());
517
518 }
519 else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
520 std::string CommentedRequired = "/* @required */";
Steve Naroff268fa592007-11-14 15:03:57 +0000521 SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf);
Fariborz Jahanian3960e9c2007-11-14 01:37:46 +0000522 Rewrite.ReplaceText(OptionalLoc, strlen("@required"),
523 CommentedRequired.c_str(), CommentedRequired.size());
524
525 }
526 }
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000527}
528
Ted Kremenek42730c52008-01-07 19:49:32 +0000529void RewriteTest::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) {
Fariborz Jahanian016a1882007-11-14 00:42:16 +0000530 SourceLocation LocStart = PDecl->getLocation();
Steve Naroff0540f3f2007-11-14 03:37:28 +0000531 if (LocStart.isInvalid())
532 assert(false && "Invalid SourceLocation");
Fariborz Jahanian016a1882007-11-14 00:42:16 +0000533 // FIXME: handle forward protocol that are declared across multiple lines.
534 Rewrite.ReplaceText(LocStart, 0, "// ", 3);
535}
536
Ted Kremenek42730c52008-01-07 19:49:32 +0000537void RewriteTest::RewriteObjCMethodDecl(ObjCMethodDecl *OMD,
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000538 std::string &ResultStr) {
539 ResultStr += "\nstatic ";
Ted Kremenek42730c52008-01-07 19:49:32 +0000540 if (OMD->getResultType()->isObjCQualifiedIdType())
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000541 ResultStr += "id";
542 else
543 ResultStr += OMD->getResultType().getAsString();
Fariborz Jahanian7202d982008-01-10 01:39:52 +0000544 ResultStr += " ";
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000545
546 // Unique method name
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +0000547 std::string NameStr;
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000548
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +0000549 if (OMD->isInstance())
550 NameStr += "_I_";
551 else
552 NameStr += "_C_";
553
554 NameStr += OMD->getClassInterface()->getName();
555 NameStr += "_";
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000556
557 NamedDecl *MethodContext = OMD->getMethodContext();
Ted Kremenek42730c52008-01-07 19:49:32 +0000558 if (ObjCCategoryImplDecl *CID =
559 dyn_cast<ObjCCategoryImplDecl>(MethodContext)) {
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +0000560 NameStr += CID->getName();
561 NameStr += "_";
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000562 }
563 // Append selector names, replacing ':' with '_'
564 const char *selName = OMD->getSelector().getName().c_str();
565 if (!strchr(selName, ':'))
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +0000566 NameStr += OMD->getSelector().getName();
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000567 else {
568 std::string selString = OMD->getSelector().getName();
569 int len = selString.size();
570 for (int i = 0; i < len; i++)
571 if (selString[i] == ':')
572 selString[i] = '_';
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +0000573 NameStr += selString;
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000574 }
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +0000575 // Remember this name for metadata emission
576 MethodInternalNames[OMD] = NameStr;
577 ResultStr += NameStr;
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000578
579 // Rewrite arguments
580 ResultStr += "(";
581
582 // invisible arguments
583 if (OMD->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000584 QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface());
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000585 selfTy = Context->getPointerType(selfTy);
Ted Kremenek42730c52008-01-07 19:49:32 +0000586 if (ObjCSynthesizedStructs.count(OMD->getClassInterface()))
Fariborz Jahanian0136e372007-11-13 20:04:28 +0000587 ResultStr += "struct ";
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000588 ResultStr += selfTy.getAsString();
589 }
590 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000591 ResultStr += Context->getObjCIdType().getAsString();
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000592
593 ResultStr += " self, ";
Ted Kremenek42730c52008-01-07 19:49:32 +0000594 ResultStr += Context->getObjCSelType().getAsString();
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000595 ResultStr += " _cmd";
596
597 // Method arguments.
598 for (int i = 0; i < OMD->getNumParams(); i++) {
599 ParmVarDecl *PDecl = OMD->getParamDecl(i);
600 ResultStr += ", ";
Ted Kremenek42730c52008-01-07 19:49:32 +0000601 if (PDecl->getType()->isObjCQualifiedIdType())
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000602 ResultStr += "id";
603 else
604 ResultStr += PDecl->getType().getAsString();
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000605 ResultStr += " ";
606 ResultStr += PDecl->getName();
607 }
Fariborz Jahanian7202d982008-01-10 01:39:52 +0000608 ResultStr += ") ";
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000609
610}
Fariborz Jahanian0136e372007-11-13 20:04:28 +0000611void RewriteTest::RewriteImplementationDecl(NamedDecl *OID) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000612 ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
613 ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000614
Fariborz Jahanian0136e372007-11-13 20:04:28 +0000615 if (IMD)
616 Rewrite.InsertText(IMD->getLocStart(), "// ", 3);
617 else
618 Rewrite.InsertText(CID->getLocStart(), "// ", 3);
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000619
Ted Kremenek42730c52008-01-07 19:49:32 +0000620 for (ObjCCategoryImplDecl::instmeth_iterator
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000621 I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(),
622 E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); I != E; ++I) {
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000623 std::string ResultStr;
Ted Kremenek42730c52008-01-07 19:49:32 +0000624 ObjCMethodDecl *OMD = *I;
625 RewriteObjCMethodDecl(OMD, ResultStr);
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000626 SourceLocation LocStart = OMD->getLocStart();
627 SourceLocation LocEnd = OMD->getBody()->getLocStart();
628
629 const char *startBuf = SM->getCharacterData(LocStart);
630 const char *endBuf = SM->getCharacterData(LocEnd);
631 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
632 ResultStr.c_str(), ResultStr.size());
633 }
634
Ted Kremenek42730c52008-01-07 19:49:32 +0000635 for (ObjCCategoryImplDecl::classmeth_iterator
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000636 I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(),
637 E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); I != E; ++I) {
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000638 std::string ResultStr;
Ted Kremenek42730c52008-01-07 19:49:32 +0000639 ObjCMethodDecl *OMD = *I;
640 RewriteObjCMethodDecl(OMD, ResultStr);
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000641 SourceLocation LocStart = OMD->getLocStart();
642 SourceLocation LocEnd = OMD->getBody()->getLocStart();
643
644 const char *startBuf = SM->getCharacterData(LocStart);
645 const char *endBuf = SM->getCharacterData(LocEnd);
646 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
647 ResultStr.c_str(), ResultStr.size());
648 }
Fariborz Jahanian0136e372007-11-13 20:04:28 +0000649 if (IMD)
650 Rewrite.InsertText(IMD->getLocEnd(), "// ", 3);
651 else
652 Rewrite.InsertText(CID->getLocEnd(), "// ", 3);
Fariborz Jahanian5ea3a762007-11-13 18:44:14 +0000653}
654
Ted Kremenek42730c52008-01-07 19:49:32 +0000655void RewriteTest::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000656 std::string ResultStr;
Ted Kremenek42730c52008-01-07 19:49:32 +0000657 if (!ObjCForwardDecls.count(ClassDecl)) {
Steve Naroff77d081b2007-11-01 03:35:41 +0000658 // we haven't seen a forward decl - generate a typedef.
Steve Naroff2adead72007-11-14 23:02:56 +0000659 ResultStr = "#ifndef _REWRITER_typedef_";
Steve Naroff2aeae312007-11-09 12:50:28 +0000660 ResultStr += ClassDecl->getName();
661 ResultStr += "\n";
662 ResultStr += "#define _REWRITER_typedef_";
663 ResultStr += ClassDecl->getName();
664 ResultStr += "\n";
Fariborz Jahaniana845eec2007-12-03 22:25:42 +0000665 ResultStr += "typedef struct ";
666 ResultStr += ClassDecl->getName();
667 ResultStr += " ";
Steve Naroff77d081b2007-11-01 03:35:41 +0000668 ResultStr += ClassDecl->getName();
Steve Naroff2aeae312007-11-09 12:50:28 +0000669 ResultStr += ";\n#endif\n";
Steve Naroff77d081b2007-11-01 03:35:41 +0000670
671 // Mark this typedef as having been generated.
Ted Kremenek42730c52008-01-07 19:49:32 +0000672 ObjCForwardDecls.insert(ClassDecl);
Steve Naroff77d081b2007-11-01 03:35:41 +0000673 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000674 SynthesizeObjCInternalStruct(ClassDecl, ResultStr);
Steve Naroffef20ed32007-10-30 02:23:23 +0000675
Fariborz Jahanianeca7fad2007-11-07 00:09:37 +0000676 RewriteProperties(ClassDecl->getNumPropertyDecl(),
677 ClassDecl->getPropertyDecl());
Ted Kremenek42730c52008-01-07 19:49:32 +0000678 for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000679 E = ClassDecl->instmeth_end(); I != E; ++I)
680 RewriteMethodDeclaration(*I);
Ted Kremenek42730c52008-01-07 19:49:32 +0000681 for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(),
Steve Naroff2ce399a2007-12-14 23:37:57 +0000682 E = ClassDecl->classmeth_end(); I != E; ++I)
683 RewriteMethodDeclaration(*I);
684
Steve Naroff1ccf4632007-10-30 03:43:13 +0000685 // Lastly, comment out the @end.
686 Rewrite.ReplaceText(ClassDecl->getAtEndLoc(), 0, "// ", 3);
Steve Naroff3774dd92007-10-26 20:53:56 +0000687}
688
Steve Naroff6b759ce2007-11-15 02:58:25 +0000689Stmt *RewriteTest::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000690 ObjCIvarDecl *D = IV->getDecl();
Steve Naroff6b759ce2007-11-15 02:58:25 +0000691 if (IV->isFreeIvar()) {
692 Expr *Replacement = new MemberExpr(IV->getBase(), true, D,
693 IV->getLocation());
Steve Naroffe4e5e262007-12-19 14:32:56 +0000694 if (Rewrite.ReplaceStmt(IV, Replacement)) {
695 // replacement failed.
Steve Naroffbcf0a922007-12-19 19:16:49 +0000696 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
697 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroffe4e5e262007-12-19 14:32:56 +0000698 SourceRange Range = IV->getSourceRange();
699 Diags.Report(Context->getFullLoc(IV->getLocation()), DiagID, 0, 0, &Range, 1);
Steve Naroffe4e5e262007-12-19 14:32:56 +0000700 }
Steve Naroff6b759ce2007-11-15 02:58:25 +0000701 delete IV;
702 return Replacement;
Steve Naroff292b7b92007-11-15 11:33:00 +0000703 } else {
704 if (CurMethodDecl) {
705 if (const PointerType *pType = IV->getBase()->getType()->getAsPointerType()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000706 ObjCInterfaceType *intT = dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
Steve Naroff292b7b92007-11-15 11:33:00 +0000707 if (CurMethodDecl->getClassInterface() == intT->getDecl()) {
708 IdentifierInfo *II = intT->getDecl()->getIdentifier();
709 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
710 II, 0);
711 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
712
713 CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation());
714 // Don't forget the parens to enforce the proper binding.
715 ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), castExpr);
Steve Naroffe4e5e262007-12-19 14:32:56 +0000716 if (Rewrite.ReplaceStmt(IV->getBase(), PE)) {
717 // replacement failed.
Steve Naroffbcf0a922007-12-19 19:16:49 +0000718 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
719 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroffe4e5e262007-12-19 14:32:56 +0000720 SourceRange Range = IV->getBase()->getSourceRange();
721 Diags.Report(Context->getFullLoc(IV->getBase()->getLocStart()), DiagID, 0, 0, &Range, 1);
Steve Naroffe4e5e262007-12-19 14:32:56 +0000722 }
Steve Naroff292b7b92007-11-15 11:33:00 +0000723 delete IV->getBase();
724 return PE;
725 }
726 }
727 }
Steve Naroff6b759ce2007-11-15 02:58:25 +0000728 return IV;
Steve Naroff292b7b92007-11-15 11:33:00 +0000729 }
Steve Naroff6b759ce2007-11-15 02:58:25 +0000730}
731
Chris Lattnerfce2c5a2007-10-24 17:06:59 +0000732//===----------------------------------------------------------------------===//
733// Function Body / Expression rewriting
734//===----------------------------------------------------------------------===//
735
Steve Naroff334fbc22007-11-09 15:20:18 +0000736Stmt *RewriteTest::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000737 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
738 isa<DoStmt>(S) || isa<ForStmt>(S))
739 Stmts.push_back(S);
740 else if (isa<ObjCForCollectionStmt>(S)) {
741 Stmts.push_back(S);
742 ObjCBcLabelNo.push_back(++BcLabelCount);
743 }
744
Chris Lattner6fe8b272007-10-16 22:36:42 +0000745 // Otherwise, just rewrite all children.
746 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end();
747 CI != E; ++CI)
Steve Naroffe9f69842007-11-07 04:08:17 +0000748 if (*CI) {
Steve Naroff334fbc22007-11-09 15:20:18 +0000749 Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(*CI);
Steve Naroffe9f69842007-11-07 04:08:17 +0000750 if (newStmt)
751 *CI = newStmt;
752 }
Steve Naroffe9780582007-10-23 23:50:29 +0000753
754 // Handle specific things.
755 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
756 return RewriteAtEncode(AtEncode);
Steve Naroff6b759ce2007-11-15 02:58:25 +0000757
758 if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S))
759 return RewriteObjCIvarRefExpr(IvarRefExpr);
Steve Naroff296b74f2007-11-05 14:50:49 +0000760
761 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
762 return RewriteAtSelector(AtSelector);
Steve Naroff0add5d22007-11-03 11:27:19 +0000763
764 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
765 return RewriteObjCStringLiteral(AtString);
Steve Naroffe9780582007-10-23 23:50:29 +0000766
Steve Naroff71226032007-10-24 22:48:43 +0000767 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
768 // Before we rewrite it, put the original message expression in a comment.
769 SourceLocation startLoc = MessExpr->getLocStart();
770 SourceLocation endLoc = MessExpr->getLocEnd();
771
772 const char *startBuf = SM->getCharacterData(startLoc);
773 const char *endBuf = SM->getCharacterData(endLoc);
774
775 std::string messString;
776 messString += "// ";
777 messString.append(startBuf, endBuf-startBuf+1);
778 messString += "\n";
Steve Naroff3774dd92007-10-26 20:53:56 +0000779
Steve Naroff71226032007-10-24 22:48:43 +0000780 // FIXME: Missing definition of Rewrite.InsertText(clang::SourceLocation, char const*, unsigned int).
781 // Rewrite.InsertText(startLoc, messString.c_str(), messString.size());
782 // Tried this, but it didn't work either...
Steve Narofff4b7d6a2007-10-30 16:42:30 +0000783 // Rewrite.ReplaceText(startLoc, 0, messString.c_str(), messString.size());
Steve Naroffe9780582007-10-23 23:50:29 +0000784 return RewriteMessageExpr(MessExpr);
Steve Naroff71226032007-10-24 22:48:43 +0000785 }
Fariborz Jahanian9447e462007-11-05 17:47:33 +0000786
Ted Kremenek42730c52008-01-07 19:49:32 +0000787 if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
788 return RewriteObjCTryStmt(StmtTry);
Steve Naroff8b1fb8c2007-11-07 15:32:26 +0000789
Ted Kremenek42730c52008-01-07 19:49:32 +0000790 if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
791 return RewriteObjCThrowStmt(StmtThrow);
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +0000792
793 if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
794 return RewriteObjCProtocolExpr(ProtocolExp);
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000795
796 if (ObjCForCollectionStmt *StmtForCollection =
797 dyn_cast<ObjCForCollectionStmt>(S))
798 return RewriteObjCForCollectionStmt(StmtForCollection);
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000799 if (BreakStmt *StmtBreakStmt =
800 dyn_cast<BreakStmt>(S))
801 return RewriteBreakStmt(StmtBreakStmt);
802 if (ContinueStmt *StmtContinueStmt =
803 dyn_cast<ContinueStmt>(S))
804 return RewriteContinueStmt(StmtContinueStmt);
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000805
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000806 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
807 isa<DoStmt>(S) || isa<ForStmt>(S)) {
808 assert(!Stmts.empty() && "Statement stack is empty");
809 assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
810 isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
811 && "Statement stack mismatch");
812 Stmts.pop_back();
813 }
Steve Naroff764c1ae2007-11-15 10:28:18 +0000814#if 0
815 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
816 CastExpr *Replacement = new CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation());
817 // Get the new text.
818 std::ostringstream Buf;
819 Replacement->printPretty(Buf);
820 const std::string &Str = Buf.str();
821
822 printf("CAST = %s\n", &Str[0]);
823 Rewrite.InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
824 delete S;
825 return Replacement;
826 }
827#endif
Chris Lattner0021f452007-10-24 16:57:36 +0000828 // Return this stmt unmodified.
829 return S;
Chris Lattner6fe8b272007-10-16 22:36:42 +0000830}
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000831
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000832/// SynthCountByEnumWithState - To print:
833/// ((unsigned int (*)
834/// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
835/// (void *)objc_msgSend)((id)l_collection,
836/// sel_registerName(
837/// "countByEnumeratingWithState:objects:count:"),
838/// &enumState,
839/// (id *)items, (unsigned int)16)
840///
841void RewriteTest::SynthCountByEnumWithState(std::string &buf) {
842 buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, "
843 "id *, unsigned int))(void *)objc_msgSend)";
844 buf += "\n\t\t";
845 buf += "((id)l_collection,\n\t\t";
846 buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
847 buf += "\n\t\t";
848 buf += "&enumState, "
849 "(id *)items, (unsigned int)16)";
850}
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000851
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000852/// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
853/// statement to exit to its outer synthesized loop.
854///
855Stmt *RewriteTest::RewriteBreakStmt(BreakStmt *S) {
856 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
857 return S;
858 // replace break with goto __break_label
859 std::string buf;
860
861 SourceLocation startLoc = S->getLocStart();
862 buf = "goto __break_label_";
863 buf += utostr(ObjCBcLabelNo.back());
864 Rewrite.ReplaceText(startLoc, strlen("break"), buf.c_str(), buf.size());
865
866 return 0;
867}
868
869/// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
870/// statement to continue with its inner synthesized loop.
871///
872Stmt *RewriteTest::RewriteContinueStmt(ContinueStmt *S) {
873 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
874 return S;
875 // replace continue with goto __continue_label
876 std::string buf;
877
878 SourceLocation startLoc = S->getLocStart();
879 buf = "goto __continue_label_";
880 buf += utostr(ObjCBcLabelNo.back());
881 Rewrite.ReplaceText(startLoc, strlen("continue"), buf.c_str(), buf.size());
882
883 return 0;
884}
885
Fariborz Jahanian72cf3b52008-01-09 01:25:54 +0000886/// RewriteObjCTryStmt - Rewriter for ObjC2's foreach statement.
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000887/// It rewrites:
888/// for ( type elem in collection) { stmts; }
Fariborz Jahanian45d52f72007-10-18 22:09:03 +0000889
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000890/// Into:
891/// {
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000892/// type elem;
893/// struct __objcFastEnumerationState enumState = { 0 };
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000894/// id items[16];
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000895/// id l_collection = (id)collection;
896/// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
897/// objects:items count:16];
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000898/// if (limit) {
899/// unsigned long startMutations = *enumState.mutationsPtr;
900/// do {
901/// unsigned long counter = 0;
902/// do {
903/// if (startMutations != *enumState.mutationsPtr)
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000904/// objc_enumerationMutation(l_collection);
Fariborz Jahanianfb09aa02008-01-09 18:15:42 +0000905/// elem = (type)enumState.itemsPtr[counter++];
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000906/// stmts;
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000907/// __continue_label: ;
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000908/// } while (counter < limit);
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000909/// } while (limit = [l_collection countByEnumeratingWithState:&enumState
910/// objects:items count:16]);
911/// elem = nil;
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000912/// __break_label: ;
Fariborz Jahanian955fcb82008-01-07 21:40:22 +0000913/// }
914/// else
915/// elem = nil;
916/// }
917///
918Stmt *RewriteTest::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S) {
Fariborz Jahanian13dad332008-01-15 23:58:23 +0000919 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
920 assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
921 "ObjCForCollectionStmt Statement stack mismatch");
922 assert(!ObjCBcLabelNo.empty() &&
923 "ObjCForCollectionStmt - Label No stack empty");
924
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000925 SourceLocation startLoc = S->getLocStart();
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000926 const char *startBuf = SM->getCharacterData(startLoc);
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000927 const char *elementName;
Fariborz Jahanianfb09aa02008-01-09 18:15:42 +0000928 std::string elementTypeAsString;
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000929 std::string buf;
930 buf = "\n{\n\t";
931 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
932 // type elem;
933 QualType ElementType = cast<ValueDecl>(DS->getDecl())->getType();
Fariborz Jahanianfb09aa02008-01-09 18:15:42 +0000934 elementTypeAsString = ElementType.getAsString();
935 buf += elementTypeAsString;
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000936 buf += " ";
937 elementName = DS->getDecl()->getName();
938 buf += elementName;
939 buf += ";\n\t";
940 }
Fariborz Jahanianfb09aa02008-01-09 18:15:42 +0000941 else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S->getElement())) {
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000942 elementName = DR->getDecl()->getName();
Fariborz Jahanianfb09aa02008-01-09 18:15:42 +0000943 elementTypeAsString = DR->getDecl()->getType().getAsString();
944 }
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000945 else
946 assert(false && "RewriteObjCForCollectionStmt - bad element kind");
947
948 // struct __objcFastEnumerationState enumState = { 0 };
949 buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
950 // id items[16];
951 buf += "id items[16];\n\t";
952 // id l_collection = (id)
953 buf += "id l_collection = (id)";
Fariborz Jahaniandf2b0952008-01-10 00:24:29 +0000954 // Find start location of 'collection' the hard way!
955 const char *startCollectionBuf = startBuf;
956 startCollectionBuf += 3; // skip 'for'
957 startCollectionBuf = strchr(startCollectionBuf, '(');
958 startCollectionBuf++; // skip '('
959 // find 'in' and skip it.
960 while (*startCollectionBuf != ' ' ||
961 *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
962 (*(startCollectionBuf+3) != ' ' &&
963 *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
964 startCollectionBuf++;
965 startCollectionBuf += 3;
966
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000967 // Replace: "for (type element in" with string constructed thus far.
968 Rewrite.ReplaceText(startLoc, startCollectionBuf - startBuf,
969 buf.c_str(), buf.size());
970 // Replace ')' in for '(' type elem in collection ')' with ';'
Fariborz Jahaniandf2b0952008-01-10 00:24:29 +0000971 SourceLocation rightParenLoc = S->getRParenLoc();
972 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
973 SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf);
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000974 buf = ";\n\t";
975
976 // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
977 // objects:items count:16];
978 // which is synthesized into:
979 // unsigned int limit =
980 // ((unsigned int (*)
981 // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int))
982 // (void *)objc_msgSend)((id)l_collection,
983 // sel_registerName(
984 // "countByEnumeratingWithState:objects:count:"),
985 // (struct __objcFastEnumerationState *)&state,
986 // (id *)items, (unsigned int)16);
987 buf += "unsigned long limit =\n\t\t";
988 SynthCountByEnumWithState(buf);
989 buf += ";\n\t";
990 /// if (limit) {
991 /// unsigned long startMutations = *enumState.mutationsPtr;
992 /// do {
993 /// unsigned long counter = 0;
994 /// do {
995 /// if (startMutations != *enumState.mutationsPtr)
996 /// objc_enumerationMutation(l_collection);
Fariborz Jahanianfb09aa02008-01-09 18:15:42 +0000997 /// elem = (type)enumState.itemsPtr[counter++];
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +0000998 buf += "if (limit) {\n\t";
999 buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
1000 buf += "do {\n\t\t";
1001 buf += "unsigned long counter = 0;\n\t\t";
1002 buf += "do {\n\t\t\t";
1003 buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
1004 buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
1005 buf += elementName;
Fariborz Jahanianfb09aa02008-01-09 18:15:42 +00001006 buf += " = (";
1007 buf += elementTypeAsString;
1008 buf += ")enumState.itemsPtr[counter++];";
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001009 // Replace ')' in for '(' type elem in collection ')' with all of these.
1010 Rewrite.ReplaceText(lparenLoc, 1, buf.c_str(), buf.size());
1011
Fariborz Jahanian13dad332008-01-15 23:58:23 +00001012 /// __continue_label: ;
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001013 /// } while (counter < limit);
1014 /// } while (limit = [l_collection countByEnumeratingWithState:&enumState
1015 /// objects:items count:16]);
1016 /// elem = nil;
Fariborz Jahanian13dad332008-01-15 23:58:23 +00001017 /// __break_label: ;
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001018 /// }
1019 /// else
1020 /// elem = nil;
1021 /// }
Fariborz Jahanian13dad332008-01-15 23:58:23 +00001022 ///
1023 buf = ";\n\t";
1024 buf += "__continue_label_";
1025 buf += utostr(ObjCBcLabelNo.back());
1026 buf += ": ;";
1027 buf += "\n\t\t";
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001028 buf += "} while (counter < limit);\n\t";
1029 buf += "} while (limit = ";
1030 SynthCountByEnumWithState(buf);
1031 buf += ");\n\t";
1032 buf += elementName;
1033 buf += " = nil;\n\t";
Fariborz Jahanian13dad332008-01-15 23:58:23 +00001034 buf += "__break_label_";
1035 buf += utostr(ObjCBcLabelNo.back());
1036 buf += ": ;\n\t";
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001037 buf += "}\n\t";
1038 buf += "else\n\t\t";
1039 buf += elementName;
1040 buf += " = nil;\n";
1041 buf += "}\n";
1042 // Insert all these *after* the statement body.
1043 SourceLocation endBodyLoc = S->getBody()->getLocEnd();
1044 const char *endBodyBuf = SM->getCharacterData(endBodyLoc)+1;
1045 endBodyLoc = startLoc.getFileLocWithOffset(endBodyBuf-startBuf);
1046 Rewrite.InsertText(endBodyLoc, buf.c_str(), buf.size());
Fariborz Jahanian13dad332008-01-15 23:58:23 +00001047 Stmts.pop_back();
1048 ObjCBcLabelNo.pop_back();
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001049
1050 return 0;
Fariborz Jahanian955fcb82008-01-07 21:40:22 +00001051}
1052
Ted Kremenek42730c52008-01-07 19:49:32 +00001053Stmt *RewriteTest::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
Steve Naroffe9f69842007-11-07 04:08:17 +00001054 // Get the start location and compute the semi location.
1055 SourceLocation startLoc = S->getLocStart();
1056 const char *startBuf = SM->getCharacterData(startLoc);
1057
1058 assert((*startBuf == '@') && "bogus @try location");
1059
1060 std::string buf;
1061 // declare a new scope with two variables, _stack and _rethrow.
1062 buf = "/* @try scope begin */ { struct _objc_exception_data {\n";
1063 buf += "int buf[18/*32-bit i386*/];\n";
1064 buf += "char *pointers[4];} _stack;\n";
1065 buf += "id volatile _rethrow = 0;\n";
1066 buf += "objc_exception_try_enter(&_stack);\n";
Steve Naroffd3287d82007-11-07 18:43:40 +00001067 buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n";
Steve Naroffe9f69842007-11-07 04:08:17 +00001068
1069 Rewrite.ReplaceText(startLoc, 4, buf.c_str(), buf.size());
1070
1071 startLoc = S->getTryBody()->getLocEnd();
1072 startBuf = SM->getCharacterData(startLoc);
1073
1074 assert((*startBuf == '}') && "bogus @try block");
1075
1076 SourceLocation lastCurlyLoc = startLoc;
1077
1078 startLoc = startLoc.getFileLocWithOffset(1);
1079 buf = " /* @catch begin */ else {\n";
1080 buf += " id _caught = objc_exception_extract(&_stack);\n";
1081 buf += " objc_exception_try_enter (&_stack);\n";
Steve Naroffd3287d82007-11-07 18:43:40 +00001082 buf += " if (_setjmp(_stack.buf))\n";
Steve Naroffe9f69842007-11-07 04:08:17 +00001083 buf += " _rethrow = objc_exception_extract(&_stack);\n";
1084 buf += " else { /* @catch continue */";
1085
Chris Lattner3c82a7e2007-11-08 04:41:51 +00001086 Rewrite.InsertText(startLoc, buf.c_str(), buf.size());
Steve Naroffe9f69842007-11-07 04:08:17 +00001087
1088 bool sawIdTypedCatch = false;
1089 Stmt *lastCatchBody = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001090 ObjCAtCatchStmt *catchList = S->getCatchStmts();
Steve Naroffe9f69842007-11-07 04:08:17 +00001091 while (catchList) {
1092 Stmt *catchStmt = catchList->getCatchParamStmt();
1093
1094 if (catchList == S->getCatchStmts())
1095 buf = "if ("; // we are generating code for the first catch clause
1096 else
1097 buf = "else if (";
1098 startLoc = catchList->getLocStart();
1099 startBuf = SM->getCharacterData(startLoc);
1100
1101 assert((*startBuf == '@') && "bogus @catch location");
1102
1103 const char *lParenLoc = strchr(startBuf, '(');
1104
1105 if (DeclStmt *declStmt = dyn_cast<DeclStmt>(catchStmt)) {
1106 QualType t = dyn_cast<ValueDecl>(declStmt->getDecl())->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001107 if (t == Context->getObjCIdType()) {
Steve Naroffe9f69842007-11-07 04:08:17 +00001108 buf += "1) { ";
1109 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
1110 buf.c_str(), buf.size());
1111 sawIdTypedCatch = true;
1112 } else if (const PointerType *pType = t->getAsPointerType()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001113 ObjCInterfaceType *cls; // Should be a pointer to a class.
Steve Naroffe9f69842007-11-07 04:08:17 +00001114
Ted Kremenek42730c52008-01-07 19:49:32 +00001115 cls = dyn_cast<ObjCInterfaceType>(pType->getPointeeType().getTypePtr());
Steve Naroffe9f69842007-11-07 04:08:17 +00001116 if (cls) {
Steve Naroffd3287d82007-11-07 18:43:40 +00001117 buf += "objc_exception_match((struct objc_class *)objc_getClass(\"";
Steve Naroffe9f69842007-11-07 04:08:17 +00001118 buf += cls->getDecl()->getName();
Steve Naroffd3287d82007-11-07 18:43:40 +00001119 buf += "\"), (struct objc_object *)_caught)) { ";
Steve Naroffe9f69842007-11-07 04:08:17 +00001120 Rewrite.ReplaceText(startLoc, lParenLoc-startBuf+1,
1121 buf.c_str(), buf.size());
1122 }
1123 }
1124 // Now rewrite the body...
1125 lastCatchBody = catchList->getCatchBody();
1126 SourceLocation rParenLoc = catchList->getRParenLoc();
1127 SourceLocation bodyLoc = lastCatchBody->getLocStart();
1128 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1129 const char *rParenBuf = SM->getCharacterData(rParenLoc);
1130 assert((*rParenBuf == ')') && "bogus @catch paren location");
1131 assert((*bodyBuf == '{') && "bogus @catch body location");
1132
1133 buf = " = _caught;";
1134 // Here we replace ") {" with "= _caught;" (which initializes and
1135 // declares the @catch parameter).
1136 Rewrite.ReplaceText(rParenLoc, bodyBuf-rParenBuf+1,
1137 buf.c_str(), buf.size());
Steve Naroff8b1fb8c2007-11-07 15:32:26 +00001138 } else if (!isa<NullStmt>(catchStmt)) {
Steve Naroffe9f69842007-11-07 04:08:17 +00001139 assert(false && "@catch rewrite bug");
Steve Naroff8b1fb8c2007-11-07 15:32:26 +00001140 }
Steve Naroffe9f69842007-11-07 04:08:17 +00001141 catchList = catchList->getNextCatchStmt();
1142 }
1143 // Complete the catch list...
1144 if (lastCatchBody) {
1145 SourceLocation bodyLoc = lastCatchBody->getLocEnd();
1146 const char *bodyBuf = SM->getCharacterData(bodyLoc);
1147 assert((*bodyBuf == '}') && "bogus @catch body location");
1148 bodyLoc = bodyLoc.getFileLocWithOffset(1);
1149 buf = " } } /* @catch end */\n";
1150
Chris Lattner3c82a7e2007-11-08 04:41:51 +00001151 Rewrite.InsertText(bodyLoc, buf.c_str(), buf.size());
Steve Naroffe9f69842007-11-07 04:08:17 +00001152
1153 // Set lastCurlyLoc
1154 lastCurlyLoc = lastCatchBody->getLocEnd();
1155 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001156 if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) {
Steve Naroffe9f69842007-11-07 04:08:17 +00001157 startLoc = finalStmt->getLocStart();
1158 startBuf = SM->getCharacterData(startLoc);
1159 assert((*startBuf == '@') && "bogus @finally start");
1160
1161 buf = "/* @finally */";
1162 Rewrite.ReplaceText(startLoc, 8, buf.c_str(), buf.size());
1163
1164 Stmt *body = finalStmt->getFinallyBody();
1165 SourceLocation startLoc = body->getLocStart();
1166 SourceLocation endLoc = body->getLocEnd();
1167 const char *startBuf = SM->getCharacterData(startLoc);
1168 const char *endBuf = SM->getCharacterData(endLoc);
1169 assert((*startBuf == '{') && "bogus @finally body location");
1170 assert((*endBuf == '}') && "bogus @finally body location");
1171
1172 startLoc = startLoc.getFileLocWithOffset(1);
1173 buf = " if (!_rethrow) objc_exception_try_exit(&_stack);\n";
Chris Lattner3c82a7e2007-11-08 04:41:51 +00001174 Rewrite.InsertText(startLoc, buf.c_str(), buf.size());
Steve Naroffe9f69842007-11-07 04:08:17 +00001175 endLoc = endLoc.getFileLocWithOffset(-1);
1176 buf = " if (_rethrow) objc_exception_throw(_rethrow);\n";
Chris Lattner3c82a7e2007-11-08 04:41:51 +00001177 Rewrite.InsertText(endLoc, buf.c_str(), buf.size());
Steve Naroffe9f69842007-11-07 04:08:17 +00001178
1179 // Set lastCurlyLoc
1180 lastCurlyLoc = body->getLocEnd();
1181 }
1182 // Now emit the final closing curly brace...
1183 lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1);
1184 buf = " } /* @try scope end */\n";
Chris Lattner3c82a7e2007-11-08 04:41:51 +00001185 Rewrite.InsertText(lastCurlyLoc, buf.c_str(), buf.size());
Fariborz Jahanian9447e462007-11-05 17:47:33 +00001186 return 0;
1187}
1188
Ted Kremenek42730c52008-01-07 19:49:32 +00001189Stmt *RewriteTest::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) {
Fariborz Jahanian9447e462007-11-05 17:47:33 +00001190 return 0;
1191}
1192
Ted Kremenek42730c52008-01-07 19:49:32 +00001193Stmt *RewriteTest::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) {
Fariborz Jahanian9447e462007-11-05 17:47:33 +00001194 return 0;
1195}
1196
Steve Naroff8b1fb8c2007-11-07 15:32:26 +00001197// This can't be done with Rewrite.ReplaceStmt(S, ThrowExpr), since
1198// the throw expression is typically a message expression that's already
1199// been rewritten! (which implies the SourceLocation's are invalid).
Ted Kremenek42730c52008-01-07 19:49:32 +00001200Stmt *RewriteTest::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
Steve Naroff8b1fb8c2007-11-07 15:32:26 +00001201 // Get the start location and compute the semi location.
1202 SourceLocation startLoc = S->getLocStart();
1203 const char *startBuf = SM->getCharacterData(startLoc);
1204
1205 assert((*startBuf == '@') && "bogus @throw location");
1206
1207 std::string buf;
1208 /* void objc_exception_throw(id) __attribute__((noreturn)); */
1209 buf = "objc_exception_throw(";
1210 Rewrite.ReplaceText(startLoc, 6, buf.c_str(), buf.size());
1211 const char *semiBuf = strchr(startBuf, ';');
1212 assert((*semiBuf == ';') && "@throw: can't find ';'");
1213 SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf);
1214 buf = ");";
1215 Rewrite.ReplaceText(semiLoc, 1, buf.c_str(), buf.size());
1216 return 0;
1217}
Fariborz Jahanian9447e462007-11-05 17:47:33 +00001218
Chris Lattner0021f452007-10-24 16:57:36 +00001219Stmt *RewriteTest::RewriteAtEncode(ObjCEncodeExpr *Exp) {
Chris Lattnerbf0bfa62007-10-17 22:35:30 +00001220 // Create a new string expression.
1221 QualType StrType = Context->getPointerType(Context->CharTy);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001222 std::string StrEncoding;
Ted Kremenek42730c52008-01-07 19:49:32 +00001223 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001224 Expr *Replacement = new StringLiteral(StrEncoding.c_str(),
1225 StrEncoding.length(), false, StrType,
Chris Lattnerbf0bfa62007-10-17 22:35:30 +00001226 SourceLocation(), SourceLocation());
Chris Lattner258f26c2007-11-30 22:25:36 +00001227 if (Rewrite.ReplaceStmt(Exp, Replacement)) {
1228 // replacement failed.
Steve Naroffbcf0a922007-12-19 19:16:49 +00001229 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1230 "rewriting sub-expression within a macro (may not be correct)");
Chris Lattner217df512007-12-02 01:09:57 +00001231 SourceRange Range = Exp->getSourceRange();
Ted Kremenekd7f64cd2007-12-12 22:39:36 +00001232 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Chris Lattner258f26c2007-11-30 22:25:36 +00001233 }
1234
Chris Lattner4478db92007-11-30 22:53:43 +00001235 // Replace this subexpr in the parent.
Chris Lattner0021f452007-10-24 16:57:36 +00001236 delete Exp;
1237 return Replacement;
Chris Lattner6fe8b272007-10-16 22:36:42 +00001238}
1239
Steve Naroff296b74f2007-11-05 14:50:49 +00001240Stmt *RewriteTest::RewriteAtSelector(ObjCSelectorExpr *Exp) {
1241 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
1242 // Create a call to sel_registerName("selName").
1243 llvm::SmallVector<Expr*, 8> SelExprs;
1244 QualType argType = Context->getPointerType(Context->CharTy);
1245 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
1246 Exp->getSelector().getName().size(),
1247 false, argType, SourceLocation(),
1248 SourceLocation()));
1249 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
1250 &SelExprs[0], SelExprs.size());
Steve Naroffe4e5e262007-12-19 14:32:56 +00001251 if (Rewrite.ReplaceStmt(Exp, SelExp)) {
1252 // replacement failed.
Steve Naroffbcf0a922007-12-19 19:16:49 +00001253 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1254 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroffe4e5e262007-12-19 14:32:56 +00001255 SourceRange Range = Exp->getSourceRange();
1256 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Steve Naroffe4e5e262007-12-19 14:32:56 +00001257 }
Steve Naroff296b74f2007-11-05 14:50:49 +00001258 delete Exp;
1259 return SelExp;
1260}
1261
Steve Naroff71226032007-10-24 22:48:43 +00001262CallExpr *RewriteTest::SynthesizeCallToFunctionDecl(
1263 FunctionDecl *FD, Expr **args, unsigned nargs) {
Steve Naroffe9780582007-10-23 23:50:29 +00001264 // Get the type, we will need to reference it in a couple spots.
Steve Naroff71226032007-10-24 22:48:43 +00001265 QualType msgSendType = FD->getType();
Steve Naroffe9780582007-10-23 23:50:29 +00001266
1267 // Create a reference to the objc_msgSend() declaration.
Steve Naroff71226032007-10-24 22:48:43 +00001268 DeclRefExpr *DRE = new DeclRefExpr(FD, msgSendType, SourceLocation());
Steve Naroffe9780582007-10-23 23:50:29 +00001269
1270 // Now, we cast the reference to a pointer to the objc_msgSend type.
Chris Lattnerfce2c5a2007-10-24 17:06:59 +00001271 QualType pToFunc = Context->getPointerType(msgSendType);
Steve Naroffe9780582007-10-23 23:50:29 +00001272 ImplicitCastExpr *ICE = new ImplicitCastExpr(pToFunc, DRE);
1273
1274 const FunctionType *FT = msgSendType->getAsFunctionType();
Chris Lattner0021f452007-10-24 16:57:36 +00001275
Steve Naroff71226032007-10-24 22:48:43 +00001276 return new CallExpr(ICE, args, nargs, FT->getResultType(), SourceLocation());
1277}
1278
Steve Naroffc8a92d12007-11-01 13:24:47 +00001279static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
1280 const char *&startRef, const char *&endRef) {
1281 while (startBuf < endBuf) {
1282 if (*startBuf == '<')
1283 startRef = startBuf; // mark the start.
1284 if (*startBuf == '>') {
Steve Naroff2aeae312007-11-09 12:50:28 +00001285 if (startRef && *startRef == '<') {
1286 endRef = startBuf; // mark the end.
1287 return true;
1288 }
1289 return false;
Steve Naroffc8a92d12007-11-01 13:24:47 +00001290 }
1291 startBuf++;
1292 }
1293 return false;
1294}
1295
Fariborz Jahanian150c6ea2007-12-11 22:50:14 +00001296static void scanToNextArgument(const char *&argRef) {
1297 int angle = 0;
1298 while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
1299 if (*argRef == '<')
1300 angle++;
1301 else if (*argRef == '>')
1302 angle--;
1303 argRef++;
1304 }
1305 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
1306}
Fariborz Jahaniandb2904f2007-12-11 23:04:08 +00001307
Steve Naroffc8a92d12007-11-01 13:24:47 +00001308bool RewriteTest::needToScanForQualifiers(QualType T) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001309
Ted Kremenek42730c52008-01-07 19:49:32 +00001310 if (T == Context->getObjCIdType())
Steve Naroffc8a92d12007-11-01 13:24:47 +00001311 return true;
1312
Ted Kremenek42730c52008-01-07 19:49:32 +00001313 if (T->isObjCQualifiedIdType())
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001314 return true;
1315
Steve Naroffc8a92d12007-11-01 13:24:47 +00001316 if (const PointerType *pType = T->getAsPointerType()) {
Steve Naroff05d6ff52007-10-31 04:38:33 +00001317 Type *pointeeType = pType->getPointeeType().getTypePtr();
Ted Kremenek42730c52008-01-07 19:49:32 +00001318 if (isa<ObjCQualifiedInterfaceType>(pointeeType))
Steve Naroff05d6ff52007-10-31 04:38:33 +00001319 return true; // we have "Class <Protocol> *".
1320 }
Steve Naroffc8a92d12007-11-01 13:24:47 +00001321 return false;
1322}
1323
Ted Kremenek42730c52008-01-07 19:49:32 +00001324void RewriteTest::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
Fariborz Jahanian150c6ea2007-12-11 22:50:14 +00001325 SourceLocation Loc;
1326 QualType Type;
1327 const FunctionTypeProto *proto = 0;
1328 if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
1329 Loc = VD->getLocation();
1330 Type = VD->getType();
1331 }
1332 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
1333 Loc = FD->getLocation();
1334 // Check for ObjC 'id' and class types that have been adorned with protocol
1335 // information (id<p>, C<p>*). The protocol references need to be rewritten!
1336 const FunctionType *funcType = FD->getType()->getAsFunctionType();
1337 assert(funcType && "missing function type");
1338 proto = dyn_cast<FunctionTypeProto>(funcType);
1339 if (!proto)
1340 return;
1341 Type = proto->getResultType();
1342 }
1343 else
1344 return;
Steve Naroffc8a92d12007-11-01 13:24:47 +00001345
Fariborz Jahanian150c6ea2007-12-11 22:50:14 +00001346 if (needToScanForQualifiers(Type)) {
Steve Naroffc8a92d12007-11-01 13:24:47 +00001347 // Since types are unique, we need to scan the buffer.
Steve Naroffc8a92d12007-11-01 13:24:47 +00001348
1349 const char *endBuf = SM->getCharacterData(Loc);
1350 const char *startBuf = endBuf;
Chris Lattnerae43eb72007-12-02 01:13:47 +00001351 while (*startBuf != ';' && startBuf != MainFileStart)
Steve Naroffc8a92d12007-11-01 13:24:47 +00001352 startBuf--; // scan backward (from the decl location) for return type.
1353 const char *startRef = 0, *endRef = 0;
1354 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
1355 // Get the locations of the startRef, endRef.
1356 SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf);
1357 SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1);
1358 // Comment out the protocol references.
Chris Lattner3c82a7e2007-11-08 04:41:51 +00001359 Rewrite.InsertText(LessLoc, "/*", 2);
1360 Rewrite.InsertText(GreaterLoc, "*/", 2);
Steve Naroff05d6ff52007-10-31 04:38:33 +00001361 }
1362 }
Fariborz Jahanian150c6ea2007-12-11 22:50:14 +00001363 if (!proto)
1364 return; // most likely, was a variable
Steve Naroffc8a92d12007-11-01 13:24:47 +00001365 // Now check arguments.
Fariborz Jahanian150c6ea2007-12-11 22:50:14 +00001366 const char *startBuf = SM->getCharacterData(Loc);
1367 const char *startFuncBuf = startBuf;
Steve Naroffc8a92d12007-11-01 13:24:47 +00001368 for (unsigned i = 0; i < proto->getNumArgs(); i++) {
1369 if (needToScanForQualifiers(proto->getArgType(i))) {
1370 // Since types are unique, we need to scan the buffer.
Steve Naroffc8a92d12007-11-01 13:24:47 +00001371
Steve Naroffc8a92d12007-11-01 13:24:47 +00001372 const char *endBuf = startBuf;
Fariborz Jahanian150c6ea2007-12-11 22:50:14 +00001373 // scan forward (from the decl location) for argument types.
1374 scanToNextArgument(endBuf);
Steve Naroffc8a92d12007-11-01 13:24:47 +00001375 const char *startRef = 0, *endRef = 0;
1376 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
1377 // Get the locations of the startRef, endRef.
Fariborz Jahaniandb2904f2007-12-11 23:04:08 +00001378 SourceLocation LessLoc =
1379 Loc.getFileLocWithOffset(startRef-startFuncBuf);
1380 SourceLocation GreaterLoc =
1381 Loc.getFileLocWithOffset(endRef-startFuncBuf+1);
Steve Naroffc8a92d12007-11-01 13:24:47 +00001382 // Comment out the protocol references.
Chris Lattner3c82a7e2007-11-08 04:41:51 +00001383 Rewrite.InsertText(LessLoc, "/*", 2);
1384 Rewrite.InsertText(GreaterLoc, "*/", 2);
Steve Naroffc8a92d12007-11-01 13:24:47 +00001385 }
Fariborz Jahanian150c6ea2007-12-11 22:50:14 +00001386 startBuf = ++endBuf;
1387 }
1388 else {
1389 while (*startBuf != ')' && *startBuf != ',')
1390 startBuf++; // scan forward (from the decl location) for argument types.
1391 startBuf++;
1392 }
Steve Naroffc8a92d12007-11-01 13:24:47 +00001393 }
Steve Naroff05d6ff52007-10-31 04:38:33 +00001394}
1395
Fariborz Jahanianfb8e9912007-12-04 21:47:40 +00001396// SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
1397void RewriteTest::SynthSelGetUidFunctionDecl() {
1398 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
1399 llvm::SmallVector<QualType, 16> ArgTys;
1400 ArgTys.push_back(Context->getPointerType(
1401 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremenek42730c52008-01-07 19:49:32 +00001402 QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(),
Fariborz Jahanianfb8e9912007-12-04 21:47:40 +00001403 &ArgTys[0], ArgTys.size(),
1404 false /*isVariadic*/);
1405 SelGetUidFunctionDecl = new FunctionDecl(SourceLocation(),
1406 SelGetUidIdent, getFuncType,
1407 FunctionDecl::Extern, false, 0);
1408}
1409
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +00001410// SynthGetProtocolFunctionDecl - Protocol objc_getProtocol(const char *proto);
1411void RewriteTest::SynthGetProtocolFunctionDecl() {
1412 IdentifierInfo *SelGetProtoIdent = &Context->Idents.get("objc_getProtocol");
1413 llvm::SmallVector<QualType, 16> ArgTys;
1414 ArgTys.push_back(Context->getPointerType(
1415 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremenek42730c52008-01-07 19:49:32 +00001416 QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(),
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +00001417 &ArgTys[0], ArgTys.size(),
1418 false /*isVariadic*/);
1419 GetProtocolFunctionDecl = new FunctionDecl(SourceLocation(),
1420 SelGetProtoIdent, getFuncType,
1421 FunctionDecl::Extern, false, 0);
1422}
1423
Steve Naroff02a82aa2007-10-30 23:14:51 +00001424void RewriteTest::RewriteFunctionDecl(FunctionDecl *FD) {
1425 // declared in <objc/objc.h>
Steve Naroff0add5d22007-11-03 11:27:19 +00001426 if (strcmp(FD->getName(), "sel_registerName") == 0) {
Steve Naroff02a82aa2007-10-30 23:14:51 +00001427 SelGetUidFunctionDecl = FD;
Steve Naroff05d6ff52007-10-31 04:38:33 +00001428 return;
1429 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001430 RewriteObjCQualifiedInterfaceTypes(FD);
Steve Naroff02a82aa2007-10-30 23:14:51 +00001431}
1432
1433// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
1434void RewriteTest::SynthMsgSendFunctionDecl() {
1435 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
1436 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremenek42730c52008-01-07 19:49:32 +00001437 QualType argT = Context->getObjCIdType();
Steve Naroff02a82aa2007-10-30 23:14:51 +00001438 assert(!argT.isNull() && "Can't find 'id' type");
1439 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001440 argT = Context->getObjCSelType();
Steve Naroff02a82aa2007-10-30 23:14:51 +00001441 assert(!argT.isNull() && "Can't find 'SEL' type");
1442 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001443 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff02a82aa2007-10-30 23:14:51 +00001444 &ArgTys[0], ArgTys.size(),
1445 true /*isVariadic*/);
1446 MsgSendFunctionDecl = new FunctionDecl(SourceLocation(),
1447 msgSendIdent, msgSendType,
1448 FunctionDecl::Extern, false, 0);
1449}
1450
Steve Naroff764c1ae2007-11-15 10:28:18 +00001451// SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...);
1452void RewriteTest::SynthMsgSendSuperFunctionDecl() {
1453 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
1454 llvm::SmallVector<QualType, 16> ArgTys;
1455 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
1456 &Context->Idents.get("objc_super"), 0);
1457 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
1458 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
1459 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001460 argT = Context->getObjCSelType();
Steve Naroff764c1ae2007-11-15 10:28:18 +00001461 assert(!argT.isNull() && "Can't find 'SEL' type");
1462 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001463 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff764c1ae2007-11-15 10:28:18 +00001464 &ArgTys[0], ArgTys.size(),
1465 true /*isVariadic*/);
1466 MsgSendSuperFunctionDecl = new FunctionDecl(SourceLocation(),
1467 msgSendIdent, msgSendType,
1468 FunctionDecl::Extern, false, 0);
1469}
1470
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001471// SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
1472void RewriteTest::SynthMsgSendStretFunctionDecl() {
1473 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
1474 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremenek42730c52008-01-07 19:49:32 +00001475 QualType argT = Context->getObjCIdType();
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001476 assert(!argT.isNull() && "Can't find 'id' type");
1477 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001478 argT = Context->getObjCSelType();
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001479 assert(!argT.isNull() && "Can't find 'SEL' type");
1480 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001481 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001482 &ArgTys[0], ArgTys.size(),
1483 true /*isVariadic*/);
1484 MsgSendStretFunctionDecl = new FunctionDecl(SourceLocation(),
1485 msgSendIdent, msgSendType,
1486 FunctionDecl::Extern, false, 0);
1487}
1488
1489// SynthMsgSendSuperStretFunctionDecl -
1490// id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...);
1491void RewriteTest::SynthMsgSendSuperStretFunctionDecl() {
1492 IdentifierInfo *msgSendIdent =
1493 &Context->Idents.get("objc_msgSendSuper_stret");
1494 llvm::SmallVector<QualType, 16> ArgTys;
1495 RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(),
1496 &Context->Idents.get("objc_super"), 0);
1497 QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
1498 assert(!argT.isNull() && "Can't build 'struct objc_super *' type");
1499 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001500 argT = Context->getObjCSelType();
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001501 assert(!argT.isNull() && "Can't find 'SEL' type");
1502 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001503 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001504 &ArgTys[0], ArgTys.size(),
1505 true /*isVariadic*/);
1506 MsgSendSuperStretFunctionDecl = new FunctionDecl(SourceLocation(),
1507 msgSendIdent, msgSendType,
1508 FunctionDecl::Extern, false, 0);
1509}
1510
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +00001511// SynthMsgSendFpretFunctionDecl - id objc_msgSend_fpret(id self, SEL op, ...);
1512void RewriteTest::SynthMsgSendFpretFunctionDecl() {
1513 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
1514 llvm::SmallVector<QualType, 16> ArgTys;
Ted Kremenek42730c52008-01-07 19:49:32 +00001515 QualType argT = Context->getObjCIdType();
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +00001516 assert(!argT.isNull() && "Can't find 'id' type");
1517 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001518 argT = Context->getObjCSelType();
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +00001519 assert(!argT.isNull() && "Can't find 'SEL' type");
1520 ArgTys.push_back(argT);
Ted Kremenek42730c52008-01-07 19:49:32 +00001521 QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +00001522 &ArgTys[0], ArgTys.size(),
1523 true /*isVariadic*/);
1524 MsgSendFpretFunctionDecl = new FunctionDecl(SourceLocation(),
1525 msgSendIdent, msgSendType,
1526 FunctionDecl::Extern, false, 0);
1527}
1528
Steve Naroff02a82aa2007-10-30 23:14:51 +00001529// SynthGetClassFunctionDecl - id objc_getClass(const char *name);
1530void RewriteTest::SynthGetClassFunctionDecl() {
1531 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
1532 llvm::SmallVector<QualType, 16> ArgTys;
1533 ArgTys.push_back(Context->getPointerType(
1534 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremenek42730c52008-01-07 19:49:32 +00001535 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff02a82aa2007-10-30 23:14:51 +00001536 &ArgTys[0], ArgTys.size(),
1537 false /*isVariadic*/);
1538 GetClassFunctionDecl = new FunctionDecl(SourceLocation(),
1539 getClassIdent, getClassType,
1540 FunctionDecl::Extern, false, 0);
1541}
1542
Steve Naroff3b1caac2007-12-07 03:50:46 +00001543// SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name);
1544void RewriteTest::SynthGetMetaClassFunctionDecl() {
1545 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
1546 llvm::SmallVector<QualType, 16> ArgTys;
1547 ArgTys.push_back(Context->getPointerType(
1548 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremenek42730c52008-01-07 19:49:32 +00001549 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroff3b1caac2007-12-07 03:50:46 +00001550 &ArgTys[0], ArgTys.size(),
1551 false /*isVariadic*/);
1552 GetMetaClassFunctionDecl = new FunctionDecl(SourceLocation(),
1553 getClassIdent, getClassType,
1554 FunctionDecl::Extern, false, 0);
1555}
1556
Steve Naroffabb96362007-11-08 14:30:50 +00001557// SynthCFStringFunctionDecl - id __builtin___CFStringMakeConstantString(const char *name);
1558void RewriteTest::SynthCFStringFunctionDecl() {
1559 IdentifierInfo *getClassIdent = &Context->Idents.get("__builtin___CFStringMakeConstantString");
1560 llvm::SmallVector<QualType, 16> ArgTys;
1561 ArgTys.push_back(Context->getPointerType(
1562 Context->CharTy.getQualifiedType(QualType::Const)));
Ted Kremenek42730c52008-01-07 19:49:32 +00001563 QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
Steve Naroffabb96362007-11-08 14:30:50 +00001564 &ArgTys[0], ArgTys.size(),
1565 false /*isVariadic*/);
1566 CFStringFunctionDecl = new FunctionDecl(SourceLocation(),
1567 getClassIdent, getClassType,
1568 FunctionDecl::Extern, false, 0);
1569}
1570
Steve Naroff0add5d22007-11-03 11:27:19 +00001571Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
Steve Naroffabb96362007-11-08 14:30:50 +00001572#if 1
1573 // This rewrite is specific to GCC, which has builtin support for CFString.
1574 if (!CFStringFunctionDecl)
1575 SynthCFStringFunctionDecl();
1576 // Create a call to __builtin___CFStringMakeConstantString("cstr").
1577 llvm::SmallVector<Expr*, 8> StrExpr;
1578 StrExpr.push_back(Exp->getString());
1579 CallExpr *call = SynthesizeCallToFunctionDecl(CFStringFunctionDecl,
1580 &StrExpr[0], StrExpr.size());
1581 // cast to NSConstantString *
1582 CastExpr *cast = new CastExpr(Exp->getType(), call, SourceLocation());
Steve Naroffe4e5e262007-12-19 14:32:56 +00001583 if (Rewrite.ReplaceStmt(Exp, cast)) {
1584 // replacement failed.
Steve Naroffbcf0a922007-12-19 19:16:49 +00001585 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1586 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroffe4e5e262007-12-19 14:32:56 +00001587 SourceRange Range = Exp->getSourceRange();
1588 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Steve Naroffe4e5e262007-12-19 14:32:56 +00001589 }
Steve Naroffabb96362007-11-08 14:30:50 +00001590 delete Exp;
1591 return cast;
1592#else
Steve Naroff0add5d22007-11-03 11:27:19 +00001593 assert(ConstantStringClassReference && "Can't find constant string reference");
1594 llvm::SmallVector<Expr*, 4> InitExprs;
1595
1596 // Synthesize "(Class)&_NSConstantStringClassReference"
1597 DeclRefExpr *ClsRef = new DeclRefExpr(ConstantStringClassReference,
1598 ConstantStringClassReference->getType(),
1599 SourceLocation());
1600 QualType expType = Context->getPointerType(ClsRef->getType());
1601 UnaryOperator *Unop = new UnaryOperator(ClsRef, UnaryOperator::AddrOf,
1602 expType, SourceLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001603 CastExpr *cast = new CastExpr(Context->getObjCClassType(), Unop,
Steve Naroff0add5d22007-11-03 11:27:19 +00001604 SourceLocation());
1605 InitExprs.push_back(cast); // set the 'isa'.
1606 InitExprs.push_back(Exp->getString()); // set "char *bytes".
1607 unsigned IntSize = static_cast<unsigned>(
1608 Context->getTypeSize(Context->IntTy, Exp->getLocStart()));
1609 llvm::APInt IntVal(IntSize, Exp->getString()->getByteLength());
1610 IntegerLiteral *len = new IntegerLiteral(IntVal, Context->IntTy,
1611 Exp->getLocStart());
1612 InitExprs.push_back(len); // set "int numBytes".
1613
1614 // struct NSConstantString
1615 QualType CFConstantStrType = Context->getCFConstantStringType();
1616 // (struct NSConstantString) { <exprs from above> }
1617 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1618 &InitExprs[0], InitExprs.size(),
1619 SourceLocation());
Steve Naroffbe37fc02008-01-14 18:19:28 +00001620 CompoundLiteralExpr *StrRep = new CompoundLiteralExpr(CFConstantStrType, ILE, false);
Steve Naroff0add5d22007-11-03 11:27:19 +00001621 // struct NSConstantString *
1622 expType = Context->getPointerType(StrRep->getType());
1623 Unop = new UnaryOperator(StrRep, UnaryOperator::AddrOf, expType,
1624 SourceLocation());
Steve Naroff4242b972007-11-05 14:36:37 +00001625 // cast to NSConstantString *
1626 cast = new CastExpr(Exp->getType(), Unop, SourceLocation());
Steve Naroff0add5d22007-11-03 11:27:19 +00001627 Rewrite.ReplaceStmt(Exp, cast);
1628 delete Exp;
Steve Naroff4242b972007-11-05 14:36:37 +00001629 return cast;
Steve Naroffabb96362007-11-08 14:30:50 +00001630#endif
Steve Naroff0add5d22007-11-03 11:27:19 +00001631}
1632
Ted Kremenek42730c52008-01-07 19:49:32 +00001633ObjCInterfaceDecl *RewriteTest::isSuperReceiver(Expr *recExpr) {
Steve Naroff3b1caac2007-12-07 03:50:46 +00001634 // check if we are sending a message to 'super'
1635 if (CurMethodDecl && CurMethodDecl->isInstance()) {
Steve Naroff764c1ae2007-11-15 10:28:18 +00001636 if (CastExpr *CE = dyn_cast<CastExpr>(recExpr)) {
1637 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1638 if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
1639 if (!strcmp(PVD->getName(), "self")) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001640 // is this id<P1..> type?
Ted Kremenek42730c52008-01-07 19:49:32 +00001641 if (CE->getType()->isObjCQualifiedIdType())
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001642 return 0;
Steve Naroff764c1ae2007-11-15 10:28:18 +00001643 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001644 if (ObjCInterfaceType *IT =
1645 dyn_cast<ObjCInterfaceType>(PT->getPointeeType())) {
Steve Naroff764c1ae2007-11-15 10:28:18 +00001646 if (IT->getDecl() ==
1647 CurMethodDecl->getClassInterface()->getSuperClass())
1648 return IT->getDecl();
1649 }
1650 }
1651 }
1652 }
1653 }
Steve Naroff3b1caac2007-12-07 03:50:46 +00001654 }
Steve Naroff764c1ae2007-11-15 10:28:18 +00001655 }
1656 return 0;
1657}
1658
1659// struct objc_super { struct objc_object *receiver; struct objc_class *super; };
1660QualType RewriteTest::getSuperStructType() {
1661 if (!SuperStructDecl) {
1662 SuperStructDecl = new RecordDecl(Decl::Struct, SourceLocation(),
1663 &Context->Idents.get("objc_super"), 0);
1664 QualType FieldTypes[2];
1665
1666 // struct objc_object *receiver;
Ted Kremenek42730c52008-01-07 19:49:32 +00001667 FieldTypes[0] = Context->getObjCIdType();
Steve Naroff764c1ae2007-11-15 10:28:18 +00001668 // struct objc_class *super;
Ted Kremenek42730c52008-01-07 19:49:32 +00001669 FieldTypes[1] = Context->getObjCClassType();
Steve Naroff764c1ae2007-11-15 10:28:18 +00001670 // Create fields
1671 FieldDecl *FieldDecls[2];
1672
1673 for (unsigned i = 0; i < 2; ++i)
1674 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
1675
1676 SuperStructDecl->defineBody(FieldDecls, 4);
1677 }
1678 return Context->getTagDeclType(SuperStructDecl);
1679}
1680
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001681Stmt *RewriteTest::SynthMessageExpr(ObjCMessageExpr *Exp) {
Fariborz Jahanianfb8e9912007-12-04 21:47:40 +00001682 if (!SelGetUidFunctionDecl)
1683 SynthSelGetUidFunctionDecl();
Steve Naroff02a82aa2007-10-30 23:14:51 +00001684 if (!MsgSendFunctionDecl)
1685 SynthMsgSendFunctionDecl();
Steve Naroff764c1ae2007-11-15 10:28:18 +00001686 if (!MsgSendSuperFunctionDecl)
1687 SynthMsgSendSuperFunctionDecl();
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001688 if (!MsgSendStretFunctionDecl)
1689 SynthMsgSendStretFunctionDecl();
1690 if (!MsgSendSuperStretFunctionDecl)
1691 SynthMsgSendSuperStretFunctionDecl();
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +00001692 if (!MsgSendFpretFunctionDecl)
1693 SynthMsgSendFpretFunctionDecl();
Steve Naroff02a82aa2007-10-30 23:14:51 +00001694 if (!GetClassFunctionDecl)
1695 SynthGetClassFunctionDecl();
Steve Naroff3b1caac2007-12-07 03:50:46 +00001696 if (!GetMetaClassFunctionDecl)
1697 SynthGetMetaClassFunctionDecl();
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001698
Steve Naroff764c1ae2007-11-15 10:28:18 +00001699 // default to objc_msgSend().
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001700 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
1701 // May need to use objc_msgSend_stret() as well.
1702 FunctionDecl *MsgSendStretFlavor = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001703 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001704 QualType resultType = mDecl->getResultType();
1705 if (resultType.getCanonicalType()->isStructureType()
1706 || resultType.getCanonicalType()->isUnionType())
1707 MsgSendStretFlavor = MsgSendStretFunctionDecl;
Fariborz Jahanian1d29b5d2007-12-03 21:26:48 +00001708 else if (resultType.getCanonicalType()->isRealFloatingType())
1709 MsgSendFlavor = MsgSendFpretFunctionDecl;
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001710 }
Steve Naroff764c1ae2007-11-15 10:28:18 +00001711
Steve Naroff71226032007-10-24 22:48:43 +00001712 // Synthesize a call to objc_msgSend().
1713 llvm::SmallVector<Expr*, 8> MsgExprs;
1714 IdentifierInfo *clsName = Exp->getClassName();
1715
1716 // Derive/push the receiver/selector, 2 implicit arguments to objc_msgSend().
1717 if (clsName) { // class message.
Steve Naroff3b1caac2007-12-07 03:50:46 +00001718 if (!strcmp(clsName->getName(), "super")) {
1719 MsgSendFlavor = MsgSendSuperFunctionDecl;
1720 if (MsgSendStretFlavor)
1721 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
1722 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
1723
Ted Kremenek42730c52008-01-07 19:49:32 +00001724 ObjCInterfaceDecl *SuperDecl =
Steve Naroff3b1caac2007-12-07 03:50:46 +00001725 CurMethodDecl->getClassInterface()->getSuperClass();
1726
1727 llvm::SmallVector<Expr*, 4> InitExprs;
1728
1729 // set the receiver to self, the first argument to all methods.
1730 InitExprs.push_back(new DeclRefExpr(CurMethodDecl->getSelfDecl(),
Ted Kremenek42730c52008-01-07 19:49:32 +00001731 Context->getObjCIdType(),
Steve Naroff3b1caac2007-12-07 03:50:46 +00001732 SourceLocation()));
1733 llvm::SmallVector<Expr*, 8> ClsExprs;
1734 QualType argType = Context->getPointerType(Context->CharTy);
1735 ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
1736 SuperDecl->getIdentifier()->getLength(),
1737 false, argType, SourceLocation(),
1738 SourceLocation()));
1739 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
1740 &ClsExprs[0],
1741 ClsExprs.size());
1742 // To turn off a warning, type-cast to 'id'
1743 InitExprs.push_back(
Ted Kremenek42730c52008-01-07 19:49:32 +00001744 new CastExpr(Context->getObjCIdType(),
Steve Naroff3b1caac2007-12-07 03:50:46 +00001745 Cls, SourceLocation())); // set 'super class', using objc_getClass().
1746 // struct objc_super
1747 QualType superType = getSuperStructType();
1748 // (struct objc_super) { <exprs from above> }
1749 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1750 &InitExprs[0], InitExprs.size(),
1751 SourceLocation());
Chris Lattner386ab8a2008-01-02 21:46:24 +00001752 CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
Steve Naroffbe37fc02008-01-14 18:19:28 +00001753 superType, ILE, false);
Steve Naroff3b1caac2007-12-07 03:50:46 +00001754 // struct objc_super *
1755 Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
1756 Context->getPointerType(SuperRep->getType()),
1757 SourceLocation());
1758 MsgExprs.push_back(Unop);
1759 } else {
1760 llvm::SmallVector<Expr*, 8> ClsExprs;
1761 QualType argType = Context->getPointerType(Context->CharTy);
1762 ClsExprs.push_back(new StringLiteral(clsName->getName(),
1763 clsName->getLength(),
1764 false, argType, SourceLocation(),
1765 SourceLocation()));
1766 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
1767 &ClsExprs[0],
1768 ClsExprs.size());
1769 MsgExprs.push_back(Cls);
1770 }
Steve Naroff885e2122007-11-14 23:54:14 +00001771 } else { // instance message.
1772 Expr *recExpr = Exp->getReceiver();
Steve Naroff764c1ae2007-11-15 10:28:18 +00001773
Ted Kremenek42730c52008-01-07 19:49:32 +00001774 if (ObjCInterfaceDecl *SuperDecl = isSuperReceiver(recExpr)) {
Steve Naroff764c1ae2007-11-15 10:28:18 +00001775 MsgSendFlavor = MsgSendSuperFunctionDecl;
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001776 if (MsgSendStretFlavor)
1777 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
Steve Naroff764c1ae2007-11-15 10:28:18 +00001778 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
1779
1780 llvm::SmallVector<Expr*, 4> InitExprs;
1781
Fariborz Jahaniandc25ba72007-12-04 22:32:58 +00001782 InitExprs.push_back(
Ted Kremenek42730c52008-01-07 19:49:32 +00001783 new CastExpr(Context->getObjCIdType(),
Fariborz Jahaniandc25ba72007-12-04 22:32:58 +00001784 recExpr, SourceLocation())); // set the 'receiver'.
Steve Naroff764c1ae2007-11-15 10:28:18 +00001785
1786 llvm::SmallVector<Expr*, 8> ClsExprs;
1787 QualType argType = Context->getPointerType(Context->CharTy);
Steve Naroff3b1caac2007-12-07 03:50:46 +00001788 ClsExprs.push_back(new StringLiteral(SuperDecl->getIdentifier()->getName(),
1789 SuperDecl->getIdentifier()->getLength(),
Steve Naroff764c1ae2007-11-15 10:28:18 +00001790 false, argType, SourceLocation(),
1791 SourceLocation()));
1792 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
Fariborz Jahaniandc25ba72007-12-04 22:32:58 +00001793 &ClsExprs[0],
1794 ClsExprs.size());
Fariborz Jahanianfabf3bf2007-12-05 17:29:46 +00001795 // To turn off a warning, type-cast to 'id'
Fariborz Jahaniandc25ba72007-12-04 22:32:58 +00001796 InitExprs.push_back(
Ted Kremenek42730c52008-01-07 19:49:32 +00001797 new CastExpr(Context->getObjCIdType(),
Fariborz Jahaniandc25ba72007-12-04 22:32:58 +00001798 Cls, SourceLocation())); // set 'super class', using objc_getClass().
Steve Naroff764c1ae2007-11-15 10:28:18 +00001799 // struct objc_super
1800 QualType superType = getSuperStructType();
1801 // (struct objc_super) { <exprs from above> }
1802 InitListExpr *ILE = new InitListExpr(SourceLocation(),
1803 &InitExprs[0], InitExprs.size(),
1804 SourceLocation());
Chris Lattner386ab8a2008-01-02 21:46:24 +00001805 CompoundLiteralExpr *SuperRep = new CompoundLiteralExpr(SourceLocation(),
Steve Naroffbe37fc02008-01-14 18:19:28 +00001806 superType, ILE, false);
Steve Naroff764c1ae2007-11-15 10:28:18 +00001807 // struct objc_super *
1808 Expr *Unop = new UnaryOperator(SuperRep, UnaryOperator::AddrOf,
1809 Context->getPointerType(SuperRep->getType()),
1810 SourceLocation());
1811 MsgExprs.push_back(Unop);
1812 } else {
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +00001813 // Remove all type-casts because it may contain objc-style types; e.g.
1814 // Foo<Proto> *.
1815 while (CastExpr *CE = dyn_cast<CastExpr>(recExpr))
1816 recExpr = CE->getSubExpr();
Ted Kremenek42730c52008-01-07 19:49:32 +00001817 recExpr = new CastExpr(Context->getObjCIdType(), recExpr, SourceLocation());
Steve Naroff764c1ae2007-11-15 10:28:18 +00001818 MsgExprs.push_back(recExpr);
1819 }
Steve Naroff885e2122007-11-14 23:54:14 +00001820 }
Steve Naroff0add5d22007-11-03 11:27:19 +00001821 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
Steve Naroff71226032007-10-24 22:48:43 +00001822 llvm::SmallVector<Expr*, 8> SelExprs;
1823 QualType argType = Context->getPointerType(Context->CharTy);
1824 SelExprs.push_back(new StringLiteral(Exp->getSelector().getName().c_str(),
1825 Exp->getSelector().getName().size(),
1826 false, argType, SourceLocation(),
1827 SourceLocation()));
1828 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
1829 &SelExprs[0], SelExprs.size());
1830 MsgExprs.push_back(SelExp);
1831
1832 // Now push any user supplied arguments.
1833 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
Steve Naroff885e2122007-11-14 23:54:14 +00001834 Expr *userExpr = Exp->getArg(i);
Steve Naroff6b759ce2007-11-15 02:58:25 +00001835 // Make all implicit casts explicit...ICE comes in handy:-)
1836 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
1837 // Reuse the ICE type, it is exactly what the doctor ordered.
Ted Kremenek42730c52008-01-07 19:49:32 +00001838 userExpr = new CastExpr(ICE->getType()->isObjCQualifiedIdType()
1839 ? Context->getObjCIdType()
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001840 : ICE->getType(), userExpr, SourceLocation());
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +00001841 }
1842 // Make id<P...> cast into an 'id' cast.
1843 else if (CastExpr *CE = dyn_cast<CastExpr>(userExpr)) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001844 if (CE->getType()->isObjCQualifiedIdType()) {
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +00001845 while ((CE = dyn_cast<CastExpr>(userExpr)))
1846 userExpr = CE->getSubExpr();
Ted Kremenek42730c52008-01-07 19:49:32 +00001847 userExpr = new CastExpr(Context->getObjCIdType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +00001848 userExpr, SourceLocation());
1849 }
Steve Naroff6b759ce2007-11-15 02:58:25 +00001850 }
Steve Naroff885e2122007-11-14 23:54:14 +00001851 MsgExprs.push_back(userExpr);
Steve Naroff71226032007-10-24 22:48:43 +00001852 // We've transferred the ownership to MsgExprs. Null out the argument in
1853 // the original expression, since we will delete it below.
1854 Exp->setArg(i, 0);
1855 }
Steve Naroff0744c472007-11-04 22:37:50 +00001856 // Generate the funky cast.
1857 CastExpr *cast;
1858 llvm::SmallVector<QualType, 8> ArgTypes;
1859 QualType returnType;
1860
1861 // Push 'id' and 'SEL', the 2 implicit arguments.
Steve Naroff0c04bb62007-11-15 10:43:57 +00001862 if (MsgSendFlavor == MsgSendSuperFunctionDecl)
1863 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
1864 else
Ted Kremenek42730c52008-01-07 19:49:32 +00001865 ArgTypes.push_back(Context->getObjCIdType());
1866 ArgTypes.push_back(Context->getObjCSelType());
1867 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
Steve Naroff0744c472007-11-04 22:37:50 +00001868 // Push any user argument types.
Steve Naroff4242b972007-11-05 14:36:37 +00001869 for (int i = 0; i < mDecl->getNumParams(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001870 QualType t = mDecl->getParamDecl(i)->getType()->isObjCQualifiedIdType()
1871 ? Context->getObjCIdType()
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001872 : mDecl->getParamDecl(i)->getType();
Steve Naroff4242b972007-11-05 14:36:37 +00001873 ArgTypes.push_back(t);
1874 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001875 returnType = mDecl->getResultType()->isObjCQualifiedIdType()
1876 ? Context->getObjCIdType() : mDecl->getResultType();
Steve Naroff0744c472007-11-04 22:37:50 +00001877 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +00001878 returnType = Context->getObjCIdType();
Steve Naroff0744c472007-11-04 22:37:50 +00001879 }
1880 // Get the type, we will need to reference it in a couple spots.
Steve Naroff764c1ae2007-11-15 10:28:18 +00001881 QualType msgSendType = MsgSendFlavor->getType();
Steve Naroff0744c472007-11-04 22:37:50 +00001882
1883 // Create a reference to the objc_msgSend() declaration.
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001884 DeclRefExpr *DRE = new DeclRefExpr(MsgSendFlavor, msgSendType,
1885 SourceLocation());
Steve Naroff0744c472007-11-04 22:37:50 +00001886
1887 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
1888 // If we don't do this cast, we get the following bizarre warning/note:
1889 // xx.m:13: warning: function called through a non-compatible type
1890 // xx.m:13: note: if this code is reached, the program will abort
1891 cast = new CastExpr(Context->getPointerType(Context->VoidTy), DRE,
1892 SourceLocation());
Steve Naroff29fe7462007-11-15 12:35:21 +00001893
Steve Naroff0744c472007-11-04 22:37:50 +00001894 // Now do the "normal" pointer to function cast.
1895 QualType castType = Context->getFunctionType(returnType,
Fariborz Jahanianefba8c82007-12-06 19:49:56 +00001896 &ArgTypes[0], ArgTypes.size(),
1897 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
Steve Naroff0744c472007-11-04 22:37:50 +00001898 castType = Context->getPointerType(castType);
1899 cast = new CastExpr(castType, cast, SourceLocation());
1900
1901 // Don't forget the parens to enforce the proper binding.
1902 ParenExpr *PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
1903
1904 const FunctionType *FT = msgSendType->getAsFunctionType();
1905 CallExpr *CE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
1906 FT->getResultType(), SourceLocation());
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001907 Stmt *ReplacingStmt = CE;
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001908 if (MsgSendStretFlavor) {
1909 // We have the method which returns a struct/union. Must also generate
1910 // call to objc_msgSend_stret and hang both varieties on a conditional
1911 // expression which dictate which one to envoke depending on size of
1912 // method's return type.
1913
1914 // Create a reference to the objc_msgSend_stret() declaration.
1915 DeclRefExpr *STDRE = new DeclRefExpr(MsgSendStretFlavor, msgSendType,
1916 SourceLocation());
1917 // Need to cast objc_msgSend_stret to "void *" (see above comment).
1918 cast = new CastExpr(Context->getPointerType(Context->VoidTy), STDRE,
1919 SourceLocation());
1920 // Now do the "normal" pointer to function cast.
1921 castType = Context->getFunctionType(returnType,
Fariborz Jahanianefba8c82007-12-06 19:49:56 +00001922 &ArgTypes[0], ArgTypes.size(),
1923 Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false);
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001924 castType = Context->getPointerType(castType);
1925 cast = new CastExpr(castType, cast, SourceLocation());
1926
1927 // Don't forget the parens to enforce the proper binding.
1928 PE = new ParenExpr(SourceLocation(), SourceLocation(), cast);
1929
1930 FT = msgSendType->getAsFunctionType();
1931 CallExpr *STCE = new CallExpr(PE, &MsgExprs[0], MsgExprs.size(),
1932 FT->getResultType(), SourceLocation());
1933
1934 // Build sizeof(returnType)
1935 SizeOfAlignOfTypeExpr *sizeofExpr = new SizeOfAlignOfTypeExpr(true,
1936 returnType, Context->getSizeType(),
1937 SourceLocation(), SourceLocation());
1938 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
1939 // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases.
1940 // For X86 it is more complicated and some kind of target specific routine
1941 // is needed to decide what to do.
1942 unsigned IntSize = static_cast<unsigned>(
1943 Context->getTypeSize(Context->IntTy, SourceLocation()));
1944
1945 IntegerLiteral *limit = new IntegerLiteral(llvm::APInt(IntSize, 8),
1946 Context->IntTy,
1947 SourceLocation());
1948 BinaryOperator *lessThanExpr = new BinaryOperator(sizeofExpr, limit,
1949 BinaryOperator::LE,
1950 Context->IntTy,
1951 SourceLocation());
1952 // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...))
1953 ConditionalOperator *CondExpr =
1954 new ConditionalOperator(lessThanExpr, CE, STCE, returnType);
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001955 ReplacingStmt = new ParenExpr(SourceLocation(), SourceLocation(), CondExpr);
Fariborz Jahanianc26b2502007-12-03 19:17:29 +00001956 }
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001957 return ReplacingStmt;
1958}
1959
1960Stmt *RewriteTest::RewriteMessageExpr(ObjCMessageExpr *Exp) {
1961 Stmt *ReplacingStmt = SynthMessageExpr(Exp);
Steve Naroff71226032007-10-24 22:48:43 +00001962 // Now do the actual rewrite.
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001963 if (Rewrite.ReplaceStmt(Exp, ReplacingStmt)) {
Steve Naroffe4e5e262007-12-19 14:32:56 +00001964 // replacement failed.
Steve Naroffbcf0a922007-12-19 19:16:49 +00001965 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1966 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroffe4e5e262007-12-19 14:32:56 +00001967 SourceRange Range = Exp->getSourceRange();
1968 Diags.Report(Context->getFullLoc(Exp->getLocStart()), DiagID, 0, 0, &Range, 1);
Steve Naroffe4e5e262007-12-19 14:32:56 +00001969 }
Steve Naroff71226032007-10-24 22:48:43 +00001970
Chris Lattner0021f452007-10-24 16:57:36 +00001971 delete Exp;
Fariborz Jahanian9ea6a2d2008-01-08 22:06:28 +00001972 return ReplacingStmt;
Steve Naroffe9780582007-10-23 23:50:29 +00001973}
1974
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +00001975/// RewriteObjCProtocolExpr - Rewrite a protocol expression into
1976/// call to objc_getProtocol("proto-name").
1977Stmt *RewriteTest::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
1978 if (!GetProtocolFunctionDecl)
1979 SynthGetProtocolFunctionDecl();
1980 // Create a call to objc_getProtocol("ProtocolName").
1981 llvm::SmallVector<Expr*, 8> ProtoExprs;
1982 QualType argType = Context->getPointerType(Context->CharTy);
1983 ProtoExprs.push_back(new StringLiteral(Exp->getProtocol()->getName(),
1984 strlen(Exp->getProtocol()->getName()),
1985 false, argType, SourceLocation(),
1986 SourceLocation()));
1987 CallExpr *ProtoExp = SynthesizeCallToFunctionDecl(GetProtocolFunctionDecl,
1988 &ProtoExprs[0],
1989 ProtoExprs.size());
Steve Naroffe4e5e262007-12-19 14:32:56 +00001990 if (Rewrite.ReplaceStmt(Exp, ProtoExp)) {
1991 // replacement failed.
Steve Naroffbcf0a922007-12-19 19:16:49 +00001992 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Warning,
1993 "rewriting sub-expression within a macro (may not be correct)");
Steve Naroffe4e5e262007-12-19 14:32:56 +00001994 SourceRange Range = Exp->getSourceRange();
1995 Diags.Report(Context->getFullLoc(Exp->getAtLoc()), DiagID, 0, 0, &Range, 1);
Steve Naroffe4e5e262007-12-19 14:32:56 +00001996 }
Fariborz Jahanian6ff57c62007-12-07 18:47:10 +00001997 delete Exp;
1998 return ProtoExp;
1999
2000}
2001
Ted Kremenek42730c52008-01-07 19:49:32 +00002002/// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002003/// an objective-c class with ivars.
Ted Kremenek42730c52008-01-07 19:49:32 +00002004void RewriteTest::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl,
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002005 std::string &Result) {
Ted Kremenek42730c52008-01-07 19:49:32 +00002006 assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
2007 assert(CDecl->getName() && "Name missing in SynthesizeObjCInternalStruct");
Fariborz Jahanianfb4f6a32007-10-31 23:08:24 +00002008 // Do not synthesize more than once.
Ted Kremenek42730c52008-01-07 19:49:32 +00002009 if (ObjCSynthesizedStructs.count(CDecl))
Fariborz Jahanianfb4f6a32007-10-31 23:08:24 +00002010 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002011 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
Steve Naroffdd2e26c2007-11-12 13:56:41 +00002012 int NumIvars = CDecl->getNumInstanceVariables();
Steve Naroff2c7afc92007-11-14 19:25:57 +00002013 SourceLocation LocStart = CDecl->getLocStart();
2014 SourceLocation LocEnd = CDecl->getLocEnd();
2015
2016 const char *startBuf = SM->getCharacterData(LocStart);
2017 const char *endBuf = SM->getCharacterData(LocEnd);
Fariborz Jahanian920cde32007-11-26 19:52:57 +00002018 // If no ivars and no root or if its root, directly or indirectly,
2019 // have no ivars (thus not synthesized) then no need to synthesize this class.
Ted Kremenek42730c52008-01-07 19:49:32 +00002020 if (NumIvars <= 0 && (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
Fariborz Jahanian920cde32007-11-26 19:52:57 +00002021 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
2022 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
2023 Result.c_str(), Result.size());
2024 return;
2025 }
2026
2027 // FIXME: This has potential of causing problem. If
Ted Kremenek42730c52008-01-07 19:49:32 +00002028 // SynthesizeObjCInternalStruct is ever called recursively.
Fariborz Jahanian920cde32007-11-26 19:52:57 +00002029 Result += "\nstruct ";
2030 Result += CDecl->getName();
Steve Naroff2c7afc92007-11-14 19:25:57 +00002031
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +00002032 if (NumIvars > 0) {
Steve Naroff2c7afc92007-11-14 19:25:57 +00002033 const char *cursor = strchr(startBuf, '{');
2034 assert((cursor && endBuf)
Ted Kremenek42730c52008-01-07 19:49:32 +00002035 && "SynthesizeObjCInternalStruct - malformed @interface");
Steve Naroff2c7afc92007-11-14 19:25:57 +00002036
2037 // rewrite the original header *without* disturbing the '{'
2038 Rewrite.ReplaceText(LocStart, cursor-startBuf-1,
2039 Result.c_str(), Result.size());
Ted Kremenek42730c52008-01-07 19:49:32 +00002040 if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
Steve Naroff2c7afc92007-11-14 19:25:57 +00002041 Result = "\n struct ";
2042 Result += RCDecl->getName();
Steve Narofff2ee53f2007-12-07 22:15:58 +00002043 // Note: We don't name the field decl. This simplifies the "codegen" for
2044 // accessing a superclasses instance variables (and is similar to what gcc
2045 // does internally). The unnamed struct field feature is enabled with
2046 // -fms-extensions. If the struct definition were "inlined", we wouldn't
2047 // need to use this switch. That said, I don't want to inline the def.
Steve Naroff2c7afc92007-11-14 19:25:57 +00002048 Result += ";\n";
2049
2050 // insert the super class structure definition.
2051 SourceLocation OnePastCurly = LocStart.getFileLocWithOffset(cursor-startBuf+1);
2052 Rewrite.InsertText(OnePastCurly, Result.c_str(), Result.size());
2053 }
2054 cursor++; // past '{'
2055
2056 // Now comment out any visibility specifiers.
2057 while (cursor < endBuf) {
2058 if (*cursor == '@') {
2059 SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
Chris Lattnerf04ead52007-11-14 22:57:51 +00002060 // Skip whitespace.
2061 for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor)
2062 /*scan*/;
2063
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +00002064 // FIXME: presence of @public, etc. inside comment results in
2065 // this transformation as well, which is still correct c-code.
Steve Naroff2c7afc92007-11-14 19:25:57 +00002066 if (!strncmp(cursor, "public", strlen("public")) ||
2067 !strncmp(cursor, "private", strlen("private")) ||
Fariborz Jahanian8d9c7352007-11-14 22:26:25 +00002068 !strncmp(cursor, "protected", strlen("protected")))
Steve Naroff2c7afc92007-11-14 19:25:57 +00002069 Rewrite.InsertText(atLoc, "// ", 3);
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +00002070 }
Fariborz Jahanian8d9c7352007-11-14 22:26:25 +00002071 // FIXME: If there are cases where '<' is used in ivar declaration part
2072 // of user code, then scan the ivar list and use needToScanForQualifiers
2073 // for type checking.
2074 else if (*cursor == '<') {
2075 SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
2076 Rewrite.InsertText(atLoc, "/* ", 3);
2077 cursor = strchr(cursor, '>');
2078 cursor++;
2079 atLoc = LocStart.getFileLocWithOffset(cursor-startBuf);
2080 Rewrite.InsertText(atLoc, " */", 3);
2081 }
Steve Naroff2c7afc92007-11-14 19:25:57 +00002082 cursor++;
Fariborz Jahanian6acfa2b2007-10-31 17:29:28 +00002083 }
Steve Naroff2c7afc92007-11-14 19:25:57 +00002084 // Don't forget to add a ';'!!
2085 Rewrite.InsertText(LocEnd.getFileLocWithOffset(1), ";", 1);
2086 } else { // we don't have any instance variables - insert super struct.
2087 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
2088 Result += " {\n struct ";
2089 Result += RCDecl->getName();
Steve Narofff2ee53f2007-12-07 22:15:58 +00002090 // Note: We don't name the field decl. This simplifies the "codegen" for
2091 // accessing a superclasses instance variables (and is similar to what gcc
2092 // does internally). The unnamed struct field feature is enabled with
2093 // -fms-extensions. If the struct definition were "inlined", we wouldn't
2094 // need to use this switch. That said, I don't want to inline the def.
Steve Naroff2c7afc92007-11-14 19:25:57 +00002095 Result += ";\n};\n";
2096 Rewrite.ReplaceText(LocStart, endBuf-startBuf,
2097 Result.c_str(), Result.size());
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002098 }
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002099 // Mark this struct as having been generated.
Ted Kremenek42730c52008-01-07 19:49:32 +00002100 if (!ObjCSynthesizedStructs.insert(CDecl))
2101 assert(false && "struct already synthesize- SynthesizeObjCInternalStruct");
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002102}
2103
Ted Kremenek42730c52008-01-07 19:49:32 +00002104// RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002105/// class methods.
Ted Kremenek42730c52008-01-07 19:49:32 +00002106void RewriteTest::RewriteObjCMethodsMetaData(instmeth_iterator MethodBegin,
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002107 instmeth_iterator MethodEnd,
Fariborz Jahaniana3986372007-10-25 00:14:44 +00002108 bool IsInstanceMethod,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002109 const char *prefix,
Chris Lattnerc3aa5c42007-10-25 17:07:24 +00002110 const char *ClassName,
2111 std::string &Result) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002112 if (MethodBegin == MethodEnd) return;
2113
Fariborz Jahanian04455192007-10-22 21:41:37 +00002114 static bool objc_impl_method = false;
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002115 if (!objc_impl_method) {
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002116 /* struct _objc_method {
Fariborz Jahanian04455192007-10-22 21:41:37 +00002117 SEL _cmd;
2118 char *method_types;
2119 void *_imp;
2120 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002121 */
Chris Lattnerc3aa5c42007-10-25 17:07:24 +00002122 Result += "\nstruct _objc_method {\n";
2123 Result += "\tSEL _cmd;\n";
2124 Result += "\tchar *method_types;\n";
2125 Result += "\tvoid *_imp;\n";
2126 Result += "};\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002127
2128 /* struct _objc_method_list {
2129 struct _objc_method_list *next_method;
2130 int method_count;
2131 struct _objc_method method_list[];
2132 }
2133 */
2134 Result += "\nstruct _objc_method_list {\n";
2135 Result += "\tstruct _objc_method_list *next_method;\n";
2136 Result += "\tint method_count;\n";
2137 Result += "\tstruct _objc_method method_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002138 objc_impl_method = true;
Fariborz Jahanian96b55da2007-10-19 00:36:46 +00002139 }
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002140
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002141 // Build _objc_method_list for class's methods if needed
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002142 Result += "\nstatic struct _objc_method_list _OBJC_";
2143 Result += prefix;
2144 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
2145 Result += "_METHODS_";
2146 Result += ClassName;
2147 Result += " __attribute__ ((section (\"__OBJC, __";
2148 Result += IsInstanceMethod ? "inst" : "cls";
2149 Result += "_meth\")))= ";
2150 Result += "{\n\t0, " + utostr(MethodEnd-MethodBegin) + "\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002151
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002152 Result += "\t,{{(SEL)\"";
2153 Result += (*MethodBegin)->getSelector().getName().c_str();
2154 std::string MethodTypeString;
Ted Kremenek42730c52008-01-07 19:49:32 +00002155 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002156 Result += "\", \"";
2157 Result += MethodTypeString;
2158 Result += "\", ";
2159 Result += MethodInternalNames[*MethodBegin];
2160 Result += "}\n";
2161 for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
2162 Result += "\t ,{(SEL)\"";
2163 Result += (*MethodBegin)->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002164 std::string MethodTypeString;
Ted Kremenek42730c52008-01-07 19:49:32 +00002165 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002166 Result += "\", \"";
2167 Result += MethodTypeString;
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +00002168 Result += "\", ";
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002169 Result += MethodInternalNames[*MethodBegin];
Fariborz Jahanianbd2fd922007-11-13 21:02:00 +00002170 Result += "}\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +00002171 }
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002172 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002173}
2174
Ted Kremenek42730c52008-01-07 19:49:32 +00002175/// RewriteObjCProtocolsMetaData - Rewrite protocols meta-data.
2176void RewriteTest::RewriteObjCProtocolsMetaData(ObjCProtocolDecl **Protocols,
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002177 int NumProtocols,
2178 const char *prefix,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002179 const char *ClassName,
2180 std::string &Result) {
Fariborz Jahanian04455192007-10-22 21:41:37 +00002181 static bool objc_protocol_methods = false;
Fariborz Jahanian04455192007-10-22 21:41:37 +00002182 if (NumProtocols > 0) {
Fariborz Jahanian04455192007-10-22 21:41:37 +00002183 for (int i = 0; i < NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00002184 ObjCProtocolDecl *PDecl = Protocols[i];
Fariborz Jahanian04455192007-10-22 21:41:37 +00002185 // Output struct protocol_methods holder of method selector and type.
2186 if (!objc_protocol_methods &&
2187 (PDecl->getNumInstanceMethods() > 0
2188 || PDecl->getNumClassMethods() > 0)) {
2189 /* struct protocol_methods {
2190 SEL _cmd;
2191 char *method_types;
2192 }
2193 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002194 Result += "\nstruct protocol_methods {\n";
2195 Result += "\tSEL _cmd;\n";
2196 Result += "\tchar *method_types;\n";
2197 Result += "};\n";
2198
2199 /* struct _objc_protocol_method_list {
2200 int protocol_method_count;
2201 struct protocol_methods protocols[];
2202 }
2203 */
2204 Result += "\nstruct _objc_protocol_method_list {\n";
2205 Result += "\tint protocol_method_count;\n";
2206 Result += "\tstruct protocol_methods protocols[];\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +00002207 objc_protocol_methods = true;
2208 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002209
Fariborz Jahanianbe63dd52007-12-17 17:56:10 +00002210 int NumMethods = PDecl->getNumInstanceMethods();
2211 if(NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002212 Result += "\nstatic struct _objc_protocol_method_list "
2213 "_OBJC_PROTOCOL_INSTANCE_METHODS_";
2214 Result += PDecl->getName();
2215 Result += " __attribute__ ((section (\"__OBJC, __cat_inst_meth\")))= "
2216 "{\n\t" + utostr(NumMethods) + "\n";
2217
Fariborz Jahanianbe63dd52007-12-17 17:56:10 +00002218 // Output instance methods declared in this protocol.
Ted Kremenek42730c52008-01-07 19:49:32 +00002219 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Fariborz Jahanianbe63dd52007-12-17 17:56:10 +00002220 E = PDecl->instmeth_end(); I != E; ++I) {
2221 if (I == PDecl->instmeth_begin())
2222 Result += "\t ,{{(SEL)\"";
2223 else
2224 Result += "\t ,{(SEL)\"";
2225 Result += (*I)->getSelector().getName().c_str();
2226 std::string MethodTypeString;
Ted Kremenek42730c52008-01-07 19:49:32 +00002227 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
Fariborz Jahanianbe63dd52007-12-17 17:56:10 +00002228 Result += "\", \"";
2229 Result += MethodTypeString;
2230 Result += "\"}\n";
2231 }
2232 Result += "\t }\n};\n";
2233 }
Fariborz Jahanian04455192007-10-22 21:41:37 +00002234
2235 // Output class methods declared in this protocol.
2236 NumMethods = PDecl->getNumClassMethods();
2237 if (NumMethods > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002238 Result += "\nstatic struct _objc_protocol_method_list "
2239 "_OBJC_PROTOCOL_CLASS_METHODS_";
2240 Result += PDecl->getName();
2241 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
2242 "{\n\t";
2243 Result += utostr(NumMethods);
2244 Result += "\n";
2245
Fariborz Jahanian65a3d852007-12-17 18:07:01 +00002246 // Output instance methods declared in this protocol.
Ted Kremenek42730c52008-01-07 19:49:32 +00002247 for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
Fariborz Jahanian65a3d852007-12-17 18:07:01 +00002248 E = PDecl->classmeth_end(); I != E; ++I) {
2249 if (I == PDecl->classmeth_begin())
2250 Result += "\t ,{{(SEL)\"";
2251 else
2252 Result += "\t ,{(SEL)\"";
Steve Naroff2ce399a2007-12-14 23:37:57 +00002253 Result += (*I)->getSelector().getName().c_str();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002254 std::string MethodTypeString;
Ted Kremenek42730c52008-01-07 19:49:32 +00002255 Context->getObjCEncodingForMethodDecl((*I), MethodTypeString);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002256 Result += "\", \"";
2257 Result += MethodTypeString;
2258 Result += "\"}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002259 }
2260 Result += "\t }\n};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +00002261 }
2262 // Output:
2263 /* struct _objc_protocol {
2264 // Objective-C 1.0 extensions
2265 struct _objc_protocol_extension *isa;
2266 char *protocol_name;
2267 struct _objc_protocol **protocol_list;
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002268 struct _objc_protocol_method_list *instance_methods;
2269 struct _objc_protocol_method_list *class_methods;
Fariborz Jahanian04455192007-10-22 21:41:37 +00002270 };
2271 */
2272 static bool objc_protocol = false;
2273 if (!objc_protocol) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002274 Result += "\nstruct _objc_protocol {\n";
2275 Result += "\tstruct _objc_protocol_extension *isa;\n";
2276 Result += "\tchar *protocol_name;\n";
2277 Result += "\tstruct _objc_protocol **protocol_list;\n";
2278 Result += "\tstruct _objc_protocol_method_list *instance_methods;\n";
2279 Result += "\tstruct _objc_protocol_method_list *class_methods;\n";
2280 Result += "};\n";
2281
2282 /* struct _objc_protocol_list {
2283 struct _objc_protocol_list *next;
2284 int protocol_count;
2285 struct _objc_protocol *class_protocols[];
2286 }
2287 */
2288 Result += "\nstruct _objc_protocol_list {\n";
2289 Result += "\tstruct _objc_protocol_list *next;\n";
2290 Result += "\tint protocol_count;\n";
2291 Result += "\tstruct _objc_protocol *class_protocols[];\n";
2292 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +00002293 objc_protocol = true;
2294 }
2295
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002296 Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_";
2297 Result += PDecl->getName();
2298 Result += " __attribute__ ((section (\"__OBJC, __protocol\")))= "
2299 "{\n\t0, \"";
2300 Result += PDecl->getName();
2301 Result += "\", 0, ";
Steve Naroff2ce399a2007-12-14 23:37:57 +00002302 if (PDecl->getNumInstanceMethods() > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002303 Result += "&_OBJC_PROTOCOL_INSTANCE_METHODS_";
2304 Result += PDecl->getName();
2305 Result += ", ";
2306 }
Fariborz Jahanian04455192007-10-22 21:41:37 +00002307 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002308 Result += "0, ";
Steve Naroff2ce399a2007-12-14 23:37:57 +00002309 if (PDecl->getNumClassMethods() > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002310 Result += "&_OBJC_PROTOCOL_CLASS_METHODS_";
2311 Result += PDecl->getName();
2312 Result += "\n";
2313 }
Fariborz Jahanian04455192007-10-22 21:41:37 +00002314 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002315 Result += "0\n";
2316 Result += "};\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +00002317 }
Fariborz Jahanian04455192007-10-22 21:41:37 +00002318 // Output the top lovel protocol meta-data for the class.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002319 Result += "\nstatic struct _objc_protocol_list _OBJC_";
2320 Result += prefix;
2321 Result += "_PROTOCOLS_";
2322 Result += ClassName;
2323 Result += " __attribute__ ((section (\"__OBJC, __cat_cls_meth\")))= "
2324 "{\n\t0, ";
2325 Result += utostr(NumProtocols);
2326 Result += "\n";
2327
2328 Result += "\t,{&_OBJC_PROTOCOL_";
2329 Result += Protocols[0]->getName();
2330 Result += " \n";
2331
2332 for (int i = 1; i < NumProtocols; i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00002333 ObjCProtocolDecl *PDecl = Protocols[i];
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002334 Result += "\t ,&_OBJC_PROTOCOL_";
2335 Result += PDecl->getName();
2336 Result += "\n";
Fariborz Jahanian04455192007-10-22 21:41:37 +00002337 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002338 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002339 }
2340}
2341
Ted Kremenek42730c52008-01-07 19:49:32 +00002342/// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002343/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +00002344void RewriteTest::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002345 std::string &Result) {
Ted Kremenek42730c52008-01-07 19:49:32 +00002346 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002347 // Find category declaration for this implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +00002348 ObjCCategoryDecl *CDecl;
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002349 for (CDecl = ClassDecl->getCategoryList(); CDecl;
2350 CDecl = CDecl->getNextClassCategory())
2351 if (CDecl->getIdentifier() == IDecl->getIdentifier())
2352 break;
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002353
Chris Lattnera661a4d2007-12-23 01:40:15 +00002354 std::string FullCategoryName = ClassDecl->getName();
2355 FullCategoryName += '_';
2356 FullCategoryName += IDecl->getName();
2357
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002358 // Build _objc_method_list for class's instance methods if needed
Ted Kremenek42730c52008-01-07 19:49:32 +00002359 RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
Chris Lattnera661a4d2007-12-23 01:40:15 +00002360 true, "CATEGORY_", FullCategoryName.c_str(),
2361 Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002362
2363 // Build _objc_method_list for class's class methods if needed
Ted Kremenek42730c52008-01-07 19:49:32 +00002364 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
Chris Lattnera661a4d2007-12-23 01:40:15 +00002365 false, "CATEGORY_", FullCategoryName.c_str(),
2366 Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002367
2368 // Protocols referenced in class declaration?
Fariborz Jahaniand256e062007-11-13 22:09:49 +00002369 // Null CDecl is case of a category implementation with no category interface
2370 if (CDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00002371 RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
Fariborz Jahaniand256e062007-11-13 22:09:49 +00002372 CDecl->getNumReferencedProtocols(),
2373 "CATEGORY",
Chris Lattnera661a4d2007-12-23 01:40:15 +00002374 FullCategoryName.c_str(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002375
2376 /* struct _objc_category {
2377 char *category_name;
2378 char *class_name;
2379 struct _objc_method_list *instance_methods;
2380 struct _objc_method_list *class_methods;
2381 struct _objc_protocol_list *protocols;
2382 // Objective-C 1.0 extensions
2383 uint32_t size; // sizeof (struct _objc_category)
2384 struct _objc_property_list *instance_properties; // category's own
2385 // @property decl.
2386 };
2387 */
2388
2389 static bool objc_category = false;
2390 if (!objc_category) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002391 Result += "\nstruct _objc_category {\n";
2392 Result += "\tchar *category_name;\n";
2393 Result += "\tchar *class_name;\n";
2394 Result += "\tstruct _objc_method_list *instance_methods;\n";
2395 Result += "\tstruct _objc_method_list *class_methods;\n";
2396 Result += "\tstruct _objc_protocol_list *protocols;\n";
2397 Result += "\tunsigned int size;\n";
2398 Result += "\tstruct _objc_property_list *instance_properties;\n";
2399 Result += "};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002400 objc_category = true;
Fariborz Jahanian04455192007-10-22 21:41:37 +00002401 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002402 Result += "\nstatic struct _objc_category _OBJC_CATEGORY_";
2403 Result += FullCategoryName;
2404 Result += " __attribute__ ((section (\"__OBJC, __category\")))= {\n\t\"";
2405 Result += IDecl->getName();
2406 Result += "\"\n\t, \"";
2407 Result += ClassDecl->getName();
2408 Result += "\"\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002409
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002410 if (IDecl->getNumInstanceMethods() > 0) {
2411 Result += "\t, (struct _objc_method_list *)"
2412 "&_OBJC_CATEGORY_INSTANCE_METHODS_";
2413 Result += FullCategoryName;
2414 Result += "\n";
2415 }
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002416 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002417 Result += "\t, 0\n";
2418 if (IDecl->getNumClassMethods() > 0) {
2419 Result += "\t, (struct _objc_method_list *)"
2420 "&_OBJC_CATEGORY_CLASS_METHODS_";
2421 Result += FullCategoryName;
2422 Result += "\n";
2423 }
2424 else
2425 Result += "\t, 0\n";
2426
Fariborz Jahaniand256e062007-11-13 22:09:49 +00002427 if (CDecl && CDecl->getNumReferencedProtocols() > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002428 Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_";
2429 Result += FullCategoryName;
2430 Result += "\n";
2431 }
2432 else
2433 Result += "\t, 0\n";
2434 Result += "\t, sizeof(struct _objc_category), 0\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002435}
2436
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002437/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of
2438/// ivar offset.
Ted Kremenek42730c52008-01-07 19:49:32 +00002439void RewriteTest::SynthesizeIvarOffsetComputation(ObjCImplementationDecl *IDecl,
2440 ObjCIvarDecl *ivar,
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002441 std::string &Result) {
Fariborz Jahanian9447e462007-11-05 17:47:33 +00002442 Result += "offsetof(struct ";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002443 Result += IDecl->getName();
2444 Result += ", ";
2445 Result += ivar->getName();
2446 Result += ")";
2447}
2448
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002449//===----------------------------------------------------------------------===//
2450// Meta Data Emission
2451//===----------------------------------------------------------------------===//
2452
Ted Kremenek42730c52008-01-07 19:49:32 +00002453void RewriteTest::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002454 std::string &Result) {
Ted Kremenek42730c52008-01-07 19:49:32 +00002455 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002456
Fariborz Jahanian7e216522007-11-26 20:59:57 +00002457 // Explictly declared @interface's are already synthesized.
2458 if (CDecl->ImplicitInterfaceDecl()) {
2459 // FIXME: Implementation of a class with no @interface (legacy) doese not
2460 // produce correct synthesis as yet.
Ted Kremenek42730c52008-01-07 19:49:32 +00002461 SynthesizeObjCInternalStruct(CDecl, Result);
Fariborz Jahanian7e216522007-11-26 20:59:57 +00002462 }
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00002463
Chris Lattnerc7b06752007-12-12 07:56:42 +00002464 // Build _objc_ivar_list metadata for classes ivars if needed
2465 int NumIvars = IDecl->getImplDeclNumIvars() > 0
2466 ? IDecl->getImplDeclNumIvars()
2467 : (CDecl ? CDecl->getNumInstanceVariables() : 0);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002468 if (NumIvars > 0) {
2469 static bool objc_ivar = false;
2470 if (!objc_ivar) {
2471 /* struct _objc_ivar {
2472 char *ivar_name;
2473 char *ivar_type;
2474 int ivar_offset;
2475 };
2476 */
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002477 Result += "\nstruct _objc_ivar {\n";
2478 Result += "\tchar *ivar_name;\n";
2479 Result += "\tchar *ivar_type;\n";
2480 Result += "\tint ivar_offset;\n";
2481 Result += "};\n";
2482
2483 /* struct _objc_ivar_list {
2484 int ivar_count;
2485 struct _objc_ivar ivar_list[];
2486 };
2487 */
2488 Result += "\nstruct _objc_ivar_list {\n";
2489 Result += "\tint ivar_count;\n";
2490 Result += "\tstruct _objc_ivar ivar_list[];\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002491 objc_ivar = true;
2492 }
2493
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002494 Result += "\nstatic struct _objc_ivar_list _OBJC_INSTANCE_VARIABLES_";
2495 Result += IDecl->getName();
2496 Result += " __attribute__ ((section (\"__OBJC, __instance_vars\")))= "
2497 "{\n\t";
2498 Result += utostr(NumIvars);
2499 Result += "\n";
Chris Lattnerc7b06752007-12-12 07:56:42 +00002500
Ted Kremenek42730c52008-01-07 19:49:32 +00002501 ObjCInterfaceDecl::ivar_iterator IVI, IVE;
Chris Lattnerc7b06752007-12-12 07:56:42 +00002502 if (IDecl->getImplDeclNumIvars() > 0) {
2503 IVI = IDecl->ivar_begin();
2504 IVE = IDecl->ivar_end();
2505 } else {
2506 IVI = CDecl->ivar_begin();
2507 IVE = CDecl->ivar_end();
2508 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002509 Result += "\t,{{\"";
Chris Lattnerc7b06752007-12-12 07:56:42 +00002510 Result += (*IVI)->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00002511 Result += "\", \"";
2512 std::string StrEncoding;
Ted Kremenek42730c52008-01-07 19:49:32 +00002513 Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding);
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00002514 Result += StrEncoding;
2515 Result += "\", ";
Chris Lattnerc7b06752007-12-12 07:56:42 +00002516 SynthesizeIvarOffsetComputation(IDecl, *IVI, Result);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002517 Result += "}\n";
Chris Lattnerc7b06752007-12-12 07:56:42 +00002518 for (++IVI; IVI != IVE; ++IVI) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002519 Result += "\t ,{\"";
Chris Lattnerc7b06752007-12-12 07:56:42 +00002520 Result += (*IVI)->getName();
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00002521 Result += "\", \"";
2522 std::string StrEncoding;
Ted Kremenek42730c52008-01-07 19:49:32 +00002523 Context->getObjCEncodingForType((*IVI)->getType(), StrEncoding);
Fariborz Jahaniand5ea4612007-10-29 17:16:25 +00002524 Result += StrEncoding;
2525 Result += "\", ";
Chris Lattnerc7b06752007-12-12 07:56:42 +00002526 SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result);
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002527 Result += "}\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002528 }
2529
2530 Result += "\t }\n};\n";
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002531 }
2532
2533 // Build _objc_method_list for class's instance methods if needed
Ted Kremenek42730c52008-01-07 19:49:32 +00002534 RewriteObjCMethodsMetaData(IDecl->instmeth_begin(), IDecl->instmeth_end(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002535 true, "", IDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002536
2537 // Build _objc_method_list for class's class methods if needed
Ted Kremenek42730c52008-01-07 19:49:32 +00002538 RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002539 false, "", IDecl->getName(), Result);
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002540
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002541 // Protocols referenced in class declaration?
Ted Kremenek42730c52008-01-07 19:49:32 +00002542 RewriteObjCProtocolsMetaData(CDecl->getReferencedProtocols(),
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002543 CDecl->getNumIntfRefProtocols(),
Chris Lattnerdea5bec2007-12-12 07:46:12 +00002544 "CLASS", CDecl->getName(), Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002545
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002546
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002547 // Declaration of class/meta-class metadata
2548 /* struct _objc_class {
2549 struct _objc_class *isa; // or const char *root_class_name when metadata
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002550 const char *super_class_name;
2551 char *name;
2552 long version;
2553 long info;
2554 long instance_size;
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002555 struct _objc_ivar_list *ivars;
2556 struct _objc_method_list *methods;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002557 struct objc_cache *cache;
2558 struct objc_protocol_list *protocols;
2559 const char *ivar_layout;
2560 struct _objc_class_ext *ext;
2561 };
2562 */
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002563 static bool objc_class = false;
2564 if (!objc_class) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002565 Result += "\nstruct _objc_class {\n";
2566 Result += "\tstruct _objc_class *isa;\n";
2567 Result += "\tconst char *super_class_name;\n";
2568 Result += "\tchar *name;\n";
2569 Result += "\tlong version;\n";
2570 Result += "\tlong info;\n";
2571 Result += "\tlong instance_size;\n";
2572 Result += "\tstruct _objc_ivar_list *ivars;\n";
2573 Result += "\tstruct _objc_method_list *methods;\n";
2574 Result += "\tstruct objc_cache *cache;\n";
2575 Result += "\tstruct _objc_protocol_list *protocols;\n";
2576 Result += "\tconst char *ivar_layout;\n";
2577 Result += "\tstruct _objc_class_ext *ext;\n";
2578 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002579 objc_class = true;
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002580 }
2581
2582 // Meta-class metadata generation.
Ted Kremenek42730c52008-01-07 19:49:32 +00002583 ObjCInterfaceDecl *RootClass = 0;
2584 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002585 while (SuperClass) {
2586 RootClass = SuperClass;
2587 SuperClass = SuperClass->getSuperClass();
2588 }
2589 SuperClass = CDecl->getSuperClass();
2590
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002591 Result += "\nstatic struct _objc_class _OBJC_METACLASS_";
2592 Result += CDecl->getName();
2593 Result += " __attribute__ ((section (\"__OBJC, __meta_class\")))= "
2594 "{\n\t(struct _objc_class *)\"";
2595 Result += (RootClass ? RootClass->getName() : CDecl->getName());
2596 Result += "\"";
2597
2598 if (SuperClass) {
2599 Result += ", \"";
2600 Result += SuperClass->getName();
2601 Result += "\", \"";
2602 Result += CDecl->getName();
2603 Result += "\"";
2604 }
2605 else {
2606 Result += ", 0, \"";
2607 Result += CDecl->getName();
2608 Result += "\"";
2609 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002610 // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it.
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002611 // 'info' field is initialized to CLS_META(2) for metaclass
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002612 Result += ", 0,2, sizeof(struct _objc_class), 0";
Steve Naroffbde81042007-12-05 21:49:40 +00002613 if (IDecl->getNumClassMethods() > 0) {
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002614 Result += "\n\t, &_OBJC_CLASS_METHODS_";
Steve Naroffbde81042007-12-05 21:49:40 +00002615 Result += IDecl->getName();
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002616 Result += "\n";
2617 }
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002618 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002619 Result += ", 0\n";
2620 if (CDecl->getNumIntfRefProtocols() > 0) {
2621 Result += "\t,0, &_OBJC_CLASS_PROTOCOLS_";
2622 Result += CDecl->getName();
2623 Result += ",0,0\n";
2624 }
Fariborz Jahanian0cb4d922007-10-24 20:54:23 +00002625 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002626 Result += "\t,0,0,0,0\n";
2627 Result += "};\n";
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002628
2629 // class metadata generation.
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002630 Result += "\nstatic struct _objc_class _OBJC_CLASS_";
2631 Result += CDecl->getName();
2632 Result += " __attribute__ ((section (\"__OBJC, __class\")))= "
2633 "{\n\t&_OBJC_METACLASS_";
2634 Result += CDecl->getName();
2635 if (SuperClass) {
2636 Result += ", \"";
2637 Result += SuperClass->getName();
2638 Result += "\", \"";
2639 Result += CDecl->getName();
2640 Result += "\"";
2641 }
2642 else {
2643 Result += ", 0, \"";
2644 Result += CDecl->getName();
2645 Result += "\"";
2646 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002647 // 'info' field is initialized to CLS_CLASS(1) for class
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00002648 Result += ", 0,1";
Ted Kremenek42730c52008-01-07 19:49:32 +00002649 if (!ObjCSynthesizedStructs.count(CDecl))
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00002650 Result += ",0";
2651 else {
2652 // class has size. Must synthesize its size.
Fariborz Jahanian9447e462007-11-05 17:47:33 +00002653 Result += ",sizeof(struct ";
Fariborz Jahanianab3ec252007-10-26 23:09:28 +00002654 Result += CDecl->getName();
2655 Result += ")";
2656 }
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002657 if (NumIvars > 0) {
2658 Result += ", &_OBJC_INSTANCE_VARIABLES_";
2659 Result += CDecl->getName();
2660 Result += "\n\t";
2661 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002662 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002663 Result += ",0";
2664 if (IDecl->getNumInstanceMethods() > 0) {
2665 Result += ", &_OBJC_INSTANCE_METHODS_";
2666 Result += CDecl->getName();
2667 Result += ", 0\n\t";
2668 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002669 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002670 Result += ",0,0";
2671 if (CDecl->getNumIntfRefProtocols() > 0) {
2672 Result += ", &_OBJC_CLASS_PROTOCOLS_";
2673 Result += CDecl->getName();
2674 Result += ", 0,0\n";
2675 }
Fariborz Jahanianf8fa1e72007-10-23 18:53:48 +00002676 else
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002677 Result += ",0,0,0\n";
2678 Result += "};\n";
Fariborz Jahanian0f013d12007-10-23 00:02:02 +00002679}
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00002680
Fariborz Jahanian8c664912007-11-13 19:21:13 +00002681/// RewriteImplementations - This routine rewrites all method implementations
2682/// and emits meta-data.
2683
2684void RewriteTest::RewriteImplementations(std::string &Result) {
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002685 int ClsDefCount = ClassImplementation.size();
2686 int CatDefCount = CategoryImplementation.size();
Fariborz Jahanian8c664912007-11-13 19:21:13 +00002687
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002688 if (ClsDefCount == 0 && CatDefCount == 0)
2689 return;
Fariborz Jahanian8c664912007-11-13 19:21:13 +00002690 // Rewrite implemented methods
2691 for (int i = 0; i < ClsDefCount; i++)
2692 RewriteImplementationDecl(ClassImplementation[i]);
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00002693
Fariborz Jahanian0136e372007-11-13 20:04:28 +00002694 for (int i = 0; i < CatDefCount; i++)
2695 RewriteImplementationDecl(CategoryImplementation[i]);
2696
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002697 // This is needed for use of offsetof
2698 Result += "#include <stddef.h>\n";
Fariborz Jahanian8c664912007-11-13 19:21:13 +00002699
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002700 // For each implemented class, write out all its meta data.
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00002701 for (int i = 0; i < ClsDefCount; i++)
Ted Kremenek42730c52008-01-07 19:49:32 +00002702 RewriteObjCClassMetaData(ClassImplementation[i], Result);
Fariborz Jahanian9b4e4ff2007-10-24 19:23:36 +00002703
2704 // For each implemented category, write out all its meta data.
2705 for (int i = 0; i < CatDefCount; i++)
Ted Kremenek42730c52008-01-07 19:49:32 +00002706 RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
Fariborz Jahanian45d52f72007-10-18 22:09:03 +00002707
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002708 // Write objc_symtab metadata
2709 /*
2710 struct _objc_symtab
2711 {
2712 long sel_ref_cnt;
2713 SEL *refs;
2714 short cls_def_cnt;
2715 short cat_def_cnt;
2716 void *defs[cls_def_cnt + cat_def_cnt];
2717 };
2718 */
2719
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002720 Result += "\nstruct _objc_symtab {\n";
2721 Result += "\tlong sel_ref_cnt;\n";
2722 Result += "\tSEL *refs;\n";
2723 Result += "\tshort cls_def_cnt;\n";
2724 Result += "\tshort cat_def_cnt;\n";
2725 Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n";
2726 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002727
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002728 Result += "static struct _objc_symtab "
2729 "_OBJC_SYMBOLS __attribute__((section (\"__OBJC, __symbols\")))= {\n";
2730 Result += "\t0, 0, " + utostr(ClsDefCount)
2731 + ", " + utostr(CatDefCount) + "\n";
2732 for (int i = 0; i < ClsDefCount; i++) {
2733 Result += "\t,&_OBJC_CLASS_";
2734 Result += ClassImplementation[i]->getName();
2735 Result += "\n";
2736 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002737
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002738 for (int i = 0; i < CatDefCount; i++) {
2739 Result += "\t,&_OBJC_CATEGORY_";
2740 Result += CategoryImplementation[i]->getClassInterface()->getName();
2741 Result += "_";
2742 Result += CategoryImplementation[i]->getName();
2743 Result += "\n";
2744 }
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002745
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002746 Result += "};\n\n";
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002747
2748 // Write objc_module metadata
2749
2750 /*
2751 struct _objc_module {
2752 long version;
2753 long size;
2754 const char *name;
2755 struct _objc_symtab *symtab;
2756 }
2757 */
2758
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002759 Result += "\nstruct _objc_module {\n";
2760 Result += "\tlong version;\n";
2761 Result += "\tlong size;\n";
2762 Result += "\tconst char *name;\n";
2763 Result += "\tstruct _objc_symtab *symtab;\n";
2764 Result += "};\n\n";
2765 Result += "static struct _objc_module "
2766 "_OBJC_MODULES __attribute__ ((section (\"__OBJC, __module_info\")))= {\n";
Fariborz Jahanianf185aef2007-10-26 19:46:17 +00002767 Result += "\t" + utostr(OBJC_ABI_VERSION) +
2768 ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n";
Fariborz Jahaniancf89c7e2007-10-25 20:55:25 +00002769 Result += "};\n\n";
Fariborz Jahanian8c664912007-11-13 19:21:13 +00002770
Fariborz Jahanian640a01f2007-10-18 19:23:00 +00002771}
Chris Lattner6fe8b272007-10-16 22:36:42 +00002772