30#include "llvm/ADT/SmallString.h"
31#include "llvm/Support/ConvertUTF.h"
48 if (Strings.size() != 1) {
53 for (
Expr *
E : Strings) {
54 S = cast<StringLiteral>(
E);
57 if (!S->isOrdinary()) {
58 Diag(S->getBeginLoc(), diag::err_cfstring_literal_not_string_constant)
59 << S->getSourceRange();
64 StrBuf += S->getString();
67 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
73 assert(CAT &&
"String literal not of constant array type!");
75 CAT->
getElementType(), llvm::APInt(32, StrBuf.size() + 1),
nullptr,
78 false, StrTy, &StrLocs[0],
101 std::string StringClass(
getLangOpts().ObjCConstantStringClass);
103 if (StringClass.empty())
104 NSIdent = &Context.
Idents.
get(
"NSConstantString");
106 NSIdent = &Context.
Idents.
get(StringClass);
117 Diag(S->getBeginLoc(), diag::err_no_nsconstant_string_class)
118 << NSIdent << S->getSourceRange();
158 S.
Diag(
Loc, diag::err_undeclared_boxing_method) << Sel <<
Class->getName();
165 S.
Diag(
Loc, diag::err_objc_literal_method_sig)
178 switch (LiteralKind) {
196 llvm_unreachable(
"LiteralKind can't be converted into a ClassKind");
209 S.
Diag(
Loc, diag::err_undeclared_objc_literal_class)
210 << II->
getName() << LiteralKind;
212 }
else if (!
Decl->hasDefinition() && !S.
getLangOpts().DebuggerObjCLiteral) {
213 S.
Diag(
Loc, diag::err_undeclared_objc_literal_class)
214 <<
Decl->getName() << LiteralKind;
251 bool isLiteral =
false,
253 std::optional<NSAPI::NSNumberLiteralMethodKind> Kind =
254 S.NSAPIObj->getNSNumberFactoryMethodKind(NumberType);
258 S.
Diag(
Loc, diag::err_invalid_nsnumber_type)
265 if (S.NSNumberLiteralMethods[*Kind])
266 return S.NSNumberLiteralMethods[*Kind];
268 Selector Sel = S.NSAPIObj->getNSNumberLiteralSelector(*Kind,
275 if (!S.NSNumberDecl) {
278 if (!S.NSNumberDecl) {
283 if (S.NSNumberPointer.isNull()) {
291 if (!Method && S.
getLangOpts().DebuggerObjCLiteral) {
296 ReturnTInfo, S.NSNumberDecl,
306 NumberType,
nullptr,
SC_None,
nullptr);
316 S.NSNumberLiteralMethods[*Kind] = Method;
330 switch (Char->getKind()) {
333 NumberType = Context.
CharTy;
366 Number = ConvertedNumber.
get();
379 Value ? tok::kw_true : tok::kw_false);
385 CK_IntegralToBoolean);
395 bool ArrayLiteral =
false) {
397 if (Element->isTypeDependent())
407 if (S.
getLangOpts().CPlusPlus && Element->getType()->isRecordType()) {
415 return Seq.Perform(S, Entity, Kind, Element);
418 Expr *OrigElement = Element;
427 if (!Element->getType()->isObjCObjectPointerType() &&
428 !Element->getType()->isBlockPointerType()) {
429 bool Recovered =
false;
432 if (isa<IntegerLiteral>(OrigElement) ||
433 isa<CharacterLiteral>(OrigElement) ||
434 isa<FloatingLiteral>(OrigElement) ||
435 isa<ObjCBoolLiteralExpr>(OrigElement) ||
436 isa<CXXBoolLiteralExpr>(OrigElement)) {
439 int Which = isa<CharacterLiteral>(OrigElement) ? 1
440 : (isa<CXXBoolLiteralExpr>(OrigElement) ||
441 isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2
458 else if (
StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) {
459 if (String->isOrdinary()) {
475 S.
Diag(Element->getBeginLoc(), diag::err_invalid_collection_element)
476 << Element->getType();
482 dyn_cast<ObjCStringLiteral>(OrigElement)) {
484 unsigned numConcat = SL->getNumConcatenated();
487 bool hasMacro =
false;
488 for (
unsigned i = 0; i < numConcat ; ++i)
489 if (SL->getStrTokenLoc(i).isMacroID()) {
494 S.
Diag(Element->getBeginLoc(),
495 diag::warn_concatenated_nsarray_literal)
496 << Element->getType();
506 Element->getBeginLoc(), Element);
524 ValueExpr = RValue.
get();
542 if (
auto *CE = dyn_cast<ImplicitCastExpr>(ValueExpr))
543 if (CE->getCastKind() == CK_ArrayToPointerDecay)
545 dyn_cast<StringLiteral>(CE->getSubExpr()->IgnoreParens())) {
546 assert((SL->isOrdinary() || SL->isUTF8()) &&
547 "unexpected character encoding");
548 StringRef Str = SL->getString();
549 const llvm::UTF8 *StrBegin = Str.bytes_begin();
550 const llvm::UTF8 *StrEnd = Str.bytes_end();
552 if (llvm::isLegalUTF8String(&StrBegin, StrEnd)) {
557 return new (Context)
ObjCBoxedExpr(CE, BoxedType,
nullptr, SR);
560 Diag(SL->getBeginLoc(), diag::warn_objc_boxing_invalid_utf8_string)
570 if (!BoxingMethod &&
getLangOpts().DebuggerObjCLiteral) {
595 stringWithUTF8String, BoxingMethod))
604 std::optional<NullabilityKind> Nullability =
619 dyn_cast<CharacterLiteral>(ValueExpr->
IgnoreParens())) {
622 switch (Char->getKind()) {
625 ValueType = Context.
CharTy;
647 if (!ET->getDecl()->isComplete()) {
648 Diag(
Loc, diag::err_objc_incomplete_boxed_expression_type)
654 ET->getDecl()->getIntegerType());
680 if (!BoxingMethod &&
getLangOpts().DebuggerObjCLiteral) {
703 Params.push_back(
bytes);
713 Params.push_back(
type);
720 ValueWithBytesObjCType, BoxingMethod))
727 Diag(
Loc, diag::err_objc_non_trivially_copyable_boxed_expression_type)
737 Diag(
Loc, diag::err_objc_illegal_boxed_expression_type)
760 ValueExpr = ConvertedValueExpr.
get();
773 assert(!
getLangOpts().isSubscriptPointerArithmetic());
779 "base or index cannot have dependent type here");
797 getterMethod, setterMethod, RB);
819 if (!Method &&
getLangOpts().DebuggerObjCLiteral) {
836 Params.push_back(objects);
844 Params.push_back(cnt);
859 diag::note_objc_literal_method_param)
866 if (!Method->
parameters()[1]->getType()->isIntegerType()) {
870 diag::note_objc_literal_method_param)
886 Expr **ElementsBuffer = Elements.data();
887 for (
unsigned I = 0, N = Elements.size(); I != N; ++I) {
889 SemaRef, ElementsBuffer[I], RequiredType,
true);
893 ElementsBuffer[I] = Converted.
get();
909 if (Literal->isValueDependent() || Literal->isTypeDependent())
915 struct APSIntCompare {
916 bool operator()(
const llvm::APSInt &LHS,
const llvm::APSInt &RHS)
const {
917 return llvm::APSInt::compareValues(LHS, RHS) < 0;
921 llvm::DenseMap<StringRef, SourceLocation> StringKeys;
922 std::map<llvm::APSInt, SourceLocation, APSIntCompare> IntegralKeys;
925 auto Pair = Map.insert({Key,
Loc});
927 S.
Diag(
Loc, diag::warn_nsdictionary_duplicate_key);
928 S.
Diag(Pair.first->second, diag::note_nsdictionary_duplicate_key_here);
932 for (
unsigned Idx = 0, End = Literal->getNumElements(); Idx != End; ++Idx) {
933 Expr *Key = Literal->getKeyValueElement(Idx).Key->IgnoreParenImpCasts();
935 if (
auto *StrLit = dyn_cast<ObjCStringLiteral>(Key)) {
936 StringRef Bytes = StrLit->getString()->getBytes();
938 checkOneKey(StringKeys, Bytes,
Loc);
941 if (
auto *BE = dyn_cast<ObjCBoxedExpr>(Key)) {
942 Expr *Boxed = BE->getSubExpr();
947 checkOneKey(StringKeys, Str->getBytes(),
Loc);
954 checkOneKey(IntegralKeys,
Result.Val.getInt(),
Loc);
980 if (!Method &&
getLangOpts().DebuggerObjCLiteral) {
997 Params.push_back(objects);
1005 Params.push_back(keys);
1013 Params.push_back(cnt);
1029 diag::note_objc_literal_method_param)
1063 diag::note_objc_literal_method_param)
1071 QualType CountType = Method->parameters()[2]->getType();
1075 Diag(Method->parameters()[2]->getLocation(),
1076 diag::note_objc_literal_method_param)
1086 QualType ValuesT = DictionaryWithObjectsMethod->parameters()[0]->getType();
1088 QualType KeysT = DictionaryWithObjectsMethod->parameters()[1]->getType();
1093 bool HasPackExpansions =
false;
1104 if (
Value.isInvalid())
1107 Element.Key = Key.
get();
1108 Element.Value =
Value.get();
1110 if (Element.EllipsisLoc.isInvalid())
1113 if (!Element.Key->containsUnexpandedParameterPack() &&
1114 !Element.Value->containsUnexpandedParameterPack()) {
1115 Diag(Element.EllipsisLoc,
1116 diag::err_pack_expansion_without_parameter_packs)
1118 Element.Value->getEndLoc());
1122 HasPackExpansions =
true;
1130 DictionaryWithObjectsMethod, SR);
1132 return SemaRef.MaybeBindToTemporary(Literal);
1147 diag::err_incomplete_type_objc_at_encode,
1154 if (!NotEncodedT.
isNull())
1155 Diag(AtLoc, diag::warn_incomplete_encoded_type)
1156 << EncodedType << NotEncodedT;
1163 return new (Context)
ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
1189 bool Warned =
false;
1192 if (MatchingMethodDecl == Method ||
1200 S.
Diag(AtLoc, diag::warn_multiple_selectors)
1206 S.
Diag(MatchingMethodDecl->
getLocation(), diag::note_method_declared_at)
1217 bool WarnMultipleSelectors) {
1218 if (!WarnMultipleSelectors ||
1221 bool Warned =
false;
1228 Method, InstMethList))
1234 Method, ClsMethList) || Warned)
1250 assert(Method->
getSelector() == Sel &&
"Method with wrong selector in method list");
1253 DirectMethod = Method;
1258 return DirectMethod;
1273 S, Sel,
Iter->second.first, onlyDirect, anyDirect);
1275 S, Sel,
Iter->second.second, onlyDirect, anyDirect);
1277 return DirectInstance ? DirectInstance : DirectClass;
1306 bool WarnMultipleSelectors) {
1315 Selector MatchedSel = OM->getSelector();
1318 Diag(SelLoc, diag::warn_undeclared_selector_with_typo)
1319 << Sel << MatchedSel
1323 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
1326 WarnMultipleSelectors);
1328 bool onlyDirect =
true;
1329 bool anyDirect =
false;
1334 Diag(AtLoc, diag::err_direct_selector_expression)
1338 }
else if (anyDirect) {
1344 if (LikelyTargetMethod && LikelyTargetMethod->
isDirectMethod()) {
1345 Diag(AtLoc, diag::warn_potentially_direct_selector_expression) << Sel;
1347 diag::note_direct_method_declared_at)
1349 }
else if (!LikelyTargetMethod) {
1352 Diag(AtLoc, diag::warn_strict_potentially_direct_selector_expression)
1355 diag::note_direct_method_declared_at)
1376 Diag(AtLoc, diag::err_arc_illegal_selector) <<
1406 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
1410 Diag(ProtoLoc, diag::err_objc_non_runtime_protocol_in_protocol_expr)
1413 Diag(ProtoLoc, diag::err_atprotocol_protocol) << PDecl;
1423 return new (Context)
ObjCProtocolExpr(Ty, PDecl, AtLoc, ProtoIdLoc, RParenLoc);
1469 bool isClassMessage,
1470 bool isSuperMessage) {
1471 assert(Method &&
"Must have a method");
1481 if (
auto nullability =
1506 if (isSuperMessage) {
1509 return transferNullability(
1528 return transferNullability(ReceiverType);
1534 bool isClassMessage,
1535 bool isSuperMessage) {
1539 SemaRef, ReceiverType, Method, isClassMessage, isSuperMessage);
1542 if (isClassMessage) {
1554 cast<ImplicitParamDecl>(
1556 ->getDeclContext());
1563 NewResultType, NewResultType);
1564 return NewResultType;
1576 unsigned receiverNullabilityIdx = 0;
1577 if (std::optional<NullabilityKind> nullability =
1581 receiverNullabilityIdx = 1 +
static_cast<unsigned>(*nullability);
1584 unsigned resultNullabilityIdx = 0;
1585 if (std::optional<NullabilityKind> nullability =
1589 resultNullabilityIdx = 1 +
static_cast<unsigned>(*nullability);
1594 static const uint8_t
None = 0;
1595 static const uint8_t
NonNull = 1;
1598 static const uint8_t nullabilityMap[4][4] = {
1606 unsigned newResultNullabilityIdx
1607 = nullabilityMap[receiverNullabilityIdx][resultNullabilityIdx];
1608 if (newResultNullabilityIdx == resultNullabilityIdx)
1614 if (
auto attributed = dyn_cast<AttributedType>(resultType.
getTypePtr())) {
1615 resultType = attributed->getModifiedType();
1622 if (newResultNullabilityIdx > 0) {
1627 resultType, resultType);
1646 dyn_cast<ObjCCategoryImplDecl>(impl)) {
1647 iface = catImpl->getCategoryDecl();
1649 iface = impl->getClassInterface();
1659 for (
unsigned i = 0, e = overrides.size(); i != e; ++i) {
1681 SourceRange range = overridden->getReturnTypeSourceRange();
1684 loc = overridden->getLocation();
1685 Diag(loc, diag::note_related_result_type_explicit)
1733 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
1734 SelLoc = SelectorLocs.front();
1740 for (
unsigned i = 0, e = Args.size(); i != e; i++) {
1741 if (Args[i]->isTypeDependent())
1753 Args[i] = result.
get();
1758 DiagID = diag::err_arc_method_not_found;
1760 DiagID = isClassMessage ? diag::warn_class_method_not_found
1761 : diag::warn_inst_method_not_found;
1766 DiagID = diag::err_method_not_found_with_typo;
1768 DiagID = isClassMessage ? diag::warn_class_method_not_found_with_typo
1769 : diag::warn_instance_method_not_found_with_typo;
1771 SourceRange SelectorRange(SelectorLocs.front(), SelectorLocs.back());
1773 Diag(SelLoc, DiagID)
1774 << Sel<< isClassMessage << MatchedSel
1777 Diag(SelLoc, DiagID) << Sel<< isClassMessage << MatchedSel;
1780 Diag(SelLoc, DiagID)
1781 << Sel << isClassMessage <<
SourceRange(SelectorLocs.front(),
1782 SelectorLocs.back());
1786 Diag(ThisClass->getLocation(), diag::note_receiver_class_declared);
1788 if (ThisClass->lookupClassMethod(Sel))
1789 Diag(RecRange.
getBegin(), diag::note_receiver_expr_here)
1791 ThisClass->getNameAsString());
1808 isClassMessage, isSuperMessage);
1817 if (Args.size() < NumNamedArgs) {
1818 Diag(SelLoc, diag::err_typecheck_call_too_few_args)
1819 << 2 << NumNamedArgs << static_cast<unsigned>(Args.size())
1826 std::optional<ArrayRef<QualType>> typeArgs =
1828 bool IsError =
false;
1829 for (
unsigned i = 0; i < NumNamedArgs; i++) {
1831 if (Args[i]->isTypeDependent())
1834 Expr *argExpr = Args[i];
1837 assert(argExpr &&
"CheckMessageArgumentTypes(): missing expression");
1839 if (param->
hasAttr<NoEscapeAttr>() &&
1841 if (
auto *BE = dyn_cast<BlockExpr>(
1843 BE->getBlockDecl()->setDoesNotEscape();
1848 !param->
hasAttr<CFConsumedAttr>())
1859 Args[i] = argE.
get();
1877 diag::err_call_incomplete_argument, argExpr))
1893 Args[i]->getType()->isBlockPointerType() &&
1897 Args[i] = arg.get();
1904 for (
unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) {
1905 if (Args[i]->isTypeDependent())
1911 Args[i] = Arg.
get();
1915 if (Args.size() != NumNamedArgs) {
1916 Diag(Args[NumNamedArgs]->getBeginLoc(),
1917 diag::err_typecheck_call_too_many_args)
1918 << 2 << NumNamedArgs << static_cast<unsigned>(Args.size())
1921 Args.back()->getEndLoc());
1942 if (!method)
return false;
1945 if (
DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver))
1957 if (
ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance))
1962 if (
ObjCMethodDecl *method = iface->lookupPrivateMethod(sel, isInstance))
1967 for (
const auto *I : objType->
quals())
1979 for (
const auto *PROTO : OPT->
quals()) {
1980 if ((MD = PROTO->lookupMethod(Sel, Instance))) {
1998 Diag(MemberLoc, diag::err_invalid_property_name)
2008 diag::err_property_not_found_forward_class,
2009 MemberName, BaseRange))
2018 return new (Context)
2022 return new (Context)
2027 for (
const auto *I : OPT->
quals())
2037 SuperLoc, SuperType);
2039 return new (Context)
2093 if (!(PDecl->getPropertyAttributes() &
2096 diag::warn_property_access_suggest)
2097 << MemberName <<
QualType(OPT, 0) << PDecl->getName()
2102 if (Getter || Setter) {
2104 return new (Context)
2108 return new (Context)
2124 Corrected.isKeyword() ? nullptr : Corrected.getFoundDecl();
2125 if (ChosenDecl && isa<ObjCPropertyDecl>(ChosenDecl))
2126 if (cast<ObjCPropertyDecl>(ChosenDecl)->isClassProperty()) {
2129 Diag(MemberLoc, diag::err_class_property_found) << MemberName
2137 PDiag(diag::err_property_not_found_suggest)
2138 << MemberName <<
QualType(OPT, 0));
2140 TypoResult, MemberLoc,
2141 SuperLoc, SuperType, Super);
2151 diag::err_property_not_as_forward_class,
2152 MemberName, BaseExpr))
2156 diag::err_ivar_access_using_property_syntax_suggest)
2157 << MemberName <<
QualType(OPT, 0) << Ivar->getDeclName()
2162 Diag(MemberLoc, diag::err_property_not_found)
2182 if (receiverNamePtr->
isStr(
"super")) {
2184 if (
auto classDecl = CurMethod->getClassInterface()) {
2185 SuperType =
QualType(classDecl->getSuperClassType(), 0);
2186 if (CurMethod->isInstanceMethod()) {
2187 if (SuperType.
isNull()) {
2189 Diag(receiverNameLoc, diag::err_root_class_cannot_use_super)
2190 << CurMethod->getClassInterface()->getIdentifier();
2200 receiverNameLoc,
T,
true);
2211 Diag(receiverNameLoc, diag::err_expected_either) << tok::identifier
2221 GetterSel = PD->getGetterName();
2222 SetterSel = PD->getSetterName();
2258 if (Getter || Setter) {
2260 return new (Context)
2267 propertyNameLoc, receiverNameLoc, IFace);
2269 return ExprError(
Diag(propertyNameLoc, diag::err_property_not_found)
2283 bool ValidateCandidate(
const TypoCorrection &candidate)
override {
2288 std::unique_ptr<CorrectionCandidateCallback> clone()
override {
2289 return std::make_unique<ObjCInterfaceOrSuperCCC>(*
this);
2298 bool HasTrailingDot,
ParsedType &ReceiverType) {
2300 ReceiverType =
nullptr;
2305 if (IsSuper && S->isInObjcMethodScope())
2311 switch (
Result.getResultKind()) {
2336 Result.suppressDiagnostics();
2367 Result.getLookupNameInfo(),
Result.getLookupKind(), S,
nullptr, CCC,
2369 if (Corrected.isKeyword()) {
2401 Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
2407 Diag(SuperLoc, diag::err_no_super_class_message)
2415 Diag(SuperLoc, diag::err_root_class_cannot_use_super)
2416 <<
Class->getIdentifier();
2431 LBracLoc, SelectorLocs, RBracLoc, Args);
2438 SuperLoc, Sel,
nullptr,
2439 LBracLoc, SelectorLocs, RBracLoc, Args);
2443 bool isSuperReceiver,
2449 if (!ReceiverType.
isNull())
2452 assert(((isSuperReceiver &&
Loc.
isValid()) || receiverTypeInfo) &&
2453 "Either the super receiver location needs to be valid or the receiver "
2454 "needs valid type source information");
2472 auto Builder = S.
Diag(MsgLoc, DiagID)
2480 switch (Edit.
Kind) {
2487 Builder.AddFixItHint(
2508 bool IsClassObjectCall) {
2514 const auto *SE = dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens());
2518 if (!IsClassObjectCall) {
2520 if (!OPT || !OPT->getInterfaceDecl())
2523 OPT->getInterfaceDecl()->lookupInstanceMethod(SE->getSelector());
2526 OPT->getInterfaceDecl()->lookupPrivateMethod(SE->getSelector());
2531 ImpliedMethod = IT->getDecl()->lookupClassMethod(SE->getSelector());
2534 IT->getDecl()->lookupPrivateClassMethod(SE->getSelector());
2539 if (Ret->isRecordType() || Ret->isVectorType() || Ret->isExtVectorType()) {
2540 S.
Diag(
Loc, diag::warn_objc_unsafe_perform_selector)
2542 << (!Ret->isRecordType()
2544 : Ret->isUnionType() ? 1 : 0);
2546 diag::note_objc_unsafe_perform_selector_method_declared_here)
2557 Expr **Args,
unsigned NumArgs) {
2559 bool Format =
false;
2573 if (!Format || NumArgs <= Idx)
2576 Expr *FormatExpr = Args[Idx];
2581 S.
Diag(FormatExpr->
getExprLoc(), diag::warn_objc_cdirective_format_string)
2627 Diag(
Loc, diag::err_missing_open_square_message_send)
2632 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2633 SelectorSlotLocs = SelectorLocs;
2635 SelectorSlotLocs =
Loc;
2641 unsigned NumArgs = ArgsIn.size();
2642 Expr **Args = ArgsIn.data();
2643 assert(SuperLoc.
isInvalid() &&
"Message to super with dependent type");
2645 ReceiverTypeInfo, Sel, SelectorLocs,
2647 RBracLoc, isImplicit);
2654 Diag(
Loc, diag::err_invalid_receiver_class_message)
2658 assert(
Class &&
"We don't know which class we're messaging?");
2669 ? diag::err_arc_receiver_forward_class
2670 : diag::warn_receiver_forward_class),
2680 Method =
Class->lookupClassMethod(Sel);
2684 Method =
Class->lookupPrivateClassMethod(Sel);
2687 false,
false,
Class))
2695 unsigned NumArgs = ArgsIn.size();
2696 Expr **Args = ArgsIn.data();
2699 Method,
true, SuperLoc.
isValid(), LBracLoc,
2706 diag::err_illegal_message_expr_incomplete_type))
2710 Diag(SuperLoc, diag::err_messaging_super_with_direct_method)
2725 Diag(
Loc, diag::warn_direct_initialize_call);
2732 Diag(
Loc, diag::warn_direct_super_initialize_call);
2735 Diag(CurMeth->getLocation(), diag::note_method_declared_at)
2736 << CurMeth->getDeclName();
2747 Context, ReturnType, VK, LBracLoc, SuperLoc,
false,
2748 ReceiverType, Sel, SelectorLocs, Method,
ArrayRef(Args, NumArgs),
2749 RBracLoc, isImplicit);
2752 Context, ReturnType, VK, LBracLoc, ReceiverTypeInfo, Sel, SelectorLocs,
2753 Method,
ArrayRef(Args, NumArgs), RBracLoc, isImplicit);
2759 ReceiverType,
true);
2775 if (ReceiverType.
isNull())
2778 if (!ReceiverTypeInfo)
2783 nullptr, LBracLoc, SelectorLocs, RBracLoc,
2799 const auto *Protocol = dyn_cast<ObjCProtocolDecl>(M->
getDeclContext());
2804 if (
const auto *RootClass = dyn_cast_or_null<ObjCInterfaceDecl>(
2808 if (
P->getCanonicalDecl() == Protocol->getCanonicalDecl())
2848 assert((Receiver || SuperLoc.
isValid()) &&
"If the Receiver is null, the "
2849 "SuperLoc must be valid so we can "
2858 if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
2859 SelectorSlotLocs = SelectorLocs;
2861 SelectorSlotLocs =
Loc;
2865 Diag(
Loc, diag::err_missing_open_square_message_send)
2887 unsigned NumArgs = ArgsIn.size();
2888 Expr **Args = ArgsIn.data();
2889 assert(SuperLoc.
isInvalid() &&
"Message to super with dependent type");
2892 SelectorLocs,
nullptr,
ArrayRef(Args, NumArgs), RBracLoc,
2902 ReceiverType = Receiver->
getType();
2915 Diag(
Loc, diag::warn_bad_receiver_type) << ReceiverType << RecRange;
2919 CK_CPointerToObjCPointerCast)
2925 CastKind Kind =
IsNull ? CK_NullToPointer : CK_IntegralToPointer;
2930 ReceiverType = Receiver->
getType();
2934 diag::err_incomplete_receiver_type))
2940 Receiver = result.
get();
2941 ReceiverType = Receiver->
getType();
2962 if (!Methods.empty()) {
2965 Method = Methods[0];
2969 Method = BestMethod;
2973 receiverIsIdLike, Methods))
2991 Diag(SelLoc, diag::warn_instance_method_on_class_found)
3004 Method = ClassDecl->lookupClassMethod(Sel);
3007 Method = ClassDecl->lookupPrivateClassMethod(Sel);
3022 if (!Methods.empty()) {
3025 Method = Methods[0];
3031 if (ID->getSuperClass())
3032 Diag(SelLoc, diag::warn_root_inst_method_not_found)
3039 Method = BestMethod;
3061 ClassDecl = OCIType->getInterfaceDecl();
3069 Loc, OCIType->getPointeeType(),
3071 ? diag::err_arc_receiver_forward_instance
3072 : diag::warn_receiver_forward_instance,
3077 forwardClass = OCIType->getInterfaceDecl();
3079 diag::note_receiver_is_id);
3094 Diag(SelLoc, diag::err_arc_may_not_respond)
3095 << OCIType->getPointeeType() << Sel << RecRange
3096 <<
SourceRange(SelectorLocs.front(), SelectorLocs.back());
3100 if (!Method && (!Receiver || !
isSelfExpr(Receiver))) {
3104 if (OCIType->qual_empty()) {
3109 if (!Methods.empty()) {
3112 Method = Methods[0];
3116 Method = BestMethod;
3123 if (Method && !forwardClass)
3124 Diag(SelLoc, diag::warn_maynot_respond)
3125 << OCIType->getInterfaceDecl()->getIdentifier()
3135 Diag(
Loc, diag::err_bad_receiver_type) << ReceiverType << RecRange;
3149 diag::err_messaging_unqualified_id_with_direct_method);
3161 diag::err_messaging_class_with_direct_method);
3174 Diag(SuperLoc, diag::err_messaging_super_with_direct_method);
3185 }
else if (ReceiverType->
isObjCIdType() && !isImplicit) {
3186 Diag(Receiver->
getExprLoc(), diag::warn_messaging_unqualified_id);
3189 if (DIFunctionScopeInfo &&
3192 bool isDesignatedInitChain =
false;
3199 if (!ID->declaresOrInheritsDesignatedInitializers() ||
3200 ID->isDesignatedInitializer(Sel)) {
3201 isDesignatedInitChain =
true;
3207 if (!isDesignatedInitChain) {
3210 assert(CurMD &&
"Current method declaration should not be null");
3212 CurMD->isDesignatedInitializerForTheInterface(&InitMethod);
3213 assert(isDesignated && InitMethod);
3216 diag::warn_objc_designated_init_non_designated_init_call :
3217 diag::warn_objc_designated_init_non_super_designated_init_call);
3219 diag::note_objc_designated_init_marked_here);
3223 if (DIFunctionScopeInfo &&
3227 Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call);
3234 unsigned NumArgs = ArgsIn.size();
3235 Expr **Args = ArgsIn.data();
3242 Method, ClassMessage, SuperLoc.
isValid(),
3243 LBracLoc, RBracLoc, RecRange, ReturnType, VK))
3249 diag::err_illegal_message_expr_incomplete_type))
3278 Diag(SelLoc, diag::err_arc_illegal_explicit_message)
3283 if (Method && NumArgs >= 1) {
3284 if (
const auto *SelExp =
3285 dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens())) {
3286 Selector ArgSel = SelExp->getSelector();
3289 SelExp->getSourceRange());
3293 SelExp->getSourceRange());
3296 switch (SelFamily) {
3303 if (!SelMethod->
hasAttr<NSReturnsNotRetainedAttr>()) {
3306 diag::err_arc_perform_selector_retains);
3313 if (SelMethod->
hasAttr<NSReturnsRetainedAttr>()) {
3316 diag::err_arc_perform_selector_retains);
3325 Diag(SelLoc, diag::warn_arc_perform_selector_leaks);
3326 Diag(Args[0]->getExprLoc(), diag::note_used_here);
3339 Context, ReturnType, VK, LBracLoc, SuperLoc,
true,
3340 ReceiverType, Sel, SelectorLocs, Method,
ArrayRef(Args, NumArgs),
3341 RBracLoc, isImplicit);
3344 Context, ReturnType, VK, LBracLoc, Receiver, Sel, SelectorLocs, Method,
3345 ArrayRef(Args, NumArgs), RBracLoc, isImplicit);
3350 bool IsClassObjectCall = ClassMessage;
3357 if (OPT->getObjectType()->isObjCClass()) {
3359 IsClassObjectCall =
true;
3367 ReceiverType, IsClassObjectCall);
3380 Result->setDelegateInitCall(
true);
3391 if (!isImplicit && Method) {
3415 auto Pos = S.ReferencedSelectors.find(Sel);
3416 if (Pos != S.ReferencedSelectors.end() && Pos->second ==
Loc)
3417 S.ReferencedSelectors.erase(Pos);
3434 if (isa<ParenListExpr>(Receiver)) {
3450 nullptr, LBracLoc, SelectorLocs,
3484 bool isIndirect =
false;
3488 type = ref->getPointeeType();
3495 type = ptr->getPointeeType();
3502 }
else if (
const ArrayType *array =
type->getAsArrayTypeUnsafe()) {
3503 type =
QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
3511 if (
type->isObjCARCBridgableType())
3516 if (
type->isObjCARCBridgableType())
3537 ACCResult merge(ACCResult left, ACCResult right) {
3538 if (left == right)
return left;
3539 if (left == ACC_bottom)
return right;
3540 if (right == ACC_bottom)
return left;
3546 class ARCCastChecker :
public StmtVisitor<ARCCastChecker, ACCResult> {
3556 return type->isCARCBridgableType();
3562 : Context(Context), SourceClass(source), TargetClass(target),
3563 Diagnose(diagnose) {}
3566 ACCResult Visit(
Expr *e) {
3570 ACCResult VisitStmt(
Stmt *
s) {
3575 ACCResult VisitExpr(
Expr *e) {
3591 ACCResult VisitCastExpr(
CastExpr *e) {
3593 case CK_NullToPointer:
3597 case CK_LValueToRValue:
3599 case CK_CPointerToObjCPointerCast:
3600 case CK_BlockPointerToObjCPointerCast:
3601 case CK_AnyPointerToBlockPointerCast:
3616 return Visit(e->
getRHS());
3622 if (left == ACC_invalid)
return ACC_invalid;
3633 ACCResult VisitStmtExpr(
StmtExpr *e) {
3644 !
var->hasDefinition(Context) &&
3645 var->getType().isConstQualified()) {
3652 return ACC_plusZero;
3660 ACCResult VisitCallExpr(
CallExpr *e) {
3662 if (ACCResult result = checkCallToFunction(fn))
3665 return super::VisitCallExpr(e);
3677 if (fn->
hasAttr<CFReturnsNotRetainedAttr>())
3678 return ACC_plusZero;
3683 if (fn->
hasAttr<CFReturnsRetainedAttr>())
3684 return Diagnose ? ACC_plusOne
3689 if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
3693 if (!fn->
hasAttr<CFAuditedTransferAttr>())
3698 return Diagnose ? ACC_plusOne
3701 return ACC_plusZero;
3714 return checkCallToMethod(method);
3718 if (!method)
return ACC_invalid;
3727 if (method->
hasAttr<CFReturnsNotRetainedAttr>())
3728 return ACC_plusZero;
3732 if (method->
hasAttr<CFReturnsRetainedAttr>())
3744 return ACC_plusZero;
3759template <
typename DiagBuilderT>
3763 Expr *realCast,
const char *bridgeKeyword,
const char *CFBridgeName) {
3779 NCE->getAngleBrackets().getEnd());
3783 char PrevChar = *
SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
3787 BridgeCall += CFBridgeName;
3794 castedE = CCE->getSubExpr();
3801 char PrevChar = *
SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
3805 BridgeCall += CFBridgeName;
3807 if (isa<ParenExpr>(castedE)) {
3825 std::string castCode =
"(";
3826 castCode += bridgeKeyword;
3830 NCE->getAngleBrackets().getEnd());
3834 std::string castCode =
"(";
3835 castCode += bridgeKeyword;
3840 if (isa<ParenExpr>(castedE)) {
3854template <
typename T>
3861 for (
auto *Redecl : RT->getDecl()->getMostRecentDecl()->redecls()) {
3862 if (
auto *
attr = Redecl->getAttr<
T>())
3873 TDNDecl = TD->getDecl();
3874 if (ObjCBridgeRelatedAttr *ObjCBAttr =
3875 getObjCBridgeAttr<ObjCBridgeRelatedAttr>(TD))
3892 UnavailableAttr::IR_ARCForbiddenConversion))
3905 unsigned srcKind = 0;
3929 S.
Diag(loc, diag::err_arc_cast_requires_bridge)
3938 ACCResult CreateRule =
3940 assert(CreateRule != ACC_bottom &&
"This cast should already be accepted.");
3941 if (CreateRule != ACC_plusOne)
3944 ? S.
Diag(noteLoc, diag::note_arc_bridge)
3945 : S.
Diag(noteLoc, diag::note_arc_cstyle_bridge);
3948 castType,
castExpr, realCast,
"__bridge ",
3951 if (CreateRule != ACC_plusZero)
3954 ? S.
Diag(noteLoc, diag::note_arc_cstyle_bridge_transfer)
3957 diag::note_arc_bridge_transfer)
3958 << castExprType << br;
3961 castType,
castExpr, realCast,
"__bridge_transfer ",
3962 br ?
"CFBridgingRelease" :
nullptr);
3971 S.
Diag(loc, diag::err_arc_cast_requires_bridge)
3979 ACCResult CreateRule =
3981 assert(CreateRule != ACC_bottom &&
"This cast should already be accepted.");
3982 if (CreateRule != ACC_plusOne)
3985 ? S.
Diag(noteLoc, diag::note_arc_bridge)
3986 : S.
Diag(noteLoc, diag::note_arc_cstyle_bridge);
3988 castType,
castExpr, realCast,
"__bridge ",
3991 if (CreateRule != ACC_plusZero)
3994 ? S.
Diag(noteLoc, diag::note_arc_cstyle_bridge_retained)
3997 diag::note_arc_bridge_retained)
4001 castType,
castExpr, realCast,
"__bridge_retained ",
4002 br ?
"CFBridgingRetain" :
nullptr);
4008 S.
Diag(loc, diag::err_arc_mismatched_cast)
4010 << srcKind << castExprType << castType
4011 << castRange <<
castExpr->getSourceRange();
4014template <
typename TB>
4016 bool &HadTheAttribute,
bool warn) {
4018 HadTheAttribute =
false;
4021 if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
4023 HadTheAttribute =
true;
4024 if (Parm->isStr(
"id"))
4037 = InterfacePointerType->getObjectType()->getInterface();
4038 if ((CastClass == ExprClass) ||
4042 S.
Diag(
castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
4047 castType, ExprClass)))
4054 S.
Diag(
castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
4055 <<
T <<
Target->getName() << castType;
4057 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4064 diag::err_objc_cf_bridged_not_interface)
4077template <
typename TB>
4079 bool &HadTheAttribute,
bool warn) {
4081 HadTheAttribute =
false;
4084 if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
4086 HadTheAttribute =
true;
4087 if (Parm->isStr(
"id"))
4099 castExpr->getType()->getAsObjCInterfacePointerType()) {
4101 = InterfacePointerType->getObjectType()->getInterface();
4102 if ((CastClass == ExprClass) ||
4107 diag::warn_objc_invalid_bridge_to_cf)
4108 <<
castExpr->getType()->getPointeeType() <<
T;
4112 }
else if (
castExpr->getType()->isObjCIdType() ||
4122 diag::warn_objc_invalid_bridge_to_cf)
4123 <<
castExpr->getType() << castType;
4125 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4132 diag::err_objc_ns_bridged_invalid_cfobject)
4133 <<
castExpr->getType() << castType;
4136 S.
Diag(
Target->getBeginLoc(), diag::note_declared_at);
4153 bool HasObjCBridgeAttr;
4154 bool ObjCBridgeAttrWillNotWarn = CheckObjCBridgeNSCast<ObjCBridgeAttr>(
4156 if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
4158 bool HasObjCBridgeMutableAttr;
4159 bool ObjCBridgeMutableAttrWillNotWarn =
4160 CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(
4162 if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
4165 if (HasObjCBridgeAttr)
4167 HasObjCBridgeAttr,
true);
4168 else if (HasObjCBridgeMutableAttr)
4169 CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(
4173 bool HasObjCBridgeAttr;
4174 bool ObjCBridgeAttrWillNotWarn = CheckObjCBridgeCFCast<ObjCBridgeAttr>(
4176 if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
4178 bool HasObjCBridgeMutableAttr;
4179 bool ObjCBridgeMutableAttrWillNotWarn =
4180 CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(
4182 if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
4185 if (HasObjCBridgeAttr)
4187 HasObjCBridgeAttr,
true);
4188 else if (HasObjCBridgeMutableAttr)
4189 CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(
4197 if (PRE->isExplicitProperty()) {
4199 SrcType = PDecl->getType();
4201 else if (PRE->isImplicitProperty()) {
4203 SrcType = Getter->getReturnType();
4226 : CK_CPointerToObjCPointerCast;
4238 QualType T = CfToNs ? SrcType : DestType;
4254 Diag(
Loc, diag::err_objc_bridged_related_invalid_class) << RCId
4255 << SrcType << DestType;
4262 RelatedClass = cast<ObjCInterfaceDecl>(
Target);
4265 Diag(
Loc, diag::err_objc_bridged_related_invalid_class_name) << RCId
4266 << SrcType << DestType;
4269 Diag(
Target->getBeginLoc(), diag::note_declared_at);
4275 if (CfToNs && CMId) {
4280 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4281 << SrcType << DestType << Sel <<
false;
4289 if (!CfToNs && IMId) {
4291 InstanceMethod = RelatedClass->
lookupMethod(Sel,
true);
4292 if (!InstanceMethod) {
4294 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4295 << SrcType << DestType << Sel <<
true;
4314 if (!CfToNs && !NsToCf)
4322 ClassMethod, InstanceMethod, TDNDecl,
4330 std::string ExpressionString =
"[";
4332 ExpressionString +=
" ";
4333 ExpressionString += ClassMethod->getSelector().getAsString();
4337 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4338 << SrcType << DestType << ClassMethod->getSelector() <<
false
4347 Expr *args[] = { SrcExpr };
4349 ClassMethod->getLocation(),
4350 ClassMethod->getSelector(), ClassMethod,
4352 SrcExpr = msg.
get();
4359 if (InstanceMethod) {
4361 std::string ExpressionString;
4364 if (InstanceMethod->isPropertyAccessor())
4366 InstanceMethod->findPropertyDecl()) {
4368 ExpressionString =
".";
4369 ExpressionString += PDecl->getNameAsString();
4370 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4371 << SrcType << DestType << InstanceMethod->getSelector() <<
true
4374 if (ExpressionString.empty()) {
4376 ExpressionString =
" ";
4377 ExpressionString += InstanceMethod->getSelector().getAsString();
4378 ExpressionString +=
"]";
4380 Diag(
Loc, diag::err_objc_bridged_related_known_method)
4381 << SrcType << DestType << InstanceMethod->getSelector() <<
true
4389 SrcExpr, SrcType, InstanceMethod->getLocation(),
4390 InstanceMethod->getSelector(), InstanceMethod, std::nullopt);
4391 SrcExpr = msg.
get();
4402 bool Diagnose,
bool DiagnoseCFAudited,
4415 if (exprACTC == castACTC) {
4421 castType != castExprType) {
4427 if (
const ParenType *PT = dyn_cast<ParenType>(DT))
4428 QDT = PT->desugar();
4429 else if (
const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
4430 QDT = TP->desugar();
4431 else if (
const AttributedType *AT = dyn_cast<AttributedType>(DT))
4432 QDT = AT->desugar();
4433 if (QDT != castType &&
4438 Diag(loc, diag::err_arc_nolifetime_behavior);
4472 switch (ARCCastChecker(Context, exprACTC, castACTC,
false).Visit(
castExpr)) {
4485 CK_ARCConsumeObject,
castExpr,
nullptr,
4512 (Opc == BO_NE || Opc == BO_EQ))) {
4534 castType =
cast->getTypeAsWritten();
4537 castRange =
cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange();
4538 castType =
cast->getTypeAsWritten();
4541 llvm_unreachable(
"Unexpected ImplicitCastExpr");
4560 if (
ParenExpr *pe = dyn_cast<ParenExpr>(e)) {
4562 return new (Context)
ParenExpr(pe->getLParen(), pe->getRParen(), sub);
4563 }
else if (
UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
4564 assert(uo->getOpcode() == UO_Extension);
4568 uo->getOperatorLoc(),
false,
4571 assert(!gse->isResultDependent());
4572 assert(!gse->isTypePredicate());
4574 unsigned n = gse->getNumAssocs();
4577 subExprs.reserve(n);
4578 subTypes.reserve(n);
4580 subTypes.push_back(assoc.getTypeSourceInfo());
4581 Expr *sub = assoc.getAssociationExpr();
4582 if (assoc.isSelected())
4584 subExprs.push_back(sub);
4588 Context, gse->getGenericLoc(), gse->getControllingExpr(), subTypes,
4589 subExprs, gse->getDefaultLoc(), gse->getRParenLoc(),
4590 gse->containsUnexpandedParameterPack(), gse->getResultIndex());
4592 assert(isa<ImplicitCastExpr>(e) &&
"bad form of unbridged cast!");
4593 return cast<ImplicitCastExpr>(e)->getSubExpr();
4604 if (isa<ObjCObjectPointerType>(canCastType) &&
4610 return !ObjI->isArcWeakrefUnavailable();
4617 Expr *curExpr = e, *prevExpr =
nullptr;
4622 if (
auto *pe = dyn_cast<ParenExpr>(curExpr)) {
4624 curExpr = pe->getSubExpr();
4628 if (
auto *ce = dyn_cast<CastExpr>(curExpr)) {
4629 if (
auto *ice = dyn_cast<ImplicitCastExpr>(ce))
4630 if (ice->getCastKind() == CK_ARCReclaimReturnedObject) {
4632 return ice->getSubExpr();
4633 if (
auto *pe = dyn_cast<ParenExpr>(prevExpr))
4634 pe->setSubExpr(ice->getSubExpr());
4636 cast<CastExpr>(prevExpr)->setSubExpr(ice->getSubExpr());
4641 curExpr = ce->getSubExpr();
4660 SubExpr = SubResult.
get();
4667 bool MustConsume =
false;
4674 : CK_CPointerToObjCPointerCast);
4681 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4688 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4690 Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
4693 br ?
"CFBridgingRelease "
4694 :
"__bridge_transfer ");
4724 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
4732 Diag(BridgeKeywordLoc, diag::note_arc_bridge)
4734 Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
4737 br ?
"CFBridgingRetain " :
"__bridge_retained");
4744 Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
4745 << FromType <<
T << Kind