Skip to content

Commit c934bc8

Browse files
committed
Perform overload resolution when static_cast'ing from a
pointer-to-member-to-derived to a pointer-to-member-to-base. Fixes PR6072. llvm-svn: 97923
1 parent 1ce52ca commit c934bc8

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

clang/lib/CodeGen/CGExprAgg.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
178178
//===----------------------------------------------------------------------===//
179179

180180
void AggExprEmitter::VisitCastExpr(CastExpr *E) {
181+
if (!DestPtr) {
182+
Visit(E->getSubExpr());
183+
return;
184+
}
185+
181186
switch (E->getCastKind()) {
182187
default: assert(0 && "Unhandled cast kind!");
183188

@@ -205,6 +210,11 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {
205210
break;
206211

207212
case CastExpr::CK_NullToMemberPointer: {
213+
// If the subexpression's type is the C++0x nullptr_t, emit the
214+
// subexpression, which may have side effects.
215+
if (E->getSubExpr()->getType()->isNullPtrType())
216+
Visit(E->getSubExpr());
217+
208218
const llvm::Type *PtrDiffTy =
209219
CGF.ConvertType(CGF.getContext().getPointerDiffType());
210220

clang/lib/Sema/SemaCXXCast.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ static TryCastResult TryStaticDowncast(Sema &Self, CanQualType SrcType,
8484
QualType OrigSrcType,
8585
QualType OrigDestType, unsigned &msg,
8686
CastExpr::CastKind &Kind);
87-
static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType,
87+
static TryCastResult TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr,
88+
QualType SrcType,
8889
QualType DestType,bool CStyle,
8990
const SourceRange &OpRange,
9091
unsigned &msg,
@@ -554,7 +555,7 @@ static TryCastResult TryStaticCast(Sema &Self, Expr *&SrcExpr,
554555
// Reverse member pointer conversion. C++ 4.11 specifies member pointer
555556
// conversion. C++ 5.2.9p9 has additional information.
556557
// DR54's access restrictions apply here also.
557-
tcr = TryStaticMemberPointerUpcast(Self, SrcType, DestType, CStyle,
558+
tcr = TryStaticMemberPointerUpcast(Self, SrcExpr, SrcType, DestType, CStyle,
558559
OpRange, msg, Kind);
559560
if (tcr != TC_NotApplicable)
560561
return tcr;
@@ -798,12 +799,23 @@ TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType,
798799
/// where B is a base class of D [...].
799800
///
800801
TryCastResult
801-
TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType, QualType DestType,
802-
bool CStyle, const SourceRange &OpRange,
802+
TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType,
803+
QualType DestType, bool CStyle,
804+
const SourceRange &OpRange,
803805
unsigned &msg, CastExpr::CastKind &Kind) {
804806
const MemberPointerType *DestMemPtr = DestType->getAs<MemberPointerType>();
805807
if (!DestMemPtr)
806808
return TC_NotApplicable;
809+
810+
bool WasOverloadedFunction = false;
811+
if (FunctionDecl *Fn
812+
= Self.ResolveAddressOfOverloadedFunction(SrcExpr, DestType, false)) {
813+
CXXMethodDecl *M = cast<CXXMethodDecl>(Fn);
814+
SrcType = Self.Context.getMemberPointerType(Fn->getType(),
815+
Self.Context.getTypeDeclType(M->getParent()).getTypePtr());
816+
WasOverloadedFunction = true;
817+
}
818+
807819
const MemberPointerType *SrcMemPtr = SrcType->getAs<MemberPointerType>();
808820
if (!SrcMemPtr) {
809821
msg = diag::err_bad_static_cast_member_pointer_nonmp;
@@ -853,6 +865,24 @@ TryStaticMemberPointerUpcast(Sema &Self, QualType SrcType, QualType DestType,
853865
return TC_Failed;
854866
}
855867

868+
if (WasOverloadedFunction) {
869+
// Resolve the address of the overloaded function again, this time
870+
// allowing complaints if something goes wrong.
871+
FunctionDecl *Fn = Self.ResolveAddressOfOverloadedFunction(SrcExpr,
872+
DestType,
873+
true);
874+
if (!Fn) {
875+
msg = 0;
876+
return TC_Failed;
877+
}
878+
879+
SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, Fn);
880+
if (!SrcExpr) {
881+
msg = 0;
882+
return TC_Failed;
883+
}
884+
}
885+
856886
Kind = CastExpr::CK_DerivedToBaseMemberPointer;
857887
return TC_Success;
858888
}

clang/test/CodeGenCXX/nullptr.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
// RUN: %clang_cc1 -std=c++0x %s -emit-llvm -o %t
1+
// RUN: %clang_cc1 -std=c++0x -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
22

33
int* a = nullptr;
44

55
void f() {
66
int* a = nullptr;
77
}
8+
9+
typedef decltype(nullptr) nullptr_t;
10+
11+
nullptr_t get_nullptr();
12+
13+
struct X { };
14+
void g() {
15+
// CHECK: call i8* @_Z11get_nullptrv()
16+
int (X::*pmf)(int) = get_nullptr();
17+
}

clang/test/SemaCXX/static-cast.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,17 @@ struct X4 {
181181

182182
// PR5897 - accept static_cast from const void* to const int (*)[1].
183183
void PR5897() { (void)static_cast<const int(*)[1]>((const void*)0); }
184+
185+
namespace PR6072 {
186+
struct A { };
187+
struct B : A { void f(int); void f(); };
188+
struct C : B { };
189+
struct D { };
190+
191+
void f() {
192+
(void)static_cast<void (A::*)()>(&B::f);
193+
(void)static_cast<void (B::*)()>(&B::f);
194+
(void)static_cast<void (C::*)()>(&B::f);
195+
(void)static_cast<void (D::*)()>(&B::f); // expected-error{{static_cast from '<overloaded function type>' to 'void (struct PR6072::D::*)()' is not allowed}}
196+
}
197+
}

0 commit comments

Comments
 (0)