[cfe-commits] r83264 - in /cfe/trunk: lib/CodeGen/CGExprConstant.cpp test/CodeGenCXX/member-function-pointers.cpp

Anders Carlsson andersca at mac.com
Sat Oct 3 08:02:02 PDT 2009


Author: andersca
Date: Sat Oct  3 10:02:02 2009
New Revision: 83264

URL: http://llvm.org/viewvc/llvm-project?rev=83264&view=rev
Log:
Handle members to function pointers in CGExprConstant.

Modified:
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/test/CodeGenCXX/member-function-pointers.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=83264&r1=83263&r2=83264&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Sat Oct  3 10:02:02 2009
@@ -402,7 +402,50 @@
   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: {

Modified: cfe/trunk/test/CodeGenCXX/member-function-pointers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/member-function-pointers.cpp?rev=83264&r1=83263&r2=83264&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/member-function-pointers.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/member-function-pointers.cpp Sat Oct  3 10:02:02 2009
@@ -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 (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 @@
   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;
 }





More information about the cfe-commits mailing list