2008-11-06 00:50:06 +03:00
|
|
|
//===--- SemaNamedCast.cpp - Semantic Analysis for Named Casts ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for C++ named casts.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2009-12-10 02:02:17 +03:00
|
|
|
#include "SemaInit.h"
|
2008-11-06 00:50:06 +03:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-10-06 21:59:45 +04:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-08-27 03:45:07 +04:00
|
|
|
#include "clang/Basic/PartialDiagnostic.h"
|
2008-11-06 00:50:06 +03:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-11-08 02:29:29 +03:00
|
|
|
#include <set>
|
2008-11-06 00:50:06 +03:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
enum TryCastResult {
|
|
|
|
TC_NotApplicable, ///< The cast method is not applicable.
|
|
|
|
TC_Success, ///< The cast method is appropriate and successful.
|
|
|
|
TC_Failed ///< The cast method is appropriate, but failed. A
|
|
|
|
///< diagnostic has been emitted.
|
|
|
|
};
|
|
|
|
|
|
|
|
enum CastType {
|
|
|
|
CT_Const, ///< const_cast
|
|
|
|
CT_Static, ///< static_cast
|
|
|
|
CT_Reinterpret, ///< reinterpret_cast
|
|
|
|
CT_Dynamic, ///< dynamic_cast
|
|
|
|
CT_CStyle, ///< (Type)expr
|
|
|
|
CT_Functional ///< Type(expr)
|
2008-11-08 16:00:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
|
|
|
const SourceRange &OpRange,
|
|
|
|
const SourceRange &DestRange);
|
|
|
|
static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
|
|
|
const SourceRange &OpRange,
|
2009-09-15 08:48:33 +04:00
|
|
|
const SourceRange &DestRange,
|
|
|
|
CastExpr::CastKind &Kind);
|
2008-11-08 16:00:26 +03:00
|
|
|
static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
2009-08-08 02:21:05 +04:00
|
|
|
const SourceRange &OpRange,
|
2010-04-17 02:09:46 +04:00
|
|
|
CastExpr::CastKind &Kind);
|
2008-11-08 16:00:26 +03:00
|
|
|
static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
|
|
|
const SourceRange &OpRange,
|
2009-09-09 19:08:12 +04:00
|
|
|
const SourceRange &DestRange,
|
2009-08-02 23:07:59 +04:00
|
|
|
CastExpr::CastKind &Kind);
|
2008-11-08 16:00:26 +03:00
|
|
|
|
|
|
|
static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType);
|
2009-07-25 19:41:38 +04:00
|
|
|
|
|
|
|
// The Try functions attempt a specific way of casting. If they succeed, they
|
|
|
|
// return TC_Success. If their way of casting is not appropriate for the given
|
|
|
|
// arguments, they return TC_NotApplicable and *may* set diag to a diagnostic
|
|
|
|
// to emit if no other way succeeds. If their way of casting is appropriate but
|
|
|
|
// fails, they return TC_Failed and *must* set diag; they can set it to 0 if
|
|
|
|
// they emit a specialized diagnostic.
|
|
|
|
// All diagnostics returned by these functions must expect the same three
|
|
|
|
// arguments:
|
|
|
|
// %0: Cast Type (a value from the CastType enumeration)
|
|
|
|
// %1: Source Type
|
|
|
|
// %2: Destination Type
|
|
|
|
static TryCastResult TryLValueToRValueCast(Sema &Self, Expr *SrcExpr,
|
|
|
|
QualType DestType, unsigned &msg);
|
|
|
|
static TryCastResult TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr,
|
|
|
|
QualType DestType, bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-11-12 19:53:16 +03:00
|
|
|
unsigned &msg,
|
|
|
|
CastExpr::CastKind &Kind);
|
2009-07-25 19:41:38 +04:00
|
|
|
static TryCastResult TryStaticPointerDowncast(Sema &Self, QualType SrcType,
|
|
|
|
QualType DestType, bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-11-12 19:53:16 +03:00
|
|
|
unsigned &msg,
|
|
|
|
CastExpr::CastKind &Kind);
|
2009-11-15 12:20:52 +03:00
|
|
|
static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
|
|
|
|
CanQualType DestType, bool CStyle,
|
2009-07-25 19:41:38 +04:00
|
|
|
const SourceRange &OpRange,
|
|
|
|
QualType OrigSrcType,
|
2009-11-12 19:53:16 +03:00
|
|
|
QualType OrigDestType, unsigned &msg,
|
|
|
|
CastExpr::CastKind &Kind);
|
2010-03-08 02:24:59 +03:00
|
|
|
static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr,
|
|
|
|
QualType SrcType,
|
2009-07-25 19:41:38 +04:00
|
|
|
QualType DestType,bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-10-30 03:46:35 +03:00
|
|
|
unsigned &msg,
|
|
|
|
CastExpr::CastKind &Kind);
|
2009-11-15 00:15:49 +03:00
|
|
|
static TryCastResult TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr,
|
2009-07-25 19:41:38 +04:00
|
|
|
QualType DestType, bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-08-26 22:55:36 +04:00
|
|
|
unsigned &msg,
|
2010-04-17 02:09:46 +04:00
|
|
|
CastExpr::CastKind &Kind);
|
2009-11-15 00:15:49 +03:00
|
|
|
static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
|
2009-07-25 19:41:38 +04:00
|
|
|
QualType DestType, bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-09-02 00:52:42 +04:00
|
|
|
unsigned &msg,
|
2010-04-17 02:09:46 +04:00
|
|
|
CastExpr::CastKind &Kind);
|
2009-07-25 19:41:38 +04:00
|
|
|
static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
|
|
|
|
bool CStyle, unsigned &msg);
|
|
|
|
static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
|
|
|
|
QualType DestType, bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-09-26 04:12:34 +04:00
|
|
|
unsigned &msg,
|
|
|
|
CastExpr::CastKind &Kind);
|
2008-11-08 16:00:26 +03:00
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-11-06 00:50:06 +03:00
|
|
|
Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
|
|
|
|
SourceLocation LAngleBracketLoc, TypeTy *Ty,
|
|
|
|
SourceLocation RAngleBracketLoc,
|
2009-03-15 20:47:39 +03:00
|
|
|
SourceLocation LParenLoc, ExprArg E,
|
2008-11-06 00:50:06 +03:00
|
|
|
SourceLocation RParenLoc) {
|
2010-01-15 22:13:16 +03:00
|
|
|
|
2010-01-15 21:39:57 +03:00
|
|
|
TypeSourceInfo *DestTInfo;
|
|
|
|
QualType DestType = GetTypeFromParser(Ty, &DestTInfo);
|
|
|
|
if (!DestTInfo)
|
|
|
|
DestTInfo = Context.getTrivialTypeSourceInfo(DestType, SourceLocation());
|
2010-01-15 22:13:16 +03:00
|
|
|
|
|
|
|
return BuildCXXNamedCast(OpLoc, Kind, DestTInfo, move(E),
|
|
|
|
SourceRange(LAngleBracketLoc, RAngleBracketLoc),
|
|
|
|
SourceRange(LParenLoc, RParenLoc));
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::OwningExprResult
|
|
|
|
Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
|
|
|
|
TypeSourceInfo *DestTInfo, ExprArg E,
|
|
|
|
SourceRange AngleBrackets, SourceRange Parens) {
|
|
|
|
Expr *Ex = E.takeAs<Expr>();
|
|
|
|
QualType DestType = DestTInfo->getType();
|
|
|
|
|
|
|
|
SourceRange OpRange(OpLoc, Parens.getEnd());
|
|
|
|
SourceRange DestRange = AngleBrackets;
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2008-12-18 01:52:20 +03:00
|
|
|
// If the type is dependent, we won't do the semantic analysis now.
|
|
|
|
// FIXME: should we check this in a more fine-grained manner?
|
|
|
|
bool TypeDependent = DestType->isDependentType() || Ex->isTypeDependent();
|
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
switch (Kind) {
|
|
|
|
default: assert(0 && "Unknown C++ cast!");
|
|
|
|
|
|
|
|
case tok::kw_const_cast:
|
2008-12-18 01:52:20 +03:00
|
|
|
if (!TypeDependent)
|
|
|
|
CheckConstCast(*this, Ex, DestType, OpRange, DestRange);
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXConstCastExpr(DestType.getNonReferenceType(),
|
2010-01-15 21:39:57 +03:00
|
|
|
Ex, DestTInfo, OpLoc));
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-08-02 23:07:59 +04:00
|
|
|
case tok::kw_dynamic_cast: {
|
|
|
|
CastExpr::CastKind Kind = CastExpr::CK_Unknown;
|
2010-04-24 22:38:56 +04:00
|
|
|
// FIXME: Initialize base path!
|
|
|
|
CXXBaseSpecifierArray BasePath;
|
2008-12-18 01:52:20 +03:00
|
|
|
if (!TypeDependent)
|
2009-08-02 23:07:59 +04:00
|
|
|
CheckDynamicCast(*this, Ex, DestType, OpRange, DestRange, Kind);
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context)CXXDynamicCastExpr(DestType.getNonReferenceType(),
|
2010-04-24 22:38:56 +04:00
|
|
|
Kind, Ex, BasePath, DestTInfo,
|
|
|
|
OpLoc));
|
2009-08-02 23:07:59 +04:00
|
|
|
}
|
2009-09-15 08:48:33 +04:00
|
|
|
case tok::kw_reinterpret_cast: {
|
|
|
|
CastExpr::CastKind Kind = CastExpr::CK_Unknown;
|
2010-04-24 22:38:56 +04:00
|
|
|
// FIXME: Initialize base path!
|
|
|
|
CXXBaseSpecifierArray BasePath;
|
2008-12-18 01:52:20 +03:00
|
|
|
if (!TypeDependent)
|
2009-09-15 08:48:33 +04:00
|
|
|
CheckReinterpretCast(*this, Ex, DestType, OpRange, DestRange, Kind);
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXReinterpretCastExpr(
|
|
|
|
DestType.getNonReferenceType(),
|
2010-04-24 22:38:56 +04:00
|
|
|
Kind, Ex, BasePath, DestTInfo, OpLoc));
|
2009-09-15 08:48:33 +04:00
|
|
|
}
|
2009-08-08 02:21:05 +04:00
|
|
|
case tok::kw_static_cast: {
|
|
|
|
CastExpr::CastKind Kind = CastExpr::CK_Unknown;
|
2010-04-24 22:38:56 +04:00
|
|
|
// FIXME: Initialize base path!
|
|
|
|
CXXBaseSpecifierArray BasePath;
|
2010-04-17 02:09:46 +04:00
|
|
|
if (!TypeDependent)
|
|
|
|
CheckStaticCast(*this, Ex, DestType, OpRange, Kind);
|
2009-09-10 01:33:21 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXStaticCastExpr(DestType.getNonReferenceType(),
|
2010-04-24 22:38:56 +04:00
|
|
|
Kind, Ex, BasePath,
|
|
|
|
DestTInfo, OpLoc));
|
2009-08-08 02:21:05 +04:00
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-11-18 21:10:53 +03:00
|
|
|
/// UnwrapDissimilarPointerTypes - Like Sema::UnwrapSimilarPointerTypes,
|
|
|
|
/// this removes one level of indirection from both types, provided that they're
|
|
|
|
/// the same kind of pointer (plain or to-member). Unlike the Sema function,
|
|
|
|
/// this one doesn't care if the two pointers-to-member don't point into the
|
|
|
|
/// same class. This is because CastsAwayConstness doesn't care.
|
|
|
|
bool UnwrapDissimilarPointerTypes(QualType& T1, QualType& T2) {
|
|
|
|
const PointerType *T1PtrType = T1->getAs<PointerType>(),
|
|
|
|
*T2PtrType = T2->getAs<PointerType>();
|
|
|
|
if (T1PtrType && T2PtrType) {
|
|
|
|
T1 = T1PtrType->getPointeeType();
|
|
|
|
T2 = T2PtrType->getPointeeType();
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-03 23:32:31 +03:00
|
|
|
const ObjCObjectPointerType *T1ObjCPtrType =
|
|
|
|
T1->getAs<ObjCObjectPointerType>(),
|
|
|
|
*T2ObjCPtrType =
|
|
|
|
T2->getAs<ObjCObjectPointerType>();
|
|
|
|
if (T1ObjCPtrType) {
|
|
|
|
if (T2ObjCPtrType) {
|
|
|
|
T1 = T1ObjCPtrType->getPointeeType();
|
|
|
|
T2 = T2ObjCPtrType->getPointeeType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (T2PtrType) {
|
|
|
|
T1 = T1ObjCPtrType->getPointeeType();
|
|
|
|
T2 = T2PtrType->getPointeeType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (T2ObjCPtrType) {
|
|
|
|
if (T1PtrType) {
|
|
|
|
T2 = T2ObjCPtrType->getPointeeType();
|
|
|
|
T1 = T1PtrType->getPointeeType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-18 21:10:53 +03:00
|
|
|
const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
|
|
|
|
*T2MPType = T2->getAs<MemberPointerType>();
|
|
|
|
if (T1MPType && T2MPType) {
|
|
|
|
T1 = T1MPType->getPointeeType();
|
|
|
|
T2 = T2MPType->getPointeeType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
/// CastsAwayConstness - Check if the pointer conversion from SrcType to
|
|
|
|
/// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
|
|
|
|
/// the cast checkers. Both arguments must denote pointer (possibly to member)
|
|
|
|
/// types.
|
2009-10-22 19:07:22 +04:00
|
|
|
static bool
|
2009-09-09 19:08:12 +04:00
|
|
|
CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType) {
|
2009-07-25 19:41:38 +04:00
|
|
|
// Casting away constness is defined in C++ 5.2.11p8 with reference to
|
|
|
|
// C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
|
|
|
|
// the rules are non-trivial. So first we construct Tcv *...cv* as described
|
|
|
|
// in C++ 5.2.11p8.
|
2010-02-03 23:32:31 +03:00
|
|
|
assert((SrcType->isAnyPointerType() || SrcType->isMemberPointerType()) &&
|
2009-07-25 19:41:38 +04:00
|
|
|
"Source type is not pointer or pointer to member.");
|
2010-02-03 23:32:31 +03:00
|
|
|
assert((DestType->isAnyPointerType() || DestType->isMemberPointerType()) &&
|
2009-07-25 19:41:38 +04:00
|
|
|
"Destination type is not pointer or pointer to member.");
|
2009-01-27 01:19:12 +03:00
|
|
|
|
2009-11-15 12:20:52 +03:00
|
|
|
QualType UnwrappedSrcType = Self.Context.getCanonicalType(SrcType),
|
|
|
|
UnwrappedDestType = Self.Context.getCanonicalType(DestType);
|
2009-09-24 23:53:00 +04:00
|
|
|
llvm::SmallVector<Qualifiers, 8> cv1, cv2;
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Find the qualifications.
|
2009-11-18 21:10:53 +03:00
|
|
|
while (UnwrapDissimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
|
2009-09-24 23:53:00 +04:00
|
|
|
cv1.push_back(UnwrappedSrcType.getQualifiers());
|
|
|
|
cv2.push_back(UnwrappedDestType.getQualifiers());
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
assert(cv1.size() > 0 && "Must have at least one pointer level.");
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Construct void pointers with those qualifiers (in reverse order of
|
|
|
|
// unwrapping, of course).
|
|
|
|
QualType SrcConstruct = Self.Context.VoidTy;
|
|
|
|
QualType DestConstruct = Self.Context.VoidTy;
|
2009-09-24 23:53:00 +04:00
|
|
|
ASTContext &Context = Self.Context;
|
|
|
|
for (llvm::SmallVector<Qualifiers, 8>::reverse_iterator i1 = cv1.rbegin(),
|
|
|
|
i2 = cv2.rbegin();
|
2009-09-09 19:08:12 +04:00
|
|
|
i1 != cv1.rend(); ++i1, ++i2) {
|
2009-09-24 23:53:00 +04:00
|
|
|
SrcConstruct
|
|
|
|
= Context.getPointerType(Context.getQualifiedType(SrcConstruct, *i1));
|
|
|
|
DestConstruct
|
|
|
|
= Context.getPointerType(Context.getQualifiedType(DestConstruct, *i2));
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Test if they're compatible.
|
|
|
|
return SrcConstruct != DestConstruct &&
|
|
|
|
!Self.IsQualificationConversion(SrcConstruct, DestConstruct);
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
|
|
|
|
/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
|
|
|
|
/// checked downcasts in class hierarchies.
|
2009-08-02 23:07:59 +04:00
|
|
|
static void
|
2009-07-25 19:41:38 +04:00
|
|
|
CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
|
|
|
const SourceRange &OpRange,
|
2009-09-09 19:08:12 +04:00
|
|
|
const SourceRange &DestRange, CastExpr::CastKind &Kind) {
|
2008-11-06 00:50:06 +03:00
|
|
|
QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
|
2008-11-08 16:00:26 +03:00
|
|
|
DestType = Self.Context.getCanonicalType(DestType);
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
|
|
|
|
// or "pointer to cv void".
|
2009-03-17 02:22:08 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
QualType DestPointee;
|
2009-07-30 01:53:49 +04:00
|
|
|
const PointerType *DestPointer = DestType->getAs<PointerType>();
|
|
|
|
const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
|
2009-07-25 19:41:38 +04:00
|
|
|
if (DestPointer) {
|
|
|
|
DestPointee = DestPointer->getPointeeType();
|
|
|
|
} else if (DestReference) {
|
|
|
|
DestPointee = DestReference->getPointeeType();
|
2008-11-06 00:50:06 +03:00
|
|
|
} else {
|
2009-07-25 19:41:38 +04:00
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
|
|
|
|
<< OrigDestType << DestRange;
|
2009-05-10 22:38:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const RecordType *DestRecord = DestPointee->getAs<RecordType>();
|
2009-07-25 19:41:38 +04:00
|
|
|
if (DestPointee->isVoidType()) {
|
|
|
|
assert(DestPointer && "Reference to void is not possible");
|
|
|
|
} else if (DestRecord) {
|
|
|
|
if (Self.RequireCompleteType(OpRange.getBegin(), DestPointee,
|
2010-03-30 03:34:08 +04:00
|
|
|
Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
|
2009-08-27 03:45:07 +04:00
|
|
|
<< DestRange))
|
2009-07-25 19:41:38 +04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
|
|
|
|
<< DestPointee.getUnqualifiedType() << DestRange;
|
2008-11-06 00:50:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++0x 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
|
|
|
|
// complete class type, [...]. If T is an lvalue reference type, v shall be
|
|
|
|
// an lvalue of a complete class type, [...]. If T is an rvalue reference
|
|
|
|
// type, v shall be an expression having a complete effective class type,
|
|
|
|
// [...]
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
|
|
|
|
QualType SrcPointee;
|
|
|
|
if (DestPointer) {
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
|
2009-07-25 19:41:38 +04:00
|
|
|
SrcPointee = SrcPointer->getPointeeType();
|
|
|
|
} else {
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
|
|
|
|
<< OrigSrcType << SrcExpr->getSourceRange();
|
|
|
|
return;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
} else if (DestReference->isLValueReferenceType()) {
|
|
|
|
if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
|
|
|
|
<< CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
|
|
|
|
}
|
|
|
|
SrcPointee = SrcType;
|
|
|
|
} else {
|
|
|
|
SrcPointee = SrcType;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const RecordType *SrcRecord = SrcPointee->getAs<RecordType>();
|
2009-07-25 19:41:38 +04:00
|
|
|
if (SrcRecord) {
|
|
|
|
if (Self.RequireCompleteType(OpRange.getBegin(), SrcPointee,
|
2010-03-30 03:34:08 +04:00
|
|
|
Self.PDiag(diag::err_bad_dynamic_cast_incomplete)
|
2009-08-27 03:45:07 +04:00
|
|
|
<< SrcExpr->getSourceRange()))
|
2009-07-25 19:41:38 +04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
|
|
|
|
<< SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
|
2008-11-06 00:50:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
assert((DestPointer || DestReference) &&
|
|
|
|
"Bad destination non-ptr/ref slipped through.");
|
|
|
|
assert((DestRecord || DestPointee->isVoidType()) &&
|
|
|
|
"Bad destination pointee slipped through.");
|
|
|
|
assert(SrcRecord && "Bad source pointee slipped through.");
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
|
|
|
|
if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
|
2008-11-19 01:52:51 +03:00
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
|
2009-07-25 19:41:38 +04:00
|
|
|
<< CT_Dynamic << OrigSrcType << OrigDestType << OpRange;
|
2008-11-06 00:50:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.7p3: If the type of v is the same as the required result type,
|
|
|
|
// [except for cv].
|
|
|
|
if (DestRecord == SrcRecord) {
|
2008-11-06 00:50:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.7p5
|
|
|
|
// Upcasts are resolved statically.
|
|
|
|
if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
|
|
|
|
Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
|
|
|
|
OpRange.getBegin(), OpRange);
|
2009-08-02 23:07:59 +04:00
|
|
|
Kind = CastExpr::CK_DerivedToBase;
|
2009-07-25 19:41:38 +04:00
|
|
|
// Diagnostic already emitted on error.
|
2008-11-06 00:50:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
|
2010-02-11 04:04:33 +03:00
|
|
|
const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition();
|
2009-07-25 19:41:38 +04:00
|
|
|
assert(SrcDecl && "Definition missing");
|
|
|
|
if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
|
|
|
|
<< SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done. Everything else is run-time checks.
|
2009-08-02 23:07:59 +04:00
|
|
|
Kind = CastExpr::CK_Dynamic;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
|
|
|
|
/// Refer to C++ 5.2.11 for details. const_cast is typically used in code
|
|
|
|
/// like this:
|
|
|
|
/// const char *str = "literal";
|
|
|
|
/// legacy_function(const_cast\<char*\>(str));
|
|
|
|
void
|
|
|
|
CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
2009-09-09 19:08:12 +04:00
|
|
|
const SourceRange &OpRange, const SourceRange &DestRange) {
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!DestType->isLValueReferenceType())
|
2010-02-03 03:27:59 +03:00
|
|
|
Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
unsigned msg = diag::err_bad_cxx_cast_generic;
|
|
|
|
if (TryConstCast(Self, SrcExpr, DestType, /*CStyle*/false, msg) != TC_Success
|
|
|
|
&& msg != 0)
|
|
|
|
Self.Diag(OpRange.getBegin(), msg) << CT_Const
|
|
|
|
<< SrcExpr->getType() << DestType << OpRange;
|
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
|
|
|
|
/// valid.
|
|
|
|
/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
|
|
|
|
/// like this:
|
|
|
|
/// char *bytes = reinterpret_cast\<char*\>(int_ptr);
|
|
|
|
void
|
|
|
|
CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
2009-09-15 08:48:33 +04:00
|
|
|
const SourceRange &OpRange, const SourceRange &DestRange,
|
|
|
|
CastExpr::CastKind &Kind) {
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!DestType->isLValueReferenceType())
|
2010-02-03 03:27:59 +03:00
|
|
|
Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
unsigned msg = diag::err_bad_cxx_cast_generic;
|
2009-09-26 04:12:34 +04:00
|
|
|
if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange,
|
|
|
|
msg, Kind)
|
2009-07-25 19:41:38 +04:00
|
|
|
!= TC_Success && msg != 0)
|
|
|
|
Self.Diag(OpRange.getBegin(), msg) << CT_Reinterpret
|
|
|
|
<< SrcExpr->getType() << DestType << OpRange;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
|
|
|
|
/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
|
|
|
|
/// implicit conversions explicit and getting rid of data loss warnings.
|
|
|
|
void
|
2008-11-08 16:00:26 +03:00
|
|
|
CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
2010-04-17 02:09:46 +04:00
|
|
|
const SourceRange &OpRange, CastExpr::CastKind &Kind) {
|
2009-07-25 19:41:38 +04:00
|
|
|
// This test is outside everything else because it's the only case where
|
|
|
|
// a non-lvalue-reference target type does not lead to decay.
|
|
|
|
// C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
|
2009-11-16 08:44:20 +03:00
|
|
|
if (DestType->isVoidType()) {
|
|
|
|
Kind = CastExpr::CK_ToVoid;
|
2009-07-25 19:41:38 +04:00
|
|
|
return;
|
2009-11-16 08:44:20 +03:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
|
2009-11-06 04:14:41 +03:00
|
|
|
if (!DestType->isLValueReferenceType() && !DestType->isRecordType())
|
2010-02-03 03:27:59 +03:00
|
|
|
Self.DefaultFunctionArrayLvalueConversion(SrcExpr);
|
2009-07-25 19:41:38 +04:00
|
|
|
|
|
|
|
unsigned msg = diag::err_bad_cxx_cast_generic;
|
2009-11-15 00:15:49 +03:00
|
|
|
if (TryStaticCast(Self, SrcExpr, DestType, /*CStyle*/false, OpRange, msg,
|
2010-04-17 02:09:46 +04:00
|
|
|
Kind)
|
2009-07-25 19:41:38 +04:00
|
|
|
!= TC_Success && msg != 0)
|
|
|
|
Self.Diag(OpRange.getBegin(), msg) << CT_Static
|
|
|
|
<< SrcExpr->getType() << DestType << OpRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TryStaticCast - Check if a static cast can be performed, and do so if
|
|
|
|
/// possible. If @p CStyle, ignore access restrictions on hierarchy casting
|
|
|
|
/// and casting away constness.
|
2009-11-15 00:15:49 +03:00
|
|
|
static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
|
2009-07-25 19:41:38 +04:00
|
|
|
QualType DestType, bool CStyle,
|
2009-09-02 00:52:42 +04:00
|
|
|
const SourceRange &OpRange, unsigned &msg,
|
2010-04-17 02:09:46 +04:00
|
|
|
CastExpr::CastKind &Kind) {
|
2008-11-06 00:50:06 +03:00
|
|
|
// The order the tests is not entirely arbitrary. There is one conversion
|
|
|
|
// that can be handled in two different ways. Given:
|
|
|
|
// struct A {};
|
|
|
|
// struct B : public A {
|
|
|
|
// B(); B(const A&);
|
|
|
|
// };
|
|
|
|
// const A &a = B();
|
|
|
|
// the cast static_cast<const B&>(a) could be seen as either a static
|
|
|
|
// reference downcast, or an explicit invocation of the user-defined
|
|
|
|
// conversion using B's conversion constructor.
|
|
|
|
// DR 427 specifies that the downcast is to be applied here.
|
|
|
|
|
|
|
|
// C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
|
2009-07-25 19:41:38 +04:00
|
|
|
// Done outside this function.
|
|
|
|
|
|
|
|
TryCastResult tcr;
|
2008-11-06 00:50:06 +03:00
|
|
|
|
|
|
|
// C++ 5.2.9p5, reference downcast.
|
|
|
|
// See the function for details.
|
|
|
|
// DR 427 specifies that this is to be applied before paragraph 2.
|
2009-11-15 00:15:49 +03:00
|
|
|
tcr = TryStaticReferenceDowncast(Self, SrcExpr, DestType, CStyle, OpRange,
|
2009-11-12 19:53:16 +03:00
|
|
|
msg, Kind);
|
2009-07-25 19:41:38 +04:00
|
|
|
if (tcr != TC_NotApplicable)
|
|
|
|
return tcr;
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-03-23 01:30:06 +03:00
|
|
|
// N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
|
|
|
|
// reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
|
2009-07-25 19:41:38 +04:00
|
|
|
tcr = TryLValueToRValueCast(Self, SrcExpr, DestType, msg);
|
2009-11-15 00:15:49 +03:00
|
|
|
if (tcr != TC_NotApplicable) {
|
|
|
|
Kind = CastExpr::CK_NoOp;
|
2009-07-25 19:41:38 +04:00
|
|
|
return tcr;
|
2009-11-15 00:15:49 +03:00
|
|
|
}
|
2009-03-23 01:30:06 +03:00
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
// C++ 5.2.9p2: An expression e can be explicitly converted to a type T
|
|
|
|
// [...] if the declaration "T t(e);" is well-formed, [...].
|
2009-08-26 22:55:36 +04:00
|
|
|
tcr = TryStaticImplicitCast(Self, SrcExpr, DestType, CStyle, OpRange, msg,
|
2010-04-17 02:09:46 +04:00
|
|
|
Kind);
|
2009-09-26 04:12:34 +04:00
|
|
|
if (tcr != TC_NotApplicable)
|
2009-07-25 19:41:38 +04:00
|
|
|
return tcr;
|
2009-09-10 01:33:21 +04:00
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
// C++ 5.2.9p6: May apply the reverse of any standard conversion, except
|
|
|
|
// lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
|
|
|
|
// conversions, subject to further restrictions.
|
|
|
|
// Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
|
|
|
|
// of qualification conversions impossible.
|
2009-07-25 19:41:38 +04:00
|
|
|
// In the CStyle case, the earlier attempt to const_cast should have taken
|
|
|
|
// care of reverse qualification conversions.
|
2008-11-06 00:50:06 +03:00
|
|
|
|
|
|
|
QualType OrigSrcType = SrcExpr->getType();
|
|
|
|
|
2008-11-08 16:00:26 +03:00
|
|
|
QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
|
2008-11-06 00:50:06 +03:00
|
|
|
|
|
|
|
// Reverse integral promotion/conversion. All such conversions are themselves
|
|
|
|
// again integral promotions or conversions and are thus already handled by
|
|
|
|
// p2 (TryDirectInitialization above).
|
|
|
|
// (Note: any data loss warnings should be suppressed.)
|
|
|
|
// The exception is the reverse of enum->integer, i.e. integer->enum (and
|
|
|
|
// enum->enum). See also C++ 5.2.9p7.
|
|
|
|
// The same goes for reverse floating point promotion/conversion and
|
|
|
|
// floating-integral conversions. Again, only floating->enum is relevant.
|
|
|
|
if (DestType->isEnumeralType()) {
|
|
|
|
if (SrcType->isComplexType() || SrcType->isVectorType()) {
|
|
|
|
// Fall through - these cannot be converted.
|
2009-11-16 08:44:20 +03:00
|
|
|
} else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
|
|
|
|
Kind = CastExpr::CK_IntegralCast;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2009-11-16 08:44:20 +03:00
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
|
|
|
|
// C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
|
2009-11-12 19:53:16 +03:00
|
|
|
tcr = TryStaticPointerDowncast(Self, SrcType, DestType, CStyle, OpRange, msg,
|
|
|
|
Kind);
|
2009-07-25 19:41:38 +04:00
|
|
|
if (tcr != TC_NotApplicable)
|
|
|
|
return tcr;
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-01-28 21:33:18 +03:00
|
|
|
// Reverse member pointer conversion. C++ 4.11 specifies member pointer
|
2008-11-06 00:50:06 +03:00
|
|
|
// conversion. C++ 5.2.9p9 has additional information.
|
|
|
|
// DR54's access restrictions apply here also.
|
2010-03-08 02:24:59 +03:00
|
|
|
tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
|
2009-10-30 03:46:35 +03:00
|
|
|
OpRange, msg, Kind);
|
2009-07-25 19:41:38 +04:00
|
|
|
if (tcr != TC_NotApplicable)
|
|
|
|
return tcr;
|
2008-11-06 00:50:06 +03:00
|
|
|
|
|
|
|
// Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
|
|
|
|
// void*. C++ 5.2.9p10 specifies additional restrictions, which really is
|
|
|
|
// just the usual constness stuff.
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const PointerType *SrcPointer = SrcType->getAs<PointerType>()) {
|
2008-11-06 00:50:06 +03:00
|
|
|
QualType SrcPointee = SrcPointer->getPointeeType();
|
|
|
|
if (SrcPointee->isVoidType()) {
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const PointerType *DestPointer = DestType->getAs<PointerType>()) {
|
2008-11-06 00:50:06 +03:00
|
|
|
QualType DestPointee = DestPointer->getPointeeType();
|
2009-03-24 23:13:58 +03:00
|
|
|
if (DestPointee->isIncompleteOrObjectType()) {
|
2008-11-06 00:50:06 +03:00
|
|
|
// This is definitely the intended conversion, but it might fail due
|
|
|
|
// to a const violation.
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!CStyle && !DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
|
|
|
|
msg = diag::err_bad_cxx_cast_const_away;
|
|
|
|
return TC_Failed;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
2009-11-16 08:44:20 +03:00
|
|
|
Kind = CastExpr::CK_BitCast;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
}
|
2009-12-09 02:09:15 +03:00
|
|
|
else if (CStyle && DestType->isObjCObjectPointerType()) {
|
|
|
|
// allow c-style cast of objective-c pointers as they are pervasive.
|
|
|
|
Kind = CastExpr::CK_AnyPointerToObjCPointerCast;
|
|
|
|
return TC_Success;
|
|
|
|
}
|
2009-12-12 01:40:48 +03:00
|
|
|
else if (CStyle && DestType->isBlockPointerType()) {
|
|
|
|
// allow c-style cast of void * to block pointers.
|
|
|
|
Kind = CastExpr::CK_AnyPointerToBlockPointerCast;
|
|
|
|
return TC_Success;
|
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We tried everything. Everything! Nothing works! :-(
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-03-23 01:30:06 +03:00
|
|
|
/// Tests whether a conversion according to N2844 is valid.
|
2009-07-25 19:41:38 +04:00
|
|
|
TryCastResult
|
2009-03-23 01:30:06 +03:00
|
|
|
TryLValueToRValueCast(Sema &Self, Expr *SrcExpr, QualType DestType,
|
2009-09-09 19:08:12 +04:00
|
|
|
unsigned &msg) {
|
2009-03-23 01:30:06 +03:00
|
|
|
// N2844 5.2.9p3: An lvalue of type "cv1 T1" can be cast to type "rvalue
|
|
|
|
// reference to cv2 T2" if "cv2 T2" is reference-compatible with "cv1 T1".
|
2009-07-30 01:53:49 +04:00
|
|
|
const RValueReferenceType *R = DestType->getAs<RValueReferenceType>();
|
2009-03-23 01:30:06 +03:00
|
|
|
if (!R)
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2009-03-23 01:30:06 +03:00
|
|
|
|
|
|
|
if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid)
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2009-03-23 01:30:06 +03:00
|
|
|
|
|
|
|
// Because we try the reference downcast before this function, from now on
|
|
|
|
// this is the only cast possibility, so we issue an error if we fail now.
|
2009-07-25 19:41:38 +04:00
|
|
|
// FIXME: Should allow casting away constness if CStyle.
|
2009-03-23 01:30:06 +03:00
|
|
|
bool DerivedToBase;
|
2009-11-05 16:06:35 +03:00
|
|
|
if (Self.CompareReferenceRelationship(SrcExpr->getLocStart(),
|
|
|
|
SrcExpr->getType(), R->getPointeeType(),
|
2009-03-23 01:30:06 +03:00
|
|
|
DerivedToBase) <
|
|
|
|
Sema::Ref_Compatible_With_Added_Qualification) {
|
2009-07-25 19:41:38 +04:00
|
|
|
msg = diag::err_bad_lvalue_to_rvalue_cast;
|
|
|
|
return TC_Failed;
|
2009-03-23 01:30:06 +03:00
|
|
|
}
|
|
|
|
|
2010-03-25 03:20:38 +03:00
|
|
|
// FIXME: We should probably have an AST node for lvalue-to-rvalue
|
|
|
|
// conversions.
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2009-03-23 01:30:06 +03:00
|
|
|
}
|
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
|
2009-07-25 19:41:38 +04:00
|
|
|
TryCastResult
|
2008-11-08 16:00:26 +03:00
|
|
|
TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
|
2009-07-25 19:41:38 +04:00
|
|
|
bool CStyle, const SourceRange &OpRange,
|
2009-11-12 19:53:16 +03:00
|
|
|
unsigned &msg, CastExpr::CastKind &Kind) {
|
2008-11-06 00:50:06 +03:00
|
|
|
// C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
|
|
|
|
// cast to type "reference to cv2 D", where D is a class derived from B,
|
|
|
|
// if a valid standard conversion from "pointer to D" to "pointer to B"
|
|
|
|
// exists, cv2 >= cv1, and B is not a virtual base class of D.
|
|
|
|
// In addition, DR54 clarifies that the base must be accessible in the
|
|
|
|
// current context. Although the wording of DR54 only applies to the pointer
|
|
|
|
// variant of this rule, the intent is clearly for it to apply to the this
|
2009-07-25 19:41:38 +04:00
|
|
|
// conversion as well.
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const ReferenceType *DestReference = DestType->getAs<ReferenceType>();
|
2008-11-06 00:50:06 +03:00
|
|
|
if (!DestReference) {
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
|
|
|
}
|
|
|
|
bool RValueRef = DestReference->isRValueReferenceType();
|
|
|
|
if (!RValueRef && SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
|
|
|
|
// We know the left side is an lvalue reference, so we can suggest a reason.
|
|
|
|
msg = diag::err_bad_cxx_cast_rvalue;
|
|
|
|
return TC_NotApplicable;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
QualType DestPointee = DestReference->getPointeeType();
|
|
|
|
|
2009-11-15 12:20:52 +03:00
|
|
|
return TryStaticDowncast(Self,
|
|
|
|
Self.Context.getCanonicalType(SrcExpr->getType()),
|
|
|
|
Self.Context.getCanonicalType(DestPointee), CStyle,
|
2009-11-12 19:53:16 +03:00
|
|
|
OpRange, SrcExpr->getType(), DestType, msg, Kind);
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
|
2009-07-25 19:41:38 +04:00
|
|
|
TryCastResult
|
2008-11-08 16:00:26 +03:00
|
|
|
TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
|
2009-09-09 19:08:12 +04:00
|
|
|
bool CStyle, const SourceRange &OpRange,
|
2009-11-12 19:53:16 +03:00
|
|
|
unsigned &msg, CastExpr::CastKind &Kind) {
|
2008-11-06 00:50:06 +03:00
|
|
|
// C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
|
|
|
|
// type, can be converted to an rvalue of type "pointer to cv2 D", where D
|
|
|
|
// is a class derived from B, if a valid standard conversion from "pointer
|
|
|
|
// to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
|
|
|
|
// class of D.
|
|
|
|
// In addition, DR54 clarifies that the base must be accessible in the
|
|
|
|
// current context.
|
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const PointerType *DestPointer = DestType->getAs<PointerType>();
|
2008-11-06 00:50:06 +03:00
|
|
|
if (!DestPointer) {
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
|
|
|
}
|
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const PointerType *SrcPointer = SrcType->getAs<PointerType>();
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!SrcPointer) {
|
|
|
|
msg = diag::err_bad_static_cast_pointer_nonpointer;
|
|
|
|
return TC_NotApplicable;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-11-15 12:20:52 +03:00
|
|
|
return TryStaticDowncast(Self,
|
|
|
|
Self.Context.getCanonicalType(SrcPointer->getPointeeType()),
|
|
|
|
Self.Context.getCanonicalType(DestPointer->getPointeeType()),
|
|
|
|
CStyle, OpRange, SrcType, DestType, msg, Kind);
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2008-11-08 02:29:29 +03:00
|
|
|
/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
|
|
|
|
/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
|
2009-11-15 12:20:52 +03:00
|
|
|
/// DestType is possible and allowed.
|
2009-07-25 19:41:38 +04:00
|
|
|
TryCastResult
|
2009-11-15 12:20:52 +03:00
|
|
|
TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
|
2009-07-25 19:41:38 +04:00
|
|
|
bool CStyle, const SourceRange &OpRange, QualType OrigSrcType,
|
2009-11-12 19:53:16 +03:00
|
|
|
QualType OrigDestType, unsigned &msg,
|
|
|
|
CastExpr::CastKind &Kind) {
|
2009-10-22 19:07:22 +04:00
|
|
|
// We can only work with complete types. But don't complain if it doesn't work
|
2010-03-30 03:34:08 +04:00
|
|
|
if (Self.RequireCompleteType(OpRange.getBegin(), SrcType, Self.PDiag(0)) ||
|
|
|
|
Self.RequireCompleteType(OpRange.getBegin(), DestType, Self.PDiag(0)))
|
2009-10-22 19:07:22 +04:00
|
|
|
return TC_NotApplicable;
|
|
|
|
|
2008-11-06 00:50:06 +03:00
|
|
|
// Downcast can only happen in class hierarchies, so we need classes.
|
2009-11-15 12:20:52 +03:00
|
|
|
if (!DestType->getAs<RecordType>() || !SrcType->getAs<RecordType>()) {
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-10-06 21:59:45 +04:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/!CStyle,
|
|
|
|
/*DetectVirtual=*/true);
|
2008-11-08 16:00:26 +03:00
|
|
|
if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2008-11-08 02:29:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Target type does derive from source type. Now we're serious. If an error
|
|
|
|
// appears now, it's not ignored.
|
|
|
|
// This may not be entirely in line with the standard. Take for example:
|
|
|
|
// struct A {};
|
|
|
|
// struct B : virtual A {
|
|
|
|
// B(A&);
|
|
|
|
// };
|
2009-09-09 19:08:12 +04:00
|
|
|
//
|
2008-11-08 02:29:29 +03:00
|
|
|
// void f()
|
|
|
|
// {
|
|
|
|
// (void)static_cast<const B&>(*((A*)0));
|
|
|
|
// }
|
|
|
|
// As far as the standard is concerned, p5 does not apply (A is virtual), so
|
|
|
|
// p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
|
|
|
|
// However, both GCC and Comeau reject this example, and accepting it would
|
|
|
|
// mean more complex code if we're to preserve the nice error message.
|
|
|
|
// FIXME: Being 100% compliant here would be nice to have.
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Must preserve cv, as always, unless we're in C-style mode.
|
|
|
|
if (!CStyle && !DestType.isAtLeastAsQualifiedAs(SrcType)) {
|
|
|
|
msg = diag::err_bad_cxx_cast_const_away;
|
|
|
|
return TC_Failed;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
|
2008-11-08 02:29:29 +03:00
|
|
|
// This code is analoguous to that in CheckDerivedToBaseConversion, except
|
|
|
|
// that it builds the paths in reverse order.
|
|
|
|
// To sum up: record all paths to the base and build a nice string from
|
|
|
|
// them. Use it to spice up the error message.
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!Paths.isRecordingPaths()) {
|
|
|
|
Paths.clear();
|
|
|
|
Paths.setRecordingPaths(true);
|
|
|
|
Self.IsDerivedFrom(DestType, SrcType, Paths);
|
|
|
|
}
|
2008-11-08 02:29:29 +03:00
|
|
|
std::string PathDisplayStr;
|
|
|
|
std::set<unsigned> DisplayedPaths;
|
2009-10-06 21:59:45 +04:00
|
|
|
for (CXXBasePaths::paths_iterator PI = Paths.begin(), PE = Paths.end();
|
2009-07-25 19:41:38 +04:00
|
|
|
PI != PE; ++PI) {
|
|
|
|
if (DisplayedPaths.insert(PI->back().SubobjectNumber).second) {
|
2008-11-08 02:29:29 +03:00
|
|
|
// We haven't displayed a path to this particular base
|
|
|
|
// class subobject yet.
|
|
|
|
PathDisplayStr += "\n ";
|
2009-10-06 21:59:45 +04:00
|
|
|
for (CXXBasePath::const_reverse_iterator EI = PI->rbegin(),
|
|
|
|
EE = PI->rend();
|
2009-07-25 19:41:38 +04:00
|
|
|
EI != EE; ++EI)
|
|
|
|
PathDisplayStr += EI->Base->getType().getAsString() + " -> ";
|
2009-11-15 12:20:52 +03:00
|
|
|
PathDisplayStr += QualType(DestType).getAsString();
|
2008-11-08 02:29:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 01:52:51 +03:00
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
|
2009-11-15 12:20:52 +03:00
|
|
|
<< QualType(SrcType).getUnqualifiedType()
|
|
|
|
<< QualType(DestType).getUnqualifiedType()
|
2008-11-19 01:52:51 +03:00
|
|
|
<< PathDisplayStr << OpRange;
|
2009-07-25 19:41:38 +04:00
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Paths.getDetectedVirtual() != 0) {
|
2008-11-08 02:29:29 +03:00
|
|
|
QualType VirtualBase(Paths.getDetectedVirtual(), 0);
|
2008-11-19 01:52:51 +03:00
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
|
2008-11-24 09:25:27 +03:00
|
|
|
<< OrigSrcType << OrigDestType << VirtualBase << OpRange;
|
2009-07-25 19:41:38 +04:00
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2010-02-10 12:31:12 +03:00
|
|
|
if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
|
|
|
|
SrcType, DestType,
|
2010-03-16 08:22:47 +03:00
|
|
|
Paths.front(),
|
|
|
|
diag::err_downcast_from_inaccessible_base)) {
|
2009-07-25 19:41:38 +04:00
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-11-12 19:53:16 +03:00
|
|
|
Kind = CastExpr::CK_BaseToDerived;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-01-28 21:33:18 +03:00
|
|
|
/// TryStaticMemberPointerUpcast - Tests whether a conversion according to
|
|
|
|
/// C++ 5.2.9p9 is valid:
|
|
|
|
///
|
|
|
|
/// An rvalue of type "pointer to member of D of type cv1 T" can be
|
|
|
|
/// converted to an rvalue of type "pointer to member of B of type cv2 T",
|
|
|
|
/// where B is a base class of D [...].
|
|
|
|
///
|
2009-07-25 19:41:38 +04:00
|
|
|
TryCastResult
|
2010-03-08 02:24:59 +03:00
|
|
|
TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
|
|
|
|
QualType DestType, bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-10-30 03:46:35 +03:00
|
|
|
unsigned &msg, CastExpr::CastKind &Kind) {
|
2009-07-30 01:53:49 +04:00
|
|
|
const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
|
2009-01-28 21:33:18 +03:00
|
|
|
if (!DestMemPtr)
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2010-03-08 02:24:59 +03:00
|
|
|
|
|
|
|
bool WasOverloadedFunction = false;
|
2010-03-31 01:47:33 +04:00
|
|
|
DeclAccessPair FoundOverload;
|
2010-04-15 03:11:21 +04:00
|
|
|
if (SrcExpr->getType() == Self.Context.OverloadTy) {
|
|
|
|
if (FunctionDecl *Fn
|
|
|
|
= Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false,
|
|
|
|
FoundOverload)) {
|
|
|
|
CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
|
|
|
|
SrcType = Self.Context.getMemberPointerType(Fn->getType(),
|
|
|
|
Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
|
|
|
|
WasOverloadedFunction = true;
|
|
|
|
}
|
2010-03-08 02:24:59 +03:00
|
|
|
}
|
2010-04-15 03:11:21 +04:00
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!SrcMemPtr) {
|
|
|
|
msg = diag::err_bad_static_cast_member_pointer_nonmp;
|
|
|
|
return TC_NotApplicable;
|
|
|
|
}
|
2009-01-28 21:33:18 +03:00
|
|
|
|
|
|
|
// T == T, modulo cv
|
First part of changes to eliminate problems with cv-qualifiers and
sugared types. The basic problem is that our qualifier accessors
(getQualifiers, getCVRQualifiers, isConstQualified, etc.) only look at
the current QualType and not at any qualifiers that come from sugared
types, meaning that we won't see these qualifiers through, e.g.,
typedefs:
typedef const int CInt;
typedef CInt Self;
Self.isConstQualified() currently returns false!
Various bugs (e.g., PR5383) have cropped up all over the front end due
to such problems. I'm addressing this problem by splitting each
qualifier accessor into two versions:
- the "local" version only returns qualifiers on this particular
QualType instance
- the "normal" version that will eventually combine qualifiers from this
QualType instance with the qualifiers on the canonical type to
produce the full set of qualifiers.
This commit adds the local versions and switches a few callers from
the "normal" version (e.g., isConstQualified) over to the "local"
version (e.g., isLocalConstQualified) when that is the right thing to
do, e.g., because we're printing or serializing the qualifiers. Also,
switch a bunch of
Context.getCanonicalType(T1).getUnqualifiedType() == Context.getCanonicalType(T2).getQualifiedType()
expressions over to
Context.hasSameUnqualifiedType(T1, T2)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@88969 91177308-0d34-0410-b5e6-96231b3b80d8
2009-11-17 00:35:15 +03:00
|
|
|
if (!Self.Context.hasSameUnqualifiedType(SrcMemPtr->getPointeeType(),
|
|
|
|
DestMemPtr->getPointeeType()))
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2009-01-28 21:33:18 +03:00
|
|
|
|
|
|
|
// B base of D
|
|
|
|
QualType SrcClass(SrcMemPtr->getClass(), 0);
|
|
|
|
QualType DestClass(DestMemPtr->getClass(), 0);
|
2009-10-06 21:59:45 +04:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/!CStyle,
|
2009-01-28 21:33:18 +03:00
|
|
|
/*DetectVirtual=*/true);
|
|
|
|
if (!Self.IsDerivedFrom(SrcClass, DestClass, Paths)) {
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_NotApplicable;
|
2009-01-28 21:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// B is a base of D. But is it an allowed base? If not, it's a hard error.
|
|
|
|
if (Paths.isAmbiguous(DestClass)) {
|
|
|
|
Paths.clear();
|
|
|
|
Paths.setRecordingPaths(true);
|
|
|
|
bool StillOkay = Self.IsDerivedFrom(SrcClass, DestClass, Paths);
|
|
|
|
assert(StillOkay);
|
|
|
|
StillOkay = StillOkay;
|
|
|
|
std::string PathDisplayStr = Self.getAmbiguousPathsDisplayString(Paths);
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_ambiguous_memptr_conv)
|
|
|
|
<< 1 << SrcClass << DestClass << PathDisplayStr << OpRange;
|
2009-07-25 19:41:38 +04:00
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
2009-01-28 21:33:18 +03:00
|
|
|
}
|
|
|
|
|
2009-02-28 04:32:25 +03:00
|
|
|
if (const RecordType *VBase = Paths.getDetectedVirtual()) {
|
2009-01-28 21:33:18 +03:00
|
|
|
Self.Diag(OpRange.getBegin(), diag::err_memptr_conv_via_virtual)
|
|
|
|
<< SrcClass << DestClass << QualType(VBase, 0) << OpRange;
|
2009-07-25 19:41:38 +04:00
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
2009-01-28 21:33:18 +03:00
|
|
|
}
|
|
|
|
|
2010-02-10 12:31:12 +03:00
|
|
|
if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(),
|
|
|
|
DestType, SrcType,
|
2010-03-16 08:22:47 +03:00
|
|
|
Paths.front(),
|
|
|
|
diag::err_upcast_to_inaccessible_base)) {
|
2009-07-25 19:41:38 +04:00
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
2009-01-28 21:33:18 +03:00
|
|
|
|
2010-03-08 02:24:59 +03:00
|
|
|
if (WasOverloadedFunction) {
|
|
|
|
// Resolve the address of the overloaded function again, this time
|
|
|
|
// allowing complaints if something goes wrong.
|
|
|
|
FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
|
|
|
|
DestType,
|
2010-03-31 01:47:33 +04:00
|
|
|
true,
|
|
|
|
FoundOverload);
|
2010-03-08 02:24:59 +03:00
|
|
|
if (!Fn) {
|
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
|
|
|
|
2010-03-31 01:47:33 +04:00
|
|
|
SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn);
|
2010-03-08 02:24:59 +03:00
|
|
|
if (!SrcExpr) {
|
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-30 03:46:35 +03:00
|
|
|
Kind = CastExpr::CK_DerivedToBaseMemberPointer;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2009-01-28 21:33:18 +03:00
|
|
|
}
|
|
|
|
|
2008-11-08 02:29:29 +03:00
|
|
|
/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
|
|
|
|
/// is valid:
|
|
|
|
///
|
|
|
|
/// An expression e can be explicitly converted to a type T using a
|
|
|
|
/// @c static_cast if the declaration "T t(e);" is well-formed [...].
|
2009-07-25 19:41:38 +04:00
|
|
|
TryCastResult
|
2009-11-15 00:15:49 +03:00
|
|
|
TryStaticImplicitCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
|
2009-08-26 22:55:36 +04:00
|
|
|
bool CStyle, const SourceRange &OpRange, unsigned &msg,
|
2010-04-17 02:09:46 +04:00
|
|
|
CastExpr::CastKind &Kind) {
|
2009-09-07 22:25:47 +04:00
|
|
|
if (DestType->isRecordType()) {
|
|
|
|
if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
|
|
|
|
diag::err_bad_dynamic_cast_incomplete)) {
|
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
|
|
|
}
|
2010-04-17 02:09:46 +04:00
|
|
|
|
|
|
|
// At this point of CheckStaticCast, if the destination is a reference,
|
|
|
|
// this has to work. There is no other way that works.
|
|
|
|
// On the other hand, if we're checking a C-style cast, we've still got
|
|
|
|
// the reinterpret_cast way.
|
2010-04-16 23:30:02 +04:00
|
|
|
InitializedEntity Entity = InitializedEntity::InitializeTemporary(DestType);
|
|
|
|
InitializationKind InitKind
|
2010-04-17 02:09:46 +04:00
|
|
|
= InitializationKind::CreateCast(/*FIXME:*/OpRange,
|
|
|
|
CStyle);
|
2010-04-16 23:30:02 +04:00
|
|
|
InitializationSequence InitSeq(Self, Entity, InitKind, &SrcExpr, 1);
|
2010-04-17 02:09:46 +04:00
|
|
|
if (InitSeq.getKind() == InitializationSequence::FailedSequence &&
|
|
|
|
(CStyle || !DestType->isReferenceType()))
|
2009-09-26 04:12:34 +04:00
|
|
|
return TC_NotApplicable;
|
2010-04-17 02:09:46 +04:00
|
|
|
|
2010-04-16 23:30:02 +04:00
|
|
|
Sema::OwningExprResult Result
|
2010-04-17 02:09:46 +04:00
|
|
|
= InitSeq.Perform(Self, Entity, InitKind,
|
|
|
|
Action::MultiExprArg(Self, (void**)&SrcExpr, 1));
|
2010-04-16 23:30:02 +04:00
|
|
|
if (Result.isInvalid()) {
|
|
|
|
msg = 0;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
|
|
|
|
2010-04-17 02:09:46 +04:00
|
|
|
if (InitSeq.isConstructorInitialization())
|
|
|
|
Kind = CastExpr::CK_ConstructorConversion;
|
|
|
|
else
|
|
|
|
Kind = CastExpr::CK_NoOp;
|
|
|
|
|
2010-04-16 23:30:02 +04:00
|
|
|
SrcExpr = Result.takeAs<Expr>();
|
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
/// TryConstCast - See if a const_cast from source to destination is allowed,
|
|
|
|
/// and perform it if it is.
|
|
|
|
static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
|
|
|
|
bool CStyle, unsigned &msg) {
|
2008-11-08 16:00:26 +03:00
|
|
|
DestType = Self.Context.getCanonicalType(DestType);
|
2009-07-25 19:41:38 +04:00
|
|
|
QualType SrcType = SrcExpr->getType();
|
|
|
|
if (const LValueReferenceType *DestTypeTmp =
|
2009-07-30 01:53:49 +04:00
|
|
|
DestType->getAs<LValueReferenceType>()) {
|
2009-07-25 19:41:38 +04:00
|
|
|
if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
|
|
|
|
// Cannot const_cast non-lvalue to lvalue reference type. But if this
|
|
|
|
// is C-style, static_cast might find a way, so we simply suggest a
|
|
|
|
// message and tell the parent to keep searching.
|
|
|
|
msg = diag::err_bad_cxx_cast_rvalue;
|
|
|
|
return TC_NotApplicable;
|
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
|
|
|
|
// [...] if a pointer to T1 can be [cast] to the type pointer to T2.
|
|
|
|
DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
|
|
|
|
SrcType = Self.Context.getPointerType(SrcType);
|
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
|
|
|
|
// the rules for const_cast are the same as those used for pointers.
|
|
|
|
|
|
|
|
if (!DestType->isPointerType() && !DestType->isMemberPointerType()) {
|
|
|
|
// Cannot cast to non-pointer, non-reference type. Note that, if DestType
|
|
|
|
// was a reference type, we converted it to a pointer above.
|
|
|
|
// The status of rvalue references isn't entirely clear, but it looks like
|
|
|
|
// conversion to them is simply invalid.
|
|
|
|
// C++ 5.2.11p3: For two pointer types [...]
|
|
|
|
if (!CStyle)
|
|
|
|
msg = diag::err_bad_const_cast_dest;
|
|
|
|
return TC_NotApplicable;
|
|
|
|
}
|
|
|
|
if (DestType->isFunctionPointerType() ||
|
|
|
|
DestType->isMemberFunctionPointerType()) {
|
|
|
|
// Cannot cast direct function pointers.
|
|
|
|
// C++ 5.2.11p2: [...] where T is any object type or the void type [...]
|
|
|
|
// T is the ultimate pointee of source and target type.
|
|
|
|
if (!CStyle)
|
|
|
|
msg = diag::err_bad_const_cast_dest;
|
|
|
|
return TC_NotApplicable;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
SrcType = Self.Context.getCanonicalType(SrcType);
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
|
|
|
|
// completely equal.
|
|
|
|
// FIXME: const_cast should probably not be able to convert between pointers
|
|
|
|
// to different address spaces.
|
|
|
|
// C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
|
|
|
|
// in multi-level pointers may change, but the level count must be the same,
|
|
|
|
// as must be the final pointee type.
|
|
|
|
while (SrcType != DestType &&
|
|
|
|
Self.UnwrapSimilarPointerTypes(SrcType, DestType)) {
|
2009-12-29 11:05:19 +03:00
|
|
|
Qualifiers Quals;
|
|
|
|
SrcType = Self.Context.getUnqualifiedArrayType(SrcType, Quals);
|
|
|
|
DestType = Self.Context.getUnqualifiedArrayType(DestType, Quals);
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Since we're dealing in canonical types, the remainder must be the same.
|
|
|
|
if (SrcType != DestType)
|
|
|
|
return TC_NotApplicable;
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TryCastResult TryReinterpretCast(Sema &Self, Expr *SrcExpr,
|
|
|
|
QualType DestType, bool CStyle,
|
|
|
|
const SourceRange &OpRange,
|
2009-09-26 04:12:34 +04:00
|
|
|
unsigned &msg,
|
|
|
|
CastExpr::CastKind &Kind) {
|
2009-07-25 19:41:38 +04:00
|
|
|
DestType = Self.Context.getCanonicalType(DestType);
|
|
|
|
QualType SrcType = SrcExpr->getType();
|
2009-07-30 01:53:49 +04:00
|
|
|
if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
|
2009-07-25 19:41:38 +04:00
|
|
|
bool LValue = DestTypeTmp->isLValueReferenceType();
|
|
|
|
if (LValue && SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
|
|
|
|
// Cannot cast non-lvalue to reference type. See the similar comment in
|
|
|
|
// const_cast.
|
|
|
|
msg = diag::err_bad_cxx_cast_rvalue;
|
|
|
|
return TC_NotApplicable;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
|
|
|
|
// C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
|
|
|
|
// same effect as the conversion *reinterpret_cast<T*>(&x) with the
|
|
|
|
// built-in & and * operators.
|
|
|
|
// This code does this transformation for the checked types.
|
|
|
|
DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
|
|
|
|
SrcType = Self.Context.getPointerType(SrcType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Canonicalize source for comparison.
|
|
|
|
SrcType = Self.Context.getCanonicalType(SrcType);
|
|
|
|
|
2009-07-30 01:53:49 +04:00
|
|
|
const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>(),
|
|
|
|
*SrcMemPtr = SrcType->getAs<MemberPointerType>();
|
2009-07-25 19:41:38 +04:00
|
|
|
if (DestMemPtr && SrcMemPtr) {
|
|
|
|
// C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
|
|
|
|
// can be explicitly converted to an rvalue of type "pointer to member
|
|
|
|
// of Y of type T2" if T1 and T2 are both function types or both object
|
|
|
|
// types.
|
|
|
|
if (DestMemPtr->getPointeeType()->isFunctionType() !=
|
|
|
|
SrcMemPtr->getPointeeType()->isFunctionType())
|
|
|
|
return TC_NotApplicable;
|
|
|
|
|
|
|
|
// C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
|
|
|
|
// constness.
|
|
|
|
// A reinterpret_cast followed by a const_cast can, though, so in C-style,
|
|
|
|
// we accept it.
|
|
|
|
if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
|
|
|
|
msg = diag::err_bad_cxx_cast_const_away;
|
|
|
|
return TC_Failed;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
|
|
|
|
// A valid member pointer cast.
|
2009-10-19 00:31:03 +04:00
|
|
|
Kind = CastExpr::CK_BitCast;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// See below for the enumeral issue.
|
|
|
|
if (SrcType->isNullPtrType() && DestType->isIntegralType() &&
|
|
|
|
!DestType->isEnumeralType()) {
|
|
|
|
// C++0x 5.2.10p4: A pointer can be explicitly converted to any integral
|
|
|
|
// type large enough to hold it. A value of std::nullptr_t can be
|
|
|
|
// converted to an integral type; the conversion has the same meaning
|
|
|
|
// and validity as a conversion of (void*)0 to the integral type.
|
|
|
|
if (Self.Context.getTypeSize(SrcType) >
|
|
|
|
Self.Context.getTypeSize(DestType)) {
|
|
|
|
msg = diag::err_bad_reinterpret_cast_small_int;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
2009-09-15 08:48:33 +04:00
|
|
|
Kind = CastExpr::CK_PointerToIntegral;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-09-16 23:19:43 +04:00
|
|
|
bool destIsVector = DestType->isVectorType();
|
|
|
|
bool srcIsVector = SrcType->isVectorType();
|
|
|
|
if (srcIsVector || destIsVector) {
|
|
|
|
bool srcIsScalar = SrcType->isIntegralType() && !SrcType->isEnumeralType();
|
|
|
|
bool destIsScalar =
|
|
|
|
DestType->isIntegralType() && !DestType->isEnumeralType();
|
|
|
|
|
|
|
|
// Check if this is a cast between a vector and something else.
|
|
|
|
if (!(srcIsScalar && destIsVector) && !(srcIsVector && destIsScalar) &&
|
|
|
|
!(srcIsVector && destIsVector))
|
|
|
|
return TC_NotApplicable;
|
|
|
|
|
|
|
|
// If both types have the same size, we can successfully cast.
|
2009-12-23 01:47:22 +03:00
|
|
|
if (Self.Context.getTypeSize(SrcType)
|
|
|
|
== Self.Context.getTypeSize(DestType)) {
|
|
|
|
Kind = CastExpr::CK_BitCast;
|
2009-09-16 23:19:43 +04:00
|
|
|
return TC_Success;
|
2009-12-23 01:47:22 +03:00
|
|
|
}
|
2009-09-16 23:19:43 +04:00
|
|
|
|
|
|
|
if (destIsScalar)
|
|
|
|
msg = diag::err_bad_cxx_cast_vector_to_scalar_different_size;
|
|
|
|
else if (srcIsScalar)
|
|
|
|
msg = diag::err_bad_cxx_cast_scalar_to_vector_different_size;
|
|
|
|
else
|
|
|
|
msg = diag::err_bad_cxx_cast_vector_to_vector_different_size;
|
|
|
|
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
|
|
|
|
2010-02-03 23:32:31 +03:00
|
|
|
bool destIsPtr = DestType->isAnyPointerType();
|
|
|
|
bool srcIsPtr = SrcType->isAnyPointerType();
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!destIsPtr && !srcIsPtr) {
|
|
|
|
// Except for std::nullptr_t->integer and lvalue->reference, which are
|
|
|
|
// handled above, at least one of the two arguments must be a pointer.
|
|
|
|
return TC_NotApplicable;
|
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
if (SrcType == DestType) {
|
|
|
|
// C++ 5.2.10p2 has a note that mentions that, subject to all other
|
|
|
|
// restrictions, a cast to the same type is allowed. The intent is not
|
|
|
|
// entirely clear here, since all other paragraphs explicitly forbid casts
|
|
|
|
// to the same type. However, the behavior of compilers is pretty consistent
|
|
|
|
// on this point: allow same-type conversion if the involved types are
|
|
|
|
// pointers, disallow otherwise.
|
2009-12-23 01:47:22 +03:00
|
|
|
Kind = CastExpr::CK_NoOp;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Note: Clang treats enumeration types as integral types. If this is ever
|
|
|
|
// changed for C++, the additional check here will be redundant.
|
|
|
|
if (DestType->isIntegralType() && !DestType->isEnumeralType()) {
|
|
|
|
assert(srcIsPtr && "One type must be a pointer");
|
|
|
|
// C++ 5.2.10p4: A pointer can be explicitly converted to any integral
|
|
|
|
// type large enough to hold it.
|
|
|
|
if (Self.Context.getTypeSize(SrcType) >
|
|
|
|
Self.Context.getTypeSize(DestType)) {
|
|
|
|
msg = diag::err_bad_reinterpret_cast_small_int;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
2009-09-15 08:48:33 +04:00
|
|
|
Kind = CastExpr::CK_PointerToIntegral;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
if (SrcType->isIntegralType() || SrcType->isEnumeralType()) {
|
|
|
|
assert(destIsPtr && "One type must be a pointer");
|
|
|
|
// C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
|
|
|
|
// converted to a pointer.
|
2009-09-15 08:48:33 +04:00
|
|
|
Kind = CastExpr::CK_IntegralToPointer;
|
2009-07-25 19:41:38 +04:00
|
|
|
return TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
if (!destIsPtr || !srcIsPtr) {
|
|
|
|
// With the valid non-pointer conversions out of the way, we can be even
|
|
|
|
// more stringent.
|
|
|
|
return TC_NotApplicable;
|
2008-11-06 18:59:35 +03:00
|
|
|
}
|
2008-11-06 00:50:06 +03:00
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
|
|
|
|
// The C-style cast operator can.
|
|
|
|
if (!CStyle && CastsAwayConstness(Self, SrcType, DestType)) {
|
|
|
|
msg = diag::err_bad_cxx_cast_const_away;
|
|
|
|
return TC_Failed;
|
|
|
|
}
|
2009-12-09 02:09:15 +03:00
|
|
|
if (CStyle && DestType->isObjCObjectPointerType()) {
|
|
|
|
Kind = CastExpr::CK_AnyPointerToObjCPointerCast;
|
|
|
|
return TC_Success;
|
|
|
|
}
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
// Not casting away constness, so the only remaining check is for compatible
|
|
|
|
// pointer categories.
|
2009-10-19 00:31:03 +04:00
|
|
|
Kind = CastExpr::CK_BitCast;
|
2009-07-25 19:41:38 +04:00
|
|
|
|
|
|
|
if (SrcType->isFunctionPointerType()) {
|
|
|
|
if (DestType->isFunctionPointerType()) {
|
|
|
|
// C++ 5.2.10p6: A pointer to a function can be explicitly converted to
|
|
|
|
// a pointer to a function of a different type.
|
|
|
|
return TC_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
|
|
|
|
// an object type or vice versa is conditionally-supported.
|
|
|
|
// Compilers support it in C++03 too, though, because it's necessary for
|
|
|
|
// casting the return value of dlsym() and GetProcAddress().
|
|
|
|
// FIXME: Conditionally-supported behavior should be configurable in the
|
|
|
|
// TargetInfo or similar.
|
|
|
|
if (!Self.getLangOptions().CPlusPlus0x)
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
|
|
|
|
return TC_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DestType->isFunctionPointerType()) {
|
|
|
|
// See above.
|
|
|
|
if (!Self.getLangOptions().CPlusPlus0x)
|
|
|
|
Self.Diag(OpRange.getBegin(), diag::ext_cast_fn_obj) << OpRange;
|
|
|
|
return TC_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ 5.2.10p7: A pointer to an object can be explicitly converted to
|
|
|
|
// a pointer to an object of different type.
|
|
|
|
// Void pointers are not specified, but supported by every compiler out there.
|
|
|
|
// So we finish by allowing everything that remains - it's got to be two
|
|
|
|
// object pointers.
|
|
|
|
return TC_Success;
|
|
|
|
}
|
|
|
|
|
2009-07-29 17:50:23 +04:00
|
|
|
bool Sema::CXXCheckCStyleCast(SourceRange R, QualType CastTy, Expr *&CastExpr,
|
2010-04-17 02:09:46 +04:00
|
|
|
CastExpr::CastKind &Kind, bool FunctionalStyle) {
|
2009-07-25 19:41:38 +04:00
|
|
|
// This test is outside everything else because it's the only case where
|
|
|
|
// a non-lvalue-reference target type does not lead to decay.
|
|
|
|
// C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
|
2009-10-19 00:31:03 +04:00
|
|
|
if (CastTy->isVoidType()) {
|
|
|
|
Kind = CastExpr::CK_ToVoid;
|
2009-07-25 19:41:38 +04:00
|
|
|
return false;
|
2009-10-19 00:31:03 +04:00
|
|
|
}
|
2009-07-25 19:41:38 +04:00
|
|
|
|
|
|
|
// If the type is dependent, we won't do any other semantic analysis now.
|
|
|
|
if (CastTy->isDependentType() || CastExpr->isTypeDependent())
|
|
|
|
return false;
|
|
|
|
|
2009-11-06 04:14:41 +03:00
|
|
|
if (!CastTy->isLValueReferenceType() && !CastTy->isRecordType())
|
2010-02-03 03:27:59 +03:00
|
|
|
DefaultFunctionArrayLvalueConversion(CastExpr);
|
2009-07-25 19:41:38 +04:00
|
|
|
|
|
|
|
// C++ [expr.cast]p5: The conversions performed by
|
|
|
|
// - a const_cast,
|
|
|
|
// - a static_cast,
|
|
|
|
// - a static_cast followed by a const_cast,
|
|
|
|
// - a reinterpret_cast, or
|
|
|
|
// - a reinterpret_cast followed by a const_cast,
|
|
|
|
// can be performed using the cast notation of explicit type conversion.
|
|
|
|
// [...] If a conversion can be interpreted in more than one of the ways
|
|
|
|
// listed above, the interpretation that appears first in the list is used,
|
|
|
|
// even if a cast resulting from that interpretation is ill-formed.
|
|
|
|
// In plain language, this means trying a const_cast ...
|
|
|
|
unsigned msg = diag::err_bad_cxx_cast_generic;
|
2009-09-02 00:52:42 +04:00
|
|
|
TryCastResult tcr = TryConstCast(*this, CastExpr, CastTy, /*CStyle*/true,
|
|
|
|
msg);
|
2009-10-19 22:14:28 +04:00
|
|
|
if (tcr == TC_Success)
|
|
|
|
Kind = CastExpr::CK_NoOp;
|
|
|
|
|
2009-07-25 19:41:38 +04:00
|
|
|
if (tcr == TC_NotApplicable) {
|
|
|
|
// ... or if that is not possible, a static_cast, ignoring const, ...
|
2010-04-17 02:09:46 +04:00
|
|
|
tcr = TryStaticCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg, Kind);
|
2009-07-25 19:41:38 +04:00
|
|
|
if (tcr == TC_NotApplicable) {
|
|
|
|
// ... and finally a reinterpret_cast, ignoring const.
|
2009-09-26 04:12:34 +04:00
|
|
|
tcr = TryReinterpretCast(*this, CastExpr, CastTy, /*CStyle*/true, R, msg,
|
|
|
|
Kind);
|
2009-07-25 19:41:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tcr != TC_Success && msg != 0)
|
2009-07-29 17:50:23 +04:00
|
|
|
Diag(R.getBegin(), msg) << (FunctionalStyle ? CT_Functional : CT_CStyle)
|
2009-07-25 19:41:38 +04:00
|
|
|
<< CastExpr->getType() << CastTy << R;
|
|
|
|
|
|
|
|
return tcr != TC_Success;
|
2008-11-06 00:50:06 +03:00
|
|
|
}
|