OSDN Git Service

* config/mips/mips.c (mips_arg_info): Use FUNCTION_ARG_BOUNDARY to
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38
39 \f
40 /* The lexer.  */
41
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43    and c-lex.c) and the C++ parser.  */
44
45 /* A C++ token.  */
46
47 typedef struct cp_token GTY (())
48 {
49   /* The kind of token.  */
50   ENUM_BITFIELD (cpp_ttype) type : 8;
51   /* If this token is a keyword, this value indicates which keyword.
52      Otherwise, this value is RID_MAX.  */
53   ENUM_BITFIELD (rid) keyword : 8;
54   /* Token flags.  */
55   unsigned char flags;
56   /* True if this token is from a system header.  */
57   BOOL_BITFIELD in_system_header : 1;
58   /* True if this token is from a context where it is implicitly extern "C" */
59   BOOL_BITFIELD implicit_extern_c : 1;
60   /* The value associated with this token, if any.  */
61   tree value;
62   /* The location at which this token was found.  */
63   location_t location;
64 } cp_token;
65
66 /* We use a stack of token pointer for saving token sets.  */
67 typedef struct cp_token *cp_token_position;
68 DEF_VEC_MALLOC_P (cp_token_position);
69
70 static const cp_token eof_token =
71 {
72   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
73 #if USE_MAPPED_LOCATION
74   0
75 #else
76   {0, 0}
77 #endif
78 };
79
80 /* The cp_lexer structure represents the C++ lexer.  It is responsible
81    for managing the token stream from the preprocessor and supplying
82    it to the parser.  Tokens are never added to the cp_lexer after
83    it is created.  */
84
85 typedef struct cp_lexer GTY (())
86 {
87   /* The memory allocated for the buffer.  NULL if this lexer does not
88      own the token buffer.  */
89   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
90   /* If the lexer owns the buffer, this is the number of tokens in the
91      buffer.  */
92   size_t buffer_length;
93   
94   /* A pointer just past the last available token.  The tokens
95      in this lexer are [buffer, last_token).  */
96   cp_token_position GTY ((skip)) last_token;
97
98   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
99      no more available tokens.  */
100   cp_token_position GTY ((skip)) next_token;
101
102   /* A stack indicating positions at which cp_lexer_save_tokens was
103      called.  The top entry is the most recent position at which we
104      began saving tokens.  If the stack is non-empty, we are saving
105      tokens.  */
106   VEC (cp_token_position) *GTY ((skip)) saved_tokens;
107
108   /* True if we should output debugging information.  */
109   bool debugging_p;
110
111   /* The next lexer in a linked list of lexers.  */
112   struct cp_lexer *next;
113 } cp_lexer;
114
115 /* cp_token_cache is a range of tokens.  There is no need to represent
116    allocate heap memory for it, since tokens are never removed from the
117    lexer's array.  There is also no need for the GC to walk through
118    a cp_token_cache, since everything in here is referenced through
119    a lexer.  */
120
121 typedef struct cp_token_cache GTY(())
122 {
123   /* The beginning of the token range.  */
124   cp_token * GTY((skip)) first;
125
126   /* Points immediately after the last token in the range.  */
127   cp_token * GTY ((skip)) last;
128 } cp_token_cache;
129
130 /* Prototypes.  */
131
132 static cp_lexer *cp_lexer_new_main
133   (void);
134 static cp_lexer *cp_lexer_new_from_tokens
135   (cp_token_cache *tokens);
136 static void cp_lexer_destroy
137   (cp_lexer *);
138 static int cp_lexer_saving_tokens
139   (const cp_lexer *);
140 static cp_token_position cp_lexer_token_position
141   (cp_lexer *, bool);
142 static cp_token *cp_lexer_token_at
143   (cp_lexer *, cp_token_position);
144 static void cp_lexer_get_preprocessor_token
145   (cp_lexer *, cp_token *);
146 static inline cp_token *cp_lexer_peek_token
147   (cp_lexer *);
148 static cp_token *cp_lexer_peek_nth_token
149   (cp_lexer *, size_t);
150 static inline bool cp_lexer_next_token_is
151   (cp_lexer *, enum cpp_ttype);
152 static bool cp_lexer_next_token_is_not
153   (cp_lexer *, enum cpp_ttype);
154 static bool cp_lexer_next_token_is_keyword
155   (cp_lexer *, enum rid);
156 static cp_token *cp_lexer_consume_token
157   (cp_lexer *);
158 static void cp_lexer_purge_token
159   (cp_lexer *);
160 static void cp_lexer_purge_tokens_after
161   (cp_lexer *, cp_token_position);
162 static void cp_lexer_handle_pragma
163   (cp_lexer *);
164 static void cp_lexer_save_tokens
165   (cp_lexer *);
166 static void cp_lexer_commit_tokens
167   (cp_lexer *);
168 static void cp_lexer_rollback_tokens
169   (cp_lexer *);
170 #ifdef ENABLE_CHECKING
171 static void cp_lexer_print_token
172   (FILE *, cp_token *);
173 static inline bool cp_lexer_debugging_p
174   (cp_lexer *);
175 static void cp_lexer_start_debugging
176   (cp_lexer *) ATTRIBUTE_UNUSED;
177 static void cp_lexer_stop_debugging
178   (cp_lexer *) ATTRIBUTE_UNUSED;
179 #else
180 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
181    about passing NULL to functions that require non-NULL arguments
182    (fputs, fprintf).  It will never be used, so all we need is a value
183    of the right type that's guaranteed not to be NULL.  */
184 #define cp_lexer_debug_stream stdout
185 #define cp_lexer_print_token(str, tok) (void) 0
186 #define cp_lexer_debugging_p(lexer) 0
187 #endif /* ENABLE_CHECKING */
188
189 static cp_token_cache *cp_token_cache_new
190   (cp_token *, cp_token *);
191
192 /* Manifest constants.  */
193 #define CP_LEXER_BUFFER_SIZE 10000
194 #define CP_SAVED_TOKEN_STACK 5
195
196 /* A token type for keywords, as opposed to ordinary identifiers.  */
197 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
198
199 /* A token type for template-ids.  If a template-id is processed while
200    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
201    the value of the CPP_TEMPLATE_ID is whatever was returned by
202    cp_parser_template_id.  */
203 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
204
205 /* A token type for nested-name-specifiers.  If a
206    nested-name-specifier is processed while parsing tentatively, it is
207    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
208    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
209    cp_parser_nested_name_specifier_opt.  */
210 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
211
212 /* A token type for tokens that are not tokens at all; these are used
213    to represent slots in the array where there used to be a token
214    that has now been deleted.  */
215 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
216
217 /* The number of token types, including C++-specific ones.  */
218 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
219
220 /* Variables.  */
221
222 #ifdef ENABLE_CHECKING
223 /* The stream to which debugging output should be written.  */
224 static FILE *cp_lexer_debug_stream;
225 #endif /* ENABLE_CHECKING */
226
227 /* Create a new main C++ lexer, the lexer that gets tokens from the
228    preprocessor.  */
229
230 static cp_lexer *
231 cp_lexer_new_main (void)
232 {
233   cp_token first_token;
234   cp_lexer *lexer;
235   cp_token *pos;
236   size_t alloc;
237   size_t space;
238   cp_token *buffer;
239
240   /* Tell cpplib we want CPP_PRAGMA tokens.  */
241   cpp_get_options (parse_in)->defer_pragmas = true;
242
243   /* Tell c_lex not to merge string constants.  */
244   c_lex_return_raw_strings = true;
245
246   /* It's possible that lexing the first token will load a PCH file,
247      which is a GC collection point.  So we have to grab the first
248      token before allocating any memory.  */
249   cp_lexer_get_preprocessor_token (NULL, &first_token);
250   c_common_no_more_pch ();
251
252   /* Allocate the memory.  */
253   lexer = GGC_CNEW (cp_lexer);
254
255 #ifdef ENABLE_CHECKING  
256   /* Initially we are not debugging.  */
257   lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
259   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
260          
261   /* Create the buffer.  */
262   alloc = CP_LEXER_BUFFER_SIZE;
263   buffer = ggc_alloc (alloc * sizeof (cp_token));
264
265   /* Put the first token in the buffer.  */
266   space = alloc;
267   pos = buffer;
268   *pos = first_token;
269   
270   /* Get the remaining tokens from the preprocessor.  */
271   while (pos->type != CPP_EOF)
272     {
273       pos++;
274       if (!--space)
275         {
276           space = alloc;
277           alloc *= 2;
278           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
279           pos = buffer + space;
280         }
281       cp_lexer_get_preprocessor_token (lexer, pos);
282     }
283   lexer->buffer = buffer;
284   lexer->buffer_length = alloc - space;
285   lexer->last_token = pos;
286   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
287
288   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
289      direct calls to c_lex.  Those callers all expect c_lex to do
290      string constant concatenation.  */
291   c_lex_return_raw_strings = false;
292
293   gcc_assert (lexer->next_token->type != CPP_PURGED);
294   return lexer;
295 }
296
297 /* Create a new lexer whose token stream is primed with the tokens in
298    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
299
300 static cp_lexer *
301 cp_lexer_new_from_tokens (cp_token_cache *cache)
302 {
303   cp_token *first = cache->first;
304   cp_token *last = cache->last;
305   cp_lexer *lexer = GGC_CNEW (cp_lexer);
306
307   /* We do not own the buffer.  */
308   lexer->buffer = NULL;
309   lexer->buffer_length = 0;
310   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
311   lexer->last_token = last;
312   
313   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
314
315 #ifdef ENABLE_CHECKING
316   /* Initially we are not debugging.  */
317   lexer->debugging_p = false;
318 #endif
319
320   gcc_assert (lexer->next_token->type != CPP_PURGED);
321   return lexer;
322 }
323
324 /* Frees all resources associated with LEXER.  */
325
326 static void
327 cp_lexer_destroy (cp_lexer *lexer)
328 {
329   if (lexer->buffer)
330     ggc_free (lexer->buffer);
331   VEC_free (cp_token_position, lexer->saved_tokens);
332   ggc_free (lexer);
333 }
334
335 /* Returns nonzero if debugging information should be output.  */
336
337 #ifdef ENABLE_CHECKING
338
339 static inline bool
340 cp_lexer_debugging_p (cp_lexer *lexer)
341 {
342   return lexer->debugging_p;
343 }
344
345 #endif /* ENABLE_CHECKING */
346
347 static inline cp_token_position
348 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
349 {
350   gcc_assert (!previous_p || lexer->next_token != &eof_token);
351   
352   return lexer->next_token - previous_p;
353 }
354
355 static inline cp_token *
356 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
357 {
358   return pos;
359 }
360
361 /* nonzero if we are presently saving tokens.  */
362
363 static inline int
364 cp_lexer_saving_tokens (const cp_lexer* lexer)
365 {
366   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
367 }
368
369 /* Store the next token from the preprocessor in *TOKEN.  Return true
370    if we reach EOF.  */
371
372 static void
373 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
374                                  cp_token *token)
375 {
376   static int is_extern_c = 0;
377
378    /* Get a new token from the preprocessor.  */
379   token->type = c_lex_with_flags (&token->value, &token->flags);
380   token->location = input_location;
381   token->in_system_header = in_system_header;
382
383   /* On some systems, some header files are surrounded by an 
384      implicit extern "C" block.  Set a flag in the token if it
385      comes from such a header.  */
386   is_extern_c += pending_lang_change;
387   pending_lang_change = 0;
388   token->implicit_extern_c = is_extern_c > 0;
389
390   /* Check to see if this token is a keyword.  */
391   if (token->type == CPP_NAME
392       && C_IS_RESERVED_WORD (token->value))
393     {
394       /* Mark this token as a keyword.  */
395       token->type = CPP_KEYWORD;
396       /* Record which keyword.  */
397       token->keyword = C_RID_CODE (token->value);
398       /* Update the value.  Some keywords are mapped to particular
399          entities, rather than simply having the value of the
400          corresponding IDENTIFIER_NODE.  For example, `__const' is
401          mapped to `const'.  */
402       token->value = ridpointers[token->keyword];
403     }
404   else
405     token->keyword = RID_MAX;
406 }
407
408 /* Update the globals input_location and in_system_header from TOKEN.  */
409 static inline void
410 cp_lexer_set_source_position_from_token (cp_token *token)
411 {
412   if (token->type != CPP_EOF)
413     {
414       input_location = token->location;
415       in_system_header = token->in_system_header;
416     }
417 }
418
419 /* Return a pointer to the next token in the token stream, but do not
420    consume it.  */
421
422 static inline cp_token *
423 cp_lexer_peek_token (cp_lexer *lexer)
424 {
425   if (cp_lexer_debugging_p (lexer))
426     {
427       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
428       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
429       putc ('\n', cp_lexer_debug_stream);
430     }
431   return lexer->next_token;
432 }
433
434 /* Return true if the next token has the indicated TYPE.  */
435
436 static inline bool
437 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
438 {
439   return cp_lexer_peek_token (lexer)->type == type;
440 }
441
442 /* Return true if the next token does not have the indicated TYPE.  */
443
444 static inline bool
445 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
446 {
447   return !cp_lexer_next_token_is (lexer, type);
448 }
449
450 /* Return true if the next token is the indicated KEYWORD.  */
451
452 static inline bool
453 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
454 {
455   cp_token *token;
456
457   /* Peek at the next token.  */
458   token = cp_lexer_peek_token (lexer);
459   /* Check to see if it is the indicated keyword.  */
460   return token->keyword == keyword;
461 }
462
463 /* Return a pointer to the Nth token in the token stream.  If N is 1,
464    then this is precisely equivalent to cp_lexer_peek_token (except
465    that it is not inline).  One would like to disallow that case, but
466    there is one case (cp_parser_nth_token_starts_template_id) where
467    the caller passes a variable for N and it might be 1.  */
468
469 static cp_token *
470 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
471 {
472   cp_token *token;
473
474   /* N is 1-based, not zero-based.  */
475   gcc_assert (n > 0 && lexer->next_token != &eof_token);
476
477   if (cp_lexer_debugging_p (lexer))
478     fprintf (cp_lexer_debug_stream,
479              "cp_lexer: peeking ahead %ld at token: ", (long)n);
480
481   --n;
482   token = lexer->next_token;
483   while (n != 0)
484     {
485       ++token;
486       if (token == lexer->last_token)
487         {
488           token = (cp_token *)&eof_token;
489           break;
490         }
491       
492       if (token->type != CPP_PURGED)
493         --n;
494     }
495
496   if (cp_lexer_debugging_p (lexer))
497     {
498       cp_lexer_print_token (cp_lexer_debug_stream, token);
499       putc ('\n', cp_lexer_debug_stream);
500     }
501
502   return token;
503 }
504
505 /* Return the next token, and advance the lexer's next_token pointer
506    to point to the next non-purged token.  */
507
508 static cp_token *
509 cp_lexer_consume_token (cp_lexer* lexer)
510 {
511   cp_token *token = lexer->next_token;
512
513   gcc_assert (token != &eof_token);
514   
515   do
516     {
517       lexer->next_token++;
518       if (lexer->next_token == lexer->last_token)
519         {
520           lexer->next_token = (cp_token *)&eof_token;
521           break;
522         }
523       
524     }
525   while (lexer->next_token->type == CPP_PURGED);
526   
527   cp_lexer_set_source_position_from_token (token);
528   
529   /* Provide debugging output.  */
530   if (cp_lexer_debugging_p (lexer))
531     {
532       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
533       cp_lexer_print_token (cp_lexer_debug_stream, token);
534       putc ('\n', cp_lexer_debug_stream);
535     }
536   
537   return token;
538 }
539
540 /* Permanently remove the next token from the token stream, and
541    advance the next_token pointer to refer to the next non-purged
542    token.  */
543
544 static void
545 cp_lexer_purge_token (cp_lexer *lexer)
546 {
547   cp_token *tok = lexer->next_token;
548   
549   gcc_assert (tok != &eof_token);
550   tok->type = CPP_PURGED;
551   tok->location = UNKNOWN_LOCATION;
552   tok->value = NULL_TREE;
553   tok->keyword = RID_MAX;
554
555   do
556     {
557       tok++;
558       if (tok == lexer->last_token)
559         {
560           tok = (cp_token *)&eof_token;
561           break;
562         }
563     }
564   while (tok->type == CPP_PURGED);
565   lexer->next_token = tok;
566 }
567
568 /* Permanently remove all tokens after TOK, up to, but not
569    including, the token that will be returned next by
570    cp_lexer_peek_token.  */
571
572 static void
573 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
574 {
575   cp_token *peek = lexer->next_token;
576
577   if (peek == &eof_token)
578     peek = lexer->last_token;
579   
580   gcc_assert (tok < peek);
581
582   for ( tok += 1; tok != peek; tok += 1)
583     {
584       tok->type = CPP_PURGED;
585       tok->location = UNKNOWN_LOCATION;
586       tok->value = NULL_TREE;
587       tok->keyword = RID_MAX;
588     }
589 }
590
591 /* Consume and handle a pragma token.  */
592 static void
593 cp_lexer_handle_pragma (cp_lexer *lexer)
594 {
595   cpp_string s;
596   cp_token *token = cp_lexer_consume_token (lexer);
597   gcc_assert (token->type == CPP_PRAGMA);
598   gcc_assert (token->value);
599
600   s.len = TREE_STRING_LENGTH (token->value);
601   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
602
603   cpp_handle_deferred_pragma (parse_in, &s);
604
605   /* Clearing token->value here means that we will get an ICE if we
606      try to process this #pragma again (which should be impossible).  */
607   token->value = NULL;
608 }
609
610 /* Begin saving tokens.  All tokens consumed after this point will be
611    preserved.  */
612
613 static void
614 cp_lexer_save_tokens (cp_lexer* lexer)
615 {
616   /* Provide debugging output.  */
617   if (cp_lexer_debugging_p (lexer))
618     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
619
620   VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
621 }
622
623 /* Commit to the portion of the token stream most recently saved.  */
624
625 static void
626 cp_lexer_commit_tokens (cp_lexer* lexer)
627 {
628   /* Provide debugging output.  */
629   if (cp_lexer_debugging_p (lexer))
630     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
631
632   VEC_pop (cp_token_position, lexer->saved_tokens);
633 }
634
635 /* Return all tokens saved since the last call to cp_lexer_save_tokens
636    to the token stream.  Stop saving tokens.  */
637
638 static void
639 cp_lexer_rollback_tokens (cp_lexer* lexer)
640 {
641   /* Provide debugging output.  */
642   if (cp_lexer_debugging_p (lexer))
643     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
644
645   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
646 }
647
648 /* Print a representation of the TOKEN on the STREAM.  */
649
650 #ifdef ENABLE_CHECKING
651
652 static void
653 cp_lexer_print_token (FILE * stream, cp_token *token)
654 {
655   /* We don't use cpp_type2name here because the parser defines
656      a few tokens of its own.  */
657   static const char *const token_names[] = {
658     /* cpplib-defined token types */
659 #define OP(e, s) #e,
660 #define TK(e, s) #e,
661     TTYPE_TABLE
662 #undef OP
663 #undef TK
664     /* C++ parser token types - see "Manifest constants", above.  */
665     "KEYWORD",
666     "TEMPLATE_ID",
667     "NESTED_NAME_SPECIFIER",
668     "PURGED"
669   };
670   
671   /* If we have a name for the token, print it out.  Otherwise, we
672      simply give the numeric code.  */
673   gcc_assert (token->type < ARRAY_SIZE(token_names));
674   fputs (token_names[token->type], stream);
675
676   /* For some tokens, print the associated data.  */
677   switch (token->type)
678     {
679     case CPP_KEYWORD:
680       /* Some keywords have a value that is not an IDENTIFIER_NODE.
681          For example, `struct' is mapped to an INTEGER_CST.  */
682       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
683         break;
684       /* else fall through */
685     case CPP_NAME:
686       fputs (IDENTIFIER_POINTER (token->value), stream);
687       break;
688
689     case CPP_STRING:
690     case CPP_WSTRING:
691     case CPP_PRAGMA:
692       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
693       break;
694
695     default:
696       break;
697     }
698 }
699
700 /* Start emitting debugging information.  */
701
702 static void
703 cp_lexer_start_debugging (cp_lexer* lexer)
704 {
705   ++lexer->debugging_p;
706 }
707
708 /* Stop emitting debugging information.  */
709
710 static void
711 cp_lexer_stop_debugging (cp_lexer* lexer)
712 {
713   --lexer->debugging_p;
714 }
715
716 #endif /* ENABLE_CHECKING */
717
718 /* Create a new cp_token_cache, representing a range of tokens.  */
719
720 static cp_token_cache *
721 cp_token_cache_new (cp_token *first, cp_token *last)
722 {
723   cp_token_cache *cache = GGC_NEW (cp_token_cache);
724   cache->first = first;
725   cache->last = last;
726   return cache;
727 }
728
729 \f
730 /* Decl-specifiers.  */
731
732 static void clear_decl_specs
733   (cp_decl_specifier_seq *);
734
735 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
736
737 static void
738 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
739 {
740   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
741 }
742
743 /* Declarators.  */
744
745 /* Nothing other than the parser should be creating declarators;
746    declarators are a semi-syntactic representation of C++ entities.
747    Other parts of the front end that need to create entities (like
748    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
749
750 static cp_declarator *make_id_declarator
751   (tree);
752 static cp_declarator *make_call_declarator
753   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
754 static cp_declarator *make_array_declarator
755   (cp_declarator *, tree);
756 static cp_declarator *make_pointer_declarator
757   (cp_cv_quals, cp_declarator *);
758 static cp_declarator *make_reference_declarator
759   (cp_cv_quals, cp_declarator *);
760 static cp_parameter_declarator *make_parameter_declarator
761   (cp_decl_specifier_seq *, cp_declarator *, tree);
762 static cp_declarator *make_ptrmem_declarator
763   (cp_cv_quals, tree, cp_declarator *);
764
765 cp_declarator *cp_error_declarator;
766
767 /* The obstack on which declarators and related data structures are
768    allocated.  */
769 static struct obstack declarator_obstack;
770
771 /* Alloc BYTES from the declarator memory pool.  */
772
773 static inline void *
774 alloc_declarator (size_t bytes)
775 {
776   return obstack_alloc (&declarator_obstack, bytes);
777 }
778
779 /* Allocate a declarator of the indicated KIND.  Clear fields that are
780    common to all declarators.  */
781
782 static cp_declarator *
783 make_declarator (cp_declarator_kind kind)
784 {
785   cp_declarator *declarator;
786
787   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
788   declarator->kind = kind;
789   declarator->attributes = NULL_TREE;
790   declarator->declarator = NULL;
791
792   return declarator;
793 }
794
795 /* Make a declarator for a generalized identifier.  */
796
797 cp_declarator *
798 make_id_declarator (tree id)
799 {
800   cp_declarator *declarator;
801
802   declarator = make_declarator (cdk_id);
803   declarator->u.id.name = id;
804   declarator->u.id.sfk = sfk_none;
805
806   return declarator;
807 }
808
809 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
810    of modifiers such as const or volatile to apply to the pointer
811    type, represented as identifiers.  */
812
813 cp_declarator *
814 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
815 {
816   cp_declarator *declarator;
817
818   declarator = make_declarator (cdk_pointer);
819   declarator->declarator = target;
820   declarator->u.pointer.qualifiers = cv_qualifiers;
821   declarator->u.pointer.class_type = NULL_TREE;
822
823   return declarator;
824 }
825
826 /* Like make_pointer_declarator -- but for references.  */
827
828 cp_declarator *
829 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
830 {
831   cp_declarator *declarator;
832
833   declarator = make_declarator (cdk_reference);
834   declarator->declarator = target;
835   declarator->u.pointer.qualifiers = cv_qualifiers;
836   declarator->u.pointer.class_type = NULL_TREE;
837
838   return declarator;
839 }
840
841 /* Like make_pointer_declarator -- but for a pointer to a non-static
842    member of CLASS_TYPE.  */
843
844 cp_declarator *
845 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
846                         cp_declarator *pointee)
847 {
848   cp_declarator *declarator;
849
850   declarator = make_declarator (cdk_ptrmem);
851   declarator->declarator = pointee;
852   declarator->u.pointer.qualifiers = cv_qualifiers;
853   declarator->u.pointer.class_type = class_type;
854
855   return declarator;
856 }
857
858 /* Make a declarator for the function given by TARGET, with the
859    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
860    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
861    indicates what exceptions can be thrown.  */
862
863 cp_declarator *
864 make_call_declarator (cp_declarator *target,
865                       cp_parameter_declarator *parms,
866                       cp_cv_quals cv_qualifiers,
867                       tree exception_specification)
868 {
869   cp_declarator *declarator;
870
871   declarator = make_declarator (cdk_function);
872   declarator->declarator = target;
873   declarator->u.function.parameters = parms;
874   declarator->u.function.qualifiers = cv_qualifiers;
875   declarator->u.function.exception_specification = exception_specification;
876
877   return declarator;
878 }
879
880 /* Make a declarator for an array of BOUNDS elements, each of which is
881    defined by ELEMENT.  */
882
883 cp_declarator *
884 make_array_declarator (cp_declarator *element, tree bounds)
885 {
886   cp_declarator *declarator;
887
888   declarator = make_declarator (cdk_array);
889   declarator->declarator = element;
890   declarator->u.array.bounds = bounds;
891
892   return declarator;
893 }
894
895 cp_parameter_declarator *no_parameters;
896
897 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
898    DECLARATOR and DEFAULT_ARGUMENT.  */
899
900 cp_parameter_declarator *
901 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
902                            cp_declarator *declarator,
903                            tree default_argument)
904 {
905   cp_parameter_declarator *parameter;
906
907   parameter = ((cp_parameter_declarator *)
908                alloc_declarator (sizeof (cp_parameter_declarator)));
909   parameter->next = NULL;
910   if (decl_specifiers)
911     parameter->decl_specifiers = *decl_specifiers;
912   else
913     clear_decl_specs (&parameter->decl_specifiers);
914   parameter->declarator = declarator;
915   parameter->default_argument = default_argument;
916   parameter->ellipsis_p = false;
917
918   return parameter;
919 }
920
921 /* The parser.  */
922
923 /* Overview
924    --------
925
926    A cp_parser parses the token stream as specified by the C++
927    grammar.  Its job is purely parsing, not semantic analysis.  For
928    example, the parser breaks the token stream into declarators,
929    expressions, statements, and other similar syntactic constructs.
930    It does not check that the types of the expressions on either side
931    of an assignment-statement are compatible, or that a function is
932    not declared with a parameter of type `void'.
933
934    The parser invokes routines elsewhere in the compiler to perform
935    semantic analysis and to build up the abstract syntax tree for the
936    code processed.
937
938    The parser (and the template instantiation code, which is, in a
939    way, a close relative of parsing) are the only parts of the
940    compiler that should be calling push_scope and pop_scope, or
941    related functions.  The parser (and template instantiation code)
942    keeps track of what scope is presently active; everything else
943    should simply honor that.  (The code that generates static
944    initializers may also need to set the scope, in order to check
945    access control correctly when emitting the initializers.)
946
947    Methodology
948    -----------
949
950    The parser is of the standard recursive-descent variety.  Upcoming
951    tokens in the token stream are examined in order to determine which
952    production to use when parsing a non-terminal.  Some C++ constructs
953    require arbitrary look ahead to disambiguate.  For example, it is
954    impossible, in the general case, to tell whether a statement is an
955    expression or declaration without scanning the entire statement.
956    Therefore, the parser is capable of "parsing tentatively."  When the
957    parser is not sure what construct comes next, it enters this mode.
958    Then, while we attempt to parse the construct, the parser queues up
959    error messages, rather than issuing them immediately, and saves the
960    tokens it consumes.  If the construct is parsed successfully, the
961    parser "commits", i.e., it issues any queued error messages and
962    the tokens that were being preserved are permanently discarded.
963    If, however, the construct is not parsed successfully, the parser
964    rolls back its state completely so that it can resume parsing using
965    a different alternative.
966
967    Future Improvements
968    -------------------
969
970    The performance of the parser could probably be improved substantially.
971    We could often eliminate the need to parse tentatively by looking ahead
972    a little bit.  In some places, this approach might not entirely eliminate
973    the need to parse tentatively, but it might still speed up the average
974    case.  */
975
976 /* Flags that are passed to some parsing functions.  These values can
977    be bitwise-ored together.  */
978
979 typedef enum cp_parser_flags
980 {
981   /* No flags.  */
982   CP_PARSER_FLAGS_NONE = 0x0,
983   /* The construct is optional.  If it is not present, then no error
984      should be issued.  */
985   CP_PARSER_FLAGS_OPTIONAL = 0x1,
986   /* When parsing a type-specifier, do not allow user-defined types.  */
987   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
988 } cp_parser_flags;
989
990 /* The different kinds of declarators we want to parse.  */
991
992 typedef enum cp_parser_declarator_kind
993 {
994   /* We want an abstract declarator.  */
995   CP_PARSER_DECLARATOR_ABSTRACT,
996   /* We want a named declarator.  */
997   CP_PARSER_DECLARATOR_NAMED,
998   /* We don't mind, but the name must be an unqualified-id.  */
999   CP_PARSER_DECLARATOR_EITHER
1000 } cp_parser_declarator_kind;
1001
1002 /* The precedence values used to parse binary expressions.  The minimum value
1003    of PREC must be 1, because zero is reserved to quickly discriminate
1004    binary operators from other tokens.  */
1005
1006 enum cp_parser_prec
1007 {
1008   PREC_NOT_OPERATOR,
1009   PREC_LOGICAL_OR_EXPRESSION,
1010   PREC_LOGICAL_AND_EXPRESSION,
1011   PREC_INCLUSIVE_OR_EXPRESSION,
1012   PREC_EXCLUSIVE_OR_EXPRESSION,
1013   PREC_AND_EXPRESSION,
1014   PREC_EQUALITY_EXPRESSION,
1015   PREC_RELATIONAL_EXPRESSION,
1016   PREC_SHIFT_EXPRESSION,
1017   PREC_ADDITIVE_EXPRESSION,
1018   PREC_MULTIPLICATIVE_EXPRESSION,
1019   PREC_PM_EXPRESSION,
1020   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1021 };
1022
1023 /* A mapping from a token type to a corresponding tree node type, with a
1024    precedence value.  */
1025
1026 typedef struct cp_parser_binary_operations_map_node
1027 {
1028   /* The token type.  */
1029   enum cpp_ttype token_type;
1030   /* The corresponding tree code.  */
1031   enum tree_code tree_type;
1032   /* The precedence of this operator.  */
1033   enum cp_parser_prec prec;
1034 } cp_parser_binary_operations_map_node;
1035
1036 /* The status of a tentative parse.  */
1037
1038 typedef enum cp_parser_status_kind
1039 {
1040   /* No errors have occurred.  */
1041   CP_PARSER_STATUS_KIND_NO_ERROR,
1042   /* An error has occurred.  */
1043   CP_PARSER_STATUS_KIND_ERROR,
1044   /* We are committed to this tentative parse, whether or not an error
1045      has occurred.  */
1046   CP_PARSER_STATUS_KIND_COMMITTED
1047 } cp_parser_status_kind;
1048
1049 typedef struct cp_parser_expression_stack_entry
1050 {
1051   tree lhs;
1052   enum tree_code tree_type;
1053   int prec;
1054 } cp_parser_expression_stack_entry;
1055
1056 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1057    entries because precedence levels on the stack are monotonically
1058    increasing.  */
1059 typedef struct cp_parser_expression_stack_entry
1060   cp_parser_expression_stack[NUM_PREC_VALUES];
1061
1062 /* Context that is saved and restored when parsing tentatively.  */
1063 typedef struct cp_parser_context GTY (())
1064 {
1065   /* If this is a tentative parsing context, the status of the
1066      tentative parse.  */
1067   enum cp_parser_status_kind status;
1068   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1069      that are looked up in this context must be looked up both in the
1070      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1071      the context of the containing expression.  */
1072   tree object_type;
1073
1074   /* The next parsing context in the stack.  */
1075   struct cp_parser_context *next;
1076 } cp_parser_context;
1077
1078 /* Prototypes.  */
1079
1080 /* Constructors and destructors.  */
1081
1082 static cp_parser_context *cp_parser_context_new
1083   (cp_parser_context *);
1084
1085 /* Class variables.  */
1086
1087 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1088
1089 /* The operator-precedence table used by cp_parser_binary_expression.
1090    Transformed into an associative array (binops_by_token) by
1091    cp_parser_new.  */
1092
1093 static const cp_parser_binary_operations_map_node binops[] = {
1094   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1095   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1096
1097   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1098   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1099   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1100
1101   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1102   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1103
1104   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1105   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1106
1107   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1108   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1109   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1110   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1111   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1112   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1113
1114   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1115   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1116
1117   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1118
1119   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1120
1121   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1122
1123   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1124
1125   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1126 };
1127
1128 /* The same as binops, but initialized by cp_parser_new so that
1129    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1130    for speed.  */
1131 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1132
1133 /* Constructors and destructors.  */
1134
1135 /* Construct a new context.  The context below this one on the stack
1136    is given by NEXT.  */
1137
1138 static cp_parser_context *
1139 cp_parser_context_new (cp_parser_context* next)
1140 {
1141   cp_parser_context *context;
1142
1143   /* Allocate the storage.  */
1144   if (cp_parser_context_free_list != NULL)
1145     {
1146       /* Pull the first entry from the free list.  */
1147       context = cp_parser_context_free_list;
1148       cp_parser_context_free_list = context->next;
1149       memset (context, 0, sizeof (*context));
1150     }
1151   else
1152     context = GGC_CNEW (cp_parser_context);
1153
1154   /* No errors have occurred yet in this context.  */
1155   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1156   /* If this is not the bottomost context, copy information that we
1157      need from the previous context.  */
1158   if (next)
1159     {
1160       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1161          expression, then we are parsing one in this context, too.  */
1162       context->object_type = next->object_type;
1163       /* Thread the stack.  */
1164       context->next = next;
1165     }
1166
1167   return context;
1168 }
1169
1170 /* The cp_parser structure represents the C++ parser.  */
1171
1172 typedef struct cp_parser GTY(())
1173 {
1174   /* The lexer from which we are obtaining tokens.  */
1175   cp_lexer *lexer;
1176
1177   /* The scope in which names should be looked up.  If NULL_TREE, then
1178      we look up names in the scope that is currently open in the
1179      source program.  If non-NULL, this is either a TYPE or
1180      NAMESPACE_DECL for the scope in which we should look.
1181
1182      This value is not cleared automatically after a name is looked
1183      up, so we must be careful to clear it before starting a new look
1184      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1185      will look up `Z' in the scope of `X', rather than the current
1186      scope.)  Unfortunately, it is difficult to tell when name lookup
1187      is complete, because we sometimes peek at a token, look it up,
1188      and then decide not to consume it.  */
1189   tree scope;
1190
1191   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1192      last lookup took place.  OBJECT_SCOPE is used if an expression
1193      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1194      respectively.  QUALIFYING_SCOPE is used for an expression of the
1195      form "X::Y"; it refers to X.  */
1196   tree object_scope;
1197   tree qualifying_scope;
1198
1199   /* A stack of parsing contexts.  All but the bottom entry on the
1200      stack will be tentative contexts.
1201
1202      We parse tentatively in order to determine which construct is in
1203      use in some situations.  For example, in order to determine
1204      whether a statement is an expression-statement or a
1205      declaration-statement we parse it tentatively as a
1206      declaration-statement.  If that fails, we then reparse the same
1207      token stream as an expression-statement.  */
1208   cp_parser_context *context;
1209
1210   /* True if we are parsing GNU C++.  If this flag is not set, then
1211      GNU extensions are not recognized.  */
1212   bool allow_gnu_extensions_p;
1213
1214   /* TRUE if the `>' token should be interpreted as the greater-than
1215      operator.  FALSE if it is the end of a template-id or
1216      template-parameter-list.  */
1217   bool greater_than_is_operator_p;
1218
1219   /* TRUE if default arguments are allowed within a parameter list
1220      that starts at this point. FALSE if only a gnu extension makes
1221      them permissible.  */
1222   bool default_arg_ok_p;
1223
1224   /* TRUE if we are parsing an integral constant-expression.  See
1225      [expr.const] for a precise definition.  */
1226   bool integral_constant_expression_p;
1227
1228   /* TRUE if we are parsing an integral constant-expression -- but a
1229      non-constant expression should be permitted as well.  This flag
1230      is used when parsing an array bound so that GNU variable-length
1231      arrays are tolerated.  */
1232   bool allow_non_integral_constant_expression_p;
1233
1234   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1235      been seen that makes the expression non-constant.  */
1236   bool non_integral_constant_expression_p;
1237
1238   /* TRUE if local variable names and `this' are forbidden in the
1239      current context.  */
1240   bool local_variables_forbidden_p;
1241
1242   /* TRUE if the declaration we are parsing is part of a
1243      linkage-specification of the form `extern string-literal
1244      declaration'.  */
1245   bool in_unbraced_linkage_specification_p;
1246
1247   /* TRUE if we are presently parsing a declarator, after the
1248      direct-declarator.  */
1249   bool in_declarator_p;
1250
1251   /* TRUE if we are presently parsing a template-argument-list.  */
1252   bool in_template_argument_list_p;
1253
1254   /* TRUE if we are presently parsing the body of an
1255      iteration-statement.  */
1256   bool in_iteration_statement_p;
1257
1258   /* TRUE if we are presently parsing the body of a switch
1259      statement.  */
1260   bool in_switch_statement_p;
1261
1262   /* TRUE if we are parsing a type-id in an expression context.  In
1263      such a situation, both "type (expr)" and "type (type)" are valid
1264      alternatives.  */
1265   bool in_type_id_in_expr_p;
1266
1267   /* TRUE if we are currently in a header file where declarations are
1268      implicitly extern "C".  */
1269   bool implicit_extern_c;
1270
1271   /* TRUE if strings in expressions should be translated to the execution
1272      character set.  */
1273   bool translate_strings_p;
1274
1275   /* If non-NULL, then we are parsing a construct where new type
1276      definitions are not permitted.  The string stored here will be
1277      issued as an error message if a type is defined.  */
1278   const char *type_definition_forbidden_message;
1279
1280   /* A list of lists. The outer list is a stack, used for member
1281      functions of local classes. At each level there are two sub-list,
1282      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1283      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1284      TREE_VALUE's. The functions are chained in reverse declaration
1285      order.
1286
1287      The TREE_PURPOSE sublist contains those functions with default
1288      arguments that need post processing, and the TREE_VALUE sublist
1289      contains those functions with definitions that need post
1290      processing.
1291
1292      These lists can only be processed once the outermost class being
1293      defined is complete.  */
1294   tree unparsed_functions_queues;
1295
1296   /* The number of classes whose definitions are currently in
1297      progress.  */
1298   unsigned num_classes_being_defined;
1299
1300   /* The number of template parameter lists that apply directly to the
1301      current declaration.  */
1302   unsigned num_template_parameter_lists;
1303 } cp_parser;
1304
1305 /* The type of a function that parses some kind of expression.  */
1306 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1307
1308 /* Prototypes.  */
1309
1310 /* Constructors and destructors.  */
1311
1312 static cp_parser *cp_parser_new
1313   (void);
1314
1315 /* Routines to parse various constructs.
1316
1317    Those that return `tree' will return the error_mark_node (rather
1318    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1319    Sometimes, they will return an ordinary node if error-recovery was
1320    attempted, even though a parse error occurred.  So, to check
1321    whether or not a parse error occurred, you should always use
1322    cp_parser_error_occurred.  If the construct is optional (indicated
1323    either by an `_opt' in the name of the function that does the
1324    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1325    the construct is not present.  */
1326
1327 /* Lexical conventions [gram.lex]  */
1328
1329 static tree cp_parser_identifier
1330   (cp_parser *);
1331 static tree cp_parser_string_literal
1332   (cp_parser *, bool, bool);
1333
1334 /* Basic concepts [gram.basic]  */
1335
1336 static bool cp_parser_translation_unit
1337   (cp_parser *);
1338
1339 /* Expressions [gram.expr]  */
1340
1341 static tree cp_parser_primary_expression
1342   (cp_parser *, cp_id_kind *, tree *);
1343 static tree cp_parser_id_expression
1344   (cp_parser *, bool, bool, bool *, bool);
1345 static tree cp_parser_unqualified_id
1346   (cp_parser *, bool, bool, bool);
1347 static tree cp_parser_nested_name_specifier_opt
1348   (cp_parser *, bool, bool, bool, bool);
1349 static tree cp_parser_nested_name_specifier
1350   (cp_parser *, bool, bool, bool, bool);
1351 static tree cp_parser_class_or_namespace_name
1352   (cp_parser *, bool, bool, bool, bool, bool);
1353 static tree cp_parser_postfix_expression
1354   (cp_parser *, bool);
1355 static tree cp_parser_postfix_open_square_expression
1356   (cp_parser *, tree, bool);
1357 static tree cp_parser_postfix_dot_deref_expression
1358   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1359 static tree cp_parser_parenthesized_expression_list
1360   (cp_parser *, bool, bool *);
1361 static void cp_parser_pseudo_destructor_name
1362   (cp_parser *, tree *, tree *);
1363 static tree cp_parser_unary_expression
1364   (cp_parser *, bool);
1365 static enum tree_code cp_parser_unary_operator
1366   (cp_token *);
1367 static tree cp_parser_new_expression
1368   (cp_parser *);
1369 static tree cp_parser_new_placement
1370   (cp_parser *);
1371 static tree cp_parser_new_type_id
1372   (cp_parser *, tree *);
1373 static cp_declarator *cp_parser_new_declarator_opt
1374   (cp_parser *);
1375 static cp_declarator *cp_parser_direct_new_declarator
1376   (cp_parser *);
1377 static tree cp_parser_new_initializer
1378   (cp_parser *);
1379 static tree cp_parser_delete_expression
1380   (cp_parser *);
1381 static tree cp_parser_cast_expression
1382   (cp_parser *, bool);
1383 static tree cp_parser_binary_expression
1384   (cp_parser *);
1385 static tree cp_parser_question_colon_clause
1386   (cp_parser *, tree);
1387 static tree cp_parser_assignment_expression
1388   (cp_parser *);
1389 static enum tree_code cp_parser_assignment_operator_opt
1390   (cp_parser *);
1391 static tree cp_parser_expression
1392   (cp_parser *);
1393 static tree cp_parser_constant_expression
1394   (cp_parser *, bool, bool *);
1395 static tree cp_parser_builtin_offsetof
1396   (cp_parser *);
1397
1398 /* Statements [gram.stmt.stmt]  */
1399
1400 static void cp_parser_statement
1401   (cp_parser *, tree);
1402 static tree cp_parser_labeled_statement
1403   (cp_parser *, tree);
1404 static tree cp_parser_expression_statement
1405   (cp_parser *, tree);
1406 static tree cp_parser_compound_statement
1407   (cp_parser *, tree, bool);
1408 static void cp_parser_statement_seq_opt
1409   (cp_parser *, tree);
1410 static tree cp_parser_selection_statement
1411   (cp_parser *);
1412 static tree cp_parser_condition
1413   (cp_parser *);
1414 static tree cp_parser_iteration_statement
1415   (cp_parser *);
1416 static void cp_parser_for_init_statement
1417   (cp_parser *);
1418 static tree cp_parser_jump_statement
1419   (cp_parser *);
1420 static void cp_parser_declaration_statement
1421   (cp_parser *);
1422
1423 static tree cp_parser_implicitly_scoped_statement
1424   (cp_parser *);
1425 static void cp_parser_already_scoped_statement
1426   (cp_parser *);
1427
1428 /* Declarations [gram.dcl.dcl] */
1429
1430 static void cp_parser_declaration_seq_opt
1431   (cp_parser *);
1432 static void cp_parser_declaration
1433   (cp_parser *);
1434 static void cp_parser_block_declaration
1435   (cp_parser *, bool);
1436 static void cp_parser_simple_declaration
1437   (cp_parser *, bool);
1438 static void cp_parser_decl_specifier_seq
1439   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1440 static tree cp_parser_storage_class_specifier_opt
1441   (cp_parser *);
1442 static tree cp_parser_function_specifier_opt
1443   (cp_parser *, cp_decl_specifier_seq *);
1444 static tree cp_parser_type_specifier
1445   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1446    int *, bool *);
1447 static tree cp_parser_simple_type_specifier
1448   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1449 static tree cp_parser_type_name
1450   (cp_parser *);
1451 static tree cp_parser_elaborated_type_specifier
1452   (cp_parser *, bool, bool);
1453 static tree cp_parser_enum_specifier
1454   (cp_parser *);
1455 static void cp_parser_enumerator_list
1456   (cp_parser *, tree);
1457 static void cp_parser_enumerator_definition
1458   (cp_parser *, tree);
1459 static tree cp_parser_namespace_name
1460   (cp_parser *);
1461 static void cp_parser_namespace_definition
1462   (cp_parser *);
1463 static void cp_parser_namespace_body
1464   (cp_parser *);
1465 static tree cp_parser_qualified_namespace_specifier
1466   (cp_parser *);
1467 static void cp_parser_namespace_alias_definition
1468   (cp_parser *);
1469 static void cp_parser_using_declaration
1470   (cp_parser *);
1471 static void cp_parser_using_directive
1472   (cp_parser *);
1473 static void cp_parser_asm_definition
1474   (cp_parser *);
1475 static void cp_parser_linkage_specification
1476   (cp_parser *);
1477
1478 /* Declarators [gram.dcl.decl] */
1479
1480 static tree cp_parser_init_declarator
1481   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1482 static cp_declarator *cp_parser_declarator
1483   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1484 static cp_declarator *cp_parser_direct_declarator
1485   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1486 static enum tree_code cp_parser_ptr_operator
1487   (cp_parser *, tree *, cp_cv_quals *);
1488 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1489   (cp_parser *);
1490 static tree cp_parser_declarator_id
1491   (cp_parser *);
1492 static tree cp_parser_type_id
1493   (cp_parser *);
1494 static void cp_parser_type_specifier_seq
1495   (cp_parser *, cp_decl_specifier_seq *);
1496 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1497   (cp_parser *);
1498 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1499   (cp_parser *, bool *);
1500 static cp_parameter_declarator *cp_parser_parameter_declaration
1501   (cp_parser *, bool, bool *);
1502 static void cp_parser_function_body
1503   (cp_parser *);
1504 static tree cp_parser_initializer
1505   (cp_parser *, bool *, bool *);
1506 static tree cp_parser_initializer_clause
1507   (cp_parser *, bool *);
1508 static tree cp_parser_initializer_list
1509   (cp_parser *, bool *);
1510
1511 static bool cp_parser_ctor_initializer_opt_and_function_body
1512   (cp_parser *);
1513
1514 /* Classes [gram.class] */
1515
1516 static tree cp_parser_class_name
1517   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1518 static tree cp_parser_class_specifier
1519   (cp_parser *);
1520 static tree cp_parser_class_head
1521   (cp_parser *, bool *, tree *);
1522 static enum tag_types cp_parser_class_key
1523   (cp_parser *);
1524 static void cp_parser_member_specification_opt
1525   (cp_parser *);
1526 static void cp_parser_member_declaration
1527   (cp_parser *);
1528 static tree cp_parser_pure_specifier
1529   (cp_parser *);
1530 static tree cp_parser_constant_initializer
1531   (cp_parser *);
1532
1533 /* Derived classes [gram.class.derived] */
1534
1535 static tree cp_parser_base_clause
1536   (cp_parser *);
1537 static tree cp_parser_base_specifier
1538   (cp_parser *);
1539
1540 /* Special member functions [gram.special] */
1541
1542 static tree cp_parser_conversion_function_id
1543   (cp_parser *);
1544 static tree cp_parser_conversion_type_id
1545   (cp_parser *);
1546 static cp_declarator *cp_parser_conversion_declarator_opt
1547   (cp_parser *);
1548 static bool cp_parser_ctor_initializer_opt
1549   (cp_parser *);
1550 static void cp_parser_mem_initializer_list
1551   (cp_parser *);
1552 static tree cp_parser_mem_initializer
1553   (cp_parser *);
1554 static tree cp_parser_mem_initializer_id
1555   (cp_parser *);
1556
1557 /* Overloading [gram.over] */
1558
1559 static tree cp_parser_operator_function_id
1560   (cp_parser *);
1561 static tree cp_parser_operator
1562   (cp_parser *);
1563
1564 /* Templates [gram.temp] */
1565
1566 static void cp_parser_template_declaration
1567   (cp_parser *, bool);
1568 static tree cp_parser_template_parameter_list
1569   (cp_parser *);
1570 static tree cp_parser_template_parameter
1571   (cp_parser *, bool *);
1572 static tree cp_parser_type_parameter
1573   (cp_parser *);
1574 static tree cp_parser_template_id
1575   (cp_parser *, bool, bool, bool);
1576 static tree cp_parser_template_name
1577   (cp_parser *, bool, bool, bool, bool *);
1578 static tree cp_parser_template_argument_list
1579   (cp_parser *);
1580 static tree cp_parser_template_argument
1581   (cp_parser *);
1582 static void cp_parser_explicit_instantiation
1583   (cp_parser *);
1584 static void cp_parser_explicit_specialization
1585   (cp_parser *);
1586
1587 /* Exception handling [gram.exception] */
1588
1589 static tree cp_parser_try_block
1590   (cp_parser *);
1591 static bool cp_parser_function_try_block
1592   (cp_parser *);
1593 static void cp_parser_handler_seq
1594   (cp_parser *);
1595 static void cp_parser_handler
1596   (cp_parser *);
1597 static tree cp_parser_exception_declaration
1598   (cp_parser *);
1599 static tree cp_parser_throw_expression
1600   (cp_parser *);
1601 static tree cp_parser_exception_specification_opt
1602   (cp_parser *);
1603 static tree cp_parser_type_id_list
1604   (cp_parser *);
1605
1606 /* GNU Extensions */
1607
1608 static tree cp_parser_asm_specification_opt
1609   (cp_parser *);
1610 static tree cp_parser_asm_operand_list
1611   (cp_parser *);
1612 static tree cp_parser_asm_clobber_list
1613   (cp_parser *);
1614 static tree cp_parser_attributes_opt
1615   (cp_parser *);
1616 static tree cp_parser_attribute_list
1617   (cp_parser *);
1618 static bool cp_parser_extension_opt
1619   (cp_parser *, int *);
1620 static void cp_parser_label_declaration
1621   (cp_parser *);
1622
1623 /* Utility Routines */
1624
1625 static tree cp_parser_lookup_name
1626   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1627 static tree cp_parser_lookup_name_simple
1628   (cp_parser *, tree);
1629 static tree cp_parser_maybe_treat_template_as_class
1630   (tree, bool);
1631 static bool cp_parser_check_declarator_template_parameters
1632   (cp_parser *, cp_declarator *);
1633 static bool cp_parser_check_template_parameters
1634   (cp_parser *, unsigned);
1635 static tree cp_parser_simple_cast_expression
1636   (cp_parser *);
1637 static tree cp_parser_global_scope_opt
1638   (cp_parser *, bool);
1639 static bool cp_parser_constructor_declarator_p
1640   (cp_parser *, bool);
1641 static tree cp_parser_function_definition_from_specifiers_and_declarator
1642   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1643 static tree cp_parser_function_definition_after_declarator
1644   (cp_parser *, bool);
1645 static void cp_parser_template_declaration_after_export
1646   (cp_parser *, bool);
1647 static tree cp_parser_single_declaration
1648   (cp_parser *, bool, bool *);
1649 static tree cp_parser_functional_cast
1650   (cp_parser *, tree);
1651 static tree cp_parser_save_member_function_body
1652   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1653 static tree cp_parser_enclosed_template_argument_list
1654   (cp_parser *);
1655 static void cp_parser_save_default_args
1656   (cp_parser *, tree);
1657 static void cp_parser_late_parsing_for_member
1658   (cp_parser *, tree);
1659 static void cp_parser_late_parsing_default_args
1660   (cp_parser *, tree);
1661 static tree cp_parser_sizeof_operand
1662   (cp_parser *, enum rid);
1663 static bool cp_parser_declares_only_class_p
1664   (cp_parser *);
1665 static void cp_parser_set_storage_class
1666   (cp_decl_specifier_seq *, cp_storage_class);
1667 static void cp_parser_set_decl_spec_type
1668   (cp_decl_specifier_seq *, tree, bool);
1669 static bool cp_parser_friend_p
1670   (const cp_decl_specifier_seq *);
1671 static cp_token *cp_parser_require
1672   (cp_parser *, enum cpp_ttype, const char *);
1673 static cp_token *cp_parser_require_keyword
1674   (cp_parser *, enum rid, const char *);
1675 static bool cp_parser_token_starts_function_definition_p
1676   (cp_token *);
1677 static bool cp_parser_next_token_starts_class_definition_p
1678   (cp_parser *);
1679 static bool cp_parser_next_token_ends_template_argument_p
1680   (cp_parser *);
1681 static bool cp_parser_nth_token_starts_template_argument_list_p
1682   (cp_parser *, size_t);
1683 static enum tag_types cp_parser_token_is_class_key
1684   (cp_token *);
1685 static void cp_parser_check_class_key
1686   (enum tag_types, tree type);
1687 static void cp_parser_check_access_in_redeclaration
1688   (tree type);
1689 static bool cp_parser_optional_template_keyword
1690   (cp_parser *);
1691 static void cp_parser_pre_parsed_nested_name_specifier
1692   (cp_parser *);
1693 static void cp_parser_cache_group
1694   (cp_parser *, enum cpp_ttype, unsigned);
1695 static void cp_parser_parse_tentatively
1696   (cp_parser *);
1697 static void cp_parser_commit_to_tentative_parse
1698   (cp_parser *);
1699 static void cp_parser_abort_tentative_parse
1700   (cp_parser *);
1701 static bool cp_parser_parse_definitely
1702   (cp_parser *);
1703 static inline bool cp_parser_parsing_tentatively
1704   (cp_parser *);
1705 static bool cp_parser_uncommitted_to_tentative_parse_p
1706   (cp_parser *);
1707 static void cp_parser_error
1708   (cp_parser *, const char *);
1709 static void cp_parser_name_lookup_error
1710   (cp_parser *, tree, tree, const char *);
1711 static bool cp_parser_simulate_error
1712   (cp_parser *);
1713 static void cp_parser_check_type_definition
1714   (cp_parser *);
1715 static void cp_parser_check_for_definition_in_return_type
1716   (cp_declarator *, tree);
1717 static void cp_parser_check_for_invalid_template_id
1718   (cp_parser *, tree);
1719 static bool cp_parser_non_integral_constant_expression
1720   (cp_parser *, const char *);
1721 static void cp_parser_diagnose_invalid_type_name
1722   (cp_parser *, tree, tree);
1723 static bool cp_parser_parse_and_diagnose_invalid_type_name
1724   (cp_parser *);
1725 static int cp_parser_skip_to_closing_parenthesis
1726   (cp_parser *, bool, bool, bool);
1727 static void cp_parser_skip_to_end_of_statement
1728   (cp_parser *);
1729 static void cp_parser_consume_semicolon_at_end_of_statement
1730   (cp_parser *);
1731 static void cp_parser_skip_to_end_of_block_or_statement
1732   (cp_parser *);
1733 static void cp_parser_skip_to_closing_brace
1734   (cp_parser *);
1735 static void cp_parser_skip_until_found
1736   (cp_parser *, enum cpp_ttype, const char *);
1737 static bool cp_parser_error_occurred
1738   (cp_parser *);
1739 static bool cp_parser_allow_gnu_extensions_p
1740   (cp_parser *);
1741 static bool cp_parser_is_string_literal
1742   (cp_token *);
1743 static bool cp_parser_is_keyword
1744   (cp_token *, enum rid);
1745 static tree cp_parser_make_typename_type
1746   (cp_parser *, tree, tree);
1747
1748 /* Returns nonzero if we are parsing tentatively.  */
1749
1750 static inline bool
1751 cp_parser_parsing_tentatively (cp_parser* parser)
1752 {
1753   return parser->context->next != NULL;
1754 }
1755
1756 /* Returns nonzero if TOKEN is a string literal.  */
1757
1758 static bool
1759 cp_parser_is_string_literal (cp_token* token)
1760 {
1761   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1762 }
1763
1764 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1765
1766 static bool
1767 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1768 {
1769   return token->keyword == keyword;
1770 }
1771
1772 /* If not parsing tentatively, issue a diagnostic of the form
1773       FILE:LINE: MESSAGE before TOKEN
1774    where TOKEN is the next token in the input stream.  MESSAGE
1775    (specified by the caller) is usually of the form "expected
1776    OTHER-TOKEN".  */
1777
1778 static void
1779 cp_parser_error (cp_parser* parser, const char* message)
1780 {
1781   if (!cp_parser_simulate_error (parser))
1782     {
1783       cp_token *token = cp_lexer_peek_token (parser->lexer);
1784       /* This diagnostic makes more sense if it is tagged to the line
1785          of the token we just peeked at.  */
1786       cp_lexer_set_source_position_from_token (token);
1787       c_parse_error (message,
1788                      /* Because c_parser_error does not understand
1789                         CPP_KEYWORD, keywords are treated like
1790                         identifiers.  */
1791                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1792                      token->value);
1793     }
1794 }
1795
1796 /* Issue an error about name-lookup failing.  NAME is the
1797    IDENTIFIER_NODE DECL is the result of
1798    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1799    the thing that we hoped to find.  */
1800
1801 static void
1802 cp_parser_name_lookup_error (cp_parser* parser,
1803                              tree name,
1804                              tree decl,
1805                              const char* desired)
1806 {
1807   /* If name lookup completely failed, tell the user that NAME was not
1808      declared.  */
1809   if (decl == error_mark_node)
1810     {
1811       if (parser->scope && parser->scope != global_namespace)
1812         error ("%<%D::%D%> has not been declared",
1813                parser->scope, name);
1814       else if (parser->scope == global_namespace)
1815         error ("%<::%D%> has not been declared", name);
1816       else if (parser->object_scope 
1817                && !CLASS_TYPE_P (parser->object_scope))
1818         error ("request for member %qD in non-class type %qT",
1819                name, parser->object_scope);
1820       else if (parser->object_scope)
1821         error ("%<%T::%D%> has not been declared", 
1822                parser->object_scope, name);
1823       else
1824         error ("%qD has not been declared", name);
1825     }
1826   else if (parser->scope && parser->scope != global_namespace)
1827     error ("%<%D::%D%> %s", parser->scope, name, desired);
1828   else if (parser->scope == global_namespace)
1829     error ("%<::%D%> %s", name, desired);
1830   else
1831     error ("%qD %s", name, desired);
1832 }
1833
1834 /* If we are parsing tentatively, remember that an error has occurred
1835    during this tentative parse.  Returns true if the error was
1836    simulated; false if a message should be issued by the caller.  */
1837
1838 static bool
1839 cp_parser_simulate_error (cp_parser* parser)
1840 {
1841   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1842     {
1843       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1844       return true;
1845     }
1846   return false;
1847 }
1848
1849 /* This function is called when a type is defined.  If type
1850    definitions are forbidden at this point, an error message is
1851    issued.  */
1852
1853 static void
1854 cp_parser_check_type_definition (cp_parser* parser)
1855 {
1856   /* If types are forbidden here, issue a message.  */
1857   if (parser->type_definition_forbidden_message)
1858     /* Use `%s' to print the string in case there are any escape
1859        characters in the message.  */
1860     error ("%s", parser->type_definition_forbidden_message);
1861 }
1862
1863 /* This function is called when the DECLARATOR is processed.  The TYPE
1864    was a type defined in the decl-specifiers.  If it is invalid to
1865    define a type in the decl-specifiers for DECLARATOR, an error is
1866    issued.  */
1867
1868 static void
1869 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1870                                                tree type)
1871 {
1872   /* [dcl.fct] forbids type definitions in return types.
1873      Unfortunately, it's not easy to know whether or not we are
1874      processing a return type until after the fact.  */
1875   while (declarator
1876          && (declarator->kind == cdk_pointer
1877              || declarator->kind == cdk_reference
1878              || declarator->kind == cdk_ptrmem))
1879     declarator = declarator->declarator;
1880   if (declarator
1881       && declarator->kind == cdk_function)
1882     {
1883       error ("new types may not be defined in a return type");
1884       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1885               type);
1886     }
1887 }
1888
1889 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1890    "<" in any valid C++ program.  If the next token is indeed "<",
1891    issue a message warning the user about what appears to be an
1892    invalid attempt to form a template-id.  */
1893
1894 static void
1895 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1896                                          tree type)
1897 {
1898   cp_token_position start = 0;
1899
1900   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1901     {
1902       if (TYPE_P (type))
1903         error ("%qT is not a template", type);
1904       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1905         error ("%qE is not a template", type);
1906       else
1907         error ("invalid template-id");
1908       /* Remember the location of the invalid "<".  */
1909       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1910         start = cp_lexer_token_position (parser->lexer, true);
1911       /* Consume the "<".  */
1912       cp_lexer_consume_token (parser->lexer);
1913       /* Parse the template arguments.  */
1914       cp_parser_enclosed_template_argument_list (parser);
1915       /* Permanently remove the invalid template arguments so that
1916          this error message is not issued again.  */
1917       if (start)
1918         cp_lexer_purge_tokens_after (parser->lexer, start);
1919     }
1920 }
1921
1922 /* If parsing an integral constant-expression, issue an error message
1923    about the fact that THING appeared and return true.  Otherwise,
1924    return false, marking the current expression as non-constant.  */
1925
1926 static bool
1927 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1928                                             const char *thing)
1929 {
1930   if (parser->integral_constant_expression_p)
1931     {
1932       if (!parser->allow_non_integral_constant_expression_p)
1933         {
1934           error ("%s cannot appear in a constant-expression", thing);
1935           return true;
1936         }
1937       parser->non_integral_constant_expression_p = true;
1938     }
1939   return false;
1940 }
1941
1942 /* Emit a diagnostic for an invalid type name.  SCOPE is the
1943    qualifying scope (or NULL, if none) for ID.  */
1944
1945 static void
1946 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1947 {
1948   tree decl, old_scope;
1949   /* Try to lookup the identifier.  */
1950   old_scope = parser->scope;
1951   parser->scope = scope;
1952   decl = cp_parser_lookup_name_simple (parser, id);
1953   parser->scope = old_scope;
1954   /* If the lookup found a template-name, it means that the user forgot
1955   to specify an argument list. Emit an useful error message.  */
1956   if (TREE_CODE (decl) == TEMPLATE_DECL)
1957     error ("invalid use of template-name %qE without an argument list",
1958       decl);
1959   else if (!parser->scope)
1960     {
1961       /* Issue an error message.  */
1962       error ("%qE does not name a type", id);
1963       /* If we're in a template class, it's possible that the user was
1964          referring to a type from a base class.  For example:
1965
1966            template <typename T> struct A { typedef T X; };
1967            template <typename T> struct B : public A<T> { X x; };
1968
1969          The user should have said "typename A<T>::X".  */
1970       if (processing_template_decl && current_class_type)
1971         {
1972           tree b;
1973
1974           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1975                b;
1976                b = TREE_CHAIN (b))
1977             {
1978               tree base_type = BINFO_TYPE (b);
1979               if (CLASS_TYPE_P (base_type)
1980                   && dependent_type_p (base_type))
1981                 {
1982                   tree field;
1983                   /* Go from a particular instantiation of the
1984                      template (which will have an empty TYPE_FIELDs),
1985                      to the main version.  */
1986                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1987                   for (field = TYPE_FIELDS (base_type);
1988                        field;
1989                        field = TREE_CHAIN (field))
1990                     if (TREE_CODE (field) == TYPE_DECL
1991                         && DECL_NAME (field) == id)
1992                       {
1993                         inform ("(perhaps %<typename %T::%E%> was intended)",
1994                                 BINFO_TYPE (b), id);
1995                         break;
1996                       }
1997                   if (field)
1998                     break;
1999                 }
2000             }
2001         }
2002     }
2003   /* Here we diagnose qualified-ids where the scope is actually correct,
2004      but the identifier does not resolve to a valid type name.  */
2005   else
2006     {
2007       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2008         error ("%qE in namespace %qE does not name a type",
2009                id, parser->scope);
2010       else if (TYPE_P (parser->scope))
2011         error ("%qE in class %qT does not name a type", id, parser->scope);
2012       else
2013         gcc_unreachable ();
2014     }
2015 }
2016
2017 /* Check for a common situation where a type-name should be present,
2018    but is not, and issue a sensible error message.  Returns true if an
2019    invalid type-name was detected.
2020
2021    The situation handled by this function are variable declarations of the
2022    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2023    Usually, `ID' should name a type, but if we got here it means that it
2024    does not. We try to emit the best possible error message depending on
2025    how exactly the id-expression looks like.
2026 */
2027
2028 static bool
2029 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2030 {
2031   tree id;
2032
2033   cp_parser_parse_tentatively (parser);
2034   id = cp_parser_id_expression (parser,
2035                                 /*template_keyword_p=*/false,
2036                                 /*check_dependency_p=*/true,
2037                                 /*template_p=*/NULL,
2038                                 /*declarator_p=*/true);
2039   /* After the id-expression, there should be a plain identifier,
2040      otherwise this is not a simple variable declaration. Also, if
2041      the scope is dependent, we cannot do much.  */
2042   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2043       || (parser->scope && TYPE_P (parser->scope)
2044           && dependent_type_p (parser->scope)))
2045     {
2046       cp_parser_abort_tentative_parse (parser);
2047       return false;
2048     }
2049   if (!cp_parser_parse_definitely (parser)
2050       || TREE_CODE (id) != IDENTIFIER_NODE)
2051     return false;
2052
2053   /* Emit a diagnostic for the invalid type.  */
2054   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2055   /* Skip to the end of the declaration; there's no point in
2056      trying to process it.  */
2057   cp_parser_skip_to_end_of_block_or_statement (parser);
2058   return true;
2059 }
2060
2061 /* Consume tokens up to, and including, the next non-nested closing `)'.
2062    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2063    are doing error recovery. Returns -1 if OR_COMMA is true and we
2064    found an unnested comma.  */
2065
2066 static int
2067 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2068                                        bool recovering,
2069                                        bool or_comma,
2070                                        bool consume_paren)
2071 {
2072   unsigned paren_depth = 0;
2073   unsigned brace_depth = 0;
2074   int result;
2075
2076   if (recovering && !or_comma
2077       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2078     return 0;
2079
2080   while (true)
2081     {
2082       cp_token *token;
2083
2084       /* If we've run out of tokens, then there is no closing `)'.  */
2085       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2086         {
2087           result = 0;
2088           break;
2089         }
2090
2091       token = cp_lexer_peek_token (parser->lexer);
2092
2093       /* This matches the processing in skip_to_end_of_statement.  */
2094       if (token->type == CPP_SEMICOLON && !brace_depth)
2095         {
2096           result = 0;
2097           break;
2098         }
2099       if (token->type == CPP_OPEN_BRACE)
2100         ++brace_depth;
2101       if (token->type == CPP_CLOSE_BRACE)
2102         {
2103           if (!brace_depth--)
2104             {
2105               result = 0;
2106               break;
2107             }
2108         }
2109       if (recovering && or_comma && token->type == CPP_COMMA
2110           && !brace_depth && !paren_depth)
2111         {
2112           result = -1;
2113           break;
2114         }
2115
2116       if (!brace_depth)
2117         {
2118           /* If it is an `(', we have entered another level of nesting.  */
2119           if (token->type == CPP_OPEN_PAREN)
2120             ++paren_depth;
2121           /* If it is a `)', then we might be done.  */
2122           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2123             {
2124               if (consume_paren)
2125                 cp_lexer_consume_token (parser->lexer);
2126               {
2127                 result = 1;
2128                 break;
2129               }
2130             }
2131         }
2132
2133       /* Consume the token.  */
2134       cp_lexer_consume_token (parser->lexer);
2135     }
2136
2137   return result;
2138 }
2139
2140 /* Consume tokens until we reach the end of the current statement.
2141    Normally, that will be just before consuming a `;'.  However, if a
2142    non-nested `}' comes first, then we stop before consuming that.  */
2143
2144 static void
2145 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2146 {
2147   unsigned nesting_depth = 0;
2148
2149   while (true)
2150     {
2151       cp_token *token;
2152
2153       /* Peek at the next token.  */
2154       token = cp_lexer_peek_token (parser->lexer);
2155       /* If we've run out of tokens, stop.  */
2156       if (token->type == CPP_EOF)
2157         break;
2158       /* If the next token is a `;', we have reached the end of the
2159          statement.  */
2160       if (token->type == CPP_SEMICOLON && !nesting_depth)
2161         break;
2162       /* If the next token is a non-nested `}', then we have reached
2163          the end of the current block.  */
2164       if (token->type == CPP_CLOSE_BRACE)
2165         {
2166           /* If this is a non-nested `}', stop before consuming it.
2167              That way, when confronted with something like:
2168
2169                { 3 + }
2170
2171              we stop before consuming the closing `}', even though we
2172              have not yet reached a `;'.  */
2173           if (nesting_depth == 0)
2174             break;
2175           /* If it is the closing `}' for a block that we have
2176              scanned, stop -- but only after consuming the token.
2177              That way given:
2178
2179                 void f g () { ... }
2180                 typedef int I;
2181
2182              we will stop after the body of the erroneously declared
2183              function, but before consuming the following `typedef'
2184              declaration.  */
2185           if (--nesting_depth == 0)
2186             {
2187               cp_lexer_consume_token (parser->lexer);
2188               break;
2189             }
2190         }
2191       /* If it the next token is a `{', then we are entering a new
2192          block.  Consume the entire block.  */
2193       else if (token->type == CPP_OPEN_BRACE)
2194         ++nesting_depth;
2195       /* Consume the token.  */
2196       cp_lexer_consume_token (parser->lexer);
2197     }
2198 }
2199
2200 /* This function is called at the end of a statement or declaration.
2201    If the next token is a semicolon, it is consumed; otherwise, error
2202    recovery is attempted.  */
2203
2204 static void
2205 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2206 {
2207   /* Look for the trailing `;'.  */
2208   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2209     {
2210       /* If there is additional (erroneous) input, skip to the end of
2211          the statement.  */
2212       cp_parser_skip_to_end_of_statement (parser);
2213       /* If the next token is now a `;', consume it.  */
2214       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2215         cp_lexer_consume_token (parser->lexer);
2216     }
2217 }
2218
2219 /* Skip tokens until we have consumed an entire block, or until we
2220    have consumed a non-nested `;'.  */
2221
2222 static void
2223 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2224 {
2225   unsigned nesting_depth = 0;
2226
2227   while (true)
2228     {
2229       cp_token *token;
2230
2231       /* Peek at the next token.  */
2232       token = cp_lexer_peek_token (parser->lexer);
2233       /* If we've run out of tokens, stop.  */
2234       if (token->type == CPP_EOF)
2235         break;
2236       /* If the next token is a `;', we have reached the end of the
2237          statement.  */
2238       if (token->type == CPP_SEMICOLON && !nesting_depth)
2239         {
2240           /* Consume the `;'.  */
2241           cp_lexer_consume_token (parser->lexer);
2242           break;
2243         }
2244       /* Consume the token.  */
2245       token = cp_lexer_consume_token (parser->lexer);
2246       /* If the next token is a non-nested `}', then we have reached
2247          the end of the current block.  */
2248       if (token->type == CPP_CLOSE_BRACE
2249           && (nesting_depth == 0 || --nesting_depth == 0))
2250         break;
2251       /* If it the next token is a `{', then we are entering a new
2252          block.  Consume the entire block.  */
2253       if (token->type == CPP_OPEN_BRACE)
2254         ++nesting_depth;
2255     }
2256 }
2257
2258 /* Skip tokens until a non-nested closing curly brace is the next
2259    token.  */
2260
2261 static void
2262 cp_parser_skip_to_closing_brace (cp_parser *parser)
2263 {
2264   unsigned nesting_depth = 0;
2265
2266   while (true)
2267     {
2268       cp_token *token;
2269
2270       /* Peek at the next token.  */
2271       token = cp_lexer_peek_token (parser->lexer);
2272       /* If we've run out of tokens, stop.  */
2273       if (token->type == CPP_EOF)
2274         break;
2275       /* If the next token is a non-nested `}', then we have reached
2276          the end of the current block.  */
2277       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2278         break;
2279       /* If it the next token is a `{', then we are entering a new
2280          block.  Consume the entire block.  */
2281       else if (token->type == CPP_OPEN_BRACE)
2282         ++nesting_depth;
2283       /* Consume the token.  */
2284       cp_lexer_consume_token (parser->lexer);
2285     }
2286 }
2287
2288 /* This is a simple wrapper around make_typename_type. When the id is
2289    an unresolved identifier node, we can provide a superior diagnostic
2290    using cp_parser_diagnose_invalid_type_name.  */
2291
2292 static tree
2293 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2294 {
2295   tree result;
2296   if (TREE_CODE (id) == IDENTIFIER_NODE)
2297     {
2298       result = make_typename_type (scope, id, typename_type,
2299                                    /*complain=*/0);
2300       if (result == error_mark_node)
2301         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2302       return result;
2303     }
2304   return make_typename_type (scope, id, typename_type, tf_error);
2305 }
2306
2307
2308 /* Create a new C++ parser.  */
2309
2310 static cp_parser *
2311 cp_parser_new (void)
2312 {
2313   cp_parser *parser;
2314   cp_lexer *lexer;
2315   unsigned i;
2316
2317   /* cp_lexer_new_main is called before calling ggc_alloc because
2318      cp_lexer_new_main might load a PCH file.  */
2319   lexer = cp_lexer_new_main ();
2320
2321   /* Initialize the binops_by_token so that we can get the tree
2322      directly from the token.  */
2323   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2324     binops_by_token[binops[i].token_type] = binops[i];
2325
2326   parser = GGC_CNEW (cp_parser);
2327   parser->lexer = lexer;
2328   parser->context = cp_parser_context_new (NULL);
2329
2330   /* For now, we always accept GNU extensions.  */
2331   parser->allow_gnu_extensions_p = 1;
2332
2333   /* The `>' token is a greater-than operator, not the end of a
2334      template-id.  */
2335   parser->greater_than_is_operator_p = true;
2336
2337   parser->default_arg_ok_p = true;
2338
2339   /* We are not parsing a constant-expression.  */
2340   parser->integral_constant_expression_p = false;
2341   parser->allow_non_integral_constant_expression_p = false;
2342   parser->non_integral_constant_expression_p = false;
2343
2344   /* Local variable names are not forbidden.  */
2345   parser->local_variables_forbidden_p = false;
2346
2347   /* We are not processing an `extern "C"' declaration.  */
2348   parser->in_unbraced_linkage_specification_p = false;
2349
2350   /* We are not processing a declarator.  */
2351   parser->in_declarator_p = false;
2352
2353   /* We are not processing a template-argument-list.  */
2354   parser->in_template_argument_list_p = false;
2355
2356   /* We are not in an iteration statement.  */
2357   parser->in_iteration_statement_p = false;
2358
2359   /* We are not in a switch statement.  */
2360   parser->in_switch_statement_p = false;
2361
2362   /* We are not parsing a type-id inside an expression.  */
2363   parser->in_type_id_in_expr_p = false;
2364
2365   /* Declarations aren't implicitly extern "C".  */
2366   parser->implicit_extern_c = false;
2367
2368   /* String literals should be translated to the execution character set.  */
2369   parser->translate_strings_p = true;
2370
2371   /* The unparsed function queue is empty.  */
2372   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2373
2374   /* There are no classes being defined.  */
2375   parser->num_classes_being_defined = 0;
2376
2377   /* No template parameters apply.  */
2378   parser->num_template_parameter_lists = 0;
2379
2380   return parser;
2381 }
2382
2383 /* Create a cp_lexer structure which will emit the tokens in CACHE
2384    and push it onto the parser's lexer stack.  This is used for delayed
2385    parsing of in-class method bodies and default arguments, and should
2386    not be confused with tentative parsing.  */
2387 static void
2388 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2389 {
2390   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2391   lexer->next = parser->lexer;
2392   parser->lexer = lexer;
2393
2394   /* Move the current source position to that of the first token in the
2395      new lexer.  */
2396   cp_lexer_set_source_position_from_token (lexer->next_token);
2397 }
2398
2399 /* Pop the top lexer off the parser stack.  This is never used for the
2400    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2401 static void
2402 cp_parser_pop_lexer (cp_parser *parser)
2403 {
2404   cp_lexer *lexer = parser->lexer;
2405   parser->lexer = lexer->next;
2406   cp_lexer_destroy (lexer);
2407
2408   /* Put the current source position back where it was before this
2409      lexer was pushed.  */
2410   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2411 }
2412
2413 /* Lexical conventions [gram.lex]  */
2414
2415 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2416    identifier.  */
2417
2418 static tree
2419 cp_parser_identifier (cp_parser* parser)
2420 {
2421   cp_token *token;
2422
2423   /* Look for the identifier.  */
2424   token = cp_parser_require (parser, CPP_NAME, "identifier");
2425   /* Return the value.  */
2426   return token ? token->value : error_mark_node;
2427 }
2428
2429 /* Parse a sequence of adjacent string constants.  Returns a
2430    TREE_STRING representing the combined, nul-terminated string
2431    constant.  If TRANSLATE is true, translate the string to the
2432    execution character set.  If WIDE_OK is true, a wide string is
2433    invalid here.
2434
2435    C++98 [lex.string] says that if a narrow string literal token is
2436    adjacent to a wide string literal token, the behavior is undefined.
2437    However, C99 6.4.5p4 says that this results in a wide string literal.
2438    We follow C99 here, for consistency with the C front end.
2439
2440    This code is largely lifted from lex_string() in c-lex.c.
2441
2442    FUTURE: ObjC++ will need to handle @-strings here.  */
2443 static tree
2444 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2445 {
2446   tree value;
2447   bool wide = false;
2448   size_t count;
2449   struct obstack str_ob;
2450   cpp_string str, istr, *strs;
2451   cp_token *tok;
2452
2453   tok = cp_lexer_peek_token (parser->lexer);
2454   if (!cp_parser_is_string_literal (tok))
2455     {
2456       cp_parser_error (parser, "expected string-literal");
2457       return error_mark_node;
2458     }
2459
2460   /* Try to avoid the overhead of creating and destroying an obstack
2461      for the common case of just one string.  */
2462   if (!cp_parser_is_string_literal
2463       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2464     {
2465       cp_lexer_consume_token (parser->lexer);
2466
2467       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2468       str.len = TREE_STRING_LENGTH (tok->value);
2469       count = 1;
2470       if (tok->type == CPP_WSTRING)
2471         wide = true;
2472
2473       strs = &str;
2474     }
2475   else
2476     {
2477       gcc_obstack_init (&str_ob);
2478       count = 0;
2479
2480       do
2481         {
2482           cp_lexer_consume_token (parser->lexer);
2483           count++;
2484           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2485           str.len = TREE_STRING_LENGTH (tok->value);
2486           if (tok->type == CPP_WSTRING)
2487             wide = true;
2488
2489           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2490
2491           tok = cp_lexer_peek_token (parser->lexer);
2492         }
2493       while (cp_parser_is_string_literal (tok));
2494
2495       strs = (cpp_string *) obstack_finish (&str_ob);
2496     }
2497
2498   if (wide && !wide_ok)
2499     {
2500       cp_parser_error (parser, "a wide string is invalid in this context");
2501       wide = false;
2502     }
2503
2504   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2505       (parse_in, strs, count, &istr, wide))
2506     {
2507       value = build_string (istr.len, (char *)istr.text);
2508       free ((void *)istr.text);
2509
2510       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2511       value = fix_string_type (value);
2512     }
2513   else
2514     /* cpp_interpret_string has issued an error.  */
2515     value = error_mark_node;
2516
2517   if (count > 1)
2518     obstack_free (&str_ob, 0);
2519
2520   return value;
2521 }
2522
2523
2524 /* Basic concepts [gram.basic]  */
2525
2526 /* Parse a translation-unit.
2527
2528    translation-unit:
2529      declaration-seq [opt]
2530
2531    Returns TRUE if all went well.  */
2532
2533 static bool
2534 cp_parser_translation_unit (cp_parser* parser)
2535 {
2536   /* The address of the first non-permanent object on the declarator
2537      obstack.  */
2538   static void *declarator_obstack_base;
2539
2540   bool success;
2541
2542   /* Create the declarator obstack, if necessary.  */
2543   if (!cp_error_declarator)
2544     {
2545       gcc_obstack_init (&declarator_obstack);
2546       /* Create the error declarator.  */
2547       cp_error_declarator = make_declarator (cdk_error);
2548       /* Create the empty parameter list.  */
2549       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2550       /* Remember where the base of the declarator obstack lies.  */
2551       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2552     }
2553
2554   while (true)
2555     {
2556       cp_parser_declaration_seq_opt (parser);
2557
2558       /* If there are no tokens left then all went well.  */
2559       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2560         {
2561           /* Get rid of the token array; we don't need it any more.  */
2562           cp_lexer_destroy (parser->lexer);
2563           parser->lexer = NULL;
2564
2565           /* This file might have been a context that's implicitly extern
2566              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2567           if (parser->implicit_extern_c)
2568             {
2569               pop_lang_context ();
2570               parser->implicit_extern_c = false;
2571             }
2572
2573           /* Finish up.  */
2574           finish_translation_unit ();
2575
2576           success = true;
2577           break;
2578         }
2579       else
2580         {
2581           cp_parser_error (parser, "expected declaration");
2582           success = false;
2583           break;
2584         }
2585     }
2586
2587   /* Make sure the declarator obstack was fully cleaned up.  */
2588   gcc_assert (obstack_next_free (&declarator_obstack)
2589               == declarator_obstack_base);
2590
2591   /* All went well.  */
2592   return success;
2593 }
2594
2595 /* Expressions [gram.expr] */
2596
2597 /* Parse a primary-expression.
2598
2599    primary-expression:
2600      literal
2601      this
2602      ( expression )
2603      id-expression
2604
2605    GNU Extensions:
2606
2607    primary-expression:
2608      ( compound-statement )
2609      __builtin_va_arg ( assignment-expression , type-id )
2610
2611    literal:
2612      __null
2613
2614    Returns a representation of the expression.
2615
2616    *IDK indicates what kind of id-expression (if any) was present.
2617
2618    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2619    used as the operand of a pointer-to-member.  In that case,
2620    *QUALIFYING_CLASS gives the class that is used as the qualifying
2621    class in the pointer-to-member.  */
2622
2623 static tree
2624 cp_parser_primary_expression (cp_parser *parser,
2625                               cp_id_kind *idk,
2626                               tree *qualifying_class)
2627 {
2628   cp_token *token;
2629
2630   /* Assume the primary expression is not an id-expression.  */
2631   *idk = CP_ID_KIND_NONE;
2632   /* And that it cannot be used as pointer-to-member.  */
2633   *qualifying_class = NULL_TREE;
2634
2635   /* Peek at the next token.  */
2636   token = cp_lexer_peek_token (parser->lexer);
2637   switch (token->type)
2638     {
2639       /* literal:
2640            integer-literal
2641            character-literal
2642            floating-literal
2643            string-literal
2644            boolean-literal  */
2645     case CPP_CHAR:
2646     case CPP_WCHAR:
2647     case CPP_NUMBER:
2648       token = cp_lexer_consume_token (parser->lexer);
2649       return token->value;
2650
2651     case CPP_STRING:
2652     case CPP_WSTRING:
2653       /* ??? Should wide strings be allowed when parser->translate_strings_p
2654          is false (i.e. in attributes)?  If not, we can kill the third
2655          argument to cp_parser_string_literal.  */
2656       return cp_parser_string_literal (parser,
2657                                        parser->translate_strings_p,
2658                                        true);
2659
2660     case CPP_OPEN_PAREN:
2661       {
2662         tree expr;
2663         bool saved_greater_than_is_operator_p;
2664
2665         /* Consume the `('.  */
2666         cp_lexer_consume_token (parser->lexer);
2667         /* Within a parenthesized expression, a `>' token is always
2668            the greater-than operator.  */
2669         saved_greater_than_is_operator_p
2670           = parser->greater_than_is_operator_p;
2671         parser->greater_than_is_operator_p = true;
2672         /* If we see `( { ' then we are looking at the beginning of
2673            a GNU statement-expression.  */
2674         if (cp_parser_allow_gnu_extensions_p (parser)
2675             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2676           {
2677             /* Statement-expressions are not allowed by the standard.  */
2678             if (pedantic)
2679               pedwarn ("ISO C++ forbids braced-groups within expressions");
2680
2681             /* And they're not allowed outside of a function-body; you
2682                cannot, for example, write:
2683
2684                  int i = ({ int j = 3; j + 1; });
2685
2686                at class or namespace scope.  */
2687             if (!at_function_scope_p ())
2688               error ("statement-expressions are allowed only inside functions");
2689             /* Start the statement-expression.  */
2690             expr = begin_stmt_expr ();
2691             /* Parse the compound-statement.  */
2692             cp_parser_compound_statement (parser, expr, false);
2693             /* Finish up.  */
2694             expr = finish_stmt_expr (expr, false);
2695           }
2696         else
2697           {
2698             /* Parse the parenthesized expression.  */
2699             expr = cp_parser_expression (parser);
2700             /* Let the front end know that this expression was
2701                enclosed in parentheses. This matters in case, for
2702                example, the expression is of the form `A::B', since
2703                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2704                not.  */
2705             finish_parenthesized_expr (expr);
2706           }
2707         /* The `>' token might be the end of a template-id or
2708            template-parameter-list now.  */
2709         parser->greater_than_is_operator_p
2710           = saved_greater_than_is_operator_p;
2711         /* Consume the `)'.  */
2712         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2713           cp_parser_skip_to_end_of_statement (parser);
2714
2715         return expr;
2716       }
2717
2718     case CPP_KEYWORD:
2719       switch (token->keyword)
2720         {
2721           /* These two are the boolean literals.  */
2722         case RID_TRUE:
2723           cp_lexer_consume_token (parser->lexer);
2724           return boolean_true_node;
2725         case RID_FALSE:
2726           cp_lexer_consume_token (parser->lexer);
2727           return boolean_false_node;
2728
2729           /* The `__null' literal.  */
2730         case RID_NULL:
2731           cp_lexer_consume_token (parser->lexer);
2732           return null_node;
2733
2734           /* Recognize the `this' keyword.  */
2735         case RID_THIS:
2736           cp_lexer_consume_token (parser->lexer);
2737           if (parser->local_variables_forbidden_p)
2738             {
2739               error ("%<this%> may not be used in this context");
2740               return error_mark_node;
2741             }
2742           /* Pointers cannot appear in constant-expressions.  */
2743           if (cp_parser_non_integral_constant_expression (parser,
2744                                                           "`this'"))
2745             return error_mark_node;
2746           return finish_this_expr ();
2747
2748           /* The `operator' keyword can be the beginning of an
2749              id-expression.  */
2750         case RID_OPERATOR:
2751           goto id_expression;
2752
2753         case RID_FUNCTION_NAME:
2754         case RID_PRETTY_FUNCTION_NAME:
2755         case RID_C99_FUNCTION_NAME:
2756           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2757              __func__ are the names of variables -- but they are
2758              treated specially.  Therefore, they are handled here,
2759              rather than relying on the generic id-expression logic
2760              below.  Grammatically, these names are id-expressions.
2761
2762              Consume the token.  */
2763           token = cp_lexer_consume_token (parser->lexer);
2764           /* Look up the name.  */
2765           return finish_fname (token->value);
2766
2767         case RID_VA_ARG:
2768           {
2769             tree expression;
2770             tree type;
2771
2772             /* The `__builtin_va_arg' construct is used to handle
2773                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2774             cp_lexer_consume_token (parser->lexer);
2775             /* Look for the opening `('.  */
2776             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2777             /* Now, parse the assignment-expression.  */
2778             expression = cp_parser_assignment_expression (parser);
2779             /* Look for the `,'.  */
2780             cp_parser_require (parser, CPP_COMMA, "`,'");
2781             /* Parse the type-id.  */
2782             type = cp_parser_type_id (parser);
2783             /* Look for the closing `)'.  */
2784             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2785             /* Using `va_arg' in a constant-expression is not
2786                allowed.  */
2787             if (cp_parser_non_integral_constant_expression (parser,
2788                                                             "`va_arg'"))
2789               return error_mark_node;
2790             return build_x_va_arg (expression, type);
2791           }
2792
2793         case RID_OFFSETOF:
2794           return cp_parser_builtin_offsetof (parser);
2795
2796         default:
2797           cp_parser_error (parser, "expected primary-expression");
2798           return error_mark_node;
2799         }
2800
2801       /* An id-expression can start with either an identifier, a
2802          `::' as the beginning of a qualified-id, or the "operator"
2803          keyword.  */
2804     case CPP_NAME:
2805     case CPP_SCOPE:
2806     case CPP_TEMPLATE_ID:
2807     case CPP_NESTED_NAME_SPECIFIER:
2808       {
2809         tree id_expression;
2810         tree decl;
2811         const char *error_msg;
2812
2813       id_expression:
2814         /* Parse the id-expression.  */
2815         id_expression
2816           = cp_parser_id_expression (parser,
2817                                      /*template_keyword_p=*/false,
2818                                      /*check_dependency_p=*/true,
2819                                      /*template_p=*/NULL,
2820                                      /*declarator_p=*/false);
2821         if (id_expression == error_mark_node)
2822           return error_mark_node;
2823         /* If we have a template-id, then no further lookup is
2824            required.  If the template-id was for a template-class, we
2825            will sometimes have a TYPE_DECL at this point.  */
2826         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2827             || TREE_CODE (id_expression) == TYPE_DECL)
2828           decl = id_expression;
2829         /* Look up the name.  */
2830         else
2831           {
2832             bool ambiguous_p;
2833
2834             decl = cp_parser_lookup_name (parser, id_expression,
2835                                           none_type,
2836                                           /*is_template=*/false,
2837                                           /*is_namespace=*/false,
2838                                           /*check_dependency=*/true,
2839                                           &ambiguous_p);
2840             /* If the lookup was ambiguous, an error will already have
2841                been issued.  */
2842             if (ambiguous_p)
2843               return error_mark_node;
2844             /* If name lookup gives us a SCOPE_REF, then the
2845                qualifying scope was dependent.  Just propagate the
2846                name.  */
2847             if (TREE_CODE (decl) == SCOPE_REF)
2848               {
2849                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2850                   *qualifying_class = TREE_OPERAND (decl, 0);
2851                 return decl;
2852               }
2853             /* Check to see if DECL is a local variable in a context
2854                where that is forbidden.  */
2855             if (parser->local_variables_forbidden_p
2856                 && local_variable_p (decl))
2857               {
2858                 /* It might be that we only found DECL because we are
2859                    trying to be generous with pre-ISO scoping rules.
2860                    For example, consider:
2861
2862                      int i;
2863                      void g() {
2864                        for (int i = 0; i < 10; ++i) {}
2865                        extern void f(int j = i);
2866                      }
2867
2868                    Here, name look up will originally find the out
2869                    of scope `i'.  We need to issue a warning message,
2870                    but then use the global `i'.  */
2871                 decl = check_for_out_of_scope_variable (decl);
2872                 if (local_variable_p (decl))
2873                   {
2874                     error ("local variable %qD may not appear in this context",
2875                            decl);
2876                     return error_mark_node;
2877                   }
2878               }
2879           }
2880
2881         decl = finish_id_expression (id_expression, decl, parser->scope,
2882                                      idk, qualifying_class,
2883                                      parser->integral_constant_expression_p,
2884                                      parser->allow_non_integral_constant_expression_p,
2885                                      &parser->non_integral_constant_expression_p,
2886                                      &error_msg);
2887         if (error_msg)
2888           cp_parser_error (parser, error_msg);
2889         return decl;
2890       }
2891
2892       /* Anything else is an error.  */
2893     default:
2894       cp_parser_error (parser, "expected primary-expression");
2895       return error_mark_node;
2896     }
2897 }
2898
2899 /* Parse an id-expression.
2900
2901    id-expression:
2902      unqualified-id
2903      qualified-id
2904
2905    qualified-id:
2906      :: [opt] nested-name-specifier template [opt] unqualified-id
2907      :: identifier
2908      :: operator-function-id
2909      :: template-id
2910
2911    Return a representation of the unqualified portion of the
2912    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2913    a `::' or nested-name-specifier.
2914
2915    Often, if the id-expression was a qualified-id, the caller will
2916    want to make a SCOPE_REF to represent the qualified-id.  This
2917    function does not do this in order to avoid wastefully creating
2918    SCOPE_REFs when they are not required.
2919
2920    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2921    `template' keyword.
2922
2923    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2924    uninstantiated templates.
2925
2926    If *TEMPLATE_P is non-NULL, it is set to true iff the
2927    `template' keyword is used to explicitly indicate that the entity
2928    named is a template.
2929
2930    If DECLARATOR_P is true, the id-expression is appearing as part of
2931    a declarator, rather than as part of an expression.  */
2932
2933 static tree
2934 cp_parser_id_expression (cp_parser *parser,
2935                          bool template_keyword_p,
2936                          bool check_dependency_p,
2937                          bool *template_p,
2938                          bool declarator_p)
2939 {
2940   bool global_scope_p;
2941   bool nested_name_specifier_p;
2942
2943   /* Assume the `template' keyword was not used.  */
2944   if (template_p)
2945     *template_p = false;
2946
2947   /* Look for the optional `::' operator.  */
2948   global_scope_p
2949     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2950        != NULL_TREE);
2951   /* Look for the optional nested-name-specifier.  */
2952   nested_name_specifier_p
2953     = (cp_parser_nested_name_specifier_opt (parser,
2954                                             /*typename_keyword_p=*/false,
2955                                             check_dependency_p,
2956                                             /*type_p=*/false,
2957                                             declarator_p)
2958        != NULL_TREE);
2959   /* If there is a nested-name-specifier, then we are looking at
2960      the first qualified-id production.  */
2961   if (nested_name_specifier_p)
2962     {
2963       tree saved_scope;
2964       tree saved_object_scope;
2965       tree saved_qualifying_scope;
2966       tree unqualified_id;
2967       bool is_template;
2968
2969       /* See if the next token is the `template' keyword.  */
2970       if (!template_p)
2971         template_p = &is_template;
2972       *template_p = cp_parser_optional_template_keyword (parser);
2973       /* Name lookup we do during the processing of the
2974          unqualified-id might obliterate SCOPE.  */
2975       saved_scope = parser->scope;
2976       saved_object_scope = parser->object_scope;
2977       saved_qualifying_scope = parser->qualifying_scope;
2978       /* Process the final unqualified-id.  */
2979       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2980                                                  check_dependency_p,
2981                                                  declarator_p);
2982       /* Restore the SAVED_SCOPE for our caller.  */
2983       parser->scope = saved_scope;
2984       parser->object_scope = saved_object_scope;
2985       parser->qualifying_scope = saved_qualifying_scope;
2986
2987       return unqualified_id;
2988     }
2989   /* Otherwise, if we are in global scope, then we are looking at one
2990      of the other qualified-id productions.  */
2991   else if (global_scope_p)
2992     {
2993       cp_token *token;
2994       tree id;
2995
2996       /* Peek at the next token.  */
2997       token = cp_lexer_peek_token (parser->lexer);
2998
2999       /* If it's an identifier, and the next token is not a "<", then
3000          we can avoid the template-id case.  This is an optimization
3001          for this common case.  */
3002       if (token->type == CPP_NAME
3003           && !cp_parser_nth_token_starts_template_argument_list_p
3004                (parser, 2))
3005         return cp_parser_identifier (parser);
3006
3007       cp_parser_parse_tentatively (parser);
3008       /* Try a template-id.  */
3009       id = cp_parser_template_id (parser,
3010                                   /*template_keyword_p=*/false,
3011                                   /*check_dependency_p=*/true,
3012                                   declarator_p);
3013       /* If that worked, we're done.  */
3014       if (cp_parser_parse_definitely (parser))
3015         return id;
3016
3017       /* Peek at the next token.  (Changes in the token buffer may
3018          have invalidated the pointer obtained above.)  */
3019       token = cp_lexer_peek_token (parser->lexer);
3020
3021       switch (token->type)
3022         {
3023         case CPP_NAME:
3024           return cp_parser_identifier (parser);
3025
3026         case CPP_KEYWORD:
3027           if (token->keyword == RID_OPERATOR)
3028             return cp_parser_operator_function_id (parser);
3029           /* Fall through.  */
3030
3031         default:
3032           cp_parser_error (parser, "expected id-expression");
3033           return error_mark_node;
3034         }
3035     }
3036   else
3037     return cp_parser_unqualified_id (parser, template_keyword_p,
3038                                      /*check_dependency_p=*/true,
3039                                      declarator_p);
3040 }
3041
3042 /* Parse an unqualified-id.
3043
3044    unqualified-id:
3045      identifier
3046      operator-function-id
3047      conversion-function-id
3048      ~ class-name
3049      template-id
3050
3051    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3052    keyword, in a construct like `A::template ...'.
3053
3054    Returns a representation of unqualified-id.  For the `identifier'
3055    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3056    production a BIT_NOT_EXPR is returned; the operand of the
3057    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3058    other productions, see the documentation accompanying the
3059    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3060    names are looked up in uninstantiated templates.  If DECLARATOR_P
3061    is true, the unqualified-id is appearing as part of a declarator,
3062    rather than as part of an expression.  */
3063
3064 static tree
3065 cp_parser_unqualified_id (cp_parser* parser,
3066                           bool template_keyword_p,
3067                           bool check_dependency_p,
3068                           bool declarator_p)
3069 {
3070   cp_token *token;
3071
3072   /* Peek at the next token.  */
3073   token = cp_lexer_peek_token (parser->lexer);
3074
3075   switch (token->type)
3076     {
3077     case CPP_NAME:
3078       {
3079         tree id;
3080
3081         /* We don't know yet whether or not this will be a
3082            template-id.  */
3083         cp_parser_parse_tentatively (parser);
3084         /* Try a template-id.  */
3085         id = cp_parser_template_id (parser, template_keyword_p,
3086                                     check_dependency_p,
3087                                     declarator_p);
3088         /* If it worked, we're done.  */
3089         if (cp_parser_parse_definitely (parser))
3090           return id;
3091         /* Otherwise, it's an ordinary identifier.  */
3092         return cp_parser_identifier (parser);
3093       }
3094
3095     case CPP_TEMPLATE_ID:
3096       return cp_parser_template_id (parser, template_keyword_p,
3097                                     check_dependency_p,
3098                                     declarator_p);
3099
3100     case CPP_COMPL:
3101       {
3102         tree type_decl;
3103         tree qualifying_scope;
3104         tree object_scope;
3105         tree scope;
3106
3107         /* Consume the `~' token.  */
3108         cp_lexer_consume_token (parser->lexer);
3109         /* Parse the class-name.  The standard, as written, seems to
3110            say that:
3111
3112              template <typename T> struct S { ~S (); };
3113              template <typename T> S<T>::~S() {}
3114
3115            is invalid, since `~' must be followed by a class-name, but
3116            `S<T>' is dependent, and so not known to be a class.
3117            That's not right; we need to look in uninstantiated
3118            templates.  A further complication arises from:
3119
3120              template <typename T> void f(T t) {
3121                t.T::~T();
3122              }
3123
3124            Here, it is not possible to look up `T' in the scope of `T'
3125            itself.  We must look in both the current scope, and the
3126            scope of the containing complete expression.
3127
3128            Yet another issue is:
3129
3130              struct S {
3131                int S;
3132                ~S();
3133              };
3134
3135              S::~S() {}
3136
3137            The standard does not seem to say that the `S' in `~S'
3138            should refer to the type `S' and not the data member
3139            `S::S'.  */
3140
3141         /* DR 244 says that we look up the name after the "~" in the
3142            same scope as we looked up the qualifying name.  That idea
3143            isn't fully worked out; it's more complicated than that.  */
3144         scope = parser->scope;
3145         object_scope = parser->object_scope;
3146         qualifying_scope = parser->qualifying_scope;
3147
3148         /* If the name is of the form "X::~X" it's OK.  */
3149         if (scope && TYPE_P (scope)
3150             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3151             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3152                 == CPP_OPEN_PAREN)
3153             && (cp_lexer_peek_token (parser->lexer)->value
3154                 == TYPE_IDENTIFIER (scope)))
3155           {
3156             cp_lexer_consume_token (parser->lexer);
3157             return build_nt (BIT_NOT_EXPR, scope);
3158           }
3159
3160         /* If there was an explicit qualification (S::~T), first look
3161            in the scope given by the qualification (i.e., S).  */
3162         if (scope)
3163           {
3164             cp_parser_parse_tentatively (parser);
3165             type_decl = cp_parser_class_name (parser,
3166                                               /*typename_keyword_p=*/false,
3167                                               /*template_keyword_p=*/false,
3168                                               none_type,
3169                                               /*check_dependency=*/false,
3170                                               /*class_head_p=*/false,
3171                                               declarator_p);
3172             if (cp_parser_parse_definitely (parser))
3173               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3174           }
3175         /* In "N::S::~S", look in "N" as well.  */
3176         if (scope && qualifying_scope)
3177           {
3178             cp_parser_parse_tentatively (parser);
3179             parser->scope = qualifying_scope;
3180             parser->object_scope = NULL_TREE;
3181             parser->qualifying_scope = NULL_TREE;
3182             type_decl
3183               = cp_parser_class_name (parser,
3184                                       /*typename_keyword_p=*/false,
3185                                       /*template_keyword_p=*/false,
3186                                       none_type,
3187                                       /*check_dependency=*/false,
3188                                       /*class_head_p=*/false,
3189                                       declarator_p);
3190             if (cp_parser_parse_definitely (parser))
3191               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3192           }
3193         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3194         else if (object_scope)
3195           {
3196             cp_parser_parse_tentatively (parser);
3197             parser->scope = object_scope;
3198             parser->object_scope = NULL_TREE;
3199             parser->qualifying_scope = NULL_TREE;
3200             type_decl
3201               = cp_parser_class_name (parser,
3202                                       /*typename_keyword_p=*/false,
3203                                       /*template_keyword_p=*/false,
3204                                       none_type,
3205                                       /*check_dependency=*/false,
3206                                       /*class_head_p=*/false,
3207                                       declarator_p);
3208             if (cp_parser_parse_definitely (parser))
3209               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3210           }
3211         /* Look in the surrounding context.  */
3212         parser->scope = NULL_TREE;
3213         parser->object_scope = NULL_TREE;
3214         parser->qualifying_scope = NULL_TREE;
3215         type_decl
3216           = cp_parser_class_name (parser,
3217                                   /*typename_keyword_p=*/false,
3218                                   /*template_keyword_p=*/false,
3219                                   none_type,
3220                                   /*check_dependency=*/false,
3221                                   /*class_head_p=*/false,
3222                                   declarator_p);
3223         /* If an error occurred, assume that the name of the
3224            destructor is the same as the name of the qualifying
3225            class.  That allows us to keep parsing after running
3226            into ill-formed destructor names.  */
3227         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3228           return build_nt (BIT_NOT_EXPR, scope);
3229         else if (type_decl == error_mark_node)
3230           return error_mark_node;
3231
3232         /* [class.dtor]
3233
3234            A typedef-name that names a class shall not be used as the
3235            identifier in the declarator for a destructor declaration.  */
3236         if (declarator_p
3237             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3238             && !DECL_SELF_REFERENCE_P (type_decl))
3239           error ("typedef-name %qD used as destructor declarator",
3240                  type_decl);
3241
3242         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3243       }
3244
3245     case CPP_KEYWORD:
3246       if (token->keyword == RID_OPERATOR)
3247         {
3248           tree id;
3249
3250           /* This could be a template-id, so we try that first.  */
3251           cp_parser_parse_tentatively (parser);
3252           /* Try a template-id.  */
3253           id = cp_parser_template_id (parser, template_keyword_p,
3254                                       /*check_dependency_p=*/true,
3255                                       declarator_p);
3256           /* If that worked, we're done.  */
3257           if (cp_parser_parse_definitely (parser))
3258             return id;
3259           /* We still don't know whether we're looking at an
3260              operator-function-id or a conversion-function-id.  */
3261           cp_parser_parse_tentatively (parser);
3262           /* Try an operator-function-id.  */
3263           id = cp_parser_operator_function_id (parser);
3264           /* If that didn't work, try a conversion-function-id.  */
3265           if (!cp_parser_parse_definitely (parser))
3266             id = cp_parser_conversion_function_id (parser);
3267
3268           return id;
3269         }
3270       /* Fall through.  */
3271
3272     default:
3273       cp_parser_error (parser, "expected unqualified-id");
3274       return error_mark_node;
3275     }
3276 }
3277
3278 /* Parse an (optional) nested-name-specifier.
3279
3280    nested-name-specifier:
3281      class-or-namespace-name :: nested-name-specifier [opt]
3282      class-or-namespace-name :: template nested-name-specifier [opt]
3283
3284    PARSER->SCOPE should be set appropriately before this function is
3285    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3286    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3287    in name lookups.
3288
3289    Sets PARSER->SCOPE to the class (TYPE) or namespace
3290    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3291    it unchanged if there is no nested-name-specifier.  Returns the new
3292    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3293
3294    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3295    part of a declaration and/or decl-specifier.  */
3296
3297 static tree
3298 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3299                                      bool typename_keyword_p,
3300                                      bool check_dependency_p,
3301                                      bool type_p,
3302                                      bool is_declaration)
3303 {
3304   bool success = false;
3305   tree access_check = NULL_TREE;
3306   cp_token_position start = 0;
3307   cp_token *token;
3308
3309   /* If the next token corresponds to a nested name specifier, there
3310      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3311      false, it may have been true before, in which case something
3312      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3313      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3314      CHECK_DEPENDENCY_P is false, we have to fall through into the
3315      main loop.  */
3316   if (check_dependency_p
3317       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3318     {
3319       cp_parser_pre_parsed_nested_name_specifier (parser);
3320       return parser->scope;
3321     }
3322
3323   /* Remember where the nested-name-specifier starts.  */
3324   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3325     start = cp_lexer_token_position (parser->lexer, false);
3326
3327   push_deferring_access_checks (dk_deferred);
3328
3329   while (true)
3330     {
3331       tree new_scope;
3332       tree old_scope;
3333       tree saved_qualifying_scope;
3334       bool template_keyword_p;
3335
3336       /* Spot cases that cannot be the beginning of a
3337          nested-name-specifier.  */
3338       token = cp_lexer_peek_token (parser->lexer);
3339
3340       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3341          the already parsed nested-name-specifier.  */
3342       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3343         {
3344           /* Grab the nested-name-specifier and continue the loop.  */
3345           cp_parser_pre_parsed_nested_name_specifier (parser);
3346           success = true;
3347           continue;
3348         }
3349
3350       /* Spot cases that cannot be the beginning of a
3351          nested-name-specifier.  On the second and subsequent times
3352          through the loop, we look for the `template' keyword.  */
3353       if (success && token->keyword == RID_TEMPLATE)
3354         ;
3355       /* A template-id can start a nested-name-specifier.  */
3356       else if (token->type == CPP_TEMPLATE_ID)
3357         ;
3358       else
3359         {
3360           /* If the next token is not an identifier, then it is
3361              definitely not a class-or-namespace-name.  */
3362           if (token->type != CPP_NAME)
3363             break;
3364           /* If the following token is neither a `<' (to begin a
3365              template-id), nor a `::', then we are not looking at a
3366              nested-name-specifier.  */
3367           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3368           if (token->type != CPP_SCOPE
3369               && !cp_parser_nth_token_starts_template_argument_list_p
3370                   (parser, 2))
3371             break;
3372         }
3373
3374       /* The nested-name-specifier is optional, so we parse
3375          tentatively.  */
3376       cp_parser_parse_tentatively (parser);
3377
3378       /* Look for the optional `template' keyword, if this isn't the
3379          first time through the loop.  */
3380       if (success)
3381         template_keyword_p = cp_parser_optional_template_keyword (parser);
3382       else
3383         template_keyword_p = false;
3384
3385       /* Save the old scope since the name lookup we are about to do
3386          might destroy it.  */
3387       old_scope = parser->scope;
3388       saved_qualifying_scope = parser->qualifying_scope;
3389       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3390          look up names in "X<T>::I" in order to determine that "Y" is
3391          a template.  So, if we have a typename at this point, we make
3392          an effort to look through it.  */
3393       if (is_declaration 
3394           && !typename_keyword_p
3395           && parser->scope 
3396           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3397         parser->scope = resolve_typename_type (parser->scope, 
3398                                                /*only_current_p=*/false);
3399       /* Parse the qualifying entity.  */
3400       new_scope
3401         = cp_parser_class_or_namespace_name (parser,
3402                                              typename_keyword_p,
3403                                              template_keyword_p,
3404                                              check_dependency_p,
3405                                              type_p,
3406                                              is_declaration);
3407       /* Look for the `::' token.  */
3408       cp_parser_require (parser, CPP_SCOPE, "`::'");
3409
3410       /* If we found what we wanted, we keep going; otherwise, we're
3411          done.  */
3412       if (!cp_parser_parse_definitely (parser))
3413         {
3414           bool error_p = false;
3415
3416           /* Restore the OLD_SCOPE since it was valid before the
3417              failed attempt at finding the last
3418              class-or-namespace-name.  */
3419           parser->scope = old_scope;
3420           parser->qualifying_scope = saved_qualifying_scope;
3421           /* If the next token is an identifier, and the one after
3422              that is a `::', then any valid interpretation would have
3423              found a class-or-namespace-name.  */
3424           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3425                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3426                      == CPP_SCOPE)
3427                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3428                      != CPP_COMPL))
3429             {
3430               token = cp_lexer_consume_token (parser->lexer);
3431               if (!error_p)
3432                 {
3433                   tree decl;
3434
3435                   decl = cp_parser_lookup_name_simple (parser, token->value);
3436                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3437                     error ("%qD used without template parameters", decl);
3438                   else
3439                     cp_parser_name_lookup_error
3440                       (parser, token->value, decl,
3441                        "is not a class or namespace");
3442                   parser->scope = NULL_TREE;
3443                   error_p = true;
3444                   /* Treat this as a successful nested-name-specifier
3445                      due to:
3446
3447                      [basic.lookup.qual]
3448
3449                      If the name found is not a class-name (clause
3450                      _class_) or namespace-name (_namespace.def_), the
3451                      program is ill-formed.  */
3452                   success = true;
3453                 }
3454               cp_lexer_consume_token (parser->lexer);
3455             }
3456           break;
3457         }
3458
3459       /* We've found one valid nested-name-specifier.  */
3460       success = true;
3461       /* Make sure we look in the right scope the next time through
3462          the loop.  */
3463       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3464                        ? TREE_TYPE (new_scope)
3465                        : new_scope);
3466       /* If it is a class scope, try to complete it; we are about to
3467          be looking up names inside the class.  */
3468       if (TYPE_P (parser->scope)
3469           /* Since checking types for dependency can be expensive,
3470              avoid doing it if the type is already complete.  */
3471           && !COMPLETE_TYPE_P (parser->scope)
3472           /* Do not try to complete dependent types.  */
3473           && !dependent_type_p (parser->scope))
3474         complete_type (parser->scope);
3475     }
3476
3477   /* Retrieve any deferred checks.  Do not pop this access checks yet
3478      so the memory will not be reclaimed during token replacing below.  */
3479   access_check = get_deferred_access_checks ();
3480
3481   /* If parsing tentatively, replace the sequence of tokens that makes
3482      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3483      token.  That way, should we re-parse the token stream, we will
3484      not have to repeat the effort required to do the parse, nor will
3485      we issue duplicate error messages.  */
3486   if (success && start)
3487     {
3488       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3489       
3490       /* Reset the contents of the START token.  */
3491       token->type = CPP_NESTED_NAME_SPECIFIER;
3492       token->value = build_tree_list (access_check, parser->scope);
3493       TREE_TYPE (token->value) = parser->qualifying_scope;
3494       token->keyword = RID_MAX;
3495       
3496       /* Purge all subsequent tokens.  */
3497       cp_lexer_purge_tokens_after (parser->lexer, start);
3498     }
3499
3500   pop_deferring_access_checks ();
3501   return success ? parser->scope : NULL_TREE;
3502 }
3503
3504 /* Parse a nested-name-specifier.  See
3505    cp_parser_nested_name_specifier_opt for details.  This function
3506    behaves identically, except that it will an issue an error if no
3507    nested-name-specifier is present, and it will return
3508    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3509    is present.  */
3510
3511 static tree
3512 cp_parser_nested_name_specifier (cp_parser *parser,
3513                                  bool typename_keyword_p,
3514                                  bool check_dependency_p,
3515                                  bool type_p,
3516                                  bool is_declaration)
3517 {
3518   tree scope;
3519
3520   /* Look for the nested-name-specifier.  */
3521   scope = cp_parser_nested_name_specifier_opt (parser,
3522                                                typename_keyword_p,
3523                                                check_dependency_p,
3524                                                type_p,
3525                                                is_declaration);
3526   /* If it was not present, issue an error message.  */
3527   if (!scope)
3528     {
3529       cp_parser_error (parser, "expected nested-name-specifier");
3530       parser->scope = NULL_TREE;
3531       return error_mark_node;
3532     }
3533
3534   return scope;
3535 }
3536
3537 /* Parse a class-or-namespace-name.
3538
3539    class-or-namespace-name:
3540      class-name
3541      namespace-name
3542
3543    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3544    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3545    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3546    TYPE_P is TRUE iff the next name should be taken as a class-name,
3547    even the same name is declared to be another entity in the same
3548    scope.
3549
3550    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3551    specified by the class-or-namespace-name.  If neither is found the
3552    ERROR_MARK_NODE is returned.  */
3553
3554 static tree
3555 cp_parser_class_or_namespace_name (cp_parser *parser,
3556                                    bool typename_keyword_p,
3557                                    bool template_keyword_p,
3558                                    bool check_dependency_p,
3559                                    bool type_p,
3560                                    bool is_declaration)
3561 {
3562   tree saved_scope;
3563   tree saved_qualifying_scope;
3564   tree saved_object_scope;
3565   tree scope;
3566   bool only_class_p;
3567
3568   /* Before we try to parse the class-name, we must save away the
3569      current PARSER->SCOPE since cp_parser_class_name will destroy
3570      it.  */
3571   saved_scope = parser->scope;
3572   saved_qualifying_scope = parser->qualifying_scope;
3573   saved_object_scope = parser->object_scope;
3574   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3575      there is no need to look for a namespace-name.  */
3576   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3577   if (!only_class_p)
3578     cp_parser_parse_tentatively (parser);
3579   scope = cp_parser_class_name (parser,
3580                                 typename_keyword_p,
3581                                 template_keyword_p,
3582                                 type_p ? class_type : none_type,
3583                                 check_dependency_p,
3584                                 /*class_head_p=*/false,
3585                                 is_declaration);
3586   /* If that didn't work, try for a namespace-name.  */
3587   if (!only_class_p && !cp_parser_parse_definitely (parser))
3588     {
3589       /* Restore the saved scope.  */
3590       parser->scope = saved_scope;
3591       parser->qualifying_scope = saved_qualifying_scope;
3592       parser->object_scope = saved_object_scope;
3593       /* If we are not looking at an identifier followed by the scope
3594          resolution operator, then this is not part of a
3595          nested-name-specifier.  (Note that this function is only used
3596          to parse the components of a nested-name-specifier.)  */
3597       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3598           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3599         return error_mark_node;
3600       scope = cp_parser_namespace_name (parser);
3601     }
3602
3603   return scope;
3604 }
3605
3606 /* Parse a postfix-expression.
3607
3608    postfix-expression:
3609      primary-expression
3610      postfix-expression [ expression ]
3611      postfix-expression ( expression-list [opt] )
3612      simple-type-specifier ( expression-list [opt] )
3613      typename :: [opt] nested-name-specifier identifier
3614        ( expression-list [opt] )
3615      typename :: [opt] nested-name-specifier template [opt] template-id
3616        ( expression-list [opt] )
3617      postfix-expression . template [opt] id-expression
3618      postfix-expression -> template [opt] id-expression
3619      postfix-expression . pseudo-destructor-name
3620      postfix-expression -> pseudo-destructor-name
3621      postfix-expression ++
3622      postfix-expression --
3623      dynamic_cast < type-id > ( expression )
3624      static_cast < type-id > ( expression )
3625      reinterpret_cast < type-id > ( expression )
3626      const_cast < type-id > ( expression )
3627      typeid ( expression )
3628      typeid ( type-id )
3629
3630    GNU Extension:
3631
3632    postfix-expression:
3633      ( type-id ) { initializer-list , [opt] }
3634
3635    This extension is a GNU version of the C99 compound-literal
3636    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3637    but they are essentially the same concept.)
3638
3639    If ADDRESS_P is true, the postfix expression is the operand of the
3640    `&' operator.
3641
3642    Returns a representation of the expression.  */
3643
3644 static tree
3645 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3646 {
3647   cp_token *token;
3648   enum rid keyword;
3649   cp_id_kind idk = CP_ID_KIND_NONE;
3650   tree postfix_expression = NULL_TREE;
3651   /* Non-NULL only if the current postfix-expression can be used to
3652      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3653      class used to qualify the member.  */
3654   tree qualifying_class = NULL_TREE;
3655
3656   /* Peek at the next token.  */
3657   token = cp_lexer_peek_token (parser->lexer);
3658   /* Some of the productions are determined by keywords.  */
3659   keyword = token->keyword;
3660   switch (keyword)
3661     {
3662     case RID_DYNCAST:
3663     case RID_STATCAST:
3664     case RID_REINTCAST:
3665     case RID_CONSTCAST:
3666       {
3667         tree type;
3668         tree expression;
3669         const char *saved_message;
3670
3671         /* All of these can be handled in the same way from the point
3672            of view of parsing.  Begin by consuming the token
3673            identifying the cast.  */
3674         cp_lexer_consume_token (parser->lexer);
3675
3676         /* New types cannot be defined in the cast.  */
3677         saved_message = parser->type_definition_forbidden_message;
3678         parser->type_definition_forbidden_message
3679           = "types may not be defined in casts";
3680
3681         /* Look for the opening `<'.  */
3682         cp_parser_require (parser, CPP_LESS, "`<'");
3683         /* Parse the type to which we are casting.  */
3684         type = cp_parser_type_id (parser);
3685         /* Look for the closing `>'.  */
3686         cp_parser_require (parser, CPP_GREATER, "`>'");
3687         /* Restore the old message.  */
3688         parser->type_definition_forbidden_message = saved_message;
3689
3690         /* And the expression which is being cast.  */
3691         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3692         expression = cp_parser_expression (parser);
3693         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3694
3695         /* Only type conversions to integral or enumeration types
3696            can be used in constant-expressions.  */
3697         if (parser->integral_constant_expression_p
3698             && !dependent_type_p (type)
3699             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3700             && (cp_parser_non_integral_constant_expression
3701                 (parser,
3702                  "a cast to a type other than an integral or "
3703                  "enumeration type")))
3704           return error_mark_node;
3705
3706         switch (keyword)
3707           {
3708           case RID_DYNCAST:
3709             postfix_expression
3710               = build_dynamic_cast (type, expression);
3711             break;
3712           case RID_STATCAST:
3713             postfix_expression
3714               = build_static_cast (type, expression);
3715             break;
3716           case RID_REINTCAST:
3717             postfix_expression
3718               = build_reinterpret_cast (type, expression);
3719             break;
3720           case RID_CONSTCAST:
3721             postfix_expression
3722               = build_const_cast (type, expression);
3723             break;
3724           default:
3725             gcc_unreachable ();
3726           }
3727       }
3728       break;
3729
3730     case RID_TYPEID:
3731       {
3732         tree type;
3733         const char *saved_message;
3734         bool saved_in_type_id_in_expr_p;
3735
3736         /* Consume the `typeid' token.  */
3737         cp_lexer_consume_token (parser->lexer);
3738         /* Look for the `(' token.  */
3739         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3740         /* Types cannot be defined in a `typeid' expression.  */
3741         saved_message = parser->type_definition_forbidden_message;
3742         parser->type_definition_forbidden_message
3743           = "types may not be defined in a `typeid\' expression";
3744         /* We can't be sure yet whether we're looking at a type-id or an
3745            expression.  */
3746         cp_parser_parse_tentatively (parser);
3747         /* Try a type-id first.  */
3748         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3749         parser->in_type_id_in_expr_p = true;
3750         type = cp_parser_type_id (parser);
3751         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3752         /* Look for the `)' token.  Otherwise, we can't be sure that
3753            we're not looking at an expression: consider `typeid (int
3754            (3))', for example.  */
3755         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3756         /* If all went well, simply lookup the type-id.  */
3757         if (cp_parser_parse_definitely (parser))
3758           postfix_expression = get_typeid (type);
3759         /* Otherwise, fall back to the expression variant.  */
3760         else
3761           {
3762             tree expression;
3763
3764             /* Look for an expression.  */
3765             expression = cp_parser_expression (parser);
3766             /* Compute its typeid.  */
3767             postfix_expression = build_typeid (expression);
3768             /* Look for the `)' token.  */
3769             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3770           }
3771         /* `typeid' may not appear in an integral constant expression.  */
3772         if (cp_parser_non_integral_constant_expression(parser,
3773                                                        "`typeid' operator"))
3774           return error_mark_node;
3775         /* Restore the saved message.  */
3776         parser->type_definition_forbidden_message = saved_message;
3777       }
3778       break;
3779
3780     case RID_TYPENAME:
3781       {
3782         bool template_p = false;
3783         tree id;
3784         tree type;
3785
3786         /* Consume the `typename' token.  */
3787         cp_lexer_consume_token (parser->lexer);
3788         /* Look for the optional `::' operator.  */
3789         cp_parser_global_scope_opt (parser,
3790                                     /*current_scope_valid_p=*/false);
3791         /* Look for the nested-name-specifier.  */
3792         cp_parser_nested_name_specifier (parser,
3793                                          /*typename_keyword_p=*/true,
3794                                          /*check_dependency_p=*/true,
3795                                          /*type_p=*/true,
3796                                          /*is_declaration=*/true);
3797         /* Look for the optional `template' keyword.  */
3798         template_p = cp_parser_optional_template_keyword (parser);
3799         /* We don't know whether we're looking at a template-id or an
3800            identifier.  */
3801         cp_parser_parse_tentatively (parser);
3802         /* Try a template-id.  */
3803         id = cp_parser_template_id (parser, template_p,
3804                                     /*check_dependency_p=*/true,
3805                                     /*is_declaration=*/true);
3806         /* If that didn't work, try an identifier.  */
3807         if (!cp_parser_parse_definitely (parser))
3808           id = cp_parser_identifier (parser);
3809         /* If we look up a template-id in a non-dependent qualifying
3810            scope, there's no need to create a dependent type.  */
3811         if (TREE_CODE (id) == TYPE_DECL
3812             && !dependent_type_p (parser->scope))
3813           type = TREE_TYPE (id);
3814         /* Create a TYPENAME_TYPE to represent the type to which the
3815            functional cast is being performed.  */
3816         else
3817           type = make_typename_type (parser->scope, id,
3818                                      typename_type,
3819                                      /*complain=*/1);
3820
3821         postfix_expression = cp_parser_functional_cast (parser, type);
3822       }
3823       break;
3824
3825     default:
3826       {
3827         tree type;
3828
3829         /* If the next thing is a simple-type-specifier, we may be
3830            looking at a functional cast.  We could also be looking at
3831            an id-expression.  So, we try the functional cast, and if
3832            that doesn't work we fall back to the primary-expression.  */
3833         cp_parser_parse_tentatively (parser);
3834         /* Look for the simple-type-specifier.  */
3835         type = cp_parser_simple_type_specifier (parser,
3836                                                 /*decl_specs=*/NULL,
3837                                                 CP_PARSER_FLAGS_NONE);
3838         /* Parse the cast itself.  */
3839         if (!cp_parser_error_occurred (parser))
3840           postfix_expression
3841             = cp_parser_functional_cast (parser, type);
3842         /* If that worked, we're done.  */
3843         if (cp_parser_parse_definitely (parser))
3844           break;
3845
3846         /* If the functional-cast didn't work out, try a
3847            compound-literal.  */
3848         if (cp_parser_allow_gnu_extensions_p (parser)
3849             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3850           {
3851             tree initializer_list = NULL_TREE;
3852             bool saved_in_type_id_in_expr_p;
3853
3854             cp_parser_parse_tentatively (parser);
3855             /* Consume the `('.  */
3856             cp_lexer_consume_token (parser->lexer);
3857             /* Parse the type.  */
3858             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3859             parser->in_type_id_in_expr_p = true;
3860             type = cp_parser_type_id (parser);
3861             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3862             /* Look for the `)'.  */
3863             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3864             /* Look for the `{'.  */
3865             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3866             /* If things aren't going well, there's no need to
3867                keep going.  */
3868             if (!cp_parser_error_occurred (parser))
3869               {
3870                 bool non_constant_p;
3871                 /* Parse the initializer-list.  */
3872                 initializer_list
3873                   = cp_parser_initializer_list (parser, &non_constant_p);
3874                 /* Allow a trailing `,'.  */
3875                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3876                   cp_lexer_consume_token (parser->lexer);
3877                 /* Look for the final `}'.  */
3878                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3879               }
3880             /* If that worked, we're definitely looking at a
3881                compound-literal expression.  */
3882             if (cp_parser_parse_definitely (parser))
3883               {
3884                 /* Warn the user that a compound literal is not
3885                    allowed in standard C++.  */
3886                 if (pedantic)
3887                   pedwarn ("ISO C++ forbids compound-literals");
3888                 /* Form the representation of the compound-literal.  */
3889                 postfix_expression
3890                   = finish_compound_literal (type, initializer_list);
3891                 break;
3892               }
3893           }
3894
3895         /* It must be a primary-expression.  */
3896         postfix_expression = cp_parser_primary_expression (parser,
3897                                                            &idk,
3898                                                            &qualifying_class);
3899       }
3900       break;
3901     }
3902
3903   /* If we were avoiding committing to the processing of a
3904      qualified-id until we knew whether or not we had a
3905      pointer-to-member, we now know.  */
3906   if (qualifying_class)
3907     {
3908       bool done;
3909
3910       /* Peek at the next token.  */
3911       token = cp_lexer_peek_token (parser->lexer);
3912       done = (token->type != CPP_OPEN_SQUARE
3913               && token->type != CPP_OPEN_PAREN
3914               && token->type != CPP_DOT
3915               && token->type != CPP_DEREF
3916               && token->type != CPP_PLUS_PLUS
3917               && token->type != CPP_MINUS_MINUS);
3918
3919       postfix_expression = finish_qualified_id_expr (qualifying_class,
3920                                                      postfix_expression,
3921                                                      done,
3922                                                      address_p);
3923       if (done)
3924         return postfix_expression;
3925     }
3926
3927   /* Keep looping until the postfix-expression is complete.  */
3928   while (true)
3929     {
3930       if (idk == CP_ID_KIND_UNQUALIFIED
3931           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3932           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3933         /* It is not a Koenig lookup function call.  */
3934         postfix_expression
3935           = unqualified_name_lookup_error (postfix_expression);
3936
3937       /* Peek at the next token.  */
3938       token = cp_lexer_peek_token (parser->lexer);
3939
3940       switch (token->type)
3941         {
3942         case CPP_OPEN_SQUARE:
3943           postfix_expression
3944             = cp_parser_postfix_open_square_expression (parser,
3945                                                         postfix_expression,
3946                                                         false);
3947           idk = CP_ID_KIND_NONE;
3948           break;
3949
3950         case CPP_OPEN_PAREN:
3951           /* postfix-expression ( expression-list [opt] ) */
3952           {
3953             bool koenig_p;
3954             tree args = (cp_parser_parenthesized_expression_list
3955                          (parser, false, /*non_constant_p=*/NULL));
3956
3957             if (args == error_mark_node)
3958               {
3959                 postfix_expression = error_mark_node;
3960                 break;
3961               }
3962
3963             /* Function calls are not permitted in
3964                constant-expressions.  */
3965             if (cp_parser_non_integral_constant_expression (parser,
3966                                                             "a function call"))
3967               {
3968                 postfix_expression = error_mark_node;
3969                 break;
3970               }
3971
3972             koenig_p = false;
3973             if (idk == CP_ID_KIND_UNQUALIFIED)
3974               {
3975                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3976                   {
3977                     if (args)
3978                       {
3979                         koenig_p = true;
3980                         postfix_expression
3981                           = perform_koenig_lookup (postfix_expression, args);
3982                       }
3983                     else
3984                       postfix_expression
3985                         = unqualified_fn_lookup_error (postfix_expression);
3986                   }
3987                 /* We do not perform argument-dependent lookup if
3988                    normal lookup finds a non-function, in accordance
3989                    with the expected resolution of DR 218.  */
3990                 else if (args && is_overloaded_fn (postfix_expression))
3991                   {
3992                     tree fn = get_first_fn (postfix_expression);
3993
3994                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3995                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3996
3997                     /* Only do argument dependent lookup if regular
3998                        lookup does not find a set of member functions.
3999                        [basic.lookup.koenig]/2a  */
4000                     if (!DECL_FUNCTION_MEMBER_P (fn))
4001                       {
4002                         koenig_p = true;
4003                         postfix_expression
4004                           = perform_koenig_lookup (postfix_expression, args);
4005                       }
4006                   }
4007               }
4008
4009             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4010               {
4011                 tree instance = TREE_OPERAND (postfix_expression, 0);
4012                 tree fn = TREE_OPERAND (postfix_expression, 1);
4013
4014                 if (processing_template_decl
4015                     && (type_dependent_expression_p (instance)
4016                         || (!BASELINK_P (fn)
4017                             && TREE_CODE (fn) != FIELD_DECL)
4018                         || type_dependent_expression_p (fn)
4019                         || any_type_dependent_arguments_p (args)))
4020                   {
4021                     postfix_expression
4022                       = build_min_nt (CALL_EXPR, postfix_expression,
4023                                       args, NULL_TREE);
4024                     break;
4025                   }
4026
4027                 if (BASELINK_P (fn))
4028                   postfix_expression
4029                     = (build_new_method_call
4030                        (instance, fn, args, NULL_TREE,
4031                         (idk == CP_ID_KIND_QUALIFIED
4032                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4033                 else
4034                   postfix_expression
4035                     = finish_call_expr (postfix_expression, args,
4036                                         /*disallow_virtual=*/false,
4037                                         /*koenig_p=*/false);
4038               }
4039             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4040                      || TREE_CODE (postfix_expression) == MEMBER_REF
4041                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4042               postfix_expression = (build_offset_ref_call_from_tree
4043                                     (postfix_expression, args));
4044             else if (idk == CP_ID_KIND_QUALIFIED)
4045               /* A call to a static class member, or a namespace-scope
4046                  function.  */
4047               postfix_expression
4048                 = finish_call_expr (postfix_expression, args,
4049                                     /*disallow_virtual=*/true,
4050                                     koenig_p);
4051             else
4052               /* All other function calls.  */
4053               postfix_expression
4054                 = finish_call_expr (postfix_expression, args,
4055                                     /*disallow_virtual=*/false,
4056                                     koenig_p);
4057
4058             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4059             idk = CP_ID_KIND_NONE;
4060           }
4061           break;
4062
4063         case CPP_DOT:
4064         case CPP_DEREF:
4065           /* postfix-expression . template [opt] id-expression
4066              postfix-expression . pseudo-destructor-name
4067              postfix-expression -> template [opt] id-expression
4068              postfix-expression -> pseudo-destructor-name */
4069
4070           /* Consume the `.' or `->' operator.  */
4071           cp_lexer_consume_token (parser->lexer);
4072
4073           postfix_expression
4074             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4075                                                       postfix_expression,
4076                                                       false, &idk);
4077           break;
4078
4079         case CPP_PLUS_PLUS:
4080           /* postfix-expression ++  */
4081           /* Consume the `++' token.  */
4082           cp_lexer_consume_token (parser->lexer);
4083           /* Generate a representation for the complete expression.  */
4084           postfix_expression
4085             = finish_increment_expr (postfix_expression,
4086                                      POSTINCREMENT_EXPR);
4087           /* Increments may not appear in constant-expressions.  */
4088           if (cp_parser_non_integral_constant_expression (parser,
4089                                                           "an increment"))
4090             postfix_expression = error_mark_node;
4091           idk = CP_ID_KIND_NONE;
4092           break;
4093
4094         case CPP_MINUS_MINUS:
4095           /* postfix-expression -- */
4096           /* Consume the `--' token.  */
4097           cp_lexer_consume_token (parser->lexer);
4098           /* Generate a representation for the complete expression.  */
4099           postfix_expression
4100             = finish_increment_expr (postfix_expression,
4101                                      POSTDECREMENT_EXPR);
4102           /* Decrements may not appear in constant-expressions.  */
4103           if (cp_parser_non_integral_constant_expression (parser,
4104                                                           "a decrement"))
4105             postfix_expression = error_mark_node;
4106           idk = CP_ID_KIND_NONE;
4107           break;
4108
4109         default:
4110           return postfix_expression;
4111         }
4112     }
4113
4114   /* We should never get here.  */
4115   gcc_unreachable ();
4116   return error_mark_node;
4117 }
4118
4119 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4120    by cp_parser_builtin_offsetof.  We're looking for
4121
4122      postfix-expression [ expression ]
4123
4124    FOR_OFFSETOF is set if we're being called in that context, which
4125    changes how we deal with integer constant expressions.  */
4126
4127 static tree
4128 cp_parser_postfix_open_square_expression (cp_parser *parser,
4129                                           tree postfix_expression,
4130                                           bool for_offsetof)
4131 {
4132   tree index;
4133
4134   /* Consume the `[' token.  */
4135   cp_lexer_consume_token (parser->lexer);
4136
4137   /* Parse the index expression.  */
4138   /* ??? For offsetof, there is a question of what to allow here.  If
4139      offsetof is not being used in an integral constant expression context,
4140      then we *could* get the right answer by computing the value at runtime.
4141      If we are in an integral constant expression context, then we might
4142      could accept any constant expression; hard to say without analysis.
4143      Rather than open the barn door too wide right away, allow only integer
4144      constant expressions here.  */
4145   if (for_offsetof)
4146     index = cp_parser_constant_expression (parser, false, NULL);
4147   else
4148     index = cp_parser_expression (parser);
4149
4150   /* Look for the closing `]'.  */
4151   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4152
4153   /* Build the ARRAY_REF.  */
4154   postfix_expression = grok_array_decl (postfix_expression, index);
4155
4156   /* When not doing offsetof, array references are not permitted in
4157      constant-expressions.  */
4158   if (!for_offsetof
4159       && (cp_parser_non_integral_constant_expression
4160           (parser, "an array reference")))
4161     postfix_expression = error_mark_node;
4162
4163   return postfix_expression;
4164 }
4165
4166 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4167    by cp_parser_builtin_offsetof.  We're looking for
4168
4169      postfix-expression . template [opt] id-expression
4170      postfix-expression . pseudo-destructor-name
4171      postfix-expression -> template [opt] id-expression
4172      postfix-expression -> pseudo-destructor-name
4173
4174    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4175    limits what of the above we'll actually accept, but nevermind.
4176    TOKEN_TYPE is the "." or "->" token, which will already have been
4177    removed from the stream.  */
4178
4179 static tree
4180 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4181                                         enum cpp_ttype token_type,
4182                                         tree postfix_expression,
4183                                         bool for_offsetof, cp_id_kind *idk)
4184 {
4185   tree name;
4186   bool dependent_p;
4187   bool template_p;
4188   bool pseudo_destructor_p;
4189   tree scope = NULL_TREE;
4190
4191   /* If this is a `->' operator, dereference the pointer.  */
4192   if (token_type == CPP_DEREF)
4193     postfix_expression = build_x_arrow (postfix_expression);
4194   /* Check to see whether or not the expression is type-dependent.  */
4195   dependent_p = type_dependent_expression_p (postfix_expression);
4196   /* The identifier following the `->' or `.' is not qualified.  */
4197   parser->scope = NULL_TREE;
4198   parser->qualifying_scope = NULL_TREE;
4199   parser->object_scope = NULL_TREE;
4200   *idk = CP_ID_KIND_NONE;
4201   /* Enter the scope corresponding to the type of the object
4202      given by the POSTFIX_EXPRESSION.  */
4203   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4204     {
4205       scope = TREE_TYPE (postfix_expression);
4206       /* According to the standard, no expression should ever have
4207          reference type.  Unfortunately, we do not currently match
4208          the standard in this respect in that our internal representation
4209          of an expression may have reference type even when the standard
4210          says it does not.  Therefore, we have to manually obtain the
4211          underlying type here.  */
4212       scope = non_reference (scope);
4213       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4214       scope = complete_type_or_else (scope, NULL_TREE);
4215       /* Let the name lookup machinery know that we are processing a
4216          class member access expression.  */
4217       parser->context->object_type = scope;
4218       /* If something went wrong, we want to be able to discern that case,
4219          as opposed to the case where there was no SCOPE due to the type
4220          of expression being dependent.  */
4221       if (!scope)
4222         scope = error_mark_node;
4223       /* If the SCOPE was erroneous, make the various semantic analysis
4224          functions exit quickly -- and without issuing additional error
4225          messages.  */
4226       if (scope == error_mark_node)
4227         postfix_expression = error_mark_node;
4228     }
4229
4230   /* Assume this expression is not a pseudo-destructor access.  */
4231   pseudo_destructor_p = false;
4232
4233   /* If the SCOPE is a scalar type, then, if this is a valid program,
4234      we must be looking at a pseudo-destructor-name.  */
4235   if (scope && SCALAR_TYPE_P (scope))
4236     {
4237       tree s;
4238       tree type;
4239
4240       cp_parser_parse_tentatively (parser);
4241       /* Parse the pseudo-destructor-name.  */
4242       s = NULL_TREE;
4243       cp_parser_pseudo_destructor_name (parser, &s, &type);
4244       if (cp_parser_parse_definitely (parser))
4245         {
4246           pseudo_destructor_p = true;
4247           postfix_expression
4248             = finish_pseudo_destructor_expr (postfix_expression,
4249                                              s, TREE_TYPE (type));
4250         }
4251     }
4252
4253   if (!pseudo_destructor_p)
4254     {
4255       /* If the SCOPE is not a scalar type, we are looking at an
4256          ordinary class member access expression, rather than a
4257          pseudo-destructor-name.  */
4258       template_p = cp_parser_optional_template_keyword (parser);
4259       /* Parse the id-expression.  */
4260       name = cp_parser_id_expression (parser, template_p,
4261                                       /*check_dependency_p=*/true,
4262                                       /*template_p=*/NULL,
4263                                       /*declarator_p=*/false);
4264       /* In general, build a SCOPE_REF if the member name is qualified.
4265          However, if the name was not dependent and has already been
4266          resolved; there is no need to build the SCOPE_REF.  For example;
4267
4268              struct X { void f(); };
4269              template <typename T> void f(T* t) { t->X::f(); }
4270
4271          Even though "t" is dependent, "X::f" is not and has been resolved
4272          to a BASELINK; there is no need to include scope information.  */
4273
4274       /* But we do need to remember that there was an explicit scope for
4275          virtual function calls.  */
4276       if (parser->scope)
4277         *idk = CP_ID_KIND_QUALIFIED;
4278
4279       /* If the name is a template-id that names a type, we will get a
4280          TYPE_DECL here.  That is invalid code.  */
4281       if (TREE_CODE (name) == TYPE_DECL)
4282         {
4283           error ("invalid use of %qD", name);
4284           postfix_expression = error_mark_node;
4285         }
4286       else
4287         {
4288           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4289             {
4290               name = build_nt (SCOPE_REF, parser->scope, name);
4291               parser->scope = NULL_TREE;
4292               parser->qualifying_scope = NULL_TREE;
4293               parser->object_scope = NULL_TREE;
4294             }
4295           if (scope && name && BASELINK_P (name))
4296             adjust_result_of_qualified_name_lookup
4297               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4298           postfix_expression
4299             = finish_class_member_access_expr (postfix_expression, name);
4300         }
4301     }
4302
4303   /* We no longer need to look up names in the scope of the object on
4304      the left-hand side of the `.' or `->' operator.  */
4305   parser->context->object_type = NULL_TREE;
4306
4307   /* Outside of offsetof, these operators may not appear in
4308      constant-expressions.  */
4309   if (!for_offsetof
4310       && (cp_parser_non_integral_constant_expression
4311           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4312     postfix_expression = error_mark_node;
4313
4314   return postfix_expression;
4315 }
4316
4317 /* Parse a parenthesized expression-list.
4318
4319    expression-list:
4320      assignment-expression
4321      expression-list, assignment-expression
4322
4323    attribute-list:
4324      expression-list
4325      identifier
4326      identifier, expression-list
4327
4328    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4329    representation of an assignment-expression.  Note that a TREE_LIST
4330    is returned even if there is only a single expression in the list.
4331    error_mark_node is returned if the ( and or ) are
4332    missing. NULL_TREE is returned on no expressions. The parentheses
4333    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4334    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4335    indicates whether or not all of the expressions in the list were
4336    constant.  */
4337
4338 static tree
4339 cp_parser_parenthesized_expression_list (cp_parser* parser,
4340                                          bool is_attribute_list,
4341                                          bool *non_constant_p)
4342 {
4343   tree expression_list = NULL_TREE;
4344   bool fold_expr_p = is_attribute_list;
4345   tree identifier = NULL_TREE;
4346
4347   /* Assume all the expressions will be constant.  */
4348   if (non_constant_p)
4349     *non_constant_p = false;
4350
4351   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4352     return error_mark_node;
4353
4354   /* Consume expressions until there are no more.  */
4355   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4356     while (true)
4357       {
4358         tree expr;
4359
4360         /* At the beginning of attribute lists, check to see if the
4361            next token is an identifier.  */
4362         if (is_attribute_list
4363             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4364           {
4365             cp_token *token;
4366
4367             /* Consume the identifier.  */
4368             token = cp_lexer_consume_token (parser->lexer);
4369             /* Save the identifier.  */
4370             identifier = token->value;
4371           }
4372         else
4373           {
4374             /* Parse the next assignment-expression.  */
4375             if (non_constant_p)
4376               {
4377                 bool expr_non_constant_p;
4378                 expr = (cp_parser_constant_expression
4379                         (parser, /*allow_non_constant_p=*/true,
4380                          &expr_non_constant_p));
4381                 if (expr_non_constant_p)
4382                   *non_constant_p = true;
4383               }
4384             else
4385               expr = cp_parser_assignment_expression (parser);
4386
4387             if (fold_expr_p)
4388               expr = fold_non_dependent_expr (expr);
4389
4390              /* Add it to the list.  We add error_mark_node
4391                 expressions to the list, so that we can still tell if
4392                 the correct form for a parenthesized expression-list
4393                 is found. That gives better errors.  */
4394             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4395
4396             if (expr == error_mark_node)
4397               goto skip_comma;
4398           }
4399
4400         /* After the first item, attribute lists look the same as
4401            expression lists.  */
4402         is_attribute_list = false;
4403
4404       get_comma:;
4405         /* If the next token isn't a `,', then we are done.  */
4406         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4407           break;
4408
4409         /* Otherwise, consume the `,' and keep going.  */
4410         cp_lexer_consume_token (parser->lexer);
4411       }
4412
4413   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4414     {
4415       int ending;
4416
4417     skip_comma:;
4418       /* We try and resync to an unnested comma, as that will give the
4419          user better diagnostics.  */
4420       ending = cp_parser_skip_to_closing_parenthesis (parser,
4421                                                       /*recovering=*/true,
4422                                                       /*or_comma=*/true,
4423                                                       /*consume_paren=*/true);
4424       if (ending < 0)
4425         goto get_comma;
4426       if (!ending)
4427         return error_mark_node;
4428     }
4429
4430   /* We built up the list in reverse order so we must reverse it now.  */
4431   expression_list = nreverse (expression_list);
4432   if (identifier)
4433     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4434
4435   return expression_list;
4436 }
4437
4438 /* Parse a pseudo-destructor-name.
4439
4440    pseudo-destructor-name:
4441      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4442      :: [opt] nested-name-specifier template template-id :: ~ type-name
4443      :: [opt] nested-name-specifier [opt] ~ type-name
4444
4445    If either of the first two productions is used, sets *SCOPE to the
4446    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4447    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4448    or ERROR_MARK_NODE if the parse fails.  */
4449
4450 static void
4451 cp_parser_pseudo_destructor_name (cp_parser* parser,
4452                                   tree* scope,
4453                                   tree* type)
4454 {
4455   bool nested_name_specifier_p;
4456
4457   /* Assume that things will not work out.  */
4458   *type = error_mark_node;
4459
4460   /* Look for the optional `::' operator.  */
4461   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4462   /* Look for the optional nested-name-specifier.  */
4463   nested_name_specifier_p
4464     = (cp_parser_nested_name_specifier_opt (parser,
4465                                             /*typename_keyword_p=*/false,
4466                                             /*check_dependency_p=*/true,
4467                                             /*type_p=*/false,
4468                                             /*is_declaration=*/true)
4469        != NULL_TREE);
4470   /* Now, if we saw a nested-name-specifier, we might be doing the
4471      second production.  */
4472   if (nested_name_specifier_p
4473       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4474     {
4475       /* Consume the `template' keyword.  */
4476       cp_lexer_consume_token (parser->lexer);
4477       /* Parse the template-id.  */
4478       cp_parser_template_id (parser,
4479                              /*template_keyword_p=*/true,
4480                              /*check_dependency_p=*/false,
4481                              /*is_declaration=*/true);
4482       /* Look for the `::' token.  */
4483       cp_parser_require (parser, CPP_SCOPE, "`::'");
4484     }
4485   /* If the next token is not a `~', then there might be some
4486      additional qualification.  */
4487   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4488     {
4489       /* Look for the type-name.  */
4490       *scope = TREE_TYPE (cp_parser_type_name (parser));
4491
4492       if (*scope == error_mark_node)
4493         return;
4494
4495       /* If we don't have ::~, then something has gone wrong.  Since
4496          the only caller of this function is looking for something
4497          after `.' or `->' after a scalar type, most likely the
4498          program is trying to get a member of a non-aggregate
4499          type.  */
4500       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4501           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4502         {
4503           cp_parser_error (parser, "request for member of non-aggregate type");
4504           return;
4505         }
4506
4507       /* Look for the `::' token.  */
4508       cp_parser_require (parser, CPP_SCOPE, "`::'");
4509     }
4510   else
4511     *scope = NULL_TREE;
4512
4513   /* Look for the `~'.  */
4514   cp_parser_require (parser, CPP_COMPL, "`~'");
4515   /* Look for the type-name again.  We are not responsible for
4516      checking that it matches the first type-name.  */
4517   *type = cp_parser_type_name (parser);
4518 }
4519
4520 /* Parse a unary-expression.
4521
4522    unary-expression:
4523      postfix-expression
4524      ++ cast-expression
4525      -- cast-expression
4526      unary-operator cast-expression
4527      sizeof unary-expression
4528      sizeof ( type-id )
4529      new-expression
4530      delete-expression
4531
4532    GNU Extensions:
4533
4534    unary-expression:
4535      __extension__ cast-expression
4536      __alignof__ unary-expression
4537      __alignof__ ( type-id )
4538      __real__ cast-expression
4539      __imag__ cast-expression
4540      && identifier
4541
4542    ADDRESS_P is true iff the unary-expression is appearing as the
4543    operand of the `&' operator.
4544
4545    Returns a representation of the expression.  */
4546
4547 static tree
4548 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4549 {
4550   cp_token *token;
4551   enum tree_code unary_operator;
4552
4553   /* Peek at the next token.  */
4554   token = cp_lexer_peek_token (parser->lexer);
4555   /* Some keywords give away the kind of expression.  */
4556   if (token->type == CPP_KEYWORD)
4557     {
4558       enum rid keyword = token->keyword;
4559
4560       switch (keyword)
4561         {
4562         case RID_ALIGNOF:
4563         case RID_SIZEOF:
4564           {
4565             tree operand;
4566             enum tree_code op;
4567
4568             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4569             /* Consume the token.  */
4570             cp_lexer_consume_token (parser->lexer);
4571             /* Parse the operand.  */
4572             operand = cp_parser_sizeof_operand (parser, keyword);
4573
4574             if (TYPE_P (operand))
4575               return cxx_sizeof_or_alignof_type (operand, op, true);
4576             else
4577               return cxx_sizeof_or_alignof_expr (operand, op);
4578           }
4579
4580         case RID_NEW:
4581           return cp_parser_new_expression (parser);
4582
4583         case RID_DELETE:
4584           return cp_parser_delete_expression (parser);
4585
4586         case RID_EXTENSION:
4587           {
4588             /* The saved value of the PEDANTIC flag.  */
4589             int saved_pedantic;
4590             tree expr;
4591
4592             /* Save away the PEDANTIC flag.  */
4593             cp_parser_extension_opt (parser, &saved_pedantic);
4594             /* Parse the cast-expression.  */
4595             expr = cp_parser_simple_cast_expression (parser);
4596             /* Restore the PEDANTIC flag.  */
4597             pedantic = saved_pedantic;
4598
4599             return expr;
4600           }
4601
4602         case RID_REALPART:
4603         case RID_IMAGPART:
4604           {
4605             tree expression;
4606
4607             /* Consume the `__real__' or `__imag__' token.  */
4608             cp_lexer_consume_token (parser->lexer);
4609             /* Parse the cast-expression.  */
4610             expression = cp_parser_simple_cast_expression (parser);
4611             /* Create the complete representation.  */
4612             return build_x_unary_op ((keyword == RID_REALPART
4613                                       ? REALPART_EXPR : IMAGPART_EXPR),
4614                                      expression);
4615           }
4616           break;
4617
4618         default:
4619           break;
4620         }
4621     }
4622
4623   /* Look for the `:: new' and `:: delete', which also signal the
4624      beginning of a new-expression, or delete-expression,
4625      respectively.  If the next token is `::', then it might be one of
4626      these.  */
4627   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4628     {
4629       enum rid keyword;
4630
4631       /* See if the token after the `::' is one of the keywords in
4632          which we're interested.  */
4633       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4634       /* If it's `new', we have a new-expression.  */
4635       if (keyword == RID_NEW)
4636         return cp_parser_new_expression (parser);
4637       /* Similarly, for `delete'.  */
4638       else if (keyword == RID_DELETE)
4639         return cp_parser_delete_expression (parser);
4640     }
4641
4642   /* Look for a unary operator.  */
4643   unary_operator = cp_parser_unary_operator (token);
4644   /* The `++' and `--' operators can be handled similarly, even though
4645      they are not technically unary-operators in the grammar.  */
4646   if (unary_operator == ERROR_MARK)
4647     {
4648       if (token->type == CPP_PLUS_PLUS)
4649         unary_operator = PREINCREMENT_EXPR;
4650       else if (token->type == CPP_MINUS_MINUS)
4651         unary_operator = PREDECREMENT_EXPR;
4652       /* Handle the GNU address-of-label extension.  */
4653       else if (cp_parser_allow_gnu_extensions_p (parser)
4654                && token->type == CPP_AND_AND)
4655         {
4656           tree identifier;
4657
4658           /* Consume the '&&' token.  */
4659           cp_lexer_consume_token (parser->lexer);
4660           /* Look for the identifier.  */
4661           identifier = cp_parser_identifier (parser);
4662           /* Create an expression representing the address.  */
4663           return finish_label_address_expr (identifier);
4664         }
4665     }
4666   if (unary_operator != ERROR_MARK)
4667     {
4668       tree cast_expression;
4669       tree expression = error_mark_node;
4670       const char *non_constant_p = NULL;
4671
4672       /* Consume the operator token.  */
4673       token = cp_lexer_consume_token (parser->lexer);
4674       /* Parse the cast-expression.  */
4675       cast_expression
4676         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4677       /* Now, build an appropriate representation.  */
4678       switch (unary_operator)
4679         {
4680         case INDIRECT_REF:
4681           non_constant_p = "`*'";
4682           expression = build_x_indirect_ref (cast_expression, "unary *");
4683           break;
4684
4685         case ADDR_EXPR:
4686           non_constant_p = "`&'";
4687           /* Fall through.  */
4688         case BIT_NOT_EXPR:
4689           expression = build_x_unary_op (unary_operator, cast_expression);
4690           break;
4691
4692         case PREINCREMENT_EXPR:
4693         case PREDECREMENT_EXPR:
4694           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4695                             ? "`++'" : "`--'");
4696           /* Fall through.  */
4697         case CONVERT_EXPR:
4698         case NEGATE_EXPR:
4699         case TRUTH_NOT_EXPR:
4700           expression = finish_unary_op_expr (unary_operator, cast_expression);
4701           break;
4702
4703         default:
4704           gcc_unreachable ();
4705         }
4706
4707       if (non_constant_p
4708           && cp_parser_non_integral_constant_expression (parser,
4709                                                          non_constant_p))
4710         expression = error_mark_node;
4711
4712       return expression;
4713     }
4714
4715   return cp_parser_postfix_expression (parser, address_p);
4716 }
4717
4718 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4719    unary-operator, the corresponding tree code is returned.  */
4720
4721 static enum tree_code
4722 cp_parser_unary_operator (cp_token* token)
4723 {
4724   switch (token->type)
4725     {
4726     case CPP_MULT:
4727       return INDIRECT_REF;
4728
4729     case CPP_AND:
4730       return ADDR_EXPR;
4731
4732     case CPP_PLUS:
4733       return CONVERT_EXPR;
4734
4735     case CPP_MINUS:
4736       return NEGATE_EXPR;
4737
4738     case CPP_NOT:
4739       return TRUTH_NOT_EXPR;
4740
4741     case CPP_COMPL:
4742       return BIT_NOT_EXPR;
4743
4744     default:
4745       return ERROR_MARK;
4746     }
4747 }
4748
4749 /* Parse a new-expression.
4750
4751    new-expression:
4752      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4753      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4754
4755    Returns a representation of the expression.  */
4756
4757 static tree
4758 cp_parser_new_expression (cp_parser* parser)
4759 {
4760   bool global_scope_p;
4761   tree placement;
4762   tree type;
4763   tree initializer;
4764   tree nelts;
4765
4766   /* Look for the optional `::' operator.  */
4767   global_scope_p
4768     = (cp_parser_global_scope_opt (parser,
4769                                    /*current_scope_valid_p=*/false)
4770        != NULL_TREE);
4771   /* Look for the `new' operator.  */
4772   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4773   /* There's no easy way to tell a new-placement from the
4774      `( type-id )' construct.  */
4775   cp_parser_parse_tentatively (parser);
4776   /* Look for a new-placement.  */
4777   placement = cp_parser_new_placement (parser);
4778   /* If that didn't work out, there's no new-placement.  */
4779   if (!cp_parser_parse_definitely (parser))
4780     placement = NULL_TREE;
4781
4782   /* If the next token is a `(', then we have a parenthesized
4783      type-id.  */
4784   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4785     {
4786       /* Consume the `('.  */
4787       cp_lexer_consume_token (parser->lexer);
4788       /* Parse the type-id.  */
4789       type = cp_parser_type_id (parser);
4790       /* Look for the closing `)'.  */
4791       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4792       /* There should not be a direct-new-declarator in this production,
4793          but GCC used to allowed this, so we check and emit a sensible error
4794          message for this case.  */
4795       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4796         {
4797           error ("array bound forbidden after parenthesized type-id");
4798           inform ("try removing the parentheses around the type-id");
4799           cp_parser_direct_new_declarator (parser);
4800         }
4801       nelts = NULL_TREE;
4802     }
4803   /* Otherwise, there must be a new-type-id.  */
4804   else
4805     type = cp_parser_new_type_id (parser, &nelts);
4806
4807   /* If the next token is a `(', then we have a new-initializer.  */
4808   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4809     initializer = cp_parser_new_initializer (parser);
4810   else
4811     initializer = NULL_TREE;
4812
4813   /* A new-expression may not appear in an integral constant
4814      expression.  */
4815   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4816     return error_mark_node;
4817
4818   /* Create a representation of the new-expression.  */
4819   return build_new (placement, type, nelts, initializer, global_scope_p);
4820 }
4821
4822 /* Parse a new-placement.
4823
4824    new-placement:
4825      ( expression-list )
4826
4827    Returns the same representation as for an expression-list.  */
4828
4829 static tree
4830 cp_parser_new_placement (cp_parser* parser)
4831 {
4832   tree expression_list;
4833
4834   /* Parse the expression-list.  */
4835   expression_list = (cp_parser_parenthesized_expression_list
4836                      (parser, false, /*non_constant_p=*/NULL));
4837
4838   return expression_list;
4839 }
4840
4841 /* Parse a new-type-id.
4842
4843    new-type-id:
4844      type-specifier-seq new-declarator [opt]
4845
4846    Returns the TYPE allocated.  If the new-type-id indicates an array
4847    type, *NELTS is set to the number of elements in the last array
4848    bound; the TYPE will not include the last array bound.  */
4849
4850 static tree
4851 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4852 {
4853   cp_decl_specifier_seq type_specifier_seq;
4854   cp_declarator *new_declarator;
4855   cp_declarator *declarator;
4856   cp_declarator *outer_declarator;
4857   const char *saved_message;
4858   tree type;
4859
4860   /* The type-specifier sequence must not contain type definitions.
4861      (It cannot contain declarations of new types either, but if they
4862      are not definitions we will catch that because they are not
4863      complete.)  */
4864   saved_message = parser->type_definition_forbidden_message;
4865   parser->type_definition_forbidden_message
4866     = "types may not be defined in a new-type-id";
4867   /* Parse the type-specifier-seq.  */
4868   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4869   /* Restore the old message.  */
4870   parser->type_definition_forbidden_message = saved_message;
4871   /* Parse the new-declarator.  */
4872   new_declarator = cp_parser_new_declarator_opt (parser);
4873
4874   /* Determine the number of elements in the last array dimension, if
4875      any.  */
4876   *nelts = NULL_TREE;
4877   /* Skip down to the last array dimension.  */
4878   declarator = new_declarator;
4879   outer_declarator = NULL;
4880   while (declarator && (declarator->kind == cdk_pointer
4881                         || declarator->kind == cdk_ptrmem))
4882     {
4883       outer_declarator = declarator;
4884       declarator = declarator->declarator;
4885     }
4886   while (declarator
4887          && declarator->kind == cdk_array
4888          && declarator->declarator
4889          && declarator->declarator->kind == cdk_array)
4890     {
4891       outer_declarator = declarator;
4892       declarator = declarator->declarator;
4893     }
4894
4895   if (declarator && declarator->kind == cdk_array)
4896     {
4897       *nelts = declarator->u.array.bounds;
4898       if (*nelts == error_mark_node)
4899         *nelts = integer_one_node;
4900       
4901       if (outer_declarator)
4902         outer_declarator->declarator = declarator->declarator;
4903       else
4904         new_declarator = NULL;
4905     }
4906
4907   type = groktypename (&type_specifier_seq, new_declarator);
4908   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4909     {
4910       *nelts = array_type_nelts_top (type);
4911       type = TREE_TYPE (type);
4912     }
4913   return type;
4914 }
4915
4916 /* Parse an (optional) new-declarator.
4917
4918    new-declarator:
4919      ptr-operator new-declarator [opt]
4920      direct-new-declarator
4921
4922    Returns the declarator.  */
4923
4924 static cp_declarator *
4925 cp_parser_new_declarator_opt (cp_parser* parser)
4926 {
4927   enum tree_code code;
4928   tree type;
4929   cp_cv_quals cv_quals;
4930
4931   /* We don't know if there's a ptr-operator next, or not.  */
4932   cp_parser_parse_tentatively (parser);
4933   /* Look for a ptr-operator.  */
4934   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4935   /* If that worked, look for more new-declarators.  */
4936   if (cp_parser_parse_definitely (parser))
4937     {
4938       cp_declarator *declarator;
4939
4940       /* Parse another optional declarator.  */
4941       declarator = cp_parser_new_declarator_opt (parser);
4942
4943       /* Create the representation of the declarator.  */
4944       if (type)
4945         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4946       else if (code == INDIRECT_REF)
4947         declarator = make_pointer_declarator (cv_quals, declarator);
4948       else
4949         declarator = make_reference_declarator (cv_quals, declarator);
4950
4951       return declarator;
4952     }
4953
4954   /* If the next token is a `[', there is a direct-new-declarator.  */
4955   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4956     return cp_parser_direct_new_declarator (parser);
4957
4958   return NULL;
4959 }
4960
4961 /* Parse a direct-new-declarator.
4962
4963    direct-new-declarator:
4964      [ expression ]
4965      direct-new-declarator [constant-expression]
4966
4967    */
4968
4969 static cp_declarator *
4970 cp_parser_direct_new_declarator (cp_parser* parser)
4971 {
4972   cp_declarator *declarator = NULL;
4973
4974   while (true)
4975     {
4976       tree expression;
4977
4978       /* Look for the opening `['.  */
4979       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4980       /* The first expression is not required to be constant.  */
4981       if (!declarator)
4982         {
4983           expression = cp_parser_expression (parser);
4984           /* The standard requires that the expression have integral
4985              type.  DR 74 adds enumeration types.  We believe that the
4986              real intent is that these expressions be handled like the
4987              expression in a `switch' condition, which also allows
4988              classes with a single conversion to integral or
4989              enumeration type.  */
4990           if (!processing_template_decl)
4991             {
4992               expression
4993                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4994                                               expression,
4995                                               /*complain=*/true);
4996               if (!expression)
4997                 {
4998                   error ("expression in new-declarator must have integral "
4999                          "or enumeration type");
5000                   expression = error_mark_node;
5001                 }
5002             }
5003         }
5004       /* But all the other expressions must be.  */
5005       else
5006         expression
5007           = cp_parser_constant_expression (parser,
5008                                            /*allow_non_constant=*/false,
5009                                            NULL);
5010       /* Look for the closing `]'.  */
5011       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5012
5013       /* Add this bound to the declarator.  */
5014       declarator = make_array_declarator (declarator, expression);
5015
5016       /* If the next token is not a `[', then there are no more
5017          bounds.  */
5018       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5019         break;
5020     }
5021
5022   return declarator;
5023 }
5024
5025 /* Parse a new-initializer.
5026
5027    new-initializer:
5028      ( expression-list [opt] )
5029
5030    Returns a representation of the expression-list.  If there is no
5031    expression-list, VOID_ZERO_NODE is returned.  */
5032
5033 static tree
5034 cp_parser_new_initializer (cp_parser* parser)
5035 {
5036   tree expression_list;
5037
5038   expression_list = (cp_parser_parenthesized_expression_list
5039                      (parser, false, /*non_constant_p=*/NULL));
5040   if (!expression_list)
5041     expression_list = void_zero_node;
5042
5043   return expression_list;
5044 }
5045
5046 /* Parse a delete-expression.
5047
5048    delete-expression:
5049      :: [opt] delete cast-expression
5050      :: [opt] delete [ ] cast-expression
5051
5052    Returns a representation of the expression.  */
5053
5054 static tree
5055 cp_parser_delete_expression (cp_parser* parser)
5056 {
5057   bool global_scope_p;
5058   bool array_p;
5059   tree expression;
5060
5061   /* Look for the optional `::' operator.  */
5062   global_scope_p
5063     = (cp_parser_global_scope_opt (parser,
5064                                    /*current_scope_valid_p=*/false)
5065        != NULL_TREE);
5066   /* Look for the `delete' keyword.  */
5067   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5068   /* See if the array syntax is in use.  */
5069   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5070     {
5071       /* Consume the `[' token.  */
5072       cp_lexer_consume_token (parser->lexer);
5073       /* Look for the `]' token.  */
5074       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5075       /* Remember that this is the `[]' construct.  */
5076       array_p = true;
5077     }
5078   else
5079     array_p = false;
5080
5081   /* Parse the cast-expression.  */
5082   expression = cp_parser_simple_cast_expression (parser);
5083
5084   /* A delete-expression may not appear in an integral constant
5085      expression.  */
5086   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5087     return error_mark_node;
5088
5089   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5090 }
5091
5092 /* Parse a cast-expression.
5093
5094    cast-expression:
5095      unary-expression
5096      ( type-id ) cast-expression
5097
5098    Returns a representation of the expression.  */
5099
5100 static tree
5101 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5102 {
5103   /* If it's a `(', then we might be looking at a cast.  */
5104   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5105     {
5106       tree type = NULL_TREE;
5107       tree expr = NULL_TREE;
5108       bool compound_literal_p;
5109       const char *saved_message;
5110
5111       /* There's no way to know yet whether or not this is a cast.
5112          For example, `(int (3))' is a unary-expression, while `(int)
5113          3' is a cast.  So, we resort to parsing tentatively.  */
5114       cp_parser_parse_tentatively (parser);
5115       /* Types may not be defined in a cast.  */
5116       saved_message = parser->type_definition_forbidden_message;
5117       parser->type_definition_forbidden_message
5118         = "types may not be defined in casts";
5119       /* Consume the `('.  */
5120       cp_lexer_consume_token (parser->lexer);
5121       /* A very tricky bit is that `(struct S) { 3 }' is a
5122          compound-literal (which we permit in C++ as an extension).
5123          But, that construct is not a cast-expression -- it is a
5124          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5125          is legal; if the compound-literal were a cast-expression,
5126          you'd need an extra set of parentheses.)  But, if we parse
5127          the type-id, and it happens to be a class-specifier, then we
5128          will commit to the parse at that point, because we cannot
5129          undo the action that is done when creating a new class.  So,
5130          then we cannot back up and do a postfix-expression.
5131
5132          Therefore, we scan ahead to the closing `)', and check to see
5133          if the token after the `)' is a `{'.  If so, we are not
5134          looking at a cast-expression.
5135
5136          Save tokens so that we can put them back.  */
5137       cp_lexer_save_tokens (parser->lexer);
5138       /* Skip tokens until the next token is a closing parenthesis.
5139          If we find the closing `)', and the next token is a `{', then
5140          we are looking at a compound-literal.  */
5141       compound_literal_p
5142         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5143                                                   /*consume_paren=*/true)
5144            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5145       /* Roll back the tokens we skipped.  */
5146       cp_lexer_rollback_tokens (parser->lexer);
5147       /* If we were looking at a compound-literal, simulate an error
5148          so that the call to cp_parser_parse_definitely below will
5149          fail.  */
5150       if (compound_literal_p)
5151         cp_parser_simulate_error (parser);
5152       else
5153         {
5154           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5155           parser->in_type_id_in_expr_p = true;
5156           /* Look for the type-id.  */
5157           type = cp_parser_type_id (parser);
5158           /* Look for the closing `)'.  */
5159           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5160           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5161         }
5162
5163       /* Restore the saved message.  */
5164       parser->type_definition_forbidden_message = saved_message;
5165
5166       /* If ok so far, parse the dependent expression. We cannot be
5167          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5168          ctor of T, but looks like a cast to function returning T
5169          without a dependent expression.  */
5170       if (!cp_parser_error_occurred (parser))
5171         expr = cp_parser_simple_cast_expression (parser);
5172
5173       if (cp_parser_parse_definitely (parser))
5174         {
5175           /* Warn about old-style casts, if so requested.  */
5176           if (warn_old_style_cast
5177               && !in_system_header
5178               && !VOID_TYPE_P (type)
5179               && current_lang_name != lang_name_c)
5180             warning ("use of old-style cast");
5181
5182           /* Only type conversions to integral or enumeration types
5183              can be used in constant-expressions.  */
5184           if (parser->integral_constant_expression_p
5185               && !dependent_type_p (type)
5186               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5187               && (cp_parser_non_integral_constant_expression
5188                   (parser,
5189                    "a cast to a type other than an integral or "
5190                    "enumeration type")))
5191             return error_mark_node;
5192
5193           /* Perform the cast.  */
5194           expr = build_c_cast (type, expr);
5195           return expr;
5196         }
5197     }
5198
5199   /* If we get here, then it's not a cast, so it must be a
5200      unary-expression.  */
5201   return cp_parser_unary_expression (parser, address_p);
5202 }
5203
5204 /* Parse a binary expression of the general form:
5205
5206    pm-expression:
5207      cast-expression
5208      pm-expression .* cast-expression
5209      pm-expression ->* cast-expression
5210
5211    multiplicative-expression:
5212      pm-expression
5213      multiplicative-expression * pm-expression
5214      multiplicative-expression / pm-expression
5215      multiplicative-expression % pm-expression
5216
5217    additive-expression:
5218      multiplicative-expression
5219      additive-expression + multiplicative-expression
5220      additive-expression - multiplicative-expression
5221
5222    shift-expression:
5223      additive-expression
5224      shift-expression << additive-expression
5225      shift-expression >> additive-expression
5226
5227    relational-expression:
5228      shift-expression
5229      relational-expression < shift-expression
5230      relational-expression > shift-expression
5231      relational-expression <= shift-expression
5232      relational-expression >= shift-expression
5233
5234   GNU Extension:
5235   
5236    relational-expression:
5237      relational-expression <? shift-expression
5238      relational-expression >? shift-expression
5239
5240    equality-expression:
5241      relational-expression
5242      equality-expression == relational-expression
5243      equality-expression != relational-expression
5244
5245    and-expression:
5246      equality-expression
5247      and-expression & equality-expression
5248
5249    exclusive-or-expression:
5250      and-expression
5251      exclusive-or-expression ^ and-expression
5252
5253    inclusive-or-expression:
5254      exclusive-or-expression
5255      inclusive-or-expression | exclusive-or-expression
5256
5257    logical-and-expression:
5258      inclusive-or-expression
5259      logical-and-expression && inclusive-or-expression
5260
5261    logical-or-expression:
5262      logical-and-expression
5263      logical-or-expression || logical-and-expression
5264
5265    All these are implemented with a single function like:
5266
5267    binary-expression:
5268      simple-cast-expression
5269      binary-expression <token> binary-expression
5270
5271    The binops_by_token map is used to get the tree codes for each <token> type.
5272    binary-expressions are associated according to a precedence table.  */
5273
5274 #define TOKEN_PRECEDENCE(token) \
5275   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5276    ? PREC_NOT_OPERATOR \
5277    : binops_by_token[token->type].prec)
5278
5279 static tree
5280 cp_parser_binary_expression (cp_parser* parser)
5281 {
5282   cp_parser_expression_stack stack;
5283   cp_parser_expression_stack_entry *sp = &stack[0];
5284   tree lhs, rhs;
5285   cp_token *token;
5286   enum tree_code tree_type;
5287   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5288   bool overloaded_p;
5289
5290   /* Parse the first expression.  */
5291   lhs = cp_parser_simple_cast_expression (parser);
5292
5293   for (;;)
5294     {
5295       /* Get an operator token.  */
5296       token = cp_lexer_peek_token (parser->lexer);
5297       new_prec = TOKEN_PRECEDENCE (token);
5298
5299       /* Popping an entry off the stack means we completed a subexpression:
5300          - either we found a token which is not an operator (`>' where it is not
5301            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5302            will happen repeatedly;
5303          - or, we found an operator which has lower priority.  This is the case 
5304            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5305            parsing `3 * 4'.  */
5306       if (new_prec <= prec)
5307         {
5308           if (sp == stack)
5309             break;
5310           else
5311             goto pop;
5312         }
5313
5314      get_rhs:
5315       tree_type = binops_by_token[token->type].tree_type;
5316
5317       /* We used the operator token.  */
5318       cp_lexer_consume_token (parser->lexer);
5319
5320       /* Extract another operand.  It may be the RHS of this expression
5321          or the LHS of a new, higher priority expression.  */
5322       rhs = cp_parser_simple_cast_expression (parser);
5323
5324       /* Get another operator token.  Look up its precedence to avoid
5325          building a useless (immediately popped) stack entry for common
5326          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5327       token = cp_lexer_peek_token (parser->lexer);
5328       lookahead_prec = TOKEN_PRECEDENCE (token);
5329       if (lookahead_prec > new_prec)
5330         {
5331           /* ... and prepare to parse the RHS of the new, higher priority
5332              expression.  Since precedence levels on the stack are
5333              monotonically increasing, we do not have to care about
5334              stack overflows.  */
5335           sp->prec = prec;
5336           sp->tree_type = tree_type;
5337           sp->lhs = lhs;
5338           sp++;
5339           lhs = rhs;
5340           prec = new_prec;
5341           new_prec = lookahead_prec;
5342           goto get_rhs;
5343
5344          pop:
5345           /* If the stack is not empty, we have parsed into LHS the right side
5346              (`4' in the example above) of an expression we had suspended.
5347              We can use the information on the stack to recover the LHS (`3') 
5348              from the stack together with the tree code (`MULT_EXPR'), and
5349              the precedence of the higher level subexpression
5350              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5351              which will be used to actually build the additive expression.  */
5352           --sp;
5353           prec = sp->prec;
5354           tree_type = sp->tree_type;
5355           rhs = lhs;
5356           lhs = sp->lhs;
5357         }
5358
5359       overloaded_p = false;
5360       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5361
5362       /* If the binary operator required the use of an overloaded operator,
5363          then this expression cannot be an integral constant-expression.
5364          An overloaded operator can be used even if both operands are
5365          otherwise permissible in an integral constant-expression if at
5366          least one of the operands is of enumeration type.  */
5367
5368       if (overloaded_p
5369           && (cp_parser_non_integral_constant_expression 
5370               (parser, "calls to overloaded operators")))
5371         return error_mark_node;
5372     }
5373
5374   return lhs;
5375 }
5376
5377
5378 /* Parse the `? expression : assignment-expression' part of a
5379    conditional-expression.  The LOGICAL_OR_EXPR is the
5380    logical-or-expression that started the conditional-expression.
5381    Returns a representation of the entire conditional-expression.
5382
5383    This routine is used by cp_parser_assignment_expression.
5384
5385      ? expression : assignment-expression
5386
5387    GNU Extensions:
5388
5389      ? : assignment-expression */
5390
5391 static tree
5392 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5393 {
5394   tree expr;
5395   tree assignment_expr;
5396
5397   /* Consume the `?' token.  */
5398   cp_lexer_consume_token (parser->lexer);
5399   if (cp_parser_allow_gnu_extensions_p (parser)
5400       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5401     /* Implicit true clause.  */
5402     expr = NULL_TREE;
5403   else
5404     /* Parse the expression.  */
5405     expr = cp_parser_expression (parser);
5406
5407   /* The next token should be a `:'.  */
5408   cp_parser_require (parser, CPP_COLON, "`:'");
5409   /* Parse the assignment-expression.  */
5410   assignment_expr = cp_parser_assignment_expression (parser);
5411
5412   /* Build the conditional-expression.  */
5413   return build_x_conditional_expr (logical_or_expr,
5414                                    expr,
5415                                    assignment_expr);
5416 }
5417
5418 /* Parse an assignment-expression.
5419
5420    assignment-expression:
5421      conditional-expression
5422      logical-or-expression assignment-operator assignment_expression
5423      throw-expression
5424
5425    Returns a representation for the expression.  */
5426
5427 static tree
5428 cp_parser_assignment_expression (cp_parser* parser)
5429 {
5430   tree expr;
5431
5432   /* If the next token is the `throw' keyword, then we're looking at
5433      a throw-expression.  */
5434   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5435     expr = cp_parser_throw_expression (parser);
5436   /* Otherwise, it must be that we are looking at a
5437      logical-or-expression.  */
5438   else
5439     {
5440       /* Parse the binary expressions (logical-or-expression).  */
5441       expr = cp_parser_binary_expression (parser);
5442       /* If the next token is a `?' then we're actually looking at a
5443          conditional-expression.  */
5444       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5445         return cp_parser_question_colon_clause (parser, expr);
5446       else
5447         {
5448           enum tree_code assignment_operator;
5449
5450           /* If it's an assignment-operator, we're using the second
5451              production.  */
5452           assignment_operator
5453             = cp_parser_assignment_operator_opt (parser);
5454           if (assignment_operator != ERROR_MARK)
5455             {
5456               tree rhs;
5457
5458               /* Parse the right-hand side of the assignment.  */
5459               rhs = cp_parser_assignment_expression (parser);
5460               /* An assignment may not appear in a
5461                  constant-expression.  */
5462               if (cp_parser_non_integral_constant_expression (parser,
5463                                                               "an assignment"))
5464                 return error_mark_node;
5465               /* Build the assignment expression.  */
5466               expr = build_x_modify_expr (expr,
5467                                           assignment_operator,
5468                                           rhs);
5469             }
5470         }
5471     }
5472
5473   return expr;
5474 }
5475
5476 /* Parse an (optional) assignment-operator.
5477
5478    assignment-operator: one of
5479      = *= /= %= += -= >>= <<= &= ^= |=
5480
5481    GNU Extension:
5482
5483    assignment-operator: one of
5484      <?= >?=
5485
5486    If the next token is an assignment operator, the corresponding tree
5487    code is returned, and the token is consumed.  For example, for
5488    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5489    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5490    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5491    operator, ERROR_MARK is returned.  */
5492
5493 static enum tree_code
5494 cp_parser_assignment_operator_opt (cp_parser* parser)
5495 {
5496   enum tree_code op;
5497   cp_token *token;
5498
5499   /* Peek at the next toen.  */
5500   token = cp_lexer_peek_token (parser->lexer);
5501
5502   switch (token->type)
5503     {
5504     case CPP_EQ:
5505       op = NOP_EXPR;
5506       break;
5507
5508     case CPP_MULT_EQ:
5509       op = MULT_EXPR;
5510       break;
5511
5512     case CPP_DIV_EQ:
5513       op = TRUNC_DIV_EXPR;
5514       break;
5515
5516     case CPP_MOD_EQ:
5517       op = TRUNC_MOD_EXPR;
5518       break;
5519
5520     case CPP_PLUS_EQ:
5521       op = PLUS_EXPR;
5522       break;
5523
5524     case CPP_MINUS_EQ:
5525       op = MINUS_EXPR;
5526       break;
5527
5528     case CPP_RSHIFT_EQ:
5529       op = RSHIFT_EXPR;
5530       break;
5531
5532     case CPP_LSHIFT_EQ:
5533       op = LSHIFT_EXPR;
5534       break;
5535
5536     case CPP_AND_EQ:
5537       op = BIT_AND_EXPR;
5538       break;
5539
5540     case CPP_XOR_EQ:
5541       op = BIT_XOR_EXPR;
5542       break;
5543
5544     case CPP_OR_EQ:
5545       op = BIT_IOR_EXPR;
5546       break;
5547
5548     case CPP_MIN_EQ:
5549       op = MIN_EXPR;
5550       break;
5551
5552     case CPP_MAX_EQ:
5553       op = MAX_EXPR;
5554       break;
5555
5556     default:
5557       /* Nothing else is an assignment operator.  */
5558       op = ERROR_MARK;
5559     }
5560
5561   /* If it was an assignment operator, consume it.  */
5562   if (op != ERROR_MARK)
5563     cp_lexer_consume_token (parser->lexer);
5564
5565   return op;
5566 }
5567
5568 /* Parse an expression.
5569
5570    expression:
5571      assignment-expression
5572      expression , assignment-expression
5573
5574    Returns a representation of the expression.  */
5575
5576 static tree
5577 cp_parser_expression (cp_parser* parser)
5578 {
5579   tree expression = NULL_TREE;
5580
5581   while (true)
5582     {
5583       tree assignment_expression;
5584
5585       /* Parse the next assignment-expression.  */
5586       assignment_expression
5587         = cp_parser_assignment_expression (parser);
5588       /* If this is the first assignment-expression, we can just
5589          save it away.  */
5590       if (!expression)
5591         expression = assignment_expression;
5592       else
5593         expression = build_x_compound_expr (expression,
5594                                             assignment_expression);
5595       /* If the next token is not a comma, then we are done with the
5596          expression.  */
5597       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5598         break;
5599       /* Consume the `,'.  */
5600       cp_lexer_consume_token (parser->lexer);
5601       /* A comma operator cannot appear in a constant-expression.  */
5602       if (cp_parser_non_integral_constant_expression (parser,
5603                                                       "a comma operator"))
5604         expression = error_mark_node;
5605     }
5606
5607   return expression;
5608 }
5609
5610 /* Parse a constant-expression.
5611
5612    constant-expression:
5613      conditional-expression
5614
5615   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5616   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5617   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5618   is false, NON_CONSTANT_P should be NULL.  */
5619
5620 static tree
5621 cp_parser_constant_expression (cp_parser* parser,
5622                                bool allow_non_constant_p,
5623                                bool *non_constant_p)
5624 {
5625   bool saved_integral_constant_expression_p;
5626   bool saved_allow_non_integral_constant_expression_p;
5627   bool saved_non_integral_constant_expression_p;
5628   tree expression;
5629
5630   /* It might seem that we could simply parse the
5631      conditional-expression, and then check to see if it were
5632      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5633      one that the compiler can figure out is constant, possibly after
5634      doing some simplifications or optimizations.  The standard has a
5635      precise definition of constant-expression, and we must honor
5636      that, even though it is somewhat more restrictive.
5637
5638      For example:
5639
5640        int i[(2, 3)];
5641
5642      is not a legal declaration, because `(2, 3)' is not a
5643      constant-expression.  The `,' operator is forbidden in a
5644      constant-expression.  However, GCC's constant-folding machinery
5645      will fold this operation to an INTEGER_CST for `3'.  */
5646
5647   /* Save the old settings.  */
5648   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5649   saved_allow_non_integral_constant_expression_p
5650     = parser->allow_non_integral_constant_expression_p;
5651   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5652   /* We are now parsing a constant-expression.  */
5653   parser->integral_constant_expression_p = true;
5654   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5655   parser->non_integral_constant_expression_p = false;
5656   /* Although the grammar says "conditional-expression", we parse an
5657      "assignment-expression", which also permits "throw-expression"
5658      and the use of assignment operators.  In the case that
5659      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5660      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5661      actually essential that we look for an assignment-expression.
5662      For example, cp_parser_initializer_clauses uses this function to
5663      determine whether a particular assignment-expression is in fact
5664      constant.  */
5665   expression = cp_parser_assignment_expression (parser);
5666   /* Restore the old settings.  */
5667   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5668   parser->allow_non_integral_constant_expression_p
5669     = saved_allow_non_integral_constant_expression_p;
5670   if (allow_non_constant_p)
5671     *non_constant_p = parser->non_integral_constant_expression_p;
5672   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5673
5674   return expression;
5675 }
5676
5677 /* Parse __builtin_offsetof.
5678
5679    offsetof-expression:
5680      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5681
5682    offsetof-member-designator:
5683      id-expression
5684      | offsetof-member-designator "." id-expression
5685      | offsetof-member-designator "[" expression "]"
5686 */
5687
5688 static tree
5689 cp_parser_builtin_offsetof (cp_parser *parser)
5690 {
5691   int save_ice_p, save_non_ice_p;
5692   tree type, expr;
5693   cp_id_kind dummy;
5694
5695   /* We're about to accept non-integral-constant things, but will
5696      definitely yield an integral constant expression.  Save and
5697      restore these values around our local parsing.  */
5698   save_ice_p = parser->integral_constant_expression_p;
5699   save_non_ice_p = parser->non_integral_constant_expression_p;
5700
5701   /* Consume the "__builtin_offsetof" token.  */
5702   cp_lexer_consume_token (parser->lexer);
5703   /* Consume the opening `('.  */
5704   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5705   /* Parse the type-id.  */
5706   type = cp_parser_type_id (parser);
5707   /* Look for the `,'.  */
5708   cp_parser_require (parser, CPP_COMMA, "`,'");
5709
5710   /* Build the (type *)null that begins the traditional offsetof macro.  */
5711   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5712
5713   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5714   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5715                                                  true, &dummy);
5716   while (true)
5717     {
5718       cp_token *token = cp_lexer_peek_token (parser->lexer);
5719       switch (token->type)
5720         {
5721         case CPP_OPEN_SQUARE:
5722           /* offsetof-member-designator "[" expression "]" */
5723           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5724           break;
5725
5726         case CPP_DOT:
5727           /* offsetof-member-designator "." identifier */
5728           cp_lexer_consume_token (parser->lexer);
5729           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5730                                                          true, &dummy);
5731           break;
5732
5733         case CPP_CLOSE_PAREN:
5734           /* Consume the ")" token.  */
5735           cp_lexer_consume_token (parser->lexer);
5736           goto success;
5737
5738         default:
5739           /* Error.  We know the following require will fail, but
5740              that gives the proper error message.  */
5741           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5742           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5743           expr = error_mark_node;
5744           goto failure;
5745         }
5746     }
5747
5748  success:
5749   /* If we're processing a template, we can't finish the semantics yet.
5750      Otherwise we can fold the entire expression now.  */
5751   if (processing_template_decl)
5752     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5753   else
5754     expr = fold_offsetof (expr);
5755
5756  failure:
5757   parser->integral_constant_expression_p = save_ice_p;
5758   parser->non_integral_constant_expression_p = save_non_ice_p;
5759
5760   return expr;
5761 }
5762
5763 /* Statements [gram.stmt.stmt]  */
5764
5765 /* Parse a statement.
5766
5767    statement:
5768      labeled-statement
5769      expression-statement
5770      compound-statement
5771      selection-statement
5772      iteration-statement
5773      jump-statement
5774      declaration-statement
5775      try-block  */
5776
5777 static void
5778 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5779 {
5780   tree statement;
5781   cp_token *token;
5782   location_t statement_location;
5783
5784   /* There is no statement yet.  */
5785   statement = NULL_TREE;
5786   /* Peek at the next token.  */
5787   token = cp_lexer_peek_token (parser->lexer);
5788   /* Remember the location of the first token in the statement.  */
5789   statement_location = token->location;
5790   /* If this is a keyword, then that will often determine what kind of
5791      statement we have.  */
5792   if (token->type == CPP_KEYWORD)
5793     {
5794       enum rid keyword = token->keyword;
5795
5796       switch (keyword)
5797         {
5798         case RID_CASE:
5799         case RID_DEFAULT:
5800           statement = cp_parser_labeled_statement (parser,
5801                                                    in_statement_expr);
5802           break;
5803
5804         case RID_IF:
5805         case RID_SWITCH:
5806           statement = cp_parser_selection_statement (parser);
5807           break;
5808
5809         case RID_WHILE:
5810         case RID_DO:
5811         case RID_FOR:
5812           statement = cp_parser_iteration_statement (parser);
5813           break;
5814
5815         case RID_BREAK:
5816         case RID_CONTINUE:
5817         case RID_RETURN:
5818         case RID_GOTO:
5819           statement = cp_parser_jump_statement (parser);
5820           break;
5821
5822         case RID_TRY:
5823           statement = cp_parser_try_block (parser);
5824           break;
5825
5826         default:
5827           /* It might be a keyword like `int' that can start a
5828              declaration-statement.  */
5829           break;
5830         }
5831     }
5832   else if (token->type == CPP_NAME)
5833     {
5834       /* If the next token is a `:', then we are looking at a
5835          labeled-statement.  */
5836       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5837       if (token->type == CPP_COLON)
5838         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5839     }
5840   /* Anything that starts with a `{' must be a compound-statement.  */
5841   else if (token->type == CPP_OPEN_BRACE)
5842     statement = cp_parser_compound_statement (parser, NULL, false);
5843   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5844      a statement all its own.  */
5845   else if (token->type == CPP_PRAGMA)
5846     {
5847       cp_lexer_handle_pragma (parser->lexer);
5848       return;
5849     }
5850
5851   /* Everything else must be a declaration-statement or an
5852      expression-statement.  Try for the declaration-statement
5853      first, unless we are looking at a `;', in which case we know that
5854      we have an expression-statement.  */
5855   if (!statement)
5856     {
5857       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5858         {
5859           cp_parser_parse_tentatively (parser);
5860           /* Try to parse the declaration-statement.  */
5861           cp_parser_declaration_statement (parser);
5862           /* If that worked, we're done.  */
5863           if (cp_parser_parse_definitely (parser))
5864             return;
5865         }
5866       /* Look for an expression-statement instead.  */
5867       statement = cp_parser_expression_statement (parser, in_statement_expr);
5868     }
5869
5870   /* Set the line number for the statement.  */
5871   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5872     SET_EXPR_LOCATION (statement, statement_location);
5873 }
5874
5875 /* Parse a labeled-statement.
5876
5877    labeled-statement:
5878      identifier : statement
5879      case constant-expression : statement
5880      default : statement
5881
5882    GNU Extension:
5883
5884    labeled-statement:
5885      case constant-expression ... constant-expression : statement
5886
5887    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5888    For an ordinary label, returns a LABEL_EXPR.  */
5889
5890 static tree
5891 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5892 {
5893   cp_token *token;
5894   tree statement = error_mark_node;
5895
5896   /* The next token should be an identifier.  */
5897   token = cp_lexer_peek_token (parser->lexer);
5898   if (token->type != CPP_NAME
5899       && token->type != CPP_KEYWORD)
5900     {
5901       cp_parser_error (parser, "expected labeled-statement");
5902       return error_mark_node;
5903     }
5904
5905   switch (token->keyword)
5906     {
5907     case RID_CASE:
5908       {
5909         tree expr, expr_hi;
5910         cp_token *ellipsis;
5911
5912         /* Consume the `case' token.  */
5913         cp_lexer_consume_token (parser->lexer);
5914         /* Parse the constant-expression.  */
5915         expr = cp_parser_constant_expression (parser,
5916                                               /*allow_non_constant_p=*/false,
5917                                               NULL);
5918
5919         ellipsis = cp_lexer_peek_token (parser->lexer);
5920         if (ellipsis->type == CPP_ELLIPSIS)
5921           {
5922             /* Consume the `...' token.  */
5923             cp_lexer_consume_token (parser->lexer);
5924             expr_hi =
5925               cp_parser_constant_expression (parser,
5926                                              /*allow_non_constant_p=*/false,
5927                                              NULL);
5928             /* We don't need to emit warnings here, as the common code
5929                will do this for us.  */
5930           }
5931         else
5932           expr_hi = NULL_TREE;
5933
5934         if (!parser->in_switch_statement_p)
5935           error ("case label %qE not within a switch statement", expr);
5936         else
5937           statement = finish_case_label (expr, expr_hi);
5938       }
5939       break;
5940
5941     case RID_DEFAULT:
5942       /* Consume the `default' token.  */
5943       cp_lexer_consume_token (parser->lexer);
5944       if (!parser->in_switch_statement_p)
5945         error ("case label not within a switch statement");
5946       else
5947         statement = finish_case_label (NULL_TREE, NULL_TREE);
5948       break;
5949
5950     default:
5951       /* Anything else must be an ordinary label.  */
5952       statement = finish_label_stmt (cp_parser_identifier (parser));
5953       break;
5954     }
5955
5956   /* Require the `:' token.  */
5957   cp_parser_require (parser, CPP_COLON, "`:'");
5958   /* Parse the labeled statement.  */
5959   cp_parser_statement (parser, in_statement_expr);
5960
5961   /* Return the label, in the case of a `case' or `default' label.  */
5962   return statement;
5963 }
5964
5965 /* Parse an expression-statement.
5966
5967    expression-statement:
5968      expression [opt] ;
5969
5970    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5971    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5972    indicates whether this expression-statement is part of an
5973    expression statement.  */
5974
5975 static tree
5976 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5977 {
5978   tree statement = NULL_TREE;
5979
5980   /* If the next token is a ';', then there is no expression
5981      statement.  */
5982   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5983     statement = cp_parser_expression (parser);
5984
5985   /* Consume the final `;'.  */
5986   cp_parser_consume_semicolon_at_end_of_statement (parser);
5987
5988   if (in_statement_expr
5989       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5990     {
5991       /* This is the final expression statement of a statement
5992          expression.  */
5993       statement = finish_stmt_expr_expr (statement, in_statement_expr);
5994     }
5995   else if (statement)
5996     statement = finish_expr_stmt (statement);
5997   else
5998     finish_stmt ();
5999
6000   return statement;
6001 }
6002
6003 /* Parse a compound-statement.
6004
6005    compound-statement:
6006      { statement-seq [opt] }
6007
6008    Returns a tree representing the statement.  */
6009
6010 static tree
6011 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6012                               bool in_try)
6013 {
6014   tree compound_stmt;
6015
6016   /* Consume the `{'.  */
6017   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6018     return error_mark_node;
6019   /* Begin the compound-statement.  */
6020   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6021   /* Parse an (optional) statement-seq.  */
6022   cp_parser_statement_seq_opt (parser, in_statement_expr);
6023   /* Finish the compound-statement.  */
6024   finish_compound_stmt (compound_stmt);
6025   /* Consume the `}'.  */
6026   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6027
6028   return compound_stmt;
6029 }
6030
6031 /* Parse an (optional) statement-seq.
6032
6033    statement-seq:
6034      statement
6035      statement-seq [opt] statement  */
6036
6037 static void
6038 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6039 {
6040   /* Scan statements until there aren't any more.  */
6041   while (true)
6042     {
6043       /* If we're looking at a `}', then we've run out of statements.  */
6044       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6045           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6046         break;
6047
6048       /* Parse the statement.  */
6049       cp_parser_statement (parser, in_statement_expr);
6050     }
6051 }
6052
6053 /* Parse a selection-statement.
6054
6055    selection-statement:
6056      if ( condition ) statement
6057      if ( condition ) statement else statement
6058      switch ( condition ) statement
6059
6060    Returns the new IF_STMT or SWITCH_STMT.  */
6061
6062 static tree
6063 cp_parser_selection_statement (cp_parser* parser)
6064 {
6065   cp_token *token;
6066   enum rid keyword;
6067
6068   /* Peek at the next token.  */
6069   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6070
6071   /* See what kind of keyword it is.  */
6072   keyword = token->keyword;
6073   switch (keyword)
6074     {
6075     case RID_IF:
6076     case RID_SWITCH:
6077       {
6078         tree statement;
6079         tree condition;
6080
6081         /* Look for the `('.  */
6082         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6083           {
6084             cp_parser_skip_to_end_of_statement (parser);
6085             return error_mark_node;
6086           }
6087
6088         /* Begin the selection-statement.  */
6089         if (keyword == RID_IF)
6090           statement = begin_if_stmt ();
6091         else
6092           statement = begin_switch_stmt ();
6093
6094         /* Parse the condition.  */
6095         condition = cp_parser_condition (parser);
6096         /* Look for the `)'.  */
6097         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6098           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6099                                                  /*consume_paren=*/true);
6100
6101         if (keyword == RID_IF)
6102           {
6103             /* Add the condition.  */
6104             finish_if_stmt_cond (condition, statement);
6105
6106             /* Parse the then-clause.  */
6107             cp_parser_implicitly_scoped_statement (parser);
6108             finish_then_clause (statement);
6109
6110             /* If the next token is `else', parse the else-clause.  */
6111             if (cp_lexer_next_token_is_keyword (parser->lexer,
6112                                                 RID_ELSE))
6113               {
6114                 /* Consume the `else' keyword.  */
6115                 cp_lexer_consume_token (parser->lexer);
6116                 begin_else_clause (statement);
6117                 /* Parse the else-clause.  */
6118                 cp_parser_implicitly_scoped_statement (parser);
6119                 finish_else_clause (statement);
6120               }
6121
6122             /* Now we're all done with the if-statement.  */
6123             finish_if_stmt (statement);
6124           }
6125         else
6126           {
6127             bool in_switch_statement_p;
6128
6129             /* Add the condition.  */
6130             finish_switch_cond (condition, statement);
6131
6132             /* Parse the body of the switch-statement.  */
6133             in_switch_statement_p = parser->in_switch_statement_p;
6134             parser->in_switch_statement_p = true;
6135             cp_parser_implicitly_scoped_statement (parser);
6136             parser->in_switch_statement_p = in_switch_statement_p;
6137
6138             /* Now we're all done with the switch-statement.  */
6139             finish_switch_stmt (statement);
6140           }
6141
6142         return statement;
6143       }
6144       break;
6145
6146     default:
6147       cp_parser_error (parser, "expected selection-statement");
6148       return error_mark_node;
6149     }
6150 }
6151
6152 /* Parse a condition.
6153
6154    condition:
6155      expression
6156      type-specifier-seq declarator = assignment-expression
6157
6158    GNU Extension:
6159
6160    condition:
6161      type-specifier-seq declarator asm-specification [opt]
6162        attributes [opt] = assignment-expression
6163
6164    Returns the expression that should be tested.  */
6165
6166 static tree
6167 cp_parser_condition (cp_parser* parser)
6168 {
6169   cp_decl_specifier_seq type_specifiers;
6170   const char *saved_message;
6171
6172   /* Try the declaration first.  */
6173   cp_parser_parse_tentatively (parser);
6174   /* New types are not allowed in the type-specifier-seq for a
6175      condition.  */
6176   saved_message = parser->type_definition_forbidden_message;
6177   parser->type_definition_forbidden_message
6178     = "types may not be defined in conditions";
6179   /* Parse the type-specifier-seq.  */
6180   cp_parser_type_specifier_seq (parser, &type_specifiers);
6181   /* Restore the saved message.  */
6182   parser->type_definition_forbidden_message = saved_message;
6183   /* If all is well, we might be looking at a declaration.  */
6184   if (!cp_parser_error_occurred (parser))
6185     {
6186       tree decl;
6187       tree asm_specification;
6188       tree attributes;
6189       cp_declarator *declarator;
6190       tree initializer = NULL_TREE;
6191
6192       /* Parse the declarator.  */
6193       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6194                                          /*ctor_dtor_or_conv_p=*/NULL,
6195                                          /*parenthesized_p=*/NULL,
6196                                          /*member_p=*/false);
6197       /* Parse the attributes.  */
6198       attributes = cp_parser_attributes_opt (parser);
6199       /* Parse the asm-specification.  */
6200       asm_specification = cp_parser_asm_specification_opt (parser);
6201       /* If the next token is not an `=', then we might still be
6202          looking at an expression.  For example:
6203
6204            if (A(a).x)
6205
6206          looks like a decl-specifier-seq and a declarator -- but then
6207          there is no `=', so this is an expression.  */
6208       cp_parser_require (parser, CPP_EQ, "`='");
6209       /* If we did see an `=', then we are looking at a declaration
6210          for sure.  */
6211       if (cp_parser_parse_definitely (parser))
6212         {
6213           bool pop_p;   
6214
6215           /* Create the declaration.  */
6216           decl = start_decl (declarator, &type_specifiers,
6217                              /*initialized_p=*/true,
6218                              attributes, /*prefix_attributes=*/NULL_TREE,
6219                              &pop_p);
6220           /* Parse the assignment-expression.  */
6221           initializer = cp_parser_assignment_expression (parser);
6222
6223           /* Process the initializer.  */
6224           cp_finish_decl (decl,
6225                           initializer,
6226                           asm_specification,
6227                           LOOKUP_ONLYCONVERTING);
6228
6229           if (pop_p)
6230             pop_scope (DECL_CONTEXT (decl));
6231
6232           return convert_from_reference (decl);
6233         }
6234     }
6235   /* If we didn't even get past the declarator successfully, we are
6236      definitely not looking at a declaration.  */
6237   else
6238     cp_parser_abort_tentative_parse (parser);
6239
6240   /* Otherwise, we are looking at an expression.  */
6241   return cp_parser_expression (parser);
6242 }
6243
6244 /* Parse an iteration-statement.
6245
6246    iteration-statement:
6247      while ( condition ) statement
6248      do statement while ( expression ) ;
6249      for ( for-init-statement condition [opt] ; expression [opt] )
6250        statement
6251
6252    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6253
6254 static tree
6255 cp_parser_iteration_statement (cp_parser* parser)
6256 {
6257   cp_token *token;
6258   enum rid keyword;
6259   tree statement;
6260   bool in_iteration_statement_p;
6261
6262
6263   /* Peek at the next token.  */
6264   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6265   if (!token)
6266     return error_mark_node;
6267
6268   /* Remember whether or not we are already within an iteration
6269      statement.  */
6270   in_iteration_statement_p = parser->in_iteration_statement_p;
6271
6272   /* See what kind of keyword it is.  */
6273   keyword = token->keyword;
6274   switch (keyword)
6275     {
6276     case RID_WHILE:
6277       {
6278         tree condition;
6279
6280         /* Begin the while-statement.  */
6281         statement = begin_while_stmt ();
6282         /* Look for the `('.  */
6283         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6284         /* Parse the condition.  */
6285         condition = cp_parser_condition (parser);
6286         finish_while_stmt_cond (condition, statement);
6287         /* Look for the `)'.  */
6288         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6289         /* Parse the dependent statement.  */
6290         parser->in_iteration_statement_p = true;
6291         cp_parser_already_scoped_statement (parser);
6292         parser->in_iteration_statement_p = in_iteration_statement_p;
6293         /* We're done with the while-statement.  */
6294         finish_while_stmt (statement);
6295       }
6296       break;
6297
6298     case RID_DO:
6299       {
6300         tree expression;
6301
6302         /* Begin the do-statement.  */
6303         statement = begin_do_stmt ();
6304         /* Parse the body of the do-statement.  */
6305         parser->in_iteration_statement_p = true;
6306         cp_parser_implicitly_scoped_statement (parser);
6307         parser->in_iteration_statement_p = in_iteration_statement_p;
6308         finish_do_body (statement);
6309         /* Look for the `while' keyword.  */
6310         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6311         /* Look for the `('.  */
6312         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6313         /* Parse the expression.  */
6314         expression = cp_parser_expression (parser);
6315         /* We're done with the do-statement.  */
6316         finish_do_stmt (expression, statement);
6317         /* Look for the `)'.  */
6318         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6319         /* Look for the `;'.  */
6320         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6321       }
6322       break;
6323
6324     case RID_FOR:
6325       {
6326         tree condition = NULL_TREE;
6327         tree expression = NULL_TREE;
6328
6329         /* Begin the for-statement.  */
6330         statement = begin_for_stmt ();
6331         /* Look for the `('.  */
6332         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6333         /* Parse the initialization.  */
6334         cp_parser_for_init_statement (parser);
6335         finish_for_init_stmt (statement);
6336
6337         /* If there's a condition, process it.  */
6338         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6339           condition = cp_parser_condition (parser);
6340         finish_for_cond (condition, statement);
6341         /* Look for the `;'.  */
6342         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6343
6344         /* If there's an expression, process it.  */
6345         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6346           expression = cp_parser_expression (parser);
6347         finish_for_expr (expression, statement);
6348         /* Look for the `)'.  */
6349         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6350
6351         /* Parse the body of the for-statement.  */
6352         parser->in_iteration_statement_p = true;
6353         cp_parser_already_scoped_statement (parser);
6354         parser->in_iteration_statement_p = in_iteration_statement_p;
6355
6356         /* We're done with the for-statement.  */
6357         finish_for_stmt (statement);
6358       }
6359       break;
6360
6361     default:
6362       cp_parser_error (parser, "expected iteration-statement");
6363       statement = error_mark_node;
6364       break;
6365     }
6366
6367   return statement;
6368 }
6369
6370 /* Parse a for-init-statement.
6371
6372    for-init-statement:
6373      expression-statement
6374      simple-declaration  */
6375
6376 static void
6377 cp_parser_for_init_statement (cp_parser* parser)
6378 {
6379   /* If the next token is a `;', then we have an empty
6380      expression-statement.  Grammatically, this is also a
6381      simple-declaration, but an invalid one, because it does not
6382      declare anything.  Therefore, if we did not handle this case
6383      specially, we would issue an error message about an invalid
6384      declaration.  */
6385   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6386     {
6387       /* We're going to speculatively look for a declaration, falling back
6388          to an expression, if necessary.  */
6389       cp_parser_parse_tentatively (parser);
6390       /* Parse the declaration.  */
6391       cp_parser_simple_declaration (parser,
6392                                     /*function_definition_allowed_p=*/false);
6393       /* If the tentative parse failed, then we shall need to look for an
6394          expression-statement.  */
6395       if (cp_parser_parse_definitely (parser))
6396         return;
6397     }
6398
6399   cp_parser_expression_statement (parser, false);
6400 }
6401
6402 /* Parse a jump-statement.
6403
6404    jump-statement:
6405      break ;
6406      continue ;
6407      return expression [opt] ;
6408      goto identifier ;
6409
6410    GNU extension:
6411
6412    jump-statement:
6413      goto * expression ;
6414
6415    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6416
6417 static tree
6418 cp_parser_jump_statement (cp_parser* parser)
6419 {
6420   tree statement = error_mark_node;
6421   cp_token *token;
6422   enum rid keyword;
6423
6424   /* Peek at the next token.  */
6425   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6426   if (!token)
6427     return error_mark_node;
6428
6429   /* See what kind of keyword it is.  */
6430   keyword = token->keyword;
6431   switch (keyword)
6432     {
6433     case RID_BREAK:
6434       if (!parser->in_switch_statement_p
6435           && !parser->in_iteration_statement_p)
6436         {
6437           error ("break statement not within loop or switch");
6438           statement = error_mark_node;
6439         }
6440       else
6441         statement = finish_break_stmt ();
6442       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6443       break;
6444
6445     case RID_CONTINUE:
6446       if (!parser->in_iteration_statement_p)
6447         {
6448           error ("continue statement not within a loop");
6449           statement = error_mark_node;
6450         }
6451       else
6452         statement = finish_continue_stmt ();
6453       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6454       break;
6455
6456     case RID_RETURN:
6457       {
6458         tree expr;
6459
6460         /* If the next token is a `;', then there is no
6461            expression.  */
6462         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6463           expr = cp_parser_expression (parser);
6464         else
6465           expr = NULL_TREE;
6466         /* Build the return-statement.  */
6467         statement = finish_return_stmt (expr);
6468         /* Look for the final `;'.  */
6469         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6470       }
6471       break;
6472
6473     case RID_GOTO:
6474       /* Create the goto-statement.  */
6475       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6476         {
6477           /* Issue a warning about this use of a GNU extension.  */
6478           if (pedantic)
6479             pedwarn ("ISO C++ forbids computed gotos");
6480           /* Consume the '*' token.  */
6481           cp_lexer_consume_token (parser->lexer);
6482           /* Parse the dependent expression.  */
6483           finish_goto_stmt (cp_parser_expression (parser));
6484         }
6485       else
6486         finish_goto_stmt (cp_parser_identifier (parser));
6487       /* Look for the final `;'.  */
6488       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6489       break;
6490
6491     default:
6492       cp_parser_error (parser, "expected jump-statement");
6493       break;
6494     }
6495
6496   return statement;
6497 }
6498
6499 /* Parse a declaration-statement.
6500
6501    declaration-statement:
6502      block-declaration  */
6503
6504 static void
6505 cp_parser_declaration_statement (cp_parser* parser)
6506 {
6507   void *p;
6508
6509   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6510   p = obstack_alloc (&declarator_obstack, 0);
6511
6512  /* Parse the block-declaration.  */
6513   cp_parser_block_declaration (parser, /*statement_p=*/true);
6514
6515   /* Free any declarators allocated.  */
6516   obstack_free (&declarator_obstack, p);
6517
6518   /* Finish off the statement.  */
6519   finish_stmt ();
6520 }
6521
6522 /* Some dependent statements (like `if (cond) statement'), are
6523    implicitly in their own scope.  In other words, if the statement is
6524    a single statement (as opposed to a compound-statement), it is
6525    none-the-less treated as if it were enclosed in braces.  Any
6526    declarations appearing in the dependent statement are out of scope
6527    after control passes that point.  This function parses a statement,
6528    but ensures that is in its own scope, even if it is not a
6529    compound-statement.
6530
6531    Returns the new statement.  */
6532
6533 static tree
6534 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6535 {
6536   tree statement;
6537
6538   /* If the token is not a `{', then we must take special action.  */
6539   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6540     {
6541       /* Create a compound-statement.  */
6542       statement = begin_compound_stmt (0);
6543       /* Parse the dependent-statement.  */
6544       cp_parser_statement (parser, false);
6545       /* Finish the dummy compound-statement.  */
6546       finish_compound_stmt (statement);
6547     }
6548   /* Otherwise, we simply parse the statement directly.  */
6549   else
6550     statement = cp_parser_compound_statement (parser, NULL, false);
6551
6552   /* Return the statement.  */
6553   return statement;
6554 }
6555
6556 /* For some dependent statements (like `while (cond) statement'), we
6557    have already created a scope.  Therefore, even if the dependent
6558    statement is a compound-statement, we do not want to create another
6559    scope.  */
6560
6561 static void
6562 cp_parser_already_scoped_statement (cp_parser* parser)
6563 {
6564   /* If the token is a `{', then we must take special action.  */
6565   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6566     cp_parser_statement (parser, false);
6567   else
6568     {
6569       /* Avoid calling cp_parser_compound_statement, so that we
6570          don't create a new scope.  Do everything else by hand.  */
6571       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6572       cp_parser_statement_seq_opt (parser, false);
6573       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6574     }
6575 }
6576
6577 /* Declarations [gram.dcl.dcl] */
6578
6579 /* Parse an optional declaration-sequence.
6580
6581    declaration-seq:
6582      declaration
6583      declaration-seq declaration  */
6584
6585 static void
6586 cp_parser_declaration_seq_opt (cp_parser* parser)
6587 {
6588   while (true)
6589     {
6590       cp_token *token;
6591
6592       token = cp_lexer_peek_token (parser->lexer);
6593
6594       if (token->type == CPP_CLOSE_BRACE
6595           || token->type == CPP_EOF)
6596         break;
6597
6598       if (token->type == CPP_SEMICOLON)
6599         {
6600           /* A declaration consisting of a single semicolon is
6601              invalid.  Allow it unless we're being pedantic.  */
6602           cp_lexer_consume_token (parser->lexer);
6603           if (pedantic && !in_system_header)
6604             pedwarn ("extra %<;%>");
6605           continue;
6606         }
6607
6608       /* If we're entering or exiting a region that's implicitly
6609          extern "C", modify the lang context appropriately.  */
6610       if (!parser->implicit_extern_c && token->implicit_extern_c)
6611         {
6612           push_lang_context (lang_name_c);
6613           parser->implicit_extern_c = true;
6614         }
6615       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6616         {
6617           pop_lang_context ();
6618           parser->implicit_extern_c = false;
6619         }
6620
6621       if (token->type == CPP_PRAGMA)
6622         {
6623           /* A top-level declaration can consist solely of a #pragma.
6624              A nested declaration cannot, so this is done here and not
6625              in cp_parser_declaration.  (A #pragma at block scope is
6626              handled in cp_parser_statement.)  */
6627           cp_lexer_handle_pragma (parser->lexer);
6628           continue;
6629         }
6630
6631       /* Parse the declaration itself.  */
6632       cp_parser_declaration (parser);
6633     }
6634 }
6635
6636 /* Parse a declaration.
6637
6638    declaration:
6639      block-declaration
6640      function-definition
6641      template-declaration
6642      explicit-instantiation
6643      explicit-specialization
6644      linkage-specification
6645      namespace-definition
6646
6647    GNU extension:
6648
6649    declaration:
6650       __extension__ declaration */
6651
6652 static void
6653 cp_parser_declaration (cp_parser* parser)
6654 {
6655   cp_token token1;
6656   cp_token token2;
6657   int saved_pedantic;
6658   void *p;
6659
6660   /* Check for the `__extension__' keyword.  */
6661   if (cp_parser_extension_opt (parser, &saved_pedantic))
6662     {
6663       /* Parse the qualified declaration.  */
6664       cp_parser_declaration (parser);
6665       /* Restore the PEDANTIC flag.  */
6666       pedantic = saved_pedantic;
6667
6668       return;
6669     }
6670
6671   /* Try to figure out what kind of declaration is present.  */
6672   token1 = *cp_lexer_peek_token (parser->lexer);
6673
6674   if (token1.type != CPP_EOF)
6675     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6676
6677   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6678   p = obstack_alloc (&declarator_obstack, 0);
6679
6680   /* If the next token is `extern' and the following token is a string
6681      literal, then we have a linkage specification.  */
6682   if (token1.keyword == RID_EXTERN
6683       && cp_parser_is_string_literal (&token2))
6684     cp_parser_linkage_specification (parser);
6685   /* If the next token is `template', then we have either a template
6686      declaration, an explicit instantiation, or an explicit
6687      specialization.  */
6688   else if (token1.keyword == RID_TEMPLATE)
6689     {
6690       /* `template <>' indicates a template specialization.  */
6691       if (token2.type == CPP_LESS
6692           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6693         cp_parser_explicit_specialization (parser);
6694       /* `template <' indicates a template declaration.  */
6695       else if (token2.type == CPP_LESS)
6696         cp_parser_template_declaration (parser, /*member_p=*/false);
6697       /* Anything else must be an explicit instantiation.  */
6698       else
6699         cp_parser_explicit_instantiation (parser);
6700     }
6701   /* If the next token is `export', then we have a template
6702      declaration.  */
6703   else if (token1.keyword == RID_EXPORT)
6704     cp_parser_template_declaration (parser, /*member_p=*/false);
6705   /* If the next token is `extern', 'static' or 'inline' and the one
6706      after that is `template', we have a GNU extended explicit
6707      instantiation directive.  */
6708   else if (cp_parser_allow_gnu_extensions_p (parser)
6709            && (token1.keyword == RID_EXTERN
6710                || token1.keyword == RID_STATIC
6711                || token1.keyword == RID_INLINE)
6712            && token2.keyword == RID_TEMPLATE)
6713     cp_parser_explicit_instantiation (parser);
6714   /* If the next token is `namespace', check for a named or unnamed
6715      namespace definition.  */
6716   else if (token1.keyword == RID_NAMESPACE
6717            && (/* A named namespace definition.  */
6718                (token2.type == CPP_NAME
6719                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6720                     == CPP_OPEN_BRACE))
6721                /* An unnamed namespace definition.  */
6722                || token2.type == CPP_OPEN_BRACE))
6723     cp_parser_namespace_definition (parser);
6724   /* We must have either a block declaration or a function
6725      definition.  */
6726   else
6727     /* Try to parse a block-declaration, or a function-definition.  */
6728     cp_parser_block_declaration (parser, /*statement_p=*/false);
6729
6730   /* Free any declarators allocated.  */
6731   obstack_free (&declarator_obstack, p);
6732 }
6733
6734 /* Parse a block-declaration.
6735
6736    block-declaration:
6737      simple-declaration
6738      asm-definition
6739      namespace-alias-definition
6740      using-declaration
6741      using-directive
6742
6743    GNU Extension:
6744
6745    block-declaration:
6746      __extension__ block-declaration
6747      label-declaration
6748
6749    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6750    part of a declaration-statement.  */
6751
6752 static void
6753 cp_parser_block_declaration (cp_parser *parser,
6754                              bool      statement_p)
6755 {
6756   cp_token *token1;
6757   int saved_pedantic;
6758
6759   /* Check for the `__extension__' keyword.  */
6760   if (cp_parser_extension_opt (parser, &saved_pedantic))
6761     {
6762       /* Parse the qualified declaration.  */
6763       cp_parser_block_declaration (parser, statement_p);
6764       /* Restore the PEDANTIC flag.  */
6765       pedantic = saved_pedantic;
6766
6767       return;
6768     }
6769
6770   /* Peek at the next token to figure out which kind of declaration is
6771      present.  */
6772   token1 = cp_lexer_peek_token (parser->lexer);
6773
6774   /* If the next keyword is `asm', we have an asm-definition.  */
6775   if (token1->keyword == RID_ASM)
6776     {
6777       if (statement_p)
6778         cp_parser_commit_to_tentative_parse (parser);
6779       cp_parser_asm_definition (parser);
6780     }
6781   /* If the next keyword is `namespace', we have a
6782      namespace-alias-definition.  */
6783   else if (token1->keyword == RID_NAMESPACE)
6784     cp_parser_namespace_alias_definition (parser);
6785   /* If the next keyword is `using', we have either a
6786      using-declaration or a using-directive.  */
6787   else if (token1->keyword == RID_USING)
6788     {
6789       cp_token *token2;
6790
6791       if (statement_p)
6792         cp_parser_commit_to_tentative_parse (parser);
6793       /* If the token after `using' is `namespace', then we have a
6794          using-directive.  */
6795       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6796       if (token2->keyword == RID_NAMESPACE)
6797         cp_parser_using_directive (parser);
6798       /* Otherwise, it's a using-declaration.  */
6799       else
6800         cp_parser_using_declaration (parser);
6801     }
6802   /* If the next keyword is `__label__' we have a label declaration.  */
6803   else if (token1->keyword == RID_LABEL)
6804     {
6805       if (statement_p)
6806         cp_parser_commit_to_tentative_parse (parser);
6807       cp_parser_label_declaration (parser);
6808     }
6809   /* Anything else must be a simple-declaration.  */
6810   else
6811     cp_parser_simple_declaration (parser, !statement_p);
6812 }
6813
6814 /* Parse a simple-declaration.
6815
6816    simple-declaration:
6817      decl-specifier-seq [opt] init-declarator-list [opt] ;
6818
6819    init-declarator-list:
6820      init-declarator
6821      init-declarator-list , init-declarator
6822
6823    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6824    function-definition as a simple-declaration.  */
6825
6826 static void
6827 cp_parser_simple_declaration (cp_parser* parser,
6828                               bool function_definition_allowed_p)
6829 {
6830   cp_decl_specifier_seq decl_specifiers;
6831   int declares_class_or_enum;
6832   bool saw_declarator;
6833
6834   /* Defer access checks until we know what is being declared; the
6835      checks for names appearing in the decl-specifier-seq should be
6836      done as if we were in the scope of the thing being declared.  */
6837   push_deferring_access_checks (dk_deferred);
6838
6839   /* Parse the decl-specifier-seq.  We have to keep track of whether
6840      or not the decl-specifier-seq declares a named class or
6841      enumeration type, since that is the only case in which the
6842      init-declarator-list is allowed to be empty.
6843
6844      [dcl.dcl]
6845
6846      In a simple-declaration, the optional init-declarator-list can be
6847      omitted only when declaring a class or enumeration, that is when
6848      the decl-specifier-seq contains either a class-specifier, an
6849      elaborated-type-specifier, or an enum-specifier.  */
6850   cp_parser_decl_specifier_seq (parser,
6851                                 CP_PARSER_FLAGS_OPTIONAL,
6852                                 &decl_specifiers,
6853                                 &declares_class_or_enum);
6854   /* We no longer need to defer access checks.  */
6855   stop_deferring_access_checks ();
6856
6857   /* In a block scope, a valid declaration must always have a
6858      decl-specifier-seq.  By not trying to parse declarators, we can
6859      resolve the declaration/expression ambiguity more quickly.  */
6860   if (!function_definition_allowed_p
6861       && !decl_specifiers.any_specifiers_p)
6862     {
6863       cp_parser_error (parser, "expected declaration");
6864       goto done;
6865     }
6866
6867   /* If the next two tokens are both identifiers, the code is
6868      erroneous. The usual cause of this situation is code like:
6869
6870        T t;
6871
6872      where "T" should name a type -- but does not.  */
6873   if (!decl_specifiers.type
6874       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6875     {
6876       /* If parsing tentatively, we should commit; we really are
6877          looking at a declaration.  */
6878       cp_parser_commit_to_tentative_parse (parser);
6879       /* Give up.  */
6880       goto done;
6881     }
6882   
6883   /* If we have seen at least one decl-specifier, and the next token
6884      is not a parenthesis, then we must be looking at a declaration.
6885      (After "int (" we might be looking at a functional cast.)  */
6886   if (decl_specifiers.any_specifiers_p 
6887       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6888     cp_parser_commit_to_tentative_parse (parser);
6889
6890   /* Keep going until we hit the `;' at the end of the simple
6891      declaration.  */
6892   saw_declarator = false;
6893   while (cp_lexer_next_token_is_not (parser->lexer,
6894                                      CPP_SEMICOLON))
6895     {
6896       cp_token *token;
6897       bool function_definition_p;
6898       tree decl;
6899
6900       saw_declarator = true;
6901       /* Parse the init-declarator.  */
6902       decl = cp_parser_init_declarator (parser, &decl_specifiers,
6903                                         function_definition_allowed_p,
6904                                         /*member_p=*/false,
6905                                         declares_class_or_enum,
6906                                         &function_definition_p);
6907       /* If an error occurred while parsing tentatively, exit quickly.
6908          (That usually happens when in the body of a function; each
6909          statement is treated as a declaration-statement until proven
6910          otherwise.)  */
6911       if (cp_parser_error_occurred (parser))
6912         goto done;
6913       /* Handle function definitions specially.  */
6914       if (function_definition_p)
6915         {
6916           /* If the next token is a `,', then we are probably
6917              processing something like:
6918
6919                void f() {}, *p;
6920
6921              which is erroneous.  */
6922           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6923             error ("mixing declarations and function-definitions is forbidden");
6924           /* Otherwise, we're done with the list of declarators.  */
6925           else
6926             {
6927               pop_deferring_access_checks ();
6928               return;
6929             }
6930         }
6931       /* The next token should be either a `,' or a `;'.  */
6932       token = cp_lexer_peek_token (parser->lexer);
6933       /* If it's a `,', there are more declarators to come.  */
6934       if (token->type == CPP_COMMA)
6935         cp_lexer_consume_token (parser->lexer);
6936       /* If it's a `;', we are done.  */
6937       else if (token->type == CPP_SEMICOLON)
6938         break;
6939       /* Anything else is an error.  */
6940       else
6941         {
6942           /* If we have already issued an error message we don't need
6943              to issue another one.  */
6944           if (decl != error_mark_node
6945               || cp_parser_uncommitted_to_tentative_parse_p (parser))
6946             cp_parser_error (parser, "expected %<,%> or %<;%>");
6947           /* Skip tokens until we reach the end of the statement.  */
6948           cp_parser_skip_to_end_of_statement (parser);
6949           /* If the next token is now a `;', consume it.  */
6950           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6951             cp_lexer_consume_token (parser->lexer);
6952           goto done;
6953         }
6954       /* After the first time around, a function-definition is not
6955          allowed -- even if it was OK at first.  For example:
6956
6957            int i, f() {}
6958
6959          is not valid.  */
6960       function_definition_allowed_p = false;
6961     }
6962
6963   /* Issue an error message if no declarators are present, and the
6964      decl-specifier-seq does not itself declare a class or
6965      enumeration.  */
6966   if (!saw_declarator)
6967     {
6968       if (cp_parser_declares_only_class_p (parser))
6969         shadow_tag (&decl_specifiers);
6970       /* Perform any deferred access checks.  */
6971       perform_deferred_access_checks ();
6972     }
6973
6974   /* Consume the `;'.  */
6975   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6976
6977  done:
6978   pop_deferring_access_checks ();
6979 }
6980
6981 /* Parse a decl-specifier-seq.
6982
6983    decl-specifier-seq:
6984      decl-specifier-seq [opt] decl-specifier
6985
6986    decl-specifier:
6987      storage-class-specifier
6988      type-specifier
6989      function-specifier
6990      friend
6991      typedef
6992
6993    GNU Extension:
6994
6995    decl-specifier:
6996      attributes
6997
6998    Set *DECL_SPECS to a representation of the decl-specifier-seq.
6999
7000    The parser flags FLAGS is used to control type-specifier parsing.
7001
7002    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7003    flags:
7004
7005      1: one of the decl-specifiers is an elaborated-type-specifier
7006         (i.e., a type declaration)
7007      2: one of the decl-specifiers is an enum-specifier or a
7008         class-specifier (i.e., a type definition)
7009
7010    */
7011
7012 static void
7013 cp_parser_decl_specifier_seq (cp_parser* parser,
7014                               cp_parser_flags flags,
7015                               cp_decl_specifier_seq *decl_specs,
7016                               int* declares_class_or_enum)
7017 {
7018   bool constructor_possible_p = !parser->in_declarator_p;
7019
7020   /* Clear DECL_SPECS.  */
7021   clear_decl_specs (decl_specs);
7022
7023   /* Assume no class or enumeration type is declared.  */
7024   *declares_class_or_enum = 0;
7025
7026   /* Keep reading specifiers until there are no more to read.  */
7027   while (true)
7028     {
7029       bool constructor_p;
7030       bool found_decl_spec;
7031       cp_token *token;
7032
7033       /* Peek at the next token.  */
7034       token = cp_lexer_peek_token (parser->lexer);
7035       /* Handle attributes.  */
7036       if (token->keyword == RID_ATTRIBUTE)
7037         {
7038           /* Parse the attributes.  */
7039           decl_specs->attributes
7040             = chainon (decl_specs->attributes,
7041                        cp_parser_attributes_opt (parser));
7042           continue;
7043         }
7044       /* Assume we will find a decl-specifier keyword.  */
7045       found_decl_spec = true;
7046       /* If the next token is an appropriate keyword, we can simply
7047          add it to the list.  */
7048       switch (token->keyword)
7049         {
7050           /* decl-specifier:
7051                friend  */
7052         case RID_FRIEND:
7053           if (decl_specs->specs[(int) ds_friend]++)
7054             error ("duplicate %<friend%>");
7055           /* Consume the token.  */
7056           cp_lexer_consume_token (parser->lexer);
7057           break;
7058
7059           /* function-specifier:
7060                inline
7061                virtual
7062                explicit  */
7063         case RID_INLINE:
7064         case RID_VIRTUAL:
7065         case RID_EXPLICIT:
7066           cp_parser_function_specifier_opt (parser, decl_specs);
7067           break;
7068
7069           /* decl-specifier:
7070                typedef  */
7071         case RID_TYPEDEF:
7072           ++decl_specs->specs[(int) ds_typedef];
7073           /* Consume the token.  */
7074           cp_lexer_consume_token (parser->lexer);
7075           /* A constructor declarator cannot appear in a typedef.  */
7076           constructor_possible_p = false;
7077           /* The "typedef" keyword can only occur in a declaration; we
7078              may as well commit at this point.  */
7079           cp_parser_commit_to_tentative_parse (parser);
7080           break;
7081
7082           /* storage-class-specifier:
7083                auto
7084                register
7085                static
7086                extern
7087                mutable
7088
7089              GNU Extension:
7090                thread  */
7091         case RID_AUTO:
7092           /* Consume the token.  */
7093           cp_lexer_consume_token (parser->lexer);
7094           cp_parser_set_storage_class (decl_specs, sc_auto);
7095           break;
7096         case RID_REGISTER:
7097           /* Consume the token.  */
7098           cp_lexer_consume_token (parser->lexer);
7099           cp_parser_set_storage_class (decl_specs, sc_register);
7100           break;
7101         case RID_STATIC:
7102           /* Consume the token.  */
7103           cp_lexer_consume_token (parser->lexer);
7104           if (decl_specs->specs[(int) ds_thread])
7105             {
7106               error ("%<__thread%> before %<static%>");
7107               decl_specs->specs[(int) ds_thread] = 0;
7108             }
7109           cp_parser_set_storage_class (decl_specs, sc_static);
7110           break;
7111         case RID_EXTERN:
7112           /* Consume the token.  */
7113           cp_lexer_consume_token (parser->lexer);
7114           if (decl_specs->specs[(int) ds_thread])
7115             {
7116               error ("%<__thread%> before %<extern%>");
7117               decl_specs->specs[(int) ds_thread] = 0;
7118             }
7119           cp_parser_set_storage_class (decl_specs, sc_extern);
7120           break;
7121         case RID_MUTABLE:
7122           /* Consume the token.  */
7123           cp_lexer_consume_token (parser->lexer);
7124           cp_parser_set_storage_class (decl_specs, sc_mutable);
7125           break;
7126         case RID_THREAD:
7127           /* Consume the token.  */
7128           cp_lexer_consume_token (parser->lexer);
7129           ++decl_specs->specs[(int) ds_thread];
7130           break;
7131
7132         default:
7133           /* We did not yet find a decl-specifier yet.  */
7134           found_decl_spec = false;
7135           break;
7136         }
7137
7138       /* Constructors are a special case.  The `S' in `S()' is not a
7139          decl-specifier; it is the beginning of the declarator.  */
7140       constructor_p
7141         = (!found_decl_spec
7142            && constructor_possible_p
7143            && (cp_parser_constructor_declarator_p
7144                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7145
7146       /* If we don't have a DECL_SPEC yet, then we must be looking at
7147          a type-specifier.  */
7148       if (!found_decl_spec && !constructor_p)
7149         {
7150           int decl_spec_declares_class_or_enum;
7151           bool is_cv_qualifier;
7152           tree type_spec;
7153
7154           type_spec
7155             = cp_parser_type_specifier (parser, flags,
7156                                         decl_specs,
7157                                         /*is_declaration=*/true,
7158                                         &decl_spec_declares_class_or_enum,
7159                                         &is_cv_qualifier);
7160
7161           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7162
7163           /* If this type-specifier referenced a user-defined type
7164              (a typedef, class-name, etc.), then we can't allow any
7165              more such type-specifiers henceforth.
7166
7167              [dcl.spec]
7168
7169              The longest sequence of decl-specifiers that could
7170              possibly be a type name is taken as the
7171              decl-specifier-seq of a declaration.  The sequence shall
7172              be self-consistent as described below.
7173
7174              [dcl.type]
7175
7176              As a general rule, at most one type-specifier is allowed
7177              in the complete decl-specifier-seq of a declaration.  The
7178              only exceptions are the following:
7179
7180              -- const or volatile can be combined with any other
7181                 type-specifier.
7182
7183              -- signed or unsigned can be combined with char, long,
7184                 short, or int.
7185
7186              -- ..
7187
7188              Example:
7189
7190                typedef char* Pc;
7191                void g (const int Pc);
7192
7193              Here, Pc is *not* part of the decl-specifier seq; it's
7194              the declarator.  Therefore, once we see a type-specifier
7195              (other than a cv-qualifier), we forbid any additional
7196              user-defined types.  We *do* still allow things like `int
7197              int' to be considered a decl-specifier-seq, and issue the
7198              error message later.  */
7199           if (type_spec && !is_cv_qualifier)
7200             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7201           /* A constructor declarator cannot follow a type-specifier.  */
7202           if (type_spec)
7203             {
7204               constructor_possible_p = false;
7205               found_decl_spec = true;
7206             }
7207         }
7208
7209       /* If we still do not have a DECL_SPEC, then there are no more
7210          decl-specifiers.  */
7211       if (!found_decl_spec)
7212         break;
7213
7214       decl_specs->any_specifiers_p = true;
7215       /* After we see one decl-specifier, further decl-specifiers are
7216          always optional.  */
7217       flags |= CP_PARSER_FLAGS_OPTIONAL;
7218     }
7219
7220   /* Don't allow a friend specifier with a class definition.  */
7221   if (decl_specs->specs[(int) ds_friend] != 0
7222       && (*declares_class_or_enum & 2))
7223     error ("class definition may not be declared a friend");
7224 }
7225
7226 /* Parse an (optional) storage-class-specifier.
7227
7228    storage-class-specifier:
7229      auto
7230      register
7231      static
7232      extern
7233      mutable
7234
7235    GNU Extension:
7236
7237    storage-class-specifier:
7238      thread
7239
7240    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7241
7242 static tree
7243 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7244 {
7245   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7246     {
7247     case RID_AUTO:
7248     case RID_REGISTER:
7249     case RID_STATIC:
7250     case RID_EXTERN:
7251     case RID_MUTABLE:
7252     case RID_THREAD:
7253       /* Consume the token.  */
7254       return cp_lexer_consume_token (parser->lexer)->value;
7255
7256     default:
7257       return NULL_TREE;
7258     }
7259 }
7260
7261 /* Parse an (optional) function-specifier.
7262
7263    function-specifier:
7264      inline
7265      virtual
7266      explicit
7267
7268    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7269    Updates DECL_SPECS, if it is non-NULL.  */
7270
7271 static tree
7272 cp_parser_function_specifier_opt (cp_parser* parser,
7273                                   cp_decl_specifier_seq *decl_specs)
7274 {
7275   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7276     {
7277     case RID_INLINE:
7278       if (decl_specs)
7279         ++decl_specs->specs[(int) ds_inline];
7280       break;
7281
7282     case RID_VIRTUAL:
7283       if (decl_specs)
7284         ++decl_specs->specs[(int) ds_virtual];
7285       break;
7286
7287     case RID_EXPLICIT:
7288       if (decl_specs)
7289         ++decl_specs->specs[(int) ds_explicit];
7290       break;
7291
7292     default:
7293       return NULL_TREE;
7294     }
7295
7296   /* Consume the token.  */
7297   return cp_lexer_consume_token (parser->lexer)->value;
7298 }
7299
7300 /* Parse a linkage-specification.
7301
7302    linkage-specification:
7303      extern string-literal { declaration-seq [opt] }
7304      extern string-literal declaration  */
7305
7306 static void
7307 cp_parser_linkage_specification (cp_parser* parser)
7308 {
7309   tree linkage;
7310
7311   /* Look for the `extern' keyword.  */
7312   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7313
7314   /* Look for the string-literal.  */
7315   linkage = cp_parser_string_literal (parser, false, false);
7316
7317   /* Transform the literal into an identifier.  If the literal is a
7318      wide-character string, or contains embedded NULs, then we can't
7319      handle it as the user wants.  */
7320   if (strlen (TREE_STRING_POINTER (linkage))
7321       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7322     {
7323       cp_parser_error (parser, "invalid linkage-specification");
7324       /* Assume C++ linkage.  */
7325       linkage = lang_name_cplusplus;
7326     }
7327   else
7328     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7329
7330   /* We're now using the new linkage.  */
7331   push_lang_context (linkage);
7332
7333   /* If the next token is a `{', then we're using the first
7334      production.  */
7335   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7336     {
7337       /* Consume the `{' token.  */
7338       cp_lexer_consume_token (parser->lexer);
7339       /* Parse the declarations.  */
7340       cp_parser_declaration_seq_opt (parser);
7341       /* Look for the closing `}'.  */
7342       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7343     }
7344   /* Otherwise, there's just one declaration.  */
7345   else
7346     {
7347       bool saved_in_unbraced_linkage_specification_p;
7348
7349       saved_in_unbraced_linkage_specification_p
7350         = parser->in_unbraced_linkage_specification_p;
7351       parser->in_unbraced_linkage_specification_p = true;
7352       have_extern_spec = true;
7353       cp_parser_declaration (parser);
7354       have_extern_spec = false;
7355       parser->in_unbraced_linkage_specification_p
7356         = saved_in_unbraced_linkage_specification_p;
7357     }
7358
7359   /* We're done with the linkage-specification.  */
7360   pop_lang_context ();
7361 }
7362
7363 /* Special member functions [gram.special] */
7364
7365 /* Parse a conversion-function-id.
7366
7367    conversion-function-id:
7368      operator conversion-type-id
7369
7370    Returns an IDENTIFIER_NODE representing the operator.  */
7371
7372 static tree
7373 cp_parser_conversion_function_id (cp_parser* parser)
7374 {
7375   tree type;
7376   tree saved_scope;
7377   tree saved_qualifying_scope;
7378   tree saved_object_scope;
7379   bool pop_p = false;
7380
7381   /* Look for the `operator' token.  */
7382   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7383     return error_mark_node;
7384   /* When we parse the conversion-type-id, the current scope will be
7385      reset.  However, we need that information in able to look up the
7386      conversion function later, so we save it here.  */
7387   saved_scope = parser->scope;
7388   saved_qualifying_scope = parser->qualifying_scope;
7389   saved_object_scope = parser->object_scope;
7390   /* We must enter the scope of the class so that the names of
7391      entities declared within the class are available in the
7392      conversion-type-id.  For example, consider:
7393
7394        struct S {
7395          typedef int I;
7396          operator I();
7397        };
7398
7399        S::operator I() { ... }
7400
7401      In order to see that `I' is a type-name in the definition, we
7402      must be in the scope of `S'.  */
7403   if (saved_scope)
7404     pop_p = push_scope (saved_scope);
7405   /* Parse the conversion-type-id.  */
7406   type = cp_parser_conversion_type_id (parser);
7407   /* Leave the scope of the class, if any.  */
7408   if (pop_p)
7409     pop_scope (saved_scope);
7410   /* Restore the saved scope.  */
7411   parser->scope = saved_scope;
7412   parser->qualifying_scope = saved_qualifying_scope;
7413   parser->object_scope = saved_object_scope;
7414   /* If the TYPE is invalid, indicate failure.  */
7415   if (type == error_mark_node)
7416     return error_mark_node;
7417   return mangle_conv_op_name_for_type (type);
7418 }
7419
7420 /* Parse a conversion-type-id:
7421
7422    conversion-type-id:
7423      type-specifier-seq conversion-declarator [opt]
7424
7425    Returns the TYPE specified.  */
7426
7427 static tree
7428 cp_parser_conversion_type_id (cp_parser* parser)
7429 {
7430   tree attributes;
7431   cp_decl_specifier_seq type_specifiers;
7432   cp_declarator *declarator;
7433   tree type_specified;
7434
7435   /* Parse the attributes.  */
7436   attributes = cp_parser_attributes_opt (parser);
7437   /* Parse the type-specifiers.  */
7438   cp_parser_type_specifier_seq (parser, &type_specifiers);
7439   /* If that didn't work, stop.  */
7440   if (type_specifiers.type == error_mark_node)
7441     return error_mark_node;
7442   /* Parse the conversion-declarator.  */
7443   declarator = cp_parser_conversion_declarator_opt (parser);
7444
7445   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7446                                     /*initialized=*/0, &attributes);
7447   if (attributes)
7448     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7449   return type_specified;
7450 }
7451
7452 /* Parse an (optional) conversion-declarator.
7453
7454    conversion-declarator:
7455      ptr-operator conversion-declarator [opt]
7456
7457    */
7458
7459 static cp_declarator *
7460 cp_parser_conversion_declarator_opt (cp_parser* parser)
7461 {
7462   enum tree_code code;
7463   tree class_type;
7464   cp_cv_quals cv_quals;
7465
7466   /* We don't know if there's a ptr-operator next, or not.  */
7467   cp_parser_parse_tentatively (parser);
7468   /* Try the ptr-operator.  */
7469   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7470   /* If it worked, look for more conversion-declarators.  */
7471   if (cp_parser_parse_definitely (parser))
7472     {
7473       cp_declarator *declarator;
7474
7475       /* Parse another optional declarator.  */
7476       declarator = cp_parser_conversion_declarator_opt (parser);
7477
7478       /* Create the representation of the declarator.  */
7479       if (class_type)
7480         declarator = make_ptrmem_declarator (cv_quals, class_type,
7481                                              declarator);
7482       else if (code == INDIRECT_REF)
7483         declarator = make_pointer_declarator (cv_quals, declarator);
7484       else
7485         declarator = make_reference_declarator (cv_quals, declarator);
7486
7487       return declarator;
7488    }
7489
7490   return NULL;
7491 }
7492
7493 /* Parse an (optional) ctor-initializer.
7494
7495    ctor-initializer:
7496      : mem-initializer-list
7497
7498    Returns TRUE iff the ctor-initializer was actually present.  */
7499
7500 static bool
7501 cp_parser_ctor_initializer_opt (cp_parser* parser)
7502 {
7503   /* If the next token is not a `:', then there is no
7504      ctor-initializer.  */
7505   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7506     {
7507       /* Do default initialization of any bases and members.  */
7508       if (DECL_CONSTRUCTOR_P (current_function_decl))
7509         finish_mem_initializers (NULL_TREE);
7510
7511       return false;
7512     }
7513
7514   /* Consume the `:' token.  */
7515   cp_lexer_consume_token (parser->lexer);
7516   /* And the mem-initializer-list.  */
7517   cp_parser_mem_initializer_list (parser);
7518
7519   return true;
7520 }
7521
7522 /* Parse a mem-initializer-list.
7523
7524    mem-initializer-list:
7525      mem-initializer
7526      mem-initializer , mem-initializer-list  */
7527
7528 static void
7529 cp_parser_mem_initializer_list (cp_parser* parser)
7530 {
7531   tree mem_initializer_list = NULL_TREE;
7532
7533   /* Let the semantic analysis code know that we are starting the
7534      mem-initializer-list.  */
7535   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7536     error ("only constructors take base initializers");
7537
7538   /* Loop through the list.  */
7539   while (true)
7540     {
7541       tree mem_initializer;
7542
7543       /* Parse the mem-initializer.  */
7544       mem_initializer = cp_parser_mem_initializer (parser);
7545       /* Add it to the list, unless it was erroneous.  */
7546       if (mem_initializer)
7547         {
7548           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7549           mem_initializer_list = mem_initializer;
7550         }
7551       /* If the next token is not a `,', we're done.  */
7552       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7553         break;
7554       /* Consume the `,' token.  */
7555       cp_lexer_consume_token (parser->lexer);
7556     }
7557
7558   /* Perform semantic analysis.  */
7559   if (DECL_CONSTRUCTOR_P (current_function_decl))
7560     finish_mem_initializers (mem_initializer_list);
7561 }
7562
7563 /* Parse a mem-initializer.
7564
7565    mem-initializer:
7566      mem-initializer-id ( expression-list [opt] )
7567
7568    GNU extension:
7569
7570    mem-initializer:
7571      ( expression-list [opt] )
7572
7573    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7574    class) or FIELD_DECL (for a non-static data member) to initialize;
7575    the TREE_VALUE is the expression-list.  */
7576
7577 static tree
7578 cp_parser_mem_initializer (cp_parser* parser)
7579 {
7580   tree mem_initializer_id;
7581   tree expression_list;
7582   tree member;
7583
7584   /* Find out what is being initialized.  */
7585   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7586     {
7587       pedwarn ("anachronistic old-style base class initializer");
7588       mem_initializer_id = NULL_TREE;
7589     }
7590   else
7591     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7592   member = expand_member_init (mem_initializer_id);
7593   if (member && !DECL_P (member))
7594     in_base_initializer = 1;
7595
7596   expression_list
7597     = cp_parser_parenthesized_expression_list (parser, false,
7598                                                /*non_constant_p=*/NULL);
7599   if (!expression_list)
7600     expression_list = void_type_node;
7601
7602   in_base_initializer = 0;
7603
7604   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7605 }
7606
7607 /* Parse a mem-initializer-id.
7608
7609    mem-initializer-id:
7610      :: [opt] nested-name-specifier [opt] class-name
7611      identifier
7612
7613    Returns a TYPE indicating the class to be initializer for the first
7614    production.  Returns an IDENTIFIER_NODE indicating the data member
7615    to be initialized for the second production.  */
7616
7617 static tree
7618 cp_parser_mem_initializer_id (cp_parser* parser)
7619 {
7620   bool global_scope_p;
7621   bool nested_name_specifier_p;
7622   bool template_p = false;
7623   tree id;
7624
7625   /* `typename' is not allowed in this context ([temp.res]).  */
7626   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7627     {
7628       error ("keyword %<typename%> not allowed in this context (a qualified "
7629              "member initializer is implicitly a type)");
7630       cp_lexer_consume_token (parser->lexer);
7631     }
7632   /* Look for the optional `::' operator.  */
7633   global_scope_p
7634     = (cp_parser_global_scope_opt (parser,
7635                                    /*current_scope_valid_p=*/false)
7636        != NULL_TREE);
7637   /* Look for the optional nested-name-specifier.  The simplest way to
7638      implement:
7639
7640        [temp.res]
7641
7642        The keyword `typename' is not permitted in a base-specifier or
7643        mem-initializer; in these contexts a qualified name that
7644        depends on a template-parameter is implicitly assumed to be a
7645        type name.
7646
7647      is to assume that we have seen the `typename' keyword at this
7648      point.  */
7649   nested_name_specifier_p
7650     = (cp_parser_nested_name_specifier_opt (parser,
7651                                             /*typename_keyword_p=*/true,
7652                                             /*check_dependency_p=*/true,
7653                                             /*type_p=*/true,
7654                                             /*is_declaration=*/true)
7655        != NULL_TREE);
7656   if (nested_name_specifier_p)
7657     template_p = cp_parser_optional_template_keyword (parser);
7658   /* If there is a `::' operator or a nested-name-specifier, then we
7659      are definitely looking for a class-name.  */
7660   if (global_scope_p || nested_name_specifier_p)
7661     return cp_parser_class_name (parser,
7662                                  /*typename_keyword_p=*/true,
7663                                  /*template_keyword_p=*/template_p,
7664                                  none_type,
7665                                  /*check_dependency_p=*/true,
7666                                  /*class_head_p=*/false,
7667                                  /*is_declaration=*/true);
7668   /* Otherwise, we could also be looking for an ordinary identifier.  */
7669   cp_parser_parse_tentatively (parser);
7670   /* Try a class-name.  */
7671   id = cp_parser_class_name (parser,
7672                              /*typename_keyword_p=*/true,
7673                              /*template_keyword_p=*/false,
7674                              none_type,
7675                              /*check_dependency_p=*/true,
7676                              /*class_head_p=*/false,
7677                              /*is_declaration=*/true);
7678   /* If we found one, we're done.  */
7679   if (cp_parser_parse_definitely (parser))
7680     return id;
7681   /* Otherwise, look for an ordinary identifier.  */
7682   return cp_parser_identifier (parser);
7683 }
7684
7685 /* Overloading [gram.over] */
7686
7687 /* Parse an operator-function-id.
7688
7689    operator-function-id:
7690      operator operator
7691
7692    Returns an IDENTIFIER_NODE for the operator which is a
7693    human-readable spelling of the identifier, e.g., `operator +'.  */
7694
7695 static tree
7696 cp_parser_operator_function_id (cp_parser* parser)
7697 {
7698   /* Look for the `operator' keyword.  */
7699   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7700     return error_mark_node;
7701   /* And then the name of the operator itself.  */
7702   return cp_parser_operator (parser);
7703 }
7704
7705 /* Parse an operator.
7706
7707    operator:
7708      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7709      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7710      || ++ -- , ->* -> () []
7711
7712    GNU Extensions:
7713
7714    operator:
7715      <? >? <?= >?=
7716
7717    Returns an IDENTIFIER_NODE for the operator which is a
7718    human-readable spelling of the identifier, e.g., `operator +'.  */
7719
7720 static tree
7721 cp_parser_operator (cp_parser* parser)
7722 {
7723   tree id = NULL_TREE;
7724   cp_token *token;
7725
7726   /* Peek at the next token.  */
7727   token = cp_lexer_peek_token (parser->lexer);
7728   /* Figure out which operator we have.  */
7729   switch (token->type)
7730     {
7731     case CPP_KEYWORD:
7732       {
7733         enum tree_code op;
7734
7735         /* The keyword should be either `new' or `delete'.  */
7736         if (token->keyword == RID_NEW)
7737           op = NEW_EXPR;
7738         else if (token->keyword == RID_DELETE)
7739           op = DELETE_EXPR;
7740         else
7741           break;
7742
7743         /* Consume the `new' or `delete' token.  */
7744         cp_lexer_consume_token (parser->lexer);
7745
7746         /* Peek at the next token.  */
7747         token = cp_lexer_peek_token (parser->lexer);
7748         /* If it's a `[' token then this is the array variant of the
7749            operator.  */
7750         if (token->type == CPP_OPEN_SQUARE)
7751           {
7752             /* Consume the `[' token.  */
7753             cp_lexer_consume_token (parser->lexer);
7754             /* Look for the `]' token.  */
7755             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7756             id = ansi_opname (op == NEW_EXPR
7757                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7758           }
7759         /* Otherwise, we have the non-array variant.  */
7760         else
7761           id = ansi_opname (op);
7762
7763         return id;
7764       }
7765
7766     case CPP_PLUS:
7767       id = ansi_opname (PLUS_EXPR);
7768       break;
7769
7770     case CPP_MINUS:
7771       id = ansi_opname (MINUS_EXPR);
7772       break;
7773
7774     case CPP_MULT:
7775       id = ansi_opname (MULT_EXPR);
7776       break;
7777
7778     case CPP_DIV:
7779       id = ansi_opname (TRUNC_DIV_EXPR);
7780       break;
7781
7782     case CPP_MOD:
7783       id = ansi_opname (TRUNC_MOD_EXPR);
7784       break;
7785
7786     case CPP_XOR:
7787       id = ansi_opname (BIT_XOR_EXPR);
7788       break;
7789
7790     case CPP_AND:
7791       id = ansi_opname (BIT_AND_EXPR);
7792       break;
7793
7794     case CPP_OR:
7795       id = ansi_opname (BIT_IOR_EXPR);
7796       break;
7797
7798     case CPP_COMPL:
7799       id = ansi_opname (BIT_NOT_EXPR);
7800       break;
7801
7802     case CPP_NOT:
7803       id = ansi_opname (TRUTH_NOT_EXPR);
7804       break;
7805
7806     case CPP_EQ:
7807       id = ansi_assopname (NOP_EXPR);
7808       break;
7809
7810     case CPP_LESS:
7811       id = ansi_opname (LT_EXPR);
7812       break;
7813
7814     case CPP_GREATER:
7815       id = ansi_opname (GT_EXPR);
7816       break;
7817
7818     case CPP_PLUS_EQ:
7819       id = ansi_assopname (PLUS_EXPR);
7820       break;
7821
7822     case CPP_MINUS_EQ:
7823       id = ansi_assopname (MINUS_EXPR);
7824       break;
7825
7826     case CPP_MULT_EQ:
7827       id = ansi_assopname (MULT_EXPR);
7828       break;
7829
7830     case CPP_DIV_EQ:
7831       id = ansi_assopname (TRUNC_DIV_EXPR);
7832       break;
7833
7834     case CPP_MOD_EQ:
7835       id = ansi_assopname (TRUNC_MOD_EXPR);
7836       break;
7837
7838     case CPP_XOR_EQ:
7839       id = ansi_assopname (BIT_XOR_EXPR);
7840       break;
7841
7842     case CPP_AND_EQ:
7843       id = ansi_assopname (BIT_AND_EXPR);
7844       break;
7845
7846     case CPP_OR_EQ:
7847       id = ansi_assopname (BIT_IOR_EXPR);
7848       break;
7849
7850     case CPP_LSHIFT:
7851       id = ansi_opname (LSHIFT_EXPR);
7852       break;
7853
7854     case CPP_RSHIFT:
7855       id = ansi_opname (RSHIFT_EXPR);
7856       break;
7857
7858     case CPP_LSHIFT_EQ:
7859       id = ansi_assopname (LSHIFT_EXPR);
7860       break;
7861
7862     case CPP_RSHIFT_EQ:
7863       id = ansi_assopname (RSHIFT_EXPR);
7864       break;
7865
7866     case CPP_EQ_EQ:
7867       id = ansi_opname (EQ_EXPR);
7868       break;
7869
7870     case CPP_NOT_EQ:
7871       id = ansi_opname (NE_EXPR);
7872       break;
7873
7874     case CPP_LESS_EQ:
7875       id = ansi_opname (LE_EXPR);
7876       break;
7877
7878     case CPP_GREATER_EQ:
7879       id = ansi_opname (GE_EXPR);
7880       break;
7881
7882     case CPP_AND_AND:
7883       id = ansi_opname (TRUTH_ANDIF_EXPR);
7884       break;
7885
7886     case CPP_OR_OR:
7887       id = ansi_opname (TRUTH_ORIF_EXPR);
7888       break;
7889
7890     case CPP_PLUS_PLUS:
7891       id = ansi_opname (POSTINCREMENT_EXPR);
7892       break;
7893
7894     case CPP_MINUS_MINUS:
7895       id = ansi_opname (PREDECREMENT_EXPR);
7896       break;
7897
7898     case CPP_COMMA:
7899       id = ansi_opname (COMPOUND_EXPR);
7900       break;
7901
7902     case CPP_DEREF_STAR:
7903       id = ansi_opname (MEMBER_REF);
7904       break;
7905
7906     case CPP_DEREF:
7907       id = ansi_opname (COMPONENT_REF);
7908       break;
7909
7910     case CPP_OPEN_PAREN:
7911       /* Consume the `('.  */
7912       cp_lexer_consume_token (parser->lexer);
7913       /* Look for the matching `)'.  */
7914       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7915       return ansi_opname (CALL_EXPR);
7916
7917     case CPP_OPEN_SQUARE:
7918       /* Consume the `['.  */
7919       cp_lexer_consume_token (parser->lexer);
7920       /* Look for the matching `]'.  */
7921       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7922       return ansi_opname (ARRAY_REF);
7923
7924       /* Extensions.  */
7925     case CPP_MIN:
7926       id = ansi_opname (MIN_EXPR);
7927       break;
7928
7929     case CPP_MAX:
7930       id = ansi_opname (MAX_EXPR);
7931       break;
7932
7933     case CPP_MIN_EQ:
7934       id = ansi_assopname (MIN_EXPR);
7935       break;
7936
7937     case CPP_MAX_EQ:
7938       id = ansi_assopname (MAX_EXPR);
7939       break;
7940
7941     default:
7942       /* Anything else is an error.  */
7943       break;
7944     }
7945
7946   /* If we have selected an identifier, we need to consume the
7947      operator token.  */
7948   if (id)
7949     cp_lexer_consume_token (parser->lexer);
7950   /* Otherwise, no valid operator name was present.  */
7951   else
7952     {
7953       cp_parser_error (parser, "expected operator");
7954       id = error_mark_node;
7955     }
7956
7957   return id;
7958 }
7959
7960 /* Parse a template-declaration.
7961
7962    template-declaration:
7963      export [opt] template < template-parameter-list > declaration
7964
7965    If MEMBER_P is TRUE, this template-declaration occurs within a
7966    class-specifier.
7967
7968    The grammar rule given by the standard isn't correct.  What
7969    is really meant is:
7970
7971    template-declaration:
7972      export [opt] template-parameter-list-seq
7973        decl-specifier-seq [opt] init-declarator [opt] ;
7974      export [opt] template-parameter-list-seq
7975        function-definition
7976
7977    template-parameter-list-seq:
7978      template-parameter-list-seq [opt]
7979      template < template-parameter-list >  */
7980
7981 static void
7982 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7983 {
7984   /* Check for `export'.  */
7985   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7986     {
7987       /* Consume the `export' token.  */
7988       cp_lexer_consume_token (parser->lexer);
7989       /* Warn that we do not support `export'.  */
7990       warning ("keyword %<export%> not implemented, and will be ignored");
7991     }
7992
7993   cp_parser_template_declaration_after_export (parser, member_p);
7994 }
7995
7996 /* Parse a template-parameter-list.
7997
7998    template-parameter-list:
7999      template-parameter
8000      template-parameter-list , template-parameter
8001
8002    Returns a TREE_LIST.  Each node represents a template parameter.
8003    The nodes are connected via their TREE_CHAINs.  */
8004
8005 static tree
8006 cp_parser_template_parameter_list (cp_parser* parser)
8007 {
8008   tree parameter_list = NULL_TREE;
8009
8010   while (true)
8011     {
8012       tree parameter;
8013       cp_token *token;
8014       bool is_non_type;
8015
8016       /* Parse the template-parameter.  */
8017       parameter = cp_parser_template_parameter (parser, &is_non_type);
8018       /* Add it to the list.  */
8019       parameter_list = process_template_parm (parameter_list,
8020                                               parameter,
8021                                               is_non_type);
8022       /* Peek at the next token.  */
8023       token = cp_lexer_peek_token (parser->lexer);
8024       /* If it's not a `,', we're done.  */
8025       if (token->type != CPP_COMMA)
8026         break;
8027       /* Otherwise, consume the `,' token.  */
8028       cp_lexer_consume_token (parser->lexer);
8029     }
8030
8031   return parameter_list;
8032 }
8033
8034 /* Parse a template-parameter.
8035
8036    template-parameter:
8037      type-parameter
8038      parameter-declaration
8039
8040    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8041    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8042    true iff this parameter is a non-type parameter.  */
8043
8044 static tree
8045 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8046 {
8047   cp_token *token;
8048   cp_parameter_declarator *parameter_declarator;
8049
8050   /* Assume it is a type parameter or a template parameter.  */
8051   *is_non_type = false;
8052   /* Peek at the next token.  */
8053   token = cp_lexer_peek_token (parser->lexer);
8054   /* If it is `class' or `template', we have a type-parameter.  */
8055   if (token->keyword == RID_TEMPLATE)
8056     return cp_parser_type_parameter (parser);
8057   /* If it is `class' or `typename' we do not know yet whether it is a
8058      type parameter or a non-type parameter.  Consider:
8059
8060        template <typename T, typename T::X X> ...
8061
8062      or:
8063
8064        template <class C, class D*> ...
8065
8066      Here, the first parameter is a type parameter, and the second is
8067      a non-type parameter.  We can tell by looking at the token after
8068      the identifier -- if it is a `,', `=', or `>' then we have a type
8069      parameter.  */
8070   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8071     {
8072       /* Peek at the token after `class' or `typename'.  */
8073       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8074       /* If it's an identifier, skip it.  */
8075       if (token->type == CPP_NAME)
8076         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8077       /* Now, see if the token looks like the end of a template
8078          parameter.  */
8079       if (token->type == CPP_COMMA
8080           || token->type == CPP_EQ
8081           || token->type == CPP_GREATER)
8082         return cp_parser_type_parameter (parser);
8083     }
8084
8085   /* Otherwise, it is a non-type parameter.
8086
8087      [temp.param]
8088
8089      When parsing a default template-argument for a non-type
8090      template-parameter, the first non-nested `>' is taken as the end
8091      of the template parameter-list rather than a greater-than
8092      operator.  */
8093   *is_non_type = true;
8094   parameter_declarator
8095      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8096                                         /*parenthesized_p=*/NULL);
8097   return (build_tree_list
8098           (parameter_declarator->default_argument,
8099            grokdeclarator (parameter_declarator->declarator,
8100                            &parameter_declarator->decl_specifiers,
8101                            PARM, /*initialized=*/0,
8102                            /*attrlist=*/NULL)));
8103 }
8104
8105 /* Parse a type-parameter.
8106
8107    type-parameter:
8108      class identifier [opt]
8109      class identifier [opt] = type-id
8110      typename identifier [opt]
8111      typename identifier [opt] = type-id
8112      template < template-parameter-list > class identifier [opt]
8113      template < template-parameter-list > class identifier [opt]
8114        = id-expression
8115
8116    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8117    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8118    the declaration of the parameter.  */
8119
8120 static tree
8121 cp_parser_type_parameter (cp_parser* parser)
8122 {
8123   cp_token *token;
8124   tree parameter;
8125
8126   /* Look for a keyword to tell us what kind of parameter this is.  */
8127   token = cp_parser_require (parser, CPP_KEYWORD,
8128                              "`class', `typename', or `template'");
8129   if (!token)
8130     return error_mark_node;
8131
8132   switch (token->keyword)
8133     {
8134     case RID_CLASS:
8135     case RID_TYPENAME:
8136       {
8137         tree identifier;
8138         tree default_argument;
8139
8140         /* If the next token is an identifier, then it names the
8141            parameter.  */
8142         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8143           identifier = cp_parser_identifier (parser);
8144         else
8145           identifier = NULL_TREE;
8146
8147         /* Create the parameter.  */
8148         parameter = finish_template_type_parm (class_type_node, identifier);
8149
8150         /* If the next token is an `=', we have a default argument.  */
8151         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8152           {
8153             /* Consume the `=' token.  */
8154             cp_lexer_consume_token (parser->lexer);
8155             /* Parse the default-argument.  */
8156             default_argument = cp_parser_type_id (parser);
8157           }
8158         else
8159           default_argument = NULL_TREE;
8160
8161         /* Create the combined representation of the parameter and the
8162            default argument.  */
8163         parameter = build_tree_list (default_argument, parameter);
8164       }
8165       break;
8166
8167     case RID_TEMPLATE:
8168       {
8169         tree parameter_list;
8170         tree identifier;
8171         tree default_argument;
8172
8173         /* Look for the `<'.  */
8174         cp_parser_require (parser, CPP_LESS, "`<'");
8175         /* Parse the template-parameter-list.  */
8176         begin_template_parm_list ();
8177         parameter_list
8178           = cp_parser_template_parameter_list (parser);
8179         parameter_list = end_template_parm_list (parameter_list);
8180         /* Look for the `>'.  */
8181         cp_parser_require (parser, CPP_GREATER, "`>'");
8182         /* Look for the `class' keyword.  */
8183         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8184         /* If the next token is an `=', then there is a
8185            default-argument.  If the next token is a `>', we are at
8186            the end of the parameter-list.  If the next token is a `,',
8187            then we are at the end of this parameter.  */
8188         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8189             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8190             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8191           {
8192             identifier = cp_parser_identifier (parser);
8193             /* Treat invalid names as if the parameter were nameless.  */
8194             if (identifier == error_mark_node)
8195               identifier = NULL_TREE;
8196           }
8197         else
8198           identifier = NULL_TREE;
8199
8200         /* Create the template parameter.  */
8201         parameter = finish_template_template_parm (class_type_node,
8202                                                    identifier);
8203
8204         /* If the next token is an `=', then there is a
8205            default-argument.  */
8206         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8207           {
8208             bool is_template;
8209
8210             /* Consume the `='.  */
8211             cp_lexer_consume_token (parser->lexer);
8212             /* Parse the id-expression.  */
8213             default_argument
8214               = cp_parser_id_expression (parser,
8215                                          /*template_keyword_p=*/false,
8216                                          /*check_dependency_p=*/true,
8217                                          /*template_p=*/&is_template,
8218                                          /*declarator_p=*/false);
8219             if (TREE_CODE (default_argument) == TYPE_DECL)
8220               /* If the id-expression was a template-id that refers to
8221                  a template-class, we already have the declaration here,
8222                  so no further lookup is needed.  */
8223                  ;
8224             else
8225               /* Look up the name.  */
8226               default_argument
8227                 = cp_parser_lookup_name (parser, default_argument,
8228                                          none_type,
8229                                          /*is_template=*/is_template,
8230                                          /*is_namespace=*/false,
8231                                          /*check_dependency=*/true,
8232                                          /*ambiguous_p=*/NULL);
8233             /* See if the default argument is valid.  */
8234             default_argument
8235               = check_template_template_default_arg (default_argument);
8236           }
8237         else
8238           default_argument = NULL_TREE;
8239
8240         /* Create the combined representation of the parameter and the
8241            default argument.  */
8242         parameter = build_tree_list (default_argument, parameter);
8243       }
8244       break;
8245
8246     default:
8247       gcc_unreachable ();
8248       break;
8249     }
8250
8251   return parameter;
8252 }
8253
8254 /* Parse a template-id.
8255
8256    template-id:
8257      template-name < template-argument-list [opt] >
8258
8259    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8260    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8261    returned.  Otherwise, if the template-name names a function, or set
8262    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8263    names a class, returns a TYPE_DECL for the specialization.
8264
8265    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8266    uninstantiated templates.  */
8267
8268 static tree
8269 cp_parser_template_id (cp_parser *parser,
8270                        bool template_keyword_p,
8271                        bool check_dependency_p,
8272                        bool is_declaration)
8273 {
8274   tree template;
8275   tree arguments;
8276   tree template_id;
8277   cp_token_position start_of_id = 0;
8278   tree access_check = NULL_TREE;
8279   cp_token *next_token, *next_token_2;
8280   bool is_identifier;
8281
8282   /* If the next token corresponds to a template-id, there is no need
8283      to reparse it.  */
8284   next_token = cp_lexer_peek_token (parser->lexer);
8285   if (next_token->type == CPP_TEMPLATE_ID)
8286     {
8287       tree value;
8288       tree check;
8289
8290       /* Get the stored value.  */
8291       value = cp_lexer_consume_token (parser->lexer)->value;
8292       /* Perform any access checks that were deferred.  */
8293       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8294         perform_or_defer_access_check (TREE_PURPOSE (check),
8295                                        TREE_VALUE (check));
8296       /* Return the stored value.  */
8297       return TREE_VALUE (value);
8298     }
8299
8300   /* Avoid performing name lookup if there is no possibility of
8301      finding a template-id.  */
8302   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8303       || (next_token->type == CPP_NAME
8304           && !cp_parser_nth_token_starts_template_argument_list_p
8305                (parser, 2)))
8306     {
8307       cp_parser_error (parser, "expected template-id");
8308       return error_mark_node;
8309     }
8310
8311   /* Remember where the template-id starts.  */
8312   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8313     start_of_id = cp_lexer_token_position (parser->lexer, false);
8314
8315   push_deferring_access_checks (dk_deferred);
8316
8317   /* Parse the template-name.  */
8318   is_identifier = false;
8319   template = cp_parser_template_name (parser, template_keyword_p,
8320                                       check_dependency_p,
8321                                       is_declaration,
8322                                       &is_identifier);
8323   if (template == error_mark_node || is_identifier)
8324     {
8325       pop_deferring_access_checks ();
8326       return template;
8327     }
8328
8329   /* If we find the sequence `[:' after a template-name, it's probably
8330      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8331      parse correctly the argument list.  */
8332   next_token = cp_lexer_peek_token (parser->lexer);
8333   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8334   if (next_token->type == CPP_OPEN_SQUARE
8335       && next_token->flags & DIGRAPH
8336       && next_token_2->type == CPP_COLON
8337       && !(next_token_2->flags & PREV_WHITE))
8338     {
8339       cp_parser_parse_tentatively (parser);
8340       /* Change `:' into `::'.  */
8341       next_token_2->type = CPP_SCOPE;
8342       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8343          CPP_LESS.  */
8344       cp_lexer_consume_token (parser->lexer);
8345       /* Parse the arguments.  */
8346       arguments = cp_parser_enclosed_template_argument_list (parser);
8347       if (!cp_parser_parse_definitely (parser))
8348         {
8349           /* If we couldn't parse an argument list, then we revert our changes
8350              and return simply an error. Maybe this is not a template-id
8351              after all.  */
8352           next_token_2->type = CPP_COLON;
8353           cp_parser_error (parser, "expected %<<%>");
8354           pop_deferring_access_checks ();
8355           return error_mark_node;
8356         }
8357       /* Otherwise, emit an error about the invalid digraph, but continue
8358          parsing because we got our argument list.  */
8359       pedwarn ("%<<::%> cannot begin a template-argument list");
8360       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8361               "between %<<%> and %<::%>");
8362       if (!flag_permissive)
8363         {
8364           static bool hint;
8365           if (!hint)
8366             {
8367               inform ("(if you use -fpermissive G++ will accept your code)");
8368               hint = true;
8369             }
8370         }
8371     }
8372   else
8373     {
8374       /* Look for the `<' that starts the template-argument-list.  */
8375       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8376         {
8377           pop_deferring_access_checks ();
8378           return error_mark_node;
8379         }
8380       /* Parse the arguments.  */
8381       arguments = cp_parser_enclosed_template_argument_list (parser);
8382     }
8383
8384   /* Build a representation of the specialization.  */
8385   if (TREE_CODE (template) == IDENTIFIER_NODE)
8386     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8387   else if (DECL_CLASS_TEMPLATE_P (template)
8388            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8389     template_id
8390       = finish_template_type (template, arguments,
8391                               cp_lexer_next_token_is (parser->lexer,
8392                                                       CPP_SCOPE));
8393   else
8394     {
8395       /* If it's not a class-template or a template-template, it should be
8396          a function-template.  */
8397       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8398                    || TREE_CODE (template) == OVERLOAD
8399                    || BASELINK_P (template)));
8400
8401       template_id = lookup_template_function (template, arguments);
8402     }
8403
8404   /* Retrieve any deferred checks.  Do not pop this access checks yet
8405      so the memory will not be reclaimed during token replacing below.  */
8406   access_check = get_deferred_access_checks ();
8407
8408   /* If parsing tentatively, replace the sequence of tokens that makes
8409      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8410      should we re-parse the token stream, we will not have to repeat
8411      the effort required to do the parse, nor will we issue duplicate
8412      error messages about problems during instantiation of the
8413      template.  Do so only if parsing succeeded, otherwise we may
8414      silently accept template arguments with syntax errors.  */
8415   if (start_of_id && !cp_parser_error_occurred (parser))
8416     {
8417       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8418       
8419       /* Reset the contents of the START_OF_ID token.  */
8420       token->type = CPP_TEMPLATE_ID;
8421       token->value = build_tree_list (access_check, template_id);
8422       token->keyword = RID_MAX;
8423       
8424       /* Purge all subsequent tokens.  */
8425       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8426     }
8427
8428   pop_deferring_access_checks ();
8429   return template_id;
8430 }
8431
8432 /* Parse a template-name.
8433
8434    template-name:
8435      identifier
8436
8437    The standard should actually say:
8438
8439    template-name:
8440      identifier
8441      operator-function-id
8442
8443    A defect report has been filed about this issue.
8444
8445    A conversion-function-id cannot be a template name because they cannot
8446    be part of a template-id. In fact, looking at this code:
8447
8448    a.operator K<int>()
8449
8450    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8451    It is impossible to call a templated conversion-function-id with an
8452    explicit argument list, since the only allowed template parameter is
8453    the type to which it is converting.
8454
8455    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8456    `template' keyword, in a construction like:
8457
8458      T::template f<3>()
8459
8460    In that case `f' is taken to be a template-name, even though there
8461    is no way of knowing for sure.
8462
8463    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8464    name refers to a set of overloaded functions, at least one of which
8465    is a template, or an IDENTIFIER_NODE with the name of the template,
8466    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8467    names are looked up inside uninstantiated templates.  */
8468
8469 static tree
8470 cp_parser_template_name (cp_parser* parser,
8471                          bool template_keyword_p,
8472                          bool check_dependency_p,
8473                          bool is_declaration,
8474                          bool *is_identifier)
8475 {
8476   tree identifier;
8477   tree decl;
8478   tree fns;
8479
8480   /* If the next token is `operator', then we have either an
8481      operator-function-id or a conversion-function-id.  */
8482   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8483     {
8484       /* We don't know whether we're looking at an
8485          operator-function-id or a conversion-function-id.  */
8486       cp_parser_parse_tentatively (parser);
8487       /* Try an operator-function-id.  */
8488       identifier = cp_parser_operator_function_id (parser);
8489       /* If that didn't work, try a conversion-function-id.  */
8490       if (!cp_parser_parse_definitely (parser))
8491         {
8492           cp_parser_error (parser, "expected template-name");
8493           return error_mark_node;
8494         }
8495     }
8496   /* Look for the identifier.  */
8497   else
8498     identifier = cp_parser_identifier (parser);
8499
8500   /* If we didn't find an identifier, we don't have a template-id.  */
8501   if (identifier == error_mark_node)
8502     return error_mark_node;
8503
8504   /* If the name immediately followed the `template' keyword, then it
8505      is a template-name.  However, if the next token is not `<', then
8506      we do not treat it as a template-name, since it is not being used
8507      as part of a template-id.  This enables us to handle constructs
8508      like:
8509
8510        template <typename T> struct S { S(); };
8511        template <typename T> S<T>::S();
8512
8513      correctly.  We would treat `S' as a template -- if it were `S<T>'
8514      -- but we do not if there is no `<'.  */
8515
8516   if (processing_template_decl
8517       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8518     {
8519       /* In a declaration, in a dependent context, we pretend that the
8520          "template" keyword was present in order to improve error
8521          recovery.  For example, given:
8522
8523            template <typename T> void f(T::X<int>);
8524
8525          we want to treat "X<int>" as a template-id.  */
8526       if (is_declaration
8527           && !template_keyword_p
8528           && parser->scope && TYPE_P (parser->scope)
8529           && check_dependency_p
8530           && dependent_type_p (parser->scope)
8531           /* Do not do this for dtors (or ctors), since they never
8532              need the template keyword before their name.  */
8533           && !constructor_name_p (identifier, parser->scope))
8534         {
8535           cp_token_position start = 0;
8536           
8537           /* Explain what went wrong.  */
8538           error ("non-template %qD used as template", identifier);
8539           inform ("use %<%T::template %D%> to indicate that it is a template",
8540                   parser->scope, identifier);
8541           /* If parsing tentatively, find the location of the "<" token.  */
8542           if (cp_parser_simulate_error (parser))
8543             start = cp_lexer_token_position (parser->lexer, true);
8544           /* Parse the template arguments so that we can issue error
8545              messages about them.  */
8546           cp_lexer_consume_token (parser->lexer);
8547           cp_parser_enclosed_template_argument_list (parser);
8548           /* Skip tokens until we find a good place from which to
8549              continue parsing.  */
8550           cp_parser_skip_to_closing_parenthesis (parser,
8551                                                  /*recovering=*/true,
8552                                                  /*or_comma=*/true,
8553                                                  /*consume_paren=*/false);
8554           /* If parsing tentatively, permanently remove the
8555              template argument list.  That will prevent duplicate
8556              error messages from being issued about the missing
8557              "template" keyword.  */
8558           if (start)
8559             cp_lexer_purge_tokens_after (parser->lexer, start);
8560           if (is_identifier)
8561             *is_identifier = true;
8562           return identifier;
8563         }
8564
8565       /* If the "template" keyword is present, then there is generally
8566          no point in doing name-lookup, so we just return IDENTIFIER.
8567          But, if the qualifying scope is non-dependent then we can
8568          (and must) do name-lookup normally.  */
8569       if (template_keyword_p
8570           && (!parser->scope
8571               || (TYPE_P (parser->scope)
8572                   && dependent_type_p (parser->scope))))
8573         return identifier;
8574     }
8575
8576   /* Look up the name.  */
8577   decl = cp_parser_lookup_name (parser, identifier,
8578                                 none_type,
8579                                 /*is_template=*/false,
8580                                 /*is_namespace=*/false,
8581                                 check_dependency_p,
8582                                 /*ambiguous_p=*/NULL);
8583   decl = maybe_get_template_decl_from_type_decl (decl);
8584
8585   /* If DECL is a template, then the name was a template-name.  */
8586   if (TREE_CODE (decl) == TEMPLATE_DECL)
8587     ;
8588   else
8589     {
8590       /* The standard does not explicitly indicate whether a name that
8591          names a set of overloaded declarations, some of which are
8592          templates, is a template-name.  However, such a name should
8593          be a template-name; otherwise, there is no way to form a
8594          template-id for the overloaded templates.  */
8595       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8596       if (TREE_CODE (fns) == OVERLOAD)
8597         {
8598           tree fn;
8599
8600           for (fn = fns; fn; fn = OVL_NEXT (fn))
8601             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8602               break;
8603         }
8604       else
8605         {
8606           /* Otherwise, the name does not name a template.  */
8607           cp_parser_error (parser, "expected template-name");
8608           return error_mark_node;
8609         }
8610     }
8611
8612   /* If DECL is dependent, and refers to a function, then just return
8613      its name; we will look it up again during template instantiation.  */
8614   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8615     {
8616       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8617       if (TYPE_P (scope) && dependent_type_p (scope))
8618         return identifier;
8619     }
8620
8621   return decl;
8622 }
8623
8624 /* Parse a template-argument-list.
8625
8626    template-argument-list:
8627      template-argument
8628      template-argument-list , template-argument
8629
8630    Returns a TREE_VEC containing the arguments.  */
8631
8632 static tree
8633 cp_parser_template_argument_list (cp_parser* parser)
8634 {
8635   tree fixed_args[10];
8636   unsigned n_args = 0;
8637   unsigned alloced = 10;
8638   tree *arg_ary = fixed_args;
8639   tree vec;
8640   bool saved_in_template_argument_list_p;
8641
8642   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8643   parser->in_template_argument_list_p = true;
8644   do
8645     {
8646       tree argument;
8647
8648       if (n_args)
8649         /* Consume the comma.  */
8650         cp_lexer_consume_token (parser->lexer);
8651
8652       /* Parse the template-argument.  */
8653       argument = cp_parser_template_argument (parser);
8654       if (n_args == alloced)
8655         {
8656           alloced *= 2;
8657
8658           if (arg_ary == fixed_args)
8659             {
8660               arg_ary = xmalloc (sizeof (tree) * alloced);
8661               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8662             }
8663           else
8664             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8665         }
8666       arg_ary[n_args++] = argument;
8667     }
8668   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8669
8670   vec = make_tree_vec (n_args);
8671
8672   while (n_args--)
8673     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8674
8675   if (arg_ary != fixed_args)
8676     free (arg_ary);
8677   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8678   return vec;
8679 }
8680
8681 /* Parse a template-argument.
8682
8683    template-argument:
8684      assignment-expression
8685      type-id
8686      id-expression
8687
8688    The representation is that of an assignment-expression, type-id, or
8689    id-expression -- except that the qualified id-expression is
8690    evaluated, so that the value returned is either a DECL or an
8691    OVERLOAD.
8692
8693    Although the standard says "assignment-expression", it forbids
8694    throw-expressions or assignments in the template argument.
8695    Therefore, we use "conditional-expression" instead.  */
8696
8697 static tree
8698 cp_parser_template_argument (cp_parser* parser)
8699 {
8700   tree argument;
8701   bool template_p;
8702   bool address_p;
8703   bool maybe_type_id = false;
8704   cp_token *token;
8705   cp_id_kind idk;
8706   tree qualifying_class;
8707
8708   /* There's really no way to know what we're looking at, so we just
8709      try each alternative in order.
8710
8711        [temp.arg]
8712
8713        In a template-argument, an ambiguity between a type-id and an
8714        expression is resolved to a type-id, regardless of the form of
8715        the corresponding template-parameter.
8716
8717      Therefore, we try a type-id first.  */
8718   cp_parser_parse_tentatively (parser);
8719   argument = cp_parser_type_id (parser);
8720   /* If there was no error parsing the type-id but the next token is a '>>',
8721      we probably found a typo for '> >'. But there are type-id which are
8722      also valid expressions. For instance:
8723
8724      struct X { int operator >> (int); };
8725      template <int V> struct Foo {};
8726      Foo<X () >> 5> r;
8727
8728      Here 'X()' is a valid type-id of a function type, but the user just
8729      wanted to write the expression "X() >> 5". Thus, we remember that we
8730      found a valid type-id, but we still try to parse the argument as an
8731      expression to see what happens.  */
8732   if (!cp_parser_error_occurred (parser)
8733       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8734     {
8735       maybe_type_id = true;
8736       cp_parser_abort_tentative_parse (parser);
8737     }
8738   else
8739     {
8740       /* If the next token isn't a `,' or a `>', then this argument wasn't
8741       really finished. This means that the argument is not a valid
8742       type-id.  */
8743       if (!cp_parser_next_token_ends_template_argument_p (parser))
8744         cp_parser_error (parser, "expected template-argument");
8745       /* If that worked, we're done.  */
8746       if (cp_parser_parse_definitely (parser))
8747         return argument;
8748     }
8749   /* We're still not sure what the argument will be.  */
8750   cp_parser_parse_tentatively (parser);
8751   /* Try a template.  */
8752   argument = cp_parser_id_expression (parser,
8753                                       /*template_keyword_p=*/false,
8754                                       /*check_dependency_p=*/true,
8755                                       &template_p,
8756                                       /*declarator_p=*/false);
8757   /* If the next token isn't a `,' or a `>', then this argument wasn't
8758      really finished.  */
8759   if (!cp_parser_next_token_ends_template_argument_p (parser))
8760     cp_parser_error (parser, "expected template-argument");
8761   if (!cp_parser_error_occurred (parser))
8762     {
8763       /* Figure out what is being referred to.  If the id-expression
8764          was for a class template specialization, then we will have a
8765          TYPE_DECL at this point.  There is no need to do name lookup
8766          at this point in that case.  */
8767       if (TREE_CODE (argument) != TYPE_DECL)
8768         argument = cp_parser_lookup_name (parser, argument,
8769                                           none_type,
8770                                           /*is_template=*/template_p,
8771                                           /*is_namespace=*/false,
8772                                           /*check_dependency=*/true,
8773                                           /*ambiguous_p=*/NULL);
8774       if (TREE_CODE (argument) != TEMPLATE_DECL
8775           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8776         cp_parser_error (parser, "expected template-name");
8777     }
8778   if (cp_parser_parse_definitely (parser))
8779     return argument;
8780   /* It must be a non-type argument.  There permitted cases are given
8781      in [temp.arg.nontype]:
8782
8783      -- an integral constant-expression of integral or enumeration
8784         type; or
8785
8786      -- the name of a non-type template-parameter; or
8787
8788      -- the name of an object or function with external linkage...
8789
8790      -- the address of an object or function with external linkage...
8791
8792      -- a pointer to member...  */
8793   /* Look for a non-type template parameter.  */
8794   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8795     {
8796       cp_parser_parse_tentatively (parser);
8797       argument = cp_parser_primary_expression (parser,
8798                                                &idk,
8799                                                &qualifying_class);
8800       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8801           || !cp_parser_next_token_ends_template_argument_p (parser))
8802         cp_parser_simulate_error (parser);
8803       if (cp_parser_parse_definitely (parser))
8804         return argument;
8805     }
8806
8807   /* If the next token is "&", the argument must be the address of an
8808      object or function with external linkage.  */
8809   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8810   if (address_p)
8811     cp_lexer_consume_token (parser->lexer);
8812   /* See if we might have an id-expression.  */
8813   token = cp_lexer_peek_token (parser->lexer);
8814   if (token->type == CPP_NAME
8815       || token->keyword == RID_OPERATOR
8816       || token->type == CPP_SCOPE
8817       || token->type == CPP_TEMPLATE_ID
8818       || token->type == CPP_NESTED_NAME_SPECIFIER)
8819     {
8820       cp_parser_parse_tentatively (parser);
8821       argument = cp_parser_primary_expression (parser,
8822                                                &idk,
8823                                                &qualifying_class);
8824       if (cp_parser_error_occurred (parser)
8825           || !cp_parser_next_token_ends_template_argument_p (parser))
8826         cp_parser_abort_tentative_parse (parser);
8827       else
8828         {
8829           if (TREE_CODE (argument) == INDIRECT_REF)
8830             {
8831               gcc_assert (REFERENCE_REF_P (argument));
8832               argument = TREE_OPERAND (argument, 0);
8833             }
8834           
8835           if (qualifying_class)
8836             argument = finish_qualified_id_expr (qualifying_class,
8837                                                  argument,
8838                                                  /*done=*/true,
8839                                                  address_p);
8840           if (TREE_CODE (argument) == VAR_DECL)
8841             {
8842               /* A variable without external linkage might still be a
8843                  valid constant-expression, so no error is issued here
8844                  if the external-linkage check fails.  */
8845               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8846                 cp_parser_simulate_error (parser);
8847             }
8848           else if (is_overloaded_fn (argument))
8849             /* All overloaded functions are allowed; if the external
8850                linkage test does not pass, an error will be issued
8851                later.  */
8852             ;
8853           else if (address_p
8854                    && (TREE_CODE (argument) == OFFSET_REF
8855                        || TREE_CODE (argument) == SCOPE_REF))
8856             /* A pointer-to-member.  */
8857             ;
8858           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8859             ;
8860           else
8861             cp_parser_simulate_error (parser);
8862
8863           if (cp_parser_parse_definitely (parser))
8864             {
8865               if (address_p)
8866                 argument = build_x_unary_op (ADDR_EXPR, argument);
8867               return argument;
8868             }
8869         }
8870     }
8871   /* If the argument started with "&", there are no other valid
8872      alternatives at this point.  */
8873   if (address_p)
8874     {
8875       cp_parser_error (parser, "invalid non-type template argument");
8876       return error_mark_node;
8877     }
8878
8879   /* If the argument wasn't successfully parsed as a type-id followed
8880      by '>>', the argument can only be a constant expression now.
8881      Otherwise, we try parsing the constant-expression tentatively,
8882      because the argument could really be a type-id.  */
8883   if (maybe_type_id)
8884     cp_parser_parse_tentatively (parser);
8885   argument = cp_parser_constant_expression (parser,
8886                                             /*allow_non_constant_p=*/false,
8887                                             /*non_constant_p=*/NULL);
8888   argument = fold_non_dependent_expr (argument);
8889   if (!maybe_type_id)
8890     return argument;
8891   if (!cp_parser_next_token_ends_template_argument_p (parser))
8892     cp_parser_error (parser, "expected template-argument");
8893   if (cp_parser_parse_definitely (parser))
8894     return argument;
8895   /* We did our best to parse the argument as a non type-id, but that
8896      was the only alternative that matched (albeit with a '>' after
8897      it). We can assume it's just a typo from the user, and a
8898      diagnostic will then be issued.  */
8899   return cp_parser_type_id (parser);
8900 }
8901
8902 /* Parse an explicit-instantiation.
8903
8904    explicit-instantiation:
8905      template declaration
8906
8907    Although the standard says `declaration', what it really means is:
8908
8909    explicit-instantiation:
8910      template decl-specifier-seq [opt] declarator [opt] ;
8911
8912    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8913    supposed to be allowed.  A defect report has been filed about this
8914    issue.
8915
8916    GNU Extension:
8917
8918    explicit-instantiation:
8919      storage-class-specifier template
8920        decl-specifier-seq [opt] declarator [opt] ;
8921      function-specifier template
8922        decl-specifier-seq [opt] declarator [opt] ;  */
8923
8924 static void
8925 cp_parser_explicit_instantiation (cp_parser* parser)
8926 {
8927   int declares_class_or_enum;
8928   cp_decl_specifier_seq decl_specifiers;
8929   tree extension_specifier = NULL_TREE;
8930
8931   /* Look for an (optional) storage-class-specifier or
8932      function-specifier.  */
8933   if (cp_parser_allow_gnu_extensions_p (parser))
8934     {
8935       extension_specifier
8936         = cp_parser_storage_class_specifier_opt (parser);
8937       if (!extension_specifier)
8938         extension_specifier
8939           = cp_parser_function_specifier_opt (parser,
8940                                               /*decl_specs=*/NULL);
8941     }
8942
8943   /* Look for the `template' keyword.  */
8944   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8945   /* Let the front end know that we are processing an explicit
8946      instantiation.  */
8947   begin_explicit_instantiation ();
8948   /* [temp.explicit] says that we are supposed to ignore access
8949      control while processing explicit instantiation directives.  */
8950   push_deferring_access_checks (dk_no_check);
8951   /* Parse a decl-specifier-seq.  */
8952   cp_parser_decl_specifier_seq (parser,
8953                                 CP_PARSER_FLAGS_OPTIONAL,
8954                                 &decl_specifiers,
8955                                 &declares_class_or_enum);
8956   /* If there was exactly one decl-specifier, and it declared a class,
8957      and there's no declarator, then we have an explicit type
8958      instantiation.  */
8959   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8960     {
8961       tree type;
8962
8963       type = check_tag_decl (&decl_specifiers);
8964       /* Turn access control back on for names used during
8965          template instantiation.  */
8966       pop_deferring_access_checks ();
8967       if (type)
8968         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8969     }
8970   else
8971     {
8972       cp_declarator *declarator;
8973       tree decl;
8974
8975       /* Parse the declarator.  */
8976       declarator
8977         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8978                                 /*ctor_dtor_or_conv_p=*/NULL,
8979                                 /*parenthesized_p=*/NULL,
8980                                 /*member_p=*/false);
8981       if (declares_class_or_enum & 2)
8982         cp_parser_check_for_definition_in_return_type (declarator,
8983                                                        decl_specifiers.type);
8984       if (declarator != cp_error_declarator)
8985         {
8986           decl = grokdeclarator (declarator, &decl_specifiers,
8987                                  NORMAL, 0, NULL);
8988           /* Turn access control back on for names used during
8989              template instantiation.  */
8990           pop_deferring_access_checks ();
8991           /* Do the explicit instantiation.  */
8992           do_decl_instantiation (decl, extension_specifier);
8993         }
8994       else
8995         {
8996           pop_deferring_access_checks ();
8997           /* Skip the body of the explicit instantiation.  */
8998           cp_parser_skip_to_end_of_statement (parser);
8999         }
9000     }
9001   /* We're done with the instantiation.  */
9002   end_explicit_instantiation ();
9003
9004   cp_parser_consume_semicolon_at_end_of_statement (parser);
9005 }
9006
9007 /* Parse an explicit-specialization.
9008
9009    explicit-specialization:
9010      template < > declaration
9011
9012    Although the standard says `declaration', what it really means is:
9013
9014    explicit-specialization:
9015      template <> decl-specifier [opt] init-declarator [opt] ;
9016      template <> function-definition
9017      template <> explicit-specialization
9018      template <> template-declaration  */
9019
9020 static void
9021 cp_parser_explicit_specialization (cp_parser* parser)
9022 {
9023   /* Look for the `template' keyword.  */
9024   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9025   /* Look for the `<'.  */
9026   cp_parser_require (parser, CPP_LESS, "`<'");
9027   /* Look for the `>'.  */
9028   cp_parser_require (parser, CPP_GREATER, "`>'");
9029   /* We have processed another parameter list.  */
9030   ++parser->num_template_parameter_lists;
9031   /* Let the front end know that we are beginning a specialization.  */
9032   begin_specialization ();
9033
9034   /* If the next keyword is `template', we need to figure out whether
9035      or not we're looking a template-declaration.  */
9036   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9037     {
9038       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9039           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9040         cp_parser_template_declaration_after_export (parser,
9041                                                      /*member_p=*/false);
9042       else
9043         cp_parser_explicit_specialization (parser);
9044     }
9045   else
9046     /* Parse the dependent declaration.  */
9047     cp_parser_single_declaration (parser,
9048                                   /*member_p=*/false,
9049                                   /*friend_p=*/NULL);
9050
9051   /* We're done with the specialization.  */
9052   end_specialization ();
9053   /* We're done with this parameter list.  */
9054   --parser->num_template_parameter_lists;
9055 }
9056
9057 /* Parse a type-specifier.
9058
9059    type-specifier:
9060      simple-type-specifier
9061      class-specifier
9062      enum-specifier
9063      elaborated-type-specifier
9064      cv-qualifier
9065
9066    GNU Extension:
9067
9068    type-specifier:
9069      __complex__
9070
9071    Returns a representation of the type-specifier.  For a
9072    class-specifier, enum-specifier, or elaborated-type-specifier, a
9073    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9074
9075    The parser flags FLAGS is used to control type-specifier parsing.
9076
9077    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9078    in a decl-specifier-seq.
9079
9080    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9081    class-specifier, enum-specifier, or elaborated-type-specifier, then
9082    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9083    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9084    zero.
9085
9086    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9087    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9088    is set to FALSE.  */
9089
9090 static tree
9091 cp_parser_type_specifier (cp_parser* parser,
9092                           cp_parser_flags flags,
9093                           cp_decl_specifier_seq *decl_specs,
9094                           bool is_declaration,
9095                           int* declares_class_or_enum,
9096                           bool* is_cv_qualifier)
9097 {
9098   tree type_spec = NULL_TREE;
9099   cp_token *token;
9100   enum rid keyword;
9101   cp_decl_spec ds = ds_last;
9102
9103   /* Assume this type-specifier does not declare a new type.  */
9104   if (declares_class_or_enum)
9105     *declares_class_or_enum = 0;
9106   /* And that it does not specify a cv-qualifier.  */
9107   if (is_cv_qualifier)
9108     *is_cv_qualifier = false;
9109   /* Peek at the next token.  */
9110   token = cp_lexer_peek_token (parser->lexer);
9111
9112   /* If we're looking at a keyword, we can use that to guide the
9113      production we choose.  */
9114   keyword = token->keyword;
9115   switch (keyword)
9116     {
9117     case RID_ENUM:
9118       /* 'enum' [identifier] '{' introduces an enum-specifier;
9119          'enum' <anything else> introduces an elaborated-type-specifier.  */
9120       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9121           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9122               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9123                  == CPP_OPEN_BRACE))
9124         {
9125           if (parser->num_template_parameter_lists)
9126             {
9127               error ("template declaration of %qs", "enum");
9128               cp_parser_skip_to_end_of_block_or_statement (parser);
9129               type_spec = error_mark_node;
9130             }
9131           else
9132             type_spec = cp_parser_enum_specifier (parser);
9133
9134           if (declares_class_or_enum)
9135             *declares_class_or_enum = 2;
9136           if (decl_specs)
9137             cp_parser_set_decl_spec_type (decl_specs,
9138                                           type_spec,
9139                                           /*user_defined_p=*/true);
9140           return type_spec;
9141         }
9142       else
9143         goto elaborated_type_specifier;
9144
9145       /* Any of these indicate either a class-specifier, or an
9146          elaborated-type-specifier.  */
9147     case RID_CLASS:
9148     case RID_STRUCT:
9149     case RID_UNION:
9150       /* Parse tentatively so that we can back up if we don't find a
9151          class-specifier.  */
9152       cp_parser_parse_tentatively (parser);
9153       /* Look for the class-specifier.  */
9154       type_spec = cp_parser_class_specifier (parser);
9155       /* If that worked, we're done.  */
9156       if (cp_parser_parse_definitely (parser))
9157         {
9158           if (declares_class_or_enum)
9159             *declares_class_or_enum = 2;
9160           if (decl_specs)
9161             cp_parser_set_decl_spec_type (decl_specs,
9162                                           type_spec,
9163                                           /*user_defined_p=*/true);
9164           return type_spec;
9165         }
9166
9167       /* Fall through.  */
9168     elaborated_type_specifier:
9169       /* We're declaring (not defining) a class or enum.  */
9170       if (declares_class_or_enum)
9171         *declares_class_or_enum = 1;
9172
9173       /* Fall through.  */
9174     case RID_TYPENAME:
9175       /* Look for an elaborated-type-specifier.  */
9176       type_spec
9177         = (cp_parser_elaborated_type_specifier
9178            (parser,
9179             decl_specs && decl_specs->specs[(int) ds_friend],
9180             is_declaration));
9181       if (decl_specs)
9182         cp_parser_set_decl_spec_type (decl_specs,
9183                                       type_spec,
9184                                       /*user_defined_p=*/true);
9185       return type_spec;
9186
9187     case RID_CONST:
9188       ds = ds_const;
9189       if (is_cv_qualifier)
9190         *is_cv_qualifier = true;
9191       break;
9192
9193     case RID_VOLATILE:
9194       ds = ds_volatile;
9195       if (is_cv_qualifier)
9196         *is_cv_qualifier = true;
9197       break;
9198
9199     case RID_RESTRICT:
9200       ds = ds_restrict;
9201       if (is_cv_qualifier)
9202         *is_cv_qualifier = true;
9203       break;
9204
9205     case RID_COMPLEX:
9206       /* The `__complex__' keyword is a GNU extension.  */
9207       ds = ds_complex;
9208       break;
9209
9210     default:
9211       break;
9212     }
9213
9214   /* Handle simple keywords.  */
9215   if (ds != ds_last)
9216     {
9217       if (decl_specs)
9218         {
9219           ++decl_specs->specs[(int)ds];
9220           decl_specs->any_specifiers_p = true;
9221         }
9222       return cp_lexer_consume_token (parser->lexer)->value;
9223     }
9224
9225   /* If we do not already have a type-specifier, assume we are looking
9226      at a simple-type-specifier.  */
9227   type_spec = cp_parser_simple_type_specifier (parser,
9228                                                decl_specs,
9229                                                flags);
9230
9231   /* If we didn't find a type-specifier, and a type-specifier was not
9232      optional in this context, issue an error message.  */
9233   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9234     {
9235       cp_parser_error (parser, "expected type specifier");
9236       return error_mark_node;
9237     }
9238
9239   return type_spec;
9240 }
9241
9242 /* Parse a simple-type-specifier.
9243
9244    simple-type-specifier:
9245      :: [opt] nested-name-specifier [opt] type-name
9246      :: [opt] nested-name-specifier template template-id
9247      char
9248      wchar_t
9249      bool
9250      short
9251      int
9252      long
9253      signed
9254      unsigned
9255      float
9256      double
9257      void
9258
9259    GNU Extension:
9260
9261    simple-type-specifier:
9262      __typeof__ unary-expression
9263      __typeof__ ( type-id )
9264
9265    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9266    appropriately updated.  */
9267
9268 static tree
9269 cp_parser_simple_type_specifier (cp_parser* parser,
9270                                  cp_decl_specifier_seq *decl_specs,
9271                                  cp_parser_flags flags)
9272 {
9273   tree type = NULL_TREE;
9274   cp_token *token;
9275
9276   /* Peek at the next token.  */
9277   token = cp_lexer_peek_token (parser->lexer);
9278
9279   /* If we're looking at a keyword, things are easy.  */
9280   switch (token->keyword)
9281     {
9282     case RID_CHAR:
9283       if (decl_specs)
9284         decl_specs->explicit_char_p = true;
9285       type = char_type_node;
9286       break;
9287     case RID_WCHAR:
9288       type = wchar_type_node;
9289       break;
9290     case RID_BOOL:
9291       type = boolean_type_node;
9292       break;
9293     case RID_SHORT:
9294       if (decl_specs)
9295         ++decl_specs->specs[(int) ds_short];
9296       type = short_integer_type_node;
9297       break;
9298     case RID_INT:
9299       if (decl_specs)
9300         decl_specs->explicit_int_p = true;
9301       type = integer_type_node;
9302       break;
9303     case RID_LONG:
9304       if (decl_specs)
9305         ++decl_specs->specs[(int) ds_long];
9306       type = long_integer_type_node;
9307       break;
9308     case RID_SIGNED:
9309       if (decl_specs)
9310         ++decl_specs->specs[(int) ds_signed];
9311       type = integer_type_node;
9312       break;
9313     case RID_UNSIGNED:
9314       if (decl_specs)
9315         ++decl_specs->specs[(int) ds_unsigned];
9316       type = unsigned_type_node;
9317       break;
9318     case RID_FLOAT:
9319       type = float_type_node;
9320       break;
9321     case RID_DOUBLE:
9322       type = double_type_node;
9323       break;
9324     case RID_VOID:
9325       type = void_type_node;
9326       break;
9327
9328     case RID_TYPEOF:
9329       /* Consume the `typeof' token.  */
9330       cp_lexer_consume_token (parser->lexer);
9331       /* Parse the operand to `typeof'.  */
9332       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9333       /* If it is not already a TYPE, take its type.  */
9334       if (!TYPE_P (type))
9335         type = finish_typeof (type);
9336
9337       if (decl_specs)
9338         cp_parser_set_decl_spec_type (decl_specs, type,
9339                                       /*user_defined_p=*/true);
9340
9341       return type;
9342
9343     default:
9344       break;
9345     }
9346
9347   /* If the type-specifier was for a built-in type, we're done.  */
9348   if (type)
9349     {
9350       tree id;
9351
9352       /* Record the type.  */
9353       if (decl_specs
9354           && (token->keyword != RID_SIGNED
9355               && token->keyword != RID_UNSIGNED
9356               && token->keyword != RID_SHORT
9357               && token->keyword != RID_LONG))
9358         cp_parser_set_decl_spec_type (decl_specs,
9359                                       type,
9360                                       /*user_defined=*/false);
9361       if (decl_specs)
9362         decl_specs->any_specifiers_p = true;
9363
9364       /* Consume the token.  */
9365       id = cp_lexer_consume_token (parser->lexer)->value;
9366
9367       /* There is no valid C++ program where a non-template type is
9368          followed by a "<".  That usually indicates that the user thought
9369          that the type was a template.  */
9370       cp_parser_check_for_invalid_template_id (parser, type);
9371
9372       return TYPE_NAME (type);
9373     }
9374
9375   /* The type-specifier must be a user-defined type.  */
9376   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9377     {
9378       bool qualified_p;
9379       bool global_p;
9380
9381       /* Don't gobble tokens or issue error messages if this is an
9382          optional type-specifier.  */
9383       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9384         cp_parser_parse_tentatively (parser);
9385
9386       /* Look for the optional `::' operator.  */
9387       global_p
9388         = (cp_parser_global_scope_opt (parser,
9389                                        /*current_scope_valid_p=*/false)
9390            != NULL_TREE);
9391       /* Look for the nested-name specifier.  */
9392       qualified_p
9393         = (cp_parser_nested_name_specifier_opt (parser,
9394                                                 /*typename_keyword_p=*/false,
9395                                                 /*check_dependency_p=*/true,
9396                                                 /*type_p=*/false,
9397                                                 /*is_declaration=*/false)
9398            != NULL_TREE);
9399       /* If we have seen a nested-name-specifier, and the next token
9400          is `template', then we are using the template-id production.  */
9401       if (parser->scope
9402           && cp_parser_optional_template_keyword (parser))
9403         {
9404           /* Look for the template-id.  */
9405           type = cp_parser_template_id (parser,
9406                                         /*template_keyword_p=*/true,
9407                                         /*check_dependency_p=*/true,
9408                                         /*is_declaration=*/false);
9409           /* If the template-id did not name a type, we are out of
9410              luck.  */
9411           if (TREE_CODE (type) != TYPE_DECL)
9412             {
9413               cp_parser_error (parser, "expected template-id for type");
9414               type = NULL_TREE;
9415             }
9416         }
9417       /* Otherwise, look for a type-name.  */
9418       else
9419         type = cp_parser_type_name (parser);
9420       /* Keep track of all name-lookups performed in class scopes.  */
9421       if (type
9422           && !global_p
9423           && !qualified_p
9424           && TREE_CODE (type) == TYPE_DECL
9425           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9426         maybe_note_name_used_in_class (DECL_NAME (type), type);
9427       /* If it didn't work out, we don't have a TYPE.  */
9428       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9429           && !cp_parser_parse_definitely (parser))
9430         type = NULL_TREE;
9431       if (type && decl_specs)
9432         cp_parser_set_decl_spec_type (decl_specs, type,
9433                                       /*user_defined=*/true);
9434     }
9435
9436   /* If we didn't get a type-name, issue an error message.  */
9437   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9438     {
9439       cp_parser_error (parser, "expected type-name");
9440       return error_mark_node;
9441     }
9442
9443   /* There is no valid C++ program where a non-template type is
9444      followed by a "<".  That usually indicates that the user thought
9445      that the type was a template.  */
9446   if (type && type != error_mark_node)
9447     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9448
9449   return type;
9450 }
9451
9452 /* Parse a type-name.
9453
9454    type-name:
9455      class-name
9456      enum-name
9457      typedef-name
9458
9459    enum-name:
9460      identifier
9461
9462    typedef-name:
9463      identifier
9464
9465    Returns a TYPE_DECL for the the type.  */
9466
9467 static tree
9468 cp_parser_type_name (cp_parser* parser)
9469 {
9470   tree type_decl;
9471   tree identifier;
9472
9473   /* We can't know yet whether it is a class-name or not.  */
9474   cp_parser_parse_tentatively (parser);
9475   /* Try a class-name.  */
9476   type_decl = cp_parser_class_name (parser,
9477                                     /*typename_keyword_p=*/false,
9478                                     /*template_keyword_p=*/false,
9479                                     none_type,
9480                                     /*check_dependency_p=*/true,
9481                                     /*class_head_p=*/false,
9482                                     /*is_declaration=*/false);
9483   /* If it's not a class-name, keep looking.  */
9484   if (!cp_parser_parse_definitely (parser))
9485     {
9486       /* It must be a typedef-name or an enum-name.  */
9487       identifier = cp_parser_identifier (parser);
9488       if (identifier == error_mark_node)
9489         return error_mark_node;
9490
9491       /* Look up the type-name.  */
9492       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9493       /* Issue an error if we did not find a type-name.  */
9494       if (TREE_CODE (type_decl) != TYPE_DECL)
9495         {
9496           if (!cp_parser_simulate_error (parser))
9497             cp_parser_name_lookup_error (parser, identifier, type_decl,
9498                                          "is not a type");
9499           type_decl = error_mark_node;
9500         }
9501       /* Remember that the name was used in the definition of the
9502          current class so that we can check later to see if the
9503          meaning would have been different after the class was
9504          entirely defined.  */
9505       else if (type_decl != error_mark_node
9506                && !parser->scope)
9507         maybe_note_name_used_in_class (identifier, type_decl);
9508     }
9509
9510   return type_decl;
9511 }
9512
9513
9514 /* Parse an elaborated-type-specifier.  Note that the grammar given
9515    here incorporates the resolution to DR68.
9516
9517    elaborated-type-specifier:
9518      class-key :: [opt] nested-name-specifier [opt] identifier
9519      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9520      enum :: [opt] nested-name-specifier [opt] identifier
9521      typename :: [opt] nested-name-specifier identifier
9522      typename :: [opt] nested-name-specifier template [opt]
9523        template-id
9524
9525    GNU extension:
9526
9527    elaborated-type-specifier:
9528      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9529      class-key attributes :: [opt] nested-name-specifier [opt]
9530                template [opt] template-id
9531      enum attributes :: [opt] nested-name-specifier [opt] identifier
9532
9533    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9534    declared `friend'.  If IS_DECLARATION is TRUE, then this
9535    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9536    something is being declared.
9537
9538    Returns the TYPE specified.  */
9539
9540 static tree
9541 cp_parser_elaborated_type_specifier (cp_parser* parser,
9542                                      bool is_friend,
9543                                      bool is_declaration)
9544 {
9545   enum tag_types tag_type;
9546   tree identifier;
9547   tree type = NULL_TREE;
9548   tree attributes = NULL_TREE;
9549
9550   /* See if we're looking at the `enum' keyword.  */
9551   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9552     {
9553       /* Consume the `enum' token.  */
9554       cp_lexer_consume_token (parser->lexer);
9555       /* Remember that it's an enumeration type.  */
9556       tag_type = enum_type;
9557       /* Parse the attributes.  */
9558       attributes = cp_parser_attributes_opt (parser);
9559     }
9560   /* Or, it might be `typename'.  */
9561   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9562                                            RID_TYPENAME))
9563     {
9564       /* Consume the `typename' token.  */
9565       cp_lexer_consume_token (parser->lexer);
9566       /* Remember that it's a `typename' type.  */
9567       tag_type = typename_type;
9568       /* The `typename' keyword is only allowed in templates.  */
9569       if (!processing_template_decl)
9570         pedwarn ("using %<typename%> outside of template");
9571     }
9572   /* Otherwise it must be a class-key.  */
9573   else
9574     {
9575       tag_type = cp_parser_class_key (parser);
9576       if (tag_type == none_type)
9577         return error_mark_node;
9578       /* Parse the attributes.  */
9579       attributes = cp_parser_attributes_opt (parser);
9580     }
9581
9582   /* Look for the `::' operator.  */
9583   cp_parser_global_scope_opt (parser,
9584                               /*current_scope_valid_p=*/false);
9585   /* Look for the nested-name-specifier.  */
9586   if (tag_type == typename_type)
9587     {
9588       if (cp_parser_nested_name_specifier (parser,
9589                                            /*typename_keyword_p=*/true,
9590                                            /*check_dependency_p=*/true,
9591                                            /*type_p=*/true,
9592                                            is_declaration)
9593           == error_mark_node)
9594         return error_mark_node;
9595     }
9596   else
9597     /* Even though `typename' is not present, the proposed resolution
9598        to Core Issue 180 says that in `class A<T>::B', `B' should be
9599        considered a type-name, even if `A<T>' is dependent.  */
9600     cp_parser_nested_name_specifier_opt (parser,
9601                                          /*typename_keyword_p=*/true,
9602                                          /*check_dependency_p=*/true,
9603                                          /*type_p=*/true,
9604                                          is_declaration);
9605   /* For everything but enumeration types, consider a template-id.  */
9606   if (tag_type != enum_type)
9607     {
9608       bool template_p = false;
9609       tree decl;
9610
9611       /* Allow the `template' keyword.  */
9612       template_p = cp_parser_optional_template_keyword (parser);
9613       /* If we didn't see `template', we don't know if there's a
9614          template-id or not.  */
9615       if (!template_p)
9616         cp_parser_parse_tentatively (parser);
9617       /* Parse the template-id.  */
9618       decl = cp_parser_template_id (parser, template_p,
9619                                     /*check_dependency_p=*/true,
9620                                     is_declaration);
9621       /* If we didn't find a template-id, look for an ordinary
9622          identifier.  */
9623       if (!template_p && !cp_parser_parse_definitely (parser))
9624         ;
9625       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9626          in effect, then we must assume that, upon instantiation, the
9627          template will correspond to a class.  */
9628       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9629                && tag_type == typename_type)
9630         type = make_typename_type (parser->scope, decl,
9631                                    typename_type,
9632                                    /*complain=*/1);
9633       else
9634         type = TREE_TYPE (decl);
9635     }
9636
9637   /* For an enumeration type, consider only a plain identifier.  */
9638   if (!type)
9639     {
9640       identifier = cp_parser_identifier (parser);
9641
9642       if (identifier == error_mark_node)
9643         {
9644           parser->scope = NULL_TREE;
9645           return error_mark_node;
9646         }
9647
9648       /* For a `typename', we needn't call xref_tag.  */
9649       if (tag_type == typename_type 
9650           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9651         return cp_parser_make_typename_type (parser, parser->scope,
9652                                              identifier);
9653       /* Look up a qualified name in the usual way.  */
9654       if (parser->scope)
9655         {
9656           tree decl;
9657
9658           decl = cp_parser_lookup_name (parser, identifier,
9659                                         tag_type,
9660                                         /*is_template=*/false,
9661                                         /*is_namespace=*/false,
9662                                         /*check_dependency=*/true,
9663                                         /*ambiguous_p=*/NULL);
9664
9665           /* If we are parsing friend declaration, DECL may be a
9666              TEMPLATE_DECL tree node here.  However, we need to check
9667              whether this TEMPLATE_DECL results in valid code.  Consider
9668              the following example:
9669
9670                namespace N {
9671                  template <class T> class C {};
9672                }
9673                class X {
9674                  template <class T> friend class N::C; // #1, valid code
9675                };
9676                template <class T> class Y {
9677                  friend class N::C;                    // #2, invalid code
9678                };
9679
9680              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9681              name lookup of `N::C'.  We see that friend declaration must
9682              be template for the code to be valid.  Note that
9683              processing_template_decl does not work here since it is
9684              always 1 for the above two cases.  */
9685
9686           decl = (cp_parser_maybe_treat_template_as_class
9687                   (decl, /*tag_name_p=*/is_friend
9688                          && parser->num_template_parameter_lists));
9689
9690           if (TREE_CODE (decl) != TYPE_DECL)
9691             {
9692               cp_parser_diagnose_invalid_type_name (parser, 
9693                                                     parser->scope,
9694                                                     identifier);
9695               return error_mark_node;
9696             }
9697
9698           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9699             check_elaborated_type_specifier
9700               (tag_type, decl,
9701                (parser->num_template_parameter_lists
9702                 || DECL_SELF_REFERENCE_P (decl)));
9703
9704           type = TREE_TYPE (decl);
9705         }
9706       else
9707         {
9708           /* An elaborated-type-specifier sometimes introduces a new type and
9709              sometimes names an existing type.  Normally, the rule is that it
9710              introduces a new type only if there is not an existing type of
9711              the same name already in scope.  For example, given:
9712
9713                struct S {};
9714                void f() { struct S s; }
9715
9716              the `struct S' in the body of `f' is the same `struct S' as in
9717              the global scope; the existing definition is used.  However, if
9718              there were no global declaration, this would introduce a new
9719              local class named `S'.
9720
9721              An exception to this rule applies to the following code:
9722
9723                namespace N { struct S; }
9724
9725              Here, the elaborated-type-specifier names a new type
9726              unconditionally; even if there is already an `S' in the
9727              containing scope this declaration names a new type.
9728              This exception only applies if the elaborated-type-specifier
9729              forms the complete declaration:
9730
9731                [class.name]
9732
9733                A declaration consisting solely of `class-key identifier ;' is
9734                either a redeclaration of the name in the current scope or a
9735                forward declaration of the identifier as a class name.  It
9736                introduces the name into the current scope.
9737
9738              We are in this situation precisely when the next token is a `;'.
9739
9740              An exception to the exception is that a `friend' declaration does
9741              *not* name a new type; i.e., given:
9742
9743                struct S { friend struct T; };
9744
9745              `T' is not a new type in the scope of `S'.
9746
9747              Also, `new struct S' or `sizeof (struct S)' never results in the
9748              definition of a new type; a new type can only be declared in a
9749              declaration context.  */
9750
9751           tag_scope ts;
9752           if (is_friend)
9753             /* Friends have special name lookup rules.  */
9754             ts = ts_within_enclosing_non_class;
9755           else if (is_declaration
9756                    && cp_lexer_next_token_is (parser->lexer,
9757                                               CPP_SEMICOLON))
9758             /* This is a `class-key identifier ;' */
9759             ts = ts_current;
9760           else
9761             ts = ts_global;
9762
9763           /* Warn about attributes. They are ignored.  */
9764           if (attributes)
9765             warning ("type attributes are honored only at type definition");
9766
9767           type = xref_tag (tag_type, identifier, ts,
9768                            parser->num_template_parameter_lists);
9769         }
9770     }
9771   if (tag_type != enum_type)
9772     cp_parser_check_class_key (tag_type, type);
9773
9774   /* A "<" cannot follow an elaborated type specifier.  If that
9775      happens, the user was probably trying to form a template-id.  */
9776   cp_parser_check_for_invalid_template_id (parser, type);
9777
9778   return type;
9779 }
9780
9781 /* Parse an enum-specifier.
9782
9783    enum-specifier:
9784      enum identifier [opt] { enumerator-list [opt] }
9785
9786    GNU Extensions:
9787      enum identifier [opt] { enumerator-list [opt] } attributes
9788
9789    Returns an ENUM_TYPE representing the enumeration.  */
9790
9791 static tree
9792 cp_parser_enum_specifier (cp_parser* parser)
9793 {
9794   tree identifier;
9795   tree type;
9796
9797   /* Caller guarantees that the current token is 'enum', an identifier
9798      possibly follows, and the token after that is an opening brace.
9799      If we don't have an identifier, fabricate an anonymous name for
9800      the enumeration being defined.  */
9801   cp_lexer_consume_token (parser->lexer);
9802
9803   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9804     identifier = cp_parser_identifier (parser);
9805   else
9806     identifier = make_anon_name ();
9807
9808   /* Issue an error message if type-definitions are forbidden here.  */
9809   cp_parser_check_type_definition (parser);
9810
9811   /* Create the new type.  We do this before consuming the opening brace
9812      so the enum will be recorded as being on the line of its tag (or the
9813      'enum' keyword, if there is no tag).  */
9814   type = start_enum (identifier);
9815
9816   /* Consume the opening brace.  */
9817   cp_lexer_consume_token (parser->lexer);
9818
9819   /* If the next token is not '}', then there are some enumerators.  */
9820   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9821     cp_parser_enumerator_list (parser, type);
9822
9823   /* Consume the final '}'.  */
9824   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9825
9826   /* Look for trailing attributes to apply to this enumeration, and
9827      apply them if appropriate.  */
9828   if (cp_parser_allow_gnu_extensions_p (parser))
9829     {
9830       tree trailing_attr = cp_parser_attributes_opt (parser);
9831       cplus_decl_attributes (&type,
9832                              trailing_attr,
9833                              (int) ATTR_FLAG_TYPE_IN_PLACE);
9834     }
9835
9836   /* Finish up the enumeration.  */
9837   finish_enum (type);
9838
9839   return type;
9840 }
9841
9842 /* Parse an enumerator-list.  The enumerators all have the indicated
9843    TYPE.
9844
9845    enumerator-list:
9846      enumerator-definition
9847      enumerator-list , enumerator-definition  */
9848
9849 static void
9850 cp_parser_enumerator_list (cp_parser* parser, tree type)
9851 {
9852   while (true)
9853     {
9854       /* Parse an enumerator-definition.  */
9855       cp_parser_enumerator_definition (parser, type);
9856
9857       /* If the next token is not a ',', we've reached the end of
9858          the list.  */
9859       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9860         break;
9861       /* Otherwise, consume the `,' and keep going.  */
9862       cp_lexer_consume_token (parser->lexer);
9863       /* If the next token is a `}', there is a trailing comma.  */
9864       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9865         {
9866           if (pedantic && !in_system_header)
9867             pedwarn ("comma at end of enumerator list");
9868           break;
9869         }
9870     }
9871 }
9872
9873 /* Parse an enumerator-definition.  The enumerator has the indicated
9874    TYPE.
9875
9876    enumerator-definition:
9877      enumerator
9878      enumerator = constant-expression
9879
9880    enumerator:
9881      identifier  */
9882
9883 static void
9884 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9885 {
9886   tree identifier;
9887   tree value;
9888
9889   /* Look for the identifier.  */
9890   identifier = cp_parser_identifier (parser);
9891   if (identifier == error_mark_node)
9892     return;
9893
9894   /* If the next token is an '=', then there is an explicit value.  */
9895   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9896     {
9897       /* Consume the `=' token.  */
9898       cp_lexer_consume_token (parser->lexer);
9899       /* Parse the value.  */
9900       value = cp_parser_constant_expression (parser,
9901                                              /*allow_non_constant_p=*/false,
9902                                              NULL);
9903     }
9904   else
9905     value = NULL_TREE;
9906
9907   /* Create the enumerator.  */
9908   build_enumerator (identifier, value, type);
9909 }
9910
9911 /* Parse a namespace-name.
9912
9913    namespace-name:
9914      original-namespace-name
9915      namespace-alias
9916
9917    Returns the NAMESPACE_DECL for the namespace.  */
9918
9919 static tree
9920 cp_parser_namespace_name (cp_parser* parser)
9921 {
9922   tree identifier;
9923   tree namespace_decl;
9924
9925   /* Get the name of the namespace.  */
9926   identifier = cp_parser_identifier (parser);
9927   if (identifier == error_mark_node)
9928     return error_mark_node;
9929
9930   /* Look up the identifier in the currently active scope.  Look only
9931      for namespaces, due to:
9932
9933        [basic.lookup.udir]
9934
9935        When looking up a namespace-name in a using-directive or alias
9936        definition, only namespace names are considered.
9937
9938      And:
9939
9940        [basic.lookup.qual]
9941
9942        During the lookup of a name preceding the :: scope resolution
9943        operator, object, function, and enumerator names are ignored.
9944
9945      (Note that cp_parser_class_or_namespace_name only calls this
9946      function if the token after the name is the scope resolution
9947      operator.)  */
9948   namespace_decl = cp_parser_lookup_name (parser, identifier,
9949                                           none_type,
9950                                           /*is_template=*/false,
9951                                           /*is_namespace=*/true,
9952                                           /*check_dependency=*/true,
9953                                           /*ambiguous_p=*/NULL);
9954   /* If it's not a namespace, issue an error.  */
9955   if (namespace_decl == error_mark_node
9956       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9957     {
9958       cp_parser_error (parser, "expected namespace-name");
9959       namespace_decl = error_mark_node;
9960     }
9961
9962   return namespace_decl;
9963 }
9964
9965 /* Parse a namespace-definition.
9966
9967    namespace-definition:
9968      named-namespace-definition
9969      unnamed-namespace-definition
9970
9971    named-namespace-definition:
9972      original-namespace-definition
9973      extension-namespace-definition
9974
9975    original-namespace-definition:
9976      namespace identifier { namespace-body }
9977
9978    extension-namespace-definition:
9979      namespace original-namespace-name { namespace-body }
9980
9981    unnamed-namespace-definition:
9982      namespace { namespace-body } */
9983
9984 static void
9985 cp_parser_namespace_definition (cp_parser* parser)
9986 {
9987   tree identifier;
9988
9989   /* Look for the `namespace' keyword.  */
9990   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9991
9992   /* Get the name of the namespace.  We do not attempt to distinguish
9993      between an original-namespace-definition and an
9994      extension-namespace-definition at this point.  The semantic
9995      analysis routines are responsible for that.  */
9996   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9997     identifier = cp_parser_identifier (parser);
9998   else
9999     identifier = NULL_TREE;
10000
10001   /* Look for the `{' to start the namespace.  */
10002   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10003   /* Start the namespace.  */
10004   push_namespace (identifier);
10005   /* Parse the body of the namespace.  */
10006   cp_parser_namespace_body (parser);
10007   /* Finish the namespace.  */
10008   pop_namespace ();
10009   /* Look for the final `}'.  */
10010   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10011 }
10012
10013 /* Parse a namespace-body.
10014
10015    namespace-body:
10016      declaration-seq [opt]  */
10017
10018 static void
10019 cp_parser_namespace_body (cp_parser* parser)
10020 {
10021   cp_parser_declaration_seq_opt (parser);
10022 }
10023
10024 /* Parse a namespace-alias-definition.
10025
10026    namespace-alias-definition:
10027      namespace identifier = qualified-namespace-specifier ;  */
10028
10029 static void
10030 cp_parser_namespace_alias_definition (cp_parser* parser)
10031 {
10032   tree identifier;
10033   tree namespace_specifier;
10034
10035   /* Look for the `namespace' keyword.  */
10036   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10037   /* Look for the identifier.  */
10038   identifier = cp_parser_identifier (parser);
10039   if (identifier == error_mark_node)
10040     return;
10041   /* Look for the `=' token.  */
10042   cp_parser_require (parser, CPP_EQ, "`='");
10043   /* Look for the qualified-namespace-specifier.  */
10044   namespace_specifier
10045     = cp_parser_qualified_namespace_specifier (parser);
10046   /* Look for the `;' token.  */
10047   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10048
10049   /* Register the alias in the symbol table.  */
10050   do_namespace_alias (identifier, namespace_specifier);
10051 }
10052
10053 /* Parse a qualified-namespace-specifier.
10054
10055    qualified-namespace-specifier:
10056      :: [opt] nested-name-specifier [opt] namespace-name
10057
10058    Returns a NAMESPACE_DECL corresponding to the specified
10059    namespace.  */
10060
10061 static tree
10062 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10063 {
10064   /* Look for the optional `::'.  */
10065   cp_parser_global_scope_opt (parser,
10066                               /*current_scope_valid_p=*/false);
10067
10068   /* Look for the optional nested-name-specifier.  */
10069   cp_parser_nested_name_specifier_opt (parser,
10070                                        /*typename_keyword_p=*/false,
10071                                        /*check_dependency_p=*/true,
10072                                        /*type_p=*/false,
10073                                        /*is_declaration=*/true);
10074
10075   return cp_parser_namespace_name (parser);
10076 }
10077
10078 /* Parse a using-declaration.
10079
10080    using-declaration:
10081      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10082      using :: unqualified-id ;  */
10083
10084 static void
10085 cp_parser_using_declaration (cp_parser* parser)
10086 {
10087   cp_token *token;
10088   bool typename_p = false;
10089   bool global_scope_p;
10090   tree decl;
10091   tree identifier;
10092   tree qscope;
10093
10094   /* Look for the `using' keyword.  */
10095   cp_parser_require_keyword (parser, RID_USING, "`using'");
10096
10097   /* Peek at the next token.  */
10098   token = cp_lexer_peek_token (parser->lexer);
10099   /* See if it's `typename'.  */
10100   if (token->keyword == RID_TYPENAME)
10101     {
10102       /* Remember that we've seen it.  */
10103       typename_p = true;
10104       /* Consume the `typename' token.  */
10105       cp_lexer_consume_token (parser->lexer);
10106     }
10107
10108   /* Look for the optional global scope qualification.  */
10109   global_scope_p
10110     = (cp_parser_global_scope_opt (parser,
10111                                    /*current_scope_valid_p=*/false)
10112        != NULL_TREE);
10113
10114   /* If we saw `typename', or didn't see `::', then there must be a
10115      nested-name-specifier present.  */
10116   if (typename_p || !global_scope_p)
10117     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10118                                               /*check_dependency_p=*/true,
10119                                               /*type_p=*/false,
10120                                               /*is_declaration=*/true);
10121   /* Otherwise, we could be in either of the two productions.  In that
10122      case, treat the nested-name-specifier as optional.  */
10123   else
10124     qscope = cp_parser_nested_name_specifier_opt (parser,
10125                                                   /*typename_keyword_p=*/false,
10126                                                   /*check_dependency_p=*/true,
10127                                                   /*type_p=*/false,
10128                                                   /*is_declaration=*/true);
10129   if (!qscope)
10130     qscope = global_namespace;
10131
10132   /* Parse the unqualified-id.  */
10133   identifier = cp_parser_unqualified_id (parser,
10134                                          /*template_keyword_p=*/false,
10135                                          /*check_dependency_p=*/true,
10136                                          /*declarator_p=*/true);
10137
10138   /* The function we call to handle a using-declaration is different
10139      depending on what scope we are in.  */
10140   if (identifier == error_mark_node)
10141     ;
10142   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10143            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10144     /* [namespace.udecl]
10145
10146        A using declaration shall not name a template-id.  */
10147     error ("a template-id may not appear in a using-declaration");
10148   else
10149     {
10150       if (at_class_scope_p ())
10151         {
10152           /* Create the USING_DECL.  */
10153           decl = do_class_using_decl (build_nt (SCOPE_REF,
10154                                                 parser->scope,
10155                                                 identifier));
10156           /* Add it to the list of members in this class.  */
10157           finish_member_declaration (decl);
10158         }
10159       else
10160         {
10161           decl = cp_parser_lookup_name_simple (parser, identifier);
10162           if (decl == error_mark_node)
10163             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10164           else if (!at_namespace_scope_p ())
10165             do_local_using_decl (decl, qscope, identifier);
10166           else
10167             do_toplevel_using_decl (decl, qscope, identifier);
10168         }
10169     }
10170
10171   /* Look for the final `;'.  */
10172   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10173 }
10174
10175 /* Parse a using-directive.
10176
10177    using-directive:
10178      using namespace :: [opt] nested-name-specifier [opt]
10179        namespace-name ;  */
10180
10181 static void
10182 cp_parser_using_directive (cp_parser* parser)
10183 {
10184   tree namespace_decl;
10185   tree attribs;
10186
10187   /* Look for the `using' keyword.  */
10188   cp_parser_require_keyword (parser, RID_USING, "`using'");
10189   /* And the `namespace' keyword.  */
10190   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10191   /* Look for the optional `::' operator.  */
10192   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10193   /* And the optional nested-name-specifier.  */
10194   cp_parser_nested_name_specifier_opt (parser,
10195                                        /*typename_keyword_p=*/false,
10196                                        /*check_dependency_p=*/true,
10197                                        /*type_p=*/false,
10198                                        /*is_declaration=*/true);
10199   /* Get the namespace being used.  */
10200   namespace_decl = cp_parser_namespace_name (parser);
10201   /* And any specified attributes.  */
10202   attribs = cp_parser_attributes_opt (parser);
10203   /* Update the symbol table.  */
10204   parse_using_directive (namespace_decl, attribs);
10205   /* Look for the final `;'.  */
10206   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10207 }
10208
10209 /* Parse an asm-definition.
10210
10211    asm-definition:
10212      asm ( string-literal ) ;
10213
10214    GNU Extension:
10215
10216    asm-definition:
10217      asm volatile [opt] ( string-literal ) ;
10218      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10219      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10220                           : asm-operand-list [opt] ) ;
10221      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10222                           : asm-operand-list [opt]
10223                           : asm-operand-list [opt] ) ;  */
10224
10225 static void
10226 cp_parser_asm_definition (cp_parser* parser)
10227 {
10228   tree string;
10229   tree outputs = NULL_TREE;
10230   tree inputs = NULL_TREE;
10231   tree clobbers = NULL_TREE;
10232   tree asm_stmt;
10233   bool volatile_p = false;
10234   bool extended_p = false;
10235
10236   /* Look for the `asm' keyword.  */
10237   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10238   /* See if the next token is `volatile'.  */
10239   if (cp_parser_allow_gnu_extensions_p (parser)
10240       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10241     {
10242       /* Remember that we saw the `volatile' keyword.  */
10243       volatile_p = true;
10244       /* Consume the token.  */
10245       cp_lexer_consume_token (parser->lexer);
10246     }
10247   /* Look for the opening `('.  */
10248   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10249     return;
10250   /* Look for the string.  */
10251   string = cp_parser_string_literal (parser, false, false);
10252   if (string == error_mark_node)
10253     {
10254       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10255                                              /*consume_paren=*/true);
10256       return;
10257     }
10258
10259   /* If we're allowing GNU extensions, check for the extended assembly
10260      syntax.  Unfortunately, the `:' tokens need not be separated by
10261      a space in C, and so, for compatibility, we tolerate that here
10262      too.  Doing that means that we have to treat the `::' operator as
10263      two `:' tokens.  */
10264   if (cp_parser_allow_gnu_extensions_p (parser)
10265       && at_function_scope_p ()
10266       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10267           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10268     {
10269       bool inputs_p = false;
10270       bool clobbers_p = false;
10271
10272       /* The extended syntax was used.  */
10273       extended_p = true;
10274
10275       /* Look for outputs.  */
10276       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10277         {
10278           /* Consume the `:'.  */
10279           cp_lexer_consume_token (parser->lexer);
10280           /* Parse the output-operands.  */
10281           if (cp_lexer_next_token_is_not (parser->lexer,
10282                                           CPP_COLON)
10283               && cp_lexer_next_token_is_not (parser->lexer,
10284                                              CPP_SCOPE)
10285               && cp_lexer_next_token_is_not (parser->lexer,
10286                                              CPP_CLOSE_PAREN))
10287             outputs = cp_parser_asm_operand_list (parser);
10288         }
10289       /* If the next token is `::', there are no outputs, and the
10290          next token is the beginning of the inputs.  */
10291       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10292         /* The inputs are coming next.  */
10293         inputs_p = true;
10294
10295       /* Look for inputs.  */
10296       if (inputs_p
10297           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10298         {
10299           /* Consume the `:' or `::'.  */
10300           cp_lexer_consume_token (parser->lexer);
10301           /* Parse the output-operands.  */
10302           if (cp_lexer_next_token_is_not (parser->lexer,
10303                                           CPP_COLON)
10304               && cp_lexer_next_token_is_not (parser->lexer,
10305                                              CPP_CLOSE_PAREN))
10306             inputs = cp_parser_asm_operand_list (parser);
10307         }
10308       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10309         /* The clobbers are coming next.  */
10310         clobbers_p = true;
10311
10312       /* Look for clobbers.  */
10313       if (clobbers_p
10314           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10315         {
10316           /* Consume the `:' or `::'.  */
10317           cp_lexer_consume_token (parser->lexer);
10318           /* Parse the clobbers.  */
10319           if (cp_lexer_next_token_is_not (parser->lexer,
10320                                           CPP_CLOSE_PAREN))
10321             clobbers = cp_parser_asm_clobber_list (parser);
10322         }
10323     }
10324   /* Look for the closing `)'.  */
10325   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10326     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10327                                            /*consume_paren=*/true);
10328   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10329
10330   /* Create the ASM_EXPR.  */
10331   if (at_function_scope_p ())
10332     {
10333       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10334                                   inputs, clobbers);
10335       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10336       if (!extended_p)
10337         {
10338           tree temp = asm_stmt;
10339           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10340             temp = TREE_OPERAND (temp, 0);
10341           
10342           ASM_INPUT_P (temp) = 1;
10343         }
10344     }
10345   else
10346     assemble_asm (string);
10347 }
10348
10349 /* Declarators [gram.dcl.decl] */
10350
10351 /* Parse an init-declarator.
10352
10353    init-declarator:
10354      declarator initializer [opt]
10355
10356    GNU Extension:
10357
10358    init-declarator:
10359      declarator asm-specification [opt] attributes [opt] initializer [opt]
10360
10361    function-definition:
10362      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10363        function-body
10364      decl-specifier-seq [opt] declarator function-try-block
10365
10366    GNU Extension:
10367
10368    function-definition:
10369      __extension__ function-definition
10370
10371    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10372    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10373    then this declarator appears in a class scope.  The new DECL created
10374    by this declarator is returned.
10375
10376    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10377    for a function-definition here as well.  If the declarator is a
10378    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10379    be TRUE upon return.  By that point, the function-definition will
10380    have been completely parsed.
10381
10382    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10383    is FALSE.  */
10384
10385 static tree
10386 cp_parser_init_declarator (cp_parser* parser,
10387                            cp_decl_specifier_seq *decl_specifiers,
10388                            bool function_definition_allowed_p,
10389                            bool member_p,
10390                            int declares_class_or_enum,
10391                            bool* function_definition_p)
10392 {
10393   cp_token *token;
10394   cp_declarator *declarator;
10395   tree prefix_attributes;
10396   tree attributes;
10397   tree asm_specification;
10398   tree initializer;
10399   tree decl = NULL_TREE;
10400   tree scope;
10401   bool is_initialized;
10402   bool is_parenthesized_init;
10403   bool is_non_constant_init;
10404   int ctor_dtor_or_conv_p;
10405   bool friend_p;
10406   bool pop_p = false;
10407
10408   /* Gather the attributes that were provided with the
10409      decl-specifiers.  */
10410   prefix_attributes = decl_specifiers->attributes;
10411
10412   /* Assume that this is not the declarator for a function
10413      definition.  */
10414   if (function_definition_p)
10415     *function_definition_p = false;
10416
10417   /* Defer access checks while parsing the declarator; we cannot know
10418      what names are accessible until we know what is being
10419      declared.  */
10420   resume_deferring_access_checks ();
10421
10422   /* Parse the declarator.  */
10423   declarator
10424     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10425                             &ctor_dtor_or_conv_p,
10426                             /*parenthesized_p=*/NULL,
10427                             /*member_p=*/false);
10428   /* Gather up the deferred checks.  */
10429   stop_deferring_access_checks ();
10430
10431   /* If the DECLARATOR was erroneous, there's no need to go
10432      further.  */
10433   if (declarator == cp_error_declarator)
10434     return error_mark_node;
10435
10436   if (declares_class_or_enum & 2)
10437     cp_parser_check_for_definition_in_return_type (declarator,
10438                                                    decl_specifiers->type);
10439
10440   /* Figure out what scope the entity declared by the DECLARATOR is
10441      located in.  `grokdeclarator' sometimes changes the scope, so
10442      we compute it now.  */
10443   scope = get_scope_of_declarator (declarator);
10444
10445   /* If we're allowing GNU extensions, look for an asm-specification
10446      and attributes.  */
10447   if (cp_parser_allow_gnu_extensions_p (parser))
10448     {
10449       /* Look for an asm-specification.  */
10450       asm_specification = cp_parser_asm_specification_opt (parser);
10451       /* And attributes.  */
10452       attributes = cp_parser_attributes_opt (parser);
10453     }
10454   else
10455     {
10456       asm_specification = NULL_TREE;
10457       attributes = NULL_TREE;
10458     }
10459
10460   /* Peek at the next token.  */
10461   token = cp_lexer_peek_token (parser->lexer);
10462   /* Check to see if the token indicates the start of a
10463      function-definition.  */
10464   if (cp_parser_token_starts_function_definition_p (token))
10465     {
10466       if (!function_definition_allowed_p)
10467         {
10468           /* If a function-definition should not appear here, issue an
10469              error message.  */
10470           cp_parser_error (parser,
10471                            "a function-definition is not allowed here");
10472           return error_mark_node;
10473         }
10474       else
10475         {
10476           /* Neither attributes nor an asm-specification are allowed
10477              on a function-definition.  */
10478           if (asm_specification)
10479             error ("an asm-specification is not allowed on a function-definition");
10480           if (attributes)
10481             error ("attributes are not allowed on a function-definition");
10482           /* This is a function-definition.  */
10483           *function_definition_p = true;
10484
10485           /* Parse the function definition.  */
10486           if (member_p)
10487             decl = cp_parser_save_member_function_body (parser,
10488                                                         decl_specifiers,
10489                                                         declarator,
10490                                                         prefix_attributes);
10491           else
10492             decl
10493               = (cp_parser_function_definition_from_specifiers_and_declarator
10494                  (parser, decl_specifiers, prefix_attributes, declarator));
10495
10496           return decl;
10497         }
10498     }
10499
10500   /* [dcl.dcl]
10501
10502      Only in function declarations for constructors, destructors, and
10503      type conversions can the decl-specifier-seq be omitted.
10504
10505      We explicitly postpone this check past the point where we handle
10506      function-definitions because we tolerate function-definitions
10507      that are missing their return types in some modes.  */
10508   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10509     {
10510       cp_parser_error (parser,
10511                        "expected constructor, destructor, or type conversion");
10512       return error_mark_node;
10513     }
10514
10515   /* An `=' or an `(' indicates an initializer.  */
10516   is_initialized = (token->type == CPP_EQ
10517                      || token->type == CPP_OPEN_PAREN);
10518   /* If the init-declarator isn't initialized and isn't followed by a
10519      `,' or `;', it's not a valid init-declarator.  */
10520   if (!is_initialized
10521       && token->type != CPP_COMMA
10522       && token->type != CPP_SEMICOLON)
10523     {
10524       cp_parser_error (parser, "expected initializer");
10525       return error_mark_node;
10526     }
10527
10528   /* Because start_decl has side-effects, we should only call it if we
10529      know we're going ahead.  By this point, we know that we cannot
10530      possibly be looking at any other construct.  */
10531   cp_parser_commit_to_tentative_parse (parser);
10532
10533   /* If the decl specifiers were bad, issue an error now that we're
10534      sure this was intended to be a declarator.  Then continue
10535      declaring the variable(s), as int, to try to cut down on further
10536      errors.  */
10537   if (decl_specifiers->any_specifiers_p
10538       && decl_specifiers->type == error_mark_node)
10539     {
10540       cp_parser_error (parser, "invalid type in declaration");
10541       decl_specifiers->type = integer_type_node;
10542     }
10543
10544   /* Check to see whether or not this declaration is a friend.  */
10545   friend_p = cp_parser_friend_p (decl_specifiers);
10546
10547   /* Check that the number of template-parameter-lists is OK.  */
10548   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10549     return error_mark_node;
10550
10551   /* Enter the newly declared entry in the symbol table.  If we're
10552      processing a declaration in a class-specifier, we wait until
10553      after processing the initializer.  */
10554   if (!member_p)
10555     {
10556       if (parser->in_unbraced_linkage_specification_p)
10557         {
10558           decl_specifiers->storage_class = sc_extern;
10559           have_extern_spec = false;
10560         }
10561       decl = start_decl (declarator, decl_specifiers,
10562                          is_initialized, attributes, prefix_attributes,
10563                          &pop_p);
10564     }
10565   else if (scope)
10566     /* Enter the SCOPE.  That way unqualified names appearing in the
10567        initializer will be looked up in SCOPE.  */
10568     pop_p = push_scope (scope);
10569
10570   /* Perform deferred access control checks, now that we know in which
10571      SCOPE the declared entity resides.  */
10572   if (!member_p && decl)
10573     {
10574       tree saved_current_function_decl = NULL_TREE;
10575
10576       /* If the entity being declared is a function, pretend that we
10577          are in its scope.  If it is a `friend', it may have access to
10578          things that would not otherwise be accessible.  */
10579       if (TREE_CODE (decl) == FUNCTION_DECL)
10580         {
10581           saved_current_function_decl = current_function_decl;
10582           current_function_decl = decl;
10583         }
10584
10585       /* Perform the access control checks for the declarator and the
10586          the decl-specifiers.  */
10587       perform_deferred_access_checks ();
10588
10589       /* Restore the saved value.  */
10590       if (TREE_CODE (decl) == FUNCTION_DECL)
10591         current_function_decl = saved_current_function_decl;
10592     }
10593
10594   /* Parse the initializer.  */
10595   if (is_initialized)
10596     initializer = cp_parser_initializer (parser,
10597                                          &is_parenthesized_init,
10598                                          &is_non_constant_init);
10599   else
10600     {
10601       initializer = NULL_TREE;
10602       is_parenthesized_init = false;
10603       is_non_constant_init = true;
10604     }
10605
10606   /* The old parser allows attributes to appear after a parenthesized
10607      initializer.  Mark Mitchell proposed removing this functionality
10608      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10609      attributes -- but ignores them.  */
10610   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10611     if (cp_parser_attributes_opt (parser))
10612       warning ("attributes after parenthesized initializer ignored");
10613
10614   /* For an in-class declaration, use `grokfield' to create the
10615      declaration.  */
10616   if (member_p)
10617     {
10618       if (pop_p)
10619         {
10620           pop_scope (scope);
10621           pop_p = false;
10622         }
10623       decl = grokfield (declarator, decl_specifiers,
10624                         initializer, /*asmspec=*/NULL_TREE,
10625                         /*attributes=*/NULL_TREE);
10626       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10627         cp_parser_save_default_args (parser, decl);
10628     }
10629
10630   /* Finish processing the declaration.  But, skip friend
10631      declarations.  */
10632   if (!friend_p && decl && decl != error_mark_node)
10633     {
10634       cp_finish_decl (decl,
10635                       initializer,
10636                       asm_specification,
10637                       /* If the initializer is in parentheses, then this is
10638                          a direct-initialization, which means that an
10639                          `explicit' constructor is OK.  Otherwise, an
10640                          `explicit' constructor cannot be used.  */
10641                       ((is_parenthesized_init || !is_initialized)
10642                      ? 0 : LOOKUP_ONLYCONVERTING));
10643       if (pop_p)
10644         pop_scope (DECL_CONTEXT (decl));
10645     }
10646
10647   /* Remember whether or not variables were initialized by
10648      constant-expressions.  */
10649   if (decl && TREE_CODE (decl) == VAR_DECL
10650       && is_initialized && !is_non_constant_init)
10651     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10652
10653   return decl;
10654 }
10655
10656 /* Parse a declarator.
10657
10658    declarator:
10659      direct-declarator
10660      ptr-operator declarator
10661
10662    abstract-declarator:
10663      ptr-operator abstract-declarator [opt]
10664      direct-abstract-declarator
10665
10666    GNU Extensions:
10667
10668    declarator:
10669      attributes [opt] direct-declarator
10670      attributes [opt] ptr-operator declarator
10671
10672    abstract-declarator:
10673      attributes [opt] ptr-operator abstract-declarator [opt]
10674      attributes [opt] direct-abstract-declarator
10675
10676    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10677    detect constructor, destructor or conversion operators. It is set
10678    to -1 if the declarator is a name, and +1 if it is a
10679    function. Otherwise it is set to zero. Usually you just want to
10680    test for >0, but internally the negative value is used.
10681
10682    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10683    a decl-specifier-seq unless it declares a constructor, destructor,
10684    or conversion.  It might seem that we could check this condition in
10685    semantic analysis, rather than parsing, but that makes it difficult
10686    to handle something like `f()'.  We want to notice that there are
10687    no decl-specifiers, and therefore realize that this is an
10688    expression, not a declaration.)
10689
10690    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10691    the declarator is a direct-declarator of the form "(...)".  
10692
10693    MEMBER_P is true iff this declarator is a member-declarator.  */
10694
10695 static cp_declarator *
10696 cp_parser_declarator (cp_parser* parser,
10697                       cp_parser_declarator_kind dcl_kind,
10698                       int* ctor_dtor_or_conv_p,
10699                       bool* parenthesized_p,
10700                       bool member_p)
10701 {
10702   cp_token *token;
10703   cp_declarator *declarator;
10704   enum tree_code code;
10705   cp_cv_quals cv_quals;
10706   tree class_type;
10707   tree attributes = NULL_TREE;
10708
10709   /* Assume this is not a constructor, destructor, or type-conversion
10710      operator.  */
10711   if (ctor_dtor_or_conv_p)
10712     *ctor_dtor_or_conv_p = 0;
10713
10714   if (cp_parser_allow_gnu_extensions_p (parser))
10715     attributes = cp_parser_attributes_opt (parser);
10716
10717   /* Peek at the next token.  */
10718   token = cp_lexer_peek_token (parser->lexer);
10719
10720   /* Check for the ptr-operator production.  */
10721   cp_parser_parse_tentatively (parser);
10722   /* Parse the ptr-operator.  */
10723   code = cp_parser_ptr_operator (parser,
10724                                  &class_type,
10725                                  &cv_quals);
10726   /* If that worked, then we have a ptr-operator.  */
10727   if (cp_parser_parse_definitely (parser))
10728     {
10729       /* If a ptr-operator was found, then this declarator was not
10730          parenthesized.  */
10731       if (parenthesized_p)
10732         *parenthesized_p = true;
10733       /* The dependent declarator is optional if we are parsing an
10734          abstract-declarator.  */
10735       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10736         cp_parser_parse_tentatively (parser);
10737
10738       /* Parse the dependent declarator.  */
10739       declarator = cp_parser_declarator (parser, dcl_kind,
10740                                          /*ctor_dtor_or_conv_p=*/NULL,
10741                                          /*parenthesized_p=*/NULL,
10742                                          /*member_p=*/false);
10743
10744       /* If we are parsing an abstract-declarator, we must handle the
10745          case where the dependent declarator is absent.  */
10746       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10747           && !cp_parser_parse_definitely (parser))
10748         declarator = NULL;
10749
10750       /* Build the representation of the ptr-operator.  */
10751       if (class_type)
10752         declarator = make_ptrmem_declarator (cv_quals,
10753                                              class_type,
10754                                              declarator);
10755       else if (code == INDIRECT_REF)
10756         declarator = make_pointer_declarator (cv_quals, declarator);
10757       else
10758         declarator = make_reference_declarator (cv_quals, declarator);
10759     }
10760   /* Everything else is a direct-declarator.  */
10761   else
10762     {
10763       if (parenthesized_p)
10764         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10765                                                    CPP_OPEN_PAREN);
10766       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10767                                                 ctor_dtor_or_conv_p,
10768                                                 member_p);
10769     }
10770
10771   if (attributes && declarator != cp_error_declarator)
10772     declarator->attributes = attributes;
10773
10774   return declarator;
10775 }
10776
10777 /* Parse a direct-declarator or direct-abstract-declarator.
10778
10779    direct-declarator:
10780      declarator-id
10781      direct-declarator ( parameter-declaration-clause )
10782        cv-qualifier-seq [opt]
10783        exception-specification [opt]
10784      direct-declarator [ constant-expression [opt] ]
10785      ( declarator )
10786
10787    direct-abstract-declarator:
10788      direct-abstract-declarator [opt]
10789        ( parameter-declaration-clause )
10790        cv-qualifier-seq [opt]
10791        exception-specification [opt]
10792      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10793      ( abstract-declarator )
10794
10795    Returns a representation of the declarator.  DCL_KIND is
10796    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10797    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10798    we are parsing a direct-declarator.  It is
10799    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10800    of ambiguity we prefer an abstract declarator, as per
10801    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10802    cp_parser_declarator.  */
10803
10804 static cp_declarator *
10805 cp_parser_direct_declarator (cp_parser* parser,
10806                              cp_parser_declarator_kind dcl_kind,
10807                              int* ctor_dtor_or_conv_p,
10808                              bool member_p)
10809 {
10810   cp_token *token;
10811   cp_declarator *declarator = NULL;
10812   tree scope = NULL_TREE;
10813   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10814   bool saved_in_declarator_p = parser->in_declarator_p;
10815   bool first = true;
10816   bool pop_p = false;
10817
10818   while (true)
10819     {
10820       /* Peek at the next token.  */
10821       token = cp_lexer_peek_token (parser->lexer);
10822       if (token->type == CPP_OPEN_PAREN)
10823         {
10824           /* This is either a parameter-declaration-clause, or a
10825              parenthesized declarator. When we know we are parsing a
10826              named declarator, it must be a parenthesized declarator
10827              if FIRST is true. For instance, `(int)' is a
10828              parameter-declaration-clause, with an omitted
10829              direct-abstract-declarator. But `((*))', is a
10830              parenthesized abstract declarator. Finally, when T is a
10831              template parameter `(T)' is a
10832              parameter-declaration-clause, and not a parenthesized
10833              named declarator.
10834
10835              We first try and parse a parameter-declaration-clause,
10836              and then try a nested declarator (if FIRST is true).
10837
10838              It is not an error for it not to be a
10839              parameter-declaration-clause, even when FIRST is
10840              false. Consider,
10841
10842                int i (int);
10843                int i (3);
10844
10845              The first is the declaration of a function while the
10846              second is a the definition of a variable, including its
10847              initializer.
10848
10849              Having seen only the parenthesis, we cannot know which of
10850              these two alternatives should be selected.  Even more
10851              complex are examples like:
10852
10853                int i (int (a));
10854                int i (int (3));
10855
10856              The former is a function-declaration; the latter is a
10857              variable initialization.
10858
10859              Thus again, we try a parameter-declaration-clause, and if
10860              that fails, we back out and return.  */
10861
10862           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10863             {
10864               cp_parameter_declarator *params;
10865               unsigned saved_num_template_parameter_lists;
10866
10867               /* In a member-declarator, the only valid interpretation
10868                  of a parenthesis is the start of a
10869                  parameter-declaration-clause.  (It is invalid to
10870                  initialize a static data member with a parenthesized
10871                  initializer; only the "=" form of initialization is
10872                  permitted.)  */
10873               if (!member_p)
10874                 cp_parser_parse_tentatively (parser);
10875
10876               /* Consume the `('.  */
10877               cp_lexer_consume_token (parser->lexer);
10878               if (first)
10879                 {
10880                   /* If this is going to be an abstract declarator, we're
10881                      in a declarator and we can't have default args.  */
10882                   parser->default_arg_ok_p = false;
10883                   parser->in_declarator_p = true;
10884                 }
10885
10886               /* Inside the function parameter list, surrounding
10887                  template-parameter-lists do not apply.  */
10888               saved_num_template_parameter_lists
10889                 = parser->num_template_parameter_lists;
10890               parser->num_template_parameter_lists = 0;
10891
10892               /* Parse the parameter-declaration-clause.  */
10893               params = cp_parser_parameter_declaration_clause (parser);
10894
10895               parser->num_template_parameter_lists
10896                 = saved_num_template_parameter_lists;
10897
10898               /* If all went well, parse the cv-qualifier-seq and the
10899                  exception-specification.  */
10900               if (member_p || cp_parser_parse_definitely (parser))
10901                 {
10902                   cp_cv_quals cv_quals;
10903                   tree exception_specification;
10904
10905                   if (ctor_dtor_or_conv_p)
10906                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10907                   first = false;
10908                   /* Consume the `)'.  */
10909                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10910
10911                   /* Parse the cv-qualifier-seq.  */
10912                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10913                   /* And the exception-specification.  */
10914                   exception_specification
10915                     = cp_parser_exception_specification_opt (parser);
10916
10917                   /* Create the function-declarator.  */
10918                   declarator = make_call_declarator (declarator,
10919                                                      params,
10920                                                      cv_quals,
10921                                                      exception_specification);
10922                   /* Any subsequent parameter lists are to do with
10923                      return type, so are not those of the declared
10924                      function.  */
10925                   parser->default_arg_ok_p = false;
10926
10927                   /* Repeat the main loop.  */
10928                   continue;
10929                 }
10930             }
10931
10932           /* If this is the first, we can try a parenthesized
10933              declarator.  */
10934           if (first)
10935             {
10936               bool saved_in_type_id_in_expr_p;
10937
10938               parser->default_arg_ok_p = saved_default_arg_ok_p;
10939               parser->in_declarator_p = saved_in_declarator_p;
10940
10941               /* Consume the `('.  */
10942               cp_lexer_consume_token (parser->lexer);
10943               /* Parse the nested declarator.  */
10944               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10945               parser->in_type_id_in_expr_p = true;
10946               declarator
10947                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10948                                         /*parenthesized_p=*/NULL,
10949                                         member_p);
10950               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10951               first = false;
10952               /* Expect a `)'.  */
10953               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10954                 declarator = cp_error_declarator;
10955               if (declarator == cp_error_declarator)
10956                 break;
10957
10958               goto handle_declarator;
10959             }
10960           /* Otherwise, we must be done.  */
10961           else
10962             break;
10963         }
10964       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10965                && token->type == CPP_OPEN_SQUARE)
10966         {
10967           /* Parse an array-declarator.  */
10968           tree bounds;
10969
10970           if (ctor_dtor_or_conv_p)
10971             *ctor_dtor_or_conv_p = 0;
10972
10973           first = false;
10974           parser->default_arg_ok_p = false;
10975           parser->in_declarator_p = true;
10976           /* Consume the `['.  */
10977           cp_lexer_consume_token (parser->lexer);
10978           /* Peek at the next token.  */
10979           token = cp_lexer_peek_token (parser->lexer);
10980           /* If the next token is `]', then there is no
10981              constant-expression.  */
10982           if (token->type != CPP_CLOSE_SQUARE)
10983             {
10984               bool non_constant_p;
10985
10986               bounds
10987                 = cp_parser_constant_expression (parser,
10988                                                  /*allow_non_constant=*/true,
10989                                                  &non_constant_p);
10990               if (!non_constant_p)
10991                 bounds = fold_non_dependent_expr (bounds);
10992               else if (!at_function_scope_p ())
10993                 {
10994                   error ("array bound is not an integer constant");
10995                   bounds = error_mark_node;
10996                 }
10997             }
10998           else
10999             bounds = NULL_TREE;
11000           /* Look for the closing `]'.  */
11001           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11002             {
11003               declarator = cp_error_declarator;
11004               break;
11005             }
11006
11007           declarator = make_array_declarator (declarator, bounds);
11008         }
11009       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11010         {
11011           tree id;
11012
11013           /* Parse a declarator-id */
11014           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11015             cp_parser_parse_tentatively (parser);
11016           id = cp_parser_declarator_id (parser);
11017           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11018             {
11019               if (!cp_parser_parse_definitely (parser))
11020                 id = error_mark_node;
11021               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11022                 {
11023                   cp_parser_error (parser, "expected unqualified-id");
11024                   id = error_mark_node;
11025                 }
11026             }
11027
11028           if (id == error_mark_node)
11029             {
11030               declarator = cp_error_declarator;
11031               break;
11032             }
11033
11034           if (TREE_CODE (id) == SCOPE_REF && at_namespace_scope_p ())
11035             {
11036               tree scope = TREE_OPERAND (id, 0);
11037
11038               /* In the declaration of a member of a template class
11039                  outside of the class itself, the SCOPE will sometimes
11040                  be a TYPENAME_TYPE.  For example, given:
11041
11042                  template <typename T>
11043                  int S<T>::R::i = 3;
11044
11045                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11046                  this context, we must resolve S<T>::R to an ordinary
11047                  type, rather than a typename type.
11048
11049                  The reason we normally avoid resolving TYPENAME_TYPEs
11050                  is that a specialization of `S' might render
11051                  `S<T>::R' not a type.  However, if `S' is
11052                  specialized, then this `i' will not be used, so there
11053                  is no harm in resolving the types here.  */
11054               if (TREE_CODE (scope) == TYPENAME_TYPE)
11055                 {
11056                   tree type;
11057
11058                   /* Resolve the TYPENAME_TYPE.  */
11059                   type = resolve_typename_type (scope,
11060                                                  /*only_current_p=*/false);
11061                   /* If that failed, the declarator is invalid.  */
11062                   if (type == error_mark_node)
11063                     error ("%<%T::%D%> is not a type",
11064                            TYPE_CONTEXT (scope),
11065                            TYPE_IDENTIFIER (scope));
11066                   /* Build a new DECLARATOR.  */
11067                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11068                 }
11069             }
11070
11071           declarator = make_id_declarator (id);
11072           if (id)
11073             {
11074               tree class_type;
11075               tree unqualified_name;
11076
11077               if (TREE_CODE (id) == SCOPE_REF
11078                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11079                 {
11080                   class_type = TREE_OPERAND (id, 0);
11081                   unqualified_name = TREE_OPERAND (id, 1);
11082                 }
11083               else
11084                 {
11085                   class_type = current_class_type;
11086                   unqualified_name = id;
11087                 }
11088
11089               if (class_type)
11090                 {
11091                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11092                     declarator->u.id.sfk = sfk_destructor;
11093                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11094                     declarator->u.id.sfk = sfk_conversion;
11095                   else if (constructor_name_p (unqualified_name,
11096                                                class_type)
11097                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11098                                && same_type_p (TREE_TYPE (unqualified_name),
11099                                                class_type)))
11100                     declarator->u.id.sfk = sfk_constructor;
11101
11102                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11103                     *ctor_dtor_or_conv_p = -1;
11104                   if (TREE_CODE (id) == SCOPE_REF
11105                       && TREE_CODE (unqualified_name) == TYPE_DECL
11106                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11107                     {
11108                       error ("invalid use of constructor as a template");
11109                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11110                               "the constructor in a qualified name",
11111                               class_type,
11112                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11113                               class_type, class_type);
11114                     }
11115                 }
11116             }
11117
11118         handle_declarator:;
11119           scope = get_scope_of_declarator (declarator);
11120           if (scope)
11121             /* Any names that appear after the declarator-id for a
11122                member are looked up in the containing scope.  */
11123             pop_p = push_scope (scope);
11124           parser->in_declarator_p = true;
11125           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11126               || (declarator && declarator->kind == cdk_id))
11127             /* Default args are only allowed on function
11128                declarations.  */
11129             parser->default_arg_ok_p = saved_default_arg_ok_p;
11130           else
11131             parser->default_arg_ok_p = false;
11132
11133           first = false;
11134         }
11135       /* We're done.  */
11136       else
11137         break;
11138     }
11139
11140   /* For an abstract declarator, we might wind up with nothing at this
11141      point.  That's an error; the declarator is not optional.  */
11142   if (!declarator)
11143     cp_parser_error (parser, "expected declarator");
11144
11145   /* If we entered a scope, we must exit it now.  */
11146   if (pop_p)
11147     pop_scope (scope);
11148
11149   parser->default_arg_ok_p = saved_default_arg_ok_p;
11150   parser->in_declarator_p = saved_in_declarator_p;
11151
11152   return declarator;
11153 }
11154
11155 /* Parse a ptr-operator.
11156
11157    ptr-operator:
11158      * cv-qualifier-seq [opt]
11159      &
11160      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11161
11162    GNU Extension:
11163
11164    ptr-operator:
11165      & cv-qualifier-seq [opt]
11166
11167    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11168    Returns ADDR_EXPR if a reference was used.  In the case of a
11169    pointer-to-member, *TYPE is filled in with the TYPE containing the
11170    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11171    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11172    ERROR_MARK if an error occurred.  */
11173
11174 static enum tree_code
11175 cp_parser_ptr_operator (cp_parser* parser,
11176                         tree* type,
11177                         cp_cv_quals *cv_quals)
11178 {
11179   enum tree_code code = ERROR_MARK;
11180   cp_token *token;
11181
11182   /* Assume that it's not a pointer-to-member.  */
11183   *type = NULL_TREE;
11184   /* And that there are no cv-qualifiers.  */
11185   *cv_quals = TYPE_UNQUALIFIED;
11186
11187   /* Peek at the next token.  */
11188   token = cp_lexer_peek_token (parser->lexer);
11189   /* If it's a `*' or `&' we have a pointer or reference.  */
11190   if (token->type == CPP_MULT || token->type == CPP_AND)
11191     {
11192       /* Remember which ptr-operator we were processing.  */
11193       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11194
11195       /* Consume the `*' or `&'.  */
11196       cp_lexer_consume_token (parser->lexer);
11197
11198       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11199          `&', if we are allowing GNU extensions.  (The only qualifier
11200          that can legally appear after `&' is `restrict', but that is
11201          enforced during semantic analysis.  */
11202       if (code == INDIRECT_REF
11203           || cp_parser_allow_gnu_extensions_p (parser))
11204         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11205     }
11206   else
11207     {
11208       /* Try the pointer-to-member case.  */
11209       cp_parser_parse_tentatively (parser);
11210       /* Look for the optional `::' operator.  */
11211       cp_parser_global_scope_opt (parser,
11212                                   /*current_scope_valid_p=*/false);
11213       /* Look for the nested-name specifier.  */
11214       cp_parser_nested_name_specifier (parser,
11215                                        /*typename_keyword_p=*/false,
11216                                        /*check_dependency_p=*/true,
11217                                        /*type_p=*/false,
11218                                        /*is_declaration=*/false);
11219       /* If we found it, and the next token is a `*', then we are
11220          indeed looking at a pointer-to-member operator.  */
11221       if (!cp_parser_error_occurred (parser)
11222           && cp_parser_require (parser, CPP_MULT, "`*'"))
11223         {
11224           /* The type of which the member is a member is given by the
11225              current SCOPE.  */
11226           *type = parser->scope;
11227           /* The next name will not be qualified.  */
11228           parser->scope = NULL_TREE;
11229           parser->qualifying_scope = NULL_TREE;
11230           parser->object_scope = NULL_TREE;
11231           /* Indicate that the `*' operator was used.  */
11232           code = INDIRECT_REF;
11233           /* Look for the optional cv-qualifier-seq.  */
11234           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11235         }
11236       /* If that didn't work we don't have a ptr-operator.  */
11237       if (!cp_parser_parse_definitely (parser))
11238         cp_parser_error (parser, "expected ptr-operator");
11239     }
11240
11241   return code;
11242 }
11243
11244 /* Parse an (optional) cv-qualifier-seq.
11245
11246    cv-qualifier-seq:
11247      cv-qualifier cv-qualifier-seq [opt]
11248
11249    cv-qualifier:
11250      const
11251      volatile
11252
11253    GNU Extension:
11254
11255    cv-qualifier:
11256      __restrict__
11257
11258    Returns a bitmask representing the cv-qualifiers.  */
11259
11260 static cp_cv_quals
11261 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11262 {
11263   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11264
11265   while (true)
11266     {
11267       cp_token *token;
11268       cp_cv_quals cv_qualifier;
11269
11270       /* Peek at the next token.  */
11271       token = cp_lexer_peek_token (parser->lexer);
11272       /* See if it's a cv-qualifier.  */
11273       switch (token->keyword)
11274         {
11275         case RID_CONST:
11276           cv_qualifier = TYPE_QUAL_CONST;
11277           break;
11278
11279         case RID_VOLATILE:
11280           cv_qualifier = TYPE_QUAL_VOLATILE;
11281           break;
11282
11283         case RID_RESTRICT:
11284           cv_qualifier = TYPE_QUAL_RESTRICT;
11285           break;
11286
11287         default:
11288           cv_qualifier = TYPE_UNQUALIFIED;
11289           break;
11290         }
11291
11292       if (!cv_qualifier)
11293         break;
11294
11295       if (cv_quals & cv_qualifier)
11296         {
11297           error ("duplicate cv-qualifier");
11298           cp_lexer_purge_token (parser->lexer);
11299         }
11300       else
11301         {
11302           cp_lexer_consume_token (parser->lexer);
11303           cv_quals |= cv_qualifier;
11304         }
11305     }
11306
11307   return cv_quals;
11308 }
11309
11310 /* Parse a declarator-id.
11311
11312    declarator-id:
11313      id-expression
11314      :: [opt] nested-name-specifier [opt] type-name
11315
11316    In the `id-expression' case, the value returned is as for
11317    cp_parser_id_expression if the id-expression was an unqualified-id.
11318    If the id-expression was a qualified-id, then a SCOPE_REF is
11319    returned.  The first operand is the scope (either a NAMESPACE_DECL
11320    or TREE_TYPE), but the second is still just a representation of an
11321    unqualified-id.  */
11322
11323 static tree
11324 cp_parser_declarator_id (cp_parser* parser)
11325 {
11326   tree id_expression;
11327
11328   /* The expression must be an id-expression.  Assume that qualified
11329      names are the names of types so that:
11330
11331        template <class T>
11332        int S<T>::R::i = 3;
11333
11334      will work; we must treat `S<T>::R' as the name of a type.
11335      Similarly, assume that qualified names are templates, where
11336      required, so that:
11337
11338        template <class T>
11339        int S<T>::R<T>::i = 3;
11340
11341      will work, too.  */
11342   id_expression = cp_parser_id_expression (parser,
11343                                            /*template_keyword_p=*/false,
11344                                            /*check_dependency_p=*/false,
11345                                            /*template_p=*/NULL,
11346                                            /*declarator_p=*/true);
11347   /* If the name was qualified, create a SCOPE_REF to represent
11348      that.  */
11349   if (parser->scope)
11350     {
11351       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11352       parser->scope = NULL_TREE;
11353     }
11354
11355   return id_expression;
11356 }
11357
11358 /* Parse a type-id.
11359
11360    type-id:
11361      type-specifier-seq abstract-declarator [opt]
11362
11363    Returns the TYPE specified.  */
11364
11365 static tree
11366 cp_parser_type_id (cp_parser* parser)
11367 {
11368   cp_decl_specifier_seq type_specifier_seq;
11369   cp_declarator *abstract_declarator;
11370
11371   /* Parse the type-specifier-seq.  */
11372   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11373   if (type_specifier_seq.type == error_mark_node)
11374     return error_mark_node;
11375
11376   /* There might or might not be an abstract declarator.  */
11377   cp_parser_parse_tentatively (parser);
11378   /* Look for the declarator.  */
11379   abstract_declarator
11380     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11381                             /*parenthesized_p=*/NULL,
11382                             /*member_p=*/false);
11383   /* Check to see if there really was a declarator.  */
11384   if (!cp_parser_parse_definitely (parser))
11385     abstract_declarator = NULL;
11386
11387   return groktypename (&type_specifier_seq, abstract_declarator);
11388 }
11389
11390 /* Parse a type-specifier-seq.
11391
11392    type-specifier-seq:
11393      type-specifier type-specifier-seq [opt]
11394
11395    GNU extension:
11396
11397    type-specifier-seq:
11398      attributes type-specifier-seq [opt]
11399
11400    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11401
11402 static void
11403 cp_parser_type_specifier_seq (cp_parser* parser,
11404                               cp_decl_specifier_seq *type_specifier_seq)
11405 {
11406   bool seen_type_specifier = false;
11407
11408   /* Clear the TYPE_SPECIFIER_SEQ.  */
11409   clear_decl_specs (type_specifier_seq);
11410
11411   /* Parse the type-specifiers and attributes.  */
11412   while (true)
11413     {
11414       tree type_specifier;
11415
11416       /* Check for attributes first.  */
11417       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11418         {
11419           type_specifier_seq->attributes =
11420             chainon (type_specifier_seq->attributes,
11421                      cp_parser_attributes_opt (parser));
11422           continue;
11423         }
11424
11425       /* Look for the type-specifier.  */
11426       type_specifier = cp_parser_type_specifier (parser,
11427                                                  CP_PARSER_FLAGS_OPTIONAL,
11428                                                  type_specifier_seq,
11429                                                  /*is_declaration=*/false,
11430                                                  NULL,
11431                                                  NULL);
11432       /* If the first type-specifier could not be found, this is not a
11433          type-specifier-seq at all.  */
11434       if (!seen_type_specifier && !type_specifier)
11435         {
11436           cp_parser_error (parser, "expected type-specifier");
11437           type_specifier_seq->type = error_mark_node;
11438           return;
11439         }
11440       /* If subsequent type-specifiers could not be found, the
11441          type-specifier-seq is complete.  */
11442       else if (seen_type_specifier && !type_specifier)
11443         break;
11444
11445       seen_type_specifier = true;
11446     }
11447
11448   return;
11449 }
11450
11451 /* Parse a parameter-declaration-clause.
11452
11453    parameter-declaration-clause:
11454      parameter-declaration-list [opt] ... [opt]
11455      parameter-declaration-list , ...
11456
11457    Returns a representation for the parameter declarations.  A return
11458    value of NULL indicates a parameter-declaration-clause consisting
11459    only of an ellipsis.  */
11460
11461 static cp_parameter_declarator *
11462 cp_parser_parameter_declaration_clause (cp_parser* parser)
11463 {
11464   cp_parameter_declarator *parameters;
11465   cp_token *token;
11466   bool ellipsis_p;
11467   bool is_error;
11468
11469   /* Peek at the next token.  */
11470   token = cp_lexer_peek_token (parser->lexer);
11471   /* Check for trivial parameter-declaration-clauses.  */
11472   if (token->type == CPP_ELLIPSIS)
11473     {
11474       /* Consume the `...' token.  */
11475       cp_lexer_consume_token (parser->lexer);
11476       return NULL;
11477     }
11478   else if (token->type == CPP_CLOSE_PAREN)
11479     /* There are no parameters.  */
11480     {
11481 #ifndef NO_IMPLICIT_EXTERN_C
11482       if (in_system_header && current_class_type == NULL
11483           && current_lang_name == lang_name_c)
11484         return NULL;
11485       else
11486 #endif
11487         return no_parameters;
11488     }
11489   /* Check for `(void)', too, which is a special case.  */
11490   else if (token->keyword == RID_VOID
11491            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11492                == CPP_CLOSE_PAREN))
11493     {
11494       /* Consume the `void' token.  */
11495       cp_lexer_consume_token (parser->lexer);
11496       /* There are no parameters.  */
11497       return no_parameters;
11498     }
11499
11500   /* Parse the parameter-declaration-list.  */
11501   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11502   /* If a parse error occurred while parsing the
11503      parameter-declaration-list, then the entire
11504      parameter-declaration-clause is erroneous.  */
11505   if (is_error)
11506     return NULL;
11507
11508   /* Peek at the next token.  */
11509   token = cp_lexer_peek_token (parser->lexer);
11510   /* If it's a `,', the clause should terminate with an ellipsis.  */
11511   if (token->type == CPP_COMMA)
11512     {
11513       /* Consume the `,'.  */
11514       cp_lexer_consume_token (parser->lexer);
11515       /* Expect an ellipsis.  */
11516       ellipsis_p
11517         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11518     }
11519   /* It might also be `...' if the optional trailing `,' was
11520      omitted.  */
11521   else if (token->type == CPP_ELLIPSIS)
11522     {
11523       /* Consume the `...' token.  */
11524       cp_lexer_consume_token (parser->lexer);
11525       /* And remember that we saw it.  */
11526       ellipsis_p = true;
11527     }
11528   else
11529     ellipsis_p = false;
11530
11531   /* Finish the parameter list.  */
11532   if (parameters && ellipsis_p)
11533     parameters->ellipsis_p = true;
11534
11535   return parameters;
11536 }
11537
11538 /* Parse a parameter-declaration-list.
11539
11540    parameter-declaration-list:
11541      parameter-declaration
11542      parameter-declaration-list , parameter-declaration
11543
11544    Returns a representation of the parameter-declaration-list, as for
11545    cp_parser_parameter_declaration_clause.  However, the
11546    `void_list_node' is never appended to the list.  Upon return,
11547    *IS_ERROR will be true iff an error occurred.  */
11548
11549 static cp_parameter_declarator *
11550 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11551 {
11552   cp_parameter_declarator *parameters = NULL;
11553   cp_parameter_declarator **tail = &parameters;
11554
11555   /* Assume all will go well.  */
11556   *is_error = false;
11557
11558   /* Look for more parameters.  */
11559   while (true)
11560     {
11561       cp_parameter_declarator *parameter;
11562       bool parenthesized_p;
11563       /* Parse the parameter.  */
11564       parameter
11565         = cp_parser_parameter_declaration (parser,
11566                                            /*template_parm_p=*/false,
11567                                            &parenthesized_p);
11568
11569       /* If a parse error occurred parsing the parameter declaration,
11570          then the entire parameter-declaration-list is erroneous.  */
11571       if (!parameter)
11572         {
11573           *is_error = true;
11574           parameters = NULL;
11575           break;
11576         }
11577       /* Add the new parameter to the list.  */
11578       *tail = parameter;
11579       tail = &parameter->next;
11580
11581       /* Peek at the next token.  */
11582       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11583           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11584         /* The parameter-declaration-list is complete.  */
11585         break;
11586       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11587         {
11588           cp_token *token;
11589
11590           /* Peek at the next token.  */
11591           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11592           /* If it's an ellipsis, then the list is complete.  */
11593           if (token->type == CPP_ELLIPSIS)
11594             break;
11595           /* Otherwise, there must be more parameters.  Consume the
11596              `,'.  */
11597           cp_lexer_consume_token (parser->lexer);
11598           /* When parsing something like:
11599
11600                 int i(float f, double d)
11601
11602              we can tell after seeing the declaration for "f" that we
11603              are not looking at an initialization of a variable "i",
11604              but rather at the declaration of a function "i".
11605
11606              Due to the fact that the parsing of template arguments
11607              (as specified to a template-id) requires backtracking we
11608              cannot use this technique when inside a template argument
11609              list.  */
11610           if (!parser->in_template_argument_list_p
11611               && !parser->in_type_id_in_expr_p
11612               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11613               /* However, a parameter-declaration of the form
11614                  "foat(f)" (which is a valid declaration of a
11615                  parameter "f") can also be interpreted as an
11616                  expression (the conversion of "f" to "float").  */
11617               && !parenthesized_p)
11618             cp_parser_commit_to_tentative_parse (parser);
11619         }
11620       else
11621         {
11622           cp_parser_error (parser, "expected %<,%> or %<...%>");
11623           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11624             cp_parser_skip_to_closing_parenthesis (parser,
11625                                                    /*recovering=*/true,
11626                                                    /*or_comma=*/false,
11627                                                    /*consume_paren=*/false);
11628           break;
11629         }
11630     }
11631
11632   return parameters;
11633 }
11634
11635 /* Parse a parameter declaration.
11636
11637    parameter-declaration:
11638      decl-specifier-seq declarator
11639      decl-specifier-seq declarator = assignment-expression
11640      decl-specifier-seq abstract-declarator [opt]
11641      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11642
11643    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11644    declares a template parameter.  (In that case, a non-nested `>'
11645    token encountered during the parsing of the assignment-expression
11646    is not interpreted as a greater-than operator.)
11647
11648    Returns a representation of the parameter, or NULL if an error
11649    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11650    true iff the declarator is of the form "(p)".  */
11651
11652 static cp_parameter_declarator *
11653 cp_parser_parameter_declaration (cp_parser *parser,
11654                                  bool template_parm_p,
11655                                  bool *parenthesized_p)
11656 {
11657   int declares_class_or_enum;
11658   bool greater_than_is_operator_p;
11659   cp_decl_specifier_seq decl_specifiers;
11660   cp_declarator *declarator;
11661   tree default_argument;
11662   cp_token *token;
11663   const char *saved_message;
11664
11665   /* In a template parameter, `>' is not an operator.
11666
11667      [temp.param]
11668
11669      When parsing a default template-argument for a non-type
11670      template-parameter, the first non-nested `>' is taken as the end
11671      of the template parameter-list rather than a greater-than
11672      operator.  */
11673   greater_than_is_operator_p = !template_parm_p;
11674
11675   /* Type definitions may not appear in parameter types.  */
11676   saved_message = parser->type_definition_forbidden_message;
11677   parser->type_definition_forbidden_message
11678     = "types may not be defined in parameter types";
11679
11680   /* Parse the declaration-specifiers.  */
11681   cp_parser_decl_specifier_seq (parser,
11682                                 CP_PARSER_FLAGS_NONE,
11683                                 &decl_specifiers,
11684                                 &declares_class_or_enum);
11685   /* If an error occurred, there's no reason to attempt to parse the
11686      rest of the declaration.  */
11687   if (cp_parser_error_occurred (parser))
11688     {
11689       parser->type_definition_forbidden_message = saved_message;
11690       return NULL;
11691     }
11692
11693   /* Peek at the next token.  */
11694   token = cp_lexer_peek_token (parser->lexer);
11695   /* If the next token is a `)', `,', `=', `>', or `...', then there
11696      is no declarator.  */
11697   if (token->type == CPP_CLOSE_PAREN
11698       || token->type == CPP_COMMA
11699       || token->type == CPP_EQ
11700       || token->type == CPP_ELLIPSIS
11701       || token->type == CPP_GREATER)
11702     {
11703       declarator = NULL;
11704       if (parenthesized_p)
11705         *parenthesized_p = false;
11706     }
11707   /* Otherwise, there should be a declarator.  */
11708   else
11709     {
11710       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11711       parser->default_arg_ok_p = false;
11712
11713       /* After seeing a decl-specifier-seq, if the next token is not a
11714          "(", there is no possibility that the code is a valid
11715          expression.  Therefore, if parsing tentatively, we commit at
11716          this point.  */
11717       if (!parser->in_template_argument_list_p
11718           /* In an expression context, having seen:
11719
11720                (int((char ...
11721
11722              we cannot be sure whether we are looking at a
11723              function-type (taking a "char" as a parameter) or a cast
11724              of some object of type "char" to "int".  */
11725           && !parser->in_type_id_in_expr_p
11726           && cp_parser_uncommitted_to_tentative_parse_p (parser)
11727           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11728         cp_parser_commit_to_tentative_parse (parser);
11729       /* Parse the declarator.  */
11730       declarator = cp_parser_declarator (parser,
11731                                          CP_PARSER_DECLARATOR_EITHER,
11732                                          /*ctor_dtor_or_conv_p=*/NULL,
11733                                          parenthesized_p,
11734                                          /*member_p=*/false);
11735       parser->default_arg_ok_p = saved_default_arg_ok_p;
11736       /* After the declarator, allow more attributes.  */
11737       decl_specifiers.attributes
11738         = chainon (decl_specifiers.attributes,
11739                    cp_parser_attributes_opt (parser));
11740     }
11741
11742   /* The restriction on defining new types applies only to the type
11743      of the parameter, not to the default argument.  */
11744   parser->type_definition_forbidden_message = saved_message;
11745
11746   /* If the next token is `=', then process a default argument.  */
11747   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11748     {
11749       bool saved_greater_than_is_operator_p;
11750       /* Consume the `='.  */
11751       cp_lexer_consume_token (parser->lexer);
11752
11753       /* If we are defining a class, then the tokens that make up the
11754          default argument must be saved and processed later.  */
11755       if (!template_parm_p && at_class_scope_p ()
11756           && TYPE_BEING_DEFINED (current_class_type))
11757         {
11758           unsigned depth = 0;
11759           cp_token *first_token;
11760           cp_token *token;
11761
11762           /* Add tokens until we have processed the entire default
11763              argument.  We add the range [first_token, token).  */
11764           first_token = cp_lexer_peek_token (parser->lexer);
11765           while (true)
11766             {
11767               bool done = false;
11768
11769               /* Peek at the next token.  */
11770               token = cp_lexer_peek_token (parser->lexer);
11771               /* What we do depends on what token we have.  */
11772               switch (token->type)
11773                 {
11774                   /* In valid code, a default argument must be
11775                      immediately followed by a `,' `)', or `...'.  */
11776                 case CPP_COMMA:
11777                 case CPP_CLOSE_PAREN:
11778                 case CPP_ELLIPSIS:
11779                   /* If we run into a non-nested `;', `}', or `]',
11780                      then the code is invalid -- but the default
11781                      argument is certainly over.  */
11782                 case CPP_SEMICOLON:
11783                 case CPP_CLOSE_BRACE:
11784                 case CPP_CLOSE_SQUARE:
11785                   if (depth == 0)
11786                     done = true;
11787                   /* Update DEPTH, if necessary.  */
11788                   else if (token->type == CPP_CLOSE_PAREN
11789                            || token->type == CPP_CLOSE_BRACE
11790                            || token->type == CPP_CLOSE_SQUARE)
11791                     --depth;
11792                   break;
11793
11794                 case CPP_OPEN_PAREN:
11795                 case CPP_OPEN_SQUARE:
11796                 case CPP_OPEN_BRACE:
11797                   ++depth;
11798                   break;
11799
11800                 case CPP_GREATER:
11801                   /* If we see a non-nested `>', and `>' is not an
11802                      operator, then it marks the end of the default
11803                      argument.  */
11804                   if (!depth && !greater_than_is_operator_p)
11805                     done = true;
11806                   break;
11807
11808                   /* If we run out of tokens, issue an error message.  */
11809                 case CPP_EOF:
11810                   error ("file ends in default argument");
11811                   done = true;
11812                   break;
11813
11814                 case CPP_NAME:
11815                 case CPP_SCOPE:
11816                   /* In these cases, we should look for template-ids.
11817                      For example, if the default argument is
11818                      `X<int, double>()', we need to do name lookup to
11819                      figure out whether or not `X' is a template; if
11820                      so, the `,' does not end the default argument.
11821
11822                      That is not yet done.  */
11823                   break;
11824
11825                 default:
11826                   break;
11827                 }
11828
11829               /* If we've reached the end, stop.  */
11830               if (done)
11831                 break;
11832
11833               /* Add the token to the token block.  */
11834               token = cp_lexer_consume_token (parser->lexer);
11835             }
11836
11837           /* Create a DEFAULT_ARG to represented the unparsed default
11838              argument.  */
11839           default_argument = make_node (DEFAULT_ARG);
11840           DEFARG_TOKENS (default_argument)
11841             = cp_token_cache_new (first_token, token);  
11842         }
11843       /* Outside of a class definition, we can just parse the
11844          assignment-expression.  */
11845       else
11846         {
11847           bool saved_local_variables_forbidden_p;
11848
11849           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11850              set correctly.  */
11851           saved_greater_than_is_operator_p
11852             = parser->greater_than_is_operator_p;
11853           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11854           /* Local variable names (and the `this' keyword) may not
11855              appear in a default argument.  */
11856           saved_local_variables_forbidden_p
11857             = parser->local_variables_forbidden_p;
11858           parser->local_variables_forbidden_p = true;
11859           /* Parse the assignment-expression.  */
11860           default_argument = cp_parser_assignment_expression (parser);
11861           /* Restore saved state.  */
11862           parser->greater_than_is_operator_p
11863             = saved_greater_than_is_operator_p;
11864           parser->local_variables_forbidden_p
11865             = saved_local_variables_forbidden_p;
11866         }
11867       if (!parser->default_arg_ok_p)
11868         {
11869           if (!flag_pedantic_errors)
11870             warning ("deprecated use of default argument for parameter of non-function");
11871           else
11872             {
11873               error ("default arguments are only permitted for function parameters");
11874               default_argument = NULL_TREE;
11875             }
11876         }
11877     }
11878   else
11879     default_argument = NULL_TREE;
11880
11881   return make_parameter_declarator (&decl_specifiers,
11882                                     declarator,
11883                                     default_argument);
11884 }
11885
11886 /* Parse a function-body.
11887
11888    function-body:
11889      compound_statement  */
11890
11891 static void
11892 cp_parser_function_body (cp_parser *parser)
11893 {
11894   cp_parser_compound_statement (parser, NULL, false);
11895 }
11896
11897 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11898    true if a ctor-initializer was present.  */
11899
11900 static bool
11901 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11902 {
11903   tree body;
11904   bool ctor_initializer_p;
11905
11906   /* Begin the function body.  */
11907   body = begin_function_body ();
11908   /* Parse the optional ctor-initializer.  */
11909   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11910   /* Parse the function-body.  */
11911   cp_parser_function_body (parser);
11912   /* Finish the function body.  */
11913   finish_function_body (body);
11914
11915   return ctor_initializer_p;
11916 }
11917
11918 /* Parse an initializer.
11919
11920    initializer:
11921      = initializer-clause
11922      ( expression-list )
11923
11924    Returns a expression representing the initializer.  If no
11925    initializer is present, NULL_TREE is returned.
11926
11927    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11928    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11929    set to FALSE if there is no initializer present.  If there is an
11930    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11931    is set to true; otherwise it is set to false.  */
11932
11933 static tree
11934 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11935                        bool* non_constant_p)
11936 {
11937   cp_token *token;
11938   tree init;
11939
11940   /* Peek at the next token.  */
11941   token = cp_lexer_peek_token (parser->lexer);
11942
11943   /* Let our caller know whether or not this initializer was
11944      parenthesized.  */
11945   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11946   /* Assume that the initializer is constant.  */
11947   *non_constant_p = false;
11948
11949   if (token->type == CPP_EQ)
11950     {
11951       /* Consume the `='.  */
11952       cp_lexer_consume_token (parser->lexer);
11953       /* Parse the initializer-clause.  */
11954       init = cp_parser_initializer_clause (parser, non_constant_p);
11955     }
11956   else if (token->type == CPP_OPEN_PAREN)
11957     init = cp_parser_parenthesized_expression_list (parser, false,
11958                                                     non_constant_p);
11959   else
11960     {
11961       /* Anything else is an error.  */
11962       cp_parser_error (parser, "expected initializer");
11963       init = error_mark_node;
11964     }
11965
11966   return init;
11967 }
11968
11969 /* Parse an initializer-clause.
11970
11971    initializer-clause:
11972      assignment-expression
11973      { initializer-list , [opt] }
11974      { }
11975
11976    Returns an expression representing the initializer.
11977
11978    If the `assignment-expression' production is used the value
11979    returned is simply a representation for the expression.
11980
11981    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11982    the elements of the initializer-list (or NULL_TREE, if the last
11983    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11984    NULL_TREE.  There is no way to detect whether or not the optional
11985    trailing `,' was provided.  NON_CONSTANT_P is as for
11986    cp_parser_initializer.  */
11987
11988 static tree
11989 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11990 {
11991   tree initializer;
11992
11993   /* If it is not a `{', then we are looking at an
11994      assignment-expression.  */
11995   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11996     {
11997       initializer
11998         = cp_parser_constant_expression (parser,
11999                                         /*allow_non_constant_p=*/true,
12000                                         non_constant_p);
12001       if (!*non_constant_p)
12002         initializer = fold_non_dependent_expr (initializer);
12003     }
12004   else
12005     {
12006       /* Consume the `{' token.  */
12007       cp_lexer_consume_token (parser->lexer);
12008       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12009       initializer = make_node (CONSTRUCTOR);
12010       /* If it's not a `}', then there is a non-trivial initializer.  */
12011       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12012         {
12013           /* Parse the initializer list.  */
12014           CONSTRUCTOR_ELTS (initializer)
12015             = cp_parser_initializer_list (parser, non_constant_p);
12016           /* A trailing `,' token is allowed.  */
12017           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12018             cp_lexer_consume_token (parser->lexer);
12019         }
12020       /* Now, there should be a trailing `}'.  */
12021       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12022     }
12023
12024   return initializer;
12025 }
12026
12027 /* Parse an initializer-list.
12028
12029    initializer-list:
12030      initializer-clause
12031      initializer-list , initializer-clause
12032
12033    GNU Extension:
12034
12035    initializer-list:
12036      identifier : initializer-clause
12037      initializer-list, identifier : initializer-clause
12038
12039    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12040    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12041    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12042    as for cp_parser_initializer.  */
12043
12044 static tree
12045 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12046 {
12047   tree initializers = NULL_TREE;
12048
12049   /* Assume all of the expressions are constant.  */
12050   *non_constant_p = false;
12051
12052   /* Parse the rest of the list.  */
12053   while (true)
12054     {
12055       cp_token *token;
12056       tree identifier;
12057       tree initializer;
12058       bool clause_non_constant_p;
12059
12060       /* If the next token is an identifier and the following one is a
12061          colon, we are looking at the GNU designated-initializer
12062          syntax.  */
12063       if (cp_parser_allow_gnu_extensions_p (parser)
12064           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12065           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12066         {
12067           /* Consume the identifier.  */
12068           identifier = cp_lexer_consume_token (parser->lexer)->value;
12069           /* Consume the `:'.  */
12070           cp_lexer_consume_token (parser->lexer);
12071         }
12072       else
12073         identifier = NULL_TREE;
12074
12075       /* Parse the initializer.  */
12076       initializer = cp_parser_initializer_clause (parser,
12077                                                   &clause_non_constant_p);
12078       /* If any clause is non-constant, so is the entire initializer.  */
12079       if (clause_non_constant_p)
12080         *non_constant_p = true;
12081       /* Add it to the list.  */
12082       initializers = tree_cons (identifier, initializer, initializers);
12083
12084       /* If the next token is not a comma, we have reached the end of
12085          the list.  */
12086       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12087         break;
12088
12089       /* Peek at the next token.  */
12090       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12091       /* If the next token is a `}', then we're still done.  An
12092          initializer-clause can have a trailing `,' after the
12093          initializer-list and before the closing `}'.  */
12094       if (token->type == CPP_CLOSE_BRACE)
12095         break;
12096
12097       /* Consume the `,' token.  */
12098       cp_lexer_consume_token (parser->lexer);
12099     }
12100
12101   /* The initializers were built up in reverse order, so we need to
12102      reverse them now.  */
12103   return nreverse (initializers);
12104 }
12105
12106 /* Classes [gram.class] */
12107
12108 /* Parse a class-name.
12109
12110    class-name:
12111      identifier
12112      template-id
12113
12114    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12115    to indicate that names looked up in dependent types should be
12116    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12117    keyword has been used to indicate that the name that appears next
12118    is a template.  TAG_TYPE indicates the explicit tag given before
12119    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12120    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12121    is the class being defined in a class-head.
12122
12123    Returns the TYPE_DECL representing the class.  */
12124
12125 static tree
12126 cp_parser_class_name (cp_parser *parser,
12127                       bool typename_keyword_p,
12128                       bool template_keyword_p,
12129                       enum tag_types tag_type,
12130                       bool check_dependency_p,
12131                       bool class_head_p,
12132                       bool is_declaration)
12133 {
12134   tree decl;
12135   tree scope;
12136   bool typename_p;
12137   cp_token *token;
12138
12139   /* All class-names start with an identifier.  */
12140   token = cp_lexer_peek_token (parser->lexer);
12141   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12142     {
12143       cp_parser_error (parser, "expected class-name");
12144       return error_mark_node;
12145     }
12146
12147   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12148      to a template-id, so we save it here.  */
12149   scope = parser->scope;
12150   if (scope == error_mark_node)
12151     return error_mark_node;
12152
12153   /* Any name names a type if we're following the `typename' keyword
12154      in a qualified name where the enclosing scope is type-dependent.  */
12155   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12156                 && dependent_type_p (scope));
12157   /* Handle the common case (an identifier, but not a template-id)
12158      efficiently.  */
12159   if (token->type == CPP_NAME
12160       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12161     {
12162       tree identifier;
12163
12164       /* Look for the identifier.  */
12165       identifier = cp_parser_identifier (parser);
12166       /* If the next token isn't an identifier, we are certainly not
12167          looking at a class-name.  */
12168       if (identifier == error_mark_node)
12169         decl = error_mark_node;
12170       /* If we know this is a type-name, there's no need to look it
12171          up.  */
12172       else if (typename_p)
12173         decl = identifier;
12174       else
12175         {
12176           /* If the next token is a `::', then the name must be a type
12177              name.
12178
12179              [basic.lookup.qual]
12180
12181              During the lookup for a name preceding the :: scope
12182              resolution operator, object, function, and enumerator
12183              names are ignored.  */
12184           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12185             tag_type = typename_type;
12186           /* Look up the name.  */
12187           decl = cp_parser_lookup_name (parser, identifier,
12188                                         tag_type,
12189                                         /*is_template=*/false,
12190                                         /*is_namespace=*/false,
12191                                         check_dependency_p,
12192                                         /*ambiguous_p=*/NULL);
12193         }
12194     }
12195   else
12196     {
12197       /* Try a template-id.  */
12198       decl = cp_parser_template_id (parser, template_keyword_p,
12199                                     check_dependency_p,
12200                                     is_declaration);
12201       if (decl == error_mark_node)
12202         return error_mark_node;
12203     }
12204
12205   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12206
12207   /* If this is a typename, create a TYPENAME_TYPE.  */
12208   if (typename_p && decl != error_mark_node)
12209     {
12210       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12211       if (decl != error_mark_node)
12212         decl = TYPE_NAME (decl);
12213     }
12214
12215   /* Check to see that it is really the name of a class.  */
12216   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12217       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12218       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12219     /* Situations like this:
12220
12221          template <typename T> struct A {
12222            typename T::template X<int>::I i;
12223          };
12224
12225        are problematic.  Is `T::template X<int>' a class-name?  The
12226        standard does not seem to be definitive, but there is no other
12227        valid interpretation of the following `::'.  Therefore, those
12228        names are considered class-names.  */
12229     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12230   else if (decl == error_mark_node
12231            || TREE_CODE (decl) != TYPE_DECL
12232            || TREE_TYPE (decl) == error_mark_node
12233            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12234     {
12235       cp_parser_error (parser, "expected class-name");
12236       return error_mark_node;
12237     }
12238
12239   return decl;
12240 }
12241
12242 /* Parse a class-specifier.
12243
12244    class-specifier:
12245      class-head { member-specification [opt] }
12246
12247    Returns the TREE_TYPE representing the class.  */
12248
12249 static tree
12250 cp_parser_class_specifier (cp_parser* parser)
12251 {
12252   cp_token *token;
12253   tree type;
12254   tree attributes = NULL_TREE;
12255   int has_trailing_semicolon;
12256   bool nested_name_specifier_p;
12257   unsigned saved_num_template_parameter_lists;
12258   tree old_scope = NULL_TREE;
12259   tree scope = NULL_TREE;
12260
12261   push_deferring_access_checks (dk_no_deferred);
12262
12263   /* Parse the class-head.  */
12264   type = cp_parser_class_head (parser,
12265                                &nested_name_specifier_p,
12266                                &attributes);
12267   /* If the class-head was a semantic disaster, skip the entire body
12268      of the class.  */
12269   if (!type)
12270     {
12271       cp_parser_skip_to_end_of_block_or_statement (parser);
12272       pop_deferring_access_checks ();
12273       return error_mark_node;
12274     }
12275
12276   /* Look for the `{'.  */
12277   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12278     {
12279       pop_deferring_access_checks ();
12280       return error_mark_node;
12281     }
12282
12283   /* Issue an error message if type-definitions are forbidden here.  */
12284   cp_parser_check_type_definition (parser);
12285   /* Remember that we are defining one more class.  */
12286   ++parser->num_classes_being_defined;
12287   /* Inside the class, surrounding template-parameter-lists do not
12288      apply.  */
12289   saved_num_template_parameter_lists
12290     = parser->num_template_parameter_lists;
12291   parser->num_template_parameter_lists = 0;
12292
12293   /* Start the class.  */
12294   if (nested_name_specifier_p)
12295     {
12296       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12297       old_scope = push_inner_scope (scope);
12298     }
12299   type = begin_class_definition (type);
12300
12301   if (type == error_mark_node)
12302     /* If the type is erroneous, skip the entire body of the class.  */
12303     cp_parser_skip_to_closing_brace (parser);
12304   else
12305     /* Parse the member-specification.  */
12306     cp_parser_member_specification_opt (parser);
12307
12308   /* Look for the trailing `}'.  */
12309   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12310   /* We get better error messages by noticing a common problem: a
12311      missing trailing `;'.  */
12312   token = cp_lexer_peek_token (parser->lexer);
12313   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12314   /* Look for trailing attributes to apply to this class.  */
12315   if (cp_parser_allow_gnu_extensions_p (parser))
12316     {
12317       tree sub_attr = cp_parser_attributes_opt (parser);
12318       attributes = chainon (attributes, sub_attr);
12319     }
12320   if (type != error_mark_node)
12321     type = finish_struct (type, attributes);
12322   if (nested_name_specifier_p)
12323     pop_inner_scope (old_scope, scope);
12324   /* If this class is not itself within the scope of another class,
12325      then we need to parse the bodies of all of the queued function
12326      definitions.  Note that the queued functions defined in a class
12327      are not always processed immediately following the
12328      class-specifier for that class.  Consider:
12329
12330        struct A {
12331          struct B { void f() { sizeof (A); } };
12332        };
12333
12334      If `f' were processed before the processing of `A' were
12335      completed, there would be no way to compute the size of `A'.
12336      Note that the nesting we are interested in here is lexical --
12337      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12338      for:
12339
12340        struct A { struct B; };
12341        struct A::B { void f() { } };
12342
12343      there is no need to delay the parsing of `A::B::f'.  */
12344   if (--parser->num_classes_being_defined == 0)
12345     {
12346       tree queue_entry;
12347       tree fn;
12348       tree class_type;
12349       bool pop_p;
12350
12351       /* In a first pass, parse default arguments to the functions.
12352          Then, in a second pass, parse the bodies of the functions.
12353          This two-phased approach handles cases like:
12354
12355             struct S {
12356               void f() { g(); }
12357               void g(int i = 3);
12358             };
12359
12360          */
12361       class_type = NULL_TREE;
12362       pop_p = false;
12363       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12364              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12365            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12366            TREE_PURPOSE (parser->unparsed_functions_queues)
12367              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12368         {
12369           fn = TREE_VALUE (queue_entry);
12370           /* If there are default arguments that have not yet been processed,
12371              take care of them now.  */
12372           if (class_type != TREE_PURPOSE (queue_entry))
12373             {
12374               if (pop_p)
12375                 pop_scope (class_type);
12376               class_type = TREE_PURPOSE (queue_entry);
12377               pop_p = push_scope (class_type);
12378             }
12379           /* Make sure that any template parameters are in scope.  */
12380           maybe_begin_member_template_processing (fn);
12381           /* Parse the default argument expressions.  */
12382           cp_parser_late_parsing_default_args (parser, fn);
12383           /* Remove any template parameters from the symbol table.  */
12384           maybe_end_member_template_processing ();
12385         }
12386       if (pop_p)
12387         pop_scope (class_type);
12388       /* Now parse the body of the functions.  */
12389       for (TREE_VALUE (parser->unparsed_functions_queues)
12390              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12391            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12392            TREE_VALUE (parser->unparsed_functions_queues)
12393              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12394         {
12395           /* Figure out which function we need to process.  */
12396           fn = TREE_VALUE (queue_entry);
12397
12398           /* A hack to prevent garbage collection.  */
12399           function_depth++;
12400
12401           /* Parse the function.  */
12402           cp_parser_late_parsing_for_member (parser, fn);
12403           function_depth--;
12404         }
12405     }
12406
12407   /* Put back any saved access checks.  */
12408   pop_deferring_access_checks ();
12409
12410   /* Restore the count of active template-parameter-lists.  */
12411   parser->num_template_parameter_lists
12412     = saved_num_template_parameter_lists;
12413
12414   return type;
12415 }
12416
12417 /* Parse a class-head.
12418
12419    class-head:
12420      class-key identifier [opt] base-clause [opt]
12421      class-key nested-name-specifier identifier base-clause [opt]
12422      class-key nested-name-specifier [opt] template-id
12423        base-clause [opt]
12424
12425    GNU Extensions:
12426      class-key attributes identifier [opt] base-clause [opt]
12427      class-key attributes nested-name-specifier identifier base-clause [opt]
12428      class-key attributes nested-name-specifier [opt] template-id
12429        base-clause [opt]
12430
12431    Returns the TYPE of the indicated class.  Sets
12432    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12433    involving a nested-name-specifier was used, and FALSE otherwise.
12434
12435    Returns error_mark_node if this is not a class-head.
12436    
12437    Returns NULL_TREE if the class-head is syntactically valid, but
12438    semantically invalid in a way that means we should skip the entire
12439    body of the class.  */
12440
12441 static tree
12442 cp_parser_class_head (cp_parser* parser,
12443                       bool* nested_name_specifier_p,
12444                       tree *attributes_p)
12445 {
12446   tree nested_name_specifier;
12447   enum tag_types class_key;
12448   tree id = NULL_TREE;
12449   tree type = NULL_TREE;
12450   tree attributes;
12451   bool template_id_p = false;
12452   bool qualified_p = false;
12453   bool invalid_nested_name_p = false;
12454   bool invalid_explicit_specialization_p = false;
12455   bool pop_p = false;
12456   unsigned num_templates;
12457   tree bases;
12458
12459   /* Assume no nested-name-specifier will be present.  */
12460   *nested_name_specifier_p = false;
12461   /* Assume no template parameter lists will be used in defining the
12462      type.  */
12463   num_templates = 0;
12464
12465   /* Look for the class-key.  */
12466   class_key = cp_parser_class_key (parser);
12467   if (class_key == none_type)
12468     return error_mark_node;
12469
12470   /* Parse the attributes.  */
12471   attributes = cp_parser_attributes_opt (parser);
12472
12473   /* If the next token is `::', that is invalid -- but sometimes
12474      people do try to write:
12475
12476        struct ::S {};
12477
12478      Handle this gracefully by accepting the extra qualifier, and then
12479      issuing an error about it later if this really is a
12480      class-head.  If it turns out just to be an elaborated type
12481      specifier, remain silent.  */
12482   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12483     qualified_p = true;
12484
12485   push_deferring_access_checks (dk_no_check);
12486
12487   /* Determine the name of the class.  Begin by looking for an
12488      optional nested-name-specifier.  */
12489   nested_name_specifier
12490     = cp_parser_nested_name_specifier_opt (parser,
12491                                            /*typename_keyword_p=*/false,
12492                                            /*check_dependency_p=*/false,
12493                                            /*type_p=*/false,
12494                                            /*is_declaration=*/false);
12495   /* If there was a nested-name-specifier, then there *must* be an
12496      identifier.  */
12497   if (nested_name_specifier)
12498     {
12499       /* Although the grammar says `identifier', it really means
12500          `class-name' or `template-name'.  You are only allowed to
12501          define a class that has already been declared with this
12502          syntax.
12503
12504          The proposed resolution for Core Issue 180 says that whever
12505          you see `class T::X' you should treat `X' as a type-name.
12506
12507          It is OK to define an inaccessible class; for example:
12508
12509            class A { class B; };
12510            class A::B {};
12511
12512          We do not know if we will see a class-name, or a
12513          template-name.  We look for a class-name first, in case the
12514          class-name is a template-id; if we looked for the
12515          template-name first we would stop after the template-name.  */
12516       cp_parser_parse_tentatively (parser);
12517       type = cp_parser_class_name (parser,
12518                                    /*typename_keyword_p=*/false,
12519                                    /*template_keyword_p=*/false,
12520                                    class_type,
12521                                    /*check_dependency_p=*/false,
12522                                    /*class_head_p=*/true,
12523                                    /*is_declaration=*/false);
12524       /* If that didn't work, ignore the nested-name-specifier.  */
12525       if (!cp_parser_parse_definitely (parser))
12526         {
12527           invalid_nested_name_p = true;
12528           id = cp_parser_identifier (parser);
12529           if (id == error_mark_node)
12530             id = NULL_TREE;
12531         }
12532       /* If we could not find a corresponding TYPE, treat this
12533          declaration like an unqualified declaration.  */
12534       if (type == error_mark_node)
12535         nested_name_specifier = NULL_TREE;
12536       /* Otherwise, count the number of templates used in TYPE and its
12537          containing scopes.  */
12538       else
12539         {
12540           tree scope;
12541
12542           for (scope = TREE_TYPE (type);
12543                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12544                scope = (TYPE_P (scope)
12545                         ? TYPE_CONTEXT (scope)
12546                         : DECL_CONTEXT (scope)))
12547             if (TYPE_P (scope)
12548                 && CLASS_TYPE_P (scope)
12549                 && CLASSTYPE_TEMPLATE_INFO (scope)
12550                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12551                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12552               ++num_templates;
12553         }
12554     }
12555   /* Otherwise, the identifier is optional.  */
12556   else
12557     {
12558       /* We don't know whether what comes next is a template-id,
12559          an identifier, or nothing at all.  */
12560       cp_parser_parse_tentatively (parser);
12561       /* Check for a template-id.  */
12562       id = cp_parser_template_id (parser,
12563                                   /*template_keyword_p=*/false,
12564                                   /*check_dependency_p=*/true,
12565                                   /*is_declaration=*/true);
12566       /* If that didn't work, it could still be an identifier.  */
12567       if (!cp_parser_parse_definitely (parser))
12568         {
12569           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12570             id = cp_parser_identifier (parser);
12571           else
12572             id = NULL_TREE;
12573         }
12574       else
12575         {
12576           template_id_p = true;
12577           ++num_templates;
12578         }
12579     }
12580
12581   pop_deferring_access_checks ();
12582
12583   if (id)
12584     cp_parser_check_for_invalid_template_id (parser, id);
12585
12586   /* If it's not a `:' or a `{' then we can't really be looking at a
12587      class-head, since a class-head only appears as part of a
12588      class-specifier.  We have to detect this situation before calling
12589      xref_tag, since that has irreversible side-effects.  */
12590   if (!cp_parser_next_token_starts_class_definition_p (parser))
12591     {
12592       cp_parser_error (parser, "expected %<{%> or %<:%>");
12593       return error_mark_node;
12594     }
12595
12596   /* At this point, we're going ahead with the class-specifier, even
12597      if some other problem occurs.  */
12598   cp_parser_commit_to_tentative_parse (parser);
12599   /* Issue the error about the overly-qualified name now.  */
12600   if (qualified_p)
12601     cp_parser_error (parser,
12602                      "global qualification of class name is invalid");
12603   else if (invalid_nested_name_p)
12604     cp_parser_error (parser,
12605                      "qualified name does not name a class");
12606   else if (nested_name_specifier)
12607     {
12608       tree scope;
12609
12610       /* Reject typedef-names in class heads.  */
12611       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12612         {
12613           error ("invalid class name in declaration of %qD", type);
12614           type = NULL_TREE;
12615           goto done;
12616         }
12617
12618       /* Figure out in what scope the declaration is being placed.  */
12619       scope = current_scope ();
12620       /* If that scope does not contain the scope in which the
12621          class was originally declared, the program is invalid.  */
12622       if (scope && !is_ancestor (scope, nested_name_specifier))
12623         {
12624           error ("declaration of %qD in %qD which does not enclose %qD",
12625                  type, scope, nested_name_specifier);
12626           type = NULL_TREE;
12627           goto done;
12628         }
12629       /* [dcl.meaning]
12630
12631          A declarator-id shall not be qualified exception of the
12632          definition of a ... nested class outside of its class
12633          ... [or] a the definition or explicit instantiation of a
12634          class member of a namespace outside of its namespace.  */
12635       if (scope == nested_name_specifier)
12636         {
12637           pedwarn ("extra qualification ignored");
12638           nested_name_specifier = NULL_TREE;
12639           num_templates = 0;
12640         }
12641     }
12642   /* An explicit-specialization must be preceded by "template <>".  If
12643      it is not, try to recover gracefully.  */
12644   if (at_namespace_scope_p ()
12645       && parser->num_template_parameter_lists == 0
12646       && template_id_p)
12647     {
12648       error ("an explicit specialization must be preceded by %<template <>%>");
12649       invalid_explicit_specialization_p = true;
12650       /* Take the same action that would have been taken by
12651          cp_parser_explicit_specialization.  */
12652       ++parser->num_template_parameter_lists;
12653       begin_specialization ();
12654     }
12655   /* There must be no "return" statements between this point and the
12656      end of this function; set "type "to the correct return value and
12657      use "goto done;" to return.  */
12658   /* Make sure that the right number of template parameters were
12659      present.  */
12660   if (!cp_parser_check_template_parameters (parser, num_templates))
12661     {
12662       /* If something went wrong, there is no point in even trying to
12663          process the class-definition.  */
12664       type = NULL_TREE;
12665       goto done;
12666     }
12667
12668   /* Look up the type.  */
12669   if (template_id_p)
12670     {
12671       type = TREE_TYPE (id);
12672       maybe_process_partial_specialization (type);
12673     }
12674   else if (!nested_name_specifier)
12675     {
12676       /* If the class was unnamed, create a dummy name.  */
12677       if (!id)
12678         id = make_anon_name ();
12679       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
12680                        parser->num_template_parameter_lists);
12681     }
12682   else
12683     {
12684       tree class_type;
12685       bool pop_p = false;
12686
12687       /* Given:
12688
12689             template <typename T> struct S { struct T };
12690             template <typename T> struct S<T>::T { };
12691
12692          we will get a TYPENAME_TYPE when processing the definition of
12693          `S::T'.  We need to resolve it to the actual type before we
12694          try to define it.  */
12695       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12696         {
12697           class_type = resolve_typename_type (TREE_TYPE (type),
12698                                               /*only_current_p=*/false);
12699           if (class_type != error_mark_node)
12700             type = TYPE_NAME (class_type);
12701           else
12702             {
12703               cp_parser_error (parser, "could not resolve typename type");
12704               type = error_mark_node;
12705             }
12706         }
12707
12708       maybe_process_partial_specialization (TREE_TYPE (type));
12709       class_type = current_class_type;
12710       /* Enter the scope indicated by the nested-name-specifier.  */
12711       if (nested_name_specifier)
12712         pop_p = push_scope (nested_name_specifier);
12713       /* Get the canonical version of this type.  */
12714       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12715       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12716           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12717         {
12718           type = push_template_decl (type);
12719           if (type == error_mark_node)
12720             {
12721               type = NULL_TREE;
12722               goto done;
12723             }
12724         }
12725       
12726       type = TREE_TYPE (type);
12727       if (nested_name_specifier)
12728         {
12729           *nested_name_specifier_p = true;
12730           if (pop_p)
12731             pop_scope (nested_name_specifier);
12732         }
12733     }
12734   /* Indicate whether this class was declared as a `class' or as a
12735      `struct'.  */
12736   if (TREE_CODE (type) == RECORD_TYPE)
12737     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12738   cp_parser_check_class_key (class_key, type);
12739
12740   /* Enter the scope containing the class; the names of base classes
12741      should be looked up in that context.  For example, given:
12742
12743        struct A { struct B {}; struct C; };
12744        struct A::C : B {};
12745
12746      is valid.  */
12747   if (nested_name_specifier)
12748     pop_p = push_scope (nested_name_specifier);
12749
12750   bases = NULL_TREE;
12751
12752   /* Get the list of base-classes, if there is one.  */
12753   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12754     bases = cp_parser_base_clause (parser);
12755
12756   /* Process the base classes.  */
12757   xref_basetypes (type, bases);
12758
12759   /* Leave the scope given by the nested-name-specifier.  We will
12760      enter the class scope itself while processing the members.  */
12761   if (pop_p)
12762     pop_scope (nested_name_specifier);
12763
12764  done:
12765   if (invalid_explicit_specialization_p)
12766     {
12767       end_specialization ();
12768       --parser->num_template_parameter_lists;
12769     }
12770   *attributes_p = attributes;
12771   return type;
12772 }
12773
12774 /* Parse a class-key.
12775
12776    class-key:
12777      class
12778      struct
12779      union
12780
12781    Returns the kind of class-key specified, or none_type to indicate
12782    error.  */
12783
12784 static enum tag_types
12785 cp_parser_class_key (cp_parser* parser)
12786 {
12787   cp_token *token;
12788   enum tag_types tag_type;
12789
12790   /* Look for the class-key.  */
12791   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12792   if (!token)
12793     return none_type;
12794
12795   /* Check to see if the TOKEN is a class-key.  */
12796   tag_type = cp_parser_token_is_class_key (token);
12797   if (!tag_type)
12798     cp_parser_error (parser, "expected class-key");
12799   return tag_type;
12800 }
12801
12802 /* Parse an (optional) member-specification.
12803
12804    member-specification:
12805      member-declaration member-specification [opt]
12806      access-specifier : member-specification [opt]  */
12807
12808 static void
12809 cp_parser_member_specification_opt (cp_parser* parser)
12810 {
12811   while (true)
12812     {
12813       cp_token *token;
12814       enum rid keyword;
12815
12816       /* Peek at the next token.  */
12817       token = cp_lexer_peek_token (parser->lexer);
12818       /* If it's a `}', or EOF then we've seen all the members.  */
12819       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12820         break;
12821
12822       /* See if this token is a keyword.  */
12823       keyword = token->keyword;
12824       switch (keyword)
12825         {
12826         case RID_PUBLIC:
12827         case RID_PROTECTED:
12828         case RID_PRIVATE:
12829           /* Consume the access-specifier.  */
12830           cp_lexer_consume_token (parser->lexer);
12831           /* Remember which access-specifier is active.  */
12832           current_access_specifier = token->value;
12833           /* Look for the `:'.  */
12834           cp_parser_require (parser, CPP_COLON, "`:'");
12835           break;
12836
12837         default:
12838           /* Accept #pragmas at class scope.  */
12839           if (token->type == CPP_PRAGMA)
12840             {
12841               cp_lexer_handle_pragma (parser->lexer);
12842               break;
12843             }
12844
12845           /* Otherwise, the next construction must be a
12846              member-declaration.  */
12847           cp_parser_member_declaration (parser);
12848         }
12849     }
12850 }
12851
12852 /* Parse a member-declaration.
12853
12854    member-declaration:
12855      decl-specifier-seq [opt] member-declarator-list [opt] ;
12856      function-definition ; [opt]
12857      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12858      using-declaration
12859      template-declaration
12860
12861    member-declarator-list:
12862      member-declarator
12863      member-declarator-list , member-declarator
12864
12865    member-declarator:
12866      declarator pure-specifier [opt]
12867      declarator constant-initializer [opt]
12868      identifier [opt] : constant-expression
12869
12870    GNU Extensions:
12871
12872    member-declaration:
12873      __extension__ member-declaration
12874
12875    member-declarator:
12876      declarator attributes [opt] pure-specifier [opt]
12877      declarator attributes [opt] constant-initializer [opt]
12878      identifier [opt] attributes [opt] : constant-expression  */
12879
12880 static void
12881 cp_parser_member_declaration (cp_parser* parser)
12882 {
12883   cp_decl_specifier_seq decl_specifiers;
12884   tree prefix_attributes;
12885   tree decl;
12886   int declares_class_or_enum;
12887   bool friend_p;
12888   cp_token *token;
12889   int saved_pedantic;
12890
12891   /* Check for the `__extension__' keyword.  */
12892   if (cp_parser_extension_opt (parser, &saved_pedantic))
12893     {
12894       /* Recurse.  */
12895       cp_parser_member_declaration (parser);
12896       /* Restore the old value of the PEDANTIC flag.  */
12897       pedantic = saved_pedantic;
12898
12899       return;
12900     }
12901
12902   /* Check for a template-declaration.  */
12903   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12904     {
12905       /* Parse the template-declaration.  */
12906       cp_parser_template_declaration (parser, /*member_p=*/true);
12907
12908       return;
12909     }
12910
12911   /* Check for a using-declaration.  */
12912   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12913     {
12914       /* Parse the using-declaration.  */
12915       cp_parser_using_declaration (parser);
12916
12917       return;
12918     }
12919
12920   /* Parse the decl-specifier-seq.  */
12921   cp_parser_decl_specifier_seq (parser,
12922                                 CP_PARSER_FLAGS_OPTIONAL,
12923                                 &decl_specifiers,
12924                                 &declares_class_or_enum);
12925   prefix_attributes = decl_specifiers.attributes;
12926   decl_specifiers.attributes = NULL_TREE;
12927   /* Check for an invalid type-name.  */
12928   if (!decl_specifiers.type
12929       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12930     return;
12931   /* If there is no declarator, then the decl-specifier-seq should
12932      specify a type.  */
12933   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12934     {
12935       /* If there was no decl-specifier-seq, and the next token is a
12936          `;', then we have something like:
12937
12938            struct S { ; };
12939
12940          [class.mem]
12941
12942          Each member-declaration shall declare at least one member
12943          name of the class.  */
12944       if (!decl_specifiers.any_specifiers_p)
12945         {
12946           cp_token *token = cp_lexer_peek_token (parser->lexer);
12947           if (pedantic && !token->in_system_header)
12948             pedwarn ("%Hextra %<;%>", &token->location);
12949         }
12950       else
12951         {
12952           tree type;
12953
12954           /* See if this declaration is a friend.  */
12955           friend_p = cp_parser_friend_p (&decl_specifiers);
12956           /* If there were decl-specifiers, check to see if there was
12957              a class-declaration.  */
12958           type = check_tag_decl (&decl_specifiers);
12959           /* Nested classes have already been added to the class, but
12960              a `friend' needs to be explicitly registered.  */
12961           if (friend_p)
12962             {
12963               /* If the `friend' keyword was present, the friend must
12964                  be introduced with a class-key.  */
12965                if (!declares_class_or_enum)
12966                  error ("a class-key must be used when declaring a friend");
12967                /* In this case:
12968
12969                     template <typename T> struct A {
12970                       friend struct A<T>::B;
12971                     };
12972
12973                   A<T>::B will be represented by a TYPENAME_TYPE, and
12974                   therefore not recognized by check_tag_decl.  */
12975                if (!type
12976                    && decl_specifiers.type
12977                    && TYPE_P (decl_specifiers.type))
12978                  type = decl_specifiers.type;
12979                if (!type || !TYPE_P (type))
12980                  error ("friend declaration does not name a class or "
12981                         "function");
12982                else
12983                  make_friend_class (current_class_type, type,
12984                                     /*complain=*/true);
12985             }
12986           /* If there is no TYPE, an error message will already have
12987              been issued.  */
12988           else if (!type || type == error_mark_node)
12989             ;
12990           /* An anonymous aggregate has to be handled specially; such
12991              a declaration really declares a data member (with a
12992              particular type), as opposed to a nested class.  */
12993           else if (ANON_AGGR_TYPE_P (type))
12994             {
12995               /* Remove constructors and such from TYPE, now that we
12996                  know it is an anonymous aggregate.  */
12997               fixup_anonymous_aggr (type);
12998               /* And make the corresponding data member.  */
12999               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13000               /* Add it to the class.  */
13001               finish_member_declaration (decl);
13002             }
13003           else
13004             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13005         }
13006     }
13007   else
13008     {
13009       /* See if these declarations will be friends.  */
13010       friend_p = cp_parser_friend_p (&decl_specifiers);
13011
13012       /* Keep going until we hit the `;' at the end of the
13013          declaration.  */
13014       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13015         {
13016           tree attributes = NULL_TREE;
13017           tree first_attribute;
13018
13019           /* Peek at the next token.  */
13020           token = cp_lexer_peek_token (parser->lexer);
13021
13022           /* Check for a bitfield declaration.  */
13023           if (token->type == CPP_COLON
13024               || (token->type == CPP_NAME
13025                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13026                   == CPP_COLON))
13027             {
13028               tree identifier;
13029               tree width;
13030
13031               /* Get the name of the bitfield.  Note that we cannot just
13032                  check TOKEN here because it may have been invalidated by
13033                  the call to cp_lexer_peek_nth_token above.  */
13034               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13035                 identifier = cp_parser_identifier (parser);
13036               else
13037                 identifier = NULL_TREE;
13038
13039               /* Consume the `:' token.  */
13040               cp_lexer_consume_token (parser->lexer);
13041               /* Get the width of the bitfield.  */
13042               width
13043                 = cp_parser_constant_expression (parser,
13044                                                  /*allow_non_constant=*/false,
13045                                                  NULL);
13046
13047               /* Look for attributes that apply to the bitfield.  */
13048               attributes = cp_parser_attributes_opt (parser);
13049               /* Remember which attributes are prefix attributes and
13050                  which are not.  */
13051               first_attribute = attributes;
13052               /* Combine the attributes.  */
13053               attributes = chainon (prefix_attributes, attributes);
13054
13055               /* Create the bitfield declaration.  */
13056               decl = grokbitfield (identifier
13057                                    ? make_id_declarator (identifier)
13058                                    : NULL,
13059                                    &decl_specifiers,
13060                                    width);
13061               /* Apply the attributes.  */
13062               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13063             }
13064           else
13065             {
13066               cp_declarator *declarator;
13067               tree initializer;
13068               tree asm_specification;
13069               int ctor_dtor_or_conv_p;
13070
13071               /* Parse the declarator.  */
13072               declarator
13073                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13074                                         &ctor_dtor_or_conv_p,
13075                                         /*parenthesized_p=*/NULL,
13076                                         /*member_p=*/true);
13077
13078               /* If something went wrong parsing the declarator, make sure
13079                  that we at least consume some tokens.  */
13080               if (declarator == cp_error_declarator)
13081                 {
13082                   /* Skip to the end of the statement.  */
13083                   cp_parser_skip_to_end_of_statement (parser);
13084                   /* If the next token is not a semicolon, that is
13085                      probably because we just skipped over the body of
13086                      a function.  So, we consume a semicolon if
13087                      present, but do not issue an error message if it
13088                      is not present.  */
13089                   if (cp_lexer_next_token_is (parser->lexer,
13090                                               CPP_SEMICOLON))
13091                     cp_lexer_consume_token (parser->lexer);
13092                   return;
13093                 }
13094
13095               if (declares_class_or_enum & 2)
13096                 cp_parser_check_for_definition_in_return_type
13097                   (declarator, decl_specifiers.type);
13098
13099               /* Look for an asm-specification.  */
13100               asm_specification = cp_parser_asm_specification_opt (parser);
13101               /* Look for attributes that apply to the declaration.  */
13102               attributes = cp_parser_attributes_opt (parser);
13103               /* Remember which attributes are prefix attributes and
13104                  which are not.  */
13105               first_attribute = attributes;
13106               /* Combine the attributes.  */
13107               attributes = chainon (prefix_attributes, attributes);
13108
13109               /* If it's an `=', then we have a constant-initializer or a
13110                  pure-specifier.  It is not correct to parse the
13111                  initializer before registering the member declaration
13112                  since the member declaration should be in scope while
13113                  its initializer is processed.  However, the rest of the
13114                  front end does not yet provide an interface that allows
13115                  us to handle this correctly.  */
13116               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13117                 {
13118                   /* In [class.mem]:
13119
13120                      A pure-specifier shall be used only in the declaration of
13121                      a virtual function.
13122
13123                      A member-declarator can contain a constant-initializer
13124                      only if it declares a static member of integral or
13125                      enumeration type.
13126
13127                      Therefore, if the DECLARATOR is for a function, we look
13128                      for a pure-specifier; otherwise, we look for a
13129                      constant-initializer.  When we call `grokfield', it will
13130                      perform more stringent semantics checks.  */
13131                   if (declarator->kind == cdk_function)
13132                     initializer = cp_parser_pure_specifier (parser);
13133                   else
13134                     /* Parse the initializer.  */
13135                     initializer = cp_parser_constant_initializer (parser);
13136                 }
13137               /* Otherwise, there is no initializer.  */
13138               else
13139                 initializer = NULL_TREE;
13140
13141               /* See if we are probably looking at a function
13142                  definition.  We are certainly not looking at a
13143                  member-declarator.  Calling `grokfield' has
13144                  side-effects, so we must not do it unless we are sure
13145                  that we are looking at a member-declarator.  */
13146               if (cp_parser_token_starts_function_definition_p
13147                   (cp_lexer_peek_token (parser->lexer)))
13148                 {
13149                   /* The grammar does not allow a pure-specifier to be
13150                      used when a member function is defined.  (It is
13151                      possible that this fact is an oversight in the
13152                      standard, since a pure function may be defined
13153                      outside of the class-specifier.  */
13154                   if (initializer)
13155                     error ("pure-specifier on function-definition");
13156                   decl = cp_parser_save_member_function_body (parser,
13157                                                               &decl_specifiers,
13158                                                               declarator,
13159                                                               attributes);
13160                   /* If the member was not a friend, declare it here.  */
13161                   if (!friend_p)
13162                     finish_member_declaration (decl);
13163                   /* Peek at the next token.  */
13164                   token = cp_lexer_peek_token (parser->lexer);
13165                   /* If the next token is a semicolon, consume it.  */
13166                   if (token->type == CPP_SEMICOLON)
13167                     cp_lexer_consume_token (parser->lexer);
13168                   return;
13169                 }
13170               else
13171                 {
13172                   /* Create the declaration.  */
13173                   decl = grokfield (declarator, &decl_specifiers,
13174                                     initializer, asm_specification,
13175                                     attributes);
13176                   /* Any initialization must have been from a
13177                      constant-expression.  */
13178                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13179                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13180                 }
13181             }
13182
13183           /* Reset PREFIX_ATTRIBUTES.  */
13184           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13185             attributes = TREE_CHAIN (attributes);
13186           if (attributes)
13187             TREE_CHAIN (attributes) = NULL_TREE;
13188
13189           /* If there is any qualification still in effect, clear it
13190              now; we will be starting fresh with the next declarator.  */
13191           parser->scope = NULL_TREE;
13192           parser->qualifying_scope = NULL_TREE;
13193           parser->object_scope = NULL_TREE;
13194           /* If it's a `,', then there are more declarators.  */
13195           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13196             cp_lexer_consume_token (parser->lexer);
13197           /* If the next token isn't a `;', then we have a parse error.  */
13198           else if (cp_lexer_next_token_is_not (parser->lexer,
13199                                                CPP_SEMICOLON))
13200             {
13201               cp_parser_error (parser, "expected %<;%>");
13202               /* Skip tokens until we find a `;'.  */
13203               cp_parser_skip_to_end_of_statement (parser);
13204
13205               break;
13206             }
13207
13208           if (decl)
13209             {
13210               /* Add DECL to the list of members.  */
13211               if (!friend_p)
13212                 finish_member_declaration (decl);
13213
13214               if (TREE_CODE (decl) == FUNCTION_DECL)
13215                 cp_parser_save_default_args (parser, decl);
13216             }
13217         }
13218     }
13219
13220   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13221 }
13222
13223 /* Parse a pure-specifier.
13224
13225    pure-specifier:
13226      = 0
13227
13228    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13229    Otherwise, ERROR_MARK_NODE is returned.  */
13230
13231 static tree
13232 cp_parser_pure_specifier (cp_parser* parser)
13233 {
13234   cp_token *token;
13235
13236   /* Look for the `=' token.  */
13237   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13238     return error_mark_node;
13239   /* Look for the `0' token.  */
13240   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13241   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13242      to get information from the lexer about how the number was
13243      spelled in order to fix this problem.  */
13244   if (!token || !integer_zerop (token->value))
13245     return error_mark_node;
13246
13247   return integer_zero_node;
13248 }
13249
13250 /* Parse a constant-initializer.
13251
13252    constant-initializer:
13253      = constant-expression
13254
13255    Returns a representation of the constant-expression.  */
13256
13257 static tree
13258 cp_parser_constant_initializer (cp_parser* parser)
13259 {
13260   /* Look for the `=' token.  */
13261   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13262     return error_mark_node;
13263
13264   /* It is invalid to write:
13265
13266        struct S { static const int i = { 7 }; };
13267
13268      */
13269   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13270     {
13271       cp_parser_error (parser,
13272                        "a brace-enclosed initializer is not allowed here");
13273       /* Consume the opening brace.  */
13274       cp_lexer_consume_token (parser->lexer);
13275       /* Skip the initializer.  */
13276       cp_parser_skip_to_closing_brace (parser);
13277       /* Look for the trailing `}'.  */
13278       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13279
13280       return error_mark_node;
13281     }
13282
13283   return cp_parser_constant_expression (parser,
13284                                         /*allow_non_constant=*/false,
13285                                         NULL);
13286 }
13287
13288 /* Derived classes [gram.class.derived] */
13289
13290 /* Parse a base-clause.
13291
13292    base-clause:
13293      : base-specifier-list
13294
13295    base-specifier-list:
13296      base-specifier
13297      base-specifier-list , base-specifier
13298
13299    Returns a TREE_LIST representing the base-classes, in the order in
13300    which they were declared.  The representation of each node is as
13301    described by cp_parser_base_specifier.
13302
13303    In the case that no bases are specified, this function will return
13304    NULL_TREE, not ERROR_MARK_NODE.  */
13305
13306 static tree
13307 cp_parser_base_clause (cp_parser* parser)
13308 {
13309   tree bases = NULL_TREE;
13310
13311   /* Look for the `:' that begins the list.  */
13312   cp_parser_require (parser, CPP_COLON, "`:'");
13313
13314   /* Scan the base-specifier-list.  */
13315   while (true)
13316     {
13317       cp_token *token;
13318       tree base;
13319
13320       /* Look for the base-specifier.  */
13321       base = cp_parser_base_specifier (parser);
13322       /* Add BASE to the front of the list.  */
13323       if (base != error_mark_node)
13324         {
13325           TREE_CHAIN (base) = bases;
13326           bases = base;
13327         }
13328       /* Peek at the next token.  */
13329       token = cp_lexer_peek_token (parser->lexer);
13330       /* If it's not a comma, then the list is complete.  */
13331       if (token->type != CPP_COMMA)
13332         break;
13333       /* Consume the `,'.  */
13334       cp_lexer_consume_token (parser->lexer);
13335     }
13336
13337   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13338      base class had a qualified name.  However, the next name that
13339      appears is certainly not qualified.  */
13340   parser->scope = NULL_TREE;
13341   parser->qualifying_scope = NULL_TREE;
13342   parser->object_scope = NULL_TREE;
13343
13344   return nreverse (bases);
13345 }
13346
13347 /* Parse a base-specifier.
13348
13349    base-specifier:
13350      :: [opt] nested-name-specifier [opt] class-name
13351      virtual access-specifier [opt] :: [opt] nested-name-specifier
13352        [opt] class-name
13353      access-specifier virtual [opt] :: [opt] nested-name-specifier
13354        [opt] class-name
13355
13356    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13357    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13358    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13359    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13360
13361 static tree
13362 cp_parser_base_specifier (cp_parser* parser)
13363 {
13364   cp_token *token;
13365   bool done = false;
13366   bool virtual_p = false;
13367   bool duplicate_virtual_error_issued_p = false;
13368   bool duplicate_access_error_issued_p = false;
13369   bool class_scope_p, template_p;
13370   tree access = access_default_node;
13371   tree type;
13372
13373   /* Process the optional `virtual' and `access-specifier'.  */
13374   while (!done)
13375     {
13376       /* Peek at the next token.  */
13377       token = cp_lexer_peek_token (parser->lexer);
13378       /* Process `virtual'.  */
13379       switch (token->keyword)
13380         {
13381         case RID_VIRTUAL:
13382           /* If `virtual' appears more than once, issue an error.  */
13383           if (virtual_p && !duplicate_virtual_error_issued_p)
13384             {
13385               cp_parser_error (parser,
13386                                "%<virtual%> specified more than once in base-specified");
13387               duplicate_virtual_error_issued_p = true;
13388             }
13389
13390           virtual_p = true;
13391
13392           /* Consume the `virtual' token.  */
13393           cp_lexer_consume_token (parser->lexer);
13394
13395           break;
13396
13397         case RID_PUBLIC:
13398         case RID_PROTECTED:
13399         case RID_PRIVATE:
13400           /* If more than one access specifier appears, issue an
13401              error.  */
13402           if (access != access_default_node
13403               && !duplicate_access_error_issued_p)
13404             {
13405               cp_parser_error (parser,
13406                                "more than one access specifier in base-specified");
13407               duplicate_access_error_issued_p = true;
13408             }
13409
13410           access = ridpointers[(int) token->keyword];
13411
13412           /* Consume the access-specifier.  */
13413           cp_lexer_consume_token (parser->lexer);
13414
13415           break;
13416
13417         default:
13418           done = true;
13419           break;
13420         }
13421     }
13422   /* It is not uncommon to see programs mechanically, erroneously, use
13423      the 'typename' keyword to denote (dependent) qualified types
13424      as base classes.  */
13425   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13426     {
13427       if (!processing_template_decl)
13428         error ("keyword %<typename%> not allowed outside of templates");
13429       else
13430         error ("keyword %<typename%> not allowed in this context "
13431                "(the base class is implicitly a type)");
13432       cp_lexer_consume_token (parser->lexer);
13433     }
13434
13435   /* Look for the optional `::' operator.  */
13436   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13437   /* Look for the nested-name-specifier.  The simplest way to
13438      implement:
13439
13440        [temp.res]
13441
13442        The keyword `typename' is not permitted in a base-specifier or
13443        mem-initializer; in these contexts a qualified name that
13444        depends on a template-parameter is implicitly assumed to be a
13445        type name.
13446
13447      is to pretend that we have seen the `typename' keyword at this
13448      point.  */
13449   cp_parser_nested_name_specifier_opt (parser,
13450                                        /*typename_keyword_p=*/true,
13451                                        /*check_dependency_p=*/true,
13452                                        typename_type,
13453                                        /*is_declaration=*/true);
13454   /* If the base class is given by a qualified name, assume that names
13455      we see are type names or templates, as appropriate.  */
13456   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13457   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13458
13459   /* Finally, look for the class-name.  */
13460   type = cp_parser_class_name (parser,
13461                                class_scope_p,
13462                                template_p,
13463                                typename_type,
13464                                /*check_dependency_p=*/true,
13465                                /*class_head_p=*/false,
13466                                /*is_declaration=*/true);
13467
13468   if (type == error_mark_node)
13469     return error_mark_node;
13470
13471   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13472 }
13473
13474 /* Exception handling [gram.exception] */
13475
13476 /* Parse an (optional) exception-specification.
13477
13478    exception-specification:
13479      throw ( type-id-list [opt] )
13480
13481    Returns a TREE_LIST representing the exception-specification.  The
13482    TREE_VALUE of each node is a type.  */
13483
13484 static tree
13485 cp_parser_exception_specification_opt (cp_parser* parser)
13486 {
13487   cp_token *token;
13488   tree type_id_list;
13489
13490   /* Peek at the next token.  */
13491   token = cp_lexer_peek_token (parser->lexer);
13492   /* If it's not `throw', then there's no exception-specification.  */
13493   if (!cp_parser_is_keyword (token, RID_THROW))
13494     return NULL_TREE;
13495
13496   /* Consume the `throw'.  */
13497   cp_lexer_consume_token (parser->lexer);
13498
13499   /* Look for the `('.  */
13500   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13501
13502   /* Peek at the next token.  */
13503   token = cp_lexer_peek_token (parser->lexer);
13504   /* If it's not a `)', then there is a type-id-list.  */
13505   if (token->type != CPP_CLOSE_PAREN)
13506     {
13507       const char *saved_message;
13508
13509       /* Types may not be defined in an exception-specification.  */
13510       saved_message = parser->type_definition_forbidden_message;
13511       parser->type_definition_forbidden_message
13512         = "types may not be defined in an exception-specification";
13513       /* Parse the type-id-list.  */
13514       type_id_list = cp_parser_type_id_list (parser);
13515       /* Restore the saved message.  */
13516       parser->type_definition_forbidden_message = saved_message;
13517     }
13518   else
13519     type_id_list = empty_except_spec;
13520
13521   /* Look for the `)'.  */
13522   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13523
13524   return type_id_list;
13525 }
13526
13527 /* Parse an (optional) type-id-list.
13528
13529    type-id-list:
13530      type-id
13531      type-id-list , type-id
13532
13533    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13534    in the order that the types were presented.  */
13535
13536 static tree
13537 cp_parser_type_id_list (cp_parser* parser)
13538 {
13539   tree types = NULL_TREE;
13540
13541   while (true)
13542     {
13543       cp_token *token;
13544       tree type;
13545
13546       /* Get the next type-id.  */
13547       type = cp_parser_type_id (parser);
13548       /* Add it to the list.  */
13549       types = add_exception_specifier (types, type, /*complain=*/1);
13550       /* Peek at the next token.  */
13551       token = cp_lexer_peek_token (parser->lexer);
13552       /* If it is not a `,', we are done.  */
13553       if (token->type != CPP_COMMA)
13554         break;
13555       /* Consume the `,'.  */
13556       cp_lexer_consume_token (parser->lexer);
13557     }
13558
13559   return nreverse (types);
13560 }
13561
13562 /* Parse a try-block.
13563
13564    try-block:
13565      try compound-statement handler-seq  */
13566
13567 static tree
13568 cp_parser_try_block (cp_parser* parser)
13569 {
13570   tree try_block;
13571
13572   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13573   try_block = begin_try_block ();
13574   cp_parser_compound_statement (parser, NULL, true);
13575   finish_try_block (try_block);
13576   cp_parser_handler_seq (parser);
13577   finish_handler_sequence (try_block);
13578
13579   return try_block;
13580 }
13581
13582 /* Parse a function-try-block.
13583
13584    function-try-block:
13585      try ctor-initializer [opt] function-body handler-seq  */
13586
13587 static bool
13588 cp_parser_function_try_block (cp_parser* parser)
13589 {
13590   tree try_block;
13591   bool ctor_initializer_p;
13592
13593   /* Look for the `try' keyword.  */
13594   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13595     return false;
13596   /* Let the rest of the front-end know where we are.  */
13597   try_block = begin_function_try_block ();
13598   /* Parse the function-body.  */
13599   ctor_initializer_p
13600     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13601   /* We're done with the `try' part.  */
13602   finish_function_try_block (try_block);
13603   /* Parse the handlers.  */
13604   cp_parser_handler_seq (parser);
13605   /* We're done with the handlers.  */
13606   finish_function_handler_sequence (try_block);
13607
13608   return ctor_initializer_p;
13609 }
13610
13611 /* Parse a handler-seq.
13612
13613    handler-seq:
13614      handler handler-seq [opt]  */
13615
13616 static void
13617 cp_parser_handler_seq (cp_parser* parser)
13618 {
13619   while (true)
13620     {
13621       cp_token *token;
13622
13623       /* Parse the handler.  */
13624       cp_parser_handler (parser);
13625       /* Peek at the next token.  */
13626       token = cp_lexer_peek_token (parser->lexer);
13627       /* If it's not `catch' then there are no more handlers.  */
13628       if (!cp_parser_is_keyword (token, RID_CATCH))
13629         break;
13630     }
13631 }
13632
13633 /* Parse a handler.
13634
13635    handler:
13636      catch ( exception-declaration ) compound-statement  */
13637
13638 static void
13639 cp_parser_handler (cp_parser* parser)
13640 {
13641   tree handler;
13642   tree declaration;
13643
13644   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13645   handler = begin_handler ();
13646   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13647   declaration = cp_parser_exception_declaration (parser);
13648   finish_handler_parms (declaration, handler);
13649   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13650   cp_parser_compound_statement (parser, NULL, false);
13651   finish_handler (handler);
13652 }
13653
13654 /* Parse an exception-declaration.
13655
13656    exception-declaration:
13657      type-specifier-seq declarator
13658      type-specifier-seq abstract-declarator
13659      type-specifier-seq
13660      ...
13661
13662    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13663    ellipsis variant is used.  */
13664
13665 static tree
13666 cp_parser_exception_declaration (cp_parser* parser)
13667 {
13668   tree decl;
13669   cp_decl_specifier_seq type_specifiers;
13670   cp_declarator *declarator;
13671   const char *saved_message;
13672
13673   /* If it's an ellipsis, it's easy to handle.  */
13674   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13675     {
13676       /* Consume the `...' token.  */
13677       cp_lexer_consume_token (parser->lexer);
13678       return NULL_TREE;
13679     }
13680
13681   /* Types may not be defined in exception-declarations.  */
13682   saved_message = parser->type_definition_forbidden_message;
13683   parser->type_definition_forbidden_message
13684     = "types may not be defined in exception-declarations";
13685
13686   /* Parse the type-specifier-seq.  */
13687   cp_parser_type_specifier_seq (parser, &type_specifiers);
13688   /* If it's a `)', then there is no declarator.  */
13689   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13690     declarator = NULL;
13691   else
13692     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13693                                        /*ctor_dtor_or_conv_p=*/NULL,
13694                                        /*parenthesized_p=*/NULL,
13695                                        /*member_p=*/false);
13696
13697   /* Restore the saved message.  */
13698   parser->type_definition_forbidden_message = saved_message;
13699
13700   if (type_specifiers.any_specifiers_p)
13701     {
13702       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13703       if (decl == NULL_TREE)
13704         error ("invalid catch parameter");
13705     }
13706   else
13707     decl = NULL_TREE;
13708
13709   return decl;
13710 }
13711
13712 /* Parse a throw-expression.
13713
13714    throw-expression:
13715      throw assignment-expression [opt]
13716
13717    Returns a THROW_EXPR representing the throw-expression.  */
13718
13719 static tree
13720 cp_parser_throw_expression (cp_parser* parser)
13721 {
13722   tree expression;
13723   cp_token* token;
13724
13725   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13726   token = cp_lexer_peek_token (parser->lexer);
13727   /* Figure out whether or not there is an assignment-expression
13728      following the "throw" keyword.  */
13729   if (token->type == CPP_COMMA
13730       || token->type == CPP_SEMICOLON
13731       || token->type == CPP_CLOSE_PAREN
13732       || token->type == CPP_CLOSE_SQUARE
13733       || token->type == CPP_CLOSE_BRACE
13734       || token->type == CPP_COLON)
13735     expression = NULL_TREE;
13736   else
13737     expression = cp_parser_assignment_expression (parser);
13738
13739   return build_throw (expression);
13740 }
13741
13742 /* GNU Extensions */
13743
13744 /* Parse an (optional) asm-specification.
13745
13746    asm-specification:
13747      asm ( string-literal )
13748
13749    If the asm-specification is present, returns a STRING_CST
13750    corresponding to the string-literal.  Otherwise, returns
13751    NULL_TREE.  */
13752
13753 static tree
13754 cp_parser_asm_specification_opt (cp_parser* parser)
13755 {
13756   cp_token *token;
13757   tree asm_specification;
13758
13759   /* Peek at the next token.  */
13760   token = cp_lexer_peek_token (parser->lexer);
13761   /* If the next token isn't the `asm' keyword, then there's no
13762      asm-specification.  */
13763   if (!cp_parser_is_keyword (token, RID_ASM))
13764     return NULL_TREE;
13765
13766   /* Consume the `asm' token.  */
13767   cp_lexer_consume_token (parser->lexer);
13768   /* Look for the `('.  */
13769   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13770
13771   /* Look for the string-literal.  */
13772   asm_specification = cp_parser_string_literal (parser, false, false);
13773
13774   /* Look for the `)'.  */
13775   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13776
13777   return asm_specification;
13778 }
13779
13780 /* Parse an asm-operand-list.
13781
13782    asm-operand-list:
13783      asm-operand
13784      asm-operand-list , asm-operand
13785
13786    asm-operand:
13787      string-literal ( expression )
13788      [ string-literal ] string-literal ( expression )
13789
13790    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13791    each node is the expression.  The TREE_PURPOSE is itself a
13792    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13793    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13794    is a STRING_CST for the string literal before the parenthesis.  */
13795
13796 static tree
13797 cp_parser_asm_operand_list (cp_parser* parser)
13798 {
13799   tree asm_operands = NULL_TREE;
13800
13801   while (true)
13802     {
13803       tree string_literal;
13804       tree expression;
13805       tree name;
13806
13807       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13808         {
13809           /* Consume the `[' token.  */
13810           cp_lexer_consume_token (parser->lexer);
13811           /* Read the operand name.  */
13812           name = cp_parser_identifier (parser);
13813           if (name != error_mark_node)
13814             name = build_string (IDENTIFIER_LENGTH (name),
13815                                  IDENTIFIER_POINTER (name));
13816           /* Look for the closing `]'.  */
13817           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13818         }
13819       else
13820         name = NULL_TREE;
13821       /* Look for the string-literal.  */
13822       string_literal = cp_parser_string_literal (parser, false, false);
13823
13824       /* Look for the `('.  */
13825       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13826       /* Parse the expression.  */
13827       expression = cp_parser_expression (parser);
13828       /* Look for the `)'.  */
13829       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13830
13831       /* Add this operand to the list.  */
13832       asm_operands = tree_cons (build_tree_list (name, string_literal),
13833                                 expression,
13834                                 asm_operands);
13835       /* If the next token is not a `,', there are no more
13836          operands.  */
13837       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13838         break;
13839       /* Consume the `,'.  */
13840       cp_lexer_consume_token (parser->lexer);
13841     }
13842
13843   return nreverse (asm_operands);
13844 }
13845
13846 /* Parse an asm-clobber-list.
13847
13848    asm-clobber-list:
13849      string-literal
13850      asm-clobber-list , string-literal
13851
13852    Returns a TREE_LIST, indicating the clobbers in the order that they
13853    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13854
13855 static tree
13856 cp_parser_asm_clobber_list (cp_parser* parser)
13857 {
13858   tree clobbers = NULL_TREE;
13859
13860   while (true)
13861     {
13862       tree string_literal;
13863
13864       /* Look for the string literal.  */
13865       string_literal = cp_parser_string_literal (parser, false, false);
13866       /* Add it to the list.  */
13867       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13868       /* If the next token is not a `,', then the list is
13869          complete.  */
13870       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13871         break;
13872       /* Consume the `,' token.  */
13873       cp_lexer_consume_token (parser->lexer);
13874     }
13875
13876   return clobbers;
13877 }
13878
13879 /* Parse an (optional) series of attributes.
13880
13881    attributes:
13882      attributes attribute
13883
13884    attribute:
13885      __attribute__ (( attribute-list [opt] ))
13886
13887    The return value is as for cp_parser_attribute_list.  */
13888
13889 static tree
13890 cp_parser_attributes_opt (cp_parser* parser)
13891 {
13892   tree attributes = NULL_TREE;
13893
13894   while (true)
13895     {
13896       cp_token *token;
13897       tree attribute_list;
13898
13899       /* Peek at the next token.  */
13900       token = cp_lexer_peek_token (parser->lexer);
13901       /* If it's not `__attribute__', then we're done.  */
13902       if (token->keyword != RID_ATTRIBUTE)
13903         break;
13904
13905       /* Consume the `__attribute__' keyword.  */
13906       cp_lexer_consume_token (parser->lexer);
13907       /* Look for the two `(' tokens.  */
13908       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13909       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13910
13911       /* Peek at the next token.  */
13912       token = cp_lexer_peek_token (parser->lexer);
13913       if (token->type != CPP_CLOSE_PAREN)
13914         /* Parse the attribute-list.  */
13915         attribute_list = cp_parser_attribute_list (parser);
13916       else
13917         /* If the next token is a `)', then there is no attribute
13918            list.  */
13919         attribute_list = NULL;
13920
13921       /* Look for the two `)' tokens.  */
13922       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13923       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13924
13925       /* Add these new attributes to the list.  */
13926       attributes = chainon (attributes, attribute_list);
13927     }
13928
13929   return attributes;
13930 }
13931
13932 /* Parse an attribute-list.
13933
13934    attribute-list:
13935      attribute
13936      attribute-list , attribute
13937
13938    attribute:
13939      identifier
13940      identifier ( identifier )
13941      identifier ( identifier , expression-list )
13942      identifier ( expression-list )
13943
13944    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13945    TREE_PURPOSE of each node is the identifier indicating which
13946    attribute is in use.  The TREE_VALUE represents the arguments, if
13947    any.  */
13948
13949 static tree
13950 cp_parser_attribute_list (cp_parser* parser)
13951 {
13952   tree attribute_list = NULL_TREE;
13953   bool save_translate_strings_p = parser->translate_strings_p;
13954
13955   parser->translate_strings_p = false;
13956   while (true)
13957     {
13958       cp_token *token;
13959       tree identifier;
13960       tree attribute;
13961
13962       /* Look for the identifier.  We also allow keywords here; for
13963          example `__attribute__ ((const))' is legal.  */
13964       token = cp_lexer_peek_token (parser->lexer);
13965       if (token->type != CPP_NAME
13966           && token->type != CPP_KEYWORD)
13967         return error_mark_node;
13968       /* Consume the token.  */
13969       token = cp_lexer_consume_token (parser->lexer);
13970
13971       /* Save away the identifier that indicates which attribute this is.  */
13972       identifier = token->value;
13973       attribute = build_tree_list (identifier, NULL_TREE);
13974
13975       /* Peek at the next token.  */
13976       token = cp_lexer_peek_token (parser->lexer);
13977       /* If it's an `(', then parse the attribute arguments.  */
13978       if (token->type == CPP_OPEN_PAREN)
13979         {
13980           tree arguments;
13981
13982           arguments = (cp_parser_parenthesized_expression_list
13983                        (parser, true, /*non_constant_p=*/NULL));
13984           /* Save the identifier and arguments away.  */
13985           TREE_VALUE (attribute) = arguments;
13986         }
13987
13988       /* Add this attribute to the list.  */
13989       TREE_CHAIN (attribute) = attribute_list;
13990       attribute_list = attribute;
13991
13992       /* Now, look for more attributes.  */
13993       token = cp_lexer_peek_token (parser->lexer);
13994       /* If the next token isn't a `,', we're done.  */
13995       if (token->type != CPP_COMMA)
13996         break;
13997
13998       /* Consume the comma and keep going.  */
13999       cp_lexer_consume_token (parser->lexer);
14000     }
14001   parser->translate_strings_p = save_translate_strings_p;
14002
14003   /* We built up the list in reverse order.  */
14004   return nreverse (attribute_list);
14005 }
14006
14007 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14008    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14009    current value of the PEDANTIC flag, regardless of whether or not
14010    the `__extension__' keyword is present.  The caller is responsible
14011    for restoring the value of the PEDANTIC flag.  */
14012
14013 static bool
14014 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14015 {
14016   /* Save the old value of the PEDANTIC flag.  */
14017   *saved_pedantic = pedantic;
14018
14019   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14020     {
14021       /* Consume the `__extension__' token.  */
14022       cp_lexer_consume_token (parser->lexer);
14023       /* We're not being pedantic while the `__extension__' keyword is
14024          in effect.  */
14025       pedantic = 0;
14026
14027       return true;
14028     }
14029
14030   return false;
14031 }
14032
14033 /* Parse a label declaration.
14034
14035    label-declaration:
14036      __label__ label-declarator-seq ;
14037
14038    label-declarator-seq:
14039      identifier , label-declarator-seq
14040      identifier  */
14041
14042 static void
14043 cp_parser_label_declaration (cp_parser* parser)
14044 {
14045   /* Look for the `__label__' keyword.  */
14046   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14047
14048   while (true)
14049     {
14050       tree identifier;
14051
14052       /* Look for an identifier.  */
14053       identifier = cp_parser_identifier (parser);
14054       /* Declare it as a lobel.  */
14055       finish_label_decl (identifier);
14056       /* If the next token is a `;', stop.  */
14057       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14058         break;
14059       /* Look for the `,' separating the label declarations.  */
14060       cp_parser_require (parser, CPP_COMMA, "`,'");
14061     }
14062
14063   /* Look for the final `;'.  */
14064   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14065 }
14066
14067 /* Support Functions */
14068
14069 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14070    NAME should have one of the representations used for an
14071    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14072    is returned.  If PARSER->SCOPE is a dependent type, then a
14073    SCOPE_REF is returned.
14074
14075    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14076    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14077    was formed.  Abstractly, such entities should not be passed to this
14078    function, because they do not need to be looked up, but it is
14079    simpler to check for this special case here, rather than at the
14080    call-sites.
14081
14082    In cases not explicitly covered above, this function returns a
14083    DECL, OVERLOAD, or baselink representing the result of the lookup.
14084    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14085    is returned.
14086
14087    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14088    (e.g., "struct") that was used.  In that case bindings that do not
14089    refer to types are ignored.
14090
14091    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14092    ignored.
14093
14094    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14095    are ignored.
14096
14097    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14098    types.  
14099
14100    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14101    results in an ambiguity, and false otherwise.  */
14102
14103 static tree
14104 cp_parser_lookup_name (cp_parser *parser, tree name,
14105                        enum tag_types tag_type,
14106                        bool is_template, bool is_namespace,
14107                        bool check_dependency,
14108                        bool *ambiguous_p)
14109 {
14110   tree decl;
14111   tree object_type = parser->context->object_type;
14112
14113   /* Assume that the lookup will be unambiguous.  */
14114   if (ambiguous_p)
14115     *ambiguous_p = false;
14116
14117   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14118      no longer valid.  Note that if we are parsing tentatively, and
14119      the parse fails, OBJECT_TYPE will be automatically restored.  */
14120   parser->context->object_type = NULL_TREE;
14121
14122   if (name == error_mark_node)
14123     return error_mark_node;
14124
14125   /* A template-id has already been resolved; there is no lookup to
14126      do.  */
14127   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14128     return name;
14129   if (BASELINK_P (name))
14130     {
14131       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14132                   == TEMPLATE_ID_EXPR);
14133       return name;
14134     }
14135
14136   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14137      it should already have been checked to make sure that the name
14138      used matches the type being destroyed.  */
14139   if (TREE_CODE (name) == BIT_NOT_EXPR)
14140     {
14141       tree type;
14142
14143       /* Figure out to which type this destructor applies.  */
14144       if (parser->scope)
14145         type = parser->scope;
14146       else if (object_type)
14147         type = object_type;
14148       else
14149         type = current_class_type;
14150       /* If that's not a class type, there is no destructor.  */
14151       if (!type || !CLASS_TYPE_P (type))
14152         return error_mark_node;
14153       if (!CLASSTYPE_DESTRUCTORS (type))
14154           return error_mark_node;
14155       /* If it was a class type, return the destructor.  */
14156       return CLASSTYPE_DESTRUCTORS (type);
14157     }
14158
14159   /* By this point, the NAME should be an ordinary identifier.  If
14160      the id-expression was a qualified name, the qualifying scope is
14161      stored in PARSER->SCOPE at this point.  */
14162   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14163
14164   /* Perform the lookup.  */
14165   if (parser->scope)
14166     {
14167       bool dependent_p;
14168
14169       if (parser->scope == error_mark_node)
14170         return error_mark_node;
14171
14172       /* If the SCOPE is dependent, the lookup must be deferred until
14173          the template is instantiated -- unless we are explicitly
14174          looking up names in uninstantiated templates.  Even then, we
14175          cannot look up the name if the scope is not a class type; it
14176          might, for example, be a template type parameter.  */
14177       dependent_p = (TYPE_P (parser->scope)
14178                      && !(parser->in_declarator_p
14179                           && currently_open_class (parser->scope))
14180                      && dependent_type_p (parser->scope));
14181       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14182            && dependent_p)
14183         {
14184           if (tag_type)
14185             {
14186               tree type;
14187
14188               /* The resolution to Core Issue 180 says that `struct
14189                  A::B' should be considered a type-name, even if `A'
14190                  is dependent.  */
14191               type = make_typename_type (parser->scope, name, tag_type,
14192                                          /*complain=*/1);
14193               if (tag_type == enum_type)
14194                 TYPENAME_IS_ENUM_P (type) = 1;
14195               else if (tag_type != typename_type)
14196                 TYPENAME_IS_CLASS_P (type) = 1;
14197               decl = TYPE_NAME (type);
14198             }
14199           else if (is_template)
14200             decl = make_unbound_class_template (parser->scope,
14201                                                 name, NULL_TREE,
14202                                                 /*complain=*/1);
14203           else
14204             decl = build_nt (SCOPE_REF, parser->scope, name);
14205         }
14206       else
14207         {
14208           bool pop_p = false;
14209
14210           /* If PARSER->SCOPE is a dependent type, then it must be a
14211              class type, and we must not be checking dependencies;
14212              otherwise, we would have processed this lookup above.  So
14213              that PARSER->SCOPE is not considered a dependent base by
14214              lookup_member, we must enter the scope here.  */
14215           if (dependent_p)
14216             pop_p = push_scope (parser->scope);
14217           /* If the PARSER->SCOPE is a a template specialization, it
14218              may be instantiated during name lookup.  In that case,
14219              errors may be issued.  Even if we rollback the current
14220              tentative parse, those errors are valid.  */
14221           decl = lookup_qualified_name (parser->scope, name, 
14222                                         tag_type != none_type, 
14223                                         /*complain=*/true);
14224           if (pop_p)
14225             pop_scope (parser->scope);
14226         }
14227       parser->qualifying_scope = parser->scope;
14228       parser->object_scope = NULL_TREE;
14229     }
14230   else if (object_type)
14231     {
14232       tree object_decl = NULL_TREE;
14233       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14234          OBJECT_TYPE is not a class.  */
14235       if (CLASS_TYPE_P (object_type))
14236         /* If the OBJECT_TYPE is a template specialization, it may
14237            be instantiated during name lookup.  In that case, errors
14238            may be issued.  Even if we rollback the current tentative
14239            parse, those errors are valid.  */
14240         object_decl = lookup_member (object_type,
14241                                      name,
14242                                      /*protect=*/0, 
14243                                      tag_type != none_type);
14244       /* Look it up in the enclosing context, too.  */
14245       decl = lookup_name_real (name, tag_type != none_type, 
14246                                /*nonclass=*/0,
14247                                /*block_p=*/true, is_namespace,
14248                                /*flags=*/0);
14249       parser->object_scope = object_type;
14250       parser->qualifying_scope = NULL_TREE;
14251       if (object_decl)
14252         decl = object_decl;
14253     }
14254   else
14255     {
14256       decl = lookup_name_real (name, tag_type != none_type, 
14257                                /*nonclass=*/0,
14258                                /*block_p=*/true, is_namespace,
14259                                /*flags=*/0);
14260       parser->qualifying_scope = NULL_TREE;
14261       parser->object_scope = NULL_TREE;
14262     }
14263
14264   /* If the lookup failed, let our caller know.  */
14265   if (!decl
14266       || decl == error_mark_node
14267       || (TREE_CODE (decl) == FUNCTION_DECL
14268           && DECL_ANTICIPATED (decl)))
14269     return error_mark_node;
14270
14271   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14272   if (TREE_CODE (decl) == TREE_LIST)
14273     {
14274       if (ambiguous_p)
14275         *ambiguous_p = true;
14276       /* The error message we have to print is too complicated for
14277          cp_parser_error, so we incorporate its actions directly.  */
14278       if (!cp_parser_simulate_error (parser))
14279         {
14280           error ("reference to %qD is ambiguous", name);
14281           print_candidates (decl);
14282         }
14283       return error_mark_node;
14284     }
14285
14286   gcc_assert (DECL_P (decl)
14287               || TREE_CODE (decl) == OVERLOAD
14288               || TREE_CODE (decl) == SCOPE_REF
14289               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14290               || BASELINK_P (decl));
14291
14292   /* If we have resolved the name of a member declaration, check to
14293      see if the declaration is accessible.  When the name resolves to
14294      set of overloaded functions, accessibility is checked when
14295      overload resolution is done.
14296
14297      During an explicit instantiation, access is not checked at all,
14298      as per [temp.explicit].  */
14299   if (DECL_P (decl))
14300     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14301
14302   return decl;
14303 }
14304
14305 /* Like cp_parser_lookup_name, but for use in the typical case where
14306    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14307    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14308
14309 static tree
14310 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14311 {
14312   return cp_parser_lookup_name (parser, name,
14313                                 none_type,
14314                                 /*is_template=*/false,
14315                                 /*is_namespace=*/false,
14316                                 /*check_dependency=*/true,
14317                                 /*ambiguous_p=*/NULL);
14318 }
14319
14320 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14321    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14322    true, the DECL indicates the class being defined in a class-head,
14323    or declared in an elaborated-type-specifier.
14324
14325    Otherwise, return DECL.  */
14326
14327 static tree
14328 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14329 {
14330   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14331      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14332
14333        struct A {
14334          template <typename T> struct B;
14335        };
14336
14337        template <typename T> struct A::B {};
14338
14339      Similarly, in a elaborated-type-specifier:
14340
14341        namespace N { struct X{}; }
14342
14343        struct A {
14344          template <typename T> friend struct N::X;
14345        };
14346
14347      However, if the DECL refers to a class type, and we are in
14348      the scope of the class, then the name lookup automatically
14349      finds the TYPE_DECL created by build_self_reference rather
14350      than a TEMPLATE_DECL.  For example, in:
14351
14352        template <class T> struct S {
14353          S s;
14354        };
14355
14356      there is no need to handle such case.  */
14357
14358   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14359     return DECL_TEMPLATE_RESULT (decl);
14360
14361   return decl;
14362 }
14363
14364 /* If too many, or too few, template-parameter lists apply to the
14365    declarator, issue an error message.  Returns TRUE if all went well,
14366    and FALSE otherwise.  */
14367
14368 static bool
14369 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14370                                                 cp_declarator *declarator)
14371 {
14372   unsigned num_templates;
14373
14374   /* We haven't seen any classes that involve template parameters yet.  */
14375   num_templates = 0;
14376
14377   switch (declarator->kind)
14378     {
14379     case cdk_id:
14380       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14381         {
14382           tree scope;
14383           tree member;
14384
14385           scope = TREE_OPERAND (declarator->u.id.name, 0);
14386           member = TREE_OPERAND (declarator->u.id.name, 1);
14387
14388           while (scope && CLASS_TYPE_P (scope))
14389             {
14390               /* You're supposed to have one `template <...>'
14391                  for every template class, but you don't need one
14392                  for a full specialization.  For example:
14393
14394                  template <class T> struct S{};
14395                  template <> struct S<int> { void f(); };
14396                  void S<int>::f () {}
14397
14398                  is correct; there shouldn't be a `template <>' for
14399                  the definition of `S<int>::f'.  */
14400               if (CLASSTYPE_TEMPLATE_INFO (scope)
14401                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14402                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14403                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14404                 ++num_templates;
14405
14406               scope = TYPE_CONTEXT (scope);
14407             }
14408         }
14409
14410       /* If the DECLARATOR has the form `X<y>' then it uses one
14411          additional level of template parameters.  */
14412       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14413         ++num_templates;
14414
14415       return cp_parser_check_template_parameters (parser,
14416                                                   num_templates);
14417
14418     case cdk_function:
14419     case cdk_array:
14420     case cdk_pointer:
14421     case cdk_reference:
14422     case cdk_ptrmem:
14423       return (cp_parser_check_declarator_template_parameters
14424               (parser, declarator->declarator));
14425
14426     case cdk_error:
14427       return true;
14428
14429     default:
14430       gcc_unreachable ();
14431     }
14432   return false;
14433 }
14434
14435 /* NUM_TEMPLATES were used in the current declaration.  If that is
14436    invalid, return FALSE and issue an error messages.  Otherwise,
14437    return TRUE.  */
14438
14439 static bool
14440 cp_parser_check_template_parameters (cp_parser* parser,
14441                                      unsigned num_templates)
14442 {
14443   /* If there are more template classes than parameter lists, we have
14444      something like:
14445
14446        template <class T> void S<T>::R<T>::f ();  */
14447   if (parser->num_template_parameter_lists < num_templates)
14448     {
14449       error ("too few template-parameter-lists");
14450       return false;
14451     }
14452   /* If there are the same number of template classes and parameter
14453      lists, that's OK.  */
14454   if (parser->num_template_parameter_lists == num_templates)
14455     return true;
14456   /* If there are more, but only one more, then we are referring to a
14457      member template.  That's OK too.  */
14458   if (parser->num_template_parameter_lists == num_templates + 1)
14459       return true;
14460   /* Otherwise, there are too many template parameter lists.  We have
14461      something like:
14462
14463      template <class T> template <class U> void S::f();  */
14464   error ("too many template-parameter-lists");
14465   return false;
14466 }
14467
14468 /* Parse an optional `::' token indicating that the following name is
14469    from the global namespace.  If so, PARSER->SCOPE is set to the
14470    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14471    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14472    Returns the new value of PARSER->SCOPE, if the `::' token is
14473    present, and NULL_TREE otherwise.  */
14474
14475 static tree
14476 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14477 {
14478   cp_token *token;
14479
14480   /* Peek at the next token.  */
14481   token = cp_lexer_peek_token (parser->lexer);
14482   /* If we're looking at a `::' token then we're starting from the
14483      global namespace, not our current location.  */
14484   if (token->type == CPP_SCOPE)
14485     {
14486       /* Consume the `::' token.  */
14487       cp_lexer_consume_token (parser->lexer);
14488       /* Set the SCOPE so that we know where to start the lookup.  */
14489       parser->scope = global_namespace;
14490       parser->qualifying_scope = global_namespace;
14491       parser->object_scope = NULL_TREE;
14492
14493       return parser->scope;
14494     }
14495   else if (!current_scope_valid_p)
14496     {
14497       parser->scope = NULL_TREE;
14498       parser->qualifying_scope = NULL_TREE;
14499       parser->object_scope = NULL_TREE;
14500     }
14501
14502   return NULL_TREE;
14503 }
14504
14505 /* Returns TRUE if the upcoming token sequence is the start of a
14506    constructor declarator.  If FRIEND_P is true, the declarator is
14507    preceded by the `friend' specifier.  */
14508
14509 static bool
14510 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14511 {
14512   bool constructor_p;
14513   tree type_decl = NULL_TREE;
14514   bool nested_name_p;
14515   cp_token *next_token;
14516
14517   /* The common case is that this is not a constructor declarator, so
14518      try to avoid doing lots of work if at all possible.  It's not
14519      valid declare a constructor at function scope.  */
14520   if (at_function_scope_p ())
14521     return false;
14522   /* And only certain tokens can begin a constructor declarator.  */
14523   next_token = cp_lexer_peek_token (parser->lexer);
14524   if (next_token->type != CPP_NAME
14525       && next_token->type != CPP_SCOPE
14526       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14527       && next_token->type != CPP_TEMPLATE_ID)
14528     return false;
14529
14530   /* Parse tentatively; we are going to roll back all of the tokens
14531      consumed here.  */
14532   cp_parser_parse_tentatively (parser);
14533   /* Assume that we are looking at a constructor declarator.  */
14534   constructor_p = true;
14535
14536   /* Look for the optional `::' operator.  */
14537   cp_parser_global_scope_opt (parser,
14538                               /*current_scope_valid_p=*/false);
14539   /* Look for the nested-name-specifier.  */
14540   nested_name_p
14541     = (cp_parser_nested_name_specifier_opt (parser,
14542                                             /*typename_keyword_p=*/false,
14543                                             /*check_dependency_p=*/false,
14544                                             /*type_p=*/false,
14545                                             /*is_declaration=*/false)
14546        != NULL_TREE);
14547   /* Outside of a class-specifier, there must be a
14548      nested-name-specifier.  */
14549   if (!nested_name_p &&
14550       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14551        || friend_p))
14552     constructor_p = false;
14553   /* If we still think that this might be a constructor-declarator,
14554      look for a class-name.  */
14555   if (constructor_p)
14556     {
14557       /* If we have:
14558
14559            template <typename T> struct S { S(); };
14560            template <typename T> S<T>::S ();
14561
14562          we must recognize that the nested `S' names a class.
14563          Similarly, for:
14564
14565            template <typename T> S<T>::S<T> ();
14566
14567          we must recognize that the nested `S' names a template.  */
14568       type_decl = cp_parser_class_name (parser,
14569                                         /*typename_keyword_p=*/false,
14570                                         /*template_keyword_p=*/false,
14571                                         none_type,
14572                                         /*check_dependency_p=*/false,
14573                                         /*class_head_p=*/false,
14574                                         /*is_declaration=*/false);
14575       /* If there was no class-name, then this is not a constructor.  */
14576       constructor_p = !cp_parser_error_occurred (parser);
14577     }
14578
14579   /* If we're still considering a constructor, we have to see a `(',
14580      to begin the parameter-declaration-clause, followed by either a
14581      `)', an `...', or a decl-specifier.  We need to check for a
14582      type-specifier to avoid being fooled into thinking that:
14583
14584        S::S (f) (int);
14585
14586      is a constructor.  (It is actually a function named `f' that
14587      takes one parameter (of type `int') and returns a value of type
14588      `S::S'.  */
14589   if (constructor_p
14590       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14591     {
14592       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14593           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14594           /* A parameter declaration begins with a decl-specifier,
14595              which is either the "attribute" keyword, a storage class
14596              specifier, or (usually) a type-specifier.  */
14597           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14598           && !cp_parser_storage_class_specifier_opt (parser))
14599         {
14600           tree type;
14601           bool pop_p = false;
14602           unsigned saved_num_template_parameter_lists;
14603
14604           /* Names appearing in the type-specifier should be looked up
14605              in the scope of the class.  */
14606           if (current_class_type)
14607             type = NULL_TREE;
14608           else
14609             {
14610               type = TREE_TYPE (type_decl);
14611               if (TREE_CODE (type) == TYPENAME_TYPE)
14612                 {
14613                   type = resolve_typename_type (type,
14614                                                 /*only_current_p=*/false);
14615                   if (type == error_mark_node)
14616                     {
14617                       cp_parser_abort_tentative_parse (parser);
14618                       return false;
14619                     }
14620                 }
14621               pop_p = push_scope (type);
14622             }
14623
14624           /* Inside the constructor parameter list, surrounding
14625              template-parameter-lists do not apply.  */
14626           saved_num_template_parameter_lists
14627             = parser->num_template_parameter_lists;
14628           parser->num_template_parameter_lists = 0;
14629
14630           /* Look for the type-specifier.  */
14631           cp_parser_type_specifier (parser,
14632                                     CP_PARSER_FLAGS_NONE,
14633                                     /*decl_specs=*/NULL,
14634                                     /*is_declarator=*/true,
14635                                     /*declares_class_or_enum=*/NULL,
14636                                     /*is_cv_qualifier=*/NULL);
14637
14638           parser->num_template_parameter_lists
14639             = saved_num_template_parameter_lists;
14640
14641           /* Leave the scope of the class.  */
14642           if (pop_p)
14643             pop_scope (type);
14644
14645           constructor_p = !cp_parser_error_occurred (parser);
14646         }
14647     }
14648   else
14649     constructor_p = false;
14650   /* We did not really want to consume any tokens.  */
14651   cp_parser_abort_tentative_parse (parser);
14652
14653   return constructor_p;
14654 }
14655
14656 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14657    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14658    they must be performed once we are in the scope of the function.
14659
14660    Returns the function defined.  */
14661
14662 static tree
14663 cp_parser_function_definition_from_specifiers_and_declarator
14664   (cp_parser* parser,
14665    cp_decl_specifier_seq *decl_specifiers,
14666    tree attributes,
14667    const cp_declarator *declarator)
14668 {
14669   tree fn;
14670   bool success_p;
14671
14672   /* Begin the function-definition.  */
14673   success_p = start_function (decl_specifiers, declarator, attributes);
14674
14675   /* The things we're about to see are not directly qualified by any
14676      template headers we've seen thus far.  */
14677   reset_specialization ();
14678
14679   /* If there were names looked up in the decl-specifier-seq that we
14680      did not check, check them now.  We must wait until we are in the
14681      scope of the function to perform the checks, since the function
14682      might be a friend.  */
14683   perform_deferred_access_checks ();
14684
14685   if (!success_p)
14686     {
14687       /* Skip the entire function.  */
14688       error ("invalid function declaration");
14689       cp_parser_skip_to_end_of_block_or_statement (parser);
14690       fn = error_mark_node;
14691     }
14692   else
14693     fn = cp_parser_function_definition_after_declarator (parser,
14694                                                          /*inline_p=*/false);
14695
14696   return fn;
14697 }
14698
14699 /* Parse the part of a function-definition that follows the
14700    declarator.  INLINE_P is TRUE iff this function is an inline
14701    function defined with a class-specifier.
14702
14703    Returns the function defined.  */
14704
14705 static tree
14706 cp_parser_function_definition_after_declarator (cp_parser* parser,
14707                                                 bool inline_p)
14708 {
14709   tree fn;
14710   bool ctor_initializer_p = false;
14711   bool saved_in_unbraced_linkage_specification_p;
14712   unsigned saved_num_template_parameter_lists;
14713
14714   /* If the next token is `return', then the code may be trying to
14715      make use of the "named return value" extension that G++ used to
14716      support.  */
14717   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14718     {
14719       /* Consume the `return' keyword.  */
14720       cp_lexer_consume_token (parser->lexer);
14721       /* Look for the identifier that indicates what value is to be
14722          returned.  */
14723       cp_parser_identifier (parser);
14724       /* Issue an error message.  */
14725       error ("named return values are no longer supported");
14726       /* Skip tokens until we reach the start of the function body.  */
14727       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14728              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14729         cp_lexer_consume_token (parser->lexer);
14730     }
14731   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14732      anything declared inside `f'.  */
14733   saved_in_unbraced_linkage_specification_p
14734     = parser->in_unbraced_linkage_specification_p;
14735   parser->in_unbraced_linkage_specification_p = false;
14736   /* Inside the function, surrounding template-parameter-lists do not
14737      apply.  */
14738   saved_num_template_parameter_lists
14739     = parser->num_template_parameter_lists;
14740   parser->num_template_parameter_lists = 0;
14741   /* If the next token is `try', then we are looking at a
14742      function-try-block.  */
14743   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14744     ctor_initializer_p = cp_parser_function_try_block (parser);
14745   /* A function-try-block includes the function-body, so we only do
14746      this next part if we're not processing a function-try-block.  */
14747   else
14748     ctor_initializer_p
14749       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14750
14751   /* Finish the function.  */
14752   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14753                         (inline_p ? 2 : 0));
14754   /* Generate code for it, if necessary.  */
14755   expand_or_defer_fn (fn);
14756   /* Restore the saved values.  */
14757   parser->in_unbraced_linkage_specification_p
14758     = saved_in_unbraced_linkage_specification_p;
14759   parser->num_template_parameter_lists
14760     = saved_num_template_parameter_lists;
14761
14762   return fn;
14763 }
14764
14765 /* Parse a template-declaration, assuming that the `export' (and
14766    `extern') keywords, if present, has already been scanned.  MEMBER_P
14767    is as for cp_parser_template_declaration.  */
14768
14769 static void
14770 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14771 {
14772   tree decl = NULL_TREE;
14773   tree parameter_list;
14774   bool friend_p = false;
14775
14776   /* Look for the `template' keyword.  */
14777   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14778     return;
14779
14780   /* And the `<'.  */
14781   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14782     return;
14783
14784   /* If the next token is `>', then we have an invalid
14785      specialization.  Rather than complain about an invalid template
14786      parameter, issue an error message here.  */
14787   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14788     {
14789       cp_parser_error (parser, "invalid explicit specialization");
14790       begin_specialization ();
14791       parameter_list = NULL_TREE;
14792     }
14793   else
14794     {
14795       /* Parse the template parameters.  */
14796       begin_template_parm_list ();
14797       parameter_list = cp_parser_template_parameter_list (parser);
14798       parameter_list = end_template_parm_list (parameter_list);
14799     }
14800
14801   /* Look for the `>'.  */
14802   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14803   /* We just processed one more parameter list.  */
14804   ++parser->num_template_parameter_lists;
14805   /* If the next token is `template', there are more template
14806      parameters.  */
14807   if (cp_lexer_next_token_is_keyword (parser->lexer,
14808                                       RID_TEMPLATE))
14809     cp_parser_template_declaration_after_export (parser, member_p);
14810   else
14811     {
14812       /* There are no access checks when parsing a template, as we do not
14813          know if a specialization will be a friend.  */
14814       push_deferring_access_checks (dk_no_check);
14815
14816       decl = cp_parser_single_declaration (parser,
14817                                            member_p,
14818                                            &friend_p);
14819
14820       pop_deferring_access_checks ();
14821
14822       /* If this is a member template declaration, let the front
14823          end know.  */
14824       if (member_p && !friend_p && decl)
14825         {
14826           if (TREE_CODE (decl) == TYPE_DECL)
14827             cp_parser_check_access_in_redeclaration (decl);
14828
14829           decl = finish_member_template_decl (decl);
14830         }
14831       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14832         make_friend_class (current_class_type, TREE_TYPE (decl),
14833                            /*complain=*/true);
14834     }
14835   /* We are done with the current parameter list.  */
14836   --parser->num_template_parameter_lists;
14837
14838   /* Finish up.  */
14839   finish_template_decl (parameter_list);
14840
14841   /* Register member declarations.  */
14842   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14843     finish_member_declaration (decl);
14844
14845   /* If DECL is a function template, we must return to parse it later.
14846      (Even though there is no definition, there might be default
14847      arguments that need handling.)  */
14848   if (member_p && decl
14849       && (TREE_CODE (decl) == FUNCTION_DECL
14850           || DECL_FUNCTION_TEMPLATE_P (decl)))
14851     TREE_VALUE (parser->unparsed_functions_queues)
14852       = tree_cons (NULL_TREE, decl,
14853                    TREE_VALUE (parser->unparsed_functions_queues));
14854 }
14855
14856 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14857    `function-definition' sequence.  MEMBER_P is true, this declaration
14858    appears in a class scope.
14859
14860    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14861    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14862
14863 static tree
14864 cp_parser_single_declaration (cp_parser* parser,
14865                               bool member_p,
14866                               bool* friend_p)
14867 {
14868   int declares_class_or_enum;
14869   tree decl = NULL_TREE;
14870   cp_decl_specifier_seq decl_specifiers;
14871   bool function_definition_p = false;
14872
14873   /* This function is only used when processing a template
14874      declaration.  */
14875   gcc_assert (innermost_scope_kind () == sk_template_parms
14876               || innermost_scope_kind () == sk_template_spec);
14877
14878   /* Defer access checks until we know what is being declared.  */
14879   push_deferring_access_checks (dk_deferred);
14880
14881   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14882      alternative.  */
14883   cp_parser_decl_specifier_seq (parser,
14884                                 CP_PARSER_FLAGS_OPTIONAL,
14885                                 &decl_specifiers,
14886                                 &declares_class_or_enum);
14887   if (friend_p)
14888     *friend_p = cp_parser_friend_p (&decl_specifiers);
14889
14890   /* There are no template typedefs.  */
14891   if (decl_specifiers.specs[(int) ds_typedef])
14892     {
14893       error ("template declaration of %qs", "typedef");
14894       decl = error_mark_node;
14895     }
14896
14897   /* Gather up the access checks that occurred the
14898      decl-specifier-seq.  */
14899   stop_deferring_access_checks ();
14900
14901   /* Check for the declaration of a template class.  */
14902   if (declares_class_or_enum)
14903     {
14904       if (cp_parser_declares_only_class_p (parser))
14905         {
14906           decl = shadow_tag (&decl_specifiers);
14907
14908           /* In this case:
14909
14910                struct C {
14911                  friend template <typename T> struct A<T>::B;
14912                };
14913
14914              A<T>::B will be represented by a TYPENAME_TYPE, and
14915              therefore not recognized by shadow_tag.  */
14916           if (friend_p && *friend_p
14917               && !decl
14918               && decl_specifiers.type
14919               && TYPE_P (decl_specifiers.type))
14920             decl = decl_specifiers.type;
14921
14922           if (decl && decl != error_mark_node)
14923             decl = TYPE_NAME (decl);
14924           else
14925             decl = error_mark_node;
14926         }
14927     }
14928   /* If it's not a template class, try for a template function.  If
14929      the next token is a `;', then this declaration does not declare
14930      anything.  But, if there were errors in the decl-specifiers, then
14931      the error might well have come from an attempted class-specifier.
14932      In that case, there's no need to warn about a missing declarator.  */
14933   if (!decl
14934       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14935           || decl_specifiers.type != error_mark_node))
14936     decl = cp_parser_init_declarator (parser,
14937                                       &decl_specifiers,
14938                                       /*function_definition_allowed_p=*/true,
14939                                       member_p,
14940                                       declares_class_or_enum,
14941                                       &function_definition_p);
14942
14943   pop_deferring_access_checks ();
14944
14945   /* Clear any current qualification; whatever comes next is the start
14946      of something new.  */
14947   parser->scope = NULL_TREE;
14948   parser->qualifying_scope = NULL_TREE;
14949   parser->object_scope = NULL_TREE;
14950   /* Look for a trailing `;' after the declaration.  */
14951   if (!function_definition_p
14952       && (decl == error_mark_node
14953           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14954     cp_parser_skip_to_end_of_block_or_statement (parser);
14955
14956   return decl;
14957 }
14958
14959 /* Parse a cast-expression that is not the operand of a unary "&".  */
14960
14961 static tree
14962 cp_parser_simple_cast_expression (cp_parser *parser)
14963 {
14964   return cp_parser_cast_expression (parser, /*address_p=*/false);
14965 }
14966
14967 /* Parse a functional cast to TYPE.  Returns an expression
14968    representing the cast.  */
14969
14970 static tree
14971 cp_parser_functional_cast (cp_parser* parser, tree type)
14972 {
14973   tree expression_list;
14974   tree cast;
14975
14976   expression_list
14977     = cp_parser_parenthesized_expression_list (parser, false,
14978                                                /*non_constant_p=*/NULL);
14979
14980   cast = build_functional_cast (type, expression_list);
14981   /* [expr.const]/1: In an integral constant expression "only type
14982      conversions to integral or enumeration type can be used".  */
14983   if (cast != error_mark_node && !type_dependent_expression_p (type)
14984       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14985     {
14986       if (cp_parser_non_integral_constant_expression
14987           (parser, "a call to a constructor"))
14988         return error_mark_node;
14989     }
14990   return cast;
14991 }
14992
14993 /* Save the tokens that make up the body of a member function defined
14994    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14995    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14996    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14997    for the member function.  */
14998
14999 static tree
15000 cp_parser_save_member_function_body (cp_parser* parser,
15001                                      cp_decl_specifier_seq *decl_specifiers,
15002                                      cp_declarator *declarator,
15003                                      tree attributes)
15004 {
15005   cp_token *first;
15006   cp_token *last;
15007   tree fn;
15008
15009   /* Create the function-declaration.  */
15010   fn = start_method (decl_specifiers, declarator, attributes);
15011   /* If something went badly wrong, bail out now.  */
15012   if (fn == error_mark_node)
15013     {
15014       /* If there's a function-body, skip it.  */
15015       if (cp_parser_token_starts_function_definition_p
15016           (cp_lexer_peek_token (parser->lexer)))
15017         cp_parser_skip_to_end_of_block_or_statement (parser);
15018       return error_mark_node;
15019     }
15020
15021   /* Remember it, if there default args to post process.  */
15022   cp_parser_save_default_args (parser, fn);
15023
15024   /* Save away the tokens that make up the body of the
15025      function.  */
15026   first = parser->lexer->next_token;
15027   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15028   /* Handle function try blocks.  */
15029   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15030     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15031   last = parser->lexer->next_token;
15032
15033   /* Save away the inline definition; we will process it when the
15034      class is complete.  */
15035   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15036   DECL_PENDING_INLINE_P (fn) = 1;
15037
15038   /* We need to know that this was defined in the class, so that
15039      friend templates are handled correctly.  */
15040   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15041
15042   /* We're done with the inline definition.  */
15043   finish_method (fn);
15044
15045   /* Add FN to the queue of functions to be parsed later.  */
15046   TREE_VALUE (parser->unparsed_functions_queues)
15047     = tree_cons (NULL_TREE, fn,
15048                  TREE_VALUE (parser->unparsed_functions_queues));
15049
15050   return fn;
15051 }
15052
15053 /* Parse a template-argument-list, as well as the trailing ">" (but
15054    not the opening ">").  See cp_parser_template_argument_list for the
15055    return value.  */
15056
15057 static tree
15058 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15059 {
15060   tree arguments;
15061   tree saved_scope;
15062   tree saved_qualifying_scope;
15063   tree saved_object_scope;
15064   bool saved_greater_than_is_operator_p;
15065
15066   /* [temp.names]
15067
15068      When parsing a template-id, the first non-nested `>' is taken as
15069      the end of the template-argument-list rather than a greater-than
15070      operator.  */
15071   saved_greater_than_is_operator_p
15072     = parser->greater_than_is_operator_p;
15073   parser->greater_than_is_operator_p = false;
15074   /* Parsing the argument list may modify SCOPE, so we save it
15075      here.  */
15076   saved_scope = parser->scope;
15077   saved_qualifying_scope = parser->qualifying_scope;
15078   saved_object_scope = parser->object_scope;
15079   /* Parse the template-argument-list itself.  */
15080   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15081     arguments = NULL_TREE;
15082   else
15083     arguments = cp_parser_template_argument_list (parser);
15084   /* Look for the `>' that ends the template-argument-list. If we find
15085      a '>>' instead, it's probably just a typo.  */
15086   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15087     {
15088       if (!saved_greater_than_is_operator_p)
15089         {
15090           /* If we're in a nested template argument list, the '>>' has
15091             to be a typo for '> >'. We emit the error message, but we
15092             continue parsing and we push a '>' as next token, so that
15093             the argument list will be parsed correctly.  Note that the
15094             global source location is still on the token before the
15095             '>>', so we need to say explicitly where we want it.  */
15096           cp_token *token = cp_lexer_peek_token (parser->lexer);
15097           error ("%H%<>>%> should be %<> >%> "
15098                  "within a nested template argument list",
15099                  &token->location);
15100
15101           /* ??? Proper recovery should terminate two levels of
15102              template argument list here.  */
15103           token->type = CPP_GREATER;
15104         }
15105       else
15106         {
15107           /* If this is not a nested template argument list, the '>>'
15108             is a typo for '>'. Emit an error message and continue.
15109             Same deal about the token location, but here we can get it
15110             right by consuming the '>>' before issuing the diagnostic.  */
15111           cp_lexer_consume_token (parser->lexer);
15112           error ("spurious %<>>%>, use %<>%> to terminate "
15113                  "a template argument list");
15114         }
15115     }
15116   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15117     error ("missing %<>%> to terminate the template argument list");
15118   else
15119     /* It's what we want, a '>'; consume it.  */
15120     cp_lexer_consume_token (parser->lexer);
15121   /* The `>' token might be a greater-than operator again now.  */
15122   parser->greater_than_is_operator_p
15123     = saved_greater_than_is_operator_p;
15124   /* Restore the SAVED_SCOPE.  */
15125   parser->scope = saved_scope;
15126   parser->qualifying_scope = saved_qualifying_scope;
15127   parser->object_scope = saved_object_scope;
15128
15129   return arguments;
15130 }
15131
15132 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15133    arguments, or the body of the function have not yet been parsed,
15134    parse them now.  */
15135
15136 static void
15137 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15138 {
15139   /* If this member is a template, get the underlying
15140      FUNCTION_DECL.  */
15141   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15142     member_function = DECL_TEMPLATE_RESULT (member_function);
15143
15144   /* There should not be any class definitions in progress at this
15145      point; the bodies of members are only parsed outside of all class
15146      definitions.  */
15147   gcc_assert (parser->num_classes_being_defined == 0);
15148   /* While we're parsing the member functions we might encounter more
15149      classes.  We want to handle them right away, but we don't want
15150      them getting mixed up with functions that are currently in the
15151      queue.  */
15152   parser->unparsed_functions_queues
15153     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15154
15155   /* Make sure that any template parameters are in scope.  */
15156   maybe_begin_member_template_processing (member_function);
15157
15158   /* If the body of the function has not yet been parsed, parse it
15159      now.  */
15160   if (DECL_PENDING_INLINE_P (member_function))
15161     {
15162       tree function_scope;
15163       cp_token_cache *tokens;
15164
15165       /* The function is no longer pending; we are processing it.  */
15166       tokens = DECL_PENDING_INLINE_INFO (member_function);
15167       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15168       DECL_PENDING_INLINE_P (member_function) = 0;
15169       /* If this was an inline function in a local class, enter the scope
15170          of the containing function.  */
15171       function_scope = decl_function_context (member_function);
15172       if (function_scope)
15173         push_function_context_to (function_scope);
15174
15175       /* Push the body of the function onto the lexer stack.  */
15176       cp_parser_push_lexer_for_tokens (parser, tokens);
15177
15178       /* Let the front end know that we going to be defining this
15179          function.  */
15180       start_preparsed_function (member_function, NULL_TREE,
15181                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15182
15183       /* Now, parse the body of the function.  */
15184       cp_parser_function_definition_after_declarator (parser,
15185                                                       /*inline_p=*/true);
15186
15187       /* Leave the scope of the containing function.  */
15188       if (function_scope)
15189         pop_function_context_from (function_scope);
15190       cp_parser_pop_lexer (parser);
15191     }
15192
15193   /* Remove any template parameters from the symbol table.  */
15194   maybe_end_member_template_processing ();
15195
15196   /* Restore the queue.  */
15197   parser->unparsed_functions_queues
15198     = TREE_CHAIN (parser->unparsed_functions_queues);
15199 }
15200
15201 /* If DECL contains any default args, remember it on the unparsed
15202    functions queue.  */
15203
15204 static void
15205 cp_parser_save_default_args (cp_parser* parser, tree decl)
15206 {
15207   tree probe;
15208
15209   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15210        probe;
15211        probe = TREE_CHAIN (probe))
15212     if (TREE_PURPOSE (probe))
15213       {
15214         TREE_PURPOSE (parser->unparsed_functions_queues)
15215           = tree_cons (current_class_type, decl,
15216                        TREE_PURPOSE (parser->unparsed_functions_queues));
15217         break;
15218       }
15219   return;
15220 }
15221
15222 /* FN is a FUNCTION_DECL which may contains a parameter with an
15223    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15224    assumes that the current scope is the scope in which the default
15225    argument should be processed.  */
15226
15227 static void
15228 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15229 {
15230   bool saved_local_variables_forbidden_p;
15231   tree parm;
15232
15233   /* While we're parsing the default args, we might (due to the
15234      statement expression extension) encounter more classes.  We want
15235      to handle them right away, but we don't want them getting mixed
15236      up with default args that are currently in the queue.  */
15237   parser->unparsed_functions_queues
15238     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15239
15240   /* Local variable names (and the `this' keyword) may not appear
15241      in a default argument.  */
15242   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15243   parser->local_variables_forbidden_p = true;
15244
15245   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15246        parm;
15247        parm = TREE_CHAIN (parm))
15248     {
15249       cp_token_cache *tokens;
15250
15251       if (!TREE_PURPOSE (parm)
15252           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15253         continue;
15254
15255        /* Push the saved tokens for the default argument onto the parser's
15256           lexer stack.  */
15257       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15258       cp_parser_push_lexer_for_tokens (parser, tokens);
15259
15260       /* Parse the assignment-expression.  */
15261       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15262
15263       /* If the token stream has not been completely used up, then
15264          there was extra junk after the end of the default
15265          argument.  */
15266       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15267         cp_parser_error (parser, "expected %<,%>");
15268
15269       /* Revert to the main lexer.  */
15270       cp_parser_pop_lexer (parser);
15271     }
15272
15273   /* Restore the state of local_variables_forbidden_p.  */
15274   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15275
15276   /* Restore the queue.  */
15277   parser->unparsed_functions_queues
15278     = TREE_CHAIN (parser->unparsed_functions_queues);
15279 }
15280
15281 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15282    either a TYPE or an expression, depending on the form of the
15283    input.  The KEYWORD indicates which kind of expression we have
15284    encountered.  */
15285
15286 static tree
15287 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15288 {
15289   static const char *format;
15290   tree expr = NULL_TREE;
15291   const char *saved_message;
15292   bool saved_integral_constant_expression_p;
15293
15294   /* Initialize FORMAT the first time we get here.  */
15295   if (!format)
15296     format = "types may not be defined in '%s' expressions";
15297
15298   /* Types cannot be defined in a `sizeof' expression.  Save away the
15299      old message.  */
15300   saved_message = parser->type_definition_forbidden_message;
15301   /* And create the new one.  */
15302   parser->type_definition_forbidden_message
15303     = xmalloc (strlen (format)
15304                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15305                + 1 /* `\0' */);
15306   sprintf ((char *) parser->type_definition_forbidden_message,
15307            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15308
15309   /* The restrictions on constant-expressions do not apply inside
15310      sizeof expressions.  */
15311   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15312   parser->integral_constant_expression_p = false;
15313
15314   /* Do not actually evaluate the expression.  */
15315   ++skip_evaluation;
15316   /* If it's a `(', then we might be looking at the type-id
15317      construction.  */
15318   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15319     {
15320       tree type;
15321       bool saved_in_type_id_in_expr_p;
15322
15323       /* We can't be sure yet whether we're looking at a type-id or an
15324          expression.  */
15325       cp_parser_parse_tentatively (parser);
15326       /* Consume the `('.  */
15327       cp_lexer_consume_token (parser->lexer);
15328       /* Parse the type-id.  */
15329       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15330       parser->in_type_id_in_expr_p = true;
15331       type = cp_parser_type_id (parser);
15332       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15333       /* Now, look for the trailing `)'.  */
15334       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15335       /* If all went well, then we're done.  */
15336       if (cp_parser_parse_definitely (parser))
15337         {
15338           cp_decl_specifier_seq decl_specs;
15339
15340           /* Build a trivial decl-specifier-seq.  */
15341           clear_decl_specs (&decl_specs);
15342           decl_specs.type = type;
15343
15344           /* Call grokdeclarator to figure out what type this is.  */
15345           expr = grokdeclarator (NULL,
15346                                  &decl_specs,
15347                                  TYPENAME,
15348                                  /*initialized=*/0,
15349                                  /*attrlist=*/NULL);
15350         }
15351     }
15352
15353   /* If the type-id production did not work out, then we must be
15354      looking at the unary-expression production.  */
15355   if (!expr)
15356     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15357   /* Go back to evaluating expressions.  */
15358   --skip_evaluation;
15359
15360   /* Free the message we created.  */
15361   free ((char *) parser->type_definition_forbidden_message);
15362   /* And restore the old one.  */
15363   parser->type_definition_forbidden_message = saved_message;
15364   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15365
15366   return expr;
15367 }
15368
15369 /* If the current declaration has no declarator, return true.  */
15370
15371 static bool
15372 cp_parser_declares_only_class_p (cp_parser *parser)
15373 {
15374   /* If the next token is a `;' or a `,' then there is no
15375      declarator.  */
15376   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15377           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15378 }
15379
15380 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15381
15382 static void
15383 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15384                              cp_storage_class storage_class)
15385 {
15386   if (decl_specs->storage_class != sc_none)
15387     decl_specs->multiple_storage_classes_p = true;
15388   else
15389     decl_specs->storage_class = storage_class;
15390 }
15391
15392 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15393    is true, the type is a user-defined type; otherwise it is a
15394    built-in type specified by a keyword.  */
15395
15396 static void
15397 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15398                               tree type_spec,
15399                               bool user_defined_p)
15400 {
15401   decl_specs->any_specifiers_p = true;
15402
15403   /* If the user tries to redeclare bool or wchar_t (with, for
15404      example, in "typedef int wchar_t;") we remember that this is what
15405      happened.  In system headers, we ignore these declarations so
15406      that G++ can work with system headers that are not C++-safe.  */
15407   if (decl_specs->specs[(int) ds_typedef]
15408       && !user_defined_p
15409       && (type_spec == boolean_type_node
15410           || type_spec == wchar_type_node)
15411       && (decl_specs->type
15412           || decl_specs->specs[(int) ds_long]
15413           || decl_specs->specs[(int) ds_short]
15414           || decl_specs->specs[(int) ds_unsigned]
15415           || decl_specs->specs[(int) ds_signed]))
15416     {
15417       decl_specs->redefined_builtin_type = type_spec;
15418       if (!decl_specs->type)
15419         {
15420           decl_specs->type = type_spec;
15421           decl_specs->user_defined_type_p = false;
15422         }
15423     }
15424   else if (decl_specs->type)
15425     decl_specs->multiple_types_p = true;
15426   else
15427     {
15428       decl_specs->type = type_spec;
15429       decl_specs->user_defined_type_p = user_defined_p;
15430       decl_specs->redefined_builtin_type = NULL_TREE;
15431     }
15432 }
15433
15434 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15435    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15436
15437 static bool
15438 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15439 {
15440   return decl_specifiers->specs[(int) ds_friend] != 0;
15441 }
15442
15443 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15444    issue an error message indicating that TOKEN_DESC was expected.
15445
15446    Returns the token consumed, if the token had the appropriate type.
15447    Otherwise, returns NULL.  */
15448
15449 static cp_token *
15450 cp_parser_require (cp_parser* parser,
15451                    enum cpp_ttype type,
15452                    const char* token_desc)
15453 {
15454   if (cp_lexer_next_token_is (parser->lexer, type))
15455     return cp_lexer_consume_token (parser->lexer);
15456   else
15457     {
15458       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15459       if (!cp_parser_simulate_error (parser))
15460         {
15461           char *message = concat ("expected ", token_desc, NULL);
15462           cp_parser_error (parser, message);
15463           free (message);
15464         }
15465       return NULL;
15466     }
15467 }
15468
15469 /* Like cp_parser_require, except that tokens will be skipped until
15470    the desired token is found.  An error message is still produced if
15471    the next token is not as expected.  */
15472
15473 static void
15474 cp_parser_skip_until_found (cp_parser* parser,
15475                             enum cpp_ttype type,
15476                             const char* token_desc)
15477 {
15478   cp_token *token;
15479   unsigned nesting_depth = 0;
15480
15481   if (cp_parser_require (parser, type, token_desc))
15482     return;
15483
15484   /* Skip tokens until the desired token is found.  */
15485   while (true)
15486     {
15487       /* Peek at the next token.  */
15488       token = cp_lexer_peek_token (parser->lexer);
15489       /* If we've reached the token we want, consume it and
15490          stop.  */
15491       if (token->type == type && !nesting_depth)
15492         {
15493           cp_lexer_consume_token (parser->lexer);
15494           return;
15495         }
15496       /* If we've run out of tokens, stop.  */
15497       if (token->type == CPP_EOF)
15498         return;
15499       if (token->type == CPP_OPEN_BRACE
15500           || token->type == CPP_OPEN_PAREN
15501           || token->type == CPP_OPEN_SQUARE)
15502         ++nesting_depth;
15503       else if (token->type == CPP_CLOSE_BRACE
15504                || token->type == CPP_CLOSE_PAREN
15505                || token->type == CPP_CLOSE_SQUARE)
15506         {
15507           if (nesting_depth-- == 0)
15508             return;
15509         }
15510       /* Consume this token.  */
15511       cp_lexer_consume_token (parser->lexer);
15512     }
15513 }
15514
15515 /* If the next token is the indicated keyword, consume it.  Otherwise,
15516    issue an error message indicating that TOKEN_DESC was expected.
15517
15518    Returns the token consumed, if the token had the appropriate type.
15519    Otherwise, returns NULL.  */
15520
15521 static cp_token *
15522 cp_parser_require_keyword (cp_parser* parser,
15523                            enum rid keyword,
15524                            const char* token_desc)
15525 {
15526   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15527
15528   if (token && token->keyword != keyword)
15529     {
15530       dyn_string_t error_msg;
15531
15532       /* Format the error message.  */
15533       error_msg = dyn_string_new (0);
15534       dyn_string_append_cstr (error_msg, "expected ");
15535       dyn_string_append_cstr (error_msg, token_desc);
15536       cp_parser_error (parser, error_msg->s);
15537       dyn_string_delete (error_msg);
15538       return NULL;
15539     }
15540
15541   return token;
15542 }
15543
15544 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15545    function-definition.  */
15546
15547 static bool
15548 cp_parser_token_starts_function_definition_p (cp_token* token)
15549 {
15550   return (/* An ordinary function-body begins with an `{'.  */
15551           token->type == CPP_OPEN_BRACE
15552           /* A ctor-initializer begins with a `:'.  */
15553           || token->type == CPP_COLON
15554           /* A function-try-block begins with `try'.  */
15555           || token->keyword == RID_TRY
15556           /* The named return value extension begins with `return'.  */
15557           || token->keyword == RID_RETURN);
15558 }
15559
15560 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15561    definition.  */
15562
15563 static bool
15564 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15565 {
15566   cp_token *token;
15567
15568   token = cp_lexer_peek_token (parser->lexer);
15569   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15570 }
15571
15572 /* Returns TRUE iff the next token is the "," or ">" ending a
15573    template-argument.  */
15574
15575 static bool
15576 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15577 {
15578   cp_token *token;
15579
15580   token = cp_lexer_peek_token (parser->lexer);
15581   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15582 }
15583
15584 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15585    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15586
15587 static bool
15588 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15589                                                      size_t n)
15590 {
15591   cp_token *token;
15592
15593   token = cp_lexer_peek_nth_token (parser->lexer, n);
15594   if (token->type == CPP_LESS)
15595     return true;
15596   /* Check for the sequence `<::' in the original code. It would be lexed as
15597      `[:', where `[' is a digraph, and there is no whitespace before
15598      `:'.  */
15599   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15600     {
15601       cp_token *token2;
15602       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15603       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15604         return true;
15605     }
15606   return false;
15607 }
15608
15609 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15610    or none_type otherwise.  */
15611
15612 static enum tag_types
15613 cp_parser_token_is_class_key (cp_token* token)
15614 {
15615   switch (token->keyword)
15616     {
15617     case RID_CLASS:
15618       return class_type;
15619     case RID_STRUCT:
15620       return record_type;
15621     case RID_UNION:
15622       return union_type;
15623
15624     default:
15625       return none_type;
15626     }
15627 }
15628
15629 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15630
15631 static void
15632 cp_parser_check_class_key (enum tag_types class_key, tree type)
15633 {
15634   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15635     pedwarn ("%qs tag used in naming %q#T",
15636             class_key == union_type ? "union"
15637              : class_key == record_type ? "struct" : "class",
15638              type);
15639 }
15640
15641 /* Issue an error message if DECL is redeclared with different
15642    access than its original declaration [class.access.spec/3].
15643    This applies to nested classes and nested class templates.
15644    [class.mem/1].  */
15645
15646 static void
15647 cp_parser_check_access_in_redeclaration (tree decl)
15648 {
15649   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15650     return;
15651
15652   if ((TREE_PRIVATE (decl)
15653        != (current_access_specifier == access_private_node))
15654       || (TREE_PROTECTED (decl)
15655           != (current_access_specifier == access_protected_node)))
15656     error ("%qD redeclared with different access", decl);
15657 }
15658
15659 /* Look for the `template' keyword, as a syntactic disambiguator.
15660    Return TRUE iff it is present, in which case it will be
15661    consumed.  */
15662
15663 static bool
15664 cp_parser_optional_template_keyword (cp_parser *parser)
15665 {
15666   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15667     {
15668       /* The `template' keyword can only be used within templates;
15669          outside templates the parser can always figure out what is a
15670          template and what is not.  */
15671       if (!processing_template_decl)
15672         {
15673           error ("%<template%> (as a disambiguator) is only allowed "
15674                  "within templates");
15675           /* If this part of the token stream is rescanned, the same
15676              error message would be generated.  So, we purge the token
15677              from the stream.  */
15678           cp_lexer_purge_token (parser->lexer);
15679           return false;
15680         }
15681       else
15682         {
15683           /* Consume the `template' keyword.  */
15684           cp_lexer_consume_token (parser->lexer);
15685           return true;
15686         }
15687     }
15688
15689   return false;
15690 }
15691
15692 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15693    set PARSER->SCOPE, and perform other related actions.  */
15694
15695 static void
15696 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15697 {
15698   tree value;
15699   tree check;
15700
15701   /* Get the stored value.  */
15702   value = cp_lexer_consume_token (parser->lexer)->value;
15703   /* Perform any access checks that were deferred.  */
15704   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15705     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15706   /* Set the scope from the stored value.  */
15707   parser->scope = TREE_VALUE (value);
15708   parser->qualifying_scope = TREE_TYPE (value);
15709   parser->object_scope = NULL_TREE;
15710 }
15711
15712 /* Consume tokens up through a non-nested END token.  */
15713
15714 static void
15715 cp_parser_cache_group (cp_parser *parser,
15716                        enum cpp_ttype end,
15717                        unsigned depth)
15718 {
15719   while (true)
15720     {
15721       cp_token *token;
15722
15723       /* Abort a parenthesized expression if we encounter a brace.  */
15724       if ((end == CPP_CLOSE_PAREN || depth == 0)
15725           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15726         return;
15727       /* If we've reached the end of the file, stop.  */
15728       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15729         return;
15730       /* Consume the next token.  */
15731       token = cp_lexer_consume_token (parser->lexer);
15732       /* See if it starts a new group.  */
15733       if (token->type == CPP_OPEN_BRACE)
15734         {
15735           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15736           if (depth == 0)
15737             return;
15738         }
15739       else if (token->type == CPP_OPEN_PAREN)
15740         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15741       else if (token->type == end)
15742         return;
15743     }
15744 }
15745
15746 /* Begin parsing tentatively.  We always save tokens while parsing
15747    tentatively so that if the tentative parsing fails we can restore the
15748    tokens.  */
15749
15750 static void
15751 cp_parser_parse_tentatively (cp_parser* parser)
15752 {
15753   /* Enter a new parsing context.  */
15754   parser->context = cp_parser_context_new (parser->context);
15755   /* Begin saving tokens.  */
15756   cp_lexer_save_tokens (parser->lexer);
15757   /* In order to avoid repetitive access control error messages,
15758      access checks are queued up until we are no longer parsing
15759      tentatively.  */
15760   push_deferring_access_checks (dk_deferred);
15761 }
15762
15763 /* Commit to the currently active tentative parse.  */
15764
15765 static void
15766 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15767 {
15768   cp_parser_context *context;
15769   cp_lexer *lexer;
15770
15771   /* Mark all of the levels as committed.  */
15772   lexer = parser->lexer;
15773   for (context = parser->context; context->next; context = context->next)
15774     {
15775       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15776         break;
15777       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15778       while (!cp_lexer_saving_tokens (lexer))
15779         lexer = lexer->next;
15780       cp_lexer_commit_tokens (lexer);
15781     }
15782 }
15783
15784 /* Abort the currently active tentative parse.  All consumed tokens
15785    will be rolled back, and no diagnostics will be issued.  */
15786
15787 static void
15788 cp_parser_abort_tentative_parse (cp_parser* parser)
15789 {
15790   cp_parser_simulate_error (parser);
15791   /* Now, pretend that we want to see if the construct was
15792      successfully parsed.  */
15793   cp_parser_parse_definitely (parser);
15794 }
15795
15796 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15797    token stream.  Otherwise, commit to the tokens we have consumed.
15798    Returns true if no error occurred; false otherwise.  */
15799
15800 static bool
15801 cp_parser_parse_definitely (cp_parser* parser)
15802 {
15803   bool error_occurred;
15804   cp_parser_context *context;
15805
15806   /* Remember whether or not an error occurred, since we are about to
15807      destroy that information.  */
15808   error_occurred = cp_parser_error_occurred (parser);
15809   /* Remove the topmost context from the stack.  */
15810   context = parser->context;
15811   parser->context = context->next;
15812   /* If no parse errors occurred, commit to the tentative parse.  */
15813   if (!error_occurred)
15814     {
15815       /* Commit to the tokens read tentatively, unless that was
15816          already done.  */
15817       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15818         cp_lexer_commit_tokens (parser->lexer);
15819
15820       pop_to_parent_deferring_access_checks ();
15821     }
15822   /* Otherwise, if errors occurred, roll back our state so that things
15823      are just as they were before we began the tentative parse.  */
15824   else
15825     {
15826       cp_lexer_rollback_tokens (parser->lexer);
15827       pop_deferring_access_checks ();
15828     }
15829   /* Add the context to the front of the free list.  */
15830   context->next = cp_parser_context_free_list;
15831   cp_parser_context_free_list = context;
15832
15833   return !error_occurred;
15834 }
15835
15836 /* Returns true if we are parsing tentatively and are not committed to
15837    this tentative parse.  */
15838
15839 static bool
15840 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
15841 {
15842   return (cp_parser_parsing_tentatively (parser)
15843           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
15844 }
15845
15846 /* Returns nonzero iff an error has occurred during the most recent
15847    tentative parse.  */
15848
15849 static bool
15850 cp_parser_error_occurred (cp_parser* parser)
15851 {
15852   return (cp_parser_parsing_tentatively (parser)
15853           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15854 }
15855
15856 /* Returns nonzero if GNU extensions are allowed.  */
15857
15858 static bool
15859 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15860 {
15861   return parser->allow_gnu_extensions_p;
15862 }
15863
15864 \f
15865 /* The parser.  */
15866
15867 static GTY (()) cp_parser *the_parser;
15868
15869 /* External interface.  */
15870
15871 /* Parse one entire translation unit.  */
15872
15873 void
15874 c_parse_file (void)
15875 {
15876   bool error_occurred;
15877   static bool already_called = false;
15878
15879   if (already_called)
15880     {
15881       sorry ("inter-module optimizations not implemented for C++");
15882       return;
15883     }
15884   already_called = true;
15885
15886   the_parser = cp_parser_new ();
15887   push_deferring_access_checks (flag_access_control
15888                                 ? dk_no_deferred : dk_no_check);
15889   error_occurred = cp_parser_translation_unit (the_parser);
15890   the_parser = NULL;
15891 }
15892
15893 /* This variable must be provided by every front end.  */
15894
15895 int yydebug;
15896
15897 #include "gt-cp-parser.h"