[cfe-commits] r81837 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/AST/ExprCXX.h lib/AST/Expr.cpp lib/CodeGen/CGExprScalar.cpp lib/Sema/SemaCXXCast.cpp test/CodeGenCXX/reinterpret-cast.cpp

Anders Carlsson andersca at mac.com
Mon Sep 14 21:48:34 PDT 2009


Author: andersca
Date: Mon Sep 14 23:48:33 2009
New Revision: 81837

URL: http://llvm.org/viewvc/llvm-project?rev=81837&view=rev
Log:
Handle reinterpret_cast between integral types and pointer types.

Added:
    cfe/trunk/test/CodeGenCXX/reinterpret-cast.cpp
Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/AST/ExprCXX.h
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/Sema/SemaCXXCast.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=81837&r1=81836&r2=81837&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Sep 14 23:48:33 2009
@@ -1368,7 +1368,13 @@
     CK_UserDefinedConversion,
 
     /// CK_ConstructorConversion - Conversion by constructor
-    CK_ConstructorConversion
+    CK_ConstructorConversion,
+    
+    /// CK_IntegralToPointer - Integral to pointer
+    CK_IntegralToPointer,
+    
+    /// CK_PointerToIntegral - Pointer to integral
+    CK_PointerToIntegral
   };
 
   struct CastInfo {

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=81837&r1=81836&r2=81837&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Mon Sep 14 23:48:33 2009
@@ -185,9 +185,9 @@
 /// @c reinterpret_cast<int>(VoidPtr).
 class CXXReinterpretCastExpr : public CXXNamedCastExpr {
 public:
-  CXXReinterpretCastExpr(QualType ty, Expr *op, QualType writtenTy,
-                         SourceLocation l)
-    : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, CK_BitCast, op,
+  CXXReinterpretCastExpr(QualType ty, CastKind kind, Expr *op, 
+                         QualType writtenTy, SourceLocation l)
+    : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, kind, op,
                        writtenTy, l) {}
 
   static bool classof(const Stmt *T) {

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=81837&r1=81836&r2=81837&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Sep 14 23:48:33 2009
@@ -422,6 +422,10 @@
     return "UserDefinedConversion";
   case CastExpr::CK_ConstructorConversion:
     return "ConstructorConversion";
+  case CastExpr::CK_IntegralToPointer:
+    return "IntegralToPointer";
+  case CastExpr::CK_PointerToIntegral:
+    return "PointerToIntegral";
   }
 
   assert(0 && "Unhandled cast kind!");

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Mon Sep 14 23:48:33 2009
@@ -625,6 +625,12 @@
 
   switch (Kind) {
   default:
+    // FIXME: Assert here.
+    // assert(0 && "Unhandled cast kind!");
+    break;
+  case CastExpr::CK_Unknown:
+    // FIXME: We should really assert here - Unknown casts should never get
+    // as far as to codegen.
     break;
   case CastExpr::CK_BitCast: {
     Value *Src = Visit(const_cast<Expr*>(E));
@@ -685,6 +691,16 @@
                                         NullCheckValue);
   }
 
+  case CastExpr::CK_IntegralToPointer: {
+    Value *Src = Visit(const_cast<Expr*>(E));
+    return Builder.CreateIntToPtr(Src, ConvertType(DestTy));
+  }
+
+  case CastExpr::CK_PointerToIntegral: {
+    Value *Src = Visit(const_cast<Expr*>(E));
+    return Builder.CreatePtrToInt(Src, ConvertType(DestTy));
+  }
+  
   }
 
   // Handle cases where the source is an non-complex type.

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaCXXCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCXXCast.cpp Mon Sep 14 23:48:33 2009
@@ -41,7 +41,8 @@
                            const SourceRange &DestRange);
 static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
                                  const SourceRange &OpRange,
-                                 const SourceRange &DestRange);
+                                 const SourceRange &DestRange,
+                                 CastExpr::CastKind &Kind);
 static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
                             const SourceRange &OpRange,
                             CastExpr::CastKind &Kind,
@@ -135,13 +136,14 @@
     return Owned(new (Context)CXXDynamicCastExpr(DestType.getNonReferenceType(),
                                                  Kind, Ex, DestType, OpLoc));
   }
-  case tok::kw_reinterpret_cast:
+  case tok::kw_reinterpret_cast: {
+    CastExpr::CastKind Kind = CastExpr::CK_Unknown;
     if (!TypeDependent)
-      CheckReinterpretCast(*this, Ex, DestType, OpRange, DestRange);
+      CheckReinterpretCast(*this, Ex, DestType, OpRange, DestRange, Kind);
     return Owned(new (Context) CXXReinterpretCastExpr(
                                   DestType.getNonReferenceType(),
-                                  Ex, DestType, OpLoc));
-
+                                  Kind, Ex, DestType, OpLoc));
+  }
   case tok::kw_static_cast: {
     CastExpr::CastKind Kind = CastExpr::CK_Unknown;
     if (!TypeDependent) {
@@ -355,11 +357,11 @@
 /// char *bytes = reinterpret_cast\<char*\>(int_ptr);
 void
 CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
-                     const SourceRange &OpRange, const SourceRange &DestRange) {
+                     const SourceRange &OpRange, const SourceRange &DestRange,
+                     CastExpr::CastKind &Kind) {
   if (!DestType->isLValueReferenceType())
     Self.DefaultFunctionArrayConversion(SrcExpr);
 
-  CastExpr::CastKind Kind = CastExpr::CK_Unknown;
   unsigned msg = diag::err_bad_cxx_cast_generic;
   if (TryReinterpretCast(Self, SrcExpr, DestType, /*CStyle*/false, Kind,
                          OpRange, msg)
@@ -950,6 +952,7 @@
       msg = diag::err_bad_reinterpret_cast_small_int;
       return TC_Failed;
     }
+    Kind = CastExpr::CK_PointerToIntegral;
     return TC_Success;
   }
 
@@ -982,6 +985,7 @@
       msg = diag::err_bad_reinterpret_cast_small_int;
       return TC_Failed;
     }
+    Kind = CastExpr::CK_PointerToIntegral;
     return TC_Success;
   }
 
@@ -989,6 +993,7 @@
     assert(destIsPtr && "One type must be a pointer");
     // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
     //   converted to a pointer.
+    Kind = CastExpr::CK_IntegralToPointer;
     return TC_Success;
   }
 

Added: cfe/trunk/test/CodeGenCXX/reinterpret-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/reinterpret-cast.cpp?rev=81837&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/reinterpret-cast.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/reinterpret-cast.cpp Mon Sep 14 23:48:33 2009
@@ -0,0 +1,12 @@
+// RUN: clang-cc -emit-llvm -o - %s -std=c++0x
+void *f1(unsigned long l) {
+  return reinterpret_cast<void *>(l);
+}
+
+unsigned long f2() {
+  return reinterpret_cast<unsigned long>(nullptr);
+}
+
+unsigned long f3(void *p) {
+  return reinterpret_cast<unsigned long>(p);
+}
\ No newline at end of file





More information about the cfe-commits mailing list