OSDN Git Service

2007-01-07 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cp / typeck.c
index 242ae19..2a6c8f8 100644 (file)
@@ -1,6 +1,7 @@
 /* Build expressions with type checking for C++ compiler.
    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+   Free Software Foundation, Inc.
    Hacked by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GCC.
@@ -43,6 +44,7 @@ Boston, MA 02110-1301, USA.  */
 #include "target.h"
 #include "convert.h"
 #include "c-common.h"
+#include "params.h"
 
 static tree pfn_from_ptrmemfunc (tree);
 static tree convert_for_assignment (tree, tree, const char *, tree, int);
@@ -262,7 +264,7 @@ type_after_usual_arithmetic_conversions (tree t1, tree t2)
              || TREE_CODE (t1) == ENUMERAL_TYPE);
   gcc_assert (ARITHMETIC_TYPE_P (t2)
              || TREE_CODE (t2) == COMPLEX_TYPE
-             || TREE_CODE (t1) == VECTOR_TYPE
+             || TREE_CODE (t2) == VECTOR_TYPE
              || TREE_CODE (t2) == ENUMERAL_TYPE);
 
   /* In what follows, we slightly generalize the rules given in [expr] so
@@ -927,11 +929,10 @@ comp_array_types (tree t1, tree t2, bool allow_redeclaration)
   return true;
 }
 
-/* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
-   is a bitwise-or of the COMPARE_* flags.  */
+/* Subroutine in comptypes.  */
 
-bool
-comptypes (tree t1, tree t2, int strict)
+static bool
+structural_comptypes (tree t1, tree t2, int strict)
 {
   if (t1 == t2)
     return true;
@@ -1090,6 +1091,65 @@ comptypes (tree t1, tree t2, int strict)
   return targetm.comp_type_attributes (t1, t2);
 }
 
+/* Return true if T1 and T2 are related as allowed by STRICT.  STRICT
+   is a bitwise-or of the COMPARE_* flags.  */
+
+bool
+comptypes (tree t1, tree t2, int strict)
+{
+  if (strict == COMPARE_STRICT)
+    {
+      bool result;
+
+      if (t1 == t2)
+       return true;
+
+      if (t1 == error_mark_node || t2 == error_mark_node)
+       return false;
+
+      if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
+       /* At least one of the types requires structural equality, so
+          perform a deep check. */
+       return structural_comptypes (t1, t2, strict);
+
+      if (VERIFY_CANONICAL_TYPES)
+       {
+         result = structural_comptypes (t1, t2, strict);
+
+         if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
+           {
+             /* The two types are structurally equivalent, but their
+                canonical types were different. This is a failure of the
+                canonical type propagation code.*/
+             warning(0,
+                     "canonical types differ for identical types %T and %T", 
+                     t1, t2);
+             debug_tree (t1);
+             debug_tree (t2);
+           }
+         else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
+           {
+             /* Two types are structurally different, but the canonical
+                types are the same. This means we were over-eager in
+                assigning canonical types. */
+             warning (0, 
+                      "same canonical type node for different types %T and %T",
+                      t1, t2);
+             debug_tree (t1);
+             debug_tree (t2);
+           }
+         
+         return result;
+       }
+      else
+       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
+    }
+  else if (strict == COMPARE_STRUCTURAL)
+    return structural_comptypes (t1, t2, COMPARE_STRICT);
+  else
+    return structural_comptypes (t1, t2, strict);
+}
+
 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
 
 bool
@@ -1648,17 +1708,20 @@ rationalize_conditional_expr (enum tree_code code, tree t)
      are equal, so we know what conditional expression this used to be.  */
   if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
     {
+      tree op0 = TREE_OPERAND (t, 0);
+      tree op1 = TREE_OPERAND (t, 1);
+
       /* The following code is incorrect if either operand side-effects.  */
-      gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
-                 && !TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)));
+      gcc_assert (!TREE_SIDE_EFFECTS (op0)
+                 && !TREE_SIDE_EFFECTS (op1));
       return
        build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
                                                    ? LE_EXPR : GE_EXPR),
-                                                  TREE_OPERAND (t, 0),
-                                                  TREE_OPERAND (t, 1),
+                                                  op0, TREE_CODE (op0),
+                                                  op1, TREE_CODE (op1),
                                                   /*overloaded_p=*/NULL),
-                           build_unary_op (code, TREE_OPERAND (t, 0), 0),
-                           build_unary_op (code, TREE_OPERAND (t, 1), 0));
+                           build_unary_op (code, op0, 0),
+                           build_unary_op (code, op1, 0));
     }
 
   return
@@ -2865,11 +2928,19 @@ convert_arguments (tree typelist, tree values, tree fndecl, int flags)
 }
 \f
 /* Build a binary-operation expression, after performing default
-   conversions on the operands.  CODE is the kind of expression to build.  */
+   conversions on the operands.  CODE is the kind of expression to
+   build.  ARG1 and ARG2 are the arguments.  ARG1_CODE and ARG2_CODE
+   are the tree codes which correspond to ARG1 and ARG2 when issuing
+   warnings about possibly misplaced parentheses.  They may differ
+   from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
+   folding (e.g., if the parser sees "a | 1 + 1", it may call this
+   routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
+   To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
+   ARG2_CODE as ERROR_MARK.  */
 
 tree
-build_x_binary_op (enum tree_code code, tree arg1, tree arg2,
-                  bool *overloaded_p)
+build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
+                  tree arg2, enum tree_code arg2_code, bool *overloaded_p)
 {
   tree orig_arg1;
   tree orig_arg2;
@@ -2893,6 +2964,17 @@ build_x_binary_op (enum tree_code code, tree arg1, tree arg2,
     expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
                         overloaded_p);
 
+  /* Check for cases such as x+y<<z which users are likely to
+     misinterpret.  But don't warn about obj << x + y, since that is a
+     common idiom for I/O.  */
+  if (warn_parentheses
+      && !processing_template_decl
+      && !error_operand_p (arg1)
+      && !error_operand_p (arg2)
+      && (code != LSHIFT_EXPR
+         || !CLASS_TYPE_P (TREE_TYPE (arg1))))
+    warn_about_parentheses (code, arg1_code, arg2_code);
+
   if (processing_template_decl && expr != error_mark_node)
     return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
 
@@ -3093,17 +3175,19 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
          && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
              || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
        {
+         enum tree_code tcode0 = code0, tcode1 = code1;
+
          if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
            warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0%>", op0);
          else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
            warning (OPT_Wdiv_by_zero, "division by zero in %<%E / 0.%>", op0);
 
-         if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
-           code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
-         if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
-           code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
+         if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
+           tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
+         if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
+           tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
 
-         if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
+         if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
            resultcode = RDIV_EXPR;
          else
            /* When dividing two signed integers, we have to promote to int.
@@ -3250,10 +3334,22 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
                                              "comparison");
       else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
               && null_ptr_cst_p (op1))
-       result_type = type0;
+       {
+         if (TREE_CODE (op0) == ADDR_EXPR
+             && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
+           warning (OPT_Walways_true, "the address of %qD will never be NULL",
+                    TREE_OPERAND (op0, 0));
+         result_type = type0;
+       }
       else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
               && null_ptr_cst_p (op0))
-       result_type = type1;
+       {
+         if (TREE_CODE (op1) == ADDR_EXPR 
+             && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
+           warning (OPT_Walways_true, "the address of %qD will never be NULL",
+                    TREE_OPERAND (op1, 0));
+         result_type = type1;
+       }
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
        {
          result_type = type0;
@@ -3266,8 +3362,28 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
        }
       else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
        {
-         op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
-         op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
+         if (TARGET_PTRMEMFUNC_VBIT_LOCATION
+             == ptrmemfunc_vbit_in_delta)
+           {
+             tree pfn0 = pfn_from_ptrmemfunc (op0);
+             tree delta0 = build_ptrmemfunc_access_expr (op0,
+                                                         delta_identifier);
+             tree e1 = cp_build_binary_op (EQ_EXPR,
+                                           pfn0,       
+                                           fold_convert (TREE_TYPE (pfn0),
+                                                         integer_zero_node));
+             tree e2 = cp_build_binary_op (BIT_AND_EXPR, 
+                                           delta0,
+                                           integer_one_node);
+             e2 = cp_build_binary_op (EQ_EXPR, e2, integer_zero_node);
+             op0 = cp_build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
+             op1 = cp_convert (TREE_TYPE (op0), integer_one_node); 
+           }
+         else 
+           {
+             op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
+             op1 = cp_convert (TREE_TYPE (op0), integer_zero_node); 
+           }
          result_type = TREE_TYPE (op0);
        }
       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
@@ -3290,26 +3406,61 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
          if (TREE_SIDE_EFFECTS (op1))
            op1 = save_expr (op1);
 
-         /* We generate:
-
-            (op0.pfn == op1.pfn
-             && (!op0.pfn || op0.delta == op1.delta))
-
-            The reason for the `!op0.pfn' bit is that a NULL
-            pointer-to-member is any member with a zero PFN; the
-            DELTA field is unspecified.  */
          pfn0 = pfn_from_ptrmemfunc (op0);
          pfn1 = pfn_from_ptrmemfunc (op1);
          delta0 = build_ptrmemfunc_access_expr (op0,
                                                 delta_identifier);
          delta1 = build_ptrmemfunc_access_expr (op1,
                                                 delta_identifier);
-         e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
-         e2 = cp_build_binary_op (EQ_EXPR,
-                                  pfn0,
-                                  cp_convert (TREE_TYPE (pfn0),
-                                              integer_zero_node));
-         e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
+         if (TARGET_PTRMEMFUNC_VBIT_LOCATION
+             == ptrmemfunc_vbit_in_delta)
+           {
+             /* We generate:
+
+                (op0.pfn == op1.pfn
+                 && ((op0.delta == op1.delta)
+                      || (!op0.pfn && op0.delta & 1 == 0 
+                          && op1.delta & 1 == 0))
+
+                The reason for the `!op0.pfn' bit is that a NULL
+                pointer-to-member is any member with a zero PFN and
+                LSB of the DELTA field is 0.  */
+
+             e1 = cp_build_binary_op (BIT_AND_EXPR,
+                                      delta0, 
+                                      integer_one_node);
+             e1 = cp_build_binary_op (EQ_EXPR, e1, integer_zero_node);
+             e2 = cp_build_binary_op (BIT_AND_EXPR,
+                                      delta1,
+                                      integer_one_node);
+             e2 = cp_build_binary_op (EQ_EXPR, e2, integer_zero_node);
+             e1 = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
+             e2 = cp_build_binary_op (EQ_EXPR,
+                                      pfn0,
+                                      fold_convert (TREE_TYPE (pfn0),
+                                                    integer_zero_node));
+             e2 = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
+             e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
+             e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
+           }
+         else
+           {
+             /* We generate:
+
+                (op0.pfn == op1.pfn
+                && (!op0.pfn || op0.delta == op1.delta))
+
+                The reason for the `!op0.pfn' bit is that a NULL
+                pointer-to-member is any member with a zero PFN; the
+                DELTA field is unspecified.  */
+             e1 = cp_build_binary_op (EQ_EXPR, delta0, delta1);
+             e2 = cp_build_binary_op (EQ_EXPR,
+                                      pfn0,
+                                      fold_convert (TREE_TYPE (pfn0),
+                                                  integer_zero_node));
+             e1 = cp_build_binary_op (TRUTH_ORIF_EXPR, e1, e2);
+           }
          e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
          e = cp_build_binary_op (TRUTH_ANDIF_EXPR, e2, e1);
          if (code == EQ_EXPR)
@@ -3719,6 +3870,12 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
   result = fold_if_not_in_template (result);
   if (final_type != 0)
     result = cp_convert (final_type, result);
+
+  if (TREE_OVERFLOW_P (result) 
+      && !TREE_OVERFLOW_P (op0) 
+      && !TREE_OVERFLOW_P (op1))
+    overflow_warning (result);
+
   return result;
 }
 \f
@@ -3995,7 +4152,8 @@ build_unary_op (enum tree_code code, tree xarg, int noconvert)
          if (!noconvert)
            arg = default_conversion (arg);
        }
-      else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
+      else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
+                                                  | WANT_VECTOR,
                                                   arg, true)))
        errstring = "wrong type argument to bit-complement";
       else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
@@ -4146,9 +4304,6 @@ build_unary_op (enum tree_code code, tree xarg, int noconvert)
          case CONVERT_EXPR:
          case FLOAT_EXPR:
          case FIX_TRUNC_EXPR:
-         case FIX_FLOOR_EXPR:
-         case FIX_ROUND_EXPR:
-         case FIX_CEIL_EXPR:
            {
              tree incremented, modify, value, compound;
              if (! lvalue_p (arg) && pedantic)
@@ -4301,9 +4456,6 @@ build_unary_op (enum tree_code code, tree xarg, int noconvert)
        case CONVERT_EXPR:
        case FLOAT_EXPR:
        case FIX_TRUNC_EXPR:
-       case FIX_FLOOR_EXPR:
-       case FIX_ROUND_EXPR:
-       case FIX_CEIL_EXPR:
          if (! lvalue_p (arg) && pedantic)
            pedwarn ("ISO C++ forbids taking the address of a cast to a non-lvalue expression");
          break;
@@ -6232,6 +6384,17 @@ convert_for_assignment (tree type, tree rhs,
                 errtype);
     }
 
+  /* If -Wparentheses, warn about a = b = c when a has type bool.  */
+  if (warn_parentheses
+      && type == boolean_type_node
+      && TREE_CODE (rhs) == MODIFY_EXPR
+      && !TREE_NO_WARNING (rhs))
+    {
+      warning (OPT_Wparentheses,
+              "suggest parentheses around assignment used as truth value");
+      TREE_NO_WARNING (rhs) = 1;
+    }
+
   return perform_implicit_conversion (strip_top_quals (type), rhs);
 }