[cfe-commits] r39410 - in /cfe/cfe/trunk: AST/Sema.h AST/SemaExpr.cpp Sema/Sema.h Sema/SemaExpr.cpp include/clang/Basic/DiagnosticKinds.def

Steve Naroff snaroff at apple.com
Wed Jul 11 09:44:04 PDT 2007


Author: snaroff
Date: Wed Jul 11 11:44:04 2007
New Revision: 39410

URL: http://llvm.org/viewvc/llvm-project?rev=39410&view=rev
Log:
Bug #:
Submitted by:
Reviewed by:
Start hacking on binary ops...
Important bug fix...was ignoring the return value from ImplicitConversion.
This didn't bother the type checking logic, however the AST was malformed.

Modified:
    cfe/cfe/trunk/AST/Sema.h
    cfe/cfe/trunk/AST/SemaExpr.cpp
    cfe/cfe/trunk/Sema/Sema.h
    cfe/cfe/trunk/Sema/SemaExpr.cpp
    cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def

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

==============================================================================
--- cfe/cfe/trunk/AST/Sema.h (original)
+++ cfe/cfe/trunk/AST/Sema.h Wed Jul 11 11:44:04 2007
@@ -228,7 +228,9 @@
   /// or will return true if the expressions didn't type check properly.
   
   /// type checking binary operators (subroutines of ParseBinOp).
-  ExprResult CheckMultiplicativeOperands(Expr *op1, Expr *op2); // C99 6.5.5
+  ExprResult CheckMultiplicativeOperands(Expr *op1, Expr *op2,
+          SourceLocation OpLoc, 
+          unsigned /*BinaryOperator::Opcode*/OpCode); // C99 6.5.5
   ExprResult CheckAdditiveOperands(Expr *op1, Expr *op2);       // C99 6.5.6
   ExprResult CheckShiftOperands(Expr *op1, Expr *op2);          // C99 6.5.7
   ExprResult CheckRelationalOperands(Expr *op1, Expr *op2);     // C99 6.5.8

Modified: cfe/cfe/trunk/AST/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaExpr.cpp?rev=39410&r1=39409&r2=39410&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/AST/SemaExpr.cpp Wed Jul 11 11:44:04 2007
@@ -182,7 +182,8 @@
     return CheckIndirectionOperand((Expr *)Input, OpLoc, Opc);
   else { 
     // handle the arithmetic unary operators (C99 6.5.3.3)
-    QualType opType = ImplicitConversion((Expr *)Input)->getType();
+    Expr *operand = ImplicitConversion((Expr *)Input);
+    QualType opType = operand->getType();
     assert(!opType.isNull() && "no type for arithmetic unary expression");
     QualType resultType = opType;
     
@@ -214,7 +215,7 @@
     default: 
       break;
     }
-    return new UnaryOperator((Expr*)Input, Opc, resultType);
+    return new UnaryOperator(operand, Opc, resultType);
   }
 }
 
@@ -395,25 +396,25 @@
   }
 
   // perform implicit conversions (C99 6.3)
-  Expr *e1 = ImplicitConversion((Expr*)LHS);
-  Expr *e2 = ImplicitConversion((Expr*)RHS);
+  Expr *lhs = ImplicitConversion((Expr*)LHS);
+  Expr *rhs = ImplicitConversion((Expr*)RHS);
   
   if (BinaryOperator::isMultiplicativeOp(Opc)) 
-    CheckMultiplicativeOperands((Expr*)LHS, (Expr*)RHS);
+    return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc);
   else if (BinaryOperator::isAdditiveOp(Opc))
-    CheckAdditiveOperands((Expr*)LHS, (Expr*)RHS);
+    CheckAdditiveOperands(lhs, rhs);
   else if (BinaryOperator::isShiftOp(Opc))
-    CheckShiftOperands((Expr*)LHS, (Expr*)RHS);
+    CheckShiftOperands(lhs, rhs);
   else if (BinaryOperator::isRelationalOp(Opc))
-    CheckRelationalOperands((Expr*)LHS, (Expr*)RHS);
+    CheckRelationalOperands(lhs, rhs);
   else if (BinaryOperator::isEqualityOp(Opc))
-    CheckEqualityOperands((Expr*)LHS, (Expr*)RHS);
+    CheckEqualityOperands(lhs, rhs);
   else if (BinaryOperator::isBitwiseOp(Opc))
-    CheckBitwiseOperands((Expr*)LHS, (Expr*)RHS);
+    CheckBitwiseOperands(lhs, rhs);
   else if (BinaryOperator::isLogicalOp(Opc))
-    CheckLogicalOperands((Expr*)LHS, (Expr*)RHS);
+    CheckLogicalOperands(lhs, rhs);
   
-  return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
+  return new BinaryOperator(lhs, rhs, Opc);
 }
 
 /// ParseConditionalOp - Parse a ?: operation.  Note that 'LHS' may be null
@@ -443,8 +444,24 @@
   return E;
 }
 
-Action::ExprResult Sema::CheckMultiplicativeOperands(Expr *op1, Expr *op2) {
-  return false;
+Action::ExprResult Sema::CheckMultiplicativeOperands(
+  Expr *lexpr, Expr *rexpr, SourceLocation loc, unsigned code) 
+{
+  lexpr = ImplicitConversion((Expr *)lexpr);
+  rexpr = ImplicitConversion((Expr *)rexpr);
+  QualType ltype = lexpr->getType();
+  QualType rtype = rexpr->getType();
+  assert(!ltype.isNull() && "no left type for CheckMultiplicativeOperands()");
+  assert(!rtype.isNull() && "no right type for CheckMultiplicativeOperands()");
+  
+  if ((BinaryOperator::Opcode)code == BinaryOperator::Rem) {
+    if (!ltype->isIntegralType() || !rtype->isIntegralType())
+      return Diag(loc, diag::err_typecheck_invalid_operands);
+  } else {
+    if (!ltype->isArithmeticType() || !rtype->isArithmeticType())
+      return Diag(loc, diag::err_typecheck_invalid_operands);
+  }
+  return new BinaryOperator(lexpr, rexpr, (BinaryOperator::Opcode)code);
 }
 
 Action::ExprResult Sema::CheckAdditiveOperands(Expr *op1, Expr *op2) {

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

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.h (original)
+++ cfe/cfe/trunk/Sema/Sema.h Wed Jul 11 11:44:04 2007
@@ -228,7 +228,9 @@
   /// or will return true if the expressions didn't type check properly.
   
   /// type checking binary operators (subroutines of ParseBinOp).
-  ExprResult CheckMultiplicativeOperands(Expr *op1, Expr *op2); // C99 6.5.5
+  ExprResult CheckMultiplicativeOperands(Expr *op1, Expr *op2,
+          SourceLocation OpLoc, 
+          unsigned /*BinaryOperator::Opcode*/OpCode); // C99 6.5.5
   ExprResult CheckAdditiveOperands(Expr *op1, Expr *op2);       // C99 6.5.6
   ExprResult CheckShiftOperands(Expr *op1, Expr *op2);          // C99 6.5.7
   ExprResult CheckRelationalOperands(Expr *op1, Expr *op2);     // C99 6.5.8

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

==============================================================================
--- cfe/cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaExpr.cpp Wed Jul 11 11:44:04 2007
@@ -182,7 +182,8 @@
     return CheckIndirectionOperand((Expr *)Input, OpLoc, Opc);
   else { 
     // handle the arithmetic unary operators (C99 6.5.3.3)
-    QualType opType = ImplicitConversion((Expr *)Input)->getType();
+    Expr *operand = ImplicitConversion((Expr *)Input);
+    QualType opType = operand->getType();
     assert(!opType.isNull() && "no type for arithmetic unary expression");
     QualType resultType = opType;
     
@@ -214,7 +215,7 @@
     default: 
       break;
     }
-    return new UnaryOperator((Expr*)Input, Opc, resultType);
+    return new UnaryOperator(operand, Opc, resultType);
   }
 }
 
@@ -395,25 +396,25 @@
   }
 
   // perform implicit conversions (C99 6.3)
-  Expr *e1 = ImplicitConversion((Expr*)LHS);
-  Expr *e2 = ImplicitConversion((Expr*)RHS);
+  Expr *lhs = ImplicitConversion((Expr*)LHS);
+  Expr *rhs = ImplicitConversion((Expr*)RHS);
   
   if (BinaryOperator::isMultiplicativeOp(Opc)) 
-    CheckMultiplicativeOperands((Expr*)LHS, (Expr*)RHS);
+    return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc);
   else if (BinaryOperator::isAdditiveOp(Opc))
-    CheckAdditiveOperands((Expr*)LHS, (Expr*)RHS);
+    CheckAdditiveOperands(lhs, rhs);
   else if (BinaryOperator::isShiftOp(Opc))
-    CheckShiftOperands((Expr*)LHS, (Expr*)RHS);
+    CheckShiftOperands(lhs, rhs);
   else if (BinaryOperator::isRelationalOp(Opc))
-    CheckRelationalOperands((Expr*)LHS, (Expr*)RHS);
+    CheckRelationalOperands(lhs, rhs);
   else if (BinaryOperator::isEqualityOp(Opc))
-    CheckEqualityOperands((Expr*)LHS, (Expr*)RHS);
+    CheckEqualityOperands(lhs, rhs);
   else if (BinaryOperator::isBitwiseOp(Opc))
-    CheckBitwiseOperands((Expr*)LHS, (Expr*)RHS);
+    CheckBitwiseOperands(lhs, rhs);
   else if (BinaryOperator::isLogicalOp(Opc))
-    CheckLogicalOperands((Expr*)LHS, (Expr*)RHS);
+    CheckLogicalOperands(lhs, rhs);
   
-  return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
+  return new BinaryOperator(lhs, rhs, Opc);
 }
 
 /// ParseConditionalOp - Parse a ?: operation.  Note that 'LHS' may be null
@@ -443,8 +444,24 @@
   return E;
 }
 
-Action::ExprResult Sema::CheckMultiplicativeOperands(Expr *op1, Expr *op2) {
-  return false;
+Action::ExprResult Sema::CheckMultiplicativeOperands(
+  Expr *lexpr, Expr *rexpr, SourceLocation loc, unsigned code) 
+{
+  lexpr = ImplicitConversion((Expr *)lexpr);
+  rexpr = ImplicitConversion((Expr *)rexpr);
+  QualType ltype = lexpr->getType();
+  QualType rtype = rexpr->getType();
+  assert(!ltype.isNull() && "no left type for CheckMultiplicativeOperands()");
+  assert(!rtype.isNull() && "no right type for CheckMultiplicativeOperands()");
+  
+  if ((BinaryOperator::Opcode)code == BinaryOperator::Rem) {
+    if (!ltype->isIntegralType() || !rtype->isIntegralType())
+      return Diag(loc, diag::err_typecheck_invalid_operands);
+  } else {
+    if (!ltype->isArithmeticType() || !rtype->isArithmeticType())
+      return Diag(loc, diag::err_typecheck_invalid_operands);
+  }
+  return new BinaryOperator(lexpr, rexpr, (BinaryOperator::Opcode)code);
 }
 
 Action::ExprResult Sema::CheckAdditiveOperands(Expr *op1, Expr *op2) {

Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=39410&r1=39409&r2=39410&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:44:04 2007
@@ -535,6 +535,8 @@
      "invalid lvalue in address expression")
 DIAG(err_typecheck_unary_expr, ERROR,
      "invalid argument type to unary expression '%s'")
+DIAG(err_typecheck_invalid_operands, ERROR,
+     "invalid operands to binary expression")
 
 // Statements.
 DIAG(err_continue_not_in_loop, ERROR,





More information about the cfe-commits mailing list