zcov: / lib/Sema/SemaCXXCast.cpp


Files: 1 Branches Taken: 88.7% 418 / 471
Generated: 2010-02-10 01:31 Branches Executed: 100.0% 471 / 471
Line Coverage: 96.7% 442 / 457


Programs: 2 Runs 3018


       1                 : //===--- SemaNamedCast.cpp - Semantic Analysis for Named Casts ------------===//
       2                 : //
       3                 : //                     The LLVM Compiler Infrastructure
       4                 : //
       5                 : // This file is distributed under the University of Illinois Open Source
       6                 : // License. See LICENSE.TXT for details.
       7                 : //
       8                 : //===----------------------------------------------------------------------===//
       9                 : //
      10                 : //  This file implements semantic analysis for C++ named casts.
      11                 : //
      12                 : //===----------------------------------------------------------------------===//
      13                 : 
      14                 : #include "Sema.h"
      15                 : #include "SemaInit.h"
      16                 : #include "clang/AST/ExprCXX.h"
      17                 : #include "clang/AST/ASTContext.h"
      18                 : #include "clang/AST/CXXInheritance.h"
      19                 : #include "clang/Basic/PartialDiagnostic.h"
      20                 : #include "llvm/ADT/SmallVector.h"
      21                 : #include <set>
      22                 : using namespace clang;
      23                 : 
      24                 : enum TryCastResult {
      25                 :   TC_NotApplicable, ///< The cast method is not applicable.
      26                 :   TC_Success,       ///< The cast method is appropriate and successful.
      27                 :   TC_Failed         ///< The cast method is appropriate, but failed. A
      28                 :                     ///< diagnostic has been emitted.
      29                 : };
      30                 : 
      31                 : enum CastType {
      32                 :   CT_Const,       ///< const_cast
      33                 :   CT_Static,      ///< static_cast
      34                 :   CT_Reinterpret, ///< reinterpret_cast
      35                 :   CT_Dynamic,     ///< dynamic_cast
      36                 :   CT_CStyle,      ///< (Type)expr
      37                 :   CT_Functional   ///< Type(expr)
      38                 : };
      39                 : 
      40                 : static void CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
      41                 :                            const SourceRange &OpRange,
      42                 :                            const SourceRange &DestRange);
      43                 : static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
      44                 :                                  const SourceRange &OpRange,
      45                 :                                  const SourceRange &DestRange,
      46                 :                                  CastExpr::CastKind &Kind);
      47                 : static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
      48                 :                             const SourceRange &OpRange,
      49                 :                             CastExpr::CastKind &Kind,
      50                 :                             CXXMethodDecl *&ConversionDecl);
      51                 : static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
      52                 :                              const SourceRange &OpRange,
      53                 :                              const SourceRange &DestRange,
      54                 :                              CastExpr::CastKind &Kind);
      55                 : 
      56                 : static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType);
      57                 : 
      58                 : // The Try functions attempt a specific way of casting. If they succeed, they
      59                 : // return TC_Success. If their way of casting is not appropriate for the given
      60                 : // arguments, they return TC_NotApplicable and *may* set diag to a diagnostic
      61                 : // to emit if no other way succeeds. If their way of casting is appropriate but
      62                 : // fails, they return TC_Failed and *must* set diag; they can set it to 0 if
      63                 : // they emit a specialized diagnostic.
      64                 : // All diagnostics returned by these functions must expect the same three
      65                 : // arguments:
      66                 : // %0: Cast Type (a value from the CastType enumeration)
      67                 : // %1: Source Type
      68                 : // %2: Destination Type
      69                 : static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr,
      70                 :                                            QualType DestType, unsigned &msg);
      71                 : static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
      72                 :                                                 QualType DestType, bool CStyle,
      73                 :                                                 const SourceRange &OpRange,
      74                 :                                                 unsigned &msg,
      75                 :                                                 CastExpr::CastKind &Kind);
      76                 : static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
      77                 :                                               QualType DestType, bool CStyle,
      78                 :                                               const SourceRange &OpRange,
      79                 :                                               unsigned &msg,
      80                 :                                               CastExpr::CastKind &Kind);
      81                 : static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
      82                 :                                        CanQualType DestType, bool CStyle,
      83                 :                                        const SourceRange &OpRange,
      84                 :                                        QualType OrigSrcType,
      85                 :                                        QualType OrigDestType, unsigned &msg,
      86                 :                                        CastExpr::CastKind &Kind);
      87                 : static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType,
      88                 :                                                   QualType DestType,bool CStyle,
      89                 :                                                   const SourceRange &OpRange,
      90                 :                                                   unsigned &msg,
      91                 :                                                   CastExpr::CastKind &Kind);
      92                 : static TryCastResult TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr,
      93                 :                                            QualType DestType, bool CStyle,
      94                 :                                            const SourceRange &OpRange,
      95                 :                                            unsigned &msg,
      96                 :                                            CastExpr::CastKind &Kind,
      97                 :                                            CXXMethodDecl *&ConversionDecl);
      98                 : static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
      99                 :                                    QualType DestType, bool CStyle,
     100                 :                                    const SourceRange &OpRange,
     101                 :                                    unsigned &msg,
     102                 :                                    CastExpr::CastKind &Kind,
     103                 :                                    CXXMethodDecl *&ConversionDecl);
     104                 : static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
     105                 :                                   bool CStyle, unsigned &msg);
     106                 : static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
     107                 :                                         QualType DestType, bool CStyle,
     108                 :                                         const SourceRange &OpRange,
     109                 :                                         unsigned &msg,
     110                 :                                         CastExpr::CastKind &Kind);
     111                 : 
     112                 : /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
     113                 : Action::OwningExprResult
     114                 : Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
     115                 :                         SourceLocation LAngleBracketLoc, TypeTy *Ty,
     116                 :                         SourceLocation RAngleBracketLoc,
     117                 :                         SourceLocation LParenLoc, ExprArg E,
     118              251:                         SourceLocation RParenLoc) {
     119                 :   
     120                 :   TypeSourceInfo *DestTInfo;
     121              251:   QualType DestType = GetTypeFromParser(Ty, &DestTInfo);
                        0: branch 0 not taken
                      251: branch 1 taken
     122              251:   if (!DestTInfo)
     123                0:     DestTInfo = Context.getTrivialTypeSourceInfo(DestType, SourceLocation());
     124                 : 
     125                 :   return BuildCXXNamedCast(OpLoc, Kind, DestTInfo, move(E),
     126                 :                            SourceRange(LAngleBracketLoc, RAngleBracketLoc),
     127              251:                            SourceRange(LParenLoc, RParenLoc));
     128                 : }
     129                 : 
     130                 : Action::OwningExprResult
     131                 : Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
     132                 :                         TypeSourceInfo *DestTInfo, ExprArg E,
     133              268:                         SourceRange AngleBrackets, SourceRange Parens) {
     134              268:   Expr *Ex = E.takeAs<Expr>();
     135              268:   QualType DestType = DestTInfo->getType();
     136                 : 
     137              268:   SourceRange OpRange(OpLoc, Parens.getEnd());
     138              268:   SourceRange DestRange = AngleBrackets;
     139                 : 
     140                 :   // If the type is dependent, we won't do the semantic analysis now.
     141                 :   // FIXME: should we check this in a more fine-grained manner?
                      255: branch 2 taken
                       13: branch 3 taken
                        7: branch 5 taken
                      248: branch 6 taken
     142              268:   bool TypeDependent = DestType->isDependentType() || Ex->isTypeDependent();
     143                 : 
                        0: branch 0 not taken
                       23: branch 1 taken
                       43: branch 2 taken
                       72: branch 3 taken
                      130: branch 4 taken
     144              268:   switch (Kind) {
     145                0:   default: assert(0 && "Unknown C++ cast!");
     146                 : 
     147                 :   case tok::kw_const_cast:
                       20: branch 0 taken
                        3: branch 1 taken
     148               23:     if (!TypeDependent)
     149               20:       CheckConstCast(*this, Ex, DestType, OpRange, DestRange);
     150                 :     return Owned(new (Context) CXXConstCastExpr(DestType.getNonReferenceType(),
                       23: branch 2 taken
                        0: branch 3 not taken
     151               23:                                                 Ex, DestTInfo, OpLoc));
     152                 : 
     153                 :   case tok::kw_dynamic_cast: {
     154               43:     CastExpr::CastKind Kind = CastExpr::CK_Unknown;
                       40: branch 0 taken
                        3: branch 1 taken
     155               43:     if (!TypeDependent)
     156               40:       CheckDynamicCast(*this, Ex, DestType, OpRange, DestRange, Kind);
     157                 :     return Owned(new (Context)CXXDynamicCastExpr(DestType.getNonReferenceType(),
                       43: branch 2 taken
                        0: branch 3 not taken
     158               43:                                                  Kind, Ex, DestTInfo, OpLoc));
     159                 :   }
     160                 :   case tok::kw_reinterpret_cast: {
     161               72:     CastExpr::CastKind Kind = CastExpr::CK_Unknown;
                       65: branch 0 taken
                        7: branch 1 taken
     162               72:     if (!TypeDependent)
     163               65:       CheckReinterpretCast(*this, Ex, DestType, OpRange, DestRange, Kind);
     164                 :     return Owned(new (Context) CXXReinterpretCastExpr(
     165                 :                                   DestType.getNonReferenceType(),
                       72: branch 2 taken
                        0: branch 3 not taken
     166               72:                                   Kind, Ex, DestTInfo, OpLoc));
     167                 :   }
     168                 :   case tok::kw_static_cast: {
     169              130:     CastExpr::CastKind Kind = CastExpr::CK_Unknown;
                      123: branch 0 taken
                        7: branch 1 taken
     170              130:     if (!TypeDependent) {
     171              123:       CXXMethodDecl *Method = 0;
     172                 :       
     173              123:       CheckStaticCast(*this, Ex, DestType, OpRange, Kind, Method);
     174                 :       
                        8: branch 0 taken
                      115: branch 1 taken
     175              123:       if (Method) {
     176                 :         OwningExprResult CastArg 
     177                 :           = BuildCXXCastArgument(OpLoc, DestType.getNonReferenceType(), 
     178                8:                                  Kind, Method, Owned(Ex));
                        0: branch 1 not taken
                        8: branch 2 taken
     179                8:           if (CastArg.isInvalid())
     180                0:             return ExprError();
     181                 :           
                        8: branch 2 taken
                        0: branch 3 not taken
     182                8:           Ex = CastArg.takeAs<Expr>();
     183                 :       }
     184                 :     }
     185                 :     
     186                 :     return Owned(new (Context) CXXStaticCastExpr(DestType.getNonReferenceType(),
                      130: branch 2 taken
                        0: branch 3 not taken
     187              130:                                                  Kind, Ex, DestTInfo, OpLoc));
     188                 :   }
     189                 :   }
     190                 : 
     191                 :   return ExprError();
     192                 : }
     193                 : 
     194                 : /// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes,
     195                 : /// this removes one level of indirection from both types, provided that they're
     196                 : /// the same kind of pointer (plain or to-member). Unlike the Sema function,
     197                 : /// this one doesn't care if the two pointers-to-member don't point into the
     198                 : /// same class. This is because CastsAwayConstness doesn't care.
     199               60: bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
     200               60:   const PointerType *T1PtrType = T1->getAs<PointerType>(),
     201               60:                     *T2PtrType = T2->getAs<PointerType>();
                       30: branch 0 taken
                       30: branch 1 taken
                       26: branch 2 taken
                        4: branch 3 taken
     202               60:   if (T1PtrType && T2PtrType) {
     203               26:     T1 = T1PtrType->getPointeeType();
     204               26:     T2 = T2PtrType->getPointeeType();
     205               26:     return true;
     206                 :   }
     207                 :   const ObjCObjectPointerType *T1ObjCPtrType = 
     208               34:                                             T1->getAs<ObjCObjectPointerType>(),
     209                 :                               *T2ObjCPtrType = 
     210               34:                                             T2->getAs<ObjCObjectPointerType>();
                        2: branch 0 taken
                       32: branch 1 taken
     211               34:   if (T1ObjCPtrType) {
                        1: branch 0 taken
                        1: branch 1 taken
     212                2:     if (T2ObjCPtrType) {
     213                1:       T1 = T1ObjCPtrType->getPointeeType();
     214                1:       T2 = T2ObjCPtrType->getPointeeType();
     215                1:       return true;
     216                 :     }
                        1: branch 0 taken
                        0: branch 1 not taken
     217                1:     else if (T2PtrType) {
     218                1:       T1 = T1ObjCPtrType->getPointeeType();
     219                1:       T2 = T2PtrType->getPointeeType();
     220                1:       return true;
     221                 :     }
     222                 :   }
                        1: branch 0 taken
                       31: branch 1 taken
     223               32:   else if (T2ObjCPtrType) {
                        1: branch 0 taken
                        0: branch 1 not taken
     224                1:     if (T1PtrType) {
     225                1:       T2 = T2ObjCPtrType->getPointeeType();
     226                1:       T1 = T1PtrType->getPointeeType();
     227                1:       return true;
     228                 :     }
     229                 :   }
     230                 :   
     231               31:   const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
     232               31:                           *T2MPType = T2->getAs<MemberPointerType>();
                        4: branch 0 taken
                       27: branch 1 taken
                        4: branch 2 taken
                        0: branch 3 not taken
     233               31:   if (T1MPType && T2MPType) {
     234                4:     T1 = T1MPType->getPointeeType();
     235                4:     T2 = T2MPType->getPointeeType();
     236                4:     return true;
     237                 :   }
     238               27:   return false;
     239                 : }
     240                 : 
     241                 : /// CastsAwayConstness - Check if the pointer conversion from SrcType to
     242                 : /// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
     243                 : /// the cast checkers.  Both arguments must denote pointer (possibly to member)
     244                 : /// types.
     245                 : static bool
     246               27: CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) {
     247                 :   // Casting away constness is defined in C++ 5.2.11p8 with reference to
     248                 :   // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
     249                 :   // the rules are non-trivial. So first we construct Tcv *...cv* as described
     250                 :   // in C++ 5.2.11p8.
     251                 :   assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType()) &&
                        4: branch 2 taken
                       23: branch 3 taken
                        4: branch 6 taken
                        0: branch 7 not taken
     252               27:          "Source type is not pointer or pointer to member.");
     253                 :   assert((DestType->isAnyPointerType() || DestType->isMemberPointerType()) &&
                        4: branch 2 taken
                       23: branch 3 taken
                        4: branch 6 taken
                        0: branch 7 not taken
     254               31:          "Destination type is not pointer or pointer to member.");
     255                 : 
     256               27:   QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType), 
     257               27:            UnwrappedDestType = Self.Context.getCanonicalType(DestType);
     258               27:   llvm::SmallVector<Qualifiers, 8> cv1, cv2;
     259                 : 
     260                 :   // Find the qualifications.
                       33: branch 1 taken
                       27: branch 2 taken
     261               87:   while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
     262               33:     cv1.push_back(UnwrappedSrcType.getQualifiers());
     263               33:     cv2.push_back(UnwrappedDestType.getQualifiers());
     264                 :   }
                       27: branch 1 taken
                        0: branch 2 not taken
     265               27:   assert(cv1.size() > 0 && "Must have at least one pointer level.");
     266                 : 
     267                 :   // Construct void pointers with those qualifiers (in reverse order of
     268                 :   // unwrapping, of course).
     269               27:   QualType SrcConstruct = Self.Context.VoidTy;
     270               27:   QualType DestConstruct = Self.Context.VoidTy;
     271               27:   ASTContext &Context = Self.Context;
                       33: branch 5 taken
                       27: branch 6 taken
     272               87:   for (llvm::SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(),
     273               27:                                                           i2 = cv2.rbegin();
     274                 :        i1 != cv1.rend(); ++i1, ++i2) {
     275                 :     SrcConstruct
     276               33:       = Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
     277                 :     DestConstruct
     278               33:       = Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
     279                 :   }
     280                 : 
     281                 :   // Test if they're compatible.
     282                 :   return SrcConstruct != DestConstruct &&
                        7: branch 1 taken
                       20: branch 2 taken
                        4: branch 4 taken
                        3: branch 5 taken
     283               27:     !Self.IsQualificationConversion(SrcConstruct, DestConstruct);
     284                 : }
     285                 : 
     286                 : /// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
     287                 : /// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
     288                 : /// checked downcasts in class hierarchies.
     289                 : static void
     290                 : CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
     291                 :                  const SourceRange &OpRange,
     292               40:                  const SourceRange &DestRange, CastExpr::CastKind &Kind) {
     293               40:   QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
     294               40:   DestType = Self.Context.getCanonicalType(DestType);
     295                 : 
     296                 :   // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
     297                 :   //   or "pointer to cv void".
     298                 : 
     299               40:   QualType DestPointee;
     300               40:   const PointerType *DestPointer = DestType->getAs<PointerType>();
     301               40:   const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
                       28: branch 0 taken
                       12: branch 1 taken
     302               40:   if (DestPointer) {
     303               28:     DestPointee = DestPointer->getPointeeType();
                       10: branch 0 taken
                        2: branch 1 taken
     304               12:   } else if (DestReference) {
     305               10:     DestPointee = DestReference->getPointeeType();
     306                 :   } else {
     307                 :     Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
     308                2:       << OrigDestType << DestRange;
     309                2:     return;
     310                 :   }
     311                 : 
     312               38:   const RecordType *DestRecord = DestPointee->getAs<RecordType>();
                        2: branch 2 taken
                       36: branch 3 taken
     313               38:   if (DestPointee->isVoidType()) {
                        0: branch 0 not taken
                        2: branch 1 taken
     314                2:     assert(DestPointer && "Reference to void is not possible");
                       34: branch 0 taken
                        2: branch 1 taken
     315               36:   } else if (DestRecord) {
                        1: branch 10 taken
                       33: branch 11 taken
     316               34:     if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
     317                 :                                  PDiag(diag::err_bad_dynamic_cast_incomplete)
     318                 :                                    << DestRange))
     319                1:       return;
     320                 :   } else {
     321                 :     Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
     322                2:       << DestPointee.getUnqualifiedType() << DestRange;
     323                2:     return;
     324                 :   }
     325                 : 
     326                 :   // C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
     327                 :   //   complete class type, [...]. If T is an lvalue reference type, v shall be
     328                 :   //   an lvalue of a complete class type, [...]. If T is an rvalue reference
     329                 :   //   type, v shall be an expression having a complete effective class type,
     330                 :   //   [...]
     331                 : 
     332               35:   QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
     333               35:   QualType SrcPointee;
                       26: branch 0 taken
                        9: branch 1 taken
     334               35:   if (DestPointer) {
                       25: branch 2 taken
                        1: branch 3 taken
     335               26:     if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
     336               25:       SrcPointee = SrcPointer->getPointeeType();
     337                 :     } else {
     338                 :       Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
     339                1:         << OrigSrcType << SrcExpr->getSourceRange();
     340                1:       return;
     341                 :     }
                        9: branch 1 taken
                        0: branch 2 not taken
     342                9:   } else if (DestReference->isLValueReferenceType()) {
                        0: branch 1 not taken
                        9: branch 2 taken
     343                9:     if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
     344                 :       Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
     345                0:         << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
     346                 :     }
     347                9:     SrcPointee = SrcType;
     348                 :   } else {
     349                0:     SrcPointee = SrcType;
     350                 :   }
     351                 : 
     352               34:   const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
                       32: branch 0 taken
                        2: branch 1 taken
     353               34:   if (SrcRecord) {
                        1: branch 11 taken
                       31: branch 12 taken
     354               32:     if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
     355                 :                                  PDiag(diag::err_bad_dynamic_cast_incomplete)
     356                 :                                    << SrcExpr->getSourceRange()))
     357                1:       return;
     358                 :   } else {
     359                 :     Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
     360                2:       << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
     361                2:     return;
     362                 :   }
     363                 : 
     364                 :   assert((DestPointer || DestReference) &&
                        8: branch 0 taken
                       23: branch 1 taken
                        0: branch 2 not taken
                        8: branch 3 taken
     365               39:     "Bad destination non-ptr/ref slipped through.");
     366                 :   assert((DestRecord || DestPointee->isVoidType()) &&
                        2: branch 0 taken
                       29: branch 1 taken
                        2: branch 4 taken
                        0: branch 5 not taken
     367               31:     "Bad destination pointee slipped through.");
                        0: branch 0 not taken
                       31: branch 1 taken
     368               31:   assert(SrcRecord && "Bad source pointee slipped through.");
     369                 : 
     370                 :   // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
                        0: branch 1 not taken
                       31: branch 2 taken
     371               31:   if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
     372                 :     Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
     373                0:       << CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
     374                0:     return;
     375                 :   }
     376                 : 
     377                 :   // C++ 5.2.7p3: If the type of v is the same as the required result type,
     378                 :   //   [except for cv].
                        4: branch 0 taken
                       27: branch 1 taken
     379               31:   if (DestRecord == SrcRecord) {
     380                4:     return;
     381                 :   }
     382                 : 
     383                 :   // C++ 5.2.7p5
     384                 :   // Upcasts are resolved statically.
                       25: branch 0 taken
                        2: branch 1 taken
                        9: branch 3 taken
                       16: branch 4 taken
                        9: branch 5 taken
                       18: branch 6 taken
     385               27:   if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
     386                 :     Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
     387                9:                                       OpRange.getBegin(), OpRange);
     388                9:     Kind = CastExpr::CK_DerivedToBase;
     389                 :     // Diagnostic already emitted on error.
     390                9:     return;
     391                 :   }
     392                 : 
     393                 :   // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
     394               18:   const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(Self.Context);
                        0: branch 0 not taken
                       18: branch 1 taken
     395               18:   assert(SrcDecl && "Definition missing");
                        2: branch 2 taken
                       16: branch 3 taken
     396               18:   if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
     397                 :     Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
     398                2:       << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
     399                 :   }
     400                 : 
     401                 :   // Done. Everything else is run-time checks.
     402               18:   Kind = CastExpr::CK_Dynamic;
     403                 : }
     404                 : 
     405                 : /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
     406                 : /// Refer to C++ 5.2.11 for details. const_cast is typically used in code
     407                 : /// like this:
     408                 : /// const char *str = "literal";
     409                 : /// legacy_function(const_cast\<char*\>(str));
     410                 : void
     411                 : CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
     412               20:                const SourceRange &OpRange, const SourceRange &DestRange) {
                       17: branch 2 taken
                        3: branch 3 taken
     413               20:   if (!DestType->isLValueReferenceType())
     414               17:     Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
     415                 : 
     416               20:   unsigned msg = diag::err_bad_cxx_cast_generic;
                        8: branch 1 taken
                       12: branch 2 taken
                        8: branch 3 taken
                        0: branch 4 not taken
                        8: branch 5 taken
                       12: branch 6 taken
     417               20:   if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success
     418                 :       && msg != 0)
     419                 :     Self.Diag(OpRange.getBegin(), msg) << CT_Const
     420                8:       << SrcExpr->getType() << DestType << OpRange;
     421               20: }
     422                 : 
     423                 : /// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
     424                 : /// valid.
     425                 : /// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
     426                 : /// like this:
     427                 : /// char *bytes = reinterpret_cast\<char*\>(int_ptr);
     428                 : void
     429                 : CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
     430                 :                      const SourceRange &OpRange, const SourceRange &DestRange,
     431               65:                      CastExpr::CastKind &Kind) {
                       61: branch 2 taken
                        4: branch 3 taken
     432               65:   if (!DestType->isLValueReferenceType())
     433               61:     Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
     434                 : 
     435               65:   unsigned msg = diag::err_bad_cxx_cast_generic;
                       20: branch 1 taken
                       45: branch 2 taken
                       20: branch 3 taken
                        0: branch 4 not taken
                       20: branch 5 taken
                       45: branch 6 taken
     436               65:   if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange,
     437                 :                          msg, Kind)
     438                 :       != TC_Success && msg != 0)
     439                 :     Self.Diag(OpRange.getBegin(), msg) << CT_Reinterpret
     440               20:       << SrcExpr->getType() << DestType << OpRange;
     441               65: }
     442                 : 
     443                 : 
     444                 : /// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
     445                 : /// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
     446                 : /// implicit conversions explicit and getting rid of data loss warnings.
     447                 : void
     448                 : CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
     449                 :                 const SourceRange &OpRange, CastExpr::CastKind &Kind,
     450              123:                 CXXMethodDecl *&ConversionDecl) {
     451                 :   // This test is outside everything else because it's the only case where
     452                 :   // a non-lvalue-reference target type does not lead to decay.
     453                 :   // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
                       19: branch 2 taken
                      104: branch 3 taken
     454              123:   if (DestType->isVoidType()) {
     455               19:     Kind = CastExpr::CK_ToVoid;
     456               19:     return;
     457                 :   }
     458                 : 
                       81: branch 2 taken
                       23: branch 3 taken
                       70: branch 6 taken
                       11: branch 7 taken
                       70: branch 8 taken
                       34: branch 9 taken
     459              104:   if (!DestType->isLValueReferenceType() && !DestType->isRecordType())
     460               70:     Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
     461                 : 
     462              104:   unsigned msg = diag::err_bad_cxx_cast_generic;
                       28: branch 1 taken
                       76: branch 2 taken
                       13: branch 3 taken
                       15: branch 4 taken
                       13: branch 5 taken
                       91: branch 6 taken
     463              104:   if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
     464                 :                     Kind, ConversionDecl)
     465                 :       != TC_Success && msg != 0)
     466                 :     Self.Diag(OpRange.getBegin(), msg) << CT_Static
     467               13:       << SrcExpr->getType() << DestType << OpRange;
     468                 : }
     469                 : 
     470                 : /// TryStaticCast - Check if a static cast can be performed, and do so if
     471                 : /// possible. If @p CStyle, ignore access restrictions on hierarchy casting
     472                 : /// and casting away constness.
     473                 : static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
     474                 :                                    QualType DestType, bool CStyle,
     475                 :                                    const SourceRange &OpRange, unsigned &msg,
     476                 :                                    CastExpr::CastKind &Kind,
     477             1374:                                    CXXMethodDecl *&ConversionDecl) {
     478                 :   // The order the tests is not entirely arbitrary. There is one conversion
     479                 :   // that can be handled in two different ways. Given:
     480                 :   // struct A {};
     481                 :   // struct B : public A {
     482                 :   //   B(); B(const A&);
     483                 :   // };
     484                 :   // const A &a = B();
     485                 :   // the cast static_cast<const B&>(a) could be seen as either a static
     486                 :   // reference downcast, or an explicit invocation of the user-defined
     487                 :   // conversion using B's conversion constructor.
     488                 :   // DR 427 specifies that the downcast is to be applied here.
     489                 : 
     490                 :   // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
     491                 :   // Done outside this function.
     492                 : 
     493                 :   TryCastResult tcr;
     494                 : 
     495                 :   // C++ 5.2.9p5, reference downcast.
     496                 :   // See the function for details.
     497                 :   // DR 427 specifies that this is to be applied before paragraph 2.
     498                 :   tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
     499             1374:                                    msg, Kind);
                       24: branch 0 taken
                     1350: branch 1 taken
     500             1374:   if (tcr != TC_NotApplicable)
     501               24:     return tcr;
     502                 : 
     503                 :   // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
     504                 :   //   reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
     505             1350:   tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, msg);
                        4: branch 0 taken
                     1346: branch 1 taken
     506             1350:   if (tcr != TC_NotApplicable) {
     507                4:     Kind = CastExpr::CK_NoOp;
     508                4:     return tcr;
     509                 :   }
     510                 : 
     511                 :   // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
     512                 :   //   [...] if the declaration "T t(e);" is well-formed, [...].
     513                 :   tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CStyle, OpRange, msg,
     514             1346:                               Kind, ConversionDecl);
                      740: branch 0 taken
                      606: branch 1 taken
     515             1346:   if (tcr != TC_NotApplicable)
     516              740:     return tcr;
     517                 :   
     518                 :   // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
     519                 :   // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
     520                 :   // conversions, subject to further restrictions.
     521                 :   // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
     522                 :   // of qualification conversions impossible.
     523                 :   // In the CStyle case, the earlier attempt to const_cast should have taken
     524                 :   // care of reverse qualification conversions.
     525                 : 
     526              606:   QualType OrigSrcType = SrcExpr->getType();
     527                 : 
     528              606:   QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
     529                 : 
     530                 :   // Reverse integral promotion/conversion. All such conversions are themselves
     531                 :   // again integral promotions or conversions and are thus already handled by
     532                 :   // p2 (TryDirectInitialization above).
     533                 :   // (Note: any data loss warnings should be suppressed.)
     534                 :   // The exception is the reverse of enum->integer, i.e. integer->enum (and
     535                 :   // enum->enum). See also C++ 5.2.9p7.
     536                 :   // The same goes for reverse floating point promotion/conversion and
     537                 :   // floating-integral conversions. Again, only floating->enum is relevant.
                       17: branch 2 taken
                      589: branch 3 taken
     538              606:   if (DestType->isEnumeralType()) {
                       17: branch 2 taken
                        0: branch 3 not taken
                        0: branch 6 not taken
                       17: branch 7 taken
                       17: branch 8 taken
                        0: branch 9 not taken
     539               17:     if (SrcType->isComplexType() || SrcType->isVectorType()) {
     540                 :       // Fall through - these cannot be converted.
                        3: branch 2 taken
                       14: branch 3 taken
                        0: branch 6 not taken
                        3: branch 7 taken
                       14: branch 8 taken
                        3: branch 9 taken
     541               17:     } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
     542               14:       Kind = CastExpr::CK_IntegralCast;
     543               14:       return TC_Success;
     544                 :     }
     545                 :   }
     546                 : 
     547                 :   // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
     548                 :   // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
     549                 :   tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
     550              592:                                  Kind);
                       24: branch 0 taken
                      568: branch 1 taken
     551              592:   if (tcr != TC_NotApplicable)
     552               24:     return tcr;
     553                 : 
     554                 :   // Reverse member pointer conversion. C++ 4.11 specifies member pointer
     555                 :   // conversion. C++ 5.2.9p9 has additional information.
     556                 :   // DR54's access restrictions apply here also.
     557                 :   tcr = TryStaticMemberPointerUpcast(Self, SrcType, DestType, CStyle,
     558              568:                                      OpRange, msg, Kind);
                       11: branch 0 taken
                      557: branch 1 taken
     559              568:   if (tcr != TC_NotApplicable)
     560               11:     return tcr;
     561                 : 
     562                 :   // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
     563                 :   // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
     564                 :   // just the usual constness stuff.
                      114: branch 2 taken
                      443: branch 3 taken
     565              557:   if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
     566              114:     QualType SrcPointee = SrcPointer->getPointeeType();
                       35: branch 2 taken
                       79: branch 3 taken
     567              114:     if (SrcPointee->isVoidType()) {
                       21: branch 2 taken
                       14: branch 3 taken
     568               35:       if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
     569               21:         QualType DestPointee = DestPointer->getPointeeType();
                       18: branch 2 taken
                        3: branch 3 taken
     570               21:         if (DestPointee->isIncompleteOrObjectType()) {
     571                 :           // This is definitely the intended conversion, but it might fail due
     572                 :           // to a const violation.
                        4: branch 0 taken
                       14: branch 1 taken
                        1: branch 3 taken
                        3: branch 4 taken
                        1: branch 5 taken
                       17: branch 6 taken
     573               18:           if (!CStyle && !DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
     574                1:             msg = diag::err_bad_cxx_cast_const_away;
     575                1:             return TC_Failed;
     576                 :           }
     577               17:           Kind = CastExpr::CK_BitCast;
     578               17:           return TC_Success;
     579                 :         }
     580                 :       }
                       14: branch 0 taken
                        0: branch 1 not taken
                        6: branch 4 taken
                        8: branch 5 taken
                        6: branch 6 taken
                        8: branch 7 taken
     581               14:       else if (CStyle && DestType->isObjCObjectPointerType()) {
     582                 :         // allow c-style cast of objective-c pointers as they are pervasive.
     583                6:         Kind = CastExpr::CK_AnyPointerToObjCPointerCast;
     584                6:         return TC_Success;
     585                 :       }
                        8: branch 0 taken
                        0: branch 1 not taken
                        4: branch 4 taken
                        4: branch 5 taken
                        4: branch 6 taken
                        4: branch 7 taken
     586                8:       else if (CStyle && DestType->isBlockPointerType()) {
     587                 :         // allow c-style cast of void * to block pointers.
     588                4:         Kind = CastExpr::CK_AnyPointerToBlockPointerCast;
     589                4:         return TC_Success;
     590                 :       }
     591                 :     }
     592                 :   }
     593                 : 
     594                 :   // We tried everything. Everything! Nothing works! :-(
     595              529:   return TC_NotApplicable;
     596                 : }
     597                 : 
     598                 : /// Tests whether a conversion according to N2844 is valid.
     599                 : TryCastResult
     600                 : TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
     601             1350:                       unsigned &msg) {
     602                 :   // N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
     603                 :   //   reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
     604             1350:   const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
                     1346: branch 0 taken
                        4: branch 1 taken
     605             1350:   if (!R)
     606             1346:     return TC_NotApplicable;
     607                 : 
                        0: branch 1 not taken
                        4: branch 2 taken
     608                4:   if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid)
     609                0:     return TC_NotApplicable;
     610                 : 
     611                 :   // Because we try the reference downcast before this function, from now on
     612                 :   // this is the only cast possibility, so we issue an error if we fail now.
     613                 :   // FIXME: Should allow casting away constness if CStyle.
     614                 :   bool DerivedToBase;
                        1: branch 4 taken
                        3: branch 5 taken
     615                4:   if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
     616                 :                                         SrcExpr->getType(), R->getPointeeType(),
     617                 :                                         DerivedToBase) <
     618                 :         Sema::Ref_Compatible_With_Added_Qualification) {
     619                1:     msg = diag::err_bad_lvalue_to_rvalue_cast;
     620                1:     return TC_Failed;
     621                 :   }
     622                 : 
     623                 :   // FIXME: Similar to CheckReferenceInit, we actually need more AST annotation
     624                 :   // than nothing.
     625                3:   return TC_Success;
     626                 : }
     627                 : 
     628                 : /// Tests whether a conversion according to C++ 5.2.9p5 is valid.
     629                 : TryCastResult
     630                 : TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
     631                 :                            bool CStyle, const SourceRange &OpRange,
     632             1374:                            unsigned &msg, CastExpr::CastKind &Kind) {
     633                 :   // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
     634                 :   //   cast to type "reference to cv2 D", where D is a class derived from B,
     635                 :   //   if a valid standard conversion from "pointer to D" to "pointer to B"
     636                 :   //   exists, cv2 >= cv1, and B is not a virtual base class of D.
     637                 :   // In addition, DR54 clarifies that the base must be accessible in the
     638                 :   // current context. Although the wording of DR54 only applies to the pointer
     639                 :   // variant of this rule, the intent is clearly for it to apply to the this
     640                 :   // conversion as well.
     641                 : 
     642             1374:   const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
                     1323: branch 0 taken
                       51: branch 1 taken
     643             1374:   if (!DestReference) {
     644             1323:     return TC_NotApplicable;
     645                 :   }
     646               51:   bool RValueRef = DestReference->isRValueReferenceType();
                       47: branch 0 taken
                        4: branch 1 taken
                        2: branch 3 taken
                       45: branch 4 taken
                        2: branch 5 taken
                       49: branch 6 taken
     647               51:   if (!RValueRef && SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
     648                 :     // We know the left side is an lvalue reference, so we can suggest a reason.
     649                2:     msg = diag::err_bad_cxx_cast_rvalue;
     650                2:     return TC_NotApplicable;
     651                 :   }
     652                 : 
     653               49:   QualType DestPointee = DestReference->getPointeeType();
     654                 : 
     655                 :   return TryStaticDowncast(Self, 
     656                 :                            Self.Context.getCanonicalType(SrcExpr->getType()), 
     657                 :                            Self.Context.getCanonicalType(DestPointee), CStyle,
     658               49:                            OpRange, SrcExpr->getType(), DestType, msg, Kind);
     659                 : }
     660                 : 
     661                 : /// Tests whether a conversion according to C++ 5.2.9p8 is valid.
     662                 : TryCastResult
     663                 : TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
     664                 :                          bool CStyle, const SourceRange &OpRange,
     665              592:                          unsigned &msg, CastExpr::CastKind &Kind) {
     666                 :   // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
     667                 :   //   type, can be converted to an rvalue of type "pointer to cv2 D", where D
     668                 :   //   is a class derived from B, if a valid standard conversion from "pointer
     669                 :   //   to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
     670                 :   //   class of D.
     671                 :   // In addition, DR54 clarifies that the base must be accessible in the
     672                 :   // current context.
     673                 : 
     674              592:   const PointerType *DestPointer = DestType->getAs<PointerType>();
                      463: branch 0 taken
                      129: branch 1 taken
     675              592:   if (!DestPointer) {
     676              463:     return TC_NotApplicable;
     677                 :   }
     678                 : 
     679              129:   const PointerType *SrcPointer = SrcType->getAs<PointerType>();
                       19: branch 0 taken
                      110: branch 1 taken
     680              129:   if (!SrcPointer) {
     681               19:     msg = diag::err_bad_static_cast_pointer_nonpointer;
     682               19:     return TC_NotApplicable;
     683                 :   }
     684                 : 
     685                 :   return TryStaticDowncast(Self, 
     686                 :                    Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
     687                 :                   Self.Context.getCanonicalType(DestPointer->getPointeeType()), 
     688              110:                            CStyle, OpRange, SrcType, DestType, msg, Kind);
     689                 : }
     690                 : 
     691                 : /// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
     692                 : /// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
     693                 : /// DestType is possible and allowed.
     694                 : TryCastResult
     695                 : TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
     696                 :                   bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
     697                 :                   QualType OrigDestType, unsigned &msg, 
     698              159:                   CastExpr::CastKind &Kind) {
     699                 :   // We can only work with complete types. But don't complain if it doesn't work
                      131: branch 7 taken
                       28: branch 8 taken
                        6: branch 16 taken
                      125: branch 17 taken
                      131: branch 18 taken
                       28: branch 19 taken
                      131: branch 21 taken
                       28: branch 22 taken
                      131: branch 24 taken
                       28: branch 25 taken
                      159: branch 27 taken
                        0: branch 28 not taken
                      159: branch 30 taken
                        0: branch 31 not taken
                      159: branch 33 taken
                        0: branch 34 not taken
                       34: branch 36 taken
                      125: branch 37 taken
     700              159:   if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, PDiag(0)) ||
     701                 :       Self.RequireCompleteType(OpRange.getBegin(), DestType, PDiag(0)))
     702               34:     return TC_NotApplicable;
     703                 : 
     704                 :   // Downcast can only happen in class hierarchies, so we need classes.
                       64: branch 4 taken
                       61: branch 5 taken
                        3: branch 10 taken
                       61: branch 11 taken
                       64: branch 12 taken
                       61: branch 13 taken
     705              125:   if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
     706               64:     return TC_NotApplicable;
     707                 :   }
     708                 : 
     709                 :   CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/!CStyle,
     710               61:                      /*DetectVirtual=*/true);
                       13: branch 3 taken
                       48: branch 4 taken
     711               61:   if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
     712               13:     return TC_NotApplicable;
     713                 :   }
     714                 : 
     715                 :   // Target type does derive from source type. Now we're serious. If an error
     716                 :   // appears now, it's not ignored.
     717                 :   // This may not be entirely in line with the standard. Take for example:
     718                 :   // struct A {};
     719                 :   // struct B : virtual A {
     720                 :   //   B(A&);
     721                 :   // };
     722                 :   //
     723                 :   // void f()
     724                 :   // {
     725                 :   //   (void)static_cast<const B&>(*((A*)0));
     726                 :   // }
     727                 :   // As far as the standard is concerned, p5 does not apply (A is virtual), so
     728                 :   // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
     729                 :   // However, both GCC and Comeau reject this example, and accepting it would
     730                 :   // mean more complex code if we're to preserve the nice error message.
     731                 :   // FIXME: Being 100% compliant here would be nice to have.
     732                 : 
     733                 :   // Must preserve cv, as always, unless we're in C-style mode.
                       20: branch 0 taken
                       28: branch 1 taken
                        2: branch 3 taken
                       18: branch 4 taken
                        2: branch 5 taken
                       46: branch 6 taken
     734               48:   if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
     735                2:     msg = diag::err_bad_cxx_cast_const_away;
     736                2:     return TC_Failed;
     737                 :   }
     738                 : 
                        6: branch 3 taken
                       40: branch 4 taken
     739               46:   if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
     740                 :     // This code is analoguous to that in CheckDerivedToBaseConversion, except
     741                 :     // that it builds the paths in reverse order.
     742                 :     // To sum up: record all paths to the base and build a nice string from
     743                 :     // them. Use it to spice up the error message.
                        4: branch 1 taken
                        2: branch 2 taken
     744                6:     if (!Paths.isRecordingPaths()) {
     745                4:       Paths.clear();
     746                4:       Paths.setRecordingPaths(true);
     747                4:       Self.IsDerivedFrom(DestType, SrcType, Paths);
     748                 :     }
     749                6:     std::string PathDisplayStr;
     750                6:     std::set<unsigned> DisplayedPaths;
                       12: branch 4 taken
                        6: branch 5 taken
     751               18:     for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
     752                 :          PI != PE; ++PI) {
                       12: branch 3 taken
                        0: branch 4 not taken
     753               12:       if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
     754                 :         // We haven't displayed a path to this particular base
     755                 :         // class subobject yet.
     756               12:         PathDisplayStr += "\n    ";
                       36: branch 4 taken
                       12: branch 5 taken
     757               60:         for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
     758               12:                                                  EE = PI->rend();
     759                 :              EI != EE; ++EI)
     760               36:           PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
     761               12:         PathDisplayStr += QualType(DestType).getAsString();
     762                 :       }
     763                 :     }
     764                 : 
     765                 :     Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
     766                 :       << QualType(SrcType).getUnqualifiedType() 
     767                 :       << QualType(DestType).getUnqualifiedType()
     768                6:       << PathDisplayStr << OpRange;
     769                6:     msg = 0;
     770                6:     return TC_Failed;
     771                 :   }
     772                 : 
                       12: branch 1 taken
                       28: branch 2 taken
     773               40:   if (Paths.getDetectedVirtual() != 0) {
     774               12:     QualType VirtualBase(Paths.getDetectedVirtual(), 0);
     775                 :     Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
     776               12:       << OrigSrcType << OrigDestType << VirtualBase << OpRange;
     777               12:     msg = 0;
     778               12:     return TC_Failed;
     779                 :   }
     780                 : 
                       12: branch 0 taken
                       16: branch 1 taken
                        2: branch 7 taken
                       10: branch 8 taken
                        2: branch 9 taken
                       26: branch 10 taken
     781               28:   if (!CStyle && Self.CheckBaseClassAccess(DestType, SrcType,
     782                 :                           diag::err_downcast_from_inaccessible_base, Paths,
     783                 :                           OpRange.getBegin(), DeclarationName())) {
     784                2:     msg = 0;
     785                2:     return TC_Failed;
     786                 :   }
     787                 : 
     788               26:   Kind = CastExpr::CK_BaseToDerived;
     789               26:   return TC_Success;
     790                 : }
     791                 : 
     792                 : /// TryStaticMemberPointerUpcast - Tests whether a conversion according to
     793                 : /// C++ 5.2.9p9 is valid:
     794                 : ///
     795                 : ///   An rvalue of type "pointer to member of D of type cv1 T" can be
     796                 : ///   converted to an rvalue of type "pointer to member of B of type cv2 T",
     797                 : ///   where B is a base class of D [...].
     798                 : ///
     799                 : TryCastResult
     800                 : TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType, QualType DestType,
     801                 :                              bool CStyle, const SourceRange &OpRange,
     802              568:                              unsigned &msg, CastExpr::CastKind &Kind) {
     803              568:   const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
                      547: branch 0 taken
                       21: branch 1 taken
     804              568:   if (!DestMemPtr)
     805              547:     return TC_NotApplicable;
     806               21:   const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
                        0: branch 0 not taken
                       21: branch 1 taken
     807               21:   if (!SrcMemPtr) {
     808                0:     msg = diag::err_bad_static_cast_member_pointer_nonmp;
     809                0:     return TC_NotApplicable;
     810                 :   }
     811                 : 
     812                 :   // T == T, modulo cv
                       10: branch 3 taken
                       11: branch 4 taken
     813               21:   if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
     814                 :                                            DestMemPtr->getPointeeType()))
     815               10:     return TC_NotApplicable;
     816                 : 
     817                 :   // B base of D
     818               11:   QualType SrcClass(SrcMemPtr->getClass(), 0);
     819               11:   QualType DestClass(DestMemPtr->getClass(), 0);
     820                 :   CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/!CStyle,
     821               11:                   /*DetectVirtual=*/true);
                        0: branch 1 not taken
                       11: branch 2 taken
     822               11:   if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
     823                0:     return TC_NotApplicable;
     824                 :   }
     825                 : 
     826                 :   // B is a base of D. But is it an allowed base? If not, it's a hard error.
                        3: branch 1 taken
                        8: branch 2 taken
     827               11:   if (Paths.isAmbiguous(DestClass)) {
     828                3:     Paths.clear();
     829                3:     Paths.setRecordingPaths(true);
     830                3:     bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
                        0: branch 0 not taken
                        3: branch 1 taken
     831                3:     assert(StillOkay);
     832                3:     StillOkay = StillOkay;
     833                3:     std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
     834                 :     Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
     835                3:       << 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
     836                3:     msg = 0;
     837                3:     return TC_Failed;
     838                 :   }
     839                 : 
                        3: branch 1 taken
                        5: branch 2 taken
     840                8:   if (const RecordType *VBase = Paths.getDetectedVirtual()) {
     841                 :     Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
     842                3:       << SrcClass << DestClass << QualType(VBase, 0) << OpRange;
     843                3:     msg = 0;
     844                3:     return TC_Failed;
     845                 :   }
     846                 : 
                        3: branch 0 taken
                        2: branch 1 taken
                        0: branch 5 not taken
                        3: branch 6 taken
                        0: branch 7 not taken
                        5: branch 8 taken
     847                5:   if (!CStyle && Self.CheckBaseClassAccess(DestType, SrcType,
     848                 :                           diag::err_downcast_from_inaccessible_base, Paths,
     849                 :                           OpRange.getBegin(), DeclarationName())) {
     850                0:     msg = 0;
     851                0:     return TC_Failed;
     852                 :   }
     853                 : 
     854                5:   Kind = CastExpr::CK_DerivedToBaseMemberPointer;
     855                5:   return TC_Success;
     856                 : }
     857                 : 
     858                 : /// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
     859                 : /// is valid:
     860                 : ///
     861                 : ///   An expression e can be explicitly converted to a type T using a
     862                 : ///   @c static_cast if the declaration "T t(e);" is well-formed [...].
     863                 : TryCastResult
     864                 : TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
     865                 :                       bool CStyle, const SourceRange &OpRange, unsigned &msg,
     866                 :                       CastExpr::CastKind &Kind, 
     867             1346:                       CXXMethodDecl *&ConversionDecl) {
                       60: branch 2 taken
                     1286: branch 3 taken
     868             1346:   if (DestType->isRecordType()) {
                        1: branch 9 taken
                       59: branch 10 taken
     869               60:     if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
     870                 :                                  diag::err_bad_dynamic_cast_incomplete)) {
     871                1:       msg = 0;
     872                1:       return TC_Failed;
     873                 :     }
     874                 :   }
     875                 : 
                       23: branch 2 taken
                     1322: branch 3 taken
     876             1345:   if (DestType->isReferenceType()) {
     877                 :     // All reference bindings insert implicit casts above that do the actual
     878                 :     // casting.
     879               23:     Kind = CastExpr::CK_NoOp;
     880                 : 
     881                 :     // At this point of CheckStaticCast, if the destination is a reference,
     882                 :     // this has to work. There is no other way that works.
     883                 :     // On the other hand, if we're checking a C-style cast, we've still got
     884                 :     // the reinterpret_cast way. So in C-style mode, we first try the call
     885                 :     // with an ICS to suppress errors.
                       10: branch 0 taken
                       13: branch 1 taken
     886               23:     if (CStyle) {
     887               10:       ImplicitConversionSequence ICS;
                        4: branch 2 taken
                        6: branch 3 taken
     888               10:       if(Self.CheckReferenceInit(SrcExpr, DestType, OpRange.getBegin(),
     889                 :                                  /*SuppressUserConversions=*/false,
     890                 :                                  /*AllowExplicit=*/false, /*ForceRValue=*/false,
     891                 :                                  &ICS))
                        6: branch 1 taken
                        4: branch 2 taken
     892                4:         return TC_NotApplicable;
     893                 :     }
     894                 :     // Now we're committed either way.
                       17: branch 2 taken
                        2: branch 3 taken
     895               19:     if(!Self.CheckReferenceInit(SrcExpr, DestType, OpRange.getBegin(),
     896                 :                                 /*SuppressUserConversions=*/false,
     897                 :                                 /*AllowExplicit=*/false,
     898                 :                                 /*ForceRValue=*/false, 0,
     899                 :                                 /*IgnoreBaseAccess=*/CStyle))
     900               17:       return TC_Success;
     901                 : 
     902                 :     // We already got an error message.
     903                2:     msg = 0;
     904                2:     return TC_Failed;
     905                 :   }
     906                 : 
                       59: branch 2 taken
                     1263: branch 3 taken
     907             1322:   if (DestType->isRecordType()) {
                       54: branch 0 taken
                        5: branch 1 taken
     908               59:     if (CXXConstructorDecl *Constructor
     909                 :           = Self.TryInitializationByConstructor(DestType, &SrcExpr, 1,
     910                 :                                                 OpRange.getBegin(),
     911                 :               InitializationKind::CreateDirect(OpRange.getBegin(),
     912                 :                                                OpRange.getBegin(), 
     913               59:                                                OpRange.getEnd()))) {
     914               54:       ConversionDecl = Constructor;
     915               54:       Kind = CastExpr::CK_ConstructorConversion;
     916               54:       return TC_Success;
     917                 :     }
     918                 :     
     919                5:     return TC_NotApplicable;
     920                 :   }
     921                 : 
     922                 :   // FIXME: To get a proper error from invalid conversions here, we need to
     923                 :   // reimplement more of this.
     924                 :   // FIXME: This does not actually perform the conversion, and thus does not
     925                 :   // check for ambiguity or access.
     926                 :   ImplicitConversionSequence ICS =
     927                 :     Self.TryImplicitConversion(SrcExpr, DestType,
     928                 :                                /*SuppressUserConversions=*/false,
     929                 :                                /*AllowExplicit=*/true,
     930                 :                                /*ForceRValue=*/false,
     931                 :                                /*InOverloadResolution=*/false,
     932             1263:                                /*one of user provided casts*/true);
     933                 : 
                      597: branch 1 taken
                      666: branch 2 taken
     934             1263:   if (ICS.isBad())
     935              597:     return TC_NotApplicable;
     936                 : 
     937                 :   // The conversion is possible, so commit to it.
     938              666:   Kind = CastExpr::CK_NoOp;
     939              666:   msg = 0;
     940                 :   return Self.PerformImplicitConversion(SrcExpr, DestType, ICS, Sema::AA_Casting,
     941                 :                                         /*IgnoreBaseAccess*/CStyle) ?
                        2: branch 1 taken
                      664: branch 2 taken
     942              666:       TC_Failed : TC_Success;
     943                 : }
     944                 : 
     945                 : /// TryConstCast - See if a const_cast from source to destination is allowed,
     946                 : /// and perform it if it is.
     947                 : static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
     948             1319:                                   bool CStyle, unsigned &msg) {
     949             1319:   DestType = Self.Context.getCanonicalType(DestType);
     950             1319:   QualType SrcType = SrcExpr->getType();
                       34: branch 0 taken
                     1285: branch 1 taken
     951             1319:   if (const LValueReferenceType *DestTypeTmp =
     952             1319:         DestType->getAs<LValueReferenceType>()) {
                        3: branch 1 taken
                       31: branch 2 taken
     953               34:     if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
     954                 :       // Cannot const_cast non-lvalue to lvalue reference type. But if this
     955                 :       // is C-style, static_cast might find a way, so we simply suggest a
     956                 :       // message and tell the parent to keep searching.
     957                3:       msg = diag::err_bad_cxx_cast_rvalue;
     958                3:       return TC_NotApplicable;
     959                 :     }
     960                 : 
     961                 :     // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
     962                 :     //   [...] if a pointer to T1 can be [cast] to the type pointer to T2.
     963               31:     DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
     964               31:     SrcType = Self.Context.getPointerType(SrcType);
     965                 :   }
     966                 : 
     967                 :   // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
     968                 :   //   the rules for const_cast are the same as those used for pointers.
     969                 : 
                      928: branch 2 taken
                      388: branch 3 taken
                      887: branch 6 taken
                       41: branch 7 taken
                      887: branch 8 taken
                      429: branch 9 taken
     970             1316:   if (!DestType->isPointerType() && !DestType->isMemberPointerType()) {
     971                 :     // Cannot cast to non-pointer, non-reference type. Note that, if DestType
     972                 :     // was a reference type, we converted it to a pointer above.
     973                 :     // The status of rvalue references isn't entirely clear, but it looks like
     974                 :     // conversion to them is simply invalid.
     975                 :     // C++ 5.2.11p3: For two pointer types [...]
                        1: branch 0 taken
                      886: branch 1 taken
     976              887:     if (!CStyle)
     977                1:       msg = diag::err_bad_const_cast_dest;
     978              887:     return TC_NotApplicable;
     979                 :   }
                      407: branch 2 taken
                       22: branch 3 taken
                       12: branch 6 taken
                      395: branch 7 taken
                       34: branch 8 taken
                      395: branch 9 taken
     980              429:   if (DestType->isFunctionPointerType() ||
     981                 :       DestType->isMemberFunctionPointerType()) {
     982                 :     // Cannot cast direct function pointers.
     983                 :     // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
     984                 :     // T is the ultimate pointee of source and target type.
                        2: branch 0 taken
                       32: branch 1 taken
     985               34:     if (!CStyle)
     986                2:       msg = diag::err_bad_const_cast_dest;
     987               34:     return TC_NotApplicable;
     988                 :   }
     989              395:   SrcType = Self.Context.getCanonicalType(SrcType);
     990                 : 
     991                 :   // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
     992                 :   // completely equal.
     993                 :   // FIXME: const_cast should probably not be able to convert between pointers
     994                 :   // to different address spaces.
     995                 :   // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
     996                 :   // in multi-level pointers may change, but the level count must be the same,
     997                 :   // as must be the final pointee type.
                      540: branch 1 taken
                       41: branch 2 taken
                      186: branch 4 taken
                      354: branch 5 taken
                      186: branch 6 taken
                      395: branch 7 taken
     998              976:   while (SrcType != DestType &&
     999                 :          Self.UnwrapSimilarPointerTypes(SrcType, DestType)) {
    1000              186:     Qualifiers Quals;
    1001              186:     SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
    1002              186:     DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
    1003                 :   }
    1004                 : 
    1005                 :   // Since we're dealing in canonical types, the remainder must be the same.
                      354: branch 1 taken
                       41: branch 2 taken
    1006              395:   if (SrcType != DestType)
    1007              354:     return TC_NotApplicable;
    1008                 : 
    1009               41:   return TC_Success;
    1010                 : }
    1011                 : 
    1012                 : static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
    1013                 :                                         QualType DestType, bool CStyle,
    1014                 :                                         const SourceRange &OpRange,
    1015                 :                                         unsigned &msg,
    1016              585:                                         CastExpr::CastKind &Kind) {
    1017              585:   DestType = Self.Context.getCanonicalType(DestType);
    1018              585:   QualType SrcType = SrcExpr->getType();
                        8: branch 2 taken
                      577: branch 3 taken
    1019              585:   if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
    1020                8:     bool LValue = DestTypeTmp->isLValueReferenceType();
                        8: branch 0 taken
                        0: branch 1 not taken
                        3: branch 3 taken
                        5: branch 4 taken
                        3: branch 5 taken
                        5: branch 6 taken
    1021                8:     if (LValue && SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
    1022                 :       // Cannot cast non-lvalue to reference type. See the similar comment in
    1023                 :       // const_cast.
    1024                3:       msg = diag::err_bad_cxx_cast_rvalue;
    1025                3:       return TC_NotApplicable;
    1026                 :     }
    1027                 : 
    1028                 :     // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
    1029                 :     //   same effect as the conversion *reinterpret_cast<T*>(&x) with the
    1030                 :     //   built-in & and * operators.
    1031                 :     // This code does this transformation for the checked types.
    1032                5:     DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
    1033                5:     SrcType = Self.Context.getPointerType(SrcType);
    1034                 :   }
    1035                 : 
    1036                 :   // Canonicalize source for comparison.
    1037              582:   SrcType = Self.Context.getCanonicalType(SrcType);
    1038                 : 
    1039              582:   const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
    1040              582:                           *SrcMemPtr = SrcType->getAs<MemberPointerType>();
                       18: branch 0 taken
                      564: branch 1 taken
                       16: branch 2 taken
                        2: branch 3 taken
    1041              582:   if (DestMemPtr && SrcMemPtr) {
    1042                 :     // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
    1043                 :     //   can be explicitly converted to an rvalue of type "pointer to member
    1044                 :     //   of Y of type T2" if T1 and T2 are both function types or both object
    1045                 :     //   types.
                        6: branch 6 taken
                       10: branch 7 taken
    1046               16:     if (DestMemPtr->getPointeeType()->isFunctionType() !=
    1047                 :         SrcMemPtr->getPointeeType()->isFunctionType())
    1048                6:       return TC_NotApplicable;
    1049                 : 
    1050                 :     // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
    1051                 :     //   constness.
    1052                 :     // A reinterpret_cast followed by a const_cast can, though, so in C-style,
    1053                 :     // we accept it.
                        4: branch 0 taken
                        6: branch 1 taken
                        1: branch 3 taken
                        3: branch 4 taken
                        1: branch 5 taken
                        9: branch 6 taken
    1054               10:     if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
    1055                1:       msg = diag::err_bad_cxx_cast_const_away;
    1056                1:       return TC_Failed;
    1057                 :     }
    1058                 : 
    1059                 :     // A valid member pointer cast.
    1060                9:     Kind = CastExpr::CK_BitCast;
    1061                9:     return TC_Success;
    1062                 :   }
    1063                 : 
    1064                 :   // See below for the enumeral issue.
                        2: branch 2 taken
                      564: branch 3 taken
                        2: branch 6 taken
                        0: branch 7 not taken
                        2: branch 10 taken
                        0: branch 11 not taken
                        2: branch 12 taken
                      564: branch 13 taken
    1065              566:   if (SrcType->isNullPtrType() && DestType->isIntegralType() &&
    1066                 :       !DestType->isEnumeralType()) {
    1067                 :     // C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
    1068                 :     //   type large enough to hold it. A value of std::nullptr_t can be
    1069                 :     //   converted to an integral type; the conversion has the same meaning
    1070                 :     //   and validity as a conversion of (void*)0 to the integral type.
                        0: branch 2 not taken
                        2: branch 3 taken
    1071                2:     if (Self.Context.getTypeSize(SrcType) >
    1072                 :         Self.Context.getTypeSize(DestType)) {
    1073                0:       msg = diag::err_bad_reinterpret_cast_small_int;
    1074                0:       return TC_Failed;
    1075                 :     }
    1076                2:     Kind = CastExpr::CK_PointerToIntegral;
    1077                2:     return TC_Success;
    1078                 :   }
    1079                 : 
    1080              564:   bool destIsVector = DestType->isVectorType();
    1081              564:   bool srcIsVector = SrcType->isVectorType();
                      164: branch 0 taken
                      400: branch 1 taken
                        7: branch 2 taken
                      157: branch 3 taken
    1082              564:   if (srcIsVector || destIsVector) {
                        5: branch 2 taken
                      402: branch 3 taken
                        5: branch 6 taken
                        0: branch 7 not taken
    1083              407:     bool srcIsScalar = SrcType->isIntegralType() && !SrcType->isEnumeralType();
    1084                 :     bool destIsScalar = 
                       12: branch 2 taken
                      395: branch 3 taken
                       12: branch 6 taken
                        0: branch 7 not taken
    1085              407:       DestType->isIntegralType() && !DestType->isEnumeralType();
    1086                 :     
    1087                 :     // Check if this is a cast between a vector and something else.
                        5: branch 0 taken
                      402: branch 1 taken
                        0: branch 2 not taken
                        5: branch 3 taken
                      400: branch 4 taken
                        2: branch 5 taken
                      388: branch 6 taken
                       12: branch 7 taken
                      388: branch 8 taken
                        2: branch 9 taken
                        2: branch 10 taken
                      386: branch 11 taken
    1088              407:     if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
    1089                 :         !(srcIsVector && destIsVector))
    1090                4:       return TC_NotApplicable;
    1091                 : 
    1092                 :     // If both types have the same size, we can successfully cast.
                      396: branch 2 taken
                        7: branch 3 taken
    1093              403:     if (Self.Context.getTypeSize(SrcType)
    1094                 :           == Self.Context.getTypeSize(DestType)) {
    1095              396:       Kind = CastExpr::CK_BitCast;
    1096              396:       return TC_Success;
    1097                 :     }
    1098                 :     
                        2: branch 0 taken
                        5: branch 1 taken
    1099                7:     if (destIsScalar)
    1100                2:       msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
                        1: branch 0 taken
                        4: branch 1 taken
    1101                5:     else if (srcIsScalar)
    1102                1:       msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
    1103                 :     else
    1104                4:       msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
    1105                 :     
    1106                7:     return TC_Failed;
    1107                 :   }
    1108                 :   
    1109              157:   bool destIsPtr = DestType->isAnyPointerType();
    1110              157:   bool srcIsPtr = SrcType->isAnyPointerType();
                       32: branch 0 taken
                      125: branch 1 taken
                       15: branch 2 taken
                       17: branch 3 taken
    1111              157:   if (!destIsPtr && !srcIsPtr) {
    1112                 :     // Except for std::nullptr_t->integer and lvalue->reference, which are
    1113                 :     // handled above, at least one of the two arguments must be a pointer.
    1114               15:     return TC_NotApplicable;
    1115                 :   }
    1116                 : 
                        2: branch 1 taken
                      140: branch 2 taken
    1117              142:   if (SrcType == DestType) {
    1118                 :     // C++ 5.2.10p2 has a note that mentions that, subject to all other
    1119                 :     // restrictions, a cast to the same type is allowed. The intent is not
    1120                 :     // entirely clear here, since all other paragraphs explicitly forbid casts
    1121                 :     // to the same type. However, the behavior of compilers is pretty consistent
    1122                 :     // on this point: allow same-type conversion if the involved types are
    1123                 :     // pointers, disallow otherwise.
    1124                2:     Kind = CastExpr::CK_NoOp;
    1125                2:     return TC_Success;
    1126                 :   }
    1127                 : 
    1128                 :   // Note: Clang treats enumeration types as integral types. If this is ever
    1129                 :   // changed for C++, the additional check here will be redundant.
                       16: branch 2 taken
                      124: branch 3 taken
                       14: branch 6 taken
                        2: branch 7 taken
                       14: branch 8 taken
                      126: branch 9 taken
    1130              140:   if (DestType->isIntegralType() && !DestType->isEnumeralType()) {
                        0: branch 0 not taken
                       14: branch 1 taken
    1131               14:     assert(srcIsPtr && "One type must be a pointer");
    1132                 :     // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
    1133                 :     //   type large enough to hold it.
                        3: branch 2 taken
                       11: branch 3 taken
    1134               14:     if (Self.Context.getTypeSize(SrcType) >
    1135                 :         Self.Context.getTypeSize(DestType)) {
    1136                3:       msg = diag::err_bad_reinterpret_cast_small_int;
    1137                3:       return TC_Failed;
    1138                 :     }
    1139               11:     Kind = CastExpr::CK_PointerToIntegral;
    1140               11:     return TC_Success;
    1141                 :   }
    1142                 : 
                      100: branch 2 taken
                       26: branch 3 taken
                        0: branch 6 not taken
                      100: branch 7 taken
                       26: branch 8 taken
                      100: branch 9 taken
    1143              126:   if (SrcType->isIntegralType() || SrcType->isEnumeralType()) {
                        0: branch 0 not taken
                       26: branch 1 taken
    1144               26:     assert(destIsPtr && "One type must be a pointer");
    1145                 :     // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
    1146                 :     //   converted to a pointer.
    1147               26:     Kind = CastExpr::CK_IntegralToPointer;
    1148               26:     return TC_Success;
    1149                 :   }
    1150                 : 
                       97: branch 0 taken
                        3: branch 1 taken
                        2: branch 2 taken
                       95: branch 3 taken
    1151              100:   if (!destIsPtr || !srcIsPtr) {
    1152                 :     // With the valid non-pointer conversions out of the way, we can be even
    1153                 :     // more stringent.
    1154                5:     return TC_NotApplicable;
    1155                 :   }
    1156                 : 
    1157                 :   // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
    1158                 :   // The C-style cast operator can.
                       23: branch 0 taken
                       72: branch 1 taken
                        3: branch 3 taken
                       20: branch 4 taken
                        3: branch 5 taken
                       92: branch 6 taken
    1159               95:   if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
    1160                3:     msg = diag::err_bad_cxx_cast_const_away;
    1161                3:     return TC_Failed;
    1162                 :   }
                       72: branch 0 taken
                       20: branch 1 taken
                        3: branch 4 taken
                       69: branch 5 taken
                        3: branch 6 taken
                       89: branch 7 taken
    1163               92:   if (CStyle && DestType->isObjCObjectPointerType()) {
    1164                3:     Kind = CastExpr::CK_AnyPointerToObjCPointerCast;
    1165                3:     return TC_Success;
    1166                 :   }
    1167                 :   
    1168                 :   // Not casting away constness, so the only remaining check is for compatible
    1169                 :   // pointer categories.
    1170               89:   Kind = CastExpr::CK_BitCast;
    1171                 : 
                       10: branch 2 taken
                       79: branch 3 taken
    1172               89:   if (SrcType->isFunctionPointerType()) {
                        6: branch 2 taken
                        4: branch 3 taken
    1173               10:     if (DestType->isFunctionPointerType()) {
    1174                 :       // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
    1175                 :       // a pointer to a function of a different type.
    1176                6:       return TC_Success;
    1177                 :     }
    1178                 : 
    1179                 :     // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
    1180                 :     //   an object type or vice versa is conditionally-supported.
    1181                 :     // Compilers support it in C++03 too, though, because it's necessary for
    1182                 :     // casting the return value of dlsym() and GetProcAddress().
    1183                 :     // FIXME: Conditionally-supported behavior should be configurable in the
    1184                 :     // TargetInfo or similar.
                        4: branch 1 taken
                        0: branch 2 not taken
    1185                4:     if (!Self.getLangOptions().CPlusPlus0x)
    1186                4:       Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
    1187                4:     return TC_Success;
    1188                 :   }
    1189                 : 
                        5: branch 2 taken
                       74: branch 3 taken
    1190               79:   if (DestType->isFunctionPointerType()) {
    1191                 :     // See above.
                        5: branch 1 taken
                        0: branch 2 not taken
    1192                5:     if (!Self.getLangOptions().CPlusPlus0x)
    1193                5:       Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
    1194                5:     return TC_Success;
    1195                 :   }
    1196                 : 
    1197                 :   // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
    1198                 :   //   a pointer to an object of different type.
    1199                 :   // Void pointers are not specified, but supported by every compiler out there.
    1200                 :   // So we finish by allowing everything that remains - it's got to be two
    1201                 :   // object pointers.
    1202               74:   return TC_Success;
    1203                 : }
    1204                 : 
    1205                 : bool Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, Expr *&CastExpr,
    1206                 :                               CastExpr::CastKind &Kind, bool FunctionalStyle,
    1207             1763:                               CXXMethodDecl *&ConversionDecl) {
    1208                 :   // This test is outside everything else because it's the only case where
    1209                 :   // a non-lvalue-reference target type does not lead to decay.
    1210                 :   // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
                      457: branch 2 taken
                     1306: branch 3 taken
    1211             1763:   if (CastTy->isVoidType()) {
    1212              457:     Kind = CastExpr::CK_ToVoid;
    1213              457:     return false;
    1214                 :   }
    1215                 : 
    1216                 :   // If the type is dependent, we won't do any other semantic analysis now.
                     1299: branch 2 taken
                        7: branch 3 taken
                        0: branch 5 not taken
                     1299: branch 6 taken
                        7: branch 7 taken
                     1299: branch 8 taken
    1217             1306:   if (CastTy->isDependentType() || CastExpr->isTypeDependent())
    1218                7:     return false;
    1219                 : 
                     1268: branch 2 taken
                       31: branch 3 taken
                     1219: branch 6 taken
                       49: branch 7 taken
                     1219: branch 8 taken
                       80: branch 9 taken
    1220             1299:   if (!CastTy->isLValueReferenceType() && !CastTy->isRecordType())
    1221             1219:     DefaultFunctionArrayLvalueConversion(CastExpr);
    1222                 : 
    1223                 :   // C++ [expr.cast]p5: The conversions performed by
    1224                 :   //   - a const_cast,
    1225                 :   //   - a static_cast,
    1226                 :   //   - a static_cast followed by a const_cast,
    1227                 :   //   - a reinterpret_cast, or
    1228                 :   //   - a reinterpret_cast followed by a const_cast,
    1229                 :   //   can be performed using the cast notation of explicit type conversion.
    1230                 :   //   [...] If a conversion can be interpreted in more than one of the ways
    1231                 :   //   listed above, the interpretation that appears first in the list is used,
    1232                 :   //   even if a cast resulting from that interpretation is ill-formed.
    1233                 :   // In plain language, this means trying a const_cast ...
    1234             1299:   unsigned msg = diag::err_bad_cxx_cast_generic;
    1235                 :   TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
    1236             1299:                                    msg);
                       29: branch 0 taken
                     1270: branch 1 taken
    1237             1299:   if (tcr == TC_Success)
    1238               29:     Kind = CastExpr::CK_NoOp;
    1239                 : 
                     1270: branch 0 taken
                       29: branch 1 taken
    1240             1299:   if (tcr == TC_NotApplicable) {
    1241                 :     // ... or if that is not possible, a static_cast, ignoring const, ...
    1242                 :     tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
    1243             1270:                         Kind, ConversionDecl);
                      520: branch 0 taken
                      750: branch 1 taken
    1244             1270:     if (tcr == TC_NotApplicable) {
    1245                 :       // ... and finally a reinterpret_cast, ignoring const.
    1246                 :       tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
    1247              520:                                Kind);
    1248                 :     }
    1249                 :   }
    1250                 : 
                       43: branch 0 taken
                     1256: branch 1 taken
                       27: branch 2 taken
                       16: branch 3 taken
    1251             1299:   if (tcr != TC_Success && msg != 0)
    1252                 :     Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
                        7: branch 1 taken
                       20: branch 2 taken
    1253               27:       << CastExpr->getType() << CastTy << R;
    1254                 : 
    1255             1299:   return tcr != TC_Success;
    1256                 : }

Generated: 2010-02-10 01:31 by zcov