OSDN Git Service

Refine previous change.
[pf3gnuchains/gcc-fork.git] / gcc / c-parser.c
index 4eaf868..65966a9 100644 (file)
@@ -1,6 +1,6 @@
 /* Parser for C and Objective-C.
    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
    Parser actions based on the old Bison parser; structure somewhat
@@ -253,8 +253,7 @@ c_lex_one_token (c_parser *parser, c_token *token)
                /* We found an Objective-C "pq" keyword (in, out,
                   inout, bycopy, byref, oneway).  They need special
                   care because the interpretation depends on the
-                  context.
-                */
+                  context.  */
                if (parser->objc_pq_context)
                  {
                    token->type = CPP_KEYWORD;
@@ -275,21 +274,19 @@ c_lex_one_token (c_parser *parser, c_token *token)
                       you can't use 'in' as the name of the running
                       variable in a C for loop.  We could potentially
                       try to add code here to disambiguate, but it
-                      seems a reasonable limitation.
-                   */
+                      seems a reasonable limitation.  */
                    token->type = CPP_KEYWORD;
                    token->keyword = rid_code;
                    break;
                  }
                /* Else, "pq" keywords outside of the "pq" context are
                   not keywords, and we fall through to the code for
-                  normal tokens.
-               */
+                  normal tokens.  */
              }
            else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
              {
-               /* We found an Objective-C "property attribute" keyword 
-                  (readonly, copies, getter, setter, ivar). These are 
+               /* We found an Objective-C "property attribute"
+                  keyword (getter, setter, readonly, etc). These are
                   only valid in the property context.  */
                if (parser->objc_property_attr_context)
                  {
@@ -310,8 +307,7 @@ c_lex_one_token (c_parser *parser, c_token *token)
                   protected, public, try, catch, throw) without a
                   preceding '@' sign.  Do nothing and fall through to
                   the code for normal tokens (in C++ we would still
-                  consider the CXX ones keywords, but not in C).
-               */
+                  consider the CXX ones keywords, but not in C).  */
                ;
              }
            else
@@ -338,8 +334,7 @@ c_lex_one_token (c_parser *parser, c_token *token)
               variables and typedefs, and hence are shadowed by local
               declarations.  */
            if (objc_interface_decl
-               && (global_bindings_p ()
-                   || (!objc_force_identifier && !decl)))
+                && (!objc_force_identifier || global_bindings_p ()))
              {
                token->value = objc_interface_decl;
                token->id_kind = C_ID_CLASSNAME;
@@ -1049,13 +1044,15 @@ disable_extension_diagnostics (void)
             | (warn_traditional << 2)
             | (flag_iso << 3)
             | (warn_long_long << 4)
-            | (warn_cxx_compat << 5));
+            | (warn_cxx_compat << 5)
+            | (warn_overlength_strings << 6));
   cpp_opts->cpp_pedantic = pedantic = 0;
   warn_pointer_arith = 0;
   cpp_opts->cpp_warn_traditional = warn_traditional = 0;
   flag_iso = 0;
   cpp_opts->cpp_warn_long_long = warn_long_long = 0;
   warn_cxx_compat = 0;
+  warn_overlength_strings = 0;
   return ret;
 }
 
@@ -1071,6 +1068,7 @@ restore_extension_diagnostics (int flags)
   flag_iso = (flags >> 3) & 1;
   cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
   warn_cxx_compat = (flags >> 5) & 1;
+  warn_overlength_strings = (flags >> 6) & 1;
 }
 
 /* Possibly kinds of declarator to parse.  */
@@ -1112,7 +1110,8 @@ static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
                                                              bool,
                                                              struct c_declarator *);
 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
-static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
+static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
+                                                         tree);
 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
 static tree c_parser_simple_asm_expr (c_parser *);
 static tree c_parser_attributes (c_parser *);
@@ -1175,7 +1174,7 @@ static bool c_parser_objc_method_type (c_parser *);
 static void c_parser_objc_method_definition (c_parser *);
 static void c_parser_objc_methodprotolist (c_parser *);
 static void c_parser_objc_methodproto (c_parser *);
-static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
+static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
 static tree c_parser_objc_type_name (c_parser *);
 static tree c_parser_objc_protocol_refs (c_parser *);
 static void c_parser_objc_try_catch_finally_statement (c_parser *);
@@ -1555,8 +1554,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
        case RID_AT_PROPERTY:
          if (specs->attrs)
            {
-             c_parser_error (parser, 
-                             "attributes may not be specified before" );
+             c_parser_error (parser, "unexpected attribute");
              specs->attrs = NULL;
            }
          break;
@@ -1573,6 +1571,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
     {
       struct c_declarator *declarator;
       bool dummy = false;
+      timevar_id_t tv;
       tree fnbody;
       /* Declaring either one or more declarators (in which case we
         should diagnose if there were no declaration specifiers) or a
@@ -1702,6 +1701,13 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
            c_pop_function_context ();
          break;
        }
+
+      if (DECL_DECLARED_INLINE_P (current_function_decl))
+        tv = TV_PARSE_INLINE;
+      else
+        tv = TV_PARSE_FUNC;
+      timevar_push (tv);
+
       /* Parse old-style parameter declarations.  ??? Attributes are
         not allowed to start declaration specifiers here because of a
         syntax conflict between a function declaration with attribute
@@ -1740,6 +1746,8 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
          add_stmt (fnbody);
          finish_function ();
        }
+
+      timevar_pop (tv);
       break;
     }
 }
@@ -2192,11 +2200,14 @@ c_parser_enum_specifier (c_parser *parser)
     {
       /* Parse an enum definition.  */
       struct c_enum_contents the_enum;
-      tree type = start_enum (enum_loc, &the_enum, ident);
+      tree type;
       tree postfix_attrs;
       /* We chain the enumerators in reverse order, then put them in
         forward order at the end.  */
-      tree values = NULL_TREE;
+      tree values;
+      timevar_push (TV_PARSE_ENUM);
+      type = start_enum (enum_loc, &the_enum, ident);
+      values = NULL_TREE;
       c_parser_consume_token (parser);
       while (true)
        {
@@ -2260,6 +2271,7 @@ c_parser_enum_specifier (c_parser *parser)
       ret.kind = ctsk_tagdef;
       ret.expr = NULL_TREE;
       ret.expr_const_operands = true;
+      timevar_pop (TV_PARSE_ENUM);
       return ret;
     }
   else if (!ident)
@@ -2373,7 +2385,9 @@ c_parser_struct_or_union_specifier (c_parser *parser)
         semicolon separated fields than comma separated fields, and
         so we'll be minimizing the number of node traversals required
         by chainon.  */
-      tree contents = NULL_TREE;
+      tree contents;
+      timevar_push (TV_PARSE_STRUCT);
+      contents = NULL_TREE;
       c_parser_consume_token (parser);
       /* Handle the Objective-C @defs construct,
         e.g. foo(sizeof(struct{ @defs(ClassName) }));.  */
@@ -2460,6 +2474,7 @@ c_parser_struct_or_union_specifier (c_parser *parser)
       ret.kind = ctsk_tagdef;
       ret.expr = NULL_TREE;
       ret.expr_const_operands = true;
+      timevar_pop (TV_PARSE_STRUCT);
       return ret;
     }
   else if (!ident)
@@ -3087,7 +3102,8 @@ c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
     }
   else
     {
-      struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
+      struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
+                                                              NULL);
       pop_scope ();
       return ret;
     }
@@ -3095,12 +3111,15 @@ c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
 
 /* Parse a parameter list (possibly empty), including the closing
    parenthesis but not the opening one.  ATTRS are the attributes at
-   the start of the list.  */
+   the start of the list.  EXPR is NULL or an expression that needs to
+   be evaluated for the side effects of array size expressions in the
+   parameters.  */
 
 static struct c_arg_info *
-c_parser_parms_list_declarator (c_parser *parser, tree attrs)
+c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
 {
   bool bad_parm = false;
+
   /* ??? Following the old parser, forward parameter declarations may
      use abstract declarators, and if no real parameter declarations
      follow the forward declarations then this is not diagnosed.  Also
@@ -3144,31 +3163,27 @@ c_parser_parms_list_declarator (c_parser *parser, tree attrs)
       if (parm == NULL)
        bad_parm = true;
       else
-       push_parm_decl (parm);
+       push_parm_decl (parm, &expr);
       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
        {
          tree new_attrs;
          c_parser_consume_token (parser);
          mark_forward_parm_decls ();
          new_attrs = c_parser_attributes (parser);
-         return c_parser_parms_list_declarator (parser, new_attrs);
+         return c_parser_parms_list_declarator (parser, new_attrs, expr);
        }
       if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
        {
          c_parser_consume_token (parser);
          if (bad_parm)
-           {
-             get_pending_sizes ();
-             return NULL;
-           }
+           return NULL;
          else
-           return get_parm_info (false);
+           return get_parm_info (false, expr);
        }
       if (!c_parser_require (parser, CPP_COMMA,
                             "expected %<;%>, %<,%> or %<)%>"))
        {
          c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
-         get_pending_sizes ();
          return NULL;
        }
       if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
@@ -3178,18 +3193,14 @@ c_parser_parms_list_declarator (c_parser *parser, tree attrs)
            {
              c_parser_consume_token (parser);
              if (bad_parm)
-               {
-                 get_pending_sizes ();
-                 return NULL;
-               }
+               return NULL;
              else
-               return get_parm_info (true);
+               return get_parm_info (true, expr);
            }
          else
            {
              c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
                                         "expected %<)%>");
-             get_pending_sizes ();
              return NULL;
            }
        }
@@ -3266,6 +3277,8 @@ static tree
 c_parser_asm_string_literal (c_parser *parser)
 {
   tree str;
+  int save_flag = warn_overlength_strings;
+  warn_overlength_strings = 0;
   if (c_parser_next_token_is (parser, CPP_STRING))
     {
       str = c_parser_peek_token (parser)->value;
@@ -3283,6 +3296,7 @@ c_parser_asm_string_literal (c_parser *parser)
       c_parser_error (parser, "expected string literal");
       str = NULL_TREE;
     }
+  warn_overlength_strings = save_flag;
   return str;
 }
 
@@ -3788,7 +3802,7 @@ c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
                  c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
                                             "expected %<]%>");
                  mexpr.value
-                   = objc_build_message_expr (build_tree_list (rec, args));
+                   = objc_build_message_expr (rec, args);
                  mexpr.original_code = ERROR_MARK;
                  mexpr.original_type = NULL;
                  /* Now parse and process the remainder of the
@@ -4764,7 +4778,9 @@ c_parser_for_statement (c_parser *parser)
 {
   tree block, cond, incr, save_break, save_cont, body;
   /* The following are only used when parsing an ObjC foreach statement.  */
-  tree object_expression, collection_expression;
+  tree object_expression;
+  /* Silence the bogus uninitialized warning.  */
+  tree collection_expression = NULL;
   location_t loc = c_parser_peek_token (parser)->location;
   location_t for_loc = c_parser_peek_token (parser)->location;
   bool is_foreach_statement = false;
@@ -6110,7 +6126,7 @@ c_parser_postfix_expression (c_parser *parser)
          c_parser_consume_token (parser);
          brace_loc = c_parser_peek_token (parser)->location;
          c_parser_consume_token (parser);
-         if (cur_stmt_list == NULL)
+         if (!building_stmt_list_p ())
            {
              error_at (loc, "braced-group within expression allowed "
                        "only inside a function");
@@ -6453,8 +6469,7 @@ c_parser_postfix_expression (c_parser *parser)
          args = c_parser_objc_message_args (parser);
          c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
                                     "expected %<]%>");
-         expr.value = objc_build_message_expr (build_tree_list (receiver,
-                                                                args));
+         expr.value = objc_build_message_expr (receiver, args);
          break;
        }
       /* Else fall through to report error.  */
@@ -6930,7 +6945,7 @@ c_parser_objc_class_instance_variables (c_parser *parser)
       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
        {
          pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
-                  "extra semicolon in struct or union specified");
+                  "extra semicolon");
          c_parser_consume_token (parser);
          continue;
        }
@@ -6973,13 +6988,34 @@ c_parser_objc_class_instance_variables (c_parser *parser)
 
       /* Parse some comma-separated declarations.  */
       decls = c_parser_struct_declaration (parser);
-      {
-       /* Comma-separated instance variables are chained together in
-          reverse order; add them one by one.  */
-       tree ivar = nreverse (decls);
-       for (; ivar; ivar = DECL_CHAIN (ivar))
-         objc_add_instance_variable (copy_node (ivar));
-      }
+      if (decls == NULL)
+       {
+         /* There is a syntax error.  We want to skip the offending
+            tokens up to the next ';' (included) or '}'
+            (excluded).  */
+         
+         /* First, skip manually a ')' or ']'.  This is because they
+            reduce the nesting level, so c_parser_skip_until_found()
+            wouldn't be able to skip past them.  */
+         c_token *token = c_parser_peek_token (parser);
+         if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
+           c_parser_consume_token (parser);
+
+         /* Then, do the standard skipping.  */
+         c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
+
+         /* We hopefully recovered.  Start normal parsing again.  */
+         parser->error = false;
+         continue;
+       }
+      else
+       {
+         /* Comma-separated instance variables are chained together
+            in reverse order; add them one by one.  */
+         tree ivar = nreverse (decls);
+         for (; ivar; ivar = DECL_CHAIN (ivar))
+           objc_add_instance_variable (copy_node (ivar));
+       }
       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
     }
 }
@@ -6993,7 +7029,6 @@ c_parser_objc_class_instance_variables (c_parser *parser)
 static void
 c_parser_objc_class_declaration (c_parser *parser)
 {
-  tree list = NULL_TREE;
   gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
   c_parser_consume_token (parser);
   /* Any identifiers, including those declared as type names, are OK
@@ -7009,7 +7044,7 @@ c_parser_objc_class_declaration (c_parser *parser)
          return;
        }
       id = c_parser_peek_token (parser)->value;
-      list = chainon (list, build_tree_list (NULL_TREE, id));
+      objc_declare_class (id);
       c_parser_consume_token (parser);
       if (c_parser_next_token_is (parser, CPP_COMMA))
        c_parser_consume_token (parser);
@@ -7017,7 +7052,6 @@ c_parser_objc_class_declaration (c_parser *parser)
        break;
     }
   c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
-  objc_declare_class (list);
 }
 
 /* Parse an objc-alias-declaration.
@@ -7077,7 +7111,6 @@ c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
   if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
       || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
     {
-      tree list = NULL_TREE;
       /* Any identifiers, including those declared as type names, are
         OK here.  */
       while (true)
@@ -7089,7 +7122,7 @@ c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
              break;
            }
          id = c_parser_peek_token (parser)->value;
-         list = chainon (list, build_tree_list (NULL_TREE, id));
+         objc_declare_protocol (id, attributes);
          c_parser_consume_token (parser);
          if (c_parser_next_token_is (parser, CPP_COMMA))
            c_parser_consume_token (parser);
@@ -7097,7 +7130,6 @@ c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
            break;
        }
       c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
-      objc_declare_protocols (list, attributes);
     }
   else
     {
@@ -7150,9 +7182,10 @@ static void
 c_parser_objc_method_definition (c_parser *parser)
 {
   bool is_class_method = c_parser_objc_method_type (parser);
-  tree decl, attributes = NULL_TREE;
+  tree decl, attributes = NULL_TREE, expr = NULL_TREE;
   parser->objc_pq_context = true;
-  decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
+  decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
+                                   &expr);
   if (decl == error_mark_node)
     return;  /* Bail here. */
 
@@ -7170,7 +7203,7 @@ c_parser_objc_method_definition (c_parser *parser)
     }
 
   parser->objc_pq_context = false;
-  if (objc_start_method_definition (is_class_method, decl, attributes))
+  if (objc_start_method_definition (is_class_method, decl, attributes, expr))
     {
       add_stmt (c_parser_compound_statement (parser));
       objc_finish_method_definition (current_function_decl);
@@ -7261,7 +7294,8 @@ c_parser_objc_methodproto (c_parser *parser)
 
   /* Remember protocol qualifiers in prototypes.  */
   parser->objc_pq_context = true;
-  decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
+  decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
+                                   NULL);
   /* Forget protocol qualifiers now.  */
   parser->objc_pq_context = false;
 
@@ -7347,7 +7381,8 @@ c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
 */
 
 static tree
-c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
+c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
+                          tree *attributes, tree *expr)
 {
   tree type = NULL_TREE;
   tree sel;
@@ -7422,7 +7457,7 @@ c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attribu
          if (parm == NULL)
            break;
          parms = chainon (parms,
-                          build_tree_list (NULL_TREE, grokparm (parm)));
+                          build_tree_list (NULL_TREE, grokparm (parm, expr)));
        }
       sel = list;
     }
@@ -7482,6 +7517,14 @@ c_parser_objc_type_name (c_parser *parser)
     type_name = c_parser_type_name (parser);
   if (type_name)
     type = groktypename (type_name, NULL, NULL);
+
+  /* If the type is unknown, and error has already been produced and
+     we need to recover from the error.  In that case, use NULL_TREE
+     for the type, as if no type had been specified; this will use the
+     default type ('id') which is good for error recovery.  */
+  if (type == error_mark_node)
+    type = NULL_TREE;
+
   return build_tree_list (quals, type);
 }
 
@@ -7578,7 +7621,7 @@ c_parser_objc_try_catch_finally_statement (c_parser *parser)
          if (parm == NULL)
            parameter_declaration = error_mark_node;
          else
-           parameter_declaration = grokparm (parm);
+           parameter_declaration = grokparm (parm, NULL);
        }
       if (seen_open_paren)
        c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
@@ -7954,8 +7997,12 @@ c_parser_objc_at_property_declaration (c_parser *parser)
            case RID_SETTER:
              if (c_parser_next_token_is_not (parser, CPP_EQ))
                {
-                 c_parser_error (parser,
-                                 "getter/setter attribute must be followed by %<=%>");
+                 if (keyword == RID_GETTER)
+                   c_parser_error (parser,
+                                   "missing %<=%> (after %<getter%> attribute)");
+                 else
+                   c_parser_error (parser,
+                                   "missing %<=%> (after %<setter%> attribute)");
                  syntax_error = true;
                  break;
                }
@@ -9104,6 +9151,9 @@ c_parser_omp_atomic (location_t loc, c_parser *parser)
          goto saw_error;
        }
 
+      /* Arrange to pass the location of the assignment operator to
+        c_finish_omp_atomic.  */
+      loc = c_parser_peek_token (parser)->location;
       c_parser_consume_token (parser);
       {
        location_t rhs_loc = c_parser_peek_token (parser)->location;