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;
463 case OR_Success: llvm_unreachable(
"successful failed overload");
465 if (candidates.
empty())
466 msg = diag::err_ovl_no_conversion_in_cast;
468 msg = diag::err_ovl_no_viable_conversion_in_cast;
473 msg = diag::err_ovl_ambiguous_conversion_in_cast;
478 msg = diag::err_ovl_deleted_conversion_in_cast;
485 S.
PDiag(msg) << CT << srcType << destType <<
range
487 S, howManyCandidates, src);
495 bool listInitialization) {
496 if (msg == diag::err_bad_cxx_cast_generic &&
505 int DifferentPtrness = 0;
516 if (!DifferentPtrness) {
519 if (RecFrom && RecTo) {
521 if (!DeclFrom->isCompleteDefinition())
522 S.
Diag(DeclFrom->getLocation(), diag::note_type_incomplete) << DeclFrom;
524 if (!DeclTo->isCompleteDefinition())
525 S.
Diag(DeclTo->getLocation(), diag::note_type_incomplete) << DeclTo;
533 enum CastAwayConstnessKind {
540 CACK_SimilarKind = 2,
559 static CastAwayConstnessKind
561 enum { None, Ptr, MemPtr, BlockPtr, Array };
563 if (T->isAnyPointerType())
return Ptr;
564 if (T->isMemberPointerType())
return MemPtr;
565 if (T->isBlockPointerType())
return BlockPtr;
568 if (T->isConstantArrayType() || T->isIncompleteArrayType())
return Array;
574 return AT->getElementType();
575 return T->getPointeeType();
578 CastAwayConstnessKind
Kind;
586 Kind = CastAwayConstnessKind::CACK_Similar;
588 Kind = CastAwayConstnessKind::CACK_Similar;
591 int T1Class = Classify(T1);
593 return CastAwayConstnessKind::CACK_None;
595 int T2Class = Classify(T2);
597 return CastAwayConstnessKind::CACK_None;
601 Kind = T1Class == T2Class ? CastAwayConstnessKind::CACK_SimilarKind
602 : CastAwayConstnessKind::CACK_Incoherent;
612 if (Classify(T1) != Array)
615 auto T2Class = Classify(T2);
619 if (T2Class != Array)
620 Kind = CastAwayConstnessKind::CACK_Incoherent;
621 else if (
Kind != CastAwayConstnessKind::CACK_Incoherent)
622 Kind = CastAwayConstnessKind::CACK_SimilarKind;
637 static CastAwayConstnessKind
639 bool CheckCVR,
bool CheckObjCLifetime,
640 QualType *TheOffendingSrcType =
nullptr,
641 QualType *TheOffendingDestType =
nullptr,
646 return CastAwayConstnessKind::CACK_None;
651 "Source type is not pointer or pointer to member.");
654 "Destination type is not pointer or pointer to member.");
663 QualType PrevUnwrappedSrcType = UnwrappedSrcType;
664 QualType PrevUnwrappedDestType = UnwrappedDestType;
665 auto WorstKind = CastAwayConstnessKind::CACK_Similar;
666 bool AllConstSoFar =
true;
668 Self.
Context, UnwrappedSrcType, UnwrappedDestType)) {
671 if (
Kind > WorstKind)
683 UnwrappedDestType->isObjCObjectType())
692 if (SrcCvrQuals != DestCvrQuals) {
693 if (CastAwayQualifiers)
694 *CastAwayQualifiers = SrcCvrQuals - DestCvrQuals;
697 if (!DestCvrQuals.compatiblyIncludes(SrcCvrQuals)) {
698 if (TheOffendingSrcType)
699 *TheOffendingSrcType = PrevUnwrappedSrcType;
700 if (TheOffendingDestType)
701 *TheOffendingDestType = PrevUnwrappedDestType;
712 if (CheckObjCLifetime &&
718 if (AllConstSoFar && !DestQuals.
hasConst()) {
719 AllConstSoFar =
false;
720 if (TheOffendingSrcType)
721 *TheOffendingSrcType = PrevUnwrappedSrcType;
722 if (TheOffendingDestType)
723 *TheOffendingDestType = PrevUnwrappedDestType;
726 PrevUnwrappedSrcType = UnwrappedSrcType;
727 PrevUnwrappedDestType = UnwrappedDestType;
730 return CastAwayConstnessKind::CACK_None;
736 case CastAwayConstnessKind::CACK_None:
737 llvm_unreachable(
"did not cast away constness");
739 case CastAwayConstnessKind::CACK_Similar:
741 case CastAwayConstnessKind::CACK_SimilarKind:
742 DiagID = diag::err_bad_cxx_cast_qualifiers_away;
745 case CastAwayConstnessKind::CACK_Incoherent:
746 DiagID = diag::ext_bad_cxx_cast_qualifiers_away_incoherent;
750 llvm_unreachable(
"unexpected cast away constness kind");
756 void CastOperation::CheckDynamicCast() {
757 CheckNoDerefRAII NoderefCheck(*
this);
760 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
761 else if (isPlaceholder())
762 SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
763 if (SrcExpr.isInvalid())
766 QualType OrigSrcType = SrcExpr.get()->getType();
780 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
781 << this->DestType << DestRange;
788 assert(DestPointer &&
"Reference to void is not possible");
789 }
else if (DestRecord) {
790 if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
791 diag::err_bad_cast_incomplete,
797 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
813 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
814 << OrigSrcType << this->DestType << SrcExpr.get()->getSourceRange();
818 }
else if (DestReference->isLValueReferenceType()) {
819 if (!SrcExpr.get()->isLValue()) {
820 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
821 <<
CT_Dynamic << OrigSrcType << this->DestType << OpRange;
823 SrcPointee = SrcType;
827 if (SrcExpr.get()->isPRValue())
828 SrcExpr = Self.CreateMaterializeTemporaryExpr(
829 SrcType, SrcExpr.get(),
false);
830 SrcPointee = SrcType;
835 if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
836 diag::err_bad_cast_incomplete,
842 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
848 assert((DestPointer || DestReference) &&
849 "Bad destination non-ptr/ref slipped through.");
850 assert((DestRecord || DestPointee->
isVoidType()) &&
851 "Bad destination pointee slipped through.");
852 assert(SrcRecord &&
"Bad source pointee slipped through.");
856 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away)
857 <<
CT_Dynamic << OrigSrcType << this->DestType << OpRange;
864 if (DestRecord == SrcRecord) {
872 Self.IsDerivedFrom(OpRange.getBegin(), SrcPointee, DestPointee)) {
873 if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
874 OpRange.getBegin(), OpRange,
880 Kind = CK_DerivedToBase;
886 assert(SrcDecl &&
"Definition missing");
887 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
888 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
896 if (!Self.getLangOpts().RTTI && !DestPointee->
isVoidType()) {
897 Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti);
903 if (!Self.getLangOpts().RTTIData) {
905 Self.getASTContext().getTargetInfo().getCXXABI().isMicrosoft();
906 bool isClangCL = Self.getDiagnostics().getDiagnosticOptions().getFormat() ==
908 if (MicrosoftABI || !DestPointee->
isVoidType())
909 Self.Diag(OpRange.getBegin(),
910 diag::warn_no_dynamic_cast_with_rtti_disabled)
923 void CastOperation::CheckConstCast() {
924 CheckNoDerefRAII NoderefCheck(*
this);
927 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
928 else if (isPlaceholder())
929 SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
930 if (SrcExpr.isInvalid())
933 unsigned msg = diag::err_bad_cxx_cast_generic;
934 auto TCR =
TryConstCast(Self, SrcExpr, DestType,
false, msg);
936 Self.Diag(OpRange.getBegin(), msg) <<
CT_Const
937 << SrcExpr.get()->getType() << DestType << OpRange;
943 void CastOperation::CheckAddrspaceCast() {
944 unsigned msg = diag::err_bad_cxx_cast_generic;
948 Self.Diag(OpRange.getBegin(), msg)
949 <<
CT_Addrspace << SrcExpr.get()->getType() << DestType << OpRange;
986 ReinterpretKind = ReinterpretUpcast;
988 ReinterpretKind = ReinterpretDowncast;
992 bool VirtualBase =
true;
993 bool NonZeroOffset =
false;
999 bool IsVirtual =
false;
1000 for (CXXBasePath::const_iterator IElem = Path.begin(), EElem = Path.end();
1001 IElem != EElem; ++IElem) {
1002 IsVirtual = IElem->Base->isVirtual();
1005 const CXXRecordDecl *BaseRD = IElem->Base->getType()->getAsCXXRecordDecl();
1006 assert(BaseRD &&
"Base type should be a valid unqualified class type");
1010 *ClassDefinition = Class->getDefinition();
1011 if (Class->isInvalidDecl() || !ClassDefinition ||
1012 !ClassDefinition->isCompleteDefinition())
1025 NonZeroOffset =
true;
1027 VirtualBase = VirtualBase && IsVirtual;
1030 (void) NonZeroOffset;
1031 assert((VirtualBase || NonZeroOffset) &&
1032 "Should have returned if has non-virtual base with zero offset");
1035 ReinterpretKind == ReinterpretUpcast? DestType : SrcType;
1037 ReinterpretKind == ReinterpretUpcast? SrcType : DestType;
1040 Self.
Diag(BeginLoc, diag::warn_reinterpret_different_from_static)
1041 << DerivedType << BaseType << !VirtualBase <<
int(ReinterpretKind)
1043 Self.
Diag(BeginLoc, diag::note_reinterpret_updowncast_use_static)
1044 <<
int(ReinterpretKind)
1083 assert(SrcFTy && DstFTy);
1086 if (!T->getReturnType()->isVoidType())
1089 return !PT->isVariadic() && PT->getNumParams() == 0;
1094 if (IsVoidVoid(SrcFTy) || IsVoidVoid(DstFTy))
1108 const auto *SrcFPTy = cast<FunctionProtoType>(SrcFTy);
1109 const auto *DstFPTy = cast<FunctionProtoType>(DstFTy);
1113 unsigned NumParams = SrcFPTy->getNumParams();
1114 unsigned DstNumParams = DstFPTy->getNumParams();
1115 if (NumParams > DstNumParams) {
1116 if (!DstFPTy->isVariadic())
1118 NumParams = DstNumParams;
1119 }
else if (NumParams < DstNumParams) {
1120 if (!SrcFPTy->isVariadic())
1124 for (
unsigned i = 0; i < NumParams; ++i)
1126 DstFPTy->getParamType(i), Self.
Context))
1137 void CastOperation::CheckReinterpretCast() {
1138 if (ValueKind ==
VK_PRValue && !isPlaceholder(BuiltinType::Overload))
1139 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
1141 checkNonOverloadPlaceholders();
1142 if (SrcExpr.isInvalid())
1145 unsigned msg = diag::err_bad_cxx_cast_generic;
1148 false, OpRange, msg,
Kind);
1150 if (SrcExpr.isInvalid())
1152 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
1154 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
1156 << DestType << OpRange;
1157 Self.NoteAllOverloadCandidates(SrcExpr.get());
1166 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
1171 Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type)
1172 << SrcExpr.get()->getType() << DestType << OpRange;
1182 void CastOperation::CheckStaticCast() {
1183 CheckNoDerefRAII NoderefCheck(*
this);
1185 if (isPlaceholder()) {
1186 checkNonOverloadPlaceholders();
1187 if (SrcExpr.isInvalid())
1197 if (claimPlaceholder(BuiltinType::Overload)) {
1198 Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr,
1201 OpRange, DestType, diag::err_bad_static_cast_overload);
1202 if (SrcExpr.isInvalid())
1206 SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
1211 !isPlaceholder(BuiltinType::Overload)) {
1212 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
1213 if (SrcExpr.isInvalid())
1217 unsigned msg = diag::err_bad_cxx_cast_generic;
1220 Kind, BasePath,
false);
1222 if (SrcExpr.isInvalid())
1224 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
1226 Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
1227 << oe->
getName() << DestType << OpRange
1229 Self.NoteAllOverloadCandidates(SrcExpr.get());
1237 if (
Kind == CK_BitCast)
1239 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
1254 DestPtrType->getPointeeType().getAddressSpace();
1265 bool ListInitialization) {
1291 OpRange, msg,
Kind, BasePath);
1306 Kind, ListInitialization);
1328 if (Enum->getDecl()->isScoped()) {
1330 Kind = CK_IntegralToBoolean;
1333 Kind = CK_IntegralCast;
1336 Kind = CK_IntegralToFloating;
1352 diag::err_bad_cast_incomplete)) {
1360 Kind = Enum->getDecl()->isFixed() &&
1361 Enum->getDecl()->getIntegerType()->isBooleanType()
1362 ? CK_IntegralToBoolean
1366 Kind = CK_FloatingToIntegral;
1382 OpRange, msg,
Kind, BasePath);
1405 if (DestPointeeQuals != SrcPointeeQuals &&
1407 msg = diag::err_bad_cxx_cast_qualifiers_away;
1412 ? CK_AddressSpaceConversion
1421 Self.
Diag(OpRange.
getBegin(), diag::ext_ms_cast_fn_obj) << OpRange;
1429 Kind = CK_CPointerToObjCPointerCast;
1434 Kind = CK_AnyPointerToBlockPointerCast;
1455 if (SrcPointer->getPointeeType()->getAs<
RecordType>() &&
1457 msg = diag::err_bad_cxx_cast_unrelated_class;
1498 SrcExpr->
getBeginLoc(), ToType, FromType, &RefConv);
1505 msg = SrcExpr->
isLValue() ? diag::err_bad_lvalue_to_rvalue_cast
1506 : diag::err_bad_rvalue_to_rvalue_cast;
1510 if (RefConv & Sema::ReferenceConversions::DerivedToBase) {
1511 Kind = CK_DerivedToBase;
1541 if (!DestReference) {
1545 if (!RValueRef && !SrcExpr->
isLValue()) {
1547 msg = diag::err_bad_cxx_cast_rvalue;
1584 msg = diag::err_bad_static_cast_pointer_nonpointer;
1591 CStyle, OpRange, SrcType, DestType, msg,
Kind,
1601 QualType OrigDestType,
unsigned &msg,
1639 msg = diag::err_bad_cxx_cast_qualifiers_away;
1648 if (!Paths.isRecordingPaths()) {
1650 Paths.setRecordingPaths(
true);
1654 std::set<unsigned> DisplayedPaths;
1656 if (DisplayedPaths.insert(Path.back().SubobjectNumber).second) {
1659 PathDisplayStr +=
"\n ";
1661 PathDisplayStr += PE.Base->getType().getAsString() +
" -> ";
1666 Self.
Diag(OpRange.
getBegin(), diag::err_ambiguous_base_to_derived_cast)
1669 << PathDisplayStr << OpRange;
1674 if (Paths.getDetectedVirtual() !=
nullptr) {
1675 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
1676 Self.
Diag(OpRange.
getBegin(), diag::err_static_downcast_via_virtual)
1677 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
1686 diag::err_downcast_from_inaccessible_base)) {
1699 Kind = CK_BaseToDerived;
1720 bool WasOverloadedFunction =
false;
1729 WasOverloadedFunction =
true;
1735 msg = diag::err_bad_static_cast_member_pointer_nonmp;
1762 Paths.setRecordingPaths(
true);
1768 Self.
Diag(OpRange.
getBegin(), diag::err_ambiguous_memptr_conv)
1769 << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
1774 if (
const RecordType *VBase = Paths.getDetectedVirtual()) {
1775 Self.
Diag(OpRange.
getBegin(), diag::err_memptr_conv_via_virtual)
1776 << SrcClass << DestClass <<
QualType(VBase, 0) << OpRange;
1783 DestClass, SrcClass,
1785 diag::err_upcast_to_inaccessible_base)) {
1799 if (WasOverloadedFunction) {
1819 Kind = CK_DerivedToBaseMemberPointer;
1835 diag::err_bad_cast_incomplete) ||
1837 diag::err_allocation_of_abstract_type)) {
1851 Expr *SrcExprRaw = SrcExpr.
get();
1868 if (Result.isInvalid()) {
1874 Kind = CK_ConstructorConversion;
1889 bool NeedToMaterializeTemporary =
false;
1903 if (isa<LValueReferenceType>(DestTypeTmp) && !SrcExpr.
get()->
isLValue()) {
1907 msg = diag::err_bad_cxx_cast_rvalue;
1911 if (isa<RValueReferenceType>(DestTypeTmp) && SrcExpr.
get()->
isPRValue()) {
1915 msg = diag::err_bad_cxx_cast_rvalue;
1921 NeedToMaterializeTemporary =
true;
1930 msg = diag::err_bad_cxx_cast_bitfield;
1950 msg = diag::err_bad_const_cast_dest;
1959 msg = diag::err_bad_const_cast_dest;
1972 if (NeedToMaterializeTemporary)
1990 unsigned DiagID = IsDereference ?
1991 diag::warn_pointer_indirection_from_incompatible_type :
1992 diag::warn_undefined_reinterpret_cast;
1998 if (IsDereference) {
2034 Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range;
2043 if (SrcPtrTy->isObjCSelType()) {
2045 if (isa<PointerType>(DestType))
2049 diag::warn_cast_pointer_from_sel)
2064 const auto *SrcFTy =
2066 const auto *DstFTy =
2076 if (
auto *UO = dyn_cast<UnaryOperator>(Src))
2077 if (UO->getOpcode() == UO_AddrOf)
2079 auto *DRE = dyn_cast<DeclRefExpr>(Src);
2082 auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl());
2091 FD->isVariadic(), FD->isCXXInstanceMember());
2092 if (DstCC == DefaultCC || SrcCC != DefaultCC)
2098 Self.
Diag(OpRange.
getBegin(), diag::warn_cast_calling_conv)
2099 << SrcCCName << DstCCName << OpRange;
2104 if (Self.
Diags.
isIgnored(diag::warn_cast_calling_conv, OpRange.getBegin()))
2111 SourceLocation NameLoc = FD->getFirstDecl()->getNameInfo().getLoc();
2115 llvm::raw_svector_ostream OS(CCAttrText);
2118 OS <<
"__" << DstCCName;
2125 OS <<
"__attribute__((" << DstCCName <<
"))";
2126 AttrTokens.push_back(tok::kw___attribute);
2127 AttrTokens.push_back(tok::l_paren);
2128 AttrTokens.push_back(tok::l_paren);
2133 AttrTokens.push_back(tok::r_paren);
2134 AttrTokens.push_back(tok::r_paren);
2137 if (!AttrSpelling.empty())
2138 CCAttrText = AttrSpelling;
2140 Self.
Diag(NameLoc, diag::note_change_calling_conv_fixit)
2164 diag::warn_int_to_void_pointer_cast
2165 : diag::warn_int_to_pointer_cast;
2178 Expr *E = Result.get();
2195 return Result.isUsable();
2203 bool IsLValueCast =
false;
2215 assert(FixedExpr.
isUsable() &&
"Invalid result fixing overloaded expr");
2216 SrcExpr = FixedExpr;
2224 msg = diag::err_bad_cxx_cast_rvalue;
2237 const char *inappropriate =
nullptr;
2242 msg = diag::err_bad_cxx_cast_bitfield;
2247 inappropriate =
"matrix element";
2250 case OK_ObjCSubscript: inappropriate =
"container subscripting expression";
2253 if (inappropriate) {
2254 Self.
Diag(OpRange.
getBegin(), diag::err_bad_reinterpret_cast_reference)
2255 << inappropriate << DestType
2265 IsLValueCast =
true;
2273 if (DestMemPtr && SrcMemPtr) {
2279 SrcMemPtr->isMemberFunctionPointer())
2292 msg = diag::err_bad_cxx_cast_member_pointer_size;
2306 assert(!IsLValueCast);
2307 Kind = CK_ReinterpretMemberPointer;
2319 msg = diag::err_bad_reinterpret_cast_small_int;
2322 Kind = CK_PointerToIntegral;
2330 if (srcIsVector || destIsVector) {
2351 if (Self.
LangOpts.OpenCL && !CStyle) {
2363 msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
2364 else if (!srcIsVector)
2365 msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
2367 msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
2372 if (SrcType == DestType) {
2396 if (!destIsPtr && !srcIsPtr) {
2403 assert(srcIsPtr &&
"One type must be a pointer");
2409 bool MicrosoftException =
2411 if (MicrosoftException) {
2413 ? diag::warn_void_pointer_to_int_cast
2414 : diag::warn_pointer_to_int_cast;
2417 msg = diag::err_bad_reinterpret_cast_small_int;
2421 Kind = CK_PointerToIntegral;
2426 assert(destIsPtr &&
"One type must be a pointer");
2432 Kind = CK_IntegralToPointer;
2436 if (!destIsPtr || !srcIsPtr) {
2456 Kind = CK_AddressSpaceConversion;
2463 }
else if (IsLValueCast) {
2464 Kind = CK_LValueBitCast;
2469 Kind = CK_AnyPointerToBlockPointerCast;
2480 return SuccessResult;
2494 return SuccessResult;
2505 diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
2507 return SuccessResult;
2514 diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
2516 return SuccessResult;
2529 diag::warn_bad_cxx_cast_nested_pointer_addr_space)
2542 return SuccessResult;
2565 auto SrcPointeeType = SrcPtrType->getPointeeType();
2566 auto DestPointeeType = DestPtrType->getPointeeType();
2567 if (!DestPointeeType.isAddressSpaceOverlapping(SrcPointeeType)) {
2568 msg = diag::err_bad_cxx_cast_addr_space_mismatch;
2571 auto SrcPointeeTypeWithoutAS =
2573 auto DestPointeeTypeWithoutAS =
2576 DestPointeeTypeWithoutAS)) {
2577 Kind = SrcPointeeType.getAddressSpace() == DestPointeeType.getAddressSpace()
2579 : CK_AddressSpaceConversion;
2586 void CastOperation::checkAddressSpaceCast(
QualType SrcType,
QualType DestType) {
2598 if (Self.getLangOpts().OpenCL) {
2599 const Type *DestPtr, *SrcPtr;
2600 bool Nested =
false;
2601 unsigned DiagID = diag::err_typecheck_incompatible_address_space;
2602 DestPtr = Self.getASTContext().getCanonicalType(DestType.
getTypePtr()),
2603 SrcPtr = Self.getASTContext().getCanonicalType(SrcType.
getTypePtr());
2605 while (isa<PointerType>(DestPtr) && isa<PointerType>(SrcPtr)) {
2606 const PointerType *DestPPtr = cast<PointerType>(DestPtr);
2607 const PointerType *SrcPPtr = cast<PointerType>(SrcPtr);
2613 Self.Diag(OpRange.getBegin(), DiagID)
2615 << SrcExpr.get()->getSourceRange();
2624 DiagID = diag::ext_nested_pointer_qualifier_mismatch;
2630 bool SrcCompatXL = this->
getLangOpts().getAltivecSrcCompat() ==
2644 bool SrcCompatGCC = this->
getLangOpts().getAltivecSrcCompat() ==
2646 if (this->
getLangOpts().AltiVec && SrcCompatGCC) {
2648 diag::err_invalid_conversion_between_vector_and_integer)
2649 << VecTy << SrcTy << R;
2655 void CastOperation::CheckCXXCStyleCast(
bool FunctionalStyle,
2656 bool ListInitialization) {
2657 assert(Self.getLangOpts().CPlusPlus);
2660 if (isPlaceholder()) {
2662 if (claimPlaceholder(BuiltinType::UnknownAny)) {
2663 SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
2664 SrcExpr.get(),
Kind,
2665 ValueKind, BasePath);
2669 checkNonOverloadPlaceholders();
2670 if (SrcExpr.isInvalid())
2680 if (claimPlaceholder(BuiltinType::Overload)) {
2681 Self.ResolveAndFixSingleFunctionTemplateSpecialization(
2683 true, DestRange, DestType,
2684 diag::err_bad_cstyle_cast_overload);
2685 if (SrcExpr.isInvalid())
2689 SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
2694 if (DestType->
isDependentType() || SrcExpr.get()->isTypeDependent() ||
2695 SrcExpr.get()->isValueDependent()) {
2696 assert(
Kind == CK_Dependent);
2701 !isPlaceholder(BuiltinType::Overload)) {
2702 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
2703 if (SrcExpr.isInvalid())
2709 if (Self.CheckAltivecInitFromScalar(OpRange, DestType,
2710 SrcExpr.get()->getType())) {
2714 if (Self.ShouldSplatAltivecScalarInCast(vecTy) &&
2715 (SrcExpr.get()->getType()->isIntegerType() ||
2716 SrcExpr.get()->getType()->isFloatingType())) {
2717 Kind = CK_VectorSplat;
2718 SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
2735 unsigned msg = diag::err_bad_cxx_cast_generic;
2738 if (SrcExpr.isInvalid())
2748 if (SrcExpr.isInvalid())
2755 BasePath, ListInitialization);
2756 if (SrcExpr.isInvalid())
2762 OpRange, msg,
Kind);
2763 if (SrcExpr.isInvalid())
2769 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() &&
2771 checkObjCConversion(CCK);
2774 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
2776 FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(),
2785 Self.Diag(OpRange.getBegin(), diag::err_bad_cstyle_cast_overload)
2786 << OE->
getName() << DestType << OpRange
2788 Self.NoteAllOverloadCandidates(SrcExpr.get());
2792 OpRange, SrcExpr.get(), DestType, ListInitialization);
2797 if (
Kind == CK_BitCast)
2801 Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type)
2802 << SrcExpr.get()->getType() << DestType << OpRange;
2818 if (!isa<CallExpr>(SrcExpr.
get()))
2843 diag::warn_bad_function_cast)
2848 void CastOperation::CheckCStyleCast() {
2849 assert(!Self.getLangOpts().CPlusPlus);
2852 if (claimPlaceholder(BuiltinType::UnknownAny)) {
2853 SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
2854 SrcExpr.get(),
Kind,
2855 ValueKind, BasePath);
2863 SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
2864 if (SrcExpr.isInvalid())
2873 if (Self.getASTContext().isDependenceAllowed() &&
2875 SrcExpr.get()->isValueDependent())) {
2876 assert((DestType->
containsErrors() || SrcExpr.get()->containsErrors() ||
2877 SrcExpr.get()->containsErrors()) &&
2878 "should only occur in error-recovery path.");
2879 assert(
Kind == CK_Dependent);
2884 if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
2886 if (
FunctionDecl *FD = Self.ResolveAddressOfOverloadedFunction(
2887 SrcExpr.get(), DestType,
true, DAP))
2888 SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr.get(), DAP, FD);
2891 assert(SrcExpr.isUsable());
2893 SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
2894 if (SrcExpr.isInvalid())
2896 QualType SrcType = SrcExpr.get()->getType();
2900 checkAddressSpaceCast(SrcType, DestType);
2901 if (SrcExpr.isInvalid())
2904 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
2905 diag::err_typecheck_cast_to_incomplete)) {
2912 Self.Context.hasSameUnqualifiedType(DestType, SrcType)) {
2919 Self.isValidSveBitcast(SrcType, DestType)) {
2928 if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){
2930 Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar)
2931 << DestType << SrcExpr.get()->getSourceRange();
2940 Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union)
2945 Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type)
2946 << SrcType << SrcExpr.get()->getSourceRange();
2953 if (Self.getLangOpts().OpenCL && DestType->
isEventT()) {
2955 if (SrcExpr.get()->EvaluateAsInt(Result, Self.Context)) {
2958 Kind = CK_ZeroToOCLOpaqueType;
2961 Self.Diag(OpRange.getBegin(),
2962 diag::err_opencl_cast_non_zero_to_event_t)
2963 <<
toString(CastInt, 10) << SrcExpr.get()->getSourceRange();
2970 Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar)
2971 << DestType << SrcExpr.get()->getSourceRange();
2981 Self.Diag(SrcExpr.get()->getExprLoc(),
2982 diag::err_typecheck_expect_scalar_operand)
2983 << SrcType << SrcExpr.get()->getSourceRange();
2989 SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.get(),
Kind);
2994 if (Self.CheckMatrixCast(OpRange, DestType, SrcType,
Kind))
3000 if (Self.CheckAltivecInitFromScalar(OpRange, DestType, SrcType)) {
3004 if (Self.ShouldSplatAltivecScalarInCast(DestVecTy) &&
3006 Kind = CK_VectorSplat;
3007 SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
3008 }
else if (Self.CheckVectorCast(OpRange, DestType, SrcType,
Kind)) {
3015 if (Self.CheckVectorCast(OpRange, SrcType, DestType,
Kind))
3025 if (isa<ObjCSelectorExpr>(SrcExpr.get())) {
3026 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr);
3033 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_to_bfloat16)
3034 << SrcExpr.get()->getSourceRange();
3039 Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_from_bfloat16)
3040 << SrcExpr.get()->getSourceRange();
3049 Self.Diag(SrcExpr.get()->getExprLoc(),
3050 diag::err_cast_pointer_from_non_pointer_int)
3051 << SrcType << SrcExpr.get()->getSourceRange();
3060 Self.Diag(SrcExpr.get()->getBeginLoc(),
3061 diag::err_cast_pointer_to_non_pointer_int)
3062 << DestType << SrcExpr.get()->getSourceRange();
3067 if ((Self.Context.getTypeSize(SrcType) >
3068 Self.Context.getTypeSize(DestType)) &&
3078 : diag::warn_void_pointer_to_int_cast;
3080 Diag = diag::warn_pointer_to_enum_cast;
3082 Diag = diag::warn_pointer_to_int_cast;
3083 Self.Diag(OpRange.getBegin(),
Diag) << SrcType << DestType << OpRange;
3087 if (Self.getLangOpts().OpenCL && !Self.getOpenCLOptions().isAvailableOption(
3088 "cl_khr_fp16", Self.getLangOpts())) {
3090 Self.Diag(SrcExpr.get()->getBeginLoc(), diag::err_opencl_cast_to_half)
3091 << DestType << SrcExpr.get()->getSourceRange();
3098 if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) {
3100 if (SrcExpr.isInvalid())
3104 if (Self.getLangOpts().ObjCAutoRefCount && CastPtr) {
3107 Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers();
3109 ExprPtr->getPointeeType()->isObjCLifetimeType() &&
3111 Self.Diag(SrcExpr.get()->getBeginLoc(),
3112 diag::err_typecheck_incompatible_ownership)
3114 << SrcExpr.get()->getSourceRange();
3119 else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) {
3120 Self.Diag(SrcExpr.get()->getBeginLoc(),
3121 diag::err_arc_convesion_of_weak_unavailable)
3122 << 1 << SrcType << DestType << SrcExpr.get()->getSourceRange();
3129 Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type)
3130 << SrcType << DestType << OpRange;
3132 if (isa<PointerType>(SrcType) && isa<PointerType>(DestType)) {
3139 if (SrcRD && DestRD && SrcRD->
hasAttr<RandomizeLayoutAttr>() &&
3142 Self.Diag(OpRange.getBegin(), diag::err_cast_from_randomized_struct)
3143 << SrcType << DestType;
3152 Kind = Self.PrepareScalarCast(SrcExpr, DestType);
3153 if (SrcExpr.isInvalid())
3156 if (
Kind == CK_BitCast)
3160 void CastOperation::CheckBuiltinBitCast() {
3161 QualType SrcType = SrcExpr.get()->getType();
3163 if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
3164 diag::err_typecheck_cast_to_incomplete) ||
3165 Self.RequireCompleteType(OpRange.getBegin(), SrcType,
3166 diag::err_incomplete_type)) {
3171 if (SrcExpr.get()->isPRValue())
3172 SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcType, SrcExpr.get(),
3175 CharUnits DestSize = Self.Context.getTypeSizeInChars(DestType);
3176 CharUnits SourceSize = Self.Context.getTypeSizeInChars(SrcType);
3177 if (DestSize != SourceSize) {
3178 Self.Diag(OpRange.getBegin(), diag::err_bit_cast_type_size_mismatch)
3185 Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable)
3192 Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable)
3198 Kind = CK_LValueToRValueBitCast;
3213 QualType TheOffendingSrcType, TheOffendingDestType;
3216 &TheOffendingSrcType, &TheOffendingDestType,
3217 &CastAwayQualifiers) !=
3218 CastAwayConstnessKind::CACK_Similar)
3222 int qualifiers = -1;
3225 }
else if (CastAwayQualifiers.
hasConst()) {
3231 if (qualifiers == -1)
3233 << SrcType << DestType;
3236 << TheOffendingSrcType << TheOffendingDestType << qualifiers;
3248 Op.CheckCXXCStyleCast(
false,
3251 Op.CheckCStyleCast();
3254 if (Op.SrcExpr.isInvalid())
3261 Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
3270 assert(LPLoc.
isValid() &&
"List-initialization shouldn't get here.");
3275 Op.CheckCXXCStyleCast(
true,
false);
3276 if (Op.SrcExpr.isInvalid())
3279 auto *SubExpr = Op.SrcExpr.get();
3280 if (
auto *BindExpr = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
3281 SubExpr = BindExpr->getSubExpr();
3282 if (
auto *ConstructExpr = dyn_cast<CXXConstructExpr>(SubExpr))
3283 ConstructExpr->setParenOrBraceRange(
SourceRange(LPLoc, RPLoc));
3286 Context, Op.ResultType, Op.ValueKind, CastTypeInfo, Op.Kind,