]> granicus.if.org Git - clang/commitdiff
Replaced ASTContext::maxComplexType() with ASTContext::getFloatingTypeOfSizeWithinDom...
authorSteve Naroff <snaroff@apple.com>
Mon, 27 Aug 2007 01:27:54 +0000 (01:27 +0000)
committerSteve Naroff <snaroff@apple.com>
Mon, 27 Aug 2007 01:27:54 +0000 (01:27 +0000)
Changed Sema::UsualArithmeticConversions to correctly implement complex/float conversions,
using maxFloatingType() with getFloatingTypeOfSizeWithinDomain().

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41474 91177308-0d34-0410-b5e6-96231b3b80d8

AST/ASTContext.cpp
Sema/SemaExpr.cpp
include/clang/AST/ASTContext.h

index 7293d4574f8d5f3cd5e7eaffe7710819a61fe87a..919cc9e2e21d1b4c1ca535a733373bc6a5408ff4 100644 (file)
@@ -685,28 +685,28 @@ static int getFloatingRank(QualType T) {
   }
 }
 
-// maxComplexType - the following code handles 3 different combinations:
-// complex/complex, complex/float, float/complex. 
-// When both operands are complex, the shorter operand is converted to the 
-// type of the longer, and that is the type of the result. This corresponds 
-// to what is done when combining two real floating-point operands. 
-// The fun begins when size promotion occur across type domains. g
-// getFloatingRank & convertFloatingRankToComplexType handle this without 
-// enumerating all permutations. 
-// It also allows us to add new types without breakage.
-// From H&S 6.3.4: When one operand is complex and the other is a real
-// floating-point type, the less precise type is converted, within it's 
-// real or complex domain, to the precision of the other type. For example,
-// when combining a "long double" with a "double _Complex", the 
-// "double _Complex" is promoted to "long double _Complex".
-
-QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
-  switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
-  default: assert(0 && "convertRankToComplex(): illegal value for rank");
-  case FloatRank:      return FloatComplexTy;
-  case DoubleRank:     return DoubleComplexTy;
-  case LongDoubleRank: return LongDoubleComplexTy;
+/// getFloatingTypeOfSizeWithinDomain - Returns the either a real floating 
+/// point type or a complex type (based on typeDomain) of typeSize. 
+/// typeSize is expected to be a floating point type (real or complex).
+QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
+  QualType typeSize, QualType typeDomain) const {
+  if (typeDomain->isComplexType()) {
+    switch (getFloatingRank(typeSize)) {
+    default: assert(0 && "convertRankToComplex(): illegal value for rank");
+    case FloatRank:      return FloatComplexTy;
+    case DoubleRank:     return DoubleComplexTy;
+    case LongDoubleRank: return LongDoubleComplexTy;
+    }
+  }
+  if (typeDomain->isRealFloatingType()) {
+    switch (getFloatingRank(typeSize)) {
+    default: assert(0 && "convertRankToComplex(): illegal value for rank");
+    case FloatRank:      return FloatTy;
+    case DoubleRank:     return DoubleTy;
+    case LongDoubleRank: return LongDoubleTy;
+    }
   }
+  assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
 }
 
 // maxFloatingType - handles the simple case, both operands are floats.
index a859fe8ab2a324ecf5c2cd2dfc60436d025cc5b8..cc9a64f7268a14571d289be696cdfa3c3aa063b3 100644 (file)
@@ -811,13 +811,26 @@ QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
       if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
       return rhs;
     }
-    // Two complex types. Convert the smaller operand to the bigger result.
-    if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs
-      if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
-      return lhs;
+    // This handles complex/complex, complex/float, or float/complex.
+    // When both operands are complex, the shorter operand is converted to the 
+    // type of the longer, and that is the type of the result. This corresponds 
+    // to what is done when combining two real floating-point operands. 
+    // The fun begins when size promotion occur across type domains. 
+    // From H&S 6.3.4: When one operand is complex and the other is a real
+    // floating-point type, the less precise type is converted, within it's 
+    // real or complex domain, to the precision of the other type. For example,
+    // when combining a "long double" with a "double _Complex", the 
+    // "double _Complex" is promoted to "long double _Complex".
+    if (Context.maxFloatingType(lhs, rhs) == lhs) {
+      // The left side is bigger, convert rhs within it's domain. 
+      QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
+      if (!isCompAssign) promoteExprToType(rhsExpr, tMax);
+      return tMax;
     }
-    if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
-    return rhs;
+    // The right side is bigger, convert lhs within it's domain. 
+    QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
+    if (!isCompAssign) promoteExprToType(lhsExpr, tMax);
+    return tMax;
   }
   // Now handle "real" floating types (i.e. float, double, long double).
   if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
index d86a6ed70d88ede86eae104264a7b024eeef7c21..2239118a5dd26608084dd84d0c9c7b743b60244d 100644 (file)
@@ -160,14 +160,15 @@ public:
   /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
   static QualType maxIntegerType(QualType lhs, QualType rhs);
   
-  /// maxFloatingType - Returns the highest ranked float type. Both input 
-  /// types are required to be floats.
+  /// maxFloatingType - Returns the highest ranked float type. Handles 3
+  /// different combos: float/float, float/complex, complex/complex.
   static QualType maxFloatingType(QualType lt, QualType rt);
 
-  /// maxComplexType - Returns the highest ranked complex type. Handles 3
-  /// different type combos: complex/complex, complex/float, float/complex. 
-  QualType maxComplexType(QualType lt, QualType rt) const;
-  
+  /// getFloatingTypeOfSizeWithinDomain - Returns the either a real floating 
+  /// point type or a complex type (based on typeDomain) of typeSize. 
+  /// typeSize is expected to be a floating point type (real or complex).
+  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 
+                                             QualType typeDomain) const;
 private:
   ASTContext(const ASTContext&); // DO NOT IMPLEMENT
   void operator=(const ASTContext&); // DO NOT IMPLEMENT