OSDN Git Service

(lookup_field): Change name comparison to match what field_decl_cmp
[pf3gnuchains/gcc-fork.git] / gcc / c-typeck.c
index e4cfd45..8c5f2cd 100644 (file)
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
 /* This file is part of the C front end.
@@ -209,14 +210,15 @@ common_type (t1, t2)
          attributes = a2;
        else
        {
-         /* Pick the longest list, and hang on the other
-            list.  */
+         /* Pick the longest list, and hang on the other list.  */
+         /* ??? For the moment we punt on the issue of attrs with args.  */
        
          if (list_length (a1) < list_length (a2))
             attributes = a2, a2 = a1;
 
          for (; a2; a2 = TREE_CHAIN (a2))
-            if (!value_member (attributes, a2))
+           if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
+                                 attributes) == NULL_TREE)
              {
                a1 = copy_node (a2);
                TREE_CHAIN (a1) = attributes;
@@ -1136,8 +1138,6 @@ lookup_field (type, component, indirect)
       top = TYPE_LANG_SPECIFIC (type)->len;
       while (top - bot > 1)
        {
-         HOST_WIDE_INT cmp;
-
          half = (top - bot + 1) >> 1;
          field = field_array[bot+half];
 
@@ -1165,10 +1165,9 @@ lookup_field (type, component, indirect)
              continue;
            }
 
-         cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component;
-         if (cmp == 0)
+         if (DECL_NAME (field) == component)
            break;
-         if (cmp < 0)
+         if (DECL_NAME (field) < component)
            bot += half;
          else
            top = bot + half;
@@ -1897,8 +1896,12 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
      Zero means they need to be converted to RESULT_TYPE.  */
   int converted = 0;
 
+  /* Nonzero means create the expression with this type, rather than
+     RESULT_TYPE.  */
+  tree build_type = 0;
+
   /* Nonzero means after finally constructing the expression
-     give it this type.  Otherwise, give it type RESULT_TYPE.  */
+     convert it to this type.  */
   tree final_type = 0;
 
   /* Nonzero if this is an operation like MIN or MAX which can
@@ -2169,8 +2172,7 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
     case NE_EXPR:
       /* Result of comparison is always int,
         but don't convert the args to int!  */
-      result_type = integer_type_node;
-      converted = 1;
+      build_type = integer_type_node;
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
           || code0 == COMPLEX_TYPE)
          && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
@@ -2184,7 +2186,7 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
             Otherwise, the targets must be compatible
             and both must be object or both incomplete.  */
          if (comp_target_types (type0, type1))
-           ;
+           result_type = common_type (type0, type1);
          else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
            {
              /* op0 != orig_op0 detects the case of something
@@ -2201,29 +2203,28 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
            }
          else
            pedwarn ("comparison of distinct pointer types lacks a cast");
+
+         if (result_type == NULL_TREE)
+           result_type = ptr_type_node;
        }
       else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
               && integer_zerop (op1))
-       op1 = null_pointer_node;
+       result_type = type0;
       else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
               && integer_zerop (op0))
-       op0 = null_pointer_node;
+       result_type = type1;
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
        {
+         result_type = type0;
          if (! flag_traditional)
            pedwarn ("comparison between pointer and integer");
-         op1 = convert (TREE_TYPE (op0), op1);
        }
       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
        {
+         result_type = type1;
          if (! flag_traditional)
            pedwarn ("comparison between pointer and integer");
-         op0 = convert (TREE_TYPE (op1), op0);
        }
-      else
-       /* If args are not valid, clear out RESULT_TYPE
-          to cause an error message later.  */
-       result_type = 0;
       break;
 
     case MAX_EXPR:
@@ -2233,12 +2234,18 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
        shorten = 1;
       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
        {
-         if (! comp_target_types (type0, type1))
-           pedwarn ("comparison of distinct pointer types lacks a cast");
-         else if (pedantic 
-                  && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
-           pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
-         result_type = common_type (type0, type1);
+         if (comp_target_types (type0, type1))
+           {
+             result_type = common_type (type0, type1);
+             if (pedantic 
+                 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
+               pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
+           }
+         else
+           {
+             result_type = ptr_type_node;
+             pedwarn ("comparison of distinct pointer types lacks a cast");
+           }
        }
       break;
 
@@ -2246,52 +2253,54 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
     case GE_EXPR:
     case LT_EXPR:
     case GT_EXPR:
+      build_type = integer_type_node;
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
          && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
        short_compare = 1;
       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
        {
-         if (! comp_target_types (type0, type1))
-           pedwarn ("comparison of distinct pointer types lacks a cast");
-         else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
-                  != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
-           pedwarn ("comparison of complete and incomplete pointers");
-         else if (pedantic 
-                  && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
-           pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
-         result_type = integer_type_node;
+         if (comp_target_types (type0, type1))
+           {
+             result_type = common_type (type0, type1);
+             if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
+                 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
+               pedwarn ("comparison of complete and incomplete pointers");
+             else if (pedantic 
+                      && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
+               pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
+           }
+         else
+           {
+             result_type = ptr_type_node;
+             pedwarn ("comparison of distinct pointer types lacks a cast");
+           }
        }
       else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
               && integer_zerop (op1))
        {
-         result_type = integer_type_node;
-         op1 = null_pointer_node;
-         if (pedantic)
+         result_type = type0;
+         if (pedantic || extra_warnings)
            pedwarn ("ordered comparison of pointer with integer zero");
        }
       else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
               && integer_zerop (op0))
        {
-         result_type = integer_type_node;
-         op0 = null_pointer_node;
+         result_type = type1;
          if (pedantic)
            pedwarn ("ordered comparison of pointer with integer zero");
        }
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
        {
-         result_type = integer_type_node;
+         result_type = type0;
          if (! flag_traditional)
            pedwarn ("comparison between pointer and integer");
-         op1 = convert (TREE_TYPE (op0), op1);
        }
       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
        {
-         result_type = integer_type_node;
+         result_type = type1;
          if (! flag_traditional)
            pedwarn ("comparison between pointer and integer");
-         op0 = convert (TREE_TYPE (op1), op0);
        }
-      converted = 1;
       break;
     }
 
@@ -2433,7 +2442,8 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
            = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
          if (val != 0)
            return val;
-         op0 = xop0, op1 = xop1, result_type = xresult_type;
+         op0 = xop0, op1 = xop1;
+         converted = 1;
          resultcode = xresultcode;
 
          if (extra_warnings)
@@ -2441,7 +2451,9 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
              int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
              int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
 
-             tree comp_type = TREE_TYPE (op0);
+             int unsignedp0, unsignedp1;
+             tree primop0 = get_narrower (op0, &unsignedp0);
+             tree primop1 = get_narrower (op1, &unsignedp1);
 
              /* Avoid spurious warnings for comparison with enumerators.  */
  
@@ -2451,20 +2463,92 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
              STRIP_TYPE_NOPS (xop1);
 
              /* Give warnings for comparisons between signed and unsigned
-                quantities that may fail.  Do not warn if the signed quantity
-                is an unsuffixed integer literal (or some static constant
-                expression involving such literals) and it is positive.
-                Do not warn if the comparison is being done in a signed type,
-                since the signed type will only be chosen if it can represent
-                all the values of the unsigned type.  */
+                quantities that may fail.  */
              /* Do the checking based on the original operand trees, so that
                 casts will be considered, but default promotions won't be.  */
-             if (TREE_UNSIGNED (comp_type)
-                 && ((op0_signed && (TREE_CODE (xop0) != INTEGER_CST
-                                     || tree_int_cst_sgn (xop0) == -1))
-                     || (op1_signed && (TREE_CODE (xop1) != INTEGER_CST
-                                      || tree_int_cst_sgn (xop1) == -1))))
+
+             /* Do not warn if the comparison is being done in a signed type,
+                since the signed type will only be chosen if it can represent
+                all the values of the unsigned type.  */
+             if (! TREE_UNSIGNED (result_type))
+               /* OK */;
+              /* Do not warn if both operands are unsigned.  */
+              else if (op0_signed == op1_signed)
+                /* OK */;
+             /* Do not warn if the signed quantity is an unsuffixed
+                integer literal (or some static constant expression
+                involving such literals) and it is non-negative.  */
+             else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
+                       && tree_int_cst_sgn (xop0) >= 0)
+                      || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
+                          && tree_int_cst_sgn (xop1) >= 0))
+               /* OK */;
+             /* Do not warn if the comparison is an equality operation,
+                 the unsigned quantity is an integral constant and it does
+                 not use the most significant bit of result_type.  */
+             else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
+                      && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
+                           && int_fits_type_p (xop1, signed_type (result_type))
+                          || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
+                              && int_fits_type_p (xop0, signed_type (result_type))))))
+               /* OK */;
+             else
                warning ("comparison between signed and unsigned");
+
+             /* Warn if two unsigned values are being compared in a size
+                larger than their original size, and one (and only one) is the
+                result of a `~' operator.  This comparison will always fail.
+
+                Also warn if one operand is a constant, and the constant
+                does not have all bits set that are set in the ~ operand
+                when it is extended.  */
+
+             if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
+                 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
+               {
+                 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
+                   primop0 = get_narrower (TREE_OPERAND (primop0, 0),
+                                           &unsignedp0);
+                 else
+                   primop1 = get_narrower (TREE_OPERAND (primop1, 0),
+                                           &unsignedp1);
+             
+                 if (TREE_CODE (primop0) == INTEGER_CST
+                     || TREE_CODE (primop1) == INTEGER_CST)
+                   {
+                     tree primop;
+                     long constant, mask;
+                     int unsignedp, bits;
+
+                     if (TREE_CODE (primop0) == INTEGER_CST)
+                       {
+                         primop = primop1;
+                         unsignedp = unsignedp1;
+                         constant = TREE_INT_CST_LOW (primop0);
+                       }
+                     else
+                       {
+                         primop = primop0;
+                         unsignedp = unsignedp0;
+                         constant = TREE_INT_CST_LOW (primop1);
+                       }
+
+                     bits = TYPE_PRECISION (TREE_TYPE (primop));
+                     if (bits < TYPE_PRECISION (result_type)
+                         && bits < HOST_BITS_PER_LONG && unsignedp)
+                       {
+                         mask = (~0L) << bits;
+                         if ((mask & constant) != mask)
+                           warning ("comparison of promoted ~unsigned with constant");
+                       }
+                   }
+                 else if (unsignedp0 && unsignedp1
+                          && (TYPE_PRECISION (TREE_TYPE (primop0))
+                              < TYPE_PRECISION (result_type))
+                          && (TYPE_PRECISION (TREE_TYPE (primop1))
+                              < TYPE_PRECISION (result_type)))
+                   warning ("comparison of promoted ~unsigned with unsigned");
+               }
            }
        }
     }
@@ -2489,8 +2573,11 @@ build_binary_op (code, orig_op0, orig_op1, convert_p)
        op1 = convert (result_type, op1); 
     }
 
+  if (build_type == NULL_TREE)
+    build_type = result_type;
+
   {
-    register tree result = build (resultcode, result_type, op0, op1);
+    register tree result = build (resultcode, build_type, op0, op1);
     register tree folded;
 
     folded = fold (result);
@@ -4022,7 +4109,8 @@ convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
                warn_for_assignment ("pointer targets in %s differ in signedness",
                                     get_spelling (errtype), funname, parmnum);
            }
-         else
+         else if (TREE_CODE (ttl) == FUNCTION_TYPE
+                  && TREE_CODE (ttr) == FUNCTION_TYPE)
            {
              /* Because const and volatile on functions are restrictions
                 that say the function will not do certain things,
@@ -4149,7 +4237,8 @@ initializer_constant_valid_p (value, endtype)
   switch (TREE_CODE (value))
     {
     case CONSTRUCTOR:
-      if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
+      if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
+          || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
          && TREE_CONSTANT (value))
        return
          initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
@@ -4831,7 +4920,7 @@ static tree constructor_fields;
 static tree constructor_index;
 
 /* For an ARRAY_TYPE, this is the end index of the range
-   to intitialize with the next element, or NULL in the ordinary case
+   to initialize with the next element, or NULL in the ordinary case
    where the element is used just once.  */
 static tree constructor_range_end;
 
@@ -5255,6 +5344,7 @@ push_init_level (implicit)
        {
          constructor_type = TREE_TYPE (constructor_fields);
          push_member_name (constructor_fields);
+         constructor_depth++;
          if (constructor_fields != constructor_unfilled_fields)
            constructor_incremental = 0;
        }
@@ -5263,6 +5353,7 @@ push_init_level (implicit)
     {
       constructor_type = TREE_TYPE (constructor_type);
       push_array_bounds (TREE_INT_CST_LOW (constructor_index));
+      constructor_depth++;
       if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
          || constructor_range_end != 0)
        constructor_incremental = 0;
@@ -5404,7 +5495,8 @@ pop_init_level (implicit)
          && constructor_incremental)
        {
          constructor = digest_init (constructor_type, constructor,
-                                    0, 0);
+                                    require_constant_value,
+                                    require_constant_elements);
 
          /* If initializing an array of unknown size,
             determine the size now.  */
@@ -5676,7 +5768,10 @@ output_init_element (value, type, field, pending)
     constructor_erroneous = 1;
   else if (!TREE_CONSTANT (value))
     constructor_constant = 0;
-  else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
+  else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
+          || ((TREE_CODE (constructor_type) == RECORD_TYPE
+               || TREE_CODE (constructor_type) == UNION_TYPE)
+              && DECL_BIT_FIELD (field) && TREE_CODE (value) != INTEGER_CST))
     constructor_simple = 0;
 
   if (require_constant_value && ! TREE_CONSTANT (value))
@@ -5736,7 +5831,8 @@ output_init_element (value, type, field, pending)
           constructor_index, which is modified in place.  */
        constructor_pending_elts
          = tree_cons (copy_node (field),
-                      digest_init (type, value, 0, 0),
+                      digest_init (type, value, require_constant_value, 
+                                   require_constant_elements),
                       constructor_pending_elts);
     }
   else if (TREE_CODE (constructor_type) == RECORD_TYPE
@@ -5748,7 +5844,8 @@ output_init_element (value, type, field, pending)
       if (!duplicate)
        constructor_pending_elts
          = tree_cons (field,
-                      digest_init (type, value, 0, 0),
+                      digest_init (type, value, require_constant_value, 
+                                   require_constant_elements),
                       constructor_pending_elts);
     }
   else
@@ -5763,7 +5860,9 @@ output_init_element (value, type, field, pending)
              if (field && TREE_CODE (field) == INTEGER_CST)
                field = copy_node (field);
              constructor_elements
-               = tree_cons (field, digest_init (type, value, 0, 0),
+               = tree_cons (field, digest_init (type, value,
+                                                require_constant_value, 
+                                                require_constant_elements),
                             constructor_elements);
            }
          else
@@ -5784,7 +5883,9 @@ output_init_element (value, type, field, pending)
                      assemble_zeros (next - here);
                    }
                }
-             output_constant (digest_init (type, value, 0, 0),
+             output_constant (digest_init (type, value,
+                                           require_constant_value,
+                                           require_constant_elements),
                               int_size_in_bytes (type));
 
              /* For a record or union,
@@ -6246,7 +6347,8 @@ process_init_element (value)
 
   /* If the (lexically) previous elments are not now saved,
      we can discard the storage for them.  */
-  if (constructor_incremental && constructor_pending_elts == 0 && value != 0)
+  if (constructor_incremental && constructor_pending_elts == 0 && value != 0
+      && constructor_stack == 0)
     clear_momentary ();
 }
 \f