1 //===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Hacks and fun related to the code rewriter.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Rewrite/Frontend/ASTConsumers.h"
15 #include "clang/AST/AST.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/ParentMap.h"
19 #include "clang/Basic/CharInfo.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/IdentifierTable.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/Lex/Lexer.h"
25 #include "clang/Rewrite/Core/Rewriter.h"
26 #include "llvm/ADT/DenseSet.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
33 #ifdef CLANG_ENABLE_OBJC_REWRITER
35 using namespace clang;
39 class RewriteModernObjC : public ASTConsumer {
43 BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)),
45 BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */
46 BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the
48 BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy
50 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose
52 BLOCK_BYREF_CURRENT_MAX = 256
56 BLOCK_NEEDS_FREE = (1 << 24),
57 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
58 BLOCK_HAS_CXX_OBJ = (1 << 26),
59 BLOCK_IS_GC = (1 << 27),
60 BLOCK_IS_GLOBAL = (1 << 28),
61 BLOCK_HAS_DESCRIPTOR = (1 << 29)
65 DiagnosticsEngine &Diags;
66 const LangOptions &LangOpts;
69 TranslationUnitDecl *TUDecl;
71 const char *MainFileStart, *MainFileEnd;
73 ParentMap *PropParentMap; // created lazily.
74 std::string InFileName;
78 TypeDecl *ProtocolTypeDecl;
79 VarDecl *GlobalVarDecl;
80 Expr *GlobalConstructionExp;
81 unsigned RewriteFailedDiag;
82 unsigned GlobalBlockRewriteFailedDiag;
83 // ObjC string constant support.
84 unsigned NumObjCStringLiterals;
85 VarDecl *ConstantStringClassReference;
86 RecordDecl *NSStringRecord;
88 // ObjC foreach break/continue generation support.
91 unsigned TryFinallyContainsReturnDiag;
93 ObjCMethodDecl *CurMethodDef;
94 RecordDecl *SuperStructDecl;
95 RecordDecl *ConstantStringDecl;
97 FunctionDecl *MsgSendFunctionDecl;
98 FunctionDecl *MsgSendSuperFunctionDecl;
99 FunctionDecl *MsgSendStretFunctionDecl;
100 FunctionDecl *MsgSendSuperStretFunctionDecl;
101 FunctionDecl *MsgSendFpretFunctionDecl;
102 FunctionDecl *GetClassFunctionDecl;
103 FunctionDecl *GetMetaClassFunctionDecl;
104 FunctionDecl *GetSuperClassFunctionDecl;
105 FunctionDecl *SelGetUidFunctionDecl;
106 FunctionDecl *CFStringFunctionDecl;
107 FunctionDecl *SuperConstructorFunctionDecl;
108 FunctionDecl *CurFunctionDef;
110 /* Misc. containers needed for meta-data rewrite. */
111 SmallVector<ObjCImplementationDecl *, 8> ClassImplementation;
112 SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation;
113 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs;
114 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols;
115 llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCWrittenInterfaces;
116 llvm::SmallPtrSet<TagDecl*, 32> GlobalDefinedTags;
117 SmallVector<ObjCInterfaceDecl*, 32> ObjCInterfacesSeen;
118 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
119 SmallVector<ObjCInterfaceDecl*, 8> DefinedNonLazyClasses;
121 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
122 SmallVector<ObjCCategoryDecl *, 8> DefinedNonLazyCategories;
124 SmallVector<Stmt *, 32> Stmts;
125 SmallVector<int, 8> ObjCBcLabelNo;
126 // Remember all the @protocol(<expr>) expressions.
127 llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls;
129 llvm::DenseSet<uint64_t> CopyDestroyCache;
131 // Block expressions.
132 SmallVector<BlockExpr *, 32> Blocks;
133 SmallVector<int, 32> InnerDeclRefsCount;
134 SmallVector<DeclRefExpr *, 32> InnerDeclRefs;
136 SmallVector<DeclRefExpr *, 32> BlockDeclRefs;
139 // Block related declarations.
140 SmallVector<ValueDecl *, 8> BlockByCopyDecls;
141 llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet;
142 SmallVector<ValueDecl *, 8> BlockByRefDecls;
143 llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet;
144 llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo;
145 llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls;
146 llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls;
148 llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs;
149 llvm::DenseMap<ObjCInterfaceDecl *,
150 llvm::SmallPtrSet<ObjCIvarDecl *, 8> > ReferencedIvars;
152 // ivar bitfield grouping containers
153 llvm::DenseSet<const ObjCInterfaceDecl *> ObjCInterefaceHasBitfieldGroups;
154 llvm::DenseMap<const ObjCIvarDecl* , unsigned> IvarGroupNumber;
155 // This container maps an <class, group number for ivar> tuple to the type
156 // of the struct where the bitfield belongs.
157 llvm::DenseMap<std::pair<const ObjCInterfaceDecl*, unsigned>, QualType> GroupRecordType;
158 SmallVector<FunctionDecl*, 32> FunctionDefinitionsSeen;
160 // This maps an original source AST to it's rewritten form. This allows
161 // us to avoid rewriting the same node twice (which is very uncommon).
162 // This is needed to support some of the exotic property rewriting.
163 llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes;
165 // Needed for header files being rewritten
167 bool SilenceRewriteMacroWarning;
168 bool GenerateLineInfo;
169 bool objc_impl_method;
171 bool DisableReplaceStmt;
172 class DisableReplaceStmtScope {
173 RewriteModernObjC &R;
177 DisableReplaceStmtScope(RewriteModernObjC &R)
178 : R(R), SavedValue(R.DisableReplaceStmt) {
179 R.DisableReplaceStmt = true;
181 ~DisableReplaceStmtScope() {
182 R.DisableReplaceStmt = SavedValue;
185 void InitializeCommon(ASTContext &context);
188 llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames;
189 // Top Level Driver code.
190 bool HandleTopLevelDecl(DeclGroupRef D) override {
191 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
192 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*I)) {
193 if (!Class->isThisDeclarationADefinition()) {
194 RewriteForwardClassDecl(D);
197 // Keep track of all interface declarations seen.
198 ObjCInterfacesSeen.push_back(Class);
203 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*I)) {
204 if (!Proto->isThisDeclarationADefinition()) {
205 RewriteForwardProtocolDecl(D);
210 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(*I)) {
211 // Under modern abi, we cannot translate body of the function
212 // yet until all class extensions and its implementation is seen.
213 // This is because they may introduce new bitfields which must go
214 // into their grouping struct.
215 if (FDecl->isThisDeclarationADefinition() &&
216 // Not c functions defined inside an objc container.
217 !FDecl->isTopLevelDeclInObjCContainer()) {
218 FunctionDefinitionsSeen.push_back(FDecl);
222 HandleTopLevelSingleDecl(*I);
227 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
228 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
229 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(*I)) {
230 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
231 RewriteBlockPointerDecl(TD);
232 else if (TD->getUnderlyingType()->isFunctionPointerType())
233 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
235 RewriteObjCQualifiedInterfaceTypes(TD);
241 void HandleTopLevelSingleDecl(Decl *D);
242 void HandleDeclInMainFile(Decl *D);
243 RewriteModernObjC(std::string inFile, raw_ostream *OS,
244 DiagnosticsEngine &D, const LangOptions &LOpts,
245 bool silenceMacroWarn, bool LineInfo);
247 ~RewriteModernObjC() {}
249 void HandleTranslationUnit(ASTContext &C) override;
251 void ReplaceStmt(Stmt *Old, Stmt *New) {
252 Stmt *ReplacingStmt = ReplacedNodes[Old];
255 return; // We can't rewrite the same node twice.
257 if (DisableReplaceStmt)
260 // If replacement succeeded or warning disabled return with no warning.
261 if (!Rewrite.ReplaceStmt(Old, New)) {
262 ReplacedNodes[Old] = New;
265 if (SilenceRewriteMacroWarning)
267 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
268 << Old->getSourceRange();
271 void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) {
272 assert(Old != nullptr && New != nullptr && "Expected non-null Stmt's");
273 if (DisableReplaceStmt)
276 // Measure the old text.
277 int Size = Rewrite.getRangeSize(SrcRange);
279 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
280 << Old->getSourceRange();
285 llvm::raw_string_ostream S(SStr);
286 New->printPretty(S, nullptr, PrintingPolicy(LangOpts));
287 const std::string &Str = S.str();
289 // If replacement succeeded or warning disabled return with no warning.
290 if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) {
291 ReplacedNodes[Old] = New;
294 if (SilenceRewriteMacroWarning)
296 Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag)
297 << Old->getSourceRange();
300 void InsertText(SourceLocation Loc, StringRef Str,
301 bool InsertAfter = true) {
302 // If insertion succeeded or warning disabled return with no warning.
303 if (!Rewrite.InsertText(Loc, Str, InsertAfter) ||
304 SilenceRewriteMacroWarning)
307 Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
310 void ReplaceText(SourceLocation Start, unsigned OrigLength,
312 // If removal succeeded or warning disabled return with no warning.
313 if (!Rewrite.ReplaceText(Start, OrigLength, Str) ||
314 SilenceRewriteMacroWarning)
317 Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
320 // Syntactic Rewriting.
321 void RewriteRecordBody(RecordDecl *RD);
322 void RewriteInclude();
323 void RewriteLineDirective(const Decl *D);
324 void ConvertSourceLocationToLineDirective(SourceLocation Loc,
325 std::string &LineString);
326 void RewriteForwardClassDecl(DeclGroupRef D);
327 void RewriteForwardClassDecl(const SmallVectorImpl<Decl *> &DG);
328 void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl,
329 const std::string &typedefString);
330 void RewriteImplementations();
331 void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
332 ObjCImplementationDecl *IMD,
333 ObjCCategoryImplDecl *CID);
334 void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl);
335 void RewriteImplementationDecl(Decl *Dcl);
336 void RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
337 ObjCMethodDecl *MDecl, std::string &ResultStr);
338 void RewriteTypeIntoString(QualType T, std::string &ResultStr,
339 const FunctionType *&FPRetType);
340 void RewriteByRefString(std::string &ResultStr, const std::string &Name,
341 ValueDecl *VD, bool def=false);
342 void RewriteCategoryDecl(ObjCCategoryDecl *Dcl);
343 void RewriteProtocolDecl(ObjCProtocolDecl *Dcl);
344 void RewriteForwardProtocolDecl(DeclGroupRef D);
345 void RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG);
346 void RewriteMethodDeclaration(ObjCMethodDecl *Method);
347 void RewriteProperty(ObjCPropertyDecl *prop);
348 void RewriteFunctionDecl(FunctionDecl *FD);
349 void RewriteBlockPointerType(std::string& Str, QualType Type);
350 void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD);
351 void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD);
352 void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl);
353 void RewriteTypeOfDecl(VarDecl *VD);
354 void RewriteObjCQualifiedInterfaceTypes(Expr *E);
356 std::string getIvarAccessString(ObjCIvarDecl *D);
358 // Expression Rewriting.
359 Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S);
360 Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp);
361 Stmt *RewritePropertyOrImplicitGetter(PseudoObjectExpr *Pseudo);
362 Stmt *RewritePropertyOrImplicitSetter(PseudoObjectExpr *Pseudo);
363 Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp);
364 Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp);
365 Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp);
366 Stmt *RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp);
367 Stmt *RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp);
368 Stmt *RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp);
369 Stmt *RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp);
370 Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp);
371 Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S);
372 Stmt *RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
373 Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S);
374 Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S);
375 Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
376 SourceLocation OrigEnd);
377 Stmt *RewriteBreakStmt(BreakStmt *S);
378 Stmt *RewriteContinueStmt(ContinueStmt *S);
379 void RewriteCastExpr(CStyleCastExpr *CE);
380 void RewriteImplicitCastObjCExpr(CastExpr *IE);
381 void RewriteLinkageSpec(LinkageSpecDecl *LSD);
383 // Computes ivar bitfield group no.
384 unsigned ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV);
385 // Names field decl. for ivar bitfield group.
386 void ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV, std::string &Result);
387 // Names struct type for ivar bitfield group.
388 void ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV, std::string &Result);
389 // Names symbol for ivar bitfield group field offset.
390 void ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV, std::string &Result);
391 // Given an ivar bitfield, it builds (or finds) its group record type.
392 QualType GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV);
393 QualType SynthesizeBitfieldGroupStructType(
395 SmallVectorImpl<ObjCIvarDecl *> &IVars);
398 void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D);
400 // Block specific rewrite rules.
401 void RewriteBlockPointerDecl(NamedDecl *VD);
402 void RewriteByRefVar(VarDecl *VD, bool firstDecl, bool lastDecl);
403 Stmt *RewriteBlockDeclRefExpr(DeclRefExpr *VD);
404 Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE);
405 void RewriteBlockPointerFunctionArgs(FunctionDecl *FD);
407 void RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl,
408 std::string &Result);
410 void RewriteObjCFieldDecl(FieldDecl *fieldDecl, std::string &Result);
411 bool IsTagDefinedInsideClass(ObjCContainerDecl *IDecl, TagDecl *Tag,
412 bool &IsNamedDefinition);
413 void RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl,
414 std::string &Result);
416 bool RewriteObjCFieldDeclType(QualType &Type, std::string &Result);
418 void RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl,
419 std::string &Result);
421 void Initialize(ASTContext &context) override;
423 // Misc. AST transformation routines. Sometimes they end up calling
424 // rewriting routines on the new ASTs.
425 CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD,
426 Expr **args, unsigned nargs,
427 SourceLocation StartLoc=SourceLocation(),
428 SourceLocation EndLoc=SourceLocation());
430 Expr *SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor,
432 SmallVectorImpl<QualType> &ArgTypes,
433 SmallVectorImpl<Expr*> &MsgExprs,
434 ObjCMethodDecl *Method);
436 Stmt *SynthMessageExpr(ObjCMessageExpr *Exp,
437 SourceLocation StartLoc=SourceLocation(),
438 SourceLocation EndLoc=SourceLocation());
440 void SynthCountByEnumWithState(std::string &buf);
441 void SynthMsgSendFunctionDecl();
442 void SynthMsgSendSuperFunctionDecl();
443 void SynthMsgSendStretFunctionDecl();
444 void SynthMsgSendFpretFunctionDecl();
445 void SynthMsgSendSuperStretFunctionDecl();
446 void SynthGetClassFunctionDecl();
447 void SynthGetMetaClassFunctionDecl();
448 void SynthGetSuperClassFunctionDecl();
449 void SynthSelGetUidFunctionDecl();
450 void SynthSuperConstructorFunctionDecl();
452 // Rewriting metadata
453 template<typename MethodIterator>
454 void RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
455 MethodIterator MethodEnd,
456 bool IsInstanceMethod,
459 std::string &Result);
460 void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol,
461 std::string &Result);
462 void RewriteObjCProtocolListMetaData(
463 const ObjCList<ObjCProtocolDecl> &Prots,
464 StringRef prefix, StringRef ClassName, std::string &Result);
465 void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
466 std::string &Result);
467 void RewriteClassSetupInitHook(std::string &Result);
469 void RewriteMetaDataIntoBuffer(std::string &Result);
470 void WriteImageInfo(std::string &Result);
471 void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl,
472 std::string &Result);
473 void RewriteCategorySetupInitHook(std::string &Result);
476 void RewriteIvarOffsetComputation(ObjCIvarDecl *ivar,
477 std::string &Result);
478 Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV);
481 std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag);
482 std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
483 StringRef funcName, std::string Tag);
484 std::string SynthesizeBlockFunc(BlockExpr *CE, int i,
485 StringRef funcName, std::string Tag);
486 std::string SynthesizeBlockImpl(BlockExpr *CE,
487 std::string Tag, std::string Desc);
488 std::string SynthesizeBlockDescriptor(std::string DescTag,
490 int i, StringRef funcName,
492 Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp);
493 void SynthesizeBlockLiterals(SourceLocation FunLocStart,
495 FunctionDecl *SynthBlockInitFunctionDecl(StringRef name);
496 Stmt *SynthBlockInitExpr(BlockExpr *Exp,
497 const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs);
499 // Misc. helper routines.
500 QualType getProtocolType();
501 void WarnAboutReturnGotoStmts(Stmt *S);
502 void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND);
503 void InsertBlockLiteralsWithinFunction(FunctionDecl *FD);
504 void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD);
506 bool IsDeclStmtInForeachHeader(DeclStmt *DS);
507 void CollectBlockDeclRefInfo(BlockExpr *Exp);
508 void GetBlockDeclRefExprs(Stmt *S);
509 void GetInnerBlockDeclRefExprs(Stmt *S,
510 SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs,
511 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts);
513 // We avoid calling Type::isBlockPointerType(), since it operates on the
514 // canonical type. We only care if the top-level type is a closure pointer.
515 bool isTopLevelBlockPointerType(QualType T) {
516 return isa<BlockPointerType>(T);
519 /// convertBlockPointerToFunctionPointer - Converts a block-pointer type
520 /// to a function pointer type and upon success, returns true; false
522 bool convertBlockPointerToFunctionPointer(QualType &T) {
523 if (isTopLevelBlockPointerType(T)) {
524 const BlockPointerType *BPT = T->getAs<BlockPointerType>();
525 T = Context->getPointerType(BPT->getPointeeType());
531 bool convertObjCTypeToCStyleType(QualType &T);
533 bool needToScanForQualifiers(QualType T);
534 QualType getSuperStructType();
535 QualType getConstantStringStructType();
536 QualType convertFunctionTypeOfBlocks(const FunctionType *FT);
537 bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf);
539 void convertToUnqualifiedObjCType(QualType &T) {
540 if (T->isObjCQualifiedIdType()) {
541 bool isConst = T.isConstQualified();
542 T = isConst ? Context->getObjCIdType().withConst()
543 : Context->getObjCIdType();
545 else if (T->isObjCQualifiedClassType())
546 T = Context->getObjCClassType();
547 else if (T->isObjCObjectPointerType() &&
548 T->getPointeeType()->isObjCQualifiedInterfaceType()) {
549 if (const ObjCObjectPointerType * OBJPT =
550 T->getAsObjCInterfacePointerType()) {
551 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
552 T = QualType(IFaceT, 0);
553 T = Context->getPointerType(T);
558 // FIXME: This predicate seems like it would be useful to add to ASTContext.
559 bool isObjCType(QualType T) {
560 if (!LangOpts.ObjC1 && !LangOpts.ObjC2)
563 QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
565 if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
566 OCT == Context->getCanonicalType(Context->getObjCClassType()))
569 if (const PointerType *PT = OCT->getAs<PointerType>()) {
570 if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
571 PT->getPointeeType()->isObjCQualifiedIdType())
576 bool PointerTypeTakesAnyBlockArguments(QualType QT);
577 bool PointerTypeTakesAnyObjCQualifiedType(QualType QT);
578 void GetExtentOfArgList(const char *Name, const char *&LParen,
579 const char *&RParen);
581 void QuoteDoublequotes(std::string &From, std::string &To) {
582 for (unsigned i = 0; i < From.length(); i++) {
590 QualType getSimpleFunctionType(QualType result,
591 ArrayRef<QualType> args,
592 bool variadic = false) {
593 if (result == Context->getObjCInstanceType())
594 result = Context->getObjCIdType();
595 FunctionProtoType::ExtProtoInfo fpi;
596 fpi.Variadic = variadic;
597 return Context->getFunctionType(result, args, fpi);
600 // Helper function: create a CStyleCastExpr with trivial type source info.
601 CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty,
602 CastKind Kind, Expr *E) {
603 TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation());
604 return CStyleCastExpr::Create(*Ctx, Ty, VK_RValue, Kind, E, nullptr,
605 TInfo, SourceLocation(), SourceLocation());
608 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
609 IdentifierInfo* II = &Context->Idents.get("load");
610 Selector LoadSel = Context->Selectors.getSelector(0, &II);
611 return OD->getClassMethod(LoadSel) != nullptr;
614 StringLiteral *getStringLiteral(StringRef Str) {
615 QualType StrType = Context->getConstantArrayType(
616 Context->CharTy, llvm::APInt(32, Str.size() + 1), ArrayType::Normal,
618 return StringLiteral::Create(*Context, Str, StringLiteral::Ascii,
619 /*Pascal=*/false, StrType, SourceLocation());
625 void RewriteModernObjC::RewriteBlocksInFunctionProtoType(QualType funcType,
627 if (const FunctionProtoType *fproto
628 = dyn_cast<FunctionProtoType>(funcType.IgnoreParens())) {
629 for (const auto &I : fproto->param_types())
630 if (isTopLevelBlockPointerType(I)) {
631 // All the args are checked/rewritten. Don't call twice!
632 RewriteBlockPointerDecl(D);
638 void RewriteModernObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) {
639 const PointerType *PT = funcType->getAs<PointerType>();
640 if (PT && PointerTypeTakesAnyBlockArguments(funcType))
641 RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND);
644 static bool IsHeaderFile(const std::string &Filename) {
645 std::string::size_type DotPos = Filename.rfind('.');
647 if (DotPos == std::string::npos) {
652 std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
654 // C++ header: .hh or .H;
655 return Ext == "h" || Ext == "hh" || Ext == "H";
658 RewriteModernObjC::RewriteModernObjC(std::string inFile, raw_ostream* OS,
659 DiagnosticsEngine &D, const LangOptions &LOpts,
660 bool silenceMacroWarn,
662 : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS),
663 SilenceRewriteMacroWarning(silenceMacroWarn), GenerateLineInfo(LineInfo) {
664 IsHeader = IsHeaderFile(inFile);
665 RewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
666 "rewriting sub-expression within a macro (may not be correct)");
667 // FIXME. This should be an error. But if block is not called, it is OK. And it
668 // may break including some headers.
669 GlobalBlockRewriteFailedDiag = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
670 "rewriting block literal declared in global scope is not implemented");
672 TryFinallyContainsReturnDiag = Diags.getCustomDiagID(
673 DiagnosticsEngine::Warning,
674 "rewriter doesn't support user-specified control flow semantics "
675 "for @try/@finally (code may not execute properly)");
678 ASTConsumer *clang::CreateModernObjCRewriter(const std::string& InFile,
680 DiagnosticsEngine &Diags,
681 const LangOptions &LOpts,
682 bool SilenceRewriteMacroWarning,
684 return new RewriteModernObjC(InFile, OS, Diags, LOpts,
685 SilenceRewriteMacroWarning, LineInfo);
688 void RewriteModernObjC::InitializeCommon(ASTContext &context) {
690 SM = &Context->getSourceManager();
691 TUDecl = Context->getTranslationUnitDecl();
692 MsgSendFunctionDecl = nullptr;
693 MsgSendSuperFunctionDecl = nullptr;
694 MsgSendStretFunctionDecl = nullptr;
695 MsgSendSuperStretFunctionDecl = nullptr;
696 MsgSendFpretFunctionDecl = nullptr;
697 GetClassFunctionDecl = nullptr;
698 GetMetaClassFunctionDecl = nullptr;
699 GetSuperClassFunctionDecl = nullptr;
700 SelGetUidFunctionDecl = nullptr;
701 CFStringFunctionDecl = nullptr;
702 ConstantStringClassReference = nullptr;
703 NSStringRecord = nullptr;
704 CurMethodDef = nullptr;
705 CurFunctionDef = nullptr;
706 GlobalVarDecl = nullptr;
707 GlobalConstructionExp = nullptr;
708 SuperStructDecl = nullptr;
709 ProtocolTypeDecl = nullptr;
710 ConstantStringDecl = nullptr;
712 SuperConstructorFunctionDecl = nullptr;
713 NumObjCStringLiterals = 0;
714 PropParentMap = nullptr;
715 CurrentBody = nullptr;
716 DisableReplaceStmt = false;
717 objc_impl_method = false;
719 // Get the ID and start/end of the main file.
720 MainFileID = SM->getMainFileID();
721 const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID);
722 MainFileStart = MainBuf->getBufferStart();
723 MainFileEnd = MainBuf->getBufferEnd();
725 Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts());
728 //===----------------------------------------------------------------------===//
729 // Top Level Driver Code
730 //===----------------------------------------------------------------------===//
732 void RewriteModernObjC::HandleTopLevelSingleDecl(Decl *D) {
733 if (Diags.hasErrorOccurred())
736 // Two cases: either the decl could be in the main file, or it could be in a
737 // #included file. If the former, rewrite it now. If the later, check to see
738 // if we rewrote the #include/#import.
739 SourceLocation Loc = D->getLocation();
740 Loc = SM->getExpansionLoc(Loc);
742 // If this is for a builtin, ignore it.
743 if (Loc.isInvalid()) return;
745 // Look for built-in declarations that we need to refer during the rewrite.
746 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
747 RewriteFunctionDecl(FD);
748 } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) {
749 // declared in <Foundation/NSString.h>
750 if (FVD->getName() == "_NSConstantStringClassReference") {
751 ConstantStringClassReference = FVD;
754 } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
755 RewriteCategoryDecl(CD);
756 } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
757 if (PD->isThisDeclarationADefinition())
758 RewriteProtocolDecl(PD);
759 } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
760 // FIXME. This will not work in all situations and leaving it out
762 // RewriteLinkageSpec(LSD);
764 // Recurse into linkage specifications
765 for (DeclContext::decl_iterator DI = LSD->decls_begin(),
766 DIEnd = LSD->decls_end();
768 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>((*DI))) {
769 if (!IFace->isThisDeclarationADefinition()) {
770 SmallVector<Decl *, 8> DG;
771 SourceLocation StartLoc = IFace->getLocStart();
773 if (isa<ObjCInterfaceDecl>(*DI) &&
774 !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() &&
775 StartLoc == (*DI)->getLocStart())
781 } while (DI != DIEnd);
782 RewriteForwardClassDecl(DG);
786 // Keep track of all interface declarations seen.
787 ObjCInterfacesSeen.push_back(IFace);
793 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>((*DI))) {
794 if (!Proto->isThisDeclarationADefinition()) {
795 SmallVector<Decl *, 8> DG;
796 SourceLocation StartLoc = Proto->getLocStart();
798 if (isa<ObjCProtocolDecl>(*DI) &&
799 !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() &&
800 StartLoc == (*DI)->getLocStart())
806 } while (DI != DIEnd);
807 RewriteForwardProtocolDecl(DG);
812 HandleTopLevelSingleDecl(*DI);
816 // If we have a decl in the main file, see if we should rewrite it.
817 if (SM->isWrittenInMainFile(Loc))
818 return HandleDeclInMainFile(D);
821 //===----------------------------------------------------------------------===//
822 // Syntactic (non-AST) Rewriting Code
823 //===----------------------------------------------------------------------===//
825 void RewriteModernObjC::RewriteInclude() {
826 SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID);
827 StringRef MainBuf = SM->getBufferData(MainFileID);
828 const char *MainBufStart = MainBuf.begin();
829 const char *MainBufEnd = MainBuf.end();
830 size_t ImportLen = strlen("import");
832 // Loop over the whole file, looking for includes.
833 for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) {
834 if (*BufPtr == '#') {
835 if (++BufPtr == MainBufEnd)
837 while (*BufPtr == ' ' || *BufPtr == '\t')
838 if (++BufPtr == MainBufEnd)
840 if (!strncmp(BufPtr, "import", ImportLen)) {
841 // replace import with include
842 SourceLocation ImportLoc =
843 LocStart.getLocWithOffset(BufPtr-MainBufStart);
844 ReplaceText(ImportLoc, ImportLen, "include");
851 static void WriteInternalIvarName(const ObjCInterfaceDecl *IDecl,
852 ObjCIvarDecl *IvarDecl, std::string &Result) {
853 Result += "OBJC_IVAR_$_";
854 Result += IDecl->getName();
856 Result += IvarDecl->getName();
860 RewriteModernObjC::getIvarAccessString(ObjCIvarDecl *D) {
861 const ObjCInterfaceDecl *ClassDecl = D->getContainingInterface();
863 // Build name of symbol holding ivar offset.
864 std::string IvarOffsetName;
866 ObjCIvarBitfieldGroupOffset(D, IvarOffsetName);
868 WriteInternalIvarName(ClassDecl, D, IvarOffsetName);
871 std::string S = "(*(";
872 QualType IvarT = D->getType();
874 IvarT = GetGroupRecordTypeForObjCIvarBitfield(D);
876 if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) {
877 RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl();
878 RD = RD->getDefinition();
879 if (RD && !RD->getDeclName().getAsIdentifierInfo()) {
880 // decltype(((Foo_IMPL*)0)->bar) *
881 ObjCContainerDecl *CDecl =
882 dyn_cast<ObjCContainerDecl>(D->getDeclContext());
883 // ivar in class extensions requires special treatment.
884 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
885 CDecl = CatDecl->getClassInterface();
886 std::string RecName = CDecl->getName();
888 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
889 SourceLocation(), SourceLocation(),
890 &Context->Idents.get(RecName.c_str()));
891 QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD));
892 unsigned UnsignedIntSize =
893 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
894 Expr *Zero = IntegerLiteral::Create(*Context,
895 llvm::APInt(UnsignedIntSize, 0),
896 Context->UnsignedIntTy, SourceLocation());
897 Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero);
898 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
900 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
902 &Context->Idents.get(D->getNameAsString()),
904 /*BitWidth=*/nullptr, /*Mutable=*/true,
906 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
907 FD->getType(), VK_LValue,
909 IvarT = Context->getDecltypeType(ME, ME->getType());
912 convertObjCTypeToCStyleType(IvarT);
913 QualType castT = Context->getPointerType(IvarT);
914 std::string TypeString(castT.getAsString(Context->getPrintingPolicy()));
918 // ((char *)self + IVAR_OFFSET_SYMBOL_NAME)
919 S += "((char *)self + ";
922 if (D->isBitField()) {
924 S += D->getNameAsString();
926 ReferencedIvars[const_cast<ObjCInterfaceDecl *>(ClassDecl)].insert(D);
930 /// mustSynthesizeSetterGetterMethod - returns true if setter or getter has not
931 /// been found in the class implementation. In this case, it must be synthesized.
932 static bool mustSynthesizeSetterGetterMethod(ObjCImplementationDecl *IMP,
933 ObjCPropertyDecl *PD,
935 return getter ? !IMP->getInstanceMethod(PD->getGetterName())
936 : !IMP->getInstanceMethod(PD->getSetterName());
940 void RewriteModernObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID,
941 ObjCImplementationDecl *IMD,
942 ObjCCategoryImplDecl *CID) {
943 static bool objcGetPropertyDefined = false;
944 static bool objcSetPropertyDefined = false;
945 SourceLocation startGetterSetterLoc;
947 if (PID->getLocStart().isValid()) {
948 SourceLocation startLoc = PID->getLocStart();
949 InsertText(startLoc, "// ");
950 const char *startBuf = SM->getCharacterData(startLoc);
951 assert((*startBuf == '@') && "bogus @synthesize location");
952 const char *semiBuf = strchr(startBuf, ';');
953 assert((*semiBuf == ';') && "@synthesize: can't find ';'");
954 startGetterSetterLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
957 startGetterSetterLoc = IMD ? IMD->getLocEnd() : CID->getLocEnd();
959 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
960 return; // FIXME: is this correct?
962 // Generate the 'getter' function.
963 ObjCPropertyDecl *PD = PID->getPropertyDecl();
964 ObjCIvarDecl *OID = PID->getPropertyIvarDecl();
965 assert(IMD && OID && "Synthesized ivars must be attached to @implementation");
967 unsigned Attributes = PD->getPropertyAttributes();
968 if (mustSynthesizeSetterGetterMethod(IMD, PD, true /*getter*/)) {
969 bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
970 (Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
971 ObjCPropertyDecl::OBJC_PR_copy));
973 if (GenGetProperty && !objcGetPropertyDefined) {
974 objcGetPropertyDefined = true;
975 // FIXME. Is this attribute correct in all cases?
976 Getr = "\nextern \"C\" __declspec(dllimport) "
977 "id objc_getProperty(id, SEL, long, bool);\n";
979 RewriteObjCMethodDecl(OID->getContainingInterface(),
980 PD->getGetterMethodDecl(), Getr);
982 // Synthesize an explicit cast to gain access to the ivar.
983 // See objc-act.c:objc_synthesize_new_getter() for details.
984 if (GenGetProperty) {
985 // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1)
987 const FunctionType *FPRetType = nullptr;
988 RewriteTypeIntoString(PD->getGetterMethodDecl()->getReturnType(), Getr,
992 Getr += ")"; // close the precedence "scope" for "*".
994 // Now, emit the argument types (if any).
995 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)){
997 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
999 std::string ParamStr =
1000 FT->getParamType(i).getAsString(Context->getPrintingPolicy());
1003 if (FT->isVariadic()) {
1004 if (FT->getNumParams())
1013 Getr += "return (_TYPE)";
1014 Getr += "objc_getProperty(self, _cmd, ";
1015 RewriteIvarOffsetComputation(OID, Getr);
1019 Getr += "return " + getIvarAccessString(OID);
1021 InsertText(startGetterSetterLoc, Getr);
1024 if (PD->isReadOnly() ||
1025 !mustSynthesizeSetterGetterMethod(IMD, PD, false /*setter*/))
1028 // Generate the 'setter' function.
1030 bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain |
1031 ObjCPropertyDecl::OBJC_PR_copy);
1032 if (GenSetProperty && !objcSetPropertyDefined) {
1033 objcSetPropertyDefined = true;
1034 // FIXME. Is this attribute correct in all cases?
1035 Setr = "\nextern \"C\" __declspec(dllimport) "
1036 "void objc_setProperty (id, SEL, long, id, bool, bool);\n";
1039 RewriteObjCMethodDecl(OID->getContainingInterface(),
1040 PD->getSetterMethodDecl(), Setr);
1042 // Synthesize an explicit cast to initialize the ivar.
1043 // See objc-act.c:objc_synthesize_new_setter() for details.
1044 if (GenSetProperty) {
1045 Setr += "objc_setProperty (self, _cmd, ";
1046 RewriteIvarOffsetComputation(OID, Setr);
1048 Setr += PD->getName();
1050 if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic)
1054 if (Attributes & ObjCPropertyDecl::OBJC_PR_copy)
1060 Setr += getIvarAccessString(OID) + " = ";
1061 Setr += PD->getName();
1064 InsertText(startGetterSetterLoc, Setr);
1067 static void RewriteOneForwardClassDecl(ObjCInterfaceDecl *ForwardDecl,
1068 std::string &typedefString) {
1069 typedefString += "\n#ifndef _REWRITER_typedef_";
1070 typedefString += ForwardDecl->getNameAsString();
1071 typedefString += "\n";
1072 typedefString += "#define _REWRITER_typedef_";
1073 typedefString += ForwardDecl->getNameAsString();
1074 typedefString += "\n";
1075 typedefString += "typedef struct objc_object ";
1076 typedefString += ForwardDecl->getNameAsString();
1077 // typedef struct { } _objc_exc_Classname;
1078 typedefString += ";\ntypedef struct {} _objc_exc_";
1079 typedefString += ForwardDecl->getNameAsString();
1080 typedefString += ";\n#endif\n";
1083 void RewriteModernObjC::RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl,
1084 const std::string &typedefString) {
1085 SourceLocation startLoc = ClassDecl->getLocStart();
1086 const char *startBuf = SM->getCharacterData(startLoc);
1087 const char *semiPtr = strchr(startBuf, ';');
1088 // Replace the @class with typedefs corresponding to the classes.
1089 ReplaceText(startLoc, semiPtr-startBuf+1, typedefString);
1092 void RewriteModernObjC::RewriteForwardClassDecl(DeclGroupRef D) {
1093 std::string typedefString;
1094 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
1095 if (ObjCInterfaceDecl *ForwardDecl = dyn_cast<ObjCInterfaceDecl>(*I)) {
1096 if (I == D.begin()) {
1097 // Translate to typedef's that forward reference structs with the same name
1098 // as the class. As a convenience, we include the original declaration
1100 typedefString += "// @class ";
1101 typedefString += ForwardDecl->getNameAsString();
1102 typedefString += ";";
1104 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
1107 HandleTopLevelSingleDecl(*I);
1109 DeclGroupRef::iterator I = D.begin();
1110 RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(*I), typedefString);
1113 void RewriteModernObjC::RewriteForwardClassDecl(
1114 const SmallVectorImpl<Decl *> &D) {
1115 std::string typedefString;
1116 for (unsigned i = 0; i < D.size(); i++) {
1117 ObjCInterfaceDecl *ForwardDecl = cast<ObjCInterfaceDecl>(D[i]);
1119 typedefString += "// @class ";
1120 typedefString += ForwardDecl->getNameAsString();
1121 typedefString += ";";
1123 RewriteOneForwardClassDecl(ForwardDecl, typedefString);
1125 RewriteForwardClassEpilogue(cast<ObjCInterfaceDecl>(D[0]), typedefString);
1128 void RewriteModernObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) {
1129 // When method is a synthesized one, such as a getter/setter there is
1130 // nothing to rewrite.
1131 if (Method->isImplicit())
1133 SourceLocation LocStart = Method->getLocStart();
1134 SourceLocation LocEnd = Method->getLocEnd();
1136 if (SM->getExpansionLineNumber(LocEnd) >
1137 SM->getExpansionLineNumber(LocStart)) {
1138 InsertText(LocStart, "#if 0\n");
1139 ReplaceText(LocEnd, 1, ";\n#endif\n");
1141 InsertText(LocStart, "// ");
1145 void RewriteModernObjC::RewriteProperty(ObjCPropertyDecl *prop) {
1146 SourceLocation Loc = prop->getAtLoc();
1148 ReplaceText(Loc, 0, "// ");
1149 // FIXME: handle properties that are declared across multiple lines.
1152 void RewriteModernObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
1153 SourceLocation LocStart = CatDecl->getLocStart();
1155 // FIXME: handle category headers that are declared across multiple lines.
1156 if (CatDecl->getIvarRBraceLoc().isValid()) {
1157 ReplaceText(LocStart, 1, "/** ");
1158 ReplaceText(CatDecl->getIvarRBraceLoc(), 1, "**/ ");
1161 ReplaceText(LocStart, 0, "// ");
1164 for (auto *I : CatDecl->properties())
1167 for (auto *I : CatDecl->instance_methods())
1168 RewriteMethodDeclaration(I);
1169 for (auto *I : CatDecl->class_methods())
1170 RewriteMethodDeclaration(I);
1172 // Lastly, comment out the @end.
1173 ReplaceText(CatDecl->getAtEndRange().getBegin(),
1174 strlen("@end"), "/* @end */\n");
1177 void RewriteModernObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
1178 SourceLocation LocStart = PDecl->getLocStart();
1179 assert(PDecl->isThisDeclarationADefinition());
1181 // FIXME: handle protocol headers that are declared across multiple lines.
1182 ReplaceText(LocStart, 0, "// ");
1184 for (auto *I : PDecl->instance_methods())
1185 RewriteMethodDeclaration(I);
1186 for (auto *I : PDecl->class_methods())
1187 RewriteMethodDeclaration(I);
1188 for (auto *I : PDecl->properties())
1191 // Lastly, comment out the @end.
1192 SourceLocation LocEnd = PDecl->getAtEndRange().getBegin();
1193 ReplaceText(LocEnd, strlen("@end"), "/* @end */\n");
1195 // Must comment out @optional/@required
1196 const char *startBuf = SM->getCharacterData(LocStart);
1197 const char *endBuf = SM->getCharacterData(LocEnd);
1198 for (const char *p = startBuf; p < endBuf; p++) {
1199 if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) {
1200 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1201 ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */");
1204 else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) {
1205 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1206 ReplaceText(OptionalLoc, strlen("@required"), "/* @required */");
1212 void RewriteModernObjC::RewriteForwardProtocolDecl(DeclGroupRef D) {
1213 SourceLocation LocStart = (*D.begin())->getLocStart();
1214 if (LocStart.isInvalid())
1215 llvm_unreachable("Invalid SourceLocation");
1216 // FIXME: handle forward protocol that are declared across multiple lines.
1217 ReplaceText(LocStart, 0, "// ");
1221 RewriteModernObjC::RewriteForwardProtocolDecl(const SmallVectorImpl<Decl *> &DG) {
1222 SourceLocation LocStart = DG[0]->getLocStart();
1223 if (LocStart.isInvalid())
1224 llvm_unreachable("Invalid SourceLocation");
1225 // FIXME: handle forward protocol that are declared across multiple lines.
1226 ReplaceText(LocStart, 0, "// ");
1230 RewriteModernObjC::RewriteLinkageSpec(LinkageSpecDecl *LSD) {
1231 SourceLocation LocStart = LSD->getExternLoc();
1232 if (LocStart.isInvalid())
1233 llvm_unreachable("Invalid extern SourceLocation");
1235 ReplaceText(LocStart, 0, "// ");
1236 if (!LSD->hasBraces())
1238 // FIXME. We don't rewrite well if '{' is not on same line as 'extern'.
1239 SourceLocation LocRBrace = LSD->getRBraceLoc();
1240 if (LocRBrace.isInvalid())
1241 llvm_unreachable("Invalid rbrace SourceLocation");
1242 ReplaceText(LocRBrace, 0, "// ");
1245 void RewriteModernObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr,
1246 const FunctionType *&FPRetType) {
1247 if (T->isObjCQualifiedIdType())
1249 else if (T->isFunctionPointerType() ||
1250 T->isBlockPointerType()) {
1251 // needs special handling, since pointer-to-functions have special
1252 // syntax (where a decaration models use).
1253 QualType retType = T;
1255 if (const PointerType* PT = retType->getAs<PointerType>())
1256 PointeeTy = PT->getPointeeType();
1257 else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>())
1258 PointeeTy = BPT->getPointeeType();
1259 if ((FPRetType = PointeeTy->getAs<FunctionType>())) {
1261 FPRetType->getReturnType().getAsString(Context->getPrintingPolicy());
1265 ResultStr += T.getAsString(Context->getPrintingPolicy());
1268 void RewriteModernObjC::RewriteObjCMethodDecl(const ObjCInterfaceDecl *IDecl,
1269 ObjCMethodDecl *OMD,
1270 std::string &ResultStr) {
1271 //fprintf(stderr,"In RewriteObjCMethodDecl\n");
1272 const FunctionType *FPRetType = nullptr;
1273 ResultStr += "\nstatic ";
1274 RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType);
1277 // Unique method name
1278 std::string NameStr;
1280 if (OMD->isInstanceMethod())
1285 NameStr += IDecl->getNameAsString();
1288 if (ObjCCategoryImplDecl *CID =
1289 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1290 NameStr += CID->getNameAsString();
1293 // Append selector names, replacing ':' with '_'
1295 std::string selString = OMD->getSelector().getAsString();
1296 int len = selString.size();
1297 for (int i = 0; i < len; i++)
1298 if (selString[i] == ':')
1300 NameStr += selString;
1302 // Remember this name for metadata emission
1303 MethodInternalNames[OMD] = NameStr;
1304 ResultStr += NameStr;
1306 // Rewrite arguments
1309 // invisible arguments
1310 if (OMD->isInstanceMethod()) {
1311 QualType selfTy = Context->getObjCInterfaceType(IDecl);
1312 selfTy = Context->getPointerType(selfTy);
1313 if (!LangOpts.MicrosoftExt) {
1314 if (ObjCSynthesizedStructs.count(const_cast<ObjCInterfaceDecl*>(IDecl)))
1315 ResultStr += "struct ";
1317 // When rewriting for Microsoft, explicitly omit the structure name.
1318 ResultStr += IDecl->getNameAsString();
1322 ResultStr += Context->getObjCClassType().getAsString(
1323 Context->getPrintingPolicy());
1325 ResultStr += " self, ";
1326 ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy());
1327 ResultStr += " _cmd";
1329 // Method arguments.
1330 for (const auto *PDecl : OMD->params()) {
1332 if (PDecl->getType()->isObjCQualifiedIdType()) {
1334 ResultStr += PDecl->getNameAsString();
1336 std::string Name = PDecl->getNameAsString();
1337 QualType QT = PDecl->getType();
1338 // Make sure we convert "t (^)(...)" to "t (*)(...)".
1339 (void)convertBlockPointerToFunctionPointer(QT);
1340 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
1344 if (OMD->isVariadic())
1345 ResultStr += ", ...";
1349 ResultStr += ")"; // close the precedence "scope" for "*".
1351 // Now, emit the argument types (if any).
1352 if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) {
1354 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1355 if (i) ResultStr += ", ";
1356 std::string ParamStr =
1357 FT->getParamType(i).getAsString(Context->getPrintingPolicy());
1358 ResultStr += ParamStr;
1360 if (FT->isVariadic()) {
1361 if (FT->getNumParams())
1371 void RewriteModernObjC::RewriteImplementationDecl(Decl *OID) {
1372 ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID);
1373 ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID);
1376 if (IMD->getIvarRBraceLoc().isValid()) {
1377 ReplaceText(IMD->getLocStart(), 1, "/** ");
1378 ReplaceText(IMD->getIvarRBraceLoc(), 1, "**/ ");
1381 InsertText(IMD->getLocStart(), "// ");
1385 InsertText(CID->getLocStart(), "// ");
1387 for (auto *OMD : IMD ? IMD->instance_methods() : CID->instance_methods()) {
1388 std::string ResultStr;
1389 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1390 SourceLocation LocStart = OMD->getLocStart();
1391 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1393 const char *startBuf = SM->getCharacterData(LocStart);
1394 const char *endBuf = SM->getCharacterData(LocEnd);
1395 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1398 for (auto *OMD : IMD ? IMD->class_methods() : CID->class_methods()) {
1399 std::string ResultStr;
1400 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1401 SourceLocation LocStart = OMD->getLocStart();
1402 SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();
1404 const char *startBuf = SM->getCharacterData(LocStart);
1405 const char *endBuf = SM->getCharacterData(LocEnd);
1406 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1408 for (auto *I : IMD ? IMD->property_impls() : CID->property_impls())
1409 RewritePropertyImplDecl(I, IMD, CID);
1411 InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// ");
1414 void RewriteModernObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
1415 // Do not synthesize more than once.
1416 if (ObjCSynthesizedStructs.count(ClassDecl))
1418 // Make sure super class's are written before current class is written.
1419 ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass();
1420 while (SuperClass) {
1421 RewriteInterfaceDecl(SuperClass);
1422 SuperClass = SuperClass->getSuperClass();
1424 std::string ResultStr;
1425 if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) {
1426 // we haven't seen a forward decl - generate a typedef.
1427 RewriteOneForwardClassDecl(ClassDecl, ResultStr);
1428 RewriteIvarOffsetSymbols(ClassDecl, ResultStr);
1430 RewriteObjCInternalStruct(ClassDecl, ResultStr);
1431 // Mark this typedef as having been written into its c++ equivalent.
1432 ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl());
1434 for (auto *I : ClassDecl->properties())
1436 for (auto *I : ClassDecl->instance_methods())
1437 RewriteMethodDeclaration(I);
1438 for (auto *I : ClassDecl->class_methods())
1439 RewriteMethodDeclaration(I);
1441 // Lastly, comment out the @end.
1442 ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"),
1447 Stmt *RewriteModernObjC::RewritePropertyOrImplicitSetter(PseudoObjectExpr *PseudoOp) {
1448 SourceRange OldRange = PseudoOp->getSourceRange();
1450 // We just magically know some things about the structure of this
1452 ObjCMessageExpr *OldMsg =
1453 cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr(
1454 PseudoOp->getNumSemanticExprs() - 1));
1456 // Because the rewriter doesn't allow us to rewrite rewritten code,
1457 // we need to suppress rewriting the sub-statements.
1459 SmallVector<Expr*, 2> Args;
1461 DisableReplaceStmtScope S(*this);
1463 // Rebuild the base expression if we have one.
1465 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1466 Base = OldMsg->getInstanceReceiver();
1467 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1468 Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base));
1471 unsigned numArgs = OldMsg->getNumArgs();
1472 for (unsigned i = 0; i < numArgs; i++) {
1473 Expr *Arg = OldMsg->getArg(i);
1474 if (isa<OpaqueValueExpr>(Arg))
1475 Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr();
1476 Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg));
1477 Args.push_back(Arg);
1481 // TODO: avoid this copy.
1482 SmallVector<SourceLocation, 1> SelLocs;
1483 OldMsg->getSelectorLocs(SelLocs);
1485 ObjCMessageExpr *NewMsg = nullptr;
1486 switch (OldMsg->getReceiverKind()) {
1487 case ObjCMessageExpr::Class:
1488 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1489 OldMsg->getValueKind(),
1490 OldMsg->getLeftLoc(),
1491 OldMsg->getClassReceiverTypeInfo(),
1492 OldMsg->getSelector(),
1494 OldMsg->getMethodDecl(),
1496 OldMsg->getRightLoc(),
1497 OldMsg->isImplicit());
1500 case ObjCMessageExpr::Instance:
1501 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1502 OldMsg->getValueKind(),
1503 OldMsg->getLeftLoc(),
1505 OldMsg->getSelector(),
1507 OldMsg->getMethodDecl(),
1509 OldMsg->getRightLoc(),
1510 OldMsg->isImplicit());
1513 case ObjCMessageExpr::SuperClass:
1514 case ObjCMessageExpr::SuperInstance:
1515 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1516 OldMsg->getValueKind(),
1517 OldMsg->getLeftLoc(),
1518 OldMsg->getSuperLoc(),
1519 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1520 OldMsg->getSuperType(),
1521 OldMsg->getSelector(),
1523 OldMsg->getMethodDecl(),
1525 OldMsg->getRightLoc(),
1526 OldMsg->isImplicit());
1530 Stmt *Replacement = SynthMessageExpr(NewMsg);
1531 ReplaceStmtWithRange(PseudoOp, Replacement, OldRange);
1535 Stmt *RewriteModernObjC::RewritePropertyOrImplicitGetter(PseudoObjectExpr *PseudoOp) {
1536 SourceRange OldRange = PseudoOp->getSourceRange();
1538 // We just magically know some things about the structure of this
1540 ObjCMessageExpr *OldMsg =
1541 cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit());
1543 // Because the rewriter doesn't allow us to rewrite rewritten code,
1544 // we need to suppress rewriting the sub-statements.
1545 Expr *Base = nullptr;
1546 SmallVector<Expr*, 1> Args;
1548 DisableReplaceStmtScope S(*this);
1549 // Rebuild the base expression if we have one.
1550 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1551 Base = OldMsg->getInstanceReceiver();
1552 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1553 Base = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Base));
1555 unsigned numArgs = OldMsg->getNumArgs();
1556 for (unsigned i = 0; i < numArgs; i++) {
1557 Expr *Arg = OldMsg->getArg(i);
1558 if (isa<OpaqueValueExpr>(Arg))
1559 Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr();
1560 Arg = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(Arg));
1561 Args.push_back(Arg);
1565 // Intentionally empty.
1566 SmallVector<SourceLocation, 1> SelLocs;
1568 ObjCMessageExpr *NewMsg = nullptr;
1569 switch (OldMsg->getReceiverKind()) {
1570 case ObjCMessageExpr::Class:
1571 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1572 OldMsg->getValueKind(),
1573 OldMsg->getLeftLoc(),
1574 OldMsg->getClassReceiverTypeInfo(),
1575 OldMsg->getSelector(),
1577 OldMsg->getMethodDecl(),
1579 OldMsg->getRightLoc(),
1580 OldMsg->isImplicit());
1583 case ObjCMessageExpr::Instance:
1584 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1585 OldMsg->getValueKind(),
1586 OldMsg->getLeftLoc(),
1588 OldMsg->getSelector(),
1590 OldMsg->getMethodDecl(),
1592 OldMsg->getRightLoc(),
1593 OldMsg->isImplicit());
1596 case ObjCMessageExpr::SuperClass:
1597 case ObjCMessageExpr::SuperInstance:
1598 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1599 OldMsg->getValueKind(),
1600 OldMsg->getLeftLoc(),
1601 OldMsg->getSuperLoc(),
1602 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1603 OldMsg->getSuperType(),
1604 OldMsg->getSelector(),
1606 OldMsg->getMethodDecl(),
1608 OldMsg->getRightLoc(),
1609 OldMsg->isImplicit());
1613 Stmt *Replacement = SynthMessageExpr(NewMsg);
1614 ReplaceStmtWithRange(PseudoOp, Replacement, OldRange);
1618 /// SynthCountByEnumWithState - To print:
1619 /// ((NSUInteger (*)
1620 /// (id, SEL, struct __objcFastEnumerationState *, id *, NSUInteger))
1621 /// (void *)objc_msgSend)((id)l_collection,
1622 /// sel_registerName(
1623 /// "countByEnumeratingWithState:objects:count:"),
1625 /// (id *)__rw_items, (NSUInteger)16)
1627 void RewriteModernObjC::SynthCountByEnumWithState(std::string &buf) {
1628 buf += "((_WIN_NSUInteger (*) (id, SEL, struct __objcFastEnumerationState *, "
1629 "id *, _WIN_NSUInteger))(void *)objc_msgSend)";
1631 buf += "((id)l_collection,\n\t\t";
1632 buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),";
1634 buf += "&enumState, "
1635 "(id *)__rw_items, (_WIN_NSUInteger)16)";
1638 /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
1639 /// statement to exit to its outer synthesized loop.
1641 Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) {
1642 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1644 // replace break with goto __break_label
1647 SourceLocation startLoc = S->getLocStart();
1648 buf = "goto __break_label_";
1649 buf += utostr(ObjCBcLabelNo.back());
1650 ReplaceText(startLoc, strlen("break"), buf);
1655 void RewriteModernObjC::ConvertSourceLocationToLineDirective(
1657 std::string &LineString) {
1658 if (Loc.isFileID() && GenerateLineInfo) {
1659 LineString += "\n#line ";
1660 PresumedLoc PLoc = SM->getPresumedLoc(Loc);
1661 LineString += utostr(PLoc.getLine());
1662 LineString += " \"";
1663 LineString += Lexer::Stringify(PLoc.getFilename());
1664 LineString += "\"\n";
1668 /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
1669 /// statement to continue with its inner synthesized loop.
1671 Stmt *RewriteModernObjC::RewriteContinueStmt(ContinueStmt *S) {
1672 if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back()))
1674 // replace continue with goto __continue_label
1677 SourceLocation startLoc = S->getLocStart();
1678 buf = "goto __continue_label_";
1679 buf += utostr(ObjCBcLabelNo.back());
1680 ReplaceText(startLoc, strlen("continue"), buf);
1685 /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement.
1687 /// for ( type elem in collection) { stmts; }
1692 /// struct __objcFastEnumerationState enumState = { 0 };
1693 /// id __rw_items[16];
1694 /// id l_collection = (id)collection;
1695 /// NSUInteger limit = [l_collection countByEnumeratingWithState:&enumState
1696 /// objects:__rw_items count:16];
1698 /// unsigned long startMutations = *enumState.mutationsPtr;
1700 /// unsigned long counter = 0;
1702 /// if (startMutations != *enumState.mutationsPtr)
1703 /// objc_enumerationMutation(l_collection);
1704 /// elem = (type)enumState.itemsPtr[counter++];
1706 /// __continue_label: ;
1707 /// } while (counter < limit);
1708 /// } while ((limit = [l_collection countByEnumeratingWithState:&enumState
1709 /// objects:__rw_items count:16]));
1711 /// __break_label: ;
1717 Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
1718 SourceLocation OrigEnd) {
1719 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
1720 assert(isa<ObjCForCollectionStmt>(Stmts.back()) &&
1721 "ObjCForCollectionStmt Statement stack mismatch");
1722 assert(!ObjCBcLabelNo.empty() &&
1723 "ObjCForCollectionStmt - Label No stack empty");
1725 SourceLocation startLoc = S->getLocStart();
1726 const char *startBuf = SM->getCharacterData(startLoc);
1727 StringRef elementName;
1728 std::string elementTypeAsString;
1730 // line directive first.
1731 SourceLocation ForEachLoc = S->getForLoc();
1732 ConvertSourceLocationToLineDirective(ForEachLoc, buf);
1734 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
1736 NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl());
1737 QualType ElementType = cast<ValueDecl>(D)->getType();
1738 if (ElementType->isObjCQualifiedIdType() ||
1739 ElementType->isObjCQualifiedInterfaceType())
1740 // Simply use 'id' for all qualified types.
1741 elementTypeAsString = "id";
1743 elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy());
1744 buf += elementTypeAsString;
1746 elementName = D->getName();
1751 DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement());
1752 elementName = DR->getDecl()->getName();
1753 ValueDecl *VD = cast<ValueDecl>(DR->getDecl());
1754 if (VD->getType()->isObjCQualifiedIdType() ||
1755 VD->getType()->isObjCQualifiedInterfaceType())
1756 // Simply use 'id' for all qualified types.
1757 elementTypeAsString = "id";
1759 elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy());
1762 // struct __objcFastEnumerationState enumState = { 0 };
1763 buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t";
1764 // id __rw_items[16];
1765 buf += "id __rw_items[16];\n\t";
1766 // id l_collection = (id)
1767 buf += "id l_collection = (id)";
1768 // Find start location of 'collection' the hard way!
1769 const char *startCollectionBuf = startBuf;
1770 startCollectionBuf += 3; // skip 'for'
1771 startCollectionBuf = strchr(startCollectionBuf, '(');
1772 startCollectionBuf++; // skip '('
1773 // find 'in' and skip it.
1774 while (*startCollectionBuf != ' ' ||
1775 *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' ||
1776 (*(startCollectionBuf+3) != ' ' &&
1777 *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '('))
1778 startCollectionBuf++;
1779 startCollectionBuf += 3;
1781 // Replace: "for (type element in" with string constructed thus far.
1782 ReplaceText(startLoc, startCollectionBuf - startBuf, buf);
1783 // Replace ')' in for '(' type elem in collection ')' with ';'
1784 SourceLocation rightParenLoc = S->getRParenLoc();
1785 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
1786 SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf);
1789 // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState
1790 // objects:__rw_items count:16];
1791 // which is synthesized into:
1792 // NSUInteger limit =
1794 // (id, SEL, struct __objcFastEnumerationState *, id *, NSUInteger))
1795 // (void *)objc_msgSend)((id)l_collection,
1796 // sel_registerName(
1797 // "countByEnumeratingWithState:objects:count:"),
1798 // (struct __objcFastEnumerationState *)&state,
1799 // (id *)__rw_items, (NSUInteger)16);
1800 buf += "_WIN_NSUInteger limit =\n\t\t";
1801 SynthCountByEnumWithState(buf);
1804 /// unsigned long startMutations = *enumState.mutationsPtr;
1806 /// unsigned long counter = 0;
1808 /// if (startMutations != *enumState.mutationsPtr)
1809 /// objc_enumerationMutation(l_collection);
1810 /// elem = (type)enumState.itemsPtr[counter++];
1811 buf += "if (limit) {\n\t";
1812 buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t";
1813 buf += "do {\n\t\t";
1814 buf += "unsigned long counter = 0;\n\t\t";
1815 buf += "do {\n\t\t\t";
1816 buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t";
1817 buf += "objc_enumerationMutation(l_collection);\n\t\t\t";
1820 buf += elementTypeAsString;
1821 buf += ")enumState.itemsPtr[counter++];";
1822 // Replace ')' in for '(' type elem in collection ')' with all of these.
1823 ReplaceText(lparenLoc, 1, buf);
1825 /// __continue_label: ;
1826 /// } while (counter < limit);
1827 /// } while ((limit = [l_collection countByEnumeratingWithState:&enumState
1828 /// objects:__rw_items count:16]));
1830 /// __break_label: ;
1837 buf += "__continue_label_";
1838 buf += utostr(ObjCBcLabelNo.back());
1841 buf += "} while (counter < limit);\n\t";
1842 buf += "} while ((limit = ";
1843 SynthCountByEnumWithState(buf);
1847 buf += elementTypeAsString;
1849 buf += "__break_label_";
1850 buf += utostr(ObjCBcLabelNo.back());
1853 buf += "else\n\t\t";
1856 buf += elementTypeAsString;
1860 // Insert all these *after* the statement body.
1861 // FIXME: If this should support Obj-C++, support CXXTryStmt
1862 if (isa<CompoundStmt>(S->getBody())) {
1863 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(1);
1864 InsertText(endBodyLoc, buf);
1866 /* Need to treat single statements specially. For example:
1868 * for (A *a in b) if (stuff()) break;
1869 * for (A *a in b) xxxyy;
1871 * The following code simply scans ahead to the semi to find the actual end.
1873 const char *stmtBuf = SM->getCharacterData(OrigEnd);
1874 const char *semiBuf = strchr(stmtBuf, ';');
1875 assert(semiBuf && "Can't find ';'");
1876 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1);
1877 InsertText(endBodyLoc, buf);
1880 ObjCBcLabelNo.pop_back();
1884 static void Write_RethrowObject(std::string &buf) {
1885 buf += "{ struct _FIN { _FIN(id reth) : rethrow(reth) {}\n";
1886 buf += "\t~_FIN() { if (rethrow) objc_exception_throw(rethrow); }\n";
1887 buf += "\tid rethrow;\n";
1888 buf += "\t} _fin_force_rethow(_rethrow);";
1891 /// RewriteObjCSynchronizedStmt -
1892 /// This routine rewrites @synchronized(expr) stmt;
1894 /// objc_sync_enter(expr);
1895 /// @try stmt @finally { objc_sync_exit(expr); }
1897 Stmt *RewriteModernObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1898 // Get the start location and compute the semi location.
1899 SourceLocation startLoc = S->getLocStart();
1900 const char *startBuf = SM->getCharacterData(startLoc);
1902 assert((*startBuf == '@') && "bogus @synchronized location");
1905 SourceLocation SynchLoc = S->getAtSynchronizedLoc();
1906 ConvertSourceLocationToLineDirective(SynchLoc, buf);
1907 buf += "{ id _rethrow = 0; id _sync_obj = (id)";
1909 const char *lparenBuf = startBuf;
1910 while (*lparenBuf != '(') lparenBuf++;
1911 ReplaceText(startLoc, lparenBuf-startBuf+1, buf);
1913 buf = "; objc_sync_enter(_sync_obj);\n";
1914 buf += "try {\n\tstruct _SYNC_EXIT { _SYNC_EXIT(id arg) : sync_exit(arg) {}";
1915 buf += "\n\t~_SYNC_EXIT() {objc_sync_exit(sync_exit);}";
1916 buf += "\n\tid sync_exit;";
1917 buf += "\n\t} _sync_exit(_sync_obj);\n";
1919 // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since
1920 // the sync expression is typically a message expression that's already
1921 // been rewritten! (which implies the SourceLocation's are invalid).
1922 SourceLocation RParenExprLoc = S->getSynchBody()->getLocStart();
1923 const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc);
1924 while (*RParenExprLocBuf != ')') RParenExprLocBuf--;
1925 RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf);
1927 SourceLocation LBranceLoc = S->getSynchBody()->getLocStart();
1928 const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc);
1929 assert (*LBraceLocBuf == '{');
1930 ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf);
1932 SourceLocation startRBraceLoc = S->getSynchBody()->getLocEnd();
1933 assert((*SM->getCharacterData(startRBraceLoc) == '}') &&
1934 "bogus @synchronized block");
1936 buf = "} catch (id e) {_rethrow = e;}\n";
1937 Write_RethrowObject(buf);
1941 ReplaceText(startRBraceLoc, 1, buf);
1946 void RewriteModernObjC::WarnAboutReturnGotoStmts(Stmt *S)
1948 // Perform a bottom up traversal of all children.
1949 for (Stmt::child_range CI = S->children(); CI; ++CI)
1951 WarnAboutReturnGotoStmts(*CI);
1953 if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) {
1954 Diags.Report(Context->getFullLoc(S->getLocStart()),
1955 TryFinallyContainsReturnDiag);
1960 Stmt *RewriteModernObjC::RewriteObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
1961 SourceLocation startLoc = S->getAtLoc();
1962 ReplaceText(startLoc, strlen("@autoreleasepool"), "/* @autoreleasepool */");
1963 ReplaceText(S->getSubStmt()->getLocStart(), 1,
1964 "{ __AtAutoreleasePool __autoreleasepool; ");
1969 Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
1970 ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt();
1971 bool noCatch = S->getNumCatchStmts() == 0;
1973 SourceLocation TryLocation = S->getAtTryLoc();
1974 ConvertSourceLocationToLineDirective(TryLocation, buf);
1978 buf += "{ id volatile _rethrow = 0;\n";
1980 buf += "{ id volatile _rethrow = 0;\ntry {\n";
1983 // Get the start location and compute the semi location.
1984 SourceLocation startLoc = S->getLocStart();
1985 const char *startBuf = SM->getCharacterData(startLoc);
1987 assert((*startBuf == '@') && "bogus @try location");
1989 ReplaceText(startLoc, 1, buf);
1992 ReplaceText(startLoc, 1, "");
1994 for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) {
1995 ObjCAtCatchStmt *Catch = S->getCatchStmt(I);
1996 VarDecl *catchDecl = Catch->getCatchParamDecl();
1998 startLoc = Catch->getLocStart();
1999 bool AtRemoved = false;
2001 QualType t = catchDecl->getType();
2002 if (const ObjCObjectPointerType *Ptr = t->getAs<ObjCObjectPointerType>()) {
2003 // Should be a pointer to a class.
2004 ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface();
2007 ConvertSourceLocationToLineDirective(Catch->getLocStart(), Result);
2009 startBuf = SM->getCharacterData(startLoc);
2010 assert((*startBuf == '@') && "bogus @catch location");
2011 SourceLocation rParenLoc = Catch->getRParenLoc();
2012 const char *rParenBuf = SM->getCharacterData(rParenLoc);
2014 // _objc_exc_Foo *_e as argument to catch.
2015 Result += "catch (_objc_exc_"; Result += IDecl->getNameAsString();
2016 Result += " *_"; Result += catchDecl->getNameAsString();
2018 ReplaceText(startLoc, rParenBuf-startBuf+1, Result);
2019 // Foo *e = (Foo *)_e;
2022 Result += IDecl->getNameAsString();
2023 Result += " *"; Result += catchDecl->getNameAsString();
2024 Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)";
2025 Result += "_"; Result += catchDecl->getNameAsString();
2028 SourceLocation lBraceLoc = Catch->getCatchBody()->getLocStart();
2029 ReplaceText(lBraceLoc, 1, Result);
2036 ReplaceText(startLoc, 1, "");
2041 SourceLocation FinallyLoc = finalStmt->getLocStart();
2044 ConvertSourceLocationToLineDirective(FinallyLoc, buf);
2045 buf += "catch (id e) {_rethrow = e;}\n";
2049 ConvertSourceLocationToLineDirective(FinallyLoc, buf);
2050 buf += "catch (id e) {_rethrow = e;}\n";
2053 SourceLocation startFinalLoc = finalStmt->getLocStart();
2054 ReplaceText(startFinalLoc, 8, buf);
2055 Stmt *body = finalStmt->getFinallyBody();
2056 SourceLocation startFinalBodyLoc = body->getLocStart();
2058 Write_RethrowObject(buf);
2059 ReplaceText(startFinalBodyLoc, 1, buf);
2061 SourceLocation endFinalBodyLoc = body->getLocEnd();
2062 ReplaceText(endFinalBodyLoc, 1, "}\n}");
2063 // Now check for any return/continue/go statements within the @try.
2064 WarnAboutReturnGotoStmts(S->getTryBody());
2070 // This can't be done with ReplaceStmt(S, ThrowExpr), since
2071 // the throw expression is typically a message expression that's already
2072 // been rewritten! (which implies the SourceLocation's are invalid).
2073 Stmt *RewriteModernObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) {
2074 // Get the start location and compute the semi location.
2075 SourceLocation startLoc = S->getLocStart();
2076 const char *startBuf = SM->getCharacterData(startLoc);
2078 assert((*startBuf == '@') && "bogus @throw location");
2081 /* void objc_exception_throw(id) __attribute__((noreturn)); */
2082 if (S->getThrowExpr())
2083 buf = "objc_exception_throw(";
2087 // handle "@ throw" correctly.
2088 const char *wBuf = strchr(startBuf, 'w');
2089 assert((*wBuf == 'w') && "@throw: can't find 'w'");
2090 ReplaceText(startLoc, wBuf-startBuf+1, buf);
2092 SourceLocation endLoc = S->getLocEnd();
2093 const char *endBuf = SM->getCharacterData(endLoc);
2094 const char *semiBuf = strchr(endBuf, ';');
2095 assert((*semiBuf == ';') && "@throw: can't find ';'");
2096 SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf);
2097 if (S->getThrowExpr())
2098 ReplaceText(semiLoc, 1, ");");
2102 Stmt *RewriteModernObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) {
2103 // Create a new string expression.
2104 std::string StrEncoding;
2105 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
2106 Expr *Replacement = getStringLiteral(StrEncoding);
2107 ReplaceStmt(Exp, Replacement);
2109 // Replace this subexpr in the parent.
2110 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2114 Stmt *RewriteModernObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) {
2115 if (!SelGetUidFunctionDecl)
2116 SynthSelGetUidFunctionDecl();
2117 assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl");
2118 // Create a call to sel_registerName("selName").
2119 SmallVector<Expr*, 8> SelExprs;
2120 SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString()));
2121 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2122 &SelExprs[0], SelExprs.size());
2123 ReplaceStmt(Exp, SelExp);
2124 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2128 CallExpr *RewriteModernObjC::SynthesizeCallToFunctionDecl(
2129 FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc,
2130 SourceLocation EndLoc) {
2131 // Get the type, we will need to reference it in a couple spots.
2132 QualType msgSendType = FD->getType();
2134 // Create a reference to the objc_msgSend() declaration.
2136 new (Context) DeclRefExpr(FD, false, msgSendType, VK_LValue, SourceLocation());
2138 // Now, we cast the reference to a pointer to the objc_msgSend type.
2139 QualType pToFunc = Context->getPointerType(msgSendType);
2140 ImplicitCastExpr *ICE =
2141 ImplicitCastExpr::Create(*Context, pToFunc, CK_FunctionToPointerDecay,
2142 DRE, nullptr, VK_RValue);
2144 const FunctionType *FT = msgSendType->getAs<FunctionType>();
2147 new (Context) CallExpr(*Context, ICE, llvm::makeArrayRef(args, nargs),
2148 FT->getCallResultType(*Context),
2153 static bool scanForProtocolRefs(const char *startBuf, const char *endBuf,
2154 const char *&startRef, const char *&endRef) {
2155 while (startBuf < endBuf) {
2156 if (*startBuf == '<')
2157 startRef = startBuf; // mark the start.
2158 if (*startBuf == '>') {
2159 if (startRef && *startRef == '<') {
2160 endRef = startBuf; // mark the end.
2170 static void scanToNextArgument(const char *&argRef) {
2172 while (*argRef != ')' && (*argRef != ',' || angle > 0)) {
2175 else if (*argRef == '>')
2179 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
2182 bool RewriteModernObjC::needToScanForQualifiers(QualType T) {
2183 if (T->isObjCQualifiedIdType())
2185 if (const PointerType *PT = T->getAs<PointerType>()) {
2186 if (PT->getPointeeType()->isObjCQualifiedIdType())
2189 if (T->isObjCObjectPointerType()) {
2190 T = T->getPointeeType();
2191 return T->isObjCQualifiedInterfaceType();
2193 if (T->isArrayType()) {
2194 QualType ElemTy = Context->getBaseElementType(T);
2195 return needToScanForQualifiers(ElemTy);
2200 void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) {
2201 QualType Type = E->getType();
2202 if (needToScanForQualifiers(Type)) {
2203 SourceLocation Loc, EndLoc;
2205 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) {
2206 Loc = ECE->getLParenLoc();
2207 EndLoc = ECE->getRParenLoc();
2209 Loc = E->getLocStart();
2210 EndLoc = E->getLocEnd();
2212 // This will defend against trying to rewrite synthesized expressions.
2213 if (Loc.isInvalid() || EndLoc.isInvalid())
2216 const char *startBuf = SM->getCharacterData(Loc);
2217 const char *endBuf = SM->getCharacterData(EndLoc);
2218 const char *startRef = nullptr, *endRef = nullptr;
2219 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2220 // Get the locations of the startRef, endRef.
2221 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf);
2222 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1);
2223 // Comment out the protocol references.
2224 InsertText(LessLoc, "/*");
2225 InsertText(GreaterLoc, "*/");
2230 void RewriteModernObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) {
2233 const FunctionProtoType *proto = nullptr;
2234 if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) {
2235 Loc = VD->getLocation();
2236 Type = VD->getType();
2238 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) {
2239 Loc = FD->getLocation();
2240 // Check for ObjC 'id' and class types that have been adorned with protocol
2241 // information (id<p>, C<p>*). The protocol references need to be rewritten!
2242 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2243 assert(funcType && "missing function type");
2244 proto = dyn_cast<FunctionProtoType>(funcType);
2247 Type = proto->getReturnType();
2249 else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) {
2250 Loc = FD->getLocation();
2251 Type = FD->getType();
2253 else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(Dcl)) {
2254 Loc = TD->getLocation();
2255 Type = TD->getUnderlyingType();
2260 if (needToScanForQualifiers(Type)) {
2261 // Since types are unique, we need to scan the buffer.
2263 const char *endBuf = SM->getCharacterData(Loc);
2264 const char *startBuf = endBuf;
2265 while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart)
2266 startBuf--; // scan backward (from the decl location) for return type.
2267 const char *startRef = nullptr, *endRef = nullptr;
2268 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2269 // Get the locations of the startRef, endRef.
2270 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf);
2271 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1);
2272 // Comment out the protocol references.
2273 InsertText(LessLoc, "/*");
2274 InsertText(GreaterLoc, "*/");
2278 return; // most likely, was a variable
2279 // Now check arguments.
2280 const char *startBuf = SM->getCharacterData(Loc);
2281 const char *startFuncBuf = startBuf;
2282 for (unsigned i = 0; i < proto->getNumParams(); i++) {
2283 if (needToScanForQualifiers(proto->getParamType(i))) {
2284 // Since types are unique, we need to scan the buffer.
2286 const char *endBuf = startBuf;
2287 // scan forward (from the decl location) for argument types.
2288 scanToNextArgument(endBuf);
2289 const char *startRef = nullptr, *endRef = nullptr;
2290 if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) {
2291 // Get the locations of the startRef, endRef.
2292 SourceLocation LessLoc =
2293 Loc.getLocWithOffset(startRef-startFuncBuf);
2294 SourceLocation GreaterLoc =
2295 Loc.getLocWithOffset(endRef-startFuncBuf+1);
2296 // Comment out the protocol references.
2297 InsertText(LessLoc, "/*");
2298 InsertText(GreaterLoc, "*/");
2300 startBuf = ++endBuf;
2303 // If the function name is derived from a macro expansion, then the
2304 // argument buffer will not follow the name. Need to speak with Chris.
2305 while (*startBuf && *startBuf != ')' && *startBuf != ',')
2306 startBuf++; // scan forward (from the decl location) for argument types.
2312 void RewriteModernObjC::RewriteTypeOfDecl(VarDecl *ND) {
2313 QualType QT = ND->getType();
2314 const Type* TypePtr = QT->getAs<Type>();
2315 if (!isa<TypeOfExprType>(TypePtr))
2317 while (isa<TypeOfExprType>(TypePtr)) {
2318 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
2319 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
2320 TypePtr = QT->getAs<Type>();
2322 // FIXME. This will not work for multiple declarators; as in:
2323 // __typeof__(a) b,c,d;
2324 std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy()));
2325 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
2326 const char *startBuf = SM->getCharacterData(DeclLoc);
2327 if (ND->getInit()) {
2328 std::string Name(ND->getNameAsString());
2329 TypeAsString += " " + Name + " = ";
2330 Expr *E = ND->getInit();
2331 SourceLocation startLoc;
2332 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
2333 startLoc = ECE->getLParenLoc();
2335 startLoc = E->getLocStart();
2336 startLoc = SM->getExpansionLoc(startLoc);
2337 const char *endBuf = SM->getCharacterData(startLoc);
2338 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2341 SourceLocation X = ND->getLocEnd();
2342 X = SM->getExpansionLoc(X);
2343 const char *endBuf = SM->getCharacterData(X);
2344 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2348 // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
2349 void RewriteModernObjC::SynthSelGetUidFunctionDecl() {
2350 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
2351 SmallVector<QualType, 16> ArgTys;
2352 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2353 QualType getFuncType =
2354 getSimpleFunctionType(Context->getObjCSelType(), ArgTys);
2355 SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2358 SelGetUidIdent, getFuncType,
2359 nullptr, SC_Extern);
2362 void RewriteModernObjC::RewriteFunctionDecl(FunctionDecl *FD) {
2363 // declared in <objc/objc.h>
2364 if (FD->getIdentifier() &&
2365 FD->getName() == "sel_registerName") {
2366 SelGetUidFunctionDecl = FD;
2369 RewriteObjCQualifiedInterfaceTypes(FD);
2372 void RewriteModernObjC::RewriteBlockPointerType(std::string& Str, QualType Type) {
2373 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2374 const char *argPtr = TypeString.c_str();
2375 if (!strchr(argPtr, '^')) {
2380 Str += (*argPtr == '^' ? '*' : *argPtr);
2385 // FIXME. Consolidate this routine with RewriteBlockPointerType.
2386 void RewriteModernObjC::RewriteBlockPointerTypeVariable(std::string& Str,
2388 QualType Type = VD->getType();
2389 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2390 const char *argPtr = TypeString.c_str();
2405 Str += VD->getNameAsString();
2415 void RewriteModernObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) {
2416 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
2417 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2418 const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType);
2421 QualType Type = proto->getReturnType();
2422 std::string FdStr = Type.getAsString(Context->getPrintingPolicy());
2424 FdStr += FD->getName();
2426 unsigned numArgs = proto->getNumParams();
2427 for (unsigned i = 0; i < numArgs; i++) {
2428 QualType ArgType = proto->getParamType(i);
2429 RewriteBlockPointerType(FdStr, ArgType);
2433 if (FD->isVariadic()) {
2434 FdStr += (numArgs > 0) ? ", ...);\n" : "...);\n";
2438 InsertText(FunLocStart, FdStr);
2441 // SynthSuperConstructorFunctionDecl - id __rw_objc_super(id obj, id super);
2442 void RewriteModernObjC::SynthSuperConstructorFunctionDecl() {
2443 if (SuperConstructorFunctionDecl)
2445 IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super");
2446 SmallVector<QualType, 16> ArgTys;
2447 QualType argT = Context->getObjCIdType();
2448 assert(!argT.isNull() && "Can't find 'id' type");
2449 ArgTys.push_back(argT);
2450 ArgTys.push_back(argT);
2451 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2453 SuperConstructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2456 msgSendIdent, msgSendType,
2457 nullptr, SC_Extern);
2460 // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
2461 void RewriteModernObjC::SynthMsgSendFunctionDecl() {
2462 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
2463 SmallVector<QualType, 16> ArgTys;
2464 QualType argT = Context->getObjCIdType();
2465 assert(!argT.isNull() && "Can't find 'id' type");
2466 ArgTys.push_back(argT);
2467 argT = Context->getObjCSelType();
2468 assert(!argT.isNull() && "Can't find 'SEL' type");
2469 ArgTys.push_back(argT);
2470 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2471 ArgTys, /*isVariadic=*/true);
2472 MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2475 msgSendIdent, msgSendType, nullptr,
2479 // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(void);
2480 void RewriteModernObjC::SynthMsgSendSuperFunctionDecl() {
2481 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
2482 SmallVector<QualType, 2> ArgTys;
2483 ArgTys.push_back(Context->VoidTy);
2484 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2485 ArgTys, /*isVariadic=*/true);
2486 MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2489 msgSendIdent, msgSendType,
2490 nullptr, SC_Extern);
2493 // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
2494 void RewriteModernObjC::SynthMsgSendStretFunctionDecl() {
2495 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
2496 SmallVector<QualType, 16> ArgTys;
2497 QualType argT = Context->getObjCIdType();
2498 assert(!argT.isNull() && "Can't find 'id' type");
2499 ArgTys.push_back(argT);
2500 argT = Context->getObjCSelType();
2501 assert(!argT.isNull() && "Can't find 'SEL' type");
2502 ArgTys.push_back(argT);
2503 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2504 ArgTys, /*isVariadic=*/true);
2505 MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2508 msgSendIdent, msgSendType,
2509 nullptr, SC_Extern);
2512 // SynthMsgSendSuperStretFunctionDecl -
2513 // id objc_msgSendSuper_stret(void);
2514 void RewriteModernObjC::SynthMsgSendSuperStretFunctionDecl() {
2515 IdentifierInfo *msgSendIdent =
2516 &Context->Idents.get("objc_msgSendSuper_stret");
2517 SmallVector<QualType, 2> ArgTys;
2518 ArgTys.push_back(Context->VoidTy);
2519 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2520 ArgTys, /*isVariadic=*/true);
2521 MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2525 msgSendType, nullptr,
2529 // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...);
2530 void RewriteModernObjC::SynthMsgSendFpretFunctionDecl() {
2531 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
2532 SmallVector<QualType, 16> ArgTys;
2533 QualType argT = Context->getObjCIdType();
2534 assert(!argT.isNull() && "Can't find 'id' type");
2535 ArgTys.push_back(argT);
2536 argT = Context->getObjCSelType();
2537 assert(!argT.isNull() && "Can't find 'SEL' type");
2538 ArgTys.push_back(argT);
2539 QualType msgSendType = getSimpleFunctionType(Context->DoubleTy,
2540 ArgTys, /*isVariadic=*/true);
2541 MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2544 msgSendIdent, msgSendType,
2545 nullptr, SC_Extern);
2548 // SynthGetClassFunctionDecl - Class objc_getClass(const char *name);
2549 void RewriteModernObjC::SynthGetClassFunctionDecl() {
2550 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
2551 SmallVector<QualType, 16> ArgTys;
2552 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2553 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2555 GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2558 getClassIdent, getClassType,
2559 nullptr, SC_Extern);
2562 // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls);
2563 void RewriteModernObjC::SynthGetSuperClassFunctionDecl() {
2564 IdentifierInfo *getSuperClassIdent =
2565 &Context->Idents.get("class_getSuperclass");
2566 SmallVector<QualType, 16> ArgTys;
2567 ArgTys.push_back(Context->getObjCClassType());
2568 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2570 GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2574 getClassType, nullptr,
2578 // SynthGetMetaClassFunctionDecl - Class objc_getMetaClass(const char *name);
2579 void RewriteModernObjC::SynthGetMetaClassFunctionDecl() {
2580 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
2581 SmallVector<QualType, 16> ArgTys;
2582 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2583 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2585 GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
2588 getClassIdent, getClassType,
2589 nullptr, SC_Extern);
2592 Stmt *RewriteModernObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
2593 assert (Exp != nullptr && "Expected non-null ObjCStringLiteral");
2594 QualType strType = getConstantStringStructType();
2596 std::string S = "__NSConstantStringImpl_";
2598 std::string tmpName = InFileName;
2600 for (i=0; i < tmpName.length(); i++) {
2601 char c = tmpName.at(i);
2602 // replace any non-alphanumeric characters with '_'.
2603 if (!isAlphanumeric(c))
2608 S += utostr(NumObjCStringLiterals++);
2610 Preamble += "static __NSConstantStringImpl " + S;
2611 Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,";
2612 Preamble += "0x000007c8,"; // utf8_str
2613 // The pretty printer for StringLiteral handles escape characters properly.
2614 std::string prettyBufS;
2615 llvm::raw_string_ostream prettyBuf(prettyBufS);
2616 Exp->getString()->printPretty(prettyBuf, nullptr, PrintingPolicy(LangOpts));
2617 Preamble += prettyBuf.str();
2619 Preamble += utostr(Exp->getString()->getByteLength()) + "};\n";
2621 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
2622 SourceLocation(), &Context->Idents.get(S),
2623 strType, nullptr, SC_Static);
2624 DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false, strType, VK_LValue,
2626 Expr *Unop = new (Context) UnaryOperator(DRE, UO_AddrOf,
2627 Context->getPointerType(DRE->getType()),
2628 VK_RValue, OK_Ordinary,
2630 // cast to NSConstantString *
2631 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(),
2632 CK_CPointerToObjCPointerCast, Unop);
2633 ReplaceStmt(Exp, cast);
2634 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
2638 Stmt *RewriteModernObjC::RewriteObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Exp) {
2640 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
2642 Expr *FlagExp = IntegerLiteral::Create(*Context,
2643 llvm::APInt(IntSize, Exp->getValue()),
2644 Context->IntTy, Exp->getLocation());
2645 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Context->ObjCBuiltinBoolTy,
2646 CK_BitCast, FlagExp);
2647 ParenExpr *PE = new (Context) ParenExpr(Exp->getLocation(), Exp->getExprLoc(),
2649 ReplaceStmt(Exp, PE);
2653 Stmt *RewriteModernObjC::RewriteObjCBoxedExpr(ObjCBoxedExpr *Exp) {
2654 // synthesize declaration of helper functions needed in this routine.
2655 if (!SelGetUidFunctionDecl)
2656 SynthSelGetUidFunctionDecl();
2657 // use objc_msgSend() for all.
2658 if (!MsgSendFunctionDecl)
2659 SynthMsgSendFunctionDecl();
2660 if (!GetClassFunctionDecl)
2661 SynthGetClassFunctionDecl();
2663 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2664 SourceLocation StartLoc = Exp->getLocStart();
2665 SourceLocation EndLoc = Exp->getLocEnd();
2667 // Synthesize a call to objc_msgSend().
2668 SmallVector<Expr*, 4> MsgExprs;
2669 SmallVector<Expr*, 4> ClsExprs;
2671 // Create a call to objc_getClass("<BoxingClass>"). It will be the 1st argument.
2672 ObjCMethodDecl *BoxingMethod = Exp->getBoxingMethod();
2673 ObjCInterfaceDecl *BoxingClass = BoxingMethod->getClassInterface();
2675 IdentifierInfo *clsName = BoxingClass->getIdentifier();
2676 ClsExprs.push_back(getStringLiteral(clsName->getName()));
2677 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2681 MsgExprs.push_back(Cls);
2683 // Create a call to sel_registerName("<BoxingMethod>:"), etc.
2684 // it will be the 2nd argument.
2685 SmallVector<Expr*, 4> SelExprs;
2687 getStringLiteral(BoxingMethod->getSelector().getAsString()));
2688 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2689 &SelExprs[0], SelExprs.size(),
2691 MsgExprs.push_back(SelExp);
2693 // User provided sub-expression is the 3rd, and last, argument.
2694 Expr *subExpr = Exp->getSubExpr();
2695 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(subExpr)) {
2696 QualType type = ICE->getType();
2697 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
2698 CastKind CK = CK_BitCast;
2699 if (SubExpr->getType()->isIntegralType(*Context) && type->isBooleanType())
2700 CK = CK_IntegralToBoolean;
2701 subExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, subExpr);
2703 MsgExprs.push_back(subExpr);
2705 SmallVector<QualType, 4> ArgTypes;
2706 ArgTypes.push_back(Context->getObjCIdType());
2707 ArgTypes.push_back(Context->getObjCSelType());
2708 for (const auto PI : BoxingMethod->parameters())
2709 ArgTypes.push_back(PI->getType());
2711 QualType returnType = Exp->getType();
2712 // Get the type, we will need to reference it in a couple spots.
2713 QualType msgSendType = MsgSendFlavor->getType();
2715 // Create a reference to the objc_msgSend() declaration.
2716 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
2717 VK_LValue, SourceLocation());
2719 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context,
2720 Context->getPointerType(Context->VoidTy),
2723 // Now do the "normal" pointer to function cast.
2725 getSimpleFunctionType(returnType, ArgTypes, BoxingMethod->isVariadic());
2726 castType = Context->getPointerType(castType);
2727 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
2730 // Don't forget the parens to enforce the proper binding.
2731 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
2733 const FunctionType *FT = msgSendType->getAs<FunctionType>();
2734 CallExpr *CE = new (Context)
2735 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
2736 ReplaceStmt(Exp, CE);
2740 Stmt *RewriteModernObjC::RewriteObjCArrayLiteralExpr(ObjCArrayLiteral *Exp) {
2741 // synthesize declaration of helper functions needed in this routine.
2742 if (!SelGetUidFunctionDecl)
2743 SynthSelGetUidFunctionDecl();
2744 // use objc_msgSend() for all.
2745 if (!MsgSendFunctionDecl)
2746 SynthMsgSendFunctionDecl();
2747 if (!GetClassFunctionDecl)
2748 SynthGetClassFunctionDecl();
2750 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2751 SourceLocation StartLoc = Exp->getLocStart();
2752 SourceLocation EndLoc = Exp->getLocEnd();
2754 // Build the expression: __NSContainer_literal(int, ...).arr
2755 QualType IntQT = Context->IntTy;
2756 QualType NSArrayFType =
2757 getSimpleFunctionType(Context->VoidTy, IntQT, true);
2758 std::string NSArrayFName("__NSContainer_literal");
2759 FunctionDecl *NSArrayFD = SynthBlockInitFunctionDecl(NSArrayFName);
2760 DeclRefExpr *NSArrayDRE =
2761 new (Context) DeclRefExpr(NSArrayFD, false, NSArrayFType, VK_RValue,
2764 SmallVector<Expr*, 16> InitExprs;
2765 unsigned NumElements = Exp->getNumElements();
2766 unsigned UnsignedIntSize =
2767 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
2768 Expr *count = IntegerLiteral::Create(*Context,
2769 llvm::APInt(UnsignedIntSize, NumElements),
2770 Context->UnsignedIntTy, SourceLocation());
2771 InitExprs.push_back(count);
2772 for (unsigned i = 0; i < NumElements; i++)
2773 InitExprs.push_back(Exp->getElement(i));
2774 Expr *NSArrayCallExpr =
2775 new (Context) CallExpr(*Context, NSArrayDRE, InitExprs,
2776 NSArrayFType, VK_LValue, SourceLocation());
2778 FieldDecl *ARRFD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
2780 &Context->Idents.get("arr"),
2781 Context->getPointerType(Context->VoidPtrTy),
2782 nullptr, /*BitWidth=*/nullptr,
2783 /*Mutable=*/true, ICIS_NoInit);
2784 MemberExpr *ArrayLiteralME =
2785 new (Context) MemberExpr(NSArrayCallExpr, false, ARRFD,
2787 ARRFD->getType(), VK_LValue,
2789 QualType ConstIdT = Context->getObjCIdType().withConst();
2790 CStyleCastExpr * ArrayLiteralObjects =
2791 NoTypeInfoCStyleCastExpr(Context,
2792 Context->getPointerType(ConstIdT),
2796 // Synthesize a call to objc_msgSend().
2797 SmallVector<Expr*, 32> MsgExprs;
2798 SmallVector<Expr*, 4> ClsExprs;
2799 QualType expType = Exp->getType();
2801 // Create a call to objc_getClass("NSArray"). It will be th 1st argument.
2802 ObjCInterfaceDecl *Class =
2803 expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface();
2805 IdentifierInfo *clsName = Class->getIdentifier();
2806 ClsExprs.push_back(getStringLiteral(clsName->getName()));
2807 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2811 MsgExprs.push_back(Cls);
2813 // Create a call to sel_registerName("arrayWithObjects:count:").
2814 // it will be the 2nd argument.
2815 SmallVector<Expr*, 4> SelExprs;
2816 ObjCMethodDecl *ArrayMethod = Exp->getArrayWithObjectsMethod();
2818 getStringLiteral(ArrayMethod->getSelector().getAsString()));
2819 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2820 &SelExprs[0], SelExprs.size(),
2822 MsgExprs.push_back(SelExp);
2824 // (const id [])objects
2825 MsgExprs.push_back(ArrayLiteralObjects);
2828 Expr *cnt = IntegerLiteral::Create(*Context,
2829 llvm::APInt(UnsignedIntSize, NumElements),
2830 Context->UnsignedIntTy, SourceLocation());
2831 MsgExprs.push_back(cnt);
2834 SmallVector<QualType, 4> ArgTypes;
2835 ArgTypes.push_back(Context->getObjCIdType());
2836 ArgTypes.push_back(Context->getObjCSelType());
2837 for (const auto *PI : ArrayMethod->params())
2838 ArgTypes.push_back(PI->getType());
2840 QualType returnType = Exp->getType();
2841 // Get the type, we will need to reference it in a couple spots.
2842 QualType msgSendType = MsgSendFlavor->getType();
2844 // Create a reference to the objc_msgSend() declaration.
2845 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
2846 VK_LValue, SourceLocation());
2848 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context,
2849 Context->getPointerType(Context->VoidTy),
2852 // Now do the "normal" pointer to function cast.
2854 getSimpleFunctionType(returnType, ArgTypes, ArrayMethod->isVariadic());
2855 castType = Context->getPointerType(castType);
2856 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
2859 // Don't forget the parens to enforce the proper binding.
2860 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
2862 const FunctionType *FT = msgSendType->getAs<FunctionType>();
2863 CallExpr *CE = new (Context)
2864 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
2865 ReplaceStmt(Exp, CE);
2869 Stmt *RewriteModernObjC::RewriteObjCDictionaryLiteralExpr(ObjCDictionaryLiteral *Exp) {
2870 // synthesize declaration of helper functions needed in this routine.
2871 if (!SelGetUidFunctionDecl)
2872 SynthSelGetUidFunctionDecl();
2873 // use objc_msgSend() for all.
2874 if (!MsgSendFunctionDecl)
2875 SynthMsgSendFunctionDecl();
2876 if (!GetClassFunctionDecl)
2877 SynthGetClassFunctionDecl();
2879 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
2880 SourceLocation StartLoc = Exp->getLocStart();
2881 SourceLocation EndLoc = Exp->getLocEnd();
2883 // Build the expression: __NSContainer_literal(int, ...).arr
2884 QualType IntQT = Context->IntTy;
2885 QualType NSDictFType =
2886 getSimpleFunctionType(Context->VoidTy, IntQT, true);
2887 std::string NSDictFName("__NSContainer_literal");
2888 FunctionDecl *NSDictFD = SynthBlockInitFunctionDecl(NSDictFName);
2889 DeclRefExpr *NSDictDRE =
2890 new (Context) DeclRefExpr(NSDictFD, false, NSDictFType, VK_RValue,
2893 SmallVector<Expr*, 16> KeyExprs;
2894 SmallVector<Expr*, 16> ValueExprs;
2896 unsigned NumElements = Exp->getNumElements();
2897 unsigned UnsignedIntSize =
2898 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
2899 Expr *count = IntegerLiteral::Create(*Context,
2900 llvm::APInt(UnsignedIntSize, NumElements),
2901 Context->UnsignedIntTy, SourceLocation());
2902 KeyExprs.push_back(count);
2903 ValueExprs.push_back(count);
2904 for (unsigned i = 0; i < NumElements; i++) {
2905 ObjCDictionaryElement Element = Exp->getKeyValueElement(i);
2906 KeyExprs.push_back(Element.Key);
2907 ValueExprs.push_back(Element.Value);
2910 // (const id [])objects
2911 Expr *NSValueCallExpr =
2912 new (Context) CallExpr(*Context, NSDictDRE, ValueExprs,
2913 NSDictFType, VK_LValue, SourceLocation());
2915 FieldDecl *ARRFD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
2917 &Context->Idents.get("arr"),
2918 Context->getPointerType(Context->VoidPtrTy),
2919 nullptr, /*BitWidth=*/nullptr,
2920 /*Mutable=*/true, ICIS_NoInit);
2921 MemberExpr *DictLiteralValueME =
2922 new (Context) MemberExpr(NSValueCallExpr, false, ARRFD,
2924 ARRFD->getType(), VK_LValue,
2926 QualType ConstIdT = Context->getObjCIdType().withConst();
2927 CStyleCastExpr * DictValueObjects =
2928 NoTypeInfoCStyleCastExpr(Context,
2929 Context->getPointerType(ConstIdT),
2931 DictLiteralValueME);
2932 // (const id <NSCopying> [])keys
2933 Expr *NSKeyCallExpr =
2934 new (Context) CallExpr(*Context, NSDictDRE, KeyExprs,
2935 NSDictFType, VK_LValue, SourceLocation());
2937 MemberExpr *DictLiteralKeyME =
2938 new (Context) MemberExpr(NSKeyCallExpr, false, ARRFD,
2940 ARRFD->getType(), VK_LValue,
2943 CStyleCastExpr * DictKeyObjects =
2944 NoTypeInfoCStyleCastExpr(Context,
2945 Context->getPointerType(ConstIdT),
2951 // Synthesize a call to objc_msgSend().
2952 SmallVector<Expr*, 32> MsgExprs;
2953 SmallVector<Expr*, 4> ClsExprs;
2954 QualType expType = Exp->getType();
2956 // Create a call to objc_getClass("NSArray"). It will be th 1st argument.
2957 ObjCInterfaceDecl *Class =
2958 expType->getPointeeType()->getAs<ObjCObjectType>()->getInterface();
2960 IdentifierInfo *clsName = Class->getIdentifier();
2961 ClsExprs.push_back(getStringLiteral(clsName->getName()));
2962 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
2966 MsgExprs.push_back(Cls);
2968 // Create a call to sel_registerName("arrayWithObjects:count:").
2969 // it will be the 2nd argument.
2970 SmallVector<Expr*, 4> SelExprs;
2971 ObjCMethodDecl *DictMethod = Exp->getDictWithObjectsMethod();
2972 SelExprs.push_back(getStringLiteral(DictMethod->getSelector().getAsString()));
2973 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
2974 &SelExprs[0], SelExprs.size(),
2976 MsgExprs.push_back(SelExp);
2978 // (const id [])objects
2979 MsgExprs.push_back(DictValueObjects);
2981 // (const id <NSCopying> [])keys
2982 MsgExprs.push_back(DictKeyObjects);
2985 Expr *cnt = IntegerLiteral::Create(*Context,
2986 llvm::APInt(UnsignedIntSize, NumElements),
2987 Context->UnsignedIntTy, SourceLocation());
2988 MsgExprs.push_back(cnt);
2991 SmallVector<QualType, 8> ArgTypes;
2992 ArgTypes.push_back(Context->getObjCIdType());
2993 ArgTypes.push_back(Context->getObjCSelType());
2994 for (const auto *PI : DictMethod->params()) {
2995 QualType T = PI->getType();
2996 if (const PointerType* PT = T->getAs<PointerType>()) {
2997 QualType PointeeTy = PT->getPointeeType();
2998 convertToUnqualifiedObjCType(PointeeTy);
2999 T = Context->getPointerType(PointeeTy);
3001 ArgTypes.push_back(T);
3004 QualType returnType = Exp->getType();
3005 // Get the type, we will need to reference it in a couple spots.
3006 QualType msgSendType = MsgSendFlavor->getType();
3008 // Create a reference to the objc_msgSend() declaration.
3009 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
3010 VK_LValue, SourceLocation());
3012 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context,
3013 Context->getPointerType(Context->VoidTy),
3016 // Now do the "normal" pointer to function cast.
3018 getSimpleFunctionType(returnType, ArgTypes, DictMethod->isVariadic());
3019 castType = Context->getPointerType(castType);
3020 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3023 // Don't forget the parens to enforce the proper binding.
3024 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
3026 const FunctionType *FT = msgSendType->getAs<FunctionType>();
3027 CallExpr *CE = new (Context)
3028 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
3029 ReplaceStmt(Exp, CE);
3033 // struct __rw_objc_super {
3034 // struct objc_object *object; struct objc_object *superClass;
3036 QualType RewriteModernObjC::getSuperStructType() {
3037 if (!SuperStructDecl) {
3038 SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
3039 SourceLocation(), SourceLocation(),
3040 &Context->Idents.get("__rw_objc_super"));
3041 QualType FieldTypes[2];
3043 // struct objc_object *object;
3044 FieldTypes[0] = Context->getObjCIdType();
3045 // struct objc_object *superClass;
3046 FieldTypes[1] = Context->getObjCIdType();
3049 for (unsigned i = 0; i < 2; ++i) {
3050 SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl,
3052 SourceLocation(), nullptr,
3053 FieldTypes[i], nullptr,
3054 /*BitWidth=*/nullptr,
3059 SuperStructDecl->completeDefinition();
3061 return Context->getTagDeclType(SuperStructDecl);
3064 QualType RewriteModernObjC::getConstantStringStructType() {
3065 if (!ConstantStringDecl) {
3066 ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
3067 SourceLocation(), SourceLocation(),
3068 &Context->Idents.get("__NSConstantStringImpl"));
3069 QualType FieldTypes[4];
3071 // struct objc_object *receiver;
3072 FieldTypes[0] = Context->getObjCIdType();
3074 FieldTypes[1] = Context->IntTy;
3076 FieldTypes[2] = Context->getPointerType(Context->CharTy);
3078 FieldTypes[3] = Context->LongTy;
3081 for (unsigned i = 0; i < 4; ++i) {
3082 ConstantStringDecl->addDecl(FieldDecl::Create(*Context,
3085 SourceLocation(), nullptr,
3086 FieldTypes[i], nullptr,
3087 /*BitWidth=*/nullptr,
3092 ConstantStringDecl->completeDefinition();
3094 return Context->getTagDeclType(ConstantStringDecl);
3097 /// getFunctionSourceLocation - returns start location of a function
3098 /// definition. Complication arises when function has declared as
3099 /// extern "C" or extern "C" {...}
3100 static SourceLocation getFunctionSourceLocation (RewriteModernObjC &R,
3102 if (FD->isExternC() && !FD->isMain()) {
3103 const DeclContext *DC = FD->getDeclContext();
3104 if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC))
3105 // if it is extern "C" {...}, return function decl's own location.
3106 if (!LSD->getRBraceLoc().isValid())
3107 return LSD->getExternLoc();
3109 if (FD->getStorageClass() != SC_None)
3110 R.RewriteBlockLiteralFunctionDecl(FD);
3111 return FD->getTypeSpecStartLoc();
3114 void RewriteModernObjC::RewriteLineDirective(const Decl *D) {
3116 SourceLocation Location = D->getLocation();
3118 if (Location.isFileID() && GenerateLineInfo) {
3119 std::string LineString("\n#line ");
3120 PresumedLoc PLoc = SM->getPresumedLoc(Location);
3121 LineString += utostr(PLoc.getLine());
3122 LineString += " \"";
3123 LineString += Lexer::Stringify(PLoc.getFilename());
3124 if (isa<ObjCMethodDecl>(D))
3126 else LineString += "\"\n";
3128 Location = D->getLocStart();
3129 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3130 if (FD->isExternC() && !FD->isMain()) {
3131 const DeclContext *DC = FD->getDeclContext();
3132 if (const LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(DC))
3133 // if it is extern "C" {...}, return function decl's own location.
3134 if (!LSD->getRBraceLoc().isValid())
3135 Location = LSD->getExternLoc();
3138 InsertText(Location, LineString);
3142 /// SynthMsgSendStretCallExpr - This routine translates message expression
3143 /// into a call to objc_msgSend_stret() entry point. Tricky part is that
3144 /// nil check on receiver must be performed before calling objc_msgSend_stret.
3145 /// MsgSendStretFlavor - function declaration objc_msgSend_stret(...)
3146 /// msgSendType - function type of objc_msgSend_stret(...)
3147 /// returnType - Result type of the method being synthesized.
3148 /// ArgTypes - type of the arguments passed to objc_msgSend_stret, starting with receiver type.
3149 /// MsgExprs - list of argument expressions being passed to objc_msgSend_stret,
3150 /// starting with receiver.
3151 /// Method - Method being rewritten.
3152 Expr *RewriteModernObjC::SynthMsgSendStretCallExpr(FunctionDecl *MsgSendStretFlavor,
3153 QualType returnType,
3154 SmallVectorImpl<QualType> &ArgTypes,
3155 SmallVectorImpl<Expr*> &MsgExprs,
3156 ObjCMethodDecl *Method) {
3157 // Now do the "normal" pointer to function cast.
3158 QualType castType = getSimpleFunctionType(returnType, ArgTypes,
3159 Method ? Method->isVariadic()
3161 castType = Context->getPointerType(castType);
3163 // build type for containing the objc_msgSend_stret object.
3164 static unsigned stretCount=0;
3165 std::string name = "__Stret"; name += utostr(stretCount);
3167 "extern \"C\" void * __cdecl memset(void *_Dst, int _Val, size_t _Size);\n";
3168 str += "namespace {\n";
3169 str += "struct "; str += name;
3172 str += "(id receiver, SEL sel";
3173 for (unsigned i = 2; i < ArgTypes.size(); i++) {
3174 std::string ArgName = "arg"; ArgName += utostr(i);
3175 ArgTypes[i].getAsStringInternal(ArgName, Context->getPrintingPolicy());
3176 str += ", "; str += ArgName;
3179 for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) {
3180 std::string ArgName = "arg"; ArgName += utostr(i);
3181 MsgExprs[i]->getType().getAsStringInternal(ArgName,
3182 Context->getPrintingPolicy());
3183 str += ", "; str += ArgName;
3187 str += "\t unsigned size = sizeof(";
3188 str += returnType.getAsString(Context->getPrintingPolicy()); str += ");\n";
3190 str += "\t if (size == 1 || size == 2 || size == 4 || size == 8)\n";
3192 str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy());
3193 str += ")(void *)objc_msgSend)(receiver, sel";
3194 for (unsigned i = 2; i < ArgTypes.size(); i++) {
3195 str += ", arg"; str += utostr(i);
3198 for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) {
3199 str += ", arg"; str += utostr(i);
3203 str += "\t else if (receiver == 0)\n";
3204 str += "\t memset((void*)&s, 0, sizeof(s));\n";
3208 str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy());
3209 str += ")(void *)objc_msgSend_stret)(receiver, sel";
3210 for (unsigned i = 2; i < ArgTypes.size(); i++) {
3211 str += ", arg"; str += utostr(i);
3214 for (unsigned i = ArgTypes.size(); i < MsgExprs.size(); i++) {
3215 str += ", arg"; str += utostr(i);
3221 str += "\t"; str += returnType.getAsString(Context->getPrintingPolicy());
3223 str += "};\n};\n\n";
3224 SourceLocation FunLocStart;
3226 FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef);
3228 assert(CurMethodDef && "SynthMsgSendStretCallExpr - CurMethodDef is null");
3229 FunLocStart = CurMethodDef->getLocStart();
3232 InsertText(FunLocStart, str);
3235 // AST for __Stretn(receiver, args).s;
3236 IdentifierInfo *ID = &Context->Idents.get(name);
3237 FunctionDecl *FD = FunctionDecl::Create(*Context, TUDecl, SourceLocation(),
3238 SourceLocation(), ID, castType,
3239 nullptr, SC_Extern, false, false);
3240 DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, castType, VK_RValue,
3242 CallExpr *STCE = new (Context) CallExpr(*Context, DRE, MsgExprs,
3243 castType, VK_LValue, SourceLocation());
3245 FieldDecl *FieldD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
3247 &Context->Idents.get("s"),
3248 returnType, nullptr,
3249 /*BitWidth=*/nullptr,
3250 /*Mutable=*/true, ICIS_NoInit);
3251 MemberExpr *ME = new (Context) MemberExpr(STCE, false, FieldD, SourceLocation(),
3252 FieldD->getType(), VK_LValue,
3258 Stmt *RewriteModernObjC::SynthMessageExpr(ObjCMessageExpr *Exp,
3259 SourceLocation StartLoc,
3260 SourceLocation EndLoc) {
3261 if (!SelGetUidFunctionDecl)
3262 SynthSelGetUidFunctionDecl();
3263 if (!MsgSendFunctionDecl)
3264 SynthMsgSendFunctionDecl();
3265 if (!MsgSendSuperFunctionDecl)
3266 SynthMsgSendSuperFunctionDecl();
3267 if (!MsgSendStretFunctionDecl)
3268 SynthMsgSendStretFunctionDecl();
3269 if (!MsgSendSuperStretFunctionDecl)
3270 SynthMsgSendSuperStretFunctionDecl();
3271 if (!MsgSendFpretFunctionDecl)
3272 SynthMsgSendFpretFunctionDecl();
3273 if (!GetClassFunctionDecl)
3274 SynthGetClassFunctionDecl();
3275 if (!GetSuperClassFunctionDecl)
3276 SynthGetSuperClassFunctionDecl();
3277 if (!GetMetaClassFunctionDecl)
3278 SynthGetMetaClassFunctionDecl();
3280 // default to objc_msgSend().
3281 FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl;
3282 // May need to use objc_msgSend_stret() as well.
3283 FunctionDecl *MsgSendStretFlavor = nullptr;
3284 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
3285 QualType resultType = mDecl->getReturnType();
3286 if (resultType->isRecordType())
3287 MsgSendStretFlavor = MsgSendStretFunctionDecl;
3288 else if (resultType->isRealFloatingType())
3289 MsgSendFlavor = MsgSendFpretFunctionDecl;
3292 // Synthesize a call to objc_msgSend().
3293 SmallVector<Expr*, 8> MsgExprs;
3294 switch (Exp->getReceiverKind()) {
3295 case ObjCMessageExpr::SuperClass: {
3296 MsgSendFlavor = MsgSendSuperFunctionDecl;
3297 if (MsgSendStretFlavor)
3298 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
3299 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
3301 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
3303 SmallVector<Expr*, 4> InitExprs;
3305 // set the receiver to self, the first argument to all methods.
3306 InitExprs.push_back(
3307 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3309 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
3311 Context->getObjCIdType(),
3314 ); // set the 'receiver'.
3316 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3317 SmallVector<Expr*, 8> ClsExprs;
3318 ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName()));
3319 // (Class)objc_getClass("CurrentClass")
3320 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl,
3326 ClsExprs.push_back(Cls);
3327 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
3328 &ClsExprs[0], ClsExprs.size(),
3331 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3332 // To turn off a warning, type-cast to 'id'
3333 InitExprs.push_back( // set 'super class', using class_getSuperclass().
3334 NoTypeInfoCStyleCastExpr(Context,
3335 Context->getObjCIdType(),
3337 // struct __rw_objc_super
3338 QualType superType = getSuperStructType();
3341 if (LangOpts.MicrosoftExt) {
3342 SynthSuperConstructorFunctionDecl();
3343 // Simulate a constructor call...
3344 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl,
3345 false, superType, VK_LValue,
3347 SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs,
3348 superType, VK_LValue,
3350 // The code for super is a little tricky to prevent collision with
3351 // the structure definition in the header. The rewriter has it's own
3352 // internal definition (__rw_objc_super) that is uses. This is why
3353 // we need the cast below. For example:
3354 // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
3356 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
3357 Context->getPointerType(SuperRep->getType()),
3358 VK_RValue, OK_Ordinary,
3360 SuperRep = NoTypeInfoCStyleCastExpr(Context,
3361 Context->getPointerType(superType),
3362 CK_BitCast, SuperRep);
3364 // (struct __rw_objc_super) { <exprs from above> }
3366 new (Context) InitListExpr(*Context, SourceLocation(), InitExprs,
3368 TypeSourceInfo *superTInfo
3369 = Context->getTrivialTypeSourceInfo(superType);
3370 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
3371 superType, VK_LValue,
3373 // struct __rw_objc_super *
3374 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
3375 Context->getPointerType(SuperRep->getType()),
3376 VK_RValue, OK_Ordinary,
3379 MsgExprs.push_back(SuperRep);
3383 case ObjCMessageExpr::Class: {
3384 SmallVector<Expr*, 8> ClsExprs;
3385 ObjCInterfaceDecl *Class
3386 = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface();
3387 IdentifierInfo *clsName = Class->getIdentifier();
3388 ClsExprs.push_back(getStringLiteral(clsName->getName()));
3389 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
3393 CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context,
3394 Context->getObjCIdType(),
3396 MsgExprs.push_back(ArgExpr);
3400 case ObjCMessageExpr::SuperInstance:{
3401 MsgSendFlavor = MsgSendSuperFunctionDecl;
3402 if (MsgSendStretFlavor)
3403 MsgSendStretFlavor = MsgSendSuperStretFunctionDecl;
3404 assert(MsgSendFlavor && "MsgSendFlavor is NULL!");
3405 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
3406 SmallVector<Expr*, 4> InitExprs;
3408 InitExprs.push_back(
3409 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3411 new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(),
3413 Context->getObjCIdType(),
3414 VK_RValue, SourceLocation()))
3415 ); // set the 'receiver'.
3417 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3418 SmallVector<Expr*, 8> ClsExprs;
3419 ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName()));
3420 // (Class)objc_getClass("CurrentClass")
3421 CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl,
3426 ClsExprs.push_back(Cls);
3427 Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl,
3428 &ClsExprs[0], ClsExprs.size(),
3431 // (id)class_getSuperclass((Class)objc_getClass("CurrentClass"))
3432 // To turn off a warning, type-cast to 'id'
3433 InitExprs.push_back(
3434 // set 'super class', using class_getSuperclass().
3435 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3437 // struct __rw_objc_super
3438 QualType superType = getSuperStructType();
3441 if (LangOpts.MicrosoftExt) {
3442 SynthSuperConstructorFunctionDecl();
3443 // Simulate a constructor call...
3444 DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperConstructorFunctionDecl,
3445 false, superType, VK_LValue,
3447 SuperRep = new (Context) CallExpr(*Context, DRE, InitExprs,
3448 superType, VK_LValue, SourceLocation());
3449 // The code for super is a little tricky to prevent collision with
3450 // the structure definition in the header. The rewriter has it's own
3451 // internal definition (__rw_objc_super) that is uses. This is why
3452 // we need the cast below. For example:
3453 // (struct __rw_objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER"))
3455 SuperRep = new (Context) UnaryOperator(SuperRep, UO_AddrOf,
3456 Context->getPointerType(SuperRep->getType()),
3457 VK_RValue, OK_Ordinary,
3459 SuperRep = NoTypeInfoCStyleCastExpr(Context,
3460 Context->getPointerType(superType),
3461 CK_BitCast, SuperRep);
3463 // (struct __rw_objc_super) { <exprs from above> }
3465 new (Context) InitListExpr(*Context, SourceLocation(), InitExprs,
3467 TypeSourceInfo *superTInfo
3468 = Context->getTrivialTypeSourceInfo(superType);
3469 SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo,
3470 superType, VK_RValue, ILE,
3473 MsgExprs.push_back(SuperRep);
3477 case ObjCMessageExpr::Instance: {
3478 // Remove all type-casts because it may contain objc-style types; e.g.
3480 Expr *recExpr = Exp->getInstanceReceiver();
3481 while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr))
3482 recExpr = CE->getSubExpr();
3483 CastKind CK = recExpr->getType()->isObjCObjectPointerType()
3484 ? CK_BitCast : recExpr->getType()->isBlockPointerType()
3485 ? CK_BlockPointerToObjCPointerCast
3486 : CK_CPointerToObjCPointerCast;
3488 recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3490 MsgExprs.push_back(recExpr);
3495 // Create a call to sel_registerName("selName"), it will be the 2nd argument.
3496 SmallVector<Expr*, 8> SelExprs;
3497 SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString()));
3498 CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl,
3499 &SelExprs[0], SelExprs.size(),
3502 MsgExprs.push_back(SelExp);
3504 // Now push any user supplied arguments.
3505 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
3506 Expr *userExpr = Exp->getArg(i);
3507 // Make all implicit casts explicit...ICE comes in handy:-)
3508 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) {
3509 // Reuse the ICE type, it is exactly what the doctor ordered.
3510 QualType type = ICE->getType();
3511 if (needToScanForQualifiers(type))
3512 type = Context->getObjCIdType();
3513 // Make sure we convert "type (^)(...)" to "type (*)(...)".
3514 (void)convertBlockPointerToFunctionPointer(type);
3515 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
3517 if (SubExpr->getType()->isIntegralType(*Context) &&
3518 type->isBooleanType()) {
3519 CK = CK_IntegralToBoolean;
3520 } else if (type->isObjCObjectPointerType()) {
3521 if (SubExpr->getType()->isBlockPointerType()) {
3522 CK = CK_BlockPointerToObjCPointerCast;
3523 } else if (SubExpr->getType()->isPointerType()) {
3524 CK = CK_CPointerToObjCPointerCast;
3532 userExpr = NoTypeInfoCStyleCastExpr(Context, type, CK, userExpr);
3534 // Make id<P...> cast into an 'id' cast.
3535 else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) {
3536 if (CE->getType()->isObjCQualifiedIdType()) {
3537 while ((CE = dyn_cast<CStyleCastExpr>(userExpr)))
3538 userExpr = CE->getSubExpr();
3540 if (userExpr->getType()->isIntegralType(*Context)) {
3541 CK = CK_IntegralToPointer;
3542 } else if (userExpr->getType()->isBlockPointerType()) {
3543 CK = CK_BlockPointerToObjCPointerCast;
3544 } else if (userExpr->getType()->isPointerType()) {
3545 CK = CK_CPointerToObjCPointerCast;
3549 userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3553 MsgExprs.push_back(userExpr);
3554 // We've transferred the ownership to MsgExprs. For now, we *don't* null
3555 // out the argument in the original expression (since we aren't deleting
3556 // the ObjCMessageExpr). See RewritePropertyOrImplicitSetter() usage for more info.
3557 //Exp->setArg(i, 0);
3559 // Generate the funky cast.
3561 SmallVector<QualType, 8> ArgTypes;
3562 QualType returnType;
3564 // Push 'id' and 'SEL', the 2 implicit arguments.
3565 if (MsgSendFlavor == MsgSendSuperFunctionDecl)
3566 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
3568 ArgTypes.push_back(Context->getObjCIdType());
3569 ArgTypes.push_back(Context->getObjCSelType());
3570 if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) {
3571 // Push any user argument types.
3572 for (const auto *PI : OMD->params()) {
3573 QualType t = PI->getType()->isObjCQualifiedIdType()
3574 ? Context->getObjCIdType()
3576 // Make sure we convert "t (^)(...)" to "t (*)(...)".
3577 (void)convertBlockPointerToFunctionPointer(t);
3578 ArgTypes.push_back(t);
3580 returnType = Exp->getType();
3581 convertToUnqualifiedObjCType(returnType);
3582 (void)convertBlockPointerToFunctionPointer(returnType);
3584 returnType = Context->getObjCIdType();
3586 // Get the type, we will need to reference it in a couple spots.
3587 QualType msgSendType = MsgSendFlavor->getType();
3589 // Create a reference to the objc_msgSend() declaration.
3590 DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, false, msgSendType,
3591 VK_LValue, SourceLocation());
3593 // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid).
3594 // If we don't do this cast, we get the following bizarre warning/note:
3595 // xx.m:13: warning: function called through a non-compatible type
3596 // xx.m:13: note: if this code is reached, the program will abort
3597 cast = NoTypeInfoCStyleCastExpr(Context,
3598 Context->getPointerType(Context->VoidTy),
3601 // Now do the "normal" pointer to function cast.
3602 // If we don't have a method decl, force a variadic cast.
3603 const ObjCMethodDecl *MD = Exp->getMethodDecl();
3605 getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true);
3606 castType = Context->getPointerType(castType);
3607 cast = NoTypeInfoCStyleCastExpr(Context, castType, CK_BitCast,
3610 // Don't forget the parens to enforce the proper binding.
3611 ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast);
3613 const FunctionType *FT = msgSendType->getAs<FunctionType>();
3614 CallExpr *CE = new (Context)
3615 CallExpr(*Context, PE, MsgExprs, FT->getReturnType(), VK_RValue, EndLoc);
3616 Stmt *ReplacingStmt = CE;
3617 if (MsgSendStretFlavor) {
3618 // We have the method which returns a struct/union. Must also generate
3619 // call to objc_msgSend_stret and hang both varieties on a conditional
3620 // expression which dictate which one to envoke depending on size of
3621 // method's return type.
3623 Expr *STCE = SynthMsgSendStretCallExpr(MsgSendStretFlavor,
3626 Exp->getMethodDecl());
3627 ReplacingStmt = STCE;
3629 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3630 return ReplacingStmt;
3633 Stmt *RewriteModernObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) {
3634 Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(),
3637 // Now do the actual rewrite.
3638 ReplaceStmt(Exp, ReplacingStmt);
3640 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3641 return ReplacingStmt;
3644 // typedef struct objc_object Protocol;
3645 QualType RewriteModernObjC::getProtocolType() {
3646 if (!ProtocolTypeDecl) {
3647 TypeSourceInfo *TInfo
3648 = Context->getTrivialTypeSourceInfo(Context->getObjCIdType());
3649 ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl,
3650 SourceLocation(), SourceLocation(),
3651 &Context->Idents.get("Protocol"),
3654 return Context->getTypeDeclType(ProtocolTypeDecl);
3657 /// RewriteObjCProtocolExpr - Rewrite a protocol expression into
3658 /// a synthesized/forward data reference (to the protocol's metadata).
3659 /// The forward references (and metadata) are generated in
3660 /// RewriteModernObjC::HandleTranslationUnit().
3661 Stmt *RewriteModernObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) {
3662 std::string Name = "_OBJC_PROTOCOL_REFERENCE_$_" +
3663 Exp->getProtocol()->getNameAsString();
3664 IdentifierInfo *ID = &Context->Idents.get(Name);
3665 VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
3666 SourceLocation(), ID, getProtocolType(),
3667 nullptr, SC_Extern);
3668 DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, false, getProtocolType(),
3669 VK_LValue, SourceLocation());
3670 CastExpr *castExpr =
3671 NoTypeInfoCStyleCastExpr(
3672 Context, Context->getPointerType(DRE->getType()), CK_BitCast, DRE);
3673 ReplaceStmt(Exp, castExpr);
3674 ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl());
3675 // delete Exp; leak for now, see RewritePropertyOrImplicitSetter() usage for more info.
3680 bool RewriteModernObjC::BufferContainsPPDirectives(const char *startBuf,
3681 const char *endBuf) {
3682 while (startBuf < endBuf) {
3683 if (*startBuf == '#') {
3685 for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf)
3687 if (!strncmp(startBuf, "if", strlen("if")) ||
3688 !strncmp(startBuf, "ifdef", strlen("ifdef")) ||
3689 !strncmp(startBuf, "ifndef", strlen("ifndef")) ||
3690 !strncmp(startBuf, "define", strlen("define")) ||
3691 !strncmp(startBuf, "undef", strlen("undef")) ||
3692 !strncmp(startBuf, "else", strlen("else")) ||
3693 !strncmp(startBuf, "elif", strlen("elif")) ||
3694 !strncmp(startBuf, "endif", strlen("endif")) ||
3695 !strncmp(startBuf, "pragma", strlen("pragma")) ||
3696 !strncmp(startBuf, "include", strlen("include")) ||
3697 !strncmp(startBuf, "import", strlen("import")) ||
3698 !strncmp(startBuf, "include_next", strlen("include_next")))
3706 /// IsTagDefinedInsideClass - This routine checks that a named tagged type
3707 /// is defined inside an objective-c class. If so, it returns true.
3708 bool RewriteModernObjC::IsTagDefinedInsideClass(ObjCContainerDecl *IDecl,
3710 bool &IsNamedDefinition) {
3713 SourceLocation TagLocation;
3714 if (RecordDecl *RD = dyn_cast<RecordDecl>(Tag)) {
3715 RD = RD->getDefinition();
3716 if (!RD || !RD->getDeclName().getAsIdentifierInfo())
3718 IsNamedDefinition = true;
3719 TagLocation = RD->getLocation();
3720 return Context->getSourceManager().isBeforeInTranslationUnit(
3721 IDecl->getLocation(), TagLocation);
3723 if (EnumDecl *ED = dyn_cast<EnumDecl>(Tag)) {
3724 if (!ED || !ED->getDeclName().getAsIdentifierInfo())
3726 IsNamedDefinition = true;
3727 TagLocation = ED->getLocation();
3728 return Context->getSourceManager().isBeforeInTranslationUnit(
3729 IDecl->getLocation(), TagLocation);
3735 /// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer.
3736 /// It handles elaborated types, as well as enum types in the process.
3737 bool RewriteModernObjC::RewriteObjCFieldDeclType(QualType &Type,
3738 std::string &Result) {
3739 if (isa<TypedefType>(Type)) {
3744 if (Type->isArrayType()) {
3745 QualType ElemTy = Context->getBaseElementType(Type);
3746 return RewriteObjCFieldDeclType(ElemTy, Result);
3748 else if (Type->isRecordType()) {
3749 RecordDecl *RD = Type->getAs<RecordType>()->getDecl();
3750 if (RD->isCompleteDefinition()) {
3752 Result += "\n\tstruct ";
3753 else if (RD->isUnion())
3754 Result += "\n\tunion ";
3756 assert(false && "class not allowed as an ivar type");
3758 Result += RD->getName();
3759 if (GlobalDefinedTags.count(RD)) {
3760 // struct/union is defined globally, use it.
3765 for (auto *FD : RD->fields())
3766 RewriteObjCFieldDecl(FD, Result);
3771 else if (Type->isEnumeralType()) {
3772 EnumDecl *ED = Type->getAs<EnumType>()->getDecl();
3773 if (ED->isCompleteDefinition()) {
3774 Result += "\n\tenum ";
3775 Result += ED->getName();
3776 if (GlobalDefinedTags.count(ED)) {
3777 // Enum is globall defined, use it.
3783 for (const auto *EC : ED->enumerators()) {
3784 Result += "\t"; Result += EC->getName(); Result += " = ";
3785 llvm::APSInt Val = EC->getInitVal();
3786 Result += Val.toString(10);
3795 convertObjCTypeToCStyleType(Type);
3800 /// RewriteObjCFieldDecl - This routine rewrites a field into the buffer.
3801 /// It handles elaborated types, as well as enum types in the process.
3802 void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl,
3803 std::string &Result) {
3804 QualType Type = fieldDecl->getType();
3805 std::string Name = fieldDecl->getNameAsString();
3807 bool EleboratedType = RewriteObjCFieldDeclType(Type, Result);
3808 if (!EleboratedType)
3809 Type.getAsStringInternal(Name, Context->getPrintingPolicy());
3811 if (fieldDecl->isBitField()) {
3812 Result += " : "; Result += utostr(fieldDecl->getBitWidthValue(*Context));
3814 else if (EleboratedType && Type->isArrayType()) {
3815 const ArrayType *AT = Context->getAsArrayType(Type);
3817 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
3819 llvm::APInt Dim = CAT->getSize();
3820 Result += utostr(Dim.getZExtValue());
3823 AT = Context->getAsArrayType(AT->getElementType());
3830 /// RewriteLocallyDefinedNamedAggregates - This routine rewrites locally defined
3831 /// named aggregate types into the input buffer.
3832 void RewriteModernObjC::RewriteLocallyDefinedNamedAggregates(FieldDecl *fieldDecl,
3833 std::string &Result) {
3834 QualType Type = fieldDecl->getType();
3835 if (isa<TypedefType>(Type))
3837 if (Type->isArrayType())
3838 Type = Context->getBaseElementType(Type);
3839 ObjCContainerDecl *IDecl =
3840 dyn_cast<ObjCContainerDecl>(fieldDecl->getDeclContext());
3842 TagDecl *TD = nullptr;
3843 if (Type->isRecordType()) {
3844 TD = Type->getAs<RecordType>()->getDecl();
3846 else if (Type->isEnumeralType()) {
3847 TD = Type->getAs<EnumType>()->getDecl();
3851 if (GlobalDefinedTags.count(TD))
3854 bool IsNamedDefinition = false;
3855 if (IsTagDefinedInsideClass(IDecl, TD, IsNamedDefinition)) {
3856 RewriteObjCFieldDeclType(Type, Result);
3859 if (IsNamedDefinition)
3860 GlobalDefinedTags.insert(TD);
3865 unsigned RewriteModernObjC::ObjCIvarBitfieldGroupNo(ObjCIvarDecl *IV) {
3866 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3867 if (ObjCInterefaceHasBitfieldGroups.count(CDecl)) {
3868 return IvarGroupNumber[IV];
3870 unsigned GroupNo = 0;
3871 SmallVector<const ObjCIvarDecl *, 8> IVars;
3872 for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3873 IVD; IVD = IVD->getNextIvar())
3874 IVars.push_back(IVD);
3876 for (unsigned i = 0, e = IVars.size(); i < e; i++)
3877 if (IVars[i]->isBitField()) {
3878 IvarGroupNumber[IVars[i++]] = ++GroupNo;
3879 while (i < e && IVars[i]->isBitField())
3880 IvarGroupNumber[IVars[i++]] = GroupNo;
3885 ObjCInterefaceHasBitfieldGroups.insert(CDecl);
3886 return IvarGroupNumber[IV];
3889 QualType RewriteModernObjC::SynthesizeBitfieldGroupStructType(
3891 SmallVectorImpl<ObjCIvarDecl *> &IVars) {
3892 std::string StructTagName;
3893 ObjCIvarBitfieldGroupType(IV, StructTagName);
3894 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct,
3895 Context->getTranslationUnitDecl(),
3896 SourceLocation(), SourceLocation(),
3897 &Context->Idents.get(StructTagName));
3898 for (unsigned i=0, e = IVars.size(); i < e; i++) {
3899 ObjCIvarDecl *Ivar = IVars[i];
3900 RD->addDecl(FieldDecl::Create(*Context, RD, SourceLocation(), SourceLocation(),
3901 &Context->Idents.get(Ivar->getName()),
3903 nullptr, /*Expr *BW */Ivar->getBitWidth(),
3904 false, ICIS_NoInit));
3906 RD->completeDefinition();
3907 return Context->getTagDeclType(RD);
3910 QualType RewriteModernObjC::GetGroupRecordTypeForObjCIvarBitfield(ObjCIvarDecl *IV) {
3911 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3912 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV);
3913 std::pair<const ObjCInterfaceDecl*, unsigned> tuple = std::make_pair(CDecl, GroupNo);
3914 if (GroupRecordType.count(tuple))
3915 return GroupRecordType[tuple];
3917 SmallVector<ObjCIvarDecl *, 8> IVars;
3918 for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3919 IVD; IVD = IVD->getNextIvar()) {
3920 if (IVD->isBitField())
3921 IVars.push_back(const_cast<ObjCIvarDecl *>(IVD));
3923 if (!IVars.empty()) {
3924 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]);
3925 // Generate the struct type for this group of bitfield ivars.
3926 GroupRecordType[std::make_pair(CDecl, GroupNo)] =
3927 SynthesizeBitfieldGroupStructType(IVars[0], IVars);
3932 if (!IVars.empty()) {
3934 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IVars[0]);
3935 GroupRecordType[std::make_pair(CDecl, GroupNo)] =
3936 SynthesizeBitfieldGroupStructType(IVars[0], IVars);
3938 QualType RetQT = GroupRecordType[tuple];
3939 assert(!RetQT.isNull() && "GetGroupRecordTypeForObjCIvarBitfield struct type is NULL");
3944 /// ObjCIvarBitfieldGroupDecl - Names field decl. for ivar bitfield group.
3945 /// Name would be: classname__GRBF_n where n is the group number for this ivar.
3946 void RewriteModernObjC::ObjCIvarBitfieldGroupDecl(ObjCIvarDecl *IV,
3947 std::string &Result) {
3948 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3949 Result += CDecl->getName();
3950 Result += "__GRBF_";
3951 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV);
3952 Result += utostr(GroupNo);
3956 /// ObjCIvarBitfieldGroupType - Names struct type for ivar bitfield group.
3957 /// Name of the struct would be: classname__T_n where n is the group number for
3959 void RewriteModernObjC::ObjCIvarBitfieldGroupType(ObjCIvarDecl *IV,
3960 std::string &Result) {
3961 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3962 Result += CDecl->getName();
3964 unsigned GroupNo = ObjCIvarBitfieldGroupNo(IV);
3965 Result += utostr(GroupNo);
3969 /// ObjCIvarBitfieldGroupOffset - Names symbol for ivar bitfield group field offset.
3970 /// Name would be: OBJC_IVAR_$_classname__GRBF_n where n is the group number for
3972 void RewriteModernObjC::ObjCIvarBitfieldGroupOffset(ObjCIvarDecl *IV,
3973 std::string &Result) {
3974 Result += "OBJC_IVAR_$_";
3975 ObjCIvarBitfieldGroupDecl(IV, Result);
3978 #define SKIP_BITFIELDS(IX, ENDIX, VEC) { \
3979 while ((IX < ENDIX) && VEC[IX]->isBitField()) \
3985 /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to
3986 /// an objective-c class with ivars.
3987 void RewriteModernObjC::RewriteObjCInternalStruct(ObjCInterfaceDecl *CDecl,
3988 std::string &Result) {
3989 assert(CDecl && "Class missing in SynthesizeObjCInternalStruct");
3990 assert(CDecl->getName() != "" &&
3991 "Name missing in SynthesizeObjCInternalStruct");
3992 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
3993 SmallVector<ObjCIvarDecl *, 8> IVars;
3994 for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3995 IVD; IVD = IVD->getNextIvar())
3996 IVars.push_back(IVD);
3998 SourceLocation LocStart = CDecl->getLocStart();
3999 SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc();
4001 const char *startBuf = SM->getCharacterData(LocStart);
4002 const char *endBuf = SM->getCharacterData(LocEnd);
4004 // If no ivars and no root or if its root, directly or indirectly,
4005 // have no ivars (thus not synthesized) then no need to synthesize this class.
4006 if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) &&
4007 (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
4008 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
4009 ReplaceText(LocStart, endBuf-startBuf, Result);
4013 // Insert named struct/union definitions inside class to
4014 // outer scope. This follows semantics of locally defined
4015 // struct/unions in objective-c classes.
4016 for (unsigned i = 0, e = IVars.size(); i < e; i++)
4017 RewriteLocallyDefinedNamedAggregates(IVars[i], Result);
4019 // Insert named structs which are syntheized to group ivar bitfields
4020 // to outer scope as well.
4021 for (unsigned i = 0, e = IVars.size(); i < e; i++)
4022 if (IVars[i]->isBitField()) {
4023 ObjCIvarDecl *IV = IVars[i];
4024 QualType QT = GetGroupRecordTypeForObjCIvarBitfield(IV);
4025 RewriteObjCFieldDeclType(QT, Result);
4027 // skip over ivar bitfields in this group.
4028 SKIP_BITFIELDS(i , e, IVars);
4031 Result += "\nstruct ";
4032 Result += CDecl->getNameAsString();
4033 Result += "_IMPL {\n";
4035 if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) {
4036 Result += "\tstruct "; Result += RCDecl->getNameAsString();
4037 Result += "_IMPL "; Result += RCDecl->getNameAsString();
4038 Result += "_IVARS;\n";
4041 for (unsigned i = 0, e = IVars.size(); i < e; i++) {
4042 if (IVars[i]->isBitField()) {
4043 ObjCIvarDecl *IV = IVars[i];
4044 Result += "\tstruct ";
4045 ObjCIvarBitfieldGroupType(IV, Result); Result += " ";
4046 ObjCIvarBitfieldGroupDecl(IV, Result); Result += ";\n";
4047 // skip over ivar bitfields in this group.
4048 SKIP_BITFIELDS(i , e, IVars);
4051 RewriteObjCFieldDecl(IVars[i], Result);
4055 endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
4056 ReplaceText(LocStart, endBuf-startBuf, Result);
4057 // Mark this struct as having been generated.
4058 if (!ObjCSynthesizedStructs.insert(CDecl))
4059 llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct");
4062 /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which
4063 /// have been referenced in an ivar access expression.
4064 void RewriteModernObjC::RewriteIvarOffsetSymbols(ObjCInterfaceDecl *CDecl,
4065 std::string &Result) {
4066 // write out ivar offset symbols which have been referenced in an ivar
4067 // access expression.
4068 llvm::SmallPtrSet<ObjCIvarDecl *, 8> Ivars = ReferencedIvars[CDecl];
4072 llvm::DenseSet<std::pair<const ObjCInterfaceDecl*, unsigned> > GroupSymbolOutput;
4073 for (llvm::SmallPtrSet<ObjCIvarDecl *, 8>::iterator i = Ivars.begin(),
4074 e = Ivars.end(); i != e; i++) {
4075 ObjCIvarDecl *IvarDecl = (*i);
4076 const ObjCInterfaceDecl *IDecl = IvarDecl->getContainingInterface();
4077 unsigned GroupNo = 0;
4078 if (IvarDecl->isBitField()) {
4079 GroupNo = ObjCIvarBitfieldGroupNo(IvarDecl);
4080 if (GroupSymbolOutput.count(std::make_pair(IDecl, GroupNo)))
4084 if (LangOpts.MicrosoftExt)
4085 Result += "__declspec(allocate(\".objc_ivar$B\")) ";
4086 Result += "extern \"C\" ";
4087 if (LangOpts.MicrosoftExt &&
4088 IvarDecl->getAccessControl() != ObjCIvarDecl::Private &&
4089 IvarDecl->getAccessControl() != ObjCIvarDecl::Package)
4090 Result += "__declspec(dllimport) ";
4092 Result += "unsigned long ";
4093 if (IvarDecl->isBitField()) {
4094 ObjCIvarBitfieldGroupOffset(IvarDecl, Result);
4095 GroupSymbolOutput.insert(std::make_pair(IDecl, GroupNo));
4098 WriteInternalIvarName(CDecl, IvarDecl, Result);
4103 //===----------------------------------------------------------------------===//
4104 // Meta Data Emission
4105 //===----------------------------------------------------------------------===//
4108 /// RewriteImplementations - This routine rewrites all method implementations
4109 /// and emits meta-data.
4111 void RewriteModernObjC::RewriteImplementations() {
4112 int ClsDefCount = ClassImplementation.size();
4113 int CatDefCount = CategoryImplementation.size();
4115 // Rewrite implemented methods
4116 for (int i = 0; i < ClsDefCount; i++) {
4117 ObjCImplementationDecl *OIMP = ClassImplementation[i];
4118 ObjCInterfaceDecl *CDecl = OIMP->getClassInterface();
4119 if (CDecl->isImplicitInterfaceDecl())
4121 "Legacy implicit interface rewriting not supported in moder abi");
4122 RewriteImplementationDecl(OIMP);
4125 for (int i = 0; i < CatDefCount; i++) {
4126 ObjCCategoryImplDecl *CIMP = CategoryImplementation[i];
4127 ObjCInterfaceDecl *CDecl = CIMP->getClassInterface();
4128 if (CDecl->isImplicitInterfaceDecl())
4130 "Legacy implicit interface rewriting not supported in moder abi");
4131 RewriteImplementationDecl(CIMP);
4135 void RewriteModernObjC::RewriteByRefString(std::string &ResultStr,
4136 const std::string &Name,
4137 ValueDecl *VD, bool def) {
4138 assert(BlockByRefDeclNo.count(VD) &&
4139 "RewriteByRefString: ByRef decl missing");
4141 ResultStr += "struct ";
4142 ResultStr += "__Block_byref_" + Name +
4143 "_" + utostr(BlockByRefDeclNo[VD]) ;
4146 static bool HasLocalVariableExternalStorage(ValueDecl *VD) {
4147 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
4148 return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage());
4152 std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
4155 const FunctionType *AFT = CE->getFunctionType();
4156 QualType RT = AFT->getReturnType();
4157 std::string StructRef = "struct " + Tag;
4158 SourceLocation BlockLoc = CE->getExprLoc();
4160 ConvertSourceLocationToLineDirective(BlockLoc, S);
4162 S += "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" +
4163 funcName.str() + "_block_func_" + utostr(i);
4165 BlockDecl *BD = CE->getBlockDecl();
4167 if (isa<FunctionNoProtoType>(AFT)) {
4168 // No user-supplied arguments. Still need to pass in a pointer to the
4169 // block (to reference imported block decl refs).
4170 S += "(" + StructRef + " *__cself)";
4171 } else if (BD->param_empty()) {
4172 S += "(" + StructRef + " *__cself)";
4174 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
4175 assert(FT && "SynthesizeBlockFunc: No function proto");
4177 // first add the implicit argument.
4178 S += StructRef + " *__cself, ";
4179 std::string ParamStr;
4180 for (BlockDecl::param_iterator AI = BD->param_begin(),
4181 E = BD->param_end(); AI != E; ++AI) {
4182 if (AI != BD->param_begin()) S += ", ";
4183 ParamStr = (*AI)->getNameAsString();
4184 QualType QT = (*AI)->getType();
4185 (void)convertBlockPointerToFunctionPointer(QT);
4186 QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
4189 if (FT->isVariadic()) {
4190 if (!BD->param_empty()) S += ", ";
4197 // Create local declarations to avoid rewriting all closure decl ref exprs.
4198 // First, emit a declaration for all "by ref" decls.
4199 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
4200 E = BlockByRefDecls.end(); I != E; ++I) {
4202 std::string Name = (*I)->getNameAsString();
4203 std::string TypeString;
4204 RewriteByRefString(TypeString, Name, (*I));
4206 Name = TypeString + Name;
4207 S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n";
4209 // Next, emit a declaration for all "by copy" declarations.
4210 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
4211 E = BlockByCopyDecls.end(); I != E; ++I) {
4213 // Handle nested closure invocation. For example:
4215 // void (^myImportedClosure)(void);
4216 // myImportedClosure = ^(void) { setGlobalInt(x + y); };
4218 // void (^anotherClosure)(void);
4219 // anotherClosure = ^(void) {
4220 // myImportedClosure(); // import and invoke the closure
4223 if (isTopLevelBlockPointerType((*I)->getType())) {
4224 RewriteBlockPointerTypeVariable(S, (*I));
4226 RewriteBlockPointerType(S, (*I)->getType());
4228 S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n";
4231 std::string Name = (*I)->getNameAsString();
4232 QualType QT = (*I)->getType();
4233 if (HasLocalVariableExternalStorage(*I))
4234 QT = Context->getPointerType(QT);
4235 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
4236 S += Name + " = __cself->" +
4237 (*I)->getNameAsString() + "; // bound by copy\n";
4240 std::string RewrittenStr = RewrittenBlockExprs[CE];
4241 const char *cstr = RewrittenStr.c_str();
4242 while (*cstr++ != '{') ;
4248 std::string RewriteModernObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i,
4251 std::string StructRef = "struct " + Tag;
4252 std::string S = "static void __";
4255 S += "_block_copy_" + utostr(i);
4256 S += "(" + StructRef;
4257 S += "*dst, " + StructRef;
4259 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
4260 E = ImportedBlockDecls.end(); I != E; ++I) {
4261 ValueDecl *VD = (*I);
4262 S += "_Block_object_assign((void*)&dst->";
4263 S += (*I)->getNameAsString();
4264 S += ", (void*)src->";
4265 S += (*I)->getNameAsString();
4266 if (BlockByRefDeclsPtrSet.count((*I)))
4267 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
4268 else if (VD->getType()->isBlockPointerType())
4269 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
4271 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
4275 S += "\nstatic void __";
4277 S += "_block_dispose_" + utostr(i);
4278 S += "(" + StructRef;
4280 for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(),
4281 E = ImportedBlockDecls.end(); I != E; ++I) {
4282 ValueDecl *VD = (*I);
4283 S += "_Block_object_dispose((void*)src->";
4284 S += (*I)->getNameAsString();
4285 if (BlockByRefDeclsPtrSet.count((*I)))
4286 S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);";
4287 else if (VD->getType()->isBlockPointerType())
4288 S += ", " + utostr(BLOCK_FIELD_IS_BLOCK) + "/*BLOCK_FIELD_IS_BLOCK*/);";
4290 S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);";
4296 std::string RewriteModernObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag,
4298 std::string S = "\nstruct " + Tag;
4299 std::string Constructor = " " + Tag;
4301 S += " {\n struct __block_impl impl;\n";
4302 S += " struct " + Desc;
4305 Constructor += "(void *fp, "; // Invoke function pointer.
4306 Constructor += "struct " + Desc; // Descriptor pointer.
4307 Constructor += " *desc";
4309 if (BlockDeclRefs.size()) {
4310 // Output all "by copy" declarations.
4311 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
4312 E = BlockByCopyDecls.end(); I != E; ++I) {
4314 std::string FieldName = (*I)->getNameAsString();
4315 std::string ArgName = "_" + FieldName;
4316 // Handle nested closure invocation. For example:
4318 // void (^myImportedBlock)(void);
4319 // myImportedBlock = ^(void) { setGlobalInt(x + y); };
4321 // void (^anotherBlock)(void);
4322 // anotherBlock = ^(void) {
4323 // myImportedBlock(); // import and invoke the closure
4326 if (isTopLevelBlockPointerType((*I)->getType())) {
4327 S += "struct __block_impl *";
4328 Constructor += ", void *" + ArgName;
4330 QualType QT = (*I)->getType();
4331 if (HasLocalVariableExternalStorage(*I))
4332 QT = Context->getPointerType(QT);
4333 QT.getAsStringInternal(FieldName, Context->getPrintingPolicy());
4334 QT.getAsStringInternal(ArgName, Context->getPrintingPolicy());
4335 Constructor += ", " + ArgName;
4337 S += FieldName + ";\n";
4339 // Output all "by ref" declarations.
4340 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
4341 E = BlockByRefDecls.end(); I != E; ++I) {
4343 std::string FieldName = (*I)->getNameAsString();
4344 std::string ArgName = "_" + FieldName;
4346 std::string TypeString;
4347 RewriteByRefString(TypeString, FieldName, (*I));
4349 FieldName = TypeString + FieldName;
4350 ArgName = TypeString + ArgName;
4351 Constructor += ", " + ArgName;
4353 S += FieldName + "; // by ref\n";
4355 // Finish writing the constructor.
4356 Constructor += ", int flags=0)";
4357 // Initialize all "by copy" arguments.
4358 bool firsTime = true;
4359 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
4360 E = BlockByCopyDecls.end(); I != E; ++I) {
4361 std::string Name = (*I)->getNameAsString();
4363 Constructor += " : ";
4367 Constructor += ", ";
4368 if (isTopLevelBlockPointerType((*I)->getType()))
4369 Constructor += Name + "((struct __block_impl *)_" + Name + ")";
4371 Constructor += Name + "(_" + Name + ")";
4373 // Initialize all "by ref" arguments.
4374 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
4375 E = BlockByRefDecls.end(); I != E; ++I) {
4376 std::string Name = (*I)->getNameAsString();
4378 Constructor += " : ";
4382 Constructor += ", ";
4383 Constructor += Name + "(_" + Name + "->__forwarding)";
4386 Constructor += " {\n";
4388 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
4390 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
4391 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
4393 Constructor += " Desc = desc;\n";
4395 // Finish writing the constructor.
4396 Constructor += ", int flags=0) {\n";
4398 Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n";
4400 Constructor += " impl.isa = &_NSConcreteStackBlock;\n";
4401 Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n";
4402 Constructor += " Desc = desc;\n";
4405 Constructor += "}\n";
4411 std::string RewriteModernObjC::SynthesizeBlockDescriptor(std::string DescTag,
4412 std::string ImplTag, int i,
4415 std::string S = "\nstatic struct " + DescTag;
4417 S += " {\n size_t reserved;\n";
4418 S += " size_t Block_size;\n";
4420 S += " void (*copy)(struct ";
4421 S += ImplTag; S += "*, struct ";
4422 S += ImplTag; S += "*);\n";
4424 S += " void (*dispose)(struct ";
4425 S += ImplTag; S += "*);\n";
4429 S += DescTag + "_DATA = { 0, sizeof(struct ";
4432 S += ", __" + FunName.str() + "_block_copy_" + utostr(i);
4433 S += ", __" + FunName.str() + "_block_dispose_" + utostr(i);
4439 void RewriteModernObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart,
4440 StringRef FunName) {
4441 bool RewriteSC = (GlobalVarDecl &&
4443 GlobalVarDecl->getStorageClass() == SC_Static &&
4444 GlobalVarDecl->getType().getCVRQualifiers());
4446 std::string SC(" void __");
4447 SC += GlobalVarDecl->getNameAsString();
4449 InsertText(FunLocStart, SC);
4452 // Insert closures that were part of the function.
4453 for (unsigned i = 0, count=0; i < Blocks.size(); i++) {
4454 CollectBlockDeclRefInfo(Blocks[i]);
4455 // Need to copy-in the inner copied-in variables not actually used in this
4457 for (int j = 0; j < InnerDeclRefsCount[i]; j++) {
4458 DeclRefExpr *Exp = InnerDeclRefs[count++];
4459 ValueDecl *VD = Exp->getDecl();
4460 BlockDeclRefs.push_back(Exp);
4461 if (!VD->hasAttr<BlocksAttr>()) {
4462 if (!BlockByCopyDeclsPtrSet.count(VD)) {
4463 BlockByCopyDeclsPtrSet.insert(VD);
4464 BlockByCopyDecls.push_back(VD);
4469 if (!BlockByRefDeclsPtrSet.count(VD)) {
4470 BlockByRefDeclsPtrSet.insert(VD);
4471 BlockByRefDecls.push_back(VD);
4474 // imported objects in the inner blocks not used in the outer
4475 // blocks must be copied/disposed in the outer block as well.
4476 if (VD->getType()->isObjCObjectPointerType() ||
4477 VD->getType()->isBlockPointerType())
4478 ImportedBlockDecls.insert(VD);
4481 std::string ImplTag = "__" + FunName.str() + "_block_impl_" + utostr(i);
4482 std::string DescTag = "__" + FunName.str() + "_block_desc_" + utostr(i);
4484 std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag);
4486 InsertText(FunLocStart, CI);
4488 std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag);
4490 InsertText(FunLocStart, CF);
4492 if (ImportedBlockDecls.size()) {
4493 std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag);
4494 InsertText(FunLocStart, HF);
4496 std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName,
4497 ImportedBlockDecls.size() > 0);
4498 InsertText(FunLocStart, BD);
4500 BlockDeclRefs.clear();
4501 BlockByRefDecls.clear();
4502 BlockByRefDeclsPtrSet.clear();
4503 BlockByCopyDecls.clear();
4504 BlockByCopyDeclsPtrSet.clear();
4505 ImportedBlockDecls.clear();
4508 // Must insert any 'const/volatile/static here. Since it has been
4509 // removed as result of rewriting of block literals.
4511 if (GlobalVarDecl->getStorageClass() == SC_Static)
4513 if (GlobalVarDecl->getType().isConstQualified())
4515 if (GlobalVarDecl->getType().isVolatileQualified())
4517 if (GlobalVarDecl->getType().isRestrictQualified())
4519 InsertText(FunLocStart, SC);
4521 if (GlobalConstructionExp) {
4522 // extra fancy dance for global literal expression.
4524 // Always the latest block expression on the block stack.
4525 std::string Tag = "__";
4527 Tag += "_block_impl_";
4528 Tag += utostr(Blocks.size()-1);
4529 std::string globalBuf = "static ";
4530 globalBuf += Tag; globalBuf += " ";
4533 llvm::raw_string_ostream constructorExprBuf(SStr);
4534 GlobalConstructionExp->printPretty(constructorExprBuf, nullptr,
4535 PrintingPolicy(LangOpts));
4536 globalBuf += constructorExprBuf.str();
4538 InsertText(FunLocStart, globalBuf);
4539 GlobalConstructionExp = nullptr;
4543 InnerDeclRefsCount.clear();
4544 InnerDeclRefs.clear();
4545 RewrittenBlockExprs.clear();
4548 void RewriteModernObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) {
4549 SourceLocation FunLocStart =
4550 (!Blocks.empty()) ? getFunctionSourceLocation(*this, FD)
4551 : FD->getTypeSpecStartLoc();
4552 StringRef FuncName = FD->getName();
4554 SynthesizeBlockLiterals(FunLocStart, FuncName);
4557 static void BuildUniqueMethodName(std::string &Name,
4558 ObjCMethodDecl *MD) {
4559 ObjCInterfaceDecl *IFace = MD->getClassInterface();
4560 Name = IFace->getName();
4561 Name += "__" + MD->getSelector().getAsString();
4562 // Convert colons to underscores.
4563 std::string::size_type loc = 0;
4564 while ((loc = Name.find(":", loc)) != std::string::npos)
4565 Name.replace(loc, 1, "_");
4568 void RewriteModernObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) {
4569 //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n");
4570 //SourceLocation FunLocStart = MD->getLocStart();
4571 SourceLocation FunLocStart = MD->getLocStart();
4572 std::string FuncName;
4573 BuildUniqueMethodName(FuncName, MD);
4574 SynthesizeBlockLiterals(FunLocStart, FuncName);
4577 void RewriteModernObjC::GetBlockDeclRefExprs(Stmt *S) {
4578 for (Stmt::child_range CI = S->children(); CI; ++CI)
4580 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI))
4581 GetBlockDeclRefExprs(CBE->getBody());
4583 GetBlockDeclRefExprs(*CI);
4585 // Handle specific things.
4586 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
4587 if (DRE->refersToEnclosingLocal()) {
4588 // FIXME: Handle enums.
4589 if (!isa<FunctionDecl>(DRE->getDecl()))
4590 BlockDeclRefs.push_back(DRE);
4591 if (HasLocalVariableExternalStorage(DRE->getDecl()))
4592 BlockDeclRefs.push_back(DRE);
4599 void RewriteModernObjC::GetInnerBlockDeclRefExprs(Stmt *S,
4600 SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs,
4601 llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) {
4602 for (Stmt::child_range CI = S->children(); CI; ++CI)
4604 if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) {
4605 InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl()));
4606 GetInnerBlockDeclRefExprs(CBE->getBody(),
4611 GetInnerBlockDeclRefExprs(*CI,
4616 // Handle specific things.
4617 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
4618 if (DRE->refersToEnclosingLocal()) {
4619 if (!isa<FunctionDecl>(DRE->getDecl()) &&
4620 !InnerContexts.count(DRE->getDecl()->getDeclContext()))
4621 InnerBlockDeclRefs.push_back(DRE);
4622 if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl()))
4623 if (Var->isFunctionOrMethodVarDecl())
4624 ImportedLocalExternalDecls.insert(Var);
4631 /// convertObjCTypeToCStyleType - This routine converts such objc types
4632 /// as qualified objects, and blocks to their closest c/c++ types that
4633 /// it can. It returns true if input type was modified.
4634 bool RewriteModernObjC::convertObjCTypeToCStyleType(QualType &T) {
4636 convertBlockPointerToFunctionPointer(T);
4637 if (T->isFunctionPointerType()) {
4639 if (const PointerType* PT = T->getAs<PointerType>()) {
4640 PointeeTy = PT->getPointeeType();
4641 if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) {
4642 T = convertFunctionTypeOfBlocks(FT);
4643 T = Context->getPointerType(T);
4648 convertToUnqualifiedObjCType(T);
4652 /// convertFunctionTypeOfBlocks - This routine converts a function type
4653 /// whose result type may be a block pointer or whose argument type(s)
4654 /// might be block pointers to an equivalent function type replacing
4655 /// all block pointers to function pointers.
4656 QualType RewriteModernObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) {
4657 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
4658 // FTP will be null for closures that don't take arguments.
4659 // Generate a funky cast.
4660 SmallVector<QualType, 8> ArgTypes;
4661 QualType Res = FT->getReturnType();
4662 bool modified = convertObjCTypeToCStyleType(Res);
4665 for (auto &I : FTP->param_types()) {
4667 // Make sure we convert "t (^)(...)" to "t (*)(...)".
4668 if (convertObjCTypeToCStyleType(t))
4670 ArgTypes.push_back(t);
4675 FuncType = getSimpleFunctionType(Res, ArgTypes);
4676 else FuncType = QualType(FT, 0);
4680 Stmt *RewriteModernObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) {
4681 // Navigate to relevant type information.
4682 const BlockPointerType *CPT = nullptr;
4684 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) {
4685 CPT = DRE->getType()->getAs<BlockPointerType>();
4686 } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) {
4687 CPT = MExpr->getType()->getAs<BlockPointerType>();
4689 else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) {
4690 return SynthesizeBlockCall(Exp, PRE->getSubExpr());
4692 else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp))
4693 CPT = IEXPR->getType()->getAs<BlockPointerType>();
4694 else if (const ConditionalOperator *CEXPR =
4695 dyn_cast<ConditionalOperator>(BlockExp)) {
4696 Expr *LHSExp = CEXPR->getLHS();
4697 Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp);
4698 Expr *RHSExp = CEXPR->getRHS();
4699 Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp);
4700 Expr *CONDExp = CEXPR->getCond();
4701 ConditionalOperator *CondExpr =
4702 new (Context) ConditionalOperator(CONDExp,
4703 SourceLocation(), cast<Expr>(LHSStmt),
4704 SourceLocation(), cast<Expr>(RHSStmt),
4705 Exp->getType(), VK_RValue, OK_Ordinary);
4707 } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) {
4708 CPT = IRE->getType()->getAs<BlockPointerType>();
4709 } else if (const PseudoObjectExpr *POE
4710 = dyn_cast<PseudoObjectExpr>(BlockExp)) {
4711 CPT = POE->getType()->castAs<BlockPointerType>();
4713 assert(1 && "RewriteBlockClass: Bad type");
4715 assert(CPT && "RewriteBlockClass: Bad type");
4716 const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>();
4717 assert(FT && "RewriteBlockClass: Bad type");
4718 const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT);
4719 // FTP will be null for closures that don't take arguments.
4721 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
4722 SourceLocation(), SourceLocation(),
4723 &Context->Idents.get("__block_impl"));
4724 QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD));
4726 // Generate a funky cast.
4727 SmallVector<QualType, 8> ArgTypes;
4729 // Push the block argument type.
4730 ArgTypes.push_back(PtrBlock);
4732 for (auto &I : FTP->param_types()) {
4734 // Make sure we convert "t (^)(...)" to "t (*)(...)".
4735 if (!convertBlockPointerToFunctionPointer(t))
4736 convertToUnqualifiedObjCType(t);
4737 ArgTypes.push_back(t);
4740 // Now do the pointer to function cast.
4741 QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes);
4743 PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType);
4745 CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock,
4747 const_cast<Expr*>(BlockExp));
4748 // Don't forget the parens to enforce the proper binding.
4749 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
4753 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
4755 &Context->Idents.get("FuncPtr"),
4756 Context->VoidPtrTy, nullptr,
4757 /*BitWidth=*/nullptr, /*Mutable=*/true,
4759 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
4760 FD->getType(), VK_LValue,
4764 CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType,
4766 PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast);
4768 SmallVector<Expr*, 8> BlkExprs;
4769 // Add the implicit argument.
4770 BlkExprs.push_back(BlkCast);
4771 // Add the user arguments.
4772 for (CallExpr::arg_iterator I = Exp->arg_begin(),
4773 E = Exp->arg_end(); I != E; ++I) {
4774 BlkExprs.push_back(*I);
4776 CallExpr *CE = new (Context) CallExpr(*Context, PE, BlkExprs,
4777 Exp->getType(), VK_RValue,
4782 // We need to return the rewritten expression to handle cases where the
4783 // DeclRefExpr is embedded in another expression being rewritten.
4790 // void (^myblock)() = ^() {
4791 // [f test]; // f is a DeclRefExpr embedded in a message (which is being rewritten).
4795 Stmt *RewriteModernObjC::RewriteBlockDeclRefExpr(DeclRefExpr *DeclRefExp) {
4796 // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR
4797 // for each DeclRefExp where BYREFVAR is name of the variable.
4798 ValueDecl *VD = DeclRefExp->getDecl();
4799 bool isArrow = DeclRefExp->refersToEnclosingLocal();
4801 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
4803 &Context->Idents.get("__forwarding"),
4804 Context->VoidPtrTy, nullptr,
4805 /*BitWidth=*/nullptr, /*Mutable=*/true,
4807 MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow,
4808 FD, SourceLocation(),
4809 FD->getType(), VK_LValue,
4812 StringRef Name = VD->getName();
4813 FD = FieldDecl::Create(*Context, nullptr, SourceLocation(), SourceLocation(),
4814 &Context->Idents.get(Name),
4815 Context->VoidPtrTy, nullptr,
4816 /*BitWidth=*/nullptr, /*Mutable=*/true,
4818 ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(),
4819 DeclRefExp->getType(), VK_LValue, OK_Ordinary);
4823 // Need parens to enforce precedence.
4824 ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(),
4825 DeclRefExp->getExprLoc(),
4827 ReplaceStmt(DeclRefExp, PE);
4831 // Rewrites the imported local variable V with external storage
4832 // (static, extern, etc.) as *V
4834 Stmt *RewriteModernObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) {
4835 ValueDecl *VD = DRE->getDecl();
4836 if (VarDecl *Var = dyn_cast<VarDecl>(VD))
4837 if (!ImportedLocalExternalDecls.count(Var))
4839 Expr *Exp = new (Context) UnaryOperator(DRE, UO_Deref, DRE->getType(),
4840 VK_LValue, OK_Ordinary,
4841 DRE->getLocation());
4842 // Need parens to enforce precedence.
4843 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
4845 ReplaceStmt(DRE, PE);
4849 void RewriteModernObjC::RewriteCastExpr(CStyleCastExpr *CE) {
4850 SourceLocation LocStart = CE->getLParenLoc();
4851 SourceLocation LocEnd = CE->getRParenLoc();
4853 // Need to avoid trying to rewrite synthesized casts.
4854 if (LocStart.isInvalid())
4856 // Need to avoid trying to rewrite casts contained in macros.
4857 if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd))
4860 const char *startBuf = SM->getCharacterData(LocStart);
4861 const char *endBuf = SM->getCharacterData(LocEnd);
4862 QualType QT = CE->getType();
4863 const Type* TypePtr = QT->getAs<Type>();
4864 if (isa<TypeOfExprType>(TypePtr)) {
4865 const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr);
4866 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
4867 std::string TypeAsString = "(";
4868 RewriteBlockPointerType(TypeAsString, QT);
4869 TypeAsString += ")";
4870 ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString);
4873 // advance the location to startArgList.
4874 const char *argPtr = startBuf;
4876 while (*argPtr++ && (argPtr < endBuf)) {
4879 // Replace the '^' with '*'.
4880 LocStart = LocStart.getLocWithOffset(argPtr-startBuf);
4881 ReplaceText(LocStart, 1, "*");
4888 void RewriteModernObjC::RewriteImplicitCastObjCExpr(CastExpr *IC) {
4889 CastKind CastKind = IC->getCastKind();
4890 if (CastKind != CK_BlockPointerToObjCPointerCast &&
4891 CastKind != CK_AnyPointerToBlockPointerCast)
4894 QualType QT = IC->getType();
4895 (void)convertBlockPointerToFunctionPointer(QT);
4896 std::string TypeString(QT.getAsString(Context->getPrintingPolicy()));
4897 std::string Str = "(";
4900 InsertText(IC->getSubExpr()->getLocStart(), &Str[0], Str.size());
4905 void RewriteModernObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) {
4906 SourceLocation DeclLoc = FD->getLocation();
4907 unsigned parenCount = 0;
4909 // We have 1 or more arguments that have closure pointers.
4910 const char *startBuf = SM->getCharacterData(DeclLoc);
4911 const char *startArgList = strchr(startBuf, '(');
4913 assert((*startArgList == '(') && "Rewriter fuzzy parser confused");
4916 // advance the location to startArgList.
4917 DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf);
4918 assert((DeclLoc.isValid()) && "Invalid DeclLoc");
4920 const char *argPtr = startArgList;
4922 while (*argPtr++ && parenCount) {
4925 // Replace the '^' with '*'.
4926 DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList);
4927 ReplaceText(DeclLoc, 1, "*");
4940 bool RewriteModernObjC::PointerTypeTakesAnyBlockArguments(QualType QT) {
4941 const FunctionProtoType *FTP;
4942 const PointerType *PT = QT->getAs<PointerType>();
4944 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4946 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4947 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
4948 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4951 for (const auto &I : FTP->param_types())
4952 if (isTopLevelBlockPointerType(I))
4958 bool RewriteModernObjC::PointerTypeTakesAnyObjCQualifiedType(QualType QT) {
4959 const FunctionProtoType *FTP;
4960 const PointerType *PT = QT->getAs<PointerType>();
4962 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4964 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4965 assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type");
4966 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4969 for (const auto &I : FTP->param_types()) {
4970 if (I->isObjCQualifiedIdType())
4972 if (I->isObjCObjectPointerType() &&
4973 I->getPointeeType()->isObjCQualifiedInterfaceType())
4981 void RewriteModernObjC::GetExtentOfArgList(const char *Name, const char *&LParen,
4982 const char *&RParen) {
4983 const char *argPtr = strchr(Name, '(');
4984 assert((*argPtr == '(') && "Rewriter fuzzy parser confused");
4986 LParen = argPtr; // output the start.
4987 argPtr++; // skip past the left paren.
4988 unsigned parenCount = 1;
4990 while (*argPtr && parenCount) {
4992 case '(': parenCount++; break;
4993 case ')': parenCount--; break;
4996 if (parenCount) argPtr++;
4998 assert((*argPtr == ')') && "Rewriter fuzzy parser confused");
4999 RParen = argPtr; // output the end
5002 void RewriteModernObjC::RewriteBlockPointerDecl(NamedDecl *ND) {
5003 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
5004 RewriteBlockPointerFunctionArgs(FD);
5007 // Handle Variables and Typedefs.
5008 SourceLocation DeclLoc = ND->getLocation();
5010 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
5011 DeclT = VD->getType();
5012 else if (TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(ND))
5013 DeclT = TDD->getUnderlyingType();
5014 else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND))
5015 DeclT = FD->getType();
5017 llvm_unreachable("RewriteBlockPointerDecl(): Decl type not yet handled");
5019 const char *startBuf = SM->getCharacterData(DeclLoc);
5020 const char *endBuf = startBuf;
5021 // scan backward (from the decl location) for the end of the previous decl.
5022 while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart)
5024 SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf);
5026 unsigned OrigLength=0;
5027 // *startBuf != '^' if we are dealing with a pointer to function that
5028 // may take block argument types (which will be handled below).
5029 if (*startBuf == '^') {
5030 // Replace the '^' with '*', computing a negative offset.
5035 while (*startBuf != ')') {
5043 if (PointerTypeTakesAnyBlockArguments(DeclT) ||
5044 PointerTypeTakesAnyObjCQualifiedType(DeclT)) {
5045 // Replace the '^' with '*' for arguments.
5046 // Replace id<P> with id/*<>*/
5047 DeclLoc = ND->getLocation();
5048 startBuf = SM->getCharacterData(DeclLoc);
5049 const char *argListBegin, *argListEnd;
5050 GetExtentOfArgList(startBuf, argListBegin, argListEnd);
5051 while (argListBegin < argListEnd) {
5052 if (*argListBegin == '^')
5054 else if (*argListBegin == '<') {
5056 buf += *argListBegin++;
5058 while (*argListBegin != '>') {
5059 buf += *argListBegin++;
5062 buf += *argListBegin;
5066 buf += *argListBegin;
5073 ReplaceText(Start, OrigLength, buf);
5079 /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes:
5080 /// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst,
5081 /// struct Block_byref_id_object *src) {
5082 /// _Block_object_assign (&_dest->object, _src->object,
5083 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
5084 /// [|BLOCK_FIELD_IS_WEAK]) // object
5085 /// _Block_object_assign(&_dest->object, _src->object,
5086 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
5087 /// [|BLOCK_FIELD_IS_WEAK]) // block
5090 /// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) {
5091 /// _Block_object_dispose(_src->object,
5092 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT
5093 /// [|BLOCK_FIELD_IS_WEAK]) // object
5094 /// _Block_object_dispose(_src->object,
5095 /// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK
5096 /// [|BLOCK_FIELD_IS_WEAK]) // block
5099 std::string RewriteModernObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD,
5102 if (CopyDestroyCache.count(flag))
5104 CopyDestroyCache.insert(flag);
5105 S = "static void __Block_byref_id_object_copy_";
5107 S += "(void *dst, void *src) {\n";
5109 // offset into the object pointer is computed as:
5110 // void * + void* + int + int + void* + void *
5112 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
5113 unsigned VoidPtrSize =
5114 static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy));
5116 unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth();
5117 S += " _Block_object_assign((char*)dst + ";
5118 S += utostr(offset);
5119 S += ", *(void * *) ((char*)src + ";
5120 S += utostr(offset);
5125 S += "static void __Block_byref_id_object_dispose_";
5127 S += "(void *src) {\n";
5128 S += " _Block_object_dispose(*(void * *) ((char*)src + ";
5129 S += utostr(offset);
5136 /// RewriteByRefVar - For each __block typex ND variable this routine transforms
5137 /// the declaration into:
5138 /// struct __Block_byref_ND {
5139 /// void *__isa; // NULL for everything except __weak pointers
5140 /// struct __Block_byref_ND *__forwarding;
5141 /// int32_t __flags;
5143 /// void *__Block_byref_id_object_copy; // If variable is __block ObjC object
5144 /// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object
5148 /// It then replaces declaration of ND variable with:
5149 /// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag,
5150 /// __size=sizeof(struct __Block_byref_ND),
5151 /// ND=initializer-if-any};
5154 void RewriteModernObjC::RewriteByRefVar(VarDecl *ND, bool firstDecl,
5158 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
5159 if (DeclLoc.isInvalid())
5160 // If type location is missing, it is because of missing type (a warning).
5161 // Use variable's location which is good for this case.
5162 DeclLoc = ND->getLocation();
5163 const char *startBuf = SM->getCharacterData(DeclLoc);
5164 SourceLocation X = ND->getLocEnd();
5165 X = SM->getExpansionLoc(X);
5166 const char *endBuf = SM->getCharacterData(X);
5167 std::string Name(ND->getNameAsString());
5168 std::string ByrefType;
5169 RewriteByRefString(ByrefType, Name, ND, true);
5170 ByrefType += " {\n";
5171 ByrefType += " void *__isa;\n";
5172 RewriteByRefString(ByrefType, Name, ND);
5173 ByrefType += " *__forwarding;\n";
5174 ByrefType += " int __flags;\n";
5175 ByrefType += " int __size;\n";
5176 // Add void *__Block_byref_id_object_copy;
5177 // void *__Block_byref_id_object_dispose; if needed.
5178 QualType Ty = ND->getType();
5179 bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND);
5180 if (HasCopyAndDispose) {
5181 ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n";
5182 ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n";
5186 (void)convertBlockPointerToFunctionPointer(T);
5187 T.getAsStringInternal(Name, Context->getPrintingPolicy());
5189 ByrefType += " " + Name + ";\n";
5190 ByrefType += "};\n";
5191 // Insert this type in global scope. It is needed by helper function.
5192 SourceLocation FunLocStart;
5194 FunLocStart = getFunctionSourceLocation(*this, CurFunctionDef);
5196 assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null");
5197 FunLocStart = CurMethodDef->getLocStart();
5199 InsertText(FunLocStart, ByrefType);
5201 if (Ty.isObjCGCWeak()) {
5202 flag |= BLOCK_FIELD_IS_WEAK;
5205 if (HasCopyAndDispose) {
5206 flag = BLOCK_BYREF_CALLER;
5207 QualType Ty = ND->getType();
5208 // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well.
5209 if (Ty->isBlockPointerType())
5210 flag |= BLOCK_FIELD_IS_BLOCK;
5212 flag |= BLOCK_FIELD_IS_OBJECT;
5213 std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag);
5218 // struct __Block_byref_ND ND =
5219 // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND),
5220 // initializer-if-any};
5221 bool hasInit = (ND->getInit() != nullptr);
5222 // FIXME. rewriter does not support __block c++ objects which
5223 // require construction.
5225 if (CXXConstructExpr *CExp = dyn_cast<CXXConstructExpr>(ND->getInit())) {
5226 CXXConstructorDecl *CXXDecl = CExp->getConstructor();
5227 if (CXXDecl && CXXDecl->isDefaultConstructor())
5232 if (HasCopyAndDispose)
5233 flags |= BLOCK_HAS_COPY_DISPOSE;
5234 Name = ND->getNameAsString();
5236 RewriteByRefString(ByrefType, Name, ND);
5237 std::string ForwardingCastType("(");
5238 ForwardingCastType += ByrefType + " *)";
5239 ByrefType += " " + Name + " = {(void*)";
5240 ByrefType += utostr(isa);
5241 ByrefType += "," + ForwardingCastType + "&" + Name + ", ";
5242 ByrefType += utostr(flags);
5244 ByrefType += "sizeof(";
5245 RewriteByRefString(ByrefType, Name, ND);
5247 if (HasCopyAndDispose) {
5248 ByrefType += ", __Block_byref_id_object_copy_";
5249 ByrefType += utostr(flag);
5250 ByrefType += ", __Block_byref_id_object_dispose_";
5251 ByrefType += utostr(flag);
5255 // In multiple __block declarations, and for all but 1st declaration,
5256 // find location of the separating comma. This would be start location
5257 // where new text is to be inserted.
5258 DeclLoc = ND->getLocation();
5259 const char *startDeclBuf = SM->getCharacterData(DeclLoc);
5260 const char *commaBuf = startDeclBuf;
5261 while (*commaBuf != ',')
5263 assert((*commaBuf == ',') && "RewriteByRefVar: can't find ','");
5264 DeclLoc = DeclLoc.getLocWithOffset(commaBuf - startDeclBuf);
5265 startBuf = commaBuf;
5269 ByrefType += "};\n";
5270 unsigned nameSize = Name.size();
5271 // for block or function pointer declaration. Name is aleady
5272 // part of the declaration.
5273 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType())
5275 ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType);
5279 SourceLocation startLoc;
5280 Expr *E = ND->getInit();
5281 if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
5282 startLoc = ECE->getLParenLoc();
5284 startLoc = E->getLocStart();
5285 startLoc = SM->getExpansionLoc(startLoc);
5286 endBuf = SM->getCharacterData(startLoc);
5287 ReplaceText(DeclLoc, endBuf-startBuf, ByrefType);
5289 const char separator = lastDecl ? ';' : ',';
5290 const char *startInitializerBuf = SM->getCharacterData(startLoc);
5291 const char *separatorBuf = strchr(startInitializerBuf, separator);
5292 assert((*separatorBuf == separator) &&
5293 "RewriteByRefVar: can't find ';' or ','");
5294 SourceLocation separatorLoc =
5295 startLoc.getLocWithOffset(separatorBuf-startInitializerBuf);
5297 InsertText(separatorLoc, lastDecl ? "}" : "};\n");
5302 void RewriteModernObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) {
5303 // Add initializers for any closure decl refs.
5304 GetBlockDeclRefExprs(Exp->getBody());
5305 if (BlockDeclRefs.size()) {
5306 // Unique all "by copy" declarations.
5307 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5308 if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
5309 if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
5310 BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
5311 BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl());
5314 // Unique all "by ref" declarations.
5315 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5316 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>()) {
5317 if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) {
5318 BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl());
5319 BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl());
5322 // Find any imported blocks...they will need special attention.
5323 for (unsigned i = 0; i < BlockDeclRefs.size(); i++)
5324 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
5325 BlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5326 BlockDeclRefs[i]->getType()->isBlockPointerType())
5327 ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
5331 FunctionDecl *RewriteModernObjC::SynthBlockInitFunctionDecl(StringRef name) {
5332 IdentifierInfo *ID = &Context->Idents.get(name);
5333 QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy);
5334 return FunctionDecl::Create(*Context, TUDecl, SourceLocation(),
5335 SourceLocation(), ID, FType, nullptr, SC_Extern,
5339 Stmt *RewriteModernObjC::SynthBlockInitExpr(BlockExpr *Exp,
5340 const SmallVectorImpl<DeclRefExpr *> &InnerBlockDeclRefs) {
5342 const BlockDecl *block = Exp->getBlockDecl();
5344 Blocks.push_back(Exp);
5346 CollectBlockDeclRefInfo(Exp);
5348 // Add inner imported variables now used in current block.
5349 int countOfInnerDecls = 0;
5350 if (!InnerBlockDeclRefs.empty()) {
5351 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) {
5352 DeclRefExpr *Exp = InnerBlockDeclRefs[i];
5353 ValueDecl *VD = Exp->getDecl();
5354 if (!VD->hasAttr<BlocksAttr>() && !BlockByCopyDeclsPtrSet.count(VD)) {
5355 // We need to save the copied-in variables in nested
5356 // blocks because it is needed at the end for some of the API generations.
5357 // See SynthesizeBlockLiterals routine.
5358 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
5359 BlockDeclRefs.push_back(Exp);
5360 BlockByCopyDeclsPtrSet.insert(VD);
5361 BlockByCopyDecls.push_back(VD);
5363 if (VD->hasAttr<BlocksAttr>() && !BlockByRefDeclsPtrSet.count(VD)) {
5364 InnerDeclRefs.push_back(Exp); countOfInnerDecls++;
5365 BlockDeclRefs.push_back(Exp);
5366 BlockByRefDeclsPtrSet.insert(VD);
5367 BlockByRefDecls.push_back(VD);
5370 // Find any imported blocks...they will need special attention.
5371 for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++)
5372 if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
5373 InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5374 InnerBlockDeclRefs[i]->getType()->isBlockPointerType())
5375 ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl());
5377 InnerDeclRefsCount.push_back(countOfInnerDecls);
5379 std::string FuncName;
5382 FuncName = CurFunctionDef->getNameAsString();
5383 else if (CurMethodDef)
5384 BuildUniqueMethodName(FuncName, CurMethodDef);
5385 else if (GlobalVarDecl)
5386 FuncName = std::string(GlobalVarDecl->getNameAsString());
5388 bool GlobalBlockExpr =
5389 block->getDeclContext()->getRedeclContext()->isFileContext();
5391 if (GlobalBlockExpr && !GlobalVarDecl) {
5392 Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag);
5393 GlobalBlockExpr = false;
5396 std::string BlockNumber = utostr(Blocks.size()-1);
5398 std::string Func = "__" + FuncName + "_block_func_" + BlockNumber;
5400 // Get a pointer to the function type so we can cast appropriately.
5401 QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType());
5402 QualType FType = Context->getPointerType(BFT);
5407 // Simulate a constructor call...
5410 if (GlobalBlockExpr)
5414 Tag += FuncName + "_block_impl_" + BlockNumber;
5416 FD = SynthBlockInitFunctionDecl(Tag);
5417 DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, false, FType, VK_RValue,
5420 SmallVector<Expr*, 4> InitExprs;
5422 // Initialize the block function.
5423 FD = SynthBlockInitFunctionDecl(Func);
5424 DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, false, FD->getType(),
5425 VK_LValue, SourceLocation());
5426 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5428 InitExprs.push_back(castExpr);
5430 // Initialize the block descriptor.
5431 std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA";
5433 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl,
5434 SourceLocation(), SourceLocation(),
5435 &Context->Idents.get(DescData.c_str()),
5436 Context->VoidPtrTy, nullptr,
5438 UnaryOperator *DescRefExpr =
5439 new (Context) UnaryOperator(new (Context) DeclRefExpr(NewVD, false,
5444 Context->getPointerType(Context->VoidPtrTy),
5445 VK_RValue, OK_Ordinary,
5447 InitExprs.push_back(DescRefExpr);
5449 // Add initializers for any closure decl refs.
5450 if (BlockDeclRefs.size()) {
5452 // Output all "by copy" declarations.
5453 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByCopyDecls.begin(),
5454 E = BlockByCopyDecls.end(); I != E; ++I) {
5455 if (isObjCType((*I)->getType())) {
5456 // FIXME: Conform to ABI ([[obj retain] autorelease]).
5457 FD = SynthBlockInitFunctionDecl((*I)->getName());
5458 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(),
5459 VK_LValue, SourceLocation());
5460 if (HasLocalVariableExternalStorage(*I)) {
5461 QualType QT = (*I)->getType();
5462 QT = Context->getPointerType(QT);
5463 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
5464 OK_Ordinary, SourceLocation());
5466 } else if (isTopLevelBlockPointerType((*I)->getType())) {
5467 FD = SynthBlockInitFunctionDecl((*I)->getName());
5468 Arg = new (Context) DeclRefExpr(FD, false, FD->getType(),
5469 VK_LValue, SourceLocation());
5470 Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5473 FD = SynthBlockInitFunctionDecl((*I)->getName());
5474 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(),
5475 VK_LValue, SourceLocation());
5476 if (HasLocalVariableExternalStorage(*I)) {
5477 QualType QT = (*I)->getType();
5478 QT = Context->getPointerType(QT);
5479 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf, QT, VK_RValue,
5480 OK_Ordinary, SourceLocation());
5484 InitExprs.push_back(Exp);
5486 // Output all "by ref" declarations.
5487 for (SmallVectorImpl<ValueDecl *>::iterator I = BlockByRefDecls.begin(),
5488 E = BlockByRefDecls.end(); I != E; ++I) {
5489 ValueDecl *ND = (*I);
5490 std::string Name(ND->getNameAsString());
5491 std::string RecName;
5492 RewriteByRefString(RecName, Name, ND, true);
5493 IdentifierInfo *II = &Context->Idents.get(RecName.c_str()
5494 + sizeof("struct"));
5495 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
5496 SourceLocation(), SourceLocation(),
5498 assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl");
5499 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
5501 FD = SynthBlockInitFunctionDecl((*I)->getName());
5502 Exp = new (Context) DeclRefExpr(FD, false, FD->getType(), VK_LValue,
5504 bool isNestedCapturedVar = false;
5506 for (const auto &CI : block->captures()) {
5507 const VarDecl *variable = CI.getVariable();
5508 if (variable == ND && CI.isNested()) {
5509 assert (CI.isByRef() &&
5510 "SynthBlockInitExpr - captured block variable is not byref");
5511 isNestedCapturedVar = true;
5515 // captured nested byref variable has its address passed. Do not take
5516 // its address again.
5517 if (!isNestedCapturedVar)
5518 Exp = new (Context) UnaryOperator(Exp, UO_AddrOf,
5519 Context->getPointerType(Exp->getType()),
5520 VK_RValue, OK_Ordinary, SourceLocation());
5521 Exp = NoTypeInfoCStyleCastExpr(Context, castT, CK_BitCast, Exp);
5522 InitExprs.push_back(Exp);
5525 if (ImportedBlockDecls.size()) {
5526 // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR
5527 int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR);
5529 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
5530 Expr *FlagExp = IntegerLiteral::Create(*Context, llvm::APInt(IntSize, flag),
5531 Context->IntTy, SourceLocation());
5532 InitExprs.push_back(FlagExp);
5534 NewRep = new (Context) CallExpr(*Context, DRE, InitExprs,
5535 FType, VK_LValue, SourceLocation());
5537 if (GlobalBlockExpr) {
5538 assert (!GlobalConstructionExp &&
5539 "SynthBlockInitExpr - GlobalConstructionExp must be null");
5540 GlobalConstructionExp = NewRep;
5544 NewRep = new (Context) UnaryOperator(NewRep, UO_AddrOf,
5545 Context->getPointerType(NewRep->getType()),
5546 VK_RValue, OK_Ordinary, SourceLocation());
5547 NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CK_BitCast,
5549 BlockDeclRefs.clear();
5550 BlockByRefDecls.clear();
5551 BlockByRefDeclsPtrSet.clear();
5552 BlockByCopyDecls.clear();
5553 BlockByCopyDeclsPtrSet.clear();
5554 ImportedBlockDecls.clear();
5558 bool RewriteModernObjC::IsDeclStmtInForeachHeader(DeclStmt *DS) {
5559 if (const ObjCForCollectionStmt * CS =
5560 dyn_cast<ObjCForCollectionStmt>(Stmts.back()))
5561 return CS->getElement() == DS;
5565 //===----------------------------------------------------------------------===//
5566 // Function Body / Expression rewriting
5567 //===----------------------------------------------------------------------===//
5569 Stmt *RewriteModernObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
5570 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
5571 isa<DoStmt>(S) || isa<ForStmt>(S))
5573 else if (isa<ObjCForCollectionStmt>(S)) {
5575 ObjCBcLabelNo.push_back(++BcLabelCount);
5578 // Pseudo-object operations and ivar references need special
5579 // treatment because we're going to recursively rewrite them.
5580 if (PseudoObjectExpr *PseudoOp = dyn_cast<PseudoObjectExpr>(S)) {
5581 if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) {
5582 return RewritePropertyOrImplicitSetter(PseudoOp);
5584 return RewritePropertyOrImplicitGetter(PseudoOp);
5586 } else if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) {
5587 return RewriteObjCIvarRefExpr(IvarRefExpr);
5589 else if (isa<OpaqueValueExpr>(S))
5590 S = cast<OpaqueValueExpr>(S)->getSourceExpr();
5592 SourceRange OrigStmtRange = S->getSourceRange();
5594 // Perform a bottom up rewrite of all children.
5595 for (Stmt::child_range CI = S->children(); CI; ++CI)
5597 Stmt *childStmt = (*CI);
5598 Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(childStmt);
5604 if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
5605 SmallVector<DeclRefExpr *, 8> InnerBlockDeclRefs;
5606 llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts;
5607 InnerContexts.insert(BE->getBlockDecl());
5608 ImportedLocalExternalDecls.clear();
5609 GetInnerBlockDeclRefExprs(BE->getBody(),
5610 InnerBlockDeclRefs, InnerContexts);
5611 // Rewrite the block body in place.
5612 Stmt *SaveCurrentBody = CurrentBody;
5613 CurrentBody = BE->getBody();
5614 PropParentMap = nullptr;
5615 // block literal on rhs of a property-dot-sytax assignment
5616 // must be replaced by its synthesize ast so getRewrittenText
5617 // works as expected. In this case, what actually ends up on RHS
5618 // is the blockTranscribed which is the helper function for the
5619 // block literal; as in: self.c = ^() {[ace ARR];};
5620 bool saveDisableReplaceStmt = DisableReplaceStmt;
5621 DisableReplaceStmt = false;
5622 RewriteFunctionBodyOrGlobalInitializer(BE->getBody());
5623 DisableReplaceStmt = saveDisableReplaceStmt;
5624 CurrentBody = SaveCurrentBody;
5625 PropParentMap = nullptr;
5626 ImportedLocalExternalDecls.clear();
5627 // Now we snarf the rewritten text and stash it away for later use.
5628 std::string Str = Rewrite.getRewrittenText(BE->getSourceRange());
5629 RewrittenBlockExprs[BE] = Str;
5631 Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs);
5633 //blockTranscribed->dump();
5634 ReplaceStmt(S, blockTranscribed);
5635 return blockTranscribed;
5637 // Handle specific things.
5638 if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S))
5639 return RewriteAtEncode(AtEncode);
5641 if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S))
5642 return RewriteAtSelector(AtSelector);
5644 if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S))
5645 return RewriteObjCStringLiteral(AtString);
5647 if (ObjCBoolLiteralExpr *BoolLitExpr = dyn_cast<ObjCBoolLiteralExpr>(S))
5648 return RewriteObjCBoolLiteralExpr(BoolLitExpr);
5650 if (ObjCBoxedExpr *BoxedExpr = dyn_cast<ObjCBoxedExpr>(S))
5651 return RewriteObjCBoxedExpr(BoxedExpr);
5653 if (ObjCArrayLiteral *ArrayLitExpr = dyn_cast<ObjCArrayLiteral>(S))
5654 return RewriteObjCArrayLiteralExpr(ArrayLitExpr);
5656 if (ObjCDictionaryLiteral *DictionaryLitExpr =
5657 dyn_cast<ObjCDictionaryLiteral>(S))
5658 return RewriteObjCDictionaryLiteralExpr(DictionaryLitExpr);
5660 if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) {
5662 // Before we rewrite it, put the original message expression in a comment.
5663 SourceLocation startLoc = MessExpr->getLocStart();
5664 SourceLocation endLoc = MessExpr->getLocEnd();
5666 const char *startBuf = SM->getCharacterData(startLoc);
5667 const char *endBuf = SM->getCharacterData(endLoc);
5669 std::string messString;
5670 messString += "// ";
5671 messString.append(startBuf, endBuf-startBuf+1);
5674 // FIXME: Missing definition of
5675 // InsertText(clang::SourceLocation, char const*, unsigned int).
5676 // InsertText(startLoc, messString.c_str(), messString.size());
5677 // Tried this, but it didn't work either...
5678 // ReplaceText(startLoc, 0, messString.c_str(), messString.size());
5680 return RewriteMessageExpr(MessExpr);
5683 if (ObjCAutoreleasePoolStmt *StmtAutoRelease =
5684 dyn_cast<ObjCAutoreleasePoolStmt>(S)) {
5685 return RewriteObjCAutoreleasePoolStmt(StmtAutoRelease);
5688 if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S))
5689 return RewriteObjCTryStmt(StmtTry);
5691 if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S))
5692 return RewriteObjCSynchronizedStmt(StmtTry);
5694 if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S))
5695 return RewriteObjCThrowStmt(StmtThrow);
5697 if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S))
5698 return RewriteObjCProtocolExpr(ProtocolExp);
5700 if (ObjCForCollectionStmt *StmtForCollection =
5701 dyn_cast<ObjCForCollectionStmt>(S))
5702 return RewriteObjCForCollectionStmt(StmtForCollection,
5703 OrigStmtRange.getEnd());
5704 if (BreakStmt *StmtBreakStmt =
5705 dyn_cast<BreakStmt>(S))
5706 return RewriteBreakStmt(StmtBreakStmt);
5707 if (ContinueStmt *StmtContinueStmt =
5708 dyn_cast<ContinueStmt>(S))
5709 return RewriteContinueStmt(StmtContinueStmt);
5711 // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls
5713 if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
5714 // FIXME: What we're doing here is modifying the type-specifier that
5715 // precedes the first Decl. In the future the DeclGroup should have
5716 // a separate type-specifier that we can rewrite.
5717 // NOTE: We need to avoid rewriting the DeclStmt if it is within
5718 // the context of an ObjCForCollectionStmt. For example:
5719 // NSArray *someArray;
5720 // for (id <FooProtocol> index in someArray) ;
5721 // This is because RewriteObjCForCollectionStmt() does textual rewriting
5722 // and it depends on the original text locations/positions.
5723 if (Stmts.empty() || !IsDeclStmtInForeachHeader(DS))
5724 RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin());
5726 // Blocks rewrite rules.
5727 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
5730 if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
5731 if (isTopLevelBlockPointerType(ND->getType()))
5732 RewriteBlockPointerDecl(ND);
5733 else if (ND->getType()->isFunctionPointerType())
5734 CheckFunctionPointerDecl(ND->getType(), ND);
5735 if (VarDecl *VD = dyn_cast<VarDecl>(SD)) {
5736 if (VD->hasAttr<BlocksAttr>()) {
5737 static unsigned uniqueByrefDeclCount = 0;
5738 assert(!BlockByRefDeclNo.count(ND) &&
5739 "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl");
5740 BlockByRefDeclNo[ND] = uniqueByrefDeclCount++;
5741 RewriteByRefVar(VD, (DI == DS->decl_begin()), ((DI+1) == DE));
5744 RewriteTypeOfDecl(VD);
5747 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
5748 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
5749 RewriteBlockPointerDecl(TD);
5750 else if (TD->getUnderlyingType()->isFunctionPointerType())
5751 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
5756 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S))
5757 RewriteObjCQualifiedInterfaceTypes(CE);
5759 if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) ||
5760 isa<DoStmt>(S) || isa<ForStmt>(S)) {
5761 assert(!Stmts.empty() && "Statement stack is empty");
5762 assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) ||
5763 isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back()))
5764 && "Statement stack mismatch");
5767 // Handle blocks rewriting.
5768 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {
5769 ValueDecl *VD = DRE->getDecl();
5770 if (VD->hasAttr<BlocksAttr>())
5771 return RewriteBlockDeclRefExpr(DRE);
5772 if (HasLocalVariableExternalStorage(VD))
5773 return RewriteLocalVariableExternalStorage(DRE);
5776 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
5777 if (CE->getCallee()->getType()->isBlockPointerType()) {
5778 Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee());
5779 ReplaceStmt(S, BlockCall);
5783 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) {
5784 RewriteCastExpr(CE);
5786 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
5787 RewriteImplicitCastObjCExpr(ICE);
5791 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) {
5792 CastExpr *Replacement = new (Context) CastExpr(ICE->getType(),
5795 // Get the new text.
5797 llvm::raw_string_ostream Buf(SStr);
5798 Replacement->printPretty(Buf);
5799 const std::string &Str = Buf.str();
5801 printf("CAST = %s\n", &Str[0]);
5802 InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size());
5807 // Return this stmt unmodified.
5811 void RewriteModernObjC::RewriteRecordBody(RecordDecl *RD) {
5812 for (auto *FD : RD->fields()) {
5813 if (isTopLevelBlockPointerType(FD->getType()))
5814 RewriteBlockPointerDecl(FD);
5815 if (FD->getType()->isObjCQualifiedIdType() ||
5816 FD->getType()->isObjCQualifiedInterfaceType())
5817 RewriteObjCQualifiedInterfaceTypes(FD);
5821 /// HandleDeclInMainFile - This is called for each top-level decl defined in the
5822 /// main file of the input.
5823 void RewriteModernObjC::HandleDeclInMainFile(Decl *D) {
5824 switch (D->getKind()) {
5825 case Decl::Function: {
5826 FunctionDecl *FD = cast<FunctionDecl>(D);
5827 if (FD->isOverloadedOperator())
5830 // Since function prototypes don't have ParmDecl's, we check the function
5831 // prototype. This enables us to rewrite function declarations and
5832 // definitions using the same code.
5833 RewriteBlocksInFunctionProtoType(FD->getType(), FD);
5835 if (!FD->isThisDeclarationADefinition())
5838 // FIXME: If this should support Obj-C++, support CXXTryStmt
5839 if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) {
5840 CurFunctionDef = FD;
5843 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
5845 CurrentBody = nullptr;
5846 if (PropParentMap) {
5847 delete PropParentMap;
5848 PropParentMap = nullptr;
5850 // This synthesizes and inserts the block "impl" struct, invoke function,
5851 // and any copy/dispose helper functions.
5852 InsertBlockLiteralsWithinFunction(FD);
5853 RewriteLineDirective(D);
5854 CurFunctionDef = nullptr;
5858 case Decl::ObjCMethod: {
5859 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
5860 if (CompoundStmt *Body = MD->getCompoundBody()) {
5864 cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body));
5866 CurrentBody = nullptr;
5867 if (PropParentMap) {
5868 delete PropParentMap;
5869 PropParentMap = nullptr;
5871 InsertBlockLiteralsWithinMethod(MD);
5872 RewriteLineDirective(D);
5873 CurMethodDef = nullptr;
5877 case Decl::ObjCImplementation: {
5878 ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D);
5879 ClassImplementation.push_back(CI);
5882 case Decl::ObjCCategoryImpl: {
5883 ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D);
5884 CategoryImplementation.push_back(CI);
5888 VarDecl *VD = cast<VarDecl>(D);
5889 RewriteObjCQualifiedInterfaceTypes(VD);
5890 if (isTopLevelBlockPointerType(VD->getType()))
5891 RewriteBlockPointerDecl(VD);
5892 else if (VD->getType()->isFunctionPointerType()) {
5893 CheckFunctionPointerDecl(VD->getType(), VD);
5894 if (VD->getInit()) {
5895 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
5896 RewriteCastExpr(CE);
5899 } else if (VD->getType()->isRecordType()) {
5900 RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl();
5901 if (RD->isCompleteDefinition())
5902 RewriteRecordBody(RD);
5904 if (VD->getInit()) {
5906 CurrentBody = VD->getInit();
5907 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
5908 CurrentBody = nullptr;
5909 if (PropParentMap) {
5910 delete PropParentMap;
5911 PropParentMap = nullptr;
5913 SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName());
5914 GlobalVarDecl = nullptr;
5916 // This is needed for blocks.
5917 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
5918 RewriteCastExpr(CE);
5923 case Decl::TypeAlias:
5924 case Decl::Typedef: {
5925 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
5926 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
5927 RewriteBlockPointerDecl(TD);
5928 else if (TD->getUnderlyingType()->isFunctionPointerType())
5929 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
5931 RewriteObjCQualifiedInterfaceTypes(TD);
5935 case Decl::CXXRecord:
5936 case Decl::Record: {
5937 RecordDecl *RD = cast<RecordDecl>(D);
5938 if (RD->isCompleteDefinition())
5939 RewriteRecordBody(RD);
5948 /// Write_ProtocolExprReferencedMetadata - This routine writer out the
5949 /// protocol reference symbols in the for of:
5950 /// struct _protocol_t *PROTOCOL_REF = &PROTOCOL_METADATA.
5951 static void Write_ProtocolExprReferencedMetadata(ASTContext *Context,
5952 ObjCProtocolDecl *PDecl,
5953 std::string &Result) {
5954 // Also output .objc_protorefs$B section and its meta-data.
5955 if (Context->getLangOpts().MicrosoftExt)
5956 Result += "static ";
5957 Result += "struct _protocol_t *";
5958 Result += "_OBJC_PROTOCOL_REFERENCE_$_";
5959 Result += PDecl->getNameAsString();
5961 Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString();
5965 void RewriteModernObjC::HandleTranslationUnit(ASTContext &C) {
5966 if (Diags.hasErrorOccurred())
5971 for (unsigned i = 0, e = FunctionDefinitionsSeen.size(); i < e; i++) {
5972 // translation of function bodies were postponed until all class and
5973 // their extensions and implementations are seen. This is because, we
5974 // cannot build grouping structs for bitfields until they are all seen.
5975 FunctionDecl *FDecl = FunctionDefinitionsSeen[i];
5976 HandleTopLevelSingleDecl(FDecl);
5979 // Here's a great place to add any extra declarations that may be needed.
5980 // Write out meta data for each @protocol(<expr>).
5981 for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(),
5982 E = ProtocolExprDecls.end(); I != E; ++I) {
5983 RewriteObjCProtocolMetaData(*I, Preamble);
5984 Write_ProtocolExprReferencedMetadata(Context, (*I), Preamble);
5987 InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false);
5989 if (ClassImplementation.size() || CategoryImplementation.size())
5990 RewriteImplementations();
5992 for (unsigned i = 0, e = ObjCInterfacesSeen.size(); i < e; i++) {
5993 ObjCInterfaceDecl *CDecl = ObjCInterfacesSeen[i];
5994 // Write struct declaration for the class matching its ivar declarations.
5995 // Note that for modern abi, this is postponed until the end of TU
5996 // because class extensions and the implementation might declare their own
5998 RewriteInterfaceDecl(CDecl);
6001 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
6003 if (const RewriteBuffer *RewriteBuf =
6004 Rewrite.getRewriteBufferFor(MainFileID)) {
6005 //printf("Changed:\n");
6006 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
6008 llvm::errs() << "No changes\n";
6011 if (ClassImplementation.size() || CategoryImplementation.size() ||
6012 ProtocolExprDecls.size()) {
6013 // Rewrite Objective-c meta data*
6014 std::string ResultStr;
6015 RewriteMetaDataIntoBuffer(ResultStr);
6017 *OutFile << ResultStr;
6021 std::string ResultStr;
6022 WriteImageInfo(ResultStr);
6023 *OutFile << ResultStr;
6028 void RewriteModernObjC::Initialize(ASTContext &context) {
6029 InitializeCommon(context);
6031 Preamble += "#ifndef __OBJC2__\n";
6032 Preamble += "#define __OBJC2__\n";
6033 Preamble += "#endif\n";
6035 // declaring objc_selector outside the parameter list removes a silly
6036 // scope related warning...
6038 Preamble = "#pragma once\n";
6039 Preamble += "struct objc_selector; struct objc_class;\n";
6040 Preamble += "struct __rw_objc_super { \n\tstruct objc_object *object; ";
6041 Preamble += "\n\tstruct objc_object *superClass; ";
6042 // Add a constructor for creating temporary objects.
6043 Preamble += "\n\t__rw_objc_super(struct objc_object *o, struct objc_object *s) ";
6044 Preamble += ": object(o), superClass(s) {} ";
6045 Preamble += "\n};\n";
6047 if (LangOpts.MicrosoftExt) {
6048 // Define all sections using syntax that makes sense.
6049 // These are currently generated.
6050 Preamble += "\n#pragma section(\".objc_classlist$B\", long, read, write)\n";
6051 Preamble += "#pragma section(\".objc_catlist$B\", long, read, write)\n";
6052 Preamble += "#pragma section(\".objc_imageinfo$B\", long, read, write)\n";
6053 Preamble += "#pragma section(\".objc_nlclslist$B\", long, read, write)\n";
6054 Preamble += "#pragma section(\".objc_nlcatlist$B\", long, read, write)\n";
6055 // These are generated but not necessary for functionality.
6056 Preamble += "#pragma section(\".cat_cls_meth$B\", long, read, write)\n";
6057 Preamble += "#pragma section(\".inst_meth$B\", long, read, write)\n";
6058 Preamble += "#pragma section(\".cls_meth$B\", long, read, write)\n";
6059 Preamble += "#pragma section(\".objc_ivar$B\", long, read, write)\n";
6061 // These need be generated for performance. Currently they are not,
6062 // using API calls instead.
6063 Preamble += "#pragma section(\".objc_selrefs$B\", long, read, write)\n";
6064 Preamble += "#pragma section(\".objc_classrefs$B\", long, read, write)\n";
6065 Preamble += "#pragma section(\".objc_superrefs$B\", long, read, write)\n";
6068 Preamble += "#ifndef _REWRITER_typedef_Protocol\n";
6069 Preamble += "typedef struct objc_object Protocol;\n";
6070 Preamble += "#define _REWRITER_typedef_Protocol\n";
6071 Preamble += "#endif\n";
6072 if (LangOpts.MicrosoftExt) {
6073 Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n";
6074 Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n";
6077 Preamble += "#define __OBJC_RW_DLLIMPORT extern\n";
6079 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend(void);\n";
6080 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper(void);\n";
6081 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_stret(void);\n";
6082 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSendSuper_stret(void);\n";
6083 Preamble += "__OBJC_RW_DLLIMPORT void objc_msgSend_fpret(void);\n";
6085 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getClass";
6086 Preamble += "(const char *);\n";
6087 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass";
6088 Preamble += "(struct objc_class *);\n";
6089 Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *objc_getMetaClass";
6090 Preamble += "(const char *);\n";
6091 Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw( struct objc_object *);\n";
6092 // @synchronized hooks.
6093 Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_enter( struct objc_object *);\n";
6094 Preamble += "__OBJC_RW_DLLIMPORT int objc_sync_exit( struct objc_object *);\n";
6095 Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n";
6096 Preamble += "#ifdef _WIN64\n";
6097 Preamble += "typedef unsigned long long _WIN_NSUInteger;\n";
6098 Preamble += "#else\n";
6099 Preamble += "typedef unsigned int _WIN_NSUInteger;\n";
6100 Preamble += "#endif\n";
6101 Preamble += "#ifndef __FASTENUMERATIONSTATE\n";
6102 Preamble += "struct __objcFastEnumerationState {\n\t";
6103 Preamble += "unsigned long state;\n\t";
6104 Preamble += "void **itemsPtr;\n\t";
6105 Preamble += "unsigned long *mutationsPtr;\n\t";
6106 Preamble += "unsigned long extra[5];\n};\n";
6107 Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n";
6108 Preamble += "#define __FASTENUMERATIONSTATE\n";
6109 Preamble += "#endif\n";
6110 Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n";
6111 Preamble += "struct __NSConstantStringImpl {\n";
6112 Preamble += " int *isa;\n";
6113 Preamble += " int flags;\n";
6114 Preamble += " char *str;\n";
6115 Preamble += "#if _WIN64\n";
6116 Preamble += " long long length;\n";
6117 Preamble += "#else\n";
6118 Preamble += " long length;\n";
6119 Preamble += "#endif\n";
6121 Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n";
6122 Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n";
6123 Preamble += "#else\n";
6124 Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n";
6125 Preamble += "#endif\n";
6126 Preamble += "#define __NSCONSTANTSTRINGIMPL\n";
6127 Preamble += "#endif\n";
6129 Preamble += "#ifndef BLOCK_IMPL\n";
6130 Preamble += "#define BLOCK_IMPL\n";
6131 Preamble += "struct __block_impl {\n";
6132 Preamble += " void *isa;\n";
6133 Preamble += " int Flags;\n";
6134 Preamble += " int Reserved;\n";
6135 Preamble += " void *FuncPtr;\n";
6137 Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n";
6138 Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n";
6139 Preamble += "extern \"C\" __declspec(dllexport) "
6140 "void _Block_object_assign(void *, const void *, const int);\n";
6141 Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n";
6142 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n";
6143 Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n";
6144 Preamble += "#else\n";
6145 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n";
6146 Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n";
6147 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n";
6148 Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n";
6149 Preamble += "#endif\n";
6150 Preamble += "#endif\n";
6151 if (LangOpts.MicrosoftExt) {
6152 Preamble += "#undef __OBJC_RW_DLLIMPORT\n";
6153 Preamble += "#undef __OBJC_RW_STATICIMPORT\n";
6154 Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests.
6155 Preamble += "#define __attribute__(X)\n";
6156 Preamble += "#endif\n";
6157 Preamble += "#ifndef __weak\n";
6158 Preamble += "#define __weak\n";
6159 Preamble += "#endif\n";
6160 Preamble += "#ifndef __block\n";
6161 Preamble += "#define __block\n";
6162 Preamble += "#endif\n";
6165 Preamble += "#define __block\n";
6166 Preamble += "#define __weak\n";
6169 // Declarations required for modern objective-c array and dictionary literals.
6170 Preamble += "\n#include <stdarg.h>\n";
6171 Preamble += "struct __NSContainer_literal {\n";
6172 Preamble += " void * *arr;\n";
6173 Preamble += " __NSContainer_literal (unsigned int count, ...) {\n";
6174 Preamble += "\tva_list marker;\n";
6175 Preamble += "\tva_start(marker, count);\n";
6176 Preamble += "\tarr = new void *[count];\n";
6177 Preamble += "\tfor (unsigned i = 0; i < count; i++)\n";
6178 Preamble += "\t arr[i] = va_arg(marker, void *);\n";
6179 Preamble += "\tva_end( marker );\n";
6180 Preamble += " };\n";
6181 Preamble += " ~__NSContainer_literal() {\n";
6182 Preamble += "\tdelete[] arr;\n";
6186 // Declaration required for implementation of @autoreleasepool statement.
6187 Preamble += "extern \"C\" __declspec(dllimport) void * objc_autoreleasePoolPush(void);\n";
6188 Preamble += "extern \"C\" __declspec(dllimport) void objc_autoreleasePoolPop(void *);\n\n";
6189 Preamble += "struct __AtAutoreleasePool {\n";
6190 Preamble += " __AtAutoreleasePool() {atautoreleasepoolobj = objc_autoreleasePoolPush();}\n";
6191 Preamble += " ~__AtAutoreleasePool() {objc_autoreleasePoolPop(atautoreleasepoolobj);}\n";
6192 Preamble += " void * atautoreleasepoolobj;\n";
6195 // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long
6196 // as this avoids warning in any 64bit/32bit compilation model.
6197 Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n";
6200 /// RewriteIvarOffsetComputation - This rutine synthesizes computation of
6202 void RewriteModernObjC::RewriteIvarOffsetComputation(ObjCIvarDecl *ivar,
6203 std::string &Result) {
6204 Result += "__OFFSETOFIVAR__(struct ";
6205 Result += ivar->getContainingInterface()->getNameAsString();
6206 if (LangOpts.MicrosoftExt)
6209 if (ivar->isBitField())
6210 ObjCIvarBitfieldGroupDecl(ivar, Result);
6212 Result += ivar->getNameAsString();
6216 /// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI.
6217 /// struct _prop_t {
6218 /// const char *name;
6219 /// char *attributes;
6222 /// struct _prop_list_t {
6223 /// uint32_t entsize; // sizeof(struct _prop_t)
6224 /// uint32_t count_of_properties;
6225 /// struct _prop_t prop_list[count_of_properties];
6228 /// struct _protocol_t;
6230 /// struct _protocol_list_t {
6231 /// long protocol_count; // Note, this is 32/64 bit
6232 /// struct _protocol_t * protocol_list[protocol_count];
6235 /// struct _objc_method {
6237 /// const char *method_type;
6241 /// struct _method_list_t {
6242 /// uint32_t entsize; // sizeof(struct _objc_method)
6243 /// uint32_t method_count;
6244 /// struct _objc_method method_list[method_count];
6247 /// struct _protocol_t {
6249 /// const char *protocol_name;
6250 /// const struct _protocol_list_t * protocol_list; // super protocols
6251 /// const struct method_list_t *instance_methods;
6252 /// const struct method_list_t *class_methods;
6253 /// const struct method_list_t *optionalInstanceMethods;
6254 /// const struct method_list_t *optionalClassMethods;
6255 /// const struct _prop_list_t * properties;
6256 /// const uint32_t size; // sizeof(struct _protocol_t)
6257 /// const uint32_t flags; // = 0
6258 /// const char ** extendedMethodTypes;
6261 /// struct _ivar_t {
6262 /// unsigned long int *offset; // pointer to ivar offset location
6263 /// const char *name;
6264 /// const char *type;
6265 /// uint32_t alignment;
6269 /// struct _ivar_list_t {
6270 /// uint32 entsize; // sizeof(struct _ivar_t)
6272 /// struct _ivar_t list[count];
6275 /// struct _class_ro_t {
6277 /// uint32_t instanceStart;
6278 /// uint32_t instanceSize;
6279 /// uint32_t reserved; // only when building for 64bit targets
6280 /// const uint8_t *ivarLayout;
6281 /// const char *name;
6282 /// const struct _method_list_t *baseMethods;
6283 /// const struct _protocol_list_t *baseProtocols;
6284 /// const struct _ivar_list_t *ivars;
6285 /// const uint8_t *weakIvarLayout;
6286 /// const struct _prop_list_t *properties;
6289 /// struct _class_t {
6290 /// struct _class_t *isa;
6291 /// struct _class_t *superclass;
6294 /// struct _class_ro_t *ro;
6297 /// struct _category_t {
6298 /// const char *name;
6299 /// struct _class_t *cls;
6300 /// const struct _method_list_t *instance_methods;
6301 /// const struct _method_list_t *class_methods;
6302 /// const struct _protocol_list_t *protocols;
6303 /// const struct _prop_list_t *properties;
6306 /// MessageRefTy - LLVM for:
6307 /// struct _message_ref_t {
6312 /// SuperMessageRefTy - LLVM for:
6313 /// struct _super_message_ref_t {
6314 /// SUPER_IMP messenger;
6318 static void WriteModernMetadataDeclarations(ASTContext *Context, std::string &Result) {
6319 static bool meta_data_declared = false;
6320 if (meta_data_declared)
6323 Result += "\nstruct _prop_t {\n";
6324 Result += "\tconst char *name;\n";
6325 Result += "\tconst char *attributes;\n";
6328 Result += "\nstruct _protocol_t;\n";
6330 Result += "\nstruct _objc_method {\n";
6331 Result += "\tstruct objc_selector * _cmd;\n";
6332 Result += "\tconst char *method_type;\n";
6333 Result += "\tvoid *_imp;\n";
6336 Result += "\nstruct _protocol_t {\n";
6337 Result += "\tvoid * isa; // NULL\n";
6338 Result += "\tconst char *protocol_name;\n";
6339 Result += "\tconst struct _protocol_list_t * protocol_list; // super protocols\n";
6340 Result += "\tconst struct method_list_t *instance_methods;\n";
6341 Result += "\tconst struct method_list_t *class_methods;\n";
6342 Result += "\tconst struct method_list_t *optionalInstanceMethods;\n";
6343 Result += "\tconst struct method_list_t *optionalClassMethods;\n";
6344 Result += "\tconst struct _prop_list_t * properties;\n";
6345 Result += "\tconst unsigned int size; // sizeof(struct _protocol_t)\n";
6346 Result += "\tconst unsigned int flags; // = 0\n";
6347 Result += "\tconst char ** extendedMethodTypes;\n";
6350 Result += "\nstruct _ivar_t {\n";
6351 Result += "\tunsigned long int *offset; // pointer to ivar offset location\n";
6352 Result += "\tconst char *name;\n";
6353 Result += "\tconst char *type;\n";
6354 Result += "\tunsigned int alignment;\n";
6355 Result += "\tunsigned int size;\n";
6358 Result += "\nstruct _class_ro_t {\n";
6359 Result += "\tunsigned int flags;\n";
6360 Result += "\tunsigned int instanceStart;\n";
6361 Result += "\tunsigned int instanceSize;\n";
6362 const llvm::Triple &Triple(Context->getTargetInfo().getTriple());
6363 if (Triple.getArch() == llvm::Triple::x86_64)
6364 Result += "\tunsigned int reserved;\n";
6365 Result += "\tconst unsigned char *ivarLayout;\n";
6366 Result += "\tconst char *name;\n";
6367 Result += "\tconst struct _method_list_t *baseMethods;\n";
6368 Result += "\tconst struct _objc_protocol_list *baseProtocols;\n";
6369 Result += "\tconst struct _ivar_list_t *ivars;\n";
6370 Result += "\tconst unsigned char *weakIvarLayout;\n";
6371 Result += "\tconst struct _prop_list_t *properties;\n";
6374 Result += "\nstruct _class_t {\n";
6375 Result += "\tstruct _class_t *isa;\n";
6376 Result += "\tstruct _class_t *superclass;\n";
6377 Result += "\tvoid *cache;\n";
6378 Result += "\tvoid *vtable;\n";
6379 Result += "\tstruct _class_ro_t *ro;\n";
6382 Result += "\nstruct _category_t {\n";
6383 Result += "\tconst char *name;\n";
6384 Result += "\tstruct _class_t *cls;\n";
6385 Result += "\tconst struct _method_list_t *instance_methods;\n";
6386 Result += "\tconst struct _method_list_t *class_methods;\n";
6387 Result += "\tconst struct _protocol_list_t *protocols;\n";
6388 Result += "\tconst struct _prop_list_t *properties;\n";
6391 Result += "extern \"C\" __declspec(dllimport) struct objc_cache _objc_empty_cache;\n";
6392 Result += "#pragma warning(disable:4273)\n";
6393 meta_data_declared = true;
6396 static void Write_protocol_list_t_TypeDecl(std::string &Result,
6397 long super_protocol_count) {
6398 Result += "struct /*_protocol_list_t*/"; Result += " {\n";
6399 Result += "\tlong protocol_count; // Note, this is 32/64 bit\n";
6400 Result += "\tstruct _protocol_t *super_protocols[";
6401 Result += utostr(super_protocol_count); Result += "];\n";
6405 static void Write_method_list_t_TypeDecl(std::string &Result,
6406 unsigned int method_count) {
6407 Result += "struct /*_method_list_t*/"; Result += " {\n";
6408 Result += "\tunsigned int entsize; // sizeof(struct _objc_method)\n";
6409 Result += "\tunsigned int method_count;\n";
6410 Result += "\tstruct _objc_method method_list[";
6411 Result += utostr(method_count); Result += "];\n";
6415 static void Write__prop_list_t_TypeDecl(std::string &Result,
6416 unsigned int property_count) {
6417 Result += "struct /*_prop_list_t*/"; Result += " {\n";
6418 Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n";
6419 Result += "\tunsigned int count_of_properties;\n";
6420 Result += "\tstruct _prop_t prop_list[";
6421 Result += utostr(property_count); Result += "];\n";
6425 static void Write__ivar_list_t_TypeDecl(std::string &Result,
6426 unsigned int ivar_count) {
6427 Result += "struct /*_ivar_list_t*/"; Result += " {\n";
6428 Result += "\tunsigned int entsize; // sizeof(struct _prop_t)\n";
6429 Result += "\tunsigned int count;\n";
6430 Result += "\tstruct _ivar_t ivar_list[";
6431 Result += utostr(ivar_count); Result += "];\n";
6435 static void Write_protocol_list_initializer(ASTContext *Context, std::string &Result,
6436 ArrayRef<ObjCProtocolDecl *> SuperProtocols,
6438 StringRef ProtocolName) {
6439 if (SuperProtocols.size() > 0) {
6440 Result += "\nstatic ";
6441 Write_protocol_list_t_TypeDecl(Result, SuperProtocols.size());
6442 Result += " "; Result += VarName;
6443 Result += ProtocolName;
6444 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6445 Result += "\t"; Result += utostr(SuperProtocols.size()); Result += ",\n";
6446 for (unsigned i = 0, e = SuperProtocols.size(); i < e; i++) {
6447 ObjCProtocolDecl *SuperPD = SuperProtocols[i];
6448 Result += "\t&"; Result += "_OBJC_PROTOCOL_";
6449 Result += SuperPD->getNameAsString();
6458 static void Write_method_list_t_initializer(RewriteModernObjC &RewriteObj,
6459 ASTContext *Context, std::string &Result,
6460 ArrayRef<ObjCMethodDecl *> Methods,
6462 StringRef TopLevelDeclName,
6464 if (Methods.size() > 0) {
6465 Result += "\nstatic ";
6466 Write_method_list_t_TypeDecl(Result, Methods.size());
6467 Result += " "; Result += VarName;
6468 Result += TopLevelDeclName;
6469 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6470 Result += "\t"; Result += "sizeof(_objc_method)"; Result += ",\n";
6471 Result += "\t"; Result += utostr(Methods.size()); Result += ",\n";
6472 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
6473 ObjCMethodDecl *MD = Methods[i];
6475 Result += "\t{{(struct objc_selector *)\"";
6477 Result += "\t{(struct objc_selector *)\"";
6478 Result += (MD)->getSelector().getAsString(); Result += "\"";
6480 std::string MethodTypeString;
6481 Context->getObjCEncodingForMethodDecl(MD, MethodTypeString);
6482 Result += "\""; Result += MethodTypeString; Result += "\"";
6487 Result += "(void *)";
6488 Result += RewriteObj.MethodInternalNames[MD];
6499 static void Write_prop_list_t_initializer(RewriteModernObjC &RewriteObj,
6500 ASTContext *Context, std::string &Result,
6501 ArrayRef<ObjCPropertyDecl *> Properties,
6502 const Decl *Container,
6504 StringRef ProtocolName) {
6505 if (Properties.size() > 0) {
6506 Result += "\nstatic ";
6507 Write__prop_list_t_TypeDecl(Result, Properties.size());
6508 Result += " "; Result += VarName;
6509 Result += ProtocolName;
6510 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6511 Result += "\t"; Result += "sizeof(_prop_t)"; Result += ",\n";
6512 Result += "\t"; Result += utostr(Properties.size()); Result += ",\n";
6513 for (unsigned i = 0, e = Properties.size(); i < e; i++) {
6514 ObjCPropertyDecl *PropDecl = Properties[i];
6519 Result += PropDecl->getName(); Result += "\",";
6520 std::string PropertyTypeString, QuotePropertyTypeString;
6521 Context->getObjCEncodingForPropertyDecl(PropDecl, Container, PropertyTypeString);
6522 RewriteObj.QuoteDoublequotes(PropertyTypeString, QuotePropertyTypeString);
6523 Result += "\""; Result += QuotePropertyTypeString; Result += "\"";
6534 enum MetaDataDlags {
6538 OBJC2_CLS_HIDDEN = 0x10,
6539 CLS_EXCEPTION = 0x20,
6541 /// (Obsolete) ARC-specific: this class has a .release_ivars method
6542 CLS_HAS_IVAR_RELEASER = 0x40,
6543 /// class was compiled with -fobjc-arr
6544 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
6547 static void Write__class_ro_t_initializer(ASTContext *Context, std::string &Result,
6549 const std::string &InstanceStart,
6550 const std::string &InstanceSize,
6551 ArrayRef<ObjCMethodDecl *>baseMethods,
6552 ArrayRef<ObjCProtocolDecl *>baseProtocols,
6553 ArrayRef<ObjCIvarDecl *>ivars,
6554 ArrayRef<ObjCPropertyDecl *>Properties,
6556 StringRef ClassName) {
6557 Result += "\nstatic struct _class_ro_t ";
6558 Result += VarName; Result += ClassName;
6559 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6561 Result += llvm::utostr(flags); Result += ", ";
6562 Result += InstanceStart; Result += ", ";
6563 Result += InstanceSize; Result += ", \n";
6565 const llvm::Triple &Triple(Context->getTargetInfo().getTriple());
6566 if (Triple.getArch() == llvm::Triple::x86_64)
6567 // uint32_t const reserved; // only when building for 64bit targets
6568 Result += "(unsigned int)0, \n\t";
6569 // const uint8_t * const ivarLayout;
6570 Result += "0, \n\t";
6571 Result += "\""; Result += ClassName; Result += "\",\n\t";
6572 bool metaclass = ((flags & CLS_META) != 0);
6573 if (baseMethods.size() > 0) {
6574 Result += "(const struct _method_list_t *)&";
6576 Result += "_OBJC_$_CLASS_METHODS_";
6578 Result += "_OBJC_$_INSTANCE_METHODS_";
6579 Result += ClassName;
6583 Result += "0, \n\t";
6585 if (!metaclass && baseProtocols.size() > 0) {
6586 Result += "(const struct _objc_protocol_list *)&";
6587 Result += "_OBJC_CLASS_PROTOCOLS_$_"; Result += ClassName;
6591 Result += "0, \n\t";
6593 if (!metaclass && ivars.size() > 0) {
6594 Result += "(const struct _ivar_list_t *)&";
6595 Result += "_OBJC_$_INSTANCE_VARIABLES_"; Result += ClassName;
6599 Result += "0, \n\t";
6602 Result += "0, \n\t";
6603 if (!metaclass && Properties.size() > 0) {
6604 Result += "(const struct _prop_list_t *)&";
6605 Result += "_OBJC_$_PROP_LIST_"; Result += ClassName;
6614 static void Write_class_t(ASTContext *Context, std::string &Result,
6616 const ObjCInterfaceDecl *CDecl, bool metaclass) {
6617 bool rootClass = (!CDecl->getSuperClass());
6618 const ObjCInterfaceDecl *RootClass = CDecl;
6621 // Find the Root class
6622 RootClass = CDecl->getSuperClass();
6623 while (RootClass->getSuperClass()) {
6624 RootClass = RootClass->getSuperClass();
6628 if (metaclass && rootClass) {
6629 // Need to handle a case of use of forward declaration.
6631 Result += "extern \"C\" ";
6632 if (CDecl->getImplementation())
6633 Result += "__declspec(dllexport) ";
6635 Result += "__declspec(dllimport) ";
6637 Result += "struct _class_t OBJC_CLASS_$_";
6638 Result += CDecl->getNameAsString();
6641 // Also, for possibility of 'super' metadata class not having been defined yet.
6643 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
6645 Result += "extern \"C\" ";
6646 if (SuperClass->getImplementation())
6647 Result += "__declspec(dllexport) ";
6649 Result += "__declspec(dllimport) ";
6651 Result += "struct _class_t ";
6653 Result += SuperClass->getNameAsString();
6656 if (metaclass && RootClass != SuperClass) {
6657 Result += "extern \"C\" ";
6658 if (RootClass->getImplementation())
6659 Result += "__declspec(dllexport) ";
6661 Result += "__declspec(dllimport) ";
6663 Result += "struct _class_t ";
6665 Result += RootClass->getNameAsString();
6670 Result += "\nextern \"C\" __declspec(dllexport) struct _class_t ";
6671 Result += VarName; Result += CDecl->getNameAsString();
6672 Result += " __attribute__ ((used, section (\"__DATA,__objc_data\"))) = {\n";
6676 Result += "0, // &"; Result += VarName;
6677 Result += RootClass->getNameAsString();
6679 Result += "0, // &"; Result += VarName;
6680 Result += CDecl->getSuperClass()->getNameAsString();
6684 Result += "0, // &"; Result += VarName;
6685 Result += CDecl->getNameAsString();
6687 Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6692 Result += "0, // &OBJC_METACLASS_$_";
6693 Result += CDecl->getNameAsString();
6696 Result += "0, // &"; Result += VarName;
6697 Result += CDecl->getSuperClass()->getNameAsString();
6703 Result += "0, // (void *)&_objc_empty_cache,\n\t";
6704 Result += "0, // unused, was (void *)&_objc_empty_vtable,\n\t";
6706 Result += "&_OBJC_METACLASS_RO_$_";
6708 Result += "&_OBJC_CLASS_RO_$_";
6709 Result += CDecl->getNameAsString();
6710 Result += ",\n};\n";
6712 // Add static function to initialize some of the meta-data fields.
6713 // avoid doing it twice.
6717 const ObjCInterfaceDecl *SuperClass =
6718 rootClass ? CDecl : CDecl->getSuperClass();
6720 Result += "static void OBJC_CLASS_SETUP_$_";
6721 Result += CDecl->getNameAsString();
6722 Result += "(void ) {\n";
6723 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6724 Result += ".isa = "; Result += "&OBJC_METACLASS_$_";
6725 Result += RootClass->getNameAsString(); Result += ";\n";
6727 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6728 Result += ".superclass = ";
6730 Result += "&OBJC_CLASS_$_";
6732 Result += "&OBJC_METACLASS_$_";
6734 Result += SuperClass->getNameAsString(); Result += ";\n";
6736 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6737 Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n";
6739 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6740 Result += ".isa = "; Result += "&OBJC_METACLASS_$_";
6741 Result += CDecl->getNameAsString(); Result += ";\n";
6744 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6745 Result += ".superclass = "; Result += "&OBJC_CLASS_$_";
6746 Result += SuperClass->getNameAsString(); Result += ";\n";
6749 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6750 Result += ".cache = "; Result += "&_objc_empty_cache"; Result += ";\n";
6754 static void Write_category_t(RewriteModernObjC &RewriteObj, ASTContext *Context,
6755 std::string &Result,
6756 ObjCCategoryDecl *CatDecl,
6757 ObjCInterfaceDecl *ClassDecl,
6758 ArrayRef<ObjCMethodDecl *> InstanceMethods,
6759 ArrayRef<ObjCMethodDecl *> ClassMethods,
6760 ArrayRef<ObjCProtocolDecl *> RefedProtocols,
6761 ArrayRef<ObjCPropertyDecl *> ClassProperties) {
6762 StringRef CatName = CatDecl->getName();
6763 StringRef ClassName = ClassDecl->getName();
6764 // must declare an extern class object in case this class is not implemented
6767 Result += "extern \"C\" ";
6768 if (ClassDecl->getImplementation())
6769 Result += "__declspec(dllexport) ";
6771 Result += "__declspec(dllimport) ";
6773 Result += "struct _class_t ";
6774 Result += "OBJC_CLASS_$_"; Result += ClassName;
6777 Result += "\nstatic struct _category_t ";
6778 Result += "_OBJC_$_CATEGORY_";
6779 Result += ClassName; Result += "_$_"; Result += CatName;
6780 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n";
6782 Result += "\t\""; Result += ClassName; Result += "\",\n";
6783 Result += "\t0, // &"; Result += "OBJC_CLASS_$_"; Result += ClassName;
6785 if (InstanceMethods.size() > 0) {
6786 Result += "\t(const struct _method_list_t *)&";
6787 Result += "_OBJC_$_CATEGORY_INSTANCE_METHODS_";
6788 Result += ClassName; Result += "_$_"; Result += CatName;
6794 if (ClassMethods.size() > 0) {
6795 Result += "\t(const struct _method_list_t *)&";
6796 Result += "_OBJC_$_CATEGORY_CLASS_METHODS_";
6797 Result += ClassName; Result += "_$_"; Result += CatName;
6803 if (RefedProtocols.size() > 0) {
6804 Result += "\t(const struct _protocol_list_t *)&";
6805 Result += "_OBJC_CATEGORY_PROTOCOLS_$_";
6806 Result += ClassName; Result += "_$_"; Result += CatName;
6812 if (ClassProperties.size() > 0) {
6813 Result += "\t(const struct _prop_list_t *)&"; Result += "_OBJC_$_PROP_LIST_";
6814 Result += ClassName; Result += "_$_"; Result += CatName;
6822 // Add static function to initialize the class pointer in the category structure.
6823 Result += "static void OBJC_CATEGORY_SETUP_$_";
6824 Result += ClassDecl->getNameAsString();
6827 Result += "(void ) {\n";
6828 Result += "\t_OBJC_$_CATEGORY_";
6829 Result += ClassDecl->getNameAsString();
6832 Result += ".cls = "; Result += "&OBJC_CLASS_$_"; Result += ClassName;
6836 static void Write__extendedMethodTypes_initializer(RewriteModernObjC &RewriteObj,
6837 ASTContext *Context, std::string &Result,
6838 ArrayRef<ObjCMethodDecl *> Methods,
6840 StringRef ProtocolName) {
6841 if (Methods.size() == 0)
6844 Result += "\nstatic const char *";
6845 Result += VarName; Result += ProtocolName;
6846 Result += " [] __attribute__ ((used, section (\"__DATA,__objc_const\"))) = \n";
6848 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
6849 ObjCMethodDecl *MD = Methods[i];
6850 std::string MethodTypeString, QuoteMethodTypeString;
6851 Context->getObjCEncodingForMethodDecl(MD, MethodTypeString, true);
6852 RewriteObj.QuoteDoublequotes(MethodTypeString, QuoteMethodTypeString);
6853 Result += "\t\""; Result += QuoteMethodTypeString; Result += "\"";
6862 static void Write_IvarOffsetVar(RewriteModernObjC &RewriteObj,
6863 ASTContext *Context,
6864 std::string &Result,
6865 ArrayRef<ObjCIvarDecl *> Ivars,
6866 ObjCInterfaceDecl *CDecl) {
6867 // FIXME. visibilty of offset symbols may have to be set; for Darwin
6868 // this is what happens:
6870 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6871 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
6872 Class->getVisibility() == HiddenVisibility)
6873 Visibility shoud be: HiddenVisibility;
6875 Visibility shoud be: DefaultVisibility;
6879 for (unsigned i =0, e = Ivars.size(); i < e; i++) {
6880 ObjCIvarDecl *IvarDecl = Ivars[i];
6881 if (Context->getLangOpts().MicrosoftExt)
6882 Result += "__declspec(allocate(\".objc_ivar$B\")) ";
6884 if (!Context->getLangOpts().MicrosoftExt ||
6885 IvarDecl->getAccessControl() == ObjCIvarDecl::Private ||
6886 IvarDecl->getAccessControl() == ObjCIvarDecl::Package)
6887 Result += "extern \"C\" unsigned long int ";
6889 Result += "extern \"C\" __declspec(dllexport) unsigned long int ";
6890 if (Ivars[i]->isBitField())
6891 RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result);
6893 WriteInternalIvarName(CDecl, IvarDecl, Result);
6894 Result += " __attribute__ ((used, section (\"__DATA,__objc_ivar\")))";
6896 RewriteObj.RewriteIvarOffsetComputation(IvarDecl, Result);
6898 if (Ivars[i]->isBitField()) {
6899 // skip over rest of the ivar bitfields.
6900 SKIP_BITFIELDS(i , e, Ivars);
6905 static void Write__ivar_list_t_initializer(RewriteModernObjC &RewriteObj,
6906 ASTContext *Context, std::string &Result,
6907 ArrayRef<ObjCIvarDecl *> OriginalIvars,
6909 ObjCInterfaceDecl *CDecl) {
6910 if (OriginalIvars.size() > 0) {
6911 Write_IvarOffsetVar(RewriteObj, Context, Result, OriginalIvars, CDecl);
6912 SmallVector<ObjCIvarDecl *, 8> Ivars;
6913 // strip off all but the first ivar bitfield from each group of ivars.
6914 // Such ivars in the ivar list table will be replaced by their grouping struct
6916 for (unsigned i = 0, e = OriginalIvars.size(); i < e; i++) {
6917 if (OriginalIvars[i]->isBitField()) {
6918 Ivars.push_back(OriginalIvars[i]);
6919 // skip over rest of the ivar bitfields.
6920 SKIP_BITFIELDS(i , e, OriginalIvars);
6923 Ivars.push_back(OriginalIvars[i]);
6926 Result += "\nstatic ";
6927 Write__ivar_list_t_TypeDecl(Result, Ivars.size());
6928 Result += " "; Result += VarName;
6929 Result += CDecl->getNameAsString();
6930 Result += " __attribute__ ((used, section (\"__DATA,__objc_const\"))) = {\n";
6931 Result += "\t"; Result += "sizeof(_ivar_t)"; Result += ",\n";
6932 Result += "\t"; Result += utostr(Ivars.size()); Result += ",\n";
6933 for (unsigned i =0, e = Ivars.size(); i < e; i++) {
6934 ObjCIvarDecl *IvarDecl = Ivars[i];
6939 Result += "(unsigned long int *)&";
6940 if (Ivars[i]->isBitField())
6941 RewriteObj.ObjCIvarBitfieldGroupOffset(IvarDecl, Result);
6943 WriteInternalIvarName(CDecl, IvarDecl, Result);
6947 if (Ivars[i]->isBitField())
6948 RewriteObj.ObjCIvarBitfieldGroupDecl(Ivars[i], Result);
6950 Result += IvarDecl->getName();
6953 QualType IVQT = IvarDecl->getType();
6954 if (IvarDecl->isBitField())
6955 IVQT = RewriteObj.GetGroupRecordTypeForObjCIvarBitfield(IvarDecl);
6957 std::string IvarTypeString, QuoteIvarTypeString;
6958 Context->getObjCEncodingForType(IVQT, IvarTypeString,
6960 RewriteObj.QuoteDoublequotes(IvarTypeString, QuoteIvarTypeString);
6961 Result += "\""; Result += QuoteIvarTypeString; Result += "\", ";
6963 // FIXME. this alignment represents the host alignment and need be changed to
6964 // represent the target alignment.
6965 unsigned Align = Context->getTypeAlign(IVQT)/8;
6966 Align = llvm::Log2_32(Align);
6967 Result += llvm::utostr(Align); Result += ", ";
6968 CharUnits Size = Context->getTypeSizeInChars(IVQT);
6969 Result += llvm::utostr(Size.getQuantity());
6979 /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data.
6980 void RewriteModernObjC::RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl,
6981 std::string &Result) {
6983 // Do not synthesize the protocol more than once.
6984 if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl()))
6986 WriteModernMetadataDeclarations(Context, Result);
6988 if (ObjCProtocolDecl *Def = PDecl->getDefinition())
6990 // Must write out all protocol definitions in current qualifier list,
6991 // and in their nested qualifiers before writing out current definition.
6992 for (auto *I : PDecl->protocols())
6993 RewriteObjCProtocolMetaData(I, Result);
6995 // Construct method lists.
6996 std::vector<ObjCMethodDecl *> InstanceMethods, ClassMethods;
6997 std::vector<ObjCMethodDecl *> OptInstanceMethods, OptClassMethods;
6998 for (auto *MD : PDecl->instance_methods()) {
6999 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
7000 OptInstanceMethods.push_back(MD);
7002 InstanceMethods.push_back(MD);
7006 for (auto *MD : PDecl->class_methods()) {
7007 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
7008 OptClassMethods.push_back(MD);
7010 ClassMethods.push_back(MD);
7013 std::vector<ObjCMethodDecl *> AllMethods;
7014 for (unsigned i = 0, e = InstanceMethods.size(); i < e; i++)
7015 AllMethods.push_back(InstanceMethods[i]);
7016 for (unsigned i = 0, e = ClassMethods.size(); i < e; i++)
7017 AllMethods.push_back(ClassMethods[i]);
7018 for (unsigned i = 0, e = OptInstanceMethods.size(); i < e; i++)
7019 AllMethods.push_back(OptInstanceMethods[i]);
7020 for (unsigned i = 0, e = OptClassMethods.size(); i < e; i++)
7021 AllMethods.push_back(OptClassMethods[i]);
7023 Write__extendedMethodTypes_initializer(*this, Context, Result,
7025 "_OBJC_PROTOCOL_METHOD_TYPES_",
7026 PDecl->getNameAsString());
7027 // Protocol's super protocol list
7028 SmallVector<ObjCProtocolDecl *, 8> SuperProtocols(PDecl->protocols());
7029 Write_protocol_list_initializer(Context, Result, SuperProtocols,
7030 "_OBJC_PROTOCOL_REFS_",
7031 PDecl->getNameAsString());
7033 Write_method_list_t_initializer(*this, Context, Result, InstanceMethods,
7034 "_OBJC_PROTOCOL_INSTANCE_METHODS_",
7035 PDecl->getNameAsString(), false);
7037 Write_method_list_t_initializer(*this, Context, Result, ClassMethods,
7038 "_OBJC_PROTOCOL_CLASS_METHODS_",
7039 PDecl->getNameAsString(), false);
7041 Write_method_list_t_initializer(*this, Context, Result, OptInstanceMethods,
7042 "_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_",
7043 PDecl->getNameAsString(), false);
7045 Write_method_list_t_initializer(*this, Context, Result, OptClassMethods,
7046 "_OBJC_PROTOCOL_OPT_CLASS_METHODS_",
7047 PDecl->getNameAsString(), false);
7049 // Protocol's property metadata.
7050 SmallVector<ObjCPropertyDecl *, 8> ProtocolProperties(PDecl->properties());
7051 Write_prop_list_t_initializer(*this, Context, Result, ProtocolProperties,
7052 /* Container */nullptr,
7053 "_OBJC_PROTOCOL_PROPERTIES_",
7054 PDecl->getNameAsString());
7056 // Writer out root metadata for current protocol: struct _protocol_t
7058 if (LangOpts.MicrosoftExt)
7059 Result += "static ";
7060 Result += "struct _protocol_t _OBJC_PROTOCOL_";
7061 Result += PDecl->getNameAsString();
7062 Result += " __attribute__ ((used, section (\"__DATA,__datacoal_nt,coalesced\"))) = {\n";
7063 Result += "\t0,\n"; // id is; is null
7064 Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n";
7065 if (SuperProtocols.size() > 0) {
7066 Result += "\t(const struct _protocol_list_t *)&"; Result += "_OBJC_PROTOCOL_REFS_";
7067 Result += PDecl->getNameAsString(); Result += ",\n";
7071 if (InstanceMethods.size() > 0) {
7072 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_INSTANCE_METHODS_";
7073 Result += PDecl->getNameAsString(); Result += ",\n";
7078 if (ClassMethods.size() > 0) {
7079 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_CLASS_METHODS_";
7080 Result += PDecl->getNameAsString(); Result += ",\n";
7085 if (OptInstanceMethods.size() > 0) {
7086 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_INSTANCE_METHODS_";
7087 Result += PDecl->getNameAsString(); Result += ",\n";
7092 if (OptClassMethods.size() > 0) {
7093 Result += "\t(const struct method_list_t *)&_OBJC_PROTOCOL_OPT_CLASS_METHODS_";
7094 Result += PDecl->getNameAsString(); Result += ",\n";
7099 if (ProtocolProperties.size() > 0) {
7100 Result += "\t(const struct _prop_list_t *)&_OBJC_PROTOCOL_PROPERTIES_";
7101 Result += PDecl->getNameAsString(); Result += ",\n";
7106 Result += "\t"; Result += "sizeof(_protocol_t)"; Result += ",\n";
7109 if (AllMethods.size() > 0) {
7110 Result += "\t(const char **)&"; Result += "_OBJC_PROTOCOL_METHOD_TYPES_";
7111 Result += PDecl->getNameAsString();
7115 Result += "\t0\n};\n";
7117 if (LangOpts.MicrosoftExt)
7118 Result += "static ";
7119 Result += "struct _protocol_t *";
7120 Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString();
7121 Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString();
7124 // Mark this protocol as having been generated.
7125 if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl()))
7126 llvm_unreachable("protocol already synthesized");
7130 void RewriteModernObjC::RewriteObjCProtocolListMetaData(
7131 const ObjCList<ObjCProtocolDecl> &Protocols,
7132 StringRef prefix, StringRef ClassName,
7133 std::string &Result) {
7134 if (Protocols.empty()) return;
7136 for (unsigned i = 0; i != Protocols.size(); i++)
7137 RewriteObjCProtocolMetaData(Protocols[i], Result);
7139 // Output the top lovel protocol meta-data for the class.
7140 /* struct _objc_protocol_list {
7141 struct _objc_protocol_list *next;
7143 struct _objc_protocol *class_protocols[];
7147 if (LangOpts.MicrosoftExt)
7148 Result += "__declspec(allocate(\".cat_cls_meth$B\")) ";
7149 Result += "static struct {\n";
7150 Result += "\tstruct _objc_protocol_list *next;\n";
7151 Result += "\tint protocol_count;\n";
7152 Result += "\tstruct _objc_protocol *class_protocols[";
7153 Result += utostr(Protocols.size());
7154 Result += "];\n} _OBJC_";
7156 Result += "_PROTOCOLS_";
7157 Result += ClassName;
7158 Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= "
7160 Result += utostr(Protocols.size());
7163 Result += "\t,{&_OBJC_PROTOCOL_";
7164 Result += Protocols[0]->getNameAsString();
7167 for (unsigned i = 1; i != Protocols.size(); i++) {
7168 Result += "\t ,&_OBJC_PROTOCOL_";
7169 Result += Protocols[i]->getNameAsString();
7172 Result += "\t }\n};\n";
7175 /// hasObjCExceptionAttribute - Return true if this class or any super
7176 /// class has the __objc_exception__ attribute.
7177 /// FIXME. Move this to ASTContext.cpp as it is also used for IRGen.
7178 static bool hasObjCExceptionAttribute(ASTContext &Context,
7179 const ObjCInterfaceDecl *OID) {
7180 if (OID->hasAttr<ObjCExceptionAttr>())
7182 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
7183 return hasObjCExceptionAttribute(Context, Super);
7187 void RewriteModernObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl,
7188 std::string &Result) {
7189 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
7191 // Explicitly declared @interface's are already synthesized.
7192 if (CDecl->isImplicitInterfaceDecl())
7194 "Legacy implicit interface rewriting not supported in moder abi");
7196 WriteModernMetadataDeclarations(Context, Result);
7197 SmallVector<ObjCIvarDecl *, 8> IVars;
7199 for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
7200 IVD; IVD = IVD->getNextIvar()) {
7201 // Ignore unnamed bit-fields.
7202 if (!IVD->getDeclName())
7204 IVars.push_back(IVD);
7207 Write__ivar_list_t_initializer(*this, Context, Result, IVars,
7208 "_OBJC_$_INSTANCE_VARIABLES_",
7211 // Build _objc_method_list for class's instance methods if needed
7212 SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods());
7214 // If any of our property implementations have associated getters or
7215 // setters, produce metadata for them as well.
7216 for (const auto *Prop : IDecl->property_impls()) {
7217 if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
7219 if (!Prop->getPropertyIvarDecl())
7221 ObjCPropertyDecl *PD = Prop->getPropertyDecl();
7224 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
7225 if (mustSynthesizeSetterGetterMethod(IDecl, PD, true /*getter*/))
7226 InstanceMethods.push_back(Getter);
7227 if (PD->isReadOnly())
7229 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
7230 if (mustSynthesizeSetterGetterMethod(IDecl, PD, false /*setter*/))
7231 InstanceMethods.push_back(Setter);
7234 Write_method_list_t_initializer(*this, Context, Result, InstanceMethods,
7235 "_OBJC_$_INSTANCE_METHODS_",
7236 IDecl->getNameAsString(), true);
7238 SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods());
7240 Write_method_list_t_initializer(*this, Context, Result, ClassMethods,
7241 "_OBJC_$_CLASS_METHODS_",
7242 IDecl->getNameAsString(), true);
7244 // Protocols referenced in class declaration?
7245 // Protocol's super protocol list
7246 std::vector<ObjCProtocolDecl *> RefedProtocols;
7247 const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols();
7248 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
7249 E = Protocols.end();
7251 RefedProtocols.push_back(*I);
7252 // Must write out all protocol definitions in current qualifier list,
7253 // and in their nested qualifiers before writing out current definition.
7254 RewriteObjCProtocolMetaData(*I, Result);
7257 Write_protocol_list_initializer(Context, Result,
7259 "_OBJC_CLASS_PROTOCOLS_$_",
7260 IDecl->getNameAsString());
7262 // Protocol's property metadata.
7263 SmallVector<ObjCPropertyDecl *, 8> ClassProperties(CDecl->properties());
7264 Write_prop_list_t_initializer(*this, Context, Result, ClassProperties,
7265 /* Container */IDecl,
7266 "_OBJC_$_PROP_LIST_",
7267 CDecl->getNameAsString());
7270 // Data for initializing _class_ro_t metaclass meta-data
7271 uint32_t flags = CLS_META;
7272 std::string InstanceSize;
7273 std::string InstanceStart;
7276 bool classIsHidden = CDecl->getVisibility() == HiddenVisibility;
7278 flags |= OBJC2_CLS_HIDDEN;
7280 if (!CDecl->getSuperClass())
7283 InstanceSize = "sizeof(struct _class_t)";
7284 InstanceStart = InstanceSize;
7285 Write__class_ro_t_initializer(Context, Result, flags,
7286 InstanceStart, InstanceSize,
7291 "_OBJC_METACLASS_RO_$_",
7292 CDecl->getNameAsString());
7294 // Data for initializing _class_ro_t meta-data
7297 flags |= OBJC2_CLS_HIDDEN;
7299 if (hasObjCExceptionAttribute(*Context, CDecl))
7300 flags |= CLS_EXCEPTION;
7302 if (!CDecl->getSuperClass())
7306 InstanceSize.clear();
7307 InstanceStart.clear();
7308 if (!ObjCSynthesizedStructs.count(CDecl)) {
7310 InstanceStart = "0";
7313 InstanceSize = "sizeof(struct ";
7314 InstanceSize += CDecl->getNameAsString();
7315 InstanceSize += "_IMPL)";
7317 ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
7319 RewriteIvarOffsetComputation(IVD, InstanceStart);
7322 InstanceStart = InstanceSize;
7324 Write__class_ro_t_initializer(Context, Result, flags,
7325 InstanceStart, InstanceSize,
7330 "_OBJC_CLASS_RO_$_",
7331 CDecl->getNameAsString());
7333 Write_class_t(Context, Result,
7334 "OBJC_METACLASS_$_",
7335 CDecl, /*metaclass*/true);
7337 Write_class_t(Context, Result,
7339 CDecl, /*metaclass*/false);
7341 if (ImplementationIsNonLazy(IDecl))
7342 DefinedNonLazyClasses.push_back(CDecl);
7346 void RewriteModernObjC::RewriteClassSetupInitHook(std::string &Result) {
7347 int ClsDefCount = ClassImplementation.size();
7350 Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n";
7351 Result += "__declspec(allocate(\".objc_inithooks$B\")) ";
7352 Result += "static void *OBJC_CLASS_SETUP[] = {\n";
7353 for (int i = 0; i < ClsDefCount; i++) {
7354 ObjCImplementationDecl *IDecl = ClassImplementation[i];
7355 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
7356 Result += "\t(void *)&OBJC_CLASS_SETUP_$_";
7357 Result += CDecl->getName(); Result += ",\n";
7362 void RewriteModernObjC::RewriteMetaDataIntoBuffer(std::string &Result) {
7363 int ClsDefCount = ClassImplementation.size();
7364 int CatDefCount = CategoryImplementation.size();
7366 // For each implemented class, write out all its meta data.
7367 for (int i = 0; i < ClsDefCount; i++)
7368 RewriteObjCClassMetaData(ClassImplementation[i], Result);
7370 RewriteClassSetupInitHook(Result);
7372 // For each implemented category, write out all its meta data.
7373 for (int i = 0; i < CatDefCount; i++)
7374 RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result);
7376 RewriteCategorySetupInitHook(Result);
7378 if (ClsDefCount > 0) {
7379 if (LangOpts.MicrosoftExt)
7380 Result += "__declspec(allocate(\".objc_classlist$B\")) ";
7381 Result += "static struct _class_t *L_OBJC_LABEL_CLASS_$ [";
7382 Result += llvm::utostr(ClsDefCount); Result += "]";
7384 " __attribute__((used, section (\"__DATA, __objc_classlist,"
7385 "regular,no_dead_strip\")))= {\n";
7386 for (int i = 0; i < ClsDefCount; i++) {
7387 Result += "\t&OBJC_CLASS_$_";
7388 Result += ClassImplementation[i]->getNameAsString();
7393 if (!DefinedNonLazyClasses.empty()) {
7394 if (LangOpts.MicrosoftExt)
7395 Result += "__declspec(allocate(\".objc_nlclslist$B\")) \n";
7396 Result += "static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {\n\t";
7397 for (unsigned i = 0, e = DefinedNonLazyClasses.size(); i < e; i++) {
7398 Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString();
7405 if (CatDefCount > 0) {
7406 if (LangOpts.MicrosoftExt)
7407 Result += "__declspec(allocate(\".objc_catlist$B\")) ";
7408 Result += "static struct _category_t *L_OBJC_LABEL_CATEGORY_$ [";
7409 Result += llvm::utostr(CatDefCount); Result += "]";
7411 " __attribute__((used, section (\"__DATA, __objc_catlist,"
7412 "regular,no_dead_strip\")))= {\n";
7413 for (int i = 0; i < CatDefCount; i++) {
7414 Result += "\t&_OBJC_$_CATEGORY_";
7416 CategoryImplementation[i]->getClassInterface()->getNameAsString();
7418 Result += CategoryImplementation[i]->getNameAsString();
7424 if (!DefinedNonLazyCategories.empty()) {
7425 if (LangOpts.MicrosoftExt)
7426 Result += "__declspec(allocate(\".objc_nlcatlist$B\")) \n";
7427 Result += "static struct _category_t *_OBJC_LABEL_NONLAZY_CATEGORY_$[] = {\n\t";
7428 for (unsigned i = 0, e = DefinedNonLazyCategories.size(); i < e; i++) {
7429 Result += "\t&_OBJC_$_CATEGORY_";
7431 DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString();
7433 Result += DefinedNonLazyCategories[i]->getNameAsString();
7440 void RewriteModernObjC::WriteImageInfo(std::string &Result) {
7441 if (LangOpts.MicrosoftExt)
7442 Result += "__declspec(allocate(\".objc_imageinfo$B\")) \n";
7444 Result += "static struct IMAGE_INFO { unsigned version; unsigned flag; } ";
7445 // version 0, ObjCABI is 2
7446 Result += "_OBJC_IMAGE_INFO = { 0, 2 };\n";
7449 /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
7451 void RewriteModernObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl,
7452 std::string &Result) {
7453 WriteModernMetadataDeclarations(Context, Result);
7454 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
7455 // Find category declaration for this implementation.
7456 ObjCCategoryDecl *CDecl
7457 = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier());
7459 std::string FullCategoryName = ClassDecl->getNameAsString();
7460 FullCategoryName += "_$_";
7461 FullCategoryName += CDecl->getNameAsString();
7463 // Build _objc_method_list for class's instance methods if needed
7464 SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods());
7466 // If any of our property implementations have associated getters or
7467 // setters, produce metadata for them as well.
7468 for (const auto *Prop : IDecl->property_impls()) {
7469 if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
7471 if (!Prop->getPropertyIvarDecl())
7473 ObjCPropertyDecl *PD = Prop->getPropertyDecl();
7476 if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl())
7477 InstanceMethods.push_back(Getter);
7478 if (PD->isReadOnly())
7480 if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl())
7481 InstanceMethods.push_back(Setter);
7484 Write_method_list_t_initializer(*this, Context, Result, InstanceMethods,
7485 "_OBJC_$_CATEGORY_INSTANCE_METHODS_",
7486 FullCategoryName, true);
7488 SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods());
7490 Write_method_list_t_initializer(*this, Context, Result, ClassMethods,
7491 "_OBJC_$_CATEGORY_CLASS_METHODS_",
7492 FullCategoryName, true);
7494 // Protocols referenced in class declaration?
7495 // Protocol's super protocol list
7496 SmallVector<ObjCProtocolDecl *, 8> RefedProtocols(CDecl->protocols());
7497 for (auto *I : CDecl->protocols())
7498 // Must write out all protocol definitions in current qualifier list,
7499 // and in their nested qualifiers before writing out current definition.
7500 RewriteObjCProtocolMetaData(I, Result);
7502 Write_protocol_list_initializer(Context, Result,
7504 "_OBJC_CATEGORY_PROTOCOLS_$_",
7507 // Protocol's property metadata.
7508 SmallVector<ObjCPropertyDecl *, 8> ClassProperties(CDecl->properties());
7509 Write_prop_list_t_initializer(*this, Context, Result, ClassProperties,
7510 /* Container */IDecl,
7511 "_OBJC_$_PROP_LIST_",
7514 Write_category_t(*this, Context, Result,
7522 // Determine if this category is also "non-lazy".
7523 if (ImplementationIsNonLazy(IDecl))
7524 DefinedNonLazyCategories.push_back(CDecl);
7528 void RewriteModernObjC::RewriteCategorySetupInitHook(std::string &Result) {
7529 int CatDefCount = CategoryImplementation.size();
7532 Result += "#pragma section(\".objc_inithooks$B\", long, read, write)\n";
7533 Result += "__declspec(allocate(\".objc_inithooks$B\")) ";
7534 Result += "static void *OBJC_CATEGORY_SETUP[] = {\n";
7535 for (int i = 0; i < CatDefCount; i++) {
7536 ObjCCategoryImplDecl *IDecl = CategoryImplementation[i];
7537 ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl();
7538 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
7539 Result += "\t(void *)&OBJC_CATEGORY_SETUP_$_";
7540 Result += ClassDecl->getName();
7542 Result += CatDecl->getName();
7548 // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
7550 template<typename MethodIterator>
7551 void RewriteModernObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin,
7552 MethodIterator MethodEnd,
7553 bool IsInstanceMethod,
7555 StringRef ClassName,
7556 std::string &Result) {
7557 if (MethodBegin == MethodEnd) return;
7559 if (!objc_impl_method) {
7560 /* struct _objc_method {
7566 Result += "\nstruct _objc_method {\n";
7567 Result += "\tSEL _cmd;\n";
7568 Result += "\tchar *method_types;\n";
7569 Result += "\tvoid *_imp;\n";
7572 objc_impl_method = true;
7575 // Build _objc_method_list for class's methods if needed
7578 struct _objc_method_list *next_method;
7580 struct _objc_method method_list[];
7583 unsigned NumMethods = std::distance(MethodBegin, MethodEnd);
7585 if (LangOpts.MicrosoftExt) {
7586 if (IsInstanceMethod)
7587 Result += "__declspec(allocate(\".inst_meth$B\")) ";
7589 Result += "__declspec(allocate(\".cls_meth$B\")) ";
7591 Result += "static struct {\n";
7592 Result += "\tstruct _objc_method_list *next_method;\n";
7593 Result += "\tint method_count;\n";
7594 Result += "\tstruct _objc_method method_list[";
7595 Result += utostr(NumMethods);
7596 Result += "];\n} _OBJC_";
7598 Result += IsInstanceMethod ? "INSTANCE" : "CLASS";
7599 Result += "_METHODS_";
7600 Result += ClassName;
7601 Result += " __attribute__ ((used, section (\"__OBJC, __";
7602 Result += IsInstanceMethod ? "inst" : "cls";
7603 Result += "_meth\")))= ";
7604 Result += "{\n\t0, " + utostr(NumMethods) + "\n";
7606 Result += "\t,{{(SEL)\"";
7607 Result += (*MethodBegin)->getSelector().getAsString().c_str();
7608 std::string MethodTypeString;
7609 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
7611 Result += MethodTypeString;
7612 Result += "\", (void *)";
7613 Result += MethodInternalNames[*MethodBegin];
7615 for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) {
7616 Result += "\t ,{(SEL)\"";
7617 Result += (*MethodBegin)->getSelector().getAsString().c_str();
7618 std::string MethodTypeString;
7619 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
7621 Result += MethodTypeString;
7622 Result += "\", (void *)";
7623 Result += MethodInternalNames[*MethodBegin];
7626 Result += "\t }\n};\n";
7629 Stmt *RewriteModernObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) {
7630 SourceRange OldRange = IV->getSourceRange();
7631 Expr *BaseExpr = IV->getBase();
7633 // Rewrite the base, but without actually doing replaces.
7635 DisableReplaceStmtScope S(*this);
7636 BaseExpr = cast<Expr>(RewriteFunctionBodyOrGlobalInitializer(BaseExpr));
7637 IV->setBase(BaseExpr);
7640 ObjCIvarDecl *D = IV->getDecl();
7642 Expr *Replacement = IV;
7644 if (BaseExpr->getType()->isObjCObjectPointerType()) {
7645 const ObjCInterfaceType *iFaceDecl =
7646 dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType());
7647 assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null");
7648 // lookup which class implements the instance variable.
7649 ObjCInterfaceDecl *clsDeclared = nullptr;
7650 iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
7652 assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
7654 // Build name of symbol holding ivar offset.
7655 std::string IvarOffsetName;
7656 if (D->isBitField())
7657 ObjCIvarBitfieldGroupOffset(D, IvarOffsetName);
7659 WriteInternalIvarName(clsDeclared, D, IvarOffsetName);
7661 ReferencedIvars[clsDeclared].insert(D);
7663 // cast offset to "char *".
7664 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context,
7665 Context->getPointerType(Context->CharTy),
7668 VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
7669 SourceLocation(), &Context->Idents.get(IvarOffsetName),
7670 Context->UnsignedLongTy, nullptr,
7672 DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, false,
7673 Context->UnsignedLongTy, VK_LValue,
7675 BinaryOperator *addExpr =
7676 new (Context) BinaryOperator(castExpr, DRE, BO_Add,
7677 Context->getPointerType(Context->CharTy),
7678 VK_RValue, OK_Ordinary, SourceLocation(), false);
7679 // Don't forget the parens to enforce the proper binding.
7680 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(),
7683 QualType IvarT = D->getType();
7684 if (D->isBitField())
7685 IvarT = GetGroupRecordTypeForObjCIvarBitfield(D);
7687 if (!isa<TypedefType>(IvarT) && IvarT->isRecordType()) {
7688 RecordDecl *RD = IvarT->getAs<RecordType>()->getDecl();
7689 RD = RD->getDefinition();
7690 if (RD && !RD->getDeclName().getAsIdentifierInfo()) {
7691 // decltype(((Foo_IMPL*)0)->bar) *
7692 ObjCContainerDecl *CDecl =
7693 dyn_cast<ObjCContainerDecl>(D->getDeclContext());
7694 // ivar in class extensions requires special treatment.
7695 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
7696 CDecl = CatDecl->getClassInterface();
7697 std::string RecName = CDecl->getName();
7699 RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl,
7700 SourceLocation(), SourceLocation(),
7701 &Context->Idents.get(RecName.c_str()));
7702 QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD));
7703 unsigned UnsignedIntSize =
7704 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
7705 Expr *Zero = IntegerLiteral::Create(*Context,
7706 llvm::APInt(UnsignedIntSize, 0),
7707 Context->UnsignedIntTy, SourceLocation());
7708 Zero = NoTypeInfoCStyleCastExpr(Context, PtrStructIMPL, CK_BitCast, Zero);
7709 ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(),
7711 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
7713 &Context->Idents.get(D->getNameAsString()),
7715 /*BitWidth=*/nullptr,
7716 /*Mutable=*/true, ICIS_NoInit);
7717 MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(),
7718 FD->getType(), VK_LValue,
7720 IvarT = Context->getDecltypeType(ME, ME->getType());
7723 convertObjCTypeToCStyleType(IvarT);
7724 QualType castT = Context->getPointerType(IvarT);
7726 castExpr = NoTypeInfoCStyleCastExpr(Context,
7732 Expr *Exp = new (Context) UnaryOperator(castExpr, UO_Deref, IvarT,
7733 VK_LValue, OK_Ordinary,
7735 PE = new (Context) ParenExpr(OldRange.getBegin(),
7739 if (D->isBitField()) {
7740 FieldDecl *FD = FieldDecl::Create(*Context, nullptr, SourceLocation(),
7742 &Context->Idents.get(D->getNameAsString()),
7743 D->getType(), nullptr,
7744 /*BitWidth=*/D->getBitWidth(),
7745 /*Mutable=*/true, ICIS_NoInit);
7746 MemberExpr *ME = new (Context) MemberExpr(PE, /*isArrow*/false, FD, SourceLocation(),
7747 FD->getType(), VK_LValue,
7756 ReplaceStmtWithRange(IV, Replacement, OldRange);