]> granicus.if.org Git - clang/commitdiff
Code gen for pointer-to-datamember - WIP.
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 21 Oct 2009 18:38:00 +0000 (18:38 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 21 Oct 2009 18:38:00 +0000 (18:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84771 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExpr.cpp
lib/CodeGen/CodeGenFunction.h
test/CodeGenCXX/ptr-to-datamember.cpp [new file with mode: 0644]

index d3d1c61fe99f2c4c3798569e1100ebbf4261c505..2266b25c682ffd105f800bf9581ef5db0d634278 100644 (file)
@@ -866,6 +866,9 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
     llvm::Value *V = LocalDeclMap[IPD];
     assert(V && "BlockVarDecl not entered in LocalDeclMap?");
     return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
+  } else if (const QualifiedDeclRefExpr *QDRExpr = 
+             dyn_cast<QualifiedDeclRefExpr>(E)) {
+    return EmitPointerToDataMemberLValue(QDRExpr);
   }
   assert(0 && "Unimp declref");
   //an invalid LValue, but the assert will
@@ -1513,6 +1516,24 @@ LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
 }
 
 
+LValue CodeGenFunction::EmitPointerToDataMemberLValue(
+                                              const QualifiedDeclRefExpr *E) {
+  const FieldDecl *Field = cast<FieldDecl>(E->getDecl());
+  const NestedNameSpecifier *NNSpec = E->getQualifier();
+  const Type *NNSpecType = NNSpec->getAsType();
+  QualType NNSpecTy = getContext().getCanonicalType(QualType(NNSpecType, 0));
+  NNSpecTy = getContext().getPointerType(NNSpecTy);
+  llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
+  LValue MemExpLV = EmitLValueForField(V, const_cast<FieldDecl*>(Field), 
+                                       /*isUnion*/false, /*Qualifiers*/0);
+  const llvm::Type* ResultType = ConvertType(
+                                             getContext().getPointerDiffType());
+  V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, 
+                             "datamember");
+  LValue LV = LValue::MakeAddr(V, MakeQualifiers(E->getType()));
+  return LV;
+}
+
 RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
                                  CallExpr::const_arg_iterator ArgBeg,
                                  CallExpr::const_arg_iterator ArgEnd,
index 00b07e4b682658d6150592f91521b423e32d649a..3e65e47024bd29304c80b21bdc5b74172c7cbc4c 100644 (file)
@@ -819,6 +819,7 @@ public:
   LValue EmitConditionalOperatorLValue(const ConditionalOperator *E);
   LValue EmitCastLValue(const CastExpr *E);
   LValue EmitNullInitializationLValue(const CXXZeroInitValueExpr *E);
+  LValue EmitPointerToDataMemberLValue(const QualifiedDeclRefExpr *E);
   
   llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
                               const ObjCIvarDecl *Ivar);
diff --git a/test/CodeGenCXX/ptr-to-datamember.cpp b/test/CodeGenCXX/ptr-to-datamember.cpp
new file mode 100644 (file)
index 0000000..86c771a
--- /dev/null
@@ -0,0 +1,25 @@
+// RUN: clang-cc -emit-llvm -o - %s
+
+extern "C" int printf(...);
+
+class A {
+public:
+  A() : f(1.0), d(2.0), Ai(100) {}
+  float f;
+  double d;
+  int Ai;
+}; 
+
+int main() 
+{
+  A a1;
+  int A::* pa = &A::Ai;
+  float A::* pf = &A::f;
+  double A::* pd = &A::d;
+  printf("%d %d %d\n", &A::Ai, &A::f, &A::d);
+
+  // FIXME. NYI
+  //  printf(" %d, %f, %f  \n", a1.*pa, a1.f, a1.d);
+}
+
+