From cdb61979755c1c0699c1ee25eede9a50bf94d29b Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Fri, 7 Aug 2009 22:21:05 +0000 Subject: [PATCH] More CastKind work. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78415 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/Sema.h | 4 ++-- lib/Sema/SemaCXXCast.cpp | 26 ++++++++++++++------------ lib/Sema/SemaChecking.cpp | 6 +++--- lib/Sema/SemaExpr.cpp | 13 ++++++++----- lib/Sema/SemaExprCXX.cpp | 6 +++--- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index e83fcf579f..6094e6dae0 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -3264,7 +3264,7 @@ public: /// CheckCastTypes - Check type constraints for casting between types under /// C semantics, or forward to CXXCheckCStyleCast in C++. bool CheckCastTypes(SourceRange TyRange, QualType CastTy, Expr *&CastExpr, - bool FunctionalStyle = false); + CastExpr::CastKind &Kind, bool FunctionalStyle = false); // CheckVectorCast - check type constraints for vectors. // Since vectors are an extension, there are no C standard reference for this. @@ -3282,7 +3282,7 @@ public: /// CXXCheckCStyleCast - Check constraints of a C-style or function-style /// cast under C++ semantics. bool CXXCheckCStyleCast(SourceRange R, QualType CastTy, Expr *&CastExpr, - bool FunctionalStyle); + CastExpr::CastKind &Kind, bool FunctionalStyle); /// CheckMessageArgumentTypes - Check types in an Obj-C message send. /// \param Method - May be null. diff --git a/lib/Sema/SemaCXXCast.cpp b/lib/Sema/SemaCXXCast.cpp index 254af54171..b8dbd18eb3 100644 --- a/lib/Sema/SemaCXXCast.cpp +++ b/lib/Sema/SemaCXXCast.cpp @@ -42,7 +42,8 @@ static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType, const SourceRange &OpRange, const SourceRange &DestRange); static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, - const SourceRange &OpRange); + const SourceRange &OpRange, + CastExpr::CastKind &Kind); static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType, const SourceRange &OpRange, const SourceRange &DestRange, @@ -87,7 +88,7 @@ static TryCastResult TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, static TryCastResult TryStaticCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, const SourceRange &OpRange, - unsigned &msg); + CastExpr::CastKind &Kind, unsigned &msg); static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, unsigned &msg); static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr, @@ -134,12 +135,13 @@ Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind, DestType.getNonReferenceType(), Ex, DestType, OpLoc)); - case tok::kw_static_cast: + case tok::kw_static_cast: { + CastExpr::CastKind Kind = CastExpr::CK_Unknown; if (!TypeDependent) - CheckStaticCast(*this, Ex, DestType, OpRange); + CheckStaticCast(*this, Ex, DestType, OpRange, Kind); return Owned(new (Context) CXXStaticCastExpr(DestType.getNonReferenceType(), - CastExpr::CK_Unknown, Ex, - DestType, OpLoc)); + Kind, Ex, DestType, OpLoc)); + } } return ExprError(); @@ -354,7 +356,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) + 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. @@ -367,7 +369,8 @@ CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, Self.DefaultFunctionArrayConversion(SrcExpr); unsigned msg = diag::err_bad_cxx_cast_generic; - if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg) + if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, + Kind, msg) != TC_Success && msg != 0) Self.Diag(OpRange.getBegin(), msg) << CT_Static << SrcExpr->getType() << DestType << OpRange; @@ -379,7 +382,7 @@ CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType, static TryCastResult TryStaticCast(Sema &Self, Expr *SrcExpr, QualType DestType, bool CStyle, const SourceRange &OpRange, - unsigned &msg) + CastExpr::CastKind &Kind, unsigned &msg) { // The order the tests is not entirely arbitrary. There is one conversion // that can be handled in two different ways. Given: @@ -1003,9 +1006,8 @@ static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr, return TC_Success; } - bool Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, Expr *&CastExpr, - bool FunctionalStyle) + CastExpr::CastKind &Kind, bool FunctionalStyle) { // This test is outside everything else because it's the only case where // a non-lvalue-reference target type does not lead to decay. @@ -1035,7 +1037,7 @@ bool Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, Expr *&CastExpr, TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,msg); if (tcr == TC_NotApplicable) { // ... or if that is not possible, a static_cast, ignoring const, ... - tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg); + tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, Kind, msg); if (tcr == TC_NotApplicable) { // ... and finally a reinterpret_cast, ignoring const. tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg); diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 949c33dfff..b35287aa31 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -383,7 +383,8 @@ bool Sema::SemaBuiltinAtomicOverloaded(CallExpr *TheCall) { // GCC does an implicit conversion to the pointer or integer ValType. This // can fail in some cases (1i -> int**), check for this error case now. - if (CheckCastTypes(Arg->getSourceRange(), ValType, Arg)) + CastExpr::CastKind Kind = CastExpr::CK_Unknown; + if (CheckCastTypes(Arg->getSourceRange(), ValType, Arg, Kind)) return true; // Okay, we have something that *can* be converted to the right type. Check @@ -392,8 +393,7 @@ bool Sema::SemaBuiltinAtomicOverloaded(CallExpr *TheCall) { // pass in 42. The 42 gets converted to char. This is even more strange // for things like 45.123 -> char, etc. // FIXME: Do this check. - ImpCastExprToType(Arg, ValType, CastExpr::CK_Unknown, - /*isLvalue=*/false); + ImpCastExprToType(Arg, ValType, Kind, /*isLvalue=*/false); TheCall->setArg(i+1, Arg); } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 51ebd079a4..684c2dafdb 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2972,9 +2972,9 @@ Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, /// CheckCastTypes - Check type constraints for casting between types. bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, - bool FunctionalStyle) { + CastExpr::CastKind& Kind, bool FunctionalStyle) { if (getLangOptions().CPlusPlus) - return CXXCheckCStyleCast(TyR, castType, castExpr, FunctionalStyle); + return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle); UsualUnaryConversions(castExpr); @@ -3087,17 +3087,20 @@ bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, QualType SrcTy) { Action::OwningExprResult Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, SourceLocation RParenLoc, ExprArg Op) { + CastExpr::CastKind Kind = CastExpr::CK_Unknown; + assert((Ty != 0) && (Op.get() != 0) && "ActOnCastExpr(): missing type or expr"); Expr *castExpr = Op.takeAs(); QualType castType = QualType::getFromOpaquePtr(Ty); - if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) + if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, + Kind)) return ExprError(); return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), - CastExpr::CK_Unknown, castExpr, - castType, LParenLoc, RParenLoc)); + Kind, castExpr, castType, + LParenLoc, RParenLoc)); } /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 5b74938191..8a352c4ee2 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -203,12 +203,12 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, // corresponding cast expression. // if (NumExprs == 1) { - if (CheckCastTypes(TypeRange, Ty, Exprs[0], /*functional-style*/true)) + CastExpr::CastKind Kind = CastExpr::CK_Unknown; + if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, /*functional-style*/true)) return ExprError(); exprs.release(); return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), - Ty, TyBeginLoc, - CastExpr::CK_Unknown, + Ty, TyBeginLoc, Kind, Exprs[0], RParenLoc)); } -- 2.40.0