blob: 43de31c516e090dd6053450d3099aded865bcdee [file] [log] [blame]
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001//===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Hacks and fun related to the code rewriter.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek305c6132012-09-01 05:09:24 +000014#include "clang/Rewrite/Frontend/ASTConsumers.h"
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000015#include "clang/AST/AST.h"
16#include "clang/AST/ASTConsumer.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000018#include "clang/AST/ParentMap.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000019#include "clang/Basic/CharInfo.h"
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000020#include "clang/Basic/Diagnostic.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000021#include "clang/Basic/IdentifierTable.h"
22#include "clang/Basic/SourceManager.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000023#include "clang/Basic/TargetInfo.h"
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000024#include "clang/Lex/Lexer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000025#include "clang/Rewrite/Core/Rewriter.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000026#include "llvm/ADT/DenseSet.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000027#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/StringExtras.h"
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000029#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/raw_ostream.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070031#include <memory>
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000032
33using namespace clang;
34using llvm::utostr;
35
36namespace {
37 class RewriteModernObjC : public ASTConsumer {
38 protected:
39
40 enum {
41 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
42 block, ... */
43 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
44 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the
45 __block variable */
46 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
47 helpers */
48 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
49 support routines */
50 BLOCK_BYREF_CURRENT_MAX = 256
51 };
52
53 enum {
54 BLOCK_NEEDS_FREE = (1 << 24),
55 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
56 BLOCK_HAS_CXX_OBJ = (1 << 26),
57 BLOCK_IS_GC = (1 << 27),
58 BLOCK_IS_GLOBAL = (1 << 28),
59 BLOCK_HAS_DESCRIPTOR = (1 << 29)
60 };
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000061
62 Rewriter Rewrite;
63 DiagnosticsEngine &Diags;
64 const LangOptions &LangOpts;
65 ASTContext *Context;
66 SourceManager *SM;
67 TranslationUnitDecl *TUDecl;
68 FileID MainFileID;
69 const char *MainFileStart, *MainFileEnd;
70 Stmt *CurrentBody;
71 ParentMap *PropParentMap; // created lazily.
72 std::string InFileName;
73 raw_ostream* OutFile;
74 std::string Preamble;
75
76 TypeDecl *ProtocolTypeDecl;
77 VarDecl *GlobalVarDecl;
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +000078 Expr *GlobalConstructionExp;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000079 unsigned RewriteFailedDiag;
Fariborz Jahaniand13c2c22012-03-22 19:54:39 +000080 unsigned GlobalBlockRewriteFailedDiag;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +000081 // ObjC string constant support.
82 unsigned NumObjCStringLiterals;
83 VarDecl *ConstantStringClassReference;
84 RecordDecl *NSStringRecord;
85
86 // ObjC foreach break/continue generation support.
87 int BcLabelCount;
88
89 unsigned TryFinallyContainsReturnDiag;
90 // Needed for super.
91 ObjCMethodDecl *CurMethodDef;
92 RecordDecl *SuperStructDecl;
93 RecordDecl *ConstantStringDecl;
94
95 FunctionDecl *MsgSendFunctionDecl;
96 FunctionDecl *MsgSendSuperFunctionDecl;
97 FunctionDecl *MsgSendStretFunctionDecl;
98 FunctionDecl *MsgSendSuperStretFunctionDecl;
99 FunctionDecl *MsgSendFpretFunctionDecl;
100 FunctionDecl *GetClassFunctionDecl;
101 FunctionDecl *GetMetaClassFunctionDecl;
102 FunctionDecl *GetSuperClassFunctionDecl;
103 FunctionDecl *SelGetUidFunctionDecl;
104 FunctionDecl *CFStringFunctionDecl;
Benjamin Kramere5753592013-09-09 14:48:42 +0000105 FunctionDecl *SuperConstructorFunctionDecl;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000106 FunctionDecl *CurFunctionDef;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000107
108 /* Misc. containers needed for meta-data rewrite. */
109 SmallVector<ObjCImplementationDecl *, 8> ClassImplementation;
110 SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation;
111 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs;
112 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols;
Fariborz Jahaniancf4c60f2012-02-17 22:20:12 +0000113 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCWrittenInterfaces;
Fariborz Jahanian8fba8942012-04-30 23:20:30 +0000114 llvm::SmallPtrSet<TagDecl*, 32> GlobalDefinedTags;
Fariborz Jahaniancf4c60f2012-02-17 22:20:12 +0000115 SmallVector<ObjCInterfaceDecl*, 32> ObjCInterfacesSeen;
Fariborz Jahanian88f7f752012-03-14 23:18:19 +0000116 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
117 SmallVector<ObjCInterfaceDecl*, 8> DefinedNonLazyClasses;
118
119 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000120 SmallVector<ObjCCategoryDecl *, 8> DefinedNonLazyCategories;
Fariborz Jahanian88f7f752012-03-14 23:18:19 +0000121
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000122 SmallVector<Stmt *, 32> Stmts;
123 SmallVector<int, 8> ObjCBcLabelNo;
124 // Remember all the @protocol(<expr>) expressions.
125 llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls;
126
127 llvm::DenseSet<uint64_t> CopyDestroyCache;
128
129 // Block expressions.
130 SmallVector<BlockExpr *, 32> Blocks;
131 SmallVector<int, 32> InnerDeclRefsCount;
John McCallf4b88a42012-03-10 09:33:50 +0000132 SmallVector<DeclRefExpr *, 32> InnerDeclRefs;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000133
John McCallf4b88a42012-03-10 09:33:50 +0000134 SmallVector<DeclRefExpr *, 32> BlockDeclRefs;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000135
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +0000136
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000137 // Block related declarations.
138 SmallVector<ValueDecl *, 8> BlockByCopyDecls;
139 llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet;
140 SmallVector<ValueDecl *, 8> BlockByRefDecls;
141 llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet;
142 llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo;
143 llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
144 llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls;
145
146 llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs;
Fariborz Jahanian72c88f12012-02-22 18:13:25 +0000147 llvm::DenseMap<ObjCInterfaceDecl *,
148 llvm::SmallPtrSet<ObjCIvarDecl *, 8> > ReferencedIvars;
149
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +0000150 // ivar bitfield grouping containers
151 llvm::DenseSet<const ObjCInterfaceDecl *> ObjCInterefaceHasBitfieldGroups;
152 llvm::DenseMap<const ObjCIvarDecl* , unsigned> IvarGroupNumber;
153 // This container maps an <class, group number for ivar> tuple to the type
154 // of the struct where the bitfield belongs.
155 llvm::DenseMap<std::pair<const ObjCInterfaceDecl*, unsigned>, QualType> GroupRecordType;
Fariborz Jahanian31c4a4b2013-02-07 22:50:40 +0000156 SmallVector<FunctionDecl*, 32> FunctionDefinitionsSeen;
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +0000157
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000158 // This maps an original source AST to it's rewritten form. This allows
159 // us to avoid rewriting the same node twice (which is very uncommon).
160 // This is needed to support some of the exotic property rewriting.
161 llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes;
162
163 // Needed for header files being rewritten
164 bool IsHeader;
165 bool SilenceRewriteMacroWarning;
Fariborz Jahanianada71912013-02-08 00:27:34 +0000166 bool GenerateLineInfo;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000167 bool objc_impl_method;
168
169 bool DisableReplaceStmt;
170 class DisableReplaceStmtScope {
171 RewriteModernObjC &R;
172 bool SavedValue;
173
174 public:
175 DisableReplaceStmtScope(RewriteModernObjC &R)
176 : R(R), SavedValue(R.DisableReplaceStmt) {
177 R.DisableReplaceStmt = true;
178 }
179 ~DisableReplaceStmtScope() {
180 R.DisableReplaceStmt = SavedValue;
181 }
182 };
183 void InitializeCommon(ASTContext &context);
184
185 public:
Fariborz Jahanian90af4e22012-02-14 17:19:02 +0000186 llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000187 // Top Level Driver code.
Stephen Hines651f13c2014-04-23 16:59:28 -0700188 bool HandleTopLevelDecl(DeclGroupRef D) override {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000189 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
190 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) {
191 if (!Class->isThisDeclarationADefinition()) {
192 RewriteForwardClassDecl(D);
193 break;
Fariborz Jahaniancf4c60f2012-02-17 22:20:12 +0000194 } else {
195 // Keep track of all interface declarations seen.
Fariborz Jahanianf3295272012-02-24 21:42:38 +0000196 ObjCInterfacesSeen.push_back(Class);
Fariborz Jahaniancf4c60f2012-02-17 22:20:12 +0000197 break;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000198 }
199 }
200
201 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) {
202 if (!Proto->isThisDeclarationADefinition()) {
203 RewriteForwardProtocolDecl(D);
204 break;
205 }
206 }
207
Fariborz Jahanian31c4a4b2013-02-07 22:50:40 +0000208 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*I)) {
209 // Under modern abi, we cannot translate body of the function
210 // yet until all class extensions and its implementation is seen.
211 // This is because they may introduce new bitfields which must go
212 // into their grouping struct.
213 if (FDecl->isThisDeclarationADefinition() &&
214 // Not c functions defined inside an objc container.
215 !FDecl->isTopLevelDeclInObjCContainer()) {
216 FunctionDefinitionsSeen.push_back(FDecl);
217 break;
218 }
219 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000220 HandleTopLevelSingleDecl(*I);
221 }
222 return true;
223 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700224
225 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
Fariborz Jahanian040cd822013-10-07 19:54:22 +0000226 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
227 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(*I)) {
228 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
229 RewriteBlockPointerDecl(TD);
230 else if (TD->getUnderlyingType()->isFunctionPointerType())
231 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
232 else
233 RewriteObjCQualifiedInterfaceTypes(TD);
234 }
235 }
236 return;
237 }
238
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000239 void HandleTopLevelSingleDecl(Decl *D);
240 void HandleDeclInMainFile(Decl *D);
241 RewriteModernObjC(std::string inFile, raw_ostream *OS,
242 DiagnosticsEngine &D, const LangOptions &LOpts,
Fariborz Jahanianada71912013-02-08 00:27:34 +0000243 bool silenceMacroWarn, bool LineInfo);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000244
245 ~RewriteModernObjC() {}
Stephen Hines651f13c2014-04-23 16:59:28 -0700246
247 void HandleTranslationUnit(ASTContext &C) override;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000248
249 void ReplaceStmt(Stmt *Old, Stmt *New) {
250 Stmt *ReplacingStmt = ReplacedNodes[Old];
251
252 if (ReplacingStmt)
253 return; // We can't rewrite the same node twice.
254
255 if (DisableReplaceStmt)
256 return;
257
258 // If replacement succeeded or warning disabled return with no warning.
259 if (!Rewrite.ReplaceStmt(Old, New)) {
260 ReplacedNodes[Old] = New;
261 return;
262 }
263 if (SilenceRewriteMacroWarning)
264 return;
265 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
266 << Old->getSourceRange();
267 }
268
269 void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700270 assert(Old != nullptr && New != nullptr && "Expected non-null Stmt's");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000271 if (DisableReplaceStmt)
272 return;
273
274 // Measure the old text.
275 int Size = Rewrite.getRangeSize(SrcRange);
276 if (Size == -1) {
277 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
278 << Old->getSourceRange();
279 return;
280 }
281 // Get the new text.
282 std::string SStr;
283 llvm::raw_string_ostream S(SStr);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700284 New->printPretty(S, nullptr, PrintingPolicy(LangOpts));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000285 const std::string &Str = S.str();
286
287 // If replacement succeeded or warning disabled return with no warning.
288 if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) {
289 ReplacedNodes[Old] = New;
290 return;
291 }
292 if (SilenceRewriteMacroWarning)
293 return;
294 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
295 << Old->getSourceRange();
296 }
297
298 void InsertText(SourceLocation Loc, StringRef Str,
299 bool InsertAfter = true) {
300 // If insertion succeeded or warning disabled return with no warning.
301 if (!Rewrite.InsertText(Loc, Str, InsertAfter) ||
302 SilenceRewriteMacroWarning)
303 return;
304
305 Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
306 }
307
308 void ReplaceText(SourceLocation Start, unsigned OrigLength,
309 StringRef Str) {
310 // If removal succeeded or warning disabled return with no warning.
311 if (!Rewrite.ReplaceText(Start, OrigLength, Str) ||
312 SilenceRewriteMacroWarning)
313 return;
314
315 Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
316 }
317
318 // Syntactic Rewriting.
319 void RewriteRecordBody(RecordDecl *RD);
320 void RewriteInclude();
Fariborz Jahanian96205962012-11-06 17:30:23 +0000321 void RewriteLineDirective(const Decl *D);
Fariborz Jahanianf616ae22012-11-06 23:25:49 +0000322 void ConvertSourceLocationToLineDirective(SourceLocation Loc,
323 std::string &LineString);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000324 void RewriteForwardClassDecl(DeclGroupRef D);
Craig Topper6b9240e2013-07-05 19:34:19 +0000325 void RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &DG);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000326 void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl,
327 const std::string &typedefString);
328 void RewriteImplementations();
329 void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
330 ObjCImplementationDecl *IMD,
331 ObjCCategoryImplDecl *CID);
332 void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
333 void RewriteImplementationDecl(Decl *Dcl);
334 void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
335 ObjCMethodDecl *MDecl, std::string &ResultStr);
336 void RewriteTypeIntoString(QualType T, std::string &ResultStr,
337 const FunctionType *&FPRetType);
338 void RewriteByRefString(std::string &ResultStr, const std::string &Name,
339 ValueDecl *VD, bool def=false);
340 void RewriteCategoryDecl(ObjCCategoryDecl *Dcl);
341 void RewriteProtocolDecl(ObjCProtocolDecl *Dcl);
342 void RewriteForwardProtocolDecl(DeclGroupRef D);
Craig Topper6b9240e2013-07-05 19:34:19 +0000343 void RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000344 void RewriteMethodDeclaration(ObjCMethodDecl *Method);
345 void RewriteProperty(ObjCPropertyDecl *prop);
346 void RewriteFunctionDecl(FunctionDecl *FD);
347 void RewriteBlockPointerType(std::string& Str, QualType Type);
348 void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD);
Fariborz Jahanianb75f8de2012-04-19 00:50:01 +0000349 void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000350 void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
351 void RewriteTypeOfDecl(VarDecl *VD);
352 void RewriteObjCQualifiedInterfaceTypes(Expr *E);
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +0000353
354 std::string getIvarAccessString(ObjCIvarDecl *D);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000355
356 // Expression Rewriting.
357 Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
358 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
359 Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo);
360 Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo);
361 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
362 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
363 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
Fariborz Jahanian55947042012-03-27 20:17:30 +0000364 Stmt *RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000365 Stmt *RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp);
Fariborz Jahanian86cff602012-03-30 23:35:47 +0000366 Stmt *RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp);
Fariborz Jahaniane35abe12012-04-06 22:29:36 +0000367 Stmt *RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000368 Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000369 Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
Fariborz Jahanian042b91d2012-05-23 23:47:20 +0000370 Stmt *RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000371 Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S);
372 Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S);
373 Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
374 SourceLocation OrigEnd);
375 Stmt *RewriteBreakStmt(BreakStmt *S);
376 Stmt *RewriteContinueStmt(ContinueStmt *S);
377 void RewriteCastExpr(CStyleCastExpr *CE);
Fariborz Jahanianf1ee6872012-04-10 00:08:18 +0000378 void RewriteImplicitCastObjCExpr(CastExpr *IE);
Fariborz Jahanianb3f904f2012-04-03 17:35:38 +0000379 void RewriteLinkageSpec(LinkageSpecDecl *LSD);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000380
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +0000381 // Computes ivar bitfield group no.
382 unsigned ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV);
383 // Names field decl. for ivar bitfield group.
384 void ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV, std::string &Result);
385 // Names struct type for ivar bitfield group.
386 void ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV, std::string &Result);
387 // Names symbol for ivar bitfield group field offset.
388 void ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV, std::string &Result);
389 // Given an ivar bitfield, it builds (or finds) its group record type.
390 QualType GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV);
391 QualType SynthesizeBitfieldGroupStructType(
392 ObjCIvarDecl *IV,
393 SmallVectorImpl<ObjCIvarDecl *> &IVars);
394
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000395 // Block rewriting.
396 void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D);
397
398 // Block specific rewrite rules.
399 void RewriteBlockPointerDecl(NamedDecl *VD);
Fariborz Jahanian4fe261c2012-04-24 19:38:45 +0000400 void RewriteByRefVar(VarDecl *VD, bool firstDecl, bool lastDecl);
John McCallf4b88a42012-03-10 09:33:50 +0000401 Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000402 Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE);
403 void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
404
405 void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl,
406 std::string &Result);
407
Fariborz Jahanian15f87772012-02-28 22:45:07 +0000408 void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result);
Fariborz Jahanianb68258f2012-05-01 17:46:45 +0000409 bool IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, TagDecl *Tag,
Fariborz Jahanian8fba8942012-04-30 23:20:30 +0000410 bool &IsNamedDefinition);
411 void RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl,
412 std::string &Result);
Fariborz Jahanian15f87772012-02-28 22:45:07 +0000413
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +0000414 bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result);
415
Fariborz Jahanian72c88f12012-02-22 18:13:25 +0000416 void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl,
417 std::string &Result);
Stephen Hines651f13c2014-04-23 16:59:28 -0700418
419 void Initialize(ASTContext &context) override;
420
Benjamin Kramer48d798c2012-06-02 10:20:41 +0000421 // Misc. AST transformation routines. Sometimes they end up calling
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000422 // rewriting routines on the new ASTs.
423 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
424 Expr **args, unsigned nargs,
425 SourceLocation StartLoc=SourceLocation(),
426 SourceLocation EndLoc=SourceLocation());
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +0000427
428 Expr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor,
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +0000429 QualType returnType,
430 SmallVectorImpl<QualType> &ArgTypes,
431 SmallVectorImpl<Expr*> &MsgExprs,
432 ObjCMethodDecl *Method);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000433
434 Stmt *SynthMessageExpr(ObjCMessageExpr *Exp,
435 SourceLocation StartLoc=SourceLocation(),
436 SourceLocation EndLoc=SourceLocation());
437
438 void SynthCountByEnumWithState(std::string &buf);
439 void SynthMsgSendFunctionDecl();
440 void SynthMsgSendSuperFunctionDecl();
441 void SynthMsgSendStretFunctionDecl();
442 void SynthMsgSendFpretFunctionDecl();
443 void SynthMsgSendSuperStretFunctionDecl();
444 void SynthGetClassFunctionDecl();
445 void SynthGetMetaClassFunctionDecl();
446 void SynthGetSuperClassFunctionDecl();
447 void SynthSelGetUidFunctionDecl();
Benjamin Kramere5753592013-09-09 14:48:42 +0000448 void SynthSuperConstructorFunctionDecl();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000449
450 // Rewriting metadata
451 template<typename MethodIterator>
452 void RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
453 MethodIterator MethodEnd,
454 bool IsInstanceMethod,
455 StringRef prefix,
456 StringRef ClassName,
457 std::string &Result);
Fariborz Jahanianda9624a2012-02-08 19:53:58 +0000458 void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol,
459 std::string &Result);
Fariborz Jahanianb4ee8802012-04-30 16:57:52 +0000460 void RewriteObjCProtocolListMetaData(
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000461 const ObjCList<ObjCProtocolDecl> &Prots,
462 StringRef prefix, StringRef ClassName, std::string &Result);
Fariborz Jahanianb4ee8802012-04-30 16:57:52 +0000463 void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000464 std::string &Result);
Fariborz Jahanianb4ee8802012-04-30 16:57:52 +0000465 void RewriteClassSetupInitHook(std::string &Result);
Fariborz Jahaniane0335782012-03-27 18:41:05 +0000466
Fariborz Jahanianb4ee8802012-04-30 16:57:52 +0000467 void RewriteMetaDataIntoBuffer(std::string &Result);
468 void WriteImageInfo(std::string &Result);
469 void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000470 std::string &Result);
Fariborz Jahanianb4ee8802012-04-30 16:57:52 +0000471 void RewriteCategorySetupInitHook(std::string &Result);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000472
473 // Rewriting ivar
Fariborz Jahanianb4ee8802012-04-30 16:57:52 +0000474 void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000475 std::string &Result);
Fariborz Jahanianb4ee8802012-04-30 16:57:52 +0000476 Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000477
478
479 std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag);
480 std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
481 StringRef funcName, std::string Tag);
482 std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
483 StringRef funcName, std::string Tag);
484 std::string SynthesizeBlockImpl(BlockExpr *CE,
485 std::string Tag, std::string Desc);
486 std::string SynthesizeBlockDescriptor(std::string DescTag,
487 std::string ImplTag,
488 int i, StringRef funcName,
489 unsigned hasCopy);
490 Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp);
491 void SynthesizeBlockLiterals(SourceLocation FunLocStart,
492 StringRef FunName);
493 FunctionDecl *SynthBlockInitFunctionDecl(StringRef name);
494 Stmt *SynthBlockInitExpr(BlockExpr *Exp,
Craig Topper6b9240e2013-07-05 19:34:19 +0000495 const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000496
497 // Misc. helper routines.
498 QualType getProtocolType();
499 void WarnAboutReturnGotoStmts(Stmt *S);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000500 void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND);
501 void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
502 void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
503
504 bool IsDeclStmtInForeachHeader(DeclStmt *DS);
505 void CollectBlockDeclRefInfo(BlockExpr *Exp);
506 void GetBlockDeclRefExprs(Stmt *S);
Craig Topper6b9240e2013-07-05 19:34:19 +0000507 void GetInnerBlockDeclRefExprs(Stmt *S,
508 SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000509 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts);
510
511 // We avoid calling Type::isBlockPointerType(), since it operates on the
512 // canonical type. We only care if the top-level type is a closure pointer.
513 bool isTopLevelBlockPointerType(QualType T) {
514 return isa<BlockPointerType>(T);
515 }
516
517 /// convertBlockPointerToFunctionPointer - Converts a block-pointer type
518 /// to a function pointer type and upon success, returns true; false
519 /// otherwise.
520 bool convertBlockPointerToFunctionPointer(QualType &T) {
521 if (isTopLevelBlockPointerType(T)) {
522 const BlockPointerType *BPT = T->getAs<BlockPointerType>();
523 T = Context->getPointerType(BPT->getPointeeType());
524 return true;
525 }
526 return false;
527 }
528
Fariborz Jahanian164d6f82012-02-13 18:57:49 +0000529 bool convertObjCTypeToCStyleType(QualType &T);
530
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000531 bool needToScanForQualifiers(QualType T);
532 QualType getSuperStructType();
533 QualType getConstantStringStructType();
534 QualType convertFunctionTypeOfBlocks(const FunctionType *FT);
535 bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf);
536
537 void convertToUnqualifiedObjCType(QualType &T) {
Fariborz Jahaniane35abe12012-04-06 22:29:36 +0000538 if (T->isObjCQualifiedIdType()) {
539 bool isConst = T.isConstQualified();
540 T = isConst ? Context->getObjCIdType().withConst()
541 : Context->getObjCIdType();
542 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000543 else if (T->isObjCQualifiedClassType())
544 T = Context->getObjCClassType();
545 else if (T->isObjCObjectPointerType() &&
546 T->getPointeeType()->isObjCQualifiedInterfaceType()) {
547 if (const ObjCObjectPointerType * OBJPT =
548 T->getAsObjCInterfacePointerType()) {
549 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
550 T = QualType(IFaceT, 0);
551 T = Context->getPointerType(T);
552 }
553 }
554 }
555
556 // FIXME: This predicate seems like it would be useful to add to ASTContext.
557 bool isObjCType(QualType T) {
558 if (!LangOpts.ObjC1 && !LangOpts.ObjC2)
559 return false;
560
561 QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
562
563 if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
564 OCT == Context->getCanonicalType(Context->getObjCClassType()))
565 return true;
566
567 if (const PointerType *PT = OCT->getAs<PointerType>()) {
568 if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
569 PT->getPointeeType()->isObjCQualifiedIdType())
570 return true;
571 }
572 return false;
573 }
574 bool PointerTypeTakesAnyBlockArguments(QualType QT);
575 bool PointerTypeTakesAnyObjCQualifiedType(QualType QT);
576 void GetExtentOfArgList(const char *Name, const char *&LParen,
577 const char *&RParen);
578
579 void QuoteDoublequotes(std::string &From, std::string &To) {
580 for (unsigned i = 0; i < From.length(); i++) {
581 if (From[i] == '"')
582 To += "\\\"";
583 else
584 To += From[i];
585 }
586 }
587
588 QualType getSimpleFunctionType(QualType result,
Jordan Rosebea522f2013-03-08 21:51:21 +0000589 ArrayRef<QualType> args,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000590 bool variadic = false) {
591 if (result == Context->getObjCInstanceType())
592 result = Context->getObjCIdType();
593 FunctionProtoType::ExtProtoInfo fpi;
594 fpi.Variadic = variadic;
Jordan Rosebea522f2013-03-08 21:51:21 +0000595 return Context->getFunctionType(result, args, fpi);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000596 }
597
598 // Helper function: create a CStyleCastExpr with trivial type source info.
599 CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty,
600 CastKind Kind, Expr *E) {
601 TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700602 return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, nullptr,
603 TInfo, SourceLocation(), SourceLocation());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000604 }
Fariborz Jahanian88f7f752012-03-14 23:18:19 +0000605
606 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
607 IdentifierInfo* II = &Context->Idents.get("load");
608 Selector LoadSel = Context->Selectors.getSelector(0, &II);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700609 return OD->getClassMethod(LoadSel) != nullptr;
Fariborz Jahanian88f7f752012-03-14 23:18:19 +0000610 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700611
612 StringLiteral *getStringLiteral(StringRef Str) {
613 QualType StrType = Context->getConstantArrayType(
614 Context->CharTy, llvm::APInt(32, Str.size() + 1), ArrayType::Normal,
615 0);
616 return StringLiteral::Create(*Context, Str, StringLiteral::Ascii,
617 /*Pascal=*/false, StrType, SourceLocation());
618 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000619 };
620
621}
622
623void RewriteModernObjC::RewriteBlocksInFunctionProtoType(QualType funcType,
624 NamedDecl *D) {
625 if (const FunctionProtoType *fproto
626 = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700627 for (const auto &I : fproto->param_types())
628 if (isTopLevelBlockPointerType(I)) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000629 // All the args are checked/rewritten. Don't call twice!
630 RewriteBlockPointerDecl(D);
631 break;
632 }
633 }
634}
635
636void RewriteModernObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) {
637 const PointerType *PT = funcType->getAs<PointerType>();
638 if (PT && PointerTypeTakesAnyBlockArguments(funcType))
639 RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND);
640}
641
642static bool IsHeaderFile(const std::string &Filename) {
643 std::string::size_type DotPos = Filename.rfind('.');
644
645 if (DotPos == std::string::npos) {
646 // no file extension
647 return false;
648 }
649
650 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
651 // C header: .h
652 // C++ header: .hh or .H;
653 return Ext == "h" || Ext == "hh" || Ext == "H";
654}
655
656RewriteModernObjC::RewriteModernObjC(std::string inFile, raw_ostream* OS,
657 DiagnosticsEngine &D, const LangOptions &LOpts,
Fariborz Jahanianada71912013-02-08 00:27:34 +0000658 bool silenceMacroWarn,
659 bool LineInfo)
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000660 : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS),
Fariborz Jahanianada71912013-02-08 00:27:34 +0000661 SilenceRewriteMacroWarning(silenceMacroWarn), GenerateLineInfo(LineInfo) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000662 IsHeader = IsHeaderFile(inFile);
663 RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
664 "rewriting sub-expression within a macro (may not be correct)");
Fariborz Jahaniand13c2c22012-03-22 19:54:39 +0000665 // FIXME. This should be an error. But if block is not called, it is OK. And it
666 // may break including some headers.
667 GlobalBlockRewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
668 "rewriting block literal declared in global scope is not implemented");
669
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000670 TryFinallyContainsReturnDiag = Diags.getCustomDiagID(
671 DiagnosticsEngine::Warning,
672 "rewriter doesn't support user-specified control flow semantics "
673 "for @try/@finally (code may not execute properly)");
674}
675
676ASTConsumer *clang::CreateModernObjCRewriter(const std::string& InFile,
677 raw_ostream* OS,
678 DiagnosticsEngine &Diags,
679 const LangOptions &LOpts,
Fariborz Jahanianada71912013-02-08 00:27:34 +0000680 bool SilenceRewriteMacroWarning,
681 bool LineInfo) {
682 return new RewriteModernObjC(InFile, OS, Diags, LOpts,
683 SilenceRewriteMacroWarning, LineInfo);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000684}
685
686void RewriteModernObjC::InitializeCommon(ASTContext &context) {
687 Context = &context;
688 SM = &Context->getSourceManager();
689 TUDecl = Context->getTranslationUnitDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700690 MsgSendFunctionDecl = nullptr;
691 MsgSendSuperFunctionDecl = nullptr;
692 MsgSendStretFunctionDecl = nullptr;
693 MsgSendSuperStretFunctionDecl = nullptr;
694 MsgSendFpretFunctionDecl = nullptr;
695 GetClassFunctionDecl = nullptr;
696 GetMetaClassFunctionDecl = nullptr;
697 GetSuperClassFunctionDecl = nullptr;
698 SelGetUidFunctionDecl = nullptr;
699 CFStringFunctionDecl = nullptr;
700 ConstantStringClassReference = nullptr;
701 NSStringRecord = nullptr;
702 CurMethodDef = nullptr;
703 CurFunctionDef = nullptr;
704 GlobalVarDecl = nullptr;
705 GlobalConstructionExp = nullptr;
706 SuperStructDecl = nullptr;
707 ProtocolTypeDecl = nullptr;
708 ConstantStringDecl = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000709 BcLabelCount = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700710 SuperConstructorFunctionDecl = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000711 NumObjCStringLiterals = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700712 PropParentMap = nullptr;
713 CurrentBody = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000714 DisableReplaceStmt = false;
715 objc_impl_method = false;
716
717 // Get the ID and start/end of the main file.
718 MainFileID = SM->getMainFileID();
719 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
720 MainFileStart = MainBuf->getBufferStart();
721 MainFileEnd = MainBuf->getBufferEnd();
722
David Blaikie4e4d0842012-03-11 07:00:24 +0000723 Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000724}
725
726//===----------------------------------------------------------------------===//
727// Top Level Driver Code
728//===----------------------------------------------------------------------===//
729
730void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) {
731 if (Diags.hasErrorOccurred())
732 return;
733
734 // Two cases: either the decl could be in the main file, or it could be in a
735 // #included file. If the former, rewrite it now. If the later, check to see
736 // if we rewrote the #include/#import.
737 SourceLocation Loc = D->getLocation();
738 Loc = SM->getExpansionLoc(Loc);
739
740 // If this is for a builtin, ignore it.
741 if (Loc.isInvalid()) return;
742
743 // Look for built-in declarations that we need to refer during the rewrite.
744 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
745 RewriteFunctionDecl(FD);
746 } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) {
747 // declared in <Foundation/NSString.h>
748 if (FVD->getName() == "_NSConstantStringClassReference") {
749 ConstantStringClassReference = FVD;
750 return;
751 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000752 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
753 RewriteCategoryDecl(CD);
754 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
755 if (PD->isThisDeclarationADefinition())
756 RewriteProtocolDecl(PD);
757 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
Fariborz Jahanian8e86b2d2012-04-04 17:16:15 +0000758 // FIXME. This will not work in all situations and leaving it out
759 // is harmless.
760 // RewriteLinkageSpec(LSD);
761
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000762 // Recurse into linkage specifications
763 for (DeclContext::decl_iterator DI = LSD->decls_begin(),
764 DIEnd = LSD->decls_end();
765 DI != DIEnd; ) {
766 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) {
767 if (!IFace->isThisDeclarationADefinition()) {
768 SmallVector<Decl *, 8> DG;
769 SourceLocation StartLoc = IFace->getLocStart();
770 do {
771 if (isa<ObjCInterfaceDecl>(*DI) &&
772 !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() &&
773 StartLoc == (*DI)->getLocStart())
774 DG.push_back(*DI);
775 else
776 break;
777
778 ++DI;
779 } while (DI != DIEnd);
780 RewriteForwardClassDecl(DG);
781 continue;
782 }
Fariborz Jahanianb3f904f2012-04-03 17:35:38 +0000783 else {
784 // Keep track of all interface declarations seen.
785 ObjCInterfacesSeen.push_back(IFace);
786 ++DI;
787 continue;
788 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000789 }
790
791 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) {
792 if (!Proto->isThisDeclarationADefinition()) {
793 SmallVector<Decl *, 8> DG;
794 SourceLocation StartLoc = Proto->getLocStart();
795 do {
796 if (isa<ObjCProtocolDecl>(*DI) &&
797 !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() &&
798 StartLoc == (*DI)->getLocStart())
799 DG.push_back(*DI);
800 else
801 break;
802
803 ++DI;
804 } while (DI != DIEnd);
805 RewriteForwardProtocolDecl(DG);
806 continue;
807 }
808 }
809
810 HandleTopLevelSingleDecl(*DI);
811 ++DI;
812 }
813 }
814 // If we have a decl in the main file, see if we should rewrite it.
Eli Friedman24146972013-08-22 00:27:10 +0000815 if (SM->isWrittenInMainFile(Loc))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000816 return HandleDeclInMainFile(D);
817}
818
819//===----------------------------------------------------------------------===//
820// Syntactic (non-AST) Rewriting Code
821//===----------------------------------------------------------------------===//
822
823void RewriteModernObjC::RewriteInclude() {
824 SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID);
825 StringRef MainBuf = SM->getBufferData(MainFileID);
826 const char *MainBufStart = MainBuf.begin();
827 const char *MainBufEnd = MainBuf.end();
828 size_t ImportLen = strlen("import");
829
830 // Loop over the whole file, looking for includes.
831 for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) {
832 if (*BufPtr == '#') {
833 if (++BufPtr == MainBufEnd)
834 return;
835 while (*BufPtr == ' ' || *BufPtr == '\t')
836 if (++BufPtr == MainBufEnd)
837 return;
838 if (!strncmp(BufPtr, "import", ImportLen)) {
839 // replace import with include
840 SourceLocation ImportLoc =
841 LocStart.getLocWithOffset(BufPtr-MainBufStart);
842 ReplaceText(ImportLoc, ImportLen, "include");
843 BufPtr += ImportLen;
844 }
845 }
846 }
847}
848
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +0000849static void WriteInternalIvarName(const ObjCInterfaceDecl *IDecl,
850 ObjCIvarDecl *IvarDecl, std::string &Result) {
851 Result += "OBJC_IVAR_$_";
852 Result += IDecl->getName();
853 Result += "$";
854 Result += IvarDecl->getName();
855}
856
857std::string
858RewriteModernObjC::getIvarAccessString(ObjCIvarDecl *D) {
859 const ObjCInterfaceDecl *ClassDecl = D->getContainingInterface();
860
861 // Build name of symbol holding ivar offset.
862 std::string IvarOffsetName;
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +0000863 if (D->isBitField())
864 ObjCIvarBitfieldGroupOffset(D, IvarOffsetName);
865 else
866 WriteInternalIvarName(ClassDecl, D, IvarOffsetName);
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +0000867
868
869 std::string S = "(*(";
870 QualType IvarT = D->getType();
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +0000871 if (D->isBitField())
872 IvarT = GetGroupRecordTypeForObjCIvarBitfield(D);
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +0000873
874 if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) {
875 RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl();
876 RD = RD->getDefinition();
877 if (RD && !RD->getDeclName().getAsIdentifierInfo()) {
878 // decltype(((Foo_IMPL*)0)->bar) *
879 ObjCContainerDecl *CDecl =
880 dyn_cast<ObjCContainerDecl>(D->getDeclContext());
881 // ivar in class extensions requires special treatment.
882 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
883 CDecl = CatDecl->getClassInterface();
884 std::string RecName = CDecl->getName();
885 RecName += "_IMPL";
886 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
887 SourceLocation(), SourceLocation(),
888 &Context->Idents.get(RecName.c_str()));
889 QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD));
890 unsigned UnsignedIntSize =
891 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
892 Expr *Zero = IntegerLiteral::Create(*Context,
893 llvm::APInt(UnsignedIntSize, 0),
894 Context->UnsignedIntTy, SourceLocation());
895 Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero);
896 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
897 Zero);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700898 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +0000899 SourceLocation(),
900 &Context->Idents.get(D->getNameAsString()),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700901 IvarT, nullptr,
902 /*BitWidth=*/nullptr, /*Mutable=*/true,
Richard Smithca523302012-06-10 03:12:00 +0000903 ICIS_NoInit);
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +0000904 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
905 FD->getType(), VK_LValue,
906 OK_Ordinary);
907 IvarT = Context->getDecltypeType(ME, ME->getType());
908 }
909 }
910 convertObjCTypeToCStyleType(IvarT);
911 QualType castT = Context->getPointerType(IvarT);
912 std::string TypeString(castT.getAsString(Context->getPrintingPolicy()));
913 S += TypeString;
914 S += ")";
915
916 // ((char *)self + IVAR_OFFSET_SYMBOL_NAME)
917 S += "((char *)self + ";
918 S += IvarOffsetName;
919 S += "))";
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +0000920 if (D->isBitField()) {
921 S += ".";
922 S += D->getNameAsString();
923 }
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +0000924 ReferencedIvars[const_cast<ObjCInterfaceDecl *>(ClassDecl)].insert(D);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000925 return S;
926}
927
Fariborz Jahanian301e2e42012-05-03 22:52:13 +0000928/// mustSynthesizeSetterGetterMethod - returns true if setter or getter has not
929/// been found in the class implementation. In this case, it must be synthesized.
930static bool mustSynthesizeSetterGetterMethod(ObjCImplementationDecl *IMP,
931 ObjCPropertyDecl *PD,
932 bool getter) {
933 return getter ? !IMP->getInstanceMethod(PD->getGetterName())
934 : !IMP->getInstanceMethod(PD->getSetterName());
935
936}
937
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000938void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
939 ObjCImplementationDecl *IMD,
940 ObjCCategoryImplDecl *CID) {
941 static bool objcGetPropertyDefined = false;
942 static bool objcSetPropertyDefined = false;
Fariborz Jahanian301e2e42012-05-03 22:52:13 +0000943 SourceLocation startGetterSetterLoc;
944
945 if (PID->getLocStart().isValid()) {
946 SourceLocation startLoc = PID->getLocStart();
947 InsertText(startLoc, "// ");
948 const char *startBuf = SM->getCharacterData(startLoc);
949 assert((*startBuf == '@') && "bogus @synthesize location");
950 const char *semiBuf = strchr(startBuf, ';');
951 assert((*semiBuf == ';') && "@synthesize: can't find ';'");
952 startGetterSetterLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
953 }
954 else
955 startGetterSetterLoc = IMD ? IMD->getLocEnd() : CID->getLocEnd();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000956
957 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
958 return; // FIXME: is this correct?
959
960 // Generate the 'getter' function.
961 ObjCPropertyDecl *PD = PID->getPropertyDecl();
962 ObjCIvarDecl *OID = PID->getPropertyIvarDecl();
Jordan Rose62bbe072013-03-15 21:41:35 +0000963 assert(IMD && OID && "Synthesized ivars must be attached to @implementation");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000964
Bill Wendlingad017fa2012-12-20 19:22:21 +0000965 unsigned Attributes = PD->getPropertyAttributes();
Fariborz Jahanian301e2e42012-05-03 22:52:13 +0000966 if (mustSynthesizeSetterGetterMethod(IMD, PD, true /*getter*/)) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000967 bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
968 (Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000969 ObjCPropertyDecl::OBJC_PR_copy));
970 std::string Getr;
971 if (GenGetProperty && !objcGetPropertyDefined) {
972 objcGetPropertyDefined = true;
973 // FIXME. Is this attribute correct in all cases?
974 Getr = "\nextern \"C\" __declspec(dllimport) "
975 "id objc_getProperty(id, SEL, long, bool);\n";
976 }
977 RewriteObjCMethodDecl(OID->getContainingInterface(),
978 PD->getGetterMethodDecl(), Getr);
979 Getr += "{ ";
980 // Synthesize an explicit cast to gain access to the ivar.
981 // See objc-act.c:objc_synthesize_new_getter() for details.
982 if (GenGetProperty) {
983 // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1)
984 Getr += "typedef ";
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700985 const FunctionType *FPRetType = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700986 RewriteTypeIntoString(PD->getGetterMethodDecl()->getReturnType(), Getr,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000987 FPRetType);
988 Getr += " _TYPE";
989 if (FPRetType) {
990 Getr += ")"; // close the precedence "scope" for "*".
991
992 // Now, emit the argument types (if any).
993 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){
994 Getr += "(";
Stephen Hines651f13c2014-04-23 16:59:28 -0700995 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000996 if (i) Getr += ", ";
Stephen Hines651f13c2014-04-23 16:59:28 -0700997 std::string ParamStr =
998 FT->getParamType(i).getAsString(Context->getPrintingPolicy());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000999 Getr += ParamStr;
1000 }
1001 if (FT->isVariadic()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001002 if (FT->getNumParams())
1003 Getr += ", ";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001004 Getr += "...";
1005 }
1006 Getr += ")";
1007 } else
1008 Getr += "()";
1009 }
1010 Getr += ";\n";
1011 Getr += "return (_TYPE)";
1012 Getr += "objc_getProperty(self, _cmd, ";
1013 RewriteIvarOffsetComputation(OID, Getr);
1014 Getr += ", 1)";
1015 }
1016 else
1017 Getr += "return " + getIvarAccessString(OID);
1018 Getr += "; }";
Fariborz Jahanian301e2e42012-05-03 22:52:13 +00001019 InsertText(startGetterSetterLoc, Getr);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001020 }
1021
Fariborz Jahanian301e2e42012-05-03 22:52:13 +00001022 if (PD->isReadOnly() ||
1023 !mustSynthesizeSetterGetterMethod(IMD, PD, false /*setter*/))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001024 return;
1025
1026 // Generate the 'setter' function.
1027 std::string Setr;
Bill Wendlingad017fa2012-12-20 19:22:21 +00001028 bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001029 ObjCPropertyDecl::OBJC_PR_copy);
1030 if (GenSetProperty && !objcSetPropertyDefined) {
1031 objcSetPropertyDefined = true;
1032 // FIXME. Is this attribute correct in all cases?
1033 Setr = "\nextern \"C\" __declspec(dllimport) "
1034 "void objc_setProperty (id, SEL, long, id, bool, bool);\n";
1035 }
1036
1037 RewriteObjCMethodDecl(OID->getContainingInterface(),
1038 PD->getSetterMethodDecl(), Setr);
1039 Setr += "{ ";
1040 // Synthesize an explicit cast to initialize the ivar.
1041 // See objc-act.c:objc_synthesize_new_setter() for details.
1042 if (GenSetProperty) {
1043 Setr += "objc_setProperty (self, _cmd, ";
1044 RewriteIvarOffsetComputation(OID, Setr);
1045 Setr += ", (id)";
1046 Setr += PD->getName();
1047 Setr += ", ";
Bill Wendlingad017fa2012-12-20 19:22:21 +00001048 if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001049 Setr += "0, ";
1050 else
1051 Setr += "1, ";
Bill Wendlingad017fa2012-12-20 19:22:21 +00001052 if (Attributes & ObjCPropertyDecl::OBJC_PR_copy)
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001053 Setr += "1)";
1054 else
1055 Setr += "0)";
1056 }
1057 else {
1058 Setr += getIvarAccessString(OID) + " = ";
1059 Setr += PD->getName();
1060 }
Fariborz Jahanian301e2e42012-05-03 22:52:13 +00001061 Setr += "; }\n";
1062 InsertText(startGetterSetterLoc, Setr);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001063}
1064
1065static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl,
1066 std::string &typedefString) {
Fariborz Jahanianbfaa1112013-02-08 17:15:07 +00001067 typedefString += "\n#ifndef _REWRITER_typedef_";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001068 typedefString += ForwardDecl->getNameAsString();
1069 typedefString += "\n";
1070 typedefString += "#define _REWRITER_typedef_";
1071 typedefString += ForwardDecl->getNameAsString();
1072 typedefString += "\n";
1073 typedefString += "typedef struct objc_object ";
1074 typedefString += ForwardDecl->getNameAsString();
Fariborz Jahanianc38503b2012-03-12 23:58:28 +00001075 // typedef struct { } _objc_exc_Classname;
1076 typedefString += ";\ntypedef struct {} _objc_exc_";
1077 typedefString += ForwardDecl->getNameAsString();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001078 typedefString += ";\n#endif\n";
1079}
1080
1081void RewriteModernObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl,
1082 const std::string &typedefString) {
1083 SourceLocation startLoc = ClassDecl->getLocStart();
1084 const char *startBuf = SM->getCharacterData(startLoc);
1085 const char *semiPtr = strchr(startBuf, ';');
1086 // Replace the @class with typedefs corresponding to the classes.
1087 ReplaceText(startLoc, semiPtr-startBuf+1, typedefString);
1088}
1089
1090void RewriteModernObjC::RewriteForwardClassDecl(DeclGroupRef D) {
1091 std::string typedefString;
1092 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
Fariborz Jahanian2330df42013-09-24 17:03:07 +00001093 if (ObjCInterfaceDecl *ForwardDecl = dyn_cast<ObjCInterfaceDecl>(*I)) {
1094 if (I == D.begin()) {
1095 // Translate to typedef's that forward reference structs with the same name
1096 // as the class. As a convenience, we include the original declaration
1097 // as a comment.
1098 typedefString += "// @class ";
1099 typedefString += ForwardDecl->getNameAsString();
1100 typedefString += ";";
1101 }
1102 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001103 }
Fariborz Jahanian2330df42013-09-24 17:03:07 +00001104 else
1105 HandleTopLevelSingleDecl(*I);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001106 }
1107 DeclGroupRef::iterator I = D.begin();
1108 RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString);
1109}
1110
1111void RewriteModernObjC::RewriteForwardClassDecl(
Craig Topper6b9240e2013-07-05 19:34:19 +00001112 const SmallVectorImpl<Decl *> &D) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001113 std::string typedefString;
1114 for (unsigned i = 0; i < D.size(); i++) {
1115 ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]);
1116 if (i == 0) {
1117 typedefString += "// @class ";
1118 typedefString += ForwardDecl->getNameAsString();
Fariborz Jahanianbfaa1112013-02-08 17:15:07 +00001119 typedefString += ";";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001120 }
1121 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
1122 }
1123 RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString);
1124}
1125
1126void RewriteModernObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
1127 // When method is a synthesized one, such as a getter/setter there is
1128 // nothing to rewrite.
1129 if (Method->isImplicit())
1130 return;
1131 SourceLocation LocStart = Method->getLocStart();
1132 SourceLocation LocEnd = Method->getLocEnd();
1133
1134 if (SM->getExpansionLineNumber(LocEnd) >
1135 SM->getExpansionLineNumber(LocStart)) {
1136 InsertText(LocStart, "#if 0\n");
1137 ReplaceText(LocEnd, 1, ";\n#endif\n");
1138 } else {
1139 InsertText(LocStart, "// ");
1140 }
1141}
1142
1143void RewriteModernObjC::RewriteProperty(ObjCPropertyDecl *prop) {
1144 SourceLocation Loc = prop->getAtLoc();
1145
1146 ReplaceText(Loc, 0, "// ");
1147 // FIXME: handle properties that are declared across multiple lines.
1148}
1149
1150void RewriteModernObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
1151 SourceLocation LocStart = CatDecl->getLocStart();
1152
1153 // FIXME: handle category headers that are declared across multiple lines.
Fariborz Jahanianb68258f2012-05-01 17:46:45 +00001154 if (CatDecl->getIvarRBraceLoc().isValid()) {
1155 ReplaceText(LocStart, 1, "/** ");
1156 ReplaceText(CatDecl->getIvarRBraceLoc(), 1, "**/ ");
1157 }
1158 else {
Fariborz Jahaniand2aea122012-02-19 19:00:05 +00001159 ReplaceText(LocStart, 0, "// ");
Fariborz Jahanianb68258f2012-05-01 17:46:45 +00001160 }
Fariborz Jahanianaf300292012-02-20 20:09:20 +00001161
Stephen Hines651f13c2014-04-23 16:59:28 -07001162 for (auto *I : CatDecl->properties())
1163 RewriteProperty(I);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001164
Stephen Hines651f13c2014-04-23 16:59:28 -07001165 for (auto *I : CatDecl->instance_methods())
1166 RewriteMethodDeclaration(I);
1167 for (auto *I : CatDecl->class_methods())
1168 RewriteMethodDeclaration(I);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001169
1170 // Lastly, comment out the @end.
1171 ReplaceText(CatDecl->getAtEndRange().getBegin(),
Fariborz Jahanianf9f30792013-04-03 19:11:21 +00001172 strlen("@end"), "/* @end */\n");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001173}
1174
1175void RewriteModernObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
1176 SourceLocation LocStart = PDecl->getLocStart();
1177 assert(PDecl->isThisDeclarationADefinition());
1178
1179 // FIXME: handle protocol headers that are declared across multiple lines.
1180 ReplaceText(LocStart, 0, "// ");
1181
Stephen Hines651f13c2014-04-23 16:59:28 -07001182 for (auto *I : PDecl->instance_methods())
1183 RewriteMethodDeclaration(I);
1184 for (auto *I : PDecl->class_methods())
1185 RewriteMethodDeclaration(I);
1186 for (auto *I : PDecl->properties())
1187 RewriteProperty(I);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001188
1189 // Lastly, comment out the @end.
1190 SourceLocation LocEnd = PDecl->getAtEndRange().getBegin();
Fariborz Jahanianf9f30792013-04-03 19:11:21 +00001191 ReplaceText(LocEnd, strlen("@end"), "/* @end */\n");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001192
1193 // Must comment out @optional/@required
1194 const char *startBuf = SM->getCharacterData(LocStart);
1195 const char *endBuf = SM->getCharacterData(LocEnd);
1196 for (const char *p = startBuf; p < endBuf; p++) {
1197 if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
1198 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1199 ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */");
1200
1201 }
1202 else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
1203 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1204 ReplaceText(OptionalLoc, strlen("@required"), "/* @required */");
1205
1206 }
1207 }
1208}
1209
1210void RewriteModernObjC::RewriteForwardProtocolDecl(DeclGroupRef D) {
1211 SourceLocation LocStart = (*D.begin())->getLocStart();
1212 if (LocStart.isInvalid())
1213 llvm_unreachable("Invalid SourceLocation");
1214 // FIXME: handle forward protocol that are declared across multiple lines.
1215 ReplaceText(LocStart, 0, "// ");
1216}
1217
1218void
Craig Topper6b9240e2013-07-05 19:34:19 +00001219RewriteModernObjC::RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001220 SourceLocation LocStart = DG[0]->getLocStart();
1221 if (LocStart.isInvalid())
1222 llvm_unreachable("Invalid SourceLocation");
1223 // FIXME: handle forward protocol that are declared across multiple lines.
1224 ReplaceText(LocStart, 0, "// ");
1225}
1226
Fariborz Jahanianb3f904f2012-04-03 17:35:38 +00001227void
1228RewriteModernObjC::RewriteLinkageSpec(LinkageSpecDecl *LSD) {
1229 SourceLocation LocStart = LSD->getExternLoc();
1230 if (LocStart.isInvalid())
1231 llvm_unreachable("Invalid extern SourceLocation");
1232
1233 ReplaceText(LocStart, 0, "// ");
1234 if (!LSD->hasBraces())
1235 return;
1236 // FIXME. We don't rewrite well if '{' is not on same line as 'extern'.
1237 SourceLocation LocRBrace = LSD->getRBraceLoc();
1238 if (LocRBrace.isInvalid())
1239 llvm_unreachable("Invalid rbrace SourceLocation");
1240 ReplaceText(LocRBrace, 0, "// ");
1241}
1242
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001243void RewriteModernObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr,
1244 const FunctionType *&FPRetType) {
1245 if (T->isObjCQualifiedIdType())
1246 ResultStr += "id";
1247 else if (T->isFunctionPointerType() ||
1248 T->isBlockPointerType()) {
1249 // needs special handling, since pointer-to-functions have special
1250 // syntax (where a decaration models use).
1251 QualType retType = T;
1252 QualType PointeeTy;
1253 if (const PointerType* PT = retType->getAs<PointerType>())
1254 PointeeTy = PT->getPointeeType();
1255 else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>())
1256 PointeeTy = BPT->getPointeeType();
1257 if ((FPRetType = PointeeTy->getAs<FunctionType>())) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001258 ResultStr +=
1259 FPRetType->getReturnType().getAsString(Context->getPrintingPolicy());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001260 ResultStr += "(*";
1261 }
1262 } else
1263 ResultStr += T.getAsString(Context->getPrintingPolicy());
1264}
1265
1266void RewriteModernObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
1267 ObjCMethodDecl *OMD,
1268 std::string &ResultStr) {
1269 //fprintf(stderr,"In RewriteObjCMethodDecl\n");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001270 const FunctionType *FPRetType = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001271 ResultStr += "\nstatic ";
Stephen Hines651f13c2014-04-23 16:59:28 -07001272 RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001273 ResultStr += " ";
1274
1275 // Unique method name
1276 std::string NameStr;
1277
1278 if (OMD->isInstanceMethod())
1279 NameStr += "_I_";
1280 else
1281 NameStr += "_C_";
1282
1283 NameStr += IDecl->getNameAsString();
1284 NameStr += "_";
1285
1286 if (ObjCCategoryImplDecl *CID =
1287 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1288 NameStr += CID->getNameAsString();
1289 NameStr += "_";
1290 }
1291 // Append selector names, replacing ':' with '_'
1292 {
1293 std::string selString = OMD->getSelector().getAsString();
1294 int len = selString.size();
1295 for (int i = 0; i < len; i++)
1296 if (selString[i] == ':')
1297 selString[i] = '_';
1298 NameStr += selString;
1299 }
1300 // Remember this name for metadata emission
1301 MethodInternalNames[OMD] = NameStr;
1302 ResultStr += NameStr;
1303
1304 // Rewrite arguments
1305 ResultStr += "(";
1306
1307 // invisible arguments
1308 if (OMD->isInstanceMethod()) {
1309 QualType selfTy = Context->getObjCInterfaceType(IDecl);
1310 selfTy = Context->getPointerType(selfTy);
1311 if (!LangOpts.MicrosoftExt) {
1312 if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl)))
1313 ResultStr += "struct ";
1314 }
1315 // When rewriting for Microsoft, explicitly omit the structure name.
1316 ResultStr += IDecl->getNameAsString();
1317 ResultStr += " *";
1318 }
1319 else
1320 ResultStr += Context->getObjCClassType().getAsString(
1321 Context->getPrintingPolicy());
1322
1323 ResultStr += " self, ";
1324 ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy());
1325 ResultStr += " _cmd";
1326
1327 // Method arguments.
Stephen Hines651f13c2014-04-23 16:59:28 -07001328 for (const auto *PDecl : OMD->params()) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001329 ResultStr += ", ";
1330 if (PDecl->getType()->isObjCQualifiedIdType()) {
1331 ResultStr += "id ";
1332 ResultStr += PDecl->getNameAsString();
1333 } else {
1334 std::string Name = PDecl->getNameAsString();
1335 QualType QT = PDecl->getType();
1336 // Make sure we convert "t (^)(...)" to "t (*)(...)".
Fariborz Jahanian2610f902012-03-27 16:42:20 +00001337 (void)convertBlockPointerToFunctionPointer(QT);
1338 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001339 ResultStr += Name;
1340 }
1341 }
1342 if (OMD->isVariadic())
1343 ResultStr += ", ...";
1344 ResultStr += ") ";
1345
1346 if (FPRetType) {
1347 ResultStr += ")"; // close the precedence "scope" for "*".
1348
1349 // Now, emit the argument types (if any).
1350 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) {
1351 ResultStr += "(";
Stephen Hines651f13c2014-04-23 16:59:28 -07001352 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001353 if (i) ResultStr += ", ";
Stephen Hines651f13c2014-04-23 16:59:28 -07001354 std::string ParamStr =
1355 FT->getParamType(i).getAsString(Context->getPrintingPolicy());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001356 ResultStr += ParamStr;
1357 }
1358 if (FT->isVariadic()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001359 if (FT->getNumParams())
1360 ResultStr += ", ";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001361 ResultStr += "...";
1362 }
1363 ResultStr += ")";
1364 } else {
1365 ResultStr += "()";
1366 }
1367 }
1368}
1369void RewriteModernObjC::RewriteImplementationDecl(Decl *OID) {
1370 ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
1371 ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
1372
Fariborz Jahaniand2aea122012-02-19 19:00:05 +00001373 if (IMD) {
Fariborz Jahanianb68258f2012-05-01 17:46:45 +00001374 if (IMD->getIvarRBraceLoc().isValid()) {
1375 ReplaceText(IMD->getLocStart(), 1, "/** ");
1376 ReplaceText(IMD->getIvarRBraceLoc(), 1, "**/ ");
Fariborz Jahaniand2aea122012-02-19 19:00:05 +00001377 }
Fariborz Jahanianb68258f2012-05-01 17:46:45 +00001378 else {
1379 InsertText(IMD->getLocStart(), "// ");
1380 }
Fariborz Jahaniand2aea122012-02-19 19:00:05 +00001381 }
1382 else
1383 InsertText(CID->getLocStart(), "// ");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001384
Stephen Hines651f13c2014-04-23 16:59:28 -07001385 for (auto *OMD : IMD ? IMD->instance_methods() : CID->instance_methods()) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001386 std::string ResultStr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001387 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1388 SourceLocation LocStart = OMD->getLocStart();
1389 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1390
1391 const char *startBuf = SM->getCharacterData(LocStart);
1392 const char *endBuf = SM->getCharacterData(LocEnd);
1393 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1394 }
1395
Stephen Hines651f13c2014-04-23 16:59:28 -07001396 for (auto *OMD : IMD ? IMD->class_methods() : CID->class_methods()) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001397 std::string ResultStr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001398 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1399 SourceLocation LocStart = OMD->getLocStart();
1400 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1401
1402 const char *startBuf = SM->getCharacterData(LocStart);
1403 const char *endBuf = SM->getCharacterData(LocEnd);
1404 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1405 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001406 for (auto *I : IMD ? IMD->property_impls() : CID->property_impls())
1407 RewritePropertyImplDecl(I, IMD, CID);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001408
1409 InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// ");
1410}
1411
1412void RewriteModernObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
Fariborz Jahanian8f1fed02012-02-11 20:10:52 +00001413 // Do not synthesize more than once.
1414 if (ObjCSynthesizedStructs.count(ClassDecl))
1415 return;
1416 // Make sure super class's are written before current class is written.
1417 ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass();
1418 while (SuperClass) {
1419 RewriteInterfaceDecl(SuperClass);
1420 SuperClass = SuperClass->getSuperClass();
1421 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001422 std::string ResultStr;
Fariborz Jahaniancf4c60f2012-02-17 22:20:12 +00001423 if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001424 // we haven't seen a forward decl - generate a typedef.
Fariborz Jahanianc38503b2012-03-12 23:58:28 +00001425 RewriteOneForwardClassDecl(ClassDecl, ResultStr);
Fariborz Jahanian72c88f12012-02-22 18:13:25 +00001426 RewriteIvarOffsetSymbols(ClassDecl, ResultStr);
1427
Fariborz Jahanian4339bb32012-02-15 22:01:47 +00001428 RewriteObjCInternalStruct(ClassDecl, ResultStr);
Fariborz Jahaniancf4c60f2012-02-17 22:20:12 +00001429 // Mark this typedef as having been written into its c++ equivalent.
1430 ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl());
Fariborz Jahanian4339bb32012-02-15 22:01:47 +00001431
Stephen Hines651f13c2014-04-23 16:59:28 -07001432 for (auto *I : ClassDecl->properties())
1433 RewriteProperty(I);
1434 for (auto *I : ClassDecl->instance_methods())
1435 RewriteMethodDeclaration(I);
1436 for (auto *I : ClassDecl->class_methods())
1437 RewriteMethodDeclaration(I);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001438
Fariborz Jahanian4339bb32012-02-15 22:01:47 +00001439 // Lastly, comment out the @end.
1440 ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"),
Fariborz Jahanianf9f30792013-04-03 19:11:21 +00001441 "/* @end */\n");
Fariborz Jahanian4339bb32012-02-15 22:01:47 +00001442 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001443}
1444
1445Stmt *RewriteModernObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) {
1446 SourceRange OldRange = PseudoOp->getSourceRange();
1447
1448 // We just magically know some things about the structure of this
1449 // expression.
1450 ObjCMessageExpr *OldMsg =
1451 cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr(
1452 PseudoOp->getNumSemanticExprs() - 1));
1453
1454 // Because the rewriter doesn't allow us to rewrite rewritten code,
1455 // we need to suppress rewriting the sub-statements.
Fariborz Jahanian88ec6102012-04-10 22:06:54 +00001456 Expr *Base;
1457 SmallVector<Expr*, 2> Args;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001458 {
1459 DisableReplaceStmtScope S(*this);
1460
1461 // Rebuild the base expression if we have one.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001462 Base = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001463 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1464 Base = OldMsg->getInstanceReceiver();
1465 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1466 Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base));
1467 }
Fariborz Jahanian88ec6102012-04-10 22:06:54 +00001468
1469 unsigned numArgs = OldMsg->getNumArgs();
1470 for (unsigned i = 0; i < numArgs; i++) {
1471 Expr *Arg = OldMsg->getArg(i);
1472 if (isa<OpaqueValueExpr>(Arg))
1473 Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr();
1474 Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg));
1475 Args.push_back(Arg);
1476 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001477 }
1478
1479 // TODO: avoid this copy.
1480 SmallVector<SourceLocation, 1> SelLocs;
1481 OldMsg->getSelectorLocs(SelLocs);
1482
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001483 ObjCMessageExpr *NewMsg = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001484 switch (OldMsg->getReceiverKind()) {
1485 case ObjCMessageExpr::Class:
1486 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1487 OldMsg->getValueKind(),
1488 OldMsg->getLeftLoc(),
1489 OldMsg->getClassReceiverTypeInfo(),
1490 OldMsg->getSelector(),
1491 SelLocs,
1492 OldMsg->getMethodDecl(),
Fariborz Jahanian88ec6102012-04-10 22:06:54 +00001493 Args,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001494 OldMsg->getRightLoc(),
1495 OldMsg->isImplicit());
1496 break;
1497
1498 case ObjCMessageExpr::Instance:
1499 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1500 OldMsg->getValueKind(),
1501 OldMsg->getLeftLoc(),
1502 Base,
1503 OldMsg->getSelector(),
1504 SelLocs,
1505 OldMsg->getMethodDecl(),
Fariborz Jahanian88ec6102012-04-10 22:06:54 +00001506 Args,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001507 OldMsg->getRightLoc(),
1508 OldMsg->isImplicit());
1509 break;
1510
1511 case ObjCMessageExpr::SuperClass:
1512 case ObjCMessageExpr::SuperInstance:
1513 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1514 OldMsg->getValueKind(),
1515 OldMsg->getLeftLoc(),
1516 OldMsg->getSuperLoc(),
1517 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1518 OldMsg->getSuperType(),
1519 OldMsg->getSelector(),
1520 SelLocs,
1521 OldMsg->getMethodDecl(),
Fariborz Jahanian88ec6102012-04-10 22:06:54 +00001522 Args,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001523 OldMsg->getRightLoc(),
1524 OldMsg->isImplicit());
1525 break;
1526 }
1527
1528 Stmt *Replacement = SynthMessageExpr(NewMsg);
1529 ReplaceStmtWithRange(PseudoOp, Replacement, OldRange);
1530 return Replacement;
1531}
1532
1533Stmt *RewriteModernObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) {
1534 SourceRange OldRange = PseudoOp->getSourceRange();
1535
1536 // We just magically know some things about the structure of this
1537 // expression.
1538 ObjCMessageExpr *OldMsg =
1539 cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit());
1540
1541 // Because the rewriter doesn't allow us to rewrite rewritten code,
1542 // we need to suppress rewriting the sub-statements.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001543 Expr *Base = nullptr;
Fariborz Jahanian88ec6102012-04-10 22:06:54 +00001544 SmallVector<Expr*, 1> Args;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001545 {
1546 DisableReplaceStmtScope S(*this);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001547 // Rebuild the base expression if we have one.
1548 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1549 Base = OldMsg->getInstanceReceiver();
1550 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1551 Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base));
1552 }
Fariborz Jahanian88ec6102012-04-10 22:06:54 +00001553 unsigned numArgs = OldMsg->getNumArgs();
1554 for (unsigned i = 0; i < numArgs; i++) {
1555 Expr *Arg = OldMsg->getArg(i);
1556 if (isa<OpaqueValueExpr>(Arg))
1557 Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr();
1558 Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg));
1559 Args.push_back(Arg);
1560 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001561 }
1562
1563 // Intentionally empty.
1564 SmallVector<SourceLocation, 1> SelLocs;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001565
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001566 ObjCMessageExpr *NewMsg = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001567 switch (OldMsg->getReceiverKind()) {
1568 case ObjCMessageExpr::Class:
1569 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1570 OldMsg->getValueKind(),
1571 OldMsg->getLeftLoc(),
1572 OldMsg->getClassReceiverTypeInfo(),
1573 OldMsg->getSelector(),
1574 SelLocs,
1575 OldMsg->getMethodDecl(),
1576 Args,
1577 OldMsg->getRightLoc(),
1578 OldMsg->isImplicit());
1579 break;
1580
1581 case ObjCMessageExpr::Instance:
1582 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1583 OldMsg->getValueKind(),
1584 OldMsg->getLeftLoc(),
1585 Base,
1586 OldMsg->getSelector(),
1587 SelLocs,
1588 OldMsg->getMethodDecl(),
1589 Args,
1590 OldMsg->getRightLoc(),
1591 OldMsg->isImplicit());
1592 break;
1593
1594 case ObjCMessageExpr::SuperClass:
1595 case ObjCMessageExpr::SuperInstance:
1596 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1597 OldMsg->getValueKind(),
1598 OldMsg->getLeftLoc(),
1599 OldMsg->getSuperLoc(),
1600 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1601 OldMsg->getSuperType(),
1602 OldMsg->getSelector(),
1603 SelLocs,
1604 OldMsg->getMethodDecl(),
1605 Args,
1606 OldMsg->getRightLoc(),
1607 OldMsg->isImplicit());
1608 break;
1609 }
1610
1611 Stmt *Replacement = SynthMessageExpr(NewMsg);
1612 ReplaceStmtWithRange(PseudoOp, Replacement, OldRange);
1613 return Replacement;
1614}
1615
1616/// SynthCountByEnumWithState - To print:
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001617/// ((NSUInteger (*)
1618/// (id, SEL, struct __objcFastEnumerationState *, id *, NSUInteger))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001619/// (void *)objc_msgSend)((id)l_collection,
1620/// sel_registerName(
1621/// "countByEnumeratingWithState:objects:count:"),
1622/// &enumState,
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001623/// (id *)__rw_items, (NSUInteger)16)
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001624///
1625void RewriteModernObjC::SynthCountByEnumWithState(std::string &buf) {
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001626 buf += "((_WIN_NSUInteger (*) (id, SEL, struct __objcFastEnumerationState *, "
1627 "id *, _WIN_NSUInteger))(void *)objc_msgSend)";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001628 buf += "\n\t\t";
1629 buf += "((id)l_collection,\n\t\t";
1630 buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
1631 buf += "\n\t\t";
1632 buf += "&enumState, "
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001633 "(id *)__rw_items, (_WIN_NSUInteger)16)";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001634}
1635
1636/// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
1637/// statement to exit to its outer synthesized loop.
1638///
1639Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) {
1640 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1641 return S;
1642 // replace break with goto __break_label
1643 std::string buf;
1644
1645 SourceLocation startLoc = S->getLocStart();
1646 buf = "goto __break_label_";
1647 buf += utostr(ObjCBcLabelNo.back());
1648 ReplaceText(startLoc, strlen("break"), buf);
1649
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001650 return nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001651}
1652
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00001653void RewriteModernObjC::ConvertSourceLocationToLineDirective(
1654 SourceLocation Loc,
1655 std::string &LineString) {
Fariborz Jahanianada71912013-02-08 00:27:34 +00001656 if (Loc.isFileID() && GenerateLineInfo) {
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00001657 LineString += "\n#line ";
1658 PresumedLoc PLoc = SM->getPresumedLoc(Loc);
1659 LineString += utostr(PLoc.getLine());
1660 LineString += " \"";
1661 LineString += Lexer::Stringify(PLoc.getFilename());
1662 LineString += "\"\n";
1663 }
1664}
1665
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001666/// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
1667/// statement to continue with its inner synthesized loop.
1668///
1669Stmt *RewriteModernObjC::RewriteContinueStmt(ContinueStmt *S) {
1670 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1671 return S;
1672 // replace continue with goto __continue_label
1673 std::string buf;
1674
1675 SourceLocation startLoc = S->getLocStart();
1676 buf = "goto __continue_label_";
1677 buf += utostr(ObjCBcLabelNo.back());
1678 ReplaceText(startLoc, strlen("continue"), buf);
1679
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001680 return nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001681}
1682
1683/// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement.
1684/// It rewrites:
1685/// for ( type elem in collection) { stmts; }
1686
1687/// Into:
1688/// {
1689/// type elem;
1690/// struct __objcFastEnumerationState enumState = { 0 };
1691/// id __rw_items[16];
1692/// id l_collection = (id)collection;
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001693/// NSUInteger limit = [l_collection countByEnumeratingWithState:&enumState
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001694/// objects:__rw_items count:16];
1695/// if (limit) {
1696/// unsigned long startMutations = *enumState.mutationsPtr;
1697/// do {
1698/// unsigned long counter = 0;
1699/// do {
1700/// if (startMutations != *enumState.mutationsPtr)
1701/// objc_enumerationMutation(l_collection);
1702/// elem = (type)enumState.itemsPtr[counter++];
1703/// stmts;
1704/// __continue_label: ;
1705/// } while (counter < limit);
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001706/// } while ((limit = [l_collection countByEnumeratingWithState:&enumState
1707/// objects:__rw_items count:16]));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001708/// elem = nil;
1709/// __break_label: ;
1710/// }
1711/// else
1712/// elem = nil;
1713/// }
1714///
1715Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
1716 SourceLocation OrigEnd) {
1717 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
1718 assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
1719 "ObjCForCollectionStmt Statement stack mismatch");
1720 assert(!ObjCBcLabelNo.empty() &&
1721 "ObjCForCollectionStmt - Label No stack empty");
1722
1723 SourceLocation startLoc = S->getLocStart();
1724 const char *startBuf = SM->getCharacterData(startLoc);
1725 StringRef elementName;
1726 std::string elementTypeAsString;
1727 std::string buf;
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00001728 // line directive first.
1729 SourceLocation ForEachLoc = S->getForLoc();
1730 ConvertSourceLocationToLineDirective(ForEachLoc, buf);
1731 buf += "{\n\t";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001732 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
1733 // type elem;
1734 NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl());
1735 QualType ElementType = cast<ValueDecl>(D)->getType();
1736 if (ElementType->isObjCQualifiedIdType() ||
1737 ElementType->isObjCQualifiedInterfaceType())
1738 // Simply use 'id' for all qualified types.
1739 elementTypeAsString = "id";
1740 else
1741 elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy());
1742 buf += elementTypeAsString;
1743 buf += " ";
1744 elementName = D->getName();
1745 buf += elementName;
1746 buf += ";\n\t";
1747 }
1748 else {
1749 DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement());
1750 elementName = DR->getDecl()->getName();
1751 ValueDecl *VD = cast<ValueDecl>(DR->getDecl());
1752 if (VD->getType()->isObjCQualifiedIdType() ||
1753 VD->getType()->isObjCQualifiedInterfaceType())
1754 // Simply use 'id' for all qualified types.
1755 elementTypeAsString = "id";
1756 else
1757 elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy());
1758 }
1759
1760 // struct __objcFastEnumerationState enumState = { 0 };
1761 buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
1762 // id __rw_items[16];
1763 buf += "id __rw_items[16];\n\t";
1764 // id l_collection = (id)
1765 buf += "id l_collection = (id)";
1766 // Find start location of 'collection' the hard way!
1767 const char *startCollectionBuf = startBuf;
1768 startCollectionBuf += 3; // skip 'for'
1769 startCollectionBuf = strchr(startCollectionBuf, '(');
1770 startCollectionBuf++; // skip '('
1771 // find 'in' and skip it.
1772 while (*startCollectionBuf != ' ' ||
1773 *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
1774 (*(startCollectionBuf+3) != ' ' &&
1775 *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
1776 startCollectionBuf++;
1777 startCollectionBuf += 3;
1778
1779 // Replace: "for (type element in" with string constructed thus far.
1780 ReplaceText(startLoc, startCollectionBuf - startBuf, buf);
1781 // Replace ')' in for '(' type elem in collection ')' with ';'
1782 SourceLocation rightParenLoc = S->getRParenLoc();
1783 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
1784 SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf);
1785 buf = ";\n\t";
1786
1787 // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
1788 // objects:__rw_items count:16];
1789 // which is synthesized into:
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001790 // NSUInteger limit =
1791 // ((NSUInteger (*)
1792 // (id, SEL, struct __objcFastEnumerationState *, id *, NSUInteger))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001793 // (void *)objc_msgSend)((id)l_collection,
1794 // sel_registerName(
1795 // "countByEnumeratingWithState:objects:count:"),
1796 // (struct __objcFastEnumerationState *)&state,
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001797 // (id *)__rw_items, (NSUInteger)16);
1798 buf += "_WIN_NSUInteger limit =\n\t\t";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001799 SynthCountByEnumWithState(buf);
1800 buf += ";\n\t";
1801 /// if (limit) {
1802 /// unsigned long startMutations = *enumState.mutationsPtr;
1803 /// do {
1804 /// unsigned long counter = 0;
1805 /// do {
1806 /// if (startMutations != *enumState.mutationsPtr)
1807 /// objc_enumerationMutation(l_collection);
1808 /// elem = (type)enumState.itemsPtr[counter++];
1809 buf += "if (limit) {\n\t";
1810 buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
1811 buf += "do {\n\t\t";
1812 buf += "unsigned long counter = 0;\n\t\t";
1813 buf += "do {\n\t\t\t";
1814 buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
1815 buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
1816 buf += elementName;
1817 buf += " = (";
1818 buf += elementTypeAsString;
1819 buf += ")enumState.itemsPtr[counter++];";
1820 // Replace ')' in for '(' type elem in collection ')' with all of these.
1821 ReplaceText(lparenLoc, 1, buf);
1822
1823 /// __continue_label: ;
1824 /// } while (counter < limit);
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001825 /// } while ((limit = [l_collection countByEnumeratingWithState:&enumState
1826 /// objects:__rw_items count:16]));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001827 /// elem = nil;
1828 /// __break_label: ;
1829 /// }
1830 /// else
1831 /// elem = nil;
1832 /// }
1833 ///
1834 buf = ";\n\t";
1835 buf += "__continue_label_";
1836 buf += utostr(ObjCBcLabelNo.back());
1837 buf += ": ;";
1838 buf += "\n\t\t";
1839 buf += "} while (counter < limit);\n\t";
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001840 buf += "} while ((limit = ";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001841 SynthCountByEnumWithState(buf);
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00001842 buf += "));\n\t";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001843 buf += elementName;
1844 buf += " = ((";
1845 buf += elementTypeAsString;
1846 buf += ")0);\n\t";
1847 buf += "__break_label_";
1848 buf += utostr(ObjCBcLabelNo.back());
1849 buf += ": ;\n\t";
1850 buf += "}\n\t";
1851 buf += "else\n\t\t";
1852 buf += elementName;
1853 buf += " = ((";
1854 buf += elementTypeAsString;
1855 buf += ")0);\n\t";
1856 buf += "}\n";
1857
1858 // Insert all these *after* the statement body.
1859 // FIXME: If this should support Obj-C++, support CXXTryStmt
1860 if (isa<CompoundStmt>(S->getBody())) {
1861 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1);
1862 InsertText(endBodyLoc, buf);
1863 } else {
1864 /* Need to treat single statements specially. For example:
1865 *
1866 * for (A *a in b) if (stuff()) break;
1867 * for (A *a in b) xxxyy;
1868 *
1869 * The following code simply scans ahead to the semi to find the actual end.
1870 */
1871 const char *stmtBuf = SM->getCharacterData(OrigEnd);
1872 const char *semiBuf = strchr(stmtBuf, ';');
1873 assert(semiBuf && "Can't find ';'");
1874 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1);
1875 InsertText(endBodyLoc, buf);
1876 }
1877 Stmts.pop_back();
1878 ObjCBcLabelNo.pop_back();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001879 return nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001880}
1881
Fariborz Jahanian542125f2012-03-16 21:33:16 +00001882static void Write_RethrowObject(std::string &buf) {
1883 buf += "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n";
1884 buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n";
1885 buf += "\tid rethrow;\n";
1886 buf += "\t} _fin_force_rethow(_rethrow);";
1887}
1888
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001889/// RewriteObjCSynchronizedStmt -
1890/// This routine rewrites @synchronized(expr) stmt;
1891/// into:
1892/// objc_sync_enter(expr);
1893/// @try stmt @finally { objc_sync_exit(expr); }
1894///
1895Stmt *RewriteModernObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1896 // Get the start location and compute the semi location.
1897 SourceLocation startLoc = S->getLocStart();
1898 const char *startBuf = SM->getCharacterData(startLoc);
1899
1900 assert((*startBuf == '@') && "bogus @synchronized location");
1901
1902 std::string buf;
Fariborz Jahanian43f4f1e2012-11-07 00:43:05 +00001903 SourceLocation SynchLoc = S->getAtSynchronizedLoc();
1904 ConvertSourceLocationToLineDirective(SynchLoc, buf);
Fariborz Jahanian786e56f2013-09-17 17:51:48 +00001905 buf += "{ id _rethrow = 0; id _sync_obj = (id)";
Fariborz Jahanian542125f2012-03-16 21:33:16 +00001906
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001907 const char *lparenBuf = startBuf;
1908 while (*lparenBuf != '(') lparenBuf++;
1909 ReplaceText(startLoc, lparenBuf-startBuf+1, buf);
Fariborz Jahanian22e2f852012-03-17 17:46:02 +00001910
1911 buf = "; objc_sync_enter(_sync_obj);\n";
1912 buf += "try {\n\tstruct _SYNC_EXIT { _SYNC_EXIT(id arg) : sync_exit(arg) {}";
1913 buf += "\n\t~_SYNC_EXIT() {objc_sync_exit(sync_exit);}";
1914 buf += "\n\tid sync_exit;";
1915 buf += "\n\t} _sync_exit(_sync_obj);\n";
1916
1917 // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since
1918 // the sync expression is typically a message expression that's already
1919 // been rewritten! (which implies the SourceLocation's are invalid).
1920 SourceLocation RParenExprLoc = S->getSynchBody()->getLocStart();
1921 const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc);
1922 while (*RParenExprLocBuf != ')') RParenExprLocBuf--;
1923 RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf);
1924
1925 SourceLocation LBranceLoc = S->getSynchBody()->getLocStart();
1926 const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc);
1927 assert (*LBraceLocBuf == '{');
1928 ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf);
Fariborz Jahanian542125f2012-03-16 21:33:16 +00001929
Fariborz Jahanianb655bf02012-03-16 21:43:45 +00001930 SourceLocation startRBraceLoc = S->getSynchBody()->getLocEnd();
Matt Beaumont-Gay9ab511c2012-03-16 22:20:39 +00001931 assert((*SM->getCharacterData(startRBraceLoc) == '}') &&
1932 "bogus @synchronized block");
Fariborz Jahanian542125f2012-03-16 21:33:16 +00001933
1934 buf = "} catch (id e) {_rethrow = e;}\n";
1935 Write_RethrowObject(buf);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001936 buf += "}\n";
Fariborz Jahanian542125f2012-03-16 21:33:16 +00001937 buf += "}\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001938
Fariborz Jahanianb655bf02012-03-16 21:43:45 +00001939 ReplaceText(startRBraceLoc, 1, buf);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001940
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001941 return nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001942}
1943
1944void RewriteModernObjC::WarnAboutReturnGotoStmts(Stmt *S)
1945{
1946 // Perform a bottom up traversal of all children.
1947 for (Stmt::child_range CI = S->children(); CI; ++CI)
1948 if (*CI)
1949 WarnAboutReturnGotoStmts(*CI);
1950
1951 if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) {
1952 Diags.Report(Context->getFullLoc(S->getLocStart()),
1953 TryFinallyContainsReturnDiag);
1954 }
1955 return;
1956}
1957
Fariborz Jahanian042b91d2012-05-23 23:47:20 +00001958Stmt *RewriteModernObjC::RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
1959 SourceLocation startLoc = S->getAtLoc();
1960 ReplaceText(startLoc, strlen("@autoreleasepool"), "/* @autoreleasepool */");
Fariborz Jahanianc9b72b62012-05-24 22:59:56 +00001961 ReplaceText(S->getSubStmt()->getLocStart(), 1,
1962 "{ __AtAutoreleasePool __autoreleasepool; ");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001963
1964 return nullptr;
Fariborz Jahanian042b91d2012-05-23 23:47:20 +00001965}
1966
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001967Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
Fariborz Jahanianb1228182012-03-15 22:42:15 +00001968 ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt();
Fariborz Jahanian220419a2012-03-15 23:50:33 +00001969 bool noCatch = S->getNumCatchStmts() == 0;
Fariborz Jahanianb1228182012-03-15 22:42:15 +00001970 std::string buf;
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00001971 SourceLocation TryLocation = S->getAtTryLoc();
1972 ConvertSourceLocationToLineDirective(TryLocation, buf);
Fariborz Jahanianb1228182012-03-15 22:42:15 +00001973
1974 if (finalStmt) {
Fariborz Jahanian220419a2012-03-15 23:50:33 +00001975 if (noCatch)
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00001976 buf += "{ id volatile _rethrow = 0;\n";
Fariborz Jahanian220419a2012-03-15 23:50:33 +00001977 else {
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00001978 buf += "{ id volatile _rethrow = 0;\ntry {\n";
Fariborz Jahanian220419a2012-03-15 23:50:33 +00001979 }
Fariborz Jahanianb1228182012-03-15 22:42:15 +00001980 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001981 // Get the start location and compute the semi location.
1982 SourceLocation startLoc = S->getLocStart();
1983 const char *startBuf = SM->getCharacterData(startLoc);
1984
1985 assert((*startBuf == '@') && "bogus @try location");
Fariborz Jahanianb1228182012-03-15 22:42:15 +00001986 if (finalStmt)
1987 ReplaceText(startLoc, 1, buf);
1988 else
1989 // @try -> try
1990 ReplaceText(startLoc, 1, "");
1991
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001992 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
1993 ObjCAtCatchStmt *Catch = S->getCatchStmt(I);
Fariborz Jahanian4c148812012-03-15 20:11:10 +00001994 VarDecl *catchDecl = Catch->getCatchParamDecl();
Fariborz Jahanianc38503b2012-03-12 23:58:28 +00001995
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00001996 startLoc = Catch->getLocStart();
Fariborz Jahanian4c148812012-03-15 20:11:10 +00001997 bool AtRemoved = false;
1998 if (catchDecl) {
1999 QualType t = catchDecl->getType();
2000 if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) {
2001 // Should be a pointer to a class.
2002 ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface();
2003 if (IDecl) {
2004 std::string Result;
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00002005 ConvertSourceLocationToLineDirective(Catch->getLocStart(), Result);
2006
Fariborz Jahanian4c148812012-03-15 20:11:10 +00002007 startBuf = SM->getCharacterData(startLoc);
2008 assert((*startBuf == '@') && "bogus @catch location");
2009 SourceLocation rParenLoc = Catch->getRParenLoc();
2010 const char *rParenBuf = SM->getCharacterData(rParenLoc);
2011
2012 // _objc_exc_Foo *_e as argument to catch.
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00002013 Result += "catch (_objc_exc_"; Result += IDecl->getNameAsString();
Fariborz Jahanian4c148812012-03-15 20:11:10 +00002014 Result += " *_"; Result += catchDecl->getNameAsString();
2015 Result += ")";
2016 ReplaceText(startLoc, rParenBuf-startBuf+1, Result);
2017 // Foo *e = (Foo *)_e;
2018 Result.clear();
2019 Result = "{ ";
2020 Result += IDecl->getNameAsString();
2021 Result += " *"; Result += catchDecl->getNameAsString();
2022 Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)";
2023 Result += "_"; Result += catchDecl->getNameAsString();
2024
2025 Result += "; ";
2026 SourceLocation lBraceLoc = Catch->getCatchBody()->getLocStart();
2027 ReplaceText(lBraceLoc, 1, Result);
2028 AtRemoved = true;
2029 }
2030 }
2031 }
2032 if (!AtRemoved)
2033 // @catch -> catch
2034 ReplaceText(startLoc, 1, "");
Fariborz Jahanianc38503b2012-03-12 23:58:28 +00002035
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002036 }
Fariborz Jahanian220419a2012-03-15 23:50:33 +00002037 if (finalStmt) {
2038 buf.clear();
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00002039 SourceLocation FinallyLoc = finalStmt->getLocStart();
2040
2041 if (noCatch) {
2042 ConvertSourceLocationToLineDirective(FinallyLoc, buf);
2043 buf += "catch (id e) {_rethrow = e;}\n";
2044 }
2045 else {
2046 buf += "}\n";
2047 ConvertSourceLocationToLineDirective(FinallyLoc, buf);
2048 buf += "catch (id e) {_rethrow = e;}\n";
2049 }
2050
Fariborz Jahanian220419a2012-03-15 23:50:33 +00002051 SourceLocation startFinalLoc = finalStmt->getLocStart();
2052 ReplaceText(startFinalLoc, 8, buf);
2053 Stmt *body = finalStmt->getFinallyBody();
2054 SourceLocation startFinalBodyLoc = body->getLocStart();
2055 buf.clear();
Fariborz Jahanian542125f2012-03-16 21:33:16 +00002056 Write_RethrowObject(buf);
Fariborz Jahanian220419a2012-03-15 23:50:33 +00002057 ReplaceText(startFinalBodyLoc, 1, buf);
2058
2059 SourceLocation endFinalBodyLoc = body->getLocEnd();
2060 ReplaceText(endFinalBodyLoc, 1, "}\n}");
Fariborz Jahanian22e2f852012-03-17 17:46:02 +00002061 // Now check for any return/continue/go statements within the @try.
2062 WarnAboutReturnGotoStmts(S->getTryBody());
Fariborz Jahanian220419a2012-03-15 23:50:33 +00002063 }
2064
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002065 return nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002066}
2067
2068// This can't be done with ReplaceStmt(S, ThrowExpr), since
2069// the throw expression is typically a message expression that's already
2070// been rewritten! (which implies the SourceLocation's are invalid).
2071Stmt *RewriteModernObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
2072 // Get the start location and compute the semi location.
2073 SourceLocation startLoc = S->getLocStart();
2074 const char *startBuf = SM->getCharacterData(startLoc);
2075
2076 assert((*startBuf == '@') && "bogus @throw location");
2077
2078 std::string buf;
2079 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2080 if (S->getThrowExpr())
2081 buf = "objc_exception_throw(";
Fariborz Jahanian40539462012-03-16 16:52:06 +00002082 else
2083 buf = "throw";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002084
2085 // handle "@ throw" correctly.
2086 const char *wBuf = strchr(startBuf, 'w');
2087 assert((*wBuf == 'w') && "@throw: can't find 'w'");
2088 ReplaceText(startLoc, wBuf-startBuf+1, buf);
2089
Fariborz Jahaniana09cd812013-02-11 19:30:33 +00002090 SourceLocation endLoc = S->getLocEnd();
2091 const char *endBuf = SM->getCharacterData(endLoc);
2092 const char *semiBuf = strchr(endBuf, ';');
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002093 assert((*semiBuf == ';') && "@throw: can't find ';'");
2094 SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf);
Fariborz Jahanian40539462012-03-16 16:52:06 +00002095 if (S->getThrowExpr())
2096 ReplaceText(semiLoc, 1, ");");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002097 return nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002098}
2099
2100Stmt *RewriteModernObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) {
2101 // Create a new string expression.
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002102 std::string StrEncoding;
2103 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
Stephen Hines651f13c2014-04-23 16:59:28 -07002104 Expr *Replacement = getStringLiteral(StrEncoding);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002105 ReplaceStmt(Exp, Replacement);
2106
2107 // Replace this subexpr in the parent.
2108 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2109 return Replacement;
2110}
2111
2112Stmt *RewriteModernObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) {
2113 if (!SelGetUidFunctionDecl)
2114 SynthSelGetUidFunctionDecl();
2115 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
2116 // Create a call to sel_registerName("selName").
2117 SmallVector<Expr*, 8> SelExprs;
Stephen Hines651f13c2014-04-23 16:59:28 -07002118 SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString()));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002119 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2120 &SelExprs[0], SelExprs.size());
2121 ReplaceStmt(Exp, SelExp);
2122 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2123 return SelExp;
2124}
2125
2126CallExpr *RewriteModernObjC::SynthesizeCallToFunctionDecl(
2127 FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc,
2128 SourceLocation EndLoc) {
2129 // Get the type, we will need to reference it in a couple spots.
2130 QualType msgSendType = FD->getType();
2131
2132 // Create a reference to the objc_msgSend() declaration.
2133 DeclRefExpr *DRE =
John McCallf4b88a42012-03-10 09:33:50 +00002134 new (Context) DeclRefExpr(FD, false, msgSendType, VK_LValue, SourceLocation());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002135
2136 // Now, we cast the reference to a pointer to the objc_msgSend type.
2137 QualType pToFunc = Context->getPointerType(msgSendType);
2138 ImplicitCastExpr *ICE =
2139 ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002140 DRE, nullptr, VK_RValue);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002141
2142 const FunctionType *FT = msgSendType->getAs<FunctionType>();
2143
2144 CallExpr *Exp =
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00002145 new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002146 FT->getCallResultType(*Context),
2147 VK_RValue, EndLoc);
2148 return Exp;
2149}
2150
2151static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
2152 const char *&startRef, const char *&endRef) {
2153 while (startBuf < endBuf) {
2154 if (*startBuf == '<')
2155 startRef = startBuf; // mark the start.
2156 if (*startBuf == '>') {
2157 if (startRef && *startRef == '<') {
2158 endRef = startBuf; // mark the end.
2159 return true;
2160 }
2161 return false;
2162 }
2163 startBuf++;
2164 }
2165 return false;
2166}
2167
2168static void scanToNextArgument(const char *&argRef) {
2169 int angle = 0;
2170 while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
2171 if (*argRef == '<')
2172 angle++;
2173 else if (*argRef == '>')
2174 angle--;
2175 argRef++;
2176 }
2177 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
2178}
2179
2180bool RewriteModernObjC::needToScanForQualifiers(QualType T) {
2181 if (T->isObjCQualifiedIdType())
2182 return true;
2183 if (const PointerType *PT = T->getAs<PointerType>()) {
2184 if (PT->getPointeeType()->isObjCQualifiedIdType())
2185 return true;
2186 }
2187 if (T->isObjCObjectPointerType()) {
2188 T = T->getPointeeType();
2189 return T->isObjCQualifiedInterfaceType();
2190 }
2191 if (T->isArrayType()) {
2192 QualType ElemTy = Context->getBaseElementType(T);
2193 return needToScanForQualifiers(ElemTy);
2194 }
2195 return false;
2196}
2197
2198void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) {
2199 QualType Type = E->getType();
2200 if (needToScanForQualifiers(Type)) {
2201 SourceLocation Loc, EndLoc;
2202
2203 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) {
2204 Loc = ECE->getLParenLoc();
2205 EndLoc = ECE->getRParenLoc();
2206 } else {
2207 Loc = E->getLocStart();
2208 EndLoc = E->getLocEnd();
2209 }
2210 // This will defend against trying to rewrite synthesized expressions.
2211 if (Loc.isInvalid() || EndLoc.isInvalid())
2212 return;
2213
2214 const char *startBuf = SM->getCharacterData(Loc);
2215 const char *endBuf = SM->getCharacterData(EndLoc);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002216 const char *startRef = nullptr, *endRef = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002217 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2218 // Get the locations of the startRef, endRef.
2219 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf);
2220 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1);
2221 // Comment out the protocol references.
2222 InsertText(LessLoc, "/*");
2223 InsertText(GreaterLoc, "*/");
2224 }
2225 }
2226}
2227
2228void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
2229 SourceLocation Loc;
2230 QualType Type;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002231 const FunctionProtoType *proto = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002232 if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
2233 Loc = VD->getLocation();
2234 Type = VD->getType();
2235 }
2236 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
2237 Loc = FD->getLocation();
2238 // Check for ObjC 'id' and class types that have been adorned with protocol
2239 // information (id<p>, C<p>*). The protocol references need to be rewritten!
2240 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2241 assert(funcType && "missing function type");
2242 proto = dyn_cast<FunctionProtoType>(funcType);
2243 if (!proto)
2244 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07002245 Type = proto->getReturnType();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002246 }
2247 else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) {
2248 Loc = FD->getLocation();
2249 Type = FD->getType();
2250 }
Fariborz Jahanianf9f30792013-04-03 19:11:21 +00002251 else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(Dcl)) {
2252 Loc = TD->getLocation();
2253 Type = TD->getUnderlyingType();
2254 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002255 else
2256 return;
2257
2258 if (needToScanForQualifiers(Type)) {
2259 // Since types are unique, we need to scan the buffer.
2260
2261 const char *endBuf = SM->getCharacterData(Loc);
2262 const char *startBuf = endBuf;
2263 while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart)
2264 startBuf--; // scan backward (from the decl location) for return type.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002265 const char *startRef = nullptr, *endRef = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002266 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2267 // Get the locations of the startRef, endRef.
2268 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf);
2269 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1);
2270 // Comment out the protocol references.
2271 InsertText(LessLoc, "/*");
2272 InsertText(GreaterLoc, "*/");
2273 }
2274 }
2275 if (!proto)
2276 return; // most likely, was a variable
2277 // Now check arguments.
2278 const char *startBuf = SM->getCharacterData(Loc);
2279 const char *startFuncBuf = startBuf;
Stephen Hines651f13c2014-04-23 16:59:28 -07002280 for (unsigned i = 0; i < proto->getNumParams(); i++) {
2281 if (needToScanForQualifiers(proto->getParamType(i))) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002282 // Since types are unique, we need to scan the buffer.
2283
2284 const char *endBuf = startBuf;
2285 // scan forward (from the decl location) for argument types.
2286 scanToNextArgument(endBuf);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002287 const char *startRef = nullptr, *endRef = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002288 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2289 // Get the locations of the startRef, endRef.
2290 SourceLocation LessLoc =
2291 Loc.getLocWithOffset(startRef-startFuncBuf);
2292 SourceLocation GreaterLoc =
2293 Loc.getLocWithOffset(endRef-startFuncBuf+1);
2294 // Comment out the protocol references.
2295 InsertText(LessLoc, "/*");
2296 InsertText(GreaterLoc, "*/");
2297 }
2298 startBuf = ++endBuf;
2299 }
2300 else {
2301 // If the function name is derived from a macro expansion, then the
2302 // argument buffer will not follow the name. Need to speak with Chris.
2303 while (*startBuf && *startBuf != ')' && *startBuf != ',')
2304 startBuf++; // scan forward (from the decl location) for argument types.
2305 startBuf++;
2306 }
2307 }
2308}
2309
2310void RewriteModernObjC::RewriteTypeOfDecl(VarDecl *ND) {
2311 QualType QT = ND->getType();
2312 const Type* TypePtr = QT->getAs<Type>();
2313 if (!isa<TypeOfExprType>(TypePtr))
2314 return;
2315 while (isa<TypeOfExprType>(TypePtr)) {
2316 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
2317 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
2318 TypePtr = QT->getAs<Type>();
2319 }
2320 // FIXME. This will not work for multiple declarators; as in:
2321 // __typeof__(a) b,c,d;
2322 std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy()));
2323 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
2324 const char *startBuf = SM->getCharacterData(DeclLoc);
2325 if (ND->getInit()) {
2326 std::string Name(ND->getNameAsString());
2327 TypeAsString += " " + Name + " = ";
2328 Expr *E = ND->getInit();
2329 SourceLocation startLoc;
2330 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
2331 startLoc = ECE->getLParenLoc();
2332 else
2333 startLoc = E->getLocStart();
2334 startLoc = SM->getExpansionLoc(startLoc);
2335 const char *endBuf = SM->getCharacterData(startLoc);
2336 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2337 }
2338 else {
2339 SourceLocation X = ND->getLocEnd();
2340 X = SM->getExpansionLoc(X);
2341 const char *endBuf = SM->getCharacterData(X);
2342 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2343 }
2344}
2345
2346// SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
2347void RewriteModernObjC::SynthSelGetUidFunctionDecl() {
2348 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
2349 SmallVector<QualType, 16> ArgTys;
2350 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2351 QualType getFuncType =
Jordan Rosebea522f2013-03-08 21:51:21 +00002352 getSimpleFunctionType(Context->getObjCSelType(), ArgTys);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002353 SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002354 SourceLocation(),
2355 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002356 SelGetUidIdent, getFuncType,
2357 nullptr, SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002358}
2359
2360void RewriteModernObjC::RewriteFunctionDecl(FunctionDecl *FD) {
2361 // declared in <objc/objc.h>
2362 if (FD->getIdentifier() &&
2363 FD->getName() == "sel_registerName") {
2364 SelGetUidFunctionDecl = FD;
2365 return;
2366 }
2367 RewriteObjCQualifiedInterfaceTypes(FD);
2368}
2369
2370void RewriteModernObjC::RewriteBlockPointerType(std::string& Str, QualType Type) {
2371 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2372 const char *argPtr = TypeString.c_str();
2373 if (!strchr(argPtr, '^')) {
2374 Str += TypeString;
2375 return;
2376 }
2377 while (*argPtr) {
2378 Str += (*argPtr == '^' ? '*' : *argPtr);
2379 argPtr++;
2380 }
2381}
2382
2383// FIXME. Consolidate this routine with RewriteBlockPointerType.
2384void RewriteModernObjC::RewriteBlockPointerTypeVariable(std::string& Str,
2385 ValueDecl *VD) {
2386 QualType Type = VD->getType();
2387 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2388 const char *argPtr = TypeString.c_str();
2389 int paren = 0;
2390 while (*argPtr) {
2391 switch (*argPtr) {
2392 case '(':
2393 Str += *argPtr;
2394 paren++;
2395 break;
2396 case ')':
2397 Str += *argPtr;
2398 paren--;
2399 break;
2400 case '^':
2401 Str += '*';
2402 if (paren == 1)
2403 Str += VD->getNameAsString();
2404 break;
2405 default:
2406 Str += *argPtr;
2407 break;
2408 }
2409 argPtr++;
2410 }
2411}
2412
Fariborz Jahanianb75f8de2012-04-19 00:50:01 +00002413void RewriteModernObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) {
2414 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
2415 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2416 const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType);
2417 if (!proto)
2418 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07002419 QualType Type = proto->getReturnType();
Fariborz Jahanianb75f8de2012-04-19 00:50:01 +00002420 std::string FdStr = Type.getAsString(Context->getPrintingPolicy());
2421 FdStr += " ";
2422 FdStr += FD->getName();
2423 FdStr += "(";
Stephen Hines651f13c2014-04-23 16:59:28 -07002424 unsigned numArgs = proto->getNumParams();
Fariborz Jahanianb75f8de2012-04-19 00:50:01 +00002425 for (unsigned i = 0; i < numArgs; i++) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002426 QualType ArgType = proto->getParamType(i);
Fariborz Jahanianb75f8de2012-04-19 00:50:01 +00002427 RewriteBlockPointerType(FdStr, ArgType);
2428 if (i+1 < numArgs)
2429 FdStr += ", ";
2430 }
Fariborz Jahanianb5863da2012-04-19 16:30:28 +00002431 if (FD->isVariadic()) {
2432 FdStr += (numArgs > 0) ? ", ...);\n" : "...);\n";
2433 }
2434 else
2435 FdStr += ");\n";
Fariborz Jahanianb75f8de2012-04-19 00:50:01 +00002436 InsertText(FunLocStart, FdStr);
2437}
2438
Benjamin Kramere5753592013-09-09 14:48:42 +00002439// SynthSuperConstructorFunctionDecl - id __rw_objc_super(id obj, id super);
2440void RewriteModernObjC::SynthSuperConstructorFunctionDecl() {
2441 if (SuperConstructorFunctionDecl)
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002442 return;
2443 IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super");
2444 SmallVector<QualType, 16> ArgTys;
2445 QualType argT = Context->getObjCIdType();
2446 assert(!argT.isNull() && "Can't find 'id' type");
2447 ArgTys.push_back(argT);
2448 ArgTys.push_back(argT);
2449 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002450 ArgTys);
Benjamin Kramere5753592013-09-09 14:48:42 +00002451 SuperConstructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002452 SourceLocation(),
2453 SourceLocation(),
2454 msgSendIdent, msgSendType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002455 nullptr, SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002456}
2457
2458// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
2459void RewriteModernObjC::SynthMsgSendFunctionDecl() {
2460 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
2461 SmallVector<QualType, 16> ArgTys;
2462 QualType argT = Context->getObjCIdType();
2463 assert(!argT.isNull() && "Can't find 'id' type");
2464 ArgTys.push_back(argT);
2465 argT = Context->getObjCSelType();
2466 assert(!argT.isNull() && "Can't find 'SEL' type");
2467 ArgTys.push_back(argT);
2468 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002469 ArgTys, /*isVariadic=*/true);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002470 MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002471 SourceLocation(),
2472 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002473 msgSendIdent, msgSendType, nullptr,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002474 SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002475}
2476
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00002477// SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(void);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002478void RewriteModernObjC::SynthMsgSendSuperFunctionDecl() {
2479 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00002480 SmallVector<QualType, 2> ArgTys;
2481 ArgTys.push_back(Context->VoidTy);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002482 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002483 ArgTys, /*isVariadic=*/true);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002484 MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002485 SourceLocation(),
2486 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002487 msgSendIdent, msgSendType,
2488 nullptr, SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002489}
2490
2491// SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
2492void RewriteModernObjC::SynthMsgSendStretFunctionDecl() {
2493 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
2494 SmallVector<QualType, 16> ArgTys;
2495 QualType argT = Context->getObjCIdType();
2496 assert(!argT.isNull() && "Can't find 'id' type");
2497 ArgTys.push_back(argT);
2498 argT = Context->getObjCSelType();
2499 assert(!argT.isNull() && "Can't find 'SEL' type");
2500 ArgTys.push_back(argT);
2501 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002502 ArgTys, /*isVariadic=*/true);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002503 MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002504 SourceLocation(),
2505 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002506 msgSendIdent, msgSendType,
2507 nullptr, SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002508}
2509
2510// SynthMsgSendSuperStretFunctionDecl -
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00002511// id objc_msgSendSuper_stret(void);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002512void RewriteModernObjC::SynthMsgSendSuperStretFunctionDecl() {
2513 IdentifierInfo *msgSendIdent =
2514 &Context->Idents.get("objc_msgSendSuper_stret");
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00002515 SmallVector<QualType, 2> ArgTys;
2516 ArgTys.push_back(Context->VoidTy);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002517 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002518 ArgTys, /*isVariadic=*/true);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002519 MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2520 SourceLocation(),
2521 SourceLocation(),
Chad Rosiere3b29882013-01-04 22:40:33 +00002522 msgSendIdent,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002523 msgSendType, nullptr,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002524 SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002525}
2526
2527// SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...);
2528void RewriteModernObjC::SynthMsgSendFpretFunctionDecl() {
2529 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
2530 SmallVector<QualType, 16> ArgTys;
2531 QualType argT = Context->getObjCIdType();
2532 assert(!argT.isNull() && "Can't find 'id' type");
2533 ArgTys.push_back(argT);
2534 argT = Context->getObjCSelType();
2535 assert(!argT.isNull() && "Can't find 'SEL' type");
2536 ArgTys.push_back(argT);
2537 QualType msgSendType = getSimpleFunctionType(Context->DoubleTy,
Jordan Rosebea522f2013-03-08 21:51:21 +00002538 ArgTys, /*isVariadic=*/true);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002539 MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002540 SourceLocation(),
2541 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002542 msgSendIdent, msgSendType,
2543 nullptr, SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002544}
2545
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00002546// SynthGetClassFunctionDecl - Class objc_getClass(const char *name);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002547void RewriteModernObjC::SynthGetClassFunctionDecl() {
2548 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
2549 SmallVector<QualType, 16> ArgTys;
2550 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00002551 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002552 ArgTys);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002553 GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002554 SourceLocation(),
2555 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002556 getClassIdent, getClassType,
2557 nullptr, SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002558}
2559
2560// SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls);
2561void RewriteModernObjC::SynthGetSuperClassFunctionDecl() {
2562 IdentifierInfo *getSuperClassIdent =
2563 &Context->Idents.get("class_getSuperclass");
2564 SmallVector<QualType, 16> ArgTys;
2565 ArgTys.push_back(Context->getObjCClassType());
2566 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002567 ArgTys);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002568 GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2569 SourceLocation(),
2570 SourceLocation(),
2571 getSuperClassIdent,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002572 getClassType, nullptr,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002573 SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002574}
2575
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00002576// SynthGetMetaClassFunctionDecl - Class objc_getMetaClass(const char *name);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002577void RewriteModernObjC::SynthGetMetaClassFunctionDecl() {
2578 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
2579 SmallVector<QualType, 16> ArgTys;
2580 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00002581 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
Jordan Rosebea522f2013-03-08 21:51:21 +00002582 ArgTys);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002583 GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
Chad Rosiere3b29882013-01-04 22:40:33 +00002584 SourceLocation(),
2585 SourceLocation(),
2586 getClassIdent, getClassType,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002587 nullptr, SC_Extern);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002588}
2589
2590Stmt *RewriteModernObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002591 assert (Exp != nullptr && "Expected non-null ObjCStringLiteral");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002592 QualType strType = getConstantStringStructType();
2593
2594 std::string S = "__NSConstantStringImpl_";
2595
2596 std::string tmpName = InFileName;
2597 unsigned i;
2598 for (i=0; i < tmpName.length(); i++) {
2599 char c = tmpName.at(i);
Stephen Hines651f13c2014-04-23 16:59:28 -07002600 // replace any non-alphanumeric characters with '_'.
Jordan Rose3f6f51e2013-02-08 22:30:41 +00002601 if (!isAlphanumeric(c))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002602 tmpName[i] = '_';
2603 }
2604 S += tmpName;
2605 S += "_";
2606 S += utostr(NumObjCStringLiterals++);
2607
2608 Preamble += "static __NSConstantStringImpl " + S;
2609 Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,";
2610 Preamble += "0x000007c8,"; // utf8_str
2611 // The pretty printer for StringLiteral handles escape characters properly.
2612 std::string prettyBufS;
2613 llvm::raw_string_ostream prettyBuf(prettyBufS);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002614 Exp->getString()->printPretty(prettyBuf, nullptr, PrintingPolicy(LangOpts));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002615 Preamble += prettyBuf.str();
2616 Preamble += ",";
2617 Preamble += utostr(Exp->getString()->getByteLength()) + "};\n";
2618
2619 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
2620 SourceLocation(), &Context->Idents.get(S),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002621 strType, nullptr, SC_Static);
John McCallf4b88a42012-03-10 09:33:50 +00002622 DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00002623 SourceLocation());
2624 Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf,
2625 Context->getPointerType(DRE->getType()),
2626 VK_RValue, OK_Ordinary,
2627 SourceLocation());
2628 // cast to NSConstantString *
2629 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(),
2630 CK_CPointerToObjCPointerCast, Unop);
2631 ReplaceStmt(Exp, cast);
2632 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2633 return cast;
2634}
2635
Fariborz Jahanian55947042012-03-27 20:17:30 +00002636Stmt *RewriteModernObjC::RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp) {
2637 unsigned IntSize =
2638 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
2639
2640 Expr *FlagExp = IntegerLiteral::Create(*Context,
2641 llvm::APInt(IntSize, Exp->getValue()),
2642 Context->IntTy, Exp->getLocation());
2643 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Context->ObjCBuiltinBoolTy,
2644 CK_BitCast, FlagExp);
2645 ParenExpr *PE = new (Context) ParenExpr(Exp->getLocation(), Exp->getExprLoc(),
2646 cast);
2647 ReplaceStmt(Exp, PE);
2648 return PE;
2649}
2650
Patrick Beardeb382ec2012-04-19 00:25:12 +00002651Stmt *RewriteModernObjC::RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp) {
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002652 // synthesize declaration of helper functions needed in this routine.
2653 if (!SelGetUidFunctionDecl)
2654 SynthSelGetUidFunctionDecl();
2655 // use objc_msgSend() for all.
2656 if (!MsgSendFunctionDecl)
2657 SynthMsgSendFunctionDecl();
2658 if (!GetClassFunctionDecl)
2659 SynthGetClassFunctionDecl();
2660
2661 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2662 SourceLocation StartLoc = Exp->getLocStart();
2663 SourceLocation EndLoc = Exp->getLocEnd();
2664
2665 // Synthesize a call to objc_msgSend().
2666 SmallVector<Expr*, 4> MsgExprs;
2667 SmallVector<Expr*, 4> ClsExprs;
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002668
Patrick Beardeb382ec2012-04-19 00:25:12 +00002669 // Create a call to objc_getClass("<BoxingClass>"). It will be the 1st argument.
2670 ObjCMethodDecl *BoxingMethod = Exp->getBoxingMethod();
2671 ObjCInterfaceDecl *BoxingClass = BoxingMethod->getClassInterface();
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002672
Patrick Beardeb382ec2012-04-19 00:25:12 +00002673 IdentifierInfo *clsName = BoxingClass->getIdentifier();
Stephen Hines651f13c2014-04-23 16:59:28 -07002674 ClsExprs.push_back(getStringLiteral(clsName->getName()));
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002675 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2676 &ClsExprs[0],
2677 ClsExprs.size(),
2678 StartLoc, EndLoc);
2679 MsgExprs.push_back(Cls);
2680
Patrick Beardeb382ec2012-04-19 00:25:12 +00002681 // Create a call to sel_registerName("<BoxingMethod>:"), etc.
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002682 // it will be the 2nd argument.
2683 SmallVector<Expr*, 4> SelExprs;
Stephen Hines651f13c2014-04-23 16:59:28 -07002684 SelExprs.push_back(
2685 getStringLiteral(BoxingMethod->getSelector().getAsString()));
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002686 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2687 &SelExprs[0], SelExprs.size(),
2688 StartLoc, EndLoc);
2689 MsgExprs.push_back(SelExp);
2690
Patrick Beardeb382ec2012-04-19 00:25:12 +00002691 // User provided sub-expression is the 3rd, and last, argument.
2692 Expr *subExpr = Exp->getSubExpr();
2693 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(subExpr)) {
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002694 QualType type = ICE->getType();
2695 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
2696 CastKind CK = CK_BitCast;
2697 if (SubExpr->getType()->isIntegralType(*Context) && type->isBooleanType())
2698 CK = CK_IntegralToBoolean;
Patrick Beardeb382ec2012-04-19 00:25:12 +00002699 subExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, subExpr);
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002700 }
Patrick Beardeb382ec2012-04-19 00:25:12 +00002701 MsgExprs.push_back(subExpr);
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002702
2703 SmallVector<QualType, 4> ArgTypes;
2704 ArgTypes.push_back(Context->getObjCIdType());
2705 ArgTypes.push_back(Context->getObjCSelType());
Stephen Hines651f13c2014-04-23 16:59:28 -07002706 for (const auto PI : BoxingMethod->parameters())
2707 ArgTypes.push_back(PI->getType());
Patrick Beardeb382ec2012-04-19 00:25:12 +00002708
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002709 QualType returnType = Exp->getType();
2710 // Get the type, we will need to reference it in a couple spots.
2711 QualType msgSendType = MsgSendFlavor->getType();
2712
2713 // Create a reference to the objc_msgSend() declaration.
2714 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
2715 VK_LValue, SourceLocation());
2716
2717 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context,
Patrick Beardeb382ec2012-04-19 00:25:12 +00002718 Context->getPointerType(Context->VoidTy),
2719 CK_BitCast, DRE);
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002720
2721 // Now do the "normal" pointer to function cast.
2722 QualType castType =
Jordan Rosebea522f2013-03-08 21:51:21 +00002723 getSimpleFunctionType(returnType, ArgTypes, BoxingMethod->isVariadic());
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002724 castType = Context->getPointerType(castType);
2725 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
2726 cast);
2727
2728 // Don't forget the parens to enforce the proper binding.
2729 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
2730
2731 const FunctionType *FT = msgSendType->getAs<FunctionType>();
Stephen Hines651f13c2014-04-23 16:59:28 -07002732 CallExpr *CE = new (Context)
2733 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00002734 ReplaceStmt(Exp, CE);
2735 return CE;
2736}
2737
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002738Stmt *RewriteModernObjC::RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp) {
2739 // synthesize declaration of helper functions needed in this routine.
2740 if (!SelGetUidFunctionDecl)
2741 SynthSelGetUidFunctionDecl();
2742 // use objc_msgSend() for all.
2743 if (!MsgSendFunctionDecl)
2744 SynthMsgSendFunctionDecl();
2745 if (!GetClassFunctionDecl)
2746 SynthGetClassFunctionDecl();
2747
2748 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2749 SourceLocation StartLoc = Exp->getLocStart();
2750 SourceLocation EndLoc = Exp->getLocEnd();
2751
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002752 // Build the expression: __NSContainer_literal(int, ...).arr
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00002753 QualType IntQT = Context->IntTy;
2754 QualType NSArrayFType =
Jordan Rosebea522f2013-03-08 21:51:21 +00002755 getSimpleFunctionType(Context->VoidTy, IntQT, true);
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002756 std::string NSArrayFName("__NSContainer_literal");
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00002757 FunctionDecl *NSArrayFD = SynthBlockInitFunctionDecl(NSArrayFName);
2758 DeclRefExpr *NSArrayDRE =
2759 new (Context) DeclRefExpr(NSArrayFD, false, NSArrayFType, VK_RValue,
2760 SourceLocation());
2761
2762 SmallVector<Expr*, 16> InitExprs;
2763 unsigned NumElements = Exp->getNumElements();
2764 unsigned UnsignedIntSize =
2765 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
2766 Expr *count = IntegerLiteral::Create(*Context,
2767 llvm::APInt(UnsignedIntSize, NumElements),
2768 Context->UnsignedIntTy, SourceLocation());
2769 InitExprs.push_back(count);
2770 for (unsigned i = 0; i < NumElements; i++)
2771 InitExprs.push_back(Exp->getElement(i));
2772 Expr *NSArrayCallExpr =
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00002773 new (Context) CallExpr(*Context, NSArrayDRE, InitExprs,
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00002774 NSArrayFType, VK_LValue, SourceLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002775
2776 FieldDecl *ARRFD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00002777 SourceLocation(),
2778 &Context->Idents.get("arr"),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002779 Context->getPointerType(Context->VoidPtrTy),
2780 nullptr, /*BitWidth=*/nullptr,
2781 /*Mutable=*/true, ICIS_NoInit);
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00002782 MemberExpr *ArrayLiteralME =
2783 new (Context) MemberExpr(NSArrayCallExpr, false, ARRFD,
2784 SourceLocation(),
2785 ARRFD->getType(), VK_LValue,
2786 OK_Ordinary);
2787 QualType ConstIdT = Context->getObjCIdType().withConst();
2788 CStyleCastExpr * ArrayLiteralObjects =
2789 NoTypeInfoCStyleCastExpr(Context,
2790 Context->getPointerType(ConstIdT),
2791 CK_BitCast,
2792 ArrayLiteralME);
2793
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002794 // Synthesize a call to objc_msgSend().
2795 SmallVector<Expr*, 32> MsgExprs;
2796 SmallVector<Expr*, 4> ClsExprs;
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002797 QualType expType = Exp->getType();
2798
2799 // Create a call to objc_getClass("NSArray"). It will be th 1st argument.
2800 ObjCInterfaceDecl *Class =
2801 expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface();
2802
2803 IdentifierInfo *clsName = Class->getIdentifier();
Stephen Hines651f13c2014-04-23 16:59:28 -07002804 ClsExprs.push_back(getStringLiteral(clsName->getName()));
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002805 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2806 &ClsExprs[0],
2807 ClsExprs.size(),
2808 StartLoc, EndLoc);
2809 MsgExprs.push_back(Cls);
2810
2811 // Create a call to sel_registerName("arrayWithObjects:count:").
2812 // it will be the 2nd argument.
2813 SmallVector<Expr*, 4> SelExprs;
2814 ObjCMethodDecl *ArrayMethod = Exp->getArrayWithObjectsMethod();
Stephen Hines651f13c2014-04-23 16:59:28 -07002815 SelExprs.push_back(
2816 getStringLiteral(ArrayMethod->getSelector().getAsString()));
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002817 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2818 &SelExprs[0], SelExprs.size(),
2819 StartLoc, EndLoc);
2820 MsgExprs.push_back(SelExp);
2821
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00002822 // (const id [])objects
2823 MsgExprs.push_back(ArrayLiteralObjects);
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002824
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00002825 // (NSUInteger)cnt
2826 Expr *cnt = IntegerLiteral::Create(*Context,
2827 llvm::APInt(UnsignedIntSize, NumElements),
2828 Context->UnsignedIntTy, SourceLocation());
2829 MsgExprs.push_back(cnt);
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002830
2831
2832 SmallVector<QualType, 4> ArgTypes;
2833 ArgTypes.push_back(Context->getObjCIdType());
2834 ArgTypes.push_back(Context->getObjCSelType());
Stephen Hines651f13c2014-04-23 16:59:28 -07002835 for (const auto *PI : ArrayMethod->params())
2836 ArgTypes.push_back(PI->getType());
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002837
2838 QualType returnType = Exp->getType();
2839 // Get the type, we will need to reference it in a couple spots.
2840 QualType msgSendType = MsgSendFlavor->getType();
2841
2842 // Create a reference to the objc_msgSend() declaration.
2843 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
2844 VK_LValue, SourceLocation());
2845
2846 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context,
2847 Context->getPointerType(Context->VoidTy),
2848 CK_BitCast, DRE);
2849
2850 // Now do the "normal" pointer to function cast.
2851 QualType castType =
Jordan Rosebea522f2013-03-08 21:51:21 +00002852 getSimpleFunctionType(returnType, ArgTypes, ArrayMethod->isVariadic());
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002853 castType = Context->getPointerType(castType);
2854 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
2855 cast);
2856
2857 // Don't forget the parens to enforce the proper binding.
2858 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
2859
2860 const FunctionType *FT = msgSendType->getAs<FunctionType>();
Stephen Hines651f13c2014-04-23 16:59:28 -07002861 CallExpr *CE = new (Context)
2862 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
Fariborz Jahanian86cff602012-03-30 23:35:47 +00002863 ReplaceStmt(Exp, CE);
2864 return CE;
2865}
2866
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002867Stmt *RewriteModernObjC::RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp) {
2868 // synthesize declaration of helper functions needed in this routine.
2869 if (!SelGetUidFunctionDecl)
2870 SynthSelGetUidFunctionDecl();
2871 // use objc_msgSend() for all.
2872 if (!MsgSendFunctionDecl)
2873 SynthMsgSendFunctionDecl();
2874 if (!GetClassFunctionDecl)
2875 SynthGetClassFunctionDecl();
2876
2877 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2878 SourceLocation StartLoc = Exp->getLocStart();
2879 SourceLocation EndLoc = Exp->getLocEnd();
2880
2881 // Build the expression: __NSContainer_literal(int, ...).arr
2882 QualType IntQT = Context->IntTy;
2883 QualType NSDictFType =
Jordan Rosebea522f2013-03-08 21:51:21 +00002884 getSimpleFunctionType(Context->VoidTy, IntQT, true);
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002885 std::string NSDictFName("__NSContainer_literal");
2886 FunctionDecl *NSDictFD = SynthBlockInitFunctionDecl(NSDictFName);
2887 DeclRefExpr *NSDictDRE =
2888 new (Context) DeclRefExpr(NSDictFD, false, NSDictFType, VK_RValue,
2889 SourceLocation());
2890
2891 SmallVector<Expr*, 16> KeyExprs;
2892 SmallVector<Expr*, 16> ValueExprs;
2893
2894 unsigned NumElements = Exp->getNumElements();
2895 unsigned UnsignedIntSize =
2896 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
2897 Expr *count = IntegerLiteral::Create(*Context,
2898 llvm::APInt(UnsignedIntSize, NumElements),
2899 Context->UnsignedIntTy, SourceLocation());
2900 KeyExprs.push_back(count);
2901 ValueExprs.push_back(count);
2902 for (unsigned i = 0; i < NumElements; i++) {
2903 ObjCDictionaryElement Element = Exp->getKeyValueElement(i);
2904 KeyExprs.push_back(Element.Key);
2905 ValueExprs.push_back(Element.Value);
2906 }
2907
2908 // (const id [])objects
2909 Expr *NSValueCallExpr =
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00002910 new (Context) CallExpr(*Context, NSDictDRE, ValueExprs,
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002911 NSDictFType, VK_LValue, SourceLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002912
2913 FieldDecl *ARRFD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002914 SourceLocation(),
2915 &Context->Idents.get("arr"),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002916 Context->getPointerType(Context->VoidPtrTy),
2917 nullptr, /*BitWidth=*/nullptr,
2918 /*Mutable=*/true, ICIS_NoInit);
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002919 MemberExpr *DictLiteralValueME =
2920 new (Context) MemberExpr(NSValueCallExpr, false, ARRFD,
2921 SourceLocation(),
2922 ARRFD->getType(), VK_LValue,
2923 OK_Ordinary);
2924 QualType ConstIdT = Context->getObjCIdType().withConst();
2925 CStyleCastExpr * DictValueObjects =
2926 NoTypeInfoCStyleCastExpr(Context,
2927 Context->getPointerType(ConstIdT),
2928 CK_BitCast,
2929 DictLiteralValueME);
2930 // (const id <NSCopying> [])keys
2931 Expr *NSKeyCallExpr =
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00002932 new (Context) CallExpr(*Context, NSDictDRE, KeyExprs,
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002933 NSDictFType, VK_LValue, SourceLocation());
2934
2935 MemberExpr *DictLiteralKeyME =
2936 new (Context) MemberExpr(NSKeyCallExpr, false, ARRFD,
2937 SourceLocation(),
2938 ARRFD->getType(), VK_LValue,
2939 OK_Ordinary);
2940
2941 CStyleCastExpr * DictKeyObjects =
2942 NoTypeInfoCStyleCastExpr(Context,
2943 Context->getPointerType(ConstIdT),
2944 CK_BitCast,
2945 DictLiteralKeyME);
2946
2947
2948
2949 // Synthesize a call to objc_msgSend().
2950 SmallVector<Expr*, 32> MsgExprs;
2951 SmallVector<Expr*, 4> ClsExprs;
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002952 QualType expType = Exp->getType();
2953
2954 // Create a call to objc_getClass("NSArray"). It will be th 1st argument.
2955 ObjCInterfaceDecl *Class =
2956 expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface();
2957
2958 IdentifierInfo *clsName = Class->getIdentifier();
Stephen Hines651f13c2014-04-23 16:59:28 -07002959 ClsExprs.push_back(getStringLiteral(clsName->getName()));
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002960 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2961 &ClsExprs[0],
2962 ClsExprs.size(),
2963 StartLoc, EndLoc);
2964 MsgExprs.push_back(Cls);
2965
2966 // Create a call to sel_registerName("arrayWithObjects:count:").
2967 // it will be the 2nd argument.
2968 SmallVector<Expr*, 4> SelExprs;
2969 ObjCMethodDecl *DictMethod = Exp->getDictWithObjectsMethod();
Stephen Hines651f13c2014-04-23 16:59:28 -07002970 SelExprs.push_back(getStringLiteral(DictMethod->getSelector().getAsString()));
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002971 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2972 &SelExprs[0], SelExprs.size(),
2973 StartLoc, EndLoc);
2974 MsgExprs.push_back(SelExp);
2975
2976 // (const id [])objects
2977 MsgExprs.push_back(DictValueObjects);
2978
2979 // (const id <NSCopying> [])keys
2980 MsgExprs.push_back(DictKeyObjects);
2981
2982 // (NSUInteger)cnt
2983 Expr *cnt = IntegerLiteral::Create(*Context,
2984 llvm::APInt(UnsignedIntSize, NumElements),
2985 Context->UnsignedIntTy, SourceLocation());
2986 MsgExprs.push_back(cnt);
2987
2988
2989 SmallVector<QualType, 8> ArgTypes;
2990 ArgTypes.push_back(Context->getObjCIdType());
2991 ArgTypes.push_back(Context->getObjCSelType());
Stephen Hines651f13c2014-04-23 16:59:28 -07002992 for (const auto *PI : DictMethod->params()) {
2993 QualType T = PI->getType();
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00002994 if (const PointerType* PT = T->getAs<PointerType>()) {
2995 QualType PointeeTy = PT->getPointeeType();
2996 convertToUnqualifiedObjCType(PointeeTy);
2997 T = Context->getPointerType(PointeeTy);
2998 }
2999 ArgTypes.push_back(T);
3000 }
3001
3002 QualType returnType = Exp->getType();
3003 // Get the type, we will need to reference it in a couple spots.
3004 QualType msgSendType = MsgSendFlavor->getType();
3005
3006 // Create a reference to the objc_msgSend() declaration.
3007 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
3008 VK_LValue, SourceLocation());
3009
3010 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context,
3011 Context->getPointerType(Context->VoidTy),
3012 CK_BitCast, DRE);
3013
3014 // Now do the "normal" pointer to function cast.
3015 QualType castType =
Jordan Rosebea522f2013-03-08 21:51:21 +00003016 getSimpleFunctionType(returnType, ArgTypes, DictMethod->isVariadic());
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00003017 castType = Context->getPointerType(castType);
3018 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3019 cast);
3020
3021 // Don't forget the parens to enforce the proper binding.
3022 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
3023
3024 const FunctionType *FT = msgSendType->getAs<FunctionType>();
Stephen Hines651f13c2014-04-23 16:59:28 -07003025 CallExpr *CE = new (Context)
3026 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00003027 ReplaceStmt(Exp, CE);
3028 return CE;
3029}
3030
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003031// struct __rw_objc_super {
3032// struct objc_object *object; struct objc_object *superClass;
3033// };
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003034QualType RewriteModernObjC::getSuperStructType() {
3035 if (!SuperStructDecl) {
3036 SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
3037 SourceLocation(), SourceLocation(),
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003038 &Context->Idents.get("__rw_objc_super"));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003039 QualType FieldTypes[2];
3040
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003041 // struct objc_object *object;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003042 FieldTypes[0] = Context->getObjCIdType();
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003043 // struct objc_object *superClass;
3044 FieldTypes[1] = Context->getObjCIdType();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003045
3046 // Create fields
3047 for (unsigned i = 0; i < 2; ++i) {
3048 SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl,
3049 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003050 SourceLocation(), nullptr,
3051 FieldTypes[i], nullptr,
3052 /*BitWidth=*/nullptr,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003053 /*Mutable=*/false,
Richard Smithca523302012-06-10 03:12:00 +00003054 ICIS_NoInit));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003055 }
3056
3057 SuperStructDecl->completeDefinition();
3058 }
3059 return Context->getTagDeclType(SuperStructDecl);
3060}
3061
3062QualType RewriteModernObjC::getConstantStringStructType() {
3063 if (!ConstantStringDecl) {
3064 ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
3065 SourceLocation(), SourceLocation(),
3066 &Context->Idents.get("__NSConstantStringImpl"));
3067 QualType FieldTypes[4];
3068
3069 // struct objc_object *receiver;
3070 FieldTypes[0] = Context->getObjCIdType();
3071 // int flags;
3072 FieldTypes[1] = Context->IntTy;
3073 // char *str;
3074 FieldTypes[2] = Context->getPointerType(Context->CharTy);
3075 // long length;
3076 FieldTypes[3] = Context->LongTy;
3077
3078 // Create fields
3079 for (unsigned i = 0; i < 4; ++i) {
3080 ConstantStringDecl->addDecl(FieldDecl::Create(*Context,
3081 ConstantStringDecl,
3082 SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003083 SourceLocation(), nullptr,
3084 FieldTypes[i], nullptr,
3085 /*BitWidth=*/nullptr,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003086 /*Mutable=*/true,
Richard Smithca523302012-06-10 03:12:00 +00003087 ICIS_NoInit));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003088 }
3089
3090 ConstantStringDecl->completeDefinition();
3091 }
3092 return Context->getTagDeclType(ConstantStringDecl);
3093}
3094
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003095/// getFunctionSourceLocation - returns start location of a function
3096/// definition. Complication arises when function has declared as
3097/// extern "C" or extern "C" {...}
3098static SourceLocation getFunctionSourceLocation (RewriteModernObjC &R,
3099 FunctionDecl *FD) {
3100 if (FD->isExternC() && !FD->isMain()) {
3101 const DeclContext *DC = FD->getDeclContext();
3102 if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC))
3103 // if it is extern "C" {...}, return function decl's own location.
3104 if (!LSD->getRBraceLoc().isValid())
3105 return LSD->getExternLoc();
3106 }
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003107 if (FD->getStorageClass() != SC_None)
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003108 R.RewriteBlockLiteralFunctionDecl(FD);
3109 return FD->getTypeSpecStartLoc();
3110}
3111
Fariborz Jahanian96205962012-11-06 17:30:23 +00003112void RewriteModernObjC::RewriteLineDirective(const Decl *D) {
3113
3114 SourceLocation Location = D->getLocation();
3115
Fariborz Jahanianada71912013-02-08 00:27:34 +00003116 if (Location.isFileID() && GenerateLineInfo) {
Fariborz Jahanian3b45ca92012-11-07 18:15:53 +00003117 std::string LineString("\n#line ");
Fariborz Jahanian96205962012-11-06 17:30:23 +00003118 PresumedLoc PLoc = SM->getPresumedLoc(Location);
3119 LineString += utostr(PLoc.getLine());
3120 LineString += " \"";
NAKAMURA Takumiba529a92012-11-06 22:45:31 +00003121 LineString += Lexer::Stringify(PLoc.getFilename());
Fariborz Jahanian96205962012-11-06 17:30:23 +00003122 if (isa<ObjCMethodDecl>(D))
3123 LineString += "\"";
3124 else LineString += "\"\n";
3125
3126 Location = D->getLocStart();
3127 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3128 if (FD->isExternC() && !FD->isMain()) {
3129 const DeclContext *DC = FD->getDeclContext();
3130 if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC))
3131 // if it is extern "C" {...}, return function decl's own location.
3132 if (!LSD->getRBraceLoc().isValid())
3133 Location = LSD->getExternLoc();
3134 }
3135 }
3136 InsertText(Location, LineString);
3137 }
3138}
3139
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003140/// SynthMsgSendStretCallExpr - This routine translates message expression
3141/// into a call to objc_msgSend_stret() entry point. Tricky part is that
3142/// nil check on receiver must be performed before calling objc_msgSend_stret.
3143/// MsgSendStretFlavor - function declaration objc_msgSend_stret(...)
3144/// msgSendType - function type of objc_msgSend_stret(...)
3145/// returnType - Result type of the method being synthesized.
3146/// ArgTypes - type of the arguments passed to objc_msgSend_stret, starting with receiver type.
3147/// MsgExprs - list of argument expressions being passed to objc_msgSend_stret,
3148/// starting with receiver.
3149/// Method - Method being rewritten.
3150Expr *RewriteModernObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor,
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003151 QualType returnType,
3152 SmallVectorImpl<QualType> &ArgTypes,
3153 SmallVectorImpl<Expr*> &MsgExprs,
3154 ObjCMethodDecl *Method) {
3155 // Now do the "normal" pointer to function cast.
Jordan Rosebea522f2013-03-08 21:51:21 +00003156 QualType castType = getSimpleFunctionType(returnType, ArgTypes,
3157 Method ? Method->isVariadic()
3158 : false);
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003159 castType = Context->getPointerType(castType);
3160
3161 // build type for containing the objc_msgSend_stret object.
3162 static unsigned stretCount=0;
3163 std::string name = "__Stret"; name += utostr(stretCount);
Fariborz Jahanian2ca5af22012-07-25 21:48:36 +00003164 std::string str =
3165 "extern \"C\" void * __cdecl memset(void *_Dst, int _Val, size_t _Size);\n";
Fariborz Jahanian426bb9c2013-09-09 19:59:59 +00003166 str += "namespace {\n";
Fariborz Jahanian2ca5af22012-07-25 21:48:36 +00003167 str += "struct "; str += name;
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003168 str += " {\n\t";
3169 str += name;
3170 str += "(id receiver, SEL sel";
3171 for (unsigned i = 2; i < ArgTypes.size(); i++) {
Fariborz Jahanian6734ec42012-06-29 19:55:46 +00003172 std::string ArgName = "arg"; ArgName += utostr(i);
3173 ArgTypes[i].getAsStringInternal(ArgName, Context->getPrintingPolicy());
3174 str += ", "; str += ArgName;
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003175 }
3176 // could be vararg.
3177 for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) {
Fariborz Jahanian6734ec42012-06-29 19:55:46 +00003178 std::string ArgName = "arg"; ArgName += utostr(i);
3179 MsgExprs[i]->getType().getAsStringInternal(ArgName,
3180 Context->getPrintingPolicy());
3181 str += ", "; str += ArgName;
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003182 }
3183
3184 str += ") {\n";
Fariborz Jahanian426bb9c2013-09-09 19:59:59 +00003185 str += "\t unsigned size = sizeof(";
3186 str += returnType.getAsString(Context->getPrintingPolicy()); str += ");\n";
3187
3188 str += "\t if (size == 1 || size == 2 || size == 4 || size == 8)\n";
3189
3190 str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy());
3191 str += ")(void *)objc_msgSend)(receiver, sel";
3192 for (unsigned i = 2; i < ArgTypes.size(); i++) {
3193 str += ", arg"; str += utostr(i);
3194 }
3195 // could be vararg.
3196 for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) {
3197 str += ", arg"; str += utostr(i);
3198 }
3199 str+= ");\n";
3200
3201 str += "\t else if (receiver == 0)\n";
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003202 str += "\t memset((void*)&s, 0, sizeof(s));\n";
3203 str += "\t else\n";
Fariborz Jahanian426bb9c2013-09-09 19:59:59 +00003204
3205
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003206 str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy());
3207 str += ")(void *)objc_msgSend_stret)(receiver, sel";
3208 for (unsigned i = 2; i < ArgTypes.size(); i++) {
3209 str += ", arg"; str += utostr(i);
3210 }
3211 // could be vararg.
3212 for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) {
3213 str += ", arg"; str += utostr(i);
3214 }
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003215 str += ");\n";
Fariborz Jahanian426bb9c2013-09-09 19:59:59 +00003216
3217
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003218 str += "\t}\n";
3219 str += "\t"; str += returnType.getAsString(Context->getPrintingPolicy());
3220 str += " s;\n";
Fariborz Jahanian426bb9c2013-09-09 19:59:59 +00003221 str += "};\n};\n\n";
Fariborz Jahaniana6e5a6e2012-08-21 18:56:50 +00003222 SourceLocation FunLocStart;
3223 if (CurFunctionDef)
3224 FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef);
3225 else {
3226 assert(CurMethodDef && "SynthMsgSendStretCallExpr - CurMethodDef is null");
3227 FunLocStart = CurMethodDef->getLocStart();
3228 }
3229
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003230 InsertText(FunLocStart, str);
3231 ++stretCount;
3232
3233 // AST for __Stretn(receiver, args).s;
3234 IdentifierInfo *ID = &Context->Idents.get(name);
3235 FunctionDecl *FD = FunctionDecl::Create(*Context, TUDecl, SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003236 SourceLocation(), ID, castType,
3237 nullptr, SC_Extern, false, false);
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003238 DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, castType, VK_RValue,
3239 SourceLocation());
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003240 CallExpr *STCE = new (Context) CallExpr(*Context, DRE, MsgExprs,
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003241 castType, VK_LValue, SourceLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003242
3243 FieldDecl *FieldD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003244 SourceLocation(),
3245 &Context->Idents.get("s"),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003246 returnType, nullptr,
3247 /*BitWidth=*/nullptr,
3248 /*Mutable=*/true, ICIS_NoInit);
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003249 MemberExpr *ME = new (Context) MemberExpr(STCE, false, FieldD, SourceLocation(),
3250 FieldD->getType(), VK_LValue,
3251 OK_Ordinary);
3252
3253 return ME;
3254}
3255
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003256Stmt *RewriteModernObjC::SynthMessageExpr(ObjCMessageExpr *Exp,
3257 SourceLocation StartLoc,
3258 SourceLocation EndLoc) {
3259 if (!SelGetUidFunctionDecl)
3260 SynthSelGetUidFunctionDecl();
3261 if (!MsgSendFunctionDecl)
3262 SynthMsgSendFunctionDecl();
3263 if (!MsgSendSuperFunctionDecl)
3264 SynthMsgSendSuperFunctionDecl();
3265 if (!MsgSendStretFunctionDecl)
3266 SynthMsgSendStretFunctionDecl();
3267 if (!MsgSendSuperStretFunctionDecl)
3268 SynthMsgSendSuperStretFunctionDecl();
3269 if (!MsgSendFpretFunctionDecl)
3270 SynthMsgSendFpretFunctionDecl();
3271 if (!GetClassFunctionDecl)
3272 SynthGetClassFunctionDecl();
3273 if (!GetSuperClassFunctionDecl)
3274 SynthGetSuperClassFunctionDecl();
3275 if (!GetMetaClassFunctionDecl)
3276 SynthGetMetaClassFunctionDecl();
3277
3278 // default to objc_msgSend().
3279 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
3280 // May need to use objc_msgSend_stret() as well.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003281 FunctionDecl *MsgSendStretFlavor = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003282 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003283 QualType resultType = mDecl->getReturnType();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003284 if (resultType->isRecordType())
3285 MsgSendStretFlavor = MsgSendStretFunctionDecl;
3286 else if (resultType->isRealFloatingType())
3287 MsgSendFlavor = MsgSendFpretFunctionDecl;
3288 }
3289
3290 // Synthesize a call to objc_msgSend().
3291 SmallVector<Expr*, 8> MsgExprs;
3292 switch (Exp->getReceiverKind()) {
3293 case ObjCMessageExpr::SuperClass: {
3294 MsgSendFlavor = MsgSendSuperFunctionDecl;
3295 if (MsgSendStretFlavor)
3296 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
3297 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
3298
3299 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
3300
3301 SmallVector<Expr*, 4> InitExprs;
3302
3303 // set the receiver to self, the first argument to all methods.
3304 InitExprs.push_back(
3305 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3306 CK_BitCast,
3307 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
John McCallf4b88a42012-03-10 09:33:50 +00003308 false,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003309 Context->getObjCIdType(),
3310 VK_RValue,
3311 SourceLocation()))
3312 ); // set the 'receiver'.
3313
3314 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3315 SmallVector<Expr*, 8> ClsExprs;
Stephen Hines651f13c2014-04-23 16:59:28 -07003316 ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName()));
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00003317 // (Class)objc_getClass("CurrentClass")
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003318 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
3319 &ClsExprs[0],
3320 ClsExprs.size(),
3321 StartLoc,
3322 EndLoc);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003323 ClsExprs.clear();
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00003324 ClsExprs.push_back(Cls);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003325 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
3326 &ClsExprs[0], ClsExprs.size(),
3327 StartLoc, EndLoc);
3328
3329 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3330 // To turn off a warning, type-cast to 'id'
3331 InitExprs.push_back( // set 'super class', using class_getSuperclass().
3332 NoTypeInfoCStyleCastExpr(Context,
3333 Context->getObjCIdType(),
3334 CK_BitCast, Cls));
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003335 // struct __rw_objc_super
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003336 QualType superType = getSuperStructType();
3337 Expr *SuperRep;
3338
3339 if (LangOpts.MicrosoftExt) {
Benjamin Kramere5753592013-09-09 14:48:42 +00003340 SynthSuperConstructorFunctionDecl();
3341 // Simulate a constructor call...
3342 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl,
John McCallf4b88a42012-03-10 09:33:50 +00003343 false, superType, VK_LValue,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003344 SourceLocation());
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003345 SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003346 superType, VK_LValue,
3347 SourceLocation());
3348 // The code for super is a little tricky to prevent collision with
3349 // the structure definition in the header. The rewriter has it's own
3350 // internal definition (__rw_objc_super) that is uses. This is why
3351 // we need the cast below. For example:
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003352 // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003353 //
3354 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
3355 Context->getPointerType(SuperRep->getType()),
3356 VK_RValue, OK_Ordinary,
3357 SourceLocation());
3358 SuperRep = NoTypeInfoCStyleCastExpr(Context,
3359 Context->getPointerType(superType),
3360 CK_BitCast, SuperRep);
3361 } else {
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003362 // (struct __rw_objc_super) { <exprs from above> }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003363 InitListExpr *ILE =
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003364 new (Context) InitListExpr(*Context, SourceLocation(), InitExprs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003365 SourceLocation());
3366 TypeSourceInfo *superTInfo
3367 = Context->getTrivialTypeSourceInfo(superType);
3368 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
3369 superType, VK_LValue,
3370 ILE, false);
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003371 // struct __rw_objc_super *
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003372 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
3373 Context->getPointerType(SuperRep->getType()),
3374 VK_RValue, OK_Ordinary,
3375 SourceLocation());
3376 }
3377 MsgExprs.push_back(SuperRep);
3378 break;
3379 }
3380
3381 case ObjCMessageExpr::Class: {
3382 SmallVector<Expr*, 8> ClsExprs;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003383 ObjCInterfaceDecl *Class
3384 = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface();
3385 IdentifierInfo *clsName = Class->getIdentifier();
Stephen Hines651f13c2014-04-23 16:59:28 -07003386 ClsExprs.push_back(getStringLiteral(clsName->getName()));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003387 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
3388 &ClsExprs[0],
3389 ClsExprs.size(),
3390 StartLoc, EndLoc);
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00003391 CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context,
3392 Context->getObjCIdType(),
3393 CK_BitCast, Cls);
3394 MsgExprs.push_back(ArgExpr);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003395 break;
3396 }
3397
3398 case ObjCMessageExpr::SuperInstance:{
3399 MsgSendFlavor = MsgSendSuperFunctionDecl;
3400 if (MsgSendStretFlavor)
3401 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
3402 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
3403 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
3404 SmallVector<Expr*, 4> InitExprs;
3405
3406 InitExprs.push_back(
3407 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3408 CK_BitCast,
3409 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
John McCallf4b88a42012-03-10 09:33:50 +00003410 false,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003411 Context->getObjCIdType(),
3412 VK_RValue, SourceLocation()))
3413 ); // set the 'receiver'.
3414
3415 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3416 SmallVector<Expr*, 8> ClsExprs;
Stephen Hines651f13c2014-04-23 16:59:28 -07003417 ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName()));
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00003418 // (Class)objc_getClass("CurrentClass")
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003419 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
3420 &ClsExprs[0],
3421 ClsExprs.size(),
3422 StartLoc, EndLoc);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003423 ClsExprs.clear();
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00003424 ClsExprs.push_back(Cls);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003425 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
3426 &ClsExprs[0], ClsExprs.size(),
3427 StartLoc, EndLoc);
3428
3429 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3430 // To turn off a warning, type-cast to 'id'
3431 InitExprs.push_back(
3432 // set 'super class', using class_getSuperclass().
3433 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3434 CK_BitCast, Cls));
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003435 // struct __rw_objc_super
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003436 QualType superType = getSuperStructType();
3437 Expr *SuperRep;
3438
3439 if (LangOpts.MicrosoftExt) {
Benjamin Kramere5753592013-09-09 14:48:42 +00003440 SynthSuperConstructorFunctionDecl();
3441 // Simulate a constructor call...
3442 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl,
John McCallf4b88a42012-03-10 09:33:50 +00003443 false, superType, VK_LValue,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003444 SourceLocation());
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003445 SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003446 superType, VK_LValue, SourceLocation());
3447 // The code for super is a little tricky to prevent collision with
3448 // the structure definition in the header. The rewriter has it's own
3449 // internal definition (__rw_objc_super) that is uses. This is why
3450 // we need the cast below. For example:
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003451 // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003452 //
3453 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
3454 Context->getPointerType(SuperRep->getType()),
3455 VK_RValue, OK_Ordinary,
3456 SourceLocation());
3457 SuperRep = NoTypeInfoCStyleCastExpr(Context,
3458 Context->getPointerType(superType),
3459 CK_BitCast, SuperRep);
3460 } else {
Fariborz Jahanianb20c46e2012-04-13 16:20:05 +00003461 // (struct __rw_objc_super) { <exprs from above> }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003462 InitListExpr *ILE =
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003463 new (Context) InitListExpr(*Context, SourceLocation(), InitExprs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003464 SourceLocation());
3465 TypeSourceInfo *superTInfo
3466 = Context->getTrivialTypeSourceInfo(superType);
3467 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
3468 superType, VK_RValue, ILE,
3469 false);
3470 }
3471 MsgExprs.push_back(SuperRep);
3472 break;
3473 }
3474
3475 case ObjCMessageExpr::Instance: {
3476 // Remove all type-casts because it may contain objc-style types; e.g.
3477 // Foo<Proto> *.
3478 Expr *recExpr = Exp->getInstanceReceiver();
3479 while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr))
3480 recExpr = CE->getSubExpr();
3481 CastKind CK = recExpr->getType()->isObjCObjectPointerType()
3482 ? CK_BitCast : recExpr->getType()->isBlockPointerType()
3483 ? CK_BlockPointerToObjCPointerCast
3484 : CK_CPointerToObjCPointerCast;
3485
3486 recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3487 CK, recExpr);
3488 MsgExprs.push_back(recExpr);
3489 break;
3490 }
3491 }
3492
3493 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
3494 SmallVector<Expr*, 8> SelExprs;
Stephen Hines651f13c2014-04-23 16:59:28 -07003495 SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString()));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003496 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
3497 &SelExprs[0], SelExprs.size(),
3498 StartLoc,
3499 EndLoc);
3500 MsgExprs.push_back(SelExp);
3501
3502 // Now push any user supplied arguments.
3503 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
3504 Expr *userExpr = Exp->getArg(i);
3505 // Make all implicit casts explicit...ICE comes in handy:-)
3506 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
3507 // Reuse the ICE type, it is exactly what the doctor ordered.
3508 QualType type = ICE->getType();
3509 if (needToScanForQualifiers(type))
3510 type = Context->getObjCIdType();
3511 // Make sure we convert "type (^)(...)" to "type (*)(...)".
3512 (void)convertBlockPointerToFunctionPointer(type);
3513 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
3514 CastKind CK;
3515 if (SubExpr->getType()->isIntegralType(*Context) &&
3516 type->isBooleanType()) {
3517 CK = CK_IntegralToBoolean;
3518 } else if (type->isObjCObjectPointerType()) {
3519 if (SubExpr->getType()->isBlockPointerType()) {
3520 CK = CK_BlockPointerToObjCPointerCast;
3521 } else if (SubExpr->getType()->isPointerType()) {
3522 CK = CK_CPointerToObjCPointerCast;
3523 } else {
3524 CK = CK_BitCast;
3525 }
3526 } else {
3527 CK = CK_BitCast;
3528 }
3529
3530 userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr);
3531 }
3532 // Make id<P...> cast into an 'id' cast.
3533 else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) {
3534 if (CE->getType()->isObjCQualifiedIdType()) {
3535 while ((CE = dyn_cast<CStyleCastExpr>(userExpr)))
3536 userExpr = CE->getSubExpr();
3537 CastKind CK;
3538 if (userExpr->getType()->isIntegralType(*Context)) {
3539 CK = CK_IntegralToPointer;
3540 } else if (userExpr->getType()->isBlockPointerType()) {
3541 CK = CK_BlockPointerToObjCPointerCast;
3542 } else if (userExpr->getType()->isPointerType()) {
3543 CK = CK_CPointerToObjCPointerCast;
3544 } else {
3545 CK = CK_BitCast;
3546 }
3547 userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3548 CK, userExpr);
3549 }
3550 }
3551 MsgExprs.push_back(userExpr);
3552 // We've transferred the ownership to MsgExprs. For now, we *don't* null
3553 // out the argument in the original expression (since we aren't deleting
3554 // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info.
3555 //Exp->setArg(i, 0);
3556 }
3557 // Generate the funky cast.
3558 CastExpr *cast;
3559 SmallVector<QualType, 8> ArgTypes;
3560 QualType returnType;
3561
3562 // Push 'id' and 'SEL', the 2 implicit arguments.
3563 if (MsgSendFlavor == MsgSendSuperFunctionDecl)
3564 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
3565 else
3566 ArgTypes.push_back(Context->getObjCIdType());
3567 ArgTypes.push_back(Context->getObjCSelType());
3568 if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) {
3569 // Push any user argument types.
Stephen Hines651f13c2014-04-23 16:59:28 -07003570 for (const auto *PI : OMD->params()) {
3571 QualType t = PI->getType()->isObjCQualifiedIdType()
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003572 ? Context->getObjCIdType()
Stephen Hines651f13c2014-04-23 16:59:28 -07003573 : PI->getType();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003574 // Make sure we convert "t (^)(...)" to "t (*)(...)".
3575 (void)convertBlockPointerToFunctionPointer(t);
3576 ArgTypes.push_back(t);
3577 }
3578 returnType = Exp->getType();
3579 convertToUnqualifiedObjCType(returnType);
3580 (void)convertBlockPointerToFunctionPointer(returnType);
3581 } else {
3582 returnType = Context->getObjCIdType();
3583 }
3584 // Get the type, we will need to reference it in a couple spots.
3585 QualType msgSendType = MsgSendFlavor->getType();
3586
3587 // Create a reference to the objc_msgSend() declaration.
John McCallf4b88a42012-03-10 09:33:50 +00003588 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003589 VK_LValue, SourceLocation());
3590
3591 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
3592 // If we don't do this cast, we get the following bizarre warning/note:
3593 // xx.m:13: warning: function called through a non-compatible type
3594 // xx.m:13: note: if this code is reached, the program will abort
3595 cast = NoTypeInfoCStyleCastExpr(Context,
3596 Context->getPointerType(Context->VoidTy),
3597 CK_BitCast, DRE);
3598
3599 // Now do the "normal" pointer to function cast.
Jordan Rosebea522f2013-03-08 21:51:21 +00003600 // If we don't have a method decl, force a variadic cast.
3601 const ObjCMethodDecl *MD = Exp->getMethodDecl();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003602 QualType castType =
Jordan Rosebea522f2013-03-08 21:51:21 +00003603 getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003604 castType = Context->getPointerType(castType);
3605 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3606 cast);
3607
3608 // Don't forget the parens to enforce the proper binding.
3609 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
3610
3611 const FunctionType *FT = msgSendType->getAs<FunctionType>();
Stephen Hines651f13c2014-04-23 16:59:28 -07003612 CallExpr *CE = new (Context)
3613 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003614 Stmt *ReplacingStmt = CE;
3615 if (MsgSendStretFlavor) {
3616 // We have the method which returns a struct/union. Must also generate
3617 // call to objc_msgSend_stret and hang both varieties on a conditional
3618 // expression which dictate which one to envoke depending on size of
3619 // method's return type.
3620
Fariborz Jahanian426bb9c2013-09-09 19:59:59 +00003621 Expr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor,
3622 returnType,
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00003623 ArgTypes, MsgExprs,
3624 Exp->getMethodDecl());
Fariborz Jahanian426bb9c2013-09-09 19:59:59 +00003625 ReplacingStmt = STCE;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003626 }
3627 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3628 return ReplacingStmt;
3629}
3630
3631Stmt *RewriteModernObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) {
3632 Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(),
3633 Exp->getLocEnd());
3634
3635 // Now do the actual rewrite.
3636 ReplaceStmt(Exp, ReplacingStmt);
3637
3638 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3639 return ReplacingStmt;
3640}
3641
3642// typedef struct objc_object Protocol;
3643QualType RewriteModernObjC::getProtocolType() {
3644 if (!ProtocolTypeDecl) {
3645 TypeSourceInfo *TInfo
3646 = Context->getTrivialTypeSourceInfo(Context->getObjCIdType());
3647 ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl,
3648 SourceLocation(), SourceLocation(),
3649 &Context->Idents.get("Protocol"),
3650 TInfo);
3651 }
3652 return Context->getTypeDeclType(ProtocolTypeDecl);
3653}
3654
3655/// RewriteObjCProtocolExpr - Rewrite a protocol expression into
3656/// a synthesized/forward data reference (to the protocol's metadata).
3657/// The forward references (and metadata) are generated in
3658/// RewriteModernObjC::HandleTranslationUnit().
3659Stmt *RewriteModernObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00003660 std::string Name = "_OBJC_PROTOCOL_REFERENCE_$_" +
3661 Exp->getProtocol()->getNameAsString();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003662 IdentifierInfo *ID = &Context->Idents.get(Name);
3663 VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003664 SourceLocation(), ID, getProtocolType(),
3665 nullptr, SC_Extern);
John McCallf4b88a42012-03-10 09:33:50 +00003666 DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(),
3667 VK_LValue, SourceLocation());
Stephen Hines651f13c2014-04-23 16:59:28 -07003668 CastExpr *castExpr =
3669 NoTypeInfoCStyleCastExpr(
3670 Context, Context->getPointerType(DRE->getType()), CK_BitCast, DRE);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003671 ReplaceStmt(Exp, castExpr);
3672 ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl());
3673 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3674 return castExpr;
3675
3676}
3677
3678bool RewriteModernObjC::BufferContainsPPDirectives(const char *startBuf,
3679 const char *endBuf) {
3680 while (startBuf < endBuf) {
3681 if (*startBuf == '#') {
3682 // Skip whitespace.
3683 for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf)
3684 ;
3685 if (!strncmp(startBuf, "if", strlen("if")) ||
3686 !strncmp(startBuf, "ifdef", strlen("ifdef")) ||
3687 !strncmp(startBuf, "ifndef", strlen("ifndef")) ||
3688 !strncmp(startBuf, "define", strlen("define")) ||
3689 !strncmp(startBuf, "undef", strlen("undef")) ||
3690 !strncmp(startBuf, "else", strlen("else")) ||
3691 !strncmp(startBuf, "elif", strlen("elif")) ||
3692 !strncmp(startBuf, "endif", strlen("endif")) ||
3693 !strncmp(startBuf, "pragma", strlen("pragma")) ||
3694 !strncmp(startBuf, "include", strlen("include")) ||
3695 !strncmp(startBuf, "import", strlen("import")) ||
3696 !strncmp(startBuf, "include_next", strlen("include_next")))
3697 return true;
3698 }
3699 startBuf++;
3700 }
3701 return false;
3702}
3703
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003704/// IsTagDefinedInsideClass - This routine checks that a named tagged type
3705/// is defined inside an objective-c class. If so, it returns true.
Fariborz Jahanianb68258f2012-05-01 17:46:45 +00003706bool RewriteModernObjC::IsTagDefinedInsideClass(ObjCContainerDecl *IDecl,
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003707 TagDecl *Tag,
3708 bool &IsNamedDefinition) {
Fariborz Jahanian89585e802012-04-30 19:46:53 +00003709 if (!IDecl)
3710 return false;
3711 SourceLocation TagLocation;
3712 if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) {
3713 RD = RD->getDefinition();
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003714 if (!RD || !RD->getDeclName().getAsIdentifierInfo())
Fariborz Jahanian89585e802012-04-30 19:46:53 +00003715 return false;
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003716 IsNamedDefinition = true;
Fariborz Jahanian89585e802012-04-30 19:46:53 +00003717 TagLocation = RD->getLocation();
3718 return Context->getSourceManager().isBeforeInTranslationUnit(
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003719 IDecl->getLocation(), TagLocation);
3720 }
3721 if (EnumDecl *ED = dyn_cast<EnumDecl>(Tag)) {
3722 if (!ED || !ED->getDeclName().getAsIdentifierInfo())
3723 return false;
3724 IsNamedDefinition = true;
3725 TagLocation = ED->getLocation();
3726 return Context->getSourceManager().isBeforeInTranslationUnit(
3727 IDecl->getLocation(), TagLocation);
3728
Fariborz Jahanian89585e802012-04-30 19:46:53 +00003729 }
3730 return false;
3731}
3732
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003733/// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer.
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003734/// It handles elaborated types, as well as enum types in the process.
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003735bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type,
3736 std::string &Result) {
Fariborz Jahanianf5eac482012-05-02 17:34:59 +00003737 if (isa<TypedefType>(Type)) {
3738 Result += "\t";
3739 return false;
3740 }
3741
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003742 if (Type->isArrayType()) {
3743 QualType ElemTy = Context->getBaseElementType(Type);
3744 return RewriteObjCFieldDeclType(ElemTy, Result);
3745 }
3746 else if (Type->isRecordType()) {
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003747 RecordDecl *RD = Type->getAs<RecordType>()->getDecl();
3748 if (RD->isCompleteDefinition()) {
3749 if (RD->isStruct())
3750 Result += "\n\tstruct ";
3751 else if (RD->isUnion())
3752 Result += "\n\tunion ";
3753 else
3754 assert(false && "class not allowed as an ivar type");
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003755
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003756 Result += RD->getName();
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003757 if (GlobalDefinedTags.count(RD)) {
3758 // struct/union is defined globally, use it.
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003759 Result += " ";
3760 return true;
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003761 }
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003762 Result += " {\n";
Stephen Hines651f13c2014-04-23 16:59:28 -07003763 for (auto *FD : RD->fields())
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003764 RewriteObjCFieldDecl(FD, Result);
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003765 Result += "\t} ";
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003766 return true;
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003767 }
3768 }
3769 else if (Type->isEnumeralType()) {
3770 EnumDecl *ED = Type->getAs<EnumType>()->getDecl();
3771 if (ED->isCompleteDefinition()) {
3772 Result += "\n\tenum ";
3773 Result += ED->getName();
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003774 if (GlobalDefinedTags.count(ED)) {
3775 // Enum is globall defined, use it.
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003776 Result += " ";
3777 return true;
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003778 }
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003779
3780 Result += " {\n";
Stephen Hines651f13c2014-04-23 16:59:28 -07003781 for (const auto *EC : ED->enumerators()) {
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003782 Result += "\t"; Result += EC->getName(); Result += " = ";
3783 llvm::APSInt Val = EC->getInitVal();
3784 Result += Val.toString(10);
3785 Result += ",\n";
3786 }
3787 Result += "\t} ";
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003788 return true;
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003789 }
3790 }
3791
3792 Result += "\t";
3793 convertObjCTypeToCStyleType(Type);
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003794 return false;
3795}
3796
3797
3798/// RewriteObjCFieldDecl - This routine rewrites a field into the buffer.
3799/// It handles elaborated types, as well as enum types in the process.
3800void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl,
3801 std::string &Result) {
3802 QualType Type = fieldDecl->getType();
3803 std::string Name = fieldDecl->getNameAsString();
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003804
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003805 bool EleboratedType = RewriteObjCFieldDeclType(Type, Result);
3806 if (!EleboratedType)
3807 Type.getAsStringInternal(Name, Context->getPrintingPolicy());
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003808 Result += Name;
3809 if (fieldDecl->isBitField()) {
3810 Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context));
3811 }
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003812 else if (EleboratedType && Type->isArrayType()) {
Eli Friedman6febf122012-12-13 01:43:21 +00003813 const ArrayType *AT = Context->getAsArrayType(Type);
3814 do {
3815 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003816 Result += "[";
3817 llvm::APInt Dim = CAT->getSize();
3818 Result += utostr(Dim.getZExtValue());
3819 Result += "]";
3820 }
Eli Friedman6febf122012-12-13 01:43:21 +00003821 AT = Context->getAsArrayType(AT->getElementType());
3822 } while (AT);
Fariborz Jahanian97c1fd62012-03-09 23:46:23 +00003823 }
3824
Fariborz Jahanian15f87772012-02-28 22:45:07 +00003825 Result += ";\n";
3826}
3827
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003828/// RewriteLocallyDefinedNamedAggregates - This routine rewrites locally defined
3829/// named aggregate types into the input buffer.
3830void RewriteModernObjC::RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl,
3831 std::string &Result) {
3832 QualType Type = fieldDecl->getType();
Fariborz Jahanianf5eac482012-05-02 17:34:59 +00003833 if (isa<TypedefType>(Type))
3834 return;
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003835 if (Type->isArrayType())
3836 Type = Context->getBaseElementType(Type);
Fariborz Jahanianb68258f2012-05-01 17:46:45 +00003837 ObjCContainerDecl *IDecl =
3838 dyn_cast<ObjCContainerDecl>(fieldDecl->getDeclContext());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003839
3840 TagDecl *TD = nullptr;
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00003841 if (Type->isRecordType()) {
3842 TD = Type->getAs<RecordType>()->getDecl();
3843 }
3844 else if (Type->isEnumeralType()) {
3845 TD = Type->getAs<EnumType>()->getDecl();
3846 }
3847
3848 if (TD) {
3849 if (GlobalDefinedTags.count(TD))
3850 return;
3851
3852 bool IsNamedDefinition = false;
3853 if (IsTagDefinedInsideClass(IDecl, TD, IsNamedDefinition)) {
3854 RewriteObjCFieldDeclType(Type, Result);
3855 Result += ";";
3856 }
3857 if (IsNamedDefinition)
3858 GlobalDefinedTags.insert(TD);
3859 }
3860
3861}
3862
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00003863unsigned RewriteModernObjC::ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV) {
3864 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3865 if (ObjCInterefaceHasBitfieldGroups.count(CDecl)) {
3866 return IvarGroupNumber[IV];
3867 }
3868 unsigned GroupNo = 0;
3869 SmallVector<const ObjCIvarDecl *, 8> IVars;
3870 for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3871 IVD; IVD = IVD->getNextIvar())
3872 IVars.push_back(IVD);
3873
3874 for (unsigned i = 0, e = IVars.size(); i < e; i++)
3875 if (IVars[i]->isBitField()) {
3876 IvarGroupNumber[IVars[i++]] = ++GroupNo;
3877 while (i < e && IVars[i]->isBitField())
3878 IvarGroupNumber[IVars[i++]] = GroupNo;
3879 if (i < e)
3880 --i;
3881 }
3882
3883 ObjCInterefaceHasBitfieldGroups.insert(CDecl);
3884 return IvarGroupNumber[IV];
3885}
3886
3887QualType RewriteModernObjC::SynthesizeBitfieldGroupStructType(
3888 ObjCIvarDecl *IV,
3889 SmallVectorImpl<ObjCIvarDecl *> &IVars) {
3890 std::string StructTagName;
3891 ObjCIvarBitfieldGroupType(IV, StructTagName);
3892 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct,
3893 Context->getTranslationUnitDecl(),
3894 SourceLocation(), SourceLocation(),
3895 &Context->Idents.get(StructTagName));
3896 for (unsigned i=0, e = IVars.size(); i < e; i++) {
3897 ObjCIvarDecl *Ivar = IVars[i];
3898 RD->addDecl(FieldDecl::Create(*Context, RD, SourceLocation(), SourceLocation(),
3899 &Context->Idents.get(Ivar->getName()),
3900 Ivar->getType(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003901 nullptr, /*Expr *BW */Ivar->getBitWidth(),
3902 false, ICIS_NoInit));
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00003903 }
3904 RD->completeDefinition();
3905 return Context->getTagDeclType(RD);
3906}
3907
3908QualType RewriteModernObjC::GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV) {
3909 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3910 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV);
3911 std::pair<const ObjCInterfaceDecl*, unsigned> tuple = std::make_pair(CDecl, GroupNo);
3912 if (GroupRecordType.count(tuple))
3913 return GroupRecordType[tuple];
3914
3915 SmallVector<ObjCIvarDecl *, 8> IVars;
3916 for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3917 IVD; IVD = IVD->getNextIvar()) {
3918 if (IVD->isBitField())
3919 IVars.push_back(const_cast<ObjCIvarDecl *>(IVD));
3920 else {
3921 if (!IVars.empty()) {
3922 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]);
3923 // Generate the struct type for this group of bitfield ivars.
3924 GroupRecordType[std::make_pair(CDecl, GroupNo)] =
3925 SynthesizeBitfieldGroupStructType(IVars[0], IVars);
3926 IVars.clear();
3927 }
3928 }
3929 }
3930 if (!IVars.empty()) {
3931 // Do the last one.
3932 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]);
3933 GroupRecordType[std::make_pair(CDecl, GroupNo)] =
3934 SynthesizeBitfieldGroupStructType(IVars[0], IVars);
3935 }
3936 QualType RetQT = GroupRecordType[tuple];
3937 assert(!RetQT.isNull() && "GetGroupRecordTypeForObjCIvarBitfield struct type is NULL");
3938
3939 return RetQT;
3940}
3941
3942/// ObjCIvarBitfieldGroupDecl - Names field decl. for ivar bitfield group.
3943/// Name would be: classname__GRBF_n where n is the group number for this ivar.
3944void RewriteModernObjC::ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV,
3945 std::string &Result) {
3946 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3947 Result += CDecl->getName();
3948 Result += "__GRBF_";
3949 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV);
3950 Result += utostr(GroupNo);
3951 return;
3952}
3953
3954/// ObjCIvarBitfieldGroupType - Names struct type for ivar bitfield group.
3955/// Name of the struct would be: classname__T_n where n is the group number for
3956/// this ivar.
3957void RewriteModernObjC::ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV,
3958 std::string &Result) {
3959 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3960 Result += CDecl->getName();
3961 Result += "__T_";
3962 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV);
3963 Result += utostr(GroupNo);
3964 return;
3965}
3966
3967/// ObjCIvarBitfieldGroupOffset - Names symbol for ivar bitfield group field offset.
3968/// Name would be: OBJC_IVAR_$_classname__GRBF_n where n is the group number for
3969/// this ivar.
3970void RewriteModernObjC::ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV,
3971 std::string &Result) {
3972 Result += "OBJC_IVAR_$_";
3973 ObjCIvarBitfieldGroupDecl(IV, Result);
3974}
3975
3976#define SKIP_BITFIELDS(IX, ENDIX, VEC) { \
3977 while ((IX < ENDIX) && VEC[IX]->isBitField()) \
3978 ++IX; \
3979 if (IX < ENDIX) \
3980 --IX; \
3981}
3982
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003983/// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to
3984/// an objective-c class with ivars.
3985void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl,
3986 std::string &Result) {
3987 assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
3988 assert(CDecl->getName() != "" &&
3989 "Name missing in SynthesizeObjCInternalStruct");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003990 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00003991 SmallVector<ObjCIvarDecl *, 8> IVars;
3992 for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
Fariborz Jahanian9a2105b2012-03-06 17:16:27 +00003993 IVD; IVD = IVD->getNextIvar())
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00003994 IVars.push_back(IVD);
Fariborz Jahanian9a2105b2012-03-06 17:16:27 +00003995
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003996 SourceLocation LocStart = CDecl->getLocStart();
3997 SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc();
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00003998
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00003999 const char *startBuf = SM->getCharacterData(LocStart);
4000 const char *endBuf = SM->getCharacterData(LocEnd);
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00004001
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004002 // If no ivars and no root or if its root, directly or indirectly,
4003 // have no ivars (thus not synthesized) then no need to synthesize this class.
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00004004 if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) &&
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004005 (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
4006 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
4007 ReplaceText(LocStart, endBuf-startBuf, Result);
4008 return;
4009 }
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00004010
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00004011 // Insert named struct/union definitions inside class to
4012 // outer scope. This follows semantics of locally defined
4013 // struct/unions in objective-c classes.
4014 for (unsigned i = 0, e = IVars.size(); i < e; i++)
4015 RewriteLocallyDefinedNamedAggregates(IVars[i], Result);
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00004016
4017 // Insert named structs which are syntheized to group ivar bitfields
4018 // to outer scope as well.
4019 for (unsigned i = 0, e = IVars.size(); i < e; i++)
4020 if (IVars[i]->isBitField()) {
4021 ObjCIvarDecl *IV = IVars[i];
4022 QualType QT = GetGroupRecordTypeForObjCIvarBitfield(IV);
4023 RewriteObjCFieldDeclType(QT, Result);
4024 Result += ";";
4025 // skip over ivar bitfields in this group.
4026 SKIP_BITFIELDS(i , e, IVars);
4027 }
4028
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004029 Result += "\nstruct ";
4030 Result += CDecl->getNameAsString();
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00004031 Result += "_IMPL {\n";
4032
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00004033 if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00004034 Result += "\tstruct "; Result += RCDecl->getNameAsString();
4035 Result += "_IMPL "; Result += RCDecl->getNameAsString();
4036 Result += "_IVARS;\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004037 }
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00004038
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00004039 for (unsigned i = 0, e = IVars.size(); i < e; i++) {
4040 if (IVars[i]->isBitField()) {
4041 ObjCIvarDecl *IV = IVars[i];
4042 Result += "\tstruct ";
4043 ObjCIvarBitfieldGroupType(IV, Result); Result += " ";
4044 ObjCIvarBitfieldGroupDecl(IV, Result); Result += ";\n";
4045 // skip over ivar bitfields in this group.
4046 SKIP_BITFIELDS(i , e, IVars);
4047 }
4048 else
4049 RewriteObjCFieldDecl(IVars[i], Result);
4050 }
Fariborz Jahanian0b17b9a2012-02-12 21:36:23 +00004051
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00004052 Result += "};\n";
4053 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
4054 ReplaceText(LocStart, endBuf-startBuf, Result);
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00004055 // Mark this struct as having been generated.
4056 if (!ObjCSynthesizedStructs.insert(CDecl))
4057 llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004058}
4059
Fariborz Jahanian72c88f12012-02-22 18:13:25 +00004060/// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which
4061/// have been referenced in an ivar access expression.
4062void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl,
4063 std::string &Result) {
4064 // write out ivar offset symbols which have been referenced in an ivar
4065 // access expression.
4066 llvm::SmallPtrSet<ObjCIvarDecl *, 8> Ivars = ReferencedIvars[CDecl];
4067 if (Ivars.empty())
4068 return;
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00004069
4070 llvm::DenseSet<std::pair<const ObjCInterfaceDecl*, unsigned> > GroupSymbolOutput;
Fariborz Jahanian72c88f12012-02-22 18:13:25 +00004071 for (llvm::SmallPtrSet<ObjCIvarDecl *, 8>::iterator i = Ivars.begin(),
4072 e = Ivars.end(); i != e; i++) {
4073 ObjCIvarDecl *IvarDecl = (*i);
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00004074 const ObjCInterfaceDecl *IDecl = IvarDecl->getContainingInterface();
4075 unsigned GroupNo = 0;
4076 if (IvarDecl->isBitField()) {
4077 GroupNo = ObjCIvarBitfieldGroupNo(IvarDecl);
4078 if (GroupSymbolOutput.count(std::make_pair(IDecl, GroupNo)))
4079 continue;
4080 }
Fariborz Jahanian40a777a2012-03-12 16:46:58 +00004081 Result += "\n";
4082 if (LangOpts.MicrosoftExt)
4083 Result += "__declspec(allocate(\".objc_ivar$B\")) ";
Fariborz Jahanian297976d2012-03-29 17:51:09 +00004084 Result += "extern \"C\" ";
Fariborz Jahanian40a777a2012-03-12 16:46:58 +00004085 if (LangOpts.MicrosoftExt &&
4086 IvarDecl->getAccessControl() != ObjCIvarDecl::Private &&
Fariborz Jahanian297976d2012-03-29 17:51:09 +00004087 IvarDecl->getAccessControl() != ObjCIvarDecl::Package)
4088 Result += "__declspec(dllimport) ";
4089
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00004090 Result += "unsigned long ";
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00004091 if (IvarDecl->isBitField()) {
4092 ObjCIvarBitfieldGroupOffset(IvarDecl, Result);
4093 GroupSymbolOutput.insert(std::make_pair(IDecl, GroupNo));
4094 }
4095 else
4096 WriteInternalIvarName(CDecl, IvarDecl, Result);
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00004097 Result += ";";
Fariborz Jahanian72c88f12012-02-22 18:13:25 +00004098 }
4099}
4100
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004101//===----------------------------------------------------------------------===//
4102// Meta Data Emission
4103//===----------------------------------------------------------------------===//
4104
4105
4106/// RewriteImplementations - This routine rewrites all method implementations
4107/// and emits meta-data.
4108
4109void RewriteModernObjC::RewriteImplementations() {
4110 int ClsDefCount = ClassImplementation.size();
4111 int CatDefCount = CategoryImplementation.size();
4112
4113 // Rewrite implemented methods
Fariborz Jahanian8f1fed02012-02-11 20:10:52 +00004114 for (int i = 0; i < ClsDefCount; i++) {
4115 ObjCImplementationDecl *OIMP = ClassImplementation[i];
4116 ObjCInterfaceDecl *CDecl = OIMP->getClassInterface();
4117 if (CDecl->isImplicitInterfaceDecl())
Fariborz Jahaniancf4c60f2012-02-17 22:20:12 +00004118 assert(false &&
4119 "Legacy implicit interface rewriting not supported in moder abi");
Fariborz Jahanian8f1fed02012-02-11 20:10:52 +00004120 RewriteImplementationDecl(OIMP);
4121 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004122
Fariborz Jahanian8c00a1b2012-02-17 20:33:00 +00004123 for (int i = 0; i < CatDefCount; i++) {
4124 ObjCCategoryImplDecl *CIMP = CategoryImplementation[i];
4125 ObjCInterfaceDecl *CDecl = CIMP->getClassInterface();
4126 if (CDecl->isImplicitInterfaceDecl())
4127 assert(false &&
4128 "Legacy implicit interface rewriting not supported in moder abi");
Fariborz Jahanian8c00a1b2012-02-17 20:33:00 +00004129 RewriteImplementationDecl(CIMP);
4130 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004131}
4132
4133void RewriteModernObjC::RewriteByRefString(std::string &ResultStr,
4134 const std::string &Name,
4135 ValueDecl *VD, bool def) {
4136 assert(BlockByRefDeclNo.count(VD) &&
4137 "RewriteByRefString: ByRef decl missing");
4138 if (def)
4139 ResultStr += "struct ";
4140 ResultStr += "__Block_byref_" + Name +
4141 "_" + utostr(BlockByRefDeclNo[VD]) ;
4142}
4143
4144static bool HasLocalVariableExternalStorage(ValueDecl *VD) {
4145 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
4146 return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage());
4147 return false;
4148}
4149
4150std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
4151 StringRef funcName,
4152 std::string Tag) {
4153 const FunctionType *AFT = CE->getFunctionType();
Stephen Hines651f13c2014-04-23 16:59:28 -07004154 QualType RT = AFT->getReturnType();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004155 std::string StructRef = "struct " + Tag;
Fariborz Jahanianf616ae22012-11-06 23:25:49 +00004156 SourceLocation BlockLoc = CE->getExprLoc();
4157 std::string S;
4158 ConvertSourceLocationToLineDirective(BlockLoc, S);
4159
4160 S += "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" +
4161 funcName.str() + "_block_func_" + utostr(i);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004162
4163 BlockDecl *BD = CE->getBlockDecl();
4164
4165 if (isa<FunctionNoProtoType>(AFT)) {
4166 // No user-supplied arguments. Still need to pass in a pointer to the
4167 // block (to reference imported block decl refs).
4168 S += "(" + StructRef + " *__cself)";
4169 } else if (BD->param_empty()) {
4170 S += "(" + StructRef + " *__cself)";
4171 } else {
4172 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
4173 assert(FT && "SynthesizeBlockFunc: No function proto");
4174 S += '(';
4175 // first add the implicit argument.
4176 S += StructRef + " *__cself, ";
4177 std::string ParamStr;
4178 for (BlockDecl::param_iterator AI = BD->param_begin(),
4179 E = BD->param_end(); AI != E; ++AI) {
4180 if (AI != BD->param_begin()) S += ", ";
4181 ParamStr = (*AI)->getNameAsString();
4182 QualType QT = (*AI)->getType();
Fariborz Jahanian2610f902012-03-27 16:42:20 +00004183 (void)convertBlockPointerToFunctionPointer(QT);
4184 QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004185 S += ParamStr;
4186 }
4187 if (FT->isVariadic()) {
4188 if (!BD->param_empty()) S += ", ";
4189 S += "...";
4190 }
4191 S += ')';
4192 }
4193 S += " {\n";
4194
4195 // Create local declarations to avoid rewriting all closure decl ref exprs.
4196 // First, emit a declaration for all "by ref" decls.
Craig Topper09d19ef2013-07-04 03:08:24 +00004197 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004198 E = BlockByRefDecls.end(); I != E; ++I) {
4199 S += " ";
4200 std::string Name = (*I)->getNameAsString();
4201 std::string TypeString;
4202 RewriteByRefString(TypeString, Name, (*I));
4203 TypeString += " *";
4204 Name = TypeString + Name;
4205 S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
4206 }
4207 // Next, emit a declaration for all "by copy" declarations.
Craig Topper09d19ef2013-07-04 03:08:24 +00004208 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004209 E = BlockByCopyDecls.end(); I != E; ++I) {
4210 S += " ";
4211 // Handle nested closure invocation. For example:
4212 //
4213 // void (^myImportedClosure)(void);
4214 // myImportedClosure = ^(void) { setGlobalInt(x + y); };
4215 //
4216 // void (^anotherClosure)(void);
4217 // anotherClosure = ^(void) {
4218 // myImportedClosure(); // import and invoke the closure
4219 // };
4220 //
4221 if (isTopLevelBlockPointerType((*I)->getType())) {
4222 RewriteBlockPointerTypeVariable(S, (*I));
4223 S += " = (";
4224 RewriteBlockPointerType(S, (*I)->getType());
4225 S += ")";
4226 S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
4227 }
4228 else {
4229 std::string Name = (*I)->getNameAsString();
4230 QualType QT = (*I)->getType();
4231 if (HasLocalVariableExternalStorage(*I))
4232 QT = Context->getPointerType(QT);
4233 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
4234 S += Name + " = __cself->" +
4235 (*I)->getNameAsString() + "; // bound by copy\n";
4236 }
4237 }
4238 std::string RewrittenStr = RewrittenBlockExprs[CE];
4239 const char *cstr = RewrittenStr.c_str();
4240 while (*cstr++ != '{') ;
4241 S += cstr;
4242 S += "\n";
4243 return S;
4244}
4245
4246std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
4247 StringRef funcName,
4248 std::string Tag) {
4249 std::string StructRef = "struct " + Tag;
4250 std::string S = "static void __";
4251
4252 S += funcName;
4253 S += "_block_copy_" + utostr(i);
4254 S += "(" + StructRef;
4255 S += "*dst, " + StructRef;
4256 S += "*src) {";
4257 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
4258 E = ImportedBlockDecls.end(); I != E; ++I) {
4259 ValueDecl *VD = (*I);
4260 S += "_Block_object_assign((void*)&dst->";
4261 S += (*I)->getNameAsString();
4262 S += ", (void*)src->";
4263 S += (*I)->getNameAsString();
4264 if (BlockByRefDeclsPtrSet.count((*I)))
4265 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
4266 else if (VD->getType()->isBlockPointerType())
4267 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
4268 else
4269 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
4270 }
4271 S += "}\n";
4272
4273 S += "\nstatic void __";
4274 S += funcName;
4275 S += "_block_dispose_" + utostr(i);
4276 S += "(" + StructRef;
4277 S += "*src) {";
4278 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
4279 E = ImportedBlockDecls.end(); I != E; ++I) {
4280 ValueDecl *VD = (*I);
4281 S += "_Block_object_dispose((void*)src->";
4282 S += (*I)->getNameAsString();
4283 if (BlockByRefDeclsPtrSet.count((*I)))
4284 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
4285 else if (VD->getType()->isBlockPointerType())
4286 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
4287 else
4288 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
4289 }
4290 S += "}\n";
4291 return S;
4292}
4293
4294std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
4295 std::string Desc) {
4296 std::string S = "\nstruct " + Tag;
4297 std::string Constructor = " " + Tag;
4298
4299 S += " {\n struct __block_impl impl;\n";
4300 S += " struct " + Desc;
4301 S += "* Desc;\n";
4302
4303 Constructor += "(void *fp, "; // Invoke function pointer.
4304 Constructor += "struct " + Desc; // Descriptor pointer.
4305 Constructor += " *desc";
4306
4307 if (BlockDeclRefs.size()) {
4308 // Output all "by copy" declarations.
Craig Topper09d19ef2013-07-04 03:08:24 +00004309 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004310 E = BlockByCopyDecls.end(); I != E; ++I) {
4311 S += " ";
4312 std::string FieldName = (*I)->getNameAsString();
4313 std::string ArgName = "_" + FieldName;
4314 // Handle nested closure invocation. For example:
4315 //
4316 // void (^myImportedBlock)(void);
4317 // myImportedBlock = ^(void) { setGlobalInt(x + y); };
4318 //
4319 // void (^anotherBlock)(void);
4320 // anotherBlock = ^(void) {
4321 // myImportedBlock(); // import and invoke the closure
4322 // };
4323 //
4324 if (isTopLevelBlockPointerType((*I)->getType())) {
4325 S += "struct __block_impl *";
4326 Constructor += ", void *" + ArgName;
4327 } else {
4328 QualType QT = (*I)->getType();
4329 if (HasLocalVariableExternalStorage(*I))
4330 QT = Context->getPointerType(QT);
4331 QT.getAsStringInternal(FieldName, Context->getPrintingPolicy());
4332 QT.getAsStringInternal(ArgName, Context->getPrintingPolicy());
4333 Constructor += ", " + ArgName;
4334 }
4335 S += FieldName + ";\n";
4336 }
4337 // Output all "by ref" declarations.
Craig Topper09d19ef2013-07-04 03:08:24 +00004338 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004339 E = BlockByRefDecls.end(); I != E; ++I) {
4340 S += " ";
4341 std::string FieldName = (*I)->getNameAsString();
4342 std::string ArgName = "_" + FieldName;
4343 {
4344 std::string TypeString;
4345 RewriteByRefString(TypeString, FieldName, (*I));
4346 TypeString += " *";
4347 FieldName = TypeString + FieldName;
4348 ArgName = TypeString + ArgName;
4349 Constructor += ", " + ArgName;
4350 }
4351 S += FieldName + "; // by ref\n";
4352 }
4353 // Finish writing the constructor.
4354 Constructor += ", int flags=0)";
4355 // Initialize all "by copy" arguments.
4356 bool firsTime = true;
Craig Topper09d19ef2013-07-04 03:08:24 +00004357 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004358 E = BlockByCopyDecls.end(); I != E; ++I) {
4359 std::string Name = (*I)->getNameAsString();
4360 if (firsTime) {
4361 Constructor += " : ";
4362 firsTime = false;
4363 }
4364 else
4365 Constructor += ", ";
4366 if (isTopLevelBlockPointerType((*I)->getType()))
4367 Constructor += Name + "((struct __block_impl *)_" + Name + ")";
4368 else
4369 Constructor += Name + "(_" + Name + ")";
4370 }
4371 // Initialize all "by ref" arguments.
Craig Topper09d19ef2013-07-04 03:08:24 +00004372 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004373 E = BlockByRefDecls.end(); I != E; ++I) {
4374 std::string Name = (*I)->getNameAsString();
4375 if (firsTime) {
4376 Constructor += " : ";
4377 firsTime = false;
4378 }
4379 else
4380 Constructor += ", ";
4381 Constructor += Name + "(_" + Name + "->__forwarding)";
4382 }
4383
4384 Constructor += " {\n";
4385 if (GlobalVarDecl)
4386 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
4387 else
4388 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
4389 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
4390
4391 Constructor += " Desc = desc;\n";
4392 } else {
4393 // Finish writing the constructor.
4394 Constructor += ", int flags=0) {\n";
4395 if (GlobalVarDecl)
4396 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
4397 else
4398 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
4399 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
4400 Constructor += " Desc = desc;\n";
4401 }
4402 Constructor += " ";
4403 Constructor += "}\n";
4404 S += Constructor;
4405 S += "};\n";
4406 return S;
4407}
4408
4409std::string RewriteModernObjC::SynthesizeBlockDescriptor(std::string DescTag,
4410 std::string ImplTag, int i,
4411 StringRef FunName,
4412 unsigned hasCopy) {
4413 std::string S = "\nstatic struct " + DescTag;
4414
Fariborz Jahanian8b08adb2012-05-03 21:44:12 +00004415 S += " {\n size_t reserved;\n";
4416 S += " size_t Block_size;\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004417 if (hasCopy) {
4418 S += " void (*copy)(struct ";
4419 S += ImplTag; S += "*, struct ";
4420 S += ImplTag; S += "*);\n";
4421
4422 S += " void (*dispose)(struct ";
4423 S += ImplTag; S += "*);\n";
4424 }
4425 S += "} ";
4426
4427 S += DescTag + "_DATA = { 0, sizeof(struct ";
4428 S += ImplTag + ")";
4429 if (hasCopy) {
4430 S += ", __" + FunName.str() + "_block_copy_" + utostr(i);
4431 S += ", __" + FunName.str() + "_block_dispose_" + utostr(i);
4432 }
4433 S += "};\n";
4434 return S;
4435}
4436
4437void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
4438 StringRef FunName) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004439 bool RewriteSC = (GlobalVarDecl &&
4440 !Blocks.empty() &&
4441 GlobalVarDecl->getStorageClass() == SC_Static &&
4442 GlobalVarDecl->getType().getCVRQualifiers());
4443 if (RewriteSC) {
4444 std::string SC(" void __");
4445 SC += GlobalVarDecl->getNameAsString();
4446 SC += "() {}";
4447 InsertText(FunLocStart, SC);
4448 }
4449
4450 // Insert closures that were part of the function.
4451 for (unsigned i = 0, count=0; i < Blocks.size(); i++) {
4452 CollectBlockDeclRefInfo(Blocks[i]);
4453 // Need to copy-in the inner copied-in variables not actually used in this
4454 // block.
4455 for (int j = 0; j < InnerDeclRefsCount[i]; j++) {
John McCallf4b88a42012-03-10 09:33:50 +00004456 DeclRefExpr *Exp = InnerDeclRefs[count++];
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004457 ValueDecl *VD = Exp->getDecl();
4458 BlockDeclRefs.push_back(Exp);
John McCallf4b88a42012-03-10 09:33:50 +00004459 if (!VD->hasAttr<BlocksAttr>()) {
4460 if (!BlockByCopyDeclsPtrSet.count(VD)) {
4461 BlockByCopyDeclsPtrSet.insert(VD);
4462 BlockByCopyDecls.push_back(VD);
4463 }
4464 continue;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004465 }
John McCallf4b88a42012-03-10 09:33:50 +00004466
4467 if (!BlockByRefDeclsPtrSet.count(VD)) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004468 BlockByRefDeclsPtrSet.insert(VD);
4469 BlockByRefDecls.push_back(VD);
4470 }
John McCallf4b88a42012-03-10 09:33:50 +00004471
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004472 // imported objects in the inner blocks not used in the outer
4473 // blocks must be copied/disposed in the outer block as well.
John McCallf4b88a42012-03-10 09:33:50 +00004474 if (VD->getType()->isObjCObjectPointerType() ||
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004475 VD->getType()->isBlockPointerType())
4476 ImportedBlockDecls.insert(VD);
4477 }
4478
4479 std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i);
4480 std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i);
4481
4482 std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag);
4483
4484 InsertText(FunLocStart, CI);
4485
4486 std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag);
4487
4488 InsertText(FunLocStart, CF);
4489
4490 if (ImportedBlockDecls.size()) {
4491 std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag);
4492 InsertText(FunLocStart, HF);
4493 }
4494 std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName,
4495 ImportedBlockDecls.size() > 0);
4496 InsertText(FunLocStart, BD);
4497
4498 BlockDeclRefs.clear();
4499 BlockByRefDecls.clear();
4500 BlockByRefDeclsPtrSet.clear();
4501 BlockByCopyDecls.clear();
4502 BlockByCopyDeclsPtrSet.clear();
4503 ImportedBlockDecls.clear();
4504 }
4505 if (RewriteSC) {
4506 // Must insert any 'const/volatile/static here. Since it has been
4507 // removed as result of rewriting of block literals.
4508 std::string SC;
4509 if (GlobalVarDecl->getStorageClass() == SC_Static)
4510 SC = "static ";
4511 if (GlobalVarDecl->getType().isConstQualified())
4512 SC += "const ";
4513 if (GlobalVarDecl->getType().isVolatileQualified())
4514 SC += "volatile ";
4515 if (GlobalVarDecl->getType().isRestrictQualified())
4516 SC += "restrict ";
4517 InsertText(FunLocStart, SC);
4518 }
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00004519 if (GlobalConstructionExp) {
4520 // extra fancy dance for global literal expression.
4521
4522 // Always the latest block expression on the block stack.
4523 std::string Tag = "__";
4524 Tag += FunName;
4525 Tag += "_block_impl_";
4526 Tag += utostr(Blocks.size()-1);
4527 std::string globalBuf = "static ";
4528 globalBuf += Tag; globalBuf += " ";
4529 std::string SStr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004530
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00004531 llvm::raw_string_ostream constructorExprBuf(SStr);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004532 GlobalConstructionExp->printPretty(constructorExprBuf, nullptr,
4533 PrintingPolicy(LangOpts));
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00004534 globalBuf += constructorExprBuf.str();
4535 globalBuf += ";\n";
4536 InsertText(FunLocStart, globalBuf);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004537 GlobalConstructionExp = nullptr;
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00004538 }
4539
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004540 Blocks.clear();
4541 InnerDeclRefsCount.clear();
4542 InnerDeclRefs.clear();
4543 RewrittenBlockExprs.clear();
4544}
4545
4546void RewriteModernObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
Fariborz Jahanian04189532012-04-25 17:56:48 +00004547 SourceLocation FunLocStart =
4548 (!Blocks.empty()) ? getFunctionSourceLocation(*this, FD)
4549 : FD->getTypeSpecStartLoc();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004550 StringRef FuncName = FD->getName();
4551
4552 SynthesizeBlockLiterals(FunLocStart, FuncName);
4553}
4554
4555static void BuildUniqueMethodName(std::string &Name,
4556 ObjCMethodDecl *MD) {
4557 ObjCInterfaceDecl *IFace = MD->getClassInterface();
4558 Name = IFace->getName();
4559 Name += "__" + MD->getSelector().getAsString();
4560 // Convert colons to underscores.
4561 std::string::size_type loc = 0;
4562 while ((loc = Name.find(":", loc)) != std::string::npos)
4563 Name.replace(loc, 1, "_");
4564}
4565
4566void RewriteModernObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
4567 //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n");
4568 //SourceLocation FunLocStart = MD->getLocStart();
4569 SourceLocation FunLocStart = MD->getLocStart();
4570 std::string FuncName;
4571 BuildUniqueMethodName(FuncName, MD);
4572 SynthesizeBlockLiterals(FunLocStart, FuncName);
4573}
4574
4575void RewriteModernObjC::GetBlockDeclRefExprs(Stmt *S) {
4576 for (Stmt::child_range CI = S->children(); CI; ++CI)
4577 if (*CI) {
4578 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
4579 GetBlockDeclRefExprs(CBE->getBody());
4580 else
4581 GetBlockDeclRefExprs(*CI);
4582 }
4583 // Handle specific things.
Fariborz Jahanian0e976812012-04-16 23:00:57 +00004584 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
4585 if (DRE->refersToEnclosingLocal()) {
4586 // FIXME: Handle enums.
4587 if (!isa<FunctionDecl>(DRE->getDecl()))
4588 BlockDeclRefs.push_back(DRE);
4589 if (HasLocalVariableExternalStorage(DRE->getDecl()))
4590 BlockDeclRefs.push_back(DRE);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004591 }
Fariborz Jahanian0e976812012-04-16 23:00:57 +00004592 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004593
4594 return;
4595}
4596
Craig Topper6b9240e2013-07-05 19:34:19 +00004597void RewriteModernObjC::GetInnerBlockDeclRefExprs(Stmt *S,
4598 SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004599 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) {
4600 for (Stmt::child_range CI = S->children(); CI; ++CI)
4601 if (*CI) {
4602 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) {
4603 InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl()));
4604 GetInnerBlockDeclRefExprs(CBE->getBody(),
4605 InnerBlockDeclRefs,
4606 InnerContexts);
4607 }
4608 else
4609 GetInnerBlockDeclRefExprs(*CI,
4610 InnerBlockDeclRefs,
4611 InnerContexts);
4612
4613 }
4614 // Handle specific things.
John McCallf4b88a42012-03-10 09:33:50 +00004615 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
4616 if (DRE->refersToEnclosingLocal()) {
4617 if (!isa<FunctionDecl>(DRE->getDecl()) &&
4618 !InnerContexts.count(DRE->getDecl()->getDeclContext()))
4619 InnerBlockDeclRefs.push_back(DRE);
4620 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl()))
4621 if (Var->isFunctionOrMethodVarDecl())
4622 ImportedLocalExternalDecls.insert(Var);
4623 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004624 }
4625
4626 return;
4627}
4628
Fariborz Jahanian164d6f82012-02-13 18:57:49 +00004629/// convertObjCTypeToCStyleType - This routine converts such objc types
4630/// as qualified objects, and blocks to their closest c/c++ types that
4631/// it can. It returns true if input type was modified.
4632bool RewriteModernObjC::convertObjCTypeToCStyleType(QualType &T) {
4633 QualType oldT = T;
4634 convertBlockPointerToFunctionPointer(T);
4635 if (T->isFunctionPointerType()) {
4636 QualType PointeeTy;
4637 if (const PointerType* PT = T->getAs<PointerType>()) {
4638 PointeeTy = PT->getPointeeType();
4639 if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) {
4640 T = convertFunctionTypeOfBlocks(FT);
4641 T = Context->getPointerType(T);
4642 }
4643 }
4644 }
4645
4646 convertToUnqualifiedObjCType(T);
4647 return T != oldT;
4648}
4649
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004650/// convertFunctionTypeOfBlocks - This routine converts a function type
4651/// whose result type may be a block pointer or whose argument type(s)
4652/// might be block pointers to an equivalent function type replacing
4653/// all block pointers to function pointers.
4654QualType RewriteModernObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) {
4655 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
4656 // FTP will be null for closures that don't take arguments.
4657 // Generate a funky cast.
4658 SmallVector<QualType, 8> ArgTypes;
Stephen Hines651f13c2014-04-23 16:59:28 -07004659 QualType Res = FT->getReturnType();
Fariborz Jahanian164d6f82012-02-13 18:57:49 +00004660 bool modified = convertObjCTypeToCStyleType(Res);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004661
4662 if (FTP) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004663 for (auto &I : FTP->param_types()) {
4664 QualType t = I;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004665 // Make sure we convert "t (^)(...)" to "t (*)(...)".
Fariborz Jahanian164d6f82012-02-13 18:57:49 +00004666 if (convertObjCTypeToCStyleType(t))
4667 modified = true;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004668 ArgTypes.push_back(t);
4669 }
4670 }
4671 QualType FuncType;
Fariborz Jahanian164d6f82012-02-13 18:57:49 +00004672 if (modified)
Jordan Rosebea522f2013-03-08 21:51:21 +00004673 FuncType = getSimpleFunctionType(Res, ArgTypes);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004674 else FuncType = QualType(FT, 0);
4675 return FuncType;
4676}
4677
4678Stmt *RewriteModernObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) {
4679 // Navigate to relevant type information.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004680 const BlockPointerType *CPT = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004681
4682 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) {
4683 CPT = DRE->getType()->getAs<BlockPointerType>();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004684 } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) {
4685 CPT = MExpr->getType()->getAs<BlockPointerType>();
4686 }
4687 else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) {
4688 return SynthesizeBlockCall(Exp, PRE->getSubExpr());
4689 }
4690 else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp))
4691 CPT = IEXPR->getType()->getAs<BlockPointerType>();
4692 else if (const ConditionalOperator *CEXPR =
4693 dyn_cast<ConditionalOperator>(BlockExp)) {
4694 Expr *LHSExp = CEXPR->getLHS();
4695 Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp);
4696 Expr *RHSExp = CEXPR->getRHS();
4697 Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp);
4698 Expr *CONDExp = CEXPR->getCond();
4699 ConditionalOperator *CondExpr =
4700 new (Context) ConditionalOperator(CONDExp,
4701 SourceLocation(), cast<Expr>(LHSStmt),
4702 SourceLocation(), cast<Expr>(RHSStmt),
4703 Exp->getType(), VK_RValue, OK_Ordinary);
4704 return CondExpr;
4705 } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) {
4706 CPT = IRE->getType()->getAs<BlockPointerType>();
4707 } else if (const PseudoObjectExpr *POE
4708 = dyn_cast<PseudoObjectExpr>(BlockExp)) {
4709 CPT = POE->getType()->castAs<BlockPointerType>();
4710 } else {
4711 assert(1 && "RewriteBlockClass: Bad type");
4712 }
4713 assert(CPT && "RewriteBlockClass: Bad type");
4714 const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>();
4715 assert(FT && "RewriteBlockClass: Bad type");
4716 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
4717 // FTP will be null for closures that don't take arguments.
4718
4719 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
4720 SourceLocation(), SourceLocation(),
4721 &Context->Idents.get("__block_impl"));
4722 QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD));
4723
4724 // Generate a funky cast.
4725 SmallVector<QualType, 8> ArgTypes;
4726
4727 // Push the block argument type.
4728 ArgTypes.push_back(PtrBlock);
4729 if (FTP) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004730 for (auto &I : FTP->param_types()) {
4731 QualType t = I;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004732 // Make sure we convert "t (^)(...)" to "t (*)(...)".
4733 if (!convertBlockPointerToFunctionPointer(t))
4734 convertToUnqualifiedObjCType(t);
4735 ArgTypes.push_back(t);
4736 }
4737 }
4738 // Now do the pointer to function cast.
Jordan Rosebea522f2013-03-08 21:51:21 +00004739 QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004740
4741 PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType);
4742
4743 CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock,
4744 CK_BitCast,
4745 const_cast<Expr*>(BlockExp));
4746 // Don't forget the parens to enforce the proper binding.
4747 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
4748 BlkCast);
4749 //PE->dump();
4750
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004751 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004752 SourceLocation(),
4753 &Context->Idents.get("FuncPtr"),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004754 Context->VoidPtrTy, nullptr,
4755 /*BitWidth=*/nullptr, /*Mutable=*/true,
Richard Smithca523302012-06-10 03:12:00 +00004756 ICIS_NoInit);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004757 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
4758 FD->getType(), VK_LValue,
4759 OK_Ordinary);
4760
4761
4762 CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType,
4763 CK_BitCast, ME);
4764 PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast);
4765
4766 SmallVector<Expr*, 8> BlkExprs;
4767 // Add the implicit argument.
4768 BlkExprs.push_back(BlkCast);
4769 // Add the user arguments.
4770 for (CallExpr::arg_iterator I = Exp->arg_begin(),
4771 E = Exp->arg_end(); I != E; ++I) {
4772 BlkExprs.push_back(*I);
4773 }
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004774 CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004775 Exp->getType(), VK_RValue,
4776 SourceLocation());
4777 return CE;
4778}
4779
4780// We need to return the rewritten expression to handle cases where the
John McCallf4b88a42012-03-10 09:33:50 +00004781// DeclRefExpr is embedded in another expression being rewritten.
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004782// For example:
4783//
4784// int main() {
4785// __block Foo *f;
4786// __block int i;
4787//
4788// void (^myblock)() = ^() {
John McCallf4b88a42012-03-10 09:33:50 +00004789// [f test]; // f is a DeclRefExpr embedded in a message (which is being rewritten).
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004790// i = 77;
4791// };
4792//}
John McCallf4b88a42012-03-10 09:33:50 +00004793Stmt *RewriteModernObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004794 // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR
4795 // for each DeclRefExp where BYREFVAR is name of the variable.
John McCallf4b88a42012-03-10 09:33:50 +00004796 ValueDecl *VD = DeclRefExp->getDecl();
4797 bool isArrow = DeclRefExp->refersToEnclosingLocal();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004798
4799 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004800 SourceLocation(),
4801 &Context->Idents.get("__forwarding"),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004802 Context->VoidPtrTy, nullptr,
4803 /*BitWidth=*/nullptr, /*Mutable=*/true,
Richard Smithca523302012-06-10 03:12:00 +00004804 ICIS_NoInit);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004805 MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow,
4806 FD, SourceLocation(),
4807 FD->getType(), VK_LValue,
4808 OK_Ordinary);
4809
4810 StringRef Name = VD->getName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004811 FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), SourceLocation(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004812 &Context->Idents.get(Name),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004813 Context->VoidPtrTy, nullptr,
4814 /*BitWidth=*/nullptr, /*Mutable=*/true,
Richard Smithca523302012-06-10 03:12:00 +00004815 ICIS_NoInit);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004816 ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(),
4817 DeclRefExp->getType(), VK_LValue, OK_Ordinary);
4818
4819
4820
4821 // Need parens to enforce precedence.
4822 ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(),
4823 DeclRefExp->getExprLoc(),
4824 ME);
4825 ReplaceStmt(DeclRefExp, PE);
4826 return PE;
4827}
4828
4829// Rewrites the imported local variable V with external storage
4830// (static, extern, etc.) as *V
4831//
4832Stmt *RewriteModernObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) {
4833 ValueDecl *VD = DRE->getDecl();
4834 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
4835 if (!ImportedLocalExternalDecls.count(Var))
4836 return DRE;
4837 Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(),
4838 VK_LValue, OK_Ordinary,
4839 DRE->getLocation());
4840 // Need parens to enforce precedence.
4841 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
4842 Exp);
4843 ReplaceStmt(DRE, PE);
4844 return PE;
4845}
4846
4847void RewriteModernObjC::RewriteCastExpr(CStyleCastExpr *CE) {
4848 SourceLocation LocStart = CE->getLParenLoc();
4849 SourceLocation LocEnd = CE->getRParenLoc();
4850
4851 // Need to avoid trying to rewrite synthesized casts.
4852 if (LocStart.isInvalid())
4853 return;
4854 // Need to avoid trying to rewrite casts contained in macros.
4855 if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd))
4856 return;
4857
4858 const char *startBuf = SM->getCharacterData(LocStart);
4859 const char *endBuf = SM->getCharacterData(LocEnd);
4860 QualType QT = CE->getType();
4861 const Type* TypePtr = QT->getAs<Type>();
4862 if (isa<TypeOfExprType>(TypePtr)) {
4863 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
4864 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
4865 std::string TypeAsString = "(";
4866 RewriteBlockPointerType(TypeAsString, QT);
4867 TypeAsString += ")";
4868 ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString);
4869 return;
4870 }
4871 // advance the location to startArgList.
4872 const char *argPtr = startBuf;
4873
4874 while (*argPtr++ && (argPtr < endBuf)) {
4875 switch (*argPtr) {
4876 case '^':
4877 // Replace the '^' with '*'.
4878 LocStart = LocStart.getLocWithOffset(argPtr-startBuf);
4879 ReplaceText(LocStart, 1, "*");
4880 break;
4881 }
4882 }
4883 return;
4884}
4885
Fariborz Jahanianf1ee6872012-04-10 00:08:18 +00004886void RewriteModernObjC::RewriteImplicitCastObjCExpr(CastExpr *IC) {
4887 CastKind CastKind = IC->getCastKind();
Fariborz Jahanian43aa1c32012-04-16 22:14:01 +00004888 if (CastKind != CK_BlockPointerToObjCPointerCast &&
4889 CastKind != CK_AnyPointerToBlockPointerCast)
4890 return;
Fariborz Jahanianf1ee6872012-04-10 00:08:18 +00004891
Fariborz Jahanian43aa1c32012-04-16 22:14:01 +00004892 QualType QT = IC->getType();
4893 (void)convertBlockPointerToFunctionPointer(QT);
4894 std::string TypeString(QT.getAsString(Context->getPrintingPolicy()));
4895 std::string Str = "(";
4896 Str += TypeString;
4897 Str += ")";
4898 InsertText(IC->getSubExpr()->getLocStart(), &Str[0], Str.size());
4899
Fariborz Jahanianf1ee6872012-04-10 00:08:18 +00004900 return;
4901}
4902
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004903void RewriteModernObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
4904 SourceLocation DeclLoc = FD->getLocation();
4905 unsigned parenCount = 0;
4906
4907 // We have 1 or more arguments that have closure pointers.
4908 const char *startBuf = SM->getCharacterData(DeclLoc);
4909 const char *startArgList = strchr(startBuf, '(');
4910
4911 assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
4912
4913 parenCount++;
4914 // advance the location to startArgList.
4915 DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf);
4916 assert((DeclLoc.isValid()) && "Invalid DeclLoc");
4917
4918 const char *argPtr = startArgList;
4919
4920 while (*argPtr++ && parenCount) {
4921 switch (*argPtr) {
4922 case '^':
4923 // Replace the '^' with '*'.
4924 DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList);
4925 ReplaceText(DeclLoc, 1, "*");
4926 break;
4927 case '(':
4928 parenCount++;
4929 break;
4930 case ')':
4931 parenCount--;
4932 break;
4933 }
4934 }
4935 return;
4936}
4937
4938bool RewriteModernObjC::PointerTypeTakesAnyBlockArguments(QualType QT) {
4939 const FunctionProtoType *FTP;
4940 const PointerType *PT = QT->getAs<PointerType>();
4941 if (PT) {
4942 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4943 } else {
4944 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4945 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
4946 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4947 }
4948 if (FTP) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004949 for (const auto &I : FTP->param_types())
4950 if (isTopLevelBlockPointerType(I))
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004951 return true;
4952 }
4953 return false;
4954}
4955
4956bool RewriteModernObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) {
4957 const FunctionProtoType *FTP;
4958 const PointerType *PT = QT->getAs<PointerType>();
4959 if (PT) {
4960 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4961 } else {
4962 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4963 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
4964 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4965 }
4966 if (FTP) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004967 for (const auto &I : FTP->param_types()) {
4968 if (I->isObjCQualifiedIdType())
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004969 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -07004970 if (I->isObjCObjectPointerType() &&
4971 I->getPointeeType()->isObjCQualifiedInterfaceType())
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00004972 return true;
4973 }
4974
4975 }
4976 return false;
4977}
4978
4979void RewriteModernObjC::GetExtentOfArgList(const char *Name, const char *&LParen,
4980 const char *&RParen) {
4981 const char *argPtr = strchr(Name, '(');
4982 assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
4983
4984 LParen = argPtr; // output the start.
4985 argPtr++; // skip past the left paren.
4986 unsigned parenCount = 1;
4987
4988 while (*argPtr && parenCount) {
4989 switch (*argPtr) {
4990 case '(': parenCount++; break;
4991 case ')': parenCount--; break;
4992 default: break;
4993 }
4994 if (parenCount) argPtr++;
4995 }
4996 assert((*argPtr == ')') && "Rewriter fuzzy parser confused");
4997 RParen = argPtr; // output the end
4998}
4999
5000void RewriteModernObjC::RewriteBlockPointerDecl(NamedDecl *ND) {
5001 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
5002 RewriteBlockPointerFunctionArgs(FD);
5003 return;
5004 }
5005 // Handle Variables and Typedefs.
5006 SourceLocation DeclLoc = ND->getLocation();
5007 QualType DeclT;
5008 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
5009 DeclT = VD->getType();
5010 else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND))
5011 DeclT = TDD->getUnderlyingType();
5012 else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
5013 DeclT = FD->getType();
5014 else
5015 llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled");
5016
5017 const char *startBuf = SM->getCharacterData(DeclLoc);
5018 const char *endBuf = startBuf;
5019 // scan backward (from the decl location) for the end of the previous decl.
5020 while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart)
5021 startBuf--;
5022 SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf);
5023 std::string buf;
5024 unsigned OrigLength=0;
5025 // *startBuf != '^' if we are dealing with a pointer to function that
5026 // may take block argument types (which will be handled below).
5027 if (*startBuf == '^') {
5028 // Replace the '^' with '*', computing a negative offset.
5029 buf = '*';
5030 startBuf++;
5031 OrigLength++;
5032 }
5033 while (*startBuf != ')') {
5034 buf += *startBuf;
5035 startBuf++;
5036 OrigLength++;
5037 }
5038 buf += ')';
5039 OrigLength++;
5040
5041 if (PointerTypeTakesAnyBlockArguments(DeclT) ||
5042 PointerTypeTakesAnyObjCQualifiedType(DeclT)) {
5043 // Replace the '^' with '*' for arguments.
5044 // Replace id<P> with id/*<>*/
5045 DeclLoc = ND->getLocation();
5046 startBuf = SM->getCharacterData(DeclLoc);
5047 const char *argListBegin, *argListEnd;
5048 GetExtentOfArgList(startBuf, argListBegin, argListEnd);
5049 while (argListBegin < argListEnd) {
5050 if (*argListBegin == '^')
5051 buf += '*';
5052 else if (*argListBegin == '<') {
5053 buf += "/*";
5054 buf += *argListBegin++;
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +00005055 OrigLength++;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005056 while (*argListBegin != '>') {
5057 buf += *argListBegin++;
5058 OrigLength++;
5059 }
5060 buf += *argListBegin;
5061 buf += "*/";
5062 }
5063 else
5064 buf += *argListBegin;
5065 argListBegin++;
5066 OrigLength++;
5067 }
5068 buf += ')';
5069 OrigLength++;
5070 }
5071 ReplaceText(Start, OrigLength, buf);
5072
5073 return;
5074}
5075
5076
5077/// SynthesizeByrefCopyDestroyHelper - This routine synthesizes:
5078/// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst,
5079/// struct Block_byref_id_object *src) {
5080/// _Block_object_assign (&_dest->object, _src->object,
5081/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
5082/// [|BLOCK_FIELD_IS_WEAK]) // object
5083/// _Block_object_assign(&_dest->object, _src->object,
5084/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
5085/// [|BLOCK_FIELD_IS_WEAK]) // block
5086/// }
5087/// And:
5088/// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) {
5089/// _Block_object_dispose(_src->object,
5090/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
5091/// [|BLOCK_FIELD_IS_WEAK]) // object
5092/// _Block_object_dispose(_src->object,
5093/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
5094/// [|BLOCK_FIELD_IS_WEAK]) // block
5095/// }
5096
5097std::string RewriteModernObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD,
5098 int flag) {
5099 std::string S;
5100 if (CopyDestroyCache.count(flag))
5101 return S;
5102 CopyDestroyCache.insert(flag);
5103 S = "static void __Block_byref_id_object_copy_";
5104 S += utostr(flag);
5105 S += "(void *dst, void *src) {\n";
5106
5107 // offset into the object pointer is computed as:
5108 // void * + void* + int + int + void* + void *
5109 unsigned IntSize =
5110 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
5111 unsigned VoidPtrSize =
5112 static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy));
5113
5114 unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth();
5115 S += " _Block_object_assign((char*)dst + ";
5116 S += utostr(offset);
5117 S += ", *(void * *) ((char*)src + ";
5118 S += utostr(offset);
5119 S += "), ";
5120 S += utostr(flag);
5121 S += ");\n}\n";
5122
5123 S += "static void __Block_byref_id_object_dispose_";
5124 S += utostr(flag);
5125 S += "(void *src) {\n";
5126 S += " _Block_object_dispose(*(void * *) ((char*)src + ";
5127 S += utostr(offset);
5128 S += "), ";
5129 S += utostr(flag);
5130 S += ");\n}\n";
5131 return S;
5132}
5133
5134/// RewriteByRefVar - For each __block typex ND variable this routine transforms
5135/// the declaration into:
5136/// struct __Block_byref_ND {
5137/// void *__isa; // NULL for everything except __weak pointers
5138/// struct __Block_byref_ND *__forwarding;
5139/// int32_t __flags;
5140/// int32_t __size;
5141/// void *__Block_byref_id_object_copy; // If variable is __block ObjC object
5142/// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object
5143/// typex ND;
5144/// };
5145///
5146/// It then replaces declaration of ND variable with:
5147/// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag,
5148/// __size=sizeof(struct __Block_byref_ND),
5149/// ND=initializer-if-any};
5150///
5151///
Fariborz Jahanian4fe261c2012-04-24 19:38:45 +00005152void RewriteModernObjC::RewriteByRefVar(VarDecl *ND, bool firstDecl,
5153 bool lastDecl) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005154 int flag = 0;
5155 int isa = 0;
5156 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
5157 if (DeclLoc.isInvalid())
5158 // If type location is missing, it is because of missing type (a warning).
5159 // Use variable's location which is good for this case.
5160 DeclLoc = ND->getLocation();
5161 const char *startBuf = SM->getCharacterData(DeclLoc);
5162 SourceLocation X = ND->getLocEnd();
5163 X = SM->getExpansionLoc(X);
5164 const char *endBuf = SM->getCharacterData(X);
5165 std::string Name(ND->getNameAsString());
5166 std::string ByrefType;
5167 RewriteByRefString(ByrefType, Name, ND, true);
5168 ByrefType += " {\n";
5169 ByrefType += " void *__isa;\n";
5170 RewriteByRefString(ByrefType, Name, ND);
5171 ByrefType += " *__forwarding;\n";
5172 ByrefType += " int __flags;\n";
5173 ByrefType += " int __size;\n";
5174 // Add void *__Block_byref_id_object_copy;
5175 // void *__Block_byref_id_object_dispose; if needed.
5176 QualType Ty = ND->getType();
Fariborz Jahanianb15c8982012-11-28 23:12:17 +00005177 bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005178 if (HasCopyAndDispose) {
5179 ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n";
5180 ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n";
5181 }
5182
5183 QualType T = Ty;
5184 (void)convertBlockPointerToFunctionPointer(T);
5185 T.getAsStringInternal(Name, Context->getPrintingPolicy());
5186
5187 ByrefType += " " + Name + ";\n";
5188 ByrefType += "};\n";
5189 // Insert this type in global scope. It is needed by helper function.
5190 SourceLocation FunLocStart;
5191 if (CurFunctionDef)
Fariborz Jahanianb75f8de2012-04-19 00:50:01 +00005192 FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005193 else {
5194 assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null");
5195 FunLocStart = CurMethodDef->getLocStart();
5196 }
5197 InsertText(FunLocStart, ByrefType);
Fariborz Jahanian8247c4e2012-04-24 16:45:27 +00005198
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005199 if (Ty.isObjCGCWeak()) {
5200 flag |= BLOCK_FIELD_IS_WEAK;
5201 isa = 1;
5202 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005203 if (HasCopyAndDispose) {
5204 flag = BLOCK_BYREF_CALLER;
5205 QualType Ty = ND->getType();
5206 // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well.
5207 if (Ty->isBlockPointerType())
5208 flag |= BLOCK_FIELD_IS_BLOCK;
5209 else
5210 flag |= BLOCK_FIELD_IS_OBJECT;
5211 std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag);
5212 if (!HF.empty())
Fariborz Jahanian31c4a4b2013-02-07 22:50:40 +00005213 Preamble += HF;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005214 }
5215
5216 // struct __Block_byref_ND ND =
5217 // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND),
5218 // initializer-if-any};
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005219 bool hasInit = (ND->getInit() != nullptr);
Fariborz Jahanian104dbf92012-04-11 23:57:12 +00005220 // FIXME. rewriter does not support __block c++ objects which
5221 // require construction.
Fariborz Jahanian65a7c682012-04-26 23:20:25 +00005222 if (hasInit)
5223 if (CXXConstructExpr *CExp = dyn_cast<CXXConstructExpr>(ND->getInit())) {
5224 CXXConstructorDecl *CXXDecl = CExp->getConstructor();
5225 if (CXXDecl && CXXDecl->isDefaultConstructor())
5226 hasInit = false;
5227 }
5228
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005229 unsigned flags = 0;
5230 if (HasCopyAndDispose)
5231 flags |= BLOCK_HAS_COPY_DISPOSE;
5232 Name = ND->getNameAsString();
5233 ByrefType.clear();
5234 RewriteByRefString(ByrefType, Name, ND);
5235 std::string ForwardingCastType("(");
5236 ForwardingCastType += ByrefType + " *)";
Fariborz Jahanian8247c4e2012-04-24 16:45:27 +00005237 ByrefType += " " + Name + " = {(void*)";
5238 ByrefType += utostr(isa);
5239 ByrefType += "," + ForwardingCastType + "&" + Name + ", ";
5240 ByrefType += utostr(flags);
5241 ByrefType += ", ";
5242 ByrefType += "sizeof(";
5243 RewriteByRefString(ByrefType, Name, ND);
5244 ByrefType += ")";
5245 if (HasCopyAndDispose) {
5246 ByrefType += ", __Block_byref_id_object_copy_";
5247 ByrefType += utostr(flag);
5248 ByrefType += ", __Block_byref_id_object_dispose_";
5249 ByrefType += utostr(flag);
5250 }
5251
Fariborz Jahanian4fe261c2012-04-24 19:38:45 +00005252 if (!firstDecl) {
5253 // In multiple __block declarations, and for all but 1st declaration,
5254 // find location of the separating comma. This would be start location
5255 // where new text is to be inserted.
5256 DeclLoc = ND->getLocation();
5257 const char *startDeclBuf = SM->getCharacterData(DeclLoc);
5258 const char *commaBuf = startDeclBuf;
5259 while (*commaBuf != ',')
5260 commaBuf--;
5261 assert((*commaBuf == ',') && "RewriteByRefVar: can't find ','");
5262 DeclLoc = DeclLoc.getLocWithOffset(commaBuf - startDeclBuf);
5263 startBuf = commaBuf;
5264 }
5265
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005266 if (!hasInit) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005267 ByrefType += "};\n";
5268 unsigned nameSize = Name.size();
5269 // for block or function pointer declaration. Name is aleady
5270 // part of the declaration.
5271 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType())
5272 nameSize = 1;
5273 ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType);
5274 }
5275 else {
Fariborz Jahanian8247c4e2012-04-24 16:45:27 +00005276 ByrefType += ", ";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005277 SourceLocation startLoc;
5278 Expr *E = ND->getInit();
5279 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
5280 startLoc = ECE->getLParenLoc();
5281 else
5282 startLoc = E->getLocStart();
5283 startLoc = SM->getExpansionLoc(startLoc);
5284 endBuf = SM->getCharacterData(startLoc);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005285 ReplaceText(DeclLoc, endBuf-startBuf, ByrefType);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005286
Fariborz Jahanian4fe261c2012-04-24 19:38:45 +00005287 const char separator = lastDecl ? ';' : ',';
5288 const char *startInitializerBuf = SM->getCharacterData(startLoc);
5289 const char *separatorBuf = strchr(startInitializerBuf, separator);
5290 assert((*separatorBuf == separator) &&
5291 "RewriteByRefVar: can't find ';' or ','");
5292 SourceLocation separatorLoc =
5293 startLoc.getLocWithOffset(separatorBuf-startInitializerBuf);
5294
5295 InsertText(separatorLoc, lastDecl ? "}" : "};\n");
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005296 }
5297 return;
5298}
5299
5300void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) {
5301 // Add initializers for any closure decl refs.
5302 GetBlockDeclRefExprs(Exp->getBody());
5303 if (BlockDeclRefs.size()) {
5304 // Unique all "by copy" declarations.
5305 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
John McCallf4b88a42012-03-10 09:33:50 +00005306 if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005307 if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
5308 BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
5309 BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl());
5310 }
5311 }
5312 // Unique all "by ref" declarations.
5313 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
John McCallf4b88a42012-03-10 09:33:50 +00005314 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005315 if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
5316 BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
5317 BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl());
5318 }
5319 }
5320 // Find any imported blocks...they will need special attention.
5321 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
John McCallf4b88a42012-03-10 09:33:50 +00005322 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005323 BlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5324 BlockDeclRefs[i]->getType()->isBlockPointerType())
5325 ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
5326 }
5327}
5328
5329FunctionDecl *RewriteModernObjC::SynthBlockInitFunctionDecl(StringRef name) {
5330 IdentifierInfo *ID = &Context->Idents.get(name);
5331 QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy);
5332 return FunctionDecl::Create(*Context, TUDecl, SourceLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005333 SourceLocation(), ID, FType, nullptr, SC_Extern,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005334 false, false);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005335}
5336
5337Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
Craig Topper6b9240e2013-07-05 19:34:19 +00005338 const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs) {
Fariborz Jahaniand13c2c22012-03-22 19:54:39 +00005339
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005340 const BlockDecl *block = Exp->getBlockDecl();
Fariborz Jahaniand13c2c22012-03-22 19:54:39 +00005341
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005342 Blocks.push_back(Exp);
5343
5344 CollectBlockDeclRefInfo(Exp);
5345
5346 // Add inner imported variables now used in current block.
5347 int countOfInnerDecls = 0;
5348 if (!InnerBlockDeclRefs.empty()) {
5349 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) {
John McCallf4b88a42012-03-10 09:33:50 +00005350 DeclRefExpr *Exp = InnerBlockDeclRefs[i];
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005351 ValueDecl *VD = Exp->getDecl();
John McCallf4b88a42012-03-10 09:33:50 +00005352 if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005353 // We need to save the copied-in variables in nested
5354 // blocks because it is needed at the end for some of the API generations.
5355 // See SynthesizeBlockLiterals routine.
5356 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
5357 BlockDeclRefs.push_back(Exp);
5358 BlockByCopyDeclsPtrSet.insert(VD);
5359 BlockByCopyDecls.push_back(VD);
5360 }
John McCallf4b88a42012-03-10 09:33:50 +00005361 if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005362 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
5363 BlockDeclRefs.push_back(Exp);
5364 BlockByRefDeclsPtrSet.insert(VD);
5365 BlockByRefDecls.push_back(VD);
5366 }
5367 }
5368 // Find any imported blocks...they will need special attention.
5369 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++)
John McCallf4b88a42012-03-10 09:33:50 +00005370 if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005371 InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5372 InnerBlockDeclRefs[i]->getType()->isBlockPointerType())
5373 ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl());
5374 }
5375 InnerDeclRefsCount.push_back(countOfInnerDecls);
5376
5377 std::string FuncName;
5378
5379 if (CurFunctionDef)
5380 FuncName = CurFunctionDef->getNameAsString();
5381 else if (CurMethodDef)
5382 BuildUniqueMethodName(FuncName, CurMethodDef);
5383 else if (GlobalVarDecl)
5384 FuncName = std::string(GlobalVarDecl->getNameAsString());
5385
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00005386 bool GlobalBlockExpr =
5387 block->getDeclContext()->getRedeclContext()->isFileContext();
5388
5389 if (GlobalBlockExpr && !GlobalVarDecl) {
5390 Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag);
5391 GlobalBlockExpr = false;
5392 }
5393
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005394 std::string BlockNumber = utostr(Blocks.size()-1);
5395
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005396 std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
5397
5398 // Get a pointer to the function type so we can cast appropriately.
5399 QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType());
5400 QualType FType = Context->getPointerType(BFT);
5401
5402 FunctionDecl *FD;
5403 Expr *NewRep;
5404
Benjamin Kramere5753592013-09-09 14:48:42 +00005405 // Simulate a constructor call...
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00005406 std::string Tag;
5407
5408 if (GlobalBlockExpr)
5409 Tag = "__global_";
5410 else
5411 Tag = "__";
5412 Tag += FuncName + "_block_impl_" + BlockNumber;
5413
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005414 FD = SynthBlockInitFunctionDecl(Tag);
John McCallf4b88a42012-03-10 09:33:50 +00005415 DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005416 SourceLocation());
5417
5418 SmallVector<Expr*, 4> InitExprs;
5419
5420 // Initialize the block function.
5421 FD = SynthBlockInitFunctionDecl(Func);
John McCallf4b88a42012-03-10 09:33:50 +00005422 DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(),
5423 VK_LValue, SourceLocation());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005424 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5425 CK_BitCast, Arg);
5426 InitExprs.push_back(castExpr);
5427
5428 // Initialize the block descriptor.
5429 std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA";
5430
5431 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl,
5432 SourceLocation(), SourceLocation(),
5433 &Context->Idents.get(DescData.c_str()),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005434 Context->VoidPtrTy, nullptr,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00005435 SC_Static);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005436 UnaryOperator *DescRefExpr =
John McCallf4b88a42012-03-10 09:33:50 +00005437 new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005438 Context->VoidPtrTy,
5439 VK_LValue,
5440 SourceLocation()),
5441 UO_AddrOf,
5442 Context->getPointerType(Context->VoidPtrTy),
5443 VK_RValue, OK_Ordinary,
5444 SourceLocation());
5445 InitExprs.push_back(DescRefExpr);
5446
5447 // Add initializers for any closure decl refs.
5448 if (BlockDeclRefs.size()) {
5449 Expr *Exp;
5450 // Output all "by copy" declarations.
Craig Topper09d19ef2013-07-04 03:08:24 +00005451 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005452 E = BlockByCopyDecls.end(); I != E; ++I) {
5453 if (isObjCType((*I)->getType())) {
5454 // FIXME: Conform to ABI ([[obj retain] autorelease]).
5455 FD = SynthBlockInitFunctionDecl((*I)->getName());
John McCallf4b88a42012-03-10 09:33:50 +00005456 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(),
5457 VK_LValue, SourceLocation());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005458 if (HasLocalVariableExternalStorage(*I)) {
5459 QualType QT = (*I)->getType();
5460 QT = Context->getPointerType(QT);
5461 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
5462 OK_Ordinary, SourceLocation());
5463 }
5464 } else if (isTopLevelBlockPointerType((*I)->getType())) {
5465 FD = SynthBlockInitFunctionDecl((*I)->getName());
John McCallf4b88a42012-03-10 09:33:50 +00005466 Arg = new (Context) DeclRefExpr(FD, false, FD->getType(),
5467 VK_LValue, SourceLocation());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005468 Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5469 CK_BitCast, Arg);
5470 } else {
5471 FD = SynthBlockInitFunctionDecl((*I)->getName());
John McCallf4b88a42012-03-10 09:33:50 +00005472 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(),
5473 VK_LValue, SourceLocation());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005474 if (HasLocalVariableExternalStorage(*I)) {
5475 QualType QT = (*I)->getType();
5476 QT = Context->getPointerType(QT);
5477 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
5478 OK_Ordinary, SourceLocation());
5479 }
5480
5481 }
5482 InitExprs.push_back(Exp);
5483 }
5484 // Output all "by ref" declarations.
Craig Topper09d19ef2013-07-04 03:08:24 +00005485 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005486 E = BlockByRefDecls.end(); I != E; ++I) {
5487 ValueDecl *ND = (*I);
5488 std::string Name(ND->getNameAsString());
5489 std::string RecName;
5490 RewriteByRefString(RecName, Name, ND, true);
5491 IdentifierInfo *II = &Context->Idents.get(RecName.c_str()
5492 + sizeof("struct"));
5493 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
5494 SourceLocation(), SourceLocation(),
5495 II);
5496 assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl");
5497 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
5498
5499 FD = SynthBlockInitFunctionDecl((*I)->getName());
John McCallf4b88a42012-03-10 09:33:50 +00005500 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005501 SourceLocation());
5502 bool isNestedCapturedVar = false;
5503 if (block)
Stephen Hines651f13c2014-04-23 16:59:28 -07005504 for (const auto &CI : block->captures()) {
5505 const VarDecl *variable = CI.getVariable();
5506 if (variable == ND && CI.isNested()) {
5507 assert (CI.isByRef() &&
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005508 "SynthBlockInitExpr - captured block variable is not byref");
5509 isNestedCapturedVar = true;
5510 break;
5511 }
5512 }
5513 // captured nested byref variable has its address passed. Do not take
5514 // its address again.
5515 if (!isNestedCapturedVar)
5516 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf,
5517 Context->getPointerType(Exp->getType()),
5518 VK_RValue, OK_Ordinary, SourceLocation());
5519 Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp);
5520 InitExprs.push_back(Exp);
5521 }
5522 }
5523 if (ImportedBlockDecls.size()) {
5524 // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR
5525 int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR);
5526 unsigned IntSize =
5527 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
5528 Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag),
5529 Context->IntTy, SourceLocation());
5530 InitExprs.push_back(FlagExp);
5531 }
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00005532 NewRep = new (Context) CallExpr(*Context, DRE, InitExprs,
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005533 FType, VK_LValue, SourceLocation());
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00005534
5535 if (GlobalBlockExpr) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005536 assert (!GlobalConstructionExp &&
Fariborz Jahaniandf474ec2012-03-23 00:00:49 +00005537 "SynthBlockInitExpr - GlobalConstructionExp must be null");
5538 GlobalConstructionExp = NewRep;
5539 NewRep = DRE;
5540 }
5541
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005542 NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf,
5543 Context->getPointerType(NewRep->getType()),
5544 VK_RValue, OK_Ordinary, SourceLocation());
5545 NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast,
5546 NewRep);
5547 BlockDeclRefs.clear();
5548 BlockByRefDecls.clear();
5549 BlockByRefDeclsPtrSet.clear();
5550 BlockByCopyDecls.clear();
5551 BlockByCopyDeclsPtrSet.clear();
5552 ImportedBlockDecls.clear();
5553 return NewRep;
5554}
5555
5556bool RewriteModernObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) {
5557 if (const ObjCForCollectionStmt * CS =
5558 dyn_cast<ObjCForCollectionStmt>(Stmts.back()))
5559 return CS->getElement() == DS;
5560 return false;
5561}
5562
5563//===----------------------------------------------------------------------===//
5564// Function Body / Expression rewriting
5565//===----------------------------------------------------------------------===//
5566
5567Stmt *RewriteModernObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
5568 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
5569 isa<DoStmt>(S) || isa<ForStmt>(S))
5570 Stmts.push_back(S);
5571 else if (isa<ObjCForCollectionStmt>(S)) {
5572 Stmts.push_back(S);
5573 ObjCBcLabelNo.push_back(++BcLabelCount);
5574 }
5575
5576 // Pseudo-object operations and ivar references need special
5577 // treatment because we're going to recursively rewrite them.
5578 if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) {
5579 if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) {
5580 return RewritePropertyOrImplicitSetter(PseudoOp);
5581 } else {
5582 return RewritePropertyOrImplicitGetter(PseudoOp);
5583 }
5584 } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) {
5585 return RewriteObjCIvarRefExpr(IvarRefExpr);
5586 }
Fariborz Jahanian9ffd1ae2013-02-08 18:57:50 +00005587 else if (isa<OpaqueValueExpr>(S))
5588 S = cast<OpaqueValueExpr>(S)->getSourceExpr();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005589
5590 SourceRange OrigStmtRange = S->getSourceRange();
5591
5592 // Perform a bottom up rewrite of all children.
5593 for (Stmt::child_range CI = S->children(); CI; ++CI)
5594 if (*CI) {
5595 Stmt *childStmt = (*CI);
5596 Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt);
5597 if (newStmt) {
5598 *CI = newStmt;
5599 }
5600 }
5601
5602 if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
John McCallf4b88a42012-03-10 09:33:50 +00005603 SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005604 llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts;
5605 InnerContexts.insert(BE->getBlockDecl());
5606 ImportedLocalExternalDecls.clear();
5607 GetInnerBlockDeclRefExprs(BE->getBody(),
5608 InnerBlockDeclRefs, InnerContexts);
5609 // Rewrite the block body in place.
5610 Stmt *SaveCurrentBody = CurrentBody;
5611 CurrentBody = BE->getBody();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005612 PropParentMap = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005613 // block literal on rhs of a property-dot-sytax assignment
5614 // must be replaced by its synthesize ast so getRewrittenText
5615 // works as expected. In this case, what actually ends up on RHS
5616 // is the blockTranscribed which is the helper function for the
5617 // block literal; as in: self.c = ^() {[ace ARR];};
5618 bool saveDisableReplaceStmt = DisableReplaceStmt;
5619 DisableReplaceStmt = false;
5620 RewriteFunctionBodyOrGlobalInitializer(BE->getBody());
5621 DisableReplaceStmt = saveDisableReplaceStmt;
5622 CurrentBody = SaveCurrentBody;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005623 PropParentMap = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005624 ImportedLocalExternalDecls.clear();
5625 // Now we snarf the rewritten text and stash it away for later use.
5626 std::string Str = Rewrite.getRewrittenText(BE->getSourceRange());
5627 RewrittenBlockExprs[BE] = Str;
5628
5629 Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs);
5630
5631 //blockTranscribed->dump();
5632 ReplaceStmt(S, blockTranscribed);
5633 return blockTranscribed;
5634 }
5635 // Handle specific things.
5636 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
5637 return RewriteAtEncode(AtEncode);
5638
5639 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
5640 return RewriteAtSelector(AtSelector);
5641
5642 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
5643 return RewriteObjCStringLiteral(AtString);
Fariborz Jahanian55947042012-03-27 20:17:30 +00005644
5645 if (ObjCBoolLiteralExpr *BoolLitExpr = dyn_cast<ObjCBoolLiteralExpr>(S))
5646 return RewriteObjCBoolLiteralExpr(BoolLitExpr);
Fariborz Jahanian0f9b18e2012-03-30 16:49:36 +00005647
Patrick Beardeb382ec2012-04-19 00:25:12 +00005648 if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(S))
5649 return RewriteObjCBoxedExpr(BoxedExpr);
Fariborz Jahanian86cff602012-03-30 23:35:47 +00005650
5651 if (ObjCArrayLiteral *ArrayLitExpr = dyn_cast<ObjCArrayLiteral>(S))
5652 return RewriteObjCArrayLiteralExpr(ArrayLitExpr);
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00005653
5654 if (ObjCDictionaryLiteral *DictionaryLitExpr =
5655 dyn_cast<ObjCDictionaryLiteral>(S))
5656 return RewriteObjCDictionaryLiteralExpr(DictionaryLitExpr);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005657
5658 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
5659#if 0
5660 // Before we rewrite it, put the original message expression in a comment.
5661 SourceLocation startLoc = MessExpr->getLocStart();
5662 SourceLocation endLoc = MessExpr->getLocEnd();
5663
5664 const char *startBuf = SM->getCharacterData(startLoc);
5665 const char *endBuf = SM->getCharacterData(endLoc);
5666
5667 std::string messString;
5668 messString += "// ";
5669 messString.append(startBuf, endBuf-startBuf+1);
5670 messString += "\n";
5671
5672 // FIXME: Missing definition of
5673 // InsertText(clang::SourceLocation, char const*, unsigned int).
5674 // InsertText(startLoc, messString.c_str(), messString.size());
5675 // Tried this, but it didn't work either...
5676 // ReplaceText(startLoc, 0, messString.c_str(), messString.size());
5677#endif
5678 return RewriteMessageExpr(MessExpr);
5679 }
5680
Fariborz Jahanian042b91d2012-05-23 23:47:20 +00005681 if (ObjCAutoreleasePoolStmt *StmtAutoRelease =
5682 dyn_cast<ObjCAutoreleasePoolStmt>(S)) {
5683 return RewriteObjCAutoreleasePoolStmt(StmtAutoRelease);
5684 }
5685
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005686 if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
5687 return RewriteObjCTryStmt(StmtTry);
5688
5689 if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S))
5690 return RewriteObjCSynchronizedStmt(StmtTry);
5691
5692 if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
5693 return RewriteObjCThrowStmt(StmtThrow);
5694
5695 if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
5696 return RewriteObjCProtocolExpr(ProtocolExp);
5697
5698 if (ObjCForCollectionStmt *StmtForCollection =
5699 dyn_cast<ObjCForCollectionStmt>(S))
5700 return RewriteObjCForCollectionStmt(StmtForCollection,
5701 OrigStmtRange.getEnd());
5702 if (BreakStmt *StmtBreakStmt =
5703 dyn_cast<BreakStmt>(S))
5704 return RewriteBreakStmt(StmtBreakStmt);
5705 if (ContinueStmt *StmtContinueStmt =
5706 dyn_cast<ContinueStmt>(S))
5707 return RewriteContinueStmt(StmtContinueStmt);
5708
5709 // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls
5710 // and cast exprs.
5711 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
5712 // FIXME: What we're doing here is modifying the type-specifier that
5713 // precedes the first Decl. In the future the DeclGroup should have
5714 // a separate type-specifier that we can rewrite.
5715 // NOTE: We need to avoid rewriting the DeclStmt if it is within
5716 // the context of an ObjCForCollectionStmt. For example:
5717 // NSArray *someArray;
5718 // for (id <FooProtocol> index in someArray) ;
5719 // This is because RewriteObjCForCollectionStmt() does textual rewriting
5720 // and it depends on the original text locations/positions.
5721 if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS))
5722 RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin());
5723
5724 // Blocks rewrite rules.
5725 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
5726 DI != DE; ++DI) {
5727 Decl *SD = *DI;
5728 if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
5729 if (isTopLevelBlockPointerType(ND->getType()))
5730 RewriteBlockPointerDecl(ND);
5731 else if (ND->getType()->isFunctionPointerType())
5732 CheckFunctionPointerDecl(ND->getType(), ND);
5733 if (VarDecl *VD = dyn_cast<VarDecl>(SD)) {
5734 if (VD->hasAttr<BlocksAttr>()) {
5735 static unsigned uniqueByrefDeclCount = 0;
5736 assert(!BlockByRefDeclNo.count(ND) &&
5737 "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl");
5738 BlockByRefDeclNo[ND] = uniqueByrefDeclCount++;
Fariborz Jahanian4fe261c2012-04-24 19:38:45 +00005739 RewriteByRefVar(VD, (DI == DS->decl_begin()), ((DI+1) == DE));
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005740 }
5741 else
5742 RewriteTypeOfDecl(VD);
5743 }
5744 }
5745 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
5746 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
5747 RewriteBlockPointerDecl(TD);
5748 else if (TD->getUnderlyingType()->isFunctionPointerType())
5749 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
5750 }
5751 }
5752 }
5753
5754 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S))
5755 RewriteObjCQualifiedInterfaceTypes(CE);
5756
5757 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
5758 isa<DoStmt>(S) || isa<ForStmt>(S)) {
5759 assert(!Stmts.empty() && "Statement stack is empty");
5760 assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
5761 isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
5762 && "Statement stack mismatch");
5763 Stmts.pop_back();
5764 }
5765 // Handle blocks rewriting.
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005766 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
5767 ValueDecl *VD = DRE->getDecl();
5768 if (VD->hasAttr<BlocksAttr>())
5769 return RewriteBlockDeclRefExpr(DRE);
5770 if (HasLocalVariableExternalStorage(VD))
5771 return RewriteLocalVariableExternalStorage(DRE);
5772 }
5773
5774 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
5775 if (CE->getCallee()->getType()->isBlockPointerType()) {
5776 Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee());
5777 ReplaceStmt(S, BlockCall);
5778 return BlockCall;
5779 }
5780 }
5781 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) {
5782 RewriteCastExpr(CE);
5783 }
Fariborz Jahanianf1ee6872012-04-10 00:08:18 +00005784 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
5785 RewriteImplicitCastObjCExpr(ICE);
5786 }
Fariborz Jahanian43aa1c32012-04-16 22:14:01 +00005787#if 0
Fariborz Jahanian653b7cf2012-04-13 18:00:54 +00005788
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005789 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
5790 CastExpr *Replacement = new (Context) CastExpr(ICE->getType(),
5791 ICE->getSubExpr(),
5792 SourceLocation());
5793 // Get the new text.
5794 std::string SStr;
5795 llvm::raw_string_ostream Buf(SStr);
Richard Smithd1420c62012-08-16 03:56:14 +00005796 Replacement->printPretty(Buf);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005797 const std::string &Str = Buf.str();
5798
5799 printf("CAST = %s\n", &Str[0]);
5800 InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
5801 delete S;
5802 return Replacement;
5803 }
5804#endif
5805 // Return this stmt unmodified.
5806 return S;
5807}
5808
5809void RewriteModernObjC::RewriteRecordBody(RecordDecl *RD) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005810 for (auto *FD : RD->fields()) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005811 if (isTopLevelBlockPointerType(FD->getType()))
5812 RewriteBlockPointerDecl(FD);
5813 if (FD->getType()->isObjCQualifiedIdType() ||
5814 FD->getType()->isObjCQualifiedInterfaceType())
5815 RewriteObjCQualifiedInterfaceTypes(FD);
5816 }
5817}
5818
5819/// HandleDeclInMainFile - This is called for each top-level decl defined in the
5820/// main file of the input.
5821void RewriteModernObjC::HandleDeclInMainFile(Decl *D) {
5822 switch (D->getKind()) {
5823 case Decl::Function: {
5824 FunctionDecl *FD = cast<FunctionDecl>(D);
5825 if (FD->isOverloadedOperator())
5826 return;
5827
5828 // Since function prototypes don't have ParmDecl's, we check the function
5829 // prototype. This enables us to rewrite function declarations and
5830 // definitions using the same code.
5831 RewriteBlocksInFunctionProtoType(FD->getType(), FD);
5832
Argyrios Kyrtzidis9335df32012-02-12 04:48:45 +00005833 if (!FD->isThisDeclarationADefinition())
5834 break;
5835
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005836 // FIXME: If this should support Obj-C++, support CXXTryStmt
5837 if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) {
5838 CurFunctionDef = FD;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005839 CurrentBody = Body;
5840 Body =
5841 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
5842 FD->setBody(Body);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005843 CurrentBody = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005844 if (PropParentMap) {
5845 delete PropParentMap;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005846 PropParentMap = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005847 }
5848 // This synthesizes and inserts the block "impl" struct, invoke function,
5849 // and any copy/dispose helper functions.
5850 InsertBlockLiteralsWithinFunction(FD);
Fariborz Jahanian96205962012-11-06 17:30:23 +00005851 RewriteLineDirective(D);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005852 CurFunctionDef = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005853 }
5854 break;
5855 }
5856 case Decl::ObjCMethod: {
5857 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
5858 if (CompoundStmt *Body = MD->getCompoundBody()) {
5859 CurMethodDef = MD;
5860 CurrentBody = Body;
5861 Body =
5862 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
5863 MD->setBody(Body);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005864 CurrentBody = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005865 if (PropParentMap) {
5866 delete PropParentMap;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005867 PropParentMap = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005868 }
5869 InsertBlockLiteralsWithinMethod(MD);
Fariborz Jahanian96205962012-11-06 17:30:23 +00005870 RewriteLineDirective(D);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005871 CurMethodDef = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005872 }
5873 break;
5874 }
5875 case Decl::ObjCImplementation: {
5876 ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D);
5877 ClassImplementation.push_back(CI);
5878 break;
5879 }
5880 case Decl::ObjCCategoryImpl: {
5881 ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D);
5882 CategoryImplementation.push_back(CI);
5883 break;
5884 }
5885 case Decl::Var: {
5886 VarDecl *VD = cast<VarDecl>(D);
5887 RewriteObjCQualifiedInterfaceTypes(VD);
5888 if (isTopLevelBlockPointerType(VD->getType()))
5889 RewriteBlockPointerDecl(VD);
5890 else if (VD->getType()->isFunctionPointerType()) {
5891 CheckFunctionPointerDecl(VD->getType(), VD);
5892 if (VD->getInit()) {
5893 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
5894 RewriteCastExpr(CE);
5895 }
5896 }
5897 } else if (VD->getType()->isRecordType()) {
5898 RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl();
5899 if (RD->isCompleteDefinition())
5900 RewriteRecordBody(RD);
5901 }
5902 if (VD->getInit()) {
5903 GlobalVarDecl = VD;
5904 CurrentBody = VD->getInit();
5905 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005906 CurrentBody = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005907 if (PropParentMap) {
5908 delete PropParentMap;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005909 PropParentMap = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005910 }
5911 SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005912 GlobalVarDecl = nullptr;
5913
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005914 // This is needed for blocks.
5915 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
5916 RewriteCastExpr(CE);
5917 }
5918 }
5919 break;
5920 }
5921 case Decl::TypeAlias:
5922 case Decl::Typedef: {
5923 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
5924 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
5925 RewriteBlockPointerDecl(TD);
5926 else if (TD->getUnderlyingType()->isFunctionPointerType())
5927 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
Fariborz Jahanianf9f30792013-04-03 19:11:21 +00005928 else
5929 RewriteObjCQualifiedInterfaceTypes(TD);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005930 }
5931 break;
5932 }
5933 case Decl::CXXRecord:
5934 case Decl::Record: {
5935 RecordDecl *RD = cast<RecordDecl>(D);
5936 if (RD->isCompleteDefinition())
5937 RewriteRecordBody(RD);
5938 break;
5939 }
5940 default:
5941 break;
5942 }
5943 // Nothing yet.
5944}
5945
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00005946/// Write_ProtocolExprReferencedMetadata - This routine writer out the
5947/// protocol reference symbols in the for of:
5948/// struct _protocol_t *PROTOCOL_REF = &PROTOCOL_METADATA.
5949static void Write_ProtocolExprReferencedMetadata(ASTContext *Context,
5950 ObjCProtocolDecl *PDecl,
5951 std::string &Result) {
5952 // Also output .objc_protorefs$B section and its meta-data.
5953 if (Context->getLangOpts().MicrosoftExt)
Fariborz Jahanianbd78cfa2012-04-27 21:39:49 +00005954 Result += "static ";
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00005955 Result += "struct _protocol_t *";
5956 Result += "_OBJC_PROTOCOL_REFERENCE_$_";
5957 Result += PDecl->getNameAsString();
5958 Result += " = &";
5959 Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString();
5960 Result += ";\n";
5961}
5962
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005963void RewriteModernObjC::HandleTranslationUnit(ASTContext &C) {
5964 if (Diags.hasErrorOccurred())
5965 return;
5966
5967 RewriteInclude();
5968
Fariborz Jahanian31c4a4b2013-02-07 22:50:40 +00005969 for (unsigned i = 0, e = FunctionDefinitionsSeen.size(); i < e; i++) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005970 // translation of function bodies were postponed until all class and
Fariborz Jahanian31c4a4b2013-02-07 22:50:40 +00005971 // their extensions and implementations are seen. This is because, we
Stephen Hines651f13c2014-04-23 16:59:28 -07005972 // cannot build grouping structs for bitfields until they are all seen.
Fariborz Jahanian31c4a4b2013-02-07 22:50:40 +00005973 FunctionDecl *FDecl = FunctionDefinitionsSeen[i];
5974 HandleTopLevelSingleDecl(FDecl);
5975 }
5976
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005977 // Here's a great place to add any extra declarations that may be needed.
5978 // Write out meta data for each @protocol(<expr>).
5979 for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00005980 E = ProtocolExprDecls.end(); I != E; ++I) {
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00005981 RewriteObjCProtocolMetaData(*I, Preamble);
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00005982 Write_ProtocolExprReferencedMetadata(Context, (*I), Preamble);
5983 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005984
5985 InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false);
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +00005986
5987 if (ClassImplementation.size() || CategoryImplementation.size())
5988 RewriteImplementations();
5989
Fariborz Jahanian57317782012-02-21 23:58:41 +00005990 for (unsigned i = 0, e = ObjCInterfacesSeen.size(); i < e; i++) {
5991 ObjCInterfaceDecl *CDecl = ObjCInterfacesSeen[i];
5992 // Write struct declaration for the class matching its ivar declarations.
5993 // Note that for modern abi, this is postponed until the end of TU
5994 // because class extensions and the implementation might declare their own
5995 // private ivars.
5996 RewriteInterfaceDecl(CDecl);
5997 }
Fariborz Jahanian31c4a4b2013-02-07 22:50:40 +00005998
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00005999 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
6000 // we are done.
6001 if (const RewriteBuffer *RewriteBuf =
6002 Rewrite.getRewriteBufferFor(MainFileID)) {
6003 //printf("Changed:\n");
6004 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
6005 } else {
6006 llvm::errs() << "No changes\n";
6007 }
6008
6009 if (ClassImplementation.size() || CategoryImplementation.size() ||
6010 ProtocolExprDecls.size()) {
6011 // Rewrite Objective-c meta data*
6012 std::string ResultStr;
6013 RewriteMetaDataIntoBuffer(ResultStr);
6014 // Emit metadata.
6015 *OutFile << ResultStr;
6016 }
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00006017 // Emit ImageInfo;
6018 {
6019 std::string ResultStr;
6020 WriteImageInfo(ResultStr);
6021 *OutFile << ResultStr;
6022 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006023 OutFile->flush();
6024}
6025
6026void RewriteModernObjC::Initialize(ASTContext &context) {
6027 InitializeCommon(context);
6028
Fariborz Jahanian6991bc52012-03-10 17:45:38 +00006029 Preamble += "#ifndef __OBJC2__\n";
6030 Preamble += "#define __OBJC2__\n";
6031 Preamble += "#endif\n";
6032
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006033 // declaring objc_selector outside the parameter list removes a silly
6034 // scope related warning...
6035 if (IsHeader)
6036 Preamble = "#pragma once\n";
6037 Preamble += "struct objc_selector; struct objc_class;\n";
Fariborz Jahaniane2d87bc2012-04-12 23:52:52 +00006038 Preamble += "struct __rw_objc_super { \n\tstruct objc_object *object; ";
6039 Preamble += "\n\tstruct objc_object *superClass; ";
6040 // Add a constructor for creating temporary objects.
6041 Preamble += "\n\t__rw_objc_super(struct objc_object *o, struct objc_object *s) ";
6042 Preamble += ": object(o), superClass(s) {} ";
6043 Preamble += "\n};\n";
6044
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006045 if (LangOpts.MicrosoftExt) {
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00006046 // Define all sections using syntax that makes sense.
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00006047 // These are currently generated.
6048 Preamble += "\n#pragma section(\".objc_classlist$B\", long, read, write)\n";
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00006049 Preamble += "#pragma section(\".objc_catlist$B\", long, read, write)\n";
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00006050 Preamble += "#pragma section(\".objc_imageinfo$B\", long, read, write)\n";
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00006051 Preamble += "#pragma section(\".objc_nlclslist$B\", long, read, write)\n";
6052 Preamble += "#pragma section(\".objc_nlcatlist$B\", long, read, write)\n";
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00006053 // These are generated but not necessary for functionality.
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00006054 Preamble += "#pragma section(\".cat_cls_meth$B\", long, read, write)\n";
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00006055 Preamble += "#pragma section(\".inst_meth$B\", long, read, write)\n";
6056 Preamble += "#pragma section(\".cls_meth$B\", long, read, write)\n";
Fariborz Jahanian40a777a2012-03-12 16:46:58 +00006057 Preamble += "#pragma section(\".objc_ivar$B\", long, read, write)\n";
Fariborz Jahaniande5d9462012-03-14 18:09:23 +00006058
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00006059 // These need be generated for performance. Currently they are not,
6060 // using API calls instead.
6061 Preamble += "#pragma section(\".objc_selrefs$B\", long, read, write)\n";
6062 Preamble += "#pragma section(\".objc_classrefs$B\", long, read, write)\n";
6063 Preamble += "#pragma section(\".objc_superrefs$B\", long, read, write)\n";
6064
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006065 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006066 Preamble += "#ifndef _REWRITER_typedef_Protocol\n";
6067 Preamble += "typedef struct objc_object Protocol;\n";
6068 Preamble += "#define _REWRITER_typedef_Protocol\n";
6069 Preamble += "#endif\n";
6070 if (LangOpts.MicrosoftExt) {
6071 Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n";
6072 Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n";
Fariborz Jahanian5cf6b6c2012-03-21 23:41:04 +00006073 }
6074 else
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006075 Preamble += "#define __OBJC_RW_DLLIMPORT extern\n";
Fariborz Jahanian5cf6b6c2012-03-21 23:41:04 +00006076
6077 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend(void);\n";
6078 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper(void);\n";
6079 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_stret(void);\n";
6080 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper_stret(void);\n";
6081 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_fpret(void);\n";
6082
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00006083 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getClass";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006084 Preamble += "(const char *);\n";
6085 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass";
6086 Preamble += "(struct objc_class *);\n";
Fariborz Jahanian20e181a2012-05-08 20:55:55 +00006087 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getMetaClass";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006088 Preamble += "(const char *);\n";
Fariborz Jahanian55261af2012-03-19 18:11:32 +00006089 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw( struct objc_object *);\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006090 // @synchronized hooks.
Aaron Ballman2d234d732012-09-06 16:44:16 +00006091 Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter( struct objc_object *);\n";
6092 Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit( struct objc_object *);\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006093 Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n";
Fariborz Jahaniane38f08a2013-09-05 17:17:32 +00006094 Preamble += "#ifdef _WIN64\n";
6095 Preamble += "typedef unsigned long long _WIN_NSUInteger;\n";
6096 Preamble += "#else\n";
6097 Preamble += "typedef unsigned int _WIN_NSUInteger;\n";
6098 Preamble += "#endif\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006099 Preamble += "#ifndef __FASTENUMERATIONSTATE\n";
6100 Preamble += "struct __objcFastEnumerationState {\n\t";
6101 Preamble += "unsigned long state;\n\t";
6102 Preamble += "void **itemsPtr;\n\t";
6103 Preamble += "unsigned long *mutationsPtr;\n\t";
6104 Preamble += "unsigned long extra[5];\n};\n";
6105 Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n";
6106 Preamble += "#define __FASTENUMERATIONSTATE\n";
6107 Preamble += "#endif\n";
6108 Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n";
6109 Preamble += "struct __NSConstantStringImpl {\n";
6110 Preamble += " int *isa;\n";
6111 Preamble += " int flags;\n";
6112 Preamble += " char *str;\n";
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006113 Preamble += "#if _WIN64\n";
Stephen Hines651f13c2014-04-23 16:59:28 -07006114 Preamble += " long long length;\n";
6115 Preamble += "#else\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006116 Preamble += " long length;\n";
Stephen Hines651f13c2014-04-23 16:59:28 -07006117 Preamble += "#endif\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006118 Preamble += "};\n";
6119 Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n";
6120 Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n";
6121 Preamble += "#else\n";
6122 Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n";
6123 Preamble += "#endif\n";
6124 Preamble += "#define __NSCONSTANTSTRINGIMPL\n";
6125 Preamble += "#endif\n";
6126 // Blocks preamble.
6127 Preamble += "#ifndef BLOCK_IMPL\n";
6128 Preamble += "#define BLOCK_IMPL\n";
6129 Preamble += "struct __block_impl {\n";
6130 Preamble += " void *isa;\n";
6131 Preamble += " int Flags;\n";
6132 Preamble += " int Reserved;\n";
6133 Preamble += " void *FuncPtr;\n";
6134 Preamble += "};\n";
6135 Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n";
6136 Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n";
6137 Preamble += "extern \"C\" __declspec(dllexport) "
6138 "void _Block_object_assign(void *, const void *, const int);\n";
6139 Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n";
6140 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n";
6141 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n";
6142 Preamble += "#else\n";
6143 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n";
6144 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n";
6145 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n";
6146 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n";
6147 Preamble += "#endif\n";
6148 Preamble += "#endif\n";
6149 if (LangOpts.MicrosoftExt) {
6150 Preamble += "#undef __OBJC_RW_DLLIMPORT\n";
6151 Preamble += "#undef __OBJC_RW_STATICIMPORT\n";
6152 Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests.
6153 Preamble += "#define __attribute__(X)\n";
6154 Preamble += "#endif\n";
Fariborz Jahanian5ce28272012-04-12 16:33:31 +00006155 Preamble += "#ifndef __weak\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006156 Preamble += "#define __weak\n";
Fariborz Jahanian5ce28272012-04-12 16:33:31 +00006157 Preamble += "#endif\n";
6158 Preamble += "#ifndef __block\n";
6159 Preamble += "#define __block\n";
6160 Preamble += "#endif\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006161 }
6162 else {
6163 Preamble += "#define __block\n";
6164 Preamble += "#define __weak\n";
6165 }
Fariborz Jahanianbe8d55c2012-06-29 18:27:08 +00006166
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00006167 // Declarations required for modern objective-c array and dictionary literals.
6168 Preamble += "\n#include <stdarg.h>\n";
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00006169 Preamble += "struct __NSContainer_literal {\n";
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00006170 Preamble += " void * *arr;\n";
Fariborz Jahaniane35abe12012-04-06 22:29:36 +00006171 Preamble += " __NSContainer_literal (unsigned int count, ...) {\n";
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00006172 Preamble += "\tva_list marker;\n";
6173 Preamble += "\tva_start(marker, count);\n";
6174 Preamble += "\tarr = new void *[count];\n";
6175 Preamble += "\tfor (unsigned i = 0; i < count; i++)\n";
6176 Preamble += "\t arr[i] = va_arg(marker, void *);\n";
6177 Preamble += "\tva_end( marker );\n";
6178 Preamble += " };\n";
Fariborz Jahanian13a9c022012-05-02 23:53:46 +00006179 Preamble += " ~__NSContainer_literal() {\n";
Fariborz Jahanianb0f245c2012-04-06 19:47:36 +00006180 Preamble += "\tdelete[] arr;\n";
6181 Preamble += " }\n";
6182 Preamble += "};\n";
6183
Fariborz Jahanian042b91d2012-05-23 23:47:20 +00006184 // Declaration required for implementation of @autoreleasepool statement.
6185 Preamble += "extern \"C\" __declspec(dllimport) void * objc_autoreleasePoolPush(void);\n";
6186 Preamble += "extern \"C\" __declspec(dllimport) void objc_autoreleasePoolPop(void *);\n\n";
6187 Preamble += "struct __AtAutoreleasePool {\n";
6188 Preamble += " __AtAutoreleasePool() {atautoreleasepoolobj = objc_autoreleasePoolPush();}\n";
6189 Preamble += " ~__AtAutoreleasePool() {objc_autoreleasePoolPop(atautoreleasepoolobj);}\n";
6190 Preamble += " void * atautoreleasepoolobj;\n";
6191 Preamble += "};\n";
6192
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006193 // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long
6194 // as this avoids warning in any 64bit/32bit compilation model.
6195 Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n";
6196}
6197
6198/// RewriteIvarOffsetComputation - This rutine synthesizes computation of
6199/// ivar offset.
6200void RewriteModernObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar,
6201 std::string &Result) {
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006202 Result += "__OFFSETOFIVAR__(struct ";
6203 Result += ivar->getContainingInterface()->getNameAsString();
6204 if (LangOpts.MicrosoftExt)
6205 Result += "_IMPL";
6206 Result += ", ";
6207 if (ivar->isBitField())
6208 ObjCIvarBitfieldGroupDecl(ivar, Result);
6209 else
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006210 Result += ivar->getNameAsString();
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006211 Result += ")";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006212}
6213
6214/// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI.
6215/// struct _prop_t {
Fariborz Jahanianda35eac2012-02-07 23:31:52 +00006216/// const char *name;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006217/// char *attributes;
6218/// }
6219
6220/// struct _prop_list_t {
6221/// uint32_t entsize; // sizeof(struct _prop_t)
6222/// uint32_t count_of_properties;
6223/// struct _prop_t prop_list[count_of_properties];
6224/// }
6225
6226/// struct _protocol_t;
6227
6228/// struct _protocol_list_t {
6229/// long protocol_count; // Note, this is 32/64 bit
6230/// struct _protocol_t * protocol_list[protocol_count];
6231/// }
6232
6233/// struct _objc_method {
6234/// SEL _cmd;
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006235/// const char *method_type;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006236/// char *_imp;
6237/// }
6238
6239/// struct _method_list_t {
6240/// uint32_t entsize; // sizeof(struct _objc_method)
6241/// uint32_t method_count;
6242/// struct _objc_method method_list[method_count];
6243/// }
6244
6245/// struct _protocol_t {
6246/// id isa; // NULL
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006247/// const char *protocol_name;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006248/// const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006249/// const struct method_list_t *instance_methods;
6250/// const struct method_list_t *class_methods;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006251/// const struct method_list_t *optionalInstanceMethods;
6252/// const struct method_list_t *optionalClassMethods;
6253/// const struct _prop_list_t * properties;
6254/// const uint32_t size; // sizeof(struct _protocol_t)
6255/// const uint32_t flags; // = 0
6256/// const char ** extendedMethodTypes;
6257/// }
6258
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006259/// struct _ivar_t {
6260/// unsigned long int *offset; // pointer to ivar offset location
Fariborz Jahanianae932952012-02-10 20:47:10 +00006261/// const char *name;
6262/// const char *type;
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006263/// uint32_t alignment;
6264/// uint32_t size;
6265/// }
6266
6267/// struct _ivar_list_t {
6268/// uint32 entsize; // sizeof(struct _ivar_t)
6269/// uint32 count;
Fariborz Jahanianae932952012-02-10 20:47:10 +00006270/// struct _ivar_t list[count];
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006271/// }
6272
6273/// struct _class_ro_t {
Fariborz Jahanian249cd102012-03-24 16:53:16 +00006274/// uint32_t flags;
6275/// uint32_t instanceStart;
6276/// uint32_t instanceSize;
6277/// uint32_t reserved; // only when building for 64bit targets
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006278/// const uint8_t *ivarLayout;
6279/// const char *name;
6280/// const struct _method_list_t *baseMethods;
6281/// const struct _protocol_list_t *baseProtocols;
6282/// const struct _ivar_list_t *ivars;
6283/// const uint8_t *weakIvarLayout;
6284/// const struct _prop_list_t *properties;
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006285/// }
6286
6287/// struct _class_t {
6288/// struct _class_t *isa;
Fariborz Jahanianfd4ce2c2012-03-20 17:34:50 +00006289/// struct _class_t *superclass;
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006290/// void *cache;
6291/// IMP *vtable;
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006292/// struct _class_ro_t *ro;
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006293/// }
6294
6295/// struct _category_t {
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006296/// const char *name;
Fariborz Jahanian4b2fe6e2012-03-20 21:41:28 +00006297/// struct _class_t *cls;
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006298/// const struct _method_list_t *instance_methods;
6299/// const struct _method_list_t *class_methods;
6300/// const struct _protocol_list_t *protocols;
6301/// const struct _prop_list_t *properties;
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006302/// }
6303
6304/// MessageRefTy - LLVM for:
6305/// struct _message_ref_t {
6306/// IMP messenger;
6307/// SEL name;
6308/// };
6309
6310/// SuperMessageRefTy - LLVM for:
6311/// struct _super_message_ref_t {
6312/// SUPER_IMP messenger;
6313/// SEL name;
6314/// };
6315
Fariborz Jahaniande5d9462012-03-14 18:09:23 +00006316static void WriteModernMetadataDeclarations(ASTContext *Context, std::string &Result) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006317 static bool meta_data_declared = false;
6318 if (meta_data_declared)
6319 return;
6320
6321 Result += "\nstruct _prop_t {\n";
Fariborz Jahanianda35eac2012-02-07 23:31:52 +00006322 Result += "\tconst char *name;\n";
6323 Result += "\tconst char *attributes;\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006324 Result += "};\n";
6325
6326 Result += "\nstruct _protocol_t;\n";
6327
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006328 Result += "\nstruct _objc_method {\n";
6329 Result += "\tstruct objc_selector * _cmd;\n";
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006330 Result += "\tconst char *method_type;\n";
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00006331 Result += "\tvoid *_imp;\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006332 Result += "};\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006333
6334 Result += "\nstruct _protocol_t {\n";
6335 Result += "\tvoid * isa; // NULL\n";
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006336 Result += "\tconst char *protocol_name;\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006337 Result += "\tconst struct _protocol_list_t * protocol_list; // super protocols\n";
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006338 Result += "\tconst struct method_list_t *instance_methods;\n";
6339 Result += "\tconst struct method_list_t *class_methods;\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006340 Result += "\tconst struct method_list_t *optionalInstanceMethods;\n";
6341 Result += "\tconst struct method_list_t *optionalClassMethods;\n";
6342 Result += "\tconst struct _prop_list_t * properties;\n";
6343 Result += "\tconst unsigned int size; // sizeof(struct _protocol_t)\n";
6344 Result += "\tconst unsigned int flags; // = 0\n";
6345 Result += "\tconst char ** extendedMethodTypes;\n";
6346 Result += "};\n";
6347
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006348 Result += "\nstruct _ivar_t {\n";
6349 Result += "\tunsigned long int *offset; // pointer to ivar offset location\n";
Fariborz Jahanianae932952012-02-10 20:47:10 +00006350 Result += "\tconst char *name;\n";
6351 Result += "\tconst char *type;\n";
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006352 Result += "\tunsigned int alignment;\n";
6353 Result += "\tunsigned int size;\n";
6354 Result += "};\n";
6355
6356 Result += "\nstruct _class_ro_t {\n";
Fariborz Jahanian249cd102012-03-24 16:53:16 +00006357 Result += "\tunsigned int flags;\n";
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006358 Result += "\tunsigned int instanceStart;\n";
Fariborz Jahanian249cd102012-03-24 16:53:16 +00006359 Result += "\tunsigned int instanceSize;\n";
Fariborz Jahaniande5d9462012-03-14 18:09:23 +00006360 const llvm::Triple &Triple(Context->getTargetInfo().getTriple());
6361 if (Triple.getArch() == llvm::Triple::x86_64)
Fariborz Jahanian249cd102012-03-24 16:53:16 +00006362 Result += "\tunsigned int reserved;\n";
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006363 Result += "\tconst unsigned char *ivarLayout;\n";
6364 Result += "\tconst char *name;\n";
6365 Result += "\tconst struct _method_list_t *baseMethods;\n";
6366 Result += "\tconst struct _objc_protocol_list *baseProtocols;\n";
6367 Result += "\tconst struct _ivar_list_t *ivars;\n";
6368 Result += "\tconst unsigned char *weakIvarLayout;\n";
6369 Result += "\tconst struct _prop_list_t *properties;\n";
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006370 Result += "};\n";
6371
6372 Result += "\nstruct _class_t {\n";
6373 Result += "\tstruct _class_t *isa;\n";
Fariborz Jahanianfd4ce2c2012-03-20 17:34:50 +00006374 Result += "\tstruct _class_t *superclass;\n";
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006375 Result += "\tvoid *cache;\n";
6376 Result += "\tvoid *vtable;\n";
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006377 Result += "\tstruct _class_ro_t *ro;\n";
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006378 Result += "};\n";
6379
6380 Result += "\nstruct _category_t {\n";
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006381 Result += "\tconst char *name;\n";
Fariborz Jahanian4b2fe6e2012-03-20 21:41:28 +00006382 Result += "\tstruct _class_t *cls;\n";
Fariborz Jahanian4e825df2012-03-21 16:23:16 +00006383 Result += "\tconst struct _method_list_t *instance_methods;\n";
6384 Result += "\tconst struct _method_list_t *class_methods;\n";
6385 Result += "\tconst struct _protocol_list_t *protocols;\n";
6386 Result += "\tconst struct _prop_list_t *properties;\n";
Fariborz Jahanian42e9a352012-02-10 00:04:22 +00006387 Result += "};\n";
6388
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006389 Result += "extern \"C\" __declspec(dllimport) struct objc_cache _objc_empty_cache;\n";
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006390 Result += "#pragma warning(disable:4273)\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006391 meta_data_declared = true;
6392}
6393
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00006394static void Write_protocol_list_t_TypeDecl(std::string &Result,
6395 long super_protocol_count) {
6396 Result += "struct /*_protocol_list_t*/"; Result += " {\n";
6397 Result += "\tlong protocol_count; // Note, this is 32/64 bit\n";
6398 Result += "\tstruct _protocol_t *super_protocols[";
6399 Result += utostr(super_protocol_count); Result += "];\n";
6400 Result += "}";
6401}
6402
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006403static void Write_method_list_t_TypeDecl(std::string &Result,
6404 unsigned int method_count) {
6405 Result += "struct /*_method_list_t*/"; Result += " {\n";
6406 Result += "\tunsigned int entsize; // sizeof(struct _objc_method)\n";
6407 Result += "\tunsigned int method_count;\n";
6408 Result += "\tstruct _objc_method method_list[";
6409 Result += utostr(method_count); Result += "];\n";
6410 Result += "}";
6411}
6412
Fariborz Jahanianda35eac2012-02-07 23:31:52 +00006413static void Write__prop_list_t_TypeDecl(std::string &Result,
6414 unsigned int property_count) {
6415 Result += "struct /*_prop_list_t*/"; Result += " {\n";
6416 Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n";
6417 Result += "\tunsigned int count_of_properties;\n";
6418 Result += "\tstruct _prop_t prop_list[";
6419 Result += utostr(property_count); Result += "];\n";
6420 Result += "}";
6421}
6422
Fariborz Jahanianae932952012-02-10 20:47:10 +00006423static void Write__ivar_list_t_TypeDecl(std::string &Result,
6424 unsigned int ivar_count) {
6425 Result += "struct /*_ivar_list_t*/"; Result += " {\n";
6426 Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n";
6427 Result += "\tunsigned int count;\n";
6428 Result += "\tstruct _ivar_t ivar_list[";
6429 Result += utostr(ivar_count); Result += "];\n";
6430 Result += "}";
6431}
6432
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00006433static void Write_protocol_list_initializer(ASTContext *Context, std::string &Result,
6434 ArrayRef<ObjCProtocolDecl *> SuperProtocols,
6435 StringRef VarName,
6436 StringRef ProtocolName) {
6437 if (SuperProtocols.size() > 0) {
6438 Result += "\nstatic ";
6439 Write_protocol_list_t_TypeDecl(Result, SuperProtocols.size());
6440 Result += " "; Result += VarName;
6441 Result += ProtocolName;
6442 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6443 Result += "\t"; Result += utostr(SuperProtocols.size()); Result += ",\n";
6444 for (unsigned i = 0, e = SuperProtocols.size(); i < e; i++) {
6445 ObjCProtocolDecl *SuperPD = SuperProtocols[i];
6446 Result += "\t&"; Result += "_OBJC_PROTOCOL_";
6447 Result += SuperPD->getNameAsString();
6448 if (i == e-1)
6449 Result += "\n};\n";
6450 else
6451 Result += ",\n";
6452 }
6453 }
6454}
6455
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00006456static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj,
6457 ASTContext *Context, std::string &Result,
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006458 ArrayRef<ObjCMethodDecl *> Methods,
6459 StringRef VarName,
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00006460 StringRef TopLevelDeclName,
6461 bool MethodImpl) {
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006462 if (Methods.size() > 0) {
6463 Result += "\nstatic ";
6464 Write_method_list_t_TypeDecl(Result, Methods.size());
6465 Result += " "; Result += VarName;
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00006466 Result += TopLevelDeclName;
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006467 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6468 Result += "\t"; Result += "sizeof(_objc_method)"; Result += ",\n";
6469 Result += "\t"; Result += utostr(Methods.size()); Result += ",\n";
6470 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
6471 ObjCMethodDecl *MD = Methods[i];
6472 if (i == 0)
6473 Result += "\t{{(struct objc_selector *)\"";
6474 else
6475 Result += "\t{(struct objc_selector *)\"";
6476 Result += (MD)->getSelector().getAsString(); Result += "\"";
6477 Result += ", ";
6478 std::string MethodTypeString;
6479 Context->getObjCEncodingForMethodDecl(MD, MethodTypeString);
6480 Result += "\""; Result += MethodTypeString; Result += "\"";
6481 Result += ", ";
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00006482 if (!MethodImpl)
6483 Result += "0";
6484 else {
6485 Result += "(void *)";
6486 Result += RewriteObj.MethodInternalNames[MD];
6487 }
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006488 if (i == e-1)
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00006489 Result += "}}\n";
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006490 else
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00006491 Result += "},\n";
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006492 }
6493 Result += "};\n";
6494 }
6495}
6496
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006497static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj,
Fariborz Jahanianda35eac2012-02-07 23:31:52 +00006498 ASTContext *Context, std::string &Result,
6499 ArrayRef<ObjCPropertyDecl *> Properties,
6500 const Decl *Container,
6501 StringRef VarName,
6502 StringRef ProtocolName) {
6503 if (Properties.size() > 0) {
6504 Result += "\nstatic ";
6505 Write__prop_list_t_TypeDecl(Result, Properties.size());
6506 Result += " "; Result += VarName;
6507 Result += ProtocolName;
6508 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6509 Result += "\t"; Result += "sizeof(_prop_t)"; Result += ",\n";
6510 Result += "\t"; Result += utostr(Properties.size()); Result += ",\n";
6511 for (unsigned i = 0, e = Properties.size(); i < e; i++) {
6512 ObjCPropertyDecl *PropDecl = Properties[i];
6513 if (i == 0)
6514 Result += "\t{{\"";
6515 else
6516 Result += "\t{\"";
6517 Result += PropDecl->getName(); Result += "\",";
6518 std::string PropertyTypeString, QuotePropertyTypeString;
6519 Context->getObjCEncodingForPropertyDecl(PropDecl, Container, PropertyTypeString);
6520 RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString);
6521 Result += "\""; Result += QuotePropertyTypeString; Result += "\"";
6522 if (i == e-1)
6523 Result += "}}\n";
6524 else
6525 Result += "},\n";
6526 }
6527 Result += "};\n";
6528 }
6529}
6530
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00006531// Metadata flags
6532enum MetaDataDlags {
6533 CLS = 0x0,
6534 CLS_META = 0x1,
6535 CLS_ROOT = 0x2,
6536 OBJC2_CLS_HIDDEN = 0x10,
6537 CLS_EXCEPTION = 0x20,
6538
6539 /// (Obsolete) ARC-specific: this class has a .release_ivars method
6540 CLS_HAS_IVAR_RELEASER = 0x40,
6541 /// class was compiled with -fobjc-arr
6542 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
6543};
6544
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006545static void Write__class_ro_t_initializer(ASTContext *Context, std::string &Result,
6546 unsigned int flags,
6547 const std::string &InstanceStart,
6548 const std::string &InstanceSize,
6549 ArrayRef<ObjCMethodDecl *>baseMethods,
6550 ArrayRef<ObjCProtocolDecl *>baseProtocols,
6551 ArrayRef<ObjCIvarDecl *>ivars,
6552 ArrayRef<ObjCPropertyDecl *>Properties,
6553 StringRef VarName,
6554 StringRef ClassName) {
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006555 Result += "\nstatic struct _class_ro_t ";
6556 Result += VarName; Result += ClassName;
6557 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6558 Result += "\t";
6559 Result += llvm::utostr(flags); Result += ", ";
6560 Result += InstanceStart; Result += ", ";
6561 Result += InstanceSize; Result += ", \n";
6562 Result += "\t";
Fariborz Jahaniande5d9462012-03-14 18:09:23 +00006563 const llvm::Triple &Triple(Context->getTargetInfo().getTriple());
6564 if (Triple.getArch() == llvm::Triple::x86_64)
6565 // uint32_t const reserved; // only when building for 64bit targets
6566 Result += "(unsigned int)0, \n\t";
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006567 // const uint8_t * const ivarLayout;
6568 Result += "0, \n\t";
6569 Result += "\""; Result += ClassName; Result += "\",\n\t";
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00006570 bool metaclass = ((flags & CLS_META) != 0);
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006571 if (baseMethods.size() > 0) {
6572 Result += "(const struct _method_list_t *)&";
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00006573 if (metaclass)
6574 Result += "_OBJC_$_CLASS_METHODS_";
6575 else
6576 Result += "_OBJC_$_INSTANCE_METHODS_";
6577 Result += ClassName;
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006578 Result += ",\n\t";
6579 }
6580 else
6581 Result += "0, \n\t";
6582
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00006583 if (!metaclass && baseProtocols.size() > 0) {
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006584 Result += "(const struct _objc_protocol_list *)&";
6585 Result += "_OBJC_CLASS_PROTOCOLS_$_"; Result += ClassName;
6586 Result += ",\n\t";
6587 }
6588 else
6589 Result += "0, \n\t";
6590
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00006591 if (!metaclass && ivars.size() > 0) {
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006592 Result += "(const struct _ivar_list_t *)&";
6593 Result += "_OBJC_$_INSTANCE_VARIABLES_"; Result += ClassName;
6594 Result += ",\n\t";
6595 }
6596 else
6597 Result += "0, \n\t";
6598
6599 // weakIvarLayout
6600 Result += "0, \n\t";
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00006601 if (!metaclass && Properties.size() > 0) {
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006602 Result += "(const struct _prop_list_t *)&";
Fariborz Jahanianeeabf382012-02-16 21:57:59 +00006603 Result += "_OBJC_$_PROP_LIST_"; Result += ClassName;
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00006604 Result += ",\n";
6605 }
6606 else
6607 Result += "0, \n";
6608
6609 Result += "};\n";
6610}
6611
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006612static void Write_class_t(ASTContext *Context, std::string &Result,
6613 StringRef VarName,
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006614 const ObjCInterfaceDecl *CDecl, bool metaclass) {
6615 bool rootClass = (!CDecl->getSuperClass());
6616 const ObjCInterfaceDecl *RootClass = CDecl;
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006617
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006618 if (!rootClass) {
6619 // Find the Root class
6620 RootClass = CDecl->getSuperClass();
6621 while (RootClass->getSuperClass()) {
6622 RootClass = RootClass->getSuperClass();
6623 }
6624 }
6625
6626 if (metaclass && rootClass) {
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006627 // Need to handle a case of use of forward declaration.
Fariborz Jahaniance0d8972012-03-10 18:25:06 +00006628 Result += "\n";
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006629 Result += "extern \"C\" ";
Fariborz Jahaniance0d8972012-03-10 18:25:06 +00006630 if (CDecl->getImplementation())
6631 Result += "__declspec(dllexport) ";
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006632 else
6633 Result += "__declspec(dllimport) ";
6634
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006635 Result += "struct _class_t OBJC_CLASS_$_";
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006636 Result += CDecl->getNameAsString();
6637 Result += ";\n";
6638 }
6639 // Also, for possibility of 'super' metadata class not having been defined yet.
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006640 if (!rootClass) {
Fariborz Jahanian868e9852012-03-29 19:04:10 +00006641 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
Fariborz Jahaniance0d8972012-03-10 18:25:06 +00006642 Result += "\n";
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006643 Result += "extern \"C\" ";
Fariborz Jahanian868e9852012-03-29 19:04:10 +00006644 if (SuperClass->getImplementation())
Fariborz Jahaniance0d8972012-03-10 18:25:06 +00006645 Result += "__declspec(dllexport) ";
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006646 else
6647 Result += "__declspec(dllimport) ";
6648
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006649 Result += "struct _class_t ";
Fariborz Jahaniance0d8972012-03-10 18:25:06 +00006650 Result += VarName;
Fariborz Jahanian868e9852012-03-29 19:04:10 +00006651 Result += SuperClass->getNameAsString();
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006652 Result += ";\n";
Fariborz Jahanian452eac12012-03-20 21:09:58 +00006653
Fariborz Jahanian868e9852012-03-29 19:04:10 +00006654 if (metaclass && RootClass != SuperClass) {
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006655 Result += "extern \"C\" ";
Fariborz Jahanian452eac12012-03-20 21:09:58 +00006656 if (RootClass->getImplementation())
6657 Result += "__declspec(dllexport) ";
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006658 else
6659 Result += "__declspec(dllimport) ";
6660
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006661 Result += "struct _class_t ";
Fariborz Jahanian452eac12012-03-20 21:09:58 +00006662 Result += VarName;
6663 Result += RootClass->getNameAsString();
6664 Result += ";\n";
6665 }
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006666 }
6667
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006668 Result += "\nextern \"C\" __declspec(dllexport) struct _class_t ";
6669 Result += VarName; Result += CDecl->getNameAsString();
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006670 Result += " __attribute__ ((used, section (\"__DATA,__objc_data\"))) = {\n";
6671 Result += "\t";
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006672 if (metaclass) {
6673 if (!rootClass) {
6674 Result += "0, // &"; Result += VarName;
6675 Result += RootClass->getNameAsString();
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006676 Result += ",\n\t";
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006677 Result += "0, // &"; Result += VarName;
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006678 Result += CDecl->getSuperClass()->getNameAsString();
6679 Result += ",\n\t";
6680 }
6681 else {
Fariborz Jahanian452eac12012-03-20 21:09:58 +00006682 Result += "0, // &"; Result += VarName;
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006683 Result += CDecl->getNameAsString();
6684 Result += ",\n\t";
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006685 Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString();
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006686 Result += ",\n\t";
6687 }
6688 }
6689 else {
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006690 Result += "0, // &OBJC_METACLASS_$_";
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006691 Result += CDecl->getNameAsString();
6692 Result += ",\n\t";
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006693 if (!rootClass) {
6694 Result += "0, // &"; Result += VarName;
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006695 Result += CDecl->getSuperClass()->getNameAsString();
6696 Result += ",\n\t";
6697 }
6698 else
6699 Result += "0,\n\t";
6700 }
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006701 Result += "0, // (void *)&_objc_empty_cache,\n\t";
6702 Result += "0, // unused, was (void *)&_objc_empty_vtable,\n\t";
6703 if (metaclass)
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006704 Result += "&_OBJC_METACLASS_RO_$_";
6705 else
6706 Result += "&_OBJC_CLASS_RO_$_";
6707 Result += CDecl->getNameAsString();
6708 Result += ",\n};\n";
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006709
6710 // Add static function to initialize some of the meta-data fields.
6711 // avoid doing it twice.
6712 if (metaclass)
6713 return;
6714
6715 const ObjCInterfaceDecl *SuperClass =
6716 rootClass ? CDecl : CDecl->getSuperClass();
6717
6718 Result += "static void OBJC_CLASS_SETUP_$_";
6719 Result += CDecl->getNameAsString();
6720 Result += "(void ) {\n";
6721 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6722 Result += ".isa = "; Result += "&OBJC_METACLASS_$_";
Fariborz Jahanian452eac12012-03-20 21:09:58 +00006723 Result += RootClass->getNameAsString(); Result += ";\n";
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006724
6725 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
Fariborz Jahanian452eac12012-03-20 21:09:58 +00006726 Result += ".superclass = ";
6727 if (rootClass)
6728 Result += "&OBJC_CLASS_$_";
6729 else
6730 Result += "&OBJC_METACLASS_$_";
6731
Fariborz Jahaniana03e40c2012-03-20 19:54:33 +00006732 Result += SuperClass->getNameAsString(); Result += ";\n";
6733
6734 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6735 Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n";
6736
6737 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6738 Result += ".isa = "; Result += "&OBJC_METACLASS_$_";
6739 Result += CDecl->getNameAsString(); Result += ";\n";
6740
6741 if (!rootClass) {
6742 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6743 Result += ".superclass = "; Result += "&OBJC_CLASS_$_";
6744 Result += SuperClass->getNameAsString(); Result += ";\n";
6745 }
6746
6747 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6748 Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n";
6749 Result += "}\n";
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00006750}
6751
Fariborz Jahanian61186122012-02-17 18:40:41 +00006752static void Write_category_t(RewriteModernObjC &RewriteObj, ASTContext *Context,
6753 std::string &Result,
Fariborz Jahaniane0335782012-03-27 18:41:05 +00006754 ObjCCategoryDecl *CatDecl,
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00006755 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian61186122012-02-17 18:40:41 +00006756 ArrayRef<ObjCMethodDecl *> InstanceMethods,
6757 ArrayRef<ObjCMethodDecl *> ClassMethods,
6758 ArrayRef<ObjCProtocolDecl *> RefedProtocols,
6759 ArrayRef<ObjCPropertyDecl *> ClassProperties) {
Fariborz Jahaniane0335782012-03-27 18:41:05 +00006760 StringRef CatName = CatDecl->getName();
NAKAMURA Takumi20f89392012-03-21 03:21:46 +00006761 StringRef ClassName = ClassDecl->getName();
Fariborz Jahanian8c00a1b2012-02-17 20:33:00 +00006762 // must declare an extern class object in case this class is not implemented
6763 // in this TU.
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00006764 Result += "\n";
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006765 Result += "extern \"C\" ";
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00006766 if (ClassDecl->getImplementation())
6767 Result += "__declspec(dllexport) ";
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006768 else
6769 Result += "__declspec(dllimport) ";
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00006770
Fariborz Jahanian3f162c32012-03-27 16:21:30 +00006771 Result += "struct _class_t ";
Fariborz Jahanian8c00a1b2012-02-17 20:33:00 +00006772 Result += "OBJC_CLASS_$_"; Result += ClassName;
6773 Result += ";\n";
6774
Fariborz Jahanian61186122012-02-17 18:40:41 +00006775 Result += "\nstatic struct _category_t ";
6776 Result += "_OBJC_$_CATEGORY_";
6777 Result += ClassName; Result += "_$_"; Result += CatName;
6778 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n";
6779 Result += "{\n";
6780 Result += "\t\""; Result += ClassName; Result += "\",\n";
Fariborz Jahanian4b2fe6e2012-03-20 21:41:28 +00006781 Result += "\t0, // &"; Result += "OBJC_CLASS_$_"; Result += ClassName;
Fariborz Jahanian61186122012-02-17 18:40:41 +00006782 Result += ",\n";
6783 if (InstanceMethods.size() > 0) {
6784 Result += "\t(const struct _method_list_t *)&";
6785 Result += "_OBJC_$_CATEGORY_INSTANCE_METHODS_";
6786 Result += ClassName; Result += "_$_"; Result += CatName;
6787 Result += ",\n";
6788 }
6789 else
6790 Result += "\t0,\n";
6791
6792 if (ClassMethods.size() > 0) {
6793 Result += "\t(const struct _method_list_t *)&";
6794 Result += "_OBJC_$_CATEGORY_CLASS_METHODS_";
6795 Result += ClassName; Result += "_$_"; Result += CatName;
6796 Result += ",\n";
6797 }
6798 else
6799 Result += "\t0,\n";
6800
6801 if (RefedProtocols.size() > 0) {
6802 Result += "\t(const struct _protocol_list_t *)&";
6803 Result += "_OBJC_CATEGORY_PROTOCOLS_$_";
6804 Result += ClassName; Result += "_$_"; Result += CatName;
6805 Result += ",\n";
6806 }
6807 else
6808 Result += "\t0,\n";
6809
6810 if (ClassProperties.size() > 0) {
6811 Result += "\t(const struct _prop_list_t *)&"; Result += "_OBJC_$_PROP_LIST_";
6812 Result += ClassName; Result += "_$_"; Result += CatName;
6813 Result += ",\n";
6814 }
6815 else
6816 Result += "\t0,\n";
6817
6818 Result += "};\n";
Fariborz Jahanian4b2fe6e2012-03-20 21:41:28 +00006819
6820 // Add static function to initialize the class pointer in the category structure.
6821 Result += "static void OBJC_CATEGORY_SETUP_$_";
6822 Result += ClassDecl->getNameAsString();
6823 Result += "_$_";
6824 Result += CatName;
6825 Result += "(void ) {\n";
6826 Result += "\t_OBJC_$_CATEGORY_";
6827 Result += ClassDecl->getNameAsString();
6828 Result += "_$_";
6829 Result += CatName;
6830 Result += ".cls = "; Result += "&OBJC_CLASS_$_"; Result += ClassName;
6831 Result += ";\n}\n";
Fariborz Jahanian61186122012-02-17 18:40:41 +00006832}
6833
Fariborz Jahaniane0adbd82012-02-08 22:23:26 +00006834static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj,
6835 ASTContext *Context, std::string &Result,
6836 ArrayRef<ObjCMethodDecl *> Methods,
6837 StringRef VarName,
6838 StringRef ProtocolName) {
6839 if (Methods.size() == 0)
6840 return;
6841
6842 Result += "\nstatic const char *";
6843 Result += VarName; Result += ProtocolName;
6844 Result += " [] __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n";
6845 Result += "{\n";
6846 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
6847 ObjCMethodDecl *MD = Methods[i];
6848 std::string MethodTypeString, QuoteMethodTypeString;
6849 Context->getObjCEncodingForMethodDecl(MD, MethodTypeString, true);
6850 RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString);
6851 Result += "\t\""; Result += QuoteMethodTypeString; Result += "\"";
6852 if (i == e-1)
6853 Result += "\n};\n";
6854 else {
6855 Result += ",\n";
6856 }
6857 }
6858}
6859
Fariborz Jahanianacee1c92012-04-11 21:12:36 +00006860static void Write_IvarOffsetVar(RewriteModernObjC &RewriteObj,
6861 ASTContext *Context,
Fariborz Jahanian40a777a2012-03-12 16:46:58 +00006862 std::string &Result,
Fariborz Jahaniandb649232012-02-13 20:59:02 +00006863 ArrayRef<ObjCIvarDecl *> Ivars,
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00006864 ObjCInterfaceDecl *CDecl) {
Fariborz Jahaniandb649232012-02-13 20:59:02 +00006865 // FIXME. visibilty of offset symbols may have to be set; for Darwin
6866 // this is what happens:
6867 /**
6868 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6869 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
6870 Class->getVisibility() == HiddenVisibility)
6871 Visibility shoud be: HiddenVisibility;
6872 else
6873 Visibility shoud be: DefaultVisibility;
6874 */
6875
Fariborz Jahanian07e52882012-02-13 21:34:45 +00006876 Result += "\n";
6877 for (unsigned i =0, e = Ivars.size(); i < e; i++) {
6878 ObjCIvarDecl *IvarDecl = Ivars[i];
Fariborz Jahanian40a777a2012-03-12 16:46:58 +00006879 if (Context->getLangOpts().MicrosoftExt)
6880 Result += "__declspec(allocate(\".objc_ivar$B\")) ";
6881
6882 if (!Context->getLangOpts().MicrosoftExt ||
6883 IvarDecl->getAccessControl() == ObjCIvarDecl::Private ||
Fariborz Jahanian117591f2012-03-10 01:34:42 +00006884 IvarDecl->getAccessControl() == ObjCIvarDecl::Package)
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006885 Result += "extern \"C\" unsigned long int ";
Fariborz Jahaniand1c84d32012-03-10 00:53:02 +00006886 else
Fariborz Jahanian297976d2012-03-29 17:51:09 +00006887 Result += "extern \"C\" __declspec(dllexport) unsigned long int ";
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006888 if (Ivars[i]->isBitField())
6889 RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result);
6890 else
6891 WriteInternalIvarName(CDecl, IvarDecl, Result);
Fariborz Jahanian07e52882012-02-13 21:34:45 +00006892 Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))";
6893 Result += " = ";
Fariborz Jahanianacee1c92012-04-11 21:12:36 +00006894 RewriteObj.RewriteIvarOffsetComputation(IvarDecl, Result);
6895 Result += ";\n";
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006896 if (Ivars[i]->isBitField()) {
6897 // skip over rest of the ivar bitfields.
6898 SKIP_BITFIELDS(i , e, Ivars);
6899 }
Fariborz Jahaniandb649232012-02-13 20:59:02 +00006900 }
6901}
6902
Fariborz Jahanianae932952012-02-10 20:47:10 +00006903static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj,
6904 ASTContext *Context, std::string &Result,
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006905 ArrayRef<ObjCIvarDecl *> OriginalIvars,
Fariborz Jahanianae932952012-02-10 20:47:10 +00006906 StringRef VarName,
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00006907 ObjCInterfaceDecl *CDecl) {
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006908 if (OriginalIvars.size() > 0) {
6909 Write_IvarOffsetVar(RewriteObj, Context, Result, OriginalIvars, CDecl);
6910 SmallVector<ObjCIvarDecl *, 8> Ivars;
6911 // strip off all but the first ivar bitfield from each group of ivars.
6912 // Such ivars in the ivar list table will be replaced by their grouping struct
6913 // 'ivar'.
6914 for (unsigned i = 0, e = OriginalIvars.size(); i < e; i++) {
6915 if (OriginalIvars[i]->isBitField()) {
6916 Ivars.push_back(OriginalIvars[i]);
6917 // skip over rest of the ivar bitfields.
6918 SKIP_BITFIELDS(i , e, OriginalIvars);
6919 }
6920 else
6921 Ivars.push_back(OriginalIvars[i]);
6922 }
Fariborz Jahanian07e52882012-02-13 21:34:45 +00006923
Fariborz Jahanianae932952012-02-10 20:47:10 +00006924 Result += "\nstatic ";
6925 Write__ivar_list_t_TypeDecl(Result, Ivars.size());
6926 Result += " "; Result += VarName;
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00006927 Result += CDecl->getNameAsString();
Fariborz Jahanianae932952012-02-10 20:47:10 +00006928 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6929 Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n";
6930 Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n";
6931 for (unsigned i =0, e = Ivars.size(); i < e; i++) {
6932 ObjCIvarDecl *IvarDecl = Ivars[i];
6933 if (i == 0)
6934 Result += "\t{{";
6935 else
6936 Result += "\t {";
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00006937 Result += "(unsigned long int *)&";
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006938 if (Ivars[i]->isBitField())
6939 RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result);
6940 else
6941 WriteInternalIvarName(CDecl, IvarDecl, Result);
Fariborz Jahaniandb649232012-02-13 20:59:02 +00006942 Result += ", ";
Fariborz Jahanianae932952012-02-10 20:47:10 +00006943
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006944 Result += "\"";
6945 if (Ivars[i]->isBitField())
6946 RewriteObj.ObjCIvarBitfieldGroupDecl(Ivars[i], Result);
6947 else
6948 Result += IvarDecl->getName();
6949 Result += "\", ";
6950
6951 QualType IVQT = IvarDecl->getType();
6952 if (IvarDecl->isBitField())
6953 IVQT = RewriteObj.GetGroupRecordTypeForObjCIvarBitfield(IvarDecl);
6954
Fariborz Jahanianae932952012-02-10 20:47:10 +00006955 std::string IvarTypeString, QuoteIvarTypeString;
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006956 Context->getObjCEncodingForType(IVQT, IvarTypeString,
Fariborz Jahanianae932952012-02-10 20:47:10 +00006957 IvarDecl);
6958 RewriteObj.QuoteDoublequotes(IvarTypeString, QuoteIvarTypeString);
6959 Result += "\""; Result += QuoteIvarTypeString; Result += "\", ";
6960
Fariborz Jahanian8f1fed02012-02-11 20:10:52 +00006961 // FIXME. this alignment represents the host alignment and need be changed to
6962 // represent the target alignment.
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006963 unsigned Align = Context->getTypeAlign(IVQT)/8;
Fariborz Jahanian8f1fed02012-02-11 20:10:52 +00006964 Align = llvm::Log2_32(Align);
Fariborz Jahanianae932952012-02-10 20:47:10 +00006965 Result += llvm::utostr(Align); Result += ", ";
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00006966 CharUnits Size = Context->getTypeSizeInChars(IVQT);
Fariborz Jahaniana63b4222012-02-10 23:18:24 +00006967 Result += llvm::utostr(Size.getQuantity());
Fariborz Jahanianae932952012-02-10 20:47:10 +00006968 if (i == e-1)
6969 Result += "}}\n";
6970 else
6971 Result += "},\n";
6972 }
6973 Result += "};\n";
6974 }
6975}
6976
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006977/// RewriteObjCProtocolMetaData - Rewrite protocols meta-data.
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00006978void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl,
6979 std::string &Result) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006980
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006981 // Do not synthesize the protocol more than once.
6982 if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl()))
6983 return;
Fariborz Jahaniande5d9462012-03-14 18:09:23 +00006984 WriteModernMetadataDeclarations(Context, Result);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006985
6986 if (ObjCProtocolDecl *Def = PDecl->getDefinition())
6987 PDecl = Def;
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00006988 // Must write out all protocol definitions in current qualifier list,
6989 // and in their nested qualifiers before writing out current definition.
Stephen Hines651f13c2014-04-23 16:59:28 -07006990 for (auto *I : PDecl->protocols())
6991 RewriteObjCProtocolMetaData(I, Result);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00006992
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006993 // Construct method lists.
6994 std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods;
6995 std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods;
Stephen Hines651f13c2014-04-23 16:59:28 -07006996 for (auto *MD : PDecl->instance_methods()) {
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00006997 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6998 OptInstanceMethods.push_back(MD);
6999 } else {
7000 InstanceMethods.push_back(MD);
7001 }
7002 }
7003
Stephen Hines651f13c2014-04-23 16:59:28 -07007004 for (auto *MD : PDecl->class_methods()) {
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007005 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
7006 OptClassMethods.push_back(MD);
7007 } else {
7008 ClassMethods.push_back(MD);
7009 }
7010 }
Fariborz Jahaniane0adbd82012-02-08 22:23:26 +00007011 std::vector<ObjCMethodDecl *> AllMethods;
7012 for (unsigned i = 0, e = InstanceMethods.size(); i < e; i++)
7013 AllMethods.push_back(InstanceMethods[i]);
7014 for (unsigned i = 0, e = ClassMethods.size(); i < e; i++)
7015 AllMethods.push_back(ClassMethods[i]);
7016 for (unsigned i = 0, e = OptInstanceMethods.size(); i < e; i++)
7017 AllMethods.push_back(OptInstanceMethods[i]);
7018 for (unsigned i = 0, e = OptClassMethods.size(); i < e; i++)
7019 AllMethods.push_back(OptClassMethods[i]);
7020
7021 Write__extendedMethodTypes_initializer(*this, Context, Result,
7022 AllMethods,
7023 "_OBJC_PROTOCOL_METHOD_TYPES_",
7024 PDecl->getNameAsString());
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00007025 // Protocol's super protocol list
Stephen Hines651f13c2014-04-23 16:59:28 -07007026 SmallVector<ObjCProtocolDecl *, 8> SuperProtocols(PDecl->protocols());
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00007027 Write_protocol_list_initializer(Context, Result, SuperProtocols,
7028 "_OBJC_PROTOCOL_REFS_",
7029 PDecl->getNameAsString());
7030
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007031 Write_method_list_t_initializer(*this, Context, Result, InstanceMethods,
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007032 "_OBJC_PROTOCOL_INSTANCE_METHODS_",
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007033 PDecl->getNameAsString(), false);
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007034
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007035 Write_method_list_t_initializer(*this, Context, Result, ClassMethods,
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007036 "_OBJC_PROTOCOL_CLASS_METHODS_",
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007037 PDecl->getNameAsString(), false);
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007038
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007039 Write_method_list_t_initializer(*this, Context, Result, OptInstanceMethods,
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007040 "_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_",
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007041 PDecl->getNameAsString(), false);
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007042
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007043 Write_method_list_t_initializer(*this, Context, Result, OptClassMethods,
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007044 "_OBJC_PROTOCOL_OPT_CLASS_METHODS_",
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007045 PDecl->getNameAsString(), false);
Fariborz Jahanian77e4bca2012-02-07 20:15:08 +00007046
Fariborz Jahanianda35eac2012-02-07 23:31:52 +00007047 // Protocol's property metadata.
Stephen Hines651f13c2014-04-23 16:59:28 -07007048 SmallVector<ObjCPropertyDecl *, 8> ProtocolProperties(PDecl->properties());
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007049 Write_prop_list_t_initializer(*this, Context, Result, ProtocolProperties,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007050 /* Container */nullptr,
Fariborz Jahanianda35eac2012-02-07 23:31:52 +00007051 "_OBJC_PROTOCOL_PROPERTIES_",
7052 PDecl->getNameAsString());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007053
Fariborz Jahanian82848c22012-02-08 00:50:52 +00007054 // Writer out root metadata for current protocol: struct _protocol_t
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00007055 Result += "\n";
7056 if (LangOpts.MicrosoftExt)
Fariborz Jahanian8590d862012-04-14 17:13:08 +00007057 Result += "static ";
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00007058 Result += "struct _protocol_t _OBJC_PROTOCOL_";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007059 Result += PDecl->getNameAsString();
Fariborz Jahanian82848c22012-02-08 00:50:52 +00007060 Result += " __attribute__ ((used, section (\"__DATA,__datacoal_nt,coalesced\"))) = {\n";
7061 Result += "\t0,\n"; // id is; is null
7062 Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n";
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00007063 if (SuperProtocols.size() > 0) {
7064 Result += "\t(const struct _protocol_list_t *)&"; Result += "_OBJC_PROTOCOL_REFS_";
7065 Result += PDecl->getNameAsString(); Result += ",\n";
7066 }
7067 else
7068 Result += "\t0,\n";
Fariborz Jahanian82848c22012-02-08 00:50:52 +00007069 if (InstanceMethods.size() > 0) {
7070 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_";
7071 Result += PDecl->getNameAsString(); Result += ",\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007072 }
7073 else
Fariborz Jahanian82848c22012-02-08 00:50:52 +00007074 Result += "\t0,\n";
7075
7076 if (ClassMethods.size() > 0) {
7077 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_CLASS_METHODS_";
7078 Result += PDecl->getNameAsString(); Result += ",\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007079 }
7080 else
Fariborz Jahanian82848c22012-02-08 00:50:52 +00007081 Result += "\t0,\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007082
Fariborz Jahanian82848c22012-02-08 00:50:52 +00007083 if (OptInstanceMethods.size() > 0) {
7084 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_";
7085 Result += PDecl->getNameAsString(); Result += ",\n";
7086 }
7087 else
7088 Result += "\t0,\n";
7089
7090 if (OptClassMethods.size() > 0) {
7091 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_CLASS_METHODS_";
7092 Result += PDecl->getNameAsString(); Result += ",\n";
7093 }
7094 else
7095 Result += "\t0,\n";
7096
7097 if (ProtocolProperties.size() > 0) {
7098 Result += "\t(const struct _prop_list_t *)&_OBJC_PROTOCOL_PROPERTIES_";
7099 Result += PDecl->getNameAsString(); Result += ",\n";
7100 }
7101 else
7102 Result += "\t0,\n";
7103
7104 Result += "\t"; Result += "sizeof(_protocol_t)"; Result += ",\n";
7105 Result += "\t0,\n";
7106
Fariborz Jahaniane0adbd82012-02-08 22:23:26 +00007107 if (AllMethods.size() > 0) {
7108 Result += "\t(const char **)&"; Result += "_OBJC_PROTOCOL_METHOD_TYPES_";
7109 Result += PDecl->getNameAsString();
7110 Result += "\n};\n";
7111 }
7112 else
7113 Result += "\t0\n};\n";
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00007114
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00007115 if (LangOpts.MicrosoftExt)
Fariborz Jahanian8590d862012-04-14 17:13:08 +00007116 Result += "static ";
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00007117 Result += "struct _protocol_t *";
7118 Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString();
7119 Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString();
7120 Result += ";\n";
Fariborz Jahanian82848c22012-02-08 00:50:52 +00007121
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007122 // Mark this protocol as having been generated.
7123 if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl()))
7124 llvm_unreachable("protocol already synthesized");
7125
7126}
7127
7128void RewriteModernObjC::RewriteObjCProtocolListMetaData(
7129 const ObjCList<ObjCProtocolDecl> &Protocols,
7130 StringRef prefix, StringRef ClassName,
7131 std::string &Result) {
7132 if (Protocols.empty()) return;
7133
7134 for (unsigned i = 0; i != Protocols.size(); i++)
Fariborz Jahanianda9624a2012-02-08 19:53:58 +00007135 RewriteObjCProtocolMetaData(Protocols[i], Result);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007136
7137 // Output the top lovel protocol meta-data for the class.
7138 /* struct _objc_protocol_list {
7139 struct _objc_protocol_list *next;
7140 int protocol_count;
7141 struct _objc_protocol *class_protocols[];
7142 }
7143 */
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00007144 Result += "\n";
7145 if (LangOpts.MicrosoftExt)
7146 Result += "__declspec(allocate(\".cat_cls_meth$B\")) ";
7147 Result += "static struct {\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007148 Result += "\tstruct _objc_protocol_list *next;\n";
7149 Result += "\tint protocol_count;\n";
7150 Result += "\tstruct _objc_protocol *class_protocols[";
7151 Result += utostr(Protocols.size());
7152 Result += "];\n} _OBJC_";
7153 Result += prefix;
7154 Result += "_PROTOCOLS_";
7155 Result += ClassName;
7156 Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
7157 "{\n\t0, ";
7158 Result += utostr(Protocols.size());
7159 Result += "\n";
7160
7161 Result += "\t,{&_OBJC_PROTOCOL_";
7162 Result += Protocols[0]->getNameAsString();
7163 Result += " \n";
7164
7165 for (unsigned i = 1; i != Protocols.size(); i++) {
7166 Result += "\t ,&_OBJC_PROTOCOL_";
7167 Result += Protocols[i]->getNameAsString();
7168 Result += "\n";
7169 }
7170 Result += "\t }\n};\n";
7171}
7172
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00007173/// hasObjCExceptionAttribute - Return true if this class or any super
7174/// class has the __objc_exception__ attribute.
7175/// FIXME. Move this to ASTContext.cpp as it is also used for IRGen.
7176static bool hasObjCExceptionAttribute(ASTContext &Context,
7177 const ObjCInterfaceDecl *OID) {
7178 if (OID->hasAttr<ObjCExceptionAttr>())
7179 return true;
7180 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
7181 return hasObjCExceptionAttribute(Context, Super);
7182 return false;
7183}
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007184
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007185void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
7186 std::string &Result) {
7187 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
7188
7189 // Explicitly declared @interface's are already synthesized.
Fariborz Jahanianae932952012-02-10 20:47:10 +00007190 if (CDecl->isImplicitInterfaceDecl())
7191 assert(false &&
7192 "Legacy implicit interface rewriting not supported in moder abi");
Fariborz Jahanian8f1fed02012-02-11 20:10:52 +00007193
Fariborz Jahaniande5d9462012-03-14 18:09:23 +00007194 WriteModernMetadataDeclarations(Context, Result);
Fariborz Jahanianae932952012-02-10 20:47:10 +00007195 SmallVector<ObjCIvarDecl *, 8> IVars;
7196
7197 for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
7198 IVD; IVD = IVD->getNextIvar()) {
7199 // Ignore unnamed bit-fields.
7200 if (!IVD->getDeclName())
7201 continue;
7202 IVars.push_back(IVD);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007203 }
7204
Fariborz Jahanianae932952012-02-10 20:47:10 +00007205 Write__ivar_list_t_initializer(*this, Context, Result, IVars,
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007206 "_OBJC_$_INSTANCE_VARIABLES_",
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00007207 CDecl);
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007208
7209 // Build _objc_method_list for class's instance methods if needed
Stephen Hines651f13c2014-04-23 16:59:28 -07007210 SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods());
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007211
7212 // If any of our property implementations have associated getters or
7213 // setters, produce metadata for them as well.
Stephen Hines651f13c2014-04-23 16:59:28 -07007214 for (const auto *Prop : IDecl->property_impls()) {
David Blaikie262bc182012-04-30 02:36:29 +00007215 if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007216 continue;
David Blaikie262bc182012-04-30 02:36:29 +00007217 if (!Prop->getPropertyIvarDecl())
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007218 continue;
David Blaikie262bc182012-04-30 02:36:29 +00007219 ObjCPropertyDecl *PD = Prop->getPropertyDecl();
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007220 if (!PD)
7221 continue;
7222 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
Fariborz Jahanian301e2e42012-05-03 22:52:13 +00007223 if (mustSynthesizeSetterGetterMethod(IDecl, PD, true /*getter*/))
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007224 InstanceMethods.push_back(Getter);
7225 if (PD->isReadOnly())
7226 continue;
7227 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
Fariborz Jahanian301e2e42012-05-03 22:52:13 +00007228 if (mustSynthesizeSetterGetterMethod(IDecl, PD, false /*setter*/))
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007229 InstanceMethods.push_back(Setter);
7230 }
7231
7232 Write_method_list_t_initializer(*this, Context, Result, InstanceMethods,
7233 "_OBJC_$_INSTANCE_METHODS_",
7234 IDecl->getNameAsString(), true);
7235
Stephen Hines651f13c2014-04-23 16:59:28 -07007236 SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods());
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007237
7238 Write_method_list_t_initializer(*this, Context, Result, ClassMethods,
7239 "_OBJC_$_CLASS_METHODS_",
7240 IDecl->getNameAsString(), true);
Fariborz Jahanian0a525342012-02-14 19:31:35 +00007241
7242 // Protocols referenced in class declaration?
7243 // Protocol's super protocol list
7244 std::vector<ObjCProtocolDecl *> RefedProtocols;
7245 const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols();
7246 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
7247 E = Protocols.end();
7248 I != E; ++I) {
7249 RefedProtocols.push_back(*I);
7250 // Must write out all protocol definitions in current qualifier list,
7251 // and in their nested qualifiers before writing out current definition.
7252 RewriteObjCProtocolMetaData(*I, Result);
7253 }
7254
7255 Write_protocol_list_initializer(Context, Result,
7256 RefedProtocols,
7257 "_OBJC_CLASS_PROTOCOLS_$_",
7258 IDecl->getNameAsString());
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007259
7260 // Protocol's property metadata.
Stephen Hines651f13c2014-04-23 16:59:28 -07007261 SmallVector<ObjCPropertyDecl *, 8> ClassProperties(CDecl->properties());
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007262 Write_prop_list_t_initializer(*this, Context, Result, ClassProperties,
Fariborz Jahanian2df089d2012-03-22 17:39:35 +00007263 /* Container */IDecl,
Fariborz Jahanianeeabf382012-02-16 21:57:59 +00007264 "_OBJC_$_PROP_LIST_",
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007265 CDecl->getNameAsString());
Fariborz Jahanian90af4e22012-02-14 17:19:02 +00007266
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007267
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00007268 // Data for initializing _class_ro_t metaclass meta-data
7269 uint32_t flags = CLS_META;
7270 std::string InstanceSize;
7271 std::string InstanceStart;
7272
7273
7274 bool classIsHidden = CDecl->getVisibility() == HiddenVisibility;
7275 if (classIsHidden)
7276 flags |= OBJC2_CLS_HIDDEN;
7277
7278 if (!CDecl->getSuperClass())
7279 // class is root
7280 flags |= CLS_ROOT;
7281 InstanceSize = "sizeof(struct _class_t)";
7282 InstanceStart = InstanceSize;
7283 Write__class_ro_t_initializer(Context, Result, flags,
7284 InstanceStart, InstanceSize,
7285 ClassMethods,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007286 nullptr,
7287 nullptr,
7288 nullptr,
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00007289 "_OBJC_METACLASS_RO_$_",
7290 CDecl->getNameAsString());
7291
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007292 // Data for initializing _class_ro_t meta-data
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00007293 flags = CLS;
7294 if (classIsHidden)
7295 flags |= OBJC2_CLS_HIDDEN;
7296
7297 if (hasObjCExceptionAttribute(*Context, CDecl))
7298 flags |= CLS_EXCEPTION;
7299
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007300 if (!CDecl->getSuperClass())
7301 // class is root
7302 flags |= CLS_ROOT;
7303
Fariborz Jahanian6ade3432012-02-16 18:54:09 +00007304 InstanceSize.clear();
7305 InstanceStart.clear();
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007306 if (!ObjCSynthesizedStructs.count(CDecl)) {
7307 InstanceSize = "0";
7308 InstanceStart = "0";
7309 }
7310 else {
7311 InstanceSize = "sizeof(struct ";
7312 InstanceSize += CDecl->getNameAsString();
7313 InstanceSize += "_IMPL)";
7314
7315 ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
7316 if (IVD) {
Fariborz Jahanianacee1c92012-04-11 21:12:36 +00007317 RewriteIvarOffsetComputation(IVD, InstanceStart);
Fariborz Jahanianf1c1d9a2012-02-15 00:50:11 +00007318 }
7319 else
7320 InstanceStart = InstanceSize;
7321 }
7322 Write__class_ro_t_initializer(Context, Result, flags,
7323 InstanceStart, InstanceSize,
7324 InstanceMethods,
7325 RefedProtocols,
7326 IVars,
7327 ClassProperties,
7328 "_OBJC_CLASS_RO_$_",
7329 CDecl->getNameAsString());
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00007330
7331 Write_class_t(Context, Result,
7332 "OBJC_METACLASS_$_",
7333 CDecl, /*metaclass*/true);
7334
7335 Write_class_t(Context, Result,
7336 "OBJC_CLASS_$_",
7337 CDecl, /*metaclass*/false);
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00007338
7339 if (ImplementationIsNonLazy(IDecl))
7340 DefinedNonLazyClasses.push_back(CDecl);
Fariborz Jahanian3f77c7b2012-02-16 21:37:05 +00007341
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007342}
7343
Fariborz Jahaniane0335782012-03-27 18:41:05 +00007344void RewriteModernObjC::RewriteClassSetupInitHook(std::string &Result) {
7345 int ClsDefCount = ClassImplementation.size();
7346 if (!ClsDefCount)
7347 return;
7348 Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n";
7349 Result += "__declspec(allocate(\".objc_inithooks$B\")) ";
7350 Result += "static void *OBJC_CLASS_SETUP[] = {\n";
7351 for (int i = 0; i < ClsDefCount; i++) {
7352 ObjCImplementationDecl *IDecl = ClassImplementation[i];
7353 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
7354 Result += "\t(void *)&OBJC_CLASS_SETUP_$_";
7355 Result += CDecl->getName(); Result += ",\n";
7356 }
7357 Result += "};\n";
7358}
7359
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007360void RewriteModernObjC::RewriteMetaDataIntoBuffer(std::string &Result) {
7361 int ClsDefCount = ClassImplementation.size();
7362 int CatDefCount = CategoryImplementation.size();
7363
7364 // For each implemented class, write out all its meta data.
7365 for (int i = 0; i < ClsDefCount; i++)
7366 RewriteObjCClassMetaData(ClassImplementation[i], Result);
7367
Fariborz Jahaniane0335782012-03-27 18:41:05 +00007368 RewriteClassSetupInitHook(Result);
7369
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007370 // For each implemented category, write out all its meta data.
7371 for (int i = 0; i < CatDefCount; i++)
7372 RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
7373
Fariborz Jahaniane0335782012-03-27 18:41:05 +00007374 RewriteCategorySetupInitHook(Result);
7375
Fariborz Jahaniandf795672012-02-17 00:06:14 +00007376 if (ClsDefCount > 0) {
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00007377 if (LangOpts.MicrosoftExt)
7378 Result += "__declspec(allocate(\".objc_classlist$B\")) ";
Fariborz Jahaniandf795672012-02-17 00:06:14 +00007379 Result += "static struct _class_t *L_OBJC_LABEL_CLASS_$ [";
7380 Result += llvm::utostr(ClsDefCount); Result += "]";
7381 Result +=
7382 " __attribute__((used, section (\"__DATA, __objc_classlist,"
7383 "regular,no_dead_strip\")))= {\n";
7384 for (int i = 0; i < ClsDefCount; i++) {
7385 Result += "\t&OBJC_CLASS_$_";
7386 Result += ClassImplementation[i]->getNameAsString();
7387 Result += ",\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007388 }
Fariborz Jahaniandf795672012-02-17 00:06:14 +00007389 Result += "};\n";
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00007390
7391 if (!DefinedNonLazyClasses.empty()) {
7392 if (LangOpts.MicrosoftExt)
7393 Result += "__declspec(allocate(\".objc_nlclslist$B\")) \n";
7394 Result += "static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {\n\t";
7395 for (unsigned i = 0, e = DefinedNonLazyClasses.size(); i < e; i++) {
7396 Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString();
7397 Result += ",\n";
7398 }
7399 Result += "};\n";
7400 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007401 }
Fariborz Jahanian61186122012-02-17 18:40:41 +00007402
7403 if (CatDefCount > 0) {
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00007404 if (LangOpts.MicrosoftExt)
7405 Result += "__declspec(allocate(\".objc_catlist$B\")) ";
Fariborz Jahanian61186122012-02-17 18:40:41 +00007406 Result += "static struct _category_t *L_OBJC_LABEL_CATEGORY_$ [";
7407 Result += llvm::utostr(CatDefCount); Result += "]";
7408 Result +=
7409 " __attribute__((used, section (\"__DATA, __objc_catlist,"
7410 "regular,no_dead_strip\")))= {\n";
7411 for (int i = 0; i < CatDefCount; i++) {
7412 Result += "\t&_OBJC_$_CATEGORY_";
7413 Result +=
7414 CategoryImplementation[i]->getClassInterface()->getNameAsString();
7415 Result += "_$_";
7416 Result += CategoryImplementation[i]->getNameAsString();
7417 Result += ",\n";
7418 }
7419 Result += "};\n";
7420 }
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00007421
7422 if (!DefinedNonLazyCategories.empty()) {
7423 if (LangOpts.MicrosoftExt)
7424 Result += "__declspec(allocate(\".objc_nlcatlist$B\")) \n";
7425 Result += "static struct _category_t *_OBJC_LABEL_NONLAZY_CATEGORY_$[] = {\n\t";
7426 for (unsigned i = 0, e = DefinedNonLazyCategories.size(); i < e; i++) {
7427 Result += "\t&_OBJC_$_CATEGORY_";
7428 Result +=
7429 DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString();
7430 Result += "_$_";
7431 Result += DefinedNonLazyCategories[i]->getNameAsString();
7432 Result += ",\n";
7433 }
7434 Result += "};\n";
7435 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007436}
7437
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00007438void RewriteModernObjC::WriteImageInfo(std::string &Result) {
7439 if (LangOpts.MicrosoftExt)
7440 Result += "__declspec(allocate(\".objc_imageinfo$B\")) \n";
7441
7442 Result += "static struct IMAGE_INFO { unsigned version; unsigned flag; } ";
7443 // version 0, ObjCABI is 2
Fariborz Jahanian30650eb2012-03-15 17:05:33 +00007444 Result += "_OBJC_IMAGE_INFO = { 0, 2 };\n";
Fariborz Jahanian10cde2f2012-03-14 21:44:09 +00007445}
7446
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007447/// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
7448/// implementation.
7449void RewriteModernObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
7450 std::string &Result) {
Fariborz Jahaniande5d9462012-03-14 18:09:23 +00007451 WriteModernMetadataDeclarations(Context, Result);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007452 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
7453 // Find category declaration for this implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00007454 ObjCCategoryDecl *CDecl
7455 = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007456
7457 std::string FullCategoryName = ClassDecl->getNameAsString();
Fariborz Jahanian61186122012-02-17 18:40:41 +00007458 FullCategoryName += "_$_";
7459 FullCategoryName += CDecl->getNameAsString();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007460
7461 // Build _objc_method_list for class's instance methods if needed
Stephen Hines651f13c2014-04-23 16:59:28 -07007462 SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007463
7464 // If any of our property implementations have associated getters or
7465 // setters, produce metadata for them as well.
Stephen Hines651f13c2014-04-23 16:59:28 -07007466 for (const auto *Prop : IDecl->property_impls()) {
David Blaikie262bc182012-04-30 02:36:29 +00007467 if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007468 continue;
David Blaikie262bc182012-04-30 02:36:29 +00007469 if (!Prop->getPropertyIvarDecl())
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007470 continue;
David Blaikie262bc182012-04-30 02:36:29 +00007471 ObjCPropertyDecl *PD = Prop->getPropertyDecl();
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007472 if (!PD)
7473 continue;
7474 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
7475 InstanceMethods.push_back(Getter);
7476 if (PD->isReadOnly())
7477 continue;
7478 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
7479 InstanceMethods.push_back(Setter);
7480 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007481
Fariborz Jahanian61186122012-02-17 18:40:41 +00007482 Write_method_list_t_initializer(*this, Context, Result, InstanceMethods,
7483 "_OBJC_$_CATEGORY_INSTANCE_METHODS_",
7484 FullCategoryName, true);
7485
Stephen Hines651f13c2014-04-23 16:59:28 -07007486 SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods());
Fariborz Jahanian61186122012-02-17 18:40:41 +00007487
7488 Write_method_list_t_initializer(*this, Context, Result, ClassMethods,
7489 "_OBJC_$_CATEGORY_CLASS_METHODS_",
7490 FullCategoryName, true);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007491
7492 // Protocols referenced in class declaration?
Fariborz Jahanian61186122012-02-17 18:40:41 +00007493 // Protocol's super protocol list
Stephen Hines651f13c2014-04-23 16:59:28 -07007494 SmallVector<ObjCProtocolDecl *, 8> RefedProtocols(CDecl->protocols());
7495 for (auto *I : CDecl->protocols())
Fariborz Jahanian61186122012-02-17 18:40:41 +00007496 // Must write out all protocol definitions in current qualifier list,
7497 // and in their nested qualifiers before writing out current definition.
Stephen Hines651f13c2014-04-23 16:59:28 -07007498 RewriteObjCProtocolMetaData(I, Result);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007499
Fariborz Jahanian61186122012-02-17 18:40:41 +00007500 Write_protocol_list_initializer(Context, Result,
7501 RefedProtocols,
7502 "_OBJC_CATEGORY_PROTOCOLS_$_",
7503 FullCategoryName);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007504
Fariborz Jahanian61186122012-02-17 18:40:41 +00007505 // Protocol's property metadata.
Stephen Hines651f13c2014-04-23 16:59:28 -07007506 SmallVector<ObjCPropertyDecl *, 8> ClassProperties(CDecl->properties());
Fariborz Jahanian61186122012-02-17 18:40:41 +00007507 Write_prop_list_t_initializer(*this, Context, Result, ClassProperties,
Fariborz Jahanianebfa2722012-05-03 23:19:33 +00007508 /* Container */IDecl,
Fariborz Jahanian61186122012-02-17 18:40:41 +00007509 "_OBJC_$_PROP_LIST_",
7510 FullCategoryName);
7511
7512 Write_category_t(*this, Context, Result,
Fariborz Jahaniane0335782012-03-27 18:41:05 +00007513 CDecl,
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00007514 ClassDecl,
Fariborz Jahanian61186122012-02-17 18:40:41 +00007515 InstanceMethods,
7516 ClassMethods,
7517 RefedProtocols,
7518 ClassProperties);
7519
Fariborz Jahanian88f7f752012-03-14 23:18:19 +00007520 // Determine if this category is also "non-lazy".
7521 if (ImplementationIsNonLazy(IDecl))
7522 DefinedNonLazyCategories.push_back(CDecl);
Fariborz Jahaniane0335782012-03-27 18:41:05 +00007523
7524}
7525
7526void RewriteModernObjC::RewriteCategorySetupInitHook(std::string &Result) {
7527 int CatDefCount = CategoryImplementation.size();
7528 if (!CatDefCount)
7529 return;
7530 Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n";
7531 Result += "__declspec(allocate(\".objc_inithooks$B\")) ";
7532 Result += "static void *OBJC_CATEGORY_SETUP[] = {\n";
7533 for (int i = 0; i < CatDefCount; i++) {
7534 ObjCCategoryImplDecl *IDecl = CategoryImplementation[i];
7535 ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl();
7536 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
7537 Result += "\t(void *)&OBJC_CATEGORY_SETUP_$_";
7538 Result += ClassDecl->getName();
7539 Result += "_$_";
7540 Result += CatDecl->getName();
7541 Result += ",\n";
7542 }
7543 Result += "};\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007544}
7545
7546// RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
7547/// class methods.
7548template<typename MethodIterator>
7549void RewriteModernObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
7550 MethodIterator MethodEnd,
7551 bool IsInstanceMethod,
7552 StringRef prefix,
7553 StringRef ClassName,
7554 std::string &Result) {
7555 if (MethodBegin == MethodEnd) return;
7556
7557 if (!objc_impl_method) {
7558 /* struct _objc_method {
7559 SEL _cmd;
7560 char *method_types;
7561 void *_imp;
7562 }
7563 */
7564 Result += "\nstruct _objc_method {\n";
7565 Result += "\tSEL _cmd;\n";
7566 Result += "\tchar *method_types;\n";
7567 Result += "\tvoid *_imp;\n";
7568 Result += "};\n";
7569
7570 objc_impl_method = true;
7571 }
7572
7573 // Build _objc_method_list for class's methods if needed
7574
7575 /* struct {
7576 struct _objc_method_list *next_method;
7577 int method_count;
7578 struct _objc_method method_list[];
7579 }
7580 */
7581 unsigned NumMethods = std::distance(MethodBegin, MethodEnd);
Fariborz Jahanian1ca052c2012-03-11 19:41:56 +00007582 Result += "\n";
7583 if (LangOpts.MicrosoftExt) {
7584 if (IsInstanceMethod)
7585 Result += "__declspec(allocate(\".inst_meth$B\")) ";
7586 else
7587 Result += "__declspec(allocate(\".cls_meth$B\")) ";
7588 }
7589 Result += "static struct {\n";
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007590 Result += "\tstruct _objc_method_list *next_method;\n";
7591 Result += "\tint method_count;\n";
7592 Result += "\tstruct _objc_method method_list[";
7593 Result += utostr(NumMethods);
7594 Result += "];\n} _OBJC_";
7595 Result += prefix;
7596 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
7597 Result += "_METHODS_";
7598 Result += ClassName;
7599 Result += " __attribute__ ((used, section (\"__OBJC, __";
7600 Result += IsInstanceMethod ? "inst" : "cls";
7601 Result += "_meth\")))= ";
7602 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
7603
7604 Result += "\t,{{(SEL)\"";
7605 Result += (*MethodBegin)->getSelector().getAsString().c_str();
7606 std::string MethodTypeString;
7607 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
7608 Result += "\", \"";
7609 Result += MethodTypeString;
7610 Result += "\", (void *)";
7611 Result += MethodInternalNames[*MethodBegin];
7612 Result += "}\n";
7613 for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
7614 Result += "\t ,{(SEL)\"";
7615 Result += (*MethodBegin)->getSelector().getAsString().c_str();
7616 std::string MethodTypeString;
7617 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
7618 Result += "\", \"";
7619 Result += MethodTypeString;
7620 Result += "\", (void *)";
7621 Result += MethodInternalNames[*MethodBegin];
7622 Result += "}\n";
7623 }
7624 Result += "\t }\n};\n";
7625}
7626
7627Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) {
7628 SourceRange OldRange = IV->getSourceRange();
7629 Expr *BaseExpr = IV->getBase();
7630
7631 // Rewrite the base, but without actually doing replaces.
7632 {
7633 DisableReplaceStmtScope S(*this);
7634 BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr));
7635 IV->setBase(BaseExpr);
7636 }
7637
7638 ObjCIvarDecl *D = IV->getDecl();
7639
7640 Expr *Replacement = IV;
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007641
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007642 if (BaseExpr->getType()->isObjCObjectPointerType()) {
7643 const ObjCInterfaceType *iFaceDecl =
Fariborz Jahanian163d3ce2012-05-08 23:54:35 +00007644 dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType());
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007645 assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null");
7646 // lookup which class implements the instance variable.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007647 ObjCInterfaceDecl *clsDeclared = nullptr;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007648 iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
7649 clsDeclared);
7650 assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
7651
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007652 // Build name of symbol holding ivar offset.
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00007653 std::string IvarOffsetName;
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00007654 if (D->isBitField())
7655 ObjCIvarBitfieldGroupOffset(D, IvarOffsetName);
7656 else
7657 WriteInternalIvarName(clsDeclared, D, IvarOffsetName);
Fariborz Jahanian7cb2a1b2012-03-20 17:13:39 +00007658
Fariborz Jahanian72c88f12012-02-22 18:13:25 +00007659 ReferencedIvars[clsDeclared].insert(D);
7660
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007661 // cast offset to "char *".
7662 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context,
7663 Context->getPointerType(Context->CharTy),
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007664 CK_BitCast,
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007665 BaseExpr);
7666 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
7667 SourceLocation(), &Context->Idents.get(IvarOffsetName),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007668 Context->UnsignedLongTy, nullptr,
7669 SC_Extern);
John McCallf4b88a42012-03-10 09:33:50 +00007670 DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false,
7671 Context->UnsignedLongTy, VK_LValue,
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007672 SourceLocation());
7673 BinaryOperator *addExpr =
7674 new (Context) BinaryOperator(castExpr, DRE, BO_Add,
7675 Context->getPointerType(Context->CharTy),
Lang Hamesbe9af122012-10-02 04:45:10 +00007676 VK_RValue, OK_Ordinary, SourceLocation(), false);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007677 // Don't forget the parens to enforce the proper binding.
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007678 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(),
7679 SourceLocation(),
7680 addExpr);
Fariborz Jahanian0d6e22a2012-02-24 17:35:35 +00007681 QualType IvarT = D->getType();
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00007682 if (D->isBitField())
7683 IvarT = GetGroupRecordTypeForObjCIvarBitfield(D);
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007684
Fariborz Jahanianf5eac482012-05-02 17:34:59 +00007685 if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) {
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007686 RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl();
Fariborz Jahanian8fba8942012-04-30 23:20:30 +00007687 RD = RD->getDefinition();
7688 if (RD && !RD->getDeclName().getAsIdentifierInfo()) {
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007689 // decltype(((Foo_IMPL*)0)->bar) *
Fariborz Jahanianf5eac482012-05-02 17:34:59 +00007690 ObjCContainerDecl *CDecl =
7691 dyn_cast<ObjCContainerDecl>(D->getDeclContext());
7692 // ivar in class extensions requires special treatment.
7693 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
7694 CDecl = CatDecl->getClassInterface();
7695 std::string RecName = CDecl->getName();
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007696 RecName += "_IMPL";
7697 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
7698 SourceLocation(), SourceLocation(),
7699 &Context->Idents.get(RecName.c_str()));
7700 QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD));
7701 unsigned UnsignedIntSize =
7702 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
7703 Expr *Zero = IntegerLiteral::Create(*Context,
7704 llvm::APInt(UnsignedIntSize, 0),
7705 Context->UnsignedIntTy, SourceLocation());
7706 Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero);
7707 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
7708 Zero);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007709 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007710 SourceLocation(),
7711 &Context->Idents.get(D->getNameAsString()),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007712 IvarT, nullptr,
7713 /*BitWidth=*/nullptr,
7714 /*Mutable=*/true, ICIS_NoInit);
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007715 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
7716 FD->getType(), VK_LValue,
7717 OK_Ordinary);
7718 IvarT = Context->getDecltypeType(ME, ME->getType());
7719 }
7720 }
Fariborz Jahanian8e0913d2012-02-29 00:26:20 +00007721 convertObjCTypeToCStyleType(IvarT);
Fariborz Jahanian0d6e22a2012-02-24 17:35:35 +00007722 QualType castT = Context->getPointerType(IvarT);
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007723
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007724 castExpr = NoTypeInfoCStyleCastExpr(Context,
7725 castT,
7726 CK_BitCast,
7727 PE);
Fariborz Jahanian27fc81b2012-04-27 22:48:54 +00007728
7729
Fariborz Jahanian8e0913d2012-02-29 00:26:20 +00007730 Expr *Exp = new (Context) UnaryOperator(castExpr, UO_Deref, IvarT,
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007731 VK_LValue, OK_Ordinary,
7732 SourceLocation());
7733 PE = new (Context) ParenExpr(OldRange.getBegin(),
7734 OldRange.getEnd(),
7735 Exp);
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00007736
7737 if (D->isBitField()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007738 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00007739 SourceLocation(),
7740 &Context->Idents.get(D->getNameAsString()),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007741 D->getType(), nullptr,
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00007742 /*BitWidth=*/D->getBitWidth(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007743 /*Mutable=*/true, ICIS_NoInit);
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00007744 MemberExpr *ME = new (Context) MemberExpr(PE, /*isArrow*/false, FD, SourceLocation(),
7745 FD->getType(), VK_LValue,
7746 OK_Ordinary);
7747 Replacement = ME;
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007748
Fariborz Jahaniancd3b0362013-02-07 01:53:15 +00007749 }
7750 else
7751 Replacement = PE;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007752 }
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007753
Fariborz Jahaniane7b3fa72012-02-21 23:46:48 +00007754 ReplaceStmtWithRange(IV, Replacement, OldRange);
7755 return Replacement;
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +00007756}