Lines Matching +full:push +full:- +full:ci +full:- +full:container

1 //===-- RewriteModernObjC.cpp - Playground for the code rewriter ----------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
112 /* Misc. containers needed for meta-data rewrite. */
120 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
123 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
154 // This container maps an <class, group number for ivar> tuple to the type
193 if (!Class->isThisDeclarationADefinition()) {
204 if (!Proto->isThisDeclarationADefinition()) {
215 if (FDecl->isThisDeclarationADefinition() &&
216 // Not c functions defined inside an objc container.
217 !FDecl->isTopLevelDeclInObjCContainer()) {
230 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
232 else if (TD->getUnderlyingType()->isFunctionPointerType())
233 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
251 ReplaceStmtWithRange(Old, New, Old->getSourceRange());
255 assert(Old != nullptr && New != nullptr && "Expected non-null Stmt's");
266 if (Size == -1) {
267 Diags.Report(Context->getFullLoc(Old->getBeginLoc()), RewriteFailedDiag)
268 << Old->getSourceRange();
274 New->printPretty(S, nullptr, PrintingPolicy(LangOpts));
283 Diags.Report(Context->getFullLoc(Old->getBeginLoc()), RewriteFailedDiag)
284 << Old->getSourceRange();
294 Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag);
304 Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag);
497 // canonical type. We only care if the top-level type is a closure pointer.
502 /// convertBlockPointerToFunctionPointer - Converts a block-pointer type
507 const auto *BPT = T->castAs<BlockPointerType>();
508 T = Context->getPointerType(BPT->getPointeeType());
522 if (T->isObjCQualifiedIdType()) {
524 T = isConst ? Context->getObjCIdType().withConst()
525 : Context->getObjCIdType();
527 else if (T->isObjCQualifiedClassType())
528 T = Context->getObjCClassType();
529 else if (T->isObjCObjectPointerType() &&
530 T->getPointeeType()->isObjCQualifiedInterfaceType()) {
532 T->getAsObjCInterfacePointerType()) {
533 const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
535 T = Context->getPointerType(T);
545 QualType OCT = Context->getCanonicalType(T).getUnqualifiedType();
547 if (OCT == Context->getCanonicalType(Context->getObjCIdType()) ||
548 OCT == Context->getCanonicalType(Context->getObjCClassType()))
551 if (const PointerType *PT = OCT->getAs<PointerType>()) {
552 if (isa<ObjCInterfaceType>(PT->getPointeeType()) ||
553 PT->getPointeeType()->isObjCQualifiedIdType())
576 if (result == Context->getObjCInstanceType())
577 result = Context->getObjCIdType();
580 return Context->getFunctionType(result, args, fpi);
586 TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation());
593 const IdentifierInfo *II = &Context->Idents.get("load");
594 Selector LoadSel = Context->Selectors.getSelector(0, &II);
595 return OD->getClassMethod(LoadSel) != nullptr;
599 QualType StrType = Context->getConstantArrayType(
600 Context->CharTy, llvm::APInt(32, Str.size() + 1), nullptr,
612 for (const auto &I : fproto->param_types())
622 const PointerType *PT = funcType->getAs<PointerType>();
624 RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND);
650 "rewriting sub-expression within a macro (may not be correct)");
658 "rewriter doesn't support user-specified control flow semantics "
673 SM = &Context->getSourceManager();
674 TUDecl = Context->getTranslationUnitDecl();
703 MainFileID = SM->getMainFileID();
704 llvm::MemoryBufferRef MainBuf = SM->getBufferOrFake(MainFileID);
708 Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOpts());
711 //===----------------------------------------------------------------------===//
713 //===----------------------------------------------------------------------===//
722 SourceLocation Loc = D->getLocation();
723 Loc = SM->getExpansionLoc(Loc);
728 // Look for built-in declarations that we need to refer during the rewrite.
733 if (FVD->getName() == "_NSConstantStringClassReference") {
740 if (PD->isThisDeclarationADefinition())
744 for (DeclContext::decl_iterator DI = LSD->decls_begin(),
745 DIEnd = LSD->decls_end();
748 if (!IFace->isThisDeclarationADefinition()) {
750 SourceLocation StartLoc = IFace->getBeginLoc();
753 !cast<ObjCInterfaceDecl>(*DI)->isThisDeclarationADefinition() &&
754 StartLoc == (*DI)->getBeginLoc())
773 if (!Proto->isThisDeclarationADefinition()) {
775 SourceLocation StartLoc = Proto->getBeginLoc();
778 !cast<ObjCProtocolDecl>(*DI)->isThisDeclarationADefinition() &&
779 StartLoc == (*DI)->getBeginLoc())
796 if (SM->isWrittenInMainFile(Loc))
800 //===----------------------------------------------------------------------===//
801 // Syntactic (non-AST) Rewriting Code
802 //===----------------------------------------------------------------------===//
805 SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID);
806 StringRef MainBuf = SM->getBufferData(MainFileID);
822 LocStart.getLocWithOffset(BufPtr-MainBufStart);
833 Result += IDecl->getName();
835 Result += IvarDecl->getName();
840 const ObjCInterfaceDecl *ClassDecl = D->getContainingInterface();
844 if (D->isBitField())
850 QualType IvarT = D->getType();
851 if (D->isBitField())
854 if (!IvarT->getAs<TypedefType>() && IvarT->isRecordType()) {
855 RecordDecl *RD = IvarT->castAs<RecordType>()->getDecl();
856 RD = RD->getDefinition();
857 if (RD && !RD->getDeclName().getAsIdentifierInfo()) {
858 // decltype(((Foo_IMPL*)0)->bar) *
859 auto *CDecl = cast<ObjCContainerDecl>(D->getDeclContext());
862 CDecl = CatDecl->getClassInterface();
863 std::string RecName = std::string(CDecl->getName());
867 &Context->Idents.get(RecName));
868 QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD));
870 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
873 Context->UnsignedIntTy, SourceLocation());
879 &Context->Idents.get(D->getNameAsString()),
884 *Context, PE, true, FD, FD->getType(), VK_LValue, OK_Ordinary);
885 IvarT = Context->getDecltypeType(ME, ME->getType());
889 QualType castT = Context->getPointerType(IvarT);
890 std::string TypeString(castT.getAsString(Context->getPrintingPolicy()));
898 if (D->isBitField()) {
900 S += D->getNameAsString();
906 /// mustSynthesizeSetterGetterMethod - returns true if setter or getter has not
911 auto *OMD = IMP->getInstanceMethod(getter ? PD->getGetterName()
912 : PD->getSetterName());
913 return !OMD || OMD->isSynthesizedAccessorStub();
923 if (PID->getBeginLoc().isValid()) {
924 SourceLocation startLoc = PID->getBeginLoc();
926 const char *startBuf = SM->getCharacterData(startLoc);
930 startGetterSetterLoc = startLoc.getLocWithOffset(semiBuf-startBuf+1);
932 startGetterSetterLoc = IMD ? IMD->getEndLoc() : CID->getEndLoc();
934 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
938 ObjCPropertyDecl *PD = PID->getPropertyDecl();
939 ObjCIvarDecl *OID = PID->getPropertyIvarDecl();
942 unsigned Attributes = PD->getPropertyAttributes();
955 RewriteObjCMethodDecl(OID->getContainingInterface(),
956 PID->getGetterMethodDecl(), Getr);
959 // See objc-act.c:objc_synthesize_new_getter() for details.
964 RewriteTypeIntoString(PID->getGetterMethodDecl()->getReturnType(), Getr,
973 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
976 FT->getParamType(i).getAsString(Context->getPrintingPolicy());
979 if (FT->isVariadic()) {
980 if (FT->getNumParams())
1000 if (PD->isReadOnly() ||
1015 RewriteObjCMethodDecl(OID->getContainingInterface(),
1016 PID->getSetterMethodDecl(), Setr);
1019 // See objc-act.c:objc_synthesize_new_setter() for details.
1024 Setr += PD->getName();
1037 Setr += PD->getName();
1046 typedefString += ForwardDecl->getNameAsString();
1049 typedefString += ForwardDecl->getNameAsString();
1052 typedefString += ForwardDecl->getNameAsString();
1055 typedefString += ForwardDecl->getNameAsString();
1061 SourceLocation startLoc = ClassDecl->getBeginLoc();
1062 const char *startBuf = SM->getCharacterData(startLoc);
1065 ReplaceText(startLoc, semiPtr-startBuf+1, typedefString);
1077 typedefString += ForwardDecl->getNameAsString();
1096 typedefString += ForwardDecl->getNameAsString();
1107 if (Method->isImplicit())
1109 SourceLocation LocStart = Method->getBeginLoc();
1110 SourceLocation LocEnd = Method->getEndLoc();
1112 if (SM->getExpansionLineNumber(LocEnd) >
1113 SM->getExpansionLineNumber(LocStart)) {
1122 SourceLocation Loc = prop->getAtLoc();
1129 SourceLocation LocStart = CatDecl->getBeginLoc();
1132 if (CatDecl->getIvarRBraceLoc().isValid()) {
1134 ReplaceText(CatDecl->getIvarRBraceLoc(), 1, "**/ ");
1140 for (auto *I : CatDecl->instance_properties())
1143 for (auto *I : CatDecl->instance_methods())
1145 for (auto *I : CatDecl->class_methods())
1149 ReplaceText(CatDecl->getAtEndRange().getBegin(),
1154 SourceLocation LocStart = PDecl->getBeginLoc();
1155 assert(PDecl->isThisDeclarationADefinition());
1160 for (auto *I : PDecl->instance_methods())
1162 for (auto *I : PDecl->class_methods())
1164 for (auto *I : PDecl->instance_properties())
1168 SourceLocation LocEnd = PDecl->getAtEndRange().getBegin();
1172 const char *startBuf = SM->getCharacterData(LocStart);
1173 const char *endBuf = SM->getCharacterData(LocEnd);
1176 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1181 SourceLocation OptionalLoc = LocStart.getLocWithOffset(p-startBuf);
1189 SourceLocation LocStart = (*D.begin())->getBeginLoc();
1198 SourceLocation LocStart = DG[0]->getBeginLoc();
1207 if (T->isObjCQualifiedIdType())
1209 else if (T->isFunctionPointerType() ||
1210 T->isBlockPointerType()) {
1211 // needs special handling, since pointer-to-functions have special
1215 if (const PointerType* PT = retType->getAs<PointerType>())
1216 PointeeTy = PT->getPointeeType();
1217 else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>())
1218 PointeeTy = BPT->getPointeeType();
1219 if ((FPRetType = PointeeTy->getAs<FunctionType>())) {
1221 FPRetType->getReturnType().getAsString(Context->getPrintingPolicy());
1225 ResultStr += T.getAsString(Context->getPrintingPolicy());
1234 RewriteTypeIntoString(OMD->getReturnType(), ResultStr, FPRetType);
1240 if (OMD->isInstanceMethod())
1245 NameStr += IDecl->getNameAsString();
1249 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1250 NameStr += CID->getNameAsString();
1255 std::string selString = OMD->getSelector().getAsString();
1270 if (OMD->isInstanceMethod()) {
1271 QualType selfTy = Context->getObjCInterfaceType(IDecl);
1272 selfTy = Context->getPointerType(selfTy);
1278 ResultStr += IDecl->getNameAsString();
1282 ResultStr += Context->getObjCClassType().getAsString(
1283 Context->getPrintingPolicy());
1286 ResultStr += Context->getObjCSelType().getAsString(Context->getPrintingPolicy());
1290 for (const auto *PDecl : OMD->parameters()) {
1292 if (PDecl->getType()->isObjCQualifiedIdType()) {
1294 ResultStr += PDecl->getNameAsString();
1296 std::string Name = PDecl->getNameAsString();
1297 QualType QT = PDecl->getType();
1300 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
1304 if (OMD->isVariadic())
1314 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1317 FT->getParamType(i).getAsString(Context->getPrintingPolicy());
1320 if (FT->isVariadic()) {
1321 if (FT->getNumParams())
1338 if (IMD->getIvarRBraceLoc().isValid()) {
1339 ReplaceText(IMD->getBeginLoc(), 1, "/** ");
1340 ReplaceText(IMD->getIvarRBraceLoc(), 1, "**/ ");
1343 InsertText(IMD->getBeginLoc(), "// ");
1347 InsertText(CID->getBeginLoc(), "// ");
1349 for (auto *OMD : IMD ? IMD->instance_methods() : CID->instance_methods()) {
1350 if (!OMD->getBody())
1353 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1354 SourceLocation LocStart = OMD->getBeginLoc();
1355 SourceLocation LocEnd = OMD->getCompoundBody()->getBeginLoc();
1357 const char *startBuf = SM->getCharacterData(LocStart);
1358 const char *endBuf = SM->getCharacterData(LocEnd);
1359 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1362 for (auto *OMD : IMD ? IMD->class_methods() : CID->class_methods()) {
1363 if (!OMD->getBody())
1366 RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
1367 SourceLocation LocStart = OMD->getBeginLoc();
1368 SourceLocation LocEnd = OMD->getCompoundBody()->getBeginLoc();
1370 const char *startBuf = SM->getCharacterData(LocStart);
1371 const char *endBuf = SM->getCharacterData(LocEnd);
1372 ReplaceText(LocStart, endBuf-startBuf, ResultStr);
1374 for (auto *I : IMD ? IMD->property_impls() : CID->property_impls())
1377 InsertText(IMD ? IMD->getEndLoc() : CID->getEndLoc(), "// ");
1385 ObjCInterfaceDecl *SuperClass = ClassDecl->getSuperClass();
1388 SuperClass = SuperClass->getSuperClass();
1391 if (!ObjCWrittenInterfaces.count(ClassDecl->getCanonicalDecl())) {
1392 // we haven't seen a forward decl - generate a typedef.
1398 ObjCWrittenInterfaces.insert(ClassDecl->getCanonicalDecl());
1400 for (auto *I : ClassDecl->instance_properties())
1402 for (auto *I : ClassDecl->instance_methods())
1404 for (auto *I : ClassDecl->class_methods())
1408 ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"),
1414 SourceRange OldRange = PseudoOp->getSourceRange();
1419 cast<ObjCMessageExpr>(PseudoOp->getSemanticExpr(
1420 PseudoOp->getNumSemanticExprs() - 1));
1423 // we need to suppress rewriting the sub-statements.
1431 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1432 Base = OldMsg->getInstanceReceiver();
1433 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1437 unsigned numArgs = OldMsg->getNumArgs();
1439 Expr *Arg = OldMsg->getArg(i);
1441 Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr();
1449 OldMsg->getSelectorLocs(SelLocs);
1452 switch (OldMsg->getReceiverKind()) {
1454 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1455 OldMsg->getValueKind(),
1456 OldMsg->getLeftLoc(),
1457 OldMsg->getClassReceiverTypeInfo(),
1458 OldMsg->getSelector(),
1460 OldMsg->getMethodDecl(),
1462 OldMsg->getRightLoc(),
1463 OldMsg->isImplicit());
1467 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1468 OldMsg->getValueKind(),
1469 OldMsg->getLeftLoc(),
1471 OldMsg->getSelector(),
1473 OldMsg->getMethodDecl(),
1475 OldMsg->getRightLoc(),
1476 OldMsg->isImplicit());
1481 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1482 OldMsg->getValueKind(),
1483 OldMsg->getLeftLoc(),
1484 OldMsg->getSuperLoc(),
1485 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1486 OldMsg->getSuperType(),
1487 OldMsg->getSelector(),
1489 OldMsg->getMethodDecl(),
1491 OldMsg->getRightLoc(),
1492 OldMsg->isImplicit());
1502 SourceRange OldRange = PseudoOp->getSourceRange();
1507 cast<ObjCMessageExpr>(PseudoOp->getResultExpr()->IgnoreImplicit());
1510 // we need to suppress rewriting the sub-statements.
1516 if (OldMsg->getReceiverKind() == ObjCMessageExpr::Instance) {
1517 Base = OldMsg->getInstanceReceiver();
1518 Base = cast<OpaqueValueExpr>(Base)->getSourceExpr();
1521 unsigned numArgs = OldMsg->getNumArgs();
1523 Expr *Arg = OldMsg->getArg(i);
1525 Arg = cast<OpaqueValueExpr>(Arg)->getSourceExpr();
1535 switch (OldMsg->getReceiverKind()) {
1537 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1538 OldMsg->getValueKind(),
1539 OldMsg->getLeftLoc(),
1540 OldMsg->getClassReceiverTypeInfo(),
1541 OldMsg->getSelector(),
1543 OldMsg->getMethodDecl(),
1545 OldMsg->getRightLoc(),
1546 OldMsg->isImplicit());
1550 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1551 OldMsg->getValueKind(),
1552 OldMsg->getLeftLoc(),
1554 OldMsg->getSelector(),
1556 OldMsg->getMethodDecl(),
1558 OldMsg->getRightLoc(),
1559 OldMsg->isImplicit());
1564 NewMsg = ObjCMessageExpr::Create(*Context, OldMsg->getType(),
1565 OldMsg->getValueKind(),
1566 OldMsg->getLeftLoc(),
1567 OldMsg->getSuperLoc(),
1568 OldMsg->getReceiverKind() == ObjCMessageExpr::SuperInstance,
1569 OldMsg->getSuperType(),
1570 OldMsg->getSelector(),
1572 OldMsg->getMethodDecl(),
1574 OldMsg->getRightLoc(),
1575 OldMsg->isImplicit());
1584 /// SynthCountByEnumWithState - To print:
1604 /// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach
1613 SourceLocation startLoc = S->getBeginLoc();
1626 PresumedLoc PLoc = SM->getPresumedLoc(Loc);
1634 /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
1643 SourceLocation startLoc = S->getBeginLoc();
1651 /// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement.
1685 assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty");
1689 "ObjCForCollectionStmt - Label No stack empty");
1691 SourceLocation startLoc = S->getBeginLoc();
1692 const char *startBuf = SM->getCharacterData(startLoc);
1697 SourceLocation ForEachLoc = S->getForLoc();
1700 if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
1702 NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl());
1703 QualType ElementType = cast<ValueDecl>(D)->getType();
1704 if (ElementType->isObjCQualifiedIdType() ||
1705 ElementType->isObjCQualifiedInterfaceType())
1709 elementTypeAsString = ElementType.getAsString(Context->getPrintingPolicy());
1712 elementName = D->getName();
1717 DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement());
1718 elementName = DR->getDecl()->getName();
1719 ValueDecl *VD = DR->getDecl();
1720 if (VD->getType()->isObjCQualifiedIdType() ||
1721 VD->getType()->isObjCQualifiedInterfaceType())
1725 elementTypeAsString = VD->getType().getAsString(Context->getPrintingPolicy());
1748 ReplaceText(startLoc, startCollectionBuf - startBuf, buf);
1750 SourceLocation rightParenLoc = S->getRParenLoc();
1751 const char *rparenBuf = SM->getCharacterData(rightParenLoc);
1752 SourceLocation lparenLoc = startLoc.getLocWithOffset(rparenBuf-startBuf);
1827 // FIXME: If this should support Obj-C++, support CXXTryStmt
1828 if (isa<CompoundStmt>(S->getBody())) {
1839 const char *stmtBuf = SM->getCharacterData(OrigEnd);
1842 SourceLocation endBodyLoc = OrigEnd.getLocWithOffset(semiBuf-stmtBuf+1);
1857 /// RewriteObjCSynchronizedStmt -
1865 SourceLocation startLoc = S->getBeginLoc();
1866 const char *startBuf = SM->getCharacterData(startLoc);
1871 SourceLocation SynchLoc = S->getAtSynchronizedLoc();
1877 ReplaceText(startLoc, lparenBuf-startBuf+1, buf);
1885 // We can't use S->getSynchExpr()->getEndLoc() to find the end location, since
1888 SourceLocation RParenExprLoc = S->getSynchBody()->getBeginLoc();
1889 const char *RParenExprLocBuf = SM->getCharacterData(RParenExprLoc);
1890 while (*RParenExprLocBuf != ')') RParenExprLocBuf--;
1891 RParenExprLoc = startLoc.getLocWithOffset(RParenExprLocBuf-startBuf);
1893 SourceLocation LBranceLoc = S->getSynchBody()->getBeginLoc();
1894 const char *LBraceLocBuf = SM->getCharacterData(LBranceLoc);
1896 ReplaceText(RParenExprLoc, (LBraceLocBuf - SM->getCharacterData(RParenExprLoc) + 1), buf);
1898 SourceLocation startRBraceLoc = S->getSynchBody()->getEndLoc();
1899 assert((*SM->getCharacterData(startRBraceLoc) == '}') &&
1915 for (Stmt *SubStmt : S->children())
1920 Diags.Report(Context->getFullLoc(S->getBeginLoc()),
1926 SourceLocation startLoc = S->getAtLoc();
1928 ReplaceText(S->getSubStmt()->getBeginLoc(), 1,
1935 ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt();
1936 bool noCatch = S->getNumCatchStmts() == 0;
1938 SourceLocation TryLocation = S->getAtTryLoc();
1949 SourceLocation startLoc = S->getBeginLoc();
1950 const char *startBuf = SM->getCharacterData(startLoc);
1956 // @try -> try
1959 for (ObjCAtCatchStmt *Catch : S->catch_stmts()) {
1960 VarDecl *catchDecl = Catch->getCatchParamDecl();
1962 startLoc = Catch->getBeginLoc();
1965 QualType t = catchDecl->getType();
1967 t->getAs<ObjCObjectPointerType>()) {
1969 ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface();
1972 ConvertSourceLocationToLineDirective(Catch->getBeginLoc(), Result);
1974 startBuf = SM->getCharacterData(startLoc);
1976 SourceLocation rParenLoc = Catch->getRParenLoc();
1977 const char *rParenBuf = SM->getCharacterData(rParenLoc);
1980 Result += "catch (_objc_exc_"; Result += IDecl->getNameAsString();
1981 Result += " *_"; Result += catchDecl->getNameAsString();
1983 ReplaceText(startLoc, rParenBuf-startBuf+1, Result);
1987 Result += IDecl->getNameAsString();
1988 Result += " *"; Result += catchDecl->getNameAsString();
1989 Result += " = ("; Result += IDecl->getNameAsString(); Result += "*)";
1990 Result += "_"; Result += catchDecl->getNameAsString();
1993 SourceLocation lBraceLoc = Catch->getCatchBody()->getBeginLoc();
2000 // @catch -> catch
2006 SourceLocation FinallyLoc = finalStmt->getBeginLoc();
2018 SourceLocation startFinalLoc = finalStmt->getBeginLoc();
2020 Stmt *body = finalStmt->getFinallyBody();
2021 SourceLocation startFinalBodyLoc = body->getBeginLoc();
2026 SourceLocation endFinalBodyLoc = body->getEndLoc();
2029 WarnAboutReturnGotoStmts(S->getTryBody());
2040 SourceLocation startLoc = S->getBeginLoc();
2041 const char *startBuf = SM->getCharacterData(startLoc);
2047 if (S->getThrowExpr())
2055 ReplaceText(startLoc, wBuf-startBuf+1, buf);
2057 SourceLocation endLoc = S->getEndLoc();
2058 const char *endBuf = SM->getCharacterData(endLoc);
2061 SourceLocation semiLoc = startLoc.getLocWithOffset(semiBuf-startBuf);
2062 if (S->getThrowExpr())
2070 Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding);
2085 SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString()));
2099 QualType msgSendType = FD->getType();
2106 QualType pToFunc = Context->getPointerType(msgSendType);
2111 const auto *FT = msgSendType->castAs<FunctionType>();
2113 CallExpr::Create(*Context, ICE, Args, FT->getCallResultType(*Context),
2141 angle--;
2144 assert(angle == 0 && "scanToNextArgument - bad protocol type syntax");
2148 if (T->isObjCQualifiedIdType())
2150 if (const PointerType *PT = T->getAs<PointerType>()) {
2151 if (PT->getPointeeType()->isObjCQualifiedIdType())
2154 if (T->isObjCObjectPointerType()) {
2155 T = T->getPointeeType();
2156 return T->isObjCQualifiedInterfaceType();
2158 if (T->isArrayType()) {
2159 QualType ElemTy = Context->getBaseElementType(T);
2166 QualType Type = E->getType();
2171 Loc = ECE->getLParenLoc();
2172 EndLoc = ECE->getRParenLoc();
2174 Loc = E->getBeginLoc();
2175 EndLoc = E->getEndLoc();
2181 const char *startBuf = SM->getCharacterData(Loc);
2182 const char *endBuf = SM->getCharacterData(EndLoc);
2186 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-startBuf);
2187 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-startBuf+1);
2200 Loc = VD->getLocation();
2201 Type = VD->getType();
2204 Loc = FD->getLocation();
2207 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2212 Type = proto->getReturnType();
2215 Loc = FD->getLocation();
2216 Type = FD->getType();
2219 Loc = TD->getLocation();
2220 Type = TD->getUnderlyingType();
2228 const char *endBuf = SM->getCharacterData(Loc);
2231 startBuf--; // scan backward (from the decl location) for return type.
2235 SourceLocation LessLoc = Loc.getLocWithOffset(startRef-endBuf);
2236 SourceLocation GreaterLoc = Loc.getLocWithOffset(endRef-endBuf+1);
2245 const char *startBuf = SM->getCharacterData(Loc);
2247 for (unsigned i = 0; i < proto->getNumParams(); i++) {
2248 if (needToScanForQualifiers(proto->getParamType(i))) {
2258 Loc.getLocWithOffset(startRef-startFuncBuf);
2260 Loc.getLocWithOffset(endRef-startFuncBuf+1);
2278 QualType QT = ND->getType();
2279 const Type* TypePtr = QT->getAs<Type>();
2284 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
2285 TypePtr = QT->getAs<Type>();
2289 std::string TypeAsString(QT.getAsString(Context->getPrintingPolicy()));
2290 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
2291 const char *startBuf = SM->getCharacterData(DeclLoc);
2292 if (ND->getInit()) {
2293 std::string Name(ND->getNameAsString());
2295 Expr *E = ND->getInit();
2298 startLoc = ECE->getLParenLoc();
2300 startLoc = E->getBeginLoc();
2301 startLoc = SM->getExpansionLoc(startLoc);
2302 const char *endBuf = SM->getCharacterData(startLoc);
2303 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2306 SourceLocation X = ND->getEndLoc();
2307 X = SM->getExpansionLoc(X);
2308 const char *endBuf = SM->getCharacterData(X);
2309 ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString);
2313 // SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str);
2315 IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName");
2317 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2319 getSimpleFunctionType(Context->getObjCSelType(), ArgTys);
2329 if (FD->getIdentifier() &&
2330 FD->getName() == "sel_registerName") {
2338 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2353 QualType Type = VD->getType();
2354 std::string TypeString(Type.getAsString(Context->getPrintingPolicy()));
2365 paren--;
2370 Str += VD->getNameAsString();
2381 SourceLocation FunLocStart = FD->getTypeSpecStartLoc();
2382 const FunctionType *funcType = FD->getType()->getAs<FunctionType>();
2386 QualType Type = proto->getReturnType();
2387 std::string FdStr = Type.getAsString(Context->getPrintingPolicy());
2389 FdStr += FD->getName();
2391 unsigned numArgs = proto->getNumParams();
2393 QualType ArgType = proto->getParamType(i);
2398 if (FD->isVariadic()) {
2406 // SynthSuperConstructorFunctionDecl - id __rw_objc_super(id obj, id super);
2410 IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super");
2412 QualType argT = Context->getObjCIdType();
2416 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2425 // SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...);
2427 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend");
2429 QualType argT = Context->getObjCIdType();
2432 argT = Context->getObjCSelType();
2435 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2444 // SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(void);
2446 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
2448 ArgTys.push_back(Context->VoidTy);
2449 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2458 // SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...);
2460 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret");
2462 QualType argT = Context->getObjCIdType();
2465 argT = Context->getObjCSelType();
2468 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2477 // SynthMsgSendSuperStretFunctionDecl -
2481 &Context->Idents.get("objc_msgSendSuper_stret");
2483 ArgTys.push_back(Context->VoidTy);
2484 QualType msgSendType = getSimpleFunctionType(Context->getObjCIdType(),
2494 // SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...);
2496 IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret");
2498 QualType argT = Context->getObjCIdType();
2501 argT = Context->getObjCSelType();
2504 QualType msgSendType = getSimpleFunctionType(Context->DoubleTy,
2513 // SynthGetClassFunctionDecl - Class objc_getClass(const char *name);
2515 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass");
2517 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2518 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2527 // SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls);
2530 &Context->Idents.get("class_getSuperclass");
2532 ArgTys.push_back(Context->getObjCClassType());
2533 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2543 // SynthGetMetaClassFunctionDecl - Class objc_getMetaClass(const char *name);
2545 IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass");
2547 ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst()));
2548 QualType getClassType = getSimpleFunctionType(Context->getObjCClassType(),
2558 assert (Exp != nullptr && "Expected non-null ObjCStringLiteral");
2567 // replace any non-alphanumeric characters with '_'.
2581 Exp->getString()->printPretty(prettyBuf, nullptr, PrintingPolicy(LangOpts));
2584 Preamble += utostr(Exp->getString()->getByteLength()) + "};\n";
2587 SourceLocation(), &Context->Idents.get(S),
2593 Context->getPointerType(DRE->getType()), VK_PRValue, OK_Ordinary,
2596 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(),
2605 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
2608 llvm::APInt(IntSize, Exp->getValue()),
2609 Context->IntTy, Exp->getLocation());
2610 CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Context->ObjCBuiltinBoolTy,
2612 ParenExpr *PE = new (Context) ParenExpr(Exp->getLocation(), Exp->getExprLoc(),
2629 SourceLocation StartLoc = Exp->getBeginLoc();
2630 SourceLocation EndLoc = Exp->getEndLoc();
2637 ObjCMethodDecl *BoxingMethod = Exp->getBoxingMethod();
2638 ObjCInterfaceDecl *BoxingClass = BoxingMethod->getClassInterface();
2640 IdentifierInfo *clsName = BoxingClass->getIdentifier();
2641 ClsExprs.push_back(getStringLiteral(clsName->getName()));
2650 getStringLiteral(BoxingMethod->getSelector().getAsString()));
2655 // User provided sub-expression is the 3rd, and last, argument.
2656 Expr *subExpr = Exp->getSubExpr();
2658 QualType type = ICE->getType();
2659 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
2661 if (SubExpr->getType()->isIntegralType(*Context) && type->isBooleanType())
2668 ArgTypes.push_back(Context->getObjCClassType());
2669 ArgTypes.push_back(Context->getObjCSelType());
2670 for (const auto PI : BoxingMethod->parameters())
2671 ArgTypes.push_back(PI->getType());
2673 QualType returnType = Exp->getType();
2675 QualType msgSendType = MsgSendFlavor->getType();
2682 Context, Context->getPointerType(Context->VoidTy), CK_BitCast, DRE);
2686 getSimpleFunctionType(returnType, ArgTypes, BoxingMethod->isVariadic());
2687 castType = Context->getPointerType(castType);
2694 auto *FT = msgSendType->castAs<FunctionType>();
2695 CallExpr *CE = CallExpr::Create(*Context, PE, MsgExprs, FT->getReturnType(),
2712 SourceLocation StartLoc = Exp->getBeginLoc();
2713 SourceLocation EndLoc = Exp->getEndLoc();
2716 QualType IntQT = Context->IntTy;
2718 getSimpleFunctionType(Context->VoidTy, IntQT, true);
2725 unsigned NumElements = Exp->getNumElements();
2727 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
2730 Context->UnsignedIntTy, SourceLocation());
2733 InitExprs.push_back(Exp->getElement(i));
2740 &Context->Idents.get("arr"),
2741 Context->getPointerType(Context->VoidPtrTy),
2746 ARRFD->getType(), VK_LValue, OK_Ordinary);
2747 QualType ConstIdT = Context->getObjCIdType().withConst();
2750 Context->getPointerType(ConstIdT),
2757 QualType expType = Exp->getType();
2761 expType->getPointeeType()->castAs<ObjCObjectType>()->getInterface();
2763 IdentifierInfo *clsName = Class->getIdentifier();
2764 ClsExprs.push_back(getStringLiteral(clsName->getName()));
2772 ObjCMethodDecl *ArrayMethod = Exp->getArrayWithObjectsMethod();
2774 getStringLiteral(ArrayMethod->getSelector().getAsString()));
2785 Context->UnsignedIntTy, SourceLocation());
2789 ArgTypes.push_back(Context->getObjCClassType());
2790 ArgTypes.push_back(Context->getObjCSelType());
2791 for (const auto *PI : ArrayMethod->parameters())
2792 ArgTypes.push_back(PI->getType());
2794 QualType returnType = Exp->getType();
2796 QualType msgSendType = MsgSendFlavor->getType();
2803 Context, Context->getPointerType(Context->VoidTy), CK_BitCast, DRE);
2807 getSimpleFunctionType(returnType, ArgTypes, ArrayMethod->isVariadic());
2808 castType = Context->getPointerType(castType);
2815 const FunctionType *FT = msgSendType->castAs<FunctionType>();
2816 CallExpr *CE = CallExpr::Create(*Context, PE, MsgExprs, FT->getReturnType(),
2833 SourceLocation StartLoc = Exp->getBeginLoc();
2834 SourceLocation EndLoc = Exp->getEndLoc();
2837 QualType IntQT = Context->IntTy;
2839 getSimpleFunctionType(Context->VoidTy, IntQT, true);
2848 unsigned NumElements = Exp->getNumElements();
2850 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
2853 Context->UnsignedIntTy, SourceLocation());
2857 ObjCDictionaryElement Element = Exp->getKeyValueElement(i);
2869 &Context->Idents.get("arr"),
2870 Context->getPointerType(Context->VoidPtrTy),
2875 ARRFD->getType(), VK_LValue, OK_Ordinary);
2876 QualType ConstIdT = Context->getObjCIdType().withConst();
2879 Context->getPointerType(ConstIdT),
2889 ARRFD->getType(), VK_LValue, OK_Ordinary);
2893 Context->getPointerType(ConstIdT),
2900 QualType expType = Exp->getType();
2904 expType->getPointeeType()->castAs<ObjCObjectType>()->getInterface();
2906 IdentifierInfo *clsName = Class->getIdentifier();
2907 ClsExprs.push_back(getStringLiteral(clsName->getName()));
2915 ObjCMethodDecl *DictMethod = Exp->getDictWithObjectsMethod();
2916 SelExprs.push_back(getStringLiteral(DictMethod->getSelector().getAsString()));
2930 Context->UnsignedIntTy, SourceLocation());
2934 ArgTypes.push_back(Context->getObjCClassType());
2935 ArgTypes.push_back(Context->getObjCSelType());
2936 for (const auto *PI : DictMethod->parameters()) {
2937 QualType T = PI->getType();
2938 if (const PointerType* PT = T->getAs<PointerType>()) {
2939 QualType PointeeTy = PT->getPointeeType();
2941 T = Context->getPointerType(PointeeTy);
2946 QualType returnType = Exp->getType();
2948 QualType msgSendType = MsgSendFlavor->getType();
2955 Context, Context->getPointerType(Context->VoidTy), CK_BitCast, DRE);
2959 getSimpleFunctionType(returnType, ArgTypes, DictMethod->isVariadic());
2960 castType = Context->getPointerType(castType);
2967 const FunctionType *FT = msgSendType->castAs<FunctionType>();
2968 CallExpr *CE = CallExpr::Create(*Context, PE, MsgExprs, FT->getReturnType(),
2981 SourceLocation(), &Context->Idents.get("__rw_objc_super"));
2985 FieldTypes[0] = Context->getObjCIdType();
2987 FieldTypes[1] = Context->getObjCIdType();
2991 SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl,
3000 SuperStructDecl->completeDefinition();
3002 return Context->getTagDeclType(SuperStructDecl);
3009 SourceLocation(), &Context->Idents.get("__NSConstantStringImpl"));
3013 FieldTypes[0] = Context->getObjCIdType();
3015 FieldTypes[1] = Context->IntTy;
3017 FieldTypes[2] = Context->getPointerType(Context->CharTy);
3019 FieldTypes[3] = Context->LongTy;
3023 ConstantStringDecl->addDecl(FieldDecl::Create(*Context,
3033 ConstantStringDecl->completeDefinition();
3035 return Context->getTagDeclType(ConstantStringDecl);
3038 /// getFunctionSourceLocation - returns start location of a function
3043 if (FD->isExternC() && !FD->isMain()) {
3044 const DeclContext *DC = FD->getDeclContext();
3047 if (!LSD->getRBraceLoc().isValid())
3048 return LSD->getExternLoc();
3050 if (FD->getStorageClass() != SC_None)
3052 return FD->getTypeSpecStartLoc();
3057 SourceLocation Location = D->getLocation();
3061 PresumedLoc PLoc = SM->getPresumedLoc(Location);
3069 Location = D->getBeginLoc();
3071 if (FD->isExternC() && !FD->isMain()) {
3072 const DeclContext *DC = FD->getDeclContext();
3075 if (!LSD->getRBraceLoc().isValid())
3076 Location = LSD->getExternLoc();
3083 /// SynthMsgSendStretCallExpr - This routine translates message expression
3086 /// MsgSendStretFlavor - function declaration objc_msgSend_stret(...)
3087 /// msgSendType - function type of objc_msgSend_stret(...)
3088 /// returnType - Result type of the method being synthesized.
3089 /// ArgTypes - type of the arguments passed to objc_msgSend_stret, starting with receiver type.
3090 /// MsgExprs - list of argument expressions being passed to objc_msgSend_stret,
3092 /// Method - Method being rewritten.
3100 returnType, ArgTypes, Method ? Method->isVariadic() : false);
3101 QualType castType = Context->getPointerType(FuncType);
3115 ArgTypes[i].getAsStringInternal(ArgName, Context->getPrintingPolicy());
3121 MsgExprs[i]->getType().getAsStringInternal(ArgName,
3122 Context->getPrintingPolicy());
3128 str += returnType.getAsString(Context->getPrintingPolicy()); str += ");\n";
3132 str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy());
3147 str += "\t s = (("; str += castType.getAsString(Context->getPrintingPolicy());
3159 str += "\t"; str += returnType.getAsString(Context->getPrintingPolicy());
3166 assert(CurMethodDef && "SynthMsgSendStretCallExpr - CurMethodDef is null");
3167 FunLocStart = CurMethodDef->getBeginLoc();
3174 IdentifierInfo *ID = &Context->Idents.get(name);
3186 &Context->Idents.get("s"),
3191 *Context, STCE, false, FieldD, FieldD->getType(), VK_LValue, OK_Ordinary);
3222 if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) {
3223 QualType resultType = mDecl->getReturnType();
3224 if (resultType->isRecordType())
3226 else if (resultType->isRealFloatingType())
3232 switch (Exp->getReceiverKind()) {
3239 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
3245 Context, Context->getObjCIdType(), CK_BitCast,
3246 new (Context) DeclRefExpr(*Context, CurMethodDef->getSelfDecl(), false,
3247 Context->getObjCIdType(), VK_PRValue,
3252 ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName()));
3262 // To turn off a warning, type-cast to 'id'
3265 Context->getObjCIdType(),
3288 Context->getPointerType(SuperRep->getType()), VK_PRValue, OK_Ordinary,
3291 Context->getPointerType(superType),
3299 = Context->getTrivialTypeSourceInfo(superType);
3306 Context->getPointerType(SuperRep->getType()), VK_PRValue, OK_Ordinary,
3316 = Exp->getClassReceiver()->castAs<ObjCObjectType>()->getInterface();
3317 IdentifierInfo *clsName = Class->getIdentifier();
3318 ClsExprs.push_back(getStringLiteral(clsName->getName()));
3322 Context->getObjCIdType(),
3333 ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface();
3337 Context, Context->getObjCIdType(), CK_BitCast,
3338 new (Context) DeclRefExpr(*Context, CurMethodDef->getSelfDecl(), false,
3339 Context->getObjCIdType(), VK_PRValue,
3344 ClsExprs.push_back(getStringLiteral(ClassDecl->getIdentifier()->getName()));
3354 // To turn off a warning, type-cast to 'id'
3357 NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3380 Context->getPointerType(SuperRep->getType()), VK_PRValue, OK_Ordinary,
3383 Context->getPointerType(superType),
3391 = Context->getTrivialTypeSourceInfo(superType);
3400 // Remove all type-casts because it may contain objc-style types; e.g.
3402 Expr *recExpr = Exp->getInstanceReceiver();
3404 recExpr = CE->getSubExpr();
3405 CastKind CK = recExpr->getType()->isObjCObjectPointerType()
3406 ? CK_BitCast : recExpr->getType()->isBlockPointerType()
3410 recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3419 SelExprs.push_back(getStringLiteral(Exp->getSelector().getAsString()));
3424 // Now push any user supplied arguments.
3425 for (unsigned i = 0; i < Exp->getNumArgs(); i++) {
3426 Expr *userExpr = Exp->getArg(i);
3427 // Make all implicit casts explicit...ICE comes in handy:-)
3430 QualType type = ICE->getType();
3432 type = Context->getObjCIdType();
3435 const Expr *SubExpr = ICE->IgnoreParenImpCasts();
3437 if (SubExpr->getType()->isIntegralType(*Context) &&
3438 type->isBooleanType()) {
3440 } else if (type->isObjCObjectPointerType()) {
3441 if (SubExpr->getType()->isBlockPointerType()) {
3443 } else if (SubExpr->getType()->isPointerType()) {
3456 if (CE->getType()->isObjCQualifiedIdType()) {
3458 userExpr = CE->getSubExpr();
3460 if (userExpr->getType()->isIntegralType(*Context)) {
3462 } else if (userExpr->getType()->isBlockPointerType()) {
3464 } else if (userExpr->getType()->isPointerType()) {
3469 userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(),
3477 //Exp->setArg(i, 0);
3484 // Push 'id' and 'SEL', the 2 implicit arguments.
3486 ArgTypes.push_back(Context->getPointerType(getSuperStructType()));
3488 ArgTypes.push_back(Context->getObjCIdType());
3489 ArgTypes.push_back(Context->getObjCSelType());
3490 if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) {
3491 // Push any user argument types.
3492 for (const auto *PI : OMD->parameters()) {
3493 QualType t = PI->getType()->isObjCQualifiedIdType()
3494 ? Context->getObjCIdType()
3495 : PI->getType();
3500 returnType = Exp->getType();
3504 returnType = Context->getObjCIdType();
3507 QualType msgSendType = MsgSendFlavor->getType();
3515 // xx.m:13: warning: function called through a non-compatible type
3518 Context->getPointerType(Context->VoidTy),
3523 const ObjCMethodDecl *MD = Exp->getMethodDecl();
3525 getSimpleFunctionType(returnType, ArgTypes, MD ? MD->isVariadic() : true);
3526 castType = Context->getPointerType(castType);
3533 const FunctionType *FT = msgSendType->castAs<FunctionType>();
3534 CallExpr *CE = CallExpr::Create(*Context, PE, MsgExprs, FT->getReturnType(),
3546 Exp->getMethodDecl());
3555 SynthMessageExpr(Exp, Exp->getBeginLoc(), Exp->getEndLoc());
3568 = Context->getTrivialTypeSourceInfo(Context->getObjCIdType());
3571 &Context->Idents.get("Protocol"),
3574 return Context->getTypeDeclType(ProtocolTypeDecl);
3577 /// RewriteObjCProtocolExpr - Rewrite a protocol expression into
3583 Exp->getProtocol()->getNameAsString();
3584 IdentifierInfo *ID = &Context->Idents.get(Name);
3591 Context, Context->getPointerType(DRE->getType()), CK_BitCast, DRE);
3593 ProtocolExprDecls.insert(Exp->getProtocol()->getCanonicalDecl());
3598 /// IsTagDefinedInsideClass - This routine checks that a named tagged type
3599 /// is defined inside an objective-c class. If so, it returns true.
3607 RD = RD->getDefinition();
3608 if (!RD || !RD->getDeclName().getAsIdentifierInfo())
3611 TagLocation = RD->getLocation();
3612 return Context->getSourceManager().isBeforeInTranslationUnit(
3613 IDecl->getLocation(), TagLocation);
3616 if (!ED || !ED->getDeclName().getAsIdentifierInfo())
3619 TagLocation = ED->getLocation();
3620 return Context->getSourceManager().isBeforeInTranslationUnit(
3621 IDecl->getLocation(), TagLocation);
3626 /// RewriteObjCFieldDeclType - This routine rewrites a type into the buffer.
3630 if (Type->getAs<TypedefType>()) {
3635 if (Type->isArrayType()) {
3636 QualType ElemTy = Context->getBaseElementType(Type);
3639 else if (Type->isRecordType()) {
3640 RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
3641 if (RD->isCompleteDefinition()) {
3642 if (RD->isStruct())
3644 else if (RD->isUnion())
3649 Result += RD->getName();
3656 for (auto *FD : RD->fields())
3662 else if (Type->isEnumeralType()) {
3663 EnumDecl *ED = Type->castAs<EnumType>()->getDecl();
3664 if (ED->isCompleteDefinition()) {
3666 Result += ED->getName();
3674 for (const auto *EC : ED->enumerators()) {
3675 Result += "\t"; Result += EC->getName(); Result += " = ";
3676 Result += toString(EC->getInitVal(), 10);
3690 /// RewriteObjCFieldDecl - This routine rewrites a field into the buffer.
3694 QualType Type = fieldDecl->getType();
3695 std::string Name = fieldDecl->getNameAsString();
3699 Type.getAsStringInternal(Name, Context->getPrintingPolicy());
3701 if (fieldDecl->isBitField()) {
3703 Result += utostr(fieldDecl->getBitWidthValue());
3705 else if (EleboratedType && Type->isArrayType()) {
3706 const ArrayType *AT = Context->getAsArrayType(Type);
3710 llvm::APInt Dim = CAT->getSize();
3714 AT = Context->getAsArrayType(AT->getElementType());
3721 /// RewriteLocallyDefinedNamedAggregates - This routine rewrites locally defined
3725 QualType Type = fieldDecl->getType();
3726 if (Type->getAs<TypedefType>())
3728 if (Type->isArrayType())
3729 Type = Context->getBaseElementType(Type);
3731 auto *IDecl = dyn_cast<ObjCContainerDecl>(fieldDecl->getDeclContext());
3734 if (Type->isRecordType()) {
3735 TD = Type->castAs<RecordType>()->getDecl();
3737 else if (Type->isEnumeralType()) {
3738 TD = Type->castAs<EnumType>()->getDecl();
3756 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3762 for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3763 IVD; IVD = IVD->getNextIvar())
3767 if (IVars[i]->isBitField()) {
3769 while (i < e && IVars[i]->isBitField())
3772 --i;
3785 *Context, TagTypeKind::Struct, Context->getTranslationUnitDecl(),
3786 SourceLocation(), SourceLocation(), &Context->Idents.get(StructTagName));
3789 RD->addDecl(FieldDecl::Create(*Context, RD, SourceLocation(), SourceLocation(),
3790 &Context->Idents.get(Ivar->getName()),
3791 Ivar->getType(),
3792 nullptr, /*Expr *BW */Ivar->getBitWidth(),
3795 RD->completeDefinition();
3796 return Context->getTagDeclType(RD);
3800 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3804 return It->second;
3807 for (const ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3808 IVD; IVD = IVD->getNextIvar()) {
3809 if (IVD->isBitField())
3833 /// ObjCIvarBitfieldGroupDecl - Names field decl. for ivar bitfield group.
3837 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3838 Result += CDecl->getName();
3844 /// ObjCIvarBitfieldGroupType - Names struct type for ivar bitfield group.
3849 const ObjCInterfaceDecl *CDecl = IV->getContainingInterface();
3850 Result += CDecl->getName();
3856 /// ObjCIvarBitfieldGroupOffset - Names symbol for ivar bitfield group field offset.
3866 while ((IX < ENDIX) && VEC[IX]->isBitField()) \
3869 --IX; \
3872 /// RewriteObjCInternalStruct - Rewrite one internal struct corresponding to
3873 /// an objective-c class with ivars.
3877 assert(CDecl->getName() != "" &&
3879 ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass();
3881 for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
3882 IVD; IVD = IVD->getNextIvar())
3885 SourceLocation LocStart = CDecl->getBeginLoc();
3886 SourceLocation LocEnd = CDecl->getEndOfDefinitionLoc();
3888 const char *startBuf = SM->getCharacterData(LocStart);
3889 const char *endBuf = SM->getCharacterData(LocEnd);
3893 if ((!CDecl->isThisDeclarationADefinition() || IVars.size() == 0) &&
3896 ReplaceText(LocStart, endBuf-startBuf, Result);
3902 // struct/unions in objective-c classes.
3909 if (IVars[i]->isBitField()) {
3919 Result += CDecl->getNameAsString();
3923 Result += "\tstruct "; Result += RCDecl->getNameAsString();
3924 Result += "_IMPL "; Result += RCDecl->getNameAsString();
3929 if (IVars[i]->isBitField()) {
3943 ReplaceText(LocStart, endBuf-startBuf, Result);
3946 llvm_unreachable("struct already synthesize- RewriteObjCInternalStruct");
3949 /// RewriteIvarOffsetSymbols - Rewrite ivar offset symbols of those ivars which
3962 const ObjCInterfaceDecl *IDecl = IvarDecl->getContainingInterface();
3964 if (IvarDecl->isBitField()) {
3974 IvarDecl->getAccessControl() != ObjCIvarDecl::Private &&
3975 IvarDecl->getAccessControl() != ObjCIvarDecl::Package)
3979 if (IvarDecl->isBitField()) {
3989 //===----------------------------------------------------------------------===//
3991 //===----------------------------------------------------------------------===//
3993 /// RewriteImplementations - This routine rewrites all method implementations
3994 /// and emits meta-data.
4003 ObjCInterfaceDecl *CDecl = OIMP->getClassInterface();
4004 if (CDecl->isImplicitInterfaceDecl())
4012 ObjCInterfaceDecl *CDecl = CIMP->getClassInterface();
4013 if (CDecl->isImplicitInterfaceDecl())
4033 return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage());
4040 const FunctionType *AFT = CE->getFunctionType();
4041 QualType RT = AFT->getReturnType();
4043 SourceLocation BlockLoc = CE->getExprLoc();
4047 S += "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" +
4050 BlockDecl *BD = CE->getBlockDecl();
4053 // No user-supplied arguments. Still need to pass in a pointer to the
4056 } else if (BD->param_empty()) {
4065 for (BlockDecl::param_iterator AI = BD->param_begin(),
4066 E = BD->param_end(); AI != E; ++AI) {
4067 if (AI != BD->param_begin()) S += ", ";
4068 ParamStr = (*AI)->getNameAsString();
4069 QualType QT = (*AI)->getType();
4071 QT.getAsStringInternal(ParamStr, Context->getPrintingPolicy());
4074 if (FT->isVariadic()) {
4075 if (!BD->param_empty()) S += ", ";
4086 std::string Name = VD->getNameAsString();
4091 S += Name + " = __cself->" + VD->getNameAsString() + "; // bound by ref\n";
4106 if (isTopLevelBlockPointerType(VD->getType())) {
4109 RewriteBlockPointerType(S, VD->getType());
4111 S += "__cself->" + VD->getNameAsString() + "; // bound by copy\n";
4113 std::string Name = VD->getNameAsString();
4114 QualType QT = VD->getType();
4116 QT = Context->getPointerType(QT);
4117 QT.getAsStringInternal(Name, Context->getPrintingPolicy());
4118 S += Name + " = __cself->" + VD->getNameAsString() +
4141 S += "_Block_object_assign((void*)&dst->";
4142 S += VD->getNameAsString();
4143 S += ", (void*)src->";
4144 S += VD->getNameAsString();
4147 else if (VD->getType()->isBlockPointerType())
4160 S += "_Block_object_dispose((void*)src->";
4161 S += VD->getNameAsString();
4164 else if (VD->getType()->isBlockPointerType())
4191 std::string FieldName = VD->getNameAsString();
4203 if (isTopLevelBlockPointerType(VD->getType())) {
4207 QualType QT = VD->getType();
4209 QT = Context->getPointerType(QT);
4210 QT.getAsStringInternal(FieldName, Context->getPrintingPolicy());
4211 QT.getAsStringInternal(ArgName, Context->getPrintingPolicy());
4219 std::string FieldName = VD->getNameAsString();
4236 std::string Name = VD->getNameAsString();
4242 if (isTopLevelBlockPointerType(VD->getType()))
4249 std::string Name = VD->getNameAsString();
4256 Constructor += Name + "(_" + Name + "->__forwarding)";
4315 GlobalVarDecl->getStorageClass() == SC_Static &&
4316 GlobalVarDecl->getType().getCVRQualifiers());
4319 SC += GlobalVarDecl->getNameAsString();
4327 // Need to copy-in the inner copied-in variables not actually used in this
4331 ValueDecl *VD = Exp->getDecl();
4333 if (!VD->hasAttr<BlocksAttr>()) {
4342 if (VD->getType()->isObjCObjectPointerType() ||
4343 VD->getType()->isBlockPointerType())
4350 std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag);
4352 InsertText(FunLocStart, CI);
4375 if (GlobalVarDecl->getStorageClass() == SC_Static)
4377 if (GlobalVarDecl->getType().isConstQualified())
4379 if (GlobalVarDecl->getType().isVolatileQualified())
4381 if (GlobalVarDecl->getType().isRestrictQualified())
4392 Tag += utostr(Blocks.size()-1);
4398 GlobalConstructionExp->printPretty(constructorExprBuf, nullptr,
4415 : FD->getTypeSpecStartLoc();
4416 StringRef FuncName = FD->getName();
4423 ObjCInterfaceDecl *IFace = MD->getClassInterface();
4424 Name = std::string(IFace->getName());
4425 Name += "__" + MD->getSelector().getAsString();
4434 // SourceLocation FunLocStart = MD->getBeginLoc();
4435 SourceLocation FunLocStart = MD->getBeginLoc();
4442 for (Stmt *SubStmt : S->children())
4445 GetBlockDeclRefExprs(CBE->getBody());
4451 if (DRE->refersToEnclosingVariableOrCapture() ||
4452 HasLocalVariableExternalStorage(DRE->getDecl()))
4460 for (Stmt *SubStmt : S->children())
4463 InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl()));
4464 GetInnerBlockDeclRefExprs(CBE->getBody(),
4473 if (DRE->refersToEnclosingVariableOrCapture() ||
4474 HasLocalVariableExternalStorage(DRE->getDecl())) {
4475 if (!InnerContexts.count(DRE->getDecl()->getDeclContext()))
4477 if (VarDecl *Var = cast<VarDecl>(DRE->getDecl()))
4478 if (Var->isFunctionOrMethodVarDecl())
4484 /// convertObjCTypeToCStyleType - This routine converts such objc types
4490 if (T->isFunctionPointerType()) {
4492 if (const PointerType* PT = T->getAs<PointerType>()) {
4493 PointeeTy = PT->getPointeeType();
4494 if (const FunctionType *FT = PointeeTy->getAs<FunctionType>()) {
4496 T = Context->getPointerType(T);
4505 /// convertFunctionTypeOfBlocks - This routine converts a function type
4514 QualType Res = FT->getReturnType();
4518 for (auto &I : FTP->param_types()) {
4538 CPT = DRE->getType()->getAs<BlockPointerType>();
4540 CPT = MExpr->getType()->getAs<BlockPointerType>();
4543 return SynthesizeBlockCall(Exp, PRE->getSubExpr());
4546 CPT = IEXPR->getType()->getAs<BlockPointerType>();
4549 Expr *LHSExp = CEXPR->getLHS();
4551 Expr *RHSExp = CEXPR->getRHS();
4553 Expr *CONDExp = CEXPR->getCond();
4556 cast<Expr>(RHSStmt), Exp->getType(), VK_PRValue, OK_Ordinary);
4559 CPT = IRE->getType()->getAs<BlockPointerType>();
4562 CPT = POE->getType()->castAs<BlockPointerType>();
4567 const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>();
4574 &Context->Idents.get("__block_impl"));
4575 QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD));
4580 // Push the block argument type.
4583 for (auto &I : FTP->param_types()) {
4592 QualType PtrToFuncCastType = getSimpleFunctionType(Exp->getType(), ArgTypes);
4594 PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType);
4602 //PE->dump();
4606 &Context->Idents.get("FuncPtr"),
4607 Context->VoidPtrTy, nullptr,
4611 *Context, PE, true, FD, FD->getType(), VK_LValue, OK_Ordinary);
4621 for (CallExpr::arg_iterator I = Exp->arg_begin(),
4622 E = Exp->arg_end(); I != E; ++I) {
4626 CallExpr::Create(*Context, PE, BlkExprs, Exp->getType(), VK_PRValue,
4645 // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR
4647 ValueDecl *VD = DeclRefExp->getDecl();
4648 bool isArrow = DeclRefExp->refersToEnclosingVariableOrCapture() ||
4649 HasLocalVariableExternalStorage(DeclRefExp->getDecl());
4653 &Context->Idents.get("__forwarding"),
4654 Context->VoidPtrTy, nullptr,
4658 *Context, DeclRefExp, isArrow, FD, FD->getType(), VK_LValue, OK_Ordinary);
4660 StringRef Name = VD->getName();
4662 &Context->Idents.get(Name),
4663 Context->VoidPtrTy, nullptr,
4666 ME = MemberExpr::CreateImplicit(*Context, ME, true, FD, DeclRefExp->getType(),
4670 ParenExpr *PE = new (Context) ParenExpr(DeclRefExp->getExprLoc(),
4671 DeclRefExp->getExprLoc(),
4681 ValueDecl *VD = DRE->getDecl();
4686 const_cast<ASTContext &>(*Context), DRE, UO_Deref, DRE->getType(),
4687 VK_LValue, OK_Ordinary, DRE->getLocation(), false, FPOptionsOverride());
4696 SourceLocation LocStart = CE->getLParenLoc();
4697 SourceLocation LocEnd = CE->getRParenLoc();
4706 const char *startBuf = SM->getCharacterData(LocStart);
4707 const char *endBuf = SM->getCharacterData(LocEnd);
4708 QualType QT = CE->getType();
4709 const Type* TypePtr = QT->getAs<Type>();
4712 QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType();
4716 ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString);
4726 LocStart = LocStart.getLocWithOffset(argPtr-startBuf);
4734 CastKind CastKind = IC->getCastKind();
4739 QualType QT = IC->getType();
4741 std::string TypeString(QT.getAsString(Context->getPrintingPolicy()));
4745 InsertText(IC->getSubExpr()->getBeginLoc(), Str);
4749 SourceLocation DeclLoc = FD->getLocation();
4753 const char *startBuf = SM->getCharacterData(DeclLoc);
4760 DeclLoc = DeclLoc.getLocWithOffset(startArgList-startBuf);
4769 DeclLoc = DeclLoc.getLocWithOffset(argPtr-startArgList);
4776 parenCount--;
4784 const PointerType *PT = QT->getAs<PointerType>();
4786 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4788 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4790 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4793 for (const auto &I : FTP->param_types())
4802 const PointerType *PT = QT->getAs<PointerType>();
4804 FTP = PT->getPointeeType()->getAs<FunctionProtoType>();
4806 const BlockPointerType *BPT = QT->getAs<BlockPointerType>();
4808 FTP = BPT->getPointeeType()->getAs<FunctionProtoType>();
4811 for (const auto &I : FTP->param_types()) {
4812 if (I->isObjCQualifiedIdType())
4814 if (I->isObjCObjectPointerType() &&
4815 I->getPointeeType()->isObjCQualifiedInterfaceType())
4835 case ')': parenCount--; break;
4850 SourceLocation DeclLoc = ND->getLocation();
4853 DeclT = VD->getType();
4855 DeclT = TDD->getUnderlyingType();
4857 DeclT = FD->getType();
4861 const char *startBuf = SM->getCharacterData(DeclLoc);
4865 startBuf--;
4866 SourceLocation Start = DeclLoc.getLocWithOffset(startBuf-endBuf);
4889 DeclLoc = ND->getLocation();
4890 startBuf = SM->getCharacterData(DeclLoc);
4918 /// SynthesizeByrefCopyDestroyHelper - This routine synthesizes:
4921 /// _Block_object_assign (&_dest->object, _src->object,
4924 /// _Block_object_assign(&_dest->object, _src->object,
4930 /// _Block_object_dispose(_src->object,
4933 /// _Block_object_dispose(_src->object,
4951 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
4953 static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy));
4955 unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/Context->getCharWidth();
4975 /// RewriteByRefVar - For each __block typex ND variable this routine transforms
4990 /// ND=initializer-if-any};
4997 SourceLocation DeclLoc = ND->getTypeSpecStartLoc();
5001 DeclLoc = ND->getLocation();
5002 const char *startBuf = SM->getCharacterData(DeclLoc);
5003 SourceLocation X = ND->getEndLoc();
5004 X = SM->getExpansionLoc(X);
5005 const char *endBuf = SM->getCharacterData(X);
5006 std::string Name(ND->getNameAsString());
5017 QualType Ty = ND->getType();
5018 bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty, ND);
5026 T.getAsStringInternal(Name, Context->getPrintingPolicy());
5035 assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null");
5036 FunLocStart = CurMethodDef->getBeginLoc();
5046 QualType Ty = ND->getType();
5048 if (Ty->isBlockPointerType())
5059 // initializer-if-any};
5060 bool hasInit = (ND->getInit() != nullptr);
5064 if (CXXConstructExpr *CExp = dyn_cast<CXXConstructExpr>(ND->getInit())) {
5065 CXXConstructorDecl *CXXDecl = CExp->getConstructor();
5066 if (CXXDecl && CXXDecl->isDefaultConstructor())
5073 Name = ND->getNameAsString();
5097 DeclLoc = ND->getLocation();
5098 const char *startDeclBuf = SM->getCharacterData(DeclLoc);
5101 commaBuf--;
5103 DeclLoc = DeclLoc.getLocWithOffset(commaBuf - startDeclBuf);
5112 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType())
5114 ReplaceText(DeclLoc, endBuf-startBuf+nameSize, ByrefType);
5119 Expr *E = ND->getInit();
5121 startLoc = ECE->getLParenLoc();
5123 startLoc = E->getBeginLoc();
5124 startLoc = SM->getExpansionLoc(startLoc);
5125 endBuf = SM->getCharacterData(startLoc);
5126 ReplaceText(DeclLoc, endBuf-startBuf, ByrefType);
5129 const char *startInitializerBuf = SM->getCharacterData(startLoc);
5134 startLoc.getLocWithOffset(separatorBuf-startInitializerBuf);
5142 GetBlockDeclRefExprs(Exp->getBody());
5146 if (!BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>())
5147 BlockByCopyDecls.insert(BlockDeclRefs[i]->getDecl());
5150 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>())
5151 BlockByRefDecls.insert(BlockDeclRefs[i]->getDecl());
5154 if (BlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
5155 BlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5156 BlockDeclRefs[i]->getType()->isBlockPointerType())
5157 ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl());
5162 IdentifierInfo *ID = &Context->Idents.get(name);
5163 QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy);
5171 const BlockDecl *block = Exp->getBlockDecl();
5182 ValueDecl *VD = Exp->getDecl();
5183 if (!VD->hasAttr<BlocksAttr>() && BlockByCopyDecls.insert(VD)) {
5184 // We need to save the copied-in variables in nested
5190 if (VD->hasAttr<BlocksAttr>() && BlockByRefDecls.insert(VD)) {
5197 if (InnerBlockDeclRefs[i]->getDecl()->hasAttr<BlocksAttr>() ||
5198 InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() ||
5199 InnerBlockDeclRefs[i]->getType()->isBlockPointerType())
5200 ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl());
5207 FuncName = CurFunctionDef->getNameAsString();
5211 FuncName = std::string(GlobalVarDecl->getNameAsString());
5214 block->getDeclContext()->getRedeclContext()->isFileContext();
5217 Diags.Report(block->getLocation(), GlobalBlockRewriteFailedDiag);
5221 std::string BlockNumber = utostr(Blocks.size()-1);
5226 QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType());
5227 QualType FType = Context->getPointerType(BFT);
5250 *Context, FD, false, FD->getType(), VK_LValue, SourceLocation());
5251 CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5260 &Context->Idents.get(DescData), Context->VoidPtrTy, nullptr, SC_Static);
5263 new (Context) DeclRefExpr(*Context, NewVD, false, Context->VoidPtrTy,
5265 UO_AddrOf, Context->getPointerType(Context->VoidPtrTy), VK_PRValue,
5274 if (isObjCType(VD->getType())) {
5276 FD = SynthBlockInitFunctionDecl(VD->getName());
5277 Exp = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
5280 QualType QT = VD->getType();
5281 QT = Context->getPointerType(QT);
5287 } else if (isTopLevelBlockPointerType(VD->getType())) {
5288 FD = SynthBlockInitFunctionDecl(VD->getName());
5289 Arg = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
5291 Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy,
5294 FD = SynthBlockInitFunctionDecl(VD->getName());
5295 Exp = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
5298 QualType QT = VD->getType();
5299 QT = Context->getPointerType(QT);
5310 std::string Name(ND->getNameAsString());
5313 IdentifierInfo *II = &Context->Idents.get(RecName.c_str()
5319 QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
5321 FD = SynthBlockInitFunctionDecl(ND->getName());
5322 Exp = new (Context) DeclRefExpr(*Context, FD, false, FD->getType(),
5325 for (const auto &CI : block->captures()) {
5326 const VarDecl *variable = CI.getVariable();
5327 if (variable == ND && CI.isNested()) {
5328 assert(CI.isByRef() &&
5329 "SynthBlockInitExpr - captured block variable is not byref");
5339 Context->getPointerType(Exp->getType()), VK_PRValue, OK_Ordinary,
5349 static_cast<unsigned>(Context->getTypeSize(Context->IntTy));
5351 Context->IntTy, SourceLocation());
5359 "SynthBlockInitExpr - GlobalConstructionExp must be null");
5366 Context->getPointerType(NewRep->getType()), VK_PRValue, OK_Ordinary,
5384 return CS->getElement() == DS;
5388 //===----------------------------------------------------------------------===//
5390 //===----------------------------------------------------------------------===//
5401 // Pseudo-object operations and ivar references need special
5404 if (isa<BinaryOperator>(PseudoOp->getSyntacticForm())) {
5413 S = cast<OpaqueValueExpr>(S)->getSourceExpr();
5415 SourceRange OrigStmtRange = S->getSourceRange();
5418 for (Stmt *&childStmt : S->children())
5429 InnerContexts.insert(BE->getBlockDecl());
5431 GetInnerBlockDeclRefExprs(BE->getBody(),
5435 CurrentBody = BE->getBody();
5437 // block literal on rhs of a property-dot-sytax assignment
5444 RewriteFunctionBodyOrGlobalInitializer(BE->getBody());
5450 std::string Str = Rewrite.getRewrittenText(BE->getSourceRange());
5455 //blockTranscribed->dump();
5485 SourceLocation startLoc = MessExpr->getBeginLoc();
5486 SourceLocation endLoc = MessExpr->getEndLoc();
5488 const char *startBuf = SM->getCharacterData(startLoc);
5489 const char *endBuf = SM->getCharacterData(endLoc);
5493 messString.append(startBuf, endBuf-startBuf+1);
5536 // FIXME: What we're doing here is modifying the type-specifier that
5538 // a separate type-specifier that we can rewrite.
5546 RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin());
5549 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
5553 if (isTopLevelBlockPointerType(ND->getType()))
5555 else if (ND->getType()->isFunctionPointerType())
5556 CheckFunctionPointerDecl(ND->getType(), ND);
5558 if (VD->hasAttr<BlocksAttr>()) {
5563 RewriteByRefVar(VD, (DI == DS->decl_begin()), ((DI+1) == DE));
5570 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
5572 else if (TD->getUnderlyingType()->isFunctionPointerType())
5573 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
5591 ValueDecl *VD = DRE->getDecl();
5592 if (VD->hasAttr<BlocksAttr>())
5599 if (CE->getCallee()->getType()->isBlockPointerType()) {
5600 Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee());
5614 CastExpr *Replacement = new (Context) CastExpr(ICE->getType(),
5615 ICE->getSubExpr(),
5620 Replacement->printPretty(Buf);
5624 InsertText(ICE->getSubExpr()->getBeginLoc(), Str);
5634 for (auto *FD : RD->fields()) {
5635 if (isTopLevelBlockPointerType(FD->getType()))
5637 if (FD->getType()->isObjCQualifiedIdType() ||
5638 FD->getType()->isObjCQualifiedInterfaceType())
5643 /// HandleDeclInMainFile - This is called for each top-level decl defined in the
5646 switch (D->getKind()) {
5649 if (FD->isOverloadedOperator())
5655 RewriteBlocksInFunctionProtoType(FD->getType(), FD);
5657 if (!FD->isThisDeclarationADefinition())
5660 // FIXME: If this should support Obj-C++, support CXXTryStmt
5661 if (CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(FD->getBody())) {
5666 FD->setBody(Body);
5682 if (CompoundStmt *Body = MD->getCompoundBody()) {
5687 MD->setBody(Body);
5700 ObjCImplementationDecl *CI = cast<ObjCImplementationDecl>(D);
5701 ClassImplementation.push_back(CI);
5705 ObjCCategoryImplDecl *CI = cast<ObjCCategoryImplDecl>(D);
5706 CategoryImplementation.push_back(CI);
5712 if (isTopLevelBlockPointerType(VD->getType()))
5714 else if (VD->getType()->isFunctionPointerType()) {
5715 CheckFunctionPointerDecl(VD->getType(), VD);
5716 if (VD->getInit()) {
5717 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
5721 } else if (VD->getType()->isRecordType()) {
5722 RecordDecl *RD = VD->getType()->castAs<RecordType>()->getDecl();
5723 if (RD->isCompleteDefinition())
5726 if (VD->getInit()) {
5728 CurrentBody = VD->getInit();
5729 RewriteFunctionBodyOrGlobalInitializer(VD->getInit());
5735 SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), VD->getName());
5739 if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) {
5748 if (isTopLevelBlockPointerType(TD->getUnderlyingType()))
5750 else if (TD->getUnderlyingType()->isFunctionPointerType())
5751 CheckFunctionPointerDecl(TD->getUnderlyingType(), TD);
5760 if (RD->isCompleteDefinition())
5770 /// Write_ProtocolExprReferencedMetadata - This routine writer out the
5776 // Also output .objc_protorefs$B section and its meta-data.
5777 if (Context->getLangOpts().MicrosoftExt)
5781 Result += PDecl->getNameAsString();
5783 Result += "_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString();
5808 InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false);
5827 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
5834 // Rewrite Objective-c meta data*
5846 OutFile->flush();
5990 // Declarations required for modern objective-c array and dictionary literals.
6018 Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n";
6021 /// RewriteIvarOffsetComputation - This routine synthesizes computation of
6026 Result += ivar->getContainingInterface()->getNameAsString();
6030 if (ivar->isBitField())
6033 Result += ivar->getNameAsString();
6037 /// WriteModernMetadataDeclarations - Writes out metadata declarations for modern ABI.
6127 /// MessageRefTy - LLVM for:
6133 /// SuperMessageRefTy - LLVM for:
6183 const llvm::Triple &Triple(Context->getTargetInfo().getTriple());
6270 Result += SuperPD->getNameAsString();
6271 if (i == e-1)
6299 Result += (MD)->getSelector().getAsString(); Result += "\"";
6301 std::string MethodTypeString = Context->getObjCEncodingForMethodDecl(MD);
6310 if (i == e-1)
6322 const Decl *Container,
6339 Result += PropDecl->getName(); Result += "\",";
6341 Context->getObjCEncodingForPropertyDecl(PropDecl, Container);
6345 if (i == e-1)
6362 /// (Obsolete) ARC-specific: this class has a .release_ivars method
6364 /// class was compiled with -fobjc-arr
6386 const llvm::Triple &Triple(Context->getTargetInfo().getTriple());
6438 bool rootClass = (!CDecl->getSuperClass());
6443 RootClass = CDecl->getSuperClass();
6444 while (RootClass->getSuperClass()) {
6445 RootClass = RootClass->getSuperClass();
6453 if (CDecl->getImplementation())
6459 Result += CDecl->getNameAsString();
6464 ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass();
6467 if (SuperClass->getImplementation())
6474 Result += SuperClass->getNameAsString();
6479 if (RootClass->getImplementation())
6486 Result += RootClass->getNameAsString();
6492 Result += VarName; Result += CDecl->getNameAsString();
6498 Result += RootClass->getNameAsString();
6501 Result += CDecl->getSuperClass()->getNameAsString();
6506 Result += CDecl->getNameAsString();
6508 Result += "0, // &OBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6514 Result += CDecl->getNameAsString();
6518 Result += CDecl->getSuperClass()->getNameAsString();
6530 Result += CDecl->getNameAsString();
6533 // Add static function to initialize some of the meta-data fields.
6539 rootClass ? CDecl : CDecl->getSuperClass();
6542 Result += CDecl->getNameAsString();
6544 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6546 Result += RootClass->getNameAsString(); Result += ";\n";
6548 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6555 Result += SuperClass->getNameAsString(); Result += ";\n";
6557 Result += "\tOBJC_METACLASS_$_"; Result += CDecl->getNameAsString();
6560 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6562 Result += CDecl->getNameAsString(); Result += ";\n";
6565 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6567 Result += SuperClass->getNameAsString(); Result += ";\n";
6570 Result += "\tOBJC_CLASS_$_"; Result += CDecl->getNameAsString();
6583 StringRef CatName = CatDecl->getName();
6584 StringRef ClassName = ClassDecl->getName();
6589 if (ClassDecl->getImplementation())
6645 Result += ClassDecl->getNameAsString();
6650 Result += ClassDecl->getNameAsString();
6672 Context->getObjCEncodingForMethodDecl(MD, true);
6676 if (i == e-1)
6692 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6693 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
6694 Class->getVisibility() == HiddenVisibility)
6703 if (Context->getLangOpts().MicrosoftExt)
6706 if (!Context->getLangOpts().MicrosoftExt ||
6707 IvarDecl->getAccessControl() == ObjCIvarDecl::Private ||
6708 IvarDecl->getAccessControl() == ObjCIvarDecl::Package)
6712 if (Ivars[i]->isBitField())
6720 if (Ivars[i]->isBitField()) {
6739 if (OriginalIvars[i]->isBitField()) {
6751 Result += CDecl->getNameAsString();
6762 if (Ivars[i]->isBitField())
6769 if (Ivars[i]->isBitField())
6772 Result += IvarDecl->getName();
6775 QualType IVQT = IvarDecl->getType();
6776 if (IvarDecl->isBitField())
6780 Context->getObjCEncodingForType(IVQT, IvarTypeString,
6787 unsigned Align = Context->getTypeAlign(IVQT)/8;
6790 CharUnits Size = Context->getTypeSizeInChars(IVQT);
6792 if (i == e-1)
6801 /// RewriteObjCProtocolMetaData - Rewrite protocols meta-data.
6806 if (ObjCSynthesizedProtocols.count(PDecl->getCanonicalDecl()))
6810 if (ObjCProtocolDecl *Def = PDecl->getDefinition())
6814 for (auto *I : PDecl->protocols())
6820 for (auto *MD : PDecl->instance_methods()) {
6821 if (MD->getImplementationControl() == ObjCImplementationControl::Optional) {
6828 for (auto *MD : PDecl->class_methods()) {
6829 if (MD->getImplementationControl() == ObjCImplementationControl::Optional) {
6848 PDecl->getNameAsString());
6850 SmallVector<ObjCProtocolDecl *, 8> SuperProtocols(PDecl->protocols());
6853 PDecl->getNameAsString());
6857 PDecl->getNameAsString(), false);
6861 PDecl->getNameAsString(), false);
6865 PDecl->getNameAsString(), false);
6869 PDecl->getNameAsString(), false);
6873 PDecl->instance_properties());
6875 /* Container */nullptr,
6877 PDecl->getNameAsString());
6884 Result += PDecl->getNameAsString();
6887 Result += "\t\""; Result += PDecl->getNameAsString(); Result += "\",\n";
6890 Result += PDecl->getNameAsString(); Result += ",\n";
6896 Result += PDecl->getNameAsString(); Result += ",\n";
6903 Result += PDecl->getNameAsString(); Result += ",\n";
6910 Result += PDecl->getNameAsString(); Result += ",\n";
6917 Result += PDecl->getNameAsString(); Result += ",\n";
6924 Result += PDecl->getNameAsString(); Result += ",\n";
6934 Result += PDecl->getNameAsString();
6943 Result += "_OBJC_LABEL_PROTOCOL_$_"; Result += PDecl->getNameAsString();
6944 Result += " = &_OBJC_PROTOCOL_"; Result += PDecl->getNameAsString();
6948 if (!ObjCSynthesizedProtocols.insert(PDecl->getCanonicalDecl()).second)
6952 /// hasObjCExceptionAttribute - Return true if this class or any super
6957 if (OID->hasAttr<ObjCExceptionAttr>())
6959 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
6966 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
6969 if (CDecl->isImplicitInterfaceDecl())
6976 for (ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
6977 IVD; IVD = IVD->getNextIvar()) {
6978 // Ignore unnamed bit-fields.
6979 if (!IVD->getDeclName())
6989 SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods());
6993 for (const auto *Prop : IDecl->property_impls()) {
6994 if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
6996 if (!Prop->getPropertyIvarDecl())
6998 ObjCPropertyDecl *PD = Prop->getPropertyDecl();
7001 if (ObjCMethodDecl *Getter = Prop->getGetterMethodDecl())
7004 if (PD->isReadOnly())
7006 if (ObjCMethodDecl *Setter = Prop->getSetterMethodDecl())
7013 IDecl->getNameAsString(), true);
7015 SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods());
7019 IDecl->getNameAsString(), true);
7024 const ObjCList<ObjCProtocolDecl> &Protocols = CDecl->getReferencedProtocols();
7037 IDecl->getNameAsString());
7041 CDecl->instance_properties());
7043 /* Container */IDecl,
7045 CDecl->getNameAsString());
7047 // Data for initializing _class_ro_t metaclass meta-data
7052 bool classIsHidden = CDecl->getVisibility() == HiddenVisibility;
7056 if (!CDecl->getSuperClass())
7068 CDecl->getNameAsString());
7070 // Data for initializing _class_ro_t meta-data
7078 if (!CDecl->getSuperClass())
7090 InstanceSize += CDecl->getNameAsString();
7093 ObjCIvarDecl *IVD = CDecl->all_declared_ivar_begin();
7107 CDecl->getNameAsString());
7130 ObjCInterfaceDecl *CDecl = IDecl->getClassInterface();
7132 Result += CDecl->getName(); Result += ",\n";
7163 Result += ClassImplementation[i]->getNameAsString();
7173 Result += "\t&OBJC_CLASS_$_"; Result += DefinedNonLazyClasses[i]->getNameAsString();
7191 CategoryImplementation[i]->getClassInterface()->getNameAsString();
7193 Result += CategoryImplementation[i]->getNameAsString();
7206 DefinedNonLazyCategories[i]->getClassInterface()->getNameAsString();
7208 Result += DefinedNonLazyCategories[i]->getNameAsString();
7224 /// RewriteObjCCategoryImplDecl - Rewrite metadata for each category
7229 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
7232 = ClassDecl->FindCategoryDeclaration(IDecl->getIdentifier());
7234 std::string FullCategoryName = ClassDecl->getNameAsString();
7236 FullCategoryName += CDecl->getNameAsString();
7239 SmallVector<ObjCMethodDecl *, 32> InstanceMethods(IDecl->instance_methods());
7243 for (const auto *Prop : IDecl->property_impls()) {
7244 if (Prop->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
7246 if (!Prop->getPropertyIvarDecl())
7248 ObjCPropertyDecl *PD = Prop->getPropertyDecl();
7251 if (ObjCMethodDecl *Getter = Prop->getGetterMethodDecl())
7253 if (PD->isReadOnly())
7255 if (ObjCMethodDecl *Setter = Prop->getSetterMethodDecl())
7263 SmallVector<ObjCMethodDecl *, 32> ClassMethods(IDecl->class_methods());
7271 SmallVector<ObjCProtocolDecl *, 8> RefedProtocols(CDecl->protocols());
7272 for (auto *I : CDecl->protocols())
7284 CDecl->instance_properties());
7286 /* Container */IDecl,
7298 // Determine if this category is also "non-lazy".
7312 ObjCCategoryDecl *CatDecl= IDecl->getCategoryDecl();
7313 ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface();
7315 Result += ClassDecl->getName();
7317 Result += CatDecl->getName();
7323 // RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or
7382 Result += (*MethodBegin)->getSelector().getAsString().c_str();
7384 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
7392 Result += (*MethodBegin)->getSelector().getAsString().c_str();
7394 Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString);
7405 SourceRange OldRange = IV->getSourceRange();
7406 Expr *BaseExpr = IV->getBase();
7412 IV->setBase(BaseExpr);
7415 ObjCIvarDecl *D = IV->getDecl();
7419 if (BaseExpr->getType()->isObjCObjectPointerType()) {
7421 dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType());
7422 assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null");
7425 iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(),
7431 if (D->isBitField())
7440 Context->getPointerType(Context->CharTy),
7444 SourceLocation(), &Context->Idents.get(IvarOffsetName),
7445 Context->UnsignedLongTy, nullptr,
7448 DeclRefExpr(*Context, NewVD, false, Context->UnsignedLongTy,
7452 Context->getPointerType(Context->CharTy), VK_PRValue, OK_Ordinary,
7458 QualType IvarT = D->getType();
7459 if (D->isBitField())
7462 if (!IvarT->getAs<TypedefType>() && IvarT->isRecordType()) {
7463 RecordDecl *RD = IvarT->castAs<RecordType>()->getDecl();
7464 RD = RD->getDefinition();
7465 if (RD && !RD->getDeclName().getAsIdentifierInfo()) {
7466 // decltype(((Foo_IMPL*)0)->bar) *
7467 auto *CDecl = cast<ObjCContainerDecl>(D->getDeclContext());
7470 CDecl = CatDecl->getClassInterface();
7471 std::string RecName = std::string(CDecl->getName());
7475 SourceLocation(), &Context->Idents.get(RecName));
7476 QualType PtrStructIMPL = Context->getPointerType(Context->getTagDeclType(RD));
7478 static_cast<unsigned>(Context->getTypeSize(Context->UnsignedIntTy));
7481 Context->UnsignedIntTy, SourceLocation());
7487 &Context->Idents.get(D->getNameAsString()),
7492 *Context, PE, true, FD, FD->getType(), VK_LValue, OK_Ordinary);
7493 IvarT = Context->getDecltypeType(ME, ME->getType());
7497 QualType castT = Context->getPointerType(IvarT);
7511 if (D->isBitField()) {
7514 &Context->Idents.get(D->getNameAsString()),
7515 D->getType(), nullptr,
7516 /*BitWidth=*/D->getBitWidth(),
7520 FD->getType(), VK_LValue, OK_Ordinary);