31#include "llvm/ADT/SmallString.h"
32#include "llvm/Support/ConvertUTF.h"
49 if (Strings.size() != 1) {
54 for (
Expr *
E : Strings) {
55 S = cast<StringLiteral>(
E);
58 if (!S->isOrdinary()) {
59 Diag(S->getBeginLoc(), diag::err_cfstring_literal_not_string_constant)
60 << S->getSourceRange();
65 StrBuf += S->getString();
68 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
74 assert(CAT &&
"String literal not of constant array type!");
76 CAT->
getElementType(), llvm::APInt(32, StrBuf.size() + 1),
nullptr,
79 false, StrTy, &StrLocs[0],
102 std::string StringClass(
getLangOpts().ObjCConstantStringClass);
104 if (StringClass.empty())
105 NSIdent = &Context.
Idents.
get(
"NSConstantString");
107 NSIdent = &Context.
Idents.
get(StringClass);
118 Diag(S->getBeginLoc(), diag::err_no_nsconstant_string_class)
119 << NSIdent << S->getSourceRange();
159 S.
Diag(
Loc, diag::err_undeclared_boxing_method) << Sel <<
Class->getName();
166 S.
Diag(
Loc, diag::err_objc_literal_method_sig)
179 switch (LiteralKind) {
197 llvm_unreachable(
"LiteralKind can't be converted into a ClassKind");
210 S.
Diag(
Loc, diag::err_undeclared_objc_literal_class)
211 << II->
getName() << LiteralKind;
213 }
else if (!
Decl->hasDefinition() && !S.
getLangOpts().DebuggerObjCLiteral) {
214 S.
Diag(
Loc, diag::err_undeclared_objc_literal_class)
215 <<
Decl->getName() << LiteralKind;
252 bool isLiteral =
false,
254 std::optional<NSAPI::NSNumberLiteralMethodKind> Kind =
255 S.NSAPIObj->getNSNumberFactoryMethodKind(NumberType);
259 S.
Diag(
Loc, diag::err_invalid_nsnumber_type)
266 if (S.NSNumberLiteralMethods[*Kind])
267 return S.NSNumberLiteralMethods[*Kind];
269 Selector Sel = S.NSAPIObj->getNSNumberLiteralSelector(*Kind,
276 if (!S.NSNumberDecl) {
279 if (!S.NSNumberDecl) {
284 if (S.NSNumberPointer.isNull()) {
292 if (!Method && S.
getLangOpts().DebuggerObjCLiteral) {
297 ReturnTInfo, S.NSNumberDecl,
307 NumberType,
nullptr,
SC_None,
nullptr);
317 S.NSNumberLiteralMethods[*Kind] = Method;
331 switch (Char->getKind()) {
334 NumberType = Context.
CharTy;
367 Number = ConvertedNumber.
get();
380 Value ? tok::kw_true : tok::kw_false);
386 CK_IntegralToBoolean);
396 bool ArrayLiteral =
false) {
398 if (Element->isTypeDependent())
408 if (S.
getLangOpts().CPlusPlus && Element->getType()->isRecordType()) {
416 return Seq.Perform(S, Entity, Kind, Element);
419 Expr *OrigElement = Element;
428 if (!Element->getType()->isObjCObjectPointerType() &&
429 !Element->getType()->isBlockPointerType()) {
430 bool Recovered =
false;
433 if (isa<IntegerLiteral>(OrigElement) ||
434 isa<CharacterLiteral>(OrigElement) ||
435 isa<FloatingLiteral>(OrigElement) ||
436 isa<ObjCBoolLiteralExpr>(OrigElement) ||
437 isa<CXXBoolLiteralExpr>(OrigElement)) {
440 int Which = isa<CharacterLiteral>(OrigElement) ? 1
441 : (isa<CXXBoolLiteralExpr>(OrigElement) ||
442 isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2
459 else if (
StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) {
460 if (String->isOrdinary()) {
476 S.
Diag(Element->getBeginLoc(), diag::err_invalid_collection_element)
477 << Element->getType();
483 dyn_cast<ObjCStringLiteral>(OrigElement)) {
485 unsigned numConcat = SL->getNumConcatenated();
488 bool hasMacro =
false;
489 for (
unsigned i = 0; i < numConcat ; ++i)
490 if (SL->getStrTokenLoc(i).isMacroID()) {
495 S.
Diag(Element->getBeginLoc(),
496 diag::warn_concatenated_nsarray_literal)
497 << Element->getType();
507 Element->getBeginLoc(), Element);
525 ValueExpr = RValue.
get();
543 if (
auto *CE = dyn_cast<ImplicitCastExpr>(ValueExpr))
544 if (CE->getCastKind() == CK_ArrayToPointerDecay)
546 dyn_cast<StringLiteral>(CE->getSubExpr()->IgnoreParens())) {
547 assert((SL->isOrdinary() || SL->isUTF8()) &&
548 "unexpected character encoding");
549 StringRef Str = SL->getString();
550 const llvm::UTF8 *StrBegin = Str.bytes_begin();
551 const llvm::UTF8 *StrEnd = Str.bytes_end();
553 if (llvm::isLegalUTF8String(&StrBegin, StrEnd)) {
558 return new (Context)
ObjCBoxedExpr(CE, BoxedType,
nullptr, SR);
561 Diag(SL->getBeginLoc(), diag::warn_objc_boxing_invalid_utf8_string)
571 if (!BoxingMethod &&
getLangOpts().DebuggerObjCLiteral) {
596 stringWithUTF8String, BoxingMethod))
605 std::optional<NullabilityKind> Nullability =
620 dyn_cast<CharacterLiteral>(ValueExpr->
IgnoreParens())) {
623 switch (Char->getKind()) {
626 ValueType = Context.
CharTy;
648 if (!ET->getDecl()->isComplete()) {
649 Diag(
Loc, diag::err_objc_incomplete_boxed_expression_type)
655 ET->getDecl()->getIntegerType());
681 if (!BoxingMethod &&
getLangOpts().DebuggerObjCLiteral) {
704 Params.push_back(
bytes);
714 Params.push_back(
type);
721 ValueWithBytesObjCType, BoxingMethod))
728 Diag(
Loc, diag::err_objc_non_trivially_copyable_boxed_expression_type)
738 Diag(
Loc, diag::err_objc_illegal_boxed_expression_type)
761 ValueExpr = ConvertedValueExpr.
get();
774 assert(!
getLangOpts().isSubscriptPointerArithmetic());
780 "base or index cannot have dependent type here");
798 getterMethod, setterMethod, RB);
820 if (!Method &&
getLangOpts().DebuggerObjCLiteral) {
837 Params.push_back(objects);
845 Params.push_back(cnt);
860 diag::note_objc_literal_method_param)
867 if (!Method->
parameters()[1]->getType()->isIntegerType()) {
871 diag::note_objc_literal_method_param)
887 Expr **ElementsBuffer = Elements.data();
888 for (
unsigned I = 0, N = Elements.size(); I != N; ++I) {
890 SemaRef, ElementsBuffer[I], RequiredType,
true);
894 ElementsBuffer[I] = Converted.
get();
910 if (Literal->isValueDependent() || Literal->isTypeDependent())
916 struct APSIntCompare {
917 bool operator()(
const llvm::APSInt &LHS,
const llvm::APSInt &RHS)
const {
918 return llvm::APSInt::compareValues(LHS, RHS) < 0;
922 llvm::DenseMap<StringRef, SourceLocation> StringKeys;
923 std::map<llvm::APSInt, SourceLocation, APSIntCompare> IntegralKeys;
926 auto Pair = Map.insert({Key,
Loc});
928 S.
Diag(
Loc, diag::warn_nsdictionary_duplicate_key);
929 S.
Diag(Pair.first->second, diag::note_nsdictionary_duplicate_key_here);
933 for (
unsigned Idx = 0, End = Literal->getNumElements(); Idx != End; ++Idx) {
934 Expr *Key = Literal->getKeyValueElement(Idx).Key->IgnoreParenImpCasts();
936 if (
auto *StrLit = dyn_cast<ObjCStringLiteral>(Key)) {
937 StringRef Bytes = StrLit->getString()->getBytes();
939 checkOneKey(StringKeys, Bytes,
Loc);
942 if (
auto *BE = dyn_cast<ObjCBoxedExpr>(Key)) {
943 Expr *Boxed = BE->getSubExpr();
948 checkOneKey(StringKeys, Str->getBytes(),
Loc);
955 checkOneKey(IntegralKeys,
Result.Val.getInt(),
Loc);
981 if (!Method &&
getLangOpts().DebuggerObjCLiteral) {
998 Params.push_back(objects);
1006 Params.push_back(keys);
1014 Params.push_back(cnt);
1030 diag::note_objc_literal_method_param)
1064 diag::note_objc_literal_method_param)
1072 QualType CountType = Method->parameters()[2]->getType();
1076 Diag(Method->parameters()[2]->getLocation(),
1077 diag::note_objc_literal_method_param)
1087 QualType ValuesT = DictionaryWithObjectsMethod->parameters()[0]->getType();
1089 QualType KeysT = DictionaryWithObjectsMethod->parameters()[1]->getType();
1094 bool HasPackExpansions =
false;
1105 if (
Value.isInvalid())
1108 Element.Key = Key.
get();
1109 Element.Value =
Value.get();
1111 if (Element.EllipsisLoc.isInvalid())
1114 if (!Element.Key->containsUnexpandedParameterPack() &&
1115 !Element.Value->containsUnexpandedParameterPack()) {
1116 Diag(Element.EllipsisLoc,
1117 diag::err_pack_expansion_without_parameter_packs)
1119 Element.Value->getEndLoc());
1123 HasPackExpansions =
true;
1131 DictionaryWithObjectsMethod, SR);
1133 return SemaRef.MaybeBindToTemporary(Literal);
1148 diag::err_incomplete_type_objc_at_encode,
1155 if (!NotEncodedT.
isNull())
1156 Diag(AtLoc, diag::warn_incomplete_encoded_type)
1157 << EncodedType << NotEncodedT;
1164 return new (Context)
ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
1190 bool Warned =
false;
1193 if (MatchingMethodDecl == Method ||
1201 S.
Diag(AtLoc, diag::warn_multiple_selectors)
1207 S.
Diag(MatchingMethodDecl->
getLocation(), diag::note_method_declared_at)
1218 bool WarnMultipleSelectors) {
1219 if (!WarnMultipleSelectors ||
1222 bool Warned =
false;
1229 Method, InstMethList))
1235 Method, ClsMethList) || Warned)
1251 assert(Method->
getSelector() == Sel &&
"Method with wrong selector in method list");
1254 DirectMethod = Method;
1259 return DirectMethod;
1274 S, Sel,
Iter->second.first, onlyDirect, anyDirect);
1276 S, Sel,
Iter->second.second, onlyDirect, anyDirect);
1278 return DirectInstance ? DirectInstance : DirectClass;
1307 bool WarnMultipleSelectors) {
1316 Selector MatchedSel = OM->getSelector();
1319 Diag(SelLoc, diag::warn_undeclared_selector_with_typo)
1320 << Sel << MatchedSel
1324 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
1327 WarnMultipleSelectors);
1329 bool onlyDirect =
true;
1330 bool anyDirect =
false;
1335 Diag(AtLoc, diag::err_direct_selector_expression)
1339 }
else if (anyDirect) {
1345 if (LikelyTargetMethod && LikelyTargetMethod->
isDirectMethod()) {
1346 Diag(AtLoc, diag::warn_potentially_direct_selector_expression) << Sel;
1348 diag::note_direct_method_declared_at)
1350 }
else if (!LikelyTargetMethod) {
1353 Diag(AtLoc, diag::warn_strict_potentially_direct_selector_expression)
1356 diag::note_direct_method_declared_at)
1377 Diag(AtLoc, diag::err_arc_illegal_selector) <<
1407 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
1411 Diag(ProtoLoc, diag::err_objc_non_runtime_protocol_in_protocol_expr)
1414 Diag(ProtoLoc, diag::err_atprotocol_protocol) << PDecl;
1424 return new (Context)
ObjCProtocolExpr(Ty, PDecl, AtLoc, ProtoIdLoc, RParenLoc);
1470 bool isClassMessage,
1471 bool isSuperMessage) {
1472 assert(Method &&
"Must have a method");
1482 if (
auto nullability =
1507 if (isSuperMessage) {
1510 return transferNullability(
1529 return transferNullability(ReceiverType);
1535 bool isClassMessage,
1536 bool isSuperMessage) {
1540 SemaRef, ReceiverType, Method, isClassMessage, isSuperMessage);
1543 if (isClassMessage) {
1555 cast<ImplicitParamDecl>(
1557 ->getDeclContext());
1564 NewResultType, NewResultType);
1565 return NewResultType;
1577 unsigned receiverNullabilityIdx = 0;
1578 if (std::optional<NullabilityKind> nullability =
1582 receiverNullabilityIdx = 1 +
static_cast<unsigned>(*nullability);
1585 unsigned resultNullabilityIdx = 0;
1586 if (std::optional<NullabilityKind> nullability =
1590 resultNullabilityIdx = 1 +
static_cast<unsigned>(*nullability);
1595 static const uint8_t
None = 0;
1596 static const uint8_t
NonNull = 1;
1599 static const uint8_t nullabilityMap[4][4] = {
1607 unsigned newResultNullabilityIdx
1608 = nullabilityMap[receiverNullabilityIdx][resultNullabilityIdx];
1609 if (newResultNullabilityIdx == resultNullabilityIdx)
1615 if (
auto attributed = dyn_cast<AttributedType>(resultType.
getTypePtr())) {
1616 resultType = attributed->getModifiedType();
1623 if (newResultNullabilityIdx > 0) {
1628 resultType, resultType);
1647 dyn_cast<ObjCCategoryImplDecl>(impl)) {
1648 iface = catImpl->getCategoryDecl();
1650 iface = impl->getClassInterface();
1660 for (
unsigned i = 0, e = overrides.size(); i != e; ++i) {
1682 SourceRange range = overridden->getReturnTypeSourceRange();
1685 loc = overridden->getLocation();
1686 Diag(loc, diag::note_related_result_type_explicit)
1734 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
1735 SelLoc = SelectorLocs.front();
1741 for (
unsigned i = 0, e = Args.size(); i != e; i++) {
1742 if (Args[i]->isTypeDependent())
1754 Args[i] = result.
get();
1759 DiagID = diag::err_arc_method_not_found;
1761 DiagID = isClassMessage ? diag::warn_class_method_not_found
1762 : diag::warn_inst_method_not_found;
1767 DiagID = diag::err_method_not_found_with_typo;
1769 DiagID = isClassMessage ? diag::warn_class_method_not_found_with_typo
1770 : diag::warn_instance_method_not_found_with_typo;
1772 SourceRange SelectorRange(SelectorLocs.front(), SelectorLocs.back());
1774 Diag(SelLoc, DiagID)
1775 << Sel<< isClassMessage << MatchedSel
1778 Diag(SelLoc, DiagID) << Sel<< isClassMessage << MatchedSel;
1781 Diag(SelLoc, DiagID)
1782 << Sel << isClassMessage <<
SourceRange(SelectorLocs.front(),
1783 SelectorLocs.back());
1787 Diag(ThisClass->getLocation(), diag::note_receiver_class_declared);
1789 if (ThisClass->lookupClassMethod(Sel))
1790 Diag(RecRange.
getBegin(), diag::note_receiver_expr_here)
1792 ThisClass->getNameAsString());
1809 isClassMessage, isSuperMessage);
1818 if (Args.size() < NumNamedArgs) {
1819 Diag(SelLoc, diag::err_typecheck_call_too_few_args)
1820 << 2 << NumNamedArgs << static_cast<unsigned>(Args.size())
1827 std::optional<ArrayRef<QualType>> typeArgs =
1829 bool IsError =
false;
1830 for (
unsigned i = 0; i < NumNamedArgs; i++) {
1832 if (Args[i]->isTypeDependent())
1835 Expr *argExpr = Args[i];
1838 assert(argExpr &&
"CheckMessageArgumentTypes(): missing expression");
1840 if (param->
hasAttr<NoEscapeAttr>() &&
1842 if (
auto *BE = dyn_cast<BlockExpr>(
1844 BE->getBlockDecl()->setDoesNotEscape();
1849 !param->
hasAttr<CFConsumedAttr>())
1860 Args[i] = argE.
get();
1878 diag::err_call_incomplete_argument, argExpr))
1894 Args[i]->getType()->isBlockPointerType() &&
1898 Args[i] = arg.get();
1905 for (
unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) {
1906 if (Args[i]->isTypeDependent())
1912 Args[i] = Arg.
get();
1916 if (Args.size() != NumNamedArgs) {
1917 Diag(Args[NumNamedArgs]->getBeginLoc(),
1918 diag::err_typecheck_call_too_many_args)
1919 << 2 << NumNamedArgs << static_cast<unsigned>(Args.size())
1922 Args.back()->getEndLoc());
1943 if (!method)
return false;
1946 if (
DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver))
1958 if (
ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance))
1963 if (
ObjCMethodDecl *method = iface->lookupPrivateMethod(sel, isInstance))
1968 for (
const auto *I : objType->
quals())
1980 for (
const auto *PROTO : OPT->
quals()) {
1981 if ((MD = PROTO->lookupMethod(Sel, Instance))) {
1999 Diag(MemberLoc, diag::err_invalid_property_name)
2009 diag::err_property_not_found_forward_class,
2010 MemberName, BaseRange))
2019 return new (Context)
2023 return new (Context)
2028 for (
const auto *I : OPT->
quals())
2038 SuperLoc, SuperType);
2040 return new (Context)
2094 if (!(PDecl->getPropertyAttributes() &
2097 diag::warn_property_access_suggest)
2098 << MemberName <<
QualType(OPT, 0) << PDecl->getName()
2103 if (Getter || Setter) {
2105 return new (Context)
2109 return new (Context)
2125 Corrected.isKeyword() ? nullptr : Corrected.getFoundDecl();
2126 if (ChosenDecl && isa<ObjCPropertyDecl>(ChosenDecl))
2127 if (cast<ObjCPropertyDecl>(ChosenDecl)->isClassProperty()) {
2130 Diag(MemberLoc, diag::err_class_property_found) << MemberName
2138 PDiag(diag::err_property_not_found_suggest)
2139 << MemberName <<
QualType(OPT, 0));
2141 TypoResult, MemberLoc,
2142 SuperLoc, SuperType, Super);
2152 diag::err_property_not_as_forward_class,
2153 MemberName, BaseExpr))
2157 diag::err_ivar_access_using_property_syntax_suggest)
2158 << MemberName <<
QualType(OPT, 0) << Ivar->getDeclName()
2163 Diag(MemberLoc, diag::err_property_not_found)
2183 if (receiverNamePtr->
isStr(
"super")) {
2185 if (
auto classDecl = CurMethod->getClassInterface()) {
2186 SuperType =
QualType(classDecl->getSuperClassType(), 0);
2187 if (CurMethod->isInstanceMethod()) {
2188 if (SuperType.
isNull()) {
2190 Diag(receiverNameLoc, diag::err_root_class_cannot_use_super)
2191 << CurMethod->getClassInterface()->getIdentifier();
2201 receiverNameLoc,
T,
true);
2212 Diag(receiverNameLoc, diag::err_expected_either) << tok::identifier
2222 GetterSel = PD->getGetterName();
2223 SetterSel = PD->getSetterName();
2259 if (Getter || Setter) {
2261 return new (Context)
2268 propertyNameLoc, receiverNameLoc, IFace);
2270 return ExprError(
Diag(propertyNameLoc, diag::err_property_not_found)
2284 bool ValidateCandidate(
const TypoCorrection &candidate)
override {
2289 std::unique_ptr<CorrectionCandidateCallback> clone()
override {
2290 return std::make_unique<ObjCInterfaceOrSuperCCC>(*
this);
2299 bool HasTrailingDot,
ParsedType &ReceiverType) {
2301 ReceiverType =
nullptr;
2306 if (IsSuper && S->isInObjcMethodScope())
2312 switch (
Result.getResultKind()) {
2337 Result.suppressDiagnostics();
2368 Result.getLookupNameInfo(),
Result.getLookupKind(), S,
nullptr, CCC,
2370 if (Corrected.isKeyword()) {
2402 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
2408 Diag(SuperLoc, diag::err_no_super_class_message)
2416 Diag(SuperLoc, diag::err_root_class_cannot_use_super)
2417 <<
Class->getIdentifier();
2432 LBracLoc, SelectorLocs, RBracLoc, Args);
2439 SuperLoc, Sel,
nullptr,
2440 LBracLoc, SelectorLocs, RBracLoc, Args);
2444 bool isSuperReceiver,
2450 if (!ReceiverType.
isNull())
2453 assert(((isSuperReceiver &&
Loc.
isValid()) || receiverTypeInfo) &&
2454 "Either the super receiver location needs to be valid or the receiver "
2455 "needs valid type source information");
2473 auto Builder = S.
Diag(MsgLoc, DiagID)
2481 switch (Edit.
Kind) {
2488 Builder.AddFixItHint(
2509 bool IsClassObjectCall) {
2515 const auto *SE = dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens());
2519 if (!IsClassObjectCall) {
2521 if (!OPT || !OPT->getInterfaceDecl())
2524 OPT->getInterfaceDecl()->lookupInstanceMethod(SE->getSelector());
2527 OPT->getInterfaceDecl()->lookupPrivateMethod(SE->getSelector());
2532 ImpliedMethod = IT->getDecl()->lookupClassMethod(SE->getSelector());
2535 IT->getDecl()->lookupPrivateClassMethod(SE->getSelector());
2540 if (Ret->isRecordType() || Ret->isVectorType() || Ret->isExtVectorType()) {
2541 S.
Diag(
Loc, diag::warn_objc_unsafe_perform_selector)
2543 << (!Ret->isRecordType()
2545 : Ret->isUnionType() ? 1 : 0);
2547 diag::note_objc_unsafe_perform_selector_method_declared_here)
2558 Expr **Args,
unsigned NumArgs) {
2560 bool Format =
false;
2574 if (!Format || NumArgs <= Idx)
2577 Expr *FormatExpr = Args[Idx];
2582 S.
Diag(FormatExpr->
getExprLoc(), diag::warn_objc_cdirective_format_string)
2628 Diag(
Loc, diag::err_missing_open_square_message_send)
2633 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2634 SelectorSlotLocs = SelectorLocs;
2636 SelectorSlotLocs =
Loc;
2642 unsigned NumArgs = ArgsIn.size();
2643 Expr **Args = ArgsIn.data();
2644 assert(SuperLoc.
isInvalid() &&
"Message to super with dependent type");
2646 ReceiverTypeInfo, Sel, SelectorLocs,
2648 RBracLoc, isImplicit);
2655 Diag(
Loc, diag::err_invalid_receiver_class_message)
2659 assert(
Class &&
"We don't know which class we're messaging?");
2670 ? diag::err_arc_receiver_forward_class
2671 : diag::warn_receiver_forward_class),
2681 Method =
Class->lookupClassMethod(Sel);
2685 Method =
Class->lookupPrivateClassMethod(Sel);
2688 false,
false,
Class))
2696 unsigned NumArgs = ArgsIn.size();
2697 Expr **Args = ArgsIn.data();
2700 Method,
true, SuperLoc.
isValid(), LBracLoc,
2707 diag::err_illegal_message_expr_incomplete_type))
2711 Diag(SuperLoc, diag::err_messaging_super_with_direct_method)
2726 Diag(
Loc, diag::warn_direct_initialize_call);
2733 Diag(
Loc, diag::warn_direct_super_initialize_call);
2736 Diag(CurMeth->getLocation(), diag::note_method_declared_at)
2737 << CurMeth->getDeclName();
2748 Context, ReturnType, VK, LBracLoc, SuperLoc,
false,
2749 ReceiverType, Sel, SelectorLocs, Method,
ArrayRef(Args, NumArgs),
2750 RBracLoc, isImplicit);
2753 Context, ReturnType, VK, LBracLoc, ReceiverTypeInfo, Sel, SelectorLocs,
2754 Method,
ArrayRef(Args, NumArgs), RBracLoc, isImplicit);
2760 ReceiverType,
true);
2776 if (ReceiverType.
isNull())
2779 if (!ReceiverTypeInfo)
2784 nullptr, LBracLoc, SelectorLocs, RBracLoc,
2800 const auto *Protocol = dyn_cast<ObjCProtocolDecl>(M->
getDeclContext());
2805 if (
const auto *RootClass = dyn_cast_or_null<ObjCInterfaceDecl>(
2809 if (
P->getCanonicalDecl() == Protocol->getCanonicalDecl())
2849 assert((Receiver || SuperLoc.
isValid()) &&
"If the Receiver is null, the "
2850 "SuperLoc must be valid so we can "
2859 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2860 SelectorSlotLocs = SelectorLocs;
2862 SelectorSlotLocs =
Loc;
2866 Diag(
Loc, diag::err_missing_open_square_message_send)
2888 unsigned NumArgs = ArgsIn.size();
2889 Expr **Args = ArgsIn.data();
2890 assert(SuperLoc.
isInvalid() &&
"Message to super with dependent type");
2893 SelectorLocs,
nullptr,
ArrayRef(Args, NumArgs), RBracLoc,
2903 ReceiverType = Receiver->
getType();
2916 Diag(
Loc, diag::warn_bad_receiver_type) << ReceiverType << RecRange;
2920 CK_CPointerToObjCPointerCast)
2926 CastKind Kind =
IsNull ? CK_NullToPointer : CK_IntegralToPointer;
2931 ReceiverType = Receiver->
getType();
2935 diag::err_incomplete_receiver_type))
2941 Receiver = result.
get();
2942 ReceiverType = Receiver->
getType();
2963 if (!Methods.empty()) {
2966 Method = Methods[0];
2970 Method = BestMethod;
2974 receiverIsIdLike, Methods))
2992 Diag(SelLoc, diag::warn_instance_method_on_class_found)
3005 Method = ClassDecl->lookupClassMethod(Sel);
3008 Method = ClassDecl->lookupPrivateClassMethod(Sel);
3023 if (!Methods.empty()) {
3026 Method = Methods[0];
3032 if (ID->getSuperClass())
3033 Diag(SelLoc, diag::warn_root_inst_method_not_found)
3040 Method = BestMethod;
3062 ClassDecl = OCIType->getInterfaceDecl();
3070 Loc, OCIType->getPointeeType(),
3072 ? diag::err_arc_receiver_forward_instance
3073 : diag::warn_receiver_forward_instance,
3078 forwardClass = OCIType->getInterfaceDecl();
3080 diag::note_receiver_is_id);
3095 Diag(SelLoc, diag::err_arc_may_not_respond)
3096 << OCIType->getPointeeType() << Sel << RecRange
3097 <<
SourceRange(SelectorLocs.front(), SelectorLocs.back());
3101 if (!Method && (!Receiver || !
isSelfExpr(Receiver))) {
3105 if (OCIType->qual_empty()) {
3110 if (!Methods.empty()) {
3113 Method = Methods[0];
3117 Method = BestMethod;
3124 if (Method && !forwardClass)
3125 Diag(SelLoc, diag::warn_maynot_respond)
3126 << OCIType->getInterfaceDecl()->getIdentifier()
3136 Diag(
Loc, diag::err_bad_receiver_type) << ReceiverType << RecRange;
3150 diag::err_messaging_unqualified_id_with_direct_method);
3162 diag::err_messaging_class_with_direct_method);
3175 Diag(SuperLoc, diag::err_messaging_super_with_direct_method);
3186 }
else if (ReceiverType->
isObjCIdType() && !isImplicit) {
3187 Diag(Receiver->
getExprLoc(), diag::warn_messaging_unqualified_id);
3190 if (DIFunctionScopeInfo &&
3193 bool isDesignatedInitChain =
false;
3200 if (!ID->declaresOrInheritsDesignatedInitializers() ||
3201 ID->isDesignatedInitializer(Sel)) {
3202 isDesignatedInitChain =
true;
3208 if (!isDesignatedInitChain) {
3211 assert(CurMD &&
"Current method declaration should not be null");
3213 CurMD->isDesignatedInitializerForTheInterface(&InitMethod);
3214 assert(isDesignated && InitMethod);
3217 diag::warn_objc_designated_init_non_designated_init_call :
3218 diag::warn_objc_designated_init_non_super_designated_init_call);
3220 diag::note_objc_designated_init_marked_here);
3224 if (DIFunctionScopeInfo &&
3228 Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call);
3235 unsigned NumArgs = ArgsIn.size();
3236 Expr **Args = ArgsIn.data();
3243 Method, ClassMessage, SuperLoc.
isValid(),
3244 LBracLoc, RBracLoc, RecRange, ReturnType, VK))
3250 diag::err_illegal_message_expr_incomplete_type))
3279 Diag(SelLoc, diag::err_arc_illegal_explicit_message)
3284 if (Method && NumArgs >= 1) {
3285 if (
const auto *SelExp =
3286 dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens())) {
3287 Selector ArgSel = SelExp->getSelector();
3290 SelExp->getSourceRange());
3294 SelExp->getSourceRange());
3297 switch (SelFamily) {
3304 if (!SelMethod->
hasAttr<NSReturnsNotRetainedAttr>()) {
3307 diag::err_arc_perform_selector_retains);
3314 if (SelMethod->
hasAttr<NSReturnsRetainedAttr>()) {
3317 diag::err_arc_perform_selector_retains);
3326 Diag(SelLoc, diag::warn_arc_perform_selector_leaks);
3327 Diag(Args[0]->getExprLoc(), diag::note_used_here);
3340 Context, ReturnType, VK, LBracLoc, SuperLoc,
true,
3341 ReceiverType, Sel, SelectorLocs, Method,
ArrayRef(Args, NumArgs),
3342 RBracLoc, isImplicit);
3345 Context, ReturnType, VK, LBracLoc, Receiver, Sel, SelectorLocs, Method,
3346 ArrayRef(Args, NumArgs), RBracLoc, isImplicit);
3351 bool IsClassObjectCall = ClassMessage;
3358 if (OPT->getObjectType()->isObjCClass()) {
3360 IsClassObjectCall =
true;
3368 ReceiverType, IsClassObjectCall);
3381 Result->setDelegateInitCall(
true);
3392 if (!isImplicit && Method) {
3416 auto Pos = S.ReferencedSelectors.find(Sel);
3417 if (Pos != S.ReferencedSelectors.end() && Pos->second ==
Loc)
3418 S.ReferencedSelectors.erase(Pos);
3435 if (isa<ParenListExpr>(Receiver)) {
3451 nullptr, LBracLoc, SelectorLocs,
3485 bool isIndirect =
false;
3489 type = ref->getPointeeType();
3496 type = ptr->getPointeeType();
3503 }
else if (
const ArrayType *array =
type->getAsArrayTypeUnsafe()) {
3504 type =
QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
3512 if (
type->isObjCARCBridgableType())
3517 if (
type->isObjCARCBridgableType())
3538 ACCResult merge(ACCResult left, ACCResult right) {
3539 if (left == right)
return left;
3540 if (left == ACC_bottom)
return right;
3541 if (right == ACC_bottom)
return left;
3547 class ARCCastChecker :
public StmtVisitor<ARCCastChecker, ACCResult> {
3557 return type->isCARCBridgableType();
3563 : Context(Context), SourceClass(source), TargetClass(target),
3564 Diagnose(diagnose) {}
3567 ACCResult Visit(
Expr *e) {
3571 ACCResult VisitStmt(
Stmt *
s) {
3576 ACCResult VisitExpr(
Expr *e) {
3592 ACCResult VisitCastExpr(
CastExpr *e) {
3594 case CK_NullToPointer:
3598 case CK_LValueToRValue:
3600 case CK_CPointerToObjCPointerCast:
3601 case CK_BlockPointerToObjCPointerCast:
3602 case CK_AnyPointerToBlockPointerCast:
3617 return Visit(e->
getRHS());
3623 if (left == ACC_invalid)
return ACC_invalid;
3634 ACCResult VisitStmtExpr(
StmtExpr *e) {
3645 !
var->hasDefinition(Context) &&
3646 var->getType().isConstQualified()) {
3653 return ACC_plusZero;
3661 ACCResult VisitCallExpr(
CallExpr *e) {
3663 if (ACCResult result = checkCallToFunction(fn))
3666 return super::VisitCallExpr(e);
3678 if (fn->
hasAttr<CFReturnsNotRetainedAttr>())
3679 return ACC_plusZero;
3684 if (fn->
hasAttr<CFReturnsRetainedAttr>())
3685 return Diagnose ? ACC_plusOne
3690 if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
3694 if (!fn->
hasAttr<CFAuditedTransferAttr>())
3699 return Diagnose ? ACC_plusOne
3702 return ACC_plusZero;
3715 return checkCallToMethod(method);
3719 if (!method)
return ACC_invalid;
3728 if (method->
hasAttr<CFReturnsNotRetainedAttr>())
3729 return ACC_plusZero;
3733 if (method->
hasAttr<CFReturnsRetainedAttr>())
3745 return ACC_plusZero;
3760template <
typename DiagBuilderT>
3764 Expr *realCast,
const char *bridgeKeyword,
const char *CFBridgeName) {
3780 NCE->getAngleBrackets().getEnd());
3784 char PrevChar = *
SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
3788 BridgeCall += CFBridgeName;
3795 castedE = CCE->getSubExpr();
3802 char PrevChar = *
SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
3806 BridgeCall += CFBridgeName;
3808 if (isa<ParenExpr>(castedE)) {
3826 std::string castCode =
"(";
3827 castCode += bridgeKeyword;
3831 NCE->getAngleBrackets().getEnd());
3835 std::string castCode =
"(";
3836 castCode += bridgeKeyword;
3841 if (isa<ParenExpr>(castedE)) {
3855template <
typename T>
3862 for (
auto *Redecl : RT->getDecl()->getMostRecentDecl()->redecls()) {
3863 if (
auto *
attr = Redecl->getAttr<
T>())
3874 TDNDecl = TD->getDecl();
3875 if (ObjCBridgeRelatedAttr *ObjCBAttr =
3876 getObjCBridgeAttr<ObjCBridgeRelatedAttr>(TD))
3893 UnavailableAttr::IR_ARCForbiddenConversion))
3906 unsigned srcKind = 0;
3930 S.
Diag(loc, diag::err_arc_cast_requires_bridge)
3939 ACCResult CreateRule =
3941 assert(CreateRule != ACC_bottom &&
"This cast should already be accepted.");
3942 if (CreateRule != ACC_plusOne)
3945 ? S.
Diag(noteLoc, diag::note_arc_bridge)
3946 : S.
Diag(noteLoc, diag::note_arc_cstyle_bridge);
3949 castType,
castExpr, realCast,
"__bridge ",
3952 if (CreateRule != ACC_plusZero)
3955 ? S.
Diag(noteLoc, diag::note_arc_cstyle_bridge_transfer)
3958 diag::note_arc_bridge_transfer)
3959 << castExprType << br;
3962 castType,
castExpr, realCast,
"__bridge_transfer ",
3963 br ?
"CFBridgingRelease" :
nullptr);
3972 S.
Diag(loc, diag::err_arc_cast_requires_bridge)
3980 ACCResult CreateRule =
3982 assert(CreateRule != ACC_bottom &&
"This cast should already be accepted.");
3983 if (CreateRule != ACC_plusOne)
3986 ? S.
Diag(noteLoc, diag::note_arc_bridge)
3987 : S.
Diag(noteLoc, diag::note_arc_cstyle_bridge);
3989 castType,
castExpr, realCast,
"__bridge ",
3992 if (CreateRule != ACC_plusZero)
3995 ? S.
Diag(noteLoc, diag::note_arc_cstyle_bridge_retained)
3998 diag::note_arc_bridge_retained)
4002 castType,
castExpr, realCast,
"__bridge_retained ",
4003 br ?
"CFBridgingRetain" :
nullptr);
4009 S.
Diag(loc, diag::err_arc_mismatched_cast)
4011 << srcKind << castExprType << castType
4012 << castRange <<
castExpr->getSourceRange();
4015template <
typename TB>
4017 bool &HadTheAttribute,
bool warn) {
4019 HadTheAttribute =
false;
4022 if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
4024 HadTheAttribute =
true;
4025 if (Parm->isStr(
"id"))
4038 = InterfacePointerType->getObjectType()->getInterface();
4039 if ((CastClass == ExprClass) ||
4043 S.
Diag(
castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
4048 castType, ExprClass)))
4055 S.
Diag(
castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
4056 <<
T <<
Target->getName() << castType;
4058 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4065 diag::err_objc_cf_bridged_not_interface)
4078template <
typename TB>
4080 bool &HadTheAttribute,
bool warn) {
4082 HadTheAttribute =
false;
4085 if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
4087 HadTheAttribute =
true;
4088 if (Parm->isStr(
"id"))
4100 castExpr->getType()->getAsObjCInterfacePointerType()) {
4102 = InterfacePointerType->getObjectType()->getInterface();
4103 if ((CastClass == ExprClass) ||
4108 diag::warn_objc_invalid_bridge_to_cf)
4109 <<
castExpr->getType()->getPointeeType() <<
T;
4113 }
else if (
castExpr->getType()->isObjCIdType() ||
4123 diag::warn_objc_invalid_bridge_to_cf)
4124 <<
castExpr->getType() << castType;
4126 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4133 diag::err_objc_ns_bridged_invalid_cfobject)
4134 <<
castExpr->getType() << castType;
4137 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4154 bool HasObjCBridgeAttr;
4155 bool ObjCBridgeAttrWillNotWarn = CheckObjCBridgeNSCast<ObjCBridgeAttr>(
4157 if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
4159 bool HasObjCBridgeMutableAttr;
4160 bool ObjCBridgeMutableAttrWillNotWarn =
4161 CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(
4163 if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
4166 if (HasObjCBridgeAttr)
4168 HasObjCBridgeAttr,
true);
4169 else if (HasObjCBridgeMutableAttr)
4170 CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(
4174 bool HasObjCBridgeAttr;
4175 bool ObjCBridgeAttrWillNotWarn = CheckObjCBridgeCFCast<ObjCBridgeAttr>(
4177 if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
4179 bool HasObjCBridgeMutableAttr;
4180 bool ObjCBridgeMutableAttrWillNotWarn =
4181 CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(
4183 if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
4186 if (HasObjCBridgeAttr)
4188 HasObjCBridgeAttr,
true);
4189 else if (HasObjCBridgeMutableAttr)
4190 CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(
4198 if (PRE->isExplicitProperty()) {
4200 SrcType = PDecl->getType();
4202 else if (PRE->isImplicitProperty()) {
4204 SrcType = Getter->getReturnType();
4227 : CK_CPointerToObjCPointerCast;
4239 QualType T = CfToNs ? SrcType : DestType;
4255 Diag(
Loc, diag::err_objc_bridged_related_invalid_class) << RCId
4256 << SrcType << DestType;
4263 RelatedClass = cast<ObjCInterfaceDecl>(
Target);
4266 Diag(
Loc, diag::err_objc_bridged_related_invalid_class_name) << RCId
4267 << SrcType << DestType;
4270 Diag(
Target->getBeginLoc(), diag::note_declared_at);
4276 if (CfToNs && CMId) {
4281 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4282 << SrcType << DestType << Sel <<
false;
4290 if (!CfToNs && IMId) {
4292 InstanceMethod = RelatedClass->
lookupMethod(Sel,
true);
4293 if (!InstanceMethod) {
4295 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4296 << SrcType << DestType << Sel <<
true;
4315 if (!CfToNs && !NsToCf)
4323 ClassMethod, InstanceMethod, TDNDecl,
4331 std::string ExpressionString =
"[";
4333 ExpressionString +=
" ";
4334 ExpressionString += ClassMethod->getSelector().getAsString();
4338 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4339 << SrcType << DestType << ClassMethod->getSelector() <<
false
4348 Expr *args[] = { SrcExpr };
4350 ClassMethod->getLocation(),
4351 ClassMethod->getSelector(), ClassMethod,
4353 SrcExpr = msg.
get();
4360 if (InstanceMethod) {
4362 std::string ExpressionString;
4365 if (InstanceMethod->isPropertyAccessor())
4367 InstanceMethod->findPropertyDecl()) {
4369 ExpressionString =
".";
4370 ExpressionString += PDecl->getNameAsString();
4371 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4372 << SrcType << DestType << InstanceMethod->getSelector() <<
true
4375 if (ExpressionString.empty()) {
4377 ExpressionString =
" ";
4378 ExpressionString += InstanceMethod->getSelector().getAsString();
4379 ExpressionString +=
"]";
4381 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4382 << SrcType << DestType << InstanceMethod->getSelector() <<
true
4390 SrcExpr, SrcType, InstanceMethod->getLocation(),
4391 InstanceMethod->getSelector(), InstanceMethod, std::nullopt);
4392 SrcExpr = msg.
get();
4403 bool Diagnose,
bool DiagnoseCFAudited,
4416 if (exprACTC == castACTC) {
4422 castType != castExprType) {
4428 if (
const ParenType *PT = dyn_cast<ParenType>(DT))
4429 QDT = PT->desugar();
4430 else if (
const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
4431 QDT = TP->desugar();
4432 else if (
const AttributedType *AT = dyn_cast<AttributedType>(DT))
4433 QDT = AT->desugar();
4434 if (QDT != castType &&
4439 Diag(loc, diag::err_arc_nolifetime_behavior);
4473 switch (ARCCastChecker(Context, exprACTC, castACTC,
false).Visit(
castExpr)) {
4486 CK_ARCConsumeObject,
castExpr,
nullptr,
4513 (Opc == BO_NE || Opc == BO_EQ))) {
4535 castType =
cast->getTypeAsWritten();
4538 castRange =
cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange();
4539 castType =
cast->getTypeAsWritten();
4542 llvm_unreachable(
"Unexpected ImplicitCastExpr");
4561 if (
ParenExpr *pe = dyn_cast<ParenExpr>(e)) {
4563 return new (Context)
ParenExpr(pe->getLParen(), pe->getRParen(), sub);
4564 }
else if (
UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
4565 assert(uo->getOpcode() == UO_Extension);
4569 uo->getOperatorLoc(),
false,
4572 assert(!gse->isResultDependent());
4573 assert(!gse->isTypePredicate());
4575 unsigned n = gse->getNumAssocs();
4578 subExprs.reserve(n);
4579 subTypes.reserve(n);
4581 subTypes.push_back(assoc.getTypeSourceInfo());
4582 Expr *sub = assoc.getAssociationExpr();
4583 if (assoc.isSelected())
4585 subExprs.push_back(sub);
4589 Context, gse->getGenericLoc(), gse->getControllingExpr(), subTypes,
4590 subExprs, gse->getDefaultLoc(), gse->getRParenLoc(),
4591 gse->containsUnexpandedParameterPack(), gse->getResultIndex());
4593 assert(isa<ImplicitCastExpr>(e) &&
"bad form of unbridged cast!");
4594 return cast<ImplicitCastExpr>(e)->getSubExpr();
4605 if (isa<ObjCObjectPointerType>(canCastType) &&
4611 return !ObjI->isArcWeakrefUnavailable();
4618 Expr *curExpr = e, *prevExpr =
nullptr;
4623 if (
auto *pe = dyn_cast<ParenExpr>(curExpr)) {
4625 curExpr = pe->getSubExpr();
4629 if (
auto *ce = dyn_cast<CastExpr>(curExpr)) {
4630 if (
auto *ice = dyn_cast<ImplicitCastExpr>(ce))
4631 if (ice->getCastKind() == CK_ARCReclaimReturnedObject) {
4633 return ice->getSubExpr();
4634 if (
auto *pe = dyn_cast<ParenExpr>(prevExpr))
4635 pe->setSubExpr(ice->getSubExpr());
4637 cast<CastExpr>(prevExpr)->setSubExpr(ice->getSubExpr());
4642 curExpr = ce->getSubExpr();
4661 SubExpr = SubResult.
get();
4668 bool MustConsume =
false;
4675 : CK_CPointerToObjCPointerCast);
4682 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4689 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4691 Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
4694 br ?
"CFBridgingRelease "
4695 :
"__bridge_transfer ");
4725 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4733 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4735 Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
4738 br ?
"CFBridgingRetain " :
"__bridge_retained");
4745 Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
4746 << FromType <<
T << Kind