27 #include "llvm/ADT/SmallVector.h"
29 using namespace clang;
57 struct CastOperation {
59 : Self(S), SrcExpr(src), DestType(destType),
62 Kind(CK_Dependent), IsARCUnbridgedCast(
false) {
69 !DestType->isArrayType()) {
70 DestType = DestType.getUnqualifiedType();
75 PlaceholderKind = placeholder->getKind();
89 bool IsARCUnbridgedCast;
95 void CheckConstCast();
96 void CheckReinterpretCast();
97 void CheckStaticCast();
98 void CheckDynamicCast();
99 void CheckCXXCStyleCast(
bool FunctionalCast,
bool ListInitialization);
100 void CheckCStyleCast();
101 void CheckBuiltinBitCast();
102 void CheckAddrspaceCast();
104 void updatePartOfExplicitCastFlags(
CastExpr *CE) {
108 for (;
auto *ICE = dyn_cast<ImplicitCastExpr>(CE->
getSubExpr()); CE = ICE)
109 ICE->setIsPartOfExplicitCast(
true);
117 if (IsARCUnbridgedCast) {
123 updatePartOfExplicitCastFlags(
castExpr);
133 if (PlaceholderKind != K)
return false;
139 bool isPlaceholder()
const {
140 return PlaceholderKind != 0;
143 return PlaceholderKind == K;
149 void checkCastAlign() {
159 IsARCUnbridgedCast =
true;
164 void checkNonOverloadPlaceholders() {
165 if (!isPlaceholder() || isPlaceholder(BuiltinType::Overload))
177 if (
const auto *PtrType = dyn_cast<PointerType>(FromType)) {
178 if (PtrType->getPointeeType()->hasAttr(attr::NoDeref)) {
179 if (
const auto *DestType = dyn_cast<PointerType>(ToType)) {
180 if (!DestType->getPointeeType()->hasAttr(attr::NoDeref)) {
181 S.
Diag(OpLoc, diag::warn_noderef_to_dereferenceable_pointer);
188 struct CheckNoDerefRAII {
189 CheckNoDerefRAII(CastOperation &Op) : Op(Op) {}
190 ~CheckNoDerefRAII() {
191 if (!Op.SrcExpr.isInvalid())
192 CheckNoDeref(Op.Self, Op.SrcExpr.get()->getType(), Op.ResultType,
193 Op.OpRange.getBegin());
235 QualType OrigDestType,
unsigned &msg,
251 bool ListInitialization);
258 bool ListInitialization);
306 CastOperation Op(*
this, DestType, E);
308 Op.DestRange = AngleBrackets;
311 default: llvm_unreachable(
"Unknown C++ cast!");
313 case tok::kw_addrspace_cast:
314 if (!TypeDependent) {
315 Op.CheckAddrspaceCast();
316 if (Op.SrcExpr.isInvalid())
320 Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
321 DestTInfo, OpLoc, Parens.getEnd(), AngleBrackets));
323 case tok::kw_const_cast:
324 if (!TypeDependent) {
326 if (Op.SrcExpr.isInvalid())
331 Op.ValueKind, Op.SrcExpr.get(), DestTInfo,
332 OpLoc, Parens.getEnd(),
335 case tok::kw_dynamic_cast: {
338 return ExprError(
Diag(OpLoc, diag::err_openclcxx_not_supported)
342 if (!TypeDependent) {
343 Op.CheckDynamicCast();
344 if (Op.SrcExpr.isInvalid())
348 Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
349 &Op.BasePath, DestTInfo,
350 OpLoc, Parens.getEnd(),
353 case tok::kw_reinterpret_cast: {
354 if (!TypeDependent) {
355 Op.CheckReinterpretCast();
356 if (Op.SrcExpr.isInvalid())
361 Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
362 nullptr, DestTInfo, OpLoc,
366 case tok::kw_static_cast: {
367 if (!TypeDependent) {
368 Op.CheckStaticCast();
369 if (Op.SrcExpr.isInvalid())
375 Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
377 Parens.getEnd(), AngleBrackets));
397 CastOperation Op(*
this, TSI->
getType(), Operand);
403 Op.CheckBuiltinBitCast();
404 if (Op.SrcExpr.isInvalid())
410 Op.SrcExpr.get(), TSI, KWLoc, RParenLoc);
411 return Op.complete(BCE);
419 bool listInitialization) {
442 range, listInitialization)
448 assert(sequence.
Failed() &&
"initialization succeeded on second try?");
450 default:
return false;
464 case OR_Success: llvm_unreachable(
"successful failed overload");
466 if (candidates.
empty())
467 msg = diag::err_ovl_no_conversion_in_cast;
469 msg = diag::err_ovl_no_viable_conversion_in_cast;
474 msg = diag::err_ovl_ambiguous_conversion_in_cast;
479 msg = diag::err_ovl_deleted_conversion_in_cast;
486 S.
PDiag(msg) << CT << srcType << destType <<
range
488 S, howManyCandidates, src);
496 bool listInitialization) {
497 if (msg == diag::err_bad_cxx_cast_generic &&
506 int DifferentPtrness = 0;
517 if (!DifferentPtrness) {
520 if (RecFrom && RecTo) {
522 if (!DeclFrom->isCompleteDefinition())
523 S.
Diag(DeclFrom->getLocation(), diag::note_type_incomplete) << DeclFrom;
525 if (!DeclTo->isCompleteDefinition())
526 S.
Diag(DeclTo->getLocation(), diag::note_type_incomplete) << DeclTo;
534 enum CastAwayConstnessKind {
541 CACK_SimilarKind = 2,
560 static CastAwayConstnessKind
562 enum { None, Ptr, MemPtr, BlockPtr, Array };
564 if (T->isAnyPointerType())
return Ptr;
565 if (T->isMemberPointerType())
return MemPtr;
566 if (T->isBlockPointerType())
return BlockPtr;
569 if (T->isConstantArrayType() || T->isIncompleteArrayType())
return Array;
575 return AT->getElementType();
576 return T->getPointeeType();
579 CastAwayConstnessKind
Kind;
587 Kind = CastAwayConstnessKind::CACK_Similar;
589 Kind = CastAwayConstnessKind::CACK_Similar;
592 int T1Class = Classify(T1);
594 return CastAwayConstnessKind::CACK_None;
596 int T2Class = Classify(T2);
598 return CastAwayConstnessKind::CACK_None;
602 Kind = T1Class == T2Class ? CastAwayConstnessKind::CACK_SimilarKind
603 : CastAwayConstnessKind::CACK_Incoherent;
613 if (Classify(T1) != Array)
616 auto T2Class = Classify(T2);
620 if (T2Class != Array)
621 Kind = CastAwayConstnessKind::CACK_Incoherent;
622 else if (
Kind != CastAwayConstnessKind::CACK_Incoherent)
623 Kind = CastAwayConstnessKind::CACK_SimilarKind;
638 static CastAwayConstnessKind
640 bool CheckCVR,
bool CheckObjCLifetime,
641 QualType *TheOffendingSrcType =
nullptr,
642 QualType *TheOffendingDestType =
nullptr,
647 return CastAwayConstnessKind::CACK_None;
652 "Source type is not pointer or pointer to member.");
655 "Destination type is not pointer or pointer to member.");
664 QualType PrevUnwrappedSrcType = UnwrappedSrcType;
665 QualType PrevUnwrappedDestType = UnwrappedDestType;
666 auto WorstKind = CastAwayConstnessKind::CACK_Similar;
667 bool AllConstSoFar =
true;
669 Self.
Context, UnwrappedSrcType, UnwrappedDestType)) {
672 if (
Kind > WorstKind)
684 UnwrappedDestType->isObjCObjectType())
693 if (SrcCvrQuals != DestCvrQuals) {
694 if (CastAwayQualifiers)
695 *CastAwayQualifiers = SrcCvrQuals - DestCvrQuals;
698 if (!DestCvrQuals.compatiblyIncludes(SrcCvrQuals)) {
699 if (TheOffendingSrcType)
700 *TheOffendingSrcType = PrevUnwrappedSrcType;
701 if (TheOffendingDestType)
702 *TheOffendingDestType = PrevUnwrappedDestType;
713 if (CheckObjCLifetime &&
719 if (AllConstSoFar && !DestQuals.
hasConst()) {
720 AllConstSoFar =
false;
721 if (TheOffendingSrcType)
722 *TheOffendingSrcType = PrevUnwrappedSrcType;
723 if (TheOffendingDestType)
724 *TheOffendingDestType = PrevUnwrappedDestType;
727 PrevUnwrappedSrcType = UnwrappedSrcType;
728 PrevUnwrappedDestType = UnwrappedDestType;
731 return CastAwayConstnessKind::CACK_None;
737 case CastAwayConstnessKind::CACK_None:
738 llvm_unreachable(
"did not cast away constness");
740 case CastAwayConstnessKind::CACK_Similar:
742 case CastAwayConstnessKind::CACK_SimilarKind:
743 DiagID = diag::err_bad_cxx_cast_qualifiers_away;
746 case CastAwayConstnessKind::CACK_Incoherent:
747 DiagID = diag::ext_bad_cxx_cast_qualifiers_away_incoherent;
751 llvm_unreachable(
"unexpected cast away constness kind");
757 void CastOperation::CheckDynamicCast() {
758 CheckNoDerefRAII NoderefCheck(*
this);
761 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
762 else if (isPlaceholder())
763 SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
764 if (SrcExpr.isInvalid())
767 QualType OrigSrcType = SrcExpr.get()->getType();
781 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
782 << this->DestType << DestRange;
789 assert(DestPointer &&
"Reference to void is not possible");
790 }
else if (DestRecord) {
791 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
792 diag::err_bad_cast_incomplete,
798 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
814 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
815 << OrigSrcType << this->DestType << SrcExpr.get()->getSourceRange();
819 }
else if (DestReference->isLValueReferenceType()) {
820 if (!SrcExpr.get()->isLValue()) {
821 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
822 <<
CT_Dynamic << OrigSrcType << this->DestType << OpRange;
824 SrcPointee = SrcType;
828 if (SrcExpr.get()->isPRValue())
829 SrcExpr = Self.CreateMaterializeTemporaryExpr(
830 SrcType, SrcExpr.get(),
false);
831 SrcPointee = SrcType;
836 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
837 diag::err_bad_cast_incomplete,
843 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
849 assert((DestPointer || DestReference) &&
850 "Bad destination non-ptr/ref slipped through.");
851 assert((DestRecord || DestPointee->
isVoidType()) &&
852 "Bad destination pointee slipped through.");
853 assert(SrcRecord &&
"Bad source pointee slipped through.");
857 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away)
858 <<
CT_Dynamic << OrigSrcType << this->DestType << OpRange;
865 if (DestRecord == SrcRecord) {
873 Self.IsDerivedFrom(OpRange.getBegin(), SrcPointee, DestPointee)) {
874 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
875 OpRange.getBegin(), OpRange,
881 Kind = CK_DerivedToBase;
887 assert(SrcDecl &&
"Definition missing");
888 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
889 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
897 if (!Self.getLangOpts().RTTI && !DestPointee->
isVoidType()) {
898 Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti);
904 if (!Self.getLangOpts().RTTIData) {
906 Self.getASTContext().getTargetInfo().getCXXABI().isMicrosoft();
907 bool isClangCL = Self.getDiagnostics().getDiagnosticOptions().getFormat() ==
909 if (MicrosoftABI || !DestPointee->
isVoidType())
910 Self.Diag(OpRange.getBegin(),
911 diag::warn_no_dynamic_cast_with_rtti_disabled)
924 void CastOperation::CheckConstCast() {
925 CheckNoDerefRAII NoderefCheck(*
this);
928 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
929 else if (isPlaceholder())
930 SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
931 if (SrcExpr.isInvalid())
934 unsigned msg = diag::err_bad_cxx_cast_generic;
935 auto TCR =
TryConstCast(Self, SrcExpr, DestType,
false, msg);
937 Self.Diag(OpRange.getBegin(), msg) <<
CT_Const
938 << SrcExpr.get()->getType() << DestType << OpRange;
944 void CastOperation::CheckAddrspaceCast() {
945 unsigned msg = diag::err_bad_cxx_cast_generic;
949 Self.Diag(OpRange.getBegin(), msg)
950 <<
CT_Addrspace << SrcExpr.get()->getType() << DestType << OpRange;
987 ReinterpretKind = ReinterpretUpcast;
989 ReinterpretKind = ReinterpretDowncast;
993 bool VirtualBase =
true;
994 bool NonZeroOffset =
false;
1000 bool IsVirtual =
false;
1001 for (CXXBasePath::const_iterator IElem = Path.begin(), EElem = Path.end();
1002 IElem != EElem; ++IElem) {
1003 IsVirtual = IElem->Base->isVirtual();
1006 const CXXRecordDecl *BaseRD = IElem->Base->getType()->getAsCXXRecordDecl();
1007 assert(BaseRD &&
"Base type should be a valid unqualified class type");
1011 *ClassDefinition = Class->getDefinition();
1012 if (Class->isInvalidDecl() || !ClassDefinition ||
1013 !ClassDefinition->isCompleteDefinition())
1026 NonZeroOffset =
true;
1028 VirtualBase = VirtualBase && IsVirtual;
1031 (void) NonZeroOffset;
1032 assert((VirtualBase || NonZeroOffset) &&
1033 "Should have returned if has non-virtual base with zero offset");
1036 ReinterpretKind == ReinterpretUpcast? DestType : SrcType;
1038 ReinterpretKind == ReinterpretUpcast? SrcType : DestType;
1041 Self.
Diag(BeginLoc, diag::warn_reinterpret_different_from_static)
1042 << DerivedType << BaseType << !VirtualBase <<
int(ReinterpretKind)
1044 Self.
Diag(BeginLoc, diag::note_reinterpret_updowncast_use_static)
1045 <<
int(ReinterpretKind)
1065 unsigned int DiagID = 0;
1066 const unsigned int DiagList[] = {diag::warn_cast_function_type_strict,
1067 diag::warn_cast_function_type};
1092 assert(SrcFTy && DstFTy);
1098 if (DiagID == diag::warn_cast_function_type_strict)
1102 if (!T->getReturnType()->isVoidType())
1105 return !PT->isVariadic() && PT->getNumParams() == 0;
1110 if (IsVoidVoid(SrcFTy) || IsVoidVoid(DstFTy))
1124 const auto *SrcFPTy = cast<FunctionProtoType>(SrcFTy);
1125 const auto *DstFPTy = cast<FunctionProtoType>(DstFTy);
1129 unsigned NumParams = SrcFPTy->getNumParams();
1130 unsigned DstNumParams = DstFPTy->getNumParams();
1131 if (NumParams > DstNumParams) {
1132 if (!DstFPTy->isVariadic())
1134 NumParams = DstNumParams;
1135 }
else if (NumParams < DstNumParams) {
1136 if (!SrcFPTy->isVariadic())
1140 for (
unsigned i = 0; i < NumParams; ++i)
1142 DstFPTy->getParamType(i), Self.
Context))
1153 void CastOperation::CheckReinterpretCast() {
1154 if (ValueKind ==
VK_PRValue && !isPlaceholder(BuiltinType::Overload))
1155 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
1157 checkNonOverloadPlaceholders();
1158 if (SrcExpr.isInvalid())
1161 unsigned msg = diag::err_bad_cxx_cast_generic;
1164 false, OpRange, msg,
Kind);
1166 if (SrcExpr.isInvalid())
1168 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
1170 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
1172 << DestType << OpRange;
1173 Self.NoteAllOverloadCandidates(SrcExpr.get());
1182 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
1187 Self.Diag(OpRange.getBegin(), DiagID)
1188 << SrcExpr.get()->getType() << DestType << OpRange;
1198 void CastOperation::CheckStaticCast() {
1199 CheckNoDerefRAII NoderefCheck(*
this);
1201 if (isPlaceholder()) {
1202 checkNonOverloadPlaceholders();
1203 if (SrcExpr.isInvalid())
1213 if (claimPlaceholder(BuiltinType::Overload)) {
1214 Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr,
1217 OpRange, DestType, diag::err_bad_static_cast_overload);
1218 if (SrcExpr.isInvalid())
1222 SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
1227 !isPlaceholder(BuiltinType::Overload)) {
1228 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
1229 if (SrcExpr.isInvalid())
1233 unsigned msg = diag::err_bad_cxx_cast_generic;
1236 Kind, BasePath,
false);
1238 if (SrcExpr.isInvalid())
1240 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
1242 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
1243 << oe->
getName() << DestType << OpRange
1245 Self.NoteAllOverloadCandidates(SrcExpr.get());
1253 if (
Kind == CK_BitCast)
1255 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
1270 DestPtrType->getPointeeType().getAddressSpace();
1281 bool ListInitialization) {
1307 OpRange, msg,
Kind, BasePath);
1322 Kind, ListInitialization);
1344 if (Enum->getDecl()->isScoped()) {
1346 Kind = CK_IntegralToBoolean;
1349 Kind = CK_IntegralCast;
1352 Kind = CK_IntegralToFloating;
1368 diag::err_bad_cast_incomplete)) {
1376 Kind = Enum->getDecl()->isFixed() &&
1377 Enum->getDecl()->getIntegerType()->isBooleanType()
1378 ? CK_IntegralToBoolean
1382 Kind = CK_FloatingToIntegral;
1398 OpRange, msg,
Kind, BasePath);
1421 if (DestPointeeQuals != SrcPointeeQuals &&
1423 msg = diag::err_bad_cxx_cast_qualifiers_away;
1428 ? CK_AddressSpaceConversion
1437 Self.
Diag(OpRange.
getBegin(), diag::ext_ms_cast_fn_obj) << OpRange;
1445 Kind = CK_CPointerToObjCPointerCast;
1450 Kind = CK_AnyPointerToBlockPointerCast;
1471 if (SrcPointer->getPointeeType()->getAs<
RecordType>() &&
1473 msg = diag::err_bad_cxx_cast_unrelated_class;
1514 SrcExpr->
getBeginLoc(), ToType, FromType, &RefConv);
1521 msg = SrcExpr->
isLValue() ? diag::err_bad_lvalue_to_rvalue_cast
1522 : diag::err_bad_rvalue_to_rvalue_cast;
1526 if (RefConv & Sema::ReferenceConversions::DerivedToBase) {
1527 Kind = CK_DerivedToBase;
1557 if (!DestReference) {
1561 if (!RValueRef && !SrcExpr->
isLValue()) {
1563 msg = diag::err_bad_cxx_cast_rvalue;
1600 msg = diag::err_bad_static_cast_pointer_nonpointer;
1607 CStyle, OpRange, SrcType, DestType, msg,
Kind,
1617 QualType OrigDestType,
unsigned &msg,
1655 msg = diag::err_bad_cxx_cast_qualifiers_away;
1664 if (!Paths.isRecordingPaths()) {
1666 Paths.setRecordingPaths(
true);
1670 std::set<unsigned> DisplayedPaths;
1672 if (DisplayedPaths.insert(Path.back().SubobjectNumber).second) {
1675 PathDisplayStr +=
"\n ";
1677 PathDisplayStr += PE.Base->getType().getAsString() +
" -> ";
1682 Self.
Diag(OpRange.
getBegin(), diag::err_ambiguous_base_to_derived_cast)
1685 << PathDisplayStr << OpRange;
1690 if (Paths.getDetectedVirtual() !=
nullptr) {
1691 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
1692 Self.
Diag(OpRange.
getBegin(), diag::err_static_downcast_via_virtual)
1693 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
1702 diag::err_downcast_from_inaccessible_base)) {
1715 Kind = CK_BaseToDerived;
1736 bool WasOverloadedFunction =
false;
1745 WasOverloadedFunction =
true;
1751 msg = diag::err_bad_static_cast_member_pointer_nonmp;
1778 Paths.setRecordingPaths(
true);
1784 Self.
Diag(OpRange.
getBegin(), diag::err_ambiguous_memptr_conv)
1785 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
1790 if (
const RecordType *VBase = Paths.getDetectedVirtual()) {
1791 Self.
Diag(OpRange.
getBegin(), diag::err_memptr_conv_via_virtual)
1792 << SrcClass << DestClass <<
QualType(VBase, 0) << OpRange;
1799 DestClass, SrcClass,
1801 diag::err_upcast_to_inaccessible_base)) {
1815 if (WasOverloadedFunction) {
1835 Kind = CK_DerivedToBaseMemberPointer;
1851 diag::err_bad_cast_incomplete) ||
1853 diag::err_allocation_of_abstract_type)) {
1867 Expr *SrcExprRaw = SrcExpr.
get();
1884 if (Result.isInvalid()) {
1890 Kind = CK_ConstructorConversion;
1905 bool NeedToMaterializeTemporary =
false;
1919 if (isa<LValueReferenceType>(DestTypeTmp) && !SrcExpr.
get()->
isLValue()) {
1923 msg = diag::err_bad_cxx_cast_rvalue;
1927 if (isa<RValueReferenceType>(DestTypeTmp) && SrcExpr.
get()->
isPRValue()) {
1931 msg = diag::err_bad_cxx_cast_rvalue;
1937 NeedToMaterializeTemporary =
true;
1946 msg = diag::err_bad_cxx_cast_bitfield;
1966 msg = diag::err_bad_const_cast_dest;
1975 msg = diag::err_bad_const_cast_dest;
1988 if (NeedToMaterializeTemporary)
2006 unsigned DiagID = IsDereference ?
2007 diag::warn_pointer_indirection_from_incompatible_type :
2008 diag::warn_undefined_reinterpret_cast;
2014 if (IsDereference) {
2050 Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range;
2059 if (SrcPtrTy->isObjCSelType()) {
2061 if (isa<PointerType>(DestType))
2065 diag::warn_cast_pointer_from_sel)
2080 const auto *SrcFTy =
2082 const auto *DstFTy =
2092 if (
auto *UO = dyn_cast<UnaryOperator>(Src))
2093 if (UO->getOpcode() == UO_AddrOf)
2095 auto *DRE = dyn_cast<DeclRefExpr>(Src);
2098 auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2107 FD->isVariadic(), FD->isCXXInstanceMember());
2108 if (DstCC == DefaultCC || SrcCC != DefaultCC)
2114 Self.
Diag(OpRange.
getBegin(), diag::warn_cast_calling_conv)
2115 << SrcCCName << DstCCName << OpRange;
2120 if (Self.
Diags.
isIgnored(diag::warn_cast_calling_conv, OpRange.getBegin()))
2127 SourceLocation NameLoc = FD->getFirstDecl()->getNameInfo().getLoc();
2131 llvm::raw_svector_ostream OS(CCAttrText);
2134 OS <<
"__" << DstCCName;
2141 OS <<
"__attribute__((" << DstCCName <<
"))";
2142 AttrTokens.push_back(tok::kw___attribute);
2143 AttrTokens.push_back(tok::l_paren);
2144 AttrTokens.push_back(tok::l_paren);
2149 AttrTokens.push_back(tok::r_paren);
2150 AttrTokens.push_back(tok::r_paren);
2153 if (!AttrSpelling.empty())
2154 CCAttrText = AttrSpelling;
2156 Self.
Diag(NameLoc, diag::note_change_calling_conv_fixit)
2180 diag::warn_int_to_void_pointer_cast
2181 : diag::warn_int_to_pointer_cast;
2194 Expr *E = Result.get();
2211 return Result.isUsable();
2219 bool IsLValueCast =
false;
2231 assert(FixedExpr.
isUsable() &&
"Invalid result fixing overloaded expr");
2232 SrcExpr = FixedExpr;
2240 msg = diag::err_bad_cxx_cast_rvalue;
2253 const char *inappropriate =
nullptr;
2258 msg = diag::err_bad_cxx_cast_bitfield;
2263 inappropriate =
"matrix element";
2266 case OK_ObjCSubscript: inappropriate =
"container subscripting expression";
2269 if (inappropriate) {
2270 Self.
Diag(OpRange.
getBegin(), diag::err_bad_reinterpret_cast_reference)
2271 << inappropriate << DestType
2281 IsLValueCast =
true;
2289 if (DestMemPtr && SrcMemPtr) {
2295 SrcMemPtr->isMemberFunctionPointer())
2308 msg = diag::err_bad_cxx_cast_member_pointer_size;
2322 assert(!IsLValueCast);
2323 Kind = CK_ReinterpretMemberPointer;
2335 msg = diag::err_bad_reinterpret_cast_small_int;
2338 Kind = CK_PointerToIntegral;
2346 if (srcIsVector || destIsVector) {
2367 if (Self.
LangOpts.OpenCL && !CStyle) {
2379 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
2380 else if (!srcIsVector)
2381 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
2383 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
2388 if (SrcType == DestType) {
2412 if (!destIsPtr && !srcIsPtr) {
2419 assert(srcIsPtr &&
"One type must be a pointer");
2425 bool MicrosoftException =
2427 if (MicrosoftException) {
2429 ? diag::warn_void_pointer_to_int_cast
2430 : diag::warn_pointer_to_int_cast;
2433 msg = diag::err_bad_reinterpret_cast_small_int;
2437 Kind = CK_PointerToIntegral;
2442 assert(destIsPtr &&
"One type must be a pointer");
2448 Kind = CK_IntegralToPointer;
2452 if (!destIsPtr || !srcIsPtr) {
2472 Kind = CK_AddressSpaceConversion;
2479 }
else if (IsLValueCast) {
2480 Kind = CK_LValueBitCast;
2485 Kind = CK_AnyPointerToBlockPointerCast;
2496 return SuccessResult;
2510 return SuccessResult;
2521 diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
2523 return SuccessResult;
2530 diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
2532 return SuccessResult;
2545 diag::warn_bad_cxx_cast_nested_pointer_addr_space)
2558 return SuccessResult;
2581 auto SrcPointeeType = SrcPtrType->getPointeeType();
2582 auto DestPointeeType = DestPtrType->getPointeeType();
2583 if (!DestPointeeType.isAddressSpaceOverlapping(SrcPointeeType)) {
2584 msg = diag::err_bad_cxx_cast_addr_space_mismatch;
2587 auto SrcPointeeTypeWithoutAS =
2589 auto DestPointeeTypeWithoutAS =
2592 DestPointeeTypeWithoutAS)) {
2593 Kind = SrcPointeeType.getAddressSpace() == DestPointeeType.getAddressSpace()
2595 : CK_AddressSpaceConversion;
2602 void CastOperation::checkAddressSpaceCast(
QualType SrcType,
QualType DestType) {
2614 if (Self.getLangOpts().OpenCL) {
2615 const Type *DestPtr, *SrcPtr;
2616 bool Nested =
false;
2617 unsigned DiagID = diag::err_typecheck_incompatible_address_space;
2618 DestPtr = Self.getASTContext().getCanonicalType(DestType.
getTypePtr()),
2619 SrcPtr = Self.getASTContext().getCanonicalType(SrcType.
getTypePtr());
2621 while (isa<PointerType>(DestPtr) && isa<PointerType>(SrcPtr)) {
2622 const PointerType *DestPPtr = cast<PointerType>(DestPtr);
2623 const PointerType *SrcPPtr = cast<PointerType>(SrcPtr);
2629 Self.Diag(OpRange.getBegin(), DiagID)
2631 << SrcExpr.get()->getSourceRange();
2640 DiagID = diag::ext_nested_pointer_qualifier_mismatch;
2646 bool SrcCompatXL = this->
getLangOpts().getAltivecSrcCompat() ==
2660 bool SrcCompatGCC = this->
getLangOpts().getAltivecSrcCompat() ==
2662 if (this->
getLangOpts().AltiVec && SrcCompatGCC) {
2664 diag::err_invalid_conversion_between_vector_and_integer)
2665 << VecTy << SrcTy << R;
2671 void CastOperation::CheckCXXCStyleCast(
bool FunctionalStyle,
2672 bool ListInitialization) {
2673 assert(Self.getLangOpts().CPlusPlus);
2676 if (isPlaceholder()) {
2678 if (claimPlaceholder(BuiltinType::UnknownAny)) {
2679 SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
2680 SrcExpr.get(),
Kind,
2681 ValueKind, BasePath);
2685 checkNonOverloadPlaceholders();
2686 if (SrcExpr.isInvalid())
2696 if (claimPlaceholder(BuiltinType::Overload)) {
2697 Self.ResolveAndFixSingleFunctionTemplateSpecialization(
2699 true, DestRange, DestType,
2700 diag::err_bad_cstyle_cast_overload);
2701 if (SrcExpr.isInvalid())
2705 SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
2710 if (DestType->
isDependentType() || SrcExpr.get()->isTypeDependent() ||
2711 SrcExpr.get()->isValueDependent()) {
2712 assert(
Kind == CK_Dependent);
2717 !isPlaceholder(BuiltinType::Overload)) {
2718 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
2719 if (SrcExpr.isInvalid())
2725 if (Self.CheckAltivecInitFromScalar(OpRange, DestType,
2726 SrcExpr.get()->getType())) {
2730 if (Self.ShouldSplatAltivecScalarInCast(vecTy) &&
2731 (SrcExpr.get()->getType()->isIntegerType() ||
2732 SrcExpr.get()->getType()->isFloatingType())) {
2733 Kind = CK_VectorSplat;
2734 SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
2751 unsigned msg = diag::err_bad_cxx_cast_generic;
2754 if (SrcExpr.isInvalid())
2764 if (SrcExpr.isInvalid())
2771 BasePath, ListInitialization);
2772 if (SrcExpr.isInvalid())
2778 OpRange, msg,
Kind);
2779 if (SrcExpr.isInvalid())
2785 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() &&
2787 checkObjCConversion(CCK);
2790 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
2792 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(),
2801 Self.Diag(OpRange.getBegin(), diag::err_bad_cstyle_cast_overload)
2802 << OE->
getName() << DestType << OpRange
2804 Self.NoteAllOverloadCandidates(SrcExpr.get());
2808 OpRange, SrcExpr.get(), DestType, ListInitialization);
2813 if (
Kind == CK_BitCast)
2817 Self.Diag(OpRange.getBegin(), DiagID)
2818 << SrcExpr.get()->getType() << DestType << OpRange;
2834 if (!isa<CallExpr>(SrcExpr.
get()))
2859 diag::warn_bad_function_cast)
2864 void CastOperation::CheckCStyleCast() {
2865 assert(!Self.getLangOpts().CPlusPlus);
2868 if (claimPlaceholder(BuiltinType::UnknownAny)) {
2869 SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
2870 SrcExpr.get(),
Kind,
2871 ValueKind, BasePath);
2879 SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
2880 if (SrcExpr.isInvalid())
2889 if (Self.getASTContext().isDependenceAllowed() &&
2891 SrcExpr.get()->isValueDependent())) {
2892 assert((DestType->
containsErrors() || SrcExpr.get()->containsErrors() ||
2893 SrcExpr.get()->containsErrors()) &&
2894 "should only occur in error-recovery path.");
2895 assert(
Kind == CK_Dependent);
2900 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
2902 if (
FunctionDecl *FD = Self.ResolveAddressOfOverloadedFunction(
2903 SrcExpr.get(), DestType,
true, DAP))
2904 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr.get(), DAP, FD);
2907 assert(SrcExpr.isUsable());
2909 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
2910 if (SrcExpr.isInvalid())
2912 QualType SrcType = SrcExpr.get()->getType();
2916 checkAddressSpaceCast(SrcType, DestType);
2917 if (SrcExpr.isInvalid())
2920 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
2921 diag::err_typecheck_cast_to_incomplete)) {
2928 Self.Context.hasSameUnqualifiedType(DestType, SrcType)) {
2935 Self.isValidSveBitcast(SrcType, DestType)) {
2944 if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){
2946 Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar)
2947 << DestType << SrcExpr.get()->getSourceRange();
2956 Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union)
2961 Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type)
2962 << SrcType << SrcExpr.get()->getSourceRange();
2969 if (Self.getLangOpts().OpenCL && DestType->
isEventT()) {
2971 if (SrcExpr.get()->EvaluateAsInt(Result, Self.Context)) {
2974 Kind = CK_ZeroToOCLOpaqueType;
2977 Self.Diag(OpRange.getBegin(),
2978 diag::err_opencl_cast_non_zero_to_event_t)
2979 <<
toString(CastInt, 10) << SrcExpr.get()->getSourceRange();
2986 Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar)
2987 << DestType << SrcExpr.get()->getSourceRange();
2997 Self.Diag(SrcExpr.get()->getExprLoc(),
2998 diag::err_typecheck_expect_scalar_operand)
2999 << SrcType << SrcExpr.get()->getSourceRange();
3014 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_nullptr_cast)
3025 Self.CurFPFeatureOverrides());
3029 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_nullptr_cast)
3036 SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.get(),
Kind);
3041 if (Self.CheckMatrixCast(OpRange, DestType, SrcType,
Kind))
3047 if (Self.CheckAltivecInitFromScalar(OpRange, DestType, SrcType)) {
3051 if (Self.ShouldSplatAltivecScalarInCast(DestVecTy) &&
3053 Kind = CK_VectorSplat;
3054 SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
3055 }
else if (Self.CheckVectorCast(OpRange, DestType, SrcType,
Kind)) {
3062 if (Self.CheckVectorCast(OpRange, SrcType, DestType,
Kind))
3072 if (isa<ObjCSelectorExpr>(SrcExpr.get())) {
3073 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr);
3080 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_to_bfloat16)
3081 << SrcExpr.get()->getSourceRange();
3086 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_from_bfloat16)
3087 << SrcExpr.get()->getSourceRange();
3096 Self.Diag(SrcExpr.get()->getExprLoc(),
3097 diag::err_cast_pointer_from_non_pointer_int)
3098 << SrcType << SrcExpr.get()->getSourceRange();
3107 Self.Diag(SrcExpr.get()->getBeginLoc(),
3108 diag::err_cast_pointer_to_non_pointer_int)
3109 << DestType << SrcExpr.get()->getSourceRange();
3114 if ((Self.Context.getTypeSize(SrcType) >
3115 Self.Context.getTypeSize(DestType)) &&
3125 : diag::warn_void_pointer_to_int_cast;
3127 Diag = diag::warn_pointer_to_enum_cast;
3129 Diag = diag::warn_pointer_to_int_cast;
3130 Self.Diag(OpRange.getBegin(),
Diag) << SrcType << DestType << OpRange;
3134 if (Self.getLangOpts().OpenCL && !Self.getOpenCLOptions().isAvailableOption(
3135 "cl_khr_fp16", Self.getLangOpts())) {
3137 Self.Diag(SrcExpr.get()->getBeginLoc(), diag::err_opencl_cast_to_half)
3138 << DestType << SrcExpr.get()->getSourceRange();
3145 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) {
3147 if (SrcExpr.isInvalid())
3151 if (Self.getLangOpts().ObjCAutoRefCount && CastPtr) {
3154 Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers();
3156 ExprPtr->getPointeeType()->isObjCLifetimeType() &&
3158 Self.Diag(SrcExpr.get()->getBeginLoc(),
3159 diag::err_typecheck_incompatible_ownership)
3161 << SrcExpr.get()->getSourceRange();
3166 else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) {
3167 Self.Diag(SrcExpr.get()->getBeginLoc(),
3168 diag::err_arc_convesion_of_weak_unavailable)
3169 << 1 << SrcType << DestType << SrcExpr.get()->getSourceRange();
3176 Self.Diag(OpRange.getBegin(), DiagID) << SrcType << DestType << OpRange;
3178 if (isa<PointerType>(SrcType) && isa<PointerType>(DestType)) {
3185 if (SrcRD && DestRD && SrcRD->
hasAttr<RandomizeLayoutAttr>() &&
3188 Self.Diag(OpRange.getBegin(), diag::err_cast_from_randomized_struct)
3189 << SrcType << DestType;
3198 Kind = Self.PrepareScalarCast(SrcExpr, DestType);
3199 if (SrcExpr.isInvalid())
3202 if (
Kind == CK_BitCast)
3206 void CastOperation::CheckBuiltinBitCast() {
3207 QualType SrcType = SrcExpr.get()->getType();
3209 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
3210 diag::err_typecheck_cast_to_incomplete) ||
3211 Self.RequireCompleteType(OpRange.getBegin(), SrcType,
3212 diag::err_incomplete_type)) {
3217 if (SrcExpr.get()->isPRValue())
3218 SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcType, SrcExpr.get(),
3221 CharUnits DestSize = Self.Context.getTypeSizeInChars(DestType);
3222 CharUnits SourceSize = Self.Context.getTypeSizeInChars(SrcType);
3223 if (DestSize != SourceSize) {
3224 Self.Diag(OpRange.getBegin(), diag::err_bit_cast_type_size_mismatch)
3231 Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable)
3238 Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable)
3244 Kind = CK_LValueToRValueBitCast;
3259 QualType TheOffendingSrcType, TheOffendingDestType;
3262 &TheOffendingSrcType, &TheOffendingDestType,
3263 &CastAwayQualifiers) !=
3264 CastAwayConstnessKind::CACK_Similar)
3268 int qualifiers = -1;
3271 }
else if (CastAwayQualifiers.
hasConst()) {
3277 if (qualifiers == -1)
3279 << SrcType << DestType;
3282 << TheOffendingSrcType << TheOffendingDestType << qualifiers;
3294 Op.CheckCXXCStyleCast(
false,
3297 Op.CheckCStyleCast();
3300 if (Op.SrcExpr.isInvalid())
3307 Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
3316 assert(LPLoc.
isValid() &&
"List-initialization shouldn't get here.");
3321 Op.CheckCXXCStyleCast(
true,
false);
3322 if (Op.SrcExpr.isInvalid())
3325 auto *SubExpr = Op.SrcExpr.get();
3326 if (
auto *BindExpr = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
3327 SubExpr = BindExpr->getSubExpr();
3328 if (
auto *ConstructExpr = dyn_cast<CXXConstructExpr>(SubExpr))
3329 ConstructExpr->setParenOrBraceRange(
SourceRange(LPLoc, RPLoc));
3332 Context, Op.ResultType, Op.ValueKind, CastTypeInfo, Op.Kind,