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

Steve Naroff snaroff at apple.com
Mon Aug 27 08:30:22 PDT 2007


Author: snaroff
Date: Mon Aug 27 10:30:22 2007
New Revision: 41483

URL: http://llvm.org/viewvc/llvm-project?rev=41483&view=rev
Log:
Replaced ASTContext::maxFloatingType() with ASTContext::compareFloatingType().

Changed Sema::UsualArithmeticConversions to use the new API.

This fixes the following case...

_Complex double X;
double y;

void foo() {
  X = X + y;
}

[dylan:~/llvm/tools/clang] admin% ../../Debug/bin/clang complex.c -parse-ast-dump
Read top-level variable decl: 'X'
Read top-level variable decl: 'y'

void foo()
(CompoundStmt 0x2605cc0
  (BinaryOperator 0x2605ca0 '_Complex double' '='
    (DeclRefExpr 0x2605c10 '_Complex double' Decl='X' 0x2605ab0)
    (BinaryOperator 0x2605c80 '_Complex double' '+'
      (DeclRefExpr 0x2605c30 '_Complex double' Decl='X' 0x2605ab0)
      (ImplicitCastExpr 0x2605c70 '_Complex double'
        (DeclRefExpr 0x2605c50 'double' Decl='y' 0x2605ae0)))))


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

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

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Mon Aug 27 10:30:22 2007
@@ -710,9 +710,15 @@
   assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
 }
 
-// maxFloatingType - handles the simple case, both operands are floats.
-QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
-  return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
+/// compareFloatingType - 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. 
+int ASTContext::compareFloatingType(QualType lt, QualType rt) {
+  if (getFloatingRank(lt) == getFloatingRank(rt))
+    return 0;
+  if (getFloatingRank(lt) > getFloatingRank(rt))
+    return 1;
+  return -1;
 }
 
 // maxIntegerType - Returns the highest ranked integer type. Handles 3 case:

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

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Mon Aug 27 10:30:22 2007
@@ -821,16 +821,26 @@
     // 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. 
+    int result = Context.compareFloatingType(lhs, rhs);
+    
+    if (result > 0) { // The left side is bigger, convert rhs. 
       QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
       if (!isCompAssign) promoteExprToType(rhsExpr, tMax);
       return tMax;
     }
-    // The right side is bigger, convert lhs within it's domain. 
-    QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
-    if (!isCompAssign) promoteExprToType(lhsExpr, tMax);
-    return tMax;
+    if (result < 0) { // The right side is bigger, convert lhs. 
+      QualType tMax = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
+      if (!isCompAssign) promoteExprToType(lhsExpr, tMax);
+      return tMax;
+    }
+    // The floating point types were ranked equally.
+    if (lhs->isComplexType()) { // handle "_Complex double, double".
+      if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
+      return lhs;
+    }
+    // The right side is complex, handle "double, _Complex double".
+    if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
+    return rhs;
   }
   // Now handle "real" floating types (i.e. float, double, long double).
   if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
@@ -845,12 +855,17 @@
     }
     // We have two real floating types, float/complex combos were handled above.
     // Convert the smaller operand to the bigger result.
-    if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs
+    int result = Context.compareFloatingType(lhs, rhs);
+    
+    if (result > 0) { // convert the rhs
       if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
       return lhs;
     }
-    if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
-    return rhs;
+    if (result < 0) { // convert the lhs
+      if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
+      return rhs;
+    }
+    assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
   }
   // Finally, we have two differing integer types.
   if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Mon Aug 27 10:30:22 2007
@@ -160,9 +160,10 @@
   /// different type combos: unsigned/unsigned, signed/signed, signed/unsigned.
   static QualType maxIntegerType(QualType lhs, QualType rhs);
   
-  /// maxFloatingType - Returns the highest ranked float type. Handles 3
-  /// different combos: float/float, float/complex, complex/complex.
-  static QualType maxFloatingType(QualType lt, QualType rt);
+  /// compareFloatingType - 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. 
+  static int compareFloatingType(QualType lt, QualType rt);
 
   /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 
   /// point or a complex type (based on typeDomain/typeSize). 





More information about the cfe-commits mailing list