OSDN Git Service

* emit-rtl.c (add_insn_before): Fix comment typo.
[pf3gnuchains/gcc-fork.git] / gcc / c-decl.c
index a723c25..a0053ed 100644 (file)
@@ -1,6 +1,6 @@
 /* Process declarations and variables for C compiler.
    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -29,6 +29,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "input.h"
 #include "tm.h"
 #include "intl.h"
 #include "tree.h"
@@ -634,7 +635,7 @@ push_scope (void)
       if (current_scope && scope->depth == 0)
        {
          scope->depth--;
-         sorry ("GCC supports only %u nested scopes\n", scope->depth);
+         sorry ("GCC supports only %u nested scopes", scope->depth);
        }
 
       current_scope        = scope;
@@ -1005,14 +1006,14 @@ diagnose_arglist_conflict (tree newdecl, tree olddecl,
       if (TREE_CHAIN (t) == 0
          && TYPE_MAIN_VARIANT (type) != void_type_node)
        {
-         inform ("a parameter list with an ellipsis can't match "
+         inform ("a parameter list with an ellipsis can%'t match "
                  "an empty parameter name list declaration");
          break;
        }
 
       if (c_type_promotes_to (type) != type)
        {
-         inform ("an argument type that has a default promotion can't match "
+         inform ("an argument type that has a default promotion can%'t match "
                  "an empty parameter name list declaration");
          break;
        }
@@ -1063,7 +1064,8 @@ validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
         for the arg.  */
       else if (!comptypes (oldargtype, newargtype))
        {
-         error ("%Jprototype for %qD declares arg %d with incompatible type",
+         error ("%Jprototype for %qD declares argument %d"
+                " with incompatible type",
                 newdecl, newdecl, i);
          return false;
        }
@@ -1190,7 +1192,7 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl,
       else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
               && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
               && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
-              && C_FUNCTION_IMPLICIT_INT (newdecl))
+              && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
        {
          pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
          /* Make sure we keep void as the return type.  */
@@ -1198,6 +1200,18 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl,
          C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
          pedwarned = true;
        }
+      /* Permit void foo (...) to match an earlier call to foo (...) with
+        no declared type (thus, implicitly int).  */
+      else if (TREE_CODE (newdecl) == FUNCTION_DECL
+              && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
+              && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
+              && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
+       {
+         pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
+         /* Make sure we keep void as the return type.  */
+         TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
+         pedwarned = true;
+       }
       else
        {
          if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
@@ -2056,30 +2070,6 @@ pushdecl (tree x)
            }
        }
     }
-  /* Similarly, a declaration of a function with static linkage at
-     block scope must be checked against any existing declaration
-     of that function at file scope.  */
-  else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
-          && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
-    {
-      if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
-       warning ("nested static declaration of %qD", x);
-
-      while (b && !B_IN_FILE_SCOPE (b))
-       b = b->shadowed;
-
-      if (b && same_translation_unit_p (x, b->decl)
-         && duplicate_decls (x, b->decl))
-       {
-         bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
-         return b->decl;
-       }
-      else
-       {
-         bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
-         nested = true;
-       }
-    }
 
   warn_if_shadowing (x);
 
@@ -2128,12 +2118,11 @@ pushdecl_top_level (tree x)
 {
   tree name;
   bool nested = false;
-
-  gcc_assert (TREE_CODE (x) == VAR_DECL);
+  gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
 
   name = DECL_NAME (x);
 
 gcc_assert (!I_SYMBOL_BINDING (name));
gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
 
   if (TREE_PUBLIC (x))
     {
@@ -2696,8 +2685,6 @@ shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
 {
   bool found_tag = false;
 
-  pending_invalid_xref = 0;
-
   if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
     {
       tree value = declspecs->type;
@@ -2721,8 +2708,29 @@ shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
                  warned = 1;
                }
            }
+         else if (!declspecs->tag_defined_p
+                  && declspecs->storage_class != csc_none)
+           {
+             if (warned != 1)
+               pedwarn ("empty declaration with storage class specifier "
+                        "does not redeclare tag");
+             warned = 1;
+             pending_xref_error ();
+           }
+         else if (!declspecs->tag_defined_p
+                  && (declspecs->const_p
+                      || declspecs->volatile_p
+                      || declspecs->restrict_p))
+           {
+             if (warned != 1)
+               pedwarn ("empty declaration with type qualifier "
+                        "does not redeclare tag");
+             warned = 1;
+             pending_xref_error ();
+           }
          else
            {
+             pending_invalid_xref = 0;
              t = lookup_tag (code, name, 1);
 
              if (t == 0)
@@ -2747,6 +2755,8 @@ shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
       warned = 1;
     }
 
+  pending_invalid_xref = 0;
+
   if (declspecs->inline_p)
     {
       error ("%<inline%> in empty declaration");
@@ -2931,6 +2941,8 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
 
   decl = grokdeclarator (declarator, declspecs,
                         NORMAL, initialized, NULL);
+  if (!decl)
+    return 0;
 
   deprecated_state = DEPRECATED_NORMAL;
 
@@ -3200,14 +3212,7 @@ finish_decl (tree decl, tree init, tree asmspec_tree)
   if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
     {
       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
-       {
-         tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
-         set_user_assembler_name (builtin, asmspec);
-          if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
-            init_block_move_fn (asmspec);
-          else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
-            init_block_clear_fn (asmspec);
-        }
+       set_builtin_user_assembler_name (decl, asmspec);
       set_user_assembler_name (decl, asmspec);
     }
 
@@ -3475,6 +3480,7 @@ build_compound_literal (tree type, tree init)
       DECL_DEFER_OUTPUT (decl) = 1;
       DECL_COMDAT (decl) = 1;
       DECL_ARTIFICIAL (decl) = 1;
+      DECL_IGNORED_P (decl) = 1;
       pushdecl (decl);
       rest_of_decl_compilation (decl, 1, 0);
     }
@@ -3788,10 +3794,6 @@ grokdeclarator (const struct c_declarator *declarator,
       && TREE_CODE (type) == INTEGER_TYPE)
     type = c_common_unsigned_type (type);
 
-  /* Check the type and width of a bit-field.  */
-  if (bitfield)
-    check_bitfield_type_and_width (&type, width, orig_name);
-
   /* Figure out the type qualifiers for the declaration.  There are
      two ways a declaration can become qualified.  One is something
      like `const int i' where the `const' is explicit.  Another is
@@ -4055,6 +4057,12 @@ grokdeclarator (const struct c_declarator *declarator,
                  }
                else
                  {
+                   /* Arrange for the SAVE_EXPR on the inside of the
+                      MINUS_EXPR, which allows the -1 to get folded
+                      with the +1 that happens when building TYPE_SIZE.  */
+                   if (size_varies)
+                     size = variable_size (size);
+
                    /* Compute the maximum valid index, that is, size
                       - 1.  Do the calculation in index_type, so that
                       if it is a variable the computations will be
@@ -4078,8 +4086,6 @@ grokdeclarator (const struct c_declarator *declarator,
                        continue;
                      }
                    
-                   if (size_varies)
-                     itype = variable_size (itype);
                    itype = build_index_type (itype);
                  }
              }
@@ -4113,14 +4119,9 @@ grokdeclarator (const struct c_declarator *declarator,
               zero.  */
            if (size && integer_zerop (size))
              {
-               layout_type (type);
                TYPE_SIZE (type) = bitsize_zero_node;
                TYPE_SIZE_UNIT (type) = size_zero_node;
              }
-           else if (declarator->kind == cdk_pointer)
-             /* We can never complete an array type which is the
-                target of a pointer, so go ahead and lay it out.  */
-             layout_type (type);
 
            if (decl_context != PARM
                && (array_ptr_quals != TYPE_UNQUALIFIED
@@ -4236,6 +4237,10 @@ grokdeclarator (const struct c_declarator *declarator,
 
   /* Now TYPE has the actual type.  */
 
+  /* Check the type and width of a bit-field.  */
+  if (bitfield)
+    check_bitfield_type_and_width (&type, width, orig_name);
+
   /* Did array size calculations overflow?  */
 
   if (TREE_CODE (type) == ARRAY_TYPE
@@ -4413,7 +4418,9 @@ grokdeclarator (const struct c_declarator *declarator,
     else if (TREE_CODE (type) == FUNCTION_TYPE)
       {
        if (storage_class == csc_register || threadp)
-         error ("invalid storage class for function %qs", name);
+         {
+           error ("invalid storage class for function %qs", name);
+          }
        else if (current_scope != file_scope)
          {
            /* Function declaration not at file scope.  Storage
@@ -4426,8 +4433,14 @@ grokdeclarator (const struct c_declarator *declarator,
                if (pedantic)
                  pedwarn ("invalid storage class for function %qs", name);
              }
-           if (storage_class == csc_static)
-             error ("invalid storage class for function %qs", name);
+           else if (storage_class == csc_static)
+             {
+               error ("invalid storage class for function %qs", name);
+               if (funcdef_flag)
+                 storage_class = declspecs->storage_class = csc_none;
+               else
+                 return 0;
+             }
          }
 
        decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
@@ -4648,10 +4661,14 @@ grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
       tree parm, type, typelt;
       unsigned int parmno;
 
-      /* If the arg types are incomplete in a declaration, they must
-        include undefined tags.  These tags can never be defined in
-        the scope of the declaration, so the types can never be
-        completed, and no call can be compiled successfully.  */
+      /* If there is a parameter of incomplete type in a definition,
+        this is an error.  In a declaration this is valid, and a
+        struct or union type may be completed later, before any calls
+        or definition of the function.  In the case where the tag was
+        first declared within the parameter list, a warning has
+        already been given.  If a parameter has void type, then
+        however the function cannot be defined or called, so
+        warn.  */
 
       for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
           parm;
@@ -4675,13 +4692,13 @@ grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
                  TREE_VALUE (typelt) = error_mark_node;
                  TREE_TYPE (parm) = error_mark_node;
                }
-             else
+             else if (VOID_TYPE_P (type))
                {
                  if (DECL_NAME (parm))
-                   warning ("%Jparameter %u (%qD) has incomplete type",
+                   warning ("%Jparameter %u (%qD) has void type",
                             parm, parmno, parm);
                  else
-                   warning ("%Jparameter %u has incomplete type",
+                   warning ("%Jparameter %u has void type",
                             parm, parmno);
                }
            }
@@ -4836,10 +4853,13 @@ get_parm_info (bool ellipsis)
 
        case CONST_DECL:
        case TYPE_DECL:
+       case FUNCTION_DECL:
          /* CONST_DECLs appear here when we have an embedded enum,
             and TYPE_DECLs appear here when we have an embedded struct
             or union.  No warnings for this - we already warned about the
-            type itself.  */
+            type itself.  FUNCTION_DECLs appear when there is an implicit
+            function declaration in the parameter list.  */
+
          TREE_CHAIN (decl) = others;
          others = decl;
          /* fall through */
@@ -4856,7 +4876,6 @@ get_parm_info (bool ellipsis)
 
          /* Other things that might be encountered.  */
        case LABEL_DECL:
-       case FUNCTION_DECL:
        case VAR_DECL:
        default:
          gcc_unreachable ();
@@ -4873,11 +4892,13 @@ get_parm_info (bool ellipsis)
 }
 \f
 /* Get the struct, enum or union (CODE says which) with tag NAME.
-   Define the tag as a forward-reference if it is not defined.  */
+   Define the tag as a forward-reference if it is not defined.
+   Return a c_typespec structure for the type specifier.  */
 
-tree
-xref_tag (enum tree_code code, tree name)
+struct c_typespec
+parser_xref_tag (enum tree_code code, tree name)
 {
+  struct c_typespec ret;
   /* If a cross reference is requested, look up the type
      already defined for this tag and return it.  */
 
@@ -4893,8 +4914,12 @@ xref_tag (enum tree_code code, tree name)
      this would not work properly if we return the reference found.
      (For example, with "struct foo" in an outer scope, "union foo;"
      must shadow that tag with a new one of union type.)  */
+  ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
   if (ref && TREE_CODE (ref) == code)
-    return ref;
+    {
+      ret.spec = ref;
+      return ret;
+    }
 
   /* If no such tag is yet defined, create a forward-reference node
      and record it as the "definition".
@@ -4917,7 +4942,18 @@ xref_tag (enum tree_code code, tree name)
 
   pushtag (name, ref);
 
-  return ref;
+  ret.spec = ref;
+  return ret;
+}
+
+/* Get the struct, enum or union (CODE says which) with tag NAME.
+   Define the tag as a forward-reference if it is not defined.
+   Return a tree for the type.  */
+
+tree
+xref_tag (enum tree_code code, tree name)
+{
+  return parser_xref_tag (code, name).spec;
 }
 \f
 /* Make sure that the tag NAME is defined *in the current scope*
@@ -5003,27 +5039,29 @@ grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
         that took root before someone noticed the bug...  */
 
       tree type = declspecs->type;
+      bool type_ok = (TREE_CODE (type) == RECORD_TYPE
+                     || TREE_CODE (type) == UNION_TYPE);
+      bool ok = false;
 
-      if (type
-         && (TREE_CODE (type) == RECORD_TYPE
-             || TREE_CODE (type) == UNION_TYPE)
+      if (type_ok
          && (flag_ms_extensions || !declspecs->typedef_p))
        {
          if (flag_ms_extensions)
-           ; /* ok */
+           ok = true;
          else if (flag_iso)
-           goto warn_unnamed_field;
+           ok = false;
          else if (TYPE_NAME (type) == NULL)
-           ; /* ok */
+           ok = true;
          else
-           goto warn_unnamed_field;
+           ok = false;
        }
-      else
+      if (!ok)
        {
-       warn_unnamed_field:
-         warning ("declaration does not declare anything");
+         pedwarn ("declaration does not declare anything");
          return NULL_TREE;
        }
+      if (pedantic)
+       pedwarn ("ISO C doesn%'t support unnamed structs/unions");
     }
 
   value = grokdeclarator (declarator, declspecs, FIELD, false,
@@ -5117,9 +5155,22 @@ finish_struct (tree t, tree fieldlist, tree attributes)
          break;
 
       if (x == 0)
-       pedwarn ("%s has no %s",
-                TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
-                fieldlist ? _("named members") : _("members"));
+       {
+         if (TREE_CODE (t) == UNION_TYPE)
+           {
+             if (fieldlist)
+               pedwarn ("union has no named members");
+             else
+               pedwarn ("union has no members");
+           }
+         else
+           {
+             if (fieldlist)
+               pedwarn ("struct has no named members");
+             else
+               pedwarn ("struct has no members");
+           }
+       }
     }
 
   /* Install struct as DECL_CONTEXT of each field decl.
@@ -5323,6 +5374,12 @@ finish_struct (tree t, tree fieldlist, tree attributes)
   /* Finish debugging output for this type.  */
   rest_of_type_compilation (t, toplevel);
 
+  /* If we're inside a function proper, i.e. not file-scope and not still
+     parsing parameters, then arrange for the size of a variable sized type
+     to be bound now.  */
+  if (cur_stmt_list && variably_modified_type_p (t, NULL))
+    add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
+
   return t;
 }
 
@@ -5660,11 +5717,9 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
   DECL_INITIAL (decl1) = error_mark_node;
 
   /* If this definition isn't a prototype and we had a prototype declaration
-     before, copy the arg type info from that prototype.
-     But not if what we had before was a builtin function.  */
+     before, copy the arg type info from that prototype.  */
   old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
   if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
-      && !DECL_BUILT_IN (old_decl)
       && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
                    TREE_TYPE (TREE_TYPE (old_decl)))
       && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
@@ -5958,7 +6013,8 @@ store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
       if (TREE_CODE (parm) != PARM_DECL)
        continue;
 
-      if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
+      if (TREE_TYPE (parm) != error_mark_node
+         && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
        {
          error ("%Jparameter %qD has incomplete type", parm, parm);
          TREE_TYPE (parm) = error_mark_node;
@@ -6152,8 +6208,11 @@ store_parm_decls (void)
   DECL_SAVED_TREE (fndecl) = push_stmt_list ();
 
   /* ??? Insert the contents of the pending sizes list into the function
-     to be evaluated.  This just changes mis-behavior until assign_parms
-     phase ordering problems are resolved.  */
+     to be evaluated.  The only reason left to have this is
+       void foo(int n, int array[n++])
+     because we throw away the array type in favor of a pointer type, and
+     thus won't naturally see the SAVE_EXPR containing the increment.  All
+     other pending sizes would be handled by gimplify_parameters.  */
   {
     tree t;
     for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
@@ -6229,7 +6288,13 @@ finish_function (void)
       else
        {
          if (flag_isoc99)
-           c_finish_return (integer_zero_node);
+           {
+             tree stmt = c_finish_return (integer_zero_node);
+             /* Hack.  We don't want the middle-end to warn that this
+                return is unreachable, so put the statement on the
+                special line 0.  */
+             annotate_with_file_line (stmt, input_filename, 0);
+           }
        }
     }
 
@@ -6637,6 +6702,7 @@ build_null_declspecs (void)
   ret->storage_class = csc_none;
   ret->non_sc_seen_p = false;
   ret->typedef_p = false;
+  ret->tag_defined_p = false;
   ret->explicit_signed_p = false;
   ret->deprecated_p = false;
   ret->default_int_p = false;
@@ -6692,8 +6758,9 @@ declspecs_add_qual (struct c_declspecs *specs, tree qual)
    returning SPECS.  */
 
 struct c_declspecs *
-declspecs_add_type (struct c_declspecs *specs, tree type)
+declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
 {
+  tree type = spec.spec;
   specs->non_sc_seen_p = true;
   if (TREE_DEPRECATED (type))
     specs->deprecated_p = true;
@@ -6969,7 +7036,13 @@ declspecs_add_type (struct c_declspecs *specs, tree type)
        specs->type = TREE_TYPE (t);
     }
   else if (TREE_CODE (type) != ERROR_MARK)
-    specs->type = type;
+    {
+      if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
+       specs->tag_defined_p = true;
+      if (spec.kind == ctsk_typeof)
+       specs->typedef_p = true;
+      specs->type = type;
+    }
 
   return specs;
 }
@@ -7294,11 +7367,6 @@ c_write_global_declarations (void)
   /* We're done parsing; proceed to optimize and emit assembly.
      FIXME: shouldn't be the front end's responsibility to call this.  */
   cgraph_optimize ();
-
-  /* Presently this has to happen after cgraph_optimize.
-     FIXME: shouldn't be the front end's responsibility to call this.  */
-  if (flag_mudflap)
-    mudflap_finish_file ();
 }
 
 #include "gt-c-decl.h"