[cfe-commits] r39581 - in /cfe/cfe/trunk/Sema: Sema.h SemaExpr.cpp SemaStmt.cpp

Steve Naroff snaroff at apple.com
Wed Jul 11 09:45:52 PDT 2007


Author: snaroff
Date: Wed Jul 11 11:45:52 2007
New Revision: 39581

URL: http://llvm.org/viewvc/llvm-project?rev=39581&view=rev
Log:
Bug #:
Submitted by:
Reviewed by:
Fixed an "impurity" in the typechecking code (to more closely model the
C99 spec)...

- Added function Sema::DefaultFunctionArrayConversion(). This is called
by UsualUnaryConversions(). It is also called from several contexts that
don't call for integer promotions (logical negation for expressions, and
while/if/do/for conditions in statements). It is also used in
UsualAssignmentConversions (which is cleaner than the check I was using
before).

- Changed the name from UsualUnaryConversion->UsualUnaryConversions.

- Added comments to the header.

Since several contexts don't call for integer
promotions,

Modified:
    cfe/cfe/trunk/Sema/Sema.h
    cfe/cfe/trunk/Sema/SemaExpr.cpp
    cfe/cfe/trunk/Sema/SemaStmt.cpp

Modified: cfe/cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/Sema.h?rev=39581&r1=39580&r2=39581&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.h (original)
+++ cfe/cfe/trunk/Sema/Sema.h Wed Jul 11 11:45:52 2007
@@ -259,8 +259,19 @@
   );
     
 private:
-  QualType UsualUnaryConversion(QualType t); // C99 6.3
-  QualType UsualArithmeticConversions(QualType t1, QualType t2); // C99 6.3.1.8
+  // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
+  // functions and arrays to their respective pointers (C99 6.3.2.1)
+  QualType UsualUnaryConversions(QualType t); 
+  // UsualArithmeticConversions - performs the UsualUnaryConversions on it's
+  // operands and then handles various conversions that are common to binary
+  // operators (C99 6.3.1.8). If both operands aren't arithmetic, this
+  // routine returns the first non-arithmetic type found. The client is 
+  // responsible for emitting appropriate error diagnostics.
+  QualType UsualArithmeticConversions(QualType t1, QualType t2);
+  // DefaultFunctionArrayConversion - simply converts functions and arrays
+  // to their respective pointers (C99 6.3.2.1). If the type isn't a function
+  // or array, this routine simply returns the input type (unmodified).
+  QualType DefaultFunctionArrayConversion(QualType t);
   
   enum AssignmentConversionResult {
     Compatible,

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

==============================================================================
--- cfe/cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaExpr.cpp Wed Jul 11 11:45:52 2007
@@ -222,7 +222,7 @@
   return new ParenExpr(L, R, e);
 }
 
-/// The UsualUnaryConversion() function is *not* called by this routine.
+/// The UsualUnaryConversions() function is *not* called by this routine.
 /// See C99 6.3.2.1p[2-4] for more details.
 QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType, 
                                          SourceLocation OpLoc, bool isSizeof) {
@@ -466,9 +466,9 @@
   assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
   assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
 
-  cond = UsualUnaryConversion(cond);
-  lhs = UsualUnaryConversion(lhs);
-  rhs = UsualUnaryConversion(rhs);
+  cond = UsualUnaryConversions(cond);
+  lhs = UsualUnaryConversions(lhs);
+  rhs = UsualUnaryConversions(rhs);
   
   // first, check the condition.
   if (!cond->isScalarType()) { // C99 6.5.15p2
@@ -548,21 +548,25 @@
   return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
 }
 
+inline QualType Sema::DefaultFunctionArrayConversion(QualType t) {
+  if (t->isFunctionType()) // C99 6.3.2.1p4
+    return Context.getPointerType(t);
+  if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
+    return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
+  return t;
+}
+
 /// UsualUnaryConversion - Performs various conversions that are common to most
 /// operators (C99 6.3). The conversions of array and function types are 
 /// sometimes surpressed. For example, the array->pointer conversion doesn't
 /// apply if the array is an argument to the sizeof or address (&) operators.
 /// In these instances, this routine should *not* be called.
-QualType Sema::UsualUnaryConversion(QualType t) {
-  assert(!t.isNull() && "UsualUnaryConversion - missing type");
+QualType Sema::UsualUnaryConversions(QualType t) {
+  assert(!t.isNull() && "UsualUnaryConversions - missing type");
   
   if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
     return Context.IntTy;
-  if (t->isFunctionType()) // C99 6.3.2.1p4
-    return Context.getPointerType(t);
-  if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
-    return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
-  return t;
+  return DefaultFunctionArrayConversion(t);
 }
 
 /// UsualArithmeticConversions - Performs various conversions that are common to 
@@ -570,8 +574,8 @@
 /// routine returns the first non-arithmetic type found. The client is 
 /// responsible for emitting appropriate error diagnostics.
 QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
-  QualType lhs = UsualUnaryConversion(t1);
-  QualType rhs = UsualUnaryConversion(t2);
+  QualType lhs = UsualUnaryConversions(t1);
+  QualType rhs = UsualUnaryConversions(t2);
   
   // If both types are identical, no conversion is needed.
   if (lhs == rhs) 
@@ -676,8 +680,7 @@
   // conversion of functions/arrays. If the conversion were done for all
   // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary
   // expressions that surpress this implicit conversion (&, sizeof).
-  if (rhsType->isFunctionType() || rhsType->isArrayType())
-    rhsType = UsualUnaryConversion(rhsType);
+  rhsType = DefaultFunctionArrayConversion(rhsType);
     
   r = Compatible;
   if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
@@ -834,8 +837,8 @@
 inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
   Expr *lex, Expr *rex, SourceLocation loc) 
 {
-  QualType lhsType = UsualUnaryConversion(lex->getType());
-  QualType rhsType = UsualUnaryConversion(rex->getType());
+  QualType lhsType = UsualUnaryConversions(lex->getType());
+  QualType rhsType = UsualUnaryConversions(rex->getType());
   
   if (lhsType->isScalarType() || rhsType->isScalarType())
     return Context.IntTy;
@@ -921,7 +924,7 @@
 
 inline QualType Sema::CheckCommaOperands( // C99 6.5.17
   Expr *lex, Expr *rex, SourceLocation loc) {
-  return UsualUnaryConversion(rex->getType());
+  return UsualUnaryConversions(rex->getType());
 }
 
 QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
@@ -1012,7 +1015,7 @@
 }
 
 QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
-  QualType qType = UsualUnaryConversion(op->getType());
+  QualType qType = UsualUnaryConversions(op->getType());
   
   assert(!qType.isNull() && "no type for * expression");
 
@@ -1220,19 +1223,20 @@
     break;
   case UnaryOperator::Plus:
   case UnaryOperator::Minus:
-    resultType = UsualUnaryConversion(((Expr *)Input)->getType());
+    resultType = UsualUnaryConversions(((Expr *)Input)->getType());
     if (!resultType->isArithmeticType())  // C99 6.5.3.3p1
       return Diag(OpLoc, diag::err_typecheck_unary_expr, 
                   resultType.getAsString());
     break;
   case UnaryOperator::Not: // bitwise complement
-    resultType = UsualUnaryConversion(((Expr *)Input)->getType());
+    resultType = UsualUnaryConversions(((Expr *)Input)->getType());
     if (!resultType->isIntegerType())  // C99 6.5.3.3p1
       return Diag(OpLoc, diag::err_typecheck_unary_expr,
                   resultType.getAsString());
     break;
   case UnaryOperator::LNot: // logical negation
-    resultType = UsualUnaryConversion(((Expr *)Input)->getType());
+    // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
+    resultType = DefaultFunctionArrayConversion(((Expr *)Input)->getType());
     if (!resultType->isScalarType()) // C99 6.5.3.3p1
       return Diag(OpLoc, diag::err_typecheck_unary_expr,
                   resultType.getAsString());

Modified: cfe/cfe/trunk/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaStmt.cpp?rev=39581&r1=39580&r2=39581&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaStmt.cpp Wed Jul 11 11:45:52 2007
@@ -96,7 +96,7 @@
   Expr *condExpr = (Expr *)CondVal;
   assert(condExpr && "ParseIfStmt(): missing expression");
   
-  QualType condType = UsualUnaryConversion(condExpr->getType());
+  QualType condType = DefaultFunctionArrayConversion(condExpr->getType());
   assert(!condType.isNull() && "ParseIfStmt(): missing expression type");
   
   if (!condType->isScalarType()) // C99 6.8.4.1p1
@@ -116,7 +116,7 @@
   Expr *condExpr = (Expr *)Cond;
   assert(condExpr && "ParseWhileStmt(): missing expression");
   
-  QualType condType = UsualUnaryConversion(condExpr->getType());
+  QualType condType = DefaultFunctionArrayConversion(condExpr->getType());
   assert(!condType.isNull() && "ParseWhileStmt(): missing expression type");
   
   if (!condType->isScalarType()) // C99 6.8.5p2
@@ -132,7 +132,7 @@
   Expr *condExpr = (Expr *)Cond;
   assert(condExpr && "ParseDoStmt(): missing expression");
   
-  QualType condType = UsualUnaryConversion(condExpr->getType());
+  QualType condType = DefaultFunctionArrayConversion(condExpr->getType());
   assert(!condType.isNull() && "ParseDoStmt(): missing expression type");
   
   if (!condType->isScalarType()) // C99 6.8.5p2
@@ -153,7 +153,7 @@
   }
   if (Second) {
     Expr *testExpr = (Expr *)Second;
-    QualType testType = UsualUnaryConversion(testExpr->getType());
+    QualType testType = DefaultFunctionArrayConversion(testExpr->getType());
     assert(!testType.isNull() && "ParseForStmt(): missing test expression type");
     
     if (!testType->isScalarType()) // C99 6.8.5p2





More information about the cfe-commits mailing list