]> granicus.if.org Git - clang/commitdiff
Code gen for '.*' binary expressions - WIP.
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 21 Oct 2009 23:45:42 +0000 (23:45 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 21 Oct 2009 23:45:42 +0000 (23:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84800 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprScalar.cpp
test/CodeGenCXX/ptr-to-datamember.cpp

index c3598edd8b8b2cd4f4472005c1b860f7a1e752a1..0042754f98c0ca65fd64d94b5b2a1941fb102ecb 100644 (file)
@@ -106,6 +106,8 @@ public:
     return 0;
   }
   Value *VisitExpr(Expr *S);
+  Value *VisitPointerToDataMemberBinaryExpr(const BinaryOperator *BExpr);
+  
   Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
 
   // Leaves.
@@ -536,12 +538,37 @@ EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
 //===----------------------------------------------------------------------===//
 
 Value *ScalarExprEmitter::VisitExpr(Expr *E) {
+  if (const BinaryOperator *BExpr = dyn_cast<BinaryOperator>(E))
+    if (BExpr->getOpcode() == BinaryOperator::PtrMemD)
+      return VisitPointerToDataMemberBinaryExpr(BExpr);
+  
   CGF.ErrorUnsupported(E, "scalar expression");
   if (E->getType()->isVoidType())
     return 0;
   return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
 }
 
+Value *ScalarExprEmitter::VisitPointerToDataMemberBinaryExpr(
+                                                    const BinaryOperator *E) {
+  Value *BaseV = EmitLValue(E->getLHS()).getAddress();
+  const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
+  BaseV = Builder.CreateBitCast(BaseV, i8Ty);
+  Value *OffsetV = EmitLoadOfLValue(E->getRHS());
+  const llvm::Type* ResultType = ConvertType(
+                                        CGF.getContext().getPointerDiffType());
+  OffsetV = Builder.CreateBitCast(OffsetV, ResultType);
+  Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
+  QualType Ty = E->getRHS()->getType();
+  const MemberPointerType *MemPtrType = Ty->getAs<MemberPointerType>();
+  Ty = MemPtrType->getPointeeType();
+  const llvm::Type* PType = 
+  ConvertType(CGF.getContext().getPointerType(Ty));
+  AddV = Builder.CreateBitCast(AddV, PType);
+  LValue LV = LValue::MakeAddr(AddV, CGF.MakeQualifiers(Ty));
+  Value *InVal = CGF.EmitLoadOfLValue(LV, Ty).getScalarVal();
+  return InVal;
+}
+
 Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
   llvm::SmallVector<llvm::Constant*, 32> indices;
   for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
index 7e945a20dcdb167ae7fdc3feeda8364332f54317..b0d7ddc7796fc9e2bb2490caa76d543af6b7c495 100644 (file)
@@ -19,6 +19,7 @@ struct B1  : virtual V{
 
 class A  : public B, public B1 {
 public:
+  A() : f(1.0), d(2.0), Ai(3) {}
   float f;
   double d;
   int Ai;
@@ -38,6 +39,5 @@ int main()
   printf("%d\n", &A::B1::iV);
   printf("%d\n", &A::B::V::iV);
   printf("%d\n", &A::B1::V::iV);
-  // FIXME. NYI
-  //  printf(" %d, %f, %f  \n", a1.*pa, a1.f, a1.d);
+  printf("%d, %f, %f  \n", a1.*pa, a1.*pf, a1.*pd);
 }