[cfe-commits] r49297 - in /cfe/trunk: include/clang/AST/ASTContext.h lib/AST/ASTContext.cpp lib/Sema/SemaExpr.cpp

Chris Lattner sabre at nondot.org
Sun Apr 6 16:55:33 PDT 2008


Author: lattner
Date: Sun Apr  6 18:55:33 2008
New Revision: 49297

URL: http://llvm.org/viewvc/llvm-project?rev=49297&view=rev
Log:
simplify max type computation by making it return an integer (like 
getFloatingTypeOrder) instead of a type.  Fix a fixme.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Sun Apr  6 18:55:33 2008
@@ -303,9 +303,10 @@
   /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
   QualType getArrayDecayedType(QualType T);
   
-  /// getMaxIntegerType - Returns the highest ranked integer type: 
-  /// C99 6.3.1.8p1.
-  QualType getMaxIntegerType(QualType LHS, QualType RHS);
+  /// getIntegerTypeOrder - Returns the highest ranked integer type: 
+  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
+  /// LHS < RHS, return -1. 
+  int getIntegerTypeOrder(QualType LHS, QualType RHS);
   
   /// getFloatingTypeOrder - Compare the rank of the two specified floating
   /// point types, ignoring the domain of the type (i.e. 'double' ==

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Sun Apr  6 18:55:33 2008
@@ -1050,9 +1050,10 @@
   return VoidTy;
 }
 
-/// getFloatingTypeOrder - Handles 3 different combos: 
-/// float/float, float/complex, complex/complex. 
-/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. 
+/// getFloatingTypeOrder - Compare the rank of the two specified floating
+/// point types, ignoring the domain of the type (i.e. 'double' ==
+/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
+/// LHS < RHS, return -1. 
 int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
   FloatingRank LHSR = getFloatingRank(LHS);
   FloatingRank RHSR = getFloatingRank(RHS);
@@ -1073,58 +1074,68 @@
     return 4;
   
   switch (cast<BuiltinType>(T)->getKind()) {
-    default: assert(0 && "getIntegerRank(): not a built-in integer");
-    case BuiltinType::Bool:
-      return 1;
-    case BuiltinType::Char_S:
-    case BuiltinType::Char_U:
-    case BuiltinType::SChar:
-    case BuiltinType::UChar:
-      return 2;
-    case BuiltinType::Short:
-    case BuiltinType::UShort:
-      return 3;
-    case BuiltinType::Int:
-    case BuiltinType::UInt:
-      return 4;
-    case BuiltinType::Long:
-    case BuiltinType::ULong:
-      return 5;
-    case BuiltinType::LongLong:
-    case BuiltinType::ULongLong:
-      return 6;
+  default: assert(0 && "getIntegerRank(): not a built-in integer");
+  case BuiltinType::Bool:
+    return 1;
+  case BuiltinType::Char_S:
+  case BuiltinType::Char_U:
+  case BuiltinType::SChar:
+  case BuiltinType::UChar:
+    return 2;
+  case BuiltinType::Short:
+  case BuiltinType::UShort:
+    return 3;
+  case BuiltinType::Int:
+  case BuiltinType::UInt:
+    return 4;
+  case BuiltinType::Long:
+  case BuiltinType::ULong:
+    return 5;
+  case BuiltinType::LongLong:
+  case BuiltinType::ULongLong:
+    return 6;
   }
 }
 
-// getMaxIntegerType - Returns the highest ranked integer type. Handles 3 case:
-// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
-QualType ASTContext::getMaxIntegerType(QualType LHS, QualType RHS) {
+/// getIntegerTypeOrder - Returns the highest ranked integer type: 
+/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
+/// LHS < RHS, return -1. 
+int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
   Type *LHSC = getCanonicalType(LHS).getTypePtr();
   Type *RHSC = getCanonicalType(RHS).getTypePtr();
-  if (LHSC == RHSC) return LHS;
+  if (LHSC == RHSC) return 0;
   
   bool LHSUnsigned = LHSC->isUnsignedIntegerType();
   bool RHSUnsigned = RHSC->isUnsignedIntegerType();
   
-  if (LHSUnsigned == RHSUnsigned)  // Both signed or both unsigned.
-    return getIntegerRank(LHSC) >= getIntegerRank(RHSC) ? LHS : RHS; 
+  unsigned LHSRank = getIntegerRank(LHSC);
+  unsigned RHSRank = getIntegerRank(RHSC);
   
-  // We have two integer types with differing signs.
-  Type *UnsignedType = LHSC, *SignedType = RHSC;
+  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
+    if (LHSRank == RHSRank) return 0;
+    return LHSRank > RHSRank ? 1 : -1;
+  }
   
-  if (RHSUnsigned)
-    std::swap(UnsignedType, SignedType);
+  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
+  if (LHSUnsigned) {
+    // If the unsigned [LHS] type is larger, return it.
+    if (LHSRank >= RHSRank)
+      return 1;
+    
+    // If the signed type can represent all values of the unsigned type, it
+    // wins.  Because we are dealing with 2's complement and types that are
+    // powers of two larger than each other, this is always safe. 
+    return -1;
+  }
 
-  // If the unsigned type is larger, return it.
-  if (getIntegerRank(UnsignedType) >= getIntegerRank(SignedType))
-    return LHSUnsigned ? LHS : RHS;
-
-  // FIXME: Need to check if the signed type can represent all values of the 
-  // unsigned type. If it can, then the result is the signed type. 
-  // If it can't, then the result is the unsigned version of the signed type.  
-  // Should probably add a helper that returns a signed integer type from 
-  // an unsigned (and vice versa). C99 6.3.1.8.
-  return RHSUnsigned ? LHS : RHS;
+  // If the unsigned [RHS] type is larger, return it.
+  if (RHSRank >= LHSRank)
+    return -1;
+  
+  // If the signed type can represent all values of the unsigned type, it
+  // wins.  Because we are dealing with 2's complement and types that are
+  // powers of two larger than each other, this is always safe. 
+  return 1;
 }
 
 // getCFConstantStringType - Return the type used for constant CFStrings. 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sun Apr  6 18:55:33 2008
@@ -1068,8 +1068,8 @@
     const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
 
     if (lhsComplexInt && rhsComplexInt) {
-      if (Context.getMaxIntegerType(lhsComplexInt->getElementType(), 
-                                    rhsComplexInt->getElementType()) == lhs) {
+      if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), 
+                                      rhsComplexInt->getElementType()) >= 0) {
         // convert the rhs
         if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
         return lhs;
@@ -1088,7 +1088,7 @@
     }
   }
   // Finally, we have two differing integer types.
-  if (Context.getMaxIntegerType(lhs, rhs) == lhs) { // convert the rhs
+  if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
     if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
     return lhs;
   }





More information about the cfe-commits mailing list