]> granicus.if.org Git - clang/commitdiff
Calculate offset correctly when taking the address of a virtual member function.
authorAnders Carlsson <andersca@mac.com>
Thu, 4 Feb 2010 16:38:05 +0000 (16:38 +0000)
committerAnders Carlsson <andersca@mac.com>
Thu, 4 Feb 2010 16:38:05 +0000 (16:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95305 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprAgg.cpp
lib/CodeGen/CGExprConstant.cpp
test/CodeGenCXX/member-function-pointers.cpp

index cb6e1d814030b26ef740d7bbe4a9f7e74d78ef3c..928cb616b2495e54a740be982a16315208853511 100644 (file)
@@ -327,7 +327,11 @@ void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) {
     int64_t Index = 
       CGF.CGM.getVtableInfo().getMethodVtableIndex(MD);
     
-    FuncPtr = llvm::ConstantInt::get(PtrDiffTy, Index + 1);
+    // Itanium C++ ABI 2.3:
+    //   For a non-virtual function, this field is a simple function pointer. 
+    //   For a virtual function, it is 1 plus the virtual table offset 
+    //   (in bytes) of the function, represented as a ptrdiff_t. 
+    FuncPtr = llvm::ConstantInt::get(PtrDiffTy, (Index * 8) + 1);
   } else {
     FuncPtr = llvm::ConstantExpr::getPtrToInt(CGF.CGM.GetAddrOfFunction(MD), 
                                               PtrDiffTy);
index 81209da6c6fb37f74266cea5e7bd1e741c017afc..a358b54fa3c6ede71630500b1d0176351db6bf2d 100644 (file)
@@ -418,8 +418,11 @@ public:
     // Get the function pointer (or index if this is a virtual function).
     if (MD->isVirtual()) {
       uint64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
-      
-      // The pointer is 1 + the virtual table offset in bytes.
+
+      // Itanium C++ ABI 2.3:
+      //   For a non-virtual function, this field is a simple function pointer. 
+      //   For a virtual function, it is 1 plus the virtual table offset 
+      //   (in bytes) of the function, represented as a ptrdiff_t. 
       Values[0] = llvm::ConstantInt::get(PtrDiffTy, (Index * 8) + 1);
     } else {
       llvm::Constant *FuncPtr = CGM.GetAddrOfFunction(MD);
index e1353a72b396cfb721a1faedd5ef8de123a5364a..b69aea7fb6e152b6c58bf6a8af4956c691c7e027 100644 (file)
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-apple-darwin9 | FileCheck %s
 
-struct A { int a; void f(); virtual void vf(); };
+struct A { int a; void f(); virtual void vf1(); virtual void vf2(); };
 struct B { int b; virtual void g(); };
 struct C : B, A { };
 
@@ -13,13 +13,16 @@ void (C::*pc)();
 void (A::*pa2)() = &A::f;
 
 // CHECK: @pa3 = global %0 { i64 1, i64 0 }, align 8
-void (A::*pa3)() = &A::vf;
+void (A::*pa3)() = &A::vf1;
+
+// CHECK: @pa4 = global %0 { i64 9, i64 0 }, align 8
+void (A::*pa4)() = &A::vf2;
 
 // CHECK: @pc2 = global %0 { i64 ptrtoint (void ()* @_ZN1A1fEv to i64), i64 16 }, align 8
 void (C::*pc2)() = &C::f;
 
 // CHECK: @pc3 = global %0 { i64 1, i64 0 }, align 8
-void (A::*pc3)() = &A::vf;
+void (A::*pc3)() = &A::vf1;
 
 void f() {
   // CHECK: store i64 0, i64* getelementptr inbounds (%0* @pa, i32 0, i32 0)
@@ -52,7 +55,13 @@ void f2() {
   // CHECK: store i64 1, i64* [[pa3ptr]]
   // CHECK: [[pa3adj:%[a-zA-Z0-9\.]+]] = getelementptr inbounds %0* %pa3, i32 0, i32 1
   // CHECK: store i64 0, i64* [[pa3adj]]
-  void (A::*pa3)() = &A::vf;
+  void (A::*pa3)() = &A::vf1;
+  
+  // CHECK: [[pa4ptr:%[a-zA-Z0-9\.]+]] = getelementptr inbounds %0* %pa4, i32 0, i32 0 
+  // CHECK: store i64 9, i64* [[pa4ptr]]
+  // CHECK: [[pa4adj:%[a-zA-Z0-9\.]+]] = getelementptr inbounds %0* %pa4, i32 0, i32 1
+  // CHECK: store i64 0, i64* [[pa4adj]]
+  void (A::*pa4)() = &A::vf2;
 }
 
 void f3(A *a, A &ar) {