diff options
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/CodeGen/CGExprConstant.cpp | 43 | ||||
-rw-r--r-- | clang/test/CodeGenCXX/member-function-pointers.cpp | 14 |
2 files changed, 53 insertions, 4 deletions
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index c67b41f4e2d..144fa28e19b 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/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<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: { diff --git a/clang/test/CodeGenCXX/member-function-pointers.cpp b/clang/test/CodeGenCXX/member-function-pointers.cpp index bf24aea9fc4..38c13d29195 100644 --- a/clang/test/CodeGenCXX/member-function-pointers.cpp +++ b/clang/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; } |