#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTStructuralEquivalence.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/RecordLayout.h"
#include "clang/Basic/PartialDiagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Initialization.h"
#include "clang/Sema/SemaInternal.h"
#include "llvm/ADT/SmallVector.h"
#include <set>
using namespace clang;
enum TryCastResult {
TC_NotApplicable, TC_Success, TC_Extension, TC_Failed };
static bool isValidCast(TryCastResult TCR) {
return TCR == TC_Success || TCR == TC_Extension;
}
enum CastType {
CT_Const, CT_Static, CT_Reinterpret, CT_Dynamic, CT_CStyle, CT_Functional, CT_Addrspace };
namespace {
struct CastOperation {
CastOperation(Sema &S, QualType destType, ExprResult src)
: Self(S), SrcExpr(src), DestType(destType),
ResultType(destType.getNonLValueExprType(S.Context)),
ValueKind(Expr::getValueKindForType(destType)),
Kind(CK_Dependent), IsARCUnbridgedCast(false) {
if (!S.Context.getLangOpts().ObjC && !DestType->isRecordType() &&
!DestType->isArrayType()) {
DestType = DestType.getUnqualifiedType();
}
if (const BuiltinType *placeholder =
src.get()->getType()->getAsPlaceholderType()) {
PlaceholderKind = placeholder->getKind();
} else {
PlaceholderKind = (BuiltinType::Kind) 0;
}
}
Sema &Self;
ExprResult SrcExpr;
QualType DestType;
QualType ResultType;
ExprValueKind ValueKind;
CastKind Kind;
BuiltinType::Kind PlaceholderKind;
CXXCastPath BasePath;
bool IsARCUnbridgedCast;
SourceRange OpRange;
SourceRange DestRange;
void CheckConstCast();
void CheckReinterpretCast();
void CheckStaticCast();
void CheckDynamicCast();
void CheckCXXCStyleCast(bool FunctionalCast, bool ListInitialization);
void CheckCStyleCast();
void CheckBuiltinBitCast();
void CheckAddrspaceCast();
void updatePartOfExplicitCastFlags(CastExpr *CE) {
for (; auto *ICE = dyn_cast<ImplicitCastExpr>(CE->getSubExpr()); CE = ICE)
ICE->setIsPartOfExplicitCast(true);
}
ExprResult complete(CastExpr *castExpr) {
if (IsARCUnbridgedCast) {
castExpr = ImplicitCastExpr::Create(
Self.Context, Self.Context.ARCUnbridgedCastTy, CK_Dependent,
castExpr, nullptr, castExpr->getValueKind(),
Self.CurFPFeatureOverrides());
}
updatePartOfExplicitCastFlags(castExpr);
return castExpr;
}
bool claimPlaceholder(BuiltinType::Kind K) {
if (PlaceholderKind != K) return false;
PlaceholderKind = (BuiltinType::Kind) 0;
return true;
}
bool isPlaceholder() const {
return PlaceholderKind != 0;
}
bool isPlaceholder(BuiltinType::Kind K) const {
return PlaceholderKind == K;
}
void checkAddressSpaceCast(QualType SrcType, QualType DestType);
void checkCastAlign() {
Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange);
}
void checkObjCConversion(Sema::CheckedConversionKind CCK) {
assert(Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers());
Expr *src = SrcExpr.get();
if (Self.CheckObjCConversion(OpRange, DestType, src, CCK) ==
Sema::ACR_unbridged)
IsARCUnbridgedCast = true;
SrcExpr = src;
}
void checkNonOverloadPlaceholders() {
if (!isPlaceholder() || isPlaceholder(BuiltinType::Overload))
return;
SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
if (SrcExpr.isInvalid())
return;
PlaceholderKind = (BuiltinType::Kind) 0;
}
};
void CheckNoDeref(Sema &S, const QualType FromType, const QualType ToType,
SourceLocation OpLoc) {
if (const auto *PtrType = dyn_cast<PointerType>(FromType)) {
if (PtrType->getPointeeType()->hasAttr(attr::NoDeref)) {
if (const auto *DestType = dyn_cast<PointerType>(ToType)) {
if (!DestType->getPointeeType()->hasAttr(attr::NoDeref)) {
S.Diag(OpLoc, diag::warn_noderef_to_dereferenceable_pointer);
}
}
}
}
}
struct CheckNoDerefRAII {
CheckNoDerefRAII(CastOperation &Op) : Op(Op) {}
~CheckNoDerefRAII() {
if (!Op.SrcExpr.isInvalid())
CheckNoDeref(Op.Self, Op.SrcExpr.get()->getType(), Op.ResultType,
Op.OpRange.getBegin());
}
CastOperation &Op;
};
}
static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr,
QualType DestType);
static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr,
QualType DestType, bool CStyle,
CastKind &Kind,
CXXCastPath &BasePath,
unsigned &msg);
static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
QualType DestType, bool CStyle,
SourceRange OpRange,
unsigned &msg,
CastKind &Kind,
CXXCastPath &BasePath);
static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
QualType DestType, bool CStyle,
SourceRange OpRange,
unsigned &msg,
CastKind &Kind,
CXXCastPath &BasePath);
static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
CanQualType DestType, bool CStyle,
SourceRange OpRange,
QualType OrigSrcType,
QualType OrigDestType, unsigned &msg,
CastKind &Kind,
CXXCastPath &BasePath);
static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr,
QualType SrcType,
QualType DestType,bool CStyle,
SourceRange OpRange,
unsigned &msg,
CastKind &Kind,
CXXCastPath &BasePath);
static TryCastResult TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType,
Sema::CheckedConversionKind CCK,
SourceRange OpRange,
unsigned &msg, CastKind &Kind,
bool ListInitialization);
static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType,
Sema::CheckedConversionKind CCK,
SourceRange OpRange,
unsigned &msg, CastKind &Kind,
CXXCastPath &BasePath,
bool ListInitialization);
static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType, bool CStyle,
unsigned &msg);
static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType, bool CStyle,
SourceRange OpRange, unsigned &msg,
CastKind &Kind);
static TryCastResult TryAddressSpaceCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType, bool CStyle,
unsigned &msg, CastKind &Kind);
ExprResult
Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
SourceLocation LAngleBracketLoc, Declarator &D,
SourceLocation RAngleBracketLoc,
SourceLocation LParenLoc, Expr *E,
SourceLocation RParenLoc) {
assert(!D.isInvalidType());
TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, E->getType());
if (D.isInvalidType())
return ExprError();
if (getLangOpts().CPlusPlus) {
CheckExtraCXXDefaultArguments(D);
}
return BuildCXXNamedCast(OpLoc, Kind, TInfo, E,
SourceRange(LAngleBracketLoc, RAngleBracketLoc),
SourceRange(LParenLoc, RParenLoc));
}
ExprResult
Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
TypeSourceInfo *DestTInfo, Expr *E,
SourceRange AngleBrackets, SourceRange Parens) {
ExprResult Ex = E;
QualType DestType = DestTInfo->getType();
bool TypeDependent =
DestType->isDependentType() || Ex.get()->isTypeDependent();
CastOperation Op(*this, DestType, E);
Op.OpRange = SourceRange(OpLoc, Parens.getEnd());
Op.DestRange = AngleBrackets;
switch (Kind) {
default: llvm_unreachable("Unknown C++ cast!");
case tok::kw_addrspace_cast:
if (!TypeDependent) {
Op.CheckAddrspaceCast();
if (Op.SrcExpr.isInvalid())
return ExprError();
}
return Op.complete(CXXAddrspaceCastExpr::Create(
Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
DestTInfo, OpLoc, Parens.getEnd(), AngleBrackets));
case tok::kw_const_cast:
if (!TypeDependent) {
Op.CheckConstCast();
if (Op.SrcExpr.isInvalid())
return ExprError();
DiscardMisalignedMemberAddress(DestType.getTypePtr(), E);
}
return Op.complete(CXXConstCastExpr::Create(Context, Op.ResultType,
Op.ValueKind, Op.SrcExpr.get(), DestTInfo,
OpLoc, Parens.getEnd(),
AngleBrackets));
case tok::kw_dynamic_cast: {
if (getLangOpts().OpenCLCPlusPlus) {
return ExprError(Diag(OpLoc, diag::err_openclcxx_not_supported)
<< "dynamic_cast");
}
if (!TypeDependent) {
Op.CheckDynamicCast();
if (Op.SrcExpr.isInvalid())
return ExprError();
}
return Op.complete(CXXDynamicCastExpr::Create(Context, Op.ResultType,
Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
&Op.BasePath, DestTInfo,
OpLoc, Parens.getEnd(),
AngleBrackets));
}
case tok::kw_reinterpret_cast: {
if (!TypeDependent) {
Op.CheckReinterpretCast();
if (Op.SrcExpr.isInvalid())
return ExprError();
DiscardMisalignedMemberAddress(DestType.getTypePtr(), E);
}
return Op.complete(CXXReinterpretCastExpr::Create(Context, Op.ResultType,
Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
nullptr, DestTInfo, OpLoc,
Parens.getEnd(),
AngleBrackets));
}
case tok::kw_static_cast: {
if (!TypeDependent) {
Op.CheckStaticCast();
if (Op.SrcExpr.isInvalid())
return ExprError();
DiscardMisalignedMemberAddress(DestType.getTypePtr(), E);
}
return Op.complete(CXXStaticCastExpr::Create(
Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
&Op.BasePath, DestTInfo, CurFPFeatureOverrides(), OpLoc,
Parens.getEnd(), AngleBrackets));
}
}
}
ExprResult Sema::ActOnBuiltinBitCastExpr(SourceLocation KWLoc, Declarator &D,
ExprResult Operand,
SourceLocation RParenLoc) {
assert(!D.isInvalidType());
TypeSourceInfo *TInfo = GetTypeForDeclaratorCast(D, Operand.get()->getType());
if (D.isInvalidType())
return ExprError();
return BuildBuiltinBitCastExpr(KWLoc, TInfo, Operand.get(), RParenLoc);
}
ExprResult Sema::BuildBuiltinBitCastExpr(SourceLocation KWLoc,
TypeSourceInfo *TSI, Expr *Operand,
SourceLocation RParenLoc) {
CastOperation Op(*this, TSI->getType(), Operand);
Op.OpRange = SourceRange(KWLoc, RParenLoc);
TypeLoc TL = TSI->getTypeLoc();
Op.DestRange = SourceRange(TL.getBeginLoc(), TL.getEndLoc());
if (!Operand->isTypeDependent() && !TSI->getType()->isDependentType()) {
Op.CheckBuiltinBitCast();
if (Op.SrcExpr.isInvalid())
return ExprError();
}
BuiltinBitCastExpr *BCE =
new (Context) BuiltinBitCastExpr(Op.ResultType, Op.ValueKind, Op.Kind,
Op.SrcExpr.get(), TSI, KWLoc, RParenLoc);
return Op.complete(BCE);
}
static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT,
SourceRange range, Expr *src,
QualType destType,
bool listInitialization) {
switch (CT) {
case CT_Const:
case CT_Reinterpret:
case CT_Dynamic:
case CT_Addrspace:
return false;
case CT_Static:
case CT_CStyle:
case CT_Functional:
break;
}
QualType srcType = src->getType();
if (!destType->isRecordType() && !srcType->isRecordType())
return false;
InitializedEntity entity = InitializedEntity::InitializeTemporary(destType);
InitializationKind initKind
= (CT == CT_CStyle)? InitializationKind::CreateCStyleCast(range.getBegin(),
range, listInitialization)
: (CT == CT_Functional)? InitializationKind::CreateFunctionalCast(range,
listInitialization)
: InitializationKind::CreateCast( range);
InitializationSequence sequence(S, entity, initKind, src);
assert(sequence.Failed() && "initialization succeeded on second try?");
switch (sequence.getFailureKind()) {
default: return false;
case InitializationSequence::FK_ConstructorOverloadFailed:
case InitializationSequence::FK_UserConversionOverloadFailed:
break;
}
OverloadCandidateSet &candidates = sequence.getFailedCandidateSet();
unsigned msg = 0;
OverloadCandidateDisplayKind howManyCandidates = OCD_AllCandidates;
switch (sequence.getFailedOverloadResult()) {
case OR_Success: llvm_unreachable("successful failed overload");
case OR_No_Viable_Function:
if (candidates.empty())
msg = diag::err_ovl_no_conversion_in_cast;
else
msg = diag::err_ovl_no_viable_conversion_in_cast;
howManyCandidates = OCD_AllCandidates;
break;
case OR_Ambiguous:
msg = diag::err_ovl_ambiguous_conversion_in_cast;
howManyCandidates = OCD_AmbiguousCandidates;
break;
case OR_Deleted:
msg = diag::err_ovl_deleted_conversion_in_cast;
howManyCandidates = OCD_ViableCandidates;
break;
}
candidates.NoteCandidates(
PartialDiagnosticAt(range.getBegin(),
S.PDiag(msg) << CT << srcType << destType << range
<< src->getSourceRange()),
S, howManyCandidates, src);
return true;
}
static void diagnoseBadCast(Sema &S, unsigned msg, CastType castType,
SourceRange opRange, Expr *src, QualType destType,
bool listInitialization) {
if (msg == diag::err_bad_cxx_cast_generic &&
tryDiagnoseOverloadedCast(S, castType, opRange, src, destType,
listInitialization))
return;
S.Diag(opRange.getBegin(), msg) << castType
<< src->getType() << destType << opRange << src->getSourceRange();
int DifferentPtrness = 0;
QualType From = destType;
if (auto Ptr = From->getAs<PointerType>()) {
From = Ptr->getPointeeType();
DifferentPtrness++;
}
QualType To = src->getType();
if (auto Ptr = To->getAs<PointerType>()) {
To = Ptr->getPointeeType();
DifferentPtrness--;
}
if (!DifferentPtrness) {
auto RecFrom = From->getAs<RecordType>();
auto RecTo = To->getAs<RecordType>();
if (RecFrom && RecTo) {
auto DeclFrom = RecFrom->getAsCXXRecordDecl();
if (!DeclFrom->isCompleteDefinition())
S.Diag(DeclFrom->getLocation(), diag::note_type_incomplete) << DeclFrom;
auto DeclTo = RecTo->getAsCXXRecordDecl();
if (!DeclTo->isCompleteDefinition())
S.Diag(DeclTo->getLocation(), diag::note_type_incomplete) << DeclTo;
}
}
}
namespace {
enum CastAwayConstnessKind {
CACK_None = 0,
CACK_Similar = 1,
CACK_SimilarKind = 2,
CACK_Incoherent = 3,
};
}
static CastAwayConstnessKind
unwrapCastAwayConstnessLevel(ASTContext &Context, QualType &T1, QualType &T2) {
enum { None, Ptr, MemPtr, BlockPtr, Array };
auto Classify = [](QualType T) {
if (T->isAnyPointerType()) return Ptr;
if (T->isMemberPointerType()) return MemPtr;
if (T->isBlockPointerType()) return BlockPtr;
if (T->isConstantArrayType() || T->isIncompleteArrayType()) return Array;
return None;
};
auto Unwrap = [&](QualType T) {
if (auto *AT = Context.getAsArrayType(T))
return AT->getElementType();
return T->getPointeeType();
};
CastAwayConstnessKind Kind;
if (T2->isReferenceType()) {
T2 = T2->getPointeeType();
Kind = CastAwayConstnessKind::CACK_Similar;
} else if (Context.UnwrapSimilarTypes(T1, T2)) {
Kind = CastAwayConstnessKind::CACK_Similar;
} else {
int T1Class = Classify(T1);
if (T1Class == None)
return CastAwayConstnessKind::CACK_None;
int T2Class = Classify(T2);
if (T2Class == None)
return CastAwayConstnessKind::CACK_None;
T1 = Unwrap(T1);
T2 = Unwrap(T2);
Kind = T1Class == T2Class ? CastAwayConstnessKind::CACK_SimilarKind
: CastAwayConstnessKind::CACK_Incoherent;
}
while (true) {
Context.UnwrapSimilarArrayTypes(T1, T2);
if (Classify(T1) != Array)
break;
auto T2Class = Classify(T2);
if (T2Class == None)
break;
if (T2Class != Array)
Kind = CastAwayConstnessKind::CACK_Incoherent;
else if (Kind != CastAwayConstnessKind::CACK_Incoherent)
Kind = CastAwayConstnessKind::CACK_SimilarKind;
T1 = Unwrap(T1);
T2 = Unwrap(T2).withCVRQualifiers(T2.getCVRQualifiers());
}
return Kind;
}
static CastAwayConstnessKind
CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType,
bool CheckCVR, bool CheckObjCLifetime,
QualType *TheOffendingSrcType = nullptr,
QualType *TheOffendingDestType = nullptr,
Qualifiers *CastAwayQualifiers = nullptr) {
if (!CheckCVR && CheckObjCLifetime && !Self.Context.getLangOpts().ObjC)
return CastAwayConstnessKind::CACK_None;
if (!DestType->isReferenceType()) {
assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType() ||
SrcType->isBlockPointerType()) &&
"Source type is not pointer or pointer to member.");
assert((DestType->isAnyPointerType() || DestType->isMemberPointerType() ||
DestType->isBlockPointerType()) &&
"Destination type is not pointer or pointer to member.");
}
QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
UnwrappedDestType = Self.Context.getCanonicalType(DestType);
QualType PrevUnwrappedSrcType = UnwrappedSrcType;
QualType PrevUnwrappedDestType = UnwrappedDestType;
auto WorstKind = CastAwayConstnessKind::CACK_Similar;
bool AllConstSoFar = true;
while (auto Kind = unwrapCastAwayConstnessLevel(
Self.Context, UnwrappedSrcType, UnwrappedDestType)) {
if (Kind > WorstKind)
WorstKind = Kind;
Qualifiers SrcQuals, DestQuals;
Self.Context.getUnqualifiedArrayType(UnwrappedSrcType, SrcQuals);
Self.Context.getUnqualifiedArrayType(UnwrappedDestType, DestQuals);
if (UnwrappedSrcType->isObjCObjectType() ||
UnwrappedDestType->isObjCObjectType())
SrcQuals.removeConst();
if (CheckCVR) {
Qualifiers SrcCvrQuals =
Qualifiers::fromCVRMask(SrcQuals.getCVRQualifiers());
Qualifiers DestCvrQuals =
Qualifiers::fromCVRMask(DestQuals.getCVRQualifiers());
if (SrcCvrQuals != DestCvrQuals) {
if (CastAwayQualifiers)
*CastAwayQualifiers = SrcCvrQuals - DestCvrQuals;
if (!DestCvrQuals.compatiblyIncludes(SrcCvrQuals)) {
if (TheOffendingSrcType)
*TheOffendingSrcType = PrevUnwrappedSrcType;
if (TheOffendingDestType)
*TheOffendingDestType = PrevUnwrappedDestType;
return WorstKind;
}
if (!AllConstSoFar)
return WorstKind;
}
}
if (CheckObjCLifetime &&
!DestQuals.compatiblyIncludesObjCLifetime(SrcQuals))
return WorstKind;
if (AllConstSoFar && !DestQuals.hasConst()) {
AllConstSoFar = false;
if (TheOffendingSrcType)
*TheOffendingSrcType = PrevUnwrappedSrcType;
if (TheOffendingDestType)
*TheOffendingDestType = PrevUnwrappedDestType;
}
PrevUnwrappedSrcType = UnwrappedSrcType;
PrevUnwrappedDestType = UnwrappedDestType;
}
return CastAwayConstnessKind::CACK_None;
}
static TryCastResult getCastAwayConstnessCastKind(CastAwayConstnessKind CACK,
unsigned &DiagID) {
switch (CACK) {
case CastAwayConstnessKind::CACK_None:
llvm_unreachable("did not cast away constness");
case CastAwayConstnessKind::CACK_Similar:
case CastAwayConstnessKind::CACK_SimilarKind:
DiagID = diag::err_bad_cxx_cast_qualifiers_away;
return TC_Failed;
case CastAwayConstnessKind::CACK_Incoherent:
DiagID = diag::ext_bad_cxx_cast_qualifiers_away_incoherent;
return TC_Extension;
}
llvm_unreachable("unexpected cast away constness kind");
}
void CastOperation::CheckDynamicCast() {
CheckNoDerefRAII NoderefCheck(*this);
if (ValueKind == VK_PRValue)
SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
else if (isPlaceholder())
SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
if (SrcExpr.isInvalid()) return;
QualType OrigSrcType = SrcExpr.get()->getType();
QualType DestType = Self.Context.getCanonicalType(this->DestType);
QualType DestPointee;
const PointerType *DestPointer = DestType->getAs<PointerType>();
const ReferenceType *DestReference = nullptr;
if (DestPointer) {
DestPointee = DestPointer->getPointeeType();
} else if ((DestReference = DestType->getAs<ReferenceType>())) {
DestPointee = DestReference->getPointeeType();
} else {
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
<< this->DestType << DestRange;
SrcExpr = ExprError();
return;
}
const RecordType *DestRecord = DestPointee->getAs<RecordType>();
if (DestPointee->isVoidType()) {
assert(DestPointer && "Reference to void is not possible");
} else if (DestRecord) {
if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
diag::err_bad_cast_incomplete,
DestRange)) {
SrcExpr = ExprError();
return;
}
} else {
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
<< DestPointee.getUnqualifiedType() << DestRange;
SrcExpr = ExprError();
return;
}
QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
QualType SrcPointee;
if (DestPointer) {
if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
SrcPointee = SrcPointer->getPointeeType();
} else {
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
<< OrigSrcType << this->DestType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
} else if (DestReference->isLValueReferenceType()) {
if (!SrcExpr.get()->isLValue()) {
Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
<< CT_Dynamic << OrigSrcType << this->DestType << OpRange;
}
SrcPointee = SrcType;
} else {
if (SrcExpr.get()->isPRValue())
SrcExpr = Self.CreateMaterializeTemporaryExpr(
SrcType, SrcExpr.get(), false);
SrcPointee = SrcType;
}
const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
if (SrcRecord) {
if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
diag::err_bad_cast_incomplete,
SrcExpr.get())) {
SrcExpr = ExprError();
return;
}
} else {
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
<< SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
assert((DestPointer || DestReference) &&
"Bad destination non-ptr/ref slipped through.");
assert((DestRecord || DestPointee->isVoidType()) &&
"Bad destination pointee slipped through.");
assert(SrcRecord && "Bad source pointee slipped through.");
if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_qualifiers_away)
<< CT_Dynamic << OrigSrcType << this->DestType << OpRange;
SrcExpr = ExprError();
return;
}
if (DestRecord == SrcRecord) {
Kind = CK_NoOp;
return;
}
if (DestRecord &&
Self.IsDerivedFrom(OpRange.getBegin(), SrcPointee, DestPointee)) {
if (Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
OpRange.getBegin(), OpRange,
&BasePath)) {
SrcExpr = ExprError();
return;
}
Kind = CK_DerivedToBase;
return;
}
const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
assert(SrcDecl && "Definition missing");
if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
<< SrcPointee.getUnqualifiedType() << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
}
if (!Self.getLangOpts().RTTI && !DestPointee->isVoidType()) {
Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti);
SrcExpr = ExprError();
return;
}
if (!Self.getLangOpts().RTTIData) {
bool MicrosoftABI =
Self.getASTContext().getTargetInfo().getCXXABI().isMicrosoft();
bool isClangCL = Self.getDiagnostics().getDiagnosticOptions().getFormat() ==
DiagnosticOptions::MSVC;
if (MicrosoftABI || !DestPointee->isVoidType())
Self.Diag(OpRange.getBegin(),
diag::warn_no_dynamic_cast_with_rtti_disabled)
<< isClangCL;
}
Kind = CK_Dynamic;
}
void CastOperation::CheckConstCast() {
CheckNoDerefRAII NoderefCheck(*this);
if (ValueKind == VK_PRValue)
SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
else if (isPlaceholder())
SrcExpr = Self.CheckPlaceholderExpr(SrcExpr.get());
if (SrcExpr.isInvalid()) return;
unsigned msg = diag::err_bad_cxx_cast_generic;
auto TCR = TryConstCast(Self, SrcExpr, DestType, false, msg);
if (TCR != TC_Success && msg != 0) {
Self.Diag(OpRange.getBegin(), msg) << CT_Const
<< SrcExpr.get()->getType() << DestType << OpRange;
}
if (!isValidCast(TCR))
SrcExpr = ExprError();
}
void CastOperation::CheckAddrspaceCast() {
unsigned msg = diag::err_bad_cxx_cast_generic;
auto TCR =
TryAddressSpaceCast(Self, SrcExpr, DestType, false, msg, Kind);
if (TCR != TC_Success && msg != 0) {
Self.Diag(OpRange.getBegin(), msg)
<< CT_Addrspace << SrcExpr.get()->getType() << DestType << OpRange;
}
if (!isValidCast(TCR))
SrcExpr = ExprError();
}
static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr,
QualType DestType,
SourceRange OpRange) {
QualType SrcType = SrcExpr->getType();
const CXXRecordDecl *SrcPointeeRD = SrcType->getPointeeCXXRecordDecl();
const CXXRecordDecl *SrcRD =
SrcPointeeRD ? SrcPointeeRD : SrcType->getAsCXXRecordDecl();
if (!SrcRD || !SrcRD->isCompleteDefinition() || SrcRD->isInvalidDecl())
return;
const CXXRecordDecl *DestRD = DestType->getPointeeCXXRecordDecl();
if (!DestRD || !DestRD->isCompleteDefinition() || DestRD->isInvalidDecl())
return;
enum {
ReinterpretUpcast,
ReinterpretDowncast
} ReinterpretKind;
CXXBasePaths BasePaths;
if (SrcRD->isDerivedFrom(DestRD, BasePaths))
ReinterpretKind = ReinterpretUpcast;
else if (DestRD->isDerivedFrom(SrcRD, BasePaths))
ReinterpretKind = ReinterpretDowncast;
else
return;
bool VirtualBase = true;
bool NonZeroOffset = false;
for (CXXBasePaths::const_paths_iterator I = BasePaths.begin(),
E = BasePaths.end();
I != E; ++I) {
const CXXBasePath &Path = *I;
CharUnits Offset = CharUnits::Zero();
bool IsVirtual = false;
for (CXXBasePath::const_iterator IElem = Path.begin(), EElem = Path.end();
IElem != EElem; ++IElem) {
IsVirtual = IElem->Base->isVirtual();
if (IsVirtual)
break;
const CXXRecordDecl *BaseRD = IElem->Base->getType()->getAsCXXRecordDecl();
assert(BaseRD && "Base type should be a valid unqualified class type");
const CXXRecordDecl *Class = IElem->Class,
*ClassDefinition = Class->getDefinition();
if (Class->isInvalidDecl() || !ClassDefinition ||
!ClassDefinition->isCompleteDefinition())
return;
const ASTRecordLayout &DerivedLayout =
Self.Context.getASTRecordLayout(Class);
Offset += DerivedLayout.getBaseClassOffset(BaseRD);
}
if (!IsVirtual) {
if (Offset.isZero())
return;
else
NonZeroOffset = true;
}
VirtualBase = VirtualBase && IsVirtual;
}
(void) NonZeroOffset; assert((VirtualBase || NonZeroOffset) &&
"Should have returned if has non-virtual base with zero offset");
QualType BaseType =
ReinterpretKind == ReinterpretUpcast? DestType : SrcType;
QualType DerivedType =
ReinterpretKind == ReinterpretUpcast? SrcType : DestType;
SourceLocation BeginLoc = OpRange.getBegin();
Self.Diag(BeginLoc, diag::warn_reinterpret_different_from_static)
<< DerivedType << BaseType << !VirtualBase << int(ReinterpretKind)
<< OpRange;
Self.Diag(BeginLoc, diag::note_reinterpret_updowncast_use_static)
<< int(ReinterpretKind)
<< FixItHint::CreateReplacement(BeginLoc, "static_cast");
}
static bool argTypeIsABIEquivalent(QualType SrcType, QualType DestType,
ASTContext &Context) {
if (SrcType->isPointerType() && DestType->isPointerType())
return true;
if (SrcType->isIntegralType(Context) && DestType->isIntegralType(Context))
if (Context.getTypeInfoInChars(SrcType).Width ==
Context.getTypeInfoInChars(DestType).Width)
return true;
return Context.hasSameUnqualifiedType(SrcType, DestType);
}
static bool checkCastFunctionType(Sema &Self, const ExprResult &SrcExpr,
QualType DestType) {
if (Self.Diags.isIgnored(diag::warn_cast_function_type,
SrcExpr.get()->getExprLoc()))
return true;
QualType SrcType = SrcExpr.get()->getType();
const FunctionType *SrcFTy = nullptr;
const FunctionType *DstFTy = nullptr;
if (((SrcType->isBlockPointerType() || SrcType->isFunctionPointerType()) &&
DestType->isFunctionPointerType()) ||
(SrcType->isMemberFunctionPointerType() &&
DestType->isMemberFunctionPointerType())) {
SrcFTy = SrcType->getPointeeType()->castAs<FunctionType>();
DstFTy = DestType->getPointeeType()->castAs<FunctionType>();
} else if (SrcType->isFunctionType() && DestType->isFunctionReferenceType()) {
SrcFTy = SrcType->castAs<FunctionType>();
DstFTy = DestType.getNonReferenceType()->castAs<FunctionType>();
} else {
return true;
}
assert(SrcFTy && DstFTy);
auto IsVoidVoid = [](const FunctionType *T) {
if (!T->getReturnType()->isVoidType())
return false;
if (const auto *PT = T->getAs<FunctionProtoType>())
return !PT->isVariadic() && PT->getNumParams() == 0;
return false;
};
if (IsVoidVoid(SrcFTy) || IsVoidVoid(DstFTy))
return true;
if (!argTypeIsABIEquivalent(SrcFTy->getReturnType(), DstFTy->getReturnType(),
Self.Context))
return false;
if (SrcFTy->isFunctionNoProtoType() || DstFTy->isFunctionNoProtoType())
return true;
const auto *SrcFPTy = cast<FunctionProtoType>(SrcFTy);
const auto *DstFPTy = cast<FunctionProtoType>(DstFTy);
unsigned NumParams = SrcFPTy->getNumParams();
unsigned DstNumParams = DstFPTy->getNumParams();
if (NumParams > DstNumParams) {
if (!DstFPTy->isVariadic())
return false;
NumParams = DstNumParams;
} else if (NumParams < DstNumParams) {
if (!SrcFPTy->isVariadic())
return false;
}
for (unsigned i = 0; i < NumParams; ++i)
if (!argTypeIsABIEquivalent(SrcFPTy->getParamType(i),
DstFPTy->getParamType(i), Self.Context))
return false;
return true;
}
void CastOperation::CheckReinterpretCast() {
if (ValueKind == VK_PRValue && !isPlaceholder(BuiltinType::Overload))
SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
else
checkNonOverloadPlaceholders();
if (SrcExpr.isInvalid()) return;
unsigned msg = diag::err_bad_cxx_cast_generic;
TryCastResult tcr =
TryReinterpretCast(Self, SrcExpr, DestType,
false, OpRange, msg, Kind);
if (tcr != TC_Success && msg != 0) {
if (SrcExpr.isInvalid()) return;
if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_overload)
<< OverloadExpr::find(SrcExpr.get()).Expression->getName()
<< DestType << OpRange;
Self.NoteAllOverloadCandidates(SrcExpr.get());
} else {
diagnoseBadCast(Self, msg, CT_Reinterpret, OpRange, SrcExpr.get(),
DestType, false);
}
}
if (isValidCast(tcr)) {
if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
checkObjCConversion(Sema::CCK_OtherCast);
DiagnoseReinterpretUpDownCast(Self, SrcExpr.get(), DestType, OpRange);
if (!checkCastFunctionType(Self, SrcExpr, DestType))
Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type)
<< SrcExpr.get()->getType() << DestType << OpRange;
} else {
SrcExpr = ExprError();
}
}
void CastOperation::CheckStaticCast() {
CheckNoDerefRAII NoderefCheck(*this);
if (isPlaceholder()) {
checkNonOverloadPlaceholders();
if (SrcExpr.isInvalid())
return;
}
if (DestType->isVoidType()) {
Kind = CK_ToVoid;
if (claimPlaceholder(BuiltinType::Overload)) {
Self.ResolveAndFixSingleFunctionTemplateSpecialization(SrcExpr,
false, true, OpRange, DestType, diag::err_bad_static_cast_overload);
if (SrcExpr.isInvalid())
return;
}
SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
return;
}
if (ValueKind == VK_PRValue && !DestType->isRecordType() &&
!isPlaceholder(BuiltinType::Overload)) {
SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
if (SrcExpr.isInvalid()) return;
}
unsigned msg = diag::err_bad_cxx_cast_generic;
TryCastResult tcr
= TryStaticCast(Self, SrcExpr, DestType, Sema::CCK_OtherCast, OpRange, msg,
Kind, BasePath, false);
if (tcr != TC_Success && msg != 0) {
if (SrcExpr.isInvalid())
return;
if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
OverloadExpr* oe = OverloadExpr::find(SrcExpr.get()).Expression;
Self.Diag(OpRange.getBegin(), diag::err_bad_static_cast_overload)
<< oe->getName() << DestType << OpRange
<< oe->getQualifierLoc().getSourceRange();
Self.NoteAllOverloadCandidates(SrcExpr.get());
} else {
diagnoseBadCast(Self, msg, CT_Static, OpRange, SrcExpr.get(), DestType,
false);
}
}
if (isValidCast(tcr)) {
if (Kind == CK_BitCast)
checkCastAlign();
if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
checkObjCConversion(Sema::CCK_OtherCast);
} else {
SrcExpr = ExprError();
}
}
static bool IsAddressSpaceConversion(QualType SrcType, QualType DestType) {
auto *SrcPtrType = SrcType->getAs<PointerType>();
if (!SrcPtrType)
return false;
auto *DestPtrType = DestType->getAs<PointerType>();
if (!DestPtrType)
return false;
return SrcPtrType->getPointeeType().getAddressSpace() !=
DestPtrType->getPointeeType().getAddressSpace();
}
static TryCastResult TryStaticCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType,
Sema::CheckedConversionKind CCK,
SourceRange OpRange, unsigned &msg,
CastKind &Kind, CXXCastPath &BasePath,
bool ListInitialization) {
bool CStyle
= (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
TryCastResult tcr;
tcr = TryStaticReferenceDowncast(Self, SrcExpr.get(), DestType, CStyle,
OpRange, msg, Kind, BasePath);
if (tcr != TC_NotApplicable)
return tcr;
tcr = TryLValueToRValueCast(Self, SrcExpr.get(), DestType, CStyle, Kind,
BasePath, msg);
if (tcr != TC_NotApplicable)
return tcr;
tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CCK, OpRange, msg,
Kind, ListInitialization);
if (SrcExpr.isInvalid())
return TC_Failed;
if (tcr != TC_NotApplicable)
return tcr;
QualType SrcType = Self.Context.getCanonicalType(SrcExpr.get()->getType());
if (const EnumType *Enum = SrcType->getAs<EnumType>()) {
if (Enum->getDecl()->isScoped()) {
if (DestType->isBooleanType()) {
Kind = CK_IntegralToBoolean;
return TC_Success;
} else if (DestType->isIntegralType(Self.Context)) {
Kind = CK_IntegralCast;
return TC_Success;
} else if (DestType->isRealFloatingType()) {
Kind = CK_IntegralToFloating;
return TC_Success;
}
}
}
if (DestType->isEnumeralType()) {
if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
diag::err_bad_cast_incomplete)) {
SrcExpr = ExprError();
return TC_Failed;
}
if (SrcType->isIntegralOrEnumerationType()) {
const EnumType *Enum = DestType->castAs<EnumType>();
Kind = Enum->getDecl()->isFixed() &&
Enum->getDecl()->getIntegerType()->isBooleanType()
? CK_IntegralToBoolean
: CK_IntegralCast;
return TC_Success;
} else if (SrcType->isRealFloatingType()) {
Kind = CK_FloatingToIntegral;
return TC_Success;
}
}
tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
Kind, BasePath);
if (tcr != TC_NotApplicable)
return tcr;
tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
OpRange, msg, Kind, BasePath);
if (tcr != TC_NotApplicable)
return tcr;
if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
QualType SrcPointee = SrcPointer->getPointeeType();
if (SrcPointee->isVoidType()) {
if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
QualType DestPointee = DestPointer->getPointeeType();
if (DestPointee->isIncompleteOrObjectType()) {
if (!CStyle) {
Qualifiers DestPointeeQuals = DestPointee.getQualifiers();
Qualifiers SrcPointeeQuals = SrcPointee.getQualifiers();
DestPointeeQuals.removeObjCGCAttr();
DestPointeeQuals.removeObjCLifetime();
SrcPointeeQuals.removeObjCGCAttr();
SrcPointeeQuals.removeObjCLifetime();
if (DestPointeeQuals != SrcPointeeQuals &&
!DestPointeeQuals.compatiblyIncludes(SrcPointeeQuals)) {
msg = diag::err_bad_cxx_cast_qualifiers_away;
return TC_Failed;
}
}
Kind = IsAddressSpaceConversion(SrcType, DestType)
? CK_AddressSpaceConversion
: CK_BitCast;
return TC_Success;
}
if (!CStyle && Self.getLangOpts().MSVCCompat &&
DestPointee->isFunctionType()) {
Self.Diag(OpRange.getBegin(), diag::ext_ms_cast_fn_obj) << OpRange;
Kind = CK_BitCast;
return TC_Success;
}
}
else if (DestType->isObjCObjectPointerType()) {
Kind = CK_CPointerToObjCPointerCast;
return TC_Success;
}
else if (CStyle && DestType->isBlockPointerType()) {
Kind = CK_AnyPointerToBlockPointerCast;
return TC_Success;
}
}
}
if (SrcType->isObjCObjectPointerType() &&
DestType->isObjCObjectPointerType()) {
Kind = CK_BitCast;
return TC_Success;
}
if (!CStyle &&
Self.CheckTollFreeBridgeStaticCast(DestType, SrcExpr.get(), Kind))
return TC_Success;
if (auto SrcPointer = SrcType->getAs<PointerType>())
if (auto DestPointer = DestType->getAs<PointerType>())
if (SrcPointer->getPointeeType()->getAs<RecordType>() &&
DestPointer->getPointeeType()->getAs<RecordType>())
msg = diag::err_bad_cxx_cast_unrelated_class;
if (SrcType->isMatrixType() && DestType->isMatrixType()) {
if (Self.CheckMatrixCast(OpRange, DestType, SrcType, Kind)) {
SrcExpr = ExprError();
return TC_Failed;
}
return TC_Success;
}
return TC_NotApplicable;
}
TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr,
QualType DestType, bool CStyle,
CastKind &Kind, CXXCastPath &BasePath,
unsigned &msg) {
const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
if (!R)
return TC_NotApplicable;
if (!SrcExpr->isGLValue())
return TC_NotApplicable;
QualType FromType = SrcExpr->getType();
QualType ToType = R->getPointeeType();
if (CStyle) {
FromType = FromType.getUnqualifiedType();
ToType = ToType.getUnqualifiedType();
}
Sema::ReferenceConversions RefConv;
Sema::ReferenceCompareResult RefResult = Self.CompareReferenceRelationship(
SrcExpr->getBeginLoc(), ToType, FromType, &RefConv);
if (RefResult != Sema::Ref_Compatible) {
if (CStyle || RefResult == Sema::Ref_Incompatible)
return TC_NotApplicable;
msg = SrcExpr->isLValue() ? diag::err_bad_lvalue_to_rvalue_cast
: diag::err_bad_rvalue_to_rvalue_cast;
return TC_Failed;
}
if (RefConv & Sema::ReferenceConversions::DerivedToBase) {
Kind = CK_DerivedToBase;
CXXBasePaths Paths(true, true,
true);
if (!Self.IsDerivedFrom(SrcExpr->getBeginLoc(), SrcExpr->getType(),
R->getPointeeType(), Paths))
return TC_NotApplicable;
Self.BuildBasePathArray(Paths, BasePath);
} else
Kind = CK_NoOp;
return TC_Success;
}
TryCastResult
TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
bool CStyle, SourceRange OpRange,
unsigned &msg, CastKind &Kind,
CXXCastPath &BasePath) {
const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
if (!DestReference) {
return TC_NotApplicable;
}
bool RValueRef = DestReference->isRValueReferenceType();
if (!RValueRef && !SrcExpr->isLValue()) {
msg = diag::err_bad_cxx_cast_rvalue;
return TC_NotApplicable;
}
QualType DestPointee = DestReference->getPointeeType();
return TryStaticDowncast(Self,
Self.Context.getCanonicalType(SrcExpr->getType()),
Self.Context.getCanonicalType(DestPointee), CStyle,
OpRange, SrcExpr->getType(), DestType, msg, Kind,
BasePath);
}
TryCastResult
TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
bool CStyle, SourceRange OpRange,
unsigned &msg, CastKind &Kind,
CXXCastPath &BasePath) {
const PointerType *DestPointer = DestType->getAs<PointerType>();
if (!DestPointer) {
return TC_NotApplicable;
}
const PointerType *SrcPointer = SrcType->getAs<PointerType>();
if (!SrcPointer) {
msg = diag::err_bad_static_cast_pointer_nonpointer;
return TC_NotApplicable;
}
return TryStaticDowncast(Self,
Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
Self.Context.getCanonicalType(DestPointer->getPointeeType()),
CStyle, OpRange, SrcType, DestType, msg, Kind,
BasePath);
}
TryCastResult
TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
bool CStyle, SourceRange OpRange, QualType OrigSrcType,
QualType OrigDestType, unsigned &msg,
CastKind &Kind, CXXCastPath &BasePath) {
if (!Self.isCompleteType(OpRange.getBegin(), SrcType) ||
!Self.isCompleteType(OpRange.getBegin(), DestType))
return TC_NotApplicable;
if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
return TC_NotApplicable;
}
CXXBasePaths Paths(true, true,
true);
if (!Self.IsDerivedFrom(OpRange.getBegin(), DestType, SrcType, Paths)) {
return TC_NotApplicable;
}
if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
msg = diag::err_bad_cxx_cast_qualifiers_away;
return TC_Failed;
}
if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
if (!Paths.isRecordingPaths()) {
Paths.clear();
Paths.setRecordingPaths(true);
Self.IsDerivedFrom(OpRange.getBegin(), DestType, SrcType, Paths);
}
std::string PathDisplayStr;
std::set<unsigned> DisplayedPaths;
for (clang::CXXBasePath &Path : Paths) {
if (DisplayedPaths.insert(Path.back().SubobjectNumber).second) {
PathDisplayStr += "\n ";
for (CXXBasePathElement &PE : llvm::reverse(Path))
PathDisplayStr += PE.Base->getType().getAsString() + " -> ";
PathDisplayStr += QualType(DestType).getAsString();
}
}
Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
<< QualType(SrcType).getUnqualifiedType()
<< QualType(DestType).getUnqualifiedType()
<< PathDisplayStr << OpRange;
msg = 0;
return TC_Failed;
}
if (Paths.getDetectedVirtual() != nullptr) {
QualType VirtualBase(Paths.getDetectedVirtual(), 0);
Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
<< OrigSrcType << OrigDestType << VirtualBase << OpRange;
msg = 0;
return TC_Failed;
}
if (!CStyle) {
switch (Self.CheckBaseClassAccess(OpRange.getBegin(),
SrcType, DestType,
Paths.front(),
diag::err_downcast_from_inaccessible_base)) {
case Sema::AR_accessible:
case Sema::AR_delayed: case Sema::AR_dependent: break;
case Sema::AR_inaccessible:
msg = 0;
return TC_Failed;
}
}
Self.BuildBasePathArray(Paths, BasePath);
Kind = CK_BaseToDerived;
return TC_Success;
}
TryCastResult
TryStaticMemberPointerUpcast(Sema &Self, ExprResult &SrcExpr, QualType SrcType,
QualType DestType, bool CStyle,
SourceRange OpRange,
unsigned &msg, CastKind &Kind,
CXXCastPath &BasePath) {
const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
if (!DestMemPtr)
return TC_NotApplicable;
bool WasOverloadedFunction = false;
DeclAccessPair FoundOverload;
if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
if (FunctionDecl *Fn
= Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(), DestType, false,
FoundOverload)) {
CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
SrcType = Self.Context.getMemberPointerType(Fn->getType(),
Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
WasOverloadedFunction = true;
}
}
const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
if (!SrcMemPtr) {
msg = diag::err_bad_static_cast_member_pointer_nonmp;
return TC_NotApplicable;
}
if (Self.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
(void)Self.isCompleteType(OpRange.getBegin(), SrcType);
(void)Self.isCompleteType(OpRange.getBegin(), DestType);
}
if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
DestMemPtr->getPointeeType()))
return TC_NotApplicable;
QualType SrcClass(SrcMemPtr->getClass(), 0);
QualType DestClass(DestMemPtr->getClass(), 0);
CXXBasePaths Paths(true, true,
true);
if (!Self.IsDerivedFrom(OpRange.getBegin(), SrcClass, DestClass, Paths))
return TC_NotApplicable;
if (Paths.isAmbiguous(Self.Context.getCanonicalType(DestClass))) {
Paths.clear();
Paths.setRecordingPaths(true);
bool StillOkay =
Self.IsDerivedFrom(OpRange.getBegin(), SrcClass, DestClass, Paths);
assert(StillOkay);
(void)StillOkay;
std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
<< 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
msg = 0;
return TC_Failed;
}
if (const RecordType *VBase = Paths.getDetectedVirtual()) {
Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
<< SrcClass << DestClass << QualType(VBase, 0) << OpRange;
msg = 0;
return TC_Failed;
}
if (!CStyle) {
switch (Self.CheckBaseClassAccess(OpRange.getBegin(),
DestClass, SrcClass,
Paths.front(),
diag::err_upcast_to_inaccessible_base)) {
case Sema::AR_accessible:
case Sema::AR_delayed:
case Sema::AR_dependent:
break;
case Sema::AR_inaccessible:
msg = 0;
return TC_Failed;
}
}
if (WasOverloadedFunction) {
FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(),
DestType,
true,
FoundOverload);
if (!Fn) {
msg = 0;
return TC_Failed;
}
SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
if (!SrcExpr.isUsable()) {
msg = 0;
return TC_Failed;
}
}
Self.BuildBasePathArray(Paths, BasePath);
Kind = CK_DerivedToBaseMemberPointer;
return TC_Success;
}
TryCastResult
TryStaticImplicitCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
Sema::CheckedConversionKind CCK,
SourceRange OpRange, unsigned &msg,
CastKind &Kind, bool ListInitialization) {
if (DestType->isRecordType()) {
if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
diag::err_bad_cast_incomplete) ||
Self.RequireNonAbstractType(OpRange.getBegin(), DestType,
diag::err_allocation_of_abstract_type)) {
msg = 0;
return TC_Failed;
}
}
InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
InitializationKind InitKind
= (CCK == Sema::CCK_CStyleCast)
? InitializationKind::CreateCStyleCast(OpRange.getBegin(), OpRange,
ListInitialization)
: (CCK == Sema::CCK_FunctionalCast)
? InitializationKind::CreateFunctionalCast(OpRange, ListInitialization)
: InitializationKind::CreateCast(OpRange);
Expr *SrcExprRaw = SrcExpr.get();
InitializationSequence InitSeq(Self, Entity, InitKind, SrcExprRaw);
bool CStyle
= (CCK == Sema::CCK_CStyleCast || CCK == Sema::CCK_FunctionalCast);
if (InitSeq.Failed() && (CStyle || !DestType->isReferenceType()))
return TC_NotApplicable;
ExprResult Result = InitSeq.Perform(Self, Entity, InitKind, SrcExprRaw);
if (Result.isInvalid()) {
msg = 0;
return TC_Failed;
}
if (InitSeq.isConstructorInitialization())
Kind = CK_ConstructorConversion;
else
Kind = CK_NoOp;
SrcExpr = Result;
return TC_Success;
}
static TryCastResult TryConstCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType, bool CStyle,
unsigned &msg) {
DestType = Self.Context.getCanonicalType(DestType);
QualType SrcType = SrcExpr.get()->getType();
bool NeedToMaterializeTemporary = false;
if (const ReferenceType *DestTypeTmp =DestType->getAs<ReferenceType>()) {
if (isa<LValueReferenceType>(DestTypeTmp) && !SrcExpr.get()->isLValue()) {
msg = diag::err_bad_cxx_cast_rvalue;
return TC_NotApplicable;
}
if (isa<RValueReferenceType>(DestTypeTmp) && SrcExpr.get()->isPRValue()) {
if (!SrcType->isRecordType()) {
msg = diag::err_bad_cxx_cast_rvalue;
return TC_NotApplicable;
}
NeedToMaterializeTemporary = true;
}
if (SrcExpr.get()->refersToBitField()) {
msg = diag::err_bad_cxx_cast_bitfield;
return TC_NotApplicable;
}
DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
SrcType = Self.Context.getPointerType(SrcType);
}
if (!DestType->isPointerType() &&
!DestType->isMemberPointerType() &&
!DestType->isObjCObjectPointerType()) {
if (!CStyle)
msg = diag::err_bad_const_cast_dest;
return TC_NotApplicable;
}
if (DestType->isFunctionPointerType() ||
DestType->isMemberFunctionPointerType()) {
if (!CStyle)
msg = diag::err_bad_const_cast_dest;
return TC_NotApplicable;
}
if (!Self.Context.hasCvrSimilarType(SrcType, DestType))
return TC_NotApplicable;
if (NeedToMaterializeTemporary)
SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcExpr.get()->getType(),
SrcExpr.get(),
false);
return TC_Success;
}
void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType,
bool IsDereference,
SourceRange Range) {
unsigned DiagID = IsDereference ?
diag::warn_pointer_indirection_from_incompatible_type :
diag::warn_undefined_reinterpret_cast;
if (Diags.isIgnored(DiagID, Range.getBegin()))
return;
QualType SrcTy, DestTy;
if (IsDereference) {
if (!SrcType->getAs<PointerType>() || !DestType->getAs<PointerType>()) {
return;
}
SrcTy = SrcType->getPointeeType();
DestTy = DestType->getPointeeType();
} else {
if (!DestType->getAs<ReferenceType>()) {
return;
}
SrcTy = SrcType;
DestTy = DestType->getPointeeType();
}
if (Context.hasSameUnqualifiedType(DestTy, SrcTy)) {
return;
}
if (DestTy->isAnyCharacterType() || DestTy->isVoidType() ||
SrcTy->isAnyCharacterType() || SrcTy->isVoidType()) {
return;
}
if (SrcTy->getAs<TagType>() || DestTy->getAs<TagType>()) {
return;
}
if ((SrcTy->isUnsignedIntegerType() && DestTy->isSignedIntegerType()) ||
(SrcTy->isSignedIntegerType() && DestTy->isUnsignedIntegerType())) {
if (Context.getTypeSize(DestTy) == Context.getTypeSize(SrcTy)) {
return;
}
}
Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range;
}
static void DiagnoseCastOfObjCSEL(Sema &Self, const ExprResult &SrcExpr,
QualType DestType) {
QualType SrcType = SrcExpr.get()->getType();
if (Self.Context.hasSameType(SrcType, DestType))
return;
if (const PointerType *SrcPtrTy = SrcType->getAs<PointerType>())
if (SrcPtrTy->isObjCSelType()) {
QualType DT = DestType;
if (isa<PointerType>(DestType))
DT = DestType->getPointeeType();
if (!DT.getUnqualifiedType()->isVoidType())
Self.Diag(SrcExpr.get()->getExprLoc(),
diag::warn_cast_pointer_from_sel)
<< SrcType << DestType << SrcExpr.get()->getSourceRange();
}
}
static void DiagnoseCallingConvCast(Sema &Self, const ExprResult &SrcExpr,
QualType DstType, SourceRange OpRange) {
QualType SrcType = SrcExpr.get()->getType();
if (Self.Context.hasSameType(SrcType, DstType) ||
!SrcType->isFunctionPointerType() || !DstType->isFunctionPointerType())
return;
const auto *SrcFTy =
SrcType->castAs<PointerType>()->getPointeeType()->castAs<FunctionType>();
const auto *DstFTy =
DstType->castAs<PointerType>()->getPointeeType()->castAs<FunctionType>();
CallingConv SrcCC = SrcFTy->getCallConv();
CallingConv DstCC = DstFTy->getCallConv();
if (SrcCC == DstCC)
return;
Expr *Src = SrcExpr.get()->IgnoreParenImpCasts();
if (auto *UO = dyn_cast<UnaryOperator>(Src))
if (UO->getOpcode() == UO_AddrOf)
Src = UO->getSubExpr()->IgnoreParenImpCasts();
auto *DRE = dyn_cast<DeclRefExpr>(Src);
if (!DRE)
return;
auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl());
if (!FD)
return;
CallingConv DefaultCC = Self.getASTContext().getDefaultCallingConvention(
FD->isVariadic(), FD->isCXXInstanceMember());
if (DstCC == DefaultCC || SrcCC != DefaultCC)
return;
StringRef SrcCCName = FunctionType::getNameForCallConv(SrcCC);
StringRef DstCCName = FunctionType::getNameForCallConv(DstCC);
Self.Diag(OpRange.getBegin(), diag::warn_cast_calling_conv)
<< SrcCCName << DstCCName << OpRange;
if (Self.Diags.isIgnored(diag::warn_cast_calling_conv, OpRange.getBegin()))
return;
SourceLocation NameLoc = FD->getFirstDecl()->getNameInfo().getLoc();
Preprocessor &PP = Self.getPreprocessor();
SmallVector<TokenValue, 6> AttrTokens;
SmallString<64> CCAttrText;
llvm::raw_svector_ostream OS(CCAttrText);
if (Self.getLangOpts().MicrosoftExt) {
OS << "__" << DstCCName;
IdentifierInfo *II = PP.getIdentifierInfo(OS.str());
AttrTokens.push_back(II->isKeyword(Self.getLangOpts())
? TokenValue(II->getTokenID())
: TokenValue(II));
} else {
OS << "__attribute__((" << DstCCName << "))";
AttrTokens.push_back(tok::kw___attribute);
AttrTokens.push_back(tok::l_paren);
AttrTokens.push_back(tok::l_paren);
IdentifierInfo *II = PP.getIdentifierInfo(DstCCName);
AttrTokens.push_back(II->isKeyword(Self.getLangOpts())
? TokenValue(II->getTokenID())
: TokenValue(II));
AttrTokens.push_back(tok::r_paren);
AttrTokens.push_back(tok::r_paren);
}
StringRef AttrSpelling = PP.getLastMacroWithSpelling(NameLoc, AttrTokens);
if (!AttrSpelling.empty())
CCAttrText = AttrSpelling;
OS << ' ';
Self.Diag(NameLoc, diag::note_change_calling_conv_fixit)
<< FD << DstCCName << FixItHint::CreateInsertion(NameLoc, CCAttrText);
}
static void checkIntToPointerCast(bool CStyle, const SourceRange &OpRange,
const Expr *SrcExpr, QualType DestType,
Sema &Self) {
QualType SrcType = SrcExpr->getType();
if (CStyle && SrcType->isIntegralType(Self.Context)
&& !SrcType->isBooleanType()
&& !SrcType->isEnumeralType()
&& !SrcExpr->isIntegerConstantExpr(Self.Context)
&& Self.Context.getTypeSize(DestType) >
Self.Context.getTypeSize(SrcType)) {
unsigned Diag = DestType->isVoidPointerType() ?
diag::warn_int_to_void_pointer_cast
: diag::warn_int_to_pointer_cast;
Self.Diag(OpRange.getBegin(), Diag) << SrcType << DestType << OpRange;
}
}
static bool fixOverloadedReinterpretCastExpr(Sema &Self, QualType DestType,
ExprResult &Result) {
Expr *E = Result.get();
if (Self.ResolveAndFixSingleFunctionTemplateSpecialization(
Result,
Expr::getValueKindForType(DestType) ==
VK_PRValue ) &&
Result.isUsable())
return true;
Result = E;
if (!Self.resolveAndFixAddressOfSingleOverloadCandidate(
Result, true))
return false;
return Result.isUsable();
}
static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType, bool CStyle,
SourceRange OpRange,
unsigned &msg,
CastKind &Kind) {
bool IsLValueCast = false;
DestType = Self.Context.getCanonicalType(DestType);
QualType SrcType = SrcExpr.get()->getType();
if (SrcType == Self.Context.OverloadTy) {
ExprResult FixedExpr = SrcExpr;
if (!fixOverloadedReinterpretCastExpr(Self, DestType, FixedExpr))
return TC_NotApplicable;
assert(FixedExpr.isUsable() && "Invalid result fixing overloaded expr");
SrcExpr = FixedExpr;
SrcType = SrcExpr.get()->getType();
}
if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
if (!SrcExpr.get()->isGLValue()) {
msg = diag::err_bad_cxx_cast_rvalue;
return TC_NotApplicable;
}
if (!CStyle) {
Self.CheckCompatibleReinterpretCast(SrcType, DestType,
false, OpRange);
}
const char *inappropriate = nullptr;
switch (SrcExpr.get()->getObjectKind()) {
case OK_Ordinary:
break;
case OK_BitField:
msg = diag::err_bad_cxx_cast_bitfield;
return TC_NotApplicable;
case OK_VectorComponent: inappropriate = "vector element"; break;
case OK_MatrixComponent:
inappropriate = "matrix element";
break;
case OK_ObjCProperty: inappropriate = "property expression"; break;
case OK_ObjCSubscript: inappropriate = "container subscripting expression";
break;
}
if (inappropriate) {
Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_reference)
<< inappropriate << DestType
<< OpRange << SrcExpr.get()->getSourceRange();
msg = 0; SrcExpr = ExprError();
return TC_NotApplicable;
}
DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
SrcType = Self.Context.getPointerType(SrcType);
IsLValueCast = true;
}
SrcType = Self.Context.getCanonicalType(SrcType);
const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
*SrcMemPtr = SrcType->getAs<MemberPointerType>();
if (DestMemPtr && SrcMemPtr) {
if (DestMemPtr->isMemberFunctionPointer() !=
SrcMemPtr->isMemberFunctionPointer())
return TC_NotApplicable;
if (Self.Context.getTargetInfo().getCXXABI().isMicrosoft()) {
(void)Self.isCompleteType(OpRange.getBegin(), SrcType);
(void)Self.isCompleteType(OpRange.getBegin(), DestType);
}
if (Self.Context.getTypeSize(DestMemPtr) !=
Self.Context.getTypeSize(SrcMemPtr)) {
msg = diag::err_bad_cxx_cast_member_pointer_size;
return TC_Failed;
}
if (auto CACK =
CastsAwayConstness(Self, SrcType, DestType, !CStyle,
CStyle))
return getCastAwayConstnessCastKind(CACK, msg);
assert(!IsLValueCast);
Kind = CK_ReinterpretMemberPointer;
return TC_Success;
}
if (SrcType->isNullPtrType() && DestType->isIntegralType(Self.Context)) {
if (Self.Context.getTypeSize(SrcType) >
Self.Context.getTypeSize(DestType)) {
msg = diag::err_bad_reinterpret_cast_small_int;
return TC_Failed;
}
Kind = CK_PointerToIntegral;
return TC_Success;
}
bool destIsVector = DestType->isVectorType();
bool srcIsVector = SrcType->isVectorType();
if (srcIsVector || destIsVector) {
if (Self.isValidSveBitcast(SrcType, DestType)) {
Kind = CK_BitCast;
return TC_Success;
}
if ((!destIsVector && !DestType->isIntegralType(Self.Context)) ||
(!srcIsVector && !SrcType->isIntegralType(Self.Context)))
return TC_NotApplicable;
if (Self.areLaxCompatibleVectorTypes(SrcType, DestType)) {
Kind = CK_BitCast;
return TC_Success;
}
if (Self.LangOpts.OpenCL && !CStyle) {
if (DestType->isExtVectorType() || SrcType->isExtVectorType()) {
if (Self.areVectorTypesSameSize(SrcType, DestType)) {
Kind = CK_BitCast;
return TC_Success;
}
}
}
if (!destIsVector)
msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
else if (!srcIsVector)
msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
else
msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
return TC_Failed;
}
if (SrcType == DestType) {
Kind = CK_NoOp;
TryCastResult Result = TC_NotApplicable;
if (SrcType->isIntegralOrEnumerationType() ||
SrcType->isAnyPointerType() ||
SrcType->isMemberPointerType() ||
SrcType->isBlockPointerType()) {
Result = TC_Success;
}
return Result;
}
bool destIsPtr = DestType->isAnyPointerType() ||
DestType->isBlockPointerType();
bool srcIsPtr = SrcType->isAnyPointerType() ||
SrcType->isBlockPointerType();
if (!destIsPtr && !srcIsPtr) {
return TC_NotApplicable;
}
if (DestType->isIntegralType(Self.Context)) {
assert(srcIsPtr && "One type must be a pointer");
if ((Self.Context.getTypeSize(SrcType) >
Self.Context.getTypeSize(DestType))) {
bool MicrosoftException =
Self.getLangOpts().MicrosoftExt && !DestType->isBooleanType();
if (MicrosoftException) {
unsigned Diag = SrcType->isVoidPointerType()
? diag::warn_void_pointer_to_int_cast
: diag::warn_pointer_to_int_cast;
Self.Diag(OpRange.getBegin(), Diag) << SrcType << DestType << OpRange;
} else {
msg = diag::err_bad_reinterpret_cast_small_int;
return TC_Failed;
}
}
Kind = CK_PointerToIntegral;
return TC_Success;
}
if (SrcType->isIntegralOrEnumerationType()) {
assert(destIsPtr && "One type must be a pointer");
checkIntToPointerCast(CStyle, OpRange, SrcExpr.get(), DestType, Self);
Kind = CK_IntegralToPointer;
return TC_Success;
}
if (!destIsPtr || !srcIsPtr) {
return TC_NotApplicable;
}
if ((SrcType->isBlockPointerType() && DestType->isObjCObjectPointerType()) ||
(DestType->isBlockPointerType() && SrcType->isObjCObjectPointerType()))
return TC_NotApplicable;
TryCastResult SuccessResult = TC_Success;
if (auto CACK =
CastsAwayConstness(Self, SrcType, DestType, !CStyle,
CStyle))
SuccessResult = getCastAwayConstnessCastKind(CACK, msg);
if (IsAddressSpaceConversion(SrcType, DestType)) {
Kind = CK_AddressSpaceConversion;
assert(SrcType->isPointerType() && DestType->isPointerType());
if (!CStyle &&
!DestType->getPointeeType().getQualifiers().isAddressSpaceSupersetOf(
SrcType->getPointeeType().getQualifiers())) {
SuccessResult = TC_Failed;
}
} else if (IsLValueCast) {
Kind = CK_LValueBitCast;
} else if (DestType->isObjCObjectPointerType()) {
Kind = Self.PrepareCastToObjCObjectPointer(SrcExpr);
} else if (DestType->isBlockPointerType()) {
if (!SrcType->isBlockPointerType()) {
Kind = CK_AnyPointerToBlockPointerCast;
} else {
Kind = CK_BitCast;
}
} else {
Kind = CK_BitCast;
}
if (CStyle && DestType->isObjCObjectPointerType()) {
return SuccessResult;
}
if (CStyle)
DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType);
DiagnoseCallingConvCast(Self, SrcExpr, DestType, OpRange);
if (SrcType->isFunctionPointerType()) {
if (DestType->isFunctionPointerType()) {
return SuccessResult;
}
Self.Diag(OpRange.getBegin(),
Self.getLangOpts().CPlusPlus11 ?
diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
<< OpRange;
return SuccessResult;
}
if (DestType->isFunctionPointerType()) {
Self.Diag(OpRange.getBegin(),
Self.getLangOpts().CPlusPlus11 ?
diag::warn_cxx98_compat_cast_fn_obj : diag::ext_cast_fn_obj)
<< OpRange;
return SuccessResult;
}
QualType DestPtee = DestType->getPointeeType().isNull()
? DestType->getPointeeType()
: DestType->getPointeeType()->getPointeeType();
QualType SrcPtee = SrcType->getPointeeType().isNull()
? SrcType->getPointeeType()
: SrcType->getPointeeType()->getPointeeType();
while (!DestPtee.isNull() && !SrcPtee.isNull()) {
if (DestPtee.getAddressSpace() != SrcPtee.getAddressSpace()) {
Self.Diag(OpRange.getBegin(),
diag::warn_bad_cxx_cast_nested_pointer_addr_space)
<< CStyle << SrcType << DestType << SrcExpr.get()->getSourceRange();
break;
}
DestPtee = DestPtee->getPointeeType();
SrcPtee = SrcPtee->getPointeeType();
}
return SuccessResult;
}
static TryCastResult TryAddressSpaceCast(Sema &Self, ExprResult &SrcExpr,
QualType DestType, bool CStyle,
unsigned &msg, CastKind &Kind) {
if (!Self.getLangOpts().OpenCL && !Self.getLangOpts().SYCLIsDevice)
return TC_NotApplicable;
auto SrcType = SrcExpr.get()->getType();
auto SrcPtrType = SrcType->getAs<PointerType>();
if (!SrcPtrType)
return TC_NotApplicable;
auto DestPtrType = DestType->getAs<PointerType>();
if (!DestPtrType)
return TC_NotApplicable;
auto SrcPointeeType = SrcPtrType->getPointeeType();
auto DestPointeeType = DestPtrType->getPointeeType();
if (!DestPointeeType.isAddressSpaceOverlapping(SrcPointeeType)) {
msg = diag::err_bad_cxx_cast_addr_space_mismatch;
return TC_Failed;
}
auto SrcPointeeTypeWithoutAS =
Self.Context.removeAddrSpaceQualType(SrcPointeeType.getCanonicalType());
auto DestPointeeTypeWithoutAS =
Self.Context.removeAddrSpaceQualType(DestPointeeType.getCanonicalType());
if (Self.Context.hasSameType(SrcPointeeTypeWithoutAS,
DestPointeeTypeWithoutAS)) {
Kind = SrcPointeeType.getAddressSpace() == DestPointeeType.getAddressSpace()
? CK_NoOp
: CK_AddressSpaceConversion;
return TC_Success;
} else {
return TC_NotApplicable;
}
}
void CastOperation::checkAddressSpaceCast(QualType SrcType, QualType DestType) {
if (Self.getLangOpts().OpenCL) {
const Type *DestPtr, *SrcPtr;
bool Nested = false;
unsigned DiagID = diag::err_typecheck_incompatible_address_space;
DestPtr = Self.getASTContext().getCanonicalType(DestType.getTypePtr()),
SrcPtr = Self.getASTContext().getCanonicalType(SrcType.getTypePtr());
while (isa<PointerType>(DestPtr) && isa<PointerType>(SrcPtr)) {
const PointerType *DestPPtr = cast<PointerType>(DestPtr);
const PointerType *SrcPPtr = cast<PointerType>(SrcPtr);
QualType DestPPointee = DestPPtr->getPointeeType();
QualType SrcPPointee = SrcPPtr->getPointeeType();
if (Nested
? DestPPointee.getAddressSpace() != SrcPPointee.getAddressSpace()
: !DestPPointee.isAddressSpaceOverlapping(SrcPPointee)) {
Self.Diag(OpRange.getBegin(), DiagID)
<< SrcType << DestType << Sema::AA_Casting
<< SrcExpr.get()->getSourceRange();
if (!Nested)
SrcExpr = ExprError();
return;
}
DestPtr = DestPPtr->getPointeeType().getTypePtr();
SrcPtr = SrcPPtr->getPointeeType().getTypePtr();
Nested = true;
DiagID = diag::ext_nested_pointer_qualifier_mismatch;
}
}
}
bool Sema::ShouldSplatAltivecScalarInCast(const VectorType *VecTy) {
bool SrcCompatXL = this->getLangOpts().getAltivecSrcCompat() ==
LangOptions::AltivecSrcCompatKind::XL;
VectorType::VectorKind VKind = VecTy->getVectorKind();
if ((VKind == VectorType::AltiVecVector) ||
(SrcCompatXL && ((VKind == VectorType::AltiVecBool) ||
(VKind == VectorType::AltiVecPixel)))) {
return true;
}
return false;
}
bool Sema::CheckAltivecInitFromScalar(SourceRange R, QualType VecTy,
QualType SrcTy) {
bool SrcCompatGCC = this->getLangOpts().getAltivecSrcCompat() ==
LangOptions::AltivecSrcCompatKind::GCC;
if (this->getLangOpts().AltiVec && SrcCompatGCC) {
this->Diag(R.getBegin(),
diag::err_invalid_conversion_between_vector_and_integer)
<< VecTy << SrcTy << R;
return true;
}
return false;
}
void CastOperation::CheckCXXCStyleCast(bool FunctionalStyle,
bool ListInitialization) {
assert(Self.getLangOpts().CPlusPlus);
if (isPlaceholder()) {
if (claimPlaceholder(BuiltinType::UnknownAny)) {
SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
SrcExpr.get(), Kind,
ValueKind, BasePath);
return;
}
checkNonOverloadPlaceholders();
if (SrcExpr.isInvalid())
return;
}
if (DestType->isVoidType()) {
Kind = CK_ToVoid;
if (claimPlaceholder(BuiltinType::Overload)) {
Self.ResolveAndFixSingleFunctionTemplateSpecialization(
SrcExpr, false,
true, DestRange, DestType,
diag::err_bad_cstyle_cast_overload);
if (SrcExpr.isInvalid())
return;
}
SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
return;
}
if (DestType->isDependentType() || SrcExpr.get()->isTypeDependent() ||
SrcExpr.get()->isValueDependent()) {
assert(Kind == CK_Dependent);
return;
}
if (ValueKind == VK_PRValue && !DestType->isRecordType() &&
!isPlaceholder(BuiltinType::Overload)) {
SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
if (SrcExpr.isInvalid())
return;
}
if (const VectorType *vecTy = DestType->getAs<VectorType>()) {
if (Self.CheckAltivecInitFromScalar(OpRange, DestType,
SrcExpr.get()->getType())) {
SrcExpr = ExprError();
return;
}
if (Self.ShouldSplatAltivecScalarInCast(vecTy) &&
(SrcExpr.get()->getType()->isIntegerType() ||
SrcExpr.get()->getType()->isFloatingType())) {
Kind = CK_VectorSplat;
SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
return;
}
}
unsigned msg = diag::err_bad_cxx_cast_generic;
TryCastResult tcr = TryConstCast(Self, SrcExpr, DestType,
true, msg);
if (SrcExpr.isInvalid())
return;
if (isValidCast(tcr))
Kind = CK_NoOp;
Sema::CheckedConversionKind CCK =
FunctionalStyle ? Sema::CCK_FunctionalCast : Sema::CCK_CStyleCast;
if (tcr == TC_NotApplicable) {
tcr = TryAddressSpaceCast(Self, SrcExpr, DestType, true, msg,
Kind);
if (SrcExpr.isInvalid())
return;
if (tcr == TC_NotApplicable) {
tcr = TryStaticCast(Self, SrcExpr, DestType, CCK, OpRange, msg, Kind,
BasePath, ListInitialization);
if (SrcExpr.isInvalid())
return;
if (tcr == TC_NotApplicable) {
tcr = TryReinterpretCast(Self, SrcExpr, DestType, true,
OpRange, msg, Kind);
if (SrcExpr.isInvalid())
return;
}
}
}
if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers() &&
isValidCast(tcr))
checkObjCConversion(CCK);
if (tcr != TC_Success && msg != 0) {
if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
DeclAccessPair Found;
FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr.get(),
DestType,
true,
Found);
if (Fn) {
OverloadExpr *OE = OverloadExpr::find(SrcExpr.get()).Expression;
Self.Diag(OpRange.getBegin(), diag::err_bad_cstyle_cast_overload)
<< OE->getName() << DestType << OpRange
<< OE->getQualifierLoc().getSourceRange();
Self.NoteAllOverloadCandidates(SrcExpr.get());
}
} else {
diagnoseBadCast(Self, msg, (FunctionalStyle ? CT_Functional : CT_CStyle),
OpRange, SrcExpr.get(), DestType, ListInitialization);
}
}
if (isValidCast(tcr)) {
if (Kind == CK_BitCast)
checkCastAlign();
if (!checkCastFunctionType(Self, SrcExpr, DestType))
Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type)
<< SrcExpr.get()->getType() << DestType << OpRange;
} else {
SrcExpr = ExprError();
}
}
static void DiagnoseBadFunctionCast(Sema &Self, const ExprResult &SrcExpr,
QualType DestType) {
if (Self.Diags.isIgnored(diag::warn_bad_function_cast,
SrcExpr.get()->getExprLoc()))
return;
if (!isa<CallExpr>(SrcExpr.get()))
return;
QualType SrcType = SrcExpr.get()->getType();
if (DestType.getUnqualifiedType()->isVoidType())
return;
if ((SrcType->isAnyPointerType() || SrcType->isBlockPointerType())
&& (DestType->isAnyPointerType() || DestType->isBlockPointerType()))
return;
if (SrcType->isIntegerType() && DestType->isIntegerType() &&
(SrcType->isBooleanType() == DestType->isBooleanType()) &&
(SrcType->isEnumeralType() == DestType->isEnumeralType()))
return;
if (SrcType->isRealFloatingType() && DestType->isRealFloatingType())
return;
if (SrcType->isEnumeralType() && DestType->isEnumeralType())
return;
if (SrcType->isComplexType() && DestType->isComplexType())
return;
if (SrcType->isComplexIntegerType() && DestType->isComplexIntegerType())
return;
if (SrcType->isFixedPointType() && DestType->isFixedPointType())
return;
Self.Diag(SrcExpr.get()->getExprLoc(),
diag::warn_bad_function_cast)
<< SrcType << DestType << SrcExpr.get()->getSourceRange();
}
void CastOperation::CheckCStyleCast() {
assert(!Self.getLangOpts().CPlusPlus);
if (claimPlaceholder(BuiltinType::UnknownAny)) {
SrcExpr = Self.checkUnknownAnyCast(DestRange, DestType,
SrcExpr.get(), Kind,
ValueKind, BasePath);
return;
}
if (DestType->isVoidType()) {
SrcExpr = Self.IgnoredValueConversions(SrcExpr.get());
if (SrcExpr.isInvalid())
return;
Kind = CK_ToVoid;
return;
}
if (Self.getASTContext().isDependenceAllowed() &&
(DestType->isDependentType() || SrcExpr.get()->isTypeDependent() ||
SrcExpr.get()->isValueDependent())) {
assert((DestType->containsErrors() || SrcExpr.get()->containsErrors() ||
SrcExpr.get()->containsErrors()) &&
"should only occur in error-recovery path.");
assert(Kind == CK_Dependent);
return;
}
if (SrcExpr.get()->getType() == Self.Context.OverloadTy) {
DeclAccessPair DAP;
if (FunctionDecl *FD = Self.ResolveAddressOfOverloadedFunction(
SrcExpr.get(), DestType, true, DAP))
SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr.get(), DAP, FD);
else
return;
assert(SrcExpr.isUsable());
}
SrcExpr = Self.DefaultFunctionArrayLvalueConversion(SrcExpr.get());
if (SrcExpr.isInvalid())
return;
QualType SrcType = SrcExpr.get()->getType();
assert(!SrcType->isPlaceholderType());
checkAddressSpaceCast(SrcType, DestType);
if (SrcExpr.isInvalid())
return;
if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
diag::err_typecheck_cast_to_incomplete)) {
SrcExpr = ExprError();
return;
}
if (DestType->isSizelessBuiltinType() &&
Self.Context.hasSameUnqualifiedType(DestType, SrcType)) {
Kind = CK_NoOp;
return;
}
if ((SrcType->isVectorType() || DestType->isVectorType()) &&
Self.isValidSveBitcast(SrcType, DestType)) {
Kind = CK_BitCast;
return;
}
if (!DestType->isScalarType() && !DestType->isVectorType() &&
!DestType->isMatrixType()) {
const RecordType *DestRecordTy = DestType->getAs<RecordType>();
if (DestRecordTy && Self.Context.hasSameUnqualifiedType(DestType, SrcType)){
Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_nonscalar)
<< DestType << SrcExpr.get()->getSourceRange();
Kind = CK_NoOp;
return;
}
if (DestRecordTy && DestRecordTy->getDecl()->isUnion()) {
RecordDecl *RD = DestRecordTy->getDecl();
if (CastExpr::getTargetFieldForToUnionCast(RD, SrcType)) {
Self.Diag(OpRange.getBegin(), diag::ext_typecheck_cast_to_union)
<< SrcExpr.get()->getSourceRange();
Kind = CK_ToUnion;
return;
} else {
Self.Diag(OpRange.getBegin(), diag::err_typecheck_cast_to_union_no_type)
<< SrcType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
}
if (Self.getLangOpts().OpenCL && DestType->isEventT()) {
Expr::EvalResult Result;
if (SrcExpr.get()->EvaluateAsInt(Result, Self.Context)) {
llvm::APSInt CastInt = Result.Val.getInt();
if (0 == CastInt) {
Kind = CK_ZeroToOCLOpaqueType;
return;
}
Self.Diag(OpRange.getBegin(),
diag::err_opencl_cast_non_zero_to_event_t)
<< toString(CastInt, 10) << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
}
Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar)
<< DestType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
if (!SrcType->isScalarType() && !SrcType->isVectorType() &&
!SrcType->isMatrixType()) {
Self.Diag(SrcExpr.get()->getExprLoc(),
diag::err_typecheck_expect_scalar_operand)
<< SrcType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
if (DestType->isExtVectorType()) {
SrcExpr = Self.CheckExtVectorCast(OpRange, DestType, SrcExpr.get(), Kind);
return;
}
if (DestType->getAs<MatrixType>() || SrcType->getAs<MatrixType>()) {
if (Self.CheckMatrixCast(OpRange, DestType, SrcType, Kind))
SrcExpr = ExprError();
return;
}
if (const VectorType *DestVecTy = DestType->getAs<VectorType>()) {
if (Self.CheckAltivecInitFromScalar(OpRange, DestType, SrcType)) {
SrcExpr = ExprError();
return;
}
if (Self.ShouldSplatAltivecScalarInCast(DestVecTy) &&
(SrcType->isIntegerType() || SrcType->isFloatingType())) {
Kind = CK_VectorSplat;
SrcExpr = Self.prepareVectorSplat(DestType, SrcExpr.get());
} else if (Self.CheckVectorCast(OpRange, DestType, SrcType, Kind)) {
SrcExpr = ExprError();
}
return;
}
if (SrcType->isVectorType()) {
if (Self.CheckVectorCast(OpRange, SrcType, DestType, Kind))
SrcExpr = ExprError();
return;
}
if (isa<ObjCSelectorExpr>(SrcExpr.get())) {
Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_selector_expr);
SrcExpr = ExprError();
return;
}
if (DestType->isBFloat16Type() && !SrcType->isBFloat16Type()) {
Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_to_bfloat16)
<< SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
if (SrcType->isBFloat16Type() && !DestType->isBFloat16Type()) {
Self.Diag(SrcExpr.get()->getExprLoc(), diag::err_cast_from_bfloat16)
<< SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
if (!DestType->isArithmeticType()) {
if (!SrcType->isIntegralType(Self.Context) && SrcType->isArithmeticType()) {
Self.Diag(SrcExpr.get()->getExprLoc(),
diag::err_cast_pointer_from_non_pointer_int)
<< SrcType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
checkIntToPointerCast( true, OpRange, SrcExpr.get(), DestType,
Self);
} else if (!SrcType->isArithmeticType()) {
if (!DestType->isIntegralType(Self.Context) &&
DestType->isArithmeticType()) {
Self.Diag(SrcExpr.get()->getBeginLoc(),
diag::err_cast_pointer_to_non_pointer_int)
<< DestType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
if ((Self.Context.getTypeSize(SrcType) >
Self.Context.getTypeSize(DestType)) &&
!DestType->isBooleanType()) {
unsigned Diag;
if (SrcType->isVoidPointerType())
Diag = DestType->isEnumeralType() ? diag::warn_void_pointer_to_enum_cast
: diag::warn_void_pointer_to_int_cast;
else if (DestType->isEnumeralType())
Diag = diag::warn_pointer_to_enum_cast;
else
Diag = diag::warn_pointer_to_int_cast;
Self.Diag(OpRange.getBegin(), Diag) << SrcType << DestType << OpRange;
}
}
if (Self.getLangOpts().OpenCL && !Self.getOpenCLOptions().isAvailableOption(
"cl_khr_fp16", Self.getLangOpts())) {
if (DestType->isHalfType()) {
Self.Diag(SrcExpr.get()->getBeginLoc(), diag::err_opencl_cast_to_half)
<< DestType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
}
if (Self.getLangOpts().allowsNonTrivialObjCLifetimeQualifiers()) {
checkObjCConversion(Sema::CCK_CStyleCast);
if (SrcExpr.isInvalid())
return;
const PointerType *CastPtr = DestType->getAs<PointerType>();
if (Self.getLangOpts().ObjCAutoRefCount && CastPtr) {
if (const PointerType *ExprPtr = SrcType->getAs<PointerType>()) {
Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers();
Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers();
if (CastPtr->getPointeeType()->isObjCLifetimeType() &&
ExprPtr->getPointeeType()->isObjCLifetimeType() &&
!CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) {
Self.Diag(SrcExpr.get()->getBeginLoc(),
diag::err_typecheck_incompatible_ownership)
<< SrcType << DestType << Sema::AA_Casting
<< SrcExpr.get()->getSourceRange();
return;
}
}
}
else if (!Self.CheckObjCARCUnavailableWeakConversion(DestType, SrcType)) {
Self.Diag(SrcExpr.get()->getBeginLoc(),
diag::err_arc_convesion_of_weak_unavailable)
<< 1 << SrcType << DestType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
}
if (!checkCastFunctionType(Self, SrcExpr, DestType))
Self.Diag(OpRange.getBegin(), diag::warn_cast_function_type)
<< SrcType << DestType << OpRange;
if (isa<PointerType>(SrcType) && isa<PointerType>(DestType)) {
QualType SrcTy = cast<PointerType>(SrcType)->getPointeeType();
QualType DestTy = cast<PointerType>(DestType)->getPointeeType();
const RecordDecl *SrcRD = SrcTy->getAsRecordDecl();
const RecordDecl *DestRD = DestTy->getAsRecordDecl();
if (SrcRD && DestRD && SrcRD->hasAttr<RandomizeLayoutAttr>() &&
SrcRD != DestRD) {
Self.Diag(OpRange.getBegin(), diag::err_cast_from_randomized_struct)
<< SrcType << DestType;
SrcExpr = ExprError();
return;
}
}
DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType);
DiagnoseCallingConvCast(Self, SrcExpr, DestType, OpRange);
DiagnoseBadFunctionCast(Self, SrcExpr, DestType);
Kind = Self.PrepareScalarCast(SrcExpr, DestType);
if (SrcExpr.isInvalid())
return;
if (Kind == CK_BitCast)
checkCastAlign();
}
void CastOperation::CheckBuiltinBitCast() {
QualType SrcType = SrcExpr.get()->getType();
if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
diag::err_typecheck_cast_to_incomplete) ||
Self.RequireCompleteType(OpRange.getBegin(), SrcType,
diag::err_incomplete_type)) {
SrcExpr = ExprError();
return;
}
if (SrcExpr.get()->isPRValue())
SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcType, SrcExpr.get(),
false);
CharUnits DestSize = Self.Context.getTypeSizeInChars(DestType);
CharUnits SourceSize = Self.Context.getTypeSizeInChars(SrcType);
if (DestSize != SourceSize) {
Self.Diag(OpRange.getBegin(), diag::err_bit_cast_type_size_mismatch)
<< (int)SourceSize.getQuantity() << (int)DestSize.getQuantity();
SrcExpr = ExprError();
return;
}
if (!DestType.isTriviallyCopyableType(Self.Context)) {
Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable)
<< 1;
SrcExpr = ExprError();
return;
}
if (!SrcType.isTriviallyCopyableType(Self.Context)) {
Self.Diag(OpRange.getBegin(), diag::err_bit_cast_non_trivially_copyable)
<< 0;
SrcExpr = ExprError();
return;
}
Kind = CK_LValueToRValueBitCast;
}
static void DiagnoseCastQual(Sema &Self, const ExprResult &SrcExpr,
QualType DestType) {
if (SrcExpr.isInvalid())
return;
QualType SrcType = SrcExpr.get()->getType();
if (!((SrcType->isAnyPointerType() && DestType->isAnyPointerType()) ||
DestType->isLValueReferenceType()))
return;
QualType TheOffendingSrcType, TheOffendingDestType;
Qualifiers CastAwayQualifiers;
if (CastsAwayConstness(Self, SrcType, DestType, true, false,
&TheOffendingSrcType, &TheOffendingDestType,
&CastAwayQualifiers) !=
CastAwayConstnessKind::CACK_Similar)
return;
int qualifiers = -1;
if (CastAwayQualifiers.hasConst() && CastAwayQualifiers.hasVolatile()) {
qualifiers = 0;
} else if (CastAwayQualifiers.hasConst()) {
qualifiers = 1;
} else if (CastAwayQualifiers.hasVolatile()) {
qualifiers = 2;
}
if (qualifiers == -1)
Self.Diag(SrcExpr.get()->getBeginLoc(), diag::warn_cast_qual2)
<< SrcType << DestType;
else
Self.Diag(SrcExpr.get()->getBeginLoc(), diag::warn_cast_qual)
<< TheOffendingSrcType << TheOffendingDestType << qualifiers;
}
ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc,
TypeSourceInfo *CastTypeInfo,
SourceLocation RPLoc,
Expr *CastExpr) {
CastOperation Op(*this, CastTypeInfo->getType(), CastExpr);
Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange();
Op.OpRange = SourceRange(LPLoc, CastExpr->getEndLoc());
if (getLangOpts().CPlusPlus) {
Op.CheckCXXCStyleCast( false,
isa<InitListExpr>(CastExpr));
} else {
Op.CheckCStyleCast();
}
if (Op.SrcExpr.isInvalid())
return ExprError();
DiagnoseCastQual(Op.Self, Op.SrcExpr, Op.DestType);
return Op.complete(CStyleCastExpr::Create(
Context, Op.ResultType, Op.ValueKind, Op.Kind, Op.SrcExpr.get(),
&Op.BasePath, CurFPFeatureOverrides(), CastTypeInfo, LPLoc, RPLoc));
}
ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo,
QualType Type,
SourceLocation LPLoc,
Expr *CastExpr,
SourceLocation RPLoc) {
assert(LPLoc.isValid() && "List-initialization shouldn't get here.");
CastOperation Op(*this, Type, CastExpr);
Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange();
Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getEndLoc());
Op.CheckCXXCStyleCast(true, false);
if (Op.SrcExpr.isInvalid())
return ExprError();
auto *SubExpr = Op.SrcExpr.get();
if (auto *BindExpr = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
SubExpr = BindExpr->getSubExpr();
if (auto *ConstructExpr = dyn_cast<CXXConstructExpr>(SubExpr))
ConstructExpr->setParenOrBraceRange(SourceRange(LPLoc, RPLoc));
return Op.complete(CXXFunctionalCastExpr::Create(
Context, Op.ResultType, Op.ValueKind, CastTypeInfo, Op.Kind,
Op.SrcExpr.get(), &Op.BasePath, CurFPFeatureOverrides(), LPLoc, RPLoc));
}