[cfe-commits] r39888 - in /cfe/trunk: AST/Expr.cpp AST/StmtPrinter.cpp include/clang/AST/Expr.h test/Sema/i-c-e2.c

Chris Lattner sabre at nondot.org
Sun Jul 15 16:54:51 PDT 2007


Author: lattner
Date: Sun Jul 15 18:54:50 2007
New Revision: 39888

URL: http://llvm.org/viewvc/llvm-project?rev=39888&view=rev
Log:
Remove an extraneous QualType from CastExpr, it's type is always
the result type of the expr node.

Implement isIntegerConstantExpr for ImplicitCastExpr nodes the same
was as for CastExpr nodes.

Implement proper sign/zero extension as well as truncation and noop
conversion in the i-c-e evaluator.  This allows us to correctly
handle i-c-e's like these:

char array[1024/(sizeof (long))];
int x['\xBb' == (char) 187 ? 1: -1];

this implements test/Sema/i-c-e2.c


Modified:
    cfe/trunk/AST/Expr.cpp
    cfe/trunk/AST/StmtPrinter.cpp
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/test/Sema/i-c-e2.c

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

==============================================================================
--- cfe/trunk/AST/Expr.cpp (original)
+++ cfe/trunk/AST/Expr.cpp Sun Jul 15 18:54:50 2007
@@ -279,9 +279,6 @@
   default:
     if (Loc) *Loc = getLocStart();
     return false;
-  case ImplicitCastExprClass:
-    return cast<ImplicitCastExpr>(this)->getSubExpr()->
-                     isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
   case ParenExprClass:
     return cast<ParenExpr>(this)->getSubExpr()->
                      isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
@@ -455,27 +452,44 @@
     assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
     break;
   }
+  case ImplicitCastExprClass:
   case CastExprClass: {
-    const CastExpr *Exp = cast<CastExpr>(this);    
+    const Expr *SubExpr;
+    SourceLocation CastLoc;
+    if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
+      SubExpr = C->getSubExpr();
+      CastLoc = C->getLParenLoc();
+    } else {
+      SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
+      CastLoc = getLocStart();
+    }
+    
     // C99 6.6p6: shall only convert arithmetic types to integer types.
-    if (!Exp->getSubExpr()->getType()->isArithmeticType() ||
-        !Exp->getDestType()->isIntegerType()) {
-      if (Loc) *Loc = Exp->getSubExpr()->getLocStart();
+    if (!SubExpr->getType()->isArithmeticType() ||
+        !getType()->isIntegerType()) {
+      if (Loc) *Loc = SubExpr->getLocStart();
       return false;
     }
       
     // Handle simple integer->integer casts.
-    if (Exp->getSubExpr()->getType()->isIntegerType()) {
-      if (!Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx,
-                                                    Loc, isEvaluated))
+    if (SubExpr->getType()->isIntegerType()) {
+      if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
         return false;
-      // FIXME: do the conversion on Result.
+      
+      // Figure out if this is a truncate, extend or noop cast.
+      unsigned DestWidth = Ctx.getTypeSize(getType(), CastLoc);
+      
+      // If the input is signed, do a sign extend, noop, or truncate.
+      if (SubExpr->getType()->isSignedIntegerType())
+        Result.sextOrTrunc(DestWidth);
+      else  // If the input is unsigned, do a zero extend, noop, or truncate.
+        Result.zextOrTrunc(DestWidth);
       break;
     }
     
     // Allow floating constants that are the immediate operands of casts or that
     // are parenthesized.
-    const Expr *Operand = Exp->getSubExpr();
+    const Expr *Operand = SubExpr;
     while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
       Operand = PE->getSubExpr();
     

Modified: cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtPrinter.cpp?rev=39888&r1=39887&r2=39888&view=diff

==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Sun Jul 15 18:54:50 2007
@@ -420,7 +420,7 @@
   OS << Field->getName();
 }
 void StmtPrinter::VisitCastExpr(CastExpr *Node) {
-  OS << "(" << Node->getDestType().getAsString() << ")";
+  OS << "(" << Node->getType().getAsString() << ")";
   PrintExpr(Node->getSubExpr());
 }
 void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Sun Jul 15 18:54:50 2007
@@ -456,16 +456,14 @@
 /// CastExpr - [C99 6.5.4] Cast Operators.
 ///
 class CastExpr : public Expr {
-  QualType Ty;
   Expr *Op;
   SourceLocation Loc; // the location of the left paren
 public:
   CastExpr(QualType ty, Expr *op, SourceLocation l) : 
-    Expr(CastExprClass, ty), Ty(ty), Op(op), Loc(l) {}
+    Expr(CastExprClass, ty), Op(op), Loc(l) {}
 
   SourceLocation getLParenLoc() const { return Loc; }
   
-  QualType getDestType() const { return Ty; }
   Expr *getSubExpr() const { return Op; }
   
   virtual SourceRange getSourceRange() const {

Modified: cfe/trunk/test/Sema/i-c-e2.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/i-c-e2.c?rev=39888&r1=39887&r2=39888&view=diff

==============================================================================
--- cfe/trunk/test/Sema/i-c-e2.c (original)
+++ cfe/trunk/test/Sema/i-c-e2.c Sun Jul 15 18:54:50 2007
@@ -1,6 +1,6 @@
 // RUN: clang %s -fsyntax-only
 
-// FIXME: This fails until conversions are fully explicit in the ast and i-c-e is updated to handle this.
-// XFAIL: *
-
 char array[1024/(sizeof (long))];
+
+int x['\xBb' == (char) 187 ? 1: -1];
+





More information about the cfe-commits mailing list