[cfe-commits] r80433 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp lib/CodeGen/CGExprAgg.cpp lib/CodeGen/CGExprScalar.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaExprCXX.cpp test/CodeGenCXX/conversion-function.cpp

Fariborz Jahanian fjahanian at apple.com
Sat Aug 29 12:15:16 PDT 2009


Author: fjahanian
Date: Sat Aug 29 14:15:16 2009
New Revision: 80433

URL: http://llvm.org/viewvc/llvm-project?rev=80433&view=rev
Log:
Patch for code gen. for c-style cast which ends in
using class's conversion functions [12.3.2-p2]

Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/test/CodeGenCXX/conversion-function.cpp

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Sat Aug 29 14:15:16 2009
@@ -1173,8 +1173,13 @@
 /// noop aggregate casts, and cast from scalar to union.
 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
   if (E->getCastKind() == CastExpr::CK_UserDefinedConversion) {
-    const CXXFunctionalCastExpr *CXXFExpr = cast<CXXFunctionalCastExpr>(E);
-    return  LValue::MakeAddr(EmitCXXFunctionalCastExpr(CXXFExpr).getScalarVal(), 0);
+    if (const CXXFunctionalCastExpr *CXXFExpr = 
+          dyn_cast<CXXFunctionalCastExpr>(E))
+      return  LValue::MakeAddr(
+                EmitCXXFunctionalCastExpr(CXXFExpr).getScalarVal(), 0);
+    assert(isa<CStyleCastExpr>(E) && 
+           "EmitCastLValue - Expected CStyleCastExpr");
+    return EmitLValue(E->getSubExpr());
   }
   
   // If this is an aggregate-to-aggregate cast, just use the input's address as
@@ -1188,10 +1193,6 @@
     if (ICE->isLvalueCast())
       return EmitLValue(E->getSubExpr());
 
-  // FIXME: Implement this properly!
-  if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
-    return EmitUnsupportedLValue(E, "user-defined conversion");
-
   // Otherwise, we must have a cast from scalar to union.
   assert(E->getCastKind() == CastExpr::CK_ToUnion &&
          "Expected scalar-to-union cast");

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Sat Aug 29 14:15:16 2009
@@ -178,8 +178,12 @@
     return;
   }
   if (E->getCastKind() == CastExpr::CK_UserDefinedConversion) {
-    CXXFunctionalCastExpr *CXXFExpr = cast<CXXFunctionalCastExpr>(E);
-    CGF.EmitCXXFunctionalCastExpr(CXXFExpr);
+    if (const CXXFunctionalCastExpr *CXXFExpr = 
+          dyn_cast<CXXFunctionalCastExpr>(E))
+      CGF.EmitCXXFunctionalCastExpr(CXXFExpr);
+    else 
+      if (isa<CStyleCastExpr>(E))
+        Visit(E->getSubExpr());
     return;
   }
   

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Sat Aug 29 14:15:16 2009
@@ -228,8 +228,11 @@
   }
   Value *VisitCastExpr(const CastExpr *E) {
     if (E->getCastKind() == CastExpr::CK_UserDefinedConversion) {
-      const CXXFunctionalCastExpr *CXXFExpr = cast<CXXFunctionalCastExpr>(E);
-      return CGF.EmitCXXFunctionalCastExpr(CXXFExpr).getScalarVal();
+      if (const CXXFunctionalCastExpr *CXXFExpr = 
+            dyn_cast<CXXFunctionalCastExpr>(E))
+        return CGF.EmitCXXFunctionalCastExpr(CXXFExpr).getScalarVal();
+      assert(isa<CStyleCastExpr>(E) && 
+             "VisitCastExpr - missing CStyleCastExpr");
     }
       
     // Make sure to evaluate VLA bounds now so that we have them for later.

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=80433&r1=80432&r2=80433&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Aug 29 14:15:16 2009
@@ -3060,6 +3060,19 @@
   if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, 
                      Kind, ConversionDecl))
     return ExprError();
+  if (ConversionDecl) {
+    // encounterred a c-style cast requiring a conversion function.
+    if (CXXConversionDecl *CD = dyn_cast<CXXConversionDecl>(ConversionDecl)) {
+      castExpr = 
+        new (Context) CXXFunctionalCastExpr(castType.getNonReferenceType(),
+                                            castType, LParenLoc, 
+                                            CastExpr::CK_UserDefinedConversion,
+                                            castExpr, CD,
+                                            RParenLoc);
+      Kind = CastExpr::CK_UserDefinedConversion;
+    }
+    // FIXME. AST for when dealing with conversion functions (FunctionDecl).
+  }
   
   Op.release();
   return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(),

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=80433&r1=80432&r2=80433&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Sat Aug 29 14:15:16 2009
@@ -939,6 +939,7 @@
         OwningExprResult InitResult = 
           BuildCXXConstructExpr(ToType.getNonReferenceType(), 
                                 CD, &From, 1);
+        // Take ownership of this expression.
         From = InitResult.takeAs<Expr>();
         CastKind = CastExpr::CK_ConstructorConversion ;
       }

Modified: cfe/trunk/test/CodeGenCXX/conversion-function.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/conversion-function.cpp?rev=80433&r1=80432&r2=80433&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/conversion-function.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/conversion-function.cpp Sat Aug 29 14:15:16 2009
@@ -49,6 +49,15 @@
     i = int(X(Z(y)));
     f = float(X(Z(y)));
     printf("i = %d float = %f\n", i,f);
+    f = (float)x;
+    i = (int)x;
+    printf("i = %d float = %f\n", i,f);
+
+    int d = (X)((Z)y);
+    printf("d = %d\n", d);
+
+    int e = (int)((X)((Z)y));
+    printf("e = %d\n", e);
 }
 // CHECK-LP64: .globl  __ZN1ScviEv
 // CHECK-LP64-NEXT: __ZN1ScviEv:





More information about the cfe-commits mailing list