Patch to provide cast of objects in member access
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 29 Jul 2009 18:40:24 +0000 (18:40 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 29 Jul 2009 18:40:24 +0000 (18:40 +0000)
excpression, if needed, and remove some ir-gen code
now unnencessary.

llvm-svn: 77490

clang/lib/CodeGen/CGExpr.cpp
clang/lib/Sema/Sema.h
clang/lib/Sema/SemaExpr.cpp
clang/test/CodeGenCXX/constructor-init.cpp

index 39ef799..bf3a0ae 100644 (file)
@@ -993,13 +993,6 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
     if (PTy->getPointeeType()->isUnionType())
       isUnion = true;
     CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
-    if (const CXXRecordDecl *ClassDecl = 
-          BaseExpr->getType()->getCXXRecordDeclForPointerType()) {
-      FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
-      if (const CXXRecordDecl *BaseClassDecl = 
-          dyn_cast<CXXRecordDecl>(Field->getDeclContext()))
-        BaseValue = AddressCXXOfBaseClass(BaseValue, ClassDecl, BaseClassDecl);
-    }
   } else if (isa<ObjCPropertyRefExpr>(BaseExpr) ||
              isa<ObjCKVCRefExpr>(BaseExpr)) {
     RValue RV = EmitObjCPropertyGet(BaseExpr);
@@ -1019,15 +1012,6 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
     if (BaseTy->isUnionType())
       isUnion = true;
     CVRQualifiers = BaseTy.getCVRQualifiers();
-    if (const CXXRecordDecl *ClassDecl =
-          dyn_cast<CXXRecordDecl>(
-                        BaseTy->getAsRecordType()->getDecl())) {
-        FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
-        if (const CXXRecordDecl *BaseClassDecl = 
-            dyn_cast<CXXRecordDecl>(Field->getDeclContext()))
-            BaseValue = 
-              AddressCXXOfBaseClass(BaseValue, ClassDecl, BaseClassDecl);
-    }
   }
 
   FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
index 79ea3e1..1a2c821 100644 (file)
@@ -690,6 +690,8 @@ public:
 
   ImplicitConversionSequence TryContextuallyConvertToBool(Expr *From);
   bool PerformContextuallyConvertToBool(Expr *&From);
+  
+  void PerformObjectMemberConversion(Expr *&From, NamedDecl *Member);
 
   /// OverloadingResult - Capture the result of performing overload
   /// resolution.
index f9abb3d..6d21863 100644 (file)
@@ -1023,6 +1023,22 @@ Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc,
   
   return BuildDeclarationNameExpr(Loc, D, HasTrailingLParen, SS, isAddressOfOperand);
 }
+/// \brief Cast member's object to its own class if necessary.
+void
+Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) {
+  if (FieldDecl *FD = dyn_cast<FieldDecl>(Member))
+    if (CXXRecordDecl *RD = 
+        dyn_cast<CXXRecordDecl>(FD->getDeclContext())) {
+      QualType DestType = 
+        Context.getCanonicalType(Context.getTypeDeclType(RD));
+      if (!DestType->isDependentType() &&
+          !From->getType()->isDependentType()) {
+        if (From->getType()->getAsPointerType())
+          DestType = Context.getPointerType(DestType);
+        ImpCastExprToType(From, DestType, /*isLvalue=*/true);
+      }
+    }
+}
 
 /// \brief Complete semantic analysis for a reference to the given declaration.
 Sema::OwningExprResult
@@ -1114,6 +1130,7 @@ Sema::BuildDeclarationNameExpr(SourceLocation Loc, NamedDecl *D,
           Expr *This = new (Context) CXXThisExpr(SourceLocation(),
                                                  MD->getThisType(Context));
           MarkDeclarationReferenced(Loc, D);
+          PerformObjectMemberConversion(This, D);
           return Owned(new (Context) MemberExpr(This, true, D,
                                                 Loc, MemberType));
         }
@@ -2190,6 +2207,7 @@ Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc,
       }
 
       MarkDeclarationReferenced(MemberLoc, FD);
+      PerformObjectMemberConversion(BaseExpr, FD);
       return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD,
                                             MemberLoc, MemberType));
     }
index c46a561..a9d5c28 100644 (file)
@@ -38,8 +38,10 @@ struct N : M , P, Q {
              printf("iQ = %d\n", iQ);
              printf("iP = %d\n", iP);
               printf("iM = %d\n", iM);
-             printf("iQ = %d\n", (*this).iQ);
-             printf("iP = %d\n", ((*this)).iP);
+             // FIXME. We don't yet support this syntax.
+             // printf("iQ = %d\n", (*this).iQ);
+             printf("iQ = %d\n", this->iQ);
+             printf("iP = %d\n", this->iP);
               printf("iM = %d\n", this->iM);
             }
   float ld;