From: Steve Naroff Date: Mon, 27 Aug 2007 01:27:54 +0000 (+0000) Subject: Replaced ASTContext::maxComplexType() with ASTContext::getFloatingTypeOfSizeWithinDom... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f1448a0e4a1e868ff873a8530a61a09cb68666cc;p=clang Replaced ASTContext::maxComplexType() with ASTContext::getFloatingTypeOfSizeWithinDomain(). 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 --- diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index 7293d4574f..919cc9e2e2 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -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. diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp index a859fe8ab2..cc9a64f726 100644 --- a/Sema/SemaExpr.cpp +++ b/Sema/SemaExpr.cpp @@ -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()) { diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index d86a6ed70d..2239118a5d 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -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