Removed useless ImplicitCast nodes in explicit cstyle and static casts

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145244 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index fe393dd..0ce816b 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -234,13 +234,16 @@
   AnalysisWarnings.PrintStats();
 }
 
-/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
-/// If there is already an implicit cast, merge into the existing one.
-/// The result is of the given category.
-ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
-                                   CastKind Kind, ExprValueKind VK,
-                                   const CXXCastPath *BasePath,
-                                   CheckedConversionKind CCK) {
+/// CastExprToType - If Expr is not of type 'Type', insert a cast of the
+/// specified kind.
+/// Redundant implicit casts are merged together.
+/// Pay attention: if CCK != CCK_ImplicitConversion,
+/// users of this function must fill
+/// SourceTypeInfos and SourceLocations later
+ExprResult Sema::CastExprToType(Expr *E, QualType Ty,
+                                CastKind Kind, ExprValueKind VK,
+                                const CXXCastPath *BasePath,
+                                CheckedConversionKind CCK) {
 #ifndef NDEBUG
   if (VK == VK_RValue && !E->isRValue()) {
     switch (Kind) {
@@ -276,16 +279,41 @@
       MarkVTableUsed(E->getLocStart(), 
                      cast<CXXRecordDecl>(RecordTy->getDecl()));
   }
-
-  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
-    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
-      ImpCast->setType(Ty);
-      ImpCast->setValueKind(VK);
-      return Owned(E);
-    }
+  
+  switch(CCK) {
+    default:
+      llvm_unreachable("Unexpected CheckedConversionKind");
+    case CCK_ImplicitConversion:
+      if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
+        if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
+          ImpCast->setType(Ty);
+          ImpCast->setValueKind(VK);
+          return Owned(E);
+        }
+      }
+      return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK));
+    case CCK_CStyleCast:
+      return Owned(CStyleCastExpr::Create(Context, Ty, VK, Kind, E, BasePath,
+                                          0, SourceLocation(), SourceLocation()));
+    case CCK_FunctionalCast:
+      return Owned(CXXFunctionalCastExpr::Create(Context, Ty, VK, 0,
+                                                 SourceLocation(), Kind, E,
+                                                 BasePath, SourceLocation()));
+    case CCK_StaticCast:
+      return Owned(CXXStaticCastExpr::Create(Context, Ty, VK, Kind, E, BasePath,
+                                             0, SourceLocation(),
+                                             SourceLocation()));
   }
+  
+}
 
-  return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK));
+/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
+/// If there is already an implicit cast, merge into the existing one.
+/// The result is of the given category.
+ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
+                                   CastKind Kind, ExprValueKind VK,
+                                   const CXXCastPath *BasePath) {
+  return CastExprToType(E, Ty, Kind, VK, BasePath, CCK_ImplicitConversion);
 }
 
 /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding