Refactor the expression class hierarchy for casts. Most importantly:
  - CastExpr is the root of all casts
  - ImplicitCastExpr is (still) used for all explicit casts
  - ExplicitCastExpr is now the root of all *explicit* casts
  - ExplicitCCastExpr (new name needed!?) is a C-style cast in C or C++
  - CXXFunctionalCastExpr inherits from ExplicitCastExpr
  - CXXNamedCastExpr inherits from ExplicitCastExpr and is the root of all
    of the C++ named cast expression types (static_cast, dynamic_cast, etc.)
  - Added classes CXXStaticCastExpr, CXXDynamicCastExpr, 
    CXXReinterpretCastExpr, and CXXConstCastExpr to 

Also, fixed returned-stack-addr.cpp, which broke once when we fixed
reinterpret_cast to diagnose double->int* conversions and again when
we eliminated implicit conversions to reference types. The fix is in
both testcase and SemaChecking.cpp.

Most of this patch is simply support for the renaming. There's very
little actual change in semantics.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58264 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index a2872a2..f84b5c5 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -695,12 +695,12 @@
                                              SourceLocation *CommaLocs,
                                              SourceLocation RParenLoc);
 
-  /// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
-  virtual ExprResult ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
-                                   SourceLocation LAngleBracketLoc, TypeTy *Ty,
-                                   SourceLocation RAngleBracketLoc,
-                                   SourceLocation LParenLoc, ExprTy *E,
-                                   SourceLocation RParenLoc);
+  /// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
+  virtual ExprResult ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
+                                       SourceLocation LAngleBracketLoc, TypeTy *Ty,
+                                       SourceLocation RAngleBracketLoc,
+                                       SourceLocation LParenLoc, ExprTy *E,
+                                       SourceLocation RParenLoc);
 
   // Helpers for ActOnCXXCasts
   bool CastsAwayConstness(QualType SrcType, QualType DestType);
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index 27f3abe..3365992 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -736,12 +736,11 @@
   }
   // Perform checking for stack values returned by reference.
   else if (lhsType->isReferenceType()) {
-    // Check for an implicit cast to a reference.
-    if (ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(RetValExp))
-      if (DeclRefExpr *DR = EvalVal(I->getSubExpr()))
-        Diag(DR->getLocStart(), diag::warn_ret_stack_ref,
-             DR->getDecl()->getIdentifier()->getName(),
-             RetValExp->getSourceRange());
+    // Check for a reference to the stack
+    if (DeclRefExpr *DR = EvalVal(RetValExp))
+      Diag(DR->getLocStart(), diag::warn_ret_stack_ref,
+           DR->getDecl()->getIdentifier()->getName(),
+           RetValExp->getSourceRange());
   }
 }
 
@@ -826,9 +825,9 @@
     
   // For casts, we need to handle conversions from arrays to
   // pointer values, and pointer-to-pointer conversions.
-  case Stmt::ExplicitCastExprClass:
-  case Stmt::ImplicitCastExprClass: {
-    
+  case Stmt::ImplicitCastExprClass:
+  case Stmt::ExplicitCCastExprClass:
+  case Stmt::CXXFunctionalCastExprClass: {
     Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
     QualType T = SubExpr->getType();
     
@@ -844,21 +843,21 @@
     
   // C++ casts.  For dynamic casts, static casts, and const casts, we
   // are always converting from a pointer-to-pointer, so we just blow
-  // through the cast.  In the case the dynamic cast doesn't fail
-  // (and return NULL), we take the conservative route and report cases
+  // through the cast.  In the case the dynamic cast doesn't fail (and
+  // return NULL), we take the conservative route and report cases
   // where we return the address of a stack variable.  For Reinterpre
-  case Stmt::CXXCastExprClass: {
-    CXXCastExpr *C = cast<CXXCastExpr>(E);
-    
-    if (C->getOpcode() == CXXCastExpr::ReinterpretCast) {
-      Expr *S = C->getSubExpr();
+  // FIXME: The comment about is wrong; we're not always converting
+  // from pointer to pointer. I'm guessing that this code should also
+  // handle references to objects.  
+  case Stmt::CXXStaticCastExprClass: 
+  case Stmt::CXXDynamicCastExprClass: 
+  case Stmt::CXXConstCastExprClass:
+  case Stmt::CXXReinterpretCastExprClass: {
+      Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr();
       if (S->getType()->isPointerType() || S->getType()->isBlockPointerType())
         return EvalAddr(S);
       else
         return NULL;
-    }
-    else
-      return EvalAddr(C->getSubExpr());
   }
     
   // Everything else: we simply don't reason about them.
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 624c576..7b88ec2 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -1060,7 +1060,7 @@
            CheckArithmeticConstantExpression(IExp);
   }
   case Expr::ImplicitCastExprClass:
-  case Expr::ExplicitCastExprClass: {
+  case Expr::ExplicitCCastExprClass: {
     const Expr* SubExpr = cast<CastExpr>(Init)->getSubExpr();
     if (Init->getStmtClass() == Expr::ImplicitCastExprClass) {
       // Check for implicit promotion
@@ -1190,7 +1190,7 @@
     // if we don't, we'll figure it out later
     return 0;
   }
-  case Expr::ExplicitCastExprClass: {
+  case Expr::ExplicitCCastExprClass: {
     const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
 
     // Check for pointer->pointer cast
@@ -1310,7 +1310,7 @@
     return true;
   }
   case Expr::ImplicitCastExprClass:
-  case Expr::ExplicitCastExprClass: {
+  case Expr::ExplicitCCastExprClass: {
     const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr();
     if (SubExpr->getType()->isArithmeticType())
       return CheckArithmeticConstantExpression(SubExpr);
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index d1be2a4..a73f0dd 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -1333,7 +1333,7 @@
 
   if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr))
     return true;
-  return new ExplicitCastExpr(castType, castExpr, LParenLoc);
+  return new ExplicitCCastExpr(castType, castExpr, castType, LParenLoc);
 }
 
 /// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index fa81f11..34eff25 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -21,36 +21,39 @@
 #include "llvm/Support/Debug.h"
 using namespace clang;
 
-/// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
+/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
 Action::ExprResult
-Sema::ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
-                    SourceLocation LAngleBracketLoc, TypeTy *Ty,
-                    SourceLocation RAngleBracketLoc,
-                    SourceLocation LParenLoc, ExprTy *E,
-                    SourceLocation RParenLoc) {
-  CXXCastExpr::Opcode Op;
+Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
+                        SourceLocation LAngleBracketLoc, TypeTy *Ty,
+                        SourceLocation RAngleBracketLoc,
+                        SourceLocation LParenLoc, ExprTy *E,
+                        SourceLocation RParenLoc) {
   Expr *Ex = (Expr*)E;
   QualType DestType = QualType::getFromOpaquePtr(Ty);
 
   switch (Kind) {
   default: assert(0 && "Unknown C++ cast!");
+
   case tok::kw_const_cast:
-    Op = CXXCastExpr::ConstCast;
     CheckConstCast(OpLoc, Ex, DestType);
-    break;
+    return new CXXConstCastExpr(DestType.getNonReferenceType(), Ex, 
+                                DestType, OpLoc);
+
   case tok::kw_dynamic_cast:
-    Op = CXXCastExpr::DynamicCast;
-    break;
+    return new CXXDynamicCastExpr(DestType.getNonReferenceType(), Ex, 
+                                  DestType, OpLoc);
+
   case tok::kw_reinterpret_cast:
-    Op = CXXCastExpr::ReinterpretCast;
     CheckReinterpretCast(OpLoc, Ex, DestType);
-    break;
+    return new CXXReinterpretCastExpr(DestType.getNonReferenceType(), Ex, 
+                                      DestType, OpLoc);
+
   case tok::kw_static_cast:
-    Op = CXXCastExpr::StaticCast;
-    break;
+    return new CXXStaticCastExpr(DestType.getNonReferenceType(), Ex, 
+                                 DestType, OpLoc);
   }
   
-  return new CXXCastExpr(Op, DestType, Ex, OpLoc);
+  return true;
 }
 
 /// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
@@ -437,7 +440,8 @@
   if (NumExprs == 1) {
     if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
       return true;
-    return new CXXFunctionalCastExpr(Ty, TyBeginLoc, Exprs[0], RParenLoc);
+    return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc, 
+                                     Exprs[0], RParenLoc);
   }
 
   // C++ 5.2.3p1: