Handle members to function pointers in CGExprConstant.

llvm-svn: 83264
This commit is contained in:
Anders Carlsson 2009-10-03 15:02:02 +00:00
parent 32bfb1ce8f
commit 128a5d5147
2 changed files with 53 additions and 4 deletions

View File

@ -402,7 +402,50 @@ public:
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
return Visit(E->getInitializer());
}
llvm::Constant *EmitMemberFunctionPointer(CXXMethodDecl *MD) {
assert(MD->isInstance() && "Member function must not be static!");
const llvm::Type *PtrDiffTy =
CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
llvm::Constant *Values[2];
// Get the function pointer (or index if this is a virtual function).
if (MD->isVirtual()) {
uint64_t Index = CGM.GetVtableIndex(MD);
Values[0] = llvm::ConstantInt::get(PtrDiffTy, Index + 1);
} else {
llvm::Constant *FuncPtr = CGM.GetAddrOfFunction(MD);
Values[0] = llvm::ConstantExpr::getPtrToInt(FuncPtr, PtrDiffTy);
}
// The adjustment will always be 0.
Values[1] = llvm::ConstantInt::get(PtrDiffTy, 0);
return llvm::ConstantStruct::get(CGM.getLLVMContext(),
Values, 2, /*Packed=*/false);
}
llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) {
if (const MemberPointerType *MPT =
E->getType()->getAs<MemberPointerType>()) {
QualType T = MPT->getPointeeType();
if (T->isFunctionProtoType()) {
QualifiedDeclRefExpr *DRE = cast<QualifiedDeclRefExpr>(E->getSubExpr());
return EmitMemberFunctionPointer(cast<CXXMethodDecl>(DRE->getDecl()));
}
// FIXME: Should we handle other member pointer types here too,
// or should they be handled by Expr::Evaluate?
}
return 0;
}
llvm::Constant *VisitCastExpr(CastExpr* E) {
switch (E->getCastKind()) {
case CastExpr::CK_ToUnion: {

View File

@ -1,7 +1,7 @@
// RUN: clang-cc %s -emit-llvm -o - -triple=x86_64-apple-darwin9 | FileCheck %s
struct A { int a; };
struct B { int b; };
struct A { int a; void f(); virtual void vf(); };
struct B { int b; virtual void g(); };
struct C : B, A { };
void (A::*pa)();
@ -9,6 +9,12 @@ void (A::*volatile vpa)();
void (B::*pb)();
void (C::*pc)();
// CHECK: @pa2 = global %0 { i64 ptrtoint (void ()* @_ZN1A1fEv to i64), i64 0 }, align 8
void (A::*pa2)() = &A::f;
// CHECK: @pa3 = global %0 { i64 1, i64 0 }, align 8
void (A::*pa3)() = &A::vf;
void f() {
// CHECK: store i64 0, i64* getelementptr inbounds (%0* @pa, i32 0, i32 0)
// CHECK: store i64 0, i64* getelementptr inbounds (%0* @pa, i32 0, i32 1)
@ -19,7 +25,7 @@ void f() {
vpa = 0;
// CHECK: store i64 %0, i64* getelementptr inbounds (%0* @pc, i32 0, i32 0)
// CHECK: [[ADJ:%[a-zA-Z0-9]+]] = add i64 %1, 4
// CHECK: [[ADJ:%[a-zA-Z0-9]+]] = add i64 %1, 16
// CHECK: store i64 [[ADJ]], i64* getelementptr inbounds (%0* @pc, i32 0, i32 1)
pc = pa;
pc = pa;
}