OSDN Git Service

cp:
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
index 51b8f10..dc650d4 100644 (file)
@@ -1702,7 +1702,7 @@ static bool cp_parser_parse_definitely
   (cp_parser *);
 static inline bool cp_parser_parsing_tentatively
   (cp_parser *);
-static bool cp_parser_committed_to_tentative_parse
+static bool cp_parser_uncommitted_to_tentative_parse_p
   (cp_parser *);
 static void cp_parser_error
   (cp_parser *, const char *);
@@ -1838,8 +1838,7 @@ cp_parser_name_lookup_error (cp_parser* parser,
 static bool
 cp_parser_simulate_error (cp_parser* parser)
 {
-  if (cp_parser_parsing_tentatively (parser)
-      && !cp_parser_committed_to_tentative_parse (parser))
+  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
     {
       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
       return true;
@@ -1907,8 +1906,7 @@ cp_parser_check_for_invalid_template_id (cp_parser* parser,
       else
        error ("invalid template-id");
       /* Remember the location of the invalid "<".  */
-      if (cp_parser_parsing_tentatively (parser)
-         && !cp_parser_committed_to_tentative_parse (parser))
+      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
        start = cp_lexer_token_position (parser->lexer, true);
       /* Consume the "<".  */
       cp_lexer_consume_token (parser->lexer);
@@ -1941,9 +1939,8 @@ cp_parser_non_integral_constant_expression (cp_parser  *parser,
   return false;
 }
 
-/* Emit a diagnostic for an invalid type name. Consider also if it is
-   qualified or not and the result of a lookup, to provide a better
-   message.  */
+/* Emit a diagnostic for an invalid type name.  SCOPE is the
+   qualifying scope (or NULL, if none) for ID.  */
 
 static void
 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
@@ -2076,8 +2073,8 @@ cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
   unsigned brace_depth = 0;
   int result;
 
-  if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
-      && !cp_parser_committed_to_tentative_parse (parser))
+  if (recovering && !or_comma
+      && cp_parser_uncommitted_to_tentative_parse_p (parser))
     return 0;
 
   while (true)
@@ -3324,8 +3321,7 @@ cp_parser_nested_name_specifier_opt (cp_parser *parser,
     }
 
   /* Remember where the nested-name-specifier starts.  */
-  if (cp_parser_parsing_tentatively (parser)
-      && !cp_parser_committed_to_tentative_parse (parser))
+  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
     start = cp_lexer_token_position (parser->lexer, false);
 
   push_deferring_access_checks (dk_deferred);
@@ -4901,15 +4897,7 @@ cp_parser_new_type_id (cp_parser* parser, tree *nelts)
       *nelts = declarator->u.array.bounds;
       if (*nelts == error_mark_node)
        *nelts = integer_one_node;
-      else if (!processing_template_decl)
-       {
-         if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
-                                          false))
-           pedwarn ("size in array new must have integral type");
-         *nelts = save_expr (cp_convert (sizetype, *nelts));
-         if (*nelts == integer_zero_node)
-           warning ("zero size array reserves no space");
-       }
+      
       if (outer_declarator)
        outer_declarator->declarator = declarator->declarator;
       else
@@ -6954,8 +6942,7 @@ cp_parser_simple_declaration (cp_parser* parser,
          /* If we have already issued an error message we don't need
             to issue another one.  */
          if (decl != error_mark_node
-             || (cp_parser_parsing_tentatively (parser)
-                 && !cp_parser_committed_to_tentative_parse (parser)))
+             || cp_parser_uncommitted_to_tentative_parse_p (parser))
            cp_parser_error (parser, "expected %<,%> or %<;%>");
          /* Skip tokens until we reach the end of the statement.  */
          cp_parser_skip_to_end_of_statement (parser);
@@ -8322,8 +8309,7 @@ cp_parser_template_id (cp_parser *parser,
     }
 
   /* Remember where the template-id starts.  */
-  if (cp_parser_parsing_tentatively (parser)
-      && !cp_parser_committed_to_tentative_parse (parser))
+  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
     start_of_id = cp_lexer_token_position (parser->lexer, false);
 
   push_deferring_access_checks (dk_deferred);
@@ -8424,8 +8410,9 @@ cp_parser_template_id (cp_parser *parser,
      should we re-parse the token stream, we will not have to repeat
      the effort required to do the parse, nor will we issue duplicate
      error messages about problems during instantiation of the
-     template.  */
-  if (start_of_id)
+     template.  Do so only if parsing succeeded, otherwise we may
+     silently accept template arguments with syntax errors.  */
+  if (start_of_id && !cp_parser_error_occurred (parser))
     {
       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
       
@@ -8551,14 +8538,9 @@ cp_parser_template_name (cp_parser* parser,
          error ("non-template %qD used as template", identifier);
          inform ("use %<%T::template %D%> to indicate that it is a template",
                  parser->scope, identifier);
-         /* If parsing tentatively, find the location of the "<"
-            token.  */
-         if (cp_parser_parsing_tentatively (parser)
-             && !cp_parser_committed_to_tentative_parse (parser))
-           {
-             cp_parser_simulate_error (parser);
-             start = cp_lexer_token_position (parser->lexer, true);
-           }
+         /* If parsing tentatively, find the location of the "<" token.  */
+         if (cp_parser_simulate_error (parser))
+           start = cp_lexer_token_position (parser->lexer, true);
          /* Parse the template arguments so that we can issue error
             messages about them.  */
          cp_lexer_consume_token (parser->lexer);
@@ -8821,6 +8803,7 @@ cp_parser_template_argument (cp_parser* parser)
       if (cp_parser_parse_definitely (parser))
        return argument;
     }
+
   /* If the next token is "&", the argument must be the address of an
      object or function with external linkage.  */
   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
@@ -8843,6 +8826,12 @@ cp_parser_template_argument (cp_parser* parser)
        cp_parser_abort_tentative_parse (parser);
       else
        {
+         if (TREE_CODE (argument) == INDIRECT_REF)
+           {
+             gcc_assert (REFERENCE_REF_P (argument));
+             argument = TREE_OPERAND (argument, 0);
+           }
+         
          if (qualifying_class)
            argument = finish_qualified_id_expr (qualifying_class,
                                                 argument,
@@ -8866,6 +8855,8 @@ cp_parser_template_argument (cp_parser* parser)
                       || TREE_CODE (argument) == SCOPE_REF))
            /* A pointer-to-member.  */
            ;
+         else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
+           ;
          else
            cp_parser_simulate_error (parser);
 
@@ -8884,6 +8875,7 @@ cp_parser_template_argument (cp_parser* parser)
       cp_parser_error (parser, "invalid non-type template argument");
       return error_mark_node;
     }
+
   /* If the argument wasn't successfully parsed as a type-id followed
      by '>>', the argument can only be a constant expression now.
      Otherwise, we try parsing the constant-expression tentatively,
@@ -9654,7 +9646,8 @@ cp_parser_elaborated_type_specifier (cp_parser* parser,
        }
 
       /* For a `typename', we needn't call xref_tag.  */
-      if (tag_type == typename_type)
+      if (tag_type == typename_type 
+         && TREE_CODE (parser->scope) != NAMESPACE_DECL)
        return cp_parser_make_typename_type (parser, parser->scope,
                                             identifier);
       /* Look up a qualified name in the usual way.  */
@@ -9662,9 +9655,6 @@ cp_parser_elaborated_type_specifier (cp_parser* parser,
        {
          tree decl;
 
-         /* In an elaborated-type-specifier, names are assumed to name
-            types, so we set IS_TYPE to TRUE when calling
-            cp_parser_lookup_name.  */
          decl = cp_parser_lookup_name (parser, identifier,
                                        tag_type,
                                        /*is_template=*/false,
@@ -9699,7 +9689,9 @@ cp_parser_elaborated_type_specifier (cp_parser* parser,
 
          if (TREE_CODE (decl) != TYPE_DECL)
            {
-             error ("expected type-name");
+             cp_parser_diagnose_invalid_type_name (parser, 
+                                                   parser->scope,
+                                                   identifier);
              return error_mark_node;
            }
 
@@ -11617,8 +11609,7 @@ cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
             list.  */
          if (!parser->in_template_argument_list_p
              && !parser->in_type_id_in_expr_p
-             && cp_parser_parsing_tentatively (parser)
-             && !cp_parser_committed_to_tentative_parse (parser)
+             && cp_parser_uncommitted_to_tentative_parse_p (parser)
              /* However, a parameter-declaration of the form
                 "foat(f)" (which is a valid declaration of a
                 parameter "f") can also be interpreted as an
@@ -11629,8 +11620,7 @@ cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
       else
        {
          cp_parser_error (parser, "expected %<,%> or %<...%>");
-         if (!cp_parser_parsing_tentatively (parser)
-             || cp_parser_committed_to_tentative_parse (parser))
+         if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
            cp_parser_skip_to_closing_parenthesis (parser,
                                                   /*recovering=*/true,
                                                   /*or_comma=*/false,
@@ -11733,8 +11723,7 @@ cp_parser_parameter_declaration (cp_parser *parser,
             function-type (taking a "char" as a parameter) or a cast
             of some object of type "char" to "int".  */
          && !parser->in_type_id_in_expr_p
-         && cp_parser_parsing_tentatively (parser)
-         && !cp_parser_committed_to_tentative_parse (parser)
+         && cp_parser_uncommitted_to_tentative_parse_p (parser)
          && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
        cp_parser_commit_to_tentative_parse (parser);
       /* Parse the declarator.  */
@@ -12443,6 +12432,8 @@ cp_parser_class_specifier (cp_parser* parser)
    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
    involving a nested-name-specifier was used, and FALSE otherwise.
 
+   Returns error_mark_node if this is not a class-head.
+   
    Returns NULL_TREE if the class-head is syntactically valid, but
    semantically invalid in a way that means we should skip the entire
    body of the class.  */
@@ -12615,6 +12606,15 @@ cp_parser_class_head (cp_parser* parser,
   else if (nested_name_specifier)
     {
       tree scope;
+
+      /* Reject typedef-names in class heads.  */
+      if (!DECL_IMPLICIT_TYPEDEF_P (type))
+       {
+         error ("invalid class name in declaration of %qD", type);
+         type = NULL_TREE;
+         goto done;
+       }
+
       /* Figure out in what scope the declaration is being placed.  */
       scope = current_scope ();
       /* If that scope does not contain the scope in which the
@@ -12714,7 +12714,15 @@ cp_parser_class_head (cp_parser* parser,
       type = TYPE_MAIN_DECL (TREE_TYPE (type));
       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
          && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
-       type = push_template_decl (type);
+       {
+         type = push_template_decl (type);
+         if (type == error_mark_node)
+           {
+             type = NULL_TREE;
+             goto done;
+           }
+       }
+      
       type = TREE_TYPE (type);
       if (nested_name_specifier)
        {
@@ -13131,7 +13139,7 @@ cp_parser_member_declaration (cp_parser* parser)
                initializer = NULL_TREE;
 
              /* See if we are probably looking at a function
-                definition.  We are certainly not looking at at a
+                definition.  We are certainly not looking at a
                 member-declarator.  Calling `grokfield' has
                 side-effects, so we must not do it unless we are sure
                 that we are looking at a member-declarator.  */
@@ -14182,10 +14190,6 @@ cp_parser_lookup_name (cp_parser *parser, tree name,
                 is dependent.  */
              type = make_typename_type (parser->scope, name, tag_type,
                                         /*complain=*/1);
-             if (tag_type == enum_type)
-               TYPENAME_IS_ENUM_P (type) = 1;
-             else if (tag_type != typename_type)
-               TYPENAME_IS_CLASS_P (type) = 1;
              decl = TYPE_NAME (type);
            }
          else if (is_template)
@@ -15825,14 +15829,14 @@ cp_parser_parse_definitely (cp_parser* parser)
   return !error_occurred;
 }
 
-/* Returns true if we are parsing tentatively -- but have decided that
-   we will stick with this tentative parse, even if errors occur.  */
+/* Returns true if we are parsing tentatively and are not committed to
+   this tentative parse.  */
 
 static bool
-cp_parser_committed_to_tentative_parse (cp_parser* parser)
+cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
 {
   return (cp_parser_parsing_tentatively (parser)
-         && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
+         && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
 }
 
 /* Returns nonzero iff an error has occurred during the most recent