diff options
Diffstat (limited to 'lib/Sema/SemaCXXCast.cpp')
-rw-r--r-- | lib/Sema/SemaCXXCast.cpp | 67 |
1 files changed, 27 insertions, 40 deletions
diff --git a/lib/Sema/SemaCXXCast.cpp b/lib/Sema/SemaCXXCast.cpp index ceb2de7ae8..ac85b79b39 100644 --- a/lib/Sema/SemaCXXCast.cpp +++ b/lib/Sema/SemaCXXCast.cpp @@ -47,7 +47,7 @@ static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, CastExpr::CastKind &Kind); static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType, const SourceRange &OpRange, - const SourceRange &DestRange, + const SourceRange &DestRange, CastExpr::CastKind &Kind); static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType); @@ -89,7 +89,7 @@ static TryCastResult TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, CXXMethodDecl *&ConversionDecl); static TryCastResult TryStaticCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, - CastExpr::CastKind &Kind, + CastExpr::CastKind &Kind, const SourceRange &OpRange, unsigned &msg, CXXMethodDecl *&ConversionDecl); @@ -97,7 +97,7 @@ static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, unsigned &msg); static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, - CastExpr::CastKind &Kind, + CastExpr::CastKind &Kind, const SourceRange &OpRange, unsigned &msg); @@ -158,8 +158,7 @@ Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, /// the cast checkers. Both arguments must denote pointer (possibly to member) /// types. bool -CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) -{ +CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) { // Casting away constness is defined in C++ 5.2.11p8 with reference to // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since // the rules are non-trivial. So first we construct Tcv *...cv* as described @@ -185,8 +184,7 @@ CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) QualType DestConstruct = Self.Context.VoidTy; for (llvm::SmallVector<unsigned, 8>::reverse_iterator i1 = cv1.rbegin(), i2 = cv2.rbegin(); - i1 != cv1.rend(); ++i1, ++i2) - { + i1 != cv1.rend(); ++i1, ++i2) { SrcConstruct = Self.Context.getPointerType( SrcConstruct.getQualifiedType(*i1)); DestConstruct = Self.Context.getPointerType( @@ -204,8 +202,7 @@ CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType, const SourceRange &OpRange, - const SourceRange &DestRange, CastExpr::CastKind &Kind) -{ + const SourceRange &DestRange, CastExpr::CastKind &Kind) { QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType(); DestType = Self.Context.getCanonicalType(DestType); @@ -325,8 +322,7 @@ CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType, /// legacy_function(const_cast\<char*\>(str)); void CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, - const SourceRange &OpRange, const SourceRange &DestRange) -{ + const SourceRange &OpRange, const SourceRange &DestRange) { if (!DestType->isLValueReferenceType()) Self.DefaultFunctionArrayConversion(SrcExpr); @@ -344,8 +340,7 @@ CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType, /// char *bytes = reinterpret_cast\<char*\>(int_ptr); void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType, - const SourceRange &OpRange, const SourceRange &DestRange) -{ + const SourceRange &OpRange, const SourceRange &DestRange) { if (!DestType->isLValueReferenceType()) Self.DefaultFunctionArrayConversion(SrcExpr); @@ -364,8 +359,7 @@ CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType, /// implicit conversions explicit and getting rid of data loss warnings. void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, - const SourceRange &OpRange, CastExpr::CastKind &Kind) -{ + const SourceRange &OpRange, CastExpr::CastKind &Kind) { // This test is outside everything else because it's the only case where // a non-lvalue-reference target type does not lead to decay. // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". @@ -378,7 +372,7 @@ CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, unsigned msg = diag::err_bad_cxx_cast_generic; CXXMethodDecl *ConversionDecl = 0; - if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, Kind, + if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, Kind, OpRange, msg, ConversionDecl) != TC_Success && msg != 0) Self.Diag(OpRange.getBegin(), msg) << CT_Static @@ -390,10 +384,9 @@ CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, /// and casting away constness. static TryCastResult TryStaticCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, - CastExpr::CastKind &Kind, + CastExpr::CastKind &Kind, const SourceRange &OpRange, unsigned &msg, - CXXMethodDecl *&ConversionDecl) -{ + CXXMethodDecl *&ConversionDecl) { // The order the tests is not entirely arbitrary. There is one conversion // that can be handled in two different ways. Given: // struct A {}; @@ -500,8 +493,7 @@ static TryCastResult TryStaticCast(Sema &Self, Expr *SrcExpr, /// Tests whether a conversion according to N2844 is valid. TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, - unsigned &msg) -{ + unsigned &msg) { // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue // reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1". const RValueReferenceType *R = DestType->getAs<RValueReferenceType>(); @@ -531,8 +523,7 @@ TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType, TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, const SourceRange &OpRange, - unsigned &msg) -{ + unsigned &msg) { // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be // cast to type "reference to cv2 D", where D is a class derived from B, // if a valid standard conversion from "pointer to D" to "pointer to B" @@ -562,8 +553,8 @@ TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType, /// Tests whether a conversion according to C++ 5.2.9p8 is valid. TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, - bool CStyle, const SourceRange &OpRange, unsigned &msg) -{ + bool CStyle, const SourceRange &OpRange, + unsigned &msg) { // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class // type, can be converted to an rvalue of type "pointer to cv2 D", where D // is a class derived from B, if a valid standard conversion from "pointer @@ -594,8 +585,7 @@ TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType, TryCastResult TryStaticDowncast(Sema &Self, QualType SrcType, QualType DestType, bool CStyle, const SourceRange &OpRange, QualType OrigSrcType, - QualType OrigDestType, unsigned &msg) -{ + QualType OrigDestType, unsigned &msg) { // Downcast can only happen in class hierarchies, so we need classes. if (!DestType->isRecordType() || !SrcType->isRecordType()) { return TC_NotApplicable; @@ -614,7 +604,7 @@ TryStaticDowncast(Sema &Self, QualType SrcType, QualType DestType, // struct B : virtual A { // B(A&); // }; - // + // // void f() // { // (void)static_cast<const B&>(*((A*)0)); @@ -691,8 +681,7 @@ TryStaticDowncast(Sema &Self, QualType SrcType, QualType DestType, TryCastResult TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType, QualType DestType, bool CStyle, const SourceRange &OpRange, - unsigned &msg) -{ + unsigned &msg) { const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(); if (!DestMemPtr) return TC_NotApplicable; @@ -757,8 +746,7 @@ TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType, QualType DestType, TryCastResult TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, const SourceRange &OpRange, unsigned &msg, - CXXMethodDecl *&ConversionDecl) -{ + CXXMethodDecl *&ConversionDecl) { if (DestType->isRecordType()) { if (Self.RequireCompleteType(OpRange.getBegin(), DestType, diag::err_bad_dynamic_cast_incomplete)) { @@ -766,7 +754,7 @@ TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, QualType DestType, return TC_Failed; } } - + if (DestType->isReferenceType()) { // At this point of CheckStaticCast, if the destination is a reference, // this has to work. There is no other way that works. @@ -774,7 +762,7 @@ TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, QualType DestType, // the reinterpret_cast way. In that case, we pass an ICS so we don't // get error messages. ImplicitConversionSequence ICS; - bool failed = Self.CheckReferenceInit(SrcExpr, DestType, + bool failed = Self.CheckReferenceInit(SrcExpr, DestType, /*SuppressUserConversions=*/false, /*AllowExplicit=*/false, /*ForceRValue=*/false, @@ -792,15 +780,15 @@ TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, QualType DestType, // reimplement more of this. // FIXME: This does not actually perform the conversion, and thus does not // check for ambiguity or access. - ImplicitConversionSequence ICS = + ImplicitConversionSequence ICS = Self.TryImplicitConversion(SrcExpr, DestType, /*SuppressUserConversions=*/false, /*AllowExplicit=*/true, /*ForceRValue=*/false, /*InOverloadResolution=*/false); - + if (ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) - if (CXXMethodDecl *MD = + if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ICS.UserDefined.ConversionFunction)) ConversionDecl = MD; return ICS.ConversionKind == ImplicitConversionSequence::BadConversion ? @@ -1032,8 +1020,7 @@ static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr, bool Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, Expr *&CastExpr, CastExpr::CastKind &Kind, bool FunctionalStyle, - CXXMethodDecl *&ConversionDecl) -{ + CXXMethodDecl *&ConversionDecl) { // This test is outside everything else because it's the only case where // a non-lvalue-reference target type does not lead to decay. // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void". @@ -1067,7 +1054,7 @@ bool Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, Expr *&CastExpr, ConversionDecl); if (tcr == TC_NotApplicable) { // ... and finally a reinterpret_cast, ignoring const. - tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, Kind, + tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, Kind, R, msg); } } |