From: Anders Carlsson Date: Sat, 3 Oct 2009 15:02:02 +0000 (+0000) Subject: Handle members to function pointers in CGExprConstant. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f57b4e44d03266f2ec1b427645555ed8952bc830;p=clang Handle members to function pointers in CGExprConstant. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83264 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index c67b41f4e2..144fa28e19 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -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()) { + QualType T = MPT->getPointeeType(); + if (T->isFunctionProtoType()) { + QualifiedDeclRefExpr *DRE = cast(E->getSubExpr()); + + return EmitMemberFunctionPointer(cast(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: { diff --git a/test/CodeGenCXX/member-function-pointers.cpp b/test/CodeGenCXX/member-function-pointers.cpp index bf24aea9fc..38c13d2919 100644 --- a/test/CodeGenCXX/member-function-pointers.cpp +++ b/test/CodeGenCXX/member-function-pointers.cpp @@ -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; }